Linux filesystem development
 help / color / mirror / Atom feed
From: Will Deacon <will@kernel.org>
To: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Mikulas Patocka <mpatocka@redhat.com>,
	Ard Biesheuvel <ardb@kernel.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Alan Stern <stern@rowland.harvard.edu>,
	Andrea Parri <parri.andrea@gmail.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	Nicholas Piggin <npiggin@gmail.com>,
	David Howells <dhowells@redhat.com>,
	Jade Alglave <j.alglave@ucl.ac.uk>,
	Luc Maranget <luc.maranget@inria.fr>,
	Akira Yokosawa <akiyks@gmail.com>,
	Daniel Lustig <dlustig@nvidia.com>,
	Joel Fernandes <joel@joelfernandes.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-arch <linux-arch@vger.kernel.org>,
	linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH v2] make buffer_locked provide an acquire semantics
Date: Tue, 2 Aug 2022 09:54:55 +0100	[thread overview]
Message-ID: <20220802085455.GC26962@willie-the-truck> (raw)
In-Reply-To: <20220801192035.GA2860372@paulmck-ThinkPad-P17-Gen-1>

On Mon, Aug 01, 2022 at 12:20:35PM -0700, Paul E. McKenney wrote:
> On Mon, Aug 01, 2022 at 04:41:09PM +0100, Will Deacon wrote:
> > Apologies for the slow response here; believe it or not, I was attending
> > a workshop about memory ordering.
> 
> Nice!!!  Anything that I can/should know from that gathering?  ;-)

Oh come off it, you know this stuff already ;)

> > On Sun, Jul 31, 2022 at 10:30:11AM -0700, Paul E. McKenney wrote:
> > > On Sun, Jul 31, 2022 at 09:51:47AM -0700, Linus Torvalds wrote:
> > > > Even alpha is specified to be locally ordered wrt *one* memory
> > > > location, including for reads (See table 5-1: "Processor issue order",
> > > > and also 5.6.2.2: "Litmus test 2"). So if a previous read has seen a
> > > > new value, a subsequent read is not allowed to see an older one - even
> > > > without a memory barrier.
> > > > 
> > > > Will, Paul? Maybe that's only for overlapping loads/stores, not for
> > > > loads/loads. Because maybe alpha for once isn't the weakest possible
> > > > ordering.
> > > 
> > > The "bad boy" in this case is Itanium, which can do some VLIW reordering
> > > of accesses.  Or could, I am not sure that newer Itanium hardware
> > > does this.  But this is why Itanium compilers made volatile loads use
> > > the ld,acq instruction.
> > > 
> > > Which means that aligned same-sized marked accesses to a single location
> > > really do execute consistently with some global ordering, even on Itanium.
> > 
> > Although this is true, there's a really subtle issue which crops up if you
> > try to compose this read-after-read ordering with dependencies in the case
> > where the two reads read the same value (which is encapsulated by the
> > unusual RSW litmus test that I've tried to convert to C below):
> 
> RSW from the infamous test6.pdf, correct?

That's the badger. I've no doubt that you're aware of it already, but I
thought it was a useful exercise to transcribe it to C and have it on the
mailing list for folks to look at.

> > /* Global definitions; assume everything zero-initialised */
> > struct foo {
> > 	int *x;
> > };
> > 
> > int x;
> > struct foo foo;
> > struct foo *ptr;
> > 
> > 
> > /* CPU 0 */
> > WRITE_ONCE(x, 1);
> 
> Your x is RSW's z?

Yes.

> > WRITE_ONCE(foo.x, &x);
> 
> And your foo.x is RSW's x?  If so, the above WRITE_ONCE() could happen at
> compile time, correct?  Or in the initialization clause of a litmus test?

Yes, although I think it's a tiny bit more like real code to have it done
here, although it means that the "surprising" outcome relies on this being
reordered before the store to x.

> > /*
> >  * Release ordering to ensure that somebody following a non-NULL ptr will
> >  * see a fully-initialised 'foo'. smp_[w]mb() would work as well.
> >  */
> > smp_store_release(&ptr, &foo);
> 
> Your ptr is RSW's y, correct?

Yes.

> > /* CPU 1 */
> > int *xp1, *xp2, val;
> > struct foo *foop;
> > 
> > /* Load the global pointer and check that it's not NULL. */
> > foop = READ_ONCE(ptr);
> > if (!foop)
> > 	return;
> 
> A litmus tests can do this via the filter clause.

Indeed, but I was trying to make this look like C code for non-litmus
speakers!

> > /*
> >  * Load 'foo.x' via the pointer we just loaded. This is ordered after the
> >  * previous READ_ONCE() because of the address dependency.
> >  */
> > xp1 = READ_ONCE(foop->x);
> > 
> > /*
> >  * Load 'foo.x' directly via the global 'foo'.
> >  * _This is loading the same address as the previous READ_ONCE() and
> >  *  therefore cannot return a stale (NULL) value!_
> >  */
> > xp2 = READ_ONCE(foo.x);
> 
> OK, same location, but RSW calls only for po, not addr from the initial
> read to this read, got it.  (My first attempt left out this nuance,
> in case you were wondering.)

Right, there is only po from the initial read to this read. If there was an
address dependency, then we'd have a chain of address dependencies from the
first read to the last read on this CPU and the result (of x == 0) would be
forbidden.

> > /*
> >  * Load 'x' via the pointer we just loaded.
> >  * _We may see zero here!_
> >  */
> > val = READ_ONCE(*xp2);
> 
> And herd7/LKMM agree with this, at least assuming I got the litmus
> test right.  (I used RSW's variables as a cross-check.)

That's promising, but see below...

> C rsw
> 
> {
> 	a=0;
> 	x=z;
> 	y=a;
> 	z=0;
> }
> 
> P0(int *x, int **y, int *z)
> {
> 	WRITE_ONCE(*z, 1);
> 	WRITE_ONCE(*y, x);
> }

Ah wait, you need a barrier between these two writes, don't you? I used
an smp_store_release() but smp[w]_mb() should do too.

Will

  reply	other threads:[~2022-08-02  8:55 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-31 11:43 [PATCH] Add a read memory barrier to wait_on_buffer Mikulas Patocka
2022-07-31 12:00 ` Ard Biesheuvel
2022-07-31 13:41   ` Mikulas Patocka
2022-07-31 15:08     ` [PATCH v2] make buffer_locked provide an acquire semantics Mikulas Patocka
2022-07-31 16:51       ` Linus Torvalds
2022-07-31 17:30         ` Paul E. McKenney
2022-07-31 22:48           ` Matthew Wilcox
2022-08-01  3:20             ` Paul E. McKenney
2022-08-01 15:41           ` Will Deacon
2022-08-01 19:20             ` Paul E. McKenney
2022-08-02  8:54               ` Will Deacon [this message]
2022-08-02 13:49                 ` Paul E. McKenney
2022-08-02 15:29                   ` Paul E. McKenney
2022-07-31 20:39         ` Mikulas Patocka
2022-07-31 20:40           ` [PATCH v3 1/2] wait_bit: do read barrier after testing a bit Mikulas Patocka
2022-07-31 20:57             ` Linus Torvalds
2022-08-01 10:40               ` Mikulas Patocka
2022-08-01 10:43                 ` [PATCH v4 2/2] change buffer_locked, so that it has acquire semantics Mikulas Patocka
2022-08-01 14:37                   ` Matthew Wilcox
2022-08-01 15:01                     ` Mikulas Patocka
2022-08-05  3:22                       ` Matthew Wilcox
2022-08-07 11:37                         ` [PATCH v5] add barriers to buffer functions Mikulas Patocka
2022-08-07 14:50                           ` Matthew Wilcox
2022-08-08 14:26                             ` Mikulas Patocka
2022-08-08 14:40                               ` Matthew Wilcox
2022-08-08 14:57                                 ` Mikulas Patocka
2022-08-08 15:31                                   ` Paul E. McKenney
2022-08-08 15:39                                   ` Matthew Wilcox
2022-08-09 18:32                                     ` [PATCH v6] add barriers to buffer_uptodate and set_buffer_uptodate Mikulas Patocka
2022-08-09 19:44                                       ` Matthew Wilcox
2022-08-09 22:06                                       ` Linus Torvalds
2022-08-01 10:42               ` [PATCH v4 1/2] introduce test_bit_acquire and use it in wait_on_bit Mikulas Patocka
2022-08-01 15:54                 ` Will Deacon
2022-08-01 16:12                   ` Mikulas Patocka
2022-08-01 18:17                     ` Boqun Feng
2022-08-02  8:00                       ` David Laight
2022-08-02  8:40                     ` Will Deacon
2022-08-02 11:38                       ` Mikulas Patocka
2022-08-02 13:36                         ` Will Deacon
2022-08-02 15:57                           ` Mikulas Patocka
2022-08-01  0:27             ` [PATCH v3 1/2] wait_bit: do read barrier after testing a bit Alan Stern
2022-07-31 20:43           ` [PATCH v3 2/2] make buffer_locked provide an acquire semantics Mikulas Patocka
2022-07-31 20:51             ` Linus Torvalds
2022-07-31 22:14             ` Matthew Wilcox
2022-07-31 22:31               ` Ard Biesheuvel
2022-07-31 22:48                 ` Ard Biesheuvel
2022-07-31 20:46           ` [PATCH v2] " Linus Torvalds

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=20220802085455.GC26962@willie-the-truck \
    --to=will@kernel.org \
    --cc=akiyks@gmail.com \
    --cc=ardb@kernel.org \
    --cc=boqun.feng@gmail.com \
    --cc=dhowells@redhat.com \
    --cc=dlustig@nvidia.com \
    --cc=j.alglave@ucl.ac.uk \
    --cc=joel@joelfernandes.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luc.maranget@inria.fr \
    --cc=mpatocka@redhat.com \
    --cc=npiggin@gmail.com \
    --cc=parri.andrea@gmail.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=stern@rowland.harvard.edu \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /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