Section 17.14

Coherence requires both publication and reclamation

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

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.

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.

With one bundle pointer, only the first two pairs can occur.

Reclamation, with times in microseconds:

  1. At t=0t = 0, reader R1R_1 loads current, obtains bundle A at address 0x20001000 and enters its read-side section.

  2. At t=10t = 10, the writer publishes bundle B at address 0x20002000.

  3. At t=12t = 12, reader R2R_2 loads current and obtains B.

  4. At t=25t = 25, R1R_1 finishes its decision and leaves its read-side section.

The grace period starts at t=10t = 10. It must wait for R1R_1, which was inside a section at t=10t = 10, and need not wait for R2R_2, which started later. It ends at t=25t = 25, and from then on the writer may free A. Had the writer freed A at t=10t = 10, and had the allocator reused address 0x20001000 at t=15t = 15, R1R_1 would have read unrelated bytes as the packet limit of A between t=15t = 15 and t=25t = 25.

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].