LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Alan Stern <stern@rowland.harvard.edu>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Daniel Jordan <daniel.m.jordan@oracle.com>,
	<parri.andrea@gmail.com>, <will.deacon@arm.com>,
	<boqun.feng@gmail.com>, <npiggin@gmail.com>,
	<dhowells@redhat.com>, <j.alglave@ucl.ac.uk>,
	<luc.maranget@inria.fr>, <paulmck@linux.vnet.ibm.com>,
	<akiyks@gmail.com>, <linux-kernel@vger.kernel.org>,
	Steven Sistare <steven.sistare@oracle.com>,
	Pasha Tatashin <pasha.tatashin@oracle.com>
Subject: Re: Control dependency between prior load in while condition and later store?
Date: Thu, 5 Apr 2018 10:35:22 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.44L0.1804051022170.1384-100000@iolanthe.rowland.org> (raw)
In-Reply-To: <20180405073204.GP4043@hirez.programming.kicks-ass.net>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: TEXT/PLAIN; charset=UTF-8, Size: 1920 bytes --]

On Thu, 5 Apr 2018, Peter Zijlstra wrote:

> On Wed, Apr 04, 2018 at 04:35:32PM -0400, Alan Stern wrote:
> > On Wed, 4 Apr 2018, Daniel Jordan wrote:
> > 
> > > A question for memory-barriers.txt aficionados.
> > > 
> > > Is there a control dependency between the prior load of 'a' and the 
> > > later store of 'c'?:
> > > 
> > >    while (READ_ONCE(a));
> > >    WRITE_ONCE(c, 1);
> > 
> > I would say that yes, there is.
> 
> Indeed.
> 
> > Yes, except that a more accurate view of the object code would be
> > something like this:
> > 
> > Loop:	r1 = READ_ONCE(a);
> > 	if (r1)
> > 		goto Loop;
> > 	else
> > 		;	// Do nothing
> > 	WRITE_ONCE(c, 1);
> > 
> > Here you can see that one path branches backward, so everything 
> > following the "if" is dependent on the READ_ONCE.
> 
> Agreed, and I think I even have code that relies on such a pattern
> somewhere.. Ah.. yes, see smp_cond_load_acquire().

One does have to be very careful when talking about compiler behavior.  
This happens to be a particularly delicate point.  My old copy of the
C++11 draft standard says (section 1.10 paragraph 24):


The implementation may assume that any thread will eventually do one of 
the following:

â- terminate,
â- make a call to a library I/O function,
â- access or modify a volatile object, or
â- perform a synchronization operation or an atomic operation.

[ Note: This is intended to allow compiler transformations such as 
removal of empty loops, even when termination cannot be proven. - end 
note ]


In this example, READ_ONCE() is in fact a volatile access, so we're 
okay.  But if it weren't, the compiler might decide to assume the loop 
will eventually terminate, meaning that the WRITE_ONCE() would always 
be executed eventually.  Then there would be nothing to prevent the 
compiler from moving the WRITE_ONCE() up before the start of the loop, 
which would of course destroy the control dependency.

Alan

  reply	other threads:[~2018-04-05 14:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-04 19:29 Daniel Jordan
2018-04-04 20:35 ` Alan Stern
2018-04-04 21:10   ` Daniel Jordan
2018-04-05  7:32   ` Peter Zijlstra
2018-04-05 14:35     ` Alan Stern [this message]
2018-04-05 14:56       ` Peter Zijlstra
2018-04-05 15:16         ` Alan Stern

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=Pine.LNX.4.44L0.1804051022170.1384-100000@iolanthe.rowland.org \
    --to=stern@rowland.harvard.edu \
    --cc=akiyks@gmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=daniel.m.jordan@oracle.com \
    --cc=dhowells@redhat.com \
    --cc=j.alglave@ucl.ac.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luc.maranget@inria.fr \
    --cc=npiggin@gmail.com \
    --cc=parri.andrea@gmail.com \
    --cc=pasha.tatashin@oracle.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=steven.sistare@oracle.com \
    --cc=will.deacon@arm.com \
    --subject='Re: Control dependency between prior load in while condition and later store?' \
    /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

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