Definition
Read-copy update (RCU) is a synchronization mechanism for shared data that is read far more often than it is changed. Readers take no lock and never wait for an updater. An updater reads the current version, copies it, changes the copy and publishes the copy by replacing one pointer; these steps give RCU its name. The updater frees the old version only after a grace period §9.21: a wait that ends once every reader that might still hold the old version has finished. The Linux kernel uses RCU widely.
The idea
Take a table of network routes that the operating system consults for every incoming packet and that an administrator changes a few times a day. Many processors read the table at the same moment. A mutex §9.5, a lock that lets only one thread (one of several instruction sequences sharing one memory) proceed at a time, would make every packet wait its turn for a table that almost never changes. A reader-writer lock lets readers proceed together, but every reader still writes the lock’s shared counter when it enters and when it leaves. On a machine with many processors, those writes to one memory location become the bottleneck, although the table itself is not changing.
RCU puts the cost on the rare updater instead. The updater never changes data that a reader may be looking at. It prepares a new version on the side and then switches one pointer, so every reader sees either the complete old version or the complete new one. The difficulty is the old version. After the switch, new readers cannot find the old version, but a reader that loaded the pointer just before the switch may still be using it. RCU therefore splits an update into two phases. Removal makes the old version unreachable for new readers. Reclamation frees its memory later, once no earlier reader can still be using it.
A restaurant that changes its menu works the same way. The host puts the new menu at the door, so guests who arrive from then on get the new menu. Guests already seated keep the old menu until they have ordered, and the old menus are collected only after every guest who was seated before the change has ordered. The picture stops in one respect. The host can see who holds an old menu. RCU does not record which reader holds which version, so it waits for every reader that started before the switch, including readers that never looked at this data.
How it works
The Linux kernel names the operations as follows, and the user-space library liburcu uses the same names.
A read-side critical section §9.4 is the stretch of reader code between
rcu_read_lock()andrcu_read_unlock(). Unlike a region protected by a mutex, it does not keep other threads out: many readers and an updater can run at the same time. What it excludes is the freeing of any data that the reader reached inside it.rcu_dereference(p)loads the protected pointerpinside a read-side critical section. It guarantees that the reader sees the fields of the object as the updater initialized them before publishing the object.rcu_assign_pointer(p, v)publishes the new objectvby storing its address intopwith release ordering: every write the updater made before this store becomes visible to readers no later than the new address does (see the acquire-release ordering §9.11 entry).synchronize_rcu()blocks the caller until a grace period has passed, that is, until every read-side critical section that had begun before the call has ended.call_rcu()registers a callback §14.22, a function that RCU runs after a grace period, so the updater does not have to block.
A reader follows four steps.
Call
rcu_read_lock().Load the shared pointer once with
rcu_dereferenceand keep the result in a local variable.Read the fields through that local pointer. In the basic form of RCU, the reader must not block or sleep inside the section.
Call
rcu_read_unlock(). After this call the reader must not use the pointer. A reader that needs the object for longer must take another protection inside the section before it leaves, for example a counted reference, as the reference counting §9.22 entry describes.
An updater that replaces version A with version B follows six steps.
Take a lock shared by all updaters. RCU orders readers against updaters, not updaters against each other.
Allocate B, copy A into B and change B. No reader can see B yet.
Call
rcu_assign_pointerto make the shared pointer point to B. This step is the removal of A: a reader that starts from now on finds B.Release the updaters’ lock.
Wait for a grace period with
synchronize_rcu(), or hand a function that frees A tocall_rcu().Free A.
Readers pay almost nothing. In a kernel where a thread inside a read-side critical section cannot be switched out, and that is built without debugging options, rcu_read_lock() and rcu_read_unlock() produce no machine instructions at all; they only stop the compiler from moving memory accesses across them. The grace period entry explains how RCU then detects that all earlier readers have finished.
Worked example
A shared pointer cfg points to version A of a configuration record. The record occupies 64 bytes, and its field limit holds 100. An updater changes limit to 200 while two readers, R1 and R2, run on other processors. Times are in microseconds (s).
: R1 calls
rcu_read_lock()and loadscfg. R1 gets A.: the updater allocates B (64 bytes), copies A into B and sets B’s
limitto 200.: the updater calls
rcu_assign_pointer(cfg, B). A is removed. The updater then callssynchronize_rcu(), and the grace period starts.: R2 calls
rcu_read_lock(), loadscfg, gets B and reads 200.: R1 reads
limitthrough its pointer to A and gets 100. A is still allocated, so 100 is the correct value of version A.: R1 calls
rcu_read_unlock().From on, the grace period may end. R1 was the only reader inside a read-side critical section when the grace period started. R2 started later and found B, so the grace period does not wait for R2.
synchronize_rcu()returns, and the updater frees A.
From until A is freed, both versions exist and occupy bytes instead of 64. Holding extra versions during an update is the memory price of RCU.
Now suppose the updater frees A right after publishing B, at . At , R1 reads freed memory: a use-after-free §4.6. If the memory allocator has meanwhile given those 64 bytes to another object, R1 reads whatever that object stored there and uses it as a limit. R1 made no mistake: R1 loaded a valid pointer to a fully initialized record at . Publishing B correctly cannot repair the error, because the error concerns the lifetime of A, not the contents of B.
Stated precisely
RCU gives two guarantees.
Publication. If a reader’s
rcu_dereferencereturns the address that an updater stored withrcu_assign_pointer, the reader’s reads through that address see every write the updater made to the object before the store.Grace period. Let
synchronize_rcu()be called at time and return at time . Every read-side critical section that began before has ended before , and every memory access made inside such a section is complete before . A function handed tocall_rcu()at time runs only after such a time .
Freeing A at or after is then safe when four conditions hold.
Removal before the wait. Before , every pointer to A in shared data has been replaced, so a reader that starts after cannot find A.
Confinement. A reader uses A only inside the read-side critical section in which it obtained the pointer, and keeps no copy of the pointer after
rcu_read_unlock(), unless it took another protection, such as a counted reference, inside the section.One retirement. Exactly one updater removes and frees A; updaters exclude each other with their own lock.
Reader rules. Readers obey the rules of the RCU variant in use; in basic RCU, a reader does not block or sleep inside its section.
The argument is short. A reader that uses A at or after obtained A inside some section. If that section began before , it ended before , and by condition 2 the reader no longer uses A. If the section began after , condition 1 means it could not have found A.
Limits and common mistakes
RCU does not coordinate updaters. Two updaters that both copy version A, each change their own copy and each publish it produce a lost update: the second publication discards the first change. Updaters need their own lock, or a compare-and-swap §9.9 on the shared pointer that retries when another updater got there first.
Readers may see an old version after a new one has been published. In the worked example, R1 reads 100 at , which is s after 200 was published, and R1 and R2 see different values at the same moment. RCU promises each reader a complete version, not the newest one. A decision that must use the latest value needs a different protocol.
RCU protects one pointer dereference at a time. A reader that follows two separately published pointers can get the new version of one and the old version of the other. Fields that must match belong in one object behind one pointer, as the immutable publication §9.24 and coherent snapshot §9.23 entries explain.
Updates are expensive. Each one copies data, and each old version waits for a grace period, which in the Linux kernel typically lasts milliseconds. Frequent updates therefore keep many old versions alive at once. RCU suits data that is read often and changed rarely.
A reader that never leaves its read-side critical section, for example because it loops forever, prevents every later grace period from ending. Nothing is freed, and memory runs out. The Linux kernel detects long waits of this kind and reports them as RCU CPU stall warnings.
An atomic pointer swap alone is not RCU. A design that swaps a pointer and frees the old object after a fixed delay, or when it guesses that readers are done, has publication without a grace period. A fixed delay proves nothing, because the updater cannot know how long an interrupt §6.11 or the scheduler stopped a reader in the middle of its section.
In basic RCU a reader must not sleep inside its section. Linux offers a variant, sleepable RCU (SRCU), whose readers may sleep; its read-side operations cost more.
Questions answered
Why is pointer publication insufficient without deferred reclamation?
Pointer publication is insufficient because it only controls what a reader finds when it loads the pointer from that moment on; it does nothing for a reader that loaded the old pointer earlier and is still using the old object. Publication with release ordering guarantees that a reader who obtains the new pointer sees a fully initialized new object. It cannot tell the updater when the last earlier reader has stopped using the old object. If the updater frees the old object right after publishing, such a reader reads freed memory, a use-after-free: the memory may already hold another object, so the reader acts on wrong values or corrupts data.
Deferred reclamation closes this gap. The updater frees the old object only after a grace period, a wait that ends once every reader that started before the switch has finished. In read-copy update, readers mark where their use of protected data begins and ends, and the updater either waits for a grace period with synchronize_rcu() or asks for the free to run after one with call_rcu().
Related entries: Grace period (§9.21), Immutable publication (§9.24), Use-after-free (§4.6), Acquire-release ordering (§9.11), Coherent snapshot (§9.23). Further reading: [23].