Question
How do readers avoid combining new code with old policy data?
Short answer
Readers avoid the mix by finding code and policy data through one shared pointer instead of two. The writer puts one version’s code and data into one object, finishes building it, and publishes it with a single atomic store that has release ordering. Each reader loads that pointer once, with acquire ordering, and uses only that object until it has finished. Every reader therefore sees one complete version, old or new. The writer must also keep the old object alive until every reader that may still hold it has finished.
Terms in the question
Reader: any code that uses the current version, for example a thread (one of several instruction sequences that run concurrently over the same memory) or an interrupt handler §6.12 (a function that the processor runs when a hardware event occurs) that applies the policy to each incoming event.
Code and policy data: the policy code is a program that makes decisions, such as allow or deny. The policy data is the set of values it consults, such as tables of limits. Each version’s code is written for that version’s data and assumes its layout and meaning.
Combining: one decision that uses code from one version and data from another. Neither version was written or checked for that combination.
Writer: the code that installs a new version while readers keep running.
Atomic: an atomic operation §9.8 is a memory access that every other thread observes either completely or not at all.
Release and acquire: a store with release ordering makes every memory write that the writer performed before it visible to any thread whose load with acquire ordering reads the stored value. Together they form acquire-release ordering §9.11, defined by the memory models of C and C++ since their 2011 standards [78, 247].
Full answer
Step 1: two pointers allow a mixed pair. Call the old version A and the new version B. Let the code and the data be reached through two shared pointers, code_ptr and data_ptr. The writer stores the address of the new code into code_ptr and then the address of the new data into data_ptr. A reader loads code_ptr and then data_ptr. If the reader’s first load comes before both stores and its second load after them, the reader holds the code of version A with the data of version B. Reversing the writer’s order only moves the problem: then B’s code with A’s data becomes possible. Making each load atomic prevents a torn pointer, one made of part of an old address and part of a new one. It does not prevent a mixed pair, because the mix arises between two loads. Of the four possible pairs, two are versions that exist and two are not.
Step 2: make one version one object. The writer builds a bundle, one object that holds the code, the data and a version number, in memory that no reader can reach yet. It fills every field. Then it stores the address of the bundle into one shared pointer, current, with a single atomic store with release ordering. From that store on, nothing modifies the bundle. This pattern is immutable publication §9.24.
Step 3: read once per decision. The reader loads current once, with acquire ordering, and takes both the code and the data from that bundle for the whole decision. Together with the writer’s release store, the acquire load guarantees that a reader who sees the new address also sees every field of the bundle as the writer filled it. Without that ordering, the compiler or the processor may reorder memory accesses, so a reader could see the new address and still read stale contents from the memory of the bundle. The reader must not load current a second time within the same decision: a second load can return a newer bundle and recreate the mix. A reader that follows these rules always holds a coherent snapshot §9.23, a set of values that all belong to one version.
Step 4: publication is half the problem; reclamation is the other half. When the writer publishes B, a reader that loaded A a moment earlier is still using A. If the writer frees A now, the allocator can hand the memory of A to something else, and that reader then reads foreign bytes as policy: a use-after-free §4.6. The writer must wait until no reader can still hold A. Three standard methods exist.
A grace period §9.21. Each reader marks the code from its load of
currentto its last use of the bundle as a read-side critical section. After publishing B, the writer waits until every reader that was inside such a section at that moment has left it. Readers that started later can only have seen B. This is the method of read-copy update §9.20 (RCU).Reference counting §9.22. Each bundle carries a count of its current users. A reader increments the count before use and decrements it afterwards, and the bundle is freed when its count reaches zero after it has been replaced. The load of
currentand the increment must be made safe together; otherwise the writer can free the bundle between them, and the increment writes into freed memory. A short lock around the two steps prevents this. So do hazard pointers: each reader announces the pointer it is about to use and then checks thatcurrentstill holds it, and the writer frees only bundles that no reader has announced.A quiescent point §14.34 by construction. If every reader runs to completion in the same thread as the writer, one event at a time, then no reader is in progress while the writer runs, and A can be freed at once. A single interrupt handler that reads
currentbreaks this condition.
Step 5: references that outlive the decision. A reader that saves the address of the bundle for later, for example in a callback §14.22 that a timer will run, keeps the bundle in use beyond its read-side section. A grace period does not wait for such saved addresses. The saved reference must hold its own count, or the callback must load current afresh when it runs.
Worked example
The data of version A is one 4-byte field at offset 0: the maximum number of packets per second, 100. The code of A reads offset 0 as the packet limit. The data of version B has two 4-byte fields: offset 0 holds the maximum number of bytes per second, 2,000, and offset 4 holds the maximum number of packets per second, 50. The code of B reads the packet limit from offset 4.
With two pointers, the four possible pairs are (code A, data A), (code B, data B), (code A, data B) and (code B, data A). The last two are wrong.
Code A with data B reads offset 0, which now holds 2,000, as the packet limit. It allows 2,000 packets per second: times the limit of A and times the limit of B.
Code B with data A reads offsets 4 to 7 of a data block that is only 4 bytes long, so it reads memory outside the block.
With one bundle pointer, only the first two pairs can occur.
Reclamation, with times in microseconds:
At , reader loads
current, obtains bundle A at address0x20001000and enters its read-side section.At , the writer publishes bundle B at address
0x20002000.At , reader loads
currentand obtains B.At , finishes its decision and leaves its read-side section.
The grace period starts at . It must wait for , which was inside a section at , and need not wait for , which started later. It ends at , and from then on the writer may free A. Had the writer freed A at , and had the allocator reused address 0x20001000 at , would have read unrelated bytes as the packet limit of A between and .
Follow-up questions and limits
Is the volatile qualifier of C enough? No. volatile tells the compiler that every access must really take place. It makes no access atomic, and it does not order the ordinary writes that fill the bundle before the pointer store, either for the compiler or for the processor. Since its 2011 standard, C provides atomic types, with memory_order_release for the store and memory_order_acquire for the load.
Would a lock do? Yes. If readers and the writer take the same mutex §9.5 (mutual exclusion lock) around the whole decision and the whole update, no reader can see a mix, and the writer can free A while it holds the lock, because no reader is inside a decision then. The cost is that readers wait for the writer and for one another, and an interrupt handler cannot wait for a mutex.
What about a sequence counter? In a sequence lock, the writer makes a counter odd, writes the fields, and makes the counter even again. The reader reads the counter, copies the fields, reads the counter again, and retries if the counter was odd or has changed. This gives a coherent copy of a few small values without making the writer wait. In C, however, the reader’s plain reads of fields that the writer is changing at the same moment form a data race §9.3, which is undefined behavior §4.12, and a retry afterwards does not undo it; the fields must be read with atomic operations. A sequence lock also suits copying a few values, not running code in place for a whole decision.
When is B in force? A decision that loaded A before the publication can finish after it. If the system must guarantee that no decision under A completes after a given moment, for example because the policy of A was withdrawn for a security reason, the writer may report B as active only after the grace period has ended.
This answer covers policy code that an interpreter §2.13 reads as data. If the code is native machine code, publishing it also needs instruction-cache maintenance on processors that have an instruction cache, so that the processor does not run stale instructions from that cache.
Fullest treatment: Coherent snapshot (§9.23).
Related entries: Immutable publication (§9.24), Read-copy update (RCU) (§9.20), Data race (§9.3), Object lifetime (§4.1), Grace period (§9.21), Reference counting (§9.22), Acquire-release ordering (§9.11), Use-after-free (§4.6), Instruction-cache coherence and executable-code publication (§19.5.3). Further reading: [78, 247].