xref: /openbmc/linux/tools/memory-model/Documentation/litmus-tests.txt (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1984f272bSPaul E. McKenneyLinux-Kernel Memory Model Litmus Tests
2984f272bSPaul E. McKenney======================================
3984f272bSPaul E. McKenney
4984f272bSPaul E. McKenneyThis file describes the LKMM litmus-test format by example, describes
5984f272bSPaul E. McKenneysome tricks and traps, and finally outlines LKMM's limitations.  Earlier
6984f272bSPaul E. McKenneyversions of this material appeared in a number of LWN articles, including:
7984f272bSPaul E. McKenney
8984f272bSPaul E. McKenneyhttps://lwn.net/Articles/720550/
9984f272bSPaul E. McKenney	A formal kernel memory-ordering model (part 2)
10984f272bSPaul E. McKenneyhttps://lwn.net/Articles/608550/
11984f272bSPaul E. McKenney	Axiomatic validation of memory barriers and atomic instructions
12984f272bSPaul E. McKenneyhttps://lwn.net/Articles/470681/
13984f272bSPaul E. McKenney	Validating Memory Barriers and Atomic Instructions
14984f272bSPaul E. McKenney
15984f272bSPaul E. McKenneyThis document presents information in decreasing order of applicability,
16984f272bSPaul E. McKenneyso that, where possible, the information that has proven more commonly
17984f272bSPaul E. McKenneyuseful is shown near the beginning.
18984f272bSPaul E. McKenney
19984f272bSPaul E. McKenneyFor information on installing LKMM, including the underlying "herd7"
20984f272bSPaul E. McKenneytool, please see tools/memory-model/README.
21984f272bSPaul E. McKenney
22984f272bSPaul E. McKenney
23984f272bSPaul E. McKenneyCopy-Pasta
24984f272bSPaul E. McKenney==========
25984f272bSPaul E. McKenney
26984f272bSPaul E. McKenneyAs with other software, it is often better (if less macho) to adapt an
27984f272bSPaul E. McKenneyexisting litmus test than it is to create one from scratch.  A number
28984f272bSPaul E. McKenneyof litmus tests may be found in the kernel source tree:
29984f272bSPaul E. McKenney
30984f272bSPaul E. McKenney	tools/memory-model/litmus-tests/
31984f272bSPaul E. McKenney	Documentation/litmus-tests/
32984f272bSPaul E. McKenney
33984f272bSPaul E. McKenneySeveral thousand more example litmus tests are available on github
34984f272bSPaul E. McKenneyand kernel.org:
35984f272bSPaul E. McKenney
36984f272bSPaul E. McKenney	https://github.com/paulmckrcu/litmus
37984f272bSPaul E. McKenney	https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/perfbook.git/tree/CodeSamples/formal/herd
38984f272bSPaul E. McKenney	https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/perfbook.git/tree/CodeSamples/formal/litmus
39984f272bSPaul E. McKenney
40984f272bSPaul E. McKenneyThe -l and -L arguments to "git grep" can be quite helpful in identifying
41984f272bSPaul E. McKenneyexisting litmus tests that are similar to the one you need.  But even if
42984f272bSPaul E. McKenneyyou start with an existing litmus test, it is still helpful to have a
43984f272bSPaul E. McKenneygood understanding of the litmus-test format.
44984f272bSPaul E. McKenney
45984f272bSPaul E. McKenney
46984f272bSPaul E. McKenneyExamples and Format
47984f272bSPaul E. McKenney===================
48984f272bSPaul E. McKenney
49984f272bSPaul E. McKenneyThis section describes the overall format of litmus tests, starting
50984f272bSPaul E. McKenneywith a small example of the message-passing pattern and moving on to
51984f272bSPaul E. McKenneymore complex examples that illustrate explicit initialization and LKMM's
52984f272bSPaul E. McKenneyminimalistic set of flow-control statements.
53984f272bSPaul E. McKenney
54984f272bSPaul E. McKenney
55984f272bSPaul E. McKenneyMessage-Passing Example
56984f272bSPaul E. McKenney-----------------------
57984f272bSPaul E. McKenney
58984f272bSPaul E. McKenneyThis section gives an overview of the format of a litmus test using an
59984f272bSPaul E. McKenneyexample based on the common message-passing use case.  This use case
60984f272bSPaul E. McKenneyappears often in the Linux kernel.  For example, a flag (modeled by "y"
61984f272bSPaul E. McKenneybelow) indicates that a buffer (modeled by "x" below) is now completely
62984f272bSPaul E. McKenneyfilled in and ready for use.  It would be very bad if the consumer saw the
63984f272bSPaul E. McKenneyflag set, but, due to memory misordering, saw old values in the buffer.
64984f272bSPaul E. McKenney
65984f272bSPaul E. McKenneyThis example asks whether smp_store_release() and smp_load_acquire()
66984f272bSPaul E. McKenneysuffices to avoid this bad outcome:
67984f272bSPaul E. McKenney
68984f272bSPaul E. McKenney 1 C MP+pooncerelease+poacquireonce
69984f272bSPaul E. McKenney 2
70984f272bSPaul E. McKenney 3 {}
71984f272bSPaul E. McKenney 4
72984f272bSPaul E. McKenney 5 P0(int *x, int *y)
73984f272bSPaul E. McKenney 6 {
74984f272bSPaul E. McKenney 7   WRITE_ONCE(*x, 1);
75984f272bSPaul E. McKenney 8   smp_store_release(y, 1);
76984f272bSPaul E. McKenney 9 }
77984f272bSPaul E. McKenney10
78984f272bSPaul E. McKenney11 P1(int *x, int *y)
79984f272bSPaul E. McKenney12 {
80984f272bSPaul E. McKenney13   int r0;
81984f272bSPaul E. McKenney14   int r1;
82984f272bSPaul E. McKenney15
83984f272bSPaul E. McKenney16   r0 = smp_load_acquire(y);
84984f272bSPaul E. McKenney17   r1 = READ_ONCE(*x);
85984f272bSPaul E. McKenney18 }
86984f272bSPaul E. McKenney19
87984f272bSPaul E. McKenney20 exists (1:r0=1 /\ 1:r1=0)
88984f272bSPaul E. McKenney
89984f272bSPaul E. McKenneyLine 1 starts with "C", which identifies this file as being in the
90984f272bSPaul E. McKenneyLKMM C-language format (which, as we will see, is a small fragment
91984f272bSPaul E. McKenneyof the full C language).  The remainder of line 1 is the name of
92984f272bSPaul E. McKenneythe test, which by convention is the filename with the ".litmus"
93984f272bSPaul E. McKenneysuffix stripped.  In this case, the actual test may be found in
94984f272bSPaul E. McKenneytools/memory-model/litmus-tests/MP+pooncerelease+poacquireonce.litmus
95984f272bSPaul E. McKenneyin the Linux-kernel source tree.
96984f272bSPaul E. McKenney
97984f272bSPaul E. McKenneyMechanically generated litmus tests will often have an optional
98984f272bSPaul E. McKenneydouble-quoted comment string on the second line.  Such strings are ignored
99984f272bSPaul E. McKenneywhen running the test.  Yes, you can add your own comments to litmus
100984f272bSPaul E. McKenneytests, but this is a bit involved due to the use of multiple parsers.
101984f272bSPaul E. McKenneyFor now, you can use C-language comments in the C code, and these comments
102984f272bSPaul E. McKenneymay be in either the "/* */" or the "//" style.  A later section will
103984f272bSPaul E. McKenneycover the full litmus-test commenting story.
104984f272bSPaul E. McKenney
105984f272bSPaul E. McKenneyLine 3 is the initialization section.  Because the default initialization
106984f272bSPaul E. McKenneyto zero suffices for this test, the "{}" syntax is used, which mean the
107984f272bSPaul E. McKenneyinitialization section is empty.  Litmus tests requiring non-default
108984f272bSPaul E. McKenneyinitialization must have non-empty initialization sections, as in the
109984f272bSPaul E. McKenneyexample that will be presented later in this document.
110984f272bSPaul E. McKenney
111984f272bSPaul E. McKenneyLines 5-9 show the first process and lines 11-18 the second process.  Each
112984f272bSPaul E. McKenneyprocess corresponds to a Linux-kernel task (or kthread, workqueue, thread,
113984f272bSPaul E. McKenneyand so on; LKMM discussions often use these terms interchangeably).
114984f272bSPaul E. McKenneyThe name of the first process is "P0" and that of the second "P1".
115984f272bSPaul E. McKenneyYou can name your processes anything you like as long as the names consist
116984f272bSPaul E. McKenneyof a single "P" followed by a number, and as long as the numbers are
117984f272bSPaul E. McKenneyconsecutive starting with zero.  This can actually be quite helpful,
118984f272bSPaul E. McKenneyfor example, a .litmus file matching "^P1(" but not matching "^P2("
119984f272bSPaul E. McKenneymust contain a two-process litmus test.
120984f272bSPaul E. McKenney
121984f272bSPaul E. McKenneyThe argument list for each function are pointers to the global variables
122984f272bSPaul E. McKenneyused by that function.  Unlike normal C-language function parameters, the
123984f272bSPaul E. McKenneynames are significant.  The fact that both P0() and P1() have a formal
124984f272bSPaul E. McKenneyparameter named "x" means that these two processes are working with the
125984f272bSPaul E. McKenneysame global variable, also named "x".  So the "int *x, int *y" on P0()
126984f272bSPaul E. McKenneyand P1() mean that both processes are working with two shared global
127984f272bSPaul E. McKenneyvariables, "x" and "y".  Global variables are always passed to processes
128984f272bSPaul E. McKenneyby reference, hence "P0(int *x, int *y)", but *never* "P0(int x, int y)".
129984f272bSPaul E. McKenney
130984f272bSPaul E. McKenneyP0() has no local variables, but P1() has two of them named "r0" and "r1".
131984f272bSPaul E. McKenneyThese names may be freely chosen, but for historical reasons stemming from
132984f272bSPaul E. McKenneyother litmus-test formats, it is conventional to use names consisting of
133984f272bSPaul E. McKenney"r" followed by a number as shown here.  A common bug in litmus tests
134984f272bSPaul E. McKenneyis forgetting to add a global variable to a process's parameter list.
135984f272bSPaul E. McKenneyThis will sometimes result in an error message, but can also cause the
136984f272bSPaul E. McKenneyintended global to instead be silently treated as an undeclared local
137984f272bSPaul E. McKenneyvariable.
138984f272bSPaul E. McKenney
139984f272bSPaul E. McKenneyEach process's code is similar to Linux-kernel C, as can be seen on lines
140984f272bSPaul E. McKenney7-8 and 13-17.  This code may use many of the Linux kernel's atomic
141984f272bSPaul E. McKenneyoperations, some of its exclusive-lock functions, and some of its RCU
142984f272bSPaul E. McKenneyand SRCU functions.  An approximate list of the currently supported
143984f272bSPaul E. McKenneyfunctions may be found in the linux-kernel.def file.
144984f272bSPaul E. McKenney
145984f272bSPaul E. McKenneyThe P0() process does "WRITE_ONCE(*x, 1)" on line 7.  Because "x" is a
146984f272bSPaul E. McKenneypointer in P0()'s parameter list, this does an unordered store to global
147984f272bSPaul E. McKenneyvariable "x".  Line 8 does "smp_store_release(y, 1)", and because "y"
148984f272bSPaul E. McKenneyis also in P0()'s parameter list, this does a release store to global
149984f272bSPaul E. McKenneyvariable "y".
150984f272bSPaul E. McKenney
151984f272bSPaul E. McKenneyThe P1() process declares two local variables on lines 13 and 14.
152984f272bSPaul E. McKenneyLine 16 does "r0 = smp_load_acquire(y)" which does an acquire load
153984f272bSPaul E. McKenneyfrom global variable "y" into local variable "r0".  Line 17 does a
154984f272bSPaul E. McKenney"r1 = READ_ONCE(*x)", which does an unordered load from "*x" into local
155984f272bSPaul E. McKenneyvariable "r1".  Both "x" and "y" are in P1()'s parameter list, so both
156984f272bSPaul E. McKenneyreference the same global variables that are used by P0().
157984f272bSPaul E. McKenney
158984f272bSPaul E. McKenneyLine 20 is the "exists" assertion expression to evaluate the final state.
159984f272bSPaul E. McKenneyThis final state is evaluated after the dust has settled: both processes
160984f272bSPaul E. McKenneyhave completed and all of their memory references and memory barriers
161984f272bSPaul E. McKenneyhave propagated to all parts of the system.  The references to the local
162984f272bSPaul E. McKenneyvariables "r0" and "r1" in line 24 must be prefixed with "1:" to specify
163984f272bSPaul E. McKenneywhich process they are local to.
164984f272bSPaul E. McKenney
165984f272bSPaul E. McKenneyNote that the assertion expression is written in the litmus-test
166984f272bSPaul E. McKenneylanguage rather than in C.  For example, single "=" is an equality
167984f272bSPaul E. McKenneyoperator rather than an assignment.  The "/\" character combination means
168984f272bSPaul E. McKenney"and".  Similarly, "\/" stands for "or".  Both of these are ASCII-art
169984f272bSPaul E. McKenneyrepresentations of the corresponding mathematical symbols.  Finally,
170984f272bSPaul E. McKenney"~" stands for "logical not", which is "!" in C, and not to be confused
171984f272bSPaul E. McKenneywith the C-language "~" operator which instead stands for "bitwise not".
172984f272bSPaul E. McKenneyParentheses may be used to override precedence.
173984f272bSPaul E. McKenney
174984f272bSPaul E. McKenneyThe "exists" assertion on line 20 is satisfied if the consumer sees the
175984f272bSPaul E. McKenneyflag ("y") set but the buffer ("x") as not yet filled in, that is, if P1()
176984f272bSPaul E. McKenneyloaded a value from "x" that was equal to 1 but loaded a value from "y"
177984f272bSPaul E. McKenneythat was still equal to zero.
178984f272bSPaul E. McKenney
179984f272bSPaul E. McKenneyThis example can be checked by running the following command, which
180984f272bSPaul E. McKenneyabsolutely must be run from the tools/memory-model directory and from
181984f272bSPaul E. McKenneythis directory only:
182984f272bSPaul E. McKenney
183984f272bSPaul E. McKenneyherd7 -conf linux-kernel.cfg litmus-tests/MP+pooncerelease+poacquireonce.litmus
184984f272bSPaul E. McKenney
185984f272bSPaul E. McKenneyThe output is the result of something similar to a full state-space
186984f272bSPaul E. McKenneysearch, and is as follows:
187984f272bSPaul E. McKenney
188984f272bSPaul E. McKenney 1 Test MP+pooncerelease+poacquireonce Allowed
189984f272bSPaul E. McKenney 2 States 3
190984f272bSPaul E. McKenney 3 1:r0=0; 1:r1=0;
191984f272bSPaul E. McKenney 4 1:r0=0; 1:r1=1;
192984f272bSPaul E. McKenney 5 1:r0=1; 1:r1=1;
193984f272bSPaul E. McKenney 6 No
194984f272bSPaul E. McKenney 7 Witnesses
195984f272bSPaul E. McKenney 8 Positive: 0 Negative: 3
196984f272bSPaul E. McKenney 9 Condition exists (1:r0=1 /\ 1:r1=0)
197984f272bSPaul E. McKenney10 Observation MP+pooncerelease+poacquireonce Never 0 3
198984f272bSPaul E. McKenney11 Time MP+pooncerelease+poacquireonce 0.00
199984f272bSPaul E. McKenney12 Hash=579aaa14d8c35a39429b02e698241d09
200984f272bSPaul E. McKenney
201984f272bSPaul E. McKenneyThe most pertinent line is line 10, which contains "Never 0 3", which
202984f272bSPaul E. McKenneyindicates that the bad result flagged by the "exists" clause never
203984f272bSPaul E. McKenneyhappens.  This line might instead say "Sometimes" to indicate that the
204984f272bSPaul E. McKenneybad result happened in some but not all executions, or it might say
205984f272bSPaul E. McKenney"Always" to indicate that the bad result happened in all executions.
206984f272bSPaul E. McKenney(The herd7 tool doesn't judge, so it is only an LKMM convention that the
207984f272bSPaul E. McKenney"exists" clause indicates a bad result.  To see this, invert the "exists"
208984f272bSPaul E. McKenneyclause's condition and run the test.)  The numbers ("0 3") at the end
209984f272bSPaul E. McKenneyof this line indicate the number of end states satisfying the "exists"
210984f272bSPaul E. McKenneyclause (0) and the number not not satisfying that clause (3).
211984f272bSPaul E. McKenney
212984f272bSPaul E. McKenneyAnother important part of this output is shown in lines 2-5, repeated here:
213984f272bSPaul E. McKenney
214984f272bSPaul E. McKenney 2 States 3
215984f272bSPaul E. McKenney 3 1:r0=0; 1:r1=0;
216984f272bSPaul E. McKenney 4 1:r0=0; 1:r1=1;
217984f272bSPaul E. McKenney 5 1:r0=1; 1:r1=1;
218984f272bSPaul E. McKenney
219984f272bSPaul E. McKenneyLine 2 gives the total number of end states, and each of lines 3-5 list
220984f272bSPaul E. McKenneyone of these states, with the first ("1:r0=0; 1:r1=0;") indicating that
221984f272bSPaul E. McKenneyboth of P1()'s loads returned the value "0".  As expected, given the
222984f272bSPaul E. McKenney"Never" on line 10, the state flagged by the "exists" clause is not
223984f272bSPaul E. McKenneylisted.  This full list of states can be helpful when debugging a new
224984f272bSPaul E. McKenneylitmus test.
225984f272bSPaul E. McKenney
226984f272bSPaul E. McKenneyThe rest of the output is not normally needed, either due to irrelevance
227984f272bSPaul E. McKenneyor due to being redundant with the lines discussed above.  However, the
228984f272bSPaul E. McKenneyfollowing paragraph lists them for the benefit of readers possessed of
229984f272bSPaul E. McKenneyan insatiable curiosity.  Other readers should feel free to skip ahead.
230984f272bSPaul E. McKenney
231984f272bSPaul E. McKenneyLine 1 echos the test name, along with the "Test" and "Allowed".  Line 6's
232984f272bSPaul E. McKenney"No" says that the "exists" clause was not satisfied by any execution,
233984f272bSPaul E. McKenneyand as such it has the same meaning as line 10's "Never".  Line 7 is a
234984f272bSPaul E. McKenneylead-in to line 8's "Positive: 0 Negative: 3", which lists the number
235984f272bSPaul E. McKenneyof end states satisfying and not satisfying the "exists" clause, just
236984f272bSPaul E. McKenneylike the two numbers at the end of line 10.  Line 9 repeats the "exists"
237984f272bSPaul E. McKenneyclause so that you don't have to look it up in the litmus-test file.
238984f272bSPaul E. McKenneyThe number at the end of line 11 (which begins with "Time") gives the
239984f272bSPaul E. McKenneytime in seconds required to analyze the litmus test.  Small tests such
240984f272bSPaul E. McKenneyas this one complete in a few milliseconds, so "0.00" is quite common.
241984f272bSPaul E. McKenneyLine 12 gives a hash of the contents for the litmus-test file, and is used
242984f272bSPaul E. McKenneyby tooling that manages litmus tests and their output.  This tooling is
243984f272bSPaul E. McKenneyused by people modifying LKMM itself, and among other things lets such
244984f272bSPaul E. McKenneypeople know which of the several thousand relevant litmus tests were
245984f272bSPaul E. McKenneyaffected by a given change to LKMM.
246984f272bSPaul E. McKenney
247984f272bSPaul E. McKenney
248984f272bSPaul E. McKenneyInitialization
249984f272bSPaul E. McKenney--------------
250984f272bSPaul E. McKenney
251984f272bSPaul E. McKenneyThe previous example relied on the default zero initialization for
252984f272bSPaul E. McKenney"x" and "y", but a similar litmus test could instead initialize them
253984f272bSPaul E. McKenneyto some other value:
254984f272bSPaul E. McKenney
255984f272bSPaul E. McKenney 1 C MP+pooncerelease+poacquireonce
256984f272bSPaul E. McKenney 2
257984f272bSPaul E. McKenney 3 {
258984f272bSPaul E. McKenney 4   x=42;
259984f272bSPaul E. McKenney 5   y=42;
260984f272bSPaul E. McKenney 6 }
261984f272bSPaul E. McKenney 7
262984f272bSPaul E. McKenney 8 P0(int *x, int *y)
263984f272bSPaul E. McKenney 9 {
264984f272bSPaul E. McKenney10   WRITE_ONCE(*x, 1);
265984f272bSPaul E. McKenney11   smp_store_release(y, 1);
266984f272bSPaul E. McKenney12 }
267984f272bSPaul E. McKenney13
268984f272bSPaul E. McKenney14 P1(int *x, int *y)
269984f272bSPaul E. McKenney15 {
270984f272bSPaul E. McKenney16   int r0;
271984f272bSPaul E. McKenney17   int r1;
272984f272bSPaul E. McKenney18
273984f272bSPaul E. McKenney19   r0 = smp_load_acquire(y);
274984f272bSPaul E. McKenney20   r1 = READ_ONCE(*x);
275984f272bSPaul E. McKenney21 }
276984f272bSPaul E. McKenney22
277984f272bSPaul E. McKenney23 exists (1:r0=1 /\ 1:r1=42)
278984f272bSPaul E. McKenney
279984f272bSPaul E. McKenneyLines 3-6 now initialize both "x" and "y" to the value 42.  This also
280984f272bSPaul E. McKenneymeans that the "exists" clause on line 23 must change "1:r1=0" to
281984f272bSPaul E. McKenney"1:r1=42".
282984f272bSPaul E. McKenney
283984f272bSPaul E. McKenneyRunning the test gives the same overall result as before, but with the
284984f272bSPaul E. McKenneyvalue 42 appearing in place of the value zero:
285984f272bSPaul E. McKenney
286984f272bSPaul E. McKenney 1 Test MP+pooncerelease+poacquireonce Allowed
287984f272bSPaul E. McKenney 2 States 3
288984f272bSPaul E. McKenney 3 1:r0=1; 1:r1=1;
289984f272bSPaul E. McKenney 4 1:r0=42; 1:r1=1;
290984f272bSPaul E. McKenney 5 1:r0=42; 1:r1=42;
291984f272bSPaul E. McKenney 6 No
292984f272bSPaul E. McKenney 7 Witnesses
293984f272bSPaul E. McKenney 8 Positive: 0 Negative: 3
294984f272bSPaul E. McKenney 9 Condition exists (1:r0=1 /\ 1:r1=42)
295984f272bSPaul E. McKenney10 Observation MP+pooncerelease+poacquireonce Never 0 3
296984f272bSPaul E. McKenney11 Time MP+pooncerelease+poacquireonce 0.02
297984f272bSPaul E. McKenney12 Hash=ab9a9b7940a75a792266be279a980156
298984f272bSPaul E. McKenney
299984f272bSPaul E. McKenneyIt is tempting to avoid the open-coded repetitions of the value "42"
300984f272bSPaul E. McKenneyby defining another global variable "initval=42" and replacing all
301984f272bSPaul E. McKenneyoccurrences of "42" with "initval".  This will not, repeat *not*,
302984f272bSPaul E. McKenneyinitialize "x" and "y" to 42, but instead to the address of "initval"
303984f272bSPaul E. McKenney(try it!).  See the section below on linked lists to learn more about
304984f272bSPaul E. McKenneywhy this approach to initialization can be useful.
305984f272bSPaul E. McKenney
306984f272bSPaul E. McKenney
307984f272bSPaul E. McKenneyControl Structures
308984f272bSPaul E. McKenney------------------
309984f272bSPaul E. McKenney
310984f272bSPaul E. McKenneyLKMM supports the C-language "if" statement, which allows modeling of
311984f272bSPaul E. McKenneyconditional branches.  In LKMM, conditional branches can affect ordering,
312984f272bSPaul E. McKenneybut only if you are *very* careful (compilers are surprisingly able
313984f272bSPaul E. McKenneyto optimize away conditional branches).  The following example shows
314984f272bSPaul E. McKenneythe "load buffering" (LB) use case that is used in the Linux kernel to
315984f272bSPaul E. McKenneysynchronize between ring-buffer producers and consumers.  In the example
316984f272bSPaul E. McKenneybelow, P0() is one side checking to see if an operation may proceed and
317984f272bSPaul E. McKenneyP1() is the other side completing its update.
318984f272bSPaul E. McKenney
319984f272bSPaul E. McKenney 1 C LB+fencembonceonce+ctrlonceonce
320984f272bSPaul E. McKenney 2
321984f272bSPaul E. McKenney 3 {}
322984f272bSPaul E. McKenney 4
323984f272bSPaul E. McKenney 5 P0(int *x, int *y)
324984f272bSPaul E. McKenney 6 {
325984f272bSPaul E. McKenney 7   int r0;
326984f272bSPaul E. McKenney 8
327984f272bSPaul E. McKenney 9   r0 = READ_ONCE(*x);
328984f272bSPaul E. McKenney10   if (r0)
329984f272bSPaul E. McKenney11     WRITE_ONCE(*y, 1);
330984f272bSPaul E. McKenney12 }
331984f272bSPaul E. McKenney13
332984f272bSPaul E. McKenney14 P1(int *x, int *y)
333984f272bSPaul E. McKenney15 {
334984f272bSPaul E. McKenney16   int r0;
335984f272bSPaul E. McKenney17
336984f272bSPaul E. McKenney18   r0 = READ_ONCE(*y);
337984f272bSPaul E. McKenney19   smp_mb();
338984f272bSPaul E. McKenney20   WRITE_ONCE(*x, 1);
339984f272bSPaul E. McKenney21 }
340984f272bSPaul E. McKenney22
341984f272bSPaul E. McKenney23 exists (0:r0=1 /\ 1:r0=1)
342984f272bSPaul E. McKenney
343984f272bSPaul E. McKenneyP1()'s "if" statement on line 10 works as expected, so that line 11 is
344984f272bSPaul E. McKenneyexecuted only if line 9 loads a non-zero value from "x".  Because P1()'s
345984f272bSPaul E. McKenneywrite of "1" to "x" happens only after P1()'s read from "y", one would
346984f272bSPaul E. McKenneyhope that the "exists" clause cannot be satisfied.  LKMM agrees:
347984f272bSPaul E. McKenney
348984f272bSPaul E. McKenney 1 Test LB+fencembonceonce+ctrlonceonce Allowed
349984f272bSPaul E. McKenney 2 States 2
350984f272bSPaul E. McKenney 3 0:r0=0; 1:r0=0;
351984f272bSPaul E. McKenney 4 0:r0=1; 1:r0=0;
352984f272bSPaul E. McKenney 5 No
353984f272bSPaul E. McKenney 6 Witnesses
354984f272bSPaul E. McKenney 7 Positive: 0 Negative: 2
355984f272bSPaul E. McKenney 8 Condition exists (0:r0=1 /\ 1:r0=1)
356984f272bSPaul E. McKenney 9 Observation LB+fencembonceonce+ctrlonceonce Never 0 2
357984f272bSPaul E. McKenney10 Time LB+fencembonceonce+ctrlonceonce 0.00
358984f272bSPaul E. McKenney11 Hash=e5260556f6de495fd39b556d1b831c3b
359984f272bSPaul E. McKenney
360984f272bSPaul E. McKenneyHowever, there is no "while" statement due to the fact that full
361984f272bSPaul E. McKenneystate-space search has some difficulty with iteration.  However, there
362984f272bSPaul E. McKenneyare tricks that may be used to handle some special cases, which are
363984f272bSPaul E. McKenneydiscussed below.  In addition, loop-unrolling tricks may be applied,
364984f272bSPaul E. McKenneyalbeit sparingly.
365984f272bSPaul E. McKenney
366984f272bSPaul E. McKenney
367984f272bSPaul E. McKenneyTricks and Traps
368984f272bSPaul E. McKenney================
369984f272bSPaul E. McKenney
370984f272bSPaul E. McKenneyThis section covers extracting debug output from herd7, emulating
371984f272bSPaul E. McKenneyspin loops, handling trivial linked lists, adding comments to litmus tests,
372984f272bSPaul E. McKenneyemulating call_rcu(), and finally tricks to improve herd7 performance
373984f272bSPaul E. McKenneyin order to better handle large litmus tests.
374984f272bSPaul E. McKenney
375984f272bSPaul E. McKenney
376984f272bSPaul E. McKenneyDebug Output
377984f272bSPaul E. McKenney------------
378984f272bSPaul E. McKenney
379984f272bSPaul E. McKenneyBy default, the herd7 state output includes all variables mentioned
380984f272bSPaul E. McKenneyin the "exists" clause.  But sometimes debugging efforts are greatly
381984f272bSPaul E. McKenneyaided by the values of other variables.  Consider this litmus test
382984f272bSPaul E. McKenney(tools/memory-order/litmus-tests/SB+rfionceonce-poonceonces.litmus but
383984f272bSPaul E. McKenneyslightly modified), which probes an obscure corner of hardware memory
384984f272bSPaul E. McKenneyordering:
385984f272bSPaul E. McKenney
386984f272bSPaul E. McKenney 1 C SB+rfionceonce-poonceonces
387984f272bSPaul E. McKenney 2
388984f272bSPaul E. McKenney 3 {}
389984f272bSPaul E. McKenney 4
390984f272bSPaul E. McKenney 5 P0(int *x, int *y)
391984f272bSPaul E. McKenney 6 {
392984f272bSPaul E. McKenney 7   int r1;
393984f272bSPaul E. McKenney 8   int r2;
394984f272bSPaul E. McKenney 9
395984f272bSPaul E. McKenney10   WRITE_ONCE(*x, 1);
396984f272bSPaul E. McKenney11   r1 = READ_ONCE(*x);
397984f272bSPaul E. McKenney12   r2 = READ_ONCE(*y);
398984f272bSPaul E. McKenney13 }
399984f272bSPaul E. McKenney14
400984f272bSPaul E. McKenney15 P1(int *x, int *y)
401984f272bSPaul E. McKenney16 {
402984f272bSPaul E. McKenney17   int r3;
403984f272bSPaul E. McKenney18   int r4;
404984f272bSPaul E. McKenney19
405984f272bSPaul E. McKenney20   WRITE_ONCE(*y, 1);
406984f272bSPaul E. McKenney21   r3 = READ_ONCE(*y);
407984f272bSPaul E. McKenney22   r4 = READ_ONCE(*x);
408984f272bSPaul E. McKenney23 }
409984f272bSPaul E. McKenney24
410984f272bSPaul E. McKenney25 exists (0:r2=0 /\ 1:r4=0)
411984f272bSPaul E. McKenney
412984f272bSPaul E. McKenneyThe herd7 output is as follows:
413984f272bSPaul E. McKenney
414984f272bSPaul E. McKenney 1 Test SB+rfionceonce-poonceonces Allowed
415984f272bSPaul E. McKenney 2 States 4
416984f272bSPaul E. McKenney 3 0:r2=0; 1:r4=0;
417984f272bSPaul E. McKenney 4 0:r2=0; 1:r4=1;
418984f272bSPaul E. McKenney 5 0:r2=1; 1:r4=0;
419984f272bSPaul E. McKenney 6 0:r2=1; 1:r4=1;
420984f272bSPaul E. McKenney 7 Ok
421984f272bSPaul E. McKenney 8 Witnesses
422984f272bSPaul E. McKenney 9 Positive: 1 Negative: 3
423984f272bSPaul E. McKenney10 Condition exists (0:r2=0 /\ 1:r4=0)
424984f272bSPaul E. McKenney11 Observation SB+rfionceonce-poonceonces Sometimes 1 3
425984f272bSPaul E. McKenney12 Time SB+rfionceonce-poonceonces 0.01
426984f272bSPaul E. McKenney13 Hash=c7f30fe0faebb7d565405d55b7318ada
427984f272bSPaul E. McKenney
428984f272bSPaul E. McKenney(This output indicates that CPUs are permitted to "snoop their own
429984f272bSPaul E. McKenneystore buffers", which all of Linux's CPU families other than s390 will
430984f272bSPaul E. McKenneyhappily do.  Such snooping results in disagreement among CPUs on the
431984f272bSPaul E. McKenneyorder of stores from different CPUs, which is rarely an issue.)
432984f272bSPaul E. McKenney
433984f272bSPaul E. McKenneyBut the herd7 output shows only the two variables mentioned in the
434984f272bSPaul E. McKenney"exists" clause.  Someone modifying this test might wish to know the
435984f272bSPaul E. McKenneyvalues of "x", "y", "0:r1", and "0:r3" as well.  The "locations"
436984f272bSPaul E. McKenneystatement on line 25 shows how to cause herd7 to display additional
437984f272bSPaul E. McKenneyvariables:
438984f272bSPaul E. McKenney
439984f272bSPaul E. McKenney 1 C SB+rfionceonce-poonceonces
440984f272bSPaul E. McKenney 2
441984f272bSPaul E. McKenney 3 {}
442984f272bSPaul E. McKenney 4
443984f272bSPaul E. McKenney 5 P0(int *x, int *y)
444984f272bSPaul E. McKenney 6 {
445984f272bSPaul E. McKenney 7   int r1;
446984f272bSPaul E. McKenney 8   int r2;
447984f272bSPaul E. McKenney 9
448984f272bSPaul E. McKenney10   WRITE_ONCE(*x, 1);
449984f272bSPaul E. McKenney11   r1 = READ_ONCE(*x);
450984f272bSPaul E. McKenney12   r2 = READ_ONCE(*y);
451984f272bSPaul E. McKenney13 }
452984f272bSPaul E. McKenney14
453984f272bSPaul E. McKenney15 P1(int *x, int *y)
454984f272bSPaul E. McKenney16 {
455984f272bSPaul E. McKenney17   int r3;
456984f272bSPaul E. McKenney18   int r4;
457984f272bSPaul E. McKenney19
458984f272bSPaul E. McKenney20   WRITE_ONCE(*y, 1);
459984f272bSPaul E. McKenney21   r3 = READ_ONCE(*y);
460984f272bSPaul E. McKenney22   r4 = READ_ONCE(*x);
461984f272bSPaul E. McKenney23 }
462984f272bSPaul E. McKenney24
463984f272bSPaul E. McKenney25 locations [0:r1; 1:r3; x; y]
464984f272bSPaul E. McKenney26 exists (0:r2=0 /\ 1:r4=0)
465984f272bSPaul E. McKenney
466984f272bSPaul E. McKenneyThe herd7 output then displays the values of all the variables:
467984f272bSPaul E. McKenney
468984f272bSPaul E. McKenney 1 Test SB+rfionceonce-poonceonces Allowed
469984f272bSPaul E. McKenney 2 States 4
470984f272bSPaul E. McKenney 3 0:r1=1; 0:r2=0; 1:r3=1; 1:r4=0; x=1; y=1;
471984f272bSPaul E. McKenney 4 0:r1=1; 0:r2=0; 1:r3=1; 1:r4=1; x=1; y=1;
472984f272bSPaul E. McKenney 5 0:r1=1; 0:r2=1; 1:r3=1; 1:r4=0; x=1; y=1;
473984f272bSPaul E. McKenney 6 0:r1=1; 0:r2=1; 1:r3=1; 1:r4=1; x=1; y=1;
474984f272bSPaul E. McKenney 7 Ok
475984f272bSPaul E. McKenney 8 Witnesses
476984f272bSPaul E. McKenney 9 Positive: 1 Negative: 3
477984f272bSPaul E. McKenney10 Condition exists (0:r2=0 /\ 1:r4=0)
478984f272bSPaul E. McKenney11 Observation SB+rfionceonce-poonceonces Sometimes 1 3
479984f272bSPaul E. McKenney12 Time SB+rfionceonce-poonceonces 0.01
480984f272bSPaul E. McKenney13 Hash=40de8418c4b395388f6501cafd1ed38d
481984f272bSPaul E. McKenney
482984f272bSPaul E. McKenneyWhat if you would like to know the value of a particular global variable
483984f272bSPaul E. McKenneyat some particular point in a given process's execution?  One approach
484984f272bSPaul E. McKenneyis to use a READ_ONCE() to load that global variable into a new local
485984f272bSPaul E. McKenneyvariable, then add that local variable to the "locations" clause.
486984f272bSPaul E. McKenneyBut be careful:  In some litmus tests, adding a READ_ONCE() will change
487984f272bSPaul E. McKenneythe outcome!  For one example, please see the C-READ_ONCE.litmus and
488984f272bSPaul E. McKenneyC-READ_ONCE-omitted.litmus tests located here:
489984f272bSPaul E. McKenney
490984f272bSPaul E. McKenney	https://github.com/paulmckrcu/litmus/blob/master/manual/kernel/
491984f272bSPaul E. McKenney
492984f272bSPaul E. McKenney
493984f272bSPaul E. McKenneySpin Loops
494984f272bSPaul E. McKenney----------
495984f272bSPaul E. McKenney
496984f272bSPaul E. McKenneyThe analysis carried out by herd7 explores full state space, which is
497984f272bSPaul E. McKenneyat best of exponential time complexity.  Adding processes and increasing
498984f272bSPaul E. McKenneythe amount of code in a give process can greatly increase execution time.
499984f272bSPaul E. McKenneyPotentially infinite loops, such as those used to wait for locks to
500984f272bSPaul E. McKenneybecome available, are clearly problematic.
501984f272bSPaul E. McKenney
502984f272bSPaul E. McKenneyFortunately, it is possible to avoid state-space explosion by specially
503984f272bSPaul E. McKenneymodeling such loops.  For example, the following litmus tests emulates
504984f272bSPaul E. McKenneylocking using xchg_acquire(), but instead of enclosing xchg_acquire()
505984f272bSPaul E. McKenneyin a spin loop, it instead excludes executions that fail to acquire the
506984f272bSPaul E. McKenneylock using a herd7 "filter" clause.  Note that for exclusive locking, you
507984f272bSPaul E. McKenneyare better off using the spin_lock() and spin_unlock() that LKMM directly
508984f272bSPaul E. McKenneymodels, if for no other reason that these are much faster.  However, the
509984f272bSPaul E. McKenneytechniques illustrated in this section can be used for other purposes,
510984f272bSPaul E. McKenneysuch as emulating reader-writer locking, which LKMM does not yet model.
511984f272bSPaul E. McKenney
512984f272bSPaul E. McKenney 1 C C-SB+l-o-o-u+l-o-o-u-X
513984f272bSPaul E. McKenney 2
514984f272bSPaul E. McKenney 3 {
515984f272bSPaul E. McKenney 4 }
516984f272bSPaul E. McKenney 5
517984f272bSPaul E. McKenney 6 P0(int *sl, int *x0, int *x1)
518984f272bSPaul E. McKenney 7 {
519984f272bSPaul E. McKenney 8   int r2;
520984f272bSPaul E. McKenney 9   int r1;
521984f272bSPaul E. McKenney10
522984f272bSPaul E. McKenney11   r2 = xchg_acquire(sl, 1);
523984f272bSPaul E. McKenney12   WRITE_ONCE(*x0, 1);
524984f272bSPaul E. McKenney13   r1 = READ_ONCE(*x1);
525984f272bSPaul E. McKenney14   smp_store_release(sl, 0);
526984f272bSPaul E. McKenney15 }
527984f272bSPaul E. McKenney16
528984f272bSPaul E. McKenney17 P1(int *sl, int *x0, int *x1)
529984f272bSPaul E. McKenney18 {
530984f272bSPaul E. McKenney19   int r2;
531984f272bSPaul E. McKenney20   int r1;
532984f272bSPaul E. McKenney21
533984f272bSPaul E. McKenney22   r2 = xchg_acquire(sl, 1);
534984f272bSPaul E. McKenney23   WRITE_ONCE(*x1, 1);
535984f272bSPaul E. McKenney24   r1 = READ_ONCE(*x0);
536984f272bSPaul E. McKenney25   smp_store_release(sl, 0);
537984f272bSPaul E. McKenney26 }
538984f272bSPaul E. McKenney27
539984f272bSPaul E. McKenney28 filter (0:r2=0 /\ 1:r2=0)
540984f272bSPaul E. McKenney29 exists (0:r1=0 /\ 1:r1=0)
541984f272bSPaul E. McKenney
542984f272bSPaul E. McKenneyThis litmus test may be found here:
543984f272bSPaul E. McKenney
544984f272bSPaul E. McKenneyhttps://git.kernel.org/pub/scm/linux/kernel/git/paulmck/perfbook.git/tree/CodeSamples/formal/herd/C-SB+l-o-o-u+l-o-o-u-X.litmus
545984f272bSPaul E. McKenney
546984f272bSPaul E. McKenneyThis test uses two global variables, "x1" and "x2", and also emulates a
547984f272bSPaul E. McKenneysingle global spinlock named "sl".  This spinlock is held by whichever
548984f272bSPaul E. McKenneyprocess changes the value of "sl" from "0" to "1", and is released when
549984f272bSPaul E. McKenneythat process sets "sl" back to "0".  P0()'s lock acquisition is emulated
550984f272bSPaul E. McKenneyon line 11 using xchg_acquire(), which unconditionally stores the value
551984f272bSPaul E. McKenney"1" to "sl" and stores either "0" or "1" to "r2", depending on whether
552984f272bSPaul E. McKenneythe lock acquisition was successful or unsuccessful (due to "sl" already
553984f272bSPaul E. McKenneyhaving the value "1"), respectively.  P1() operates in a similar manner.
554984f272bSPaul E. McKenney
555984f272bSPaul E. McKenneyRather unconventionally, execution appears to proceed to the critical
556984f272bSPaul E. McKenneysection on lines 12 and 13 in either case.  Line 14 then uses an
557984f272bSPaul E. McKenneysmp_store_release() to store zero to "sl", thus emulating lock release.
558984f272bSPaul E. McKenney
559984f272bSPaul E. McKenneyThe case where xchg_acquire() fails to acquire the lock is handled by
560984f272bSPaul E. McKenneythe "filter" clause on line 28, which tells herd7 to keep only those
561984f272bSPaul E. McKenneyexecutions in which both "0:r2" and "1:r2" are zero, that is to pay
562984f272bSPaul E. McKenneyattention only to those executions in which both locks are actually
563984f272bSPaul E. McKenneyacquired.  Thus, the bogus executions that would execute the critical
564984f272bSPaul E. McKenneysections are discarded and any effects that they might have had are
565984f272bSPaul E. McKenneyignored.  Note well that the "filter" clause keeps those executions
566984f272bSPaul E. McKenneyfor which its expression is satisfied, that is, for which the expression
567984f272bSPaul E. McKenneyevaluates to true.  In other words, the "filter" clause says what to
568984f272bSPaul E. McKenneykeep, not what to discard.
569984f272bSPaul E. McKenney
570984f272bSPaul E. McKenneyThe result of running this test is as follows:
571984f272bSPaul E. McKenney
572984f272bSPaul E. McKenney 1 Test C-SB+l-o-o-u+l-o-o-u-X Allowed
573984f272bSPaul E. McKenney 2 States 2
574984f272bSPaul E. McKenney 3 0:r1=0; 1:r1=1;
575984f272bSPaul E. McKenney 4 0:r1=1; 1:r1=0;
576984f272bSPaul E. McKenney 5 No
577984f272bSPaul E. McKenney 6 Witnesses
578984f272bSPaul E. McKenney 7 Positive: 0 Negative: 2
579984f272bSPaul E. McKenney 8 Condition exists (0:r1=0 /\ 1:r1=0)
580984f272bSPaul E. McKenney 9 Observation C-SB+l-o-o-u+l-o-o-u-X Never 0 2
581984f272bSPaul E. McKenney10 Time C-SB+l-o-o-u+l-o-o-u-X 0.03
582984f272bSPaul E. McKenney
583984f272bSPaul E. McKenneyThe "Never" on line 9 indicates that this use of xchg_acquire() and
584984f272bSPaul E. McKenneysmp_store_release() really does correctly emulate locking.
585984f272bSPaul E. McKenney
586984f272bSPaul E. McKenneyWhy doesn't the litmus test take the simpler approach of using a spin loop
587984f272bSPaul E. McKenneyto handle failed spinlock acquisitions, like the kernel does?  The key
588984f272bSPaul E. McKenneyinsight behind this litmus test is that spin loops have no effect on the
589984f272bSPaul E. McKenneypossible "exists"-clause outcomes of program execution in the absence
590984f272bSPaul E. McKenneyof deadlock.  In other words, given a high-quality lock-acquisition
591984f272bSPaul E. McKenneyprimitive in a deadlock-free program running on high-quality hardware,
592984f272bSPaul E. McKenneyeach lock acquisition will eventually succeed.  Because herd7 already
593984f272bSPaul E. McKenneyexplores the full state space, the length of time required to actually
594984f272bSPaul E. McKenneyacquire the lock does not matter.  After all, herd7 already models all
595984f272bSPaul E. McKenneypossible durations of the xchg_acquire() statements.
596984f272bSPaul E. McKenney
597984f272bSPaul E. McKenneyWhy not just add the "filter" clause to the "exists" clause, thus
598984f272bSPaul E. McKenneyavoiding the "filter" clause entirely?  This does work, but is slower.
599984f272bSPaul E. McKenneyThe reason that the "filter" clause is faster is that (in the common case)
600984f272bSPaul E. McKenneyherd7 knows to abandon an execution as soon as the "filter" expression
601984f272bSPaul E. McKenneyfails to be satisfied.  In contrast, the "exists" clause is evaluated
602984f272bSPaul E. McKenneyonly at the end of time, thus requiring herd7 to waste time on bogus
603984f272bSPaul E. McKenneyexecutions in which both critical sections proceed concurrently.  In
604984f272bSPaul E. McKenneyaddition, some LKMM users like the separation of concerns provided by
605984f272bSPaul E. McKenneyusing the both the "filter" and "exists" clauses.
606984f272bSPaul E. McKenney
607984f272bSPaul E. McKenneyReaders lacking a pathological interest in odd corner cases should feel
608984f272bSPaul E. McKenneyfree to skip the remainder of this section.
609984f272bSPaul E. McKenney
610984f272bSPaul E. McKenneyBut what if the litmus test were to temporarily set "0:r2" to a non-zero
611984f272bSPaul E. McKenneyvalue?  Wouldn't that cause herd7 to abandon the execution prematurely
612984f272bSPaul E. McKenneydue to an early mismatch of the "filter" clause?
613984f272bSPaul E. McKenney
614984f272bSPaul E. McKenneyWhy not just try it?  Line 4 of the following modified litmus test
615984f272bSPaul E. McKenneyintroduces a new global variable "x2" that is initialized to "1".  Line 23
616984f272bSPaul E. McKenneyof P1() reads that variable into "1:r2" to force an early mismatch with
617984f272bSPaul E. McKenneythe "filter" clause.  Line 24 does a known-true "if" condition to avoid
618984f272bSPaul E. McKenneyand static analysis that herd7 might do.  Finally the "exists" clause
619984f272bSPaul E. McKenneyon line 32 is updated to a condition that is alway satisfied at the end
620984f272bSPaul E. McKenneyof the test.
621984f272bSPaul E. McKenney
622984f272bSPaul E. McKenney 1 C C-SB+l-o-o-u+l-o-o-u-X
623984f272bSPaul E. McKenney 2
624984f272bSPaul E. McKenney 3 {
625984f272bSPaul E. McKenney 4   x2=1;
626984f272bSPaul E. McKenney 5 }
627984f272bSPaul E. McKenney 6
628984f272bSPaul E. McKenney 7 P0(int *sl, int *x0, int *x1)
629984f272bSPaul E. McKenney 8 {
630984f272bSPaul E. McKenney 9   int r2;
631984f272bSPaul E. McKenney10   int r1;
632984f272bSPaul E. McKenney11
633984f272bSPaul E. McKenney12   r2 = xchg_acquire(sl, 1);
634984f272bSPaul E. McKenney13   WRITE_ONCE(*x0, 1);
635984f272bSPaul E. McKenney14   r1 = READ_ONCE(*x1);
636984f272bSPaul E. McKenney15   smp_store_release(sl, 0);
637984f272bSPaul E. McKenney16 }
638984f272bSPaul E. McKenney17
639984f272bSPaul E. McKenney18 P1(int *sl, int *x0, int *x1, int *x2)
640984f272bSPaul E. McKenney19 {
641984f272bSPaul E. McKenney20   int r2;
642984f272bSPaul E. McKenney21   int r1;
643984f272bSPaul E. McKenney22
644984f272bSPaul E. McKenney23   r2 = READ_ONCE(*x2);
645984f272bSPaul E. McKenney24   if (r2)
646984f272bSPaul E. McKenney25     r2 = xchg_acquire(sl, 1);
647984f272bSPaul E. McKenney26   WRITE_ONCE(*x1, 1);
648984f272bSPaul E. McKenney27   r1 = READ_ONCE(*x0);
649984f272bSPaul E. McKenney28   smp_store_release(sl, 0);
650984f272bSPaul E. McKenney29 }
651984f272bSPaul E. McKenney30
652984f272bSPaul E. McKenney31 filter (0:r2=0 /\ 1:r2=0)
653984f272bSPaul E. McKenney32 exists (x1=1)
654984f272bSPaul E. McKenney
655984f272bSPaul E. McKenneyIf the "filter" clause were to check each variable at each point in the
656984f272bSPaul E. McKenneyexecution, running this litmus test would display no executions because
657984f272bSPaul E. McKenneyall executions would be filtered out at line 23.  However, the output
658984f272bSPaul E. McKenneyis instead as follows:
659984f272bSPaul E. McKenney
660984f272bSPaul E. McKenney 1 Test C-SB+l-o-o-u+l-o-o-u-X Allowed
661984f272bSPaul E. McKenney 2 States 1
662984f272bSPaul E. McKenney 3 x1=1;
663984f272bSPaul E. McKenney 4 Ok
664984f272bSPaul E. McKenney 5 Witnesses
665984f272bSPaul E. McKenney 6 Positive: 2 Negative: 0
666984f272bSPaul E. McKenney 7 Condition exists (x1=1)
667984f272bSPaul E. McKenney 8 Observation C-SB+l-o-o-u+l-o-o-u-X Always 2 0
668984f272bSPaul E. McKenney 9 Time C-SB+l-o-o-u+l-o-o-u-X 0.04
669984f272bSPaul E. McKenney10 Hash=080bc508da7f291e122c6de76c0088e3
670984f272bSPaul E. McKenney
671984f272bSPaul E. McKenneyLine 3 shows that there is one execution that did not get filtered out,
672984f272bSPaul E. McKenneyso the "filter" clause is evaluated only on the last assignment to
673984f272bSPaul E. McKenneythe variables that it checks.  In this case, the "filter" clause is a
674984f272bSPaul E. McKenneydisjunction, so it might be evaluated twice, once at the final (and only)
675984f272bSPaul E. McKenneyassignment to "0:r2" and once at the final assignment to "1:r2".
676984f272bSPaul E. McKenney
677984f272bSPaul E. McKenney
678984f272bSPaul E. McKenneyLinked Lists
679984f272bSPaul E. McKenney------------
680984f272bSPaul E. McKenney
681984f272bSPaul E. McKenneyLKMM can handle linked lists, but only linked lists in which each node
682984f272bSPaul E. McKenneycontains nothing except a pointer to the next node in the list.  This is
683984f272bSPaul E. McKenneyof course quite restrictive, but there is nevertheless quite a bit that
684984f272bSPaul E. McKenneycan be done within these confines, as can be seen in the litmus test
685984f272bSPaul E. McKenneyat tools/memory-model/litmus-tests/MP+onceassign+derefonce.litmus:
686984f272bSPaul E. McKenney
687984f272bSPaul E. McKenney 1 C MP+onceassign+derefonce
688984f272bSPaul E. McKenney 2
689984f272bSPaul E. McKenney 3 {
690984f272bSPaul E. McKenney 4 y=z;
691984f272bSPaul E. McKenney 5 z=0;
692984f272bSPaul E. McKenney 6 }
693984f272bSPaul E. McKenney 7
694984f272bSPaul E. McKenney 8 P0(int *x, int **y)
695984f272bSPaul E. McKenney 9 {
696984f272bSPaul E. McKenney10   WRITE_ONCE(*x, 1);
697984f272bSPaul E. McKenney11   rcu_assign_pointer(*y, x);
698984f272bSPaul E. McKenney12 }
699984f272bSPaul E. McKenney13
700984f272bSPaul E. McKenney14 P1(int *x, int **y)
701984f272bSPaul E. McKenney15 {
702984f272bSPaul E. McKenney16   int *r0;
703984f272bSPaul E. McKenney17   int r1;
704984f272bSPaul E. McKenney18
705984f272bSPaul E. McKenney19   rcu_read_lock();
706984f272bSPaul E. McKenney20   r0 = rcu_dereference(*y);
707984f272bSPaul E. McKenney21   r1 = READ_ONCE(*r0);
708984f272bSPaul E. McKenney22   rcu_read_unlock();
709984f272bSPaul E. McKenney23 }
710984f272bSPaul E. McKenney24
711984f272bSPaul E. McKenney25 exists (1:r0=x /\ 1:r1=0)
712984f272bSPaul E. McKenney
713984f272bSPaul E. McKenneyLine 4's "y=z" may seem odd, given that "z" has not yet been initialized.
714984f272bSPaul E. McKenneyBut "y=z" does not set the value of "y" to that of "z", but instead
715984f272bSPaul E. McKenneysets the value of "y" to the *address* of "z".  Lines 4 and 5 therefore
716984f272bSPaul E. McKenneycreate a simple linked list, with "y" pointing to "z" and "z" having a
717984f272bSPaul E. McKenneyNULL pointer.  A much longer linked list could be created if desired,
718984f272bSPaul E. McKenneyand circular singly linked lists can also be created and manipulated.
719984f272bSPaul E. McKenney
720984f272bSPaul E. McKenneyThe "exists" clause works the same way, with the "1:r0=x" comparing P1()'s
721984f272bSPaul E. McKenney"r0" not to the value of "x", but again to its address.  This term of the
722984f272bSPaul E. McKenney"exists" clause therefore tests whether line 20's load from "y" saw the
723984f272bSPaul E. McKenneyvalue stored by line 11, which is in fact what is required in this case.
724984f272bSPaul E. McKenney
725984f272bSPaul E. McKenneyP0()'s line 10 initializes "x" to the value 1 then line 11 links to "x"
726984f272bSPaul E. McKenneyfrom "y", replacing "z".
727984f272bSPaul E. McKenney
728984f272bSPaul E. McKenneyP1()'s line 20 loads a pointer from "y", and line 21 dereferences that
7290b8c06b7SPaul E. McKenneypointer.  The RCU read-side critical section spanning lines 19-22 is just
7300b8c06b7SPaul E. McKenneyfor show in this example.  Note that the address used for line 21's load
7310b8c06b7SPaul E. McKenneydepends on (in this case, "is exactly the same as") the value loaded by
7320b8c06b7SPaul E. McKenneyline 20.  This is an example of what is called an "address dependency".
7330b8c06b7SPaul E. McKenneyThis particular address dependency extends from the load on line 20 to the
7340b8c06b7SPaul E. McKenneyload on line 21.  Address dependencies provide a weak form of ordering.
735984f272bSPaul E. McKenney
736984f272bSPaul E. McKenneyRunning this test results in the following:
737984f272bSPaul E. McKenney
738984f272bSPaul E. McKenney 1 Test MP+onceassign+derefonce Allowed
739984f272bSPaul E. McKenney 2 States 2
740984f272bSPaul E. McKenney 3 1:r0=x; 1:r1=1;
741984f272bSPaul E. McKenney 4 1:r0=z; 1:r1=0;
742984f272bSPaul E. McKenney 5 No
743984f272bSPaul E. McKenney 6 Witnesses
744984f272bSPaul E. McKenney 7 Positive: 0 Negative: 2
745984f272bSPaul E. McKenney 8 Condition exists (1:r0=x /\ 1:r1=0)
746984f272bSPaul E. McKenney 9 Observation MP+onceassign+derefonce Never 0 2
747984f272bSPaul E. McKenney10 Time MP+onceassign+derefonce 0.00
748984f272bSPaul E. McKenney11 Hash=49ef7a741563570102448a256a0c8568
749984f272bSPaul E. McKenney
750984f272bSPaul E. McKenneyThe only possible outcomes feature P1() loading a pointer to "z"
751984f272bSPaul E. McKenney(which contains zero) on the one hand and P1() loading a pointer to "x"
752984f272bSPaul E. McKenney(which contains the value one) on the other.  This should be reassuring
753984f272bSPaul E. McKenneybecause it says that RCU readers cannot see the old preinitialization
754984f272bSPaul E. McKenneyvalues when accessing a newly inserted list node.  This undesirable
755984f272bSPaul E. McKenneyscenario is flagged by the "exists" clause, and would occur if P1()
756984f272bSPaul E. McKenneyloaded a pointer to "x", but obtained the pre-initialization value of
757984f272bSPaul E. McKenneyzero after dereferencing that pointer.
758984f272bSPaul E. McKenney
759984f272bSPaul E. McKenney
760984f272bSPaul E. McKenneyComments
761984f272bSPaul E. McKenney--------
762984f272bSPaul E. McKenney
763984f272bSPaul E. McKenneyDifferent portions of a litmus test are processed by different parsers,
764984f272bSPaul E. McKenneywhich has the charming effect of requiring different comment syntax in
765984f272bSPaul E. McKenneydifferent portions of the litmus test.  The C-syntax portions use
766984f272bSPaul E. McKenneyC-language comments (either "/* */" or "//"), while the other portions
767984f272bSPaul E. McKenneyuse Ocaml comments "(* *)".
768984f272bSPaul E. McKenney
769984f272bSPaul E. McKenneyThe following litmus test illustrates the comment style corresponding
770984f272bSPaul E. McKenneyto each syntactic unit of the test:
771984f272bSPaul E. McKenney
772984f272bSPaul E. McKenney 1 C MP+onceassign+derefonce (* A *)
773984f272bSPaul E. McKenney 2
774984f272bSPaul E. McKenney 3 (* B *)
775984f272bSPaul E. McKenney 4
776984f272bSPaul E. McKenney 5 {
777984f272bSPaul E. McKenney 6 y=z; (* C *)
778984f272bSPaul E. McKenney 7 z=0;
779984f272bSPaul E. McKenney 8 } // D
780984f272bSPaul E. McKenney 9
781984f272bSPaul E. McKenney10 // E
782984f272bSPaul E. McKenney11
783984f272bSPaul E. McKenney12 P0(int *x, int **y) // F
784984f272bSPaul E. McKenney13 {
785984f272bSPaul E. McKenney14   WRITE_ONCE(*x, 1);  // G
786984f272bSPaul E. McKenney15   rcu_assign_pointer(*y, x);
787984f272bSPaul E. McKenney16 }
788984f272bSPaul E. McKenney17
789984f272bSPaul E. McKenney18 // H
790984f272bSPaul E. McKenney19
791984f272bSPaul E. McKenney20 P1(int *x, int **y)
792984f272bSPaul E. McKenney21 {
793984f272bSPaul E. McKenney22   int *r0;
794984f272bSPaul E. McKenney23   int r1;
795984f272bSPaul E. McKenney24
796984f272bSPaul E. McKenney25   rcu_read_lock();
797984f272bSPaul E. McKenney26   r0 = rcu_dereference(*y);
798984f272bSPaul E. McKenney27   r1 = READ_ONCE(*r0);
799984f272bSPaul E. McKenney28   rcu_read_unlock();
800984f272bSPaul E. McKenney29 }
801984f272bSPaul E. McKenney30
802984f272bSPaul E. McKenney31 // I
803984f272bSPaul E. McKenney32
804984f272bSPaul E. McKenney33 exists (* J *) (1:r0=x /\ (* K *) 1:r1=0) (* L *)
805984f272bSPaul E. McKenney
806984f272bSPaul E. McKenneyIn short, use C-language comments in the C code and Ocaml comments in
807984f272bSPaul E. McKenneythe rest of the litmus test.
808984f272bSPaul E. McKenney
809984f272bSPaul E. McKenneyOn the other hand, if you prefer C-style comments everywhere, the
810984f272bSPaul E. McKenneyC preprocessor is your friend.
811984f272bSPaul E. McKenney
812984f272bSPaul E. McKenney
813984f272bSPaul E. McKenneyAsynchronous RCU Grace Periods
814984f272bSPaul E. McKenney------------------------------
815984f272bSPaul E. McKenney
816984f272bSPaul E. McKenneyThe following litmus test is derived from the example show in
817984f272bSPaul E. McKenneyDocumentation/litmus-tests/rcu/RCU+sync+free.litmus, but converted to
818984f272bSPaul E. McKenneyemulate call_rcu():
819984f272bSPaul E. McKenney
820984f272bSPaul E. McKenney 1 C RCU+sync+free
821984f272bSPaul E. McKenney 2
822984f272bSPaul E. McKenney 3 {
823984f272bSPaul E. McKenney 4 int x = 1;
824984f272bSPaul E. McKenney 5 int *y = &x;
825984f272bSPaul E. McKenney 6 int z = 1;
826984f272bSPaul E. McKenney 7 }
827984f272bSPaul E. McKenney 8
828984f272bSPaul E. McKenney 9 P0(int *x, int *z, int **y)
829984f272bSPaul E. McKenney10 {
830984f272bSPaul E. McKenney11   int *r0;
831984f272bSPaul E. McKenney12   int r1;
832984f272bSPaul E. McKenney13
833984f272bSPaul E. McKenney14   rcu_read_lock();
834984f272bSPaul E. McKenney15   r0 = rcu_dereference(*y);
835984f272bSPaul E. McKenney16   r1 = READ_ONCE(*r0);
836984f272bSPaul E. McKenney17   rcu_read_unlock();
837984f272bSPaul E. McKenney18 }
838984f272bSPaul E. McKenney19
839984f272bSPaul E. McKenney20 P1(int *z, int **y, int *c)
840984f272bSPaul E. McKenney21 {
841984f272bSPaul E. McKenney22   rcu_assign_pointer(*y, z);
842984f272bSPaul E. McKenney23   smp_store_release(*c, 1); // Emulate call_rcu().
843984f272bSPaul E. McKenney24 }
844984f272bSPaul E. McKenney25
845984f272bSPaul E. McKenney26 P2(int *x, int *z, int **y, int *c)
846984f272bSPaul E. McKenney27 {
847984f272bSPaul E. McKenney28   int r0;
848984f272bSPaul E. McKenney29
849984f272bSPaul E. McKenney30   r0 = smp_load_acquire(*c); // Note call_rcu() request.
850984f272bSPaul E. McKenney31   synchronize_rcu(); // Wait one grace period.
851984f272bSPaul E. McKenney32   WRITE_ONCE(*x, 0); // Emulate the RCU callback.
852984f272bSPaul E. McKenney33 }
853984f272bSPaul E. McKenney34
854984f272bSPaul E. McKenney35 filter (2:r0=1) (* Reject too-early starts. *)
855984f272bSPaul E. McKenney36 exists (0:r0=x /\ 0:r1=0)
856984f272bSPaul E. McKenney
857984f272bSPaul E. McKenneyLines 4-6 initialize a linked list headed by "y" that initially contains
858984f272bSPaul E. McKenney"x".  In addition, "z" is pre-initialized to prepare for P1(), which
859984f272bSPaul E. McKenneywill replace "x" with "z" in this list.
860984f272bSPaul E. McKenney
861984f272bSPaul E. McKenneyP0() on lines 9-18 enters an RCU read-side critical section, loads the
862984f272bSPaul E. McKenneylist header "y" and dereferences it, leaving the node in "0:r0" and
863984f272bSPaul E. McKenneythe node's value in "0:r1".
864984f272bSPaul E. McKenney
865984f272bSPaul E. McKenneyP1() on lines 20-24 updates the list header to instead reference "z",
866984f272bSPaul E. McKenneythen emulates call_rcu() by doing a release store into "c".
867984f272bSPaul E. McKenney
868984f272bSPaul E. McKenneyP2() on lines 27-33 emulates the behind-the-scenes effect of doing a
869984f272bSPaul E. McKenneycall_rcu().  Line 30 first does an acquire load from "c", then line 31
870984f272bSPaul E. McKenneywaits for an RCU grace period to elapse, and finally line 32 emulates
871984f272bSPaul E. McKenneythe RCU callback, which in turn emulates a call to kfree().
872984f272bSPaul E. McKenney
873984f272bSPaul E. McKenneyOf course, it is possible for P2() to start too soon, so that the
874984f272bSPaul E. McKenneyvalue of "2:r0" is zero rather than the required value of "1".
875984f272bSPaul E. McKenneyThe "filter" clause on line 35 handles this possibility, rejecting
876984f272bSPaul E. McKenneyall executions in which "2:r0" is not equal to the value "1".
877984f272bSPaul E. McKenney
878984f272bSPaul E. McKenney
879984f272bSPaul E. McKenneyPerformance
880984f272bSPaul E. McKenney-----------
881984f272bSPaul E. McKenney
882984f272bSPaul E. McKenneyLKMM's exploration of the full state-space can be extremely helpful,
883984f272bSPaul E. McKenneybut it does not come for free.  The price is exponential computational
884984f272bSPaul E. McKenneycomplexity in terms of the number of processes, the average number
885984f272bSPaul E. McKenneyof statements in each process, and the total number of stores in the
886984f272bSPaul E. McKenneylitmus test.
887984f272bSPaul E. McKenney
888984f272bSPaul E. McKenneySo it is best to start small and then work up.  Where possible, break
889984f272bSPaul E. McKenneyyour code down into small pieces each representing a core concurrency
890984f272bSPaul E. McKenneyrequirement.
891984f272bSPaul E. McKenney
892984f272bSPaul E. McKenneyThat said, herd7 is quite fast.  On an unprepossessing x86 laptop, it
893984f272bSPaul E. McKenneywas able to analyze the following 10-process RCU litmus test in about
894984f272bSPaul E. McKenneysix seconds.
895984f272bSPaul E. McKenney
896984f272bSPaul E. McKenneyhttps://github.com/paulmckrcu/litmus/blob/master/auto/C-RW-R+RW-R+RW-G+RW-G+RW-G+RW-G+RW-R+RW-R+RW-R+RW-R.litmus
897984f272bSPaul E. McKenney
898984f272bSPaul E. McKenneyOne way to make herd7 run faster is to use the "-speedcheck true" option.
899984f272bSPaul E. McKenneyThis option prevents herd7 from generating all possible end states,
900984f272bSPaul E. McKenneyinstead causing it to focus solely on whether or not the "exists"
901984f272bSPaul E. McKenneyclause can be satisfied.  With this option, herd7 evaluates the above
902984f272bSPaul E. McKenneylitmus test in about 300 milliseconds, for more than an order of magnitude
903984f272bSPaul E. McKenneyimprovement in performance.
904984f272bSPaul E. McKenney
905984f272bSPaul E. McKenneyLarger 16-process litmus tests that would normally consume 15 minutes
906984f272bSPaul E. McKenneyof time complete in about 40 seconds with this option.  To be fair,
907984f272bSPaul E. McKenneyyou do get an extra 65,535 states when you leave off the "-speedcheck
908984f272bSPaul E. McKenneytrue" option.
909984f272bSPaul E. McKenney
910984f272bSPaul E. McKenneyhttps://github.com/paulmckrcu/litmus/blob/master/auto/C-RW-R+RW-R+RW-G+RW-G+RW-G+RW-G+RW-R+RW-R+RW-R+RW-R+RW-G+RW-G+RW-G+RW-G+RW-R+RW-R.litmus
911984f272bSPaul E. McKenney
912984f272bSPaul E. McKenneyNevertheless, litmus-test analysis really is of exponential complexity,
913984f272bSPaul E. McKenneywhether with or without "-speedcheck true".  Increasing by just three
914984f272bSPaul E. McKenneyprocesses to a 19-process litmus test requires 2 hours and 40 minutes
915984f272bSPaul E. McKenneywithout, and about 8 minutes with "-speedcheck true".  Each of these
916984f272bSPaul E. McKenneyresults represent roughly an order of magnitude slowdown compared to the
917984f272bSPaul E. McKenney16-process litmus test.  Again, to be fair, the multi-hour run explores
918984f272bSPaul E. McKenneyno fewer than 524,287 additional states compared to the shorter one.
919984f272bSPaul E. McKenney
920984f272bSPaul E. McKenneyhttps://github.com/paulmckrcu/litmus/blob/master/auto/C-RW-R+RW-R+RW-G+RW-G+RW-G+RW-G+RW-R+RW-R+RW-R+RW-R+RW-R+RW-R+RW-G+RW-G+RW-G+RW-G+RW-R+RW-R+RW-R.litmus
921984f272bSPaul E. McKenney
922984f272bSPaul E. McKenneyIf you don't like command-line arguments, you can obtain a similar speedup
923984f272bSPaul E. McKenneyby adding a "filter" clause with exactly the same expression as your
924984f272bSPaul E. McKenney"exists" clause.
925984f272bSPaul E. McKenney
926984f272bSPaul E. McKenneyHowever, please note that seeing the full set of states can be extremely
927984f272bSPaul E. McKenneyhelpful when developing and debugging litmus tests.
928984f272bSPaul E. McKenney
929984f272bSPaul E. McKenney
930984f272bSPaul E. McKenneyLIMITATIONS
931984f272bSPaul E. McKenney===========
932984f272bSPaul E. McKenney
933984f272bSPaul E. McKenneyLimitations of the Linux-kernel memory model (LKMM) include:
934984f272bSPaul E. McKenney
935984f272bSPaul E. McKenney1.	Compiler optimizations are not accurately modeled.  Of course,
936984f272bSPaul E. McKenney	the use of READ_ONCE() and WRITE_ONCE() limits the compiler's
937984f272bSPaul E. McKenney	ability to optimize, but under some circumstances it is possible
938984f272bSPaul E. McKenney	for the compiler to undermine the memory model.  For more
939984f272bSPaul E. McKenney	information, see Documentation/explanation.txt (in particular,
940984f272bSPaul E. McKenney	the "THE PROGRAM ORDER RELATION: po AND po-loc" and "A WARNING"
941984f272bSPaul E. McKenney	sections).
942984f272bSPaul E. McKenney
943984f272bSPaul E. McKenney	Note that this limitation in turn limits LKMM's ability to
944984f272bSPaul E. McKenney	accurately model address, control, and data dependencies.
945984f272bSPaul E. McKenney	For example, if the compiler can deduce the value of some variable
946984f272bSPaul E. McKenney	carrying a dependency, then the compiler can break that dependency
947984f272bSPaul E. McKenney	by substituting a constant of that value.
948984f272bSPaul E. McKenney
949be94ecf7SPaul Heidekrüger	Conversely, LKMM will sometimes overestimate the amount of
950be94ecf7SPaul Heidekrüger	reordering compilers and CPUs can carry out, leading it to miss
951be94ecf7SPaul Heidekrüger	some pretty obvious cases of ordering.  A simple example is:
9529270e1a7SAlan Stern
9539270e1a7SAlan Stern		r1 = READ_ONCE(x);
9549270e1a7SAlan Stern		if (r1 == 0)
9559270e1a7SAlan Stern			smp_mb();
9569270e1a7SAlan Stern		WRITE_ONCE(y, 1);
9579270e1a7SAlan Stern
958be94ecf7SPaul Heidekrüger	The WRITE_ONCE() does not depend on the READ_ONCE(), and as a
959be94ecf7SPaul Heidekrüger	result, LKMM does not claim ordering.  However, even though no
960be94ecf7SPaul Heidekrüger	dependency is present, the WRITE_ONCE() will not be executed before
961be94ecf7SPaul Heidekrüger	the READ_ONCE().  There are two reasons for this:
962be94ecf7SPaul Heidekrüger
963be94ecf7SPaul Heidekrüger                The presence of the smp_mb() in one of the branches
964be94ecf7SPaul Heidekrüger                prevents the compiler from moving the WRITE_ONCE()
965be94ecf7SPaul Heidekrüger                up before the "if" statement, since the compiler has
966be94ecf7SPaul Heidekrüger                to assume that r1 will sometimes be 0 (but see the
967be94ecf7SPaul Heidekrüger                comment below);
968be94ecf7SPaul Heidekrüger
969be94ecf7SPaul Heidekrüger                CPUs do not execute stores before po-earlier conditional
970be94ecf7SPaul Heidekrüger                branches, even in cases where the store occurs after the
971be94ecf7SPaul Heidekrüger                two arms of the branch have recombined.
972be94ecf7SPaul Heidekrüger
973be94ecf7SPaul Heidekrüger	It is clear that it is not dangerous in the slightest for LKMM to
974be94ecf7SPaul Heidekrüger	make weaker guarantees than architectures.  In fact, it is
975be94ecf7SPaul Heidekrüger	desirable, as it gives compilers room for making optimizations.
976be94ecf7SPaul Heidekrüger	For instance, suppose that a 0 value in r1 would trigger undefined
977be94ecf7SPaul Heidekrüger	behavior elsewhere.  Then a clever compiler might deduce that r1
978be94ecf7SPaul Heidekrüger	can never be 0 in the if condition.  As a result, said clever
979be94ecf7SPaul Heidekrüger	compiler might deem it safe to optimize away the smp_mb(),
980be94ecf7SPaul Heidekrüger	eliminating the branch and any ordering an architecture would
981be94ecf7SPaul Heidekrüger	guarantee otherwise.
9829270e1a7SAlan Stern
983984f272bSPaul E. McKenney2.	Multiple access sizes for a single variable are not supported,
984984f272bSPaul E. McKenney	and neither are misaligned or partially overlapping accesses.
985984f272bSPaul E. McKenney
986984f272bSPaul E. McKenney3.	Exceptions and interrupts are not modeled.  In some cases,
987984f272bSPaul E. McKenney	this limitation can be overcome by modeling the interrupt or
988984f272bSPaul E. McKenney	exception with an additional process.
989984f272bSPaul E. McKenney
990984f272bSPaul E. McKenney4.	I/O such as MMIO or DMA is not supported.
991984f272bSPaul E. McKenney
992984f272bSPaul E. McKenney5.	Self-modifying code (such as that found in the kernel's
993984f272bSPaul E. McKenney	alternatives mechanism, function tracer, Berkeley Packet Filter
994984f272bSPaul E. McKenney	JIT compiler, and module loader) is not supported.
995984f272bSPaul E. McKenney
996984f272bSPaul E. McKenney6.	Complete modeling of all variants of atomic read-modify-write
997984f272bSPaul E. McKenney	operations, locking primitives, and RCU is not provided.
998984f272bSPaul E. McKenney	For example, call_rcu() and rcu_barrier() are not supported.
999984f272bSPaul E. McKenney	However, a substantial amount of support is provided for these
1000984f272bSPaul E. McKenney	operations, as shown in the linux-kernel.def file.
1001984f272bSPaul E. McKenney
1002984f272bSPaul E. McKenney	Here are specific limitations:
1003984f272bSPaul E. McKenney
1004984f272bSPaul E. McKenney	a.	When rcu_assign_pointer() is passed NULL, the Linux
1005984f272bSPaul E. McKenney		kernel provides no ordering, but LKMM models this
1006984f272bSPaul E. McKenney		case as a store release.
1007984f272bSPaul E. McKenney
1008984f272bSPaul E. McKenney	b.	The "unless" RMW operations are not currently modeled:
1009984f272bSPaul E. McKenney		atomic_long_add_unless(), atomic_inc_unless_negative(),
1010984f272bSPaul E. McKenney		and atomic_dec_unless_positive().  These can be emulated
1011984f272bSPaul E. McKenney		in litmus tests, for example, by using atomic_cmpxchg().
1012984f272bSPaul E. McKenney
1013984f272bSPaul E. McKenney		One exception of this limitation is atomic_add_unless(),
1014984f272bSPaul E. McKenney		which is provided directly by herd7 (so no corresponding
1015984f272bSPaul E. McKenney		definition in linux-kernel.def).  atomic_add_unless() is
1016984f272bSPaul E. McKenney		modeled by herd7 therefore it can be used in litmus tests.
1017984f272bSPaul E. McKenney
1018984f272bSPaul E. McKenney	c.	The call_rcu() function is not modeled.  As was shown above,
1019984f272bSPaul E. McKenney		it can be emulated in litmus tests by adding another
1020984f272bSPaul E. McKenney		process that invokes synchronize_rcu() and the body of the
1021984f272bSPaul E. McKenney		callback function, with (for example) a release-acquire
1022984f272bSPaul E. McKenney		from the site of the emulated call_rcu() to the beginning
1023984f272bSPaul E. McKenney		of the additional process.
1024984f272bSPaul E. McKenney
1025984f272bSPaul E. McKenney	d.	The rcu_barrier() function is not modeled.  It can be
1026984f272bSPaul E. McKenney		emulated in litmus tests emulating call_rcu() via
1027984f272bSPaul E. McKenney		(for example) a release-acquire from the end of each
1028984f272bSPaul E. McKenney		additional call_rcu() process to the site of the
1029984f272bSPaul E. McKenney		emulated rcu-barrier().
1030984f272bSPaul E. McKenney
1031*cc4a2981SAndrea Parri	e.	Reader-writer locking is not modeled.  It can be
1032984f272bSPaul E. McKenney		emulated in litmus tests using atomic read-modify-write
1033984f272bSPaul E. McKenney		operations.
1034984f272bSPaul E. McKenney
1035984f272bSPaul E. McKenneyThe fragment of the C language supported by these litmus tests is quite
1036984f272bSPaul E. McKenneylimited and in some ways non-standard:
1037984f272bSPaul E. McKenney
1038984f272bSPaul E. McKenney1.	There is no automatic C-preprocessor pass.  You can of course
1039984f272bSPaul E. McKenney	run it manually, if you choose.
1040984f272bSPaul E. McKenney
1041984f272bSPaul E. McKenney2.	There is no way to create functions other than the Pn() functions
1042984f272bSPaul E. McKenney	that model the concurrent processes.
1043984f272bSPaul E. McKenney
1044984f272bSPaul E. McKenney3.	The Pn() functions' formal parameters must be pointers to the
1045984f272bSPaul E. McKenney	global shared variables.  Nothing can be passed by value into
1046984f272bSPaul E. McKenney	these functions.
1047984f272bSPaul E. McKenney
1048984f272bSPaul E. McKenney4.	The only functions that can be invoked are those built directly
1049984f272bSPaul E. McKenney	into herd7 or that are defined in the linux-kernel.def file.
1050984f272bSPaul E. McKenney
1051984f272bSPaul E. McKenney5.	The "switch", "do", "for", "while", and "goto" C statements are
1052984f272bSPaul E. McKenney	not supported.	The "switch" statement can be emulated by the
1053984f272bSPaul E. McKenney	"if" statement.  The "do", "for", and "while" statements can
1054984f272bSPaul E. McKenney	often be emulated by manually unrolling the loop, or perhaps by
1055984f272bSPaul E. McKenney	enlisting the aid of the C preprocessor to minimize the resulting
1056984f272bSPaul E. McKenney	code duplication.  Some uses of "goto" can be emulated by "if",
1057984f272bSPaul E. McKenney	and some others by unrolling.
1058984f272bSPaul E. McKenney
1059984f272bSPaul E. McKenney6.	Although you can use a wide variety of types in litmus-test
1060984f272bSPaul E. McKenney	variable declarations, and especially in global-variable
1061984f272bSPaul E. McKenney	declarations, the "herd7" tool understands only int and
1062984f272bSPaul E. McKenney	pointer types.	There is no support for floating-point types,
1063984f272bSPaul E. McKenney	enumerations, characters, strings, arrays, or structures.
1064984f272bSPaul E. McKenney
1065984f272bSPaul E. McKenney7.	Parsing of variable declarations is very loose, with almost no
1066984f272bSPaul E. McKenney	type checking.
1067984f272bSPaul E. McKenney
1068984f272bSPaul E. McKenney8.	Initializers differ from their C-language counterparts.
1069984f272bSPaul E. McKenney	For example, when an initializer contains the name of a shared
1070984f272bSPaul E. McKenney	variable, that name denotes a pointer to that variable, not
1071984f272bSPaul E. McKenney	the current value of that variable.  For example, "int x = y"
1072984f272bSPaul E. McKenney	is interpreted the way "int x = &y" would be in C.
1073984f272bSPaul E. McKenney
1074984f272bSPaul E. McKenney9.	Dynamic memory allocation is not supported, although this can
1075984f272bSPaul E. McKenney	be worked around in some cases by supplying multiple statically
1076984f272bSPaul E. McKenney	allocated variables.
1077984f272bSPaul E. McKenney
1078984f272bSPaul E. McKenneySome of these limitations may be overcome in the future, but others are
1079984f272bSPaul E. McKenneymore likely to be addressed by incorporating the Linux-kernel memory model
1080984f272bSPaul E. McKenneyinto other tools.
1081984f272bSPaul E. McKenney
1082984f272bSPaul E. McKenneyFinally, please note that LKMM is subject to change as hardware, use cases,
1083984f272bSPaul E. McKenneyand compilers evolve.
1084