LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Daniel Jordan <daniel.m.jordan@oracle.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: parri.andrea@gmail.com, will.deacon@arm.com,
	peterz@infradead.org, 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: Wed, 4 Apr 2018 17:10:05 -0400	[thread overview]
Message-ID: <4fa47ea8-e208-16d6-3b78-747049e3ee53@oracle.com> (raw)
In-Reply-To: <Pine.LNX.4.44L0.1804041623530.1401-100000@iolanthe.rowland.org>

On 04/04/2018 04:35 PM, 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.
> 
>> I have my doubts because memory-barriers.txt doesn't talk much about
>> loops and because of what that document says here:
>>
>>     In addition, control dependencies apply only to the then-clause and
>>     else-clause of the if-statement in question.  In particular, it does
>>     not necessarily apply to code following the if-statement:
>>
>>     	q = READ_ONCE(a);
>>     	if (q) {
>>     		WRITE_ONCE(b, 1);
>>     	} else {
>>     		WRITE_ONCE(b, 2);
>>     	}
>>     	WRITE_ONCE(c, 1);  /* BUG: No ordering against the read from 'a'. */
> 
> This refers to situations where the two code paths meet up at the end
> of the "if" statement.  If they don't meet up (because one of the paths
> branches away -- especially if it branches backward) then the
> disclaimer doesn't apply, and everything following the "if" is
> dependent.

Ok, that's the part I wasn't getting: this is how the while loop changes 
the situation.

> The reason is because the compiler knows that code following the "if"
> statement will be executed unconditionally if the paths meet up, so it
> can move that code back before the "if" (provided nothing else prevents
> such motion).  But if the paths don't meet up, the compiler can't
> perform the code motion -- if it did then the program might end up
> executing something that should not have been executed!
> 
>> It's not obvious to me how the then-clause/else-clause idea maps onto
>> loops, but if we think of the example at the top like this...
>>
>>     while (1) {
>>         if (!READ_ONCE(a)) {
>>             WRITE_ONCE(c, 1);
>>             break;
>>         }
>>     }
>>
>> ...then the dependent store is within the then-clause.  Viewed this way,
>> it seems there would be a control dependency between a and c.
>>
>> Is that right?
> 
> 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.

That clears it up, thanks very much!

  reply	other threads:[~2018-04-04 21:16 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 [this message]
2018-04-05  7:32   ` Peter Zijlstra
2018-04-05 14:35     ` Alan Stern
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=4fa47ea8-e208-16d6-3b78-747049e3ee53@oracle.com \
    --to=daniel.m.jordan@oracle.com \
    --cc=akiyks@gmail.com \
    --cc=boqun.feng@gmail.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=stern@rowland.harvard.edu \
    --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).