rcu.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH] rcu: Make 'rcu_assign_pointer(p, v)' of type 'typeof(p)'
@ 2019-05-23 13:32 Andrea Parri
  2019-05-23 13:50 ` Paul E. McKenney
  0 siblings, 1 reply; 6+ messages in thread
From: Andrea Parri @ 2019-05-23 13:32 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrea Parri, Paul E. McKenney, Josh Triplett, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Joel Fernandes, rcu,
	Peter Zijlstra, Will Deacon, Mark Rutland

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@amarulasolutions.com>
Cc: "Paul E. McKenney" <paulmck@linux.ibm.com>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Lai Jiangshan <jiangshanlai@gmail.com>
Cc: Joel Fernandes <joel@joelfernandes.org>
Cc: rcu@vger.kernel.org
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
---
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...)

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().)

For a related discussion, please see:

  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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH] rcu: Make 'rcu_assign_pointer(p, v)' of type 'typeof(p)'
  2019-05-23 13:32 [RFC PATCH] rcu: Make 'rcu_assign_pointer(p, v)' of type 'typeof(p)' Andrea Parri
@ 2019-05-23 13:50 ` Paul E. McKenney
  2019-05-23 14:19   ` Mark Rutland
  2019-05-23 14:53   ` Andrea Parri
  0 siblings, 2 replies; 6+ messages in thread
From: Paul E. McKenney @ 2019-05-23 13:50 UTC (permalink / raw)
  To: Andrea Parri
  Cc: linux-kernel, Josh Triplett, Steven Rostedt, Mathieu Desnoyers,
	Lai Jiangshan, Joel Fernandes, rcu, Peter Zijlstra, Will Deacon,
	Mark Rutland

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@amarulasolutions.com>
> Cc: "Paul E. McKenney" <paulmck@linux.ibm.com>
> Cc: Josh Triplett <josh@joshtriplett.org>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> Cc: Lai Jiangshan <jiangshanlai@gmail.com>
> Cc: Joel Fernandes <joel@joelfernandes.org>
> Cc: rcu@vger.kernel.org
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Will Deacon <will.deacon@arm.com>
> Cc: Mark Rutland <mark.rutland@arm.com>
> ---
> 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:
> 
>   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
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH] rcu: Make 'rcu_assign_pointer(p, v)' of type 'typeof(p)'
  2019-05-23 13:50 ` Paul E. McKenney
@ 2019-05-23 14:19   ` Mark Rutland
  2019-05-23 14:54     ` Andrea Parri
  2019-05-23 15:23     ` Paul E. McKenney
  2019-05-23 14:53   ` Andrea Parri
  1 sibling, 2 replies; 6+ messages in thread
From: Mark Rutland @ 2019-05-23 14:19 UTC (permalink / raw)
  To: Paul E. McKenney
  Cc: Andrea Parri, linux-kernel, Josh Triplett, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Joel Fernandes, rcu,
	Peter Zijlstra, Will Deacon

On Thu, May 23, 2019 at 06:50:13AM -0700, Paul E. McKenney wrote:
> 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@amarulasolutions.com>
> > Cc: "Paul E. McKenney" <paulmck@linux.ibm.com>
> > Cc: Josh Triplett <josh@joshtriplett.org>
> > Cc: Steven Rostedt <rostedt@goodmis.org>
> > Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> > Cc: Lai Jiangshan <jiangshanlai@gmail.com>
> > Cc: Joel Fernandes <joel@joelfernandes.org>
> > Cc: rcu@vger.kernel.org
> > Cc: Peter Zijlstra <peterz@infradead.org>
> > Cc: Will Deacon <will.deacon@arm.com>
> > Cc: Mark Rutland <mark.rutland@arm.com>
> > ---
> > 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.

FWIW, I completely agree, and for similar reasons I'd say we should do
the same to WRITE_ONCE(), where this 'cool feature' has been inherited
from.

For WRITE_ONCE() there's at least one user that needs to be cleaned up
first (relying on non-portable implementation detaisl of atomic*_set()),
but I suspect rcu_assign_pointer() isn't used as much as a building
block for low-level macros.

Thanks,
Mark.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH] rcu: Make 'rcu_assign_pointer(p, v)' of type 'typeof(p)'
  2019-05-23 13:50 ` Paul E. McKenney
  2019-05-23 14:19   ` Mark Rutland
@ 2019-05-23 14:53   ` Andrea Parri
  1 sibling, 0 replies; 6+ messages in thread
From: Andrea Parri @ 2019-05-23 14:53 UTC (permalink / raw)
  To: Paul E. McKenney
  Cc: linux-kernel, Josh Triplett, Steven Rostedt, Mathieu Desnoyers,
	Lai Jiangshan, Joel Fernandes, rcu, Peter Zijlstra, Will Deacon,
	Mark Rutland

> > 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.

Thanks for the confirmation, Paul; I'll prepare the new patch shortly...

  Andrea

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH] rcu: Make 'rcu_assign_pointer(p, v)' of type 'typeof(p)'
  2019-05-23 14:19   ` Mark Rutland
@ 2019-05-23 14:54     ` Andrea Parri
  2019-05-23 15:23     ` Paul E. McKenney
  1 sibling, 0 replies; 6+ messages in thread
From: Andrea Parri @ 2019-05-23 14:54 UTC (permalink / raw)
  To: Mark Rutland
  Cc: Paul E. McKenney, linux-kernel, Josh Triplett, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Joel Fernandes, rcu,
	Peter Zijlstra, Will Deacon

> > > 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.
> 
> FWIW, I completely agree, and for similar reasons I'd say we should do
> the same to WRITE_ONCE(), where this 'cool feature' has been inherited
> from.
> 
> For WRITE_ONCE() there's at least one user that needs to be cleaned up
> first (relying on non-portable implementation detaisl of atomic*_set()),
> but I suspect rcu_assign_pointer() isn't used as much as a building
> block for low-level macros.

Thanks for the confirmation, Mark.

I can look at the WRITE_ONCE() issues (user and implementation); it will
probably be a separate patchset...

Thanks,
  Andrea

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC PATCH] rcu: Make 'rcu_assign_pointer(p, v)' of type 'typeof(p)'
  2019-05-23 14:19   ` Mark Rutland
  2019-05-23 14:54     ` Andrea Parri
@ 2019-05-23 15:23     ` Paul E. McKenney
  1 sibling, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2019-05-23 15:23 UTC (permalink / raw)
  To: Mark Rutland
  Cc: Andrea Parri, linux-kernel, Josh Triplett, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Joel Fernandes, rcu,
	Peter Zijlstra, Will Deacon

On Thu, May 23, 2019 at 03:19:19PM +0100, Mark Rutland wrote:
> On Thu, May 23, 2019 at 06:50:13AM -0700, Paul E. McKenney wrote:
> > 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@amarulasolutions.com>
> > > Cc: "Paul E. McKenney" <paulmck@linux.ibm.com>
> > > Cc: Josh Triplett <josh@joshtriplett.org>
> > > Cc: Steven Rostedt <rostedt@goodmis.org>
> > > Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> > > Cc: Lai Jiangshan <jiangshanlai@gmail.com>
> > > Cc: Joel Fernandes <joel@joelfernandes.org>
> > > Cc: rcu@vger.kernel.org
> > > Cc: Peter Zijlstra <peterz@infradead.org>
> > > Cc: Will Deacon <will.deacon@arm.com>
> > > Cc: Mark Rutland <mark.rutland@arm.com>
> > > ---
> > > 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.
> 
> FWIW, I completely agree, and for similar reasons I'd say we should do
> the same to WRITE_ONCE(), where this 'cool feature' has been inherited
> from.
> 
> For WRITE_ONCE() there's at least one user that needs to be cleaned up
> first (relying on non-portable implementation detaisl of atomic*_set()),
> but I suspect rcu_assign_pointer() isn't used as much as a building
> block for low-level macros.

Agreed, for rcu_assign_pointer(), there were only a couple, and I checked
them as well.  Doesn't mean I didn't miss something, of course!

I also got an offlist report of rcu_assign_pointer() not working for
pointers to incomplete structures.  Which can be fixed by removing
the RCU_INITIALIZER() from the second argument of the smp_store_release().
Which destroys sparse's ability to check for __rcu.

One approach would be to have a separate rcu_assign_pointer_opaque()
for opaque pointers, and people would just ignore the sparse warnings.

Other suggestions?

							Thanx, Paul

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-05-23 15:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-23 13:32 [RFC PATCH] rcu: Make 'rcu_assign_pointer(p, v)' of type 'typeof(p)' Andrea Parri
2019-05-23 13:50 ` Paul E. McKenney
2019-05-23 14:19   ` Mark Rutland
2019-05-23 14:54     ` Andrea Parri
2019-05-23 15:23     ` Paul E. McKenney
2019-05-23 14:53   ` Andrea Parri

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).