README 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. =====================================
  2. LINUX KERNEL MEMORY CONSISTENCY MODEL
  3. =====================================
  4. ============
  5. INTRODUCTION
  6. ============
  7. This directory contains the memory consistency model (memory model, for
  8. short) of the Linux kernel, written in the "cat" language and executable
  9. by the externally provided "herd7" simulator, which exhaustively explores
  10. the state space of small litmus tests.
  11. In addition, the "klitmus7" tool (also externally provided) may be used
  12. to convert a litmus test to a Linux kernel module, which in turn allows
  13. that litmus test to be exercised within the Linux kernel.
  14. ============
  15. REQUIREMENTS
  16. ============
  17. Version 7.52 or higher of the "herd7" and "klitmus7" tools must be
  18. downloaded separately:
  19. https://github.com/herd/herdtools7
  20. See "herdtools7/INSTALL.md" for installation instructions.
  21. Note that although these tools usually provide backwards compatibility,
  22. this is not absolutely guaranteed. Therefore, if a later version does
  23. not work, please try using the exact version called out above.
  24. ==================
  25. BASIC USAGE: HERD7
  26. ==================
  27. The memory model is used, in conjunction with "herd7", to exhaustively
  28. explore the state space of small litmus tests.
  29. For example, to run SB+fencembonceonces.litmus against the memory model:
  30. $ herd7 -conf linux-kernel.cfg litmus-tests/SB+fencembonceonces.litmus
  31. Here is the corresponding output:
  32. Test SB+fencembonceonces Allowed
  33. States 3
  34. 0:r0=0; 1:r0=1;
  35. 0:r0=1; 1:r0=0;
  36. 0:r0=1; 1:r0=1;
  37. No
  38. Witnesses
  39. Positive: 0 Negative: 3
  40. Condition exists (0:r0=0 /\ 1:r0=0)
  41. Observation SB+fencembonceonces Never 0 3
  42. Time SB+fencembonceonces 0.01
  43. Hash=d66d99523e2cac6b06e66f4c995ebb48
  44. The "Positive: 0 Negative: 3" and the "Never 0 3" each indicate that
  45. this litmus test's "exists" clause can not be satisfied.
  46. See "herd7 -help" or "herdtools7/doc/" for more information.
  47. =====================
  48. BASIC USAGE: KLITMUS7
  49. =====================
  50. The "klitmus7" tool converts a litmus test into a Linux kernel module,
  51. which may then be loaded and run.
  52. For example, to run SB+fencembonceonces.litmus against hardware:
  53. $ mkdir mymodules
  54. $ klitmus7 -o mymodules litmus-tests/SB+fencembonceonces.litmus
  55. $ cd mymodules ; make
  56. $ sudo sh run.sh
  57. The corresponding output includes:
  58. Test SB+fencembonceonces Allowed
  59. Histogram (3 states)
  60. 644580 :>0:r0=1; 1:r0=0;
  61. 644328 :>0:r0=0; 1:r0=1;
  62. 711092 :>0:r0=1; 1:r0=1;
  63. No
  64. Witnesses
  65. Positive: 0, Negative: 2000000
  66. Condition exists (0:r0=0 /\ 1:r0=0) is NOT validated
  67. Hash=d66d99523e2cac6b06e66f4c995ebb48
  68. Observation SB+fencembonceonces Never 0 2000000
  69. Time SB+fencembonceonces 0.16
  70. The "Positive: 0 Negative: 2000000" and the "Never 0 2000000" indicate
  71. that during two million trials, the state specified in this litmus
  72. test's "exists" clause was not reached.
  73. And, as with "herd7", please see "klitmus7 -help" or "herdtools7/doc/"
  74. for more information.
  75. ====================
  76. DESCRIPTION OF FILES
  77. ====================
  78. Documentation/cheatsheet.txt
  79. Quick-reference guide to the Linux-kernel memory model.
  80. Documentation/explanation.txt
  81. Describes the memory model in detail.
  82. Documentation/recipes.txt
  83. Lists common memory-ordering patterns.
  84. Documentation/references.txt
  85. Provides background reading.
  86. linux-kernel.bell
  87. Categorizes the relevant instructions, including memory
  88. references, memory barriers, atomic read-modify-write operations,
  89. lock acquisition/release, and RCU operations.
  90. More formally, this file (1) lists the subtypes of the various
  91. event types used by the memory model and (2) performs RCU
  92. read-side critical section nesting analysis.
  93. linux-kernel.cat
  94. Specifies what reorderings are forbidden by memory references,
  95. memory barriers, atomic read-modify-write operations, and RCU.
  96. More formally, this file specifies what executions are forbidden
  97. by the memory model. Allowed executions are those which
  98. satisfy the model's "coherence", "atomic", "happens-before",
  99. "propagation", and "rcu" axioms, which are defined in the file.
  100. linux-kernel.cfg
  101. Convenience file that gathers the common-case herd7 command-line
  102. arguments.
  103. linux-kernel.def
  104. Maps from C-like syntax to herd7's internal litmus-test
  105. instruction-set architecture.
  106. litmus-tests
  107. Directory containing a few representative litmus tests, which
  108. are listed in litmus-tests/README. A great deal more litmus
  109. tests are available at https://github.com/paulmckrcu/litmus.
  110. lock.cat
  111. Provides a front-end analysis of lock acquisition and release,
  112. for example, associating a lock acquisition with the preceding
  113. and following releases and checking for self-deadlock.
  114. More formally, this file defines a performance-enhanced scheme
  115. for generation of the possible reads-from and coherence order
  116. relations on the locking primitives.
  117. README
  118. This file.
  119. scripts Various scripts, see scripts/README.
  120. ===========
  121. LIMITATIONS
  122. ===========
  123. The Linux-kernel memory model (LKMM) has the following limitations:
  124. 1. Compiler optimizations are not accurately modeled. Of course,
  125. the use of READ_ONCE() and WRITE_ONCE() limits the compiler's
  126. ability to optimize, but under some circumstances it is possible
  127. for the compiler to undermine the memory model. For more
  128. information, see Documentation/explanation.txt (in particular,
  129. the "THE PROGRAM ORDER RELATION: po AND po-loc" and "A WARNING"
  130. sections).
  131. Note that this limitation in turn limits LKMM's ability to
  132. accurately model address, control, and data dependencies.
  133. For example, if the compiler can deduce the value of some variable
  134. carrying a dependency, then the compiler can break that dependency
  135. by substituting a constant of that value.
  136. 2. Multiple access sizes for a single variable are not supported,
  137. and neither are misaligned or partially overlapping accesses.
  138. 3. Exceptions and interrupts are not modeled. In some cases,
  139. this limitation can be overcome by modeling the interrupt or
  140. exception with an additional process.
  141. 4. I/O such as MMIO or DMA is not supported.
  142. 5. Self-modifying code (such as that found in the kernel's
  143. alternatives mechanism, function tracer, Berkeley Packet Filter
  144. JIT compiler, and module loader) is not supported.
  145. 6. Complete modeling of all variants of atomic read-modify-write
  146. operations, locking primitives, and RCU is not provided.
  147. For example, call_rcu() and rcu_barrier() are not supported.
  148. However, a substantial amount of support is provided for these
  149. operations, as shown in the linux-kernel.def file.
  150. a. When rcu_assign_pointer() is passed NULL, the Linux
  151. kernel provides no ordering, but LKMM models this
  152. case as a store release.
  153. b. The "unless" RMW operations are not currently modeled:
  154. atomic_long_add_unless(), atomic_add_unless(),
  155. atomic_inc_unless_negative(), and
  156. atomic_dec_unless_positive(). These can be emulated
  157. in litmus tests, for example, by using atomic_cmpxchg().
  158. c. The call_rcu() function is not modeled. It can be
  159. emulated in litmus tests by adding another process that
  160. invokes synchronize_rcu() and the body of the callback
  161. function, with (for example) a release-acquire from
  162. the site of the emulated call_rcu() to the beginning
  163. of the additional process.
  164. d. The rcu_barrier() function is not modeled. It can be
  165. emulated in litmus tests emulating call_rcu() via
  166. (for example) a release-acquire from the end of each
  167. additional call_rcu() process to the site of the
  168. emulated rcu-barrier().
  169. e. Although sleepable RCU (SRCU) is now modeled, there
  170. are some subtle differences between its semantics and
  171. those in the Linux kernel. For example, the kernel
  172. might interpret the following sequence as two partially
  173. overlapping SRCU read-side critical sections:
  174. 1 r1 = srcu_read_lock(&my_srcu);
  175. 2 do_something_1();
  176. 3 r2 = srcu_read_lock(&my_srcu);
  177. 4 do_something_2();
  178. 5 srcu_read_unlock(&my_srcu, r1);
  179. 6 do_something_3();
  180. 7 srcu_read_unlock(&my_srcu, r2);
  181. In contrast, LKMM will interpret this as a nested pair of
  182. SRCU read-side critical sections, with the outer critical
  183. section spanning lines 1-7 and the inner critical section
  184. spanning lines 3-5.
  185. This difference would be more of a concern had anyone
  186. identified a reasonable use case for partially overlapping
  187. SRCU read-side critical sections. For more information,
  188. please see: https://paulmck.livejournal.com/40593.html
  189. f. Reader-writer locking is not modeled. It can be
  190. emulated in litmus tests using atomic read-modify-write
  191. operations.
  192. The "herd7" tool has some additional limitations of its own, apart from
  193. the memory model:
  194. 1. Non-trivial data structures such as arrays or structures are
  195. not supported. However, pointers are supported, allowing trivial
  196. linked lists to be constructed.
  197. 2. Dynamic memory allocation is not supported, although this can
  198. be worked around in some cases by supplying multiple statically
  199. allocated variables.
  200. Some of these limitations may be overcome in the future, but others are
  201. more likely to be addressed by incorporating the Linux-kernel memory model
  202. into other tools.
  203. Finally, please note that LKMM is subject to change as hardware, use cases,
  204. and compilers evolve.