Skip to content

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:

Reader:
    ptr = g_ptr;

Writer:
    free(g_ptr);

Reader:
    use(ptr);

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:

ptr = g_ptr;
sleep(2);
use(ptr);

Writer:

sleep(1);
free(g_ptr);
g_ptr = NULL;

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:

old = g_ptr;

new = malloc(...);

new->value = 43210;

g_ptr = new;

free(old);

Reader:

ptr = g_ptr;

sleep(2);

use(ptr);

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:

atomic_int g_reader_count;

Reader API:

mini_rcu_read_lock();
mini_rcu_read_unlock();

Synchronization

Writer waits until all readers leave:

while (g_reader_count != 0)
    sched_yield();

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:

Publish new object
Wait grace period
Free old object

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:

g_ptr = new;

This operation makes the new object visible to future readers.


Object Reclamation

Freeing the old object:

free(old);

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.