notes

Region reaping: what destroying a region does to the objects in it

Name: provisional, minted 2026-10-23 (UTC) by comments/929-kernel-sched-objects.

sched::reclaim_region is how a region's owner gets its memory back (§16 (object revocation)). It tears down every kernel object retyped from the region, unpins it and returns the pages. The code is kernel/src/sched/region_reaping.rs. The supervisor's way in, rendezvous::REAP (§32 (a supervisor may collect a corpse without being able to build one)), and the survey beside it are built on the same pass. The stale-reply hazard the pass is shaped around is argued in notes/blocked-thread-teardown.md, and the on_cpu refusal's evidence is in notes/stack/kernel-stack-freed-under-its-owner.md.

The order of a reclaim

  1. A region carved into children refuses before any teardown, so that refusal leaves the region exactly as it was. The children own part of its run and free those pages themselves.
  2. reap_region_objects runs under IPC_TABLES. It is the caller-driven half of revocation and must run outside any teardown Drop.
  3. user::reap_address_spaces_in_region takes the registry's lock, which takes IPC_TABLES beneath it to ask about bound threads. The registry owns bound spaces (§249 (a running address space stays nameable)), so this step also collects the space of every thread step 2 removed, and a corpse's space whose root is in the region. A space whose thread can still run was refused in step 2.
  4. memory_region::unpin and memory_region::destroy, neither of which takes IPC_TABLES. That is why the reaper's Drop to destroy path cannot deadlock against step 2.

Generational names make every capability to a dead object stale on its next use, so there is no capability tree to walk and no copies to hunt (seL4 walks its CDT instead).

The phases of reap_region_objects

An object lies in the region when its backing page does: a TCB sits at the start of its page, so the thread pointer translated back to a physical address is the whole test.

  1. Rendezvous. Every rendezvous in the region is removed, refusal or not. Removing one drains its wait queues, marks each waiter's IPC aborted and wakes it. A resident parked in RECEIVE on the region's own rendezvous becomes schedulable, so the kill the refuse phase arms can land on it. strand_callers_awaiting then frees the callers the drain cannot reach: a CALL whose request was taken has left the sender queue, and only its wait_on records that it awaits a reply here.
  2. Notifications, timers, processes. Notifications get the same sweep, for the same reason. An armed timer has no waiter to abort and simply never fires. A process in the region is ended, its embryo members detached, and forgotten once empty.
  3. Finish. Every resident whose verdict is FinishInPlace is ended where it is blocked (below).
  4. Refuse. Any resident still able to run is armed and the pass refuses. Any resident still on its own kernel stack refuses the pass without arming.
  5. Removal. Every thread in the region is Embryo, Finished or Dead, and each is removed. A supervised corpse whose death message nobody received is still parked on its supervisor's rendezvous, outside the region, so it is unlinked first. Freed while linked, it would leave a pointer the supervisor's next RECEIVE follows into a recycled page. strand_callers_of frees the callers named by reply capabilities the thread collected. depart and the kill conversion in schedule do the same for threads that ran.

One at a time, by rescan

Each phase that removes finds one victim, removes it and rescans, rather than collecting the victims into an array first. remove mutates the table, so a collect-then-remove shape needs a scratch array of MAXIMUM_RENDEZVOUS or MAXIMUM_THREADS names, and this is one of the deepest frames in the kernel. A [u64; 512] is 4 KiB of a 24 KiB kernel thread stack (notes/stack-high-water.md has the frame's measured size). Rescanning costs O(live objects) per removal, on a teardown path that removes a handful. Each pass changes the victim so it no longer matches, so each loop terminates.

The rendezvous drain wakes inside its callback for the same reason. That is safe because Rendezvous::drain_waiters pops an entry before calling back, so the thread's one intrusive link is free for wake to push it onto a run queue. And rendezvous_of returns a 'static reference, which does not hold the sched borrow the callback needs.

The verdict on one resident

region_reap_verdict(state, standing) decides, and it is a function so it can be tested without staging a race. state says whether a thread can run again. standing says whether a core is standing on its kernel stack, and freeing a Thread unmaps that stack. They are different questions.

state standing verdict what the pass does
Ready, Running either RefuseAndArm marks it killed and refuses; the scheduler converts it to a corpse at its next preemption
Blocked no FinishInPlace ends it in the finish phase
Blocked, Dead, Finished, Embryo yes RefuseStanding refuses and arms nothing
Embryo, Finished, Dead no Reap removes it

standing is handshake.on_cpu or'd with Thread::being_reaped. on_cpu is cleared by the core's successor in finish_switch. depart marks a supervised thread Dead, delivers its death message, releases IPC_TABLES and only then switches away, so a supervisor on another core can ask for a reap inside that window. being_reaped marks a thread whose stack and space the reaper is freeing outside IPC_TABLES; reaping it now would let its owner reuse memory the dying thread has not given back. Both conditions clear by themselves, so the refusal arms nothing and the owner's retry succeeds.

A Blocked thread is not armed because the arm cannot reach it. schedule() spends a kill only for a thread whose state is Running, and a thread blocked on a rendezvous nobody will serve never becomes Running again. The finish phase is calef's proposal A, ruled 2026-09-03, of milestone 133 (ending a permanently blocked thread). The holder of the untyped capability could already end every runnable thread in the region, and now it can also finish a blocked one. Nothing is added to the system call surface.

A Dead corpse never runs again, so it is reapable exactly like an Embryo or a Finished thread; that is what §26 (the fault endpoint) means by reaped with §16 revocation.

The refuse phase meets a FinishInPlace verdict only if the finish phase missed one. It refuses conservatively there rather than panicking, because a teardown-path panic costs more than one more retry by an owner that already has a retry loop.

Ending a blocked resident

finish_blocked_resident unlinks the thread from whatever queue holds it, deletes every outstanding Reply capability naming it, and writes it to Finished without waking it.

Why dropping a resident's space is safe

The pin is not what makes it safe. A drop inside reap_region_objects, under IPC_TABLES, is refused by memory_region::destroy while the region is pinned, but the reaper (finish_switch) hoists a dead thread's space out, releases IPC_TABLES and drops it later, when reclaim_region may already have unpinned. What makes it safe is that a space built from a region it does not own never frees that region (user::Backing). That is a property of the space, not of the timing.

The supervised reap

reap_supervised(ep, tid) collects a corpse for the supervisor that holds its supervision rendezvous. Authorization is the relationship the kernel already tracks: the thread's recorded fault_ep (§26 implementation note 1) must be the invoked rendezvous, so the tid needs no badge or handle. reap_decision is the rule, proved for every input by the harnesses in kernel/src/sched/region_reaping/verification.rs. A stale or recycled tid resolves to nothing and reads as unsupervised.

The reclaim is then reclaim_region on the region the TCB was retyped from, the name the owner would pass to MemoryRegion::DESTROY, so the two paths cannot drift. The pages go back to the region's owner under §13 (capability revocation and untyped reclamation), the builder, so a supervisor frees a child's memory without being able to spend it.

A corpse whose core has not switched away yet is waited for, not refused. reclaim_region refuses it, and that refusal reaches userspace as NotPermitted, which the ABI gives another meaning and which two supervisors in this tree treat as fatal. The condition ends at that core's next finish_switch and needs only the lock, so the reap releases IPC_TABLES, yields and looks again. NotPermitted still comes back for DESTROY's own reasons: the child split its budget, another thread in the region can still run, or a racing reap made the name stale. A restart policy reads it as "not yet".

ReclaimRefusal and last_reclaim_refusal (system tests only) record why the last refusal happened, because every refusal reaches userspace as one NotPermitted. The failure they were built for (milestone 321 (a corpse that did not park)) happened about once in a thousand runs.

The survey

survey_supervised is rendezvous::SURVEY, and its design is in notes/process-view.md. Three kernel-side rules:

The words it reports: CPU time is milliseconds, converted in the kernel so no reader depends on TICK_HERTZ (100 Hz outside the bench build, so it moves in steps of ten). Placement maps the kernel's u8::MAX "not placed" to record::NO_CPU (u64::MAX), because 255 is a plausible CPU id for a reader to tally. State maps the two states a supervised thread cannot reach, Embryo and Finished, to READY and DEAD, its nearest neighbors; the match is exhaustive so a new state meets it.

BUGS