xref: /OK3568_Linux_fs/kernel/tools/memory-model/README (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun		=====================================
2*4882a593Smuzhiyun		LINUX KERNEL MEMORY CONSISTENCY MODEL
3*4882a593Smuzhiyun		=====================================
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun============
6*4882a593SmuzhiyunINTRODUCTION
7*4882a593Smuzhiyun============
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunThis directory contains the memory consistency model (memory model, for
10*4882a593Smuzhiyunshort) of the Linux kernel, written in the "cat" language and executable
11*4882a593Smuzhiyunby the externally provided "herd7" simulator, which exhaustively explores
12*4882a593Smuzhiyunthe state space of small litmus tests.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunIn addition, the "klitmus7" tool (also externally provided) may be used
15*4882a593Smuzhiyunto convert a litmus test to a Linux kernel module, which in turn allows
16*4882a593Smuzhiyunthat litmus test to be exercised within the Linux kernel.
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun============
20*4882a593SmuzhiyunREQUIREMENTS
21*4882a593Smuzhiyun============
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunVersion 7.52 or higher of the "herd7" and "klitmus7" tools must be
24*4882a593Smuzhiyundownloaded separately:
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun  https://github.com/herd/herdtools7
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunSee "herdtools7/INSTALL.md" for installation instructions.
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunNote that although these tools usually provide backwards compatibility,
31*4882a593Smuzhiyunthis is not absolutely guaranteed.
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunFor example, a future version of herd7 might not work with the model
34*4882a593Smuzhiyunin this release.  A compatible model will likely be made available in
35*4882a593Smuzhiyuna later release of Linux kernel.
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunIf you absolutely need to run the model in this particular release,
38*4882a593Smuzhiyunplease try using the exact version called out above.
39*4882a593Smuzhiyun
40*4882a593Smuzhiyunklitmus7 is independent of the model provided here.  It has its own
41*4882a593Smuzhiyundependency on a target kernel release where converted code is built
42*4882a593Smuzhiyunand executed.  Any change in kernel APIs essential to klitmus7 will
43*4882a593Smuzhiyunnecessitate an upgrade of klitmus7.
44*4882a593Smuzhiyun
45*4882a593SmuzhiyunIf you find any compatibility issues in klitmus7, please inform the
46*4882a593Smuzhiyunmemory model maintainers.
47*4882a593Smuzhiyun
48*4882a593Smuzhiyunklitmus7 Compatibility Table
49*4882a593Smuzhiyun----------------------------
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun	============  ==========
52*4882a593Smuzhiyun	target Linux  herdtools7
53*4882a593Smuzhiyun	------------  ----------
54*4882a593Smuzhiyun	     -- 4.18  7.48 --
55*4882a593Smuzhiyun	4.15 -- 4.19  7.49 --
56*4882a593Smuzhiyun	4.20 -- 5.5   7.54 --
57*4882a593Smuzhiyun	5.6  --       7.56 --
58*4882a593Smuzhiyun	============  ==========
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun==================
62*4882a593SmuzhiyunBASIC USAGE: HERD7
63*4882a593Smuzhiyun==================
64*4882a593Smuzhiyun
65*4882a593SmuzhiyunThe memory model is used, in conjunction with "herd7", to exhaustively
66*4882a593Smuzhiyunexplore the state space of small litmus tests.  Documentation describing
67*4882a593Smuzhiyunthe format, features, capabilities and limitations of these litmus
68*4882a593Smuzhiyuntests is available in tools/memory-model/Documentation/litmus-tests.txt.
69*4882a593Smuzhiyun
70*4882a593SmuzhiyunExample litmus tests may be found in the Linux-kernel source tree:
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun	tools/memory-model/litmus-tests/
73*4882a593Smuzhiyun	Documentation/litmus-tests/
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunSeveral thousand more example litmus tests are available here:
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun	https://github.com/paulmckrcu/litmus
78*4882a593Smuzhiyun	https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/perfbook.git/tree/CodeSamples/formal/herd
79*4882a593Smuzhiyun	https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/perfbook.git/tree/CodeSamples/formal/litmus
80*4882a593Smuzhiyun
81*4882a593SmuzhiyunDocumentation describing litmus tests and now to use them may be found
82*4882a593Smuzhiyunhere:
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun	tools/memory-model/Documentation/litmus-tests.txt
85*4882a593Smuzhiyun
86*4882a593SmuzhiyunThe remainder of this section uses the SB+fencembonceonces.litmus test
87*4882a593Smuzhiyunlocated in the tools/memory-model directory.
88*4882a593Smuzhiyun
89*4882a593SmuzhiyunTo run SB+fencembonceonces.litmus against the memory model:
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun  $ cd $LINUX_SOURCE_TREE/tools/memory-model
92*4882a593Smuzhiyun  $ herd7 -conf linux-kernel.cfg litmus-tests/SB+fencembonceonces.litmus
93*4882a593Smuzhiyun
94*4882a593SmuzhiyunHere is the corresponding output:
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun  Test SB+fencembonceonces Allowed
97*4882a593Smuzhiyun  States 3
98*4882a593Smuzhiyun  0:r0=0; 1:r0=1;
99*4882a593Smuzhiyun  0:r0=1; 1:r0=0;
100*4882a593Smuzhiyun  0:r0=1; 1:r0=1;
101*4882a593Smuzhiyun  No
102*4882a593Smuzhiyun  Witnesses
103*4882a593Smuzhiyun  Positive: 0 Negative: 3
104*4882a593Smuzhiyun  Condition exists (0:r0=0 /\ 1:r0=0)
105*4882a593Smuzhiyun  Observation SB+fencembonceonces Never 0 3
106*4882a593Smuzhiyun  Time SB+fencembonceonces 0.01
107*4882a593Smuzhiyun  Hash=d66d99523e2cac6b06e66f4c995ebb48
108*4882a593Smuzhiyun
109*4882a593SmuzhiyunThe "Positive: 0 Negative: 3" and the "Never 0 3" each indicate that
110*4882a593Smuzhiyunthis litmus test's "exists" clause can not be satisfied.
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunSee "herd7 -help" or "herdtools7/doc/" for more information on running the
113*4882a593Smuzhiyuntool itself, but please be aware that this documentation is intended for
114*4882a593Smuzhiyunpeople who work on the memory model itself, that is, people making changes
115*4882a593Smuzhiyunto the tools/memory-model/linux-kernel.* files.  It is not intended for
116*4882a593Smuzhiyunpeople focusing on writing, understanding, and running LKMM litmus tests.
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun=====================
120*4882a593SmuzhiyunBASIC USAGE: KLITMUS7
121*4882a593Smuzhiyun=====================
122*4882a593Smuzhiyun
123*4882a593SmuzhiyunThe "klitmus7" tool converts a litmus test into a Linux kernel module,
124*4882a593Smuzhiyunwhich may then be loaded and run.
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunFor example, to run SB+fencembonceonces.litmus against hardware:
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun  $ mkdir mymodules
129*4882a593Smuzhiyun  $ klitmus7 -o mymodules litmus-tests/SB+fencembonceonces.litmus
130*4882a593Smuzhiyun  $ cd mymodules ; make
131*4882a593Smuzhiyun  $ sudo sh run.sh
132*4882a593Smuzhiyun
133*4882a593SmuzhiyunThe corresponding output includes:
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun  Test SB+fencembonceonces Allowed
136*4882a593Smuzhiyun  Histogram (3 states)
137*4882a593Smuzhiyun  644580  :>0:r0=1; 1:r0=0;
138*4882a593Smuzhiyun  644328  :>0:r0=0; 1:r0=1;
139*4882a593Smuzhiyun  711092  :>0:r0=1; 1:r0=1;
140*4882a593Smuzhiyun  No
141*4882a593Smuzhiyun  Witnesses
142*4882a593Smuzhiyun  Positive: 0, Negative: 2000000
143*4882a593Smuzhiyun  Condition exists (0:r0=0 /\ 1:r0=0) is NOT validated
144*4882a593Smuzhiyun  Hash=d66d99523e2cac6b06e66f4c995ebb48
145*4882a593Smuzhiyun  Observation SB+fencembonceonces Never 0 2000000
146*4882a593Smuzhiyun  Time SB+fencembonceonces 0.16
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunThe "Positive: 0 Negative: 2000000" and the "Never 0 2000000" indicate
149*4882a593Smuzhiyunthat during two million trials, the state specified in this litmus
150*4882a593Smuzhiyuntest's "exists" clause was not reached.
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunAnd, as with "herd7", please see "klitmus7 -help" or "herdtools7/doc/"
153*4882a593Smuzhiyunfor more information.  And again, please be aware that this documentation
154*4882a593Smuzhiyunis intended for people who work on the memory model itself, that is,
155*4882a593Smuzhiyunpeople making changes to the tools/memory-model/linux-kernel.* files.
156*4882a593SmuzhiyunIt is not intended for people focusing on writing, understanding, and
157*4882a593Smuzhiyunrunning LKMM litmus tests.
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun====================
161*4882a593SmuzhiyunDESCRIPTION OF FILES
162*4882a593Smuzhiyun====================
163*4882a593Smuzhiyun
164*4882a593SmuzhiyunDocumentation/cheatsheet.txt
165*4882a593Smuzhiyun	Quick-reference guide to the Linux-kernel memory model.
166*4882a593Smuzhiyun
167*4882a593SmuzhiyunDocumentation/explanation.txt
168*4882a593Smuzhiyun	Describes the memory model in detail.
169*4882a593Smuzhiyun
170*4882a593SmuzhiyunDocumentation/litmus-tests.txt
171*4882a593Smuzhiyun	Describes the format, features, capabilities, and limitations
172*4882a593Smuzhiyun	of the litmus tests that LKMM can evaluate.
173*4882a593Smuzhiyun
174*4882a593SmuzhiyunDocumentation/recipes.txt
175*4882a593Smuzhiyun	Lists common memory-ordering patterns.
176*4882a593Smuzhiyun
177*4882a593SmuzhiyunDocumentation/references.txt
178*4882a593Smuzhiyun	Provides background reading.
179*4882a593Smuzhiyun
180*4882a593SmuzhiyunDocumentation/simple.txt
181*4882a593Smuzhiyun	Starting point for someone new to Linux-kernel concurrency.
182*4882a593Smuzhiyun	And also for those needing a reminder of the simpler approaches
183*4882a593Smuzhiyun	to concurrency!
184*4882a593Smuzhiyun
185*4882a593Smuzhiyunlinux-kernel.bell
186*4882a593Smuzhiyun	Categorizes the relevant instructions, including memory
187*4882a593Smuzhiyun	references, memory barriers, atomic read-modify-write operations,
188*4882a593Smuzhiyun	lock acquisition/release, and RCU operations.
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun	More formally, this file (1) lists the subtypes of the various
191*4882a593Smuzhiyun	event types used by the memory model and (2) performs RCU
192*4882a593Smuzhiyun	read-side critical section nesting analysis.
193*4882a593Smuzhiyun
194*4882a593Smuzhiyunlinux-kernel.cat
195*4882a593Smuzhiyun	Specifies what reorderings are forbidden by memory references,
196*4882a593Smuzhiyun	memory barriers, atomic read-modify-write operations, and RCU.
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun	More formally, this file specifies what executions are forbidden
199*4882a593Smuzhiyun	by the memory model.  Allowed executions are those which
200*4882a593Smuzhiyun	satisfy the model's "coherence", "atomic", "happens-before",
201*4882a593Smuzhiyun	"propagation", and "rcu" axioms, which are defined in the file.
202*4882a593Smuzhiyun
203*4882a593Smuzhiyunlinux-kernel.cfg
204*4882a593Smuzhiyun	Convenience file that gathers the common-case herd7 command-line
205*4882a593Smuzhiyun	arguments.
206*4882a593Smuzhiyun
207*4882a593Smuzhiyunlinux-kernel.def
208*4882a593Smuzhiyun	Maps from C-like syntax to herd7's internal litmus-test
209*4882a593Smuzhiyun	instruction-set architecture.
210*4882a593Smuzhiyun
211*4882a593Smuzhiyunlitmus-tests
212*4882a593Smuzhiyun	Directory containing a few representative litmus tests, which
213*4882a593Smuzhiyun	are listed in litmus-tests/README.  A great deal more litmus
214*4882a593Smuzhiyun	tests are available at https://github.com/paulmckrcu/litmus.
215*4882a593Smuzhiyun
216*4882a593Smuzhiyunlock.cat
217*4882a593Smuzhiyun	Provides a front-end analysis of lock acquisition and release,
218*4882a593Smuzhiyun	for example, associating a lock acquisition with the preceding
219*4882a593Smuzhiyun	and following releases and checking for self-deadlock.
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun	More formally, this file defines a performance-enhanced scheme
222*4882a593Smuzhiyun	for generation of the possible reads-from and coherence order
223*4882a593Smuzhiyun	relations on the locking primitives.
224*4882a593Smuzhiyun
225*4882a593SmuzhiyunREADME
226*4882a593Smuzhiyun	This file.
227*4882a593Smuzhiyun
228*4882a593Smuzhiyunscripts	Various scripts, see scripts/README.
229