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 23*8f7f2fbdSPaul E. McKenneyVersion 7.48 of the "herd7" and "klitmus7" tools must be downloaded 24*8f7f2fbdSPaul 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. McKenneyAlternatively, Abhishek Bhardwaj has kindly provided a Docker image 311c27b644SPaul E. McKenneyof these tools at "abhishek40/memory-model". Abhishek suggests the 321c27b644SPaul E. McKenneyfollowing commands to install and use this image: 331c27b644SPaul E. McKenney 341c27b644SPaul E. McKenney - Users should install Docker for their distribution. 351c27b644SPaul E. McKenney - docker run -itd abhishek40/memory-model 361c27b644SPaul E. McKenney - docker attach <id-emitted-from-the-previous-command> 371c27b644SPaul E. McKenney 381c27b644SPaul E. McKenneyGentoo users might wish to make use of Patrick McLean's package: 391c27b644SPaul E. McKenney 401c27b644SPaul E. McKenney https://gitweb.gentoo.org/repo/gentoo.git/tree/dev-util/herdtools7 411c27b644SPaul E. McKenney 421c27b644SPaul E. McKenneyThese packages may not be up-to-date with respect to the GitHub 431c27b644SPaul E. McKenneyrepository. 441c27b644SPaul E. McKenney 451c27b644SPaul E. McKenney 461c27b644SPaul E. McKenney================== 471c27b644SPaul E. McKenneyBASIC USAGE: HERD7 481c27b644SPaul E. McKenney================== 491c27b644SPaul E. McKenney 501c27b644SPaul E. McKenneyThe memory model is used, in conjunction with "herd7", to exhaustively 511c27b644SPaul E. McKenneyexplore the state space of small litmus tests. 521c27b644SPaul E. McKenney 531c27b644SPaul E. McKenneyFor example, to run SB+mbonceonces.litmus against the memory model: 541c27b644SPaul E. McKenney 551c27b644SPaul E. McKenney $ herd7 -conf linux-kernel.cfg litmus-tests/SB+mbonceonces.litmus 561c27b644SPaul E. McKenney 571c27b644SPaul E. McKenneyHere is the corresponding output: 581c27b644SPaul E. McKenney 591c27b644SPaul E. McKenney Test SB+mbonceonces Allowed 601c27b644SPaul E. McKenney States 3 611c27b644SPaul E. McKenney 0:r0=0; 1:r0=1; 621c27b644SPaul E. McKenney 0:r0=1; 1:r0=0; 631c27b644SPaul E. McKenney 0:r0=1; 1:r0=1; 641c27b644SPaul E. McKenney No 651c27b644SPaul E. McKenney Witnesses 661c27b644SPaul E. McKenney Positive: 0 Negative: 3 671c27b644SPaul E. McKenney Condition exists (0:r0=0 /\ 1:r0=0) 681c27b644SPaul E. McKenney Observation SB+mbonceonces Never 0 3 691c27b644SPaul E. McKenney Time SB+mbonceonces 0.01 701c27b644SPaul E. McKenney Hash=d66d99523e2cac6b06e66f4c995ebb48 711c27b644SPaul E. McKenney 721c27b644SPaul E. McKenneyThe "Positive: 0 Negative: 3" and the "Never 0 3" each indicate that 731c27b644SPaul E. McKenneythis litmus test's "exists" clause can not be satisfied. 741c27b644SPaul E. McKenney 751c27b644SPaul E. McKenneySee "herd7 -help" or "herdtools7/doc/" for more information. 761c27b644SPaul E. McKenney 771c27b644SPaul E. McKenney 781c27b644SPaul E. McKenney===================== 791c27b644SPaul E. McKenneyBASIC USAGE: KLITMUS7 801c27b644SPaul E. McKenney===================== 811c27b644SPaul E. McKenney 821c27b644SPaul E. McKenneyThe "klitmus7" tool converts a litmus test into a Linux kernel module, 831c27b644SPaul E. McKenneywhich may then be loaded and run. 841c27b644SPaul E. McKenney 851c27b644SPaul E. McKenneyFor example, to run SB+mbonceonces.litmus against hardware: 861c27b644SPaul E. McKenney 871c27b644SPaul E. McKenney $ mkdir mymodules 881c27b644SPaul E. McKenney $ klitmus7 -o mymodules litmus-tests/SB+mbonceonces.litmus 891c27b644SPaul E. McKenney $ cd mymodules ; make 901c27b644SPaul E. McKenney $ sudo sh run.sh 911c27b644SPaul E. McKenney 921c27b644SPaul E. McKenneyThe corresponding output includes: 931c27b644SPaul E. McKenney 941c27b644SPaul E. McKenney Test SB+mbonceonces Allowed 951c27b644SPaul E. McKenney Histogram (3 states) 961c27b644SPaul E. McKenney 644580 :>0:r0=1; 1:r0=0; 971c27b644SPaul E. McKenney 644328 :>0:r0=0; 1:r0=1; 981c27b644SPaul E. McKenney 711092 :>0:r0=1; 1:r0=1; 991c27b644SPaul E. McKenney No 1001c27b644SPaul E. McKenney Witnesses 1011c27b644SPaul E. McKenney Positive: 0, Negative: 2000000 1021c27b644SPaul E. McKenney Condition exists (0:r0=0 /\ 1:r0=0) is NOT validated 1031c27b644SPaul E. McKenney Hash=d66d99523e2cac6b06e66f4c995ebb48 1041c27b644SPaul E. McKenney Observation SB+mbonceonces Never 0 2000000 1051c27b644SPaul E. McKenney Time SB+mbonceonces 0.16 1061c27b644SPaul E. McKenney 1071c27b644SPaul E. McKenneyThe "Positive: 0 Negative: 2000000" and the "Never 0 2000000" indicate 1081c27b644SPaul E. McKenneythat during two million trials, the state specified in this litmus 1091c27b644SPaul E. McKenneytest's "exists" clause was not reached. 1101c27b644SPaul E. McKenney 1111c27b644SPaul E. McKenneyAnd, as with "herd7", please see "klitmus7 -help" or "herdtools7/doc/" 1121c27b644SPaul E. McKenneyfor more information. 1131c27b644SPaul E. McKenney 1141c27b644SPaul E. McKenney 1151c27b644SPaul E. McKenney==================== 1161c27b644SPaul E. McKenneyDESCRIPTION OF FILES 1171c27b644SPaul E. McKenney==================== 1181c27b644SPaul E. McKenney 1191c27b644SPaul E. McKenneyDocumentation/cheatsheet.txt 1201c27b644SPaul E. McKenney Quick-reference guide to the Linux-kernel memory model. 1211c27b644SPaul E. McKenney 1221c27b644SPaul E. McKenneyDocumentation/explanation.txt 1231c27b644SPaul E. McKenney Describes the memory model in detail. 1241c27b644SPaul E. McKenney 1251c27b644SPaul E. McKenneyDocumentation/recipes.txt 1261c27b644SPaul E. McKenney Lists common memory-ordering patterns. 1271c27b644SPaul E. McKenney 1281c27b644SPaul E. McKenneyDocumentation/references.txt 1291c27b644SPaul E. McKenney Provides background reading. 1301c27b644SPaul E. McKenney 1311c27b644SPaul E. McKenneylinux-kernel.bell 1321c27b644SPaul E. McKenney Categorizes the relevant instructions, including memory 1331c27b644SPaul E. McKenney references, memory barriers, atomic read-modify-write operations, 1341c27b644SPaul E. McKenney lock acquisition/release, and RCU operations. 1351c27b644SPaul E. McKenney 1361c27b644SPaul E. McKenney More formally, this file (1) lists the subtypes of the various 1371c27b644SPaul E. McKenney event types used by the memory model and (2) performs RCU 1381c27b644SPaul E. McKenney read-side critical section nesting analysis. 1391c27b644SPaul E. McKenney 1401c27b644SPaul E. McKenneylinux-kernel.cat 1411c27b644SPaul E. McKenney Specifies what reorderings are forbidden by memory references, 1421c27b644SPaul E. McKenney memory barriers, atomic read-modify-write operations, and RCU. 1431c27b644SPaul E. McKenney 1441c27b644SPaul E. McKenney More formally, this file specifies what executions are forbidden 1451c27b644SPaul E. McKenney by the memory model. Allowed executions are those which 1461c27b644SPaul E. McKenney satisfy the model's "coherence", "atomic", "happens-before", 1471c27b644SPaul E. McKenney "propagation", and "rcu" axioms, which are defined in the file. 1481c27b644SPaul E. McKenney 1491c27b644SPaul E. McKenneylinux-kernel.cfg 1501c27b644SPaul E. McKenney Convenience file that gathers the common-case herd7 command-line 1511c27b644SPaul E. McKenney arguments. 1521c27b644SPaul E. McKenney 1531c27b644SPaul E. McKenneylinux-kernel.def 1541c27b644SPaul E. McKenney Maps from C-like syntax to herd7's internal litmus-test 1551c27b644SPaul E. McKenney instruction-set architecture. 1561c27b644SPaul E. McKenney 1571c27b644SPaul E. McKenneylitmus-tests 1581c27b644SPaul E. McKenney Directory containing a few representative litmus tests, which 1591c27b644SPaul E. McKenney are listed in litmus-tests/README. A great deal more litmus 1601c27b644SPaul E. McKenney tests are available at https://github.com/paulmckrcu/litmus. 1611c27b644SPaul E. McKenney 1621c27b644SPaul E. McKenneylock.cat 1631c27b644SPaul E. McKenney Provides a front-end analysis of lock acquisition and release, 1641c27b644SPaul E. McKenney for example, associating a lock acquisition with the preceding 1651c27b644SPaul E. McKenney and following releases and checking for self-deadlock. 1661c27b644SPaul E. McKenney 1671c27b644SPaul E. McKenney More formally, this file defines a performance-enhanced scheme 1681c27b644SPaul E. McKenney for generation of the possible reads-from and coherence order 1691c27b644SPaul E. McKenney relations on the locking primitives. 1701c27b644SPaul E. McKenney 1711c27b644SPaul E. McKenneyREADME 1721c27b644SPaul E. McKenney This file. 1731c27b644SPaul E. McKenney 1741c27b644SPaul E. McKenney 1751c27b644SPaul E. McKenney=========== 1761c27b644SPaul E. McKenneyLIMITATIONS 1771c27b644SPaul E. McKenney=========== 1781c27b644SPaul E. McKenney 1791c27b644SPaul E. McKenneyThe Linux-kernel memory model has the following limitations: 1801c27b644SPaul E. McKenney 1811c27b644SPaul E. McKenney1. Compiler optimizations are not modeled. Of course, the use 1821c27b644SPaul E. McKenney of READ_ONCE() and WRITE_ONCE() limits the compiler's ability 1831c27b644SPaul E. McKenney to optimize, but there is Linux-kernel code that uses bare C 1841c27b644SPaul E. McKenney memory accesses. Handling this code is on the to-do list. 1851c27b644SPaul E. McKenney For more information, see Documentation/explanation.txt (in 1861c27b644SPaul E. McKenney particular, the "THE PROGRAM ORDER RELATION: po AND po-loc" 1871c27b644SPaul E. McKenney and "A WARNING" sections). 1881c27b644SPaul E. McKenney 1891c27b644SPaul E. McKenney2. Multiple access sizes for a single variable are not supported, 1901c27b644SPaul E. McKenney and neither are misaligned or partially overlapping accesses. 1911c27b644SPaul E. McKenney 1921c27b644SPaul E. McKenney3. Exceptions and interrupts are not modeled. In some cases, 1931c27b644SPaul E. McKenney this limitation can be overcome by modeling the interrupt or 1941c27b644SPaul E. McKenney exception with an additional process. 1951c27b644SPaul E. McKenney 1961c27b644SPaul E. McKenney4. I/O such as MMIO or DMA is not supported. 1971c27b644SPaul E. McKenney 1981c27b644SPaul E. McKenney5. Self-modifying code (such as that found in the kernel's 1991c27b644SPaul E. McKenney alternatives mechanism, function tracer, Berkeley Packet Filter 2001c27b644SPaul E. McKenney JIT compiler, and module loader) is not supported. 2011c27b644SPaul E. McKenney 2021c27b644SPaul E. McKenney6. Complete modeling of all variants of atomic read-modify-write 2031c27b644SPaul E. McKenney operations, locking primitives, and RCU is not provided. 2041c27b644SPaul E. McKenney For example, call_rcu() and rcu_barrier() are not supported. 2051c27b644SPaul E. McKenney However, a substantial amount of support is provided for these 2061c27b644SPaul E. McKenney operations, as shown in the linux-kernel.def file. 2071c27b644SPaul E. McKenney 2081c27b644SPaul E. McKenneyThe "herd7" tool has some additional limitations of its own, apart from 2091c27b644SPaul E. McKenneythe memory model: 2101c27b644SPaul E. McKenney 2111c27b644SPaul E. McKenney1. Non-trivial data structures such as arrays or structures are 2121c27b644SPaul E. McKenney not supported. However, pointers are supported, allowing trivial 2131c27b644SPaul E. McKenney linked lists to be constructed. 2141c27b644SPaul E. McKenney 2151c27b644SPaul E. McKenney2. Dynamic memory allocation is not supported, although this can 2161c27b644SPaul E. McKenney be worked around in some cases by supplying multiple statically 2171c27b644SPaul E. McKenney allocated variables. 2181c27b644SPaul E. McKenney 2191c27b644SPaul E. McKenneySome of these limitations may be overcome in the future, but others are 2201c27b644SPaul E. McKenneymore likely to be addressed by incorporating the Linux-kernel memory model 2211c27b644SPaul E. McKenneyinto other tools. 222