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

rcu: Create an unrcu_pointer() to remove __rcu from a pointer

The xchg() and cmpxchg() functions are sometimes used to carry out RCU
updates. Unfortunately, this can result in sparse warnings for both
the old-value and new-value arguments, as well as for the return value.
The arguments can be dealt with using RCU_INITIALIZER():

old_p = xchg(&p, RCU_INITIALIZER(new_p));

But a sparse warning still remains due to assigning the __rcu pointer
returned from xchg to the (most likely) non-__rcu pointer old_p.

This commit therefore provides an unrcu_pointer() macro that strips
the __rcu. This macro can be used as follows:

old_p = unrcu_pointer(xchg(&p, RCU_INITIALIZER(new_p)));

Reported-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210624160609.292325-2-toke@redhat.com

authored by

Paul E. McKenney and committed by
Daniel Borkmann
b9964ce7 0bc919d3

+14
+14
include/linux/rcupdate.h
··· 363 363 #define rcu_check_sparse(p, space) 364 364 #endif /* #else #ifdef __CHECKER__ */ 365 365 366 + /** 367 + * unrcu_pointer - mark a pointer as not being RCU protected 368 + * @p: pointer needing to lose its __rcu property 369 + * 370 + * Converts @p from an __rcu pointer to a __kernel pointer. 371 + * This allows an __rcu pointer to be used with xchg() and friends. 372 + */ 373 + #define unrcu_pointer(p) \ 374 + ({ \ 375 + typeof(*p) *_________p1 = (typeof(*p) *__force)(p); \ 376 + rcu_check_sparse(p, __rcu); \ 377 + ((typeof(*p) __force __kernel *)(_________p1)); \ 378 + }) 379 + 366 380 #define __rcu_access_pointer(p, space) \ 367 381 ({ \ 368 382 typeof(*p) *_________p1 = (typeof(*p) *__force)READ_ONCE(p); \