decisions DECIDED

321. Reclaiming a whole region subtree

(The section number and title are provisional until the merge lands them.)

Raised by #3417's per-subtree ceilings, which bound what a region subtree holds but do not give it back. The code read is main and that branch (nifeos/nife@0f9675218).

Background

A memory region is a run of physical pages that a program holds a capability to. The program retypes pages out of it into frames and kernel objects: threads, rendezvous, notifications, timers, processes and address spaces. SPLIT carves part of a region into a child region, and the child's pages lie inside the parent's run. DESTROY (sched::reclaim_region) tears down every object whose page lies in the region, revokes every mapping of its pages and returns the run: a root's pages go to the frame allocator, a child's go back to its parent. §16 (object revocation) holds that model. No capability derivation tree exists: objects carry generational names, so freeing an object makes every capability to it stale.

DESTROY refuses a region with live children (ReclaimRefusal::HasChildren), because freeing the parent's run would free pages a child still owns. A program can therefore leave a subtree that nobody can reclaim:

  1. It splits child regions from its own region and creates objects in them.
  2. It deletes its capabilities to the children, or exits holding them.
  3. Its supervisor calls DESTROY on the program's region and gets NotPermitted, on every retry.

No capability names the children any more, so nothing can reclaim them, and the parent refuses until they go. Their pages, region slots and objects stay held until reboot.

#3417 gives every region a live count and a ceiling for each kind of kernel object and for child regions. A creation refuses when the region or any ancestor is at its ceiling. One stranded subtree can hold at most a sixteenth of its root's ceilings. The ancestors keep its counts, though, so a parent that runs such programs one after another fills its own ceiling, and then nothing under that parent can create that kind of object.

The decision

Option (a2), ruled 2026-11-11 (UTC), and not yet built:

The premise, checked in the tree

Two places in the tree already meet the gap.

Reading reclaim_region also turned up a race. It reaps the region's objects, unpins the region and then claims it. A holder of the region's capability outside the region, on another core, can retype a kernel object into it after the reap has scanned the tables and before the unpin. The unpin then clears that object's pin, the claim succeeds, and the region frees a page that a live object occupies. A second holder can also split or retype between the unpin and the claim. The claim then refuses, destroy returns nothing, and reclaim_region still reports success. No test reproduces it. The BUGS section of region-reaping.md records it.

What makes a subtree cheap to find here

Three facts from the code shape every option's cost.

REGIONS is an InterruptSafeMutex: the holding core keeps interrupts masked for the whole hold, so what a hold costs is how many parent links it follows.

The options

(a) Reclaiming a region reclaims its whole subtree

The holder of a region's capability ends everything below it in one act: every descendant region, every object in any of them, every mapping of their pages. This is seL4's Revoke on an untyped and Fuchsia's job kill, expressed as a region operation.

It has two shapes. (a1) changes DESTROY itself, so it no longer refuses a region with children. (a2) adds a separate method on MemoryRegion (DESTROY_SUBTREE) and leaves DESTROY's refusal as it is.

The kernel's work, in order:

  1. Close the subtree: under one REGIONS hold, mark its root as closing. Every object creation and split already walks each ancestor in RegionTable::charge, under the hold that carves the page, so the mark refuses them at no added cost. Frame retypes need no check, because the claim kills every name before the revoke. The same mark on a single region closes the race above.
  2. Reap the span: reap_region_objects and the address-space sweep run over the root's span, unchanged. The ceilings bound the victims to what one region could already hold, so this costs no more than reaping a full region today.
  3. Claim the subtree: under one REGIONS hold, mark every live descendant, then remove the marked entries and the root. The root's live counts already sum its subtree, so one ancestor walk gives everything back, as claim_for_destroy does now. Below the progenitor's root each scan follows at most 1,024 × 57 parent links. Under a root without ceilings a hold could follow close to a million; that is arithmetic, not a measurement. One descendant per hold bounds each hold at one scan.
  4. Revoke and return: one revoke_region over the span outside REGIONS, then the root's run goes back as today. The descendants' pages lie in that run and need no return_to_parent.

No step recurses: each table loop stops after N steps, since a longer chain would be a cycle, and step 3 needs no scratch array. The mark costs a field in Region. #3417's Kani harnesses peak at 3.6 GB, and a symbolic flag in Region once took the model past 37 GB, so the field needs a measured harness run.

Threads and address spaces. Every thread in the span gets today's verdict (region_reap_verdict). A runnable thread has its kill armed and the call refuses; the holder retries, as DESTROY callers do. A blocked thread ends in place. Spaces rooted in the span go with the registry sweep. A refused pass leaves the subtree closed, as a Fuchsia job stays killing, so no new object races in. Waiters outside the subtree get Gone, as reclaim_region's BUGS describes.

Ceilings. One ancestor walk returns every count, and the ceilings bound the work of one call.

The ABI. (a1) changes what DESTROY means for every caller. release_budget reads the refusal as "the job left children", memory_grant_reclaim_tests asserts it, and a builder destroying its budget would now end a child it lent. (a2) adds one method and changes nothing that exists.

(b) Deleting the last capability to a region reclaims it

When the last capability naming a region goes, the kernel reclaims the region. seL4 destroys an object when its final capability goes, and EROS-style systems do the same for a bank's objects.

The kernel's work:

Threads and address spaces. A running resident refuses the reclaim, so the reaper retries until the kill lands, holding the queue entry. Deleting a capability becomes destructive: a program that drops its only capability to its own heap region kills itself. Today capability::CapabilityTable::delete documents the opposite: "The object may still exist; we simply can no longer name it."

Ceilings. Each region returns its own counts at its own reclaim. A region waiting in the reaper's queue keeps its counts until the reaper reaches it.

Authority. §16 chose an explicit trigger on purpose: reclaiming memory is an authority, and a capability system grants it through a capability someone holds and invokes. (b) makes it fire on the absence of any holder.

(c) Leave it as it stands

DESTROY keeps refusing, the 2026-11-01 contract keeps covering the shell's jobs, and the ceilings keep confining the damage to one parent's share.

The kernel pays nothing. The costs fall elsewhere. A parent that runs stranded programs one after another loses its ceiling for the boot. §40's cascade holds only for supervisors that never split, and the spawner's budget stays unreclaimable while an instance lives. The race above still needs a fix of its own.

(d) A budget that withholds a new right cannot split

Add a right that SPLIT requires and leave it out of abi::rights::SPEND_ONLY, so a leaf cannot strand a subtree. It tightens now and relaxes later compatibly, restrictive-first's own shape. It loses here because the builders that split by design (the spawner, the shell, the progenitor) keep the gap, and §40 and the race stay as they are. It also spends a bit of the rights word.

Prior art

This lane read each source where named, on 2026-11-11 (UTC), unless marked recalled.

Where nife differs

A concrete example

A parent region P sits directly under the progenitor's root, so its subtree may hold 56 regions, 28 rendezvous and 14 notifications. A supervisor splits a 64-page budget B from P and runs job J in it. J splits three 8-page regions C1, C2 and C3 from B, creates one rendezvous and one notification in each, deletes its three capabilities to them and exits.

Why (a2)

The decision refuses (b) and leaves (d) as a separate narrowing. The restrictive-first reading, option by option:

At equal cost the choice stands. (a1) and (a2) differ only in reversibility, and (a2) is the reversible one; (c) loses on §40's guarantee, not on effort.

What follows