Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

documentation: Correct doc to use rcu_dereference_protected

As there is lots of misinformation and outdated information on the
Internet about nearly all topics related to the kernel, I thought it
would be best if I based my RCU code on the guidelines of the examples
in the Documentation/ tree of the latest kernel. One thing that stuck
out when reading the whatisRCU.txt document was, "interesting how we
don't need any function to dereference rcu protected pointers when doing
updates if a lock is held. I wonder how static analyzers will work with
that." Then, a few weeks later, upon discovering sparse's __rcu support,
I ran it over my code, and lo and behold, things weren't done right.
Examining other RCU usages in the kernel reveal consistent usage of
rcu_dereference_protected, passing in lockdep_is_held as the
conditional. So, this patch adds that idiom to the documentation, so
that others ahead of me won't endure the same exercise.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Reviewed-by: Josh Triplett <josh@joshtriplett.org>

authored by

Jason A. Donenfeld and committed by
Paul E. McKenney
2c4ac34b da873def

+3 -3
+3 -3
Documentation/RCU/whatisRCU.txt
··· 364 364 }; 365 365 DEFINE_SPINLOCK(foo_mutex); 366 366 367 - struct foo *gbl_foo; 367 + struct foo __rcu *gbl_foo; 368 368 369 369 /* 370 370 * Create a new struct foo that is the same as the one currently ··· 386 386 387 387 new_fp = kmalloc(sizeof(*new_fp), GFP_KERNEL); 388 388 spin_lock(&foo_mutex); 389 - old_fp = gbl_foo; 389 + old_fp = rcu_dereference_protected(gbl_foo, lockdep_is_held(&foo_mutex)); 390 390 *new_fp = *old_fp; 391 391 new_fp->a = new_a; 392 392 rcu_assign_pointer(gbl_foo, new_fp); ··· 487 487 488 488 new_fp = kmalloc(sizeof(*new_fp), GFP_KERNEL); 489 489 spin_lock(&foo_mutex); 490 - old_fp = gbl_foo; 490 + old_fp = rcu_dereference_protected(gbl_foo, lockdep_is_held(&foo_mutex)); 491 491 *new_fp = *old_fp; 492 492 new_fp->a = new_a; 493 493 rcu_assign_pointer(gbl_foo, new_fp);