1Explanation of the Linux-Kernel Memory Consistency Model
2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3
4:Author: Alan Stern <stern@rowland.harvard.edu>
5:Created: October 2017
6
7.. Contents
8
9  1. INTRODUCTION
10  2. BACKGROUND
11  3. A SIMPLE EXAMPLE
12  4. A SELECTION OF MEMORY MODELS
13  5. ORDERING AND CYCLES
14  6. EVENTS
15  7. THE PROGRAM ORDER RELATION: po AND po-loc
16  8. A WARNING
17  9. DEPENDENCY RELATIONS: data, addr, and ctrl
18  10. THE READS-FROM RELATION: rf, rfi, and rfe
19  11. CACHE COHERENCE AND THE COHERENCE ORDER RELATION: co, coi, and coe
20  12. THE FROM-READS RELATION: fr, fri, and fre
21  13. AN OPERATIONAL MODEL
22  14. PROPAGATION ORDER RELATION: cumul-fence
23  15. DERIVATION OF THE LKMM FROM THE OPERATIONAL MODEL
24  16. SEQUENTIAL CONSISTENCY PER VARIABLE
25  17. ATOMIC UPDATES: rmw
26  18. THE PRESERVED PROGRAM ORDER RELATION: ppo
27  19. AND THEN THERE WAS ALPHA
28  20. THE HAPPENS-BEFORE RELATION: hb
29  21. THE PROPAGATES-BEFORE RELATION: pb
30  22. RCU RELATIONS: rcu-link, rcu-gp, rcu-rscsi, rcu-fence, and rb
31  23. LOCKING
32  24. ODDS AND ENDS
33
34
35
36INTRODUCTION
37------------
38
39The Linux-kernel memory consistency model (LKMM) is rather complex and
40obscure.  This is particularly evident if you read through the
41linux-kernel.bell and linux-kernel.cat files that make up the formal
42version of the model; they are extremely terse and their meanings are
43far from clear.
44
45This document describes the ideas underlying the LKMM.  It is meant
46for people who want to understand how the model was designed.  It does
47not go into the details of the code in the .bell and .cat files;
48rather, it explains in English what the code expresses symbolically.
49
50Sections 2 (BACKGROUND) through 5 (ORDERING AND CYCLES) are aimed
51toward beginners; they explain what memory consistency models are and
52the basic notions shared by all such models.  People already familiar
53with these concepts can skim or skip over them.  Sections 6 (EVENTS)
54through 12 (THE FROM_READS RELATION) describe the fundamental
55relations used in many models.  Starting in Section 13 (AN OPERATIONAL
56MODEL), the workings of the LKMM itself are covered.
57
58Warning: The code examples in this document are not written in the
59proper format for litmus tests.  They don't include a header line, the
60initializations are not enclosed in braces, the global variables are
61not passed by pointers, and they don't have an "exists" clause at the
62end.  Converting them to the right format is left as an exercise for
63the reader.
64
65
66BACKGROUND
67----------
68
69A memory consistency model (or just memory model, for short) is
70something which predicts, given a piece of computer code running on a
71particular kind of system, what values may be obtained by the code's
72load instructions.  The LKMM makes these predictions for code running
73as part of the Linux kernel.
74
75In practice, people tend to use memory models the other way around.
76That is, given a piece of code and a collection of values specified
77for the loads, the model will predict whether it is possible for the
78code to run in such a way that the loads will indeed obtain the
79specified values.  Of course, this is just another way of expressing
80the same idea.
81
82For code running on a uniprocessor system, the predictions are easy:
83Each load instruction must obtain the value written by the most recent
84store instruction accessing the same location (we ignore complicating
85factors such as DMA and mixed-size accesses.)  But on multiprocessor
86systems, with multiple CPUs making concurrent accesses to shared
87memory locations, things aren't so simple.
88
89Different architectures have differing memory models, and the Linux
90kernel supports a variety of architectures.  The LKMM has to be fairly
91permissive, in the sense that any behavior allowed by one of these
92architectures also has to be allowed by the LKMM.
93
94
95A SIMPLE EXAMPLE
96----------------
97
98Here is a simple example to illustrate the basic concepts.  Consider
99some code running as part of a device driver for an input device.  The
100driver might contain an interrupt handler which collects data from the
101device, stores it in a buffer, and sets a flag to indicate the buffer
102is full.  Running concurrently on a different CPU might be a part of
103the driver code being executed by a process in the midst of a read(2)
104system call.  This code tests the flag to see whether the buffer is
105ready, and if it is, copies the data back to userspace.  The buffer
106and the flag are memory locations shared between the two CPUs.
107
108We can abstract out the important pieces of the driver code as follows
109(the reason for using WRITE_ONCE() and READ_ONCE() instead of simple
110assignment statements is discussed later):
111
112	int buf = 0, flag = 0;
113
114	P0()
115	{
116		WRITE_ONCE(buf, 1);
117		WRITE_ONCE(flag, 1);
118	}
119
120	P1()
121	{
122		int r1;
123		int r2 = 0;
124
125		r1 = READ_ONCE(flag);
126		if (r1)
127			r2 = READ_ONCE(buf);
128	}
129
130Here the P0() function represents the interrupt handler running on one
131CPU and P1() represents the read() routine running on another.  The
132value 1 stored in buf represents input data collected from the device.
133Thus, P0 stores the data in buf and then sets flag.  Meanwhile, P1
134reads flag into the private variable r1, and if it is set, reads the
135data from buf into a second private variable r2 for copying to
136userspace.  (Presumably if flag is not set then the driver will wait a
137while and try again.)
138
139This pattern of memory accesses, where one CPU stores values to two
140shared memory locations and another CPU loads from those locations in
141the opposite order, is widely known as the "Message Passing" or MP
142pattern.  It is typical of memory access patterns in the kernel.
143
144Please note that this example code is a simplified abstraction.  Real
145buffers are usually larger than a single integer, real device drivers
146usually use sleep and wakeup mechanisms rather than polling for I/O
147completion, and real code generally doesn't bother to copy values into
148private variables before using them.  All that is beside the point;
149the idea here is simply to illustrate the overall pattern of memory
150accesses by the CPUs.
151
152A memory model will predict what values P1 might obtain for its loads
153from flag and buf, or equivalently, what values r1 and r2 might end up
154with after the code has finished running.
155
156Some predictions are trivial.  For instance, no sane memory model would
157predict that r1 = 42 or r2 = -7, because neither of those values ever
158gets stored in flag or buf.
159
160Some nontrivial predictions are nonetheless quite simple.  For
161instance, P1 might run entirely before P0 begins, in which case r1 and
162r2 will both be 0 at the end.  Or P0 might run entirely before P1
163begins, in which case r1 and r2 will both be 1.
164
165The interesting predictions concern what might happen when the two
166routines run concurrently.  One possibility is that P1 runs after P0's
167store to buf but before the store to flag.  In this case, r1 and r2
168will again both be 0.  (If P1 had been designed to read buf
169unconditionally then we would instead have r1 = 0 and r2 = 1.)
170
171However, the most interesting possibility is where r1 = 1 and r2 = 0.
172If this were to occur it would mean the driver contains a bug, because
173incorrect data would get sent to the user: 0 instead of 1.  As it
174happens, the LKMM does predict this outcome can occur, and the example
175driver code shown above is indeed buggy.
176
177
178A SELECTION OF MEMORY MODELS
179----------------------------
180
181The first widely cited memory model, and the simplest to understand,
182is Sequential Consistency.  According to this model, systems behave as
183if each CPU executed its instructions in order but with unspecified
184timing.  In other words, the instructions from the various CPUs get
185interleaved in a nondeterministic way, always according to some single
186global order that agrees with the order of the instructions in the
187program source for each CPU.  The model says that the value obtained
188by each load is simply the value written by the most recently executed
189store to the same memory location, from any CPU.
190
191For the MP example code shown above, Sequential Consistency predicts
192that the undesired result r1 = 1, r2 = 0 cannot occur.  The reasoning
193goes like this:
194
195	Since r1 = 1, P0 must store 1 to flag before P1 loads 1 from
196	it, as loads can obtain values only from earlier stores.
197
198	P1 loads from flag before loading from buf, since CPUs execute
199	their instructions in order.
200
201	P1 must load 0 from buf before P0 stores 1 to it; otherwise r2
202	would be 1 since a load obtains its value from the most recent
203	store to the same address.
204
205	P0 stores 1 to buf before storing 1 to flag, since it executes
206	its instructions in order.
207
208	Since an instruction (in this case, P1's store to flag) cannot
209	execute before itself, the specified outcome is impossible.
210
211However, real computer hardware almost never follows the Sequential
212Consistency memory model; doing so would rule out too many valuable
213performance optimizations.  On ARM and PowerPC architectures, for
214instance, the MP example code really does sometimes yield r1 = 1 and
215r2 = 0.
216
217x86 and SPARC follow yet a different memory model: TSO (Total Store
218Ordering).  This model predicts that the undesired outcome for the MP
219pattern cannot occur, but in other respects it differs from Sequential
220Consistency.  One example is the Store Buffer (SB) pattern, in which
221each CPU stores to its own shared location and then loads from the
222other CPU's location:
223
224	int x = 0, y = 0;
225
226	P0()
227	{
228		int r0;
229
230		WRITE_ONCE(x, 1);
231		r0 = READ_ONCE(y);
232	}
233
234	P1()
235	{
236		int r1;
237
238		WRITE_ONCE(y, 1);
239		r1 = READ_ONCE(x);
240	}
241
242Sequential Consistency predicts that the outcome r0 = 0, r1 = 0 is
243impossible.  (Exercise: Figure out the reasoning.)  But TSO allows
244this outcome to occur, and in fact it does sometimes occur on x86 and
245SPARC systems.
246
247The LKMM was inspired by the memory models followed by PowerPC, ARM,
248x86, Alpha, and other architectures.  However, it is different in
249detail from each of them.
250
251
252ORDERING AND CYCLES
253-------------------
254
255Memory models are all about ordering.  Often this is temporal ordering
256(i.e., the order in which certain events occur) but it doesn't have to
257be; consider for example the order of instructions in a program's
258source code.  We saw above that Sequential Consistency makes an
259important assumption that CPUs execute instructions in the same order
260as those instructions occur in the code, and there are many other
261instances of ordering playing central roles in memory models.
262
263The counterpart to ordering is a cycle.  Ordering rules out cycles:
264It's not possible to have X ordered before Y, Y ordered before Z, and
265Z ordered before X, because this would mean that X is ordered before
266itself.  The analysis of the MP example under Sequential Consistency
267involved just such an impossible cycle:
268
269	W: P0 stores 1 to flag   executes before
270	X: P1 loads 1 from flag  executes before
271	Y: P1 loads 0 from buf   executes before
272	Z: P0 stores 1 to buf    executes before
273	W: P0 stores 1 to flag.
274
275In short, if a memory model requires certain accesses to be ordered,
276and a certain outcome for the loads in a piece of code can happen only
277if those accesses would form a cycle, then the memory model predicts
278that outcome cannot occur.
279
280The LKMM is defined largely in terms of cycles, as we will see.
281
282
283EVENTS
284------
285
286The LKMM does not work directly with the C statements that make up
287kernel source code.  Instead it considers the effects of those
288statements in a more abstract form, namely, events.  The model
289includes three types of events:
290
291	Read events correspond to loads from shared memory, such as
292	calls to READ_ONCE(), smp_load_acquire(), or
293	rcu_dereference().
294
295	Write events correspond to stores to shared memory, such as
296	calls to WRITE_ONCE(), smp_store_release(), or atomic_set().
297
298	Fence events correspond to memory barriers (also known as
299	fences), such as calls to smp_rmb() or rcu_read_lock().
300
301These categories are not exclusive; a read or write event can also be
302a fence.  This happens with functions like smp_load_acquire() or
303spin_lock().  However, no single event can be both a read and a write.
304Atomic read-modify-write accesses, such as atomic_inc() or xchg(),
305correspond to a pair of events: a read followed by a write.  (The
306write event is omitted for executions where it doesn't occur, such as
307a cmpxchg() where the comparison fails.)
308
309Other parts of the code, those which do not involve interaction with
310shared memory, do not give rise to events.  Thus, arithmetic and
311logical computations, control-flow instructions, or accesses to
312private memory or CPU registers are not of central interest to the
313memory model.  They only affect the model's predictions indirectly.
314For example, an arithmetic computation might determine the value that
315gets stored to a shared memory location (or in the case of an array
316index, the address where the value gets stored), but the memory model
317is concerned only with the store itself -- its value and its address
318-- not the computation leading up to it.
319
320Events in the LKMM can be linked by various relations, which we will
321describe in the following sections.  The memory model requires certain
322of these relations to be orderings, that is, it requires them not to
323have any cycles.
324
325
326THE PROGRAM ORDER RELATION: po AND po-loc
327-----------------------------------------
328
329The most important relation between events is program order (po).  You
330can think of it as the order in which statements occur in the source
331code after branches are taken into account and loops have been
332unrolled.  A better description might be the order in which
333instructions are presented to a CPU's execution unit.  Thus, we say
334that X is po-before Y (written as "X ->po Y" in formulas) if X occurs
335before Y in the instruction stream.
336
337This is inherently a single-CPU relation; two instructions executing
338on different CPUs are never linked by po.  Also, it is by definition
339an ordering so it cannot have any cycles.
340
341po-loc is a sub-relation of po.  It links two memory accesses when the
342first comes before the second in program order and they access the
343same memory location (the "-loc" suffix).
344
345Although this may seem straightforward, there is one subtle aspect to
346program order we need to explain.  The LKMM was inspired by low-level
347architectural memory models which describe the behavior of machine
348code, and it retains their outlook to a considerable extent.  The
349read, write, and fence events used by the model are close in spirit to
350individual machine instructions.  Nevertheless, the LKMM describes
351kernel code written in C, and the mapping from C to machine code can
352be extremely complex.
353
354Optimizing compilers have great freedom in the way they translate
355source code to object code.  They are allowed to apply transformations
356that add memory accesses, eliminate accesses, combine them, split them
357into pieces, or move them around.  Faced with all these possibilities,
358the LKMM basically gives up.  It insists that the code it analyzes
359must contain no ordinary accesses to shared memory; all accesses must
360be performed using READ_ONCE(), WRITE_ONCE(), or one of the other
361atomic or synchronization primitives.  These primitives prevent a
362large number of compiler optimizations.  In particular, it is
363guaranteed that the compiler will not remove such accesses from the
364generated code (unless it can prove the accesses will never be
365executed), it will not change the order in which they occur in the
366code (within limits imposed by the C standard), and it will not
367introduce extraneous accesses.
368
369This explains why the MP and SB examples above used READ_ONCE() and
370WRITE_ONCE() rather than ordinary memory accesses.  Thanks to this
371usage, we can be certain that in the MP example, P0's write event to
372buf really is po-before its write event to flag, and similarly for the
373other shared memory accesses in the examples.
374
375Private variables are not subject to this restriction.  Since they are
376not shared between CPUs, they can be accessed normally without
377READ_ONCE() or WRITE_ONCE(), and there will be no ill effects.  In
378fact, they need not even be stored in normal memory at all -- in
379principle a private variable could be stored in a CPU register (hence
380the convention that these variables have names starting with the
381letter 'r').
382
383
384A WARNING
385---------
386
387The protections provided by READ_ONCE(), WRITE_ONCE(), and others are
388not perfect; and under some circumstances it is possible for the
389compiler to undermine the memory model.  Here is an example.  Suppose
390both branches of an "if" statement store the same value to the same
391location:
392
393	r1 = READ_ONCE(x);
394	if (r1) {
395		WRITE_ONCE(y, 2);
396		...  /* do something */
397	} else {
398		WRITE_ONCE(y, 2);
399		...  /* do something else */
400	}
401
402For this code, the LKMM predicts that the load from x will always be
403executed before either of the stores to y.  However, a compiler could
404lift the stores out of the conditional, transforming the code into
405something resembling:
406
407	r1 = READ_ONCE(x);
408	WRITE_ONCE(y, 2);
409	if (r1) {
410		...  /* do something */
411	} else {
412		...  /* do something else */
413	}
414
415Given this version of the code, the LKMM would predict that the load
416from x could be executed after the store to y.  Thus, the memory
417model's original prediction could be invalidated by the compiler.
418
419Another issue arises from the fact that in C, arguments to many
420operators and function calls can be evaluated in any order.  For
421example:
422
423	r1 = f(5) + g(6);
424
425The object code might call f(5) either before or after g(6); the
426memory model cannot assume there is a fixed program order relation
427between them.  (In fact, if the functions are inlined then the
428compiler might even interleave their object code.)
429
430
431DEPENDENCY RELATIONS: data, addr, and ctrl
432------------------------------------------
433
434We say that two events are linked by a dependency relation when the
435execution of the second event depends in some way on a value obtained
436from memory by the first.  The first event must be a read, and the
437value it obtains must somehow affect what the second event does.
438There are three kinds of dependencies: data, address (addr), and
439control (ctrl).
440
441A read and a write event are linked by a data dependency if the value
442obtained by the read affects the value stored by the write.  As a very
443simple example:
444
445	int x, y;
446
447	r1 = READ_ONCE(x);
448	WRITE_ONCE(y, r1 + 5);
449
450The value stored by the WRITE_ONCE obviously depends on the value
451loaded by the READ_ONCE.  Such dependencies can wind through
452arbitrarily complicated computations, and a write can depend on the
453values of multiple reads.
454
455A read event and another memory access event are linked by an address
456dependency if the value obtained by the read affects the location
457accessed by the other event.  The second event can be either a read or
458a write.  Here's another simple example:
459
460	int a[20];
461	int i;
462
463	r1 = READ_ONCE(i);
464	r2 = READ_ONCE(a[r1]);
465
466Here the location accessed by the second READ_ONCE() depends on the
467index value loaded by the first.  Pointer indirection also gives rise
468to address dependencies, since the address of a location accessed
469through a pointer will depend on the value read earlier from that
470pointer.
471
472Finally, a read event and another memory access event are linked by a
473control dependency if the value obtained by the read affects whether
474the second event is executed at all.  Simple example:
475
476	int x, y;
477
478	r1 = READ_ONCE(x);
479	if (r1)
480		WRITE_ONCE(y, 1984);
481
482Execution of the WRITE_ONCE() is controlled by a conditional expression
483which depends on the value obtained by the READ_ONCE(); hence there is
484a control dependency from the load to the store.
485
486It should be pretty obvious that events can only depend on reads that
487come earlier in program order.  Symbolically, if we have R ->data X,
488R ->addr X, or R ->ctrl X (where R is a read event), then we must also
489have R ->po X.  It wouldn't make sense for a computation to depend
490somehow on a value that doesn't get loaded from shared memory until
491later in the code!
492
493
494THE READS-FROM RELATION: rf, rfi, and rfe
495-----------------------------------------
496
497The reads-from relation (rf) links a write event to a read event when
498the value loaded by the read is the value that was stored by the
499write.  In colloquial terms, the load "reads from" the store.  We
500write W ->rf R to indicate that the load R reads from the store W.  We
501further distinguish the cases where the load and the store occur on
502the same CPU (internal reads-from, or rfi) and where they occur on
503different CPUs (external reads-from, or rfe).
504
505For our purposes, a memory location's initial value is treated as
506though it had been written there by an imaginary initial store that
507executes on a separate CPU before the program runs.
508
509Usage of the rf relation implicitly assumes that loads will always
510read from a single store.  It doesn't apply properly in the presence
511of load-tearing, where a load obtains some of its bits from one store
512and some of them from another store.  Fortunately, use of READ_ONCE()
513and WRITE_ONCE() will prevent load-tearing; it's not possible to have:
514
515	int x = 0;
516
517	P0()
518	{
519		WRITE_ONCE(x, 0x1234);
520	}
521
522	P1()
523	{
524		int r1;
525
526		r1 = READ_ONCE(x);
527	}
528
529and end up with r1 = 0x1200 (partly from x's initial value and partly
530from the value stored by P0).
531
532On the other hand, load-tearing is unavoidable when mixed-size
533accesses are used.  Consider this example:
534
535	union {
536		u32	w;
537		u16	h[2];
538	} x;
539
540	P0()
541	{
542		WRITE_ONCE(x.h[0], 0x1234);
543		WRITE_ONCE(x.h[1], 0x5678);
544	}
545
546	P1()
547	{
548		int r1;
549
550		r1 = READ_ONCE(x.w);
551	}
552
553If r1 = 0x56781234 (little-endian!) at the end, then P1 must have read
554from both of P0's stores.  It is possible to handle mixed-size and
555unaligned accesses in a memory model, but the LKMM currently does not
556attempt to do so.  It requires all accesses to be properly aligned and
557of the location's actual size.
558
559
560CACHE COHERENCE AND THE COHERENCE ORDER RELATION: co, coi, and coe
561------------------------------------------------------------------
562
563Cache coherence is a general principle requiring that in a
564multi-processor system, the CPUs must share a consistent view of the
565memory contents.  Specifically, it requires that for each location in
566shared memory, the stores to that location must form a single global
567ordering which all the CPUs agree on (the coherence order), and this
568ordering must be consistent with the program order for accesses to
569that location.
570
571To put it another way, for any variable x, the coherence order (co) of
572the stores to x is simply the order in which the stores overwrite one
573another.  The imaginary store which establishes x's initial value
574comes first in the coherence order; the store which directly
575overwrites the initial value comes second; the store which overwrites
576that value comes third, and so on.
577
578You can think of the coherence order as being the order in which the
579stores reach x's location in memory (or if you prefer a more
580hardware-centric view, the order in which the stores get written to
581x's cache line).  We write W ->co W' if W comes before W' in the
582coherence order, that is, if the value stored by W gets overwritten,
583directly or indirectly, by the value stored by W'.
584
585Coherence order is required to be consistent with program order.  This
586requirement takes the form of four coherency rules:
587
588	Write-write coherence: If W ->po-loc W' (i.e., W comes before
589	W' in program order and they access the same location), where W
590	and W' are two stores, then W ->co W'.
591
592	Write-read coherence: If W ->po-loc R, where W is a store and R
593	is a load, then R must read from W or from some other store
594	which comes after W in the coherence order.
595
596	Read-write coherence: If R ->po-loc W, where R is a load and W
597	is a store, then the store which R reads from must come before
598	W in the coherence order.
599
600	Read-read coherence: If R ->po-loc R', where R and R' are two
601	loads, then either they read from the same store or else the
602	store read by R comes before the store read by R' in the
603	coherence order.
604
605This is sometimes referred to as sequential consistency per variable,
606because it means that the accesses to any single memory location obey
607the rules of the Sequential Consistency memory model.  (According to
608Wikipedia, sequential consistency per variable and cache coherence
609mean the same thing except that cache coherence includes an extra
610requirement that every store eventually becomes visible to every CPU.)
611
612Any reasonable memory model will include cache coherence.  Indeed, our
613expectation of cache coherence is so deeply ingrained that violations
614of its requirements look more like hardware bugs than programming
615errors:
616
617	int x;
618
619	P0()
620	{
621		WRITE_ONCE(x, 17);
622		WRITE_ONCE(x, 23);
623	}
624
625If the final value stored in x after this code ran was 17, you would
626think your computer was broken.  It would be a violation of the
627write-write coherence rule: Since the store of 23 comes later in
628program order, it must also come later in x's coherence order and
629thus must overwrite the store of 17.
630
631	int x = 0;
632
633	P0()
634	{
635		int r1;
636
637		r1 = READ_ONCE(x);
638		WRITE_ONCE(x, 666);
639	}
640
641If r1 = 666 at the end, this would violate the read-write coherence
642rule: The READ_ONCE() load comes before the WRITE_ONCE() store in
643program order, so it must not read from that store but rather from one
644coming earlier in the coherence order (in this case, x's initial
645value).
646
647	int x = 0;
648
649	P0()
650	{
651		WRITE_ONCE(x, 5);
652	}
653
654	P1()
655	{
656		int r1, r2;
657
658		r1 = READ_ONCE(x);
659		r2 = READ_ONCE(x);
660	}
661
662If r1 = 5 (reading from P0's store) and r2 = 0 (reading from the
663imaginary store which establishes x's initial value) at the end, this
664would violate the read-read coherence rule: The r1 load comes before
665the r2 load in program order, so it must not read from a store that
666comes later in the coherence order.
667
668(As a minor curiosity, if this code had used normal loads instead of
669READ_ONCE() in P1, on Itanium it sometimes could end up with r1 = 5
670and r2 = 0!  This results from parallel execution of the operations
671encoded in Itanium's Very-Long-Instruction-Word format, and it is yet
672another motivation for using READ_ONCE() when accessing shared memory
673locations.)
674
675Just like the po relation, co is inherently an ordering -- it is not
676possible for a store to directly or indirectly overwrite itself!  And
677just like with the rf relation, we distinguish between stores that
678occur on the same CPU (internal coherence order, or coi) and stores
679that occur on different CPUs (external coherence order, or coe).
680
681On the other hand, stores to different memory locations are never
682related by co, just as instructions on different CPUs are never
683related by po.  Coherence order is strictly per-location, or if you
684prefer, each location has its own independent coherence order.
685
686
687THE FROM-READS RELATION: fr, fri, and fre
688-----------------------------------------
689
690The from-reads relation (fr) can be a little difficult for people to
691grok.  It describes the situation where a load reads a value that gets
692overwritten by a store.  In other words, we have R ->fr W when the
693value that R reads is overwritten (directly or indirectly) by W, or
694equivalently, when R reads from a store which comes earlier than W in
695the coherence order.
696
697For example:
698
699	int x = 0;
700
701	P0()
702	{
703		int r1;
704
705		r1 = READ_ONCE(x);
706		WRITE_ONCE(x, 2);
707	}
708
709The value loaded from x will be 0 (assuming cache coherence!), and it
710gets overwritten by the value 2.  Thus there is an fr link from the
711READ_ONCE() to the WRITE_ONCE().  If the code contained any later
712stores to x, there would also be fr links from the READ_ONCE() to
713them.
714
715As with rf, rfi, and rfe, we subdivide the fr relation into fri (when
716the load and the store are on the same CPU) and fre (when they are on
717different CPUs).
718
719Note that the fr relation is determined entirely by the rf and co
720relations; it is not independent.  Given a read event R and a write
721event W for the same location, we will have R ->fr W if and only if
722the write which R reads from is co-before W.  In symbols,
723
724	(R ->fr W) := (there exists W' with W' ->rf R and W' ->co W).
725
726
727AN OPERATIONAL MODEL
728--------------------
729
730The LKMM is based on various operational memory models, meaning that
731the models arise from an abstract view of how a computer system
732operates.  Here are the main ideas, as incorporated into the LKMM.
733
734The system as a whole is divided into the CPUs and a memory subsystem.
735The CPUs are responsible for executing instructions (not necessarily
736in program order), and they communicate with the memory subsystem.
737For the most part, executing an instruction requires a CPU to perform
738only internal operations.  However, loads, stores, and fences involve
739more.
740
741When CPU C executes a store instruction, it tells the memory subsystem
742to store a certain value at a certain location.  The memory subsystem
743propagates the store to all the other CPUs as well as to RAM.  (As a
744special case, we say that the store propagates to its own CPU at the
745time it is executed.)  The memory subsystem also determines where the
746store falls in the location's coherence order.  In particular, it must
747arrange for the store to be co-later than (i.e., to overwrite) any
748other store to the same location which has already propagated to CPU C.
749
750When a CPU executes a load instruction R, it first checks to see
751whether there are any as-yet unexecuted store instructions, for the
752same location, that come before R in program order.  If there are, it
753uses the value of the po-latest such store as the value obtained by R,
754and we say that the store's value is forwarded to R.  Otherwise, the
755CPU asks the memory subsystem for the value to load and we say that R
756is satisfied from memory.  The memory subsystem hands back the value
757of the co-latest store to the location in question which has already
758propagated to that CPU.
759
760(In fact, the picture needs to be a little more complicated than this.
761CPUs have local caches, and propagating a store to a CPU really means
762propagating it to the CPU's local cache.  A local cache can take some
763time to process the stores that it receives, and a store can't be used
764to satisfy one of the CPU's loads until it has been processed.  On
765most architectures, the local caches process stores in
766First-In-First-Out order, and consequently the processing delay
767doesn't matter for the memory model.  But on Alpha, the local caches
768have a partitioned design that results in non-FIFO behavior.  We will
769discuss this in more detail later.)
770
771Note that load instructions may be executed speculatively and may be
772restarted under certain circumstances.  The memory model ignores these
773premature executions; we simply say that the load executes at the
774final time it is forwarded or satisfied.
775
776Executing a fence (or memory barrier) instruction doesn't require a
777CPU to do anything special other than informing the memory subsystem
778about the fence.  However, fences do constrain the way CPUs and the
779memory subsystem handle other instructions, in two respects.
780
781First, a fence forces the CPU to execute various instructions in
782program order.  Exactly which instructions are ordered depends on the
783type of fence:
784
785	Strong fences, including smp_mb() and synchronize_rcu(), force
786	the CPU to execute all po-earlier instructions before any
787	po-later instructions;
788
789	smp_rmb() forces the CPU to execute all po-earlier loads
790	before any po-later loads;
791
792	smp_wmb() forces the CPU to execute all po-earlier stores
793	before any po-later stores;
794
795	Acquire fences, such as smp_load_acquire(), force the CPU to
796	execute the load associated with the fence (e.g., the load
797	part of an smp_load_acquire()) before any po-later
798	instructions;
799
800	Release fences, such as smp_store_release(), force the CPU to
801	execute all po-earlier instructions before the store
802	associated with the fence (e.g., the store part of an
803	smp_store_release()).
804
805Second, some types of fence affect the way the memory subsystem
806propagates stores.  When a fence instruction is executed on CPU C:
807
808	For each other CPU C', smp_wmb() forces all po-earlier stores
809	on C to propagate to C' before any po-later stores do.
810
811	For each other CPU C', any store which propagates to C before
812	a release fence is executed (including all po-earlier
813	stores executed on C) is forced to propagate to C' before the
814	store associated with the release fence does.
815
816	Any store which propagates to C before a strong fence is
817	executed (including all po-earlier stores on C) is forced to
818	propagate to all other CPUs before any instructions po-after
819	the strong fence are executed on C.
820
821The propagation ordering enforced by release fences and strong fences
822affects stores from other CPUs that propagate to CPU C before the
823fence is executed, as well as stores that are executed on C before the
824fence.  We describe this property by saying that release fences and
825strong fences are A-cumulative.  By contrast, smp_wmb() fences are not
826A-cumulative; they only affect the propagation of stores that are
827executed on C before the fence (i.e., those which precede the fence in
828program order).
829
830rcu_read_lock(), rcu_read_unlock(), and synchronize_rcu() fences have
831other properties which we discuss later.
832
833
834PROPAGATION ORDER RELATION: cumul-fence
835---------------------------------------
836
837The fences which affect propagation order (i.e., strong, release, and
838smp_wmb() fences) are collectively referred to as cumul-fences, even
839though smp_wmb() isn't A-cumulative.  The cumul-fence relation is
840defined to link memory access events E and F whenever:
841
842	E and F are both stores on the same CPU and an smp_wmb() fence
843	event occurs between them in program order; or
844
845	F is a release fence and some X comes before F in program order,
846	where either X = E or else E ->rf X; or
847
848	A strong fence event occurs between some X and F in program
849	order, where either X = E or else E ->rf X.
850
851The operational model requires that whenever W and W' are both stores
852and W ->cumul-fence W', then W must propagate to any given CPU
853before W' does.  However, for different CPUs C and C', it does not
854require W to propagate to C before W' propagates to C'.
855
856
857DERIVATION OF THE LKMM FROM THE OPERATIONAL MODEL
858-------------------------------------------------
859
860The LKMM is derived from the restrictions imposed by the design
861outlined above.  These restrictions involve the necessity of
862maintaining cache coherence and the fact that a CPU can't operate on a
863value before it knows what that value is, among other things.
864
865The formal version of the LKMM is defined by five requirements, or
866axioms:
867
868	Sequential consistency per variable: This requires that the
869	system obey the four coherency rules.
870
871	Atomicity: This requires that atomic read-modify-write
872	operations really are atomic, that is, no other stores can
873	sneak into the middle of such an update.
874
875	Happens-before: This requires that certain instructions are
876	executed in a specific order.
877
878	Propagation: This requires that certain stores propagate to
879	CPUs and to RAM in a specific order.
880
881	Rcu: This requires that RCU read-side critical sections and
882	grace periods obey the rules of RCU, in particular, the
883	Grace-Period Guarantee.
884
885The first and second are quite common; they can be found in many
886memory models (such as those for C11/C++11).  The "happens-before" and
887"propagation" axioms have analogs in other memory models as well.  The
888"rcu" axiom is specific to the LKMM.
889
890Each of these axioms is discussed below.
891
892
893SEQUENTIAL CONSISTENCY PER VARIABLE
894-----------------------------------
895
896According to the principle of cache coherence, the stores to any fixed
897shared location in memory form a global ordering.  We can imagine
898inserting the loads from that location into this ordering, by placing
899each load between the store that it reads from and the following
900store.  This leaves the relative positions of loads that read from the
901same store unspecified; let's say they are inserted in program order,
902first for CPU 0, then CPU 1, etc.
903
904You can check that the four coherency rules imply that the rf, co, fr,
905and po-loc relations agree with this global ordering; in other words,
906whenever we have X ->rf Y or X ->co Y or X ->fr Y or X ->po-loc Y, the
907X event comes before the Y event in the global ordering.  The LKMM's
908"coherence" axiom expresses this by requiring the union of these
909relations not to have any cycles.  This means it must not be possible
910to find events
911
912	X0 -> X1 -> X2 -> ... -> Xn -> X0,
913
914where each of the links is either rf, co, fr, or po-loc.  This has to
915hold if the accesses to the fixed memory location can be ordered as
916cache coherence demands.
917
918Although it is not obvious, it can be shown that the converse is also
919true: This LKMM axiom implies that the four coherency rules are
920obeyed.
921
922
923ATOMIC UPDATES: rmw
924-------------------
925
926What does it mean to say that a read-modify-write (rmw) update, such
927as atomic_inc(&x), is atomic?  It means that the memory location (x in
928this case) does not get altered between the read and the write events
929making up the atomic operation.  In particular, if two CPUs perform
930atomic_inc(&x) concurrently, it must be guaranteed that the final
931value of x will be the initial value plus two.  We should never have
932the following sequence of events:
933
934	CPU 0 loads x obtaining 13;
935					CPU 1 loads x obtaining 13;
936	CPU 0 stores 14 to x;
937					CPU 1 stores 14 to x;
938
939where the final value of x is wrong (14 rather than 15).
940
941In this example, CPU 0's increment effectively gets lost because it
942occurs in between CPU 1's load and store.  To put it another way, the
943problem is that the position of CPU 0's store in x's coherence order
944is between the store that CPU 1 reads from and the store that CPU 1
945performs.
946
947The same analysis applies to all atomic update operations.  Therefore,
948to enforce atomicity the LKMM requires that atomic updates follow this
949rule: Whenever R and W are the read and write events composing an
950atomic read-modify-write and W' is the write event which R reads from,
951there must not be any stores coming between W' and W in the coherence
952order.  Equivalently,
953
954	(R ->rmw W) implies (there is no X with R ->fr X and X ->co W),
955
956where the rmw relation links the read and write events making up each
957atomic update.  This is what the LKMM's "atomic" axiom says.
958
959
960THE PRESERVED PROGRAM ORDER RELATION: ppo
961-----------------------------------------
962
963There are many situations where a CPU is obligated to execute two
964instructions in program order.  We amalgamate them into the ppo (for
965"preserved program order") relation, which links the po-earlier
966instruction to the po-later instruction and is thus a sub-relation of
967po.
968
969The operational model already includes a description of one such
970situation: Fences are a source of ppo links.  Suppose X and Y are
971memory accesses with X ->po Y; then the CPU must execute X before Y if
972any of the following hold:
973
974	A strong (smp_mb() or synchronize_rcu()) fence occurs between
975	X and Y;
976
977	X and Y are both stores and an smp_wmb() fence occurs between
978	them;
979
980	X and Y are both loads and an smp_rmb() fence occurs between
981	them;
982
983	X is also an acquire fence, such as smp_load_acquire();
984
985	Y is also a release fence, such as smp_store_release().
986
987Another possibility, not mentioned earlier but discussed in the next
988section, is:
989
990	X and Y are both loads, X ->addr Y (i.e., there is an address
991	dependency from X to Y), and X is a READ_ONCE() or an atomic
992	access.
993
994Dependencies can also cause instructions to be executed in program
995order.  This is uncontroversial when the second instruction is a
996store; either a data, address, or control dependency from a load R to
997a store W will force the CPU to execute R before W.  This is very
998simply because the CPU cannot tell the memory subsystem about W's
999store before it knows what value should be stored (in the case of a
1000data dependency), what location it should be stored into (in the case
1001of an address dependency), or whether the store should actually take
1002place (in the case of a control dependency).
1003
1004Dependencies to load instructions are more problematic.  To begin with,
1005there is no such thing as a data dependency to a load.  Next, a CPU
1006has no reason to respect a control dependency to a load, because it
1007can always satisfy the second load speculatively before the first, and
1008then ignore the result if it turns out that the second load shouldn't
1009be executed after all.  And lastly, the real difficulties begin when
1010we consider address dependencies to loads.
1011
1012To be fair about it, all Linux-supported architectures do execute
1013loads in program order if there is an address dependency between them.
1014After all, a CPU cannot ask the memory subsystem to load a value from
1015a particular location before it knows what that location is.  However,
1016the split-cache design used by Alpha can cause it to behave in a way
1017that looks as if the loads were executed out of order (see the next
1018section for more details).  The kernel includes a workaround for this
1019problem when the loads come from READ_ONCE(), and therefore the LKMM
1020includes address dependencies to loads in the ppo relation.
1021
1022On the other hand, dependencies can indirectly affect the ordering of
1023two loads.  This happens when there is a dependency from a load to a
1024store and a second, po-later load reads from that store:
1025
1026	R ->dep W ->rfi R',
1027
1028where the dep link can be either an address or a data dependency.  In
1029this situation we know it is possible for the CPU to execute R' before
1030W, because it can forward the value that W will store to R'.  But it
1031cannot execute R' before R, because it cannot forward the value before
1032it knows what that value is, or that W and R' do access the same
1033location.  However, if there is merely a control dependency between R
1034and W then the CPU can speculatively forward W to R' before executing
1035R; if the speculation turns out to be wrong then the CPU merely has to
1036restart or abandon R'.
1037
1038(In theory, a CPU might forward a store to a load when it runs across
1039an address dependency like this:
1040
1041	r1 = READ_ONCE(ptr);
1042	WRITE_ONCE(*r1, 17);
1043	r2 = READ_ONCE(*r1);
1044
1045because it could tell that the store and the second load access the
1046same location even before it knows what the location's address is.
1047However, none of the architectures supported by the Linux kernel do
1048this.)
1049
1050Two memory accesses of the same location must always be executed in
1051program order if the second access is a store.  Thus, if we have
1052
1053	R ->po-loc W
1054
1055(the po-loc link says that R comes before W in program order and they
1056access the same location), the CPU is obliged to execute W after R.
1057If it executed W first then the memory subsystem would respond to R's
1058read request with the value stored by W (or an even later store), in
1059violation of the read-write coherence rule.  Similarly, if we had
1060
1061	W ->po-loc W'
1062
1063and the CPU executed W' before W, then the memory subsystem would put
1064W' before W in the coherence order.  It would effectively cause W to
1065overwrite W', in violation of the write-write coherence rule.
1066(Interestingly, an early ARMv8 memory model, now obsolete, proposed
1067allowing out-of-order writes like this to occur.  The model avoided
1068violating the write-write coherence rule by requiring the CPU not to
1069send the W write to the memory subsystem at all!)
1070
1071
1072AND THEN THERE WAS ALPHA
1073------------------------
1074
1075As mentioned above, the Alpha architecture is unique in that it does
1076not appear to respect address dependencies to loads.  This means that
1077code such as the following:
1078
1079	int x = 0;
1080	int y = -1;
1081	int *ptr = &y;
1082
1083	P0()
1084	{
1085		WRITE_ONCE(x, 1);
1086		smp_wmb();
1087		WRITE_ONCE(ptr, &x);
1088	}
1089
1090	P1()
1091	{
1092		int *r1;
1093		int r2;
1094
1095		r1 = ptr;
1096		r2 = READ_ONCE(*r1);
1097	}
1098
1099can malfunction on Alpha systems (notice that P1 uses an ordinary load
1100to read ptr instead of READ_ONCE()).  It is quite possible that r1 = &x
1101and r2 = 0 at the end, in spite of the address dependency.
1102
1103At first glance this doesn't seem to make sense.  We know that the
1104smp_wmb() forces P0's store to x to propagate to P1 before the store
1105to ptr does.  And since P1 can't execute its second load
1106until it knows what location to load from, i.e., after executing its
1107first load, the value x = 1 must have propagated to P1 before the
1108second load executed.  So why doesn't r2 end up equal to 1?
1109
1110The answer lies in the Alpha's split local caches.  Although the two
1111stores do reach P1's local cache in the proper order, it can happen
1112that the first store is processed by a busy part of the cache while
1113the second store is processed by an idle part.  As a result, the x = 1
1114value may not become available for P1's CPU to read until after the
1115ptr = &x value does, leading to the undesirable result above.  The
1116final effect is that even though the two loads really are executed in
1117program order, it appears that they aren't.
1118
1119This could not have happened if the local cache had processed the
1120incoming stores in FIFO order.  By contrast, other architectures
1121maintain at least the appearance of FIFO order.
1122
1123In practice, this difficulty is solved by inserting a special fence
1124between P1's two loads when the kernel is compiled for the Alpha
1125architecture.  In fact, as of version 4.15, the kernel automatically
1126adds this fence (called smp_read_barrier_depends() and defined as
1127nothing at all on non-Alpha builds) after every READ_ONCE() and atomic
1128load.  The effect of the fence is to cause the CPU not to execute any
1129po-later instructions until after the local cache has finished
1130processing all the stores it has already received.  Thus, if the code
1131was changed to:
1132
1133	P1()
1134	{
1135		int *r1;
1136		int r2;
1137
1138		r1 = READ_ONCE(ptr);
1139		r2 = READ_ONCE(*r1);
1140	}
1141
1142then we would never get r1 = &x and r2 = 0.  By the time P1 executed
1143its second load, the x = 1 store would already be fully processed by
1144the local cache and available for satisfying the read request.  Thus
1145we have yet another reason why shared data should always be read with
1146READ_ONCE() or another synchronization primitive rather than accessed
1147directly.
1148
1149The LKMM requires that smp_rmb(), acquire fences, and strong fences
1150share this property with smp_read_barrier_depends(): They do not allow
1151the CPU to execute any po-later instructions (or po-later loads in the
1152case of smp_rmb()) until all outstanding stores have been processed by
1153the local cache.  In the case of a strong fence, the CPU first has to
1154wait for all of its po-earlier stores to propagate to every other CPU
1155in the system; then it has to wait for the local cache to process all
1156the stores received as of that time -- not just the stores received
1157when the strong fence began.
1158
1159And of course, none of this matters for any architecture other than
1160Alpha.
1161
1162
1163THE HAPPENS-BEFORE RELATION: hb
1164-------------------------------
1165
1166The happens-before relation (hb) links memory accesses that have to
1167execute in a certain order.  hb includes the ppo relation and two
1168others, one of which is rfe.
1169
1170W ->rfe R implies that W and R are on different CPUs.  It also means
1171that W's store must have propagated to R's CPU before R executed;
1172otherwise R could not have read the value stored by W.  Therefore W
1173must have executed before R, and so we have W ->hb R.
1174
1175The equivalent fact need not hold if W ->rfi R (i.e., W and R are on
1176the same CPU).  As we have already seen, the operational model allows
1177W's value to be forwarded to R in such cases, meaning that R may well
1178execute before W does.
1179
1180It's important to understand that neither coe nor fre is included in
1181hb, despite their similarities to rfe.  For example, suppose we have
1182W ->coe W'.  This means that W and W' are stores to the same location,
1183they execute on different CPUs, and W comes before W' in the coherence
1184order (i.e., W' overwrites W).  Nevertheless, it is possible for W' to
1185execute before W, because the decision as to which store overwrites
1186the other is made later by the memory subsystem.  When the stores are
1187nearly simultaneous, either one can come out on top.  Similarly,
1188R ->fre W means that W overwrites the value which R reads, but it
1189doesn't mean that W has to execute after R.  All that's necessary is
1190for the memory subsystem not to propagate W to R's CPU until after R
1191has executed, which is possible if W executes shortly before R.
1192
1193The third relation included in hb is like ppo, in that it only links
1194events that are on the same CPU.  However it is more difficult to
1195explain, because it arises only indirectly from the requirement of
1196cache coherence.  The relation is called prop, and it links two events
1197on CPU C in situations where a store from some other CPU comes after
1198the first event in the coherence order and propagates to C before the
1199second event executes.
1200
1201This is best explained with some examples.  The simplest case looks
1202like this:
1203
1204	int x;
1205
1206	P0()
1207	{
1208		int r1;
1209
1210		WRITE_ONCE(x, 1);
1211		r1 = READ_ONCE(x);
1212	}
1213
1214	P1()
1215	{
1216		WRITE_ONCE(x, 8);
1217	}
1218
1219If r1 = 8 at the end then P0's accesses must have executed in program
1220order.  We can deduce this from the operational model; if P0's load
1221had executed before its store then the value of the store would have
1222been forwarded to the load, so r1 would have ended up equal to 1, not
12238.  In this case there is a prop link from P0's write event to its read
1224event, because P1's store came after P0's store in x's coherence
1225order, and P1's store propagated to P0 before P0's load executed.
1226
1227An equally simple case involves two loads of the same location that
1228read from different stores:
1229
1230	int x = 0;
1231
1232	P0()
1233	{
1234		int r1, r2;
1235
1236		r1 = READ_ONCE(x);
1237		r2 = READ_ONCE(x);
1238	}
1239
1240	P1()
1241	{
1242		WRITE_ONCE(x, 9);
1243	}
1244
1245If r1 = 0 and r2 = 9 at the end then P0's accesses must have executed
1246in program order.  If the second load had executed before the first
1247then the x = 9 store must have been propagated to P0 before the first
1248load executed, and so r1 would have been 9 rather than 0.  In this
1249case there is a prop link from P0's first read event to its second,
1250because P1's store overwrote the value read by P0's first load, and
1251P1's store propagated to P0 before P0's second load executed.
1252
1253Less trivial examples of prop all involve fences.  Unlike the simple
1254examples above, they can require that some instructions are executed
1255out of program order.  This next one should look familiar:
1256
1257	int buf = 0, flag = 0;
1258
1259	P0()
1260	{
1261		WRITE_ONCE(buf, 1);
1262		smp_wmb();
1263		WRITE_ONCE(flag, 1);
1264	}
1265
1266	P1()
1267	{
1268		int r1;
1269		int r2;
1270
1271		r1 = READ_ONCE(flag);
1272		r2 = READ_ONCE(buf);
1273	}
1274
1275This is the MP pattern again, with an smp_wmb() fence between the two
1276stores.  If r1 = 1 and r2 = 0 at the end then there is a prop link
1277from P1's second load to its first (backwards!).  The reason is
1278similar to the previous examples: The value P1 loads from buf gets
1279overwritten by P0's store to buf, the fence guarantees that the store
1280to buf will propagate to P1 before the store to flag does, and the
1281store to flag propagates to P1 before P1 reads flag.
1282
1283The prop link says that in order to obtain the r1 = 1, r2 = 0 result,
1284P1 must execute its second load before the first.  Indeed, if the load
1285from flag were executed first, then the buf = 1 store would already
1286have propagated to P1 by the time P1's load from buf executed, so r2
1287would have been 1 at the end, not 0.  (The reasoning holds even for
1288Alpha, although the details are more complicated and we will not go
1289into them.)
1290
1291But what if we put an smp_rmb() fence between P1's loads?  The fence
1292would force the two loads to be executed in program order, and it
1293would generate a cycle in the hb relation: The fence would create a ppo
1294link (hence an hb link) from the first load to the second, and the
1295prop relation would give an hb link from the second load to the first.
1296Since an instruction can't execute before itself, we are forced to
1297conclude that if an smp_rmb() fence is added, the r1 = 1, r2 = 0
1298outcome is impossible -- as it should be.
1299
1300The formal definition of the prop relation involves a coe or fre link,
1301followed by an arbitrary number of cumul-fence links, ending with an
1302rfe link.  You can concoct more exotic examples, containing more than
1303one fence, although this quickly leads to diminishing returns in terms
1304of complexity.  For instance, here's an example containing a coe link
1305followed by two fences and an rfe link, utilizing the fact that
1306release fences are A-cumulative:
1307
1308	int x, y, z;
1309
1310	P0()
1311	{
1312		int r0;
1313
1314		WRITE_ONCE(x, 1);
1315		r0 = READ_ONCE(z);
1316	}
1317
1318	P1()
1319	{
1320		WRITE_ONCE(x, 2);
1321		smp_wmb();
1322		WRITE_ONCE(y, 1);
1323	}
1324
1325	P2()
1326	{
1327		int r2;
1328
1329		r2 = READ_ONCE(y);
1330		smp_store_release(&z, 1);
1331	}
1332
1333If x = 2, r0 = 1, and r2 = 1 after this code runs then there is a prop
1334link from P0's store to its load.  This is because P0's store gets
1335overwritten by P1's store since x = 2 at the end (a coe link), the
1336smp_wmb() ensures that P1's store to x propagates to P2 before the
1337store to y does (the first fence), the store to y propagates to P2
1338before P2's load and store execute, P2's smp_store_release()
1339guarantees that the stores to x and y both propagate to P0 before the
1340store to z does (the second fence), and P0's load executes after the
1341store to z has propagated to P0 (an rfe link).
1342
1343In summary, the fact that the hb relation links memory access events
1344in the order they execute means that it must not have cycles.  This
1345requirement is the content of the LKMM's "happens-before" axiom.
1346
1347The LKMM defines yet another relation connected to times of
1348instruction execution, but it is not included in hb.  It relies on the
1349particular properties of strong fences, which we cover in the next
1350section.
1351
1352
1353THE PROPAGATES-BEFORE RELATION: pb
1354----------------------------------
1355
1356The propagates-before (pb) relation capitalizes on the special
1357features of strong fences.  It links two events E and F whenever some
1358store is coherence-later than E and propagates to every CPU and to RAM
1359before F executes.  The formal definition requires that E be linked to
1360F via a coe or fre link, an arbitrary number of cumul-fences, an
1361optional rfe link, a strong fence, and an arbitrary number of hb
1362links.  Let's see how this definition works out.
1363
1364Consider first the case where E is a store (implying that the sequence
1365of links begins with coe).  Then there are events W, X, Y, and Z such
1366that:
1367
1368	E ->coe W ->cumul-fence* X ->rfe? Y ->strong-fence Z ->hb* F,
1369
1370where the * suffix indicates an arbitrary number of links of the
1371specified type, and the ? suffix indicates the link is optional (Y may
1372be equal to X).  Because of the cumul-fence links, we know that W will
1373propagate to Y's CPU before X does, hence before Y executes and hence
1374before the strong fence executes.  Because this fence is strong, we
1375know that W will propagate to every CPU and to RAM before Z executes.
1376And because of the hb links, we know that Z will execute before F.
1377Thus W, which comes later than E in the coherence order, will
1378propagate to every CPU and to RAM before F executes.
1379
1380The case where E is a load is exactly the same, except that the first
1381link in the sequence is fre instead of coe.
1382
1383The existence of a pb link from E to F implies that E must execute
1384before F.  To see why, suppose that F executed first.  Then W would
1385have propagated to E's CPU before E executed.  If E was a store, the
1386memory subsystem would then be forced to make E come after W in the
1387coherence order, contradicting the fact that E ->coe W.  If E was a
1388load, the memory subsystem would then be forced to satisfy E's read
1389request with the value stored by W or an even later store,
1390contradicting the fact that E ->fre W.
1391
1392A good example illustrating how pb works is the SB pattern with strong
1393fences:
1394
1395	int x = 0, y = 0;
1396
1397	P0()
1398	{
1399		int r0;
1400
1401		WRITE_ONCE(x, 1);
1402		smp_mb();
1403		r0 = READ_ONCE(y);
1404	}
1405
1406	P1()
1407	{
1408		int r1;
1409
1410		WRITE_ONCE(y, 1);
1411		smp_mb();
1412		r1 = READ_ONCE(x);
1413	}
1414
1415If r0 = 0 at the end then there is a pb link from P0's load to P1's
1416load: an fre link from P0's load to P1's store (which overwrites the
1417value read by P0), and a strong fence between P1's store and its load.
1418In this example, the sequences of cumul-fence and hb links are empty.
1419Note that this pb link is not included in hb as an instance of prop,
1420because it does not start and end on the same CPU.
1421
1422Similarly, if r1 = 0 at the end then there is a pb link from P1's load
1423to P0's.  This means that if both r1 and r2 were 0 there would be a
1424cycle in pb, which is not possible since an instruction cannot execute
1425before itself.  Thus, adding smp_mb() fences to the SB pattern
1426prevents the r0 = 0, r1 = 0 outcome.
1427
1428In summary, the fact that the pb relation links events in the order
1429they execute means that it cannot have cycles.  This requirement is
1430the content of the LKMM's "propagation" axiom.
1431
1432
1433RCU RELATIONS: rcu-link, rcu-gp, rcu-rscsi, rcu-fence, and rb
1434-------------------------------------------------------------
1435
1436RCU (Read-Copy-Update) is a powerful synchronization mechanism.  It
1437rests on two concepts: grace periods and read-side critical sections.
1438
1439A grace period is the span of time occupied by a call to
1440synchronize_rcu().  A read-side critical section (or just critical
1441section, for short) is a region of code delimited by rcu_read_lock()
1442at the start and rcu_read_unlock() at the end.  Critical sections can
1443be nested, although we won't make use of this fact.
1444
1445As far as memory models are concerned, RCU's main feature is its
1446Grace-Period Guarantee, which states that a critical section can never
1447span a full grace period.  In more detail, the Guarantee says:
1448
1449	For any critical section C and any grace period G, at least
1450	one of the following statements must hold:
1451
1452(1)	C ends before G does, and in addition, every store that
1453	propagates to C's CPU before the end of C must propagate to
1454	every CPU before G ends.
1455
1456(2)	G starts before C does, and in addition, every store that
1457	propagates to G's CPU before the start of G must propagate
1458	to every CPU before C starts.
1459
1460In particular, it is not possible for a critical section to both start
1461before and end after a grace period.
1462
1463Here is a simple example of RCU in action:
1464
1465	int x, y;
1466
1467	P0()
1468	{
1469		rcu_read_lock();
1470		WRITE_ONCE(x, 1);
1471		WRITE_ONCE(y, 1);
1472		rcu_read_unlock();
1473	}
1474
1475	P1()
1476	{
1477		int r1, r2;
1478
1479		r1 = READ_ONCE(x);
1480		synchronize_rcu();
1481		r2 = READ_ONCE(y);
1482	}
1483
1484The Grace Period Guarantee tells us that when this code runs, it will
1485never end with r1 = 1 and r2 = 0.  The reasoning is as follows.  r1 = 1
1486means that P0's store to x propagated to P1 before P1 called
1487synchronize_rcu(), so P0's critical section must have started before
1488P1's grace period, contrary to part (2) of the Guarantee.  On the
1489other hand, r2 = 0 means that P0's store to y, which occurs before the
1490end of the critical section, did not propagate to P1 before the end of
1491the grace period, contrary to part (1).  Together the results violate
1492the Guarantee.
1493
1494In the kernel's implementations of RCU, the requirements for stores
1495to propagate to every CPU are fulfilled by placing strong fences at
1496suitable places in the RCU-related code.  Thus, if a critical section
1497starts before a grace period does then the critical section's CPU will
1498execute an smp_mb() fence after the end of the critical section and
1499some time before the grace period's synchronize_rcu() call returns.
1500And if a critical section ends after a grace period does then the
1501synchronize_rcu() routine will execute an smp_mb() fence at its start
1502and some time before the critical section's opening rcu_read_lock()
1503executes.
1504
1505What exactly do we mean by saying that a critical section "starts
1506before" or "ends after" a grace period?  Some aspects of the meaning
1507are pretty obvious, as in the example above, but the details aren't
1508entirely clear.  The LKMM formalizes this notion by means of the
1509rcu-link relation.  rcu-link encompasses a very general notion of
1510"before": If E and F are RCU fence events (i.e., rcu_read_lock(),
1511rcu_read_unlock(), or synchronize_rcu()) then among other things,
1512E ->rcu-link F includes cases where E is po-before some memory-access
1513event X, F is po-after some memory-access event Y, and we have any of
1514X ->rfe Y, X ->co Y, or X ->fr Y.
1515
1516The formal definition of the rcu-link relation is more than a little
1517obscure, and we won't give it here.  It is closely related to the pb
1518relation, and the details don't matter unless you want to comb through
1519a somewhat lengthy formal proof.  Pretty much all you need to know
1520about rcu-link is the information in the preceding paragraph.
1521
1522The LKMM also defines the rcu-gp and rcu-rscsi relations.  They bring
1523grace periods and read-side critical sections into the picture, in the
1524following way:
1525
1526	E ->rcu-gp F means that E and F are in fact the same event,
1527	and that event is a synchronize_rcu() fence (i.e., a grace
1528	period).
1529
1530	E ->rcu-rscsi F means that E and F are the rcu_read_unlock()
1531	and rcu_read_lock() fence events delimiting some read-side
1532	critical section.  (The 'i' at the end of the name emphasizes
1533	that this relation is "inverted": It links the end of the
1534	critical section to the start.)
1535
1536If we think of the rcu-link relation as standing for an extended
1537"before", then X ->rcu-gp Y ->rcu-link Z roughly says that X is a
1538grace period which ends before Z begins.  (In fact it covers more than
1539this, because it also includes cases where some store propagates to
1540Z's CPU before Z begins but doesn't propagate to some other CPU until
1541after X ends.)  Similarly, X ->rcu-rscsi Y ->rcu-link Z says that X is
1542the end of a critical section which starts before Z begins.
1543
1544The LKMM goes on to define the rcu-fence relation as a sequence of
1545rcu-gp and rcu-rscsi links separated by rcu-link links, in which the
1546number of rcu-gp links is >= the number of rcu-rscsi links.  For
1547example:
1548
1549	X ->rcu-gp Y ->rcu-link Z ->rcu-rscsi T ->rcu-link U ->rcu-gp V
1550
1551would imply that X ->rcu-fence V, because this sequence contains two
1552rcu-gp links and one rcu-rscsi link.  (It also implies that
1553X ->rcu-fence T and Z ->rcu-fence V.)  On the other hand:
1554
1555	X ->rcu-rscsi Y ->rcu-link Z ->rcu-rscsi T ->rcu-link U ->rcu-gp V
1556
1557does not imply X ->rcu-fence V, because the sequence contains only
1558one rcu-gp link but two rcu-rscsi links.
1559
1560The rcu-fence relation is important because the Grace Period Guarantee
1561means that rcu-fence acts kind of like a strong fence.  In particular,
1562E ->rcu-fence F implies not only that E begins before F ends, but also
1563that any write po-before E will propagate to every CPU before any
1564instruction po-after F can execute.  (However, it does not imply that
1565E must execute before F; in fact, each synchronize_rcu() fence event
1566is linked to itself by rcu-fence as a degenerate case.)
1567
1568To prove this in full generality requires some intellectual effort.
1569We'll consider just a very simple case:
1570
1571	G ->rcu-gp W ->rcu-link Z ->rcu-rscsi F.
1572
1573This formula means that G and W are the same event (a grace period),
1574and there are events X, Y and a read-side critical section C such that:
1575
1576	1. G = W is po-before or equal to X;
1577
1578	2. X comes "before" Y in some sense (including rfe, co and fr);
1579
1580	2. Y is po-before Z;
1581
1582	4. Z is the rcu_read_unlock() event marking the end of C;
1583
1584	5. F is the rcu_read_lock() event marking the start of C.
1585
1586From 1 - 4 we deduce that the grace period G ends before the critical
1587section C.  Then part (2) of the Grace Period Guarantee says not only
1588that G starts before C does, but also that any write which executes on
1589G's CPU before G starts must propagate to every CPU before C starts.
1590In particular, the write propagates to every CPU before F finishes
1591executing and hence before any instruction po-after F can execute.
1592This sort of reasoning can be extended to handle all the situations
1593covered by rcu-fence.
1594
1595Finally, the LKMM defines the RCU-before (rb) relation in terms of
1596rcu-fence.  This is done in essentially the same way as the pb
1597relation was defined in terms of strong-fence.  We will omit the
1598details; the end result is that E ->rb F implies E must execute
1599before F, just as E ->pb F does (and for much the same reasons).
1600
1601Putting this all together, the LKMM expresses the Grace Period
1602Guarantee by requiring that the rb relation does not contain a cycle.
1603Equivalently, this "rcu" axiom requires that there are no events E
1604and F with E ->rcu-link F ->rcu-fence E.  Or to put it a third way,
1605the axiom requires that there are no cycles consisting of rcu-gp and
1606rcu-rscsi alternating with rcu-link, where the number of rcu-gp links
1607is >= the number of rcu-rscsi links.
1608
1609Justifying the axiom isn't easy, but it is in fact a valid
1610formalization of the Grace Period Guarantee.  We won't attempt to go
1611through the detailed argument, but the following analysis gives a
1612taste of what is involved.  Suppose both parts of the Guarantee are
1613violated: A critical section starts before a grace period, and some
1614store propagates to the critical section's CPU before the end of the
1615critical section but doesn't propagate to some other CPU until after
1616the end of the grace period.
1617
1618Putting symbols to these ideas, let L and U be the rcu_read_lock() and
1619rcu_read_unlock() fence events delimiting the critical section in
1620question, and let S be the synchronize_rcu() fence event for the grace
1621period.  Saying that the critical section starts before S means there
1622are events Q and R where Q is po-after L (which marks the start of the
1623critical section), Q is "before" R in the sense used by the rcu-link
1624relation, and R is po-before the grace period S.  Thus we have:
1625
1626	L ->rcu-link S.
1627
1628Let W be the store mentioned above, let Y come before the end of the
1629critical section and witness that W propagates to the critical
1630section's CPU by reading from W, and let Z on some arbitrary CPU be a
1631witness that W has not propagated to that CPU, where Z happens after
1632some event X which is po-after S.  Symbolically, this amounts to:
1633
1634	S ->po X ->hb* Z ->fr W ->rf Y ->po U.
1635
1636The fr link from Z to W indicates that W has not propagated to Z's CPU
1637at the time that Z executes.  From this, it can be shown (see the
1638discussion of the rcu-link relation earlier) that S and U are related
1639by rcu-link:
1640
1641	S ->rcu-link U.
1642
1643Since S is a grace period we have S ->rcu-gp S, and since L and U are
1644the start and end of the critical section C we have U ->rcu-rscsi L.
1645From this we obtain:
1646
1647	S ->rcu-gp S ->rcu-link U ->rcu-rscsi L ->rcu-link S,
1648
1649a forbidden cycle.  Thus the "rcu" axiom rules out this violation of
1650the Grace Period Guarantee.
1651
1652For something a little more down-to-earth, let's see how the axiom
1653works out in practice.  Consider the RCU code example from above, this
1654time with statement labels added:
1655
1656	int x, y;
1657
1658	P0()
1659	{
1660		L: rcu_read_lock();
1661		X: WRITE_ONCE(x, 1);
1662		Y: WRITE_ONCE(y, 1);
1663		U: rcu_read_unlock();
1664	}
1665
1666	P1()
1667	{
1668		int r1, r2;
1669
1670		Z: r1 = READ_ONCE(x);
1671		S: synchronize_rcu();
1672		W: r2 = READ_ONCE(y);
1673	}
1674
1675
1676If r2 = 0 at the end then P0's store at Y overwrites the value that
1677P1's load at W reads from, so we have W ->fre Y.  Since S ->po W and
1678also Y ->po U, we get S ->rcu-link U.  In addition, S ->rcu-gp S
1679because S is a grace period.
1680
1681If r1 = 1 at the end then P1's load at Z reads from P0's store at X,
1682so we have X ->rfe Z.  Together with L ->po X and Z ->po S, this
1683yields L ->rcu-link S.  And since L and U are the start and end of a
1684critical section, we have U ->rcu-rscsi L.
1685
1686Then U ->rcu-rscsi L ->rcu-link S ->rcu-gp S ->rcu-link U is a
1687forbidden cycle, violating the "rcu" axiom.  Hence the outcome is not
1688allowed by the LKMM, as we would expect.
1689
1690For contrast, let's see what can happen in a more complicated example:
1691
1692	int x, y, z;
1693
1694	P0()
1695	{
1696		int r0;
1697
1698		L0: rcu_read_lock();
1699		    r0 = READ_ONCE(x);
1700		    WRITE_ONCE(y, 1);
1701		U0: rcu_read_unlock();
1702	}
1703
1704	P1()
1705	{
1706		int r1;
1707
1708		    r1 = READ_ONCE(y);
1709		S1: synchronize_rcu();
1710		    WRITE_ONCE(z, 1);
1711	}
1712
1713	P2()
1714	{
1715		int r2;
1716
1717		L2: rcu_read_lock();
1718		    r2 = READ_ONCE(z);
1719		    WRITE_ONCE(x, 1);
1720		U2: rcu_read_unlock();
1721	}
1722
1723If r0 = r1 = r2 = 1 at the end, then similar reasoning to before shows
1724that U0 ->rcu-rscsi L0 ->rcu-link S1 ->rcu-gp S1 ->rcu-link U2 ->rcu-rscsi
1725L2 ->rcu-link U0.  However this cycle is not forbidden, because the
1726sequence of relations contains fewer instances of rcu-gp (one) than of
1727rcu-rscsi (two).  Consequently the outcome is allowed by the LKMM.
1728The following instruction timing diagram shows how it might actually
1729occur:
1730
1731P0			P1			P2
1732--------------------	--------------------	--------------------
1733rcu_read_lock()
1734WRITE_ONCE(y, 1)
1735			r1 = READ_ONCE(y)
1736			synchronize_rcu() starts
1737			.			rcu_read_lock()
1738			.			WRITE_ONCE(x, 1)
1739r0 = READ_ONCE(x)	.
1740rcu_read_unlock()	.
1741			synchronize_rcu() ends
1742			WRITE_ONCE(z, 1)
1743						r2 = READ_ONCE(z)
1744						rcu_read_unlock()
1745
1746This requires P0 and P2 to execute their loads and stores out of
1747program order, but of course they are allowed to do so.  And as you
1748can see, the Grace Period Guarantee is not violated: The critical
1749section in P0 both starts before P1's grace period does and ends
1750before it does, and the critical section in P2 both starts after P1's
1751grace period does and ends after it does.
1752
1753Addendum: The LKMM now supports SRCU (Sleepable Read-Copy-Update) in
1754addition to normal RCU.  The ideas involved are much the same as
1755above, with new relations srcu-gp and srcu-rscsi added to represent
1756SRCU grace periods and read-side critical sections.  There is a
1757restriction on the srcu-gp and srcu-rscsi links that can appear in an
1758rcu-fence sequence (the srcu-rscsi links must be paired with srcu-gp
1759links having the same SRCU domain with proper nesting); the details
1760are relatively unimportant.
1761
1762
1763LOCKING
1764-------
1765
1766The LKMM includes locking.  In fact, there is special code for locking
1767in the formal model, added in order to make tools run faster.
1768However, this special code is intended to be more or less equivalent
1769to concepts we have already covered.  A spinlock_t variable is treated
1770the same as an int, and spin_lock(&s) is treated almost the same as:
1771
1772	while (cmpxchg_acquire(&s, 0, 1) != 0)
1773		cpu_relax();
1774
1775This waits until s is equal to 0 and then atomically sets it to 1,
1776and the read part of the cmpxchg operation acts as an acquire fence.
1777An alternate way to express the same thing would be:
1778
1779	r = xchg_acquire(&s, 1);
1780
1781along with a requirement that at the end, r = 0.  Similarly,
1782spin_trylock(&s) is treated almost the same as:
1783
1784	return !cmpxchg_acquire(&s, 0, 1);
1785
1786which atomically sets s to 1 if it is currently equal to 0 and returns
1787true if it succeeds (the read part of the cmpxchg operation acts as an
1788acquire fence only if the operation is successful).  spin_unlock(&s)
1789is treated almost the same as:
1790
1791	smp_store_release(&s, 0);
1792
1793The "almost" qualifiers above need some explanation.  In the LKMM, the
1794store-release in a spin_unlock() and the load-acquire which forms the
1795first half of the atomic rmw update in a spin_lock() or a successful
1796spin_trylock() -- we can call these things lock-releases and
1797lock-acquires -- have two properties beyond those of ordinary releases
1798and acquires.
1799
1800First, when a lock-acquire reads from a lock-release, the LKMM
1801requires that every instruction po-before the lock-release must
1802execute before any instruction po-after the lock-acquire.  This would
1803naturally hold if the release and acquire operations were on different
1804CPUs, but the LKMM says it holds even when they are on the same CPU.
1805For example:
1806
1807	int x, y;
1808	spinlock_t s;
1809
1810	P0()
1811	{
1812		int r1, r2;
1813
1814		spin_lock(&s);
1815		r1 = READ_ONCE(x);
1816		spin_unlock(&s);
1817		spin_lock(&s);
1818		r2 = READ_ONCE(y);
1819		spin_unlock(&s);
1820	}
1821
1822	P1()
1823	{
1824		WRITE_ONCE(y, 1);
1825		smp_wmb();
1826		WRITE_ONCE(x, 1);
1827	}
1828
1829Here the second spin_lock() reads from the first spin_unlock(), and
1830therefore the load of x must execute before the load of y.  Thus we
1831cannot have r1 = 1 and r2 = 0 at the end (this is an instance of the
1832MP pattern).
1833
1834This requirement does not apply to ordinary release and acquire
1835fences, only to lock-related operations.  For instance, suppose P0()
1836in the example had been written as:
1837
1838	P0()
1839	{
1840		int r1, r2, r3;
1841
1842		r1 = READ_ONCE(x);
1843		smp_store_release(&s, 1);
1844		r3 = smp_load_acquire(&s);
1845		r2 = READ_ONCE(y);
1846	}
1847
1848Then the CPU would be allowed to forward the s = 1 value from the
1849smp_store_release() to the smp_load_acquire(), executing the
1850instructions in the following order:
1851
1852		r3 = smp_load_acquire(&s);	// Obtains r3 = 1
1853		r2 = READ_ONCE(y);
1854		r1 = READ_ONCE(x);
1855		smp_store_release(&s, 1);	// Value is forwarded
1856
1857and thus it could load y before x, obtaining r2 = 0 and r1 = 1.
1858
1859Second, when a lock-acquire reads from a lock-release, and some other
1860stores W and W' occur po-before the lock-release and po-after the
1861lock-acquire respectively, the LKMM requires that W must propagate to
1862each CPU before W' does.  For example, consider:
1863
1864	int x, y;
1865	spinlock_t x;
1866
1867	P0()
1868	{
1869		spin_lock(&s);
1870		WRITE_ONCE(x, 1);
1871		spin_unlock(&s);
1872	}
1873
1874	P1()
1875	{
1876		int r1;
1877
1878		spin_lock(&s);
1879		r1 = READ_ONCE(x);
1880		WRITE_ONCE(y, 1);
1881		spin_unlock(&s);
1882	}
1883
1884	P2()
1885	{
1886		int r2, r3;
1887
1888		r2 = READ_ONCE(y);
1889		smp_rmb();
1890		r3 = READ_ONCE(x);
1891	}
1892
1893If r1 = 1 at the end then the spin_lock() in P1 must have read from
1894the spin_unlock() in P0.  Hence the store to x must propagate to P2
1895before the store to y does, so we cannot have r2 = 1 and r3 = 0.
1896
1897These two special requirements for lock-release and lock-acquire do
1898not arise from the operational model.  Nevertheless, kernel developers
1899have come to expect and rely on them because they do hold on all
1900architectures supported by the Linux kernel, albeit for various
1901differing reasons.
1902
1903
1904ODDS AND ENDS
1905-------------
1906
1907This section covers material that didn't quite fit anywhere in the
1908earlier sections.
1909
1910The descriptions in this document don't always match the formal
1911version of the LKMM exactly.  For example, the actual formal
1912definition of the prop relation makes the initial coe or fre part
1913optional, and it doesn't require the events linked by the relation to
1914be on the same CPU.  These differences are very unimportant; indeed,
1915instances where the coe/fre part of prop is missing are of no interest
1916because all the other parts (fences and rfe) are already included in
1917hb anyway, and where the formal model adds prop into hb, it includes
1918an explicit requirement that the events being linked are on the same
1919CPU.
1920
1921Another minor difference has to do with events that are both memory
1922accesses and fences, such as those corresponding to smp_load_acquire()
1923calls.  In the formal model, these events aren't actually both reads
1924and fences; rather, they are read events with an annotation marking
1925them as acquires.  (Or write events annotated as releases, in the case
1926smp_store_release().)  The final effect is the same.
1927
1928Although we didn't mention it above, the instruction execution
1929ordering provided by the smp_rmb() fence doesn't apply to read events
1930that are part of a non-value-returning atomic update.  For instance,
1931given:
1932
1933	atomic_inc(&x);
1934	smp_rmb();
1935	r1 = READ_ONCE(y);
1936
1937it is not guaranteed that the load from y will execute after the
1938update to x.  This is because the ARMv8 architecture allows
1939non-value-returning atomic operations effectively to be executed off
1940the CPU.  Basically, the CPU tells the memory subsystem to increment
1941x, and then the increment is carried out by the memory hardware with
1942no further involvement from the CPU.  Since the CPU doesn't ever read
1943the value of x, there is nothing for the smp_rmb() fence to act on.
1944
1945The LKMM defines a few extra synchronization operations in terms of
1946things we have already covered.  In particular, rcu_dereference() is
1947treated as READ_ONCE() and rcu_assign_pointer() is treated as
1948smp_store_release() -- which is basically how the Linux kernel treats
1949them.
1950
1951There are a few oddball fences which need special treatment:
1952smp_mb__before_atomic(), smp_mb__after_atomic(), and
1953smp_mb__after_spinlock().  The LKMM uses fence events with special
1954annotations for them; they act as strong fences just like smp_mb()
1955except for the sets of events that they order.  Instead of ordering
1956all po-earlier events against all po-later events, as smp_mb() does,
1957they behave as follows:
1958
1959	smp_mb__before_atomic() orders all po-earlier events against
1960	po-later atomic updates and the events following them;
1961
1962	smp_mb__after_atomic() orders po-earlier atomic updates and
1963	the events preceding them against all po-later events;
1964
1965	smp_mb_after_spinlock() orders po-earlier lock acquisition
1966	events and the events preceding them against all po-later
1967	events.
1968
1969Interestingly, RCU and locking each introduce the possibility of
1970deadlock.  When faced with code sequences such as:
1971
1972	spin_lock(&s);
1973	spin_lock(&s);
1974	spin_unlock(&s);
1975	spin_unlock(&s);
1976
1977or:
1978
1979	rcu_read_lock();
1980	synchronize_rcu();
1981	rcu_read_unlock();
1982
1983what does the LKMM have to say?  Answer: It says there are no allowed
1984executions at all, which makes sense.  But this can also lead to
1985misleading results, because if a piece of code has multiple possible
1986executions, some of which deadlock, the model will report only on the
1987non-deadlocking executions.  For example:
1988
1989	int x, y;
1990
1991	P0()
1992	{
1993		int r0;
1994
1995		WRITE_ONCE(x, 1);
1996		r0 = READ_ONCE(y);
1997	}
1998
1999	P1()
2000	{
2001		rcu_read_lock();
2002		if (READ_ONCE(x) > 0) {
2003			WRITE_ONCE(y, 36);
2004			synchronize_rcu();
2005		}
2006		rcu_read_unlock();
2007	}
2008
2009Is it possible to end up with r0 = 36 at the end?  The LKMM will tell
2010you it is not, but the model won't mention that this is because P1
2011will self-deadlock in the executions where it stores 36 in y.
2012