summaryrefslogtreecommitdiffstats
path: root/Documentation/memory-barriers.txt
diff options
context:
space:
mode:
authorPaul E. McKenney <paulmck@linux.vnet.ibm.com>2015-02-17 13:00:06 -0500
committerPaul E. McKenney <paulmck@linux.vnet.ibm.com>2015-02-26 14:57:32 -0500
commitff382810590e7182a1482a225965d6943e61699c (patch)
treeae5066e255ea457d4c9d402d7f56666039484235 /Documentation/memory-barriers.txt
parentdaf1aab9acfaaded09f53fa91dfe6e4e6926ec39 (diff)
documentation: Clarify control-dependency pairing
This commit explicitly states that control dependencies pair normally with other barriers, and gives an example of such pairing. Reported-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Diffstat (limited to 'Documentation/memory-barriers.txt')
-rw-r--r--Documentation/memory-barriers.txt42
1 files changed, 29 insertions, 13 deletions
diff --git a/Documentation/memory-barriers.txt b/Documentation/memory-barriers.txt
index ca2387ef27ab..6974f1c2b4e1 100644
--- a/Documentation/memory-barriers.txt
+++ b/Documentation/memory-barriers.txt
@@ -592,9 +592,9 @@ See also the subsection on "Cache Coherency" for a more thorough example.
592CONTROL DEPENDENCIES 592CONTROL DEPENDENCIES
593-------------------- 593--------------------
594 594
595A control dependency requires a full read memory barrier, not simply a data 595A load-load control dependency requires a full read memory barrier, not
596dependency barrier to make it work correctly. Consider the following bit of 596simply a data dependency barrier to make it work correctly. Consider the
597code: 597following bit of code:
598 598
599 q = ACCESS_ONCE(a); 599 q = ACCESS_ONCE(a);
600 if (q) { 600 if (q) {
@@ -615,14 +615,15 @@ case what's actually required is:
615 } 615 }
616 616
617However, stores are not speculated. This means that ordering -is- provided 617However, stores are not speculated. This means that ordering -is- provided
618in the following example: 618for load-store control dependencies, as in the following example:
619 619
620 q = ACCESS_ONCE(a); 620 q = ACCESS_ONCE(a);
621 if (q) { 621 if (q) {
622 ACCESS_ONCE(b) = p; 622 ACCESS_ONCE(b) = p;
623 } 623 }
624 624
625Please note that ACCESS_ONCE() is not optional! Without the 625Control dependencies pair normally with other types of barriers.
626That said, please note that ACCESS_ONCE() is not optional! Without the
626ACCESS_ONCE(), might combine the load from 'a' with other loads from 627ACCESS_ONCE(), might combine the load from 'a' with other loads from
627'a', and the store to 'b' with other stores to 'b', with possible highly 628'a', and the store to 'b' with other stores to 'b', with possible highly
628counterintuitive effects on ordering. 629counterintuitive effects on ordering.
@@ -813,6 +814,8 @@ In summary:
813 barrier() can help to preserve your control dependency. Please 814 barrier() can help to preserve your control dependency. Please
814 see the Compiler Barrier section for more information. 815 see the Compiler Barrier section for more information.
815 816
817 (*) Control dependencies pair normally with other types of barriers.
818
816 (*) Control dependencies do -not- provide transitivity. If you 819 (*) Control dependencies do -not- provide transitivity. If you
817 need transitivity, use smp_mb(). 820 need transitivity, use smp_mb().
818 821
@@ -823,14 +826,14 @@ SMP BARRIER PAIRING
823When dealing with CPU-CPU interactions, certain types of memory barrier should 826When dealing with CPU-CPU interactions, certain types of memory barrier should
824always be paired. A lack of appropriate pairing is almost certainly an error. 827always be paired. A lack of appropriate pairing is almost certainly an error.
825 828
826General barriers pair with each other, though they also pair with 829General barriers pair with each other, though they also pair with most
827most other types of barriers, albeit without transitivity. An acquire 830other types of barriers, albeit without transitivity. An acquire barrier
828barrier pairs with a release barrier, but both may also pair with other 831pairs with a release barrier, but both may also pair with other barriers,
829barriers, including of course general barriers. A write barrier pairs 832including of course general barriers. A write barrier pairs with a data
830with a data dependency barrier, an acquire barrier, a release barrier, 833dependency barrier, a control dependency, an acquire barrier, a release
831a read barrier, or a general barrier. Similarly a read barrier or a 834barrier, a read barrier, or a general barrier. Similarly a read barrier,
832data dependency barrier pairs with a write barrier, an acquire barrier, 835control dependency, or a data dependency barrier pairs with a write
833a release barrier, or a general barrier: 836barrier, an acquire barrier, a release barrier, or a general barrier:
834 837
835 CPU 1 CPU 2 838 CPU 1 CPU 2
836 =============== =============== 839 =============== ===============
@@ -850,6 +853,19 @@ Or:
850 <data dependency barrier> 853 <data dependency barrier>
851 y = *x; 854 y = *x;
852 855
856Or even:
857
858 CPU 1 CPU 2
859 =============== ===============================
860 r1 = ACCESS_ONCE(y);
861 <general barrier>
862 ACCESS_ONCE(y) = 1; if (r2 = ACCESS_ONCE(x)) {
863 <implicit control dependency>
864 ACCESS_ONCE(y) = 1;
865 }
866
867 assert(r1 == 0 || r2 == 0);
868
853Basically, the read barrier always has to be there, even though it can be of 869Basically, the read barrier always has to be there, even though it can be of
854the "weaker" type. 870the "weaker" type.
855 871