Commit graph

7182 commits

Author SHA1 Message Date
bunnie
641b6dbeb3 remove unused configurable OOM thresholds in the kernel
turns out that

- hard OOM has a clearly defined threshold: 0. we shouldn't
make it higher, because then it forces us to count free memory
on every allocation.

- it's better to have the tuning in userspace (I think) for
OOM, because that's where all the algorithms are
2024-05-21 03:46:54 +08:00
bunnie
a57945e2c0 add helper to extract vaddr prefixes (used by swapper to fliter candidates) 2024-05-21 03:46:29 +08:00
bunnie
751042e202 quite down debug in swapper 2024-05-21 02:56:27 +08:00
bunnie
cf6c084696 quite down debug in kernel 2024-05-21 02:56:08 +08:00
bunnie
b7becf27e7 quiet down warnings when debug-print is off
because the print! macro becomes a dummy when debug printing is
off, we get a bunch of warnings about unused variables because
the print! arguments aren't used. Add _ before variable names
to quiet these warnings.
2024-05-21 01:52:30 +08:00
bunnie
353f4d5f1b fix config flag 2024-05-21 01:46:02 +08:00
bunnie
b781cb9e1f modify the swapper tester to hard-OOM
allocate more memory than we can possibly process in a single
vector, to force the swapper to cycle through pretty much every
page of memory in order to service the computation.
2024-05-21 01:42:57 +08:00
bunnie
2ea6c83bf0 implement the userspace side of hard-oom and oom-doom handlers
see the comments added in the main.rs file for a discussion of all
the things.
2024-05-21 01:42:19 +08:00
bunnie
8d7065d741 implement hard-OOM handler
A hard-OOM is what happens when the allocator really runs out
of memory. To review, there are two types of OOMs handled by
the swapper:

- "OOM Doom": impending OOM. This is detected by a userspace
process that can do more sophisticated processing and
algorithms to predict an impending OOM. The
OOM Doom handler is also interruptable, which allows other
progresses to make progress while memory is being cleared up,
hopefully allowing for a more graceful failure curve.

- "Hard OOM": Literally no more pages are available. This is
triggered when the page allocator can't map a single more page.
The hard-OOM handler turns off all interrupts, and all the OOM
process happens within the uninterruptable userspace context.
This effectively turns the OOM handler "inside out" compared to
the normal, interruptable userspace handler. In order to support
this, a set of calls that implement the kernel space pre-amble
and post-amble (i.e. StealPage and ReleaseMemory) are introduced.

This does introduce an extra edge case in the exit path of
the hard-OOM, namely, if the hard-OOM failed on a page that was
swapped. Here, after clearing memory, we have an empty page,
but no data; we have to again re-enter the userspace handler to
fill that page. The twist here is we have to remember to re-instate
interrupts on this edge case otherwise we lose pre-emption.
2024-05-21 01:13:13 +08:00
bunnie
a677824160 improve debug output on entry/exit from irqs 2024-05-21 01:11:58 +08:00
bunnie
b684420d28 pass the masked address to alloc_page
was accidentally passing the whole address before, which was
causing the LSB's that are assumed to be empty to muck up
some page table computations.

Also make sure old PTE entries are properly masked when recycling
flags.
2024-05-21 01:10:23 +08:00
bunnie
d7a8671e7e allow a process to identify itself
this makes debugging code a lot more ergonomic. I don't think
this impacts security...
2024-05-21 01:07:59 +08:00
bunnie
853f9d3cd3 add hooks to hard-oom handler in the alloc_page_oomable routine
also add some features for debugginng.
2024-05-21 01:07:13 +08:00
bunnie
efd65425b3 add syscalls for "inverse" OOM'er
These calls allow for a blocking context in a userspace handler
to evict memory, as would be needed in the case of a hard-OOM.
2024-05-21 01:03:42 +08:00
bunnie
75e938680d fix page table explanation to match reality better 2024-05-21 01:03:19 +08:00
bunnie
9743957466 add pipe to local terminal for capturing logfiles 2024-05-21 01:02:43 +08:00
bunnie
3c9278ed5b invoke the epoch renorm when the epoch timestamp overflows
for testing, the time stamp rollover threshold can be set super
low, but normally, it's one less than u32::MAX (u32::MAX is
reserved by the renorm algorithm as a sentinel).

Renorms should still be "fairly rare", but because the alloc counters
may be bumped every time a memory message is sent, it's conceivable
that it could happen within months to years of uptime (if this were
running on a GHz-class processor, maybe even as short as a few
days). Thus, we should handle this corner case.
2024-05-18 00:28:34 +08:00
bunnie
33d5ed540d cleanup some warnings in other targets 2024-05-18 00:28:11 +08:00
bunnie
8d318d3ed9 add epoch rollover core handler
This is put in the `mem.rs` file because it has to have access
to its private allocation table.

The renorm strategy used does an in-place reduction of epoch
values down to the minimum set that can uniquely represent every
value within the array. The approach only uses stack allocated
temporary storage, and the amount of stack required is chosen
to fit within the one-page goal of the kernel's overhead.

This does mean that the algorithm doesn't run as fast as
it possibly could, in fact, it has to do multiple passes
on the allocation table due to the limited storage for normalized
values.

However, the allocation table itself is probably going to be
limited to hundreds to low-thousands of values, and the renorm
routine happens once in a blue moon, so I think this is
acceptable, and preferable to the YOLO punt of setting all
epoch stamps to 0 and re-discovering the LRU ordering, or
allocating a temporary storage structure equivalent to that
of the allocation table to make the thing run optimally fast.
2024-05-18 00:24:52 +08:00
bunnie
c412f44b51 quiet down debug in the irq routine 2024-05-18 00:24:25 +08:00
bunnie
aa61e091ef add unit test for MinSet / renormalization 2024-05-18 00:24:01 +08:00
bunnie
fdbbe8d63d move MinSet to utils so that we can test it
thought it would be a good idea to have a unit test for
the MinSet routine (which is used to renormalize the swap
epoch when it overflows). Turns out that was...complicated...
because of the usual problems of test running on host and
the inability to pull through hardware dependencies.
2024-05-18 00:22:50 +08:00
bunnie
44890d72ac add test and debug deps/features 2024-05-18 00:22:23 +08:00
bunnie
52d629308b make loader dependency only for hardware targets 2024-05-18 00:10:29 +08:00
bunnie
b115b7e60c add swap-verbose placeholder 2024-05-18 00:09:57 +08:00
bunnie
d2d982b9f2
Merge branch 'main' into swap-opt 2024-05-16 14:56:02 +05:30
bunnie
f8f163d531 update todo list 2024-05-16 03:59:38 +08:00
bunnie
e3b3960a40 add a "debug-verbose" option on the swapper
we'll surely need this again someday...
2024-05-16 03:58:03 +08:00
bunnie
55ebb556ba quite down some ll debug 2024-05-16 03:57:45 +08:00
bunnie
c1b68018e1 lots of debuggening 2024-05-16 03:57:29 +08:00
bunnie
e94e447e93 handle IRQ swaps & lighter-fingered about flag sets
previously was setting all pages as USER even if they shouldn't
be. Trust the flags are set correctly, just mark the valid bit.

Also, detect swaps in an IRQ handler and manage accordingly.
In particular, don't re-disable IRQs (as it overwrites the
saved IRQ mask), don't re-enable IRQs on exit, and don't
modify process Run states.
2024-05-16 03:55:41 +08:00
bunnie
19128a83a3 add ability to "touch" an alloc so that the LRU count updates
also add some debug access functions
2024-05-16 03:54:51 +08:00
bunnie
3112f41d96 fix bug in debug 2024-05-16 03:54:35 +08:00
bunnie
c06612e43e lock down some key pages
- no swapping of kernel pages
- mark L1 PT's properly in the memory allocation table
  (was accidentally marking them with the VA of the first address
   allocated)
2024-05-16 03:53:07 +08:00
bunnie
37109b19a8 fix ordering of the SwapAlloc for BinaryHeap impl 2024-05-16 03:52:22 +08:00
bunnie
e75f6d80db tone down some debug output 2024-05-16 03:51:59 +08:00
bunnie
03515934df clarify function of the thread context area 2024-05-16 03:51:38 +08:00
bunnie
6d01e69faf fix debug formatting 2024-05-16 03:51:03 +08:00
bunnie
82da94b3da add code to try to handle the case of swap in IRQ
the cases are delineated but still getting a fault on return
from the handler.

also optimize out a memory space swap that was incurred due to
inappropriate abstraction boundaries.
2024-05-15 04:26:08 +08:00
bunnie
5f127b4548 create swap resume functions; differentiate irq context calls
- invoking the swapper context is different in an IRQ because
we don't want to modify the runnability states of the original
process

- didn't want to make a custom version of swap resume but
with the IRQ issue it made sense. In the process of doing this,
we can also optimize out a memory space transition, so it's
probably worth it even if we're not entirely going down the
right path to fix the bug of interest.
2024-05-15 04:24:19 +08:00
bunnie
900d829bf4 a lot of bug fixes in the core kernel swapper
probably should have done this in smaller commit chunks but it
was hard to say if this was going in the right direction until now.

at the moment, system can boot and evict quite a bit of memory before
hitting a panic. The current state is that we panic on returning
from an interrupt handler that hit a swapped page.

This is a pretty decent point to check point everything. hopefully
this next arc will be a bit cleaner.
2024-05-15 02:44:11 +08:00
bunnie
0fbedcebbf add the skeleton for the kernel OOM handler 2024-05-15 02:43:06 +08:00
bunnie
2b05626b69 add context switching debug option 2024-05-15 02:42:29 +08:00
bunnie
1b0fc181a5 ensure process state is correct on errors; remove excess mmu flushes 2024-05-15 02:41:15 +08:00
bunnie
b86ee4b036 move irq-renable as late as possible; add helper function to access irq state 2024-05-15 02:40:31 +08:00
bunnie
27d7fb414c add debug on evictpage 2024-05-15 02:39:58 +08:00
bunnie
ff42e1ab20 better handling of oom_doom; wired page handling; fix bugs 2024-05-15 02:39:06 +08:00
bunnie
ef5388a30e ensure that process descriptors aren't swapped out 2024-05-15 02:38:24 +08:00
bunnie
f66debdd01 make susres page ownership not hard coded; mark PTs as wired
also marks the ProcessImpl page as wired too
2024-05-15 02:31:40 +08:00
bunnie
ad9ef3dedc cleanup comments and fix ordering of events 2024-05-13 21:05:46 +08:00