Re: [RFC PATCH] rcu: Make 'rcu_assign_pointer(p, v)' of type 'typeof(p)'

From: Paul E. McKenney
Date: Thu May 23 2019 - 09:55:08 EST


On Thu, May 23, 2019 at 03:32:20PM +0200, Andrea Parri wrote:
> The expression
>
> rcu_assign_pointer(p, typeof(p) v)
>
> is reported to be of type 'typeof(p)' in the documentation (c.f., e.g.,
> Documentation/RCU/whatisRCU.txt) but this is not the case: for example,
> the following snippet
>
> int **y;
> int *x;
> int *r0;
>
> ...
>
> r0 = rcu_assign_pointer(*y, x);
>
> can currently result in the compiler warning
>
> warning: assignment to âint *â from âuintptr_tâ {aka âlong unsigned intâ} makes pointer from integer without a cast [-Wint-conversion]
>
> Cast the uintptr_t value to a typeof(p) value.
>
> Signed-off-by: Andrea Parri <andrea.parri@xxxxxxxxxxxxxxxxxxxx>
> Cc: "Paul E. McKenney" <paulmck@xxxxxxxxxxxxx>
> Cc: Josh Triplett <josh@xxxxxxxxxxxxxxxx>
> Cc: Steven Rostedt <rostedt@xxxxxxxxxxx>
> Cc: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
> Cc: Lai Jiangshan <jiangshanlai@xxxxxxxxx>
> Cc: Joel Fernandes <joel@xxxxxxxxxxxxxxxxx>
> Cc: rcu@xxxxxxxxxxxxxxx
> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Cc: Will Deacon <will.deacon@xxxxxxx>
> Cc: Mark Rutland <mark.rutland@xxxxxxx>
> ---
> NOTE:
>
> TBH, I'm not sure this is 'the right patch' (hence the RFC...): in
> fact, I'm currently missing the motivations for allowing assignments
> such as the "r0 = ..." assignment above in generic code. (BTW, it's
> not currently possible to use such assignments in litmus tests...)

Given that a quick (and perhaps error-prone) search of the uses of
rcu_assign_pointer() in v5.1 didn't find a single use of the return
value, let's please instead change the documentation and implementation
to eliminate the return value.

> The usual concern is, of course, that if something is allowed (read
> 'compile!' ;/) then people will soon or later use it and they'll do
> it in all sorts of 'creative' ways, such as 'to extend dependencies
> across rcu_assign_pointer() calls' as in
>
> x = READ_ONCE(*z);
> r0 = rcu_assign_pointer(*y, x);
> WRITE_ONCE(*w, r0);
>
> Notice that using a 'do { ... } while (0)', say, would prevent such
> tricks/rvalues. (The same approach is used by smp_store_release().)

As you in fact suggest here. ;-)

Thanx, Paul

> For a related discussion, please see:
>
> 20190523083013.GA4616@andrea">https://lkml.kernel.org/r/20190523083013.GA4616@andrea
>
> Thoughts?
>
> Andrea
> ---
> include/linux/rcupdate.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> index 915460ec08722..b94ba5de78fba 100644
> --- a/include/linux/rcupdate.h
> +++ b/include/linux/rcupdate.h
> @@ -375,7 +375,7 @@ static inline void rcu_preempt_sleep_check(void) { }
> WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
> else \
> smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
> - _r_a_p__v; \
> + ((typeof(p))_r_a_p__v); \
> })
>
> /**
> --
> 2.7.4
>