aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/memory-barriers.txt
diff options
context:
space:
mode:
authorPaul E. McKenney <paulmck@linux.vnet.ibm.com>2014-02-12 23:19:47 -0500
committerPaul E. McKenney <paulmck@linux.vnet.ibm.com>2014-02-17 17:56:09 -0500
commit9b2b3bf53124dca4ac815bd2fca53a31e5e262bd (patch)
tree7984451c41db3ca509fc860a00e2e81d727a2dcc /Documentation/memory-barriers.txt
parentbbf393b0d5350d68dbcf1f231b8af07b1b31121d (diff)
Documentation/memory-barriers.txt: Need barriers() for some control dependencies
Current compilers can "speculate" stores in the case where both legs of the "if" statement start with identical stores. Because the stores are identical, the compiler knows that the store will unconditionally execute regardless of the "if" condition, and so the compiler is within its rights to hoist the store to precede the condition. Such hoisting destroys the control-dependency ordering. This ordering can be restored by placing a barrier() at the beginning of each leg of the "if" statement. This commit adds this requirement to the control-dependencies section. Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Diffstat (limited to 'Documentation/memory-barriers.txt')
-rw-r--r--Documentation/memory-barriers.txt26
1 files changed, 19 insertions, 7 deletions
diff --git a/Documentation/memory-barriers.txt b/Documentation/memory-barriers.txt
index 6b25efd45583..9dde54c55b24 100644
--- a/Documentation/memory-barriers.txt
+++ b/Documentation/memory-barriers.txt
@@ -608,26 +608,30 @@ as follows:
608 b = p; /* BUG: Compiler can reorder!!! */ 608 b = p; /* BUG: Compiler can reorder!!! */
609 do_something(); 609 do_something();
610 610
611The solution is again ACCESS_ONCE(), which preserves the ordering between 611The solution is again ACCESS_ONCE() and barrier(), which preserves the
612the load from variable 'a' and the store to variable 'b': 612ordering between the load from variable 'a' and the store to variable 'b':
613 613
614 q = ACCESS_ONCE(a); 614 q = ACCESS_ONCE(a);
615 if (q) { 615 if (q) {
616 barrier();
616 ACCESS_ONCE(b) = p; 617 ACCESS_ONCE(b) = p;
617 do_something(); 618 do_something();
618 } else { 619 } else {
620 barrier();
619 ACCESS_ONCE(b) = p; 621 ACCESS_ONCE(b) = p;
620 do_something_else(); 622 do_something_else();
621 } 623 }
622 624
623You could also use barrier() to prevent the compiler from moving 625The initial ACCESS_ONCE() is required to prevent the compiler from
624the stores to variable 'b', but barrier() would not prevent the 626proving the value of 'a', and the pair of barrier() invocations are
625compiler from proving to itself that a==1 always, so ACCESS_ONCE() 627required to prevent the compiler from pulling the two identical stores
626is also needed. 628to 'b' out from the legs of the "if" statement.
627 629
628It is important to note that control dependencies absolutely require a 630It is important to note that control dependencies absolutely require a
629a conditional. For example, the following "optimized" version of 631a conditional. For example, the following "optimized" version of
630the above example breaks ordering: 632the above example breaks ordering, which is why the barrier() invocations
633are absolutely required if you have identical stores in both legs of
634the "if" statement:
631 635
632 q = ACCESS_ONCE(a); 636 q = ACCESS_ONCE(a);
633 ACCESS_ONCE(b) = p; /* BUG: No ordering vs. load from a!!! */ 637 ACCESS_ONCE(b) = p; /* BUG: No ordering vs. load from a!!! */
@@ -643,9 +647,11 @@ It is of course legal for the prior load to be part of the conditional,
643for example, as follows: 647for example, as follows:
644 648
645 if (ACCESS_ONCE(a) > 0) { 649 if (ACCESS_ONCE(a) > 0) {
650 barrier();
646 ACCESS_ONCE(b) = q / 2; 651 ACCESS_ONCE(b) = q / 2;
647 do_something(); 652 do_something();
648 } else { 653 } else {
654 barrier();
649 ACCESS_ONCE(b) = q / 3; 655 ACCESS_ONCE(b) = q / 3;
650 do_something_else(); 656 do_something_else();
651 } 657 }
@@ -659,9 +665,11 @@ the needed conditional. For example:
659 665
660 q = ACCESS_ONCE(a); 666 q = ACCESS_ONCE(a);
661 if (q % MAX) { 667 if (q % MAX) {
668 barrier();
662 ACCESS_ONCE(b) = p; 669 ACCESS_ONCE(b) = p;
663 do_something(); 670 do_something();
664 } else { 671 } else {
672 barrier();
665 ACCESS_ONCE(b) = p; 673 ACCESS_ONCE(b) = p;
666 do_something_else(); 674 do_something_else();
667 } 675 }
@@ -723,6 +731,10 @@ In summary:
723 use smb_rmb(), smp_wmb(), or, in the case of prior stores and 731 use smb_rmb(), smp_wmb(), or, in the case of prior stores and
724 later loads, smp_mb(). 732 later loads, smp_mb().
725 733
734 (*) If both legs of the "if" statement begin with identical stores
735 to the same variable, a barrier() statement is required at the
736 beginning of each leg of the "if" statement.
737
726 (*) Control dependencies require at least one run-time conditional 738 (*) Control dependencies require at least one run-time conditional
727 between the prior load and the subsequent store, and this 739 between the prior load and the subsequent store, and this
728 conditional must involve the prior load. If the compiler 740 conditional must involve the prior load. If the compiler