Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


If the object pointed at byptrhas to be modified, this must be done withrcu_assign_pointer:

struct super_duper *new_ptr = kmalloc(...);

new_ptr->meaning = xyz;
new_ptr->of = 42;
new_ptr->life = 23;

rcu_assign_pointer(ptr, new_ptr);

In RCU terminology, thispublishesthe pointer, and subsequent read operations will see the new structure
instead of the old one.

If updates can come from many places in the kernel, protection against concurrent
write operations must be provided using regular synchronization primitives, for
instance, spinlocks. While RCU protects readers from writers, it does not protect
writers against writers!

What happens to the old structure once the new valuehas been published? After all readers are gone,
the kernel can get rid of the memory — but it needs to know when this is safe to do. RCU provides two
more functions for this purpose:

❑ synchronize_rcu()waits until all existing readers have finished their work. After the function
returns, it is safe to free the memory associated with the old pointer.
❑ call_rcucan be used to register a function that is called after all existing readers to a shared
resource are gone. This requires that an instance ofrcu_headis embedded — and not just acces-
sible via a pointer — into the data stucture protected by RCU:
struct super_duper {
struct rcu_head head;
int meaning, of, life;
};

The callback gets thercu_headof the object passed as parameter and can use thecontainer_of
mechanism to access the object.
kernel/rcupdate.c
void fastcall call_rcu(struct rcu_head *head,
void (*func)(struct rcu_head *rcu))

List Operations


Generic pointers are not the only objects that can beprotected by RCU. The kernel also provides stan-
dard functions that allow for protecting doubly linked lists by the RCU mechanism, and this is the most
prominent application within the kernel. Additionally, hash lists that consist ofstruct hlist_headand
struct hlist_nodepairs can also be protected by RCU.

The nice thing about list protection by RCU is that the standard list elements can still be used — it is only
necessary to invoke the RCU variants of standard functions to iterate over lists and change and delete list
elements. The names of the functions are easy to remember: Just append_rcuto the standard functions.
Free download pdf