call_rcu()¶
Purpose¶
call_rcu() schedules deferred reclamation of an RCU-protected object.
Instead of blocking the writer until a grace period completes, call_rcu() registers a callback that will execute after the grace period.
This allows the writer to continue immediately.
Prototype¶
Header¶
Parameters¶
head¶
Pointer to an embedded struct rcu_head.
Example:
func¶
Callback executed after the grace period completes.
Example:
Usage Pattern¶
old = rcu_dereference_protected(g_obj, 1);
rcu_assign_pointer(g_obj, new);
call_rcu(&old->rcu, my_free_callback);
The writer returns immediately.
Object reclamation occurs later.
Callback Example¶
static void my_free_callback(struct rcu_head *head)
{
struct my_object *obj;
obj = container_of(head,
struct my_object,
rcu);
kfree(obj);
}
Concept¶
RCU updates consist of three phases:
With:
the grace period and reclamation occur asynchronously.
Update Flow¶
Allocate New Object
↓
Modify New Object
↓
Publish New Object
↓
call_rcu()
↓
Writer Continues
↓
Grace Period Completes
↓
Callback Executes
↓
Old Object Reclaimed
Why Use call_rcu()?¶
The writer does not need to wait.
Compared with:
which performs:
call_rcu() performs:
Typical Use Case¶
struct my_object *old;
struct my_object *new;
new = kmalloc(sizeof(*new), GFP_KERNEL);
old = rcu_dereference_protected(g_obj, 1);
*new = *old;
new->value = 123;
rcu_assign_pointer(g_obj, new);
call_rcu(&old->rcu, my_free_callback);
Embedded rcu_head¶
RCU callbacks require an embedded:
inside the object.
Example:
This allows the callback to recover the parent object:
and reclaim memory.
Advantages¶
Non-Blocking Writers¶
The update path remains short:
Better Scalability¶
Frequent updates do not stall waiting for grace periods.
Natural Deferred Reclamation¶
Old objects are automatically reclaimed when safe.
Disadvantages¶
More Complex¶
A callback function is required.
Delayed Reclamation¶
Memory is not released immediately.
Objects remain alive until:
Additional Memory Usage¶
Large numbers of pending callbacks may temporarily increase memory consumption.
call_rcu() vs synchronize_rcu()¶
synchronize_rcu()¶
Example:
call_rcu()¶
Example:
Common Mistake¶
Incorrect:
The callback is scheduled for the wrong object.
The old object should be reclaimed:
Common Mistake¶
Incorrect:
The object is freed twice.
The callback is responsible for reclamation.
Correct:
and:
must only occur inside the callback.