Commit graph

857 commits

Author SHA1 Message Date
bunnie
010bdd0180 more debug tweaking in irq reporting 2024-05-24 23:59:41 +08:00
bunnie
84ef3ed694 add thread reporting to irq entry 2024-05-23 16:49:39 +08:00
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
cf6c084696 quite down debug in kernel 2024-05-21 02:56:08 +08:00
bunnie
353f4d5f1b fix config flag 2024-05-21 01:46:02 +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
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
d2d982b9f2
Merge branch 'main' into swap-opt 2024-05-16 14:56:02 +05:30
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
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
ad9ef3dedc cleanup comments and fix ordering of events 2024-05-13 21:05:46 +08:00
bunnie
8824372101 make debug a little more compact for the renode swap target 2024-05-13 21:02:32 +08:00
bunnie
327205edb5 release page relies on a physical address, not a virtual address. 2024-05-13 21:00:17 +08:00
bunnie
235806025c clarify api names; add evict target pages; more error checking on eviction 2024-05-13 19:20:59 +08:00
bunnie
0847f99433 swapper abi fixes
- evict error checking - reject requests to swap kernel or swapper
- add target amount of pages to evict on oom handler
2024-05-13 19:19:50 +08:00
bunnie
0fca04f14e swapper needs write privs to the lent page
encryption is in-place to save on memory
2024-05-13 19:18:49 +08:00
bunnie
74f75e1ef5 re-org swap code for readability, add oom_doom routine
some thought needs to go into when and how we call oom_doom
but let's test more core functionality of the swapper before we
worry about that. I can think of several places where we could
use that call, the main tricky bit is that oom_doom diverges
and so wherever we patch it has to be able to resume. If we put
it into e.g. somewhere in the main memory alloc loop, the trouble
is we would have to track all of the multiple places that we
could have arrived there...ideally we can find a single spot
to patch this, with a clean entry and exit path.
2024-05-13 17:24:35 +08:00
bunnie
a07ab0e121 update comments and add license headers 2024-05-13 14:21:53 +08:00
bunnie
dcef549099 move SwapAlloc to the swap module 2024-05-13 14:15:38 +08:00
bunnie
9b89bde46a refactor alloc API
instead of sending scalar messages on every page alloc/free, which
is a lot of traffic, introduce an API where the swapper can request
a snapshot of the alloc status by mapping the kernel's alloc
table as a read-only structure into the swapper's memory space,
and then invoking the swapper in its interrupt context to read that out.

The structure is unmapped on return from the context.

additionally, a running tally of free space is now maintained
and this can be queried with a fast syscall.

Thus, the normal workflow is the swapper would have a timer that
periodically queries the kernel's free space level, and when it
falls below a critical amount, it will invoke the expensive snapshot.

this can also be extended so that the kernel can call into
the swapper's interrupt context with a "help, OOM imminent"
message down the road.
2024-05-13 13:59:17 +08:00
bunnie
84d42460eb correspending kernel changes for run time page tracker split
memory alloc table split into RAM region and everything else ("extra")
2024-05-12 20:23:29 +08:00
bunnie
a3e55834e9 checking in the corresponding kernel changes to the RPT type alias 2024-05-12 16:28:14 +08:00
bunnie
90108f266e allow this trait to be unused
it's not used in all configs but it's not a bad thing to have
it around.
2024-05-10 00:38:41 +08:00
bunnie
1f12a97b64 cleanup debug 2024-05-08 07:22:43 +08:00
bunnie
1112ef5df6 remove unused function (forgot this arg was already available) 2024-05-08 07:22:21 +08:00
bunnie
a58a8dc438 cleanup some debug 2024-05-08 07:21:06 +08:00
bunnie
ee0b1a042c optimize kernel-side alloc advisories; cleanup some APIs 2024-05-08 07:20:33 +08:00
bunnie
4f471544bd add nesting depth = 2 for swapper ops
I think that structurally we might never exceed a nesting depth
of 2, but there are some specific cases where an alloc can call
an alloc in the kernel (e.g., having to create a new superpage
in the midst of a routine alloc), therefore, we have to handle
some nesting.

There's also chance that maybe nesting could be due to race
conditions or self-circular behavior in the swapper, but ideally
we avoid that entirely because we can't handle non-structural,
unbounded nesting of re-entry to the swapper routine.
2024-05-08 00:57:10 +08:00
bunnie
43c452bf97 add a call to get the free memory status from the kernel
this will be helpful for debugging our page tracker
2024-05-07 04:12:46 +08:00