kernel: a grant is a loan — a dead borrower returns the device to its lender

When a driver dies, a device it was *given* now goes back to whoever lent
it, rather than to nobody. The device manager gets its hardware back the
instant a driver dies and hands it to the replacement, with no window in
between.

That window was real: the kernel released the claim to no one and the
manager re-claimed first-come, so every driver restart reopened the hole
this run is closing. It also becomes load-bearing at the next step — once
claim refuses a device that has a giver, releasing to nobody would strand a
dead driver's hardware permanently, because nobody could ever take it again.

A dead lender is no lender: the claim and the giver clear together, so a
device is never owed to a ghost. A device nobody lent is released outright,
exactly as before.

The broker cannot see the task table, so liveness arrives through the same
hook idiom the scheduler already uses. Null means assume dead, so a kernel
built without the hook frees claims rather than handing them to a ghost.

A stale binary nearly passed as proof for the third time this session: the
first discrimination patch left `alive` unused, the build failed with three
errors, and the old binary reported every assertion passing. Checking the
build before reading results is what caught it.

Suite 118/118.
This commit is contained in:
Daniel Samson
2026-08-08 22:21:25 +01:00
parent 4ca57fc37e
commit 1a1d92cba9
4 changed files with 55 additions and 4 deletions
+12
View File
@@ -4154,8 +4154,20 @@ fn deviceTransferTest(boot_information: *const boot_handoff.BootInformation) voi
const absent = if (devices_broker.transfer(9999, me, child)) |_| false else |e| e == error.NoSuchDevice;
check("a device that does not exist is refused", absent);
// **A grant is a loan.** The child holds device 0 and `me` lent it, so the child's
// death must hand it back rather than release it to nobody. That is what lets the
// device manager re-delegate to a restarted driver — and, once `claim` refuses a
// device that has a giver, it is the only thing that stops a dead driver's hardware
// being stranded forever.
check("the child still holds the lent device", devices_broker.ownerOf(0) == child);
devices_broker.releaseAllOwnedBy(child);
check("the borrower's death returns the device to its lender", devices_broker.ownerOf(0) == me);
check("and it is no longer on loan", devices_broker.giverOf(0) == null);
// A device with no lender still simply frees on death, as it always did.
check("claimed a second device with no lender", claimOk(parentless, me));
devices_broker.releaseAllOwnedBy(me);
check("a device nobody lent is released outright", devices_broker.ownerOf(parentless) == null);
result();
}