RCU¶
Overview¶
Read-Copy-Update (RCU) is a synchronization mechanism optimized for read-mostly workloads.
Unlike traditional locking primitives, RCU allows readers to access shared objects without acquiring locks.
RCU focuses on safe object publication and object lifetime management.
It is widely used throughout the Linux kernel in performance-critical read paths.
Motivation¶
Traditional synchronization mechanisms focus on protecting shared data.
Examples:
These mechanisms ensure data consistency.
However, another problem exists:
Consider:
A writer may replace:
while readers are still accessing:
The challenge is determining when it is safe to free the old object.
Core Idea¶
RCU separates object updates into three phases:
Publication¶
Writers publish a new object.
Conceptually:
Existing readers may still access the old object.
Grace Period¶
A grace period allows existing readers to complete.
During this period:
while:
Reclamation¶
After the grace period:
the old object can be safely reclaimed.
Reader Side¶
Readers enter an RCU read-side critical section.
Typical pattern:
Readers do not block each other.
Readers do not acquire traditional locks.
This provides extremely low reader overhead.
Writer Side¶
Writers allocate a new object.
Example:
Typical flow:
The old object remains valid until the grace period completes.
Key RCU APIs¶
rcu_read_lock()¶
Marks the beginning of a read-side critical section.
rcu_read_unlock()¶
Marks the end of a read-side critical section.
rcu_dereference()¶
Safely obtains a published pointer.
Provides the required memory-ordering guarantees for readers.
rcu_assign_pointer()¶
Publishes a new object.
Provides the required memory-ordering guarantees for writers.
synchronize_rcu()¶
Waits for a grace period to complete.
After it returns:
call_rcu()¶
Schedules deferred reclamation.
The callback executes after the grace period completes.
This avoids blocking the writer.
synchronize_rcu() vs call_rcu()¶
synchronize_rcu()¶
Example:
call_rcu()¶
Example:
The callback performs reclamation later.
Why Not Use RWLock?¶
RWLock allows multiple readers.
However:
Every reader performs synchronization operations.
Reader overhead remains.
Why Not Use Seqlock?¶
Seqlock provides:
but does not solve:
Example:
remain unsafe with seqlock alone.
Quiescent State¶
A quiescent state (QS) represents a point where a CPU can no longer hold an old reader from the beginning of the current grace period.
Conceptually:
Once every relevant CPU reaches a quiescent state:
Reader Counting vs Progress Tracking¶
Educational Mini RCU:
Linux RCU:
Linux focuses on CPU progress rather than maintaining a global reader counter.
Tree RCU¶
Modern systems may contain:
Tracking every CPU individually becomes expensive.
Linux aggregates CPU state hierarchically.
Conceptually:
This design is known as Tree RCU.
Tree RCU enables scalable grace period detection on large systems.
Typical Use Cases¶
Routing Tables¶
Device Lookup Tables¶
Process and File Tables¶
Read-Mostly Configuration Data¶
Comparison¶
| Primitive | Main Goal |
|---|---|
| Mutex | Mutual exclusion |
| RWLock | Multiple readers |
| Seqlock | Consistent snapshot |
| RCU | Object lifetime management |
Summary¶
RCU is a synchronization mechanism designed for read-mostly workloads.
Its primary goal is safe object lifetime management with minimal reader overhead.
RCU achieves this through:
Linux extends this design using:
to provide scalable synchronization on modern multi-core systems.