4 A major difficulty with control dependencies is that current compilers
5 do not support them. One purpose of this document is therefore to
6 help you prevent your compiler from breaking your code. However,
7 control dependencies also pose other challenges, which leads to the
8 second purpose of this document, namely to help you to avoid breaking
9 your own code, even in the absence of help from your compiler.
11 One such challenge is that control dependencies order only later stores.
12 Therefore, a load-load control dependency will not preserve ordering
13 unless a read memory barrier is provided. Consider the following code:
19 This is not guaranteed to provide any ordering because some types of CPUs
20 are permitted to predict the result of the load from "b". This prediction
21 can cause other CPUs to see this load as having happened before the load
22 from "a". This means that an explicit read barrier is required, for example
31 However, stores are not speculated. This means that ordering is
32 (usually) guaranteed for load-store control dependencies, as in the
39 Control dependencies can pair with each other and with other types
40 of ordering. But please note that neither the READ_ONCE() nor the
41 WRITE_ONCE() are optional. Without the READ_ONCE(), the compiler might
42 fuse the load from "a" with other loads. Without the WRITE_ONCE(),
43 the compiler might fuse the store to "b" with other stores. Worse yet,
44 the compiler might convert the store into a load and a check followed
45 by a store, and this compiler-generated load would not be ordered by
46 the control dependency.
48 Furthermore, if the compiler is able to prove that the value of variable
49 "a" is always non-zero, it would be well within its rights to optimize
50 the original example by eliminating the "if" statement as follows:
53 b = 1; /* BUG: Compiler and CPU can both reorder!!! */
55 So don't leave out either the READ_ONCE() or the WRITE_ONCE().
56 In particular, although READ_ONCE() does force the compiler to emit a
57 load, it does *not* force the compiler to actually use the loaded value.
59 It is tempting to try use control dependencies to enforce ordering on
60 identical stores on both branches of the "if" statement as follows:
73 Unfortunately, current compilers will transform this as follows at high
78 WRITE_ONCE(b, 1); /* BUG: No ordering vs. load from a!!! */
80 /* WRITE_ONCE(b, 1); -- moved up, BUG!!! */
83 /* WRITE_ONCE(b, 1); -- moved up, BUG!!! */
87 Now there is no conditional between the load from "a" and the store to
88 "b", which means that the CPU is within its rights to reorder them: The
89 conditional is absolutely required, and must be present in the final
90 assembly code, after all of the compiler and link-time optimizations
91 have been applied. Therefore, if you need ordering in this example,
92 you must use explicit memory ordering, for example, smp_store_release():
96 smp_store_release(&b, 1);
99 smp_store_release(&b, 1);
103 Without explicit memory ordering, control-dependency-based ordering is
104 guaranteed only when the stores differ, for example:
115 The initial READ_ONCE() is still required to prevent the compiler from
116 knowing too much about the value of "a".
118 But please note that you need to be careful what you do with the local
119 variable "q", otherwise the compiler might be able to guess the value
120 and again remove the conditional branch that is absolutely required to
121 preserve ordering. For example:
132 If MAX is compile-time defined to be 1, then the compiler knows that
133 (q % MAX) must be equal to zero, regardless of the value of "q".
134 The compiler is therefore within its rights to transform the above code
141 Given this transformation, the CPU is not required to respect the ordering
142 between the load from variable "a" and the store to variable "b". It is
143 tempting to add a barrier(), but this does not help. The conditional
144 is gone, and the barrier won't bring it back. Therefore, if you need
145 to relying on control dependencies to produce this ordering, you should
146 make sure that MAX is greater than one, perhaps as follows:
149 BUILD_BUG_ON(MAX <= 1); /* Order load from a with store to b. */
158 Please note once again that each leg of the "if" statement absolutely
159 must store different values to "b". As in previous examples, if the two
160 values were identical, the compiler could pull this store outside of the
161 "if" statement, destroying the control dependency's ordering properties.
163 You must also be careful avoid relying too much on boolean short-circuit
164 evaluation. Consider this example:
170 Because the first condition cannot fault and the second condition is
171 always true, the compiler can transform this example as follows, again
172 destroying the control dependency's ordering:
177 This is yet another example showing the importance of preventing the
178 compiler from out-guessing your code. Again, although READ_ONCE() really
179 does force the compiler to emit code for a given load, the compiler is
180 within its rights to discard the loaded value.
182 In addition, control dependencies apply only to the then-clause and
183 else-clause of the "if" statement in question. In particular, they do
184 not necessarily order the code following the entire "if" statement:
192 WRITE_ONCE(c, 1); /* BUG: No ordering against the read from "a". */
194 It is tempting to argue that there in fact is ordering because the
195 compiler cannot reorder volatile accesses and also cannot reorder
196 the writes to "b" with the condition. Unfortunately for this line
197 of reasoning, the compiler might compile the two writes to "b" as
198 conditional-move instructions, as in this fanciful pseudo-assembly
208 The control dependencies would then extend only to the pair of cmov
209 instructions and the store depending on them. This means that a weakly
210 ordered CPU would have no dependency of any sort between the load from
211 "a" and the store to "c". In short, control dependencies provide ordering
212 only to the stores in the then-clause and else-clause of the "if" statement
213 in question (including functions invoked by those two clauses), and not
214 to code following that "if" statement.
219 (*) Control dependencies can order prior loads against later stores.
220 However, they do *not* guarantee any other sort of ordering:
221 Not prior loads against later loads, nor prior stores against
222 later anything. If you need these other forms of ordering, use
223 smp_load_acquire(), smp_store_release(), or, in the case of prior
224 stores and later loads, smp_mb().
226 (*) If both legs of the "if" statement contain identical stores to
227 the same variable, then you must explicitly order those stores,
228 either by preceding both of them with smp_mb() or by using
229 smp_store_release(). Please note that it is *not* sufficient to use
230 barrier() at beginning and end of each leg of the "if" statement
231 because, as shown by the example above, optimizing compilers can
232 destroy the control dependency while respecting the letter of the
235 (*) Control dependencies require at least one run-time conditional
236 between the prior load and the subsequent store, and this
237 conditional must involve the prior load. If the compiler is able
238 to optimize the conditional away, it will have also optimized
239 away the ordering. Careful use of READ_ONCE() and WRITE_ONCE()
240 can help to preserve the needed conditional.
242 (*) Control dependencies require that the compiler avoid reordering the
243 dependency into nonexistence. Careful use of READ_ONCE() or
244 atomic{,64}_read() can help to preserve your control dependency.
246 (*) Control dependencies apply only to the then-clause and else-clause
247 of the "if" statement containing the control dependency, including
248 any functions that these two clauses call. Control dependencies
249 do *not* apply to code beyond the end of that "if" statement.
251 (*) Control dependencies pair normally with other types of barriers.
253 (*) Control dependencies do *not* provide multicopy atomicity. If you
254 need all the CPUs to agree on the ordering of a given store against
255 all other accesses, use smp_mb().
257 (*) Compilers do not understand control dependencies. It is therefore
258 your job to ensure that they do not break your code.