Section 9.21

Grace period

Definition

A grace period is the wait that read-copy update §9.20 (RCU) places between removing an old version of shared data and freeing that version. An RCU reader marks each stretch of code that uses shared data as a read-side critical section §9.4. A grace period starts once no shared pointer leads to the old version, and it ends once every reader that was inside a read-side critical section at that start has left its section. Later readers find only the new version and are not waited for.

The idea

An updater has replaced version A of some shared data with version B and wants to free A. The updater cannot ask readers whether they still hold A, because RCU readers record nothing about what they read; recording nothing is exactly what keeps reading cheap. The updater must instead wait until no reader can still hold A. Waiting until there are no readers at all does not work: on a busy system, readers overlap continuously, and a moment with no reader inside a read-side critical section may never come.

The key observation is that only readers that were already running when A was removed can hold A. A reader that starts after the removal follows the shared pointer and finds B. So the updater sorts readers by when they started, relative to the start of the grace period, and waits only for the earlier group. That group is fixed when the grace period starts, and every member of it eventually leaves its section, so the wait ends even while new readers keep arriving.

A museum that closes one gallery for repairs works the same way. Staff stop admitting visitors at the gallery door and then wait until everyone who was inside when the door closed has walked out. Visitors who arrive later are sent to another gallery and do not delay the work. The picture stops in one respect. The museum staff can count the people in that one gallery. RCU does not know which readers saw the old version, so it waits for every reader that was inside any read-side critical section when the grace period began, even readers that were using unrelated data.

How it works

Implementations detect the end of a grace period through quiescent states. A processor is in a quiescent state at any moment when it is not running a read-side critical section; the quiescent point §14.34 entry treats the general idea. A read-side critical section is one continuous stretch of code. In a kernel where a reader cannot be switched out in the middle of its section, a processor that has passed through a quiescent state after the grace period started has therefore finished every section it was running at the start.

With that rule, a grace period runs in five steps.

  1. The updater removes the old version A: it replaces every shared pointer to A, using rcu_assign_pointer, which stores with release ordering.

  2. The grace period starts. RCU records which processors still owe a quiescent state.

  3. Each processor reports a quiescent state when it passes through one. In the classic Linux kernel without preemptive scheduling §6.37, a context switch, the processor changing from one thread (one of several instruction sequences sharing the processors and memory) to another, is a quiescent state, because a reader cannot be switched out inside its section. Running user programs and being idle also count.

  4. When the last processor has reported, the grace period ends. synchronize_rcu() returns to its caller, and RCU runs the callbacks §14.22 that were handed to call_rcu(), each a function to be run after a grace period.

  5. The updater, or the callback, frees A.

The Linux documentation gives a toy version of steps 2 to 4 for a kernel without preemption: synchronize_rcu() makes the calling thread run once on each processor in turn. For the thread to run on a processor, the scheduler must switch to the thread there, and that switch is a quiescent state for that processor. When the thread has run on every processor, every reader that was running when the call started has finished.

Kernels with preemptive scheduling let a reader be switched out inside its section. There the read-side operations keep a nesting count in the thread, and the grace period also waits for every thread that was switched out inside a section until that thread leaves the section. Epoch-based reclamation, used in user-space libraries, reaches the same guarantee differently: each reader records a global epoch number when it enters a section, and memory retired in one epoch is freed only after every reader that could have been active in that epoch has moved on.

A grace period may last longer than strictly needed. An implementation may wait for more quiescent states than necessary, and it may serve many updaters with one grace period. A grace period may never end early.

Worked example

Three processors run three threads, and times are in microseconds (μ\mus).

The grace period lasted 10−5=510 - 5 = 5 μ\mus.

Compare a fixed delay. Suppose the updater sleeps 3 μ\mus instead of waiting for the grace period, and frees A at t=5+3=8t = 5 + 3 = 8. R1 is still inside its section until t=9t = 9 and may read A at t=8.5t = 8.5: a use-after-free §4.6. In this run the shortest safe wait was 9−5=49 - 5 = 4 μ\mus, but in the next run an interrupt §6.11 may stop R1 for 500 μ\mus in the middle of its section. No fixed delay is right for every run, because the updater does not control how long a reader’s section lasts.

Compare also waiting for a moment with no readers. Suppose readers keep arriving: R1 runs from 4 to 9, R2 from 6 to 14, R3 from 12 to 20, R4 from 18 to 25, and so on, each starting before the previous one ends. No instant after t=4t = 4 has zero readers, so an updater waiting for such an instant waits for as long as the pattern continues. The grace period still ended at t=10t = 10, because it waits only for R1.

Stated precisely

Let ss be the time at which a grace period starts and gg the time at which it ends. Call a read-side critical section pre-existing if it began before ss. The grace-period guarantee is: every pre-existing read-side critical section ends before gg, and every memory access made inside it is complete and visible before any code that the updater runs after gg. Equivalently, if any part of a read-side critical section comes before the start of a grace period, the whole section comes before the end of that grace period. The guarantee says nothing about sections that begin after ss; the grace period may end while they are still running. The Linux documentation describes synchronize_rcu() in these terms: it waits until all pre-existing read-side critical sections have completed.

Freeing an old version A after gg is safe under four assumptions.

  1. Removal before the start. Every shared pointer to A was replaced before ss, so a section that begins after ss cannot find A.

  2. Confinement. Readers obtain A only inside a read-side critical section and stop using A when that section ends.

  3. No other reference. No other mechanism, such as a counted reference or a copy of the pointer stored elsewhere, still holds A.

  4. Reader rules. Readers obey the rules of the RCU variant in use; in basic RCU a reader does not block inside its section.

Under these assumptions, a reader that uses A at or after gg would need a section that began before ss, which has ended, or one that began after ss, which cannot have found A. Neither exists.

Limits and common mistakes

A grace period is not a time delay. A sleep of fixed length proves nothing, because interrupts, preemption or a slow processor can stretch any reader’s section beyond it.

A grace period does not need a moment when no reader is active. Waiting for a count of active readers to reach zero is a much stronger condition: under steady reading it may never be met, and the updater suffers starvation §9.18, waiting indefinitely while readers keep making progress.

A grace period covers only pointers that readers obtain and drop inside read-side critical sections. A pointer copied into a long-lived structure, or passed to another thread, escapes the grace period. Such a pointer needs its own protection, for example a counted reference taken inside the section.

A grace period is long compared with one read; in the Linux kernel it typically lasts milliseconds. An updater that calls synchronize_rcu() after every small change can therefore make only a limited number of changes per second. Handing the free to call_rcu(), or grouping several changes under one grace period, avoids that limit.

A reader that never leaves its section blocks every later grace period, so nothing is freed; the effect is the same as a memory leak §4.8 that grows with every update. The Linux kernel detects such long waits and reports them as RCU CPU stall warnings.

Grace periods of one RCU variant do not wait for readers of another. A grace period of one domain of sleepable RCU (SRCU), the Linux variant whose readers may sleep, waits only for readers that entered sections of that same domain. Readers must use the read-side operations that match the wait the updater uses.

A grace period says nothing about which version a reader sees. A reader that started just before the switch keeps using the old version until it finishes.

Questions answered

How are readers that begin after publication distinguished from earlier readers?

Readers that begin after publication are distinguished from earlier readers by when they start relative to the start of the grace period, not by any label or timestamp that a reader records. The updater first publishes the new version, which removes the old version: no shared pointer leads to the old version any more. Only then does the grace period start. A reader that begins after that start can find only the new version, so it never needs to be waited for. A reader that was already inside a read-side critical section, the marked stretch of reader code, when the grace period started may hold the old version, so the grace period lasts until every such reader has left its section. A reader that started in the short window between the publication and the start of the grace period is treated as an earlier reader; waiting for it is unnecessary but harmless.

Implementations detect the end without tracking individual readers. In a kernel where a reader cannot be switched out inside its section, they wait until every processor has passed through a quiescent state, a moment outside any read-side critical section, after the start. A processor that has done so cannot still be running a section that began before the start. A clock reading or a fixed delay shows neither that the old version is unreachable nor that the earlier readers have finished.

Related entries: Read-copy update (RCU) (§9.20), Memory leak (§4.8), Monotonic clock (§9.35), Quiescent point (§14.34), Starvation (§9.18). Further reading: [23].