xref: /openbmc/linux/tools/memory-model/README (revision 5b759db4)
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,
31d075a78aSAkira Yokosawathis is not absolutely guaranteed.
32d075a78aSAkira Yokosawa
33d075a78aSAkira YokosawaFor example, a future version of herd7 might not work with the model
34d075a78aSAkira Yokosawain this release.  A compatible model will likely be made available in
35d075a78aSAkira Yokosawaa later release of Linux kernel.
36d075a78aSAkira Yokosawa
37d075a78aSAkira YokosawaIf you absolutely need to run the model in this particular release,
38d075a78aSAkira Yokosawaplease try using the exact version called out above.
39d075a78aSAkira Yokosawa
40d075a78aSAkira Yokosawaklitmus7 is independent of the model provided here.  It has its own
41d075a78aSAkira Yokosawadependency on a target kernel release where converted code is built
42d075a78aSAkira Yokosawaand executed.  Any change in kernel APIs essential to klitmus7 will
43d075a78aSAkira Yokosawanecessitate an upgrade of klitmus7.
44d075a78aSAkira Yokosawa
45d075a78aSAkira YokosawaIf you find any compatibility issues in klitmus7, please inform the
46d075a78aSAkira Yokosawamemory model maintainers.
47d075a78aSAkira Yokosawa
48d075a78aSAkira Yokosawaklitmus7 Compatibility Table
49d075a78aSAkira Yokosawa----------------------------
50d075a78aSAkira Yokosawa
51d075a78aSAkira Yokosawa	============  ==========
52d075a78aSAkira Yokosawa	target Linux  herdtools7
53d075a78aSAkira Yokosawa	------------  ----------
543d5c7032SAkira Yokosawa	     -- 4.14  7.48 --
55d075a78aSAkira Yokosawa	4.15 -- 4.19  7.49 --
56d075a78aSAkira Yokosawa	4.20 -- 5.5   7.54 --
57*5b759db4SAkira Yokosawa	5.6  -- 5.16  7.56 --
58*5b759db4SAkira Yokosawa	5.17 --       7.56.1 --
59d075a78aSAkira Yokosawa	============  ==========
60034fb712SAndrea Parri
611c27b644SPaul E. McKenney
621c27b644SPaul E. McKenney==================
631c27b644SPaul E. McKenneyBASIC USAGE: HERD7
641c27b644SPaul E. McKenney==================
651c27b644SPaul E. McKenney
661c27b644SPaul E. McKenneyThe memory model is used, in conjunction with "herd7", to exhaustively
67984f272bSPaul E. McKenneyexplore the state space of small litmus tests.  Documentation describing
68984f272bSPaul E. McKenneythe format, features, capabilities and limitations of these litmus
69984f272bSPaul E. McKenneytests is available in tools/memory-model/Documentation/litmus-tests.txt.
701c27b644SPaul E. McKenney
71984f272bSPaul E. McKenneyExample litmus tests may be found in the Linux-kernel source tree:
721c27b644SPaul E. McKenney
73984f272bSPaul E. McKenney	tools/memory-model/litmus-tests/
74984f272bSPaul E. McKenney	Documentation/litmus-tests/
75984f272bSPaul E. McKenney
76984f272bSPaul E. McKenneySeveral thousand more example litmus tests are available here:
77984f272bSPaul E. McKenney
78984f272bSPaul E. McKenney	https://github.com/paulmckrcu/litmus
79984f272bSPaul E. McKenney	https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/perfbook.git/tree/CodeSamples/formal/herd
80984f272bSPaul E. McKenney	https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/perfbook.git/tree/CodeSamples/formal/litmus
81984f272bSPaul E. McKenney
82984f272bSPaul E. McKenneyDocumentation describing litmus tests and now to use them may be found
83984f272bSPaul E. McKenneyhere:
84984f272bSPaul E. McKenney
85984f272bSPaul E. McKenney	tools/memory-model/Documentation/litmus-tests.txt
86984f272bSPaul E. McKenney
87984f272bSPaul E. McKenneyThe remainder of this section uses the SB+fencembonceonces.litmus test
88984f272bSPaul E. McKenneylocated in the tools/memory-model directory.
89984f272bSPaul E. McKenney
90984f272bSPaul E. McKenneyTo run SB+fencembonceonces.litmus against the memory model:
91984f272bSPaul E. McKenney
92984f272bSPaul E. McKenney  $ cd $LINUX_SOURCE_TREE/tools/memory-model
9371b7ff5eSAndrea Parri  $ herd7 -conf linux-kernel.cfg litmus-tests/SB+fencembonceonces.litmus
941c27b644SPaul E. McKenney
951c27b644SPaul E. McKenneyHere is the corresponding output:
961c27b644SPaul E. McKenney
9771b7ff5eSAndrea Parri  Test SB+fencembonceonces Allowed
981c27b644SPaul E. McKenney  States 3
991c27b644SPaul E. McKenney  0:r0=0; 1:r0=1;
1001c27b644SPaul E. McKenney  0:r0=1; 1:r0=0;
1011c27b644SPaul E. McKenney  0:r0=1; 1:r0=1;
1021c27b644SPaul E. McKenney  No
1031c27b644SPaul E. McKenney  Witnesses
1041c27b644SPaul E. McKenney  Positive: 0 Negative: 3
1051c27b644SPaul E. McKenney  Condition exists (0:r0=0 /\ 1:r0=0)
10671b7ff5eSAndrea Parri  Observation SB+fencembonceonces Never 0 3
10771b7ff5eSAndrea Parri  Time SB+fencembonceonces 0.01
1081c27b644SPaul E. McKenney  Hash=d66d99523e2cac6b06e66f4c995ebb48
1091c27b644SPaul E. McKenney
1101c27b644SPaul E. McKenneyThe "Positive: 0 Negative: 3" and the "Never 0 3" each indicate that
1111c27b644SPaul E. McKenneythis litmus test's "exists" clause can not be satisfied.
1121c27b644SPaul E. McKenney
113984f272bSPaul E. McKenneySee "herd7 -help" or "herdtools7/doc/" for more information on running the
114984f272bSPaul E. McKenneytool itself, but please be aware that this documentation is intended for
115984f272bSPaul E. McKenneypeople who work on the memory model itself, that is, people making changes
116984f272bSPaul E. McKenneyto the tools/memory-model/linux-kernel.* files.  It is not intended for
117984f272bSPaul E. McKenneypeople focusing on writing, understanding, and running LKMM litmus tests.
1181c27b644SPaul E. McKenney
1191c27b644SPaul E. McKenney
1201c27b644SPaul E. McKenney=====================
1211c27b644SPaul E. McKenneyBASIC USAGE: KLITMUS7
1221c27b644SPaul E. McKenney=====================
1231c27b644SPaul E. McKenney
1241c27b644SPaul E. McKenneyThe "klitmus7" tool converts a litmus test into a Linux kernel module,
1251c27b644SPaul E. McKenneywhich may then be loaded and run.
1261c27b644SPaul E. McKenney
12771b7ff5eSAndrea ParriFor example, to run SB+fencembonceonces.litmus against hardware:
1281c27b644SPaul E. McKenney
1291c27b644SPaul E. McKenney  $ mkdir mymodules
13071b7ff5eSAndrea Parri  $ klitmus7 -o mymodules litmus-tests/SB+fencembonceonces.litmus
1311c27b644SPaul E. McKenney  $ cd mymodules ; make
1321c27b644SPaul E. McKenney  $ sudo sh run.sh
1331c27b644SPaul E. McKenney
1341c27b644SPaul E. McKenneyThe corresponding output includes:
1351c27b644SPaul E. McKenney
13671b7ff5eSAndrea Parri  Test SB+fencembonceonces Allowed
1371c27b644SPaul E. McKenney  Histogram (3 states)
1381c27b644SPaul E. McKenney  644580  :>0:r0=1; 1:r0=0;
1391c27b644SPaul E. McKenney  644328  :>0:r0=0; 1:r0=1;
1401c27b644SPaul E. McKenney  711092  :>0:r0=1; 1:r0=1;
1411c27b644SPaul E. McKenney  No
1421c27b644SPaul E. McKenney  Witnesses
1431c27b644SPaul E. McKenney  Positive: 0, Negative: 2000000
1441c27b644SPaul E. McKenney  Condition exists (0:r0=0 /\ 1:r0=0) is NOT validated
1451c27b644SPaul E. McKenney  Hash=d66d99523e2cac6b06e66f4c995ebb48
14671b7ff5eSAndrea Parri  Observation SB+fencembonceonces Never 0 2000000
14771b7ff5eSAndrea Parri  Time SB+fencembonceonces 0.16
1481c27b644SPaul E. McKenney
1491c27b644SPaul E. McKenneyThe "Positive: 0 Negative: 2000000" and the "Never 0 2000000" indicate
1501c27b644SPaul E. McKenneythat during two million trials, the state specified in this litmus
1511c27b644SPaul E. McKenneytest's "exists" clause was not reached.
1521c27b644SPaul E. McKenney
1531c27b644SPaul E. McKenneyAnd, as with "herd7", please see "klitmus7 -help" or "herdtools7/doc/"
154984f272bSPaul E. McKenneyfor more information.  And again, please be aware that this documentation
155984f272bSPaul E. McKenneyis intended for people who work on the memory model itself, that is,
156984f272bSPaul E. McKenneypeople making changes to the tools/memory-model/linux-kernel.* files.
157984f272bSPaul E. McKenneyIt is not intended for people focusing on writing, understanding, and
158984f272bSPaul E. McKenneyrunning LKMM litmus tests.
1591c27b644SPaul E. McKenney
1601c27b644SPaul E. McKenney
1611c27b644SPaul E. McKenney====================
1621c27b644SPaul E. McKenneyDESCRIPTION OF FILES
1631c27b644SPaul E. McKenney====================
1641c27b644SPaul E. McKenney
165ab8bcad6SPaul E. McKenneyDocumentation/README
166ab8bcad6SPaul E. McKenney	Guide to the other documents in the Documentation/ directory.
1670b8c06b7SPaul E. McKenney
1681c27b644SPaul E. McKenneylinux-kernel.bell
1691c27b644SPaul E. McKenney	Categorizes the relevant instructions, including memory
1701c27b644SPaul E. McKenney	references, memory barriers, atomic read-modify-write operations,
1711c27b644SPaul E. McKenney	lock acquisition/release, and RCU operations.
1721c27b644SPaul E. McKenney
1731c27b644SPaul E. McKenney	More formally, this file (1) lists the subtypes of the various
1741c27b644SPaul E. McKenney	event types used by the memory model and (2) performs RCU
1751c27b644SPaul E. McKenney	read-side critical section nesting analysis.
1761c27b644SPaul E. McKenney
1771c27b644SPaul E. McKenneylinux-kernel.cat
1781c27b644SPaul E. McKenney	Specifies what reorderings are forbidden by memory references,
1791c27b644SPaul E. McKenney	memory barriers, atomic read-modify-write operations, and RCU.
1801c27b644SPaul E. McKenney
1811c27b644SPaul E. McKenney	More formally, this file specifies what executions are forbidden
1821c27b644SPaul E. McKenney	by the memory model.  Allowed executions are those which
1831c27b644SPaul E. McKenney	satisfy the model's "coherence", "atomic", "happens-before",
1841c27b644SPaul E. McKenney	"propagation", and "rcu" axioms, which are defined in the file.
1851c27b644SPaul E. McKenney
1861c27b644SPaul E. McKenneylinux-kernel.cfg
1871c27b644SPaul E. McKenney	Convenience file that gathers the common-case herd7 command-line
1881c27b644SPaul E. McKenney	arguments.
1891c27b644SPaul E. McKenney
1901c27b644SPaul E. McKenneylinux-kernel.def
1911c27b644SPaul E. McKenney	Maps from C-like syntax to herd7's internal litmus-test
1921c27b644SPaul E. McKenney	instruction-set architecture.
1931c27b644SPaul E. McKenney
1941c27b644SPaul E. McKenneylitmus-tests
1951c27b644SPaul E. McKenney	Directory containing a few representative litmus tests, which
1961c27b644SPaul E. McKenney	are listed in litmus-tests/README.  A great deal more litmus
1971c27b644SPaul E. McKenney	tests are available at https://github.com/paulmckrcu/litmus.
1981c27b644SPaul E. McKenney
199b47c05ecSBoqun Feng	By "representative", it means the one in the litmus-tests
200b47c05ecSBoqun Feng	directory is:
201b47c05ecSBoqun Feng
202b47c05ecSBoqun Feng		1) simple, the number of threads should be relatively
203b47c05ecSBoqun Feng		   small and each thread function should be relatively
204b47c05ecSBoqun Feng		   simple.
205b47c05ecSBoqun Feng		2) orthogonal, there should be no two litmus tests
206b47c05ecSBoqun Feng		   describing the same aspect of the memory model.
207b47c05ecSBoqun Feng		3) textbook, developers can easily copy-paste-modify
208b47c05ecSBoqun Feng		   the litmus tests to use the patterns on their own
209b47c05ecSBoqun Feng		   code.
210b47c05ecSBoqun Feng
2111c27b644SPaul E. McKenneylock.cat
2121c27b644SPaul E. McKenney	Provides a front-end analysis of lock acquisition and release,
2131c27b644SPaul E. McKenney	for example, associating a lock acquisition with the preceding
2141c27b644SPaul E. McKenney	and following releases and checking for self-deadlock.
2151c27b644SPaul E. McKenney
2161c27b644SPaul E. McKenney	More formally, this file defines a performance-enhanced scheme
2171c27b644SPaul E. McKenney	for generation of the possible reads-from and coherence order
2181c27b644SPaul E. McKenney	relations on the locking primitives.
2191c27b644SPaul E. McKenney
2201c27b644SPaul E. McKenneyREADME
2211c27b644SPaul E. McKenney	This file.
2221c27b644SPaul E. McKenney
223b02eb5b0SPaul E. McKenneyscripts	Various scripts, see scripts/README.
224