control-dependencies.txt 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. CONTROL DEPENDENCIES
  2. ====================
  3. A major difficulty with control dependencies is that current compilers
  4. do not support them. One purpose of this document is therefore to
  5. help you prevent your compiler from breaking your code. However,
  6. control dependencies also pose other challenges, which leads to the
  7. second purpose of this document, namely to help you to avoid breaking
  8. your own code, even in the absence of help from your compiler.
  9. One such challenge is that control dependencies order only later stores.
  10. Therefore, a load-load control dependency will not preserve ordering
  11. unless a read memory barrier is provided. Consider the following code:
  12. q = READ_ONCE(a);
  13. if (q)
  14. p = READ_ONCE(b);
  15. This is not guaranteed to provide any ordering because some types of CPUs
  16. are permitted to predict the result of the load from "b". This prediction
  17. can cause other CPUs to see this load as having happened before the load
  18. from "a". This means that an explicit read barrier is required, for example
  19. as follows:
  20. q = READ_ONCE(a);
  21. if (q) {
  22. smp_rmb();
  23. p = READ_ONCE(b);
  24. }
  25. However, stores are not speculated. This means that ordering is
  26. (usually) guaranteed for load-store control dependencies, as in the
  27. following example:
  28. q = READ_ONCE(a);
  29. if (q)
  30. WRITE_ONCE(b, 1);
  31. Control dependencies can pair with each other and with other types
  32. of ordering. But please note that neither the READ_ONCE() nor the
  33. WRITE_ONCE() are optional. Without the READ_ONCE(), the compiler might
  34. fuse the load from "a" with other loads. Without the WRITE_ONCE(),
  35. the compiler might fuse the store to "b" with other stores. Worse yet,
  36. the compiler might convert the store into a load and a check followed
  37. by a store, and this compiler-generated load would not be ordered by
  38. the control dependency.
  39. Furthermore, if the compiler is able to prove that the value of variable
  40. "a" is always non-zero, it would be well within its rights to optimize
  41. the original example by eliminating the "if" statement as follows:
  42. q = a;
  43. b = 1; /* BUG: Compiler and CPU can both reorder!!! */
  44. So don't leave out either the READ_ONCE() or the WRITE_ONCE().
  45. In particular, although READ_ONCE() does force the compiler to emit a
  46. load, it does *not* force the compiler to actually use the loaded value.
  47. It is tempting to try use control dependencies to enforce ordering on
  48. identical stores on both branches of the "if" statement as follows:
  49. q = READ_ONCE(a);
  50. if (q) {
  51. barrier();
  52. WRITE_ONCE(b, 1);
  53. do_something();
  54. } else {
  55. barrier();
  56. WRITE_ONCE(b, 1);
  57. do_something_else();
  58. }
  59. Unfortunately, current compilers will transform this as follows at high
  60. optimization levels:
  61. q = READ_ONCE(a);
  62. barrier();
  63. WRITE_ONCE(b, 1); /* BUG: No ordering vs. load from a!!! */
  64. if (q) {
  65. /* WRITE_ONCE(b, 1); -- moved up, BUG!!! */
  66. do_something();
  67. } else {
  68. /* WRITE_ONCE(b, 1); -- moved up, BUG!!! */
  69. do_something_else();
  70. }
  71. Now there is no conditional between the load from "a" and the store to
  72. "b", which means that the CPU is within its rights to reorder them: The
  73. conditional is absolutely required, and must be present in the final
  74. assembly code, after all of the compiler and link-time optimizations
  75. have been applied. Therefore, if you need ordering in this example,
  76. you must use explicit memory ordering, for example, smp_store_release():
  77. q = READ_ONCE(a);
  78. if (q) {
  79. smp_store_release(&b, 1);
  80. do_something();
  81. } else {
  82. smp_store_release(&b, 1);
  83. do_something_else();
  84. }
  85. Without explicit memory ordering, control-dependency-based ordering is
  86. guaranteed only when the stores differ, for example:
  87. q = READ_ONCE(a);
  88. if (q) {
  89. WRITE_ONCE(b, 1);
  90. do_something();
  91. } else {
  92. WRITE_ONCE(b, 2);
  93. do_something_else();
  94. }
  95. The initial READ_ONCE() is still required to prevent the compiler from
  96. knowing too much about the value of "a".
  97. But please note that you need to be careful what you do with the local
  98. variable "q", otherwise the compiler might be able to guess the value
  99. and again remove the conditional branch that is absolutely required to
  100. preserve ordering. For example:
  101. q = READ_ONCE(a);
  102. if (q % MAX) {
  103. WRITE_ONCE(b, 1);
  104. do_something();
  105. } else {
  106. WRITE_ONCE(b, 2);
  107. do_something_else();
  108. }
  109. If MAX is compile-time defined to be 1, then the compiler knows that
  110. (q % MAX) must be equal to zero, regardless of the value of "q".
  111. The compiler is therefore within its rights to transform the above code
  112. into the following:
  113. q = READ_ONCE(a);
  114. WRITE_ONCE(b, 2);
  115. do_something_else();
  116. Given this transformation, the CPU is not required to respect the ordering
  117. between the load from variable "a" and the store to variable "b". It is
  118. tempting to add a barrier(), but this does not help. The conditional
  119. is gone, and the barrier won't bring it back. Therefore, if you need
  120. to relying on control dependencies to produce this ordering, you should
  121. make sure that MAX is greater than one, perhaps as follows:
  122. q = READ_ONCE(a);
  123. BUILD_BUG_ON(MAX <= 1); /* Order load from a with store to b. */
  124. if (q % MAX) {
  125. WRITE_ONCE(b, 1);
  126. do_something();
  127. } else {
  128. WRITE_ONCE(b, 2);
  129. do_something_else();
  130. }
  131. Please note once again that each leg of the "if" statement absolutely
  132. must store different values to "b". As in previous examples, if the two
  133. values were identical, the compiler could pull this store outside of the
  134. "if" statement, destroying the control dependency's ordering properties.
  135. You must also be careful avoid relying too much on boolean short-circuit
  136. evaluation. Consider this example:
  137. q = READ_ONCE(a);
  138. if (q || 1 > 0)
  139. WRITE_ONCE(b, 1);
  140. Because the first condition cannot fault and the second condition is
  141. always true, the compiler can transform this example as follows, again
  142. destroying the control dependency's ordering:
  143. q = READ_ONCE(a);
  144. WRITE_ONCE(b, 1);
  145. This is yet another example showing the importance of preventing the
  146. compiler from out-guessing your code. Again, although READ_ONCE() really
  147. does force the compiler to emit code for a given load, the compiler is
  148. within its rights to discard the loaded value.
  149. In addition, control dependencies apply only to the then-clause and
  150. else-clause of the "if" statement in question. In particular, they do
  151. not necessarily order the code following the entire "if" statement:
  152. q = READ_ONCE(a);
  153. if (q) {
  154. WRITE_ONCE(b, 1);
  155. } else {
  156. WRITE_ONCE(b, 2);
  157. }
  158. WRITE_ONCE(c, 1); /* BUG: No ordering against the read from "a". */
  159. It is tempting to argue that there in fact is ordering because the
  160. compiler cannot reorder volatile accesses and also cannot reorder
  161. the writes to "b" with the condition. Unfortunately for this line
  162. of reasoning, the compiler might compile the two writes to "b" as
  163. conditional-move instructions, as in this fanciful pseudo-assembly
  164. language:
  165. ld r1,a
  166. cmp r1,$0
  167. cmov,ne r4,$1
  168. cmov,eq r4,$2
  169. st r4,b
  170. st $1,c
  171. The control dependencies would then extend only to the pair of cmov
  172. instructions and the store depending on them. This means that a weakly
  173. ordered CPU would have no dependency of any sort between the load from
  174. "a" and the store to "c". In short, control dependencies provide ordering
  175. only to the stores in the then-clause and else-clause of the "if" statement
  176. in question (including functions invoked by those two clauses), and not
  177. to code following that "if" statement.
  178. In summary:
  179. (*) Control dependencies can order prior loads against later stores.
  180. However, they do *not* guarantee any other sort of ordering:
  181. Not prior loads against later loads, nor prior stores against
  182. later anything. If you need these other forms of ordering, use
  183. smp_load_acquire(), smp_store_release(), or, in the case of prior
  184. stores and later loads, smp_mb().
  185. (*) If both legs of the "if" statement contain identical stores to
  186. the same variable, then you must explicitly order those stores,
  187. either by preceding both of them with smp_mb() or by using
  188. smp_store_release(). Please note that it is *not* sufficient to use
  189. barrier() at beginning and end of each leg of the "if" statement
  190. because, as shown by the example above, optimizing compilers can
  191. destroy the control dependency while respecting the letter of the
  192. barrier() law.
  193. (*) Control dependencies require at least one run-time conditional
  194. between the prior load and the subsequent store, and this
  195. conditional must involve the prior load. If the compiler is able
  196. to optimize the conditional away, it will have also optimized
  197. away the ordering. Careful use of READ_ONCE() and WRITE_ONCE()
  198. can help to preserve the needed conditional.
  199. (*) Control dependencies require that the compiler avoid reordering the
  200. dependency into nonexistence. Careful use of READ_ONCE() or
  201. atomic{,64}_read() can help to preserve your control dependency.
  202. (*) Control dependencies apply only to the then-clause and else-clause
  203. of the "if" statement containing the control dependency, including
  204. any functions that these two clauses call. Control dependencies
  205. do *not* apply to code beyond the end of that "if" statement.
  206. (*) Control dependencies pair normally with other types of barriers.
  207. (*) Control dependencies do *not* provide multicopy atomicity. If you
  208. need all the CPUs to agree on the ordering of a given store against
  209. all other accesses, use smp_mb().
  210. (*) Compilers do not understand control dependencies. It is therefore
  211. your job to ensure that they do not break your code.