mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Re: [PATCH] Document Linux's memory barriers [try #5]
@ 2006-03-28 22:25 Suzanne Wood
  2006-03-29 17:54 ` David Howells
  0 siblings, 1 reply; 49+ messages in thread
From: Suzanne Wood @ 2006-03-28 22:25 UTC (permalink / raw)
  To: dhowells; +Cc: linux-kernel, paulmck

Hello,
This is just a question about intended meaning in a paragraph  
at the end of the "What are memory barriers?" section.

 > From: Paul E. McKenney Thu Mar 16 2006 - 18:14:11 EST 
 > On Wed, Mar 15, 2006 at 02:23:19PM +0000, David Howells wrote:
 > > 
 > > The attached patch documents the Linux kernel's memory barriers.
 > > 
 > > I've updated it from the comments I've been given.
 > > 
 > . . .
 > 
 > Good stuff!!! Please see comments interspersed, search for empty lines.
 > 
 > One particularly serious issue involve your smp_read_barrier_depends()
 > example.
 > 
 > > Signed-Off-By: David Howells <dhowells@redhat.com>
 > > ---
 > > warthog>diffstat -p1 /tmp/mb.diff 
 > > Documentation/memory-barriers.txt | 1039 
 > ++++++++++++++++++++++++++++++++++++++
 > > 1 files changed, 1039 insertions(+)
 > > 
 > > diff --git a/Documentation/memory-barriers.txt 
 > b/Documentation/memory-barriers.txt
 > > new file mode 100644
 > > index 0000000..fd7a6f1
 > > --- /dev/null
 > > +++ b/Documentation/memory-barriers.txt
 > > @@ -0,0 +1,1039 @@
 > > + ============================
 > > + LINUX KERNEL MEMORY BARRIERS
 > > + ============================
 
 . . .
 
 > > +=========================
 > > +WHAT ARE MEMORY BARRIERS?
 > > +=========================
 > > +
 
 > . . . 
 
 > > +It is also guaranteed that a CPU will be self-consistent: it will see its _own_
 > > +accesses appear to be correctly ordered, without the need for a memory barrier.
 > > +For instance with the following code:
 > > +
 > > + X = *A;
 > > + *A = Y;
 > > + Z = *A;
 > > +
 > > +assuming no intervention by an external influence, it can be taken that:
 > > +
 > > + (*) X will hold the old value of *A, and will never happen after the write and
 > > + thus end up being given the value that was assigned to *A from Y instead;
 
Seems like the subject of "will never happen" is the read from memory for the 
asmt to X, but does that sentence say that?  

 > > + and
 > > +
 > > + (*) Z will always be given the value in *A that was assigned there from Y, and
 > > + will never happen before the write, and thus end up with the same value
 > > + that was in *A initially.
 
Similarly, the read from memory for the asmt to Z won't precede the write of Y
to *A.
 
 > > +
 > > +(This is ignoring the fact that the value initially in *A may appear to be the
 > > +same as the value assigned to *A from Y).
 > > +
 > > +
 > > +=================================
 > > +WHERE ARE MEMORY BARRIERS NEEDED?
 > > +=================================

It seems to require more effort than necessary to understand in regard to
all that is presented in this document.

Thanks.
Suzanne

^ permalink raw reply	[flat|nested] 49+ messages in thread
* Re: [PATCH] Document Linux's memory barriers [try #5]
@ 2006-03-29 20:51 Suzanne Wood
  2006-03-30 20:18 ` David Howells
  0 siblings, 1 reply; 49+ messages in thread
From: Suzanne Wood @ 2006-03-29 20:51 UTC (permalink / raw)
  To: dhowells; +Cc: linux-kernel, paulmck

Hello and thank you for your response.

> From: "David Howells" Wednesday, March 29, 2006 9:54 AM
> 
>  Suzanne Wood wrote:
> 
> > Seems like the subject of "will never happen" is the read from memory for the 
> > asmt to X, but does that sentence say that?  
> 
> "asmt"?

assignment

> I agree that it doesn't make much sense, so how's this instead?
> 
> + However, it is guaranteed that a CPU will be self-consistent: it will see its
> + _own_ accesses appear to be correctly ordered, without the need for a memory
> + barrier.  For instance with the following code:
> + 
> + 	X = *A;
> + 	*A = Y;
> + 	Z = *A;
> + 
> + and assuming no intervention by an external influence, it can be taken that:
> + 
> +  (*) X will end up holding the original value of *A, as
> + 
> +  (*) the load of X from *A will never happen after the store of Y into *A, and
> +      thus
> + 
> +  (*) X will never be given instead the value that was assigned from Y to *A;
> +      and
> + 
> +  (*) Z will always be given the value in *A that was assigned there from Y, as
> + 
> +  (*) the load of Z from *A will never happen before the store, and thus
> + 
> +  (*) Z will never be given instead the value that was in *A initially.
> + 
> + (This ignores the fact that the value initially in *A may appear to be the same
> + as the value assigned to *A from Y).
> 
> I'm not sure I want to split the points up that way, but it does make them
> clearer.  I'm not sure that method of linking them works, since it looks like
> a bunch of incomplete statements.
> 
> Really, this should be described mathematically, if at all.

Do you mean to formalize preconditions on the value of Y and contents of A 
and consider postconditions after the execution of the three statements of 
the example where the value of X is the prior content of A and A contains and 
Z equals the value of Y.

> > It seems to require more effort than necessary to understand in regard to
> > all that is presented in this document.
> 
> Are you referring to my attempt to define a self-consistent CPU?  Or to the
> subject in general?

Sorry to be unclear.  I was just asking about the explanation of the 
self-consistent CPU example.  The other ideas in the document are more
difficult, so thought this part might be simplified.
 
> If the former, you may be right.  I'll look at compressing the whole thing
> down to a single paragraph.
> 
> David

Thanks again.
Suzanne

^ permalink raw reply	[flat|nested] 49+ messages in thread
* Re: [PATCH] Document Linux's memory barriers [try #5]
@ 2006-03-31  0:55 Suzanne Wood
  2006-03-31 14:51 ` David Howells
  0 siblings, 1 reply; 49+ messages in thread
From: Suzanne Wood @ 2006-03-31  0:55 UTC (permalink / raw)
  To: dhowells; +Cc: linux-kernel, paulmck

Hello.  Just minor questions below.  Thank you.

  > From: "David Howells" Thursday, March 30, 2006 12:18 PM
  > 
  > Suzanne Wood <suzannew@cs.pdx.edu> wrote:
  > 
  > > Do you mean to formalize preconditions on the value of Y and contents of A 
  > > and consider postconditions after the execution of the three statements of 
  > > the example where the value of X is the prior content of A and A contains and 
  > > Z equals the value of Y.
  > 
  > Something like that, yes.
  > 
  > How about the attached instead?  My explanation didn't give a write/write
  > example, so I've added that in too.
  > 
  > > Sorry to be unclear.  I was just asking about the explanation of the 
  > > self-consistent CPU example.  The other ideas in the document are more
  > > difficult, so thought this part might be simplified.
  > 
  > The whole subject is fraught with unclarity:-)
  > 
  > David
  > 
  > 
  > However, it is guaranteed that a CPU will be self-consistent: it will see its
  > _own_ accesses appear to be correctly ordered, without the need for a memory
  > barrier.  For instance with the following code:
  > 
  > 	U = *A;
  > 	*A = V;
  > 	*A = W;
  > 	X = *A;
  > 	*A = Y;
  > 	Z = *A;
  > 
  > and assuming no intervention by an external influence, it can be taken that the
  > final result will appear to be:
  > 
  > 	U == the original value of *A
  > 	X == W
  > 	Z == Y
  > 	*A == Y
  > 
  > The code above may cause the CPU to generate the full sequence of memory
  > accesses:
  > 
  > 	U=LOAD *A, STORE *A=V, STORE *A=W, X=LOAD *A, STORE *A=Y, Z=LOAD *A
  > 
  > But, without intervention, the sequence may have almost any combination of
  > elements barring the load of *A into U and the store of Y into *A discarded as
  > the CPU may combine or discard memory accesses as it sees fit, provided _its_
  > view of the world is consistent.  This leads to the following possible other
  > sequences:
  > 
  > 	U=LOAD *A, STORE *A=V, STORE *A=W, X=LOAD *A, STORE *A=Y
  > 	U=LOAD *A, STORE *A=V, STORE *A=W, STORE *A=Y, Z=LOAD *A
  > 	U=LOAD *A, STORE *A=V, STORE *A=W, STORE *A=Y
  > 	U=LOAD *A, STORE *A=V, STORE *A=Y, Z=LOAD *A
  > 	U=LOAD *A, STORE *A=V, STORE *A=Y
  > 	U=LOAD *A, STORE *A=W, X=LOAD *A, STORE *A=Y
  > 	U=LOAD *A, STORE *A=W, STORE *A=Y, Z=LOAD *A
  > 	U=LOAD *A, STORE *A=W, STORE *A=Y
  > 	U=LOAD *A, STORE *A=Y, Z=LOAD *A
  > 	U=LOAD *A, STORE *A=Y
  > 
  > Note:
  > 
  >  (*) STORE *A=W can only be dispensed with if X=LOAD *A is also dispensed with,
  >      otherwise X would be given the original value of *A or the value assigned
  >      there from V, and not the value assigned there from W.

OK, you can dispense with a store if the value is not used before another store 
to the same memory location.  So if, for some other reason, X = *A goes away, 
you discard *A = W.

  >  (*) STORE *A=V would probably be discarded entirely by the CPU - if not the
  >      compiler - as it's effect is overridden by STORE *A=W without anything
  >      seeing it.

Right, why include STORE *A=V on the first three possibilities?

  >  (*) STORE *A=Y may be deferred indefinitely until the CPU has some reason to
  >      perform it or discard it.

What's Z see?  Or is that load what you refer to as the reason to perform the store?

  >  (*) Even the two 'fixed' operations can be dispensed with if they can be
  >      combined or discarded with respect to the surrounding code.
  > 
  >  (*) The compiler may also combine, discard or defer elements of the sequence

Should these comments support the final result you provided above?  
E.g., X <- W may disappear in terms of your first note.
Thanks.
Suzanne

^ permalink raw reply	[flat|nested] 49+ messages in thread
* Re: [PATCH] Document Linux's memory barriers [try #5]
@ 2006-03-31 16:16 Suzanne Wood
  0 siblings, 0 replies; 49+ messages in thread
From: Suzanne Wood @ 2006-03-31 16:16 UTC (permalink / raw)
  To: dhowells; +Cc: linux-kernel, paulmck

Thank you.

  > From David Howells Fri Mar 31 06:51:59 2006

  > Suzanne Wood <suzannew@cs.pdx.edu> wrote:

  > > OK, you can dispense with a store if the value is not used before another
  > > store to the same memory location.  So if, for some other reason, X = *A
  > > goes away, you discard *A = W.

  > What I was actually thinking of is:

  > 	*A = Y;
  > 	Z = *A;

  > Can be changed by the compiler or the CPU into:

  > 	*A = Y;
  > 	Z = Y;

  > Which would eliminate the externally visible "Z = LOAD *A" entirely by
  > combination with the store.

  > However, that's reintroducing the concept of caching back into the abstract
  > CPU again - which complicates things once again.

  > I've decided to rewrite what I was trying to say to the attached.

  > David


  > However, it is guaranteed that a CPU will be self-consistent: it will see its
  > _own_ accesses appear to be correctly ordered, without the need for a memory
  > barrier.  For instance with the following code:

  > 	U = *A;
  > 	*A = V;
  > 	*A = W;
  > 	X = *A;
  > 	*A = Y;
  > 	Z = *A;

  > and assuming no intervention by an external influence, it can be assumed that
  > the final result will appear to be:

  > 	U == the original value of *A
  > 	X == W
  > 	Z == Y
  > 	*A == Y

  > The code above may cause the CPU to generate the full sequence of memory
  > accesses:

  > 	U=LOAD *A, STORE *A=V, STORE *A=W, X=LOAD *A, STORE *A=Y, Z=LOAD *A

  > in that order, but, without intervention, the sequence may have almost any
  > combination of elements combined or discarded, provided the program's view of
  > the world remains consistent.

  > The compiler may also combine, discard or defer elements of the sequence before
  > the CPU even sees them.

  > For instance:

  > 	*A = V;
  > 	*A = W;

  > may be reduced to:

  > 	*A = W;

  > since, without a write barrier, it can be assumed that the effect of the
  > storage of V to *A is lost.  Similarly:

  > 	*A = Y;
  > 	Z = *A;

  > may, without a memory barrier, be reduced to:

  > 	*A = Y;
  > 	Z = Y;

  > and the LOAD operation never appear outside of the CPU.

Looks good.  Now I can see the gain without a loss.
Thanks.
Suzanne

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

end of thread, other threads:[~2006-03-31 16:17 UTC | newest]

Thread overview: 49+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20060315200956.4a9e2cb3.akpm@osdl.org>
2006-03-09 20:29 ` [PATCH] Document Linux's memory barriers [try #4] David Howells
2006-03-09 23:34   ` Paul Mackerras
2006-03-09 23:45     ` Michael Buesch
2006-03-09 23:56       ` Linus Torvalds
2006-03-10  0:07         ` Michael Buesch
2006-03-10  0:48     ` Alan Cox
2006-03-10  0:54       ` Paul Mackerras
2006-03-10  5:28   ` Nick Piggin
2006-03-10 15:19   ` David Howells
2006-03-11  0:01     ` Paul Mackerras
2006-03-12 17:15   ` Eric W. Biederman
2006-03-13 12:32   ` Sergei Organov
2006-03-14 20:31   ` David Howells
2006-03-14 21:11     ` linux-os (Dick Johnson)
2006-03-15  9:09       ` Sergei Organov
2006-03-15  9:04     ` Sergei Organov
2006-03-14 20:35   ` David Howells
2006-03-15  9:11     ` Sergei Organov
2006-03-14 21:26   ` David Howells
2006-03-14 21:48     ` Paul Mackerras
2006-03-14 23:59     ` David Howells
2006-03-15  0:20       ` Linus Torvalds
2006-03-15  1:25         ` Nick Piggin
2006-03-15  0:54       ` Paul Mackerras
2006-03-15  1:19       ` David Howells
2006-03-15  1:47         ` Linus Torvalds
2006-03-15 11:10   ` David Howells
2006-03-15 11:51     ` Nick Piggin
2006-03-15 13:47     ` David Howells
2006-03-15 23:21       ` Nick Piggin
2006-03-15 14:23   ` [PATCH] Document Linux's memory barriers [try #5] David Howells
2006-03-16 11:50     ` David Howells
2006-03-16 17:18       ` Linus Torvalds
2006-03-17  1:20         ` Nick Piggin
2006-03-16 23:17     ` Paul E. McKenney
2006-03-16 23:55       ` Linus Torvalds
2006-03-17  1:29         ` Paul E. McKenney
2006-03-17  5:32           ` Linus Torvalds
2006-03-17  6:23             ` Paul E. McKenney
2006-03-23 18:34     ` David Howells
2006-03-23 19:28       ` Linus Torvalds
2006-03-23 22:26       ` Paul E. McKenney
2006-03-28 22:25 Suzanne Wood
2006-03-29 17:54 ` David Howells
2006-03-29 20:51 Suzanne Wood
2006-03-30 20:18 ` David Howells
2006-03-31  0:55 Suzanne Wood
2006-03-31 14:51 ` David Howells
2006-03-31 16:16 Suzanne Wood

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome