diff options
author | Paul E. McKenney <paulmck@linux.vnet.ibm.com> | 2014-10-12 10:55:47 -0400 |
---|---|---|
committer | Paul E. McKenney <paulmck@linux.vnet.ibm.com> | 2014-11-13 13:34:53 -0500 |
commit | 8b19d1dead8413442ba0ff0b4e19b08f69d2f1b7 (patch) | |
tree | e2dcecc0d8057974aac7b5929f4adfa374351688 /Documentation | |
parent | 74860feed5ed570659e0f3852dd945be5b046038 (diff) |
documentation: Additional restriction for control dependencies
Short-circuit booleans are not defences against compilers breaking
your intended control dependencies.
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Reviewed-by: Pranith Kumar <bobby.prani@gmail.com>
Diffstat (limited to 'Documentation')
-rw-r--r-- | Documentation/memory-barriers.txt | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Documentation/memory-barriers.txt b/Documentation/memory-barriers.txt index 22a969cdd476..1073e019ef06 100644 --- a/Documentation/memory-barriers.txt +++ b/Documentation/memory-barriers.txt | |||
@@ -694,6 +694,24 @@ Please note once again that the stores to 'b' differ. If they were | |||
694 | identical, as noted earlier, the compiler could pull this store outside | 694 | identical, as noted earlier, the compiler could pull this store outside |
695 | of the 'if' statement. | 695 | of the 'if' statement. |
696 | 696 | ||
697 | You must also be careful not to rely too much on boolean short-circuit | ||
698 | evaluation. Consider this example: | ||
699 | |||
700 | q = ACCESS_ONCE(a); | ||
701 | if (a || 1 > 0) | ||
702 | ACCESS_ONCE(b) = 1; | ||
703 | |||
704 | Because the second condition is always true, the compiler can transform | ||
705 | this example as following, defeating control dependency: | ||
706 | |||
707 | q = ACCESS_ONCE(a); | ||
708 | ACCESS_ONCE(b) = 1; | ||
709 | |||
710 | This example underscores the need to ensure that the compiler cannot | ||
711 | out-guess your code. More generally, although ACCESS_ONCE() does force | ||
712 | the compiler to actually emit code for a given load, it does not force | ||
713 | the compiler to use the results. | ||
714 | |||
697 | Finally, control dependencies do -not- provide transitivity. This is | 715 | Finally, control dependencies do -not- provide transitivity. This is |
698 | demonstrated by two related examples, with the initial values of | 716 | demonstrated by two related examples, with the initial values of |
699 | x and y both being zero: | 717 | x and y both being zero: |