All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
@ 2018-10-28  4:30 Joel Fernandes (Google)
  2018-10-30 22:26 ` Joel Fernandes
  0 siblings, 1 reply; 16+ messages in thread
From: Joel Fernandes (Google) @ 2018-10-28  4:30 UTC (permalink / raw)
  To: linux-kernel; +Cc: Joel Fernandes (Google), Paul E. McKenney

As per this thread [1], it seems this smp_mb isn't needed anymore:
"So the smp_mb() that I was trying to add doesn't need to be there."

So let us remove this part from the memory ordering documentation.

[1] https://lkml.org/lkml/2017/10/6/707

Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
---
 .../Tree-RCU-Memory-Ordering.html             | 32 +------------------
 1 file changed, 1 insertion(+), 31 deletions(-)

diff --git a/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html b/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
index a346ce0116eb..0fb1511763d4 100644
--- a/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
+++ b/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
@@ -77,7 +77,7 @@ The key point is that the lock-acquisition functions, including
 <tt>smp_mb__after_unlock_lock()</tt> immediately after successful
 acquisition of the lock.
 
-<p>Therefore, for any given <tt>rcu_node</tt> struction, any access
+<p>Therefore, for any given <tt>rcu_node</tt> structure, any access
 happening before one of the above lock-release functions will be seen
 by all CPUs as happening before any access happening after a later
 one of the above lock-acquisition functions.
@@ -162,36 +162,6 @@ an <tt>atomic_add_return()</tt> of zero) to detect idle CPUs.
 <tr><td>&nbsp;</td></tr>
 </table>
 
-<p>The approach must be extended to handle one final case, that
-of waking a task blocked in <tt>synchronize_rcu()</tt>.
-This task might be affinitied to a CPU that is not yet aware that
-the grace period has ended, and thus might not yet be subject to
-the grace period's memory ordering.
-Therefore, there is an <tt>smp_mb()</tt> after the return from
-<tt>wait_for_completion()</tt> in the <tt>synchronize_rcu()</tt>
-code path.
-
-<table>
-<tr><th>&nbsp;</th></tr>
-<tr><th align="left">Quick Quiz:</th></tr>
-<tr><td>
-	What?  Where???
-	I don't see any <tt>smp_mb()</tt> after the return from
-	<tt>wait_for_completion()</tt>!!!
-</td></tr>
-<tr><th align="left">Answer:</th></tr>
-<tr><td bgcolor="#ffffff"><font color="ffffff">
-	That would be because I spotted the need for that
-	<tt>smp_mb()</tt> during the creation of this documentation,
-	and it is therefore unlikely to hit mainline before v4.14.
-	Kudos to Lance Roy, Will Deacon, Peter Zijlstra, and
-	Jonathan Cameron for asking questions that sensitized me
-	to the rather elaborate sequence of events that demonstrate
-	the need for this memory barrier.
-</font></td></tr>
-<tr><td>&nbsp;</td></tr>
-</table>
-
 <p>Tree RCU's grace--period memory-ordering guarantees rely most
 heavily on the <tt>rcu_node</tt> structure's <tt>-&gt;lock</tt>
 field, so much so that it is necessary to abbreviate this pattern
-- 
2.19.1.568.g152ad8e336-goog


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-10-28  4:30 [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu Joel Fernandes (Google)
@ 2018-10-30 22:26 ` Joel Fernandes
  2018-10-30 23:43   ` Paul E. McKenney
  0 siblings, 1 reply; 16+ messages in thread
From: Joel Fernandes @ 2018-10-30 22:26 UTC (permalink / raw)
  To: paulmck, linux-kernel

Hi Paul,

On Sat, Oct 27, 2018 at 09:30:46PM -0700, Joel Fernandes (Google) wrote:
> As per this thread [1], it seems this smp_mb isn't needed anymore:
> "So the smp_mb() that I was trying to add doesn't need to be there."
> 
> So let us remove this part from the memory ordering documentation.
> 
> [1] https://lkml.org/lkml/2017/10/6/707
> 
> Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>

I was just checking about this patch. Do you feel it is correct to remove
this part from the docs? Are you satisified that a barrier isn't needed there
now? Or did I miss something?

thanks,

- Joel


> ---
>  .../Tree-RCU-Memory-Ordering.html             | 32 +------------------
>  1 file changed, 1 insertion(+), 31 deletions(-)
> 
> diff --git a/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html b/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
> index a346ce0116eb..0fb1511763d4 100644
> --- a/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
> +++ b/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
> @@ -77,7 +77,7 @@ The key point is that the lock-acquisition functions, including
>  <tt>smp_mb__after_unlock_lock()</tt> immediately after successful
>  acquisition of the lock.
>  
> -<p>Therefore, for any given <tt>rcu_node</tt> struction, any access
> +<p>Therefore, for any given <tt>rcu_node</tt> structure, any access
>  happening before one of the above lock-release functions will be seen
>  by all CPUs as happening before any access happening after a later
>  one of the above lock-acquisition functions.
> @@ -162,36 +162,6 @@ an <tt>atomic_add_return()</tt> of zero) to detect idle CPUs.
>  <tr><td>&nbsp;</td></tr>
>  </table>
>  
> -<p>The approach must be extended to handle one final case, that
> -of waking a task blocked in <tt>synchronize_rcu()</tt>.
> -This task might be affinitied to a CPU that is not yet aware that
> -the grace period has ended, and thus might not yet be subject to
> -the grace period's memory ordering.
> -Therefore, there is an <tt>smp_mb()</tt> after the return from
> -<tt>wait_for_completion()</tt> in the <tt>synchronize_rcu()</tt>
> -code path.
> -
> -<table>
> -<tr><th>&nbsp;</th></tr>
> -<tr><th align="left">Quick Quiz:</th></tr>
> -<tr><td>
> -	What?  Where???
> -	I don't see any <tt>smp_mb()</tt> after the return from
> -	<tt>wait_for_completion()</tt>!!!
> -</td></tr>
> -<tr><th align="left">Answer:</th></tr>
> -<tr><td bgcolor="#ffffff"><font color="ffffff">
> -	That would be because I spotted the need for that
> -	<tt>smp_mb()</tt> during the creation of this documentation,
> -	and it is therefore unlikely to hit mainline before v4.14.
> -	Kudos to Lance Roy, Will Deacon, Peter Zijlstra, and
> -	Jonathan Cameron for asking questions that sensitized me
> -	to the rather elaborate sequence of events that demonstrate
> -	the need for this memory barrier.
> -</font></td></tr>
> -<tr><td>&nbsp;</td></tr>
> -</table>
> -
>  <p>Tree RCU's grace--period memory-ordering guarantees rely most
>  heavily on the <tt>rcu_node</tt> structure's <tt>-&gt;lock</tt>
>  field, so much so that it is necessary to abbreviate this pattern
> -- 
> 2.19.1.568.g152ad8e336-goog
> 

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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-10-30 22:26 ` Joel Fernandes
@ 2018-10-30 23:43   ` Paul E. McKenney
  2018-10-31  1:11     ` Joel Fernandes
  0 siblings, 1 reply; 16+ messages in thread
From: Paul E. McKenney @ 2018-10-30 23:43 UTC (permalink / raw)
  To: Joel Fernandes; +Cc: linux-kernel

On Tue, Oct 30, 2018 at 03:26:49PM -0700, Joel Fernandes wrote:
> Hi Paul,
> 
> On Sat, Oct 27, 2018 at 09:30:46PM -0700, Joel Fernandes (Google) wrote:
> > As per this thread [1], it seems this smp_mb isn't needed anymore:
> > "So the smp_mb() that I was trying to add doesn't need to be there."
> > 
> > So let us remove this part from the memory ordering documentation.
> > 
> > [1] https://lkml.org/lkml/2017/10/6/707
> > 
> > Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> 
> I was just checking about this patch. Do you feel it is correct to remove
> this part from the docs? Are you satisified that a barrier isn't needed there
> now? Or did I miss something?

Apologies, it got lost in the shuffle.  I have now applied it with a
bit of rework to the commit log, thank you!

							Thanx, Paul

> thanks,
> 
> - Joel
> 
> 
> > ---
> >  .../Tree-RCU-Memory-Ordering.html             | 32 +------------------
> >  1 file changed, 1 insertion(+), 31 deletions(-)
> > 
> > diff --git a/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html b/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
> > index a346ce0116eb..0fb1511763d4 100644
> > --- a/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
> > +++ b/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
> > @@ -77,7 +77,7 @@ The key point is that the lock-acquisition functions, including
> >  <tt>smp_mb__after_unlock_lock()</tt> immediately after successful
> >  acquisition of the lock.
> >  
> > -<p>Therefore, for any given <tt>rcu_node</tt> struction, any access
> > +<p>Therefore, for any given <tt>rcu_node</tt> structure, any access
> >  happening before one of the above lock-release functions will be seen
> >  by all CPUs as happening before any access happening after a later
> >  one of the above lock-acquisition functions.
> > @@ -162,36 +162,6 @@ an <tt>atomic_add_return()</tt> of zero) to detect idle CPUs.
> >  <tr><td>&nbsp;</td></tr>
> >  </table>
> >  
> > -<p>The approach must be extended to handle one final case, that
> > -of waking a task blocked in <tt>synchronize_rcu()</tt>.
> > -This task might be affinitied to a CPU that is not yet aware that
> > -the grace period has ended, and thus might not yet be subject to
> > -the grace period's memory ordering.
> > -Therefore, there is an <tt>smp_mb()</tt> after the return from
> > -<tt>wait_for_completion()</tt> in the <tt>synchronize_rcu()</tt>
> > -code path.
> > -
> > -<table>
> > -<tr><th>&nbsp;</th></tr>
> > -<tr><th align="left">Quick Quiz:</th></tr>
> > -<tr><td>
> > -	What?  Where???
> > -	I don't see any <tt>smp_mb()</tt> after the return from
> > -	<tt>wait_for_completion()</tt>!!!
> > -</td></tr>
> > -<tr><th align="left">Answer:</th></tr>
> > -<tr><td bgcolor="#ffffff"><font color="ffffff">
> > -	That would be because I spotted the need for that
> > -	<tt>smp_mb()</tt> during the creation of this documentation,
> > -	and it is therefore unlikely to hit mainline before v4.14.
> > -	Kudos to Lance Roy, Will Deacon, Peter Zijlstra, and
> > -	Jonathan Cameron for asking questions that sensitized me
> > -	to the rather elaborate sequence of events that demonstrate
> > -	the need for this memory barrier.
> > -</font></td></tr>
> > -<tr><td>&nbsp;</td></tr>
> > -</table>
> > -
> >  <p>Tree RCU's grace--period memory-ordering guarantees rely most
> >  heavily on the <tt>rcu_node</tt> structure's <tt>-&gt;lock</tt>
> >  field, so much so that it is necessary to abbreviate this pattern
> > -- 
> > 2.19.1.568.g152ad8e336-goog
> > 
> 


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-10-30 23:43   ` Paul E. McKenney
@ 2018-10-31  1:11     ` Joel Fernandes
  2018-10-31 18:17       ` Paul E. McKenney
  0 siblings, 1 reply; 16+ messages in thread
From: Joel Fernandes @ 2018-10-31  1:11 UTC (permalink / raw)
  To: Paul E. McKenney; +Cc: linux-kernel

Hi Paul,

On Tue, Oct 30, 2018 at 04:43:36PM -0700, Paul E. McKenney wrote:
> On Tue, Oct 30, 2018 at 03:26:49PM -0700, Joel Fernandes wrote:
> > Hi Paul,
> > 
> > On Sat, Oct 27, 2018 at 09:30:46PM -0700, Joel Fernandes (Google) wrote:
> > > As per this thread [1], it seems this smp_mb isn't needed anymore:
> > > "So the smp_mb() that I was trying to add doesn't need to be there."
> > > 
> > > So let us remove this part from the memory ordering documentation.
> > > 
> > > [1] https://lkml.org/lkml/2017/10/6/707
> > > 
> > > Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> > 
> > I was just checking about this patch. Do you feel it is correct to remove
> > this part from the docs? Are you satisified that a barrier isn't needed there
> > now? Or did I miss something?
> 
> Apologies, it got lost in the shuffle.  I have now applied it with a
> bit of rework to the commit log, thank you!

No worries, thanks for taking it!

Just wanted to update you on my progress reading/correcting the docs. The
'Memory Ordering' is taking a bit of time so I paused that and I'm focusing
on finishing all the other low hanging fruit. This activity is mostly during
night hours after the baby is asleep but some times I also manage to sneak it
into the day job ;-)

BTW I do want to discuss about this smp_mb patch above with you at LPC if you
had time, even though we are removing it from the documentation. I thought
about it a few times, and I was not able to fully appreciate the need for the
barrier (that is even assuming that complete() etc did not do the right
thing).  Specifically I was wondering same thing Peter said in the above
thread I think that - if that rcu_read_unlock() triggered all the spin
locking up the tree of nodes, then why is that locking not sufficient to
prevent reads from the read-side section from bleeding out? That would
prevent the reader that just unlocked from seeing anything that happens
_after_ the synchronize_rcu.

Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
me that the RCU reader-sections are virtually extended both forward and
backward and whereever it ends, those paths do heavy-weight synchronization
that should be sufficient to prevent memory ordering issues (such as those
you mentioned in the Requierments document). That is exactly why we don't
need explicit barriers during rcu_read_unlock. If I recall I asked you why
those are not needed. So that answer made sense, but then now on going
through the 'Memory Ordering' document, I see that you mentioned there is
reliance on the locking. Is that reliance on locking necessary to maintain
ordering then?

Or did I miss the points completely? :(

----------------------
TODO list of the index file marking which ones I have finished perusing:

arrayRCU.txt			DONE
checklist.txt			DONE
listRCU.txt			DONE
lockdep.txt			DONE
lockdep-splat.txt		DONE
NMI-RCU.txt
rcu_dereference.txt
rcubarrier.txt
rculist_nulls.txt
rcuref.txt
rcu.txt
RTFP.txt			DONE
stallwarn.txt			DONE
torture.txt
UP.txt
whatisRCU.txt			DONE

Design
 - Data-Structures		DONE
 - Requirements			DONE
 - Expedited-Grace-Periods
 - Memory Ordering		next


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-10-31  1:11     ` Joel Fernandes
@ 2018-10-31 18:17       ` Paul E. McKenney
  2018-11-01  5:00         ` Joel Fernandes
  0 siblings, 1 reply; 16+ messages in thread
From: Paul E. McKenney @ 2018-10-31 18:17 UTC (permalink / raw)
  To: Joel Fernandes; +Cc: linux-kernel

On Tue, Oct 30, 2018 at 06:11:19PM -0700, Joel Fernandes wrote:
> Hi Paul,
> 
> On Tue, Oct 30, 2018 at 04:43:36PM -0700, Paul E. McKenney wrote:
> > On Tue, Oct 30, 2018 at 03:26:49PM -0700, Joel Fernandes wrote:
> > > Hi Paul,
> > > 
> > > On Sat, Oct 27, 2018 at 09:30:46PM -0700, Joel Fernandes (Google) wrote:
> > > > As per this thread [1], it seems this smp_mb isn't needed anymore:
> > > > "So the smp_mb() that I was trying to add doesn't need to be there."
> > > > 
> > > > So let us remove this part from the memory ordering documentation.
> > > > 
> > > > [1] https://lkml.org/lkml/2017/10/6/707
> > > > 
> > > > Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> > > 
> > > I was just checking about this patch. Do you feel it is correct to remove
> > > this part from the docs? Are you satisified that a barrier isn't needed there
> > > now? Or did I miss something?
> > 
> > Apologies, it got lost in the shuffle.  I have now applied it with a
> > bit of rework to the commit log, thank you!
> 
> No worries, thanks for taking it!
> 
> Just wanted to update you on my progress reading/correcting the docs. The
> 'Memory Ordering' is taking a bit of time so I paused that and I'm focusing
> on finishing all the other low hanging fruit. This activity is mostly during
> night hours after the baby is asleep but some times I also manage to sneak it
> into the day job ;-)

If there is anything I can do to make this a more sustainable task for
you, please do not keep it a secret!!!

> BTW I do want to discuss about this smp_mb patch above with you at LPC if you
> had time, even though we are removing it from the documentation. I thought
> about it a few times, and I was not able to fully appreciate the need for the
> barrier (that is even assuming that complete() etc did not do the right
> thing).  Specifically I was wondering same thing Peter said in the above
> thread I think that - if that rcu_read_unlock() triggered all the spin
> locking up the tree of nodes, then why is that locking not sufficient to
> prevent reads from the read-side section from bleeding out? That would
> prevent the reader that just unlocked from seeing anything that happens
> _after_ the synchronize_rcu.

Actually, I recall an smp_mb() being added, but am not seeing it anywhere
relevant to wait_for_completion().  So I might need to add the smp_mb()
to synchronize_rcu() and remove the patch (retaining the typo fix).  :-/

The short form answer is that anything before a grace period on any CPU
must be seen by any CPU as being before anything on any CPU after that
same grace period.  This guarantee requires a rather big hammer.

But yes, let's talk at LPC!

> Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> me that the RCU reader-sections are virtually extended both forward and
> backward and whereever it ends, those paths do heavy-weight synchronization
> that should be sufficient to prevent memory ordering issues (such as those
> you mentioned in the Requierments document). That is exactly why we don't
> need explicit barriers during rcu_read_unlock. If I recall I asked you why
> those are not needed. So that answer made sense, but then now on going
> through the 'Memory Ordering' document, I see that you mentioned there is
> reliance on the locking. Is that reliance on locking necessary to maintain
> ordering then?

There is a "network" of locking augmented by smp_mb__after_unlock_lock()
that implements the all-to-all memory ordering mentioned above.  But it
also needs to handle all the possible complete()/wait_for_completion()
races, even those assisted by hypervisor vCPU preemption.

> Or did I miss the points completely? :(

A question for the ages for both of us!  ;-)

> ----------------------
> TODO list of the index file marking which ones I have finished perusing:
> 
> arrayRCU.txt			DONE
> checklist.txt			DONE
> listRCU.txt			DONE
> lockdep.txt			DONE
> lockdep-splat.txt		DONE
> NMI-RCU.txt
> rcu_dereference.txt
> rcubarrier.txt
> rculist_nulls.txt
> rcuref.txt
> rcu.txt
> RTFP.txt			DONE
> stallwarn.txt			DONE
> torture.txt
> UP.txt
> whatisRCU.txt			DONE
> 
> Design
>  - Data-Structures		DONE
>  - Requirements			DONE
>  - Expedited-Grace-Periods
>  - Memory Ordering		next

Great progress, and again, thank you!!!

							Thanx, Paul


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-10-31 18:17       ` Paul E. McKenney
@ 2018-11-01  5:00         ` Joel Fernandes
  2018-11-01 16:13           ` Paul E. McKenney
  0 siblings, 1 reply; 16+ messages in thread
From: Joel Fernandes @ 2018-11-01  5:00 UTC (permalink / raw)
  To: Paul E. McKenney; +Cc: linux-kernel

On Wed, Oct 31, 2018 at 11:17:48AM -0700, Paul E. McKenney wrote:
> On Tue, Oct 30, 2018 at 06:11:19PM -0700, Joel Fernandes wrote:
> > Hi Paul,
> > 
> > On Tue, Oct 30, 2018 at 04:43:36PM -0700, Paul E. McKenney wrote:
> > > On Tue, Oct 30, 2018 at 03:26:49PM -0700, Joel Fernandes wrote:
> > > > Hi Paul,
> > > > 
> > > > On Sat, Oct 27, 2018 at 09:30:46PM -0700, Joel Fernandes (Google) wrote:
> > > > > As per this thread [1], it seems this smp_mb isn't needed anymore:
> > > > > "So the smp_mb() that I was trying to add doesn't need to be there."
> > > > > 
> > > > > So let us remove this part from the memory ordering documentation.
> > > > > 
> > > > > [1] https://lkml.org/lkml/2017/10/6/707
> > > > > 
> > > > > Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> > > > 
> > > > I was just checking about this patch. Do you feel it is correct to remove
> > > > this part from the docs? Are you satisified that a barrier isn't needed there
> > > > now? Or did I miss something?
> > > 
> > > Apologies, it got lost in the shuffle.  I have now applied it with a
> > > bit of rework to the commit log, thank you!
> > 
> > No worries, thanks for taking it!
> > 
> > Just wanted to update you on my progress reading/correcting the docs. The
> > 'Memory Ordering' is taking a bit of time so I paused that and I'm focusing
> > on finishing all the other low hanging fruit. This activity is mostly during
> > night hours after the baby is asleep but some times I also manage to sneak it
> > into the day job ;-)
> 
> If there is anything I can do to make this a more sustainable task for
> you, please do not keep it a secret!!!

Thanks a lot, that means a lot to me! Will do!

> > BTW I do want to discuss about this smp_mb patch above with you at LPC if you
> > had time, even though we are removing it from the documentation. I thought
> > about it a few times, and I was not able to fully appreciate the need for the
> > barrier (that is even assuming that complete() etc did not do the right
> > thing).  Specifically I was wondering same thing Peter said in the above
> > thread I think that - if that rcu_read_unlock() triggered all the spin
> > locking up the tree of nodes, then why is that locking not sufficient to
> > prevent reads from the read-side section from bleeding out? That would
> > prevent the reader that just unlocked from seeing anything that happens
> > _after_ the synchronize_rcu.
> 
> Actually, I recall an smp_mb() being added, but am not seeing it anywhere
> relevant to wait_for_completion().  So I might need to add the smp_mb()
> to synchronize_rcu() and remove the patch (retaining the typo fix).  :-/

No problem, I'm glad atleast the patch resurfaced the topic of the potential
issue :-)

> The short form answer is that anything before a grace period on any CPU
> must be seen by any CPU as being before anything on any CPU after that
> same grace period.  This guarantee requires a rather big hammer.
> 
> But yes, let's talk at LPC!

Sounds great, looking forward to discussing this.

> > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > me that the RCU reader-sections are virtually extended both forward and
> > backward and whereever it ends, those paths do heavy-weight synchronization
> > that should be sufficient to prevent memory ordering issues (such as those
> > you mentioned in the Requierments document). That is exactly why we don't
> > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > those are not needed. So that answer made sense, but then now on going
> > through the 'Memory Ordering' document, I see that you mentioned there is
> > reliance on the locking. Is that reliance on locking necessary to maintain
> > ordering then?
> 
> There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> that implements the all-to-all memory ordering mentioned above.  But it
> also needs to handle all the possible complete()/wait_for_completion()
> races, even those assisted by hypervisor vCPU preemption.

I see, so it sounds like the lock network is just a partial solution. For
some reason I thought before that complete() was even called on the CPU
executing the callback, all the CPUs would have acquired and released a lock
in the "lock network" atleast once thus ensuring the ordering (due to the
fact that the quiescent state reporting has to travel up the tree starting
from the leaves), but I think that's not necessarily true so I see your point
now.

Did you feel this will violate condition 1. or condition 2. in "Memory-Barrier
Guarantees"? Or both?
https://www.kernel.org/doc/Documentation/RCU/Design/Requirements/Requirements.html#Memory-Barrier%20Guarantees

> > ----------------------
> > TODO list of the index file marking which ones I have finished perusing:
> > 
> > arrayRCU.txt			DONE
> > checklist.txt			DONE
> > listRCU.txt			DONE
> > lockdep.txt			DONE
> > lockdep-splat.txt		DONE
> > NMI-RCU.txt
> > rcu_dereference.txt
> > rcubarrier.txt
> > rculist_nulls.txt
> > rcuref.txt
> > rcu.txt
> > RTFP.txt			DONE
> > stallwarn.txt			DONE
> > torture.txt
> > UP.txt
> > whatisRCU.txt			DONE
> > 
> > Design
> >  - Data-Structures		DONE
> >  - Requirements			DONE
> >  - Expedited-Grace-Periods
> >  - Memory Ordering		next
> 
> Great progress, and again, thank you!!!

Thanks and you're welcome!

 - Joel


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-11-01  5:00         ` Joel Fernandes
@ 2018-11-01 16:13           ` Paul E. McKenney
  2018-11-02  6:15             ` Joel Fernandes
  2018-11-03  5:12             ` Joel Fernandes
  0 siblings, 2 replies; 16+ messages in thread
From: Paul E. McKenney @ 2018-11-01 16:13 UTC (permalink / raw)
  To: Joel Fernandes; +Cc: linux-kernel

On Wed, Oct 31, 2018 at 10:00:19PM -0700, Joel Fernandes wrote:
> On Wed, Oct 31, 2018 at 11:17:48AM -0700, Paul E. McKenney wrote:
> > On Tue, Oct 30, 2018 at 06:11:19PM -0700, Joel Fernandes wrote:
> > > Hi Paul,
> > > 
> > > On Tue, Oct 30, 2018 at 04:43:36PM -0700, Paul E. McKenney wrote:
> > > > On Tue, Oct 30, 2018 at 03:26:49PM -0700, Joel Fernandes wrote:
> > > > > Hi Paul,
> > > > > 
> > > > > On Sat, Oct 27, 2018 at 09:30:46PM -0700, Joel Fernandes (Google) wrote:
> > > > > > As per this thread [1], it seems this smp_mb isn't needed anymore:
> > > > > > "So the smp_mb() that I was trying to add doesn't need to be there."
> > > > > > 
> > > > > > So let us remove this part from the memory ordering documentation.
> > > > > > 
> > > > > > [1] https://lkml.org/lkml/2017/10/6/707
> > > > > > 
> > > > > > Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> > > > > 
> > > > > I was just checking about this patch. Do you feel it is correct to remove
> > > > > this part from the docs? Are you satisified that a barrier isn't needed there
> > > > > now? Or did I miss something?
> > > > 
> > > > Apologies, it got lost in the shuffle.  I have now applied it with a
> > > > bit of rework to the commit log, thank you!
> > > 
> > > No worries, thanks for taking it!
> > > 
> > > Just wanted to update you on my progress reading/correcting the docs. The
> > > 'Memory Ordering' is taking a bit of time so I paused that and I'm focusing
> > > on finishing all the other low hanging fruit. This activity is mostly during
> > > night hours after the baby is asleep but some times I also manage to sneak it
> > > into the day job ;-)
> > 
> > If there is anything I can do to make this a more sustainable task for
> > you, please do not keep it a secret!!!
> 
> Thanks a lot, that means a lot to me! Will do!
> 
> > > BTW I do want to discuss about this smp_mb patch above with you at LPC if you
> > > had time, even though we are removing it from the documentation. I thought
> > > about it a few times, and I was not able to fully appreciate the need for the
> > > barrier (that is even assuming that complete() etc did not do the right
> > > thing).  Specifically I was wondering same thing Peter said in the above
> > > thread I think that - if that rcu_read_unlock() triggered all the spin
> > > locking up the tree of nodes, then why is that locking not sufficient to
> > > prevent reads from the read-side section from bleeding out? That would
> > > prevent the reader that just unlocked from seeing anything that happens
> > > _after_ the synchronize_rcu.
> > 
> > Actually, I recall an smp_mb() being added, but am not seeing it anywhere
> > relevant to wait_for_completion().  So I might need to add the smp_mb()
> > to synchronize_rcu() and remove the patch (retaining the typo fix).  :-/
> 
> No problem, I'm glad atleast the patch resurfaced the topic of the potential
> issue :-)

And an smp_mb() is needed in Tree RCU's __wait_rcu_gp().  This is
because wait_for_completion() might get a "fly-by" wakeup, which would
mean no ordering for code naively thinking that it was ordered after a
grace period.

> > The short form answer is that anything before a grace period on any CPU
> > must be seen by any CPU as being before anything on any CPU after that
> > same grace period.  This guarantee requires a rather big hammer.
> > 
> > But yes, let's talk at LPC!
> 
> Sounds great, looking forward to discussing this.

Would it make sense to have an RCU-implementation BoF?

> > > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > > me that the RCU reader-sections are virtually extended both forward and
> > > backward and whereever it ends, those paths do heavy-weight synchronization
> > > that should be sufficient to prevent memory ordering issues (such as those
> > > you mentioned in the Requierments document). That is exactly why we don't
> > > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > > those are not needed. So that answer made sense, but then now on going
> > > through the 'Memory Ordering' document, I see that you mentioned there is
> > > reliance on the locking. Is that reliance on locking necessary to maintain
> > > ordering then?
> > 
> > There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> > that implements the all-to-all memory ordering mentioned above.  But it
> > also needs to handle all the possible complete()/wait_for_completion()
> > races, even those assisted by hypervisor vCPU preemption.
> 
> I see, so it sounds like the lock network is just a partial solution. For
> some reason I thought before that complete() was even called on the CPU
> executing the callback, all the CPUs would have acquired and released a lock
> in the "lock network" atleast once thus ensuring the ordering (due to the
> fact that the quiescent state reporting has to travel up the tree starting
> from the leaves), but I think that's not necessarily true so I see your point
> now.

There is indeed a lock that is unconditionally acquired and released by
wait_for_completion(), but it lacks the smp_mb__after_unlock_lock() that
is required to get full-up any-to-any ordering.  And unfortunate timing
(as well as spurious wakeups) allow the interaction to have only normal
lock-release/acquire ordering, which does not suffice in all cases.

SRCU and expedited RCU grace periods handle this correctly.  Only the
normal grace periods are missing the needed barrier.  The probability of
failure is extremely low in the common case, which involves all sorts
of synchronization on the wakeup path.  It would be quite strange (but
not impossible) for the wait_for_completion() exit path to -not- to do
a full wakeup.  Plus the bug requires a reader before the grace period
to do a store to some location that post-grace-period code loads from.
Which is a very rare use case.

But it still should be fixed.  ;-)

> Did you feel this will violate condition 1. or condition 2. in "Memory-Barrier
> Guarantees"? Or both?
> https://www.kernel.org/doc/Documentation/RCU/Design/Requirements/Requirements.html#Memory-Barrier%20Guarantees

Condition 1.  There might be some strange combination of events that
could also cause it to also violate condition 2, but I am not immediately
seeing it.

> > > ----------------------
> > > TODO list of the index file marking which ones I have finished perusing:
> > > 
> > > arrayRCU.txt			DONE
> > > checklist.txt			DONE
> > > listRCU.txt			DONE
> > > lockdep.txt			DONE
> > > lockdep-splat.txt		DONE
> > > NMI-RCU.txt
> > > rcu_dereference.txt
> > > rcubarrier.txt
> > > rculist_nulls.txt
> > > rcuref.txt
> > > rcu.txt
> > > RTFP.txt			DONE
> > > stallwarn.txt			DONE
> > > torture.txt
> > > UP.txt
> > > whatisRCU.txt			DONE
> > > 
> > > Design
> > >  - Data-Structures		DONE
> > >  - Requirements			DONE
> > >  - Expedited-Grace-Periods
> > >  - Memory Ordering		next
> > 
> > Great progress, and again, thank you!!!
> 
> Thanks and you're welcome!

And here is the fix proposed above.  Thoughts?

							Thanx, Paul

------------------------------------------------------------------------

commit bf3c11b7b9789283f993d9beb80caaabc4403916
Author: Paul E. McKenney <paulmck@linux.ibm.com>
Date:   Thu Nov 1 09:05:02 2018 -0700

    rcu: Add full memory barrier in __wait_rcu_gp()
    
    RCU grace periods have extremely strong any-to-any ordering
    requirements that are met by placing full barriers in various places
    in the grace-period computation.  However, normal grace period requests
    might be subjected to a "fly-by" wakeup in which the requesting process
    doesn't actually sleep and in which the corresponding CPU is not yet
    aware that the grace period has ended.  In this case, loads in the code
    immediately following the synchronize_rcu() call might potentially see
    values before stores preceding the grace period on other CPUs.
    
    This is an unusual use case, because RCU readers normally read.  However,
    they can do writes, and if they do, we need post-grace-period reads to
    see those writes.
    
    This commit therefore adds an smp_mb() to the end of __wait_rcu_gp().
    
    Many thanks to Joel Fernandes for the series of questions leading to me
    realizing that this bug exists!
    
    Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>

diff --git a/kernel/rcu/update.c b/kernel/rcu/update.c
index 1971869c4072..74020b558216 100644
--- a/kernel/rcu/update.c
+++ b/kernel/rcu/update.c
@@ -360,6 +360,7 @@ void __wait_rcu_gp(bool checktiny, int n, call_rcu_func_t *crcu_array,
 			wait_for_completion(&rs_array[i].completion);
 		destroy_rcu_head_on_stack(&rs_array[i].head);
 	}
+	smp_mb(); /* Provide ordering in case of fly-by wakeup. */
 }
 EXPORT_SYMBOL_GPL(__wait_rcu_gp);
 


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-11-01 16:13           ` Paul E. McKenney
@ 2018-11-02  6:15             ` Joel Fernandes
  2018-11-02 20:00               ` Paul E. McKenney
  2018-11-03  5:12             ` Joel Fernandes
  1 sibling, 1 reply; 16+ messages in thread
From: Joel Fernandes @ 2018-11-02  6:15 UTC (permalink / raw)
  To: Paul E. McKenney; +Cc: linux-kernel

On Thu, Nov 01, 2018 at 09:13:07AM -0700, Paul E. McKenney wrote:
> > > > BTW I do want to discuss about this smp_mb patch above with you at LPC if you
> > > > had time, even though we are removing it from the documentation. I thought
> > > > about it a few times, and I was not able to fully appreciate the need for the
> > > > barrier (that is even assuming that complete() etc did not do the right
> > > > thing).  Specifically I was wondering same thing Peter said in the above
> > > > thread I think that - if that rcu_read_unlock() triggered all the spin
> > > > locking up the tree of nodes, then why is that locking not sufficient to
> > > > prevent reads from the read-side section from bleeding out? That would
> > > > prevent the reader that just unlocked from seeing anything that happens
> > > > _after_ the synchronize_rcu.
> > > 
> > > Actually, I recall an smp_mb() being added, but am not seeing it anywhere
> > > relevant to wait_for_completion().  So I might need to add the smp_mb()
> > > to synchronize_rcu() and remove the patch (retaining the typo fix).  :-/
> > 
> > No problem, I'm glad atleast the patch resurfaced the topic of the potential
> > issue :-)
> 
> And an smp_mb() is needed in Tree RCU's __wait_rcu_gp().  This is
> because wait_for_completion() might get a "fly-by" wakeup, which would
> mean no ordering for code naively thinking that it was ordered after a
> grace period.

Makes sense.

> > > The short form answer is that anything before a grace period on any CPU
> > > must be seen by any CPU as being before anything on any CPU after that
> > > same grace period.  This guarantee requires a rather big hammer.
> > > 
> > > But yes, let's talk at LPC!
> > 
> > Sounds great, looking forward to discussing this.
> 
> Would it make sense to have an RCU-implementation BoF?

Yes, I would very much like that. I also spoke with my colleage Daniel
Colascione and he said he would be interested too.

I think it would make sense also to combine it with other memory-ordering
topics like the memory model and rseq/cpu-opv things that Mathieu was doing
(if it makes sense to combine). But yes, I am definitely interested in an
RCU-implementation BoF session.

> > > > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > > > me that the RCU reader-sections are virtually extended both forward and
> > > > backward and whereever it ends, those paths do heavy-weight synchronization
> > > > that should be sufficient to prevent memory ordering issues (such as those
> > > > you mentioned in the Requierments document). That is exactly why we don't
> > > > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > > > those are not needed. So that answer made sense, but then now on going
> > > > through the 'Memory Ordering' document, I see that you mentioned there is
> > > > reliance on the locking. Is that reliance on locking necessary to maintain
> > > > ordering then?
> > > 
> > > There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> > > that implements the all-to-all memory ordering mentioned above.  But it
> > > also needs to handle all the possible complete()/wait_for_completion()
> > > races, even those assisted by hypervisor vCPU preemption.
> > 
> > I see, so it sounds like the lock network is just a partial solution. For
> > some reason I thought before that complete() was even called on the CPU
> > executing the callback, all the CPUs would have acquired and released a lock
> > in the "lock network" atleast once thus ensuring the ordering (due to the
> > fact that the quiescent state reporting has to travel up the tree starting
> > from the leaves), but I think that's not necessarily true so I see your point
> > now.
> 
> There is indeed a lock that is unconditionally acquired and released by
> wait_for_completion(), but it lacks the smp_mb__after_unlock_lock() that
> is required to get full-up any-to-any ordering.  And unfortunate timing
> (as well as spurious wakeups) allow the interaction to have only normal
> lock-release/acquire ordering, which does not suffice in all cases.
> 
> SRCU and expedited RCU grace periods handle this correctly.  Only the
> normal grace periods are missing the needed barrier.  The probability of
> failure is extremely low in the common case, which involves all sorts
> of synchronization on the wakeup path.  It would be quite strange (but
> not impossible) for the wait_for_completion() exit path to -not- to do
> a full wakeup.  Plus the bug requires a reader before the grace period
> to do a store to some location that post-grace-period code loads from.
> Which is a very rare use case.
> 
> But it still should be fixed.  ;-)
> 
> > Did you feel this will violate condition 1. or condition 2. in "Memory-Barrier
> > Guarantees"? Or both?
> > https://www.kernel.org/doc/Documentation/RCU/Design/Requirements/Requirements.html#Memory-Barrier%20Guarantees
> 
> Condition 1.  There might be some strange combination of events that
> could also cause it to also violate condition 2, but I am not immediately
> seeing it.

In the previous paragraph, you mentioned the bug "requires a reader before
the GP to do a store". However, condition 1 is really different - it is a
reader holding a reference to a pointer that is used *after* the
synchronize_rcu returns. So that reader's load of the pointer should have
completed by the time GP ends, otherwise the reader can look at kfree'd data.
That's different right?

For condition 2, I analyzed it below, let me know what you think:

> 							Thanx, Paul
> 
> ------------------------------------------------------------------------
> 
> commit bf3c11b7b9789283f993d9beb80caaabc4403916
> Author: Paul E. McKenney <paulmck@linux.ibm.com>
> Date:   Thu Nov 1 09:05:02 2018 -0700
> 
>     rcu: Add full memory barrier in __wait_rcu_gp()
>     
>     RCU grace periods have extremely strong any-to-any ordering
>     requirements that are met by placing full barriers in various places
>     in the grace-period computation.  However, normal grace period requests
>     might be subjected to a "fly-by" wakeup in which the requesting process
>     doesn't actually sleep and in which the corresponding CPU is not yet
>     aware that the grace period has ended.  In this case, loads in the code
>     immediately following the synchronize_rcu() call might potentially see
>     values before stores preceding the grace period on other CPUs.
>     
>     This is an unusual use case, because RCU readers normally read.  However,
>     they can do writes, and if they do, we need post-grace-period reads to
>     see those writes.
>     
>     This commit therefore adds an smp_mb() to the end of __wait_rcu_gp().
>     
>     Many thanks to Joel Fernandes for the series of questions leading to me
>     realizing that this bug exists!
>     
>     Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>
> 
> diff --git a/kernel/rcu/update.c b/kernel/rcu/update.c
> index 1971869c4072..74020b558216 100644
> --- a/kernel/rcu/update.c
> +++ b/kernel/rcu/update.c
> @@ -360,6 +360,7 @@ void __wait_rcu_gp(bool checktiny, int n, call_rcu_func_t *crcu_array,
>  			wait_for_completion(&rs_array[i].completion);
>  		destroy_rcu_head_on_stack(&rs_array[i].head);
>  	}
> +	smp_mb(); /* Provide ordering in case of fly-by wakeup. */
>  }
>  EXPORT_SYMBOL_GPL(__wait_rcu_gp);
>  

The fix looks fine to me. Thanks.

If I understand correctly the wait_for_completion() is an ACQUIRE operation,
and the complete() is a RELEASE operation aka the "MP pattern". The
ACQUIRE/RELEASE semantics allow any writes that happened before the ACQUIRE
to get ordered after it. So that would actually imply it is not strong enough
for ordering purposes during a "fly-by" wake up scenario and would be a
violation of CONDITION 2, I think (not only condition 1 as you said).  This
is because future readers may accidentally see the writes that happened
*before* the synchronize_rcu which is CONDITION 2 in the requirements:
https://goo.gl/8mrDHN  (I had to shortlink it ;))

Cheers,

- Joel

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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-11-02  6:15             ` Joel Fernandes
@ 2018-11-02 20:00               ` Paul E. McKenney
  2018-11-02 22:14                 ` Joel Fernandes
  0 siblings, 1 reply; 16+ messages in thread
From: Paul E. McKenney @ 2018-11-02 20:00 UTC (permalink / raw)
  To: Joel Fernandes; +Cc: linux-kernel

On Thu, Nov 01, 2018 at 11:15:18PM -0700, Joel Fernandes wrote:
> On Thu, Nov 01, 2018 at 09:13:07AM -0700, Paul E. McKenney wrote:
> > > > > BTW I do want to discuss about this smp_mb patch above with you at LPC if you
> > > > > had time, even though we are removing it from the documentation. I thought
> > > > > about it a few times, and I was not able to fully appreciate the need for the
> > > > > barrier (that is even assuming that complete() etc did not do the right
> > > > > thing).  Specifically I was wondering same thing Peter said in the above
> > > > > thread I think that - if that rcu_read_unlock() triggered all the spin
> > > > > locking up the tree of nodes, then why is that locking not sufficient to
> > > > > prevent reads from the read-side section from bleeding out? That would
> > > > > prevent the reader that just unlocked from seeing anything that happens
> > > > > _after_ the synchronize_rcu.
> > > > 
> > > > Actually, I recall an smp_mb() being added, but am not seeing it anywhere
> > > > relevant to wait_for_completion().  So I might need to add the smp_mb()
> > > > to synchronize_rcu() and remove the patch (retaining the typo fix).  :-/
> > > 
> > > No problem, I'm glad atleast the patch resurfaced the topic of the potential
> > > issue :-)
> > 
> > And an smp_mb() is needed in Tree RCU's __wait_rcu_gp().  This is
> > because wait_for_completion() might get a "fly-by" wakeup, which would
> > mean no ordering for code naively thinking that it was ordered after a
> > grace period.
> 
> Makes sense.
> 
> > > > The short form answer is that anything before a grace period on any CPU
> > > > must be seen by any CPU as being before anything on any CPU after that
> > > > same grace period.  This guarantee requires a rather big hammer.
> > > > 
> > > > But yes, let's talk at LPC!
> > > 
> > > Sounds great, looking forward to discussing this.
> > 
> > Would it make sense to have an RCU-implementation BoF?
> 
> Yes, I would very much like that. I also spoke with my colleage Daniel
> Colascione and he said he would be interested too.

Sounds good!

> I think it would make sense also to combine it with other memory-ordering
> topics like the memory model and rseq/cpu-opv things that Mathieu was doing
> (if it makes sense to combine). But yes, I am definitely interested in an
> RCU-implementation BoF session.

There is an LKMM kernel summit track presentation.  I believe that
Mathieu's rseq/cpu-opv would be a good one as well, but Mathieu needs
to lead this up and it should be a separate BoF.  Please do feel free
to reach out to him.  I am sure that he would be particularly interested
in potential uses of rseq and especially cpu-opv.

> > > > > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > > > > me that the RCU reader-sections are virtually extended both forward and
> > > > > backward and whereever it ends, those paths do heavy-weight synchronization
> > > > > that should be sufficient to prevent memory ordering issues (such as those
> > > > > you mentioned in the Requierments document). That is exactly why we don't
> > > > > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > > > > those are not needed. So that answer made sense, but then now on going
> > > > > through the 'Memory Ordering' document, I see that you mentioned there is
> > > > > reliance on the locking. Is that reliance on locking necessary to maintain
> > > > > ordering then?
> > > > 
> > > > There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> > > > that implements the all-to-all memory ordering mentioned above.  But it
> > > > also needs to handle all the possible complete()/wait_for_completion()
> > > > races, even those assisted by hypervisor vCPU preemption.
> > > 
> > > I see, so it sounds like the lock network is just a partial solution. For
> > > some reason I thought before that complete() was even called on the CPU
> > > executing the callback, all the CPUs would have acquired and released a lock
> > > in the "lock network" atleast once thus ensuring the ordering (due to the
> > > fact that the quiescent state reporting has to travel up the tree starting
> > > from the leaves), but I think that's not necessarily true so I see your point
> > > now.
> > 
> > There is indeed a lock that is unconditionally acquired and released by
> > wait_for_completion(), but it lacks the smp_mb__after_unlock_lock() that
> > is required to get full-up any-to-any ordering.  And unfortunate timing
> > (as well as spurious wakeups) allow the interaction to have only normal
> > lock-release/acquire ordering, which does not suffice in all cases.
> > 
> > SRCU and expedited RCU grace periods handle this correctly.  Only the
> > normal grace periods are missing the needed barrier.  The probability of
> > failure is extremely low in the common case, which involves all sorts
> > of synchronization on the wakeup path.  It would be quite strange (but
> > not impossible) for the wait_for_completion() exit path to -not- to do
> > a full wakeup.  Plus the bug requires a reader before the grace period
> > to do a store to some location that post-grace-period code loads from.
> > Which is a very rare use case.
> > 
> > But it still should be fixed.  ;-)
> > 
> > > Did you feel this will violate condition 1. or condition 2. in "Memory-Barrier
> > > Guarantees"? Or both?
> > > https://www.kernel.org/doc/Documentation/RCU/Design/Requirements/Requirements.html#Memory-Barrier%20Guarantees
> > 
> > Condition 1.  There might be some strange combination of events that
> > could also cause it to also violate condition 2, but I am not immediately
> > seeing it.
> 
> In the previous paragraph, you mentioned the bug "requires a reader before
> the GP to do a store". However, condition 1 is really different - it is a
> reader holding a reference to a pointer that is used *after* the
> synchronize_rcu returns. So that reader's load of the pointer should have
> completed by the time GP ends, otherwise the reader can look at kfree'd data.
> That's different right?

More specifically, the fix prevents a prior reader's -store- within its
critical section to be seen as happening after a load that follows the
end of the grace period.  So I stand by Condition 1.  ;-)

And again, a store within an RCU read-side critical section is a bit
on the strange side, but this sort of thing is perfectly legal and
is used, albeit rather rarely.

> For condition 2, I analyzed it below, let me know what you think:
> 
> > 							Thanx, Paul
> > 
> > ------------------------------------------------------------------------
> > 
> > commit bf3c11b7b9789283f993d9beb80caaabc4403916
> > Author: Paul E. McKenney <paulmck@linux.ibm.com>
> > Date:   Thu Nov 1 09:05:02 2018 -0700
> > 
> >     rcu: Add full memory barrier in __wait_rcu_gp()
> >     
> >     RCU grace periods have extremely strong any-to-any ordering
> >     requirements that are met by placing full barriers in various places
> >     in the grace-period computation.  However, normal grace period requests
> >     might be subjected to a "fly-by" wakeup in which the requesting process
> >     doesn't actually sleep and in which the corresponding CPU is not yet
> >     aware that the grace period has ended.  In this case, loads in the code
> >     immediately following the synchronize_rcu() call might potentially see
> >     values before stores preceding the grace period on other CPUs.
> >     
> >     This is an unusual use case, because RCU readers normally read.  However,
> >     they can do writes, and if they do, we need post-grace-period reads to
> >     see those writes.
> >     
> >     This commit therefore adds an smp_mb() to the end of __wait_rcu_gp().
> >     
> >     Many thanks to Joel Fernandes for the series of questions leading to me
> >     realizing that this bug exists!
> >     
> >     Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>
> > 
> > diff --git a/kernel/rcu/update.c b/kernel/rcu/update.c
> > index 1971869c4072..74020b558216 100644
> > --- a/kernel/rcu/update.c
> > +++ b/kernel/rcu/update.c
> > @@ -360,6 +360,7 @@ void __wait_rcu_gp(bool checktiny, int n, call_rcu_func_t *crcu_array,
> >  			wait_for_completion(&rs_array[i].completion);
> >  		destroy_rcu_head_on_stack(&rs_array[i].head);
> >  	}
> > +	smp_mb(); /* Provide ordering in case of fly-by wakeup. */
> >  }
> >  EXPORT_SYMBOL_GPL(__wait_rcu_gp);
> >  
> 
> The fix looks fine to me. Thanks.
> 
> If I understand correctly the wait_for_completion() is an ACQUIRE operation,
> and the complete() is a RELEASE operation aka the "MP pattern". The
> ACQUIRE/RELEASE semantics allow any writes that happened before the ACQUIRE
> to get ordered after it. So that would actually imply it is not strong enough
> for ordering purposes during a "fly-by" wake up scenario and would be a
> violation of CONDITION 2, I think (not only condition 1 as you said).  This
> is because future readers may accidentally see the writes that happened
> *before* the synchronize_rcu which is CONDITION 2 in the requirements:
> https://goo.gl/8mrDHN  (I had to shortlink it ;))

I do appreciate the shorter link.  ;-)

A write happening before the grace period is ordered by the grace period's
network of strong barriers, so the fix does not matter in that case.
Also, the exact end of the grace period is irrelevant for Condition 2,
it is instead the beginning of the grace period compared to the beginning
of later RCU read-side critical sections.

Not saying that Condition 2 cannot somehow happen without the memory
barrier, just saying that it will take quite a bit more creativity to
find a relevant scenario.

Please see below for the updated patch, containing only the typo fix.
Please let me know if I messed anything up.

							Thanx, Paul

------------------------------------------------------------------------

commit bdf892699a32e82305c7203d61f93cffdfbe8735
Author: Joel Fernandes (Google) <joel@joelfernandes.org>
Date:   Sat Oct 27 21:30:46 2018 -0700

    doc: Fix "struction" typo in RCU memory-ordering documentation.
    
    This commit replaces "struction" with the correct "structure".
    
    Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
    Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>

diff --git a/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html b/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
index a346ce0116eb..e4d94fba6c89 100644
--- a/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
+++ b/Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.html
@@ -77,7 +77,7 @@ The key point is that the lock-acquisition functions, including
 <tt>smp_mb__after_unlock_lock()</tt> immediately after successful
 acquisition of the lock.
 
-<p>Therefore, for any given <tt>rcu_node</tt> struction, any access
+<p>Therefore, for any given <tt>rcu_node</tt> structure, any access
 happening before one of the above lock-release functions will be seen
 by all CPUs as happening before any access happening after a later
 one of the above lock-acquisition functions.


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-11-02 20:00               ` Paul E. McKenney
@ 2018-11-02 22:14                 ` Joel Fernandes
  2018-11-02 22:24                   ` Paul E. McKenney
  0 siblings, 1 reply; 16+ messages in thread
From: Joel Fernandes @ 2018-11-02 22:14 UTC (permalink / raw)
  To: Paul E. McKenney; +Cc: linux-kernel

On Fri, Nov 02, 2018 at 01:00:00PM -0700, Paul E. McKenney wrote:
[..] 
> > I think it would make sense also to combine it with other memory-ordering
> > topics like the memory model and rseq/cpu-opv things that Mathieu was doing
> > (if it makes sense to combine). But yes, I am definitely interested in an
> > RCU-implementation BoF session.
> 
> There is an LKMM kernel summit track presentation.  I believe that
> Mathieu's rseq/cpu-opv would be a good one as well, but Mathieu needs
> to lead this up and it should be a separate BoF.  Please do feel free
> to reach out to him.  I am sure that he would be particularly interested
> in potential uses of rseq and especially cpu-opv.

Cool! Looking forward to LKMM talk and I'll keep in mind to reach out to
Mathieu about rseq usecases.

> > > > > > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > > > > > me that the RCU reader-sections are virtually extended both forward and
> > > > > > backward and whereever it ends, those paths do heavy-weight synchronization
> > > > > > that should be sufficient to prevent memory ordering issues (such as those
> > > > > > you mentioned in the Requierments document). That is exactly why we don't
> > > > > > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > > > > > those are not needed. So that answer made sense, but then now on going
> > > > > > through the 'Memory Ordering' document, I see that you mentioned there is
> > > > > > reliance on the locking. Is that reliance on locking necessary to maintain
> > > > > > ordering then?
> > > > > 
> > > > > There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> > > > > that implements the all-to-all memory ordering mentioned above.  But it
> > > > > also needs to handle all the possible complete()/wait_for_completion()
> > > > > races, even those assisted by hypervisor vCPU preemption.
> > > > 
> > > > I see, so it sounds like the lock network is just a partial solution. For
> > > > some reason I thought before that complete() was even called on the CPU
> > > > executing the callback, all the CPUs would have acquired and released a lock
> > > > in the "lock network" atleast once thus ensuring the ordering (due to the
> > > > fact that the quiescent state reporting has to travel up the tree starting
> > > > from the leaves), but I think that's not necessarily true so I see your point
> > > > now.
> > > 
> > > There is indeed a lock that is unconditionally acquired and released by
> > > wait_for_completion(), but it lacks the smp_mb__after_unlock_lock() that
> > > is required to get full-up any-to-any ordering.  And unfortunate timing
> > > (as well as spurious wakeups) allow the interaction to have only normal
> > > lock-release/acquire ordering, which does not suffice in all cases.
> > > 
> > > SRCU and expedited RCU grace periods handle this correctly.  Only the
> > > normal grace periods are missing the needed barrier.  The probability of
> > > failure is extremely low in the common case, which involves all sorts
> > > of synchronization on the wakeup path.  It would be quite strange (but
> > > not impossible) for the wait_for_completion() exit path to -not- to do
> > > a full wakeup.  Plus the bug requires a reader before the grace period
> > > to do a store to some location that post-grace-period code loads from.
> > > Which is a very rare use case.
> > > 
> > > But it still should be fixed.  ;-)
> > > 
> > > > Did you feel this will violate condition 1. or condition 2. in "Memory-Barrier
> > > > Guarantees"? Or both?
> > > > https://www.kernel.org/doc/Documentation/RCU/Design/Requirements/Requirements.html#Memory-Barrier%20Guarantees
> > > 
> > > Condition 1.  There might be some strange combination of events that
> > > could also cause it to also violate condition 2, but I am not immediately
> > > seeing it.
> > 
> > In the previous paragraph, you mentioned the bug "requires a reader before
> > the GP to do a store". However, condition 1 is really different - it is a
> > reader holding a reference to a pointer that is used *after* the
> > synchronize_rcu returns. So that reader's load of the pointer should have
> > completed by the time GP ends, otherwise the reader can look at kfree'd data.
> > That's different right?
> 
> More specifically, the fix prevents a prior reader's -store- within its
> critical section to be seen as happening after a load that follows the
> end of the grace period.  So I stand by Condition 1.  ;-)
> And again, a store within an RCU read-side critical section is a bit
> on the strange side, but this sort of thing is perfectly legal and
> is used, albeit rather rarely.

Cool :) I never thought about condition 1 this way but good to know that's
possible :)

> > For condition 2, I analyzed it below, let me know what you think:
> > 
> > > 							Thanx, Paul
> > > 
> > > ------------------------------------------------------------------------
> > > 
> > > commit bf3c11b7b9789283f993d9beb80caaabc4403916
> > > Author: Paul E. McKenney <paulmck@linux.ibm.com>
> > > Date:   Thu Nov 1 09:05:02 2018 -0700
> > > 
> > >     rcu: Add full memory barrier in __wait_rcu_gp()
> > >     
> > >     RCU grace periods have extremely strong any-to-any ordering
> > >     requirements that are met by placing full barriers in various places
> > >     in the grace-period computation.  However, normal grace period requests
> > >     might be subjected to a "fly-by" wakeup in which the requesting process
> > >     doesn't actually sleep and in which the corresponding CPU is not yet
> > >     aware that the grace period has ended.  In this case, loads in the code
> > >     immediately following the synchronize_rcu() call might potentially see
> > >     values before stores preceding the grace period on other CPUs.
> > >     
> > >     This is an unusual use case, because RCU readers normally read.  However,
> > >     they can do writes, and if they do, we need post-grace-period reads to
> > >     see those writes.
> > >     
> > >     This commit therefore adds an smp_mb() to the end of __wait_rcu_gp().
> > >     
> > >     Many thanks to Joel Fernandes for the series of questions leading to me
> > >     realizing that this bug exists!
> > >     
> > >     Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>
> > > 
> > > diff --git a/kernel/rcu/update.c b/kernel/rcu/update.c
> > > index 1971869c4072..74020b558216 100644
> > > --- a/kernel/rcu/update.c
> > > +++ b/kernel/rcu/update.c
> > > @@ -360,6 +360,7 @@ void __wait_rcu_gp(bool checktiny, int n, call_rcu_func_t *crcu_array,
> > >  			wait_for_completion(&rs_array[i].completion);
> > >  		destroy_rcu_head_on_stack(&rs_array[i].head);
> > >  	}
> > > +	smp_mb(); /* Provide ordering in case of fly-by wakeup. */
> > >  }
> > >  EXPORT_SYMBOL_GPL(__wait_rcu_gp);
> > >  
https://cs.corp.google.com/piper///depot/google3/base/percpu.cc?type=cs&q=%22rseq%22+restart&sq=package:piper+file://depot/google3+-file:google3/experimental&g=0&l=247> > 
> > The fix looks fine to me. Thanks.
> > 
> > If I understand correctly the wait_for_completion() is an ACQUIRE operation,
> > and the complete() is a RELEASE operation aka the "MP pattern". The
> > ACQUIRE/RELEASE semantics allow any writes that happened before the ACQUIRE
> > to get ordered after it. So that would actually imply it is not strong enough
> > for ordering purposes during a "fly-by" wake up scenario and would be a
> > violation of CONDITION 2, I think (not only condition 1 as you said).  This
> > is because future readers may accidentally see the writes that happened
> > *before* the synchronize_rcu which is CONDITION 2 in the requirements:
> > https://goo.gl/8mrDHN  (I had to shortlink it ;))
> 
> I do appreciate the shorter link.  ;-)
> 
> A write happening before the grace period is ordered by the grace period's
> network of strong barriers, so the fix does not matter in that case.

I was talking about the acquire/release pairs in the calls to spin_lock and
spin_unlock in wait_for_completion, not in the grace period network of rnp locks.
Does that make sense?

I thought that during a "fly-by" wake up, that network of strong barriers
doesn't really trigger and that that's the problematic scenario. Did I miss
something?  I was talking about the acquire/release pair in
wait_for_completion during that fly-by scenario.

> Also, the exact end of the grace period is irrelevant for Condition 2,
> it is instead the beginning of the grace period compared to the beginning
> of later RCU read-side critical sections.
> 
> Not saying that Condition 2 cannot somehow happen without the memory
> barrier, just saying that it will take quite a bit more creativity to
> find a relevant scenario.
> 
> Please see below for the updated patch, containing only the typo fix.
> Please let me know if I messed anything up.

Looks good to me, thanks!

 - Joel



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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-11-02 22:14                 ` Joel Fernandes
@ 2018-11-02 22:24                   ` Paul E. McKenney
  0 siblings, 0 replies; 16+ messages in thread
From: Paul E. McKenney @ 2018-11-02 22:24 UTC (permalink / raw)
  To: Joel Fernandes; +Cc: linux-kernel

On Fri, Nov 02, 2018 at 03:14:29PM -0700, Joel Fernandes wrote:
> On Fri, Nov 02, 2018 at 01:00:00PM -0700, Paul E. McKenney wrote:
> [..] 
> > > I think it would make sense also to combine it with other memory-ordering
> > > topics like the memory model and rseq/cpu-opv things that Mathieu was doing
> > > (if it makes sense to combine). But yes, I am definitely interested in an
> > > RCU-implementation BoF session.
> > 
> > There is an LKMM kernel summit track presentation.  I believe that
> > Mathieu's rseq/cpu-opv would be a good one as well, but Mathieu needs
> > to lead this up and it should be a separate BoF.  Please do feel free
> > to reach out to him.  I am sure that he would be particularly interested
> > in potential uses of rseq and especially cpu-opv.
> 
> Cool! Looking forward to LKMM talk and I'll keep in mind to reach out to
> Mathieu about rseq usecases.

Sounds good!

> > > > > > > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > > > > > > me that the RCU reader-sections are virtually extended both forward and
> > > > > > > backward and whereever it ends, those paths do heavy-weight synchronization
> > > > > > > that should be sufficient to prevent memory ordering issues (such as those
> > > > > > > you mentioned in the Requierments document). That is exactly why we don't
> > > > > > > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > > > > > > those are not needed. So that answer made sense, but then now on going
> > > > > > > through the 'Memory Ordering' document, I see that you mentioned there is
> > > > > > > reliance on the locking. Is that reliance on locking necessary to maintain
> > > > > > > ordering then?
> > > > > > 
> > > > > > There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> > > > > > that implements the all-to-all memory ordering mentioned above.  But it
> > > > > > also needs to handle all the possible complete()/wait_for_completion()
> > > > > > races, even those assisted by hypervisor vCPU preemption.
> > > > > 
> > > > > I see, so it sounds like the lock network is just a partial solution. For
> > > > > some reason I thought before that complete() was even called on the CPU
> > > > > executing the callback, all the CPUs would have acquired and released a lock
> > > > > in the "lock network" atleast once thus ensuring the ordering (due to the
> > > > > fact that the quiescent state reporting has to travel up the tree starting
> > > > > from the leaves), but I think that's not necessarily true so I see your point
> > > > > now.
> > > > 
> > > > There is indeed a lock that is unconditionally acquired and released by
> > > > wait_for_completion(), but it lacks the smp_mb__after_unlock_lock() that
> > > > is required to get full-up any-to-any ordering.  And unfortunate timing
> > > > (as well as spurious wakeups) allow the interaction to have only normal
> > > > lock-release/acquire ordering, which does not suffice in all cases.
> > > > 
> > > > SRCU and expedited RCU grace periods handle this correctly.  Only the
> > > > normal grace periods are missing the needed barrier.  The probability of
> > > > failure is extremely low in the common case, which involves all sorts
> > > > of synchronization on the wakeup path.  It would be quite strange (but
> > > > not impossible) for the wait_for_completion() exit path to -not- to do
> > > > a full wakeup.  Plus the bug requires a reader before the grace period
> > > > to do a store to some location that post-grace-period code loads from.
> > > > Which is a very rare use case.
> > > > 
> > > > But it still should be fixed.  ;-)
> > > > 
> > > > > Did you feel this will violate condition 1. or condition 2. in "Memory-Barrier
> > > > > Guarantees"? Or both?
> > > > > https://www.kernel.org/doc/Documentation/RCU/Design/Requirements/Requirements.html#Memory-Barrier%20Guarantees
> > > > 
> > > > Condition 1.  There might be some strange combination of events that
> > > > could also cause it to also violate condition 2, but I am not immediately
> > > > seeing it.
> > > 
> > > In the previous paragraph, you mentioned the bug "requires a reader before
> > > the GP to do a store". However, condition 1 is really different - it is a
> > > reader holding a reference to a pointer that is used *after* the
> > > synchronize_rcu returns. So that reader's load of the pointer should have
> > > completed by the time GP ends, otherwise the reader can look at kfree'd data.
> > > That's different right?
> > 
> > More specifically, the fix prevents a prior reader's -store- within its
> > critical section to be seen as happening after a load that follows the
> > end of the grace period.  So I stand by Condition 1.  ;-)
> > And again, a store within an RCU read-side critical section is a bit
> > on the strange side, but this sort of thing is perfectly legal and
> > is used, albeit rather rarely.
> 
> Cool :) I never thought about condition 1 this way but good to know that's
> possible :)

I know that feeling...  ;-)

> > > For condition 2, I analyzed it below, let me know what you think:
> > > 
> > > > 							Thanx, Paul
> > > > 
> > > > ------------------------------------------------------------------------
> > > > 
> > > > commit bf3c11b7b9789283f993d9beb80caaabc4403916
> > > > Author: Paul E. McKenney <paulmck@linux.ibm.com>
> > > > Date:   Thu Nov 1 09:05:02 2018 -0700
> > > > 
> > > >     rcu: Add full memory barrier in __wait_rcu_gp()
> > > >     
> > > >     RCU grace periods have extremely strong any-to-any ordering
> > > >     requirements that are met by placing full barriers in various places
> > > >     in the grace-period computation.  However, normal grace period requests
> > > >     might be subjected to a "fly-by" wakeup in which the requesting process
> > > >     doesn't actually sleep and in which the corresponding CPU is not yet
> > > >     aware that the grace period has ended.  In this case, loads in the code
> > > >     immediately following the synchronize_rcu() call might potentially see
> > > >     values before stores preceding the grace period on other CPUs.
> > > >     
> > > >     This is an unusual use case, because RCU readers normally read.  However,
> > > >     they can do writes, and if they do, we need post-grace-period reads to
> > > >     see those writes.
> > > >     
> > > >     This commit therefore adds an smp_mb() to the end of __wait_rcu_gp().
> > > >     
> > > >     Many thanks to Joel Fernandes for the series of questions leading to me
> > > >     realizing that this bug exists!
> > > >     
> > > >     Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>
> > > > 
> > > > diff --git a/kernel/rcu/update.c b/kernel/rcu/update.c
> > > > index 1971869c4072..74020b558216 100644
> > > > --- a/kernel/rcu/update.c
> > > > +++ b/kernel/rcu/update.c
> > > > @@ -360,6 +360,7 @@ void __wait_rcu_gp(bool checktiny, int n, call_rcu_func_t *crcu_array,
> > > >  			wait_for_completion(&rs_array[i].completion);
> > > >  		destroy_rcu_head_on_stack(&rs_array[i].head);
> > > >  	}
> > > > +	smp_mb(); /* Provide ordering in case of fly-by wakeup. */
> > > >  }
> > > >  EXPORT_SYMBOL_GPL(__wait_rcu_gp);
> > > >  
> https://cs.corp.google.com/piper///depot/google3/base/percpu.cc?type=cs&q=%22rseq%22+restart&sq=package:piper+file://depot/google3+-file:google3/experimental&g=0&l=247> > 
> > > The fix looks fine to me. Thanks.
> > > 
> > > If I understand correctly the wait_for_completion() is an ACQUIRE operation,
> > > and the complete() is a RELEASE operation aka the "MP pattern". The
> > > ACQUIRE/RELEASE semantics allow any writes that happened before the ACQUIRE
> > > to get ordered after it. So that would actually imply it is not strong enough
> > > for ordering purposes during a "fly-by" wake up scenario and would be a
> > > violation of CONDITION 2, I think (not only condition 1 as you said).  This
> > > is because future readers may accidentally see the writes that happened
> > > *before* the synchronize_rcu which is CONDITION 2 in the requirements:
> > > https://goo.gl/8mrDHN  (I had to shortlink it ;))
> > 
> > I do appreciate the shorter link.  ;-)
> > 
> > A write happening before the grace period is ordered by the grace period's
> > network of strong barriers, so the fix does not matter in that case.
> 
> I was talking about the acquire/release pairs in the calls to spin_lock and
> spin_unlock in wait_for_completion, not in the grace period network of rnp locks.
> Does that make sense?
> 
> I thought that during a "fly-by" wake up, that network of strong barriers
> doesn't really trigger and that that's the problematic scenario. Did I miss
> something?  I was talking about the acquire/release pair in
> wait_for_completion during that fly-by scenario.

The thing is that the ordering of the wakeup doesn't matter, because
in Condition 2, the end of the grace period (which is where the wakeup
happens) isn't involved in the ordering.  It is instead the ordering of
the beginning of the grace period with the beginning of some later RCU
read-side critical section.

Another way to look at this is that in Condition 2, we don't have to
care when the grace period ends.

> > Also, the exact end of the grace period is irrelevant for Condition 2,
> > it is instead the beginning of the grace period compared to the beginning
> > of later RCU read-side critical sections.
> > 
> > Not saying that Condition 2 cannot somehow happen without the memory
> > barrier, just saying that it will take quite a bit more creativity to
> > find a relevant scenario.
> > 
> > Please see below for the updated patch, containing only the typo fix.
> > Please let me know if I messed anything up.
> 
> Looks good to me, thanks!

It is queued, likely for the next merge window.

							Thanx, Paul


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-11-01 16:13           ` Paul E. McKenney
  2018-11-02  6:15             ` Joel Fernandes
@ 2018-11-03  5:12             ` Joel Fernandes
  2018-11-03 23:22               ` Paul E. McKenney
  1 sibling, 1 reply; 16+ messages in thread
From: Joel Fernandes @ 2018-11-03  5:12 UTC (permalink / raw)
  To: Paul E. McKenney; +Cc: linux-kernel

On Thu, Nov 01, 2018 at 09:13:07AM -0700, Paul E. McKenney wrote:
> On Wed, Oct 31, 2018 at 10:00:19PM -0700, Joel Fernandes wrote:
> > On Wed, Oct 31, 2018 at 11:17:48AM -0700, Paul E. McKenney wrote:
> > > On Tue, Oct 30, 2018 at 06:11:19PM -0700, Joel Fernandes wrote:
> > > > Hi Paul,
> > > > 
> > > > On Tue, Oct 30, 2018 at 04:43:36PM -0700, Paul E. McKenney wrote:
> > > > > On Tue, Oct 30, 2018 at 03:26:49PM -0700, Joel Fernandes wrote:
> > > > > > Hi Paul,
> > > > > > 
> > > > > > On Sat, Oct 27, 2018 at 09:30:46PM -0700, Joel Fernandes (Google) wrote:
> > > > > > > As per this thread [1], it seems this smp_mb isn't needed anymore:
> > > > > > > "So the smp_mb() that I was trying to add doesn't need to be there."
> > > > > > > 
> > > > > > > So let us remove this part from the memory ordering documentation.
> > > > > > > 
> > > > > > > [1] https://lkml.org/lkml/2017/10/6/707
> > > > > > > 
> > > > > > > Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> > > > > > 
> > > > > > I was just checking about this patch. Do you feel it is correct to remove
> > > > > > this part from the docs? Are you satisified that a barrier isn't needed there
> > > > > > now? Or did I miss something?
> > > > > 
> > > > > Apologies, it got lost in the shuffle.  I have now applied it with a
> > > > > bit of rework to the commit log, thank you!
> > > > 
> > > > No worries, thanks for taking it!
> > > > 
> > > > Just wanted to update you on my progress reading/correcting the docs. The
> > > > 'Memory Ordering' is taking a bit of time so I paused that and I'm focusing
> > > > on finishing all the other low hanging fruit. This activity is mostly during
> > > > night hours after the baby is asleep but some times I also manage to sneak it
> > > > into the day job ;-)
> > > 
> > > If there is anything I can do to make this a more sustainable task for
> > > you, please do not keep it a secret!!!
> > 
> > Thanks a lot, that means a lot to me! Will do!
> > 
> > > > BTW I do want to discuss about this smp_mb patch above with you at LPC if you
> > > > had time, even though we are removing it from the documentation. I thought
> > > > about it a few times, and I was not able to fully appreciate the need for the
> > > > barrier (that is even assuming that complete() etc did not do the right
> > > > thing).  Specifically I was wondering same thing Peter said in the above
> > > > thread I think that - if that rcu_read_unlock() triggered all the spin
> > > > locking up the tree of nodes, then why is that locking not sufficient to
> > > > prevent reads from the read-side section from bleeding out? That would
> > > > prevent the reader that just unlocked from seeing anything that happens
> > > > _after_ the synchronize_rcu.
> > > 
> > > Actually, I recall an smp_mb() being added, but am not seeing it anywhere
> > > relevant to wait_for_completion().  So I might need to add the smp_mb()
> > > to synchronize_rcu() and remove the patch (retaining the typo fix).  :-/
> > 
> > No problem, I'm glad atleast the patch resurfaced the topic of the potential
> > issue :-)
> 
> And an smp_mb() is needed in Tree RCU's __wait_rcu_gp().  This is
> because wait_for_completion() might get a "fly-by" wakeup, which would
> mean no ordering for code naively thinking that it was ordered after a
> grace period.
> 
> > > The short form answer is that anything before a grace period on any CPU
> > > must be seen by any CPU as being before anything on any CPU after that
> > > same grace period.  This guarantee requires a rather big hammer.
> > > 
> > > But yes, let's talk at LPC!
> > 
> > Sounds great, looking forward to discussing this.
> 
> Would it make sense to have an RCU-implementation BoF?
> 
> > > > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > > > me that the RCU reader-sections are virtually extended both forward and
> > > > backward and whereever it ends, those paths do heavy-weight synchronization
> > > > that should be sufficient to prevent memory ordering issues (such as those
> > > > you mentioned in the Requierments document). That is exactly why we don't
> > > > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > > > those are not needed. So that answer made sense, but then now on going
> > > > through the 'Memory Ordering' document, I see that you mentioned there is
> > > > reliance on the locking. Is that reliance on locking necessary to maintain
> > > > ordering then?
> > > 
> > > There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> > > that implements the all-to-all memory ordering mentioned above.  But it
> > > also needs to handle all the possible complete()/wait_for_completion()
> > > races, even those assisted by hypervisor vCPU preemption.
> > 
> > I see, so it sounds like the lock network is just a partial solution. For
> > some reason I thought before that complete() was even called on the CPU
> > executing the callback, all the CPUs would have acquired and released a lock
> > in the "lock network" atleast once thus ensuring the ordering (due to the
> > fact that the quiescent state reporting has to travel up the tree starting
> > from the leaves), but I think that's not necessarily true so I see your point
> > now.
> 
> There is indeed a lock that is unconditionally acquired and released by
> wait_for_completion(), but it lacks the smp_mb__after_unlock_lock() that
> is required to get full-up any-to-any ordering.  And unfortunate timing
> (as well as spurious wakeups) allow the interaction to have only normal
> lock-release/acquire ordering, which does not suffice in all cases.

Sorry to be so persistent, but I did spend some time on this and I still
don't get why every CPU would _not_ have executed smp_mb__after_unlock_lock at least
once before the wait_for_completion() returns, because every CPU should have
atleast called rcu_report_qs_rdp() -> rcu_report_qs_rnp() atleast once to
report its QS up the tree right?. Before that procedure, the complete()
cannot happen because the complete() itself is in an RCU callback which is
executed only once all the QS(s) have been reported.

So I still couldn't see how the synchronize_rcu can return without the
rcu_report_qs_rnp called atleast once on the CPU reporting its QS during a
grace period.

Would it be possible to provide a small example showing this in least number
of steps? I appreciate your time and it would be really helpful. If you feel
its too complicated, then feel free to keep this for LPC discussion :)

Thanks,

-Joel


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-11-03  5:12             ` Joel Fernandes
@ 2018-11-03 23:22               ` Paul E. McKenney
  2018-11-04  3:49                 ` Joel Fernandes
  0 siblings, 1 reply; 16+ messages in thread
From: Paul E. McKenney @ 2018-11-03 23:22 UTC (permalink / raw)
  To: Joel Fernandes; +Cc: linux-kernel

On Fri, Nov 02, 2018 at 10:12:26PM -0700, Joel Fernandes wrote:
> On Thu, Nov 01, 2018 at 09:13:07AM -0700, Paul E. McKenney wrote:
> > On Wed, Oct 31, 2018 at 10:00:19PM -0700, Joel Fernandes wrote:
> > > On Wed, Oct 31, 2018 at 11:17:48AM -0700, Paul E. McKenney wrote:
> > > > On Tue, Oct 30, 2018 at 06:11:19PM -0700, Joel Fernandes wrote:
> > > > > Hi Paul,
> > > > > 
> > > > > On Tue, Oct 30, 2018 at 04:43:36PM -0700, Paul E. McKenney wrote:
> > > > > > On Tue, Oct 30, 2018 at 03:26:49PM -0700, Joel Fernandes wrote:
> > > > > > > Hi Paul,
> > > > > > > 
> > > > > > > On Sat, Oct 27, 2018 at 09:30:46PM -0700, Joel Fernandes (Google) wrote:
> > > > > > > > As per this thread [1], it seems this smp_mb isn't needed anymore:
> > > > > > > > "So the smp_mb() that I was trying to add doesn't need to be there."
> > > > > > > > 
> > > > > > > > So let us remove this part from the memory ordering documentation.
> > > > > > > > 
> > > > > > > > [1] https://lkml.org/lkml/2017/10/6/707
> > > > > > > > 
> > > > > > > > Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> > > > > > > 
> > > > > > > I was just checking about this patch. Do you feel it is correct to remove
> > > > > > > this part from the docs? Are you satisified that a barrier isn't needed there
> > > > > > > now? Or did I miss something?
> > > > > > 
> > > > > > Apologies, it got lost in the shuffle.  I have now applied it with a
> > > > > > bit of rework to the commit log, thank you!
> > > > > 
> > > > > No worries, thanks for taking it!
> > > > > 
> > > > > Just wanted to update you on my progress reading/correcting the docs. The
> > > > > 'Memory Ordering' is taking a bit of time so I paused that and I'm focusing
> > > > > on finishing all the other low hanging fruit. This activity is mostly during
> > > > > night hours after the baby is asleep but some times I also manage to sneak it
> > > > > into the day job ;-)
> > > > 
> > > > If there is anything I can do to make this a more sustainable task for
> > > > you, please do not keep it a secret!!!
> > > 
> > > Thanks a lot, that means a lot to me! Will do!
> > > 
> > > > > BTW I do want to discuss about this smp_mb patch above with you at LPC if you
> > > > > had time, even though we are removing it from the documentation. I thought
> > > > > about it a few times, and I was not able to fully appreciate the need for the
> > > > > barrier (that is even assuming that complete() etc did not do the right
> > > > > thing).  Specifically I was wondering same thing Peter said in the above
> > > > > thread I think that - if that rcu_read_unlock() triggered all the spin
> > > > > locking up the tree of nodes, then why is that locking not sufficient to
> > > > > prevent reads from the read-side section from bleeding out? That would
> > > > > prevent the reader that just unlocked from seeing anything that happens
> > > > > _after_ the synchronize_rcu.
> > > > 
> > > > Actually, I recall an smp_mb() being added, but am not seeing it anywhere
> > > > relevant to wait_for_completion().  So I might need to add the smp_mb()
> > > > to synchronize_rcu() and remove the patch (retaining the typo fix).  :-/
> > > 
> > > No problem, I'm glad atleast the patch resurfaced the topic of the potential
> > > issue :-)
> > 
> > And an smp_mb() is needed in Tree RCU's __wait_rcu_gp().  This is
> > because wait_for_completion() might get a "fly-by" wakeup, which would
> > mean no ordering for code naively thinking that it was ordered after a
> > grace period.
> > 
> > > > The short form answer is that anything before a grace period on any CPU
> > > > must be seen by any CPU as being before anything on any CPU after that
> > > > same grace period.  This guarantee requires a rather big hammer.
> > > > 
> > > > But yes, let's talk at LPC!
> > > 
> > > Sounds great, looking forward to discussing this.
> > 
> > Would it make sense to have an RCU-implementation BoF?
> > 
> > > > > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > > > > me that the RCU reader-sections are virtually extended both forward and
> > > > > backward and whereever it ends, those paths do heavy-weight synchronization
> > > > > that should be sufficient to prevent memory ordering issues (such as those
> > > > > you mentioned in the Requierments document). That is exactly why we don't
> > > > > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > > > > those are not needed. So that answer made sense, but then now on going
> > > > > through the 'Memory Ordering' document, I see that you mentioned there is
> > > > > reliance on the locking. Is that reliance on locking necessary to maintain
> > > > > ordering then?
> > > > 
> > > > There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> > > > that implements the all-to-all memory ordering mentioned above.  But it
> > > > also needs to handle all the possible complete()/wait_for_completion()
> > > > races, even those assisted by hypervisor vCPU preemption.
> > > 
> > > I see, so it sounds like the lock network is just a partial solution. For
> > > some reason I thought before that complete() was even called on the CPU
> > > executing the callback, all the CPUs would have acquired and released a lock
> > > in the "lock network" atleast once thus ensuring the ordering (due to the
> > > fact that the quiescent state reporting has to travel up the tree starting
> > > from the leaves), but I think that's not necessarily true so I see your point
> > > now.
> > 
> > There is indeed a lock that is unconditionally acquired and released by
> > wait_for_completion(), but it lacks the smp_mb__after_unlock_lock() that
> > is required to get full-up any-to-any ordering.  And unfortunate timing
> > (as well as spurious wakeups) allow the interaction to have only normal
> > lock-release/acquire ordering, which does not suffice in all cases.
> 
> Sorry to be so persistent, but I did spend some time on this and I still
> don't get why every CPU would _not_ have executed smp_mb__after_unlock_lock at least
> once before the wait_for_completion() returns, because every CPU should have
> atleast called rcu_report_qs_rdp() -> rcu_report_qs_rnp() atleast once to
> report its QS up the tree right?. Before that procedure, the complete()
> cannot happen because the complete() itself is in an RCU callback which is
> executed only once all the QS(s) have been reported.
> 
> So I still couldn't see how the synchronize_rcu can return without the
> rcu_report_qs_rnp called atleast once on the CPU reporting its QS during a
> grace period.
> 
> Would it be possible to provide a small example showing this in least number
> of steps? I appreciate your time and it would be really helpful. If you feel
> its too complicated, then feel free to keep this for LPC discussion :)

The key point is that "at least once" does not suffice, other than for the
CPU that detects the end of the grace period.  The rest of the CPUs must
do at least -two- full barriers, which could of course be either smp_mb()
on the one hand or smp_mb__after_unlock_lock() after a lock on the other.

The reason for this is the all-to-all ordering that is required.  First,
the grace period does a fan-in operation as each CPU responds to the
grace-period request, informing RCU of its quiescent state while holding a
fully ordered lock.  Second, some CPU detects the end of the grace period
while holding a fully ordered lock.  Third, each remaining CPU notes
the end of the grace period, again while holding a fully ordered lock.

Now suppose that the wakeup callback for a given call to synchronize_rcu()
is on the CPU that detected the end of the grace period, but the task
to be awakened will run on some other CPU.  Suppose further that this
other CPU hasn't yet noticed the end of the grace period, and further
yet that it was the first one to report a quiescent state.  This means
that its most recent fully ordered lock was released too early to order
against the other CPUs' quiescent states.  Now, as you say, the lock
acquisitions and releases for the completion() and wait_for_completion()
will cause the awakened CPU to correctly see any pre-grace-period
activity on the CPU that detected the end of the grace period.  But
this is not the case for any of the other CPUs, and won't be until the
awakened CPU notes the end of the grace period while holding its fully
ordered lock.

So we really do need that memory barrier at the end of __wait_rcu_gp().

							Thanx, Paul


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-11-03 23:22               ` Paul E. McKenney
@ 2018-11-04  3:49                 ` Joel Fernandes
  2018-11-05  3:43                   ` Paul E. McKenney
  0 siblings, 1 reply; 16+ messages in thread
From: Joel Fernandes @ 2018-11-04  3:49 UTC (permalink / raw)
  To: Paul E. McKenney; +Cc: linux-kernel

On Sat, Nov 03, 2018 at 04:22:59PM -0700, Paul E. McKenney wrote:
> On Fri, Nov 02, 2018 at 10:12:26PM -0700, Joel Fernandes wrote:
> > On Thu, Nov 01, 2018 at 09:13:07AM -0700, Paul E. McKenney wrote:
> > > On Wed, Oct 31, 2018 at 10:00:19PM -0700, Joel Fernandes wrote:
> > > > On Wed, Oct 31, 2018 at 11:17:48AM -0700, Paul E. McKenney wrote:
> > > > > On Tue, Oct 30, 2018 at 06:11:19PM -0700, Joel Fernandes wrote:
> > > > > > Hi Paul,
> > > > > > 
> > > > > > On Tue, Oct 30, 2018 at 04:43:36PM -0700, Paul E. McKenney wrote:
> > > > > > > On Tue, Oct 30, 2018 at 03:26:49PM -0700, Joel Fernandes wrote:
> > > > > > > > Hi Paul,
> > > > > > > > 
> > > > > > > > On Sat, Oct 27, 2018 at 09:30:46PM -0700, Joel Fernandes (Google) wrote:
> > > > > > > > > As per this thread [1], it seems this smp_mb isn't needed anymore:
> > > > > > > > > "So the smp_mb() that I was trying to add doesn't need to be there."
> > > > > > > > > 
> > > > > > > > > So let us remove this part from the memory ordering documentation.
> > > > > > > > > 
> > > > > > > > > [1] https://lkml.org/lkml/2017/10/6/707
> > > > > > > > > 
> > > > > > > > > Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> > > > > > > > 
> > > > > > > > I was just checking about this patch. Do you feel it is correct to remove
> > > > > > > > this part from the docs? Are you satisified that a barrier isn't needed there
> > > > > > > > now? Or did I miss something?
> > > > > > > 
> > > > > > > Apologies, it got lost in the shuffle.  I have now applied it with a
> > > > > > > bit of rework to the commit log, thank you!
> > > > > > 
> > > > > > No worries, thanks for taking it!
> > > > > > 
> > > > > > Just wanted to update you on my progress reading/correcting the docs. The
> > > > > > 'Memory Ordering' is taking a bit of time so I paused that and I'm focusing
> > > > > > on finishing all the other low hanging fruit. This activity is mostly during
> > > > > > night hours after the baby is asleep but some times I also manage to sneak it
> > > > > > into the day job ;-)
> > > > > 
> > > > > If there is anything I can do to make this a more sustainable task for
> > > > > you, please do not keep it a secret!!!
> > > > 
> > > > Thanks a lot, that means a lot to me! Will do!
> > > > 
> > > > > > BTW I do want to discuss about this smp_mb patch above with you at LPC if you
> > > > > > had time, even though we are removing it from the documentation. I thought
> > > > > > about it a few times, and I was not able to fully appreciate the need for the
> > > > > > barrier (that is even assuming that complete() etc did not do the right
> > > > > > thing).  Specifically I was wondering same thing Peter said in the above
> > > > > > thread I think that - if that rcu_read_unlock() triggered all the spin
> > > > > > locking up the tree of nodes, then why is that locking not sufficient to
> > > > > > prevent reads from the read-side section from bleeding out? That would
> > > > > > prevent the reader that just unlocked from seeing anything that happens
> > > > > > _after_ the synchronize_rcu.
> > > > > 
> > > > > Actually, I recall an smp_mb() being added, but am not seeing it anywhere
> > > > > relevant to wait_for_completion().  So I might need to add the smp_mb()
> > > > > to synchronize_rcu() and remove the patch (retaining the typo fix).  :-/
> > > > 
> > > > No problem, I'm glad atleast the patch resurfaced the topic of the potential
> > > > issue :-)
> > > 
> > > And an smp_mb() is needed in Tree RCU's __wait_rcu_gp().  This is
> > > because wait_for_completion() might get a "fly-by" wakeup, which would
> > > mean no ordering for code naively thinking that it was ordered after a
> > > grace period.
> > > 
> > > > > The short form answer is that anything before a grace period on any CPU
> > > > > must be seen by any CPU as being before anything on any CPU after that
> > > > > same grace period.  This guarantee requires a rather big hammer.
> > > > > 
> > > > > But yes, let's talk at LPC!
> > > > 
> > > > Sounds great, looking forward to discussing this.
> > > 
> > > Would it make sense to have an RCU-implementation BoF?
> > > 
> > > > > > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > > > > > me that the RCU reader-sections are virtually extended both forward and
> > > > > > backward and whereever it ends, those paths do heavy-weight synchronization
> > > > > > that should be sufficient to prevent memory ordering issues (such as those
> > > > > > you mentioned in the Requierments document). That is exactly why we don't
> > > > > > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > > > > > those are not needed. So that answer made sense, but then now on going
> > > > > > through the 'Memory Ordering' document, I see that you mentioned there is
> > > > > > reliance on the locking. Is that reliance on locking necessary to maintain
> > > > > > ordering then?
> > > > > 
> > > > > There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> > > > > that implements the all-to-all memory ordering mentioned above.  But it
> > > > > also needs to handle all the possible complete()/wait_for_completion()
> > > > > races, even those assisted by hypervisor vCPU preemption.
> > > > 
> > > > I see, so it sounds like the lock network is just a partial solution. For
> > > > some reason I thought before that complete() was even called on the CPU
> > > > executing the callback, all the CPUs would have acquired and released a lock
> > > > in the "lock network" atleast once thus ensuring the ordering (due to the
> > > > fact that the quiescent state reporting has to travel up the tree starting
> > > > from the leaves), but I think that's not necessarily true so I see your point
> > > > now.
> > > 
> > > There is indeed a lock that is unconditionally acquired and released by
> > > wait_for_completion(), but it lacks the smp_mb__after_unlock_lock() that
> > > is required to get full-up any-to-any ordering.  And unfortunate timing
> > > (as well as spurious wakeups) allow the interaction to have only normal
> > > lock-release/acquire ordering, which does not suffice in all cases.
> > 
> > Sorry to be so persistent, but I did spend some time on this and I still
> > don't get why every CPU would _not_ have executed smp_mb__after_unlock_lock at least
> > once before the wait_for_completion() returns, because every CPU should have
> > atleast called rcu_report_qs_rdp() -> rcu_report_qs_rnp() atleast once to
> > report its QS up the tree right?. Before that procedure, the complete()
> > cannot happen because the complete() itself is in an RCU callback which is
> > executed only once all the QS(s) have been reported.
> > 
> > So I still couldn't see how the synchronize_rcu can return without the
> > rcu_report_qs_rnp called atleast once on the CPU reporting its QS during a
> > grace period.
> > 
> > Would it be possible to provide a small example showing this in least number
> > of steps? I appreciate your time and it would be really helpful. If you feel
> > its too complicated, then feel free to keep this for LPC discussion :)
> 
> The key point is that "at least once" does not suffice, other than for the
> CPU that detects the end of the grace period.  The rest of the CPUs must
> do at least -two- full barriers, which could of course be either smp_mb()
> on the one hand or smp_mb__after_unlock_lock() after a lock on the other.

I thought I'll atleast get an understanding of the "atleast two full
barriers" point and ask you any questions at LPC, because that's what I'm
missing I think. Trying to understand what can go wrong without two full
barriers. I'm sure an RCU implementation BoF could really in this regard.

I guess its also documented somewhere in Tree-RCU-Memory-Ordering.html but a
quick search through that document didn't show a mention of the two full
barriers need.. I think its also a great idea for us to document it there
and/or discuss it during the conference.

I went through the litmus test here for some hints on the two-barriers but
couldn't find any:
https://lkml.org/lkml/2017/10/5/636

Atleast this commit made me think no extra memory barrier is needed for
tree RCU:  :-\
https://lore.kernel.org/patchwork/patch/813386/

I'm sure your last email will be useful to me in the future once I can make
more sense of the ordering and the need for two full barriers, so thanks a
lot for writing it!

- Joel


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-11-04  3:49                 ` Joel Fernandes
@ 2018-11-05  3:43                   ` Paul E. McKenney
  2018-11-05  4:43                     ` Joel Fernandes
  0 siblings, 1 reply; 16+ messages in thread
From: Paul E. McKenney @ 2018-11-05  3:43 UTC (permalink / raw)
  To: Joel Fernandes; +Cc: linux-kernel

On Sat, Nov 03, 2018 at 08:49:56PM -0700, Joel Fernandes wrote:
> On Sat, Nov 03, 2018 at 04:22:59PM -0700, Paul E. McKenney wrote:
> > On Fri, Nov 02, 2018 at 10:12:26PM -0700, Joel Fernandes wrote:
> > > On Thu, Nov 01, 2018 at 09:13:07AM -0700, Paul E. McKenney wrote:
> > > > On Wed, Oct 31, 2018 at 10:00:19PM -0700, Joel Fernandes wrote:
> > > > > On Wed, Oct 31, 2018 at 11:17:48AM -0700, Paul E. McKenney wrote:
> > > > > > On Tue, Oct 30, 2018 at 06:11:19PM -0700, Joel Fernandes wrote:
> > > > > > > Hi Paul,
> > > > > > > 
> > > > > > > On Tue, Oct 30, 2018 at 04:43:36PM -0700, Paul E. McKenney wrote:
> > > > > > > > On Tue, Oct 30, 2018 at 03:26:49PM -0700, Joel Fernandes wrote:
> > > > > > > > > Hi Paul,
> > > > > > > > > 
> > > > > > > > > On Sat, Oct 27, 2018 at 09:30:46PM -0700, Joel Fernandes (Google) wrote:
> > > > > > > > > > As per this thread [1], it seems this smp_mb isn't needed anymore:
> > > > > > > > > > "So the smp_mb() that I was trying to add doesn't need to be there."
> > > > > > > > > > 
> > > > > > > > > > So let us remove this part from the memory ordering documentation.
> > > > > > > > > > 
> > > > > > > > > > [1] https://lkml.org/lkml/2017/10/6/707
> > > > > > > > > > 
> > > > > > > > > > Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> > > > > > > > > 
> > > > > > > > > I was just checking about this patch. Do you feel it is correct to remove
> > > > > > > > > this part from the docs? Are you satisified that a barrier isn't needed there
> > > > > > > > > now? Or did I miss something?
> > > > > > > > 
> > > > > > > > Apologies, it got lost in the shuffle.  I have now applied it with a
> > > > > > > > bit of rework to the commit log, thank you!
> > > > > > > 
> > > > > > > No worries, thanks for taking it!
> > > > > > > 
> > > > > > > Just wanted to update you on my progress reading/correcting the docs. The
> > > > > > > 'Memory Ordering' is taking a bit of time so I paused that and I'm focusing
> > > > > > > on finishing all the other low hanging fruit. This activity is mostly during
> > > > > > > night hours after the baby is asleep but some times I also manage to sneak it
> > > > > > > into the day job ;-)
> > > > > > 
> > > > > > If there is anything I can do to make this a more sustainable task for
> > > > > > you, please do not keep it a secret!!!
> > > > > 
> > > > > Thanks a lot, that means a lot to me! Will do!
> > > > > 
> > > > > > > BTW I do want to discuss about this smp_mb patch above with you at LPC if you
> > > > > > > had time, even though we are removing it from the documentation. I thought
> > > > > > > about it a few times, and I was not able to fully appreciate the need for the
> > > > > > > barrier (that is even assuming that complete() etc did not do the right
> > > > > > > thing).  Specifically I was wondering same thing Peter said in the above
> > > > > > > thread I think that - if that rcu_read_unlock() triggered all the spin
> > > > > > > locking up the tree of nodes, then why is that locking not sufficient to
> > > > > > > prevent reads from the read-side section from bleeding out? That would
> > > > > > > prevent the reader that just unlocked from seeing anything that happens
> > > > > > > _after_ the synchronize_rcu.
> > > > > > 
> > > > > > Actually, I recall an smp_mb() being added, but am not seeing it anywhere
> > > > > > relevant to wait_for_completion().  So I might need to add the smp_mb()
> > > > > > to synchronize_rcu() and remove the patch (retaining the typo fix).  :-/
> > > > > 
> > > > > No problem, I'm glad atleast the patch resurfaced the topic of the potential
> > > > > issue :-)
> > > > 
> > > > And an smp_mb() is needed in Tree RCU's __wait_rcu_gp().  This is
> > > > because wait_for_completion() might get a "fly-by" wakeup, which would
> > > > mean no ordering for code naively thinking that it was ordered after a
> > > > grace period.
> > > > 
> > > > > > The short form answer is that anything before a grace period on any CPU
> > > > > > must be seen by any CPU as being before anything on any CPU after that
> > > > > > same grace period.  This guarantee requires a rather big hammer.
> > > > > > 
> > > > > > But yes, let's talk at LPC!
> > > > > 
> > > > > Sounds great, looking forward to discussing this.
> > > > 
> > > > Would it make sense to have an RCU-implementation BoF?
> > > > 
> > > > > > > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > > > > > > me that the RCU reader-sections are virtually extended both forward and
> > > > > > > backward and whereever it ends, those paths do heavy-weight synchronization
> > > > > > > that should be sufficient to prevent memory ordering issues (such as those
> > > > > > > you mentioned in the Requierments document). That is exactly why we don't
> > > > > > > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > > > > > > those are not needed. So that answer made sense, but then now on going
> > > > > > > through the 'Memory Ordering' document, I see that you mentioned there is
> > > > > > > reliance on the locking. Is that reliance on locking necessary to maintain
> > > > > > > ordering then?
> > > > > > 
> > > > > > There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> > > > > > that implements the all-to-all memory ordering mentioned above.  But it
> > > > > > also needs to handle all the possible complete()/wait_for_completion()
> > > > > > races, even those assisted by hypervisor vCPU preemption.
> > > > > 
> > > > > I see, so it sounds like the lock network is just a partial solution. For
> > > > > some reason I thought before that complete() was even called on the CPU
> > > > > executing the callback, all the CPUs would have acquired and released a lock
> > > > > in the "lock network" atleast once thus ensuring the ordering (due to the
> > > > > fact that the quiescent state reporting has to travel up the tree starting
> > > > > from the leaves), but I think that's not necessarily true so I see your point
> > > > > now.
> > > > 
> > > > There is indeed a lock that is unconditionally acquired and released by
> > > > wait_for_completion(), but it lacks the smp_mb__after_unlock_lock() that
> > > > is required to get full-up any-to-any ordering.  And unfortunate timing
> > > > (as well as spurious wakeups) allow the interaction to have only normal
> > > > lock-release/acquire ordering, which does not suffice in all cases.
> > > 
> > > Sorry to be so persistent, but I did spend some time on this and I still
> > > don't get why every CPU would _not_ have executed smp_mb__after_unlock_lock at least
> > > once before the wait_for_completion() returns, because every CPU should have
> > > atleast called rcu_report_qs_rdp() -> rcu_report_qs_rnp() atleast once to
> > > report its QS up the tree right?. Before that procedure, the complete()
> > > cannot happen because the complete() itself is in an RCU callback which is
> > > executed only once all the QS(s) have been reported.
> > > 
> > > So I still couldn't see how the synchronize_rcu can return without the
> > > rcu_report_qs_rnp called atleast once on the CPU reporting its QS during a
> > > grace period.
> > > 
> > > Would it be possible to provide a small example showing this in least number
> > > of steps? I appreciate your time and it would be really helpful. If you feel
> > > its too complicated, then feel free to keep this for LPC discussion :)
> > 
> > The key point is that "at least once" does not suffice, other than for the
> > CPU that detects the end of the grace period.  The rest of the CPUs must
> > do at least -two- full barriers, which could of course be either smp_mb()
> > on the one hand or smp_mb__after_unlock_lock() after a lock on the other.
> 
> I thought I'll atleast get an understanding of the "atleast two full
> barriers" point and ask you any questions at LPC, because that's what I'm
> missing I think. Trying to understand what can go wrong without two full
> barriers. I'm sure an RCU implementation BoF could really in this regard.
> 
> I guess its also documented somewhere in Tree-RCU-Memory-Ordering.html but a
> quick search through that document didn't show a mention of the two full
> barriers need.. I think its also a great idea for us to document it there
> and/or discuss it during the conference.
> 
> I went through the litmus test here for some hints on the two-barriers but
> couldn't find any:
> https://lkml.org/lkml/2017/10/5/636
> 
> Atleast this commit made me think no extra memory barrier is needed for
> tree RCU:  :-\
> https://lore.kernel.org/patchwork/patch/813386/
> 
> I'm sure your last email will be useful to me in the future once I can make
> more sense of the ordering and the need for two full barriers, so thanks a
> lot for writing it!

Hmmm...  I have had this argument before, haven't I?  Perhaps I should
take some time and get my story straight.  ;-)

In my defense, I have been doing RCU since the early 1990s, long before
executable formal memory models.  I kept it working through sheer
paranoia, and that is a hard habit to shake...

							Thanx, Paul


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

* Re: [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu
  2018-11-05  3:43                   ` Paul E. McKenney
@ 2018-11-05  4:43                     ` Joel Fernandes
  0 siblings, 0 replies; 16+ messages in thread
From: Joel Fernandes @ 2018-11-05  4:43 UTC (permalink / raw)
  To: Paul E. McKenney; +Cc: linux-kernel

On Sun, Nov 04, 2018 at 07:43:30PM -0800, Paul E. McKenney wrote:
[...]
> > > > > > > > Also about GP memory ordering and RCU-tree-locking, I think you mentioned to
> > > > > > > > me that the RCU reader-sections are virtually extended both forward and
> > > > > > > > backward and whereever it ends, those paths do heavy-weight synchronization
> > > > > > > > that should be sufficient to prevent memory ordering issues (such as those
> > > > > > > > you mentioned in the Requierments document). That is exactly why we don't
> > > > > > > > need explicit barriers during rcu_read_unlock. If I recall I asked you why
> > > > > > > > those are not needed. So that answer made sense, but then now on going
> > > > > > > > through the 'Memory Ordering' document, I see that you mentioned there is
> > > > > > > > reliance on the locking. Is that reliance on locking necessary to maintain
> > > > > > > > ordering then?
> > > > > > > 
> > > > > > > There is a "network" of locking augmented by smp_mb__after_unlock_lock()
> > > > > > > that implements the all-to-all memory ordering mentioned above.  But it
> > > > > > > also needs to handle all the possible complete()/wait_for_completion()
> > > > > > > races, even those assisted by hypervisor vCPU preemption.
> > > > > > 
> > > > > > I see, so it sounds like the lock network is just a partial solution. For
> > > > > > some reason I thought before that complete() was even called on the CPU
> > > > > > executing the callback, all the CPUs would have acquired and released a lock
> > > > > > in the "lock network" atleast once thus ensuring the ordering (due to the
> > > > > > fact that the quiescent state reporting has to travel up the tree starting
> > > > > > from the leaves), but I think that's not necessarily true so I see your point
> > > > > > now.
> > > > > 
> > > > > There is indeed a lock that is unconditionally acquired and released by
> > > > > wait_for_completion(), but it lacks the smp_mb__after_unlock_lock() that
> > > > > is required to get full-up any-to-any ordering.  And unfortunate timing
> > > > > (as well as spurious wakeups) allow the interaction to have only normal
> > > > > lock-release/acquire ordering, which does not suffice in all cases.
> > > > 
> > > > Sorry to be so persistent, but I did spend some time on this and I still
> > > > don't get why every CPU would _not_ have executed smp_mb__after_unlock_lock at least
> > > > once before the wait_for_completion() returns, because every CPU should have
> > > > atleast called rcu_report_qs_rdp() -> rcu_report_qs_rnp() atleast once to
> > > > report its QS up the tree right?. Before that procedure, the complete()
> > > > cannot happen because the complete() itself is in an RCU callback which is
> > > > executed only once all the QS(s) have been reported.
> > > > 
> > > > So I still couldn't see how the synchronize_rcu can return without the
> > > > rcu_report_qs_rnp called atleast once on the CPU reporting its QS during a
> > > > grace period.
> > > > 
> > > > Would it be possible to provide a small example showing this in least number
> > > > of steps? I appreciate your time and it would be really helpful. If you feel
> > > > its too complicated, then feel free to keep this for LPC discussion :)
> > > 
> > > The key point is that "at least once" does not suffice, other than for the
> > > CPU that detects the end of the grace period.  The rest of the CPUs must
> > > do at least -two- full barriers, which could of course be either smp_mb()
> > > on the one hand or smp_mb__after_unlock_lock() after a lock on the other.
> > 
> > I thought I'll atleast get an understanding of the "atleast two full
> > barriers" point and ask you any questions at LPC, because that's what I'm
> > missing I think. Trying to understand what can go wrong without two full
> > barriers. I'm sure an RCU implementation BoF could really in this regard.
> > 
> > I guess its also documented somewhere in Tree-RCU-Memory-Ordering.html but a
> > quick search through that document didn't show a mention of the two full
> > barriers need.. I think its also a great idea for us to document it there
> > and/or discuss it during the conference.
> > 
> > I went through the litmus test here for some hints on the two-barriers but
> > couldn't find any:
> > https://lkml.org/lkml/2017/10/5/636
> > 
> > Atleast this commit made me think no extra memory barrier is needed for
> > tree RCU:  :-\
> > https://lore.kernel.org/patchwork/patch/813386/
> > 
> > I'm sure your last email will be useful to me in the future once I can make
> > more sense of the ordering and the need for two full barriers, so thanks a
> > lot for writing it!
> 
> Hmmm...  I have had this argument before, haven't I?  Perhaps I should
> take some time and get my story straight.  ;-)
> 
> In my defense, I have been doing RCU since the early 1990s, long before
> executable formal memory models.  I kept it working through sheer
> paranoia, and that is a hard habit to shake...

Sure no problem, thanks a lot for taking another look into it!

Regards,

- Joel


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

end of thread, other threads:[~2018-11-05  4:43 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-28  4:30 [RFC] doc: rcu: remove note on smp_mb during synchronize_rcu Joel Fernandes (Google)
2018-10-30 22:26 ` Joel Fernandes
2018-10-30 23:43   ` Paul E. McKenney
2018-10-31  1:11     ` Joel Fernandes
2018-10-31 18:17       ` Paul E. McKenney
2018-11-01  5:00         ` Joel Fernandes
2018-11-01 16:13           ` Paul E. McKenney
2018-11-02  6:15             ` Joel Fernandes
2018-11-02 20:00               ` Paul E. McKenney
2018-11-02 22:14                 ` Joel Fernandes
2018-11-02 22:24                   ` Paul E. McKenney
2018-11-03  5:12             ` Joel Fernandes
2018-11-03 23:22               ` Paul E. McKenney
2018-11-04  3:49                 ` Joel Fernandes
2018-11-05  3:43                   ` Paul E. McKenney
2018-11-05  4:43                     ` Joel Fernandes

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.