Day64 - RCU Fundamentals and Grace Period Concept¶
Objective¶
Understand the motivation behind Read-Copy-Update (RCU) and learn how grace periods solve object lifetime problems in lockless read-side synchronization.
This lab series demonstrates:
- Use-after-free (UAF) issues
- Copy-and-swap update patterns
- Deferred reclamation
- Grace period concepts
- A minimal userspace RCU implementation
Background¶
Seqlock allows readers to access shared data without acquiring locks.
However, seqlock only guarantees data consistency.
It does not guarantee object lifetime.
Example:
The reader may access reclaimed memory.
This problem motivates the need for RCU.
Lab1 - Use-After-Free Demonstration¶
Goal¶
Demonstrate that a reader may access an object after it has been freed.
Design¶
Reader:
Writer:
Expected Result¶
Possible outcomes:
- Valid values
- Corrupted values
- Segmentation fault
All outcomes are valid because the behavior is undefined.
Observation¶
A reader may still hold a pointer to memory that has already been reclaimed.
Lab2 - Bad Copy-and-Swap¶
Goal¶
Show that copy-and-swap alone does not solve object lifetime issues.
Design¶
Writer:
Reader:
Expected Result¶
The reader may still access the old object after it has been freed.
Observation¶
Publishing a new object does not make the old object safe to reclaim.
Lab3 - Mini RCU¶
Goal¶
Implement a minimal userspace RCU model.
Reader Tracking¶
Global reader count:
Reader API:
Synchronization¶
Writer waits until all readers leave:
Observation¶
The old object is reclaimed only after all readers have exited.
Lab4 - Grace Period Visualization¶
Goal¶
Visualize grace period behavior using multiple readers.
Test Setup¶
Readers:
| Reader | Hold Time |
|---|---|
| Reader-1 | 1 second |
| Reader-2 | 2 seconds |
| Reader-3 | 3 seconds |
Writer:
Example Output¶
[WRITER] publish new object
[WRITER] wait readers=2
[RCU] reader count changes: 2 -> 1
[RCU] reader count changes: 1 -> 0
[WRITER] free old object
Observation¶
The writer cannot reclaim the old object until the last active reader exits.
This waiting interval is the grace period.
Key Concepts¶
Object Publication¶
Updating the shared pointer:
This operation makes the new object visible to future readers.
Object Reclamation¶
Freeing the old object:
This operation must be delayed until all active readers have completed.
Grace Period¶
Definition:
The time required for all pre-existing readers to exit their read-side critical sections.
Only after the grace period ends may the old object be safely reclaimed.
Comparison¶
| Mechanism | Consistency | Lifetime Protection | Reader Retry |
|---|---|---|---|
| Mutex | Yes | Yes | No |
| RWLock | Yes | Yes | No |
| Seqlock | Yes | No | Yes |
| RCU | No (latest data not guaranteed) | Yes | No |
Summary¶
This lab series introduced the core ideas behind RCU.
Key lessons:
- Data consistency and object lifetime are different problems.
- Copy-and-swap does not automatically solve use-after-free issues.
- RCU separates publication from reclamation.
- Grace periods allow readers to safely access old objects.
- Deferred reclamation is the foundation of RCU.
- Readers may observe old data, but they must never observe freed data.
The minimal userspace implementation provides a conceptual model for understanding Linux kernel RCU mechanisms.