public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: linux-kernel@vger.kernel.org, Ingo Molnar <mingo@kernel.org>,
	Lai Jiangshan <jiangshanlai@gmail.com>,
	dipankar@in.ibm.com, Andrew Morton <akpm@linux-foundation.org>,
	josh@joshtriplett.org, Thomas Gleixner <tglx@linutronix.de>,
	Peter Zijlstra <peterz@infradead.org>,
	rostedt <rostedt@goodmis.org>,
	David Howells <dhowells@redhat.com>,
	edumazet@google.com, dvhart@linux.intel.com, fweisbec@gmail.com,
	Oleg Nesterov <oleg@redhat.com>,
	bobby prani <bobby.prani@gmail.com>
Subject: Re: [PATCH tip/core/rcu 02/14] documentation: Fix control dependency and identical stores
Date: Wed, 24 Feb 2016 13:40:13 -0800	[thread overview]
Message-ID: <20160224214013.GF3522@linux.vnet.ibm.com> (raw)
In-Reply-To: <467632883.7240.1456348324456.JavaMail.zimbra@efficios.com>

On Wed, Feb 24, 2016 at 09:12:04PM +0000, Mathieu Desnoyers wrote:
> ----- On Feb 24, 2016, at 12:00 AM, Paul E. McKenney paulmck@linux.vnet.ibm.com wrote:
> 
> > The summary of the "CONTROL DEPENDENCIES" section incorrectly states that
> > barrier() may be used to prevent compiler reordering when more than one
> > leg of the control-dependent "if" statement start with identical stores.
> > This is incorrect at high optimization levels.  This commit therefore
> > updates the summary to match the detailed description.
> > 
> > Reported by: Jianyu Zhan <nasa4836@gmail.com>
> > Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
> > ---
> > Documentation/memory-barriers.txt | 10 +++++++---
> > 1 file changed, 7 insertions(+), 3 deletions(-)
> > 
> > diff --git a/Documentation/memory-barriers.txt
> > b/Documentation/memory-barriers.txt
> > index 904ee42d078e..e26058d3e253 100644
> > --- a/Documentation/memory-barriers.txt
> > +++ b/Documentation/memory-barriers.txt
> > @@ -800,9 +800,13 @@ In summary:
> >       use smp_rmb(), smp_wmb(), or, in the case of prior stores and
> >       later loads, smp_mb().
> > 
> > -  (*) If both legs of the "if" statement begin with identical stores
> > -      to the same variable, a barrier() statement is required at the
> > -      beginning of each leg of the "if" statement.
> > +  (*) If both legs of the "if" statement begin with identical stores to
> > +      the same variable, then those stores must be ordered, either by
> > +      preceding both of them with smp_mb() or by using smp_store_release()
> > +      to carry out the stores.  Please note that it is -not- sufficient
> > +      to use barrier() at beginning of each leg of the "if" statement,
> > +      as optimizing compilers do not necessarily respect barrier()
> > +      in this case.
> 
> Hrm, I really don't understand this one.
> 
> One caveat, as stated here, would be that optimizing compilers
> can reorder instruction with respect to barrier() placed at the
> beginning of if/else legs that start with identical stores.
> 
> It goes on stating that "smp_mb() or smp_store_release()" should
> be used rather than barrier() in those cases.
> 
> I don't get how, from a compiler optimization perspective,
> barrier() is any different from smp_mb().
> 
> #define barrier() __asm__ __volatile__("": : :"memory")
> 
> vs
> 
> #define mb()    asm volatile("mfence":::"memory")
> 
> What the compiler would observe is a "memory" clobber in both
> cases.
> 
> Now if the stated cause of this issue would have been
> internal reordering of those identical stores within the
> processor, I would understand that smp_mb() has an
> effect which differs from the compiler barrier, but since
> the paragraph begins by stating that this is purely for
> compiler optimizations, I'm confused.
> 
> What am I missing there ?
> 
> Thanks,
> 
> Mathieu
> 
> 
> > 
> >   (*) Control dependencies require at least one run-time conditional
> >       between the prior load and the subsequent store, and this

Let's take the example, replace barrier() with smp_mb(), and see what
happens:

	q = READ_ONCE(a);
	if (q) {
		smp_mb();
		WRITE_ONCE(b, p);
		do_something();
	} else {
		smp_mb();
		WRITE_ONCE(b, p);
		do_something_else();
	}

Given the same compiler transformation:

	q = READ_ONCE(a);
	smp_mb();
	WRITE_ONCE(b, p);  /* BUG: No ordering vs. load from a!!! */
	if (q) {
		/* WRITE_ONCE(b, p); -- moved up, BUG!!! */
		do_something();
	} else {
		/* WRITE_ONCE(b, p); -- moved up, BUG!!! */
		do_something_else();
	}

So ordering between the read from "a" and the write to "b" is still
preserved.  The reason this works is that the smp_mb() does all the
ordering, so the fact that the control dependency has been eliminated
is irrelevant.

							Thanx, Paul

  reply	other threads:[~2016-02-24 21:40 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-24  5:00 [PATCH tip/core/rcu 0/14] Documentation updates for 4.6 Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 01/14] documentation: Add real-time requirements from CPU-bound workloads Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 02/14] documentation: Fix control dependency and identical stores Paul E. McKenney
2016-02-24 21:12   ` Mathieu Desnoyers
2016-02-24 21:40     ` Paul E. McKenney [this message]
2016-02-25  6:41       ` Jianyu Zhan
2016-02-25 14:08         ` Paul E. McKenney
2016-02-25  8:21       ` Peter Zijlstra
2016-02-25 14:07         ` Paul E. McKenney
2016-02-25 14:48           ` Peter Zijlstra
2016-02-25 15:42             ` Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 03/14] documentation: Fix memory-barriers.txt section references Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 04/14] documentation: Add synchronize_rcu_mult() to the requirements Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 05/14] documentation: Remove obsolete reference to RCU-protected indexes Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 06/14] documentation: Subsequent writes ordered by rcu_dereference() Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 07/14] documentation: Distinguish between local and global transitivity Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 08/14] documentation: Add alternative release-acquire outcome Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 09/14] documentation: Add documentation for RCU's major data structures Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 10/14] documentation: Explain why rcu_read_lock() needs no barrier() Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 11/14] documentation: Transitivity is not cumulativity Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 12/14] documentation: Document illegality of call_rcu() from offline CPUs Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 13/14] documentation: Explain how RCU's combining tree fights contention Paul E. McKenney
2016-02-24  5:00 ` [PATCH tip/core/rcu 14/14] documentation: Clarify compiler store-fusion example Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160224214013.GF3522@linux.vnet.ibm.com \
    --to=paulmck@linux.vnet.ibm.com \
    --cc=akpm@linux-foundation.org \
    --cc=bobby.prani@gmail.com \
    --cc=dhowells@redhat.com \
    --cc=dipankar@in.ibm.com \
    --cc=dvhart@linux.intel.com \
    --cc=edumazet@google.com \
    --cc=fweisbec@gmail.com \
    --cc=jiangshanlai@gmail.com \
    --cc=josh@joshtriplett.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox