Skip to content

rcu_read_unlock()

Purpose

rcu_read_unlock() marks the end of an RCU read-side critical section.

After this call returns, the current execution context is no longer considered an active RCU reader.


Prototype

void rcu_read_unlock(void);

#include <linux/rcupdate.h>

Usage Pattern

rcu_read_lock();

obj = rcu_dereference(global_ptr);

if (obj)
    use_object(obj);

rcu_read_unlock();

Behavior

rcu_read_unlock() informs the RCU subsystem that the current reader has completed access to RCU-protected objects.

Once all readers that existed before a grace period have exited their read-side critical sections:

Grace Period Complete

and deferred object reclamation may proceed.


Reader Lifetime

Conceptually:

rcu_read_lock()
Access RCU Object
rcu_read_unlock()

The object must remain valid throughout the entire read-side critical section.


Important Rules

  • Always pair with rcu_read_lock().
  • Do not access RCU-protected objects after calling rcu_read_unlock().
  • Do not free RCU-protected objects directly from reader code.
  • Reader code should not perform object reclamation.

Common Mistake

Incorrect:

rcu_read_lock();

obj = rcu_dereference(global_ptr);

rcu_read_unlock();

printk("%d\n", obj->value);

The object is accessed after leaving the RCU read-side critical section.

Correct:

rcu_read_lock();

obj = rcu_dereference(global_ptr);

printk("%d\n", obj->value);

rcu_read_unlock();

Relationship to Grace Period

RCU writers often wait for readers to complete:

rcu_assign_pointer(global_ptr, new_obj);

synchronize_rcu();

kfree(old_obj);

The grace period cannot complete until all existing readers have executed:

rcu_read_unlock();