Linux Internals

Linux RCU Explained: Read-Copy-Update, Grace Periods and Performance

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Most kernel data is read far more often than it is written. Routing tables are consulted for every packet but change rarely; the directory entry cache is walked on every file open but updated only when files are created, renamed or removed. Protecting such data with ordinary locks wastes enormous effort, because readers block each other or bounce shared cache lines between CPUs. Read-Copy-Update (RCU) is the Linux kernel's answer: a synchronization mechanism where readers take no locks at all, and writers pay the cost instead. It is one of the main reasons Linux scales well on machines with many CPUs, and it quietly affects the behaviour of every server, including yours.

Why ordinary locks hurt readers

A spinlock or mutex serialises access, so readers wait for each other even though they do not conflict. A reader-writer lock allows parallel readers, but every reader still writes to the lock's counter. On a multi-core system that counter's cache line must move between CPUs on every acquisition, and at high rates this coherence traffic becomes the bottleneck. The fastest possible read path is one that writes to no shared memory at all, and that is what RCU provides.

How RCU works

Readers

A reader marks its critical section with rcu_read_lock() and rcu_read_unlock(), and fetches protected pointers with rcu_dereference(). On a non-preemptible kernel these markers compile to almost nothing; on a preemptible kernel they adjust a per-task counter. There is no atomic instruction, no shared write and no waiting. The only rule is that a reader must not block inside the section (except in the special SRCU variant).

Writers: copy, publish, wait, free

A writer that wants to change an RCU-protected structure does not modify it in place:

  1. Copy: allocate a new version and fill it in, based on the old one.
  2. Publish: swap the pointer with rcu_assign_pointer(), which includes the memory barrier needed so that readers see a fully initialised object.
  3. Wait: old readers may still hold a reference to the previous version, so the writer waits for a grace period, either synchronously with synchronize_rcu() or asynchronously by registering a callback with call_rcu() or kfree_rcu().
  4. Free: once the grace period ends, no reader can still see the old version, and it is reclaimed.

Writers still need their own lock among themselves; RCU only removes the cost from readers.

Grace periods and quiescent states

A grace period is any time interval long enough that every reader that was running when it started has finished. RCU does not track individual readers. Instead it watches for quiescent states, points at which a CPU is known not to be inside a read-side critical section: a context switch, running in user space, or sitting idle. Once every CPU has passed through a quiescent state, the grace period is over.

Linux implements this with Tree RCU, which arranges CPUs in a hierarchy so that reporting scales to thousands of cores. A kernel thread such as rcu_preempt or rcu_sched drives grace periods, and the RCU softirq runs completed callbacks. Grace periods typically last a few milliseconds; expedited grace periods (synchronize_rcu_expedited()) finish faster by sending IPIs to CPUs, at a higher cost to everyone else.

Where RCU shows up

RCU protects the network routing and neighbour tables, the list of network devices, netfilter rulesets, the file descriptor table of each process, and path lookup through the dentry cache, known as RCU-walk. It also underpins module unloading and many driver lists. When your server opens files or forwards packets at high rates without lock contention, RCU is a large part of why.

How this affects your VPS

You rarely tune RCU directly, but it is visible in several places:

grep RCU /proc/softirqs           # RCU callback processing per CPU
ps -eo pid,comm | grep -E 'rcu'   # rcu_preempt, rcuog, rcuop threads
dmesg | grep -i rcu               # boot configuration and stall warnings
cat /sys/module/rcupdate/parameters/rcu_cpu_stall_timeout

The most important signal is an RCU stall warning in the kernel log, such as "rcu: INFO: rcu_preempt detected stalls on CPUs/tasks". It means a CPU has not reported a quiescent state for longer than the stall timeout, 21 seconds by default. On bare metal this usually points to a kernel bug or a driver spinning with interrupts disabled. On a virtual machine there is a common extra cause: the hypervisor did not run the vCPU for a long time, so the guest could not reach a quiescent state. Heavy CPU steal, a host under memory pressure, or a paused and resumed VM can all produce stalls. Check steal with vmstat 1 and review our article on the Linux CFS and EEVDF schedulers for how steal is measured.

Stalls also delay grace periods, so memory freed through call_rcu() piles up. Workloads that create and destroy many objects, for example containers churning network namespaces, can see memory usage climb until grace periods catch up.

A few boot parameters are worth knowing:

  • rcu_nocbs=1-3 offloads callback processing from those CPUs to rcuo kernel threads, useful when you isolate CPUs for latency-sensitive work.
  • rcupdate.rcu_expedited=1 makes normal grace periods expedited, reducing latency at the price of extra IPIs; it is rarely needed on servers.
  • Kernels built with CONFIG_RCU_LAZY batch callbacks to save power, which is aimed mainly at battery devices.

If you see repeated stalls on a VPS while your own workload looks healthy, contact your provider with timestamps from dmesg and steal measurements. Plans with less contended vCPUs, listed on our pricing page, reduce the chance of long vCPU preemption in the first place.

FAQ

Does RCU make writes slower?

Writers pay for copying the data and waiting for a grace period, so individual updates are more expensive than with a simple lock. That is the intended trade-off: RCU is used where reads vastly outnumber writes, and the total cost across the system drops sharply.

Are RCU stall warnings dangerous?

They are diagnostic, not fatal, but they should not be ignored. A stall means some CPU went a long time without a quiescent state, which can delay memory reclamation and indicates either a kernel problem or, on a VPS, severe vCPU preemption by the host.

Can applications use RCU in user space?

Yes. The liburcu library implements several user-space RCU flavours with the same read-copy-update model. It is used by projects such as LTTng and Knot DNS for read-mostly data that must be accessed concurrently with minimal overhead.

Related guides