Skip to content

rcu_read_lock()

Purpose

rcu_read_lock() marks the beginning of an RCU read-side critical section.

Code inside this section may safely access RCU-protected objects obtained through rcu_dereference().


Prototype

void rcu_read_lock(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_lock() does not acquire a traditional lock.

It does not provide mutual exclusion.

Instead, it tells the RCU subsystem:

This execution context may now hold references
to RCU-protected objects.

The object must not be reclaimed until the reader exits the read-side critical section.


Important Rules

  • Always pair with rcu_read_unlock().
  • Keep the read-side critical section short.
  • Use rcu_dereference() to load RCU-protected pointers.
  • Do not assume it protects data consistency.
  • Do not use it as a replacement for a mutex.

Common Mistake

Incorrect:

rcu_read_lock();

obj = global_ptr;

rcu_read_unlock();

The pointer is loaded without rcu_dereference().

Correct:

rcu_read_lock();

obj = rcu_dereference(global_ptr);

rcu_read_unlock();