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