Skip to contentSkip to conversation

Document Linux's memory barriers [try #5]

PATCH Suzanne Wood 2 participants 2 messages Mar 31, 2006

lore
Share & tools
lore
#0 lore raw

Hello. Just minor questions below. Thank you.

Quoted 64 lines

From: "David Howells" Thursday, March 30, 2006 12:18 PM

Quoted 4 lines · Suzanne Wood

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.

Quoted 3 lines

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

Quoted 5 lines

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.

Quoted 3 lines

(*) 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?

Quoted 2 lines

(*) 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?

Quoted 4 lines

(*) 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

#1 lore raw
Quoted 3 lines · Suzanne Wood

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.