Day 64 - RCU Fundamentals and Grace Period Concept¶
Today's Goal¶
Understand the fundamental concepts behind Read-Copy-Update (RCU), including object lifetime management, grace periods, and deferred reclamation.
What I Learned¶
Why Seqlock Is Not Enough¶
Seqlock ensures data consistency during concurrent access, but it does not protect object lifetime.
A reader may successfully obtain a pointer to an object while a writer concurrently frees that object, resulting in a use-after-free (UAF) condition.
Key observation:
- Seqlock solves consistency.
- Seqlock does not solve lifetime management.
Use-After-Free and Copy-and-Swap¶
A common update pattern is:
Although this pattern avoids modifying shared data in place, it is still unsafe because readers may continue accessing the old object after it has been freed.
Key observation:
- Publishing a new object does not mean the old object is safe to reclaim.
RCU Core Idea¶
RCU separates object publication from object reclamation.
Update flow:
Readers may continue accessing old objects safely during the grace period.
Key observation:
- Readers may see old data.
- Readers must never observe freed data.
Grace Period¶
A grace period is the interval required to ensure that all pre-existing readers have exited their read-side critical sections.
Only after the grace period ends can the old object be safely reclaimed.
Key observation:
- Deferred free is the core mechanism behind RCU safety.
Mini RCU Model¶
A simplified userspace RCU model was implemented using:
Reader:
Writer:
The writer waits until all active readers have exited before freeing the old object.
Labs Completed¶
Lab1 - Use-After-Free Demonstration¶
Demonstrated that accessing an object after it has been freed results in undefined behavior.
Observed corrupted values after object reclamation.
Lab2 - Bad Copy-and-Swap¶
Implemented copy-and-swap without a grace period.
Demonstrated that replacing a pointer is insufficient to guarantee safe reclamation.
Lab3 - Mini RCU¶
Implemented a minimal RCU model using a reader counter and a synchronization phase.
Demonstrated safe deferred reclamation.
Lab4 - Grace Period Visualization¶
Created multiple readers with different hold times.
Observed:
Clearly visualized the grace period concept.
Key Takeaways¶
- RCU is primarily a lifetime management mechanism.
- Readers are allowed to observe old data.
- Readers must never access reclaimed data.
- Object publication and object reclamation are separate operations.
- Grace periods enable safe deferred reclamation.
- RCU complements lockless read-side synchronization mechanisms such as seqlock.
Next Steps¶
Study Linux Kernel RCU APIs:
- rcu_read_lock()
- rcu_read_unlock()
- rcu_dereference()
- rcu_assign_pointer()
- synchronize_rcu()
Explore how the Linux kernel detects grace periods without using a simple global reader counter.
Summary¶
Day 64 introduced the core concepts behind RCU.
The most important lesson is that data consistency and object lifetime are separate problems. Seqlock addresses consistency, while RCU addresses lifetime management through grace periods and deferred reclamation.