Re: Question regarding "Control Dependencies" in memory-barriers.txt

From: Peter Zijlstra
Date: Tue Aug 05 2014 - 08:58:35 EST


On Tue, Aug 05, 2014 at 08:13:54AM -0400, Pranith Kumar wrote:
> >> 689 q = ACCESS_ONCE(a);
> >> 690 BUILD_BUG_ON(MAX <= 1); /* Order load from a with store to b. */
> >> 691 if (q % MAX) {
> >> 692 ACCESS_ONCE(b) = p;
> >> 693 do_something();
> >> 694 } else {
> >> 695 ACCESS_ONCE(b) = p;
> >> 696 do_something_else();
> >> 697 }
> >> 698


> I don't think the write to 'b' here is speculative since it is
> happening in both the legs of the if() conditional. The write to b can
> be pulled out to before the conditional. Without the barrier(), isn't
> the following a valid transformation of the above?
>
> BUILD_BUG_ON(MAX <= 1); /* this will be compiled out if MAX != 1*/
> q = ACCESS_ONCE(a);
> ACCESS_ONCE(b) = p; / *BUG: No ordering */
> if (q % MAX) {
> do_something();
> } else {
> do_something_else();
> }
>
> I don't see how it is preserving the ordering.

Ah, that's what you meant. Yes possibly that's true.

Attachment: pgpft74Towl3U.pgp
Description: PGP signature