Chris Low-level systems developer
← Back to all posts

The Sleep That Cannot Miss Its Wakeup

ISA

No instruction-set claim is made. The whole mechanism is locks and state flags in C: register a channel, clear a channel, set a state. No instruction, CSR, or privilege behavior is asserted or measured.

MICROARCH

No claim depends on how any core implements anything. The ordering argument rests on lock serialization, which holds identically on any core; no cache, pipeline, or timing behavior is asserted.

FIRMWARE

No boot code, machine-mode service, or privilege handoff takes part. The code runs in the kernel proper, in wait(), sleep_prepare(), sleep(), exit(), and wakeup().

KERNEL

The verdict is a kernel sleep/wakeup protocol. Correctness rests on which lock is held at each step: wait() registers the channel under the process lock while still holding wait_lock, and exit()'s wakeup clears or honors it under the same process lock, which is exactly what the cited source shows.

ALGORITHM

The exclusion is a hand-checked case analysis over the three positions a wakeup can take relative to the registration, read off the lock order in the source. No independent oracle, known-answer vector, or mechanized proof is involved.

A parent calls wait(). No child has exited yet, so the parent must sleep until one does. The oldest race in kernel code lives in the breath between those two facts: the scan says "nothing yet", and before the parent actually goes to sleep, a child exits. A wakeup fired at a process that is not yet asleep reaches nobody, and a wakeup that reaches nobody is lost. The parent sleeps, and nothing will ever wake it. [1]

The fix is the order of three calls. wait() still holds the wait lock while it scans the children. Finding none exited, it does not release the lock yet. First it calls sleep_prepare(p), which takes the parent's own per-process lock and registers the sleep channel: wake me on this pointer. Only then does wait() release the wait lock, and only then does it call sleep(), which takes the per-process lock again and goes to sleep only if the channel is still registered. [1]

The child's half runs under the same wait lock. exit() reparents the dying child, then calls wakeup(p->parent). wakeup walks the process table, takes each process's per-process lock, and where the channel matches it clears the channel; if the process already reached SLEEPING, it marks it RUNNABLE instead. [1]

So order the child's exit anywhere on the timeline. Before the registration: the parent's scan found the zombie, so it never slept. Between the registration and the actual sleep: the wakeup clears the channel under the same lock, and sleep() sees an empty channel and returns without sleeping. After the sleep: SLEEPING flips to RUNNABLE. There is no fourth position. The registration is what collapses the gap: a wakeup can arrive early or late, but it can never arrive between.

The three calls that seal the gap:

sleep_prepare(p);      /* register the channel, holding p->lock */
release(&wait_lock);   /* the decision is sealed */
sleep();               /* sleeps only if the channel is still set */
Blueprint-style timeline of the sleep/wakeup ordering: the wait() sequence of scan, registration, lock release, and sleep on a timeline, with three wakeup landing positions. A wakeup before registration finds the zombie child; one between registration and sleep clears the channel so sleep() returns without sleeping; one after the sleep flips SLEEPING to RUNNABLE. No fourth position exists.
Every wakeup lands on one of three positions. Registering the channel before releasing the wait lock means there is no gap between the decision to sleep and the sleep itself.
Diagram: hand-built by Chris Dillinger for this post

The bench habit: whenever you add a sleep and wakeup pair to driver or kernel code, register your interest before you drop the guard lock, then re-read the three positions. If a wakeup can land somewhere with no verdict, the gap is still open.

Sources

  1. 01
    proc.c: sleep_prepare, sleep, wakeup, wait, and exitChris Dillinger

    The three primitives and their caller order, read directly: sleep_prepare takes the process lock and records the channel; sleep takes the process lock and sleeps only while the channel is still set; wakeup takes each process's lock, clears a matching channel, and marks an already-sleeping process runnable. wait calls sleep_prepare before releasing the wait lock, then sleep; exit calls wakeup on the parent while holding the wait lock, so the child's exit lands either before the scan or after the registration, never between.

Related work: kernel and driver correctness work, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app