148d44d4eSAndrea Parri ===================================== 248d44d4eSAndrea Parri LINUX KERNEL MEMORY CONSISTENCY MODEL 348d44d4eSAndrea Parri ===================================== 41c27b644SPaul E. McKenney 51c27b644SPaul E. McKenney============ 61c27b644SPaul E. McKenneyINTRODUCTION 71c27b644SPaul E. McKenney============ 81c27b644SPaul E. McKenney 948d44d4eSAndrea ParriThis directory contains the memory consistency model (memory model, for 1048d44d4eSAndrea Parrishort) of the Linux kernel, written in the "cat" language and executable 1148d44d4eSAndrea Parriby the externally provided "herd7" simulator, which exhaustively explores 1248d44d4eSAndrea Parrithe state space of small litmus tests. 131c27b644SPaul E. McKenney 141c27b644SPaul E. McKenneyIn addition, the "klitmus7" tool (also externally provided) may be used 151c27b644SPaul E. McKenneyto convert a litmus test to a Linux kernel module, which in turn allows 161c27b644SPaul E. McKenneythat litmus test to be exercised within the Linux kernel. 171c27b644SPaul E. McKenney 181c27b644SPaul E. McKenney 191c27b644SPaul E. McKenney============ 201c27b644SPaul E. McKenneyREQUIREMENTS 211c27b644SPaul E. McKenney============ 221c27b644SPaul E. McKenney 235b62832cSAkira YokosawaVersion 7.49 of the "herd7" and "klitmus7" tools must be downloaded 248f7f2fbdSPaul E. McKenneyseparately: 251c27b644SPaul E. McKenney 261c27b644SPaul E. McKenney https://github.com/herd/herdtools7 271c27b644SPaul E. McKenney 281c27b644SPaul E. McKenneySee "herdtools7/INSTALL.md" for installation instructions. 291c27b644SPaul E. McKenney 301c27b644SPaul E. McKenney 311c27b644SPaul E. McKenney================== 321c27b644SPaul E. McKenneyBASIC USAGE: HERD7 331c27b644SPaul E. McKenney================== 341c27b644SPaul E. McKenney 351c27b644SPaul E. McKenneyThe memory model is used, in conjunction with "herd7", to exhaustively 361c27b644SPaul E. McKenneyexplore the state space of small litmus tests. 371c27b644SPaul E. McKenney 3871b7ff5eSAndrea ParriFor example, to run SB+fencembonceonces.litmus against the memory model: 391c27b644SPaul E. McKenney 4071b7ff5eSAndrea Parri $ herd7 -conf linux-kernel.cfg litmus-tests/SB+fencembonceonces.litmus 411c27b644SPaul E. McKenney 421c27b644SPaul E. McKenneyHere is the corresponding output: 431c27b644SPaul E. McKenney 4471b7ff5eSAndrea Parri Test SB+fencembonceonces Allowed 451c27b644SPaul E. McKenney States 3 461c27b644SPaul E. McKenney 0:r0=0; 1:r0=1; 471c27b644SPaul E. McKenney 0:r0=1; 1:r0=0; 481c27b644SPaul E. McKenney 0:r0=1; 1:r0=1; 491c27b644SPaul E. McKenney No 501c27b644SPaul E. McKenney Witnesses 511c27b644SPaul E. McKenney Positive: 0 Negative: 3 521c27b644SPaul E. McKenney Condition exists (0:r0=0 /\ 1:r0=0) 5371b7ff5eSAndrea Parri Observation SB+fencembonceonces Never 0 3 5471b7ff5eSAndrea Parri Time SB+fencembonceonces 0.01 551c27b644SPaul E. McKenney Hash=d66d99523e2cac6b06e66f4c995ebb48 561c27b644SPaul E. McKenney 571c27b644SPaul E. McKenneyThe "Positive: 0 Negative: 3" and the "Never 0 3" each indicate that 581c27b644SPaul E. McKenneythis litmus test's "exists" clause can not be satisfied. 591c27b644SPaul E. McKenney 601c27b644SPaul E. McKenneySee "herd7 -help" or "herdtools7/doc/" for more information. 611c27b644SPaul E. McKenney 621c27b644SPaul E. McKenney 631c27b644SPaul E. McKenney===================== 641c27b644SPaul E. McKenneyBASIC USAGE: KLITMUS7 651c27b644SPaul E. McKenney===================== 661c27b644SPaul E. McKenney 671c27b644SPaul E. McKenneyThe "klitmus7" tool converts a litmus test into a Linux kernel module, 681c27b644SPaul E. McKenneywhich may then be loaded and run. 691c27b644SPaul E. McKenney 7071b7ff5eSAndrea ParriFor example, to run SB+fencembonceonces.litmus against hardware: 711c27b644SPaul E. McKenney 721c27b644SPaul E. McKenney $ mkdir mymodules 7371b7ff5eSAndrea Parri $ klitmus7 -o mymodules litmus-tests/SB+fencembonceonces.litmus 741c27b644SPaul E. McKenney $ cd mymodules ; make 751c27b644SPaul E. McKenney $ sudo sh run.sh 761c27b644SPaul E. McKenney 771c27b644SPaul E. McKenneyThe corresponding output includes: 781c27b644SPaul E. McKenney 7971b7ff5eSAndrea Parri Test SB+fencembonceonces Allowed 801c27b644SPaul E. McKenney Histogram (3 states) 811c27b644SPaul E. McKenney 644580 :>0:r0=1; 1:r0=0; 821c27b644SPaul E. McKenney 644328 :>0:r0=0; 1:r0=1; 831c27b644SPaul E. McKenney 711092 :>0:r0=1; 1:r0=1; 841c27b644SPaul E. McKenney No 851c27b644SPaul E. McKenney Witnesses 861c27b644SPaul E. McKenney Positive: 0, Negative: 2000000 871c27b644SPaul E. McKenney Condition exists (0:r0=0 /\ 1:r0=0) is NOT validated 881c27b644SPaul E. McKenney Hash=d66d99523e2cac6b06e66f4c995ebb48 8971b7ff5eSAndrea Parri Observation SB+fencembonceonces Never 0 2000000 9071b7ff5eSAndrea Parri Time SB+fencembonceonces 0.16 911c27b644SPaul E. McKenney 921c27b644SPaul E. McKenneyThe "Positive: 0 Negative: 2000000" and the "Never 0 2000000" indicate 931c27b644SPaul E. McKenneythat during two million trials, the state specified in this litmus 941c27b644SPaul E. McKenneytest's "exists" clause was not reached. 951c27b644SPaul E. McKenney 961c27b644SPaul E. McKenneyAnd, as with "herd7", please see "klitmus7 -help" or "herdtools7/doc/" 971c27b644SPaul E. McKenneyfor more information. 981c27b644SPaul E. McKenney 991c27b644SPaul E. McKenney 1001c27b644SPaul E. McKenney==================== 1011c27b644SPaul E. McKenneyDESCRIPTION OF FILES 1021c27b644SPaul E. McKenney==================== 1031c27b644SPaul E. McKenney 1041c27b644SPaul E. McKenneyDocumentation/cheatsheet.txt 1051c27b644SPaul E. McKenney Quick-reference guide to the Linux-kernel memory model. 1061c27b644SPaul E. McKenney 1071c27b644SPaul E. McKenneyDocumentation/explanation.txt 1081c27b644SPaul E. McKenney Describes the memory model in detail. 1091c27b644SPaul E. McKenney 1101c27b644SPaul E. McKenneyDocumentation/recipes.txt 1111c27b644SPaul E. McKenney Lists common memory-ordering patterns. 1121c27b644SPaul E. McKenney 1131c27b644SPaul E. McKenneyDocumentation/references.txt 1141c27b644SPaul E. McKenney Provides background reading. 1151c27b644SPaul E. McKenney 1161c27b644SPaul E. McKenneylinux-kernel.bell 1171c27b644SPaul E. McKenney Categorizes the relevant instructions, including memory 1181c27b644SPaul E. McKenney references, memory barriers, atomic read-modify-write operations, 1191c27b644SPaul E. McKenney lock acquisition/release, and RCU operations. 1201c27b644SPaul E. McKenney 1211c27b644SPaul E. McKenney More formally, this file (1) lists the subtypes of the various 1221c27b644SPaul E. McKenney event types used by the memory model and (2) performs RCU 1231c27b644SPaul E. McKenney read-side critical section nesting analysis. 1241c27b644SPaul E. McKenney 1251c27b644SPaul E. McKenneylinux-kernel.cat 1261c27b644SPaul E. McKenney Specifies what reorderings are forbidden by memory references, 1271c27b644SPaul E. McKenney memory barriers, atomic read-modify-write operations, and RCU. 1281c27b644SPaul E. McKenney 1291c27b644SPaul E. McKenney More formally, this file specifies what executions are forbidden 1301c27b644SPaul E. McKenney by the memory model. Allowed executions are those which 1311c27b644SPaul E. McKenney satisfy the model's "coherence", "atomic", "happens-before", 1321c27b644SPaul E. McKenney "propagation", and "rcu" axioms, which are defined in the file. 1331c27b644SPaul E. McKenney 1341c27b644SPaul E. McKenneylinux-kernel.cfg 1351c27b644SPaul E. McKenney Convenience file that gathers the common-case herd7 command-line 1361c27b644SPaul E. McKenney arguments. 1371c27b644SPaul E. McKenney 1381c27b644SPaul E. McKenneylinux-kernel.def 1391c27b644SPaul E. McKenney Maps from C-like syntax to herd7's internal litmus-test 1401c27b644SPaul E. McKenney instruction-set architecture. 1411c27b644SPaul E. McKenney 1421c27b644SPaul E. McKenneylitmus-tests 1431c27b644SPaul E. McKenney Directory containing a few representative litmus tests, which 1441c27b644SPaul E. McKenney are listed in litmus-tests/README. A great deal more litmus 1451c27b644SPaul E. McKenney tests are available at https://github.com/paulmckrcu/litmus. 1461c27b644SPaul E. McKenney 1471c27b644SPaul E. McKenneylock.cat 1481c27b644SPaul E. McKenney Provides a front-end analysis of lock acquisition and release, 1491c27b644SPaul E. McKenney for example, associating a lock acquisition with the preceding 1501c27b644SPaul E. McKenney and following releases and checking for self-deadlock. 1511c27b644SPaul E. McKenney 1521c27b644SPaul E. McKenney More formally, this file defines a performance-enhanced scheme 1531c27b644SPaul E. McKenney for generation of the possible reads-from and coherence order 1541c27b644SPaul E. McKenney relations on the locking primitives. 1551c27b644SPaul E. McKenney 1561c27b644SPaul E. McKenneyREADME 1571c27b644SPaul E. McKenney This file. 1581c27b644SPaul E. McKenney 1591c27b644SPaul E. McKenney 1601c27b644SPaul E. McKenney=========== 1611c27b644SPaul E. McKenneyLIMITATIONS 1621c27b644SPaul E. McKenney=========== 1631c27b644SPaul E. McKenney 1641c27b644SPaul E. McKenneyThe Linux-kernel memory model has the following limitations: 1651c27b644SPaul E. McKenney 1661c27b644SPaul E. McKenney1. Compiler optimizations are not modeled. Of course, the use 1671c27b644SPaul E. McKenney of READ_ONCE() and WRITE_ONCE() limits the compiler's ability 1681c27b644SPaul E. McKenney to optimize, but there is Linux-kernel code that uses bare C 1691c27b644SPaul E. McKenney memory accesses. Handling this code is on the to-do list. 1701c27b644SPaul E. McKenney For more information, see Documentation/explanation.txt (in 1711c27b644SPaul E. McKenney particular, the "THE PROGRAM ORDER RELATION: po AND po-loc" 1721c27b644SPaul E. McKenney and "A WARNING" sections). 1731c27b644SPaul E. McKenney 174d8fa25c4SPaul E. McKenney Note that this limitation in turn limits LKMM's ability to 175d8fa25c4SPaul E. McKenney accurately model address, control, and data dependencies. 176d8fa25c4SPaul E. McKenney For example, if the compiler can deduce the value of some variable 177d8fa25c4SPaul E. McKenney carrying a dependency, then the compiler can break that dependency 178d8fa25c4SPaul E. McKenney by substituting a constant of that value. 179d8fa25c4SPaul E. McKenney 1801c27b644SPaul E. McKenney2. Multiple access sizes for a single variable are not supported, 1811c27b644SPaul E. McKenney and neither are misaligned or partially overlapping accesses. 1821c27b644SPaul E. McKenney 1831c27b644SPaul E. McKenney3. Exceptions and interrupts are not modeled. In some cases, 1841c27b644SPaul E. McKenney this limitation can be overcome by modeling the interrupt or 1851c27b644SPaul E. McKenney exception with an additional process. 1861c27b644SPaul E. McKenney 1871c27b644SPaul E. McKenney4. I/O such as MMIO or DMA is not supported. 1881c27b644SPaul E. McKenney 1891c27b644SPaul E. McKenney5. Self-modifying code (such as that found in the kernel's 1901c27b644SPaul E. McKenney alternatives mechanism, function tracer, Berkeley Packet Filter 1911c27b644SPaul E. McKenney JIT compiler, and module loader) is not supported. 1921c27b644SPaul E. McKenney 1931c27b644SPaul E. McKenney6. Complete modeling of all variants of atomic read-modify-write 1941c27b644SPaul E. McKenney operations, locking primitives, and RCU is not provided. 1951c27b644SPaul E. McKenney For example, call_rcu() and rcu_barrier() are not supported. 1961c27b644SPaul E. McKenney However, a substantial amount of support is provided for these 1971c27b644SPaul E. McKenney operations, as shown in the linux-kernel.def file. 1981c27b644SPaul E. McKenney 199d8fa25c4SPaul E. McKenney a. When rcu_assign_pointer() is passed NULL, the Linux 200d8fa25c4SPaul E. McKenney kernel provides no ordering, but LKMM models this 201d8fa25c4SPaul E. McKenney case as a store release. 202d8fa25c4SPaul E. McKenney 203d8fa25c4SPaul E. McKenney b. The "unless" RMW operations are not currently modeled: 204d8fa25c4SPaul E. McKenney atomic_long_add_unless(), atomic_add_unless(), 205d8fa25c4SPaul E. McKenney atomic_inc_unless_negative(), and 206d8fa25c4SPaul E. McKenney atomic_dec_unless_positive(). These can be emulated 207d8fa25c4SPaul E. McKenney in litmus tests, for example, by using atomic_cmpxchg(). 208d8fa25c4SPaul E. McKenney 209d8fa25c4SPaul E. McKenney c. The call_rcu() function is not modeled. It can be 210d8fa25c4SPaul E. McKenney emulated in litmus tests by adding another process that 211d8fa25c4SPaul E. McKenney invokes synchronize_rcu() and the body of the callback 212d8fa25c4SPaul E. McKenney function, with (for example) a release-acquire from 213d8fa25c4SPaul E. McKenney the site of the emulated call_rcu() to the beginning 214d8fa25c4SPaul E. McKenney of the additional process. 215d8fa25c4SPaul E. McKenney 216d8fa25c4SPaul E. McKenney d. The rcu_barrier() function is not modeled. It can be 217d8fa25c4SPaul E. McKenney emulated in litmus tests emulating call_rcu() via 218d8fa25c4SPaul E. McKenney (for example) a release-acquire from the end of each 219d8fa25c4SPaul E. McKenney additional call_rcu() process to the site of the 220d8fa25c4SPaul E. McKenney emulated rcu-barrier(). 221d8fa25c4SPaul E. McKenney 222d8fa25c4SPaul E. McKenney e. Sleepable RCU (SRCU) is not modeled. It can be 223d8fa25c4SPaul E. McKenney emulated, but perhaps not simply. 224d8fa25c4SPaul E. McKenney 225d8fa25c4SPaul E. McKenney f. Reader-writer locking is not modeled. It can be 226d8fa25c4SPaul E. McKenney emulated in litmus tests using atomic read-modify-write 227d8fa25c4SPaul E. McKenney operations. 228d8fa25c4SPaul E. McKenney 2291c27b644SPaul E. McKenneyThe "herd7" tool has some additional limitations of its own, apart from 2301c27b644SPaul E. McKenneythe memory model: 2311c27b644SPaul E. McKenney 2321c27b644SPaul E. McKenney1. Non-trivial data structures such as arrays or structures are 2331c27b644SPaul E. McKenney not supported. However, pointers are supported, allowing trivial 2341c27b644SPaul E. McKenney linked lists to be constructed. 2351c27b644SPaul E. McKenney 2361c27b644SPaul E. McKenney2. Dynamic memory allocation is not supported, although this can 2371c27b644SPaul E. McKenney be worked around in some cases by supplying multiple statically 2381c27b644SPaul E. McKenney allocated variables. 2391c27b644SPaul E. McKenney 2401c27b644SPaul E. McKenneySome of these limitations may be overcome in the future, but others are 2411c27b644SPaul E. McKenneymore likely to be addressed by incorporating the Linux-kernel memory model 2421c27b644SPaul E. McKenneyinto other tools. 243d8fa25c4SPaul E. McKenney 244d8fa25c4SPaul E. McKenneyFinally, please note that LKMM is subject to change as hardware, use cases, 245d8fa25c4SPaul E. McKenneyand compilers evolve. 246