rcu: Add rcu_access_pointer and rcu_dereference_protected

This patch adds variants of rcu_dereference() that handle
situations where the RCU-protected data structure cannot change,
perhaps due to our holding the update-side lock, or where the
RCU-protected pointer is only to be fetched, not dereferenced.
These are needed due to some performance concerns with using
rcu_dereference() where it is not required, aside from the need
for lockdep/sparse checking.

The new rcu_access_pointer() primitive is for the case where the
pointer is be fetch and not dereferenced. This primitive may be
used without protection, RCU or otherwise, due to the fact that
it uses ACCESS_ONCE().

The new rcu_dereference_protected() primitive is for the case
where updates are prevented, for example, due to holding the
update-side lock. This primitive does neither ACCESS_ONCE() nor
smp_read_barrier_depends(), so can only be used when updates are
somehow prevented.

Suggested-by: David Howells <dhowells@redhat.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: laijs@cn.fujitsu.com
Cc: dipankar@in.ibm.com
Cc: mathieu.desnoyers@polymtl.ca
Cc: josh@joshtriplett.org
Cc: dvhltc@us.ibm.com
Cc: niv@us.ibm.com
Cc: peterz@infradead.org
Cc: rostedt@goodmis.org
Cc: Valdis.Kletnieks@vt.edu
Cc: dhowells@redhat.com
Cc: eric.dumazet@gmail.com
LKML-Reference: <1270852752-25278-1-git-send-email-paulmck@linux.vnet.ibm.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>

authored by Paul E. McKenney and committed by Ingo Molnar b62730ba 2ba3abd8

+32
+32
include/linux/rcupdate.h
··· 209 rcu_dereference_raw(p); \ 210 }) 211 212 #else /* #ifdef CONFIG_PROVE_RCU */ 213 214 #define rcu_dereference_check(p, c) rcu_dereference_raw(p) 215 216 #endif /* #else #ifdef CONFIG_PROVE_RCU */ 217 218 /** 219 * rcu_read_lock - mark the beginning of an RCU read-side critical section.
··· 209 rcu_dereference_raw(p); \ 210 }) 211 212 + /** 213 + * rcu_dereference_protected - fetch RCU pointer when updates prevented 214 + * 215 + * Return the value of the specified RCU-protected pointer, but omit 216 + * both the smp_read_barrier_depends() and the ACCESS_ONCE(). This 217 + * is useful in cases where update-side locks prevent the value of the 218 + * pointer from changing. Please note that this primitive does -not- 219 + * prevent the compiler from repeating this reference or combining it 220 + * with other references, so it should not be used without protection 221 + * of appropriate locks. 222 + */ 223 + #define rcu_dereference_protected(p, c) \ 224 + ({ \ 225 + if (debug_lockdep_rcu_enabled() && !(c)) \ 226 + lockdep_rcu_dereference(__FILE__, __LINE__); \ 227 + (p); \ 228 + }) 229 + 230 #else /* #ifdef CONFIG_PROVE_RCU */ 231 232 #define rcu_dereference_check(p, c) rcu_dereference_raw(p) 233 + #define rcu_dereference_protected(p, c) (p) 234 235 #endif /* #else #ifdef CONFIG_PROVE_RCU */ 236 + 237 + /** 238 + * rcu_access_pointer - fetch RCU pointer with no dereferencing 239 + * 240 + * Return the value of the specified RCU-protected pointer, but omit the 241 + * smp_read_barrier_depends() and keep the ACCESS_ONCE(). This is useful 242 + * when the value of this pointer is accessed, but the pointer is not 243 + * dereferenced, for example, when testing an RCU-protected pointer against 244 + * NULL. This may also be used in cases where update-side locks prevent 245 + * the value of the pointer from changing, but rcu_dereference_protected() 246 + * is a lighter-weight primitive for this use case. 247 + */ 248 + #define rcu_access_pointer(p) ACCESS_ONCE(p) 249 250 /** 251 * rcu_read_lock - mark the beginning of an RCU read-side critical section.