Commit Graph

5239 Commits

Author SHA1 Message Date
Andreas Kling
32a150f2b4 Kernel: Make Thread::state_string() return StringView 2021-08-06 00:37:47 +02:00
Andreas Kling
af46f2214c Kernel: Make a bunch of "char const* to_string()" return StringView 2021-08-06 00:37:47 +02:00
Andreas Kling
f35108fc31 Kernel: Simplify PageDirectory allocation failure
This patch gets rid of the "valid" bit in PageDirectory since it was
only used to communicate an allocation failure during construction.

We now do all the work in the static factory functions instead of in the
constructor, which allows us to simply return nullptr instead of an
"invalid" PageDirectory.
2021-08-06 00:37:47 +02:00
Andreas Kling
27100126c0 Kernel: Fix logic typo in AnonymousVMObject::handle_cow_fault()
Introduced in dd58d0f650
2021-08-06 00:37:47 +02:00
Idan Horowitz
3e909c0c49 Kernel: Remove double-counting of allocated pages in AnonymousVMObject
When constructing an AnonymousVMObject with the AllocateNow allocation
strategy we accidentally allocated the committed pages directly through
MemoryManager instead of taking them from our m_unused_physical_pages
CommittedPhysicalPageSet, which meant they were counted as allocated in
MemoryManager, but were still counted as unallocated in the PageSet,
who would then try to uncommit them on destruction, resulting in a
failed assertion.

To help prevent similar issues in the future a Badge<T> was added to
MM::allocate_committed_user_physical_page to prevent allocation of
commited pages not via a CommittedPhysicalPageSet.
2021-08-05 20:26:47 +02:00
Andreas Kling
dd58d0f650 Kernel: Uncommit a shared COW page when discovering it was unshared
When we hit a COW fault and discover than no other process is sharing
the physical page, we simply remap it r/w and save ourselves the
trouble. When this happens, we can also give back (uncommit) one of our
shared committed COW pages, since we won't be needing it.

We had this optimization before, but I mistakenly removed it in
50472fd69f since I had misunderstood
it to be the reason for a panic.
2021-08-05 17:41:58 +02:00
Andreas Kling
843d0d0d15 Kernel: Detach AnonymousVMObject from shared COW pages set once emptied
We currently overcommit for COW when forking a process and cloning its
memory regions. Both the parent and child process share a set of.
committed COW pages.

If there's COW sharing across more than two processeses within a lineage
(e.g parent, child & grandchild), it's possible to exhaust these pages.
When the shared set is emptied, the next COW fault in each process must
detach from the shared set and fall back to on demand allocation.

This patch makes sure that we detach from the shared set once we
discover it to be empty (during COW fault handling). This fixes an issue
where we'd try to allocate from an exhausted shared set while building
GNU binutils inside SerenityOS.
2021-08-05 17:41:58 +02:00
Andreas Kling
89a9ae7d0c Kernel: Handle AnonymousVMObject allocation failure when forking
Thanks to all the RAII, AnonymousVMObject::try_clone() can now
gracefully handle allocation failure.
2021-08-05 17:41:58 +02:00
Andreas Kling
0672163840 Kernel: Simplify AnonymousVMObject copy constructor
It was doing a bunch of things it didn't need to do. I think we had
misunderstood the base class as having copied m_lock in its copy
constructor but it's actually default initialized.
2021-08-05 17:41:58 +02:00
Andreas Kling
fa627c1eb2 Kernel: Use RAII to manage committed physical pages
We had issues with committed physical pages getting miscounted in some
situations, and instead of figuring out what was going wrong and making
sure all the commits had matching uncommits, this patch makes the
problem go away by adding an RAII class to manage this instead. :^)

MemoryManager::commit_user_physical_pages() now returns an (optional)
CommittedPhysicalPageSet. You can then allocate pages from the page set
by calling take_one() on it. Any unallocated pages are uncommitted upon
destruction of the page set.
2021-08-05 17:41:58 +02:00
Andreas Kling
ec49213f7b Kernel: Add MemoryManager to Forward.h 2021-08-05 17:41:58 +02:00
Idan Horowitz
ffee3d6c5d Kernel: Remove the always 1-sized super physical regions Vector
Since we only ever add 1 super physical region, theres no reason to
add the extra redirection and allocation that comes with a dynamically
sized Vector.
2021-08-04 21:02:40 +02:00
Luke
cbbbc38f27 Kernel: Print panic backtrace to both the screen and serial
Previously it would only print the backtrace to serial, which would be
inaccessible if you don't have serial setup.
2021-08-04 20:14:54 +02:00
Liav A
7c617394a1 Kernel: Ensure we read valid values from the RTC CMOS registers
We try to read twice from the RTC CMOS registers, and if the values are
not the same for 5 attempts, we know there's a malfunction with the
hardware so we declare these values as bogus in the kernel log.
2021-08-04 19:53:04 +02:00
Liav A
517460d3a9 Kernel: Ensure we don't get in an endless loop while querying the CMOS
When we try to query the time from the RTC CMOS, we try to check if
the CMOS is updated. If it is updated for a long period of time (as a
result of hardware malfunction), break the loop and return Unix epoch
time.
2021-08-04 19:53:04 +02:00
sin-ack
bed51d856a AK+Kernel: Print TODO when a TODO() is executed
Previously we would just print "ASSERTION FAILED: false", which was
kinda cryptic and also didn't make it clear whether this was a TODO or
an unreachable condition. Now, we actually print "ASSERTION FAILED:
TODO", making it crystal clear.
2021-08-04 11:01:16 +02:00
Brian Gianforcaro
2caafacd9b Kernel: Remove OOM unsafe API KBuffer::create_with_size 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
720a686a76 Kernel: Handle OOM when allocating Packet KBuffers 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
8c4785bd10 Kernel: Use normal initialization for TCPPacket instead of memset 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
c1a0e379e6 Kernel: Handle OOM when allocating IPv4Socket optional scratch buffer 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
a6db2f985a Kernel: Handle OOM in DiskCache when mounting Ext2 filesystems
Create the disk cache up front, so we can verify it succeeds.
Make the KBuffer allocation fail-able, so we can properly handle
failure when the user asks up to mount a Ext2 filesystem under
OOM conditions.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
187c086270 Kernel: Handle OOM from KBuffer creation in sys$module() 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
cbb263e350 Kernel: Remove OOM unsafe DoubleBuffer constructor
Remove this dangerous and now unused constructor.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
ca94a83337 Kernel: Handle OOM from DoubleBuffer usage in IPv4Socket
The IPv4Socket requires a DoubleBuffer for storage of any data it
received on the socket. However it was previously using the default
constructor which can not observe allocation failure. Address this by
plumbing the receive buffer through the various derived classes.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
109c885585 Kernel: Handle OOM from DoubleBuffer usage in Net/LocalSocket
LocalSockets keep a DoubleBuffer for both client and server usage.
This change converts the usage from using the default constructor
which is unable to observe OOM, to the new try_create factory and
plumb the result through the constructor.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
8d3b819daf Kernel: Handle OOM from DoubleBuffer creation in FIFO creation 2021-08-03 18:54:23 +02:00
Brian Gianforcaro
15cd5d324c Kernel: Handle OOM from KBuffer usage in Ext2FS::get_bitmap_block()
Fixes up error handling on an OOM-able path, and removes one more usage
of KBuffer::create_with_size.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
43f930d3aa Kernel: Convert MasterPTY creation to use DoubleBuffer factory
In order to remove the public DoubleBuffer constructor, we need to
convert the callers to the factory instead, allowing the caller to
observe OOM.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
f816abcbad Kernel: Add DoubleBuffer::try_create() factory method for OOM hardening
We need to expose the ability for DoubleBuffer creation to expose
failure, as DoubleBuffer depends on KBuffer, which also has to be able
to expose failure during OOM.

We will remove the non OOM API once all users have been converted.
2021-08-03 18:54:23 +02:00
Brian Gianforcaro
fc91eb365d Kernel: Do not cancel stale timers when servicing sys$alarm
The sys$alarm() syscall has logic to cache a m_alarm_timer to avoid
allocating a new timer for every call to alarm. Unfortunately that
logic was broken, and there were conditions in which we could have
a timer allocated, but it was no longer on the timer queue, and we
would attempt to cancel that timer again resulting in an infinite
loop waiting for the timers callback to fire.

To fix this, we need to track if a timer is currently in use or not,
allowing us to avoid attempting to cancel inactive timers.

Luke and Tom did the initial investigation, I just happened to have
time to write a repro and attempt a fix, so I'm adding them as the
as co-authors of this commit.

Co-authored-by: Luke <luke.wilde@live.co.uk>
Co-authored-by: Tom <tomut@yahoo.com>
2021-08-03 18:44:01 +02:00
Lenny Maiorani
97bd13264a Everywhere: Make use of container version of all_of
Problem:
- New `all_of` implementation takes the entire container so the user
  does not need to pass explicit begin/end iterators. This is unused
  except is in tests.

Solution:
- Make use of the new and more user-friendly version where possible.
2021-08-03 10:46:43 +02:00
Andreas Kling
26d7261347 Kernel: Make NullDevice (/dev/null) seekable 2021-08-03 10:36:48 +02:00
Andreas Kling
821a6e8b4c Kernel: Remap regions after changing purgeable VM object volatile flag
Otherwise we could end up with stale page table mappings that give us
erroneous Protection Violation faults when accessing valid addresses.
2021-08-03 10:23:36 +02:00
Thomas Wagenveld
1f078827c4 Kernel: Set initial link up status for RTL8139
On startup the link was assumed to be down, making the adapter not work
until the link up status was cycled.
2021-08-03 10:20:51 +02:00
Thomas Wagenveld
32c8d35ef0 Kernel: Expose link speed and duplex through /proc/net/adapters
Add the fields 'link_speed' (integer or -1) for the speed and
'link_full_duplex' (true for full, false for half) to indicate
link duplex.
2021-08-03 10:20:51 +02:00
Thomas Wagenveld
59fdeec7f5 Kernel: Add interface to read link speed and duplex for NetworkAdapter
Read the appropriate registers for RTL8139, RTL8168 and E1000.
For NE2000 just assume 10mbit full duplex as there is no indicator
for it in the pure NE2000 spec. Mock values for loopback.
2021-08-03 10:20:51 +02:00
brapru
9c3e6f3f63 Kernel: Send RST/ACK if no socket is available
Previously there was no way for Serenity to send a packet without an
established socket connection, and there was no way to appropriately
respond to a SYN packet on a non-listening port. This patch will respond
to any non-established socket attempts with the appropraite RST/ACK,
letting the client know to close the connection.
2021-08-02 02:45:56 +02:00
brapru
63a15ed19d Kernel: Do not send delayed ack in response to RST/ACK
In accordance with RFC 793, if the receiver is in the SYN-SENT state
it should respond to a RST by aborting the connection and immediately
move to the CLOSED state.

Previously the system would ACK all RST/ACKs, and the remote peer would
just respond with more RST packets.
2021-08-02 02:45:56 +02:00
LuK1337
3dd40535c1 VirtualFileSystem: Don't let rename() overwrite non-empty directory
According to POSIX, rename shouldn't succeed if newpath is a non-empty
directory.
2021-08-02 01:04:34 +02:00
brapru
ea2abb3200 Kernel: Convert NetworkTask to east-const style 2021-08-02 00:32:55 +02:00
Brian Gianforcaro
0fc853f5ba Kernel: Remove ThreadTracer.h include from Process.h / Thread.h
This isn't needed for Process / Thread as they only reference it
by pointer and it's already part of Kernel/Forward.h. So just include
it where the implementation needs to call it.
2021-08-01 08:10:16 +02:00
Brian Gianforcaro
44e992429f Kernel: Only include AK/SourceLocation.h if LOCK_DEBUG is enabled
Don't pay the header inclusion cost if we aren't compiling with the
LOCK_DEBUG option enabled.
2021-08-01 08:10:16 +02:00
Brian Gianforcaro
14c674183b Kernel: Only include KCOVDevice.h if ENABLE_KERNEL_COVERAGE_COLLECTION
There's no reason to include this header if we aren't going to actually
use it.
2021-08-01 08:10:16 +02:00
Brian Gianforcaro
ed996fcced Kernel: Remove unused header includes 2021-08-01 08:10:16 +02:00
Andreas Kling
363a901603 Kernel: Copy the "purgeable" flag when cloning AnonymousVMObject 2021-07-31 11:22:53 +02:00
Brian Gianforcaro
7fce0693a5 Prekernel: Disable KASAN, so it has no effect when enabled
I was working on some more KASAN changes and realized the system
no longer links when passing -DENABLE_KERNEL_ADDRESS_SANITIZER=ON.

Prekernel will likely never have KASAN support given it's limited
environment, so just suppress it's usage.
2021-07-30 16:58:09 +02:00
Andreas Kling
25b76462bf Revert "Kernel: Share committed COW pages between whole VMObject lineage"
This reverts commit 2c0df5e7e7.

This caused OOM on CI, so let's roll it back until we can figure it out.
2021-07-30 13:50:24 +02:00
Andreas Kling
2c0df5e7e7 Kernel: Share committed COW pages between whole VMObject lineage
When cloning an AnonymousVMObject, committed COW pages are shared
between the parent and child object. Whicever object COW's first will
take the shared committed page, and if the other object ends up doing
a COW as well, it will notice that the page is no longer shared by
two objects and simple remap it as read/write.

When a child is COW'ed again, while still having shared committed
pages with its own parent, the grandchild object will now join in the
sharing pool with its parent and grandparent. This means that the first
2 of 3 objects that COW will draw from the shared committed pages, and
3rd will remap read/write.

Previously, we would "fork" the shared committed pages when cloning,
which could lead to a situation where the grandparent held on to 1 of
the 3 needed shared committed pages. If both the child and grandchild
COW'ed, they wouldn't have enough pages, and since the grandparent
maintained an extra +1 ref count on the page, it wasn't possible to
to remap read/write.
2021-07-30 13:17:55 +02:00
Andreas Kling
bccdc08487 Kernel: Unmapping a non-mapped region with munmap() should be a no-op
Not a regression per se from 0fcb9efd86
since we were crashing before that which is obviously worse.
2021-07-30 13:16:55 +02:00
Brian Gianforcaro
0fcb9efd86 Kernel: Return an error when unmap finds no intersecting region
We currently always crash if a user attempts to unmap a range that
does not intersect with an existing region, no matter the size. This
happens because we will never explicitly check to see if the search
for intersecting regions found anything, instead loop over the results,
which might be an empty vector. We then attempt to deallocate the
requested range from the `RangeAllocator` unconditionally, which will
be invalid if the specified range is not managed by the RangeAllocator.
We will assert validating m_total_range.contains(..) the range we are
requesting to deallocate.

This fix to this is straight forward, error out if we weren't able to
find any intersections.

You can get stress-ng to attempt this pattern with the following
arguments, which will attempt to unmap 0x0 through some large offset:

```
stress-ng --vm-segv 1
```

Fixes: #8483

Co-authored-by: Federico Guerinoni <guerinoni.federico@gmail.com>
2021-07-30 11:28:55 +02:00