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 23034fb712SAndrea ParriVersion 7.52 or higher of the "herd7" and "klitmus7" tools must be 24034fb712SAndrea Parridownloaded separately: 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 30034fb712SAndrea ParriNote that although these tools usually provide backwards compatibility, 31034fb712SAndrea Parrithis is not absolutely guaranteed. Therefore, if a later version does 32034fb712SAndrea Parrinot work, please try using the exact version called out above. 33034fb712SAndrea Parri 341c27b644SPaul E. McKenney 351c27b644SPaul E. McKenney================== 361c27b644SPaul E. McKenneyBASIC USAGE: HERD7 371c27b644SPaul E. McKenney================== 381c27b644SPaul E. McKenney 391c27b644SPaul E. McKenneyThe memory model is used, in conjunction with "herd7", to exhaustively 401c27b644SPaul E. McKenneyexplore the state space of small litmus tests. 411c27b644SPaul E. McKenney 4271b7ff5eSAndrea ParriFor example, to run SB+fencembonceonces.litmus against the memory model: 431c27b644SPaul E. McKenney 4471b7ff5eSAndrea Parri $ herd7 -conf linux-kernel.cfg litmus-tests/SB+fencembonceonces.litmus 451c27b644SPaul E. McKenney 461c27b644SPaul E. McKenneyHere is the corresponding output: 471c27b644SPaul E. McKenney 4871b7ff5eSAndrea Parri Test SB+fencembonceonces Allowed 491c27b644SPaul E. McKenney States 3 501c27b644SPaul E. McKenney 0:r0=0; 1:r0=1; 511c27b644SPaul E. McKenney 0:r0=1; 1:r0=0; 521c27b644SPaul E. McKenney 0:r0=1; 1:r0=1; 531c27b644SPaul E. McKenney No 541c27b644SPaul E. McKenney Witnesses 551c27b644SPaul E. McKenney Positive: 0 Negative: 3 561c27b644SPaul E. McKenney Condition exists (0:r0=0 /\ 1:r0=0) 5771b7ff5eSAndrea Parri Observation SB+fencembonceonces Never 0 3 5871b7ff5eSAndrea Parri Time SB+fencembonceonces 0.01 591c27b644SPaul E. McKenney Hash=d66d99523e2cac6b06e66f4c995ebb48 601c27b644SPaul E. McKenney 611c27b644SPaul E. McKenneyThe "Positive: 0 Negative: 3" and the "Never 0 3" each indicate that 621c27b644SPaul E. McKenneythis litmus test's "exists" clause can not be satisfied. 631c27b644SPaul E. McKenney 641c27b644SPaul E. McKenneySee "herd7 -help" or "herdtools7/doc/" for more information. 651c27b644SPaul E. McKenney 661c27b644SPaul E. McKenney 671c27b644SPaul E. McKenney===================== 681c27b644SPaul E. McKenneyBASIC USAGE: KLITMUS7 691c27b644SPaul E. McKenney===================== 701c27b644SPaul E. McKenney 711c27b644SPaul E. McKenneyThe "klitmus7" tool converts a litmus test into a Linux kernel module, 721c27b644SPaul E. McKenneywhich may then be loaded and run. 731c27b644SPaul E. McKenney 7471b7ff5eSAndrea ParriFor example, to run SB+fencembonceonces.litmus against hardware: 751c27b644SPaul E. McKenney 761c27b644SPaul E. McKenney $ mkdir mymodules 7771b7ff5eSAndrea Parri $ klitmus7 -o mymodules litmus-tests/SB+fencembonceonces.litmus 781c27b644SPaul E. McKenney $ cd mymodules ; make 791c27b644SPaul E. McKenney $ sudo sh run.sh 801c27b644SPaul E. McKenney 811c27b644SPaul E. McKenneyThe corresponding output includes: 821c27b644SPaul E. McKenney 8371b7ff5eSAndrea Parri Test SB+fencembonceonces Allowed 841c27b644SPaul E. McKenney Histogram (3 states) 851c27b644SPaul E. McKenney 644580 :>0:r0=1; 1:r0=0; 861c27b644SPaul E. McKenney 644328 :>0:r0=0; 1:r0=1; 871c27b644SPaul E. McKenney 711092 :>0:r0=1; 1:r0=1; 881c27b644SPaul E. McKenney No 891c27b644SPaul E. McKenney Witnesses 901c27b644SPaul E. McKenney Positive: 0, Negative: 2000000 911c27b644SPaul E. McKenney Condition exists (0:r0=0 /\ 1:r0=0) is NOT validated 921c27b644SPaul E. McKenney Hash=d66d99523e2cac6b06e66f4c995ebb48 9371b7ff5eSAndrea Parri Observation SB+fencembonceonces Never 0 2000000 9471b7ff5eSAndrea Parri Time SB+fencembonceonces 0.16 951c27b644SPaul E. McKenney 961c27b644SPaul E. McKenneyThe "Positive: 0 Negative: 2000000" and the "Never 0 2000000" indicate 971c27b644SPaul E. McKenneythat during two million trials, the state specified in this litmus 981c27b644SPaul E. McKenneytest's "exists" clause was not reached. 991c27b644SPaul E. McKenney 1001c27b644SPaul E. McKenneyAnd, as with "herd7", please see "klitmus7 -help" or "herdtools7/doc/" 1011c27b644SPaul E. McKenneyfor more information. 1021c27b644SPaul E. McKenney 1031c27b644SPaul E. McKenney 1041c27b644SPaul E. McKenney==================== 1051c27b644SPaul E. McKenneyDESCRIPTION OF FILES 1061c27b644SPaul E. McKenney==================== 1071c27b644SPaul E. McKenney 1081c27b644SPaul E. McKenneyDocumentation/cheatsheet.txt 1091c27b644SPaul E. McKenney Quick-reference guide to the Linux-kernel memory model. 1101c27b644SPaul E. McKenney 1111c27b644SPaul E. McKenneyDocumentation/explanation.txt 1121c27b644SPaul E. McKenney Describes the memory model in detail. 1131c27b644SPaul E. McKenney 1141c27b644SPaul E. McKenneyDocumentation/recipes.txt 1151c27b644SPaul E. McKenney Lists common memory-ordering patterns. 1161c27b644SPaul E. McKenney 1171c27b644SPaul E. McKenneyDocumentation/references.txt 1181c27b644SPaul E. McKenney Provides background reading. 1191c27b644SPaul E. McKenney 1201c27b644SPaul E. McKenneylinux-kernel.bell 1211c27b644SPaul E. McKenney Categorizes the relevant instructions, including memory 1221c27b644SPaul E. McKenney references, memory barriers, atomic read-modify-write operations, 1231c27b644SPaul E. McKenney lock acquisition/release, and RCU operations. 1241c27b644SPaul E. McKenney 1251c27b644SPaul E. McKenney More formally, this file (1) lists the subtypes of the various 1261c27b644SPaul E. McKenney event types used by the memory model and (2) performs RCU 1271c27b644SPaul E. McKenney read-side critical section nesting analysis. 1281c27b644SPaul E. McKenney 1291c27b644SPaul E. McKenneylinux-kernel.cat 1301c27b644SPaul E. McKenney Specifies what reorderings are forbidden by memory references, 1311c27b644SPaul E. McKenney memory barriers, atomic read-modify-write operations, and RCU. 1321c27b644SPaul E. McKenney 1331c27b644SPaul E. McKenney More formally, this file specifies what executions are forbidden 1341c27b644SPaul E. McKenney by the memory model. Allowed executions are those which 1351c27b644SPaul E. McKenney satisfy the model's "coherence", "atomic", "happens-before", 1361c27b644SPaul E. McKenney "propagation", and "rcu" axioms, which are defined in the file. 1371c27b644SPaul E. McKenney 1381c27b644SPaul E. McKenneylinux-kernel.cfg 1391c27b644SPaul E. McKenney Convenience file that gathers the common-case herd7 command-line 1401c27b644SPaul E. McKenney arguments. 1411c27b644SPaul E. McKenney 1421c27b644SPaul E. McKenneylinux-kernel.def 1431c27b644SPaul E. McKenney Maps from C-like syntax to herd7's internal litmus-test 1441c27b644SPaul E. McKenney instruction-set architecture. 1451c27b644SPaul E. McKenney 1461c27b644SPaul E. McKenneylitmus-tests 1471c27b644SPaul E. McKenney Directory containing a few representative litmus tests, which 1481c27b644SPaul E. McKenney are listed in litmus-tests/README. A great deal more litmus 1491c27b644SPaul E. McKenney tests are available at https://github.com/paulmckrcu/litmus. 1501c27b644SPaul E. McKenney 1511c27b644SPaul E. McKenneylock.cat 1521c27b644SPaul E. McKenney Provides a front-end analysis of lock acquisition and release, 1531c27b644SPaul E. McKenney for example, associating a lock acquisition with the preceding 1541c27b644SPaul E. McKenney and following releases and checking for self-deadlock. 1551c27b644SPaul E. McKenney 1561c27b644SPaul E. McKenney More formally, this file defines a performance-enhanced scheme 1571c27b644SPaul E. McKenney for generation of the possible reads-from and coherence order 1581c27b644SPaul E. McKenney relations on the locking primitives. 1591c27b644SPaul E. McKenney 1601c27b644SPaul E. McKenneyREADME 1611c27b644SPaul E. McKenney This file. 1621c27b644SPaul E. McKenney 163b02eb5b0SPaul E. McKenneyscripts Various scripts, see scripts/README. 164b02eb5b0SPaul E. McKenney 1651c27b644SPaul E. McKenney 1661c27b644SPaul E. McKenney=========== 1671c27b644SPaul E. McKenneyLIMITATIONS 1681c27b644SPaul E. McKenney=========== 1691c27b644SPaul E. McKenney 1706738ff85SAndrea ParriThe Linux-kernel memory model (LKMM) has the following limitations: 1711c27b644SPaul E. McKenney 1726738ff85SAndrea Parri1. Compiler optimizations are not accurately modeled. Of course, 1736738ff85SAndrea Parri the use of READ_ONCE() and WRITE_ONCE() limits the compiler's 1746738ff85SAndrea Parri ability to optimize, but under some circumstances it is possible 1756738ff85SAndrea Parri for the compiler to undermine the memory model. For more 1766738ff85SAndrea Parri information, see Documentation/explanation.txt (in particular, 1776738ff85SAndrea Parri the "THE PROGRAM ORDER RELATION: po AND po-loc" and "A WARNING" 1786738ff85SAndrea Parri sections). 1791c27b644SPaul E. McKenney 180d8fa25c4SPaul E. McKenney Note that this limitation in turn limits LKMM's ability to 181d8fa25c4SPaul E. McKenney accurately model address, control, and data dependencies. 182d8fa25c4SPaul E. McKenney For example, if the compiler can deduce the value of some variable 183d8fa25c4SPaul E. McKenney carrying a dependency, then the compiler can break that dependency 184d8fa25c4SPaul E. McKenney by substituting a constant of that value. 185d8fa25c4SPaul E. McKenney 1861c27b644SPaul E. McKenney2. Multiple access sizes for a single variable are not supported, 1871c27b644SPaul E. McKenney and neither are misaligned or partially overlapping accesses. 1881c27b644SPaul E. McKenney 1891c27b644SPaul E. McKenney3. Exceptions and interrupts are not modeled. In some cases, 1901c27b644SPaul E. McKenney this limitation can be overcome by modeling the interrupt or 1911c27b644SPaul E. McKenney exception with an additional process. 1921c27b644SPaul E. McKenney 1931c27b644SPaul E. McKenney4. I/O such as MMIO or DMA is not supported. 1941c27b644SPaul E. McKenney 1951c27b644SPaul E. McKenney5. Self-modifying code (such as that found in the kernel's 1961c27b644SPaul E. McKenney alternatives mechanism, function tracer, Berkeley Packet Filter 1971c27b644SPaul E. McKenney JIT compiler, and module loader) is not supported. 1981c27b644SPaul E. McKenney 1991c27b644SPaul E. McKenney6. Complete modeling of all variants of atomic read-modify-write 2001c27b644SPaul E. McKenney operations, locking primitives, and RCU is not provided. 2011c27b644SPaul E. McKenney For example, call_rcu() and rcu_barrier() are not supported. 2021c27b644SPaul E. McKenney However, a substantial amount of support is provided for these 2031c27b644SPaul E. McKenney operations, as shown in the linux-kernel.def file. 2041c27b644SPaul E. McKenney 205d8fa25c4SPaul E. McKenney a. When rcu_assign_pointer() is passed NULL, the Linux 206d8fa25c4SPaul E. McKenney kernel provides no ordering, but LKMM models this 207d8fa25c4SPaul E. McKenney case as a store release. 208d8fa25c4SPaul E. McKenney 209d8fa25c4SPaul E. McKenney b. The "unless" RMW operations are not currently modeled: 2104a9cc65fSBoqun Feng atomic_long_add_unless(), atomic_inc_unless_negative(), 2114a9cc65fSBoqun Feng and atomic_dec_unless_positive(). These can be emulated 212d8fa25c4SPaul E. McKenney in litmus tests, for example, by using atomic_cmpxchg(). 213d8fa25c4SPaul E. McKenney 2144a9cc65fSBoqun Feng One exception of this limitation is atomic_add_unless(), 2154a9cc65fSBoqun Feng which is provided directly by herd7 (so no corresponding 2164a9cc65fSBoqun Feng definition in linux-kernel.def). atomic_add_unless() is 2174a9cc65fSBoqun Feng modeled by herd7 therefore it can be used in litmus tests. 2184a9cc65fSBoqun Feng 219d8fa25c4SPaul E. McKenney c. The call_rcu() function is not modeled. It can be 220d8fa25c4SPaul E. McKenney emulated in litmus tests by adding another process that 221d8fa25c4SPaul E. McKenney invokes synchronize_rcu() and the body of the callback 222d8fa25c4SPaul E. McKenney function, with (for example) a release-acquire from 223d8fa25c4SPaul E. McKenney the site of the emulated call_rcu() to the beginning 224d8fa25c4SPaul E. McKenney of the additional process. 225d8fa25c4SPaul E. McKenney 226d8fa25c4SPaul E. McKenney d. The rcu_barrier() function is not modeled. It can be 227d8fa25c4SPaul E. McKenney emulated in litmus tests emulating call_rcu() via 228d8fa25c4SPaul E. McKenney (for example) a release-acquire from the end of each 229d8fa25c4SPaul E. McKenney additional call_rcu() process to the site of the 230d8fa25c4SPaul E. McKenney emulated rcu-barrier(). 231d8fa25c4SPaul E. McKenney 232ad9fd20bSPaul E. McKenney e. Although sleepable RCU (SRCU) is now modeled, there 233ad9fd20bSPaul E. McKenney are some subtle differences between its semantics and 234ad9fd20bSPaul E. McKenney those in the Linux kernel. For example, the kernel 235ad9fd20bSPaul E. McKenney might interpret the following sequence as two partially 236ad9fd20bSPaul E. McKenney overlapping SRCU read-side critical sections: 237ad9fd20bSPaul E. McKenney 238ad9fd20bSPaul E. McKenney 1 r1 = srcu_read_lock(&my_srcu); 239ad9fd20bSPaul E. McKenney 2 do_something_1(); 240ad9fd20bSPaul E. McKenney 3 r2 = srcu_read_lock(&my_srcu); 241ad9fd20bSPaul E. McKenney 4 do_something_2(); 242ad9fd20bSPaul E. McKenney 5 srcu_read_unlock(&my_srcu, r1); 243ad9fd20bSPaul E. McKenney 6 do_something_3(); 244ad9fd20bSPaul E. McKenney 7 srcu_read_unlock(&my_srcu, r2); 245ad9fd20bSPaul E. McKenney 246ad9fd20bSPaul E. McKenney In contrast, LKMM will interpret this as a nested pair of 247ad9fd20bSPaul E. McKenney SRCU read-side critical sections, with the outer critical 248ad9fd20bSPaul E. McKenney section spanning lines 1-7 and the inner critical section 249ad9fd20bSPaul E. McKenney spanning lines 3-5. 250ad9fd20bSPaul E. McKenney 251ad9fd20bSPaul E. McKenney This difference would be more of a concern had anyone 252ad9fd20bSPaul E. McKenney identified a reasonable use case for partially overlapping 253ad9fd20bSPaul E. McKenney SRCU read-side critical sections. For more information, 254ad9fd20bSPaul E. McKenney please see: https://paulmck.livejournal.com/40593.html 255d8fa25c4SPaul E. McKenney 256d8fa25c4SPaul E. McKenney f. Reader-writer locking is not modeled. It can be 257d8fa25c4SPaul E. McKenney emulated in litmus tests using atomic read-modify-write 258d8fa25c4SPaul E. McKenney operations. 259d8fa25c4SPaul E. McKenney 2601c27b644SPaul E. McKenneyThe "herd7" tool has some additional limitations of its own, apart from 2611c27b644SPaul E. McKenneythe memory model: 2621c27b644SPaul E. McKenney 2631c27b644SPaul E. McKenney1. Non-trivial data structures such as arrays or structures are 2641c27b644SPaul E. McKenney not supported. However, pointers are supported, allowing trivial 2651c27b644SPaul E. McKenney linked lists to be constructed. 2661c27b644SPaul E. McKenney 2671c27b644SPaul E. McKenney2. Dynamic memory allocation is not supported, although this can 2681c27b644SPaul E. McKenney be worked around in some cases by supplying multiple statically 2691c27b644SPaul E. McKenney allocated variables. 2701c27b644SPaul E. McKenney 2711c27b644SPaul E. McKenneySome of these limitations may be overcome in the future, but others are 2721c27b644SPaul E. McKenneymore likely to be addressed by incorporating the Linux-kernel memory model 2731c27b644SPaul E. McKenneyinto other tools. 274d8fa25c4SPaul E. McKenney 275d8fa25c4SPaul E. McKenneyFinally, please note that LKMM is subject to change as hardware, use cases, 276d8fa25c4SPaul E. McKenneyand compilers evolve. 277