Section 14.34

Quiescent point

Definition

A quiescent point for an object, a piece of code or data that is to be replaced or freed, is a moment after which no part of the system is using the object and none can start using it again. From then on, the object can be replaced or its memory reused safely. Quiescence is relative to one object: a system can be quiescent for one buffer while other work goes on. The parts that count are every thread, interrupt handler §6.12, pending callback §14.22 and device that can reach the object.

The idea

A program wants to free a receive buffer. The worker thread that processes the buffer reports that it is idle. But an interrupt handler, code that the processor runs when a device signals it, keeps a pointer to the buffer and will use it at the next interrupt §6.11. And a direct memory access §4.36 (DMA) controller, a device that copies data into memory without the processor, was given the buffer’s address and will write the next incoming packet into it. If the buffer is freed now and its memory reused, the next packet overwrites someone else’s data. The worker’s idleness proved nothing about the other two users.

Reaching quiescence takes two moves. First, close the ways in: remove every pointer, registration and queued request through which a part of the system can newly find the object. Second, wait until every part that already has a way to the object has finished with it, and obtain evidence of that from each one. The moment both are done is the quiescent point. Closing a shop is a fair picture: lock the entrance, then wait until the last customer inside has left before switching off the lights. The picture stops in one respect: in a computer some customers are hard to see, such as a device that holds an address, a return address on a stack or a job waiting in a queue.

How it works

Four steps establish a quiescent point.

  1. List the users. Find every agent that can reach the object: threads (sequences of instructions that the operating system schedules) that call the code or hold pointers to the data; interrupt handlers, which can run at almost any moment; registered callbacks, queued jobs and timers, which will run later; saved return addresses on stacks, when the object is code; DMA controllers and other devices that were given the object’s address; and other processor cores.

  2. Unpublish. Remove every path by which an agent can newly obtain a reference to the object: clear the shared pointer, unregister the callback, cancel the queued job, stop the DMA channel or take back its descriptor (the record in memory that tells the controller where to transfer), and disable the interrupt or change the handler it runs. After this step the set of users can only shrink.

  3. Wait for each existing user, with positive evidence §19.3.5. Typical evidence is a reference count that returns to zero, where each user adds one before using the object and subtracts one afterwards (reference counting §9.22); the end of a grace period §9.21, the wait in read-copy update §9.20 (RCU) until every reading thread has passed a point at which it holds no reference to the old data; an acknowledgment from a worker; a status bit showing that the DMA channel has stopped; or, for code, a check that no stack holds a return address into it.

  4. Retire. When every listed user is accounted for, the quiescent point has been reached: free the memory or install the replacement.

Quiescence is needed only from the agents that can reach the object. A thread that never touches the buffer does not need to stop. Stopping the whole system, every thread and interrupt, reaches a quiescent point for every object at once; it is simple, but it delays all unrelated work and can make that work miss its deadlines. A precise list of users lets the rest of the system keep running.

RCU uses a related term. There, a quiescent state is a point in one thread’s execution at which that thread holds no references to the protected data, for example between two read operations. A grace period ends when every reading thread has passed through a quiescent state since the object was unpublished; at that moment the object has reached a quiescent point in the sense used here.

Worked example

Three users. A buffer has three users: a worker thread WW, an interrupt handler II and a DMA controller DD. The buffer is unpublished at time 0 milliseconds, so no user can obtain it again. WW acknowledges its last use at 2 milliseconds, II finishes its last use at 4 milliseconds, and DD reports through its status bit that its channel stopped at 7 milliseconds. The quiescent point is the latest of the three, max(2,4,7)=7\max(2, 4, 7) = 7 milliseconds. Freeing the buffer at 2 milliseconds, on seeing WW idle, would have been 7−2=57 - 2 = 5 milliseconds too early, while II and DD could still use it.

The same wait with a reference count. After unpublication the count is 3, one for each user. It drops to 2 at 2 milliseconds, to 1 at 4 milliseconds, and to 0 at 7 milliseconds, when the handler that runs on completion of the DMA transfer releases the reference held for DD. The count reaching 0 after unpublication is the evidence that the quiescent point has been reached.

An unrelated task. A fourth task UU logs temperatures and never touches the buffer, so UU need not be paused. The claim that UU never touches the buffer must come from UU’s code, from the absence of any path from UU to the buffer, and not from the fact that UU happened not to run during one observation.

The order of the two moves. Suppose WW acknowledges at 2 milliseconds but the shared pointer is cleared only at 3 milliseconds. Between 2 and 3 milliseconds, WW can pick up the pointer again, so its acknowledgment at 2 milliseconds is no longer evidence. Evidence counts only if it comes after the user can no longer obtain a new reference.

Stated precisely

Let OO be the object, let UU be the set of agents that can reach OO, and let tut_u be the moment of unpublication, from which no agent can obtain a new reference to OO. For each agent aa in UU, let eae_a be a moment no earlier than tut_u at which there is positive evidence that aa holds no reference to OO and is not using it. The quiescent point is tq=max(tu,maxa∈Uea),t_q = \max\bigl(t_u,\ \max_{a \in U} e_a\bigr), and OO may be freed or replaced at any moment t≥tqt \ge t_q.

The guarantee rests on three assumptions.

  1. A complete list. UU includes every agent with a path to OO: threads, interrupt handlers, pending callbacks and jobs, timers, devices, other cores and, when OO is code, every saved return address.

  2. Effective unpublication. After tut_u no path to OO remains, and every agent sees the removal. On a processor with several cores this needs memory ordering, for example a memory barrier §4.37, so that each core sees the cleared pointer before the evidence for that core is collected.

  3. Positive evidence. Each eae_a is an event that proves the end of use, such as a count reaching zero, an acknowledgment or a completion status. An agent that merely did not run during an observation has given no evidence.

Limits and common mistakes

A quiet moment is not a quiescent point. An idle agent can still hold a pointer and use it the next time it runs.

The wait may never end. A user that is stuck or loops for ever, or a device that never completes, keeps the quiescent point from arriving. Reaching it is a liveness property §1.12, separate from the safety of retirement. A timeout §9.34 is not evidence of quiescence: when it expires, the remaining user must first be stopped or reset, or the replacement abandoned, and the memory must not be freed on the timeout alone.

Copies of a reference are users too. An agent that copied the pointer into another structure, such as a queue or a device register, holds a reference that clearing the original pointer does not remove.

Interrupt handlers can be pending or running. Disabling an interrupt prevents new runs of its handler but does not stop a run already in progress, so wait for that run to finish.

When the object is code and its memory will hold new code, quiescence is not the last step: the instruction cache must also be invalidated, so that no processor executes stale copies of the old instructions.

Stopping the whole system to obtain quiescence is safe but costly, and it is unnecessary for agents that cannot reach the object.

Questions answered

Which threads, interrupts, callbacks, and devices must be quiescent?

Exactly those that can reach the object being replaced or freed, and no others. That set includes every thread that calls the old code or holds a pointer to the old data; every interrupt handler that can use them, including one that is running or pending; every registered callback, queued job and timer that will later run with a reference to them; every device, such as a direct memory access (DMA) controller, that was given the object’s address; every other processor core that can use them; and, when the object is code, every thread whose stack holds a return address into that code.

Each of these must first lose the ability to find the object again, by removing the pointers, registrations, queue entries and device settings that lead to it. Each must then give positive evidence that it has finished: a reference count reaching zero, an acknowledgment, a completed grace period, or a status showing that the device has stopped. Threads, interrupts and devices that cannot reach the object need not stop, but that conclusion must come from an analysis of their code and configuration, not from having seen them idle.

Related entries: Graph reachability search (§14.18), Dynamic patching and hot update (§14.33), Liveness property (§1.12), Concurrency versus parallelism (§9.1), Grace period (§9.21), Reference counting (§9.22), Direct memory access (DMA) (§4.36). Further reading: [23].