Blame view

Documentation/memory-barriers.txt 113 KB
108b42b4b   David Howells   [PATCH] Document ...
1
2
3
4
5
  			 ============================
  			 LINUX KERNEL MEMORY BARRIERS
  			 ============================
  
  By: David Howells <dhowells@redhat.com>
90fddabf5   David Howells   Document Linux's ...
6
      Paul E. McKenney <paulmck@linux.vnet.ibm.com>
e7720af5f   Peter Zijlstra   locking/Documenta...
7
8
      Will Deacon <will.deacon@arm.com>
      Peter Zijlstra <peterz@infradead.org>
108b42b4b   David Howells   [PATCH] Document ...
9

e7720af5f   Peter Zijlstra   locking/Documenta...
10
11
12
13
14
15
16
17
18
19
20
  ==========
  DISCLAIMER
  ==========
  
  This document is not a specification; it is intentionally (for the sake of
  brevity) and unintentionally (due to being human) incomplete. This document is
  meant as a guide to using the various memory barriers provided by Linux, but
  in case of any doubt (and there are many) please ask.
  
  To repeat, this document is not a specification of what Linux expects from
  hardware.
8d4840e84   David Howells   locking/Documenta...
21
22
23
24
25
26
27
28
  The purpose of this document is twofold:
  
   (1) to specify the minimum functionality that one can rely on for any
       particular barrier, and
  
   (2) to provide a guide as to how to use the barriers that are available.
  
  Note that an architecture can provide more than the minimum requirement
35bdc72a3   Stan Drozd   docs: Fix typo in...
29
  for any particular barrier, but if the architecture provides less than
8d4840e84   David Howells   locking/Documenta...
30
31
32
33
34
  that, that architecture is incorrect.
  
  Note also that it is possible that a barrier may be a no-op for an
  architecture because the way that arch works renders an explicit barrier
  unnecessary in that case.
e7720af5f   Peter Zijlstra   locking/Documenta...
35
36
37
  ========
  CONTENTS
  ========
108b42b4b   David Howells   [PATCH] Document ...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  
   (*) Abstract memory access model.
  
       - Device operations.
       - Guarantees.
  
   (*) What are memory barriers?
  
       - Varieties of memory barrier.
       - What may not be assumed about memory barriers?
       - Data dependency barriers.
       - Control dependencies.
       - SMP barrier pairing.
       - Examples of memory barrier sequences.
670bd95e0   David Howells   [PATCH] Further a...
52
       - Read memory barriers vs load speculation.
241e6663b   Paul E. McKenney   smp: Document tra...
53
       - Transitivity
108b42b4b   David Howells   [PATCH] Document ...
54
55
56
57
  
   (*) Explicit kernel barriers.
  
       - Compiler barrier.
81fc63235   Jarek Poplawski   Documentation/mem...
58
       - CPU memory barriers.
108b42b4b   David Howells   [PATCH] Document ...
59
60
61
       - MMIO write barrier.
  
   (*) Implicit kernel memory barriers.
166bda712   SeongJae Park   locking/Documenta...
62
       - Lock acquisition functions.
108b42b4b   David Howells   [PATCH] Document ...
63
       - Interrupt disabling functions.
50fa610a3   David Howells   sched: Document m...
64
       - Sleep and wake-up functions.
108b42b4b   David Howells   [PATCH] Document ...
65
       - Miscellaneous functions.
166bda712   SeongJae Park   locking/Documenta...
66
   (*) Inter-CPU acquiring barrier effects.
108b42b4b   David Howells   [PATCH] Document ...
67

166bda712   SeongJae Park   locking/Documenta...
68
69
       - Acquires vs memory accesses.
       - Acquires vs I/O accesses.
108b42b4b   David Howells   [PATCH] Document ...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  
   (*) Where are memory barriers needed?
  
       - Interprocessor interaction.
       - Atomic operations.
       - Accessing devices.
       - Interrupts.
  
   (*) Kernel I/O barrier effects.
  
   (*) Assumed minimum execution ordering model.
  
   (*) The effects of the cpu cache.
  
       - Cache coherency.
       - Cache coherency vs DMA.
       - Cache coherency vs MMIO.
  
   (*) The things CPUs get up to.
  
       - And then there's the Alpha.
01e1cd6de   SeongJae Park   locking/Documenta...
91
       - Virtual Machine Guests.
108b42b4b   David Howells   [PATCH] Document ...
92

90fddabf5   David Howells   Document Linux's ...
93
94
95
   (*) Example uses.
  
       - Circular buffers.
108b42b4b   David Howells   [PATCH] Document ...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
   (*) References.
  
  
  ============================
  ABSTRACT MEMORY ACCESS MODEL
  ============================
  
  Consider the following abstract model of the system:
  
  		            :                :
  		            :                :
  		            :                :
  		+-------+   :   +--------+   :   +-------+
  		|       |   :   |        |   :   |       |
  		|       |   :   |        |   :   |       |
  		| CPU 1 |<----->| Memory |<----->| CPU 2 |
  		|       |   :   |        |   :   |       |
  		|       |   :   |        |   :   |       |
  		+-------+   :   +--------+   :   +-------+
  		    ^       :       ^        :       ^
  		    |       :       |        :       |
  		    |       :       |        :       |
  		    |       :       v        :       |
  		    |       :   +--------+   :       |
  		    |       :   |        |   :       |
  		    |       :   |        |   :       |
  		    +---------->| Device |<----------+
  		            :   |        |   :
  		            :   |        |   :
  		            :   +--------+   :
  		            :                :
  
  Each CPU executes a program that generates memory access operations.  In the
  abstract CPU, memory operation ordering is very relaxed, and a CPU may actually
  perform the memory operations in any order it likes, provided program causality
  appears to be maintained.  Similarly, the compiler may also arrange the
  instructions it emits in any order it likes, provided it doesn't affect the
  apparent operation of the program.
  
  So in the above diagram, the effects of the memory operations performed by a
  CPU are perceived by the rest of the system as the operations cross the
  interface between the CPU and rest of the system (the dotted lines).
  
  
  For example, consider the following sequence of events:
  
  	CPU 1		CPU 2
  	===============	===============
  	{ A == 1; B == 2 }
615cc2c9c   Alexey Dobriyan   Documentation/mem...
145
146
  	A = 3;		x = B;
  	B = 4;		y = A;
108b42b4b   David Howells   [PATCH] Document ...
147
148
149
  
  The set of accesses as seen by the memory system in the middle can be arranged
  in 24 different combinations:
8ab8b3e18   Pranith Kumar   documentation: me...
150
151
152
153
154
155
156
  	STORE A=3,	STORE B=4,	y=LOAD A->3,	x=LOAD B->4
  	STORE A=3,	STORE B=4,	x=LOAD B->4,	y=LOAD A->3
  	STORE A=3,	y=LOAD A->3,	STORE B=4,	x=LOAD B->4
  	STORE A=3,	y=LOAD A->3,	x=LOAD B->2,	STORE B=4
  	STORE A=3,	x=LOAD B->2,	STORE B=4,	y=LOAD A->3
  	STORE A=3,	x=LOAD B->2,	y=LOAD A->3,	STORE B=4
  	STORE B=4,	STORE A=3,	y=LOAD A->3,	x=LOAD B->4
108b42b4b   David Howells   [PATCH] Document ...
157
158
159
160
  	STORE B=4, ...
  	...
  
  and can thus result in four different combinations of values:
8ab8b3e18   Pranith Kumar   documentation: me...
161
162
163
164
  	x == 2, y == 1
  	x == 2, y == 3
  	x == 4, y == 1
  	x == 4, y == 3
108b42b4b   David Howells   [PATCH] Document ...
165
166
167
168
169
170
171
172
173
174
175
  
  
  Furthermore, the stores committed by a CPU to the memory system may not be
  perceived by the loads made by another CPU in the same order as the stores were
  committed.
  
  
  As a further example, consider this sequence of events:
  
  	CPU 1		CPU 2
  	===============	===============
3dbf0913f   SeongJae Park   locking/Documenta...
176
  	{ A == 1, B == 2, C == 3, P == &A, Q == &C }
108b42b4b   David Howells   [PATCH] Document ...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  	B = 4;		Q = P;
  	P = &B		D = *Q;
  
  There is an obvious data dependency here, as the value loaded into D depends on
  the address retrieved from P by CPU 2.  At the end of the sequence, any of the
  following results are possible:
  
  	(Q == &A) and (D == 1)
  	(Q == &B) and (D == 2)
  	(Q == &B) and (D == 4)
  
  Note that CPU 2 will never try and load C into D because the CPU will load P
  into Q before issuing the load of *Q.
  
  
  DEVICE OPERATIONS
  -----------------
  
  Some devices present their control interfaces as collections of memory
  locations, but the order in which the control registers are accessed is very
  important.  For instance, imagine an ethernet card with a set of internal
  registers that are accessed through an address port register (A) and a data
  port register (D).  To read internal register 5, the following code might then
  be used:
  
  	*A = 5;
  	x = *D;
  
  but this might show up as either of the following two sequences:
  
  	STORE *A = 5, x = LOAD *D
  	x = LOAD *D, STORE *A = 5
  
  the second of which will almost certainly result in a malfunction, since it set
  the address _after_ attempting to read the register.
  
  
  GUARANTEES
  ----------
  
  There are some minimal guarantees that may be expected of a CPU:
  
   (*) On any given CPU, dependent memory accesses will be issued in order, with
       respect to itself.  This means that for:
f84cfbb0f   Chris Metcalf   Documentation/mem...
221
  	Q = READ_ONCE(P); smp_read_barrier_depends(); D = READ_ONCE(*Q);
108b42b4b   David Howells   [PATCH] Document ...
222
223
224
225
  
       the CPU will issue the following memory operations:
  
  	Q = LOAD P, D = LOAD *Q
2ecf81012   Paul E. McKenney   Documentation/mem...
226
       and always in that order.  On most systems, smp_read_barrier_depends()
9af194cef   Paul E. McKenney   documentation: Re...
227
       does nothing, but it is required for DEC Alpha.  The READ_ONCE()
f84cfbb0f   Chris Metcalf   Documentation/mem...
228
229
230
       is required to prevent compiler mischief.  Please note that you
       should normally use something like rcu_dereference() instead of
       open-coding smp_read_barrier_depends().
108b42b4b   David Howells   [PATCH] Document ...
231
232
233
  
   (*) Overlapping loads and stores within a particular CPU will appear to be
       ordered within that CPU.  This means that for:
9af194cef   Paul E. McKenney   documentation: Re...
234
  	a = READ_ONCE(*X); WRITE_ONCE(*X, b);
108b42b4b   David Howells   [PATCH] Document ...
235
236
237
238
239
240
  
       the CPU will only issue the following sequence of memory operations:
  
  	a = LOAD *X, STORE *X = b
  
       And for:
9af194cef   Paul E. McKenney   documentation: Re...
241
  	WRITE_ONCE(*X, c); d = READ_ONCE(*X);
108b42b4b   David Howells   [PATCH] Document ...
242
243
244
245
  
       the CPU will only issue:
  
  	STORE *X = c, d = LOAD *X
fa00e7e15   Matt LaPlante   Fix typos in /Doc...
246
       (Loads and stores overlap if they are targeted at overlapping pieces of
108b42b4b   David Howells   [PATCH] Document ...
247
248
249
       memory).
  
  And there are a number of things that _must_ or _must_not_ be assumed:
9af194cef   Paul E. McKenney   documentation: Re...
250
251
252
253
   (*) It _must_not_ be assumed that the compiler will do what you want
       with memory references that are not protected by READ_ONCE() and
       WRITE_ONCE().  Without them, the compiler is within its rights to
       do all sorts of "creative" transformations, which are covered in
895f55422   Paul E. McKenney   documentation: Fi...
254
       the COMPILER BARRIER section.
2ecf81012   Paul E. McKenney   Documentation/mem...
255

108b42b4b   David Howells   [PATCH] Document ...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
   (*) It _must_not_ be assumed that independent loads and stores will be issued
       in the order given.  This means that for:
  
  	X = *A; Y = *B; *D = Z;
  
       we may get any of the following sequences:
  
  	X = LOAD *A,  Y = LOAD *B,  STORE *D = Z
  	X = LOAD *A,  STORE *D = Z, Y = LOAD *B
  	Y = LOAD *B,  X = LOAD *A,  STORE *D = Z
  	Y = LOAD *B,  STORE *D = Z, X = LOAD *A
  	STORE *D = Z, X = LOAD *A,  Y = LOAD *B
  	STORE *D = Z, Y = LOAD *B,  X = LOAD *A
  
   (*) It _must_ be assumed that overlapping memory accesses may be merged or
       discarded.  This means that for:
  
  	X = *A; Y = *(A + 4);
  
       we may get any one of the following sequences:
  
  	X = LOAD *A; Y = LOAD *(A + 4);
  	Y = LOAD *(A + 4); X = LOAD *A;
  	{X, Y} = LOAD {*A, *(A + 4) };
  
       And for:
f191eec58   Paul E. McKenney   Documentation: Fi...
282
  	*A = X; *(A + 4) = Y;
108b42b4b   David Howells   [PATCH] Document ...
283

f191eec58   Paul E. McKenney   Documentation: Fi...
284
       we may get any of:
108b42b4b   David Howells   [PATCH] Document ...
285

f191eec58   Paul E. McKenney   Documentation: Fi...
286
287
288
  	STORE *A = X; STORE *(A + 4) = Y;
  	STORE *(A + 4) = Y; STORE *A = X;
  	STORE {*A, *(A + 4) } = {X, Y};
108b42b4b   David Howells   [PATCH] Document ...
289

432fbf3c6   Paul E. McKenney   documentation: Re...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  And there are anti-guarantees:
  
   (*) These guarantees do not apply to bitfields, because compilers often
       generate code to modify these using non-atomic read-modify-write
       sequences.  Do not attempt to use bitfields to synchronize parallel
       algorithms.
  
   (*) Even in cases where bitfields are protected by locks, all fields
       in a given bitfield must be protected by one lock.  If two fields
       in a given bitfield are protected by different locks, the compiler's
       non-atomic read-modify-write sequences can cause an update to one
       field to corrupt the value of an adjacent field.
  
   (*) These guarantees apply only to properly aligned and sized scalar
       variables.  "Properly sized" currently means variables that are
       the same size as "char", "short", "int" and "long".  "Properly
       aligned" means the natural alignment, thus no constraints for
       "char", two-byte alignment for "short", four-byte alignment for
       "int", and either four-byte or eight-byte alignment for "long",
       on 32-bit and 64-bit systems, respectively.  Note that these
       guarantees were introduced into the C11 standard, so beware when
       using older pre-C11 compilers (for example, gcc 4.6).  The portion
       of the standard containing this guarantee is Section 3.14, which
       defines "memory location" as follows:
  
       	memory location
  		either an object of scalar type, or a maximal sequence
  		of adjacent bit-fields all having nonzero width
  
  		NOTE 1: Two threads of execution can update and access
  		separate memory locations without interfering with
  		each other.
  
  		NOTE 2: A bit-field and an adjacent non-bit-field member
  		are in separate memory locations. The same applies
  		to two bit-fields, if one is declared inside a nested
  		structure declaration and the other is not, or if the two
  		are separated by a zero-length bit-field declaration,
  		or if they are separated by a non-bit-field member
  		declaration. It is not safe to concurrently update two
  		bit-fields in the same structure if all members declared
  		between them are also bit-fields, no matter what the
  		sizes of those intervening bit-fields happen to be.
108b42b4b   David Howells   [PATCH] Document ...
333
334
335
336
337
338
339
340
341
342
343
  
  =========================
  WHAT ARE MEMORY BARRIERS?
  =========================
  
  As can be seen above, independent memory operations are effectively performed
  in random order, but this can be a problem for CPU-CPU interaction and for I/O.
  What is required is some way of intervening to instruct the compiler and the
  CPU to restrict the order.
  
  Memory barriers are such interventions.  They impose a perceived partial
2b94895b9   David Howells   [PATCH] Another c...
344
345
346
  ordering over the memory operations on either side of the barrier.
  
  Such enforcement is important because the CPUs and other devices in a system
81fc63235   Jarek Poplawski   Documentation/mem...
347
  can use a variety of tricks to improve performance, including reordering,
2b94895b9   David Howells   [PATCH] Another c...
348
349
350
351
  deferral and combination of memory operations; speculative loads; speculative
  branch prediction and various types of caching.  Memory barriers are used to
  override or suppress these tricks, allowing the code to sanely control the
  interaction of multiple CPUs and/or devices.
108b42b4b   David Howells   [PATCH] Document ...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  
  
  VARIETIES OF MEMORY BARRIER
  ---------------------------
  
  Memory barriers come in four basic varieties:
  
   (1) Write (or store) memory barriers.
  
       A write memory barrier gives a guarantee that all the STORE operations
       specified before the barrier will appear to happen before all the STORE
       operations specified after the barrier with respect to the other
       components of the system.
  
       A write barrier is a partial ordering on stores only; it is not required
       to have any effect on loads.
6bc392741   David Howells   [PATCH] Correctio...
368
       A CPU can be viewed as committing a sequence of store operations to the
108b42b4b   David Howells   [PATCH] Document ...
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
       memory system as time progresses.  All stores before a write barrier will
       occur in the sequence _before_ all the stores after the write barrier.
  
       [!] Note that write barriers should normally be paired with read or data
       dependency barriers; see the "SMP barrier pairing" subsection.
  
  
   (2) Data dependency barriers.
  
       A data dependency barrier is a weaker form of read barrier.  In the case
       where two loads are performed such that the second depends on the result
       of the first (eg: the first load retrieves the address to which the second
       load will be directed), a data dependency barrier would be required to
       make sure that the target of the second load is updated before the address
       obtained by the first load is accessed.
  
       A data dependency barrier is a partial ordering on interdependent loads
       only; it is not required to have any effect on stores, independent loads
       or overlapping loads.
  
       As mentioned in (1), the other CPUs in the system can be viewed as
       committing sequences of stores to the memory system that the CPU being
       considered can then perceive.  A data dependency barrier issued by the CPU
       under consideration guarantees that for any load preceding it, if that
       load touches one of a sequence of stores from another CPU, then by the
       time the barrier completes, the effects of all the stores prior to that
       touched by the load will be perceptible to any loads issued after the data
       dependency barrier.
  
       See the "Examples of memory barrier sequences" subsection for diagrams
       showing the ordering constraints.
  
       [!] Note that the first load really has to have a _data_ dependency and
       not a control dependency.  If the address for the second load is dependent
       on the first load, but the dependency is through a conditional rather than
       actually loading the address itself, then it's a _control_ dependency and
       a full read barrier or better is required.  See the "Control dependencies"
       subsection for more information.
  
       [!] Note that data dependency barriers should normally be paired with
       write barriers; see the "SMP barrier pairing" subsection.
  
  
   (3) Read (or load) memory barriers.
  
       A read barrier is a data dependency barrier plus a guarantee that all the
       LOAD operations specified before the barrier will appear to happen before
       all the LOAD operations specified after the barrier with respect to the
       other components of the system.
  
       A read barrier is a partial ordering on loads only; it is not required to
       have any effect on stores.
  
       Read memory barriers imply data dependency barriers, and so can substitute
       for them.
  
       [!] Note that read barriers should normally be paired with write barriers;
       see the "SMP barrier pairing" subsection.
  
  
   (4) General memory barriers.
670bd95e0   David Howells   [PATCH] Further a...
430
431
432
433
434
435
       A general memory barrier gives a guarantee that all the LOAD and STORE
       operations specified before the barrier will appear to happen before all
       the LOAD and STORE operations specified after the barrier with respect to
       the other components of the system.
  
       A general memory barrier is a partial ordering over both loads and stores.
108b42b4b   David Howells   [PATCH] Document ...
436
437
438
439
440
441
  
       General memory barriers imply both read and write memory barriers, and so
       can substitute for either.
  
  
  And a couple of implicit varieties:
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
442
   (5) ACQUIRE operations.
108b42b4b   David Howells   [PATCH] Document ...
443
444
  
       This acts as a one-way permeable barrier.  It guarantees that all memory
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
445
446
       operations after the ACQUIRE operation will appear to happen after the
       ACQUIRE operation with respect to the other components of the system.
787df6383   Davidlohr Bueso   locking/Documenta...
447
448
449
       ACQUIRE operations include LOCK operations and both smp_load_acquire()
       and smp_cond_acquire() operations. The later builds the necessary ACQUIRE
       semantics from relying on a control dependency and smp_rmb().
108b42b4b   David Howells   [PATCH] Document ...
450

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
451
452
       Memory operations that occur before an ACQUIRE operation may appear to
       happen after it completes.
108b42b4b   David Howells   [PATCH] Document ...
453

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
454
455
       An ACQUIRE operation should almost always be paired with a RELEASE
       operation.
108b42b4b   David Howells   [PATCH] Document ...
456

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
457
   (6) RELEASE operations.
108b42b4b   David Howells   [PATCH] Document ...
458
459
  
       This also acts as a one-way permeable barrier.  It guarantees that all
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
460
461
462
463
       memory operations before the RELEASE operation will appear to happen
       before the RELEASE operation with respect to the other components of the
       system. RELEASE operations include UNLOCK operations and
       smp_store_release() operations.
108b42b4b   David Howells   [PATCH] Document ...
464

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
465
       Memory operations that occur after a RELEASE operation may appear to
108b42b4b   David Howells   [PATCH] Document ...
466
       happen before it completes.
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
467
468
469
470
471
472
473
474
475
       The use of ACQUIRE and RELEASE operations generally precludes the need
       for other sorts of memory barrier (but note the exceptions mentioned in
       the subsection "MMIO write barrier").  In addition, a RELEASE+ACQUIRE
       pair is -not- guaranteed to act as a full memory barrier.  However, after
       an ACQUIRE on a given variable, all memory accesses preceding any prior
       RELEASE on that same variable are guaranteed to be visible.  In other
       words, within a given variable's critical section, all accesses of all
       previous critical sections for that variable are guaranteed to have
       completed.
17eb88e06   Paul E. McKenney   Documentation/mem...
476

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
477
478
       This means that ACQUIRE acts as a minimal "acquire" operation and
       RELEASE acts as a minimal "release" operation.
108b42b4b   David Howells   [PATCH] Document ...
479

706eeb3e9   Peter Zijlstra   Documentation/loc...
480
481
482
483
484
  A subset of the atomic operations described in atomic_t.txt have ACQUIRE and
  RELEASE variants in addition to fully-ordered and relaxed (no barrier
  semantics) definitions.  For compound atomics performing both a load and a
  store, ACQUIRE semantics apply only to the load and RELEASE semantics apply
  only to the store portion of the operation.
108b42b4b   David Howells   [PATCH] Document ...
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  
  Memory barriers are only required where there's a possibility of interaction
  between two CPUs or between a CPU and a device.  If it can be guaranteed that
  there won't be any such interaction in any particular piece of code, then
  memory barriers are unnecessary in that piece of code.
  
  
  Note that these are the _minimum_ guarantees.  Different architectures may give
  more substantial guarantees, but they may _not_ be relied upon outside of arch
  specific code.
  
  
  WHAT MAY NOT BE ASSUMED ABOUT MEMORY BARRIERS?
  ----------------------------------------------
  
  There are certain things that the Linux kernel memory barriers do not guarantee:
  
   (*) There is no guarantee that any of the memory accesses specified before a
       memory barrier will be _complete_ by the completion of a memory barrier
       instruction; the barrier can be considered to draw a line in that CPU's
       access queue that accesses of the appropriate type may not cross.
  
   (*) There is no guarantee that issuing a memory barrier on one CPU will have
       any direct effect on another CPU or any other hardware in the system.  The
       indirect effect will be the order in which the second CPU sees the effects
       of the first CPU's accesses occur, but see the next point:
6bc392741   David Howells   [PATCH] Correctio...
511
   (*) There is no guarantee that a CPU will see the correct order of effects
108b42b4b   David Howells   [PATCH] Document ...
512
513
514
515
516
517
518
519
520
521
       from a second CPU's accesses, even _if_ the second CPU uses a memory
       barrier, unless the first CPU _also_ uses a matching memory barrier (see
       the subsection on "SMP Barrier Pairing").
  
   (*) There is no guarantee that some intervening piece of off-the-CPU
       hardware[*] will not reorder the memory accesses.  CPU cache coherency
       mechanisms should propagate the indirect effects of a memory barrier
       between CPUs, but might not do so in order.
  
  	[*] For information on bus mastering DMA and coherency please read:
4b5ff4692   Randy Dunlap   PCI: doc/pci: cre...
522
  	    Documentation/PCI/pci.txt
395cf9691   Paul Bolle   doc: fix broken r...
523
  	    Documentation/DMA-API-HOWTO.txt
108b42b4b   David Howells   [PATCH] Document ...
524
525
526
527
528
529
530
531
532
  	    Documentation/DMA-API.txt
  
  
  DATA DEPENDENCY BARRIERS
  ------------------------
  
  The usage requirements of data dependency barriers are a little subtle, and
  it's not always obvious that they're needed.  To illustrate, consider the
  following sequence of events:
2ecf81012   Paul E. McKenney   Documentation/mem...
533
534
  	CPU 1		      CPU 2
  	===============	      ===============
3dbf0913f   SeongJae Park   locking/Documenta...
535
  	{ A == 1, B == 2, C == 3, P == &A, Q == &C }
108b42b4b   David Howells   [PATCH] Document ...
536
537
  	B = 4;
  	<write barrier>
9af194cef   Paul E. McKenney   documentation: Re...
538
539
  	WRITE_ONCE(P, &B)
  			      Q = READ_ONCE(P);
2ecf81012   Paul E. McKenney   Documentation/mem...
540
  			      D = *Q;
108b42b4b   David Howells   [PATCH] Document ...
541
542
543
544
545
546
  
  There's a clear data dependency here, and it would seem that by the end of the
  sequence, Q must be either &A or &B, and that:
  
  	(Q == &A) implies (D == 1)
  	(Q == &B) implies (D == 4)
81fc63235   Jarek Poplawski   Documentation/mem...
547
  But!  CPU 2's perception of P may be updated _before_ its perception of B, thus
108b42b4b   David Howells   [PATCH] Document ...
548
549
550
551
552
553
554
  leading to the following situation:
  
  	(Q == &B) and (D == 2) ????
  
  Whilst this may seem like a failure of coherency or causality maintenance, it
  isn't, and this behaviour can be observed on certain real CPUs (such as the DEC
  Alpha).
2b94895b9   David Howells   [PATCH] Another c...
555
556
  To deal with this, a data dependency barrier or better must be inserted
  between the address load and the data load:
108b42b4b   David Howells   [PATCH] Document ...
557

2ecf81012   Paul E. McKenney   Documentation/mem...
558
559
  	CPU 1		      CPU 2
  	===============	      ===============
3dbf0913f   SeongJae Park   locking/Documenta...
560
  	{ A == 1, B == 2, C == 3, P == &A, Q == &C }
108b42b4b   David Howells   [PATCH] Document ...
561
562
  	B = 4;
  	<write barrier>
9af194cef   Paul E. McKenney   documentation: Re...
563
564
  	WRITE_ONCE(P, &B);
  			      Q = READ_ONCE(P);
2ecf81012   Paul E. McKenney   Documentation/mem...
565
566
  			      <data dependency barrier>
  			      D = *Q;
108b42b4b   David Howells   [PATCH] Document ...
567
568
569
  
  This enforces the occurrence of one of the two implications, and prevents the
  third possibility from arising.
66ce3a4dc   Paul E. McKenney   doc: Update memor...
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
  
  [!] Note that this extremely counterintuitive situation arises most easily on
  machines with split caches, so that, for example, one cache bank processes
  even-numbered cache lines and the other bank processes odd-numbered cache
  lines.  The pointer P might be stored in an odd-numbered cache line, and the
  variable B might be stored in an even-numbered cache line.  Then, if the
  even-numbered bank of the reading CPU's cache is extremely busy while the
  odd-numbered bank is idle, one can see the new value of the pointer P (&B),
  but the old value of the variable B (2).
  
  
  A data-dependency barrier is not required to order dependent writes
  because the CPUs that the Linux kernel supports don't do writes
  until they are certain (1) that the write will actually happen, (2)
  of the location of the write, and (3) of the value to be written.
  But please carefully read the "CONTROL DEPENDENCIES" section and the
  Documentation/RCU/rcu_dereference.txt file:  The compiler can and does
  break dependencies in a great many highly creative ways.
92a84dd21   Paul E. McKenney   documentation: Su...
588
589
590
591
592
593
594
595
  
  	CPU 1		      CPU 2
  	===============	      ===============
  	{ A == 1, B == 2, C = 3, P == &A, Q == &C }
  	B = 4;
  	<write barrier>
  	WRITE_ONCE(P, &B);
  			      Q = READ_ONCE(P);
66ce3a4dc   Paul E. McKenney   doc: Update memor...
596
  			      WRITE_ONCE(*Q, 5);
92a84dd21   Paul E. McKenney   documentation: Su...
597

66ce3a4dc   Paul E. McKenney   doc: Update memor...
598
599
600
  Therefore, no data-dependency barrier is required to order the read into
  Q with the store into *Q.  In other words, this outcome is prohibited,
  even without a data-dependency barrier:
92a84dd21   Paul E. McKenney   documentation: Su...
601

8b9e77155   SeongJae Park   locking/Documenta...
602
  	(Q == &B) && (B == 4)
92a84dd21   Paul E. McKenney   documentation: Su...
603
604
605
606
  
  Please note that this pattern should be rare.  After all, the whole point
  of dependency ordering is to -prevent- writes to the data structure, along
  with the expensive cache misses associated with those writes.  This pattern
66ce3a4dc   Paul E. McKenney   doc: Update memor...
607
608
  can be used to record rare error conditions and the like, and the CPUs'
  naturally occurring ordering prevents such records from being lost.
108b42b4b   David Howells   [PATCH] Document ...
609

2ecf81012   Paul E. McKenney   Documentation/mem...
610
611
612
613
614
  The data dependency barrier is very important to the RCU system,
  for example.  See rcu_assign_pointer() and rcu_dereference() in
  include/linux/rcupdate.h.  This permits the current target of an RCU'd
  pointer to be replaced with a new modified target, without the replacement
  target appearing to be incompletely initialised.
108b42b4b   David Howells   [PATCH] Document ...
615
616
617
618
619
620
  
  See also the subsection on "Cache Coherency" for a more thorough example.
  
  
  CONTROL DEPENDENCIES
  --------------------
c8241f855   Paul E. McKenney   doc: Update contr...
621
622
623
  Control dependencies can be a bit tricky because current compilers do
  not understand them.  The purpose of this section is to help you prevent
  the compiler's ignorance from breaking your code.
ff3828105   Paul E. McKenney   documentation: Cl...
624
625
626
  A load-load control dependency requires a full read memory barrier, not
  simply a data dependency barrier to make it work correctly.  Consider the
  following bit of code:
108b42b4b   David Howells   [PATCH] Document ...
627

9af194cef   Paul E. McKenney   documentation: Re...
628
  	q = READ_ONCE(a);
18c03c614   Peter Zijlstra   Documentation/mem...
629
630
  	if (q) {
  		<data dependency barrier>  /* BUG: No data dependency!!! */
9af194cef   Paul E. McKenney   documentation: Re...
631
  		p = READ_ONCE(b);
45c8a36a5   Paul E. McKenney   doc: Fix memory-b...
632
  	}
108b42b4b   David Howells   [PATCH] Document ...
633
634
  
  This will not have the desired effect because there is no actual data
2ecf81012   Paul E. McKenney   Documentation/mem...
635
636
637
638
  dependency, but rather a control dependency that the CPU may short-circuit
  by attempting to predict the outcome in advance, so that other CPUs see
  the load from b as having happened before the load from a.  In such a
  case what's actually required is:
108b42b4b   David Howells   [PATCH] Document ...
639

9af194cef   Paul E. McKenney   documentation: Re...
640
  	q = READ_ONCE(a);
18c03c614   Peter Zijlstra   Documentation/mem...
641
  	if (q) {
45c8a36a5   Paul E. McKenney   doc: Fix memory-b...
642
  		<read barrier>
9af194cef   Paul E. McKenney   documentation: Re...
643
  		p = READ_ONCE(b);
45c8a36a5   Paul E. McKenney   doc: Fix memory-b...
644
  	}
18c03c614   Peter Zijlstra   Documentation/mem...
645
646
  
  However, stores are not speculated.  This means that ordering -is- provided
ff3828105   Paul E. McKenney   documentation: Cl...
647
  for load-store control dependencies, as in the following example:
18c03c614   Peter Zijlstra   Documentation/mem...
648

105ff3cbf   Linus Torvalds   atomic: remove al...
649
  	q = READ_ONCE(a);
18c03c614   Peter Zijlstra   Documentation/mem...
650
  	if (q) {
c8241f855   Paul E. McKenney   doc: Update contr...
651
  		WRITE_ONCE(b, 1);
18c03c614   Peter Zijlstra   Documentation/mem...
652
  	}
c8241f855   Paul E. McKenney   doc: Update contr...
653
654
655
656
657
658
  Control dependencies pair normally with other types of barriers.
  That said, please note that neither READ_ONCE() nor WRITE_ONCE()
  are optional! Without the READ_ONCE(), the compiler might combine the
  load from 'a' with other loads from 'a'.  Without the WRITE_ONCE(),
  the compiler might combine the store to 'b' with other stores to 'b'.
  Either can result in highly counterintuitive effects on ordering.
18c03c614   Peter Zijlstra   Documentation/mem...
659
660
661
662
663
664
665
  
  Worse yet, if the compiler is able to prove (say) that the value of
  variable 'a' is always non-zero, it would be well within its rights
  to optimize the original example by eliminating the "if" statement
  as follows:
  
  	q = a;
c8241f855   Paul E. McKenney   doc: Update contr...
666
  	b = 1;  /* BUG: Compiler and CPU can both reorder!!! */
2456d2a61   Paul E. McKenney   memory-barriers: ...
667

105ff3cbf   Linus Torvalds   atomic: remove al...
668
  So don't leave out the READ_ONCE().
18c03c614   Peter Zijlstra   Documentation/mem...
669

2456d2a61   Paul E. McKenney   memory-barriers: ...
670
671
  It is tempting to try to enforce ordering on identical stores on both
  branches of the "if" statement as follows:
18c03c614   Peter Zijlstra   Documentation/mem...
672

105ff3cbf   Linus Torvalds   atomic: remove al...
673
  	q = READ_ONCE(a);
18c03c614   Peter Zijlstra   Documentation/mem...
674
  	if (q) {
9b2b3bf53   Paul E. McKenney   Documentation/mem...
675
  		barrier();
c8241f855   Paul E. McKenney   doc: Update contr...
676
  		WRITE_ONCE(b, 1);
18c03c614   Peter Zijlstra   Documentation/mem...
677
678
  		do_something();
  	} else {
9b2b3bf53   Paul E. McKenney   Documentation/mem...
679
  		barrier();
c8241f855   Paul E. McKenney   doc: Update contr...
680
  		WRITE_ONCE(b, 1);
18c03c614   Peter Zijlstra   Documentation/mem...
681
682
  		do_something_else();
  	}
2456d2a61   Paul E. McKenney   memory-barriers: ...
683
684
  Unfortunately, current compilers will transform this as follows at high
  optimization levels:
18c03c614   Peter Zijlstra   Documentation/mem...
685

105ff3cbf   Linus Torvalds   atomic: remove al...
686
  	q = READ_ONCE(a);
2456d2a61   Paul E. McKenney   memory-barriers: ...
687
  	barrier();
c8241f855   Paul E. McKenney   doc: Update contr...
688
  	WRITE_ONCE(b, 1);  /* BUG: No ordering vs. load from a!!! */
18c03c614   Peter Zijlstra   Documentation/mem...
689
  	if (q) {
c8241f855   Paul E. McKenney   doc: Update contr...
690
  		/* WRITE_ONCE(b, 1); -- moved up, BUG!!! */
18c03c614   Peter Zijlstra   Documentation/mem...
691
692
  		do_something();
  	} else {
c8241f855   Paul E. McKenney   doc: Update contr...
693
  		/* WRITE_ONCE(b, 1); -- moved up, BUG!!! */
18c03c614   Peter Zijlstra   Documentation/mem...
694
695
  		do_something_else();
  	}
2456d2a61   Paul E. McKenney   memory-barriers: ...
696
697
698
699
700
701
  Now there is no conditional between the load from 'a' and the store to
  'b', which means that the CPU is within its rights to reorder them:
  The conditional is absolutely required, and must be present in the
  assembly code even after all compiler optimizations have been applied.
  Therefore, if you need ordering in this example, you need explicit
  memory barriers, for example, smp_store_release():
18c03c614   Peter Zijlstra   Documentation/mem...
702

9af194cef   Paul E. McKenney   documentation: Re...
703
  	q = READ_ONCE(a);
2456d2a61   Paul E. McKenney   memory-barriers: ...
704
  	if (q) {
c8241f855   Paul E. McKenney   doc: Update contr...
705
  		smp_store_release(&b, 1);
18c03c614   Peter Zijlstra   Documentation/mem...
706
707
  		do_something();
  	} else {
c8241f855   Paul E. McKenney   doc: Update contr...
708
  		smp_store_release(&b, 1);
18c03c614   Peter Zijlstra   Documentation/mem...
709
710
  		do_something_else();
  	}
2456d2a61   Paul E. McKenney   memory-barriers: ...
711
712
  In contrast, without explicit memory barriers, two-legged-if control
  ordering is guaranteed only when the stores differ, for example:
105ff3cbf   Linus Torvalds   atomic: remove al...
713
  	q = READ_ONCE(a);
2456d2a61   Paul E. McKenney   memory-barriers: ...
714
  	if (q) {
c8241f855   Paul E. McKenney   doc: Update contr...
715
  		WRITE_ONCE(b, 1);
2456d2a61   Paul E. McKenney   memory-barriers: ...
716
717
  		do_something();
  	} else {
c8241f855   Paul E. McKenney   doc: Update contr...
718
  		WRITE_ONCE(b, 2);
2456d2a61   Paul E. McKenney   memory-barriers: ...
719
720
  		do_something_else();
  	}
105ff3cbf   Linus Torvalds   atomic: remove al...
721
722
  The initial READ_ONCE() is still required to prevent the compiler from
  proving the value of 'a'.
18c03c614   Peter Zijlstra   Documentation/mem...
723
724
725
726
  
  In addition, you need to be careful what you do with the local variable 'q',
  otherwise the compiler might be able to guess the value and again remove
  the needed conditional.  For example:
105ff3cbf   Linus Torvalds   atomic: remove al...
727
  	q = READ_ONCE(a);
18c03c614   Peter Zijlstra   Documentation/mem...
728
  	if (q % MAX) {
c8241f855   Paul E. McKenney   doc: Update contr...
729
  		WRITE_ONCE(b, 1);
18c03c614   Peter Zijlstra   Documentation/mem...
730
731
  		do_something();
  	} else {
c8241f855   Paul E. McKenney   doc: Update contr...
732
  		WRITE_ONCE(b, 2);
18c03c614   Peter Zijlstra   Documentation/mem...
733
734
735
736
737
738
  		do_something_else();
  	}
  
  If MAX is defined to be 1, then the compiler knows that (q % MAX) is
  equal to zero, in which case the compiler is within its rights to
  transform the above code into the following:
105ff3cbf   Linus Torvalds   atomic: remove al...
739
  	q = READ_ONCE(a);
b26cfc48e   pierre Kuo   doc: Update contr...
740
  	WRITE_ONCE(b, 2);
18c03c614   Peter Zijlstra   Documentation/mem...
741
  	do_something_else();
2456d2a61   Paul E. McKenney   memory-barriers: ...
742
743
744
745
746
747
  Given this transformation, the CPU is not required to respect the ordering
  between the load from variable 'a' and the store to variable 'b'.  It is
  tempting to add a barrier(), but this does not help.  The conditional
  is gone, and the barrier won't bring it back.  Therefore, if you are
  relying on this ordering, you should make sure that MAX is greater than
  one, perhaps as follows:
18c03c614   Peter Zijlstra   Documentation/mem...
748

105ff3cbf   Linus Torvalds   atomic: remove al...
749
  	q = READ_ONCE(a);
18c03c614   Peter Zijlstra   Documentation/mem...
750
751
  	BUILD_BUG_ON(MAX <= 1); /* Order load from a with store to b. */
  	if (q % MAX) {
c8241f855   Paul E. McKenney   doc: Update contr...
752
  		WRITE_ONCE(b, 1);
18c03c614   Peter Zijlstra   Documentation/mem...
753
754
  		do_something();
  	} else {
c8241f855   Paul E. McKenney   doc: Update contr...
755
  		WRITE_ONCE(b, 2);
18c03c614   Peter Zijlstra   Documentation/mem...
756
757
  		do_something_else();
  	}
2456d2a61   Paul E. McKenney   memory-barriers: ...
758
759
760
  Please note once again that the stores to 'b' differ.  If they were
  identical, as noted earlier, the compiler could pull this store outside
  of the 'if' statement.
8b19d1dea   Paul E. McKenney   documentation: Ad...
761
762
  You must also be careful not to rely too much on boolean short-circuit
  evaluation.  Consider this example:
105ff3cbf   Linus Torvalds   atomic: remove al...
763
  	q = READ_ONCE(a);
57aecae95   Paul E. McKenney   documentation: Fi...
764
  	if (q || 1 > 0)
9af194cef   Paul E. McKenney   documentation: Re...
765
  		WRITE_ONCE(b, 1);
8b19d1dea   Paul E. McKenney   documentation: Ad...
766

5af4692a7   Paul E. McKenney   smp: Make control...
767
768
769
  Because the first condition cannot fault and the second condition is
  always true, the compiler can transform this example as following,
  defeating control dependency:
8b19d1dea   Paul E. McKenney   documentation: Ad...
770

105ff3cbf   Linus Torvalds   atomic: remove al...
771
  	q = READ_ONCE(a);
9af194cef   Paul E. McKenney   documentation: Re...
772
  	WRITE_ONCE(b, 1);
8b19d1dea   Paul E. McKenney   documentation: Ad...
773
774
  
  This example underscores the need to ensure that the compiler cannot
9af194cef   Paul E. McKenney   documentation: Re...
775
  out-guess your code.  More generally, although READ_ONCE() does force
8b19d1dea   Paul E. McKenney   documentation: Ad...
776
777
  the compiler to actually emit code for a given load, it does not force
  the compiler to use the results.
ebff09a6f   Paul E. McKenney   locking/Documenta...
778
779
780
781
782
783
  In addition, control dependencies apply only to the then-clause and
  else-clause of the if-statement in question.  In particular, it does
  not necessarily apply to code following the if-statement:
  
  	q = READ_ONCE(a);
  	if (q) {
c8241f855   Paul E. McKenney   doc: Update contr...
784
  		WRITE_ONCE(b, 1);
ebff09a6f   Paul E. McKenney   locking/Documenta...
785
  	} else {
c8241f855   Paul E. McKenney   doc: Update contr...
786
  		WRITE_ONCE(b, 2);
ebff09a6f   Paul E. McKenney   locking/Documenta...
787
  	}
c8241f855   Paul E. McKenney   doc: Update contr...
788
  	WRITE_ONCE(c, 1);  /* BUG: No ordering against the read from 'a'. */
ebff09a6f   Paul E. McKenney   locking/Documenta...
789
790
791
  
  It is tempting to argue that there in fact is ordering because the
  compiler cannot reorder volatile accesses and also cannot reorder
c8241f855   Paul E. McKenney   doc: Update contr...
792
793
  the writes to 'b' with the condition.  Unfortunately for this line
  of reasoning, the compiler might compile the two writes to 'b' as
ebff09a6f   Paul E. McKenney   locking/Documenta...
794
795
796
797
  conditional-move instructions, as in this fanciful pseudo-assembly
  language:
  
  	ld r1,a
ebff09a6f   Paul E. McKenney   locking/Documenta...
798
  	cmp r1,$0
c8241f855   Paul E. McKenney   doc: Update contr...
799
800
  	cmov,ne r4,$1
  	cmov,eq r4,$2
ebff09a6f   Paul E. McKenney   locking/Documenta...
801
802
803
804
  	st r4,b
  	st $1,c
  
  A weakly ordered CPU would have no dependency of any sort between the load
c8241f855   Paul E. McKenney   doc: Update contr...
805
  from 'a' and the store to 'c'.  The control dependencies would extend
ebff09a6f   Paul E. McKenney   locking/Documenta...
806
807
808
809
  only to the pair of cmov instructions and the store depending on them.
  In short, control dependencies apply only to the stores in the then-clause
  and else-clause of the if-statement in question (including functions
  invoked by those two clauses), not to code following that if-statement.
18c03c614   Peter Zijlstra   Documentation/mem...
810
  Finally, control dependencies do -not- provide transitivity.  This is
5646f7acc   Paul E. McKenney   memory-barriers: ...
811
  demonstrated by two related examples, with the initial values of
c8241f855   Paul E. McKenney   doc: Update contr...
812
  'x' and 'y' both being zero:
18c03c614   Peter Zijlstra   Documentation/mem...
813
814
  
  	CPU 0                     CPU 1
5af4692a7   Paul E. McKenney   smp: Make control...
815
  	=======================   =======================
105ff3cbf   Linus Torvalds   atomic: remove al...
816
  	r1 = READ_ONCE(x);        r2 = READ_ONCE(y);
5646f7acc   Paul E. McKenney   memory-barriers: ...
817
  	if (r1 > 0)               if (r2 > 0)
9af194cef   Paul E. McKenney   documentation: Re...
818
  	  WRITE_ONCE(y, 1);         WRITE_ONCE(x, 1);
18c03c614   Peter Zijlstra   Documentation/mem...
819
820
821
822
823
  
  	assert(!(r1 == 1 && r2 == 1));
  
  The above two-CPU example will never trigger the assert().  However,
  if control dependencies guaranteed transitivity (which they do not),
5646f7acc   Paul E. McKenney   memory-barriers: ...
824
  then adding the following CPU would guarantee a related assertion:
18c03c614   Peter Zijlstra   Documentation/mem...
825

5646f7acc   Paul E. McKenney   memory-barriers: ...
826
827
  	CPU 2
  	=====================
9af194cef   Paul E. McKenney   documentation: Re...
828
  	WRITE_ONCE(x, 2);
5646f7acc   Paul E. McKenney   memory-barriers: ...
829
830
  
  	assert(!(r1 == 2 && r2 == 1 && x == 2)); /* FAILS!!! */
18c03c614   Peter Zijlstra   Documentation/mem...
831

5646f7acc   Paul E. McKenney   memory-barriers: ...
832
833
834
835
  But because control dependencies do -not- provide transitivity, the above
  assertion can fail after the combined three-CPU example completes.  If you
  need the three-CPU example to provide ordering, you will need smp_mb()
  between the loads and stores in the CPU 0 and CPU 1 code fragments,
5af4692a7   Paul E. McKenney   smp: Make control...
836
837
  that is, just before or just after the "if" statements.  Furthermore,
  the original two-CPU example is very fragile and should be avoided.
18c03c614   Peter Zijlstra   Documentation/mem...
838

5646f7acc   Paul E. McKenney   memory-barriers: ...
839
840
841
  These two examples are the LB and WWC litmus tests from this paper:
  http://www.cl.cam.ac.uk/users/pes20/ppc-supplemental/test6.pdf and this
  site: https://www.cl.cam.ac.uk/~pes20/ppcmem/index.html.
18c03c614   Peter Zijlstra   Documentation/mem...
842
843
844
845
846
847
848
  
  In summary:
  
    (*) Control dependencies can order prior loads against later stores.
        However, they do -not- guarantee any other sort of ordering:
        Not prior loads against later loads, nor prior stores against
        later anything.  If you need these other forms of ordering,
d87510c5a   Davidlohr Bueso   documentation: Fi...
849
        use smp_rmb(), smp_wmb(), or, in the case of prior stores and
18c03c614   Peter Zijlstra   Documentation/mem...
850
        later loads, smp_mb().
7817b799e   Paul E. McKenney   documentation: Fi...
851
852
853
854
    (*) If both legs of the "if" statement begin with identical stores to
        the same variable, then those stores must be ordered, either by
        preceding both of them with smp_mb() or by using smp_store_release()
        to carry out the stores.  Please note that it is -not- sufficient
a5052657c   Paul E. McKenney   locking/Documenta...
855
856
857
858
        to use barrier() at beginning of each leg of the "if" statement
        because, as shown by the example above, optimizing compilers can
        destroy the control dependency while respecting the letter of the
        barrier() law.
9b2b3bf53   Paul E. McKenney   Documentation/mem...
859

18c03c614   Peter Zijlstra   Documentation/mem...
860
    (*) Control dependencies require at least one run-time conditional
586dd56a4   Paul E. McKenney   Documentation/mem...
861
        between the prior load and the subsequent store, and this
9af194cef   Paul E. McKenney   documentation: Re...
862
863
        conditional must involve the prior load.  If the compiler is able
        to optimize the conditional away, it will have also optimized
105ff3cbf   Linus Torvalds   atomic: remove al...
864
865
        away the ordering.  Careful use of READ_ONCE() and WRITE_ONCE()
        can help to preserve the needed conditional.
18c03c614   Peter Zijlstra   Documentation/mem...
866
867
  
    (*) Control dependencies require that the compiler avoid reordering the
105ff3cbf   Linus Torvalds   atomic: remove al...
868
869
        dependency into nonexistence.  Careful use of READ_ONCE() or
        atomic{,64}_read() can help to preserve your control dependency.
895f55422   Paul E. McKenney   documentation: Fi...
870
        Please see the COMPILER BARRIER section for more information.
18c03c614   Peter Zijlstra   Documentation/mem...
871

ebff09a6f   Paul E. McKenney   locking/Documenta...
872
873
874
875
876
    (*) Control dependencies apply only to the then-clause and else-clause
        of the if-statement containing the control dependency, including
        any functions that these two clauses call.  Control dependencies
        do -not- apply to code following the if-statement containing the
        control dependency.
ff3828105   Paul E. McKenney   documentation: Cl...
877
    (*) Control dependencies pair normally with other types of barriers.
18c03c614   Peter Zijlstra   Documentation/mem...
878
879
    (*) Control dependencies do -not- provide transitivity.  If you
        need transitivity, use smp_mb().
108b42b4b   David Howells   [PATCH] Document ...
880

c8241f855   Paul E. McKenney   doc: Update contr...
881
882
    (*) Compilers do not understand control dependencies.  It is therefore
        your job to ensure that they do not break your code.
108b42b4b   David Howells   [PATCH] Document ...
883
884
885
886
887
888
  
  SMP BARRIER PAIRING
  -------------------
  
  When dealing with CPU-CPU interactions, certain types of memory barrier should
  always be paired.  A lack of appropriate pairing is almost certainly an error.
ff3828105   Paul E. McKenney   documentation: Cl...
889
890
891
892
893
894
895
896
  General barriers pair with each other, though they also pair with most
  other types of barriers, albeit without transitivity.  An acquire barrier
  pairs with a release barrier, but both may also pair with other barriers,
  including of course general barriers.  A write barrier pairs with a data
  dependency barrier, a control dependency, an acquire barrier, a release
  barrier, a read barrier, or a general barrier.  Similarly a read barrier,
  control dependency, or a data dependency barrier pairs with a write
  barrier, an acquire barrier, a release barrier, or a general barrier:
108b42b4b   David Howells   [PATCH] Document ...
897

2ecf81012   Paul E. McKenney   Documentation/mem...
898
899
  	CPU 1		      CPU 2
  	===============	      ===============
9af194cef   Paul E. McKenney   documentation: Re...
900
  	WRITE_ONCE(a, 1);
108b42b4b   David Howells   [PATCH] Document ...
901
  	<write barrier>
9af194cef   Paul E. McKenney   documentation: Re...
902
  	WRITE_ONCE(b, 2);     x = READ_ONCE(b);
2ecf81012   Paul E. McKenney   Documentation/mem...
903
  			      <read barrier>
9af194cef   Paul E. McKenney   documentation: Re...
904
  			      y = READ_ONCE(a);
108b42b4b   David Howells   [PATCH] Document ...
905
906
  
  Or:
2ecf81012   Paul E. McKenney   Documentation/mem...
907
908
  	CPU 1		      CPU 2
  	===============	      ===============================
108b42b4b   David Howells   [PATCH] Document ...
909
910
  	a = 1;
  	<write barrier>
9af194cef   Paul E. McKenney   documentation: Re...
911
  	WRITE_ONCE(b, &a);    x = READ_ONCE(b);
2ecf81012   Paul E. McKenney   Documentation/mem...
912
913
  			      <data dependency barrier>
  			      y = *x;
108b42b4b   David Howells   [PATCH] Document ...
914

ff3828105   Paul E. McKenney   documentation: Cl...
915
916
917
918
  Or even:
  
  	CPU 1		      CPU 2
  	===============	      ===============================
9af194cef   Paul E. McKenney   documentation: Re...
919
  	r1 = READ_ONCE(y);
ff3828105   Paul E. McKenney   documentation: Cl...
920
  	<general barrier>
9af194cef   Paul E. McKenney   documentation: Re...
921
  	WRITE_ONCE(y, 1);     if (r2 = READ_ONCE(x)) {
ff3828105   Paul E. McKenney   documentation: Cl...
922
  			         <implicit control dependency>
9af194cef   Paul E. McKenney   documentation: Re...
923
  			         WRITE_ONCE(y, 1);
ff3828105   Paul E. McKenney   documentation: Cl...
924
925
926
  			      }
  
  	assert(r1 == 0 || r2 == 0);
108b42b4b   David Howells   [PATCH] Document ...
927
928
  Basically, the read barrier always has to be there, even though it can be of
  the "weaker" type.
670bd95e0   David Howells   [PATCH] Further a...
929
  [!] Note that the stores before the write barrier would normally be expected to
81fc63235   Jarek Poplawski   Documentation/mem...
930
  match the loads after the read barrier or the data dependency barrier, and vice
670bd95e0   David Howells   [PATCH] Further a...
931
  versa:
2ecf81012   Paul E. McKenney   Documentation/mem...
932
933
  	CPU 1                               CPU 2
  	===================                 ===================
9af194cef   Paul E. McKenney   documentation: Re...
934
935
  	WRITE_ONCE(a, 1);    }----   --->{  v = READ_ONCE(c);
  	WRITE_ONCE(b, 2);    }    \ /    {  w = READ_ONCE(d);
2ecf81012   Paul E. McKenney   Documentation/mem...
936
  	<write barrier>            \        <read barrier>
9af194cef   Paul E. McKenney   documentation: Re...
937
938
  	WRITE_ONCE(c, 3);    }    / \    {  x = READ_ONCE(a);
  	WRITE_ONCE(d, 4);    }----   --->{  y = READ_ONCE(b);
670bd95e0   David Howells   [PATCH] Further a...
939

108b42b4b   David Howells   [PATCH] Document ...
940
941
942
  
  EXAMPLES OF MEMORY BARRIER SEQUENCES
  ------------------------------------
81fc63235   Jarek Poplawski   Documentation/mem...
943
  Firstly, write barriers act as partial orderings on store operations.
108b42b4b   David Howells   [PATCH] Document ...
944
945
946
947
948
949
950
951
952
953
954
955
956
  Consider the following sequence of events:
  
  	CPU 1
  	=======================
  	STORE A = 1
  	STORE B = 2
  	STORE C = 3
  	<write barrier>
  	STORE D = 4
  	STORE E = 5
  
  This sequence of events is committed to the memory coherence system in an order
  that the rest of the system might perceive as the unordered set of { STORE A,
80f7228b5   Adrian Bunk   typo fixes: occur...
957
  STORE B, STORE C } all occurring before the unordered set of { STORE D, STORE E
108b42b4b   David Howells   [PATCH] Document ...
958
959
960
961
962
  }:
  
  	+-------+       :      :
  	|       |       +------+
  	|       |------>| C=3  |     }     /\
81fc63235   Jarek Poplawski   Documentation/mem...
963
964
  	|       |  :    +------+     }-----  \  -----> Events perceptible to
  	|       |  :    | A=1  |     }        \/       the rest of the system
108b42b4b   David Howells   [PATCH] Document ...
965
966
967
968
969
970
  	|       |  :    +------+     }
  	| CPU 1 |  :    | B=2  |     }
  	|       |       +------+     }
  	|       |   wwwwwwwwwwwwwwww }   <--- At this point the write barrier
  	|       |       +------+     }        requires all stores prior to the
  	|       |  :    | E=5  |     }        barrier to be committed before
81fc63235   Jarek Poplawski   Documentation/mem...
971
  	|       |  :    +------+     }        further stores may take place
108b42b4b   David Howells   [PATCH] Document ...
972
973
974
975
  	|       |------>| D=4  |     }
  	|       |       +------+
  	+-------+       :      :
  	                   |
670bd95e0   David Howells   [PATCH] Further a...
976
977
  	                   | Sequence in which stores are committed to the
  	                   | memory system by CPU 1
108b42b4b   David Howells   [PATCH] Document ...
978
  	                   V
81fc63235   Jarek Poplawski   Documentation/mem...
979
  Secondly, data dependency barriers act as partial orderings on data-dependent
108b42b4b   David Howells   [PATCH] Document ...
980
981
982
983
  loads.  Consider the following sequence of events:
  
  	CPU 1			CPU 2
  	=======================	=======================
c14038c39   David Howells   [PATCH] Improve d...
984
  		{ B = 7; X = 9; Y = 8; C = &Y }
108b42b4b   David Howells   [PATCH] Document ...
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
  	STORE A = 1
  	STORE B = 2
  	<write barrier>
  	STORE C = &B		LOAD X
  	STORE D = 4		LOAD C (gets &B)
  				LOAD *C (reads B)
  
  Without intervention, CPU 2 may perceive the events on CPU 1 in some
  effectively random order, despite the write barrier issued by CPU 1:
  
  	+-------+       :      :                :       :
  	|       |       +------+                +-------+  | Sequence of update
  	|       |------>| B=2  |-----       --->| Y->8  |  | of perception on
  	|       |  :    +------+     \          +-------+  | CPU 2
  	| CPU 1 |  :    | A=1  |      \     --->| C->&Y |  V
  	|       |       +------+       |        +-------+
  	|       |   wwwwwwwwwwwwwwww   |        :       :
  	|       |       +------+       |        :       :
  	|       |  :    | C=&B |---    |        :       :       +-------+
  	|       |  :    +------+   \   |        +-------+       |       |
  	|       |------>| D=4  |    ----------->| C->&B |------>|       |
  	|       |       +------+       |        +-------+       |       |
  	+-------+       :      :       |        :       :       |       |
  	                               |        :       :       |       |
  	                               |        :       :       | CPU 2 |
  	                               |        +-------+       |       |
  	    Apparently incorrect --->  |        | B->7  |------>|       |
  	    perception of B (!)        |        +-------+       |       |
  	                               |        :       :       |       |
  	                               |        +-------+       |       |
  	    The load of X holds --->    \       | X->9  |------>|       |
  	    up the maintenance           \      +-------+       |       |
  	    of coherence of B             ----->| B->2  |       +-------+
  	                                        +-------+
  	                                        :       :
  
  
  In the above example, CPU 2 perceives that B is 7, despite the load of *C
670e9f34e   Paolo Ornati   Documentation: re...
1023
  (which would be B) coming after the LOAD of C.
108b42b4b   David Howells   [PATCH] Document ...
1024
1025
  
  If, however, a data dependency barrier were to be placed between the load of C
c14038c39   David Howells   [PATCH] Improve d...
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
  and the load of *C (ie: B) on CPU 2:
  
  	CPU 1			CPU 2
  	=======================	=======================
  		{ B = 7; X = 9; Y = 8; C = &Y }
  	STORE A = 1
  	STORE B = 2
  	<write barrier>
  	STORE C = &B		LOAD X
  	STORE D = 4		LOAD C (gets &B)
  				<data dependency barrier>
  				LOAD *C (reads B)
  
  then the following will occur:
108b42b4b   David Howells   [PATCH] Document ...
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
  
  	+-------+       :      :                :       :
  	|       |       +------+                +-------+
  	|       |------>| B=2  |-----       --->| Y->8  |
  	|       |  :    +------+     \          +-------+
  	| CPU 1 |  :    | A=1  |      \     --->| C->&Y |
  	|       |       +------+       |        +-------+
  	|       |   wwwwwwwwwwwwwwww   |        :       :
  	|       |       +------+       |        :       :
  	|       |  :    | C=&B |---    |        :       :       +-------+
  	|       |  :    +------+   \   |        +-------+       |       |
  	|       |------>| D=4  |    ----------->| C->&B |------>|       |
  	|       |       +------+       |        +-------+       |       |
  	+-------+       :      :       |        :       :       |       |
  	                               |        :       :       |       |
  	                               |        :       :       | CPU 2 |
  	                               |        +-------+       |       |
670bd95e0   David Howells   [PATCH] Further a...
1057
1058
1059
1060
1061
1062
  	                               |        | X->9  |------>|       |
  	                               |        +-------+       |       |
  	  Makes sure all effects --->   \   ddddddddddddddddd   |       |
  	  prior to the store of C        \      +-------+       |       |
  	  are perceptible to              ----->| B->2  |------>|       |
  	  subsequent loads                      +-------+       |       |
108b42b4b   David Howells   [PATCH] Document ...
1063
1064
1065
1066
1067
1068
1069
1070
  	                                        :       :       +-------+
  
  
  And thirdly, a read barrier acts as a partial order on loads.  Consider the
  following sequence of events:
  
  	CPU 1			CPU 2
  	=======================	=======================
670bd95e0   David Howells   [PATCH] Further a...
1071
  		{ A = 0, B = 9 }
108b42b4b   David Howells   [PATCH] Document ...
1072
  	STORE A=1
108b42b4b   David Howells   [PATCH] Document ...
1073
  	<write barrier>
670bd95e0   David Howells   [PATCH] Further a...
1074
  	STORE B=2
108b42b4b   David Howells   [PATCH] Document ...
1075
  				LOAD B
670bd95e0   David Howells   [PATCH] Further a...
1076
  				LOAD A
108b42b4b   David Howells   [PATCH] Document ...
1077
1078
1079
  
  Without intervention, CPU 2 may then choose to perceive the events on CPU 1 in
  some effectively random order, despite the write barrier issued by CPU 1:
670bd95e0   David Howells   [PATCH] Further a...
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
  	+-------+       :      :                :       :
  	|       |       +------+                +-------+
  	|       |------>| A=1  |------      --->| A->0  |
  	|       |       +------+      \         +-------+
  	| CPU 1 |   wwwwwwwwwwwwwwww   \    --->| B->9  |
  	|       |       +------+        |       +-------+
  	|       |------>| B=2  |---     |       :       :
  	|       |       +------+   \    |       :       :       +-------+
  	+-------+       :      :    \   |       +-------+       |       |
  	                             ---------->| B->2  |------>|       |
  	                                |       +-------+       | CPU 2 |
  	                                |       | A->0  |------>|       |
  	                                |       +-------+       |       |
  	                                |       :       :       +-------+
  	                                 \      :       :
  	                                  \     +-------+
  	                                   ---->| A->1  |
  	                                        +-------+
  	                                        :       :
108b42b4b   David Howells   [PATCH] Document ...
1099

670bd95e0   David Howells   [PATCH] Further a...
1100

6bc392741   David Howells   [PATCH] Correctio...
1101
  If, however, a read barrier were to be placed between the load of B and the
670bd95e0   David Howells   [PATCH] Further a...
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
  load of A on CPU 2:
  
  	CPU 1			CPU 2
  	=======================	=======================
  		{ A = 0, B = 9 }
  	STORE A=1
  	<write barrier>
  	STORE B=2
  				LOAD B
  				<read barrier>
  				LOAD A
  
  then the partial ordering imposed by CPU 1 will be perceived correctly by CPU
  2:
  
  	+-------+       :      :                :       :
  	|       |       +------+                +-------+
  	|       |------>| A=1  |------      --->| A->0  |
  	|       |       +------+      \         +-------+
  	| CPU 1 |   wwwwwwwwwwwwwwww   \    --->| B->9  |
  	|       |       +------+        |       +-------+
  	|       |------>| B=2  |---     |       :       :
  	|       |       +------+   \    |       :       :       +-------+
  	+-------+       :      :    \   |       +-------+       |       |
  	                             ---------->| B->2  |------>|       |
  	                                |       +-------+       | CPU 2 |
  	                                |       :       :       |       |
  	                                |       :       :       |       |
  	  At this point the read ---->   \  rrrrrrrrrrrrrrrrr   |       |
  	  barrier causes all effects      \     +-------+       |       |
  	  prior to the storage of B        ---->| A->1  |------>|       |
  	  to be perceptible to CPU 2            +-------+       |       |
  	                                        :       :       +-------+
  
  
  To illustrate this more completely, consider what could happen if the code
  contained a load of A either side of the read barrier:
  
  	CPU 1			CPU 2
  	=======================	=======================
  		{ A = 0, B = 9 }
  	STORE A=1
  	<write barrier>
  	STORE B=2
  				LOAD B
  				LOAD A [first load of A]
  				<read barrier>
  				LOAD A [second load of A]
  
  Even though the two loads of A both occur after the load of B, they may both
  come up with different values:
  
  	+-------+       :      :                :       :
  	|       |       +------+                +-------+
  	|       |------>| A=1  |------      --->| A->0  |
  	|       |       +------+      \         +-------+
  	| CPU 1 |   wwwwwwwwwwwwwwww   \    --->| B->9  |
  	|       |       +------+        |       +-------+
  	|       |------>| B=2  |---     |       :       :
  	|       |       +------+   \    |       :       :       +-------+
  	+-------+       :      :    \   |       +-------+       |       |
  	                             ---------->| B->2  |------>|       |
  	                                |       +-------+       | CPU 2 |
  	                                |       :       :       |       |
  	                                |       :       :       |       |
  	                                |       +-------+       |       |
  	                                |       | A->0  |------>| 1st   |
  	                                |       +-------+       |       |
  	  At this point the read ---->   \  rrrrrrrrrrrrrrrrr   |       |
  	  barrier causes all effects      \     +-------+       |       |
  	  prior to the storage of B        ---->| A->1  |------>| 2nd   |
  	  to be perceptible to CPU 2            +-------+       |       |
  	                                        :       :       +-------+
  
  
  But it may be that the update to A from CPU 1 becomes perceptible to CPU 2
  before the read barrier completes anyway:
  
  	+-------+       :      :                :       :
  	|       |       +------+                +-------+
  	|       |------>| A=1  |------      --->| A->0  |
  	|       |       +------+      \         +-------+
  	| CPU 1 |   wwwwwwwwwwwwwwww   \    --->| B->9  |
  	|       |       +------+        |       +-------+
  	|       |------>| B=2  |---     |       :       :
  	|       |       +------+   \    |       :       :       +-------+
  	+-------+       :      :    \   |       +-------+       |       |
  	                             ---------->| B->2  |------>|       |
  	                                |       +-------+       | CPU 2 |
  	                                |       :       :       |       |
  	                                 \      :       :       |       |
  	                                  \     +-------+       |       |
  	                                   ---->| A->1  |------>| 1st   |
  	                                        +-------+       |       |
  	                                    rrrrrrrrrrrrrrrrr   |       |
  	                                        +-------+       |       |
  	                                        | A->1  |------>| 2nd   |
  	                                        +-------+       |       |
  	                                        :       :       +-------+
  
  
  The guarantee is that the second load will always come up with A == 1 if the
  load of B came up with B == 2.  No such guarantee exists for the first load of
  A; that may come up with either A == 0 or A == 1.
  
  
  READ MEMORY BARRIERS VS LOAD SPECULATION
  ----------------------------------------
  
  Many CPUs speculate with loads: that is they see that they will need to load an
  item from memory, and they find a time where they're not using the bus for any
  other loads, and so do the load in advance - even though they haven't actually
  got to that point in the instruction execution flow yet.  This permits the
  actual load instruction to potentially complete immediately because the CPU
  already has the value to hand.
  
  It may turn out that the CPU didn't actually need the value - perhaps because a
  branch circumvented the load - in which case it can discard the value or just
  cache it for later use.
  
  Consider:
e0edc78f2   Ingo Molnar   Documentation/mem...
1223
  	CPU 1			CPU 2
670bd95e0   David Howells   [PATCH] Further a...
1224
  	=======================	=======================
e0edc78f2   Ingo Molnar   Documentation/mem...
1225
1226
1227
1228
  				LOAD B
  				DIVIDE		} Divide instructions generally
  				DIVIDE		} take a long time to perform
  				LOAD A
670bd95e0   David Howells   [PATCH] Further a...
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
  
  Which might appear as this:
  
  	                                        :       :       +-------+
  	                                        +-------+       |       |
  	                                    --->| B->2  |------>|       |
  	                                        +-------+       | CPU 2 |
  	                                        :       :DIVIDE |       |
  	                                        +-------+       |       |
  	The CPU being busy doing a --->     --->| A->0  |~~~~   |       |
  	division speculates on the              +-------+   ~   |       |
  	LOAD of A                               :       :   ~   |       |
  	                                        :       :DIVIDE |       |
  	                                        :       :   ~   |       |
  	Once the divisions are complete -->     :       :   ~-->|       |
  	the CPU can then perform the            :       :       |       |
  	LOAD with immediate effect              :       :       +-------+
  
  
  Placing a read barrier or a data dependency barrier just before the second
  load:
e0edc78f2   Ingo Molnar   Documentation/mem...
1250
  	CPU 1			CPU 2
670bd95e0   David Howells   [PATCH] Further a...
1251
  	=======================	=======================
e0edc78f2   Ingo Molnar   Documentation/mem...
1252
1253
1254
  				LOAD B
  				DIVIDE
  				DIVIDE
670bd95e0   David Howells   [PATCH] Further a...
1255
  				<read barrier>
e0edc78f2   Ingo Molnar   Documentation/mem...
1256
  				LOAD A
670bd95e0   David Howells   [PATCH] Further a...
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
  
  will force any value speculatively obtained to be reconsidered to an extent
  dependent on the type of barrier used.  If there was no change made to the
  speculated memory location, then the speculated value will just be used:
  
  	                                        :       :       +-------+
  	                                        +-------+       |       |
  	                                    --->| B->2  |------>|       |
  	                                        +-------+       | CPU 2 |
  	                                        :       :DIVIDE |       |
  	                                        +-------+       |       |
  	The CPU being busy doing a --->     --->| A->0  |~~~~   |       |
  	division speculates on the              +-------+   ~   |       |
  	LOAD of A                               :       :   ~   |       |
  	                                        :       :DIVIDE |       |
  	                                        :       :   ~   |       |
  	                                        :       :   ~   |       |
  	                                    rrrrrrrrrrrrrrrr~   |       |
  	                                        :       :   ~   |       |
  	                                        :       :   ~-->|       |
  	                                        :       :       |       |
  	                                        :       :       +-------+
  
  
  but if there was an update or an invalidation from another CPU pending, then
  the speculation will be cancelled and the value reloaded:
  
  	                                        :       :       +-------+
  	                                        +-------+       |       |
  	                                    --->| B->2  |------>|       |
  	                                        +-------+       | CPU 2 |
  	                                        :       :DIVIDE |       |
  	                                        +-------+       |       |
  	The CPU being busy doing a --->     --->| A->0  |~~~~   |       |
  	division speculates on the              +-------+   ~   |       |
  	LOAD of A                               :       :   ~   |       |
  	                                        :       :DIVIDE |       |
  	                                        :       :   ~   |       |
  	                                        :       :   ~   |       |
  	                                    rrrrrrrrrrrrrrrrr   |       |
  	                                        +-------+       |       |
  	The speculation is discarded --->   --->| A->1  |------>|       |
  	and an updated value is                 +-------+       |       |
  	retrieved                               :       :       +-------+
108b42b4b   David Howells   [PATCH] Document ...
1301

241e6663b   Paul E. McKenney   smp: Document tra...
1302
1303
1304
1305
1306
  TRANSITIVITY
  ------------
  
  Transitivity is a deeply intuitive notion about ordering that is not
  always provided by real computer systems.  The following example
f36fe1e70   Paul E. McKenney   documentation: Tr...
1307
  demonstrates transitivity:
241e6663b   Paul E. McKenney   smp: Document tra...
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
  
  	CPU 1			CPU 2			CPU 3
  	=======================	=======================	=======================
  		{ X = 0, Y = 0 }
  	STORE X=1		LOAD X			STORE Y=1
  				<general barrier>	<general barrier>
  				LOAD Y			LOAD X
  
  Suppose that CPU 2's load from X returns 1 and its load from Y returns 0.
  This indicates that CPU 2's load from X in some sense follows CPU 1's
  store to X and that CPU 2's load from Y in some sense preceded CPU 3's
  store to Y.  The question is then "Can CPU 3's load from X return 0?"
  
  Because CPU 2's load from X in some sense came after CPU 1's store, it
  is natural to expect that CPU 3's load from X must therefore return 1.
  This expectation is an example of transitivity: if a load executing on
  CPU A follows a load from the same variable executing on CPU B, then
  CPU A's load must either return the same value that CPU B's load did,
  or must return some later value.
  
  In the Linux kernel, use of general memory barriers guarantees
  transitivity.  Therefore, in the above example, if CPU 2's load from X
  returns 1 and its load from Y returns 0, then CPU 3's load from X must
  also return 1.
  
  However, transitivity is -not- guaranteed for read or write barriers.
  For example, suppose that CPU 2's general barrier in the above example
  is changed to a read barrier as shown below:
  
  	CPU 1			CPU 2			CPU 3
  	=======================	=======================	=======================
  		{ X = 0, Y = 0 }
  	STORE X=1		LOAD X			STORE Y=1
  				<read barrier>		<general barrier>
  				LOAD Y			LOAD X
  
  This substitution destroys transitivity: in this example, it is perfectly
  legal for CPU 2's load from X to return 1, its load from Y to return 0,
  and CPU 3's load from X to return 0.
  
  The key point is that although CPU 2's read barrier orders its pair
  of loads, it does not guarantee to order CPU 1's store.  Therefore, if
  this example runs on a system where CPUs 1 and 2 share a store buffer
  or a level of cache, CPU 2 might have early access to CPU 1's writes.
  General barriers are therefore required to ensure that all CPUs agree
  on the combined order of CPU 1's and CPU 2's accesses.
c535cc929   Paul E. McKenney   documentation: Di...
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
  General barriers provide "global transitivity", so that all CPUs will
  agree on the order of operations.  In contrast, a chain of release-acquire
  pairs provides only "local transitivity", so that only those CPUs on
  the chain are guaranteed to agree on the combined order of the accesses.
  For example, switching to C code in deference to Herman Hollerith:
  
  	int u, v, x, y, z;
  
  	void cpu0(void)
  	{
  		r0 = smp_load_acquire(&x);
  		WRITE_ONCE(u, 1);
  		smp_store_release(&y, 1);
  	}
  
  	void cpu1(void)
  	{
  		r1 = smp_load_acquire(&y);
  		r4 = READ_ONCE(v);
  		r5 = READ_ONCE(u);
  		smp_store_release(&z, 1);
  	}
  
  	void cpu2(void)
  	{
  		r2 = smp_load_acquire(&z);
  		smp_store_release(&x, 1);
  	}
  
  	void cpu3(void)
  	{
  		WRITE_ONCE(v, 1);
  		smp_mb();
  		r3 = READ_ONCE(u);
  	}
  
  Because cpu0(), cpu1(), and cpu2() participate in a local transitive
  chain of smp_store_release()/smp_load_acquire() pairs, the following
  outcome is prohibited:
  
  	r0 == 1 && r1 == 1 && r2 == 1
  
  Furthermore, because of the release-acquire relationship between cpu0()
  and cpu1(), cpu1() must see cpu0()'s writes, so that the following
  outcome is prohibited:
  
  	r1 == 1 && r5 == 0
  
  However, the transitivity of release-acquire is local to the participating
  CPUs and does not apply to cpu3().  Therefore, the following outcome
  is possible:
  
  	r0 == 0 && r1 == 1 && r2 == 1 && r3 == 0 && r4 == 0
37ef0341c   Paul E. McKenney   documentation: A...
1407
1408
1409
  As an aside, the following outcome is also possible:
  
  	r0 == 0 && r1 == 1 && r2 == 1 && r3 == 0 && r4 == 0 && r5 == 1
c535cc929   Paul E. McKenney   documentation: Di...
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
  Although cpu0(), cpu1(), and cpu2() will see their respective reads and
  writes in order, CPUs not involved in the release-acquire chain might
  well disagree on the order.  This disagreement stems from the fact that
  the weak memory-barrier instructions used to implement smp_load_acquire()
  and smp_store_release() are not required to order prior stores against
  subsequent loads in all cases.  This means that cpu3() can see cpu0()'s
  store to u as happening -after- cpu1()'s load from v, even though
  both cpu0() and cpu1() agree that these two operations occurred in the
  intended order.
  
  However, please keep in mind that smp_load_acquire() is not magic.
  In particular, it simply reads from its argument with ordering.  It does
  -not- ensure that any particular value will be read.  Therefore, the
  following outcome is possible:
  
  	r0 == 0 && r1 == 0 && r2 == 0 && r5 == 0
  
  Note that this outcome can happen even on a mythical sequentially
  consistent system where nothing is ever reordered.
  
  To reiterate, if your code requires global transitivity, use general
  barriers throughout.
241e6663b   Paul E. McKenney   smp: Document tra...
1432

108b42b4b   David Howells   [PATCH] Document ...
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
  ========================
  EXPLICIT KERNEL BARRIERS
  ========================
  
  The Linux kernel has a variety of different barriers that act at different
  levels:
  
    (*) Compiler barrier.
  
    (*) CPU memory barriers.
  
    (*) MMIO write barrier.
  
  
  COMPILER BARRIER
  ----------------
  
  The Linux kernel has an explicit compiler barrier function that prevents the
  compiler from moving the memory accesses either side of it to the other side:
  
  	barrier();
9af194cef   Paul E. McKenney   documentation: Re...
1454
1455
1456
1457
  This is a general barrier -- there are no read-read or write-write
  variants of barrier().  However, READ_ONCE() and WRITE_ONCE() can be
  thought of as weak forms of barrier() that affect only the specific
  accesses flagged by the READ_ONCE() or WRITE_ONCE().
108b42b4b   David Howells   [PATCH] Document ...
1458

692118dac   Paul E. McKenney   Documentation/mem...
1459
1460
1461
1462
1463
1464
1465
1466
1467
  The barrier() function has the following effects:
  
   (*) Prevents the compiler from reordering accesses following the
       barrier() to precede any accesses preceding the barrier().
       One example use for this property is to ease communication between
       interrupt-handler code and the code that was interrupted.
  
   (*) Within a loop, forces the compiler to load the variables used
       in that loop's conditional on each pass through that loop.
9af194cef   Paul E. McKenney   documentation: Re...
1468
1469
1470
1471
  The READ_ONCE() and WRITE_ONCE() functions can prevent any number of
  optimizations that, while perfectly safe in single-threaded code, can
  be fatal in concurrent code.  Here are some examples of these sorts
  of optimizations:
692118dac   Paul E. McKenney   Documentation/mem...
1472

449f7413c   Paul E. McKenney   Documentation/mem...
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
   (*) The compiler is within its rights to reorder loads and stores
       to the same variable, and in some cases, the CPU is within its
       rights to reorder loads to the same variable.  This means that
       the following code:
  
  	a[0] = x;
  	a[1] = x;
  
       Might result in an older value of x stored in a[1] than in a[0].
       Prevent both the compiler and the CPU from doing this as follows:
9af194cef   Paul E. McKenney   documentation: Re...
1483
1484
  	a[0] = READ_ONCE(x);
  	a[1] = READ_ONCE(x);
449f7413c   Paul E. McKenney   Documentation/mem...
1485

9af194cef   Paul E. McKenney   documentation: Re...
1486
1487
       In short, READ_ONCE() and WRITE_ONCE() provide cache coherence for
       accesses from multiple CPUs to a single variable.
449f7413c   Paul E. McKenney   Documentation/mem...
1488

692118dac   Paul E. McKenney   Documentation/mem...
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
   (*) The compiler is within its rights to merge successive loads from
       the same variable.  Such merging can cause the compiler to "optimize"
       the following code:
  
  	while (tmp = a)
  		do_something_with(tmp);
  
       into the following code, which, although in some sense legitimate
       for single-threaded code, is almost certainly not what the developer
       intended:
  
  	if (tmp = a)
  		for (;;)
  			do_something_with(tmp);
9af194cef   Paul E. McKenney   documentation: Re...
1503
       Use READ_ONCE() to prevent the compiler from doing this to you:
692118dac   Paul E. McKenney   Documentation/mem...
1504

9af194cef   Paul E. McKenney   documentation: Re...
1505
  	while (tmp = READ_ONCE(a))
692118dac   Paul E. McKenney   Documentation/mem...
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
  		do_something_with(tmp);
  
   (*) The compiler is within its rights to reload a variable, for example,
       in cases where high register pressure prevents the compiler from
       keeping all data of interest in registers.  The compiler might
       therefore optimize the variable 'tmp' out of our previous example:
  
  	while (tmp = a)
  		do_something_with(tmp);
  
       This could result in the following code, which is perfectly safe in
       single-threaded code, but can be fatal in concurrent code:
  
  	while (a)
  		do_something_with(a);
  
       For example, the optimized version of this code could result in
       passing a zero to do_something_with() in the case where the variable
       a was modified by some other CPU between the "while" statement and
       the call to do_something_with().
9af194cef   Paul E. McKenney   documentation: Re...
1526
       Again, use READ_ONCE() to prevent the compiler from doing this:
692118dac   Paul E. McKenney   Documentation/mem...
1527

9af194cef   Paul E. McKenney   documentation: Re...
1528
  	while (tmp = READ_ONCE(a))
692118dac   Paul E. McKenney   Documentation/mem...
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
  		do_something_with(tmp);
  
       Note that if the compiler runs short of registers, it might save
       tmp onto the stack.  The overhead of this saving and later restoring
       is why compilers reload variables.  Doing so is perfectly safe for
       single-threaded code, so you need to tell the compiler about cases
       where it is not safe.
  
   (*) The compiler is within its rights to omit a load entirely if it knows
       what the value will be.  For example, if the compiler can prove that
       the value of variable 'a' is always zero, it can optimize this code:
  
  	while (tmp = a)
  		do_something_with(tmp);
  
       Into this:
  
  	do { } while (0);
9af194cef   Paul E. McKenney   documentation: Re...
1547
1548
1549
1550
1551
1552
       This transformation is a win for single-threaded code because it
       gets rid of a load and a branch.  The problem is that the compiler
       will carry out its proof assuming that the current CPU is the only
       one updating variable 'a'.  If variable 'a' is shared, then the
       compiler's proof will be erroneous.  Use READ_ONCE() to tell the
       compiler that it doesn't know as much as it thinks it does:
692118dac   Paul E. McKenney   Documentation/mem...
1553

9af194cef   Paul E. McKenney   documentation: Re...
1554
  	while (tmp = READ_ONCE(a))
692118dac   Paul E. McKenney   Documentation/mem...
1555
1556
1557
  		do_something_with(tmp);
  
       But please note that the compiler is also closely watching what you
9af194cef   Paul E. McKenney   documentation: Re...
1558
       do with the value after the READ_ONCE().  For example, suppose you
692118dac   Paul E. McKenney   Documentation/mem...
1559
       do the following and MAX is a preprocessor macro with the value 1:
9af194cef   Paul E. McKenney   documentation: Re...
1560
  	while ((tmp = READ_ONCE(a)) % MAX)
692118dac   Paul E. McKenney   Documentation/mem...
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
  		do_something_with(tmp);
  
       Then the compiler knows that the result of the "%" operator applied
       to MAX will always be zero, again allowing the compiler to optimize
       the code into near-nonexistence.  (It will still load from the
       variable 'a'.)
  
   (*) Similarly, the compiler is within its rights to omit a store entirely
       if it knows that the variable already has the value being stored.
       Again, the compiler assumes that the current CPU is the only one
       storing into the variable, which can cause the compiler to do the
       wrong thing for shared variables.  For example, suppose you have
       the following:
  
  	a = 0;
65f95ff2e   SeongJae Park   documentation: Cl...
1576
  	... Code that does not store to variable a ...
692118dac   Paul E. McKenney   Documentation/mem...
1577
1578
1579
1580
1581
1582
  	a = 0;
  
       The compiler sees that the value of variable 'a' is already zero, so
       it might well omit the second store.  This would come as a fatal
       surprise if some other CPU might have stored to variable 'a' in the
       meantime.
9af194cef   Paul E. McKenney   documentation: Re...
1583
       Use WRITE_ONCE() to prevent the compiler from making this sort of
692118dac   Paul E. McKenney   Documentation/mem...
1584
       wrong guess:
9af194cef   Paul E. McKenney   documentation: Re...
1585
  	WRITE_ONCE(a, 0);
65f95ff2e   SeongJae Park   documentation: Cl...
1586
  	... Code that does not store to variable a ...
9af194cef   Paul E. McKenney   documentation: Re...
1587
  	WRITE_ONCE(a, 0);
692118dac   Paul E. McKenney   Documentation/mem...
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
  
   (*) The compiler is within its rights to reorder memory accesses unless
       you tell it not to.  For example, consider the following interaction
       between process-level code and an interrupt handler:
  
  	void process_level(void)
  	{
  		msg = get_message();
  		flag = true;
  	}
  
  	void interrupt_handler(void)
  	{
  		if (flag)
  			process_message(msg);
  	}
df5cbb278   Masanari Iida   doc: fix double w...
1604
       There is nothing to prevent the compiler from transforming
692118dac   Paul E. McKenney   Documentation/mem...
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
       process_level() to the following, in fact, this might well be a
       win for single-threaded code:
  
  	void process_level(void)
  	{
  		flag = true;
  		msg = get_message();
  	}
  
       If the interrupt occurs between these two statement, then
9af194cef   Paul E. McKenney   documentation: Re...
1615
       interrupt_handler() might be passed a garbled msg.  Use WRITE_ONCE()
692118dac   Paul E. McKenney   Documentation/mem...
1616
1617
1618
1619
       to prevent this as follows:
  
  	void process_level(void)
  	{
9af194cef   Paul E. McKenney   documentation: Re...
1620
1621
  		WRITE_ONCE(msg, get_message());
  		WRITE_ONCE(flag, true);
692118dac   Paul E. McKenney   Documentation/mem...
1622
1623
1624
1625
  	}
  
  	void interrupt_handler(void)
  	{
9af194cef   Paul E. McKenney   documentation: Re...
1626
1627
  		if (READ_ONCE(flag))
  			process_message(READ_ONCE(msg));
692118dac   Paul E. McKenney   Documentation/mem...
1628
  	}
9af194cef   Paul E. McKenney   documentation: Re...
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
       Note that the READ_ONCE() and WRITE_ONCE() wrappers in
       interrupt_handler() are needed if this interrupt handler can itself
       be interrupted by something that also accesses 'flag' and 'msg',
       for example, a nested interrupt or an NMI.  Otherwise, READ_ONCE()
       and WRITE_ONCE() are not needed in interrupt_handler() other than
       for documentation purposes.  (Note also that nested interrupts
       do not typically occur in modern Linux kernels, in fact, if an
       interrupt handler returns with interrupts enabled, you will get a
       WARN_ONCE() splat.)
  
       You should assume that the compiler can move READ_ONCE() and
       WRITE_ONCE() past code not containing READ_ONCE(), WRITE_ONCE(),
       barrier(), or similar primitives.
  
       This effect could also be achieved using barrier(), but READ_ONCE()
       and WRITE_ONCE() are more selective:  With READ_ONCE() and
       WRITE_ONCE(), the compiler need only forget the contents of the
       indicated memory locations, while with barrier() the compiler must
       discard the value of all memory locations that it has currented
       cached in any machine registers.  Of course, the compiler must also
       respect the order in which the READ_ONCE()s and WRITE_ONCE()s occur,
       though the CPU of course need not do so.
692118dac   Paul E. McKenney   Documentation/mem...
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
  
   (*) The compiler is within its rights to invent stores to a variable,
       as in the following example:
  
  	if (a)
  		b = a;
  	else
  		b = 42;
  
       The compiler might save a branch by optimizing this as follows:
  
  	b = 42;
  	if (a)
  		b = a;
  
       In single-threaded code, this is not only safe, but also saves
       a branch.  Unfortunately, in concurrent code, this optimization
       could cause some other CPU to see a spurious value of 42 -- even
       if variable 'a' was never zero -- when loading variable 'b'.
9af194cef   Paul E. McKenney   documentation: Re...
1670
       Use WRITE_ONCE() to prevent this as follows:
692118dac   Paul E. McKenney   Documentation/mem...
1671
1672
  
  	if (a)
9af194cef   Paul E. McKenney   documentation: Re...
1673
  		WRITE_ONCE(b, a);
692118dac   Paul E. McKenney   Documentation/mem...
1674
  	else
9af194cef   Paul E. McKenney   documentation: Re...
1675
  		WRITE_ONCE(b, 42);
692118dac   Paul E. McKenney   Documentation/mem...
1676
1677
1678
  
       The compiler can also invent loads.  These are usually less
       damaging, but they can result in cache-line bouncing and thus in
9af194cef   Paul E. McKenney   documentation: Re...
1679
       poor performance and scalability.  Use READ_ONCE() to prevent
692118dac   Paul E. McKenney   Documentation/mem...
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
       invented loads.
  
   (*) For aligned memory locations whose size allows them to be accessed
       with a single memory-reference instruction, prevents "load tearing"
       and "store tearing," in which a single large access is replaced by
       multiple smaller accesses.  For example, given an architecture having
       16-bit store instructions with 7-bit immediate fields, the compiler
       might be tempted to use two 16-bit store-immediate instructions to
       implement the following 32-bit store:
  
  	p = 0x00010002;
  
       Please note that GCC really does use this sort of optimization,
       which is not surprising given that it would likely take more
       than two instructions to build the constant and then store it.
       This optimization can therefore be a win in single-threaded code.
       In fact, a recent bug (since fixed) caused GCC to incorrectly use
       this optimization in a volatile store.  In the absence of such bugs,
9af194cef   Paul E. McKenney   documentation: Re...
1698
       use of WRITE_ONCE() prevents store tearing in the following example:
692118dac   Paul E. McKenney   Documentation/mem...
1699

9af194cef   Paul E. McKenney   documentation: Re...
1700
  	WRITE_ONCE(p, 0x00010002);
692118dac   Paul E. McKenney   Documentation/mem...
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
  
       Use of packed structures can also result in load and store tearing,
       as in this example:
  
  	struct __attribute__((__packed__)) foo {
  		short a;
  		int b;
  		short c;
  	};
  	struct foo foo1, foo2;
  	...
  
  	foo2.a = foo1.a;
  	foo2.b = foo1.b;
  	foo2.c = foo1.c;
9af194cef   Paul E. McKenney   documentation: Re...
1716
1717
1718
1719
1720
1721
       Because there are no READ_ONCE() or WRITE_ONCE() wrappers and no
       volatile markings, the compiler would be well within its rights to
       implement these three assignment statements as a pair of 32-bit
       loads followed by a pair of 32-bit stores.  This would result in
       load tearing on 'foo1.b' and store tearing on 'foo2.b'.  READ_ONCE()
       and WRITE_ONCE() again prevent tearing in this example:
692118dac   Paul E. McKenney   Documentation/mem...
1722
1723
  
  	foo2.a = foo1.a;
9af194cef   Paul E. McKenney   documentation: Re...
1724
  	WRITE_ONCE(foo2.b, READ_ONCE(foo1.b));
692118dac   Paul E. McKenney   Documentation/mem...
1725
  	foo2.c = foo1.c;
9af194cef   Paul E. McKenney   documentation: Re...
1726
1727
1728
1729
1730
1731
  All that aside, it is never necessary to use READ_ONCE() and
  WRITE_ONCE() on a variable that has been marked volatile.  For example,
  because 'jiffies' is marked volatile, it is never necessary to
  say READ_ONCE(jiffies).  The reason for this is that READ_ONCE() and
  WRITE_ONCE() are implemented as volatile casts, which has no effect when
  its argument is already marked volatile.
692118dac   Paul E. McKenney   Documentation/mem...
1732
1733
1734
  
  Please note that these compiler barriers have no direct effect on the CPU,
  which may then reorder things however it wishes.
108b42b4b   David Howells   [PATCH] Document ...
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
  
  
  CPU MEMORY BARRIERS
  -------------------
  
  The Linux kernel has eight basic CPU memory barriers:
  
  	TYPE		MANDATORY		SMP CONDITIONAL
  	===============	=======================	===========================
  	GENERAL		mb()			smp_mb()
  	WRITE		wmb()			smp_wmb()
  	READ		rmb()			smp_rmb()
  	DATA DEPENDENCY	read_barrier_depends()	smp_read_barrier_depends()
73f10281e   Nick Piggin   read_barrier_depe...
1748
  All memory barriers except the data dependency barriers imply a compiler
0b6fa347d   SeongJae Park   locking/Documenta...
1749
  barrier.  Data dependencies do not impose any additional compiler ordering.
73f10281e   Nick Piggin   read_barrier_depe...
1750

9af194cef   Paul E. McKenney   documentation: Re...
1751
1752
1753
1754
1755
  Aside: In the case of data dependencies, the compiler would be expected
  to issue the loads in the correct order (eg. `a[b]` would have to load
  the value of b before loading a[b]), however there is no guarantee in
  the C specification that the compiler may not speculate the value of b
  (eg. is equal to 1) and load a before b (eg. tmp = a[1]; if (b != 1)
0b6fa347d   SeongJae Park   locking/Documenta...
1756
1757
  tmp = a[b]; ).  There is also the problem of a compiler reloading b after
  having loaded a[b], thus having a newer copy of b than a[b].  A consensus
9af194cef   Paul E. McKenney   documentation: Re...
1758
1759
  has not yet been reached about these problems, however the READ_ONCE()
  macro is a good place to start looking.
108b42b4b   David Howells   [PATCH] Document ...
1760
1761
  
  SMP memory barriers are reduced to compiler barriers on uniprocessor compiled
81fc63235   Jarek Poplawski   Documentation/mem...
1762
  systems because it is assumed that a CPU will appear to be self-consistent,
108b42b4b   David Howells   [PATCH] Document ...
1763
  and will order overlapping accesses correctly with respect to itself.
6a65d2638   Michael S. Tsirkin   asm-generic: impl...
1764
  However, see the subsection on "Virtual Machine Guests" below.
108b42b4b   David Howells   [PATCH] Document ...
1765
1766
1767
1768
1769
1770
  
  [!] Note that SMP memory barriers _must_ be used to control the ordering of
  references to shared memory on SMP systems, though the use of locking instead
  is sufficient.
  
  Mandatory barriers should not be used to control SMP effects, since mandatory
6a65d2638   Michael S. Tsirkin   asm-generic: impl...
1771
1772
1773
1774
1775
  barriers impose unnecessary overhead on both SMP and UP systems. They may,
  however, be used to control MMIO effects on accesses through relaxed memory I/O
  windows.  These barriers are required even on non-SMP systems as they affect
  the order in which memory operations appear to a device by prohibiting both the
  compiler and the CPU from reordering them.
108b42b4b   David Howells   [PATCH] Document ...
1776
1777
1778
  
  
  There are some more advanced barrier functions:
b92b8b35a   Peter Zijlstra   locking/arch: Ren...
1779
   (*) smp_store_mb(var, value)
108b42b4b   David Howells   [PATCH] Document ...
1780

75b2bd55b   Oleg Nesterov   [PATCH] A minor f...
1781
       This assigns the value to the variable and then inserts a full memory
2d142e599   Davidlohr Bueso   locking/barriers,...
1782
1783
       barrier after it.  It isn't guaranteed to insert anything more than a
       compiler barrier in a UP compilation.
108b42b4b   David Howells   [PATCH] Document ...
1784

1b15611e1   Peter Zijlstra   arch,doc: Convert...
1785
1786
   (*) smp_mb__before_atomic();
   (*) smp_mb__after_atomic();
108b42b4b   David Howells   [PATCH] Document ...
1787

1b15611e1   Peter Zijlstra   arch,doc: Convert...
1788
1789
1790
1791
1792
1793
       These are for use with atomic (such as add, subtract, increment and
       decrement) functions that don't return a value, especially when used for
       reference counting.  These functions do not imply memory barriers.
  
       These are also used for atomic bitop functions that do not return a
       value (such as set_bit and clear_bit).
108b42b4b   David Howells   [PATCH] Document ...
1794
1795
1796
1797
1798
  
       As an example, consider a piece of code that marks an object as being dead
       and then decrements the object's reference count:
  
  	obj->dead = 1;
1b15611e1   Peter Zijlstra   arch,doc: Convert...
1799
  	smp_mb__before_atomic();
108b42b4b   David Howells   [PATCH] Document ...
1800
1801
1802
1803
  	atomic_dec(&obj->ref_count);
  
       This makes sure that the death mark on the object is perceived to be set
       *before* the reference counter is decremented.
706eeb3e9   Peter Zijlstra   Documentation/loc...
1804
       See Documentation/atomic_{t,bitops}.txt for more information.
108b42b4b   David Howells   [PATCH] Document ...
1805

ad2ad5d31   Paul E. McKenney   documentation: Ad...
1806
   (*) lockless_dereference();
0b6fa347d   SeongJae Park   locking/Documenta...
1807

ad2ad5d31   Paul E. McKenney   documentation: Ad...
1808
1809
1810
1811
1812
1813
1814
1815
       This can be thought of as a pointer-fetch wrapper around the
       smp_read_barrier_depends() data-dependency barrier.
  
       This is also similar to rcu_dereference(), but in cases where
       object lifetime is handled by some mechanism other than RCU, for
       example, when the objects removed only when the system goes down.
       In addition, lockless_dereference() is used in some data structures
       that can be used both with and without RCU.
1077fa36f   Alexander Duyck   arch: Add lightwe...
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
   (*) dma_wmb();
   (*) dma_rmb();
  
       These are for use with consistent memory to guarantee the ordering
       of writes or reads of shared memory accessible to both the CPU and a
       DMA capable device.
  
       For example, consider a device driver that shares memory with a device
       and uses a descriptor status value to indicate if the descriptor belongs
       to the device or the CPU, and a doorbell to notify it when new
       descriptors are available:
  
  	if (desc->status != DEVICE_OWN) {
  		/* do not read data until we own descriptor */
  		dma_rmb();
  
  		/* read/modify data */
  		read_data = desc->data;
  		desc->data = write_data;
  
  		/* flush modifications before status update */
  		dma_wmb();
  
  		/* assign ownership */
  		desc->status = DEVICE_OWN;
  
  		/* force memory to sync before notifying device via MMIO */
  		wmb();
  
  		/* notify device of new descriptors */
  		writel(DESC_NOTIFY, doorbell);
  	}
  
       The dma_rmb() allows us guarantee the device has released ownership
7a4580075   Sylvain Trias   Documentation/mem...
1850
       before we read the data from the descriptor, and the dma_wmb() allows
1077fa36f   Alexander Duyck   arch: Add lightwe...
1851
1852
1853
1854
1855
1856
       us to guarantee the data is written to the descriptor before the device
       can see it now has ownership.  The wmb() is needed to guarantee that the
       cache coherent memory writes have completed before attempting a write to
       the cache incoherent MMIO region.
  
       See Documentation/DMA-API.txt for more information on consistent memory.
dfeccea61   SeongJae Park   locking/Documenta...
1857

108b42b4b   David Howells   [PATCH] Document ...
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
  MMIO WRITE BARRIER
  ------------------
  
  The Linux kernel also has a special barrier for use with memory-mapped I/O
  writes:
  
  	mmiowb();
  
  This is a variation on the mandatory write barrier that causes writes to weakly
  ordered I/O regions to be partially ordered.  Its effects may go beyond the
  CPU->Hardware interface and actually affect the hardware at some level.
166bda712   SeongJae Park   locking/Documenta...
1869
  See the subsection "Acquires vs I/O accesses" for more information.
108b42b4b   David Howells   [PATCH] Document ...
1870
1871
1872
1873
1874
1875
1876
  
  
  ===============================
  IMPLICIT KERNEL MEMORY BARRIERS
  ===============================
  
  Some of the other functions in the linux kernel imply memory barriers, amongst
670bd95e0   David Howells   [PATCH] Further a...
1877
  which are locking and scheduling functions.
108b42b4b   David Howells   [PATCH] Document ...
1878
1879
1880
1881
  
  This specification is a _minimum_ guarantee; any particular architecture may
  provide more substantial guarantees, but these may not be relied upon outside
  of arch specific code.
166bda712   SeongJae Park   locking/Documenta...
1882
1883
  LOCK ACQUISITION FUNCTIONS
  --------------------------
108b42b4b   David Howells   [PATCH] Document ...
1884
1885
1886
1887
1888
1889
1890
1891
  
  The Linux kernel has a number of locking constructs:
  
   (*) spin locks
   (*) R/W spin locks
   (*) mutexes
   (*) semaphores
   (*) R/W semaphores
108b42b4b   David Howells   [PATCH] Document ...
1892

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1893
  In all cases there are variants on "ACQUIRE" operations and "RELEASE" operations
108b42b4b   David Howells   [PATCH] Document ...
1894
  for each construct.  These operations all imply certain barriers:
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1895
   (1) ACQUIRE operation implication:
108b42b4b   David Howells   [PATCH] Document ...
1896

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1897
1898
       Memory operations issued after the ACQUIRE will be completed after the
       ACQUIRE operation has completed.
108b42b4b   David Howells   [PATCH] Document ...
1899

8dd853d7b   Paul E. McKenney   Documentation/mem...
1900
       Memory operations issued before the ACQUIRE may be completed after
a9668cd6e   Peter Zijlstra   locking: Remove s...
1901
       the ACQUIRE operation has completed.
108b42b4b   David Howells   [PATCH] Document ...
1902

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1903
   (2) RELEASE operation implication:
108b42b4b   David Howells   [PATCH] Document ...
1904

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1905
1906
       Memory operations issued before the RELEASE will be completed before the
       RELEASE operation has completed.
108b42b4b   David Howells   [PATCH] Document ...
1907

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1908
1909
       Memory operations issued after the RELEASE may be completed before the
       RELEASE operation has completed.
108b42b4b   David Howells   [PATCH] Document ...
1910

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1911
   (3) ACQUIRE vs ACQUIRE implication:
108b42b4b   David Howells   [PATCH] Document ...
1912

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1913
1914
       All ACQUIRE operations issued before another ACQUIRE operation will be
       completed before that ACQUIRE operation.
108b42b4b   David Howells   [PATCH] Document ...
1915

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1916
   (4) ACQUIRE vs RELEASE implication:
108b42b4b   David Howells   [PATCH] Document ...
1917

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1918
1919
       All ACQUIRE operations issued before a RELEASE operation will be
       completed before the RELEASE operation.
108b42b4b   David Howells   [PATCH] Document ...
1920

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1921
   (5) Failed conditional ACQUIRE implication:
108b42b4b   David Howells   [PATCH] Document ...
1922

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1923
1924
       Certain locking variants of the ACQUIRE operation may fail, either due to
       being unable to get the lock immediately, or due to receiving an unblocked
108b42b4b   David Howells   [PATCH] Document ...
1925
1926
       signal whilst asleep waiting for the lock to become available.  Failed
       locks do not imply any sort of barrier.
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1927
1928
1929
  [!] Note: one of the consequences of lock ACQUIREs and RELEASEs being only
  one-way barriers is that the effects of instructions outside of a critical
  section may seep into the inside of the critical section.
108b42b4b   David Howells   [PATCH] Document ...
1930

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1931
1932
1933
1934
  An ACQUIRE followed by a RELEASE may not be assumed to be full memory barrier
  because it is possible for an access preceding the ACQUIRE to happen after the
  ACQUIRE, and an access following the RELEASE to happen before the RELEASE, and
  the two accesses can themselves then cross:
670bd95e0   David Howells   [PATCH] Further a...
1935
1936
  
  	*A = a;
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1937
1938
  	ACQUIRE M
  	RELEASE M
670bd95e0   David Howells   [PATCH] Further a...
1939
1940
1941
  	*B = b;
  
  may occur as:
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1942
  	ACQUIRE M, STORE *B, STORE *A, RELEASE M
17eb88e06   Paul E. McKenney   Documentation/mem...
1943

8dd853d7b   Paul E. McKenney   Documentation/mem...
1944
1945
1946
1947
1948
  When the ACQUIRE and RELEASE are a lock acquisition and release,
  respectively, this same reordering can occur if the lock's ACQUIRE and
  RELEASE are to the same lock variable, but only from the perspective of
  another CPU not holding that lock.  In short, a ACQUIRE followed by an
  RELEASE may -not- be assumed to be a full memory barrier.
12d560f4e   Paul E. McKenney   rcu,locking: Priv...
1949
1950
1951
1952
  Similarly, the reverse case of a RELEASE followed by an ACQUIRE does
  not imply a full memory barrier.  Therefore, the CPU's execution of the
  critical sections corresponding to the RELEASE and the ACQUIRE can cross,
  so that:
17eb88e06   Paul E. McKenney   Documentation/mem...
1953
1954
  
  	*A = a;
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1955
1956
  	RELEASE M
  	ACQUIRE N
17eb88e06   Paul E. McKenney   Documentation/mem...
1957
1958
1959
  	*B = b;
  
  could occur as:
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1960
  	ACQUIRE N, STORE *B, STORE *A, RELEASE M
17eb88e06   Paul E. McKenney   Documentation/mem...
1961

8dd853d7b   Paul E. McKenney   Documentation/mem...
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
  It might appear that this reordering could introduce a deadlock.
  However, this cannot happen because if such a deadlock threatened,
  the RELEASE would simply complete, thereby avoiding the deadlock.
  
  	Why does this work?
  
  	One key point is that we are only talking about the CPU doing
  	the reordering, not the compiler.  If the compiler (or, for
  	that matter, the developer) switched the operations, deadlock
  	-could- occur.
  
  	But suppose the CPU reordered the operations.  In this case,
  	the unlock precedes the lock in the assembly code.  The CPU
  	simply elected to try executing the later lock operation first.
  	If there is a deadlock, this lock operation will simply spin (or
  	try to sleep, but more on that later).	The CPU will eventually
  	execute the unlock operation (which preceded the lock operation
  	in the assembly code), which will unravel the potential deadlock,
  	allowing the lock operation to succeed.
  
  	But what if the lock is a sleeplock?  In that case, the code will
  	try to enter the scheduler, where it will eventually encounter
  	a memory barrier, which will force the earlier unlock operation
  	to complete, again unraveling the deadlock.  There might be
  	a sleep-unlock race, but the locking primitive needs to resolve
  	such races properly in any case.
108b42b4b   David Howells   [PATCH] Document ...
1988
1989
1990
1991
  Locks and semaphores may not provide any guarantee of ordering on UP compiled
  systems, and so cannot be counted on in such a situation to actually achieve
  anything at all - especially with respect to I/O accesses - unless combined
  with interrupt disabling operations.
d7cab36db   SeongJae Park   locking/Documenta...
1992
  See also the section on "Inter-CPU acquiring barrier effects".
108b42b4b   David Howells   [PATCH] Document ...
1993
1994
1995
1996
1997
1998
  
  
  As an example, consider the following:
  
  	*A = a;
  	*B = b;
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
1999
  	ACQUIRE
108b42b4b   David Howells   [PATCH] Document ...
2000
2001
  	*C = c;
  	*D = d;
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2002
  	RELEASE
108b42b4b   David Howells   [PATCH] Document ...
2003
2004
2005
2006
  	*E = e;
  	*F = f;
  
  The following sequence of events is acceptable:
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2007
  	ACQUIRE, {*F,*A}, *E, {*C,*D}, *B, RELEASE
108b42b4b   David Howells   [PATCH] Document ...
2008
2009
2010
2011
  
  	[+] Note that {*F,*A} indicates a combined access.
  
  But none of the following are:
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2012
2013
2014
2015
  	{*F,*A}, *B,	ACQUIRE, *C, *D,	RELEASE, *E
  	*A, *B, *C,	ACQUIRE, *D,		RELEASE, *E, *F
  	*A, *B,		ACQUIRE, *C,		RELEASE, *D, *E, *F
  	*B,		ACQUIRE, *C, *D,	RELEASE, {*F,*A}, *E
108b42b4b   David Howells   [PATCH] Document ...
2016
2017
2018
2019
2020
  
  
  
  INTERRUPT DISABLING FUNCTIONS
  -----------------------------
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2021
2022
  Functions that disable interrupts (ACQUIRE equivalent) and enable interrupts
  (RELEASE equivalent) will act as compiler barriers only.  So if memory or I/O
108b42b4b   David Howells   [PATCH] Document ...
2023
2024
  barriers are required in such a situation, they must be provided from some
  other means.
50fa610a3   David Howells   sched: Document m...
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
  SLEEP AND WAKE-UP FUNCTIONS
  ---------------------------
  
  Sleeping and waking on an event flagged in global data can be viewed as an
  interaction between two pieces of data: the task state of the task waiting for
  the event and the global data used to indicate the event.  To make sure that
  these appear to happen in the right order, the primitives to begin the process
  of going to sleep, and the primitives to initiate a wake up imply certain
  barriers.
  
  Firstly, the sleeper normally follows something like this sequence of events:
  
  	for (;;) {
  		set_current_state(TASK_UNINTERRUPTIBLE);
  		if (event_indicated)
  			break;
  		schedule();
  	}
  
  A general memory barrier is interpolated automatically by set_current_state()
  after it has altered the task state:
  
  	CPU 1
  	===============================
  	set_current_state();
b92b8b35a   Peter Zijlstra   locking/arch: Ren...
2050
  	  smp_store_mb();
50fa610a3   David Howells   sched: Document m...
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
  	    STORE current->state
  	    <general barrier>
  	LOAD event_indicated
  
  set_current_state() may be wrapped by:
  
  	prepare_to_wait();
  	prepare_to_wait_exclusive();
  
  which therefore also imply a general memory barrier after setting the state.
  The whole sequence above is available in various canned forms, all of which
  interpolate the memory barrier in the right place:
  
  	wait_event();
  	wait_event_interruptible();
  	wait_event_interruptible_exclusive();
  	wait_event_interruptible_timeout();
  	wait_event_killable();
  	wait_event_timeout();
  	wait_on_bit();
  	wait_on_bit_lock();
  
  
  Secondly, code that performs a wake up normally follows something like this:
  
  	event_indicated = 1;
  	wake_up(&event_wait_queue);
  
  or:
  
  	event_indicated = 1;
  	wake_up_process(event_daemon);
0b6fa347d   SeongJae Park   locking/Documenta...
2083
2084
2085
  A write memory barrier is implied by wake_up() and co.  if and only if they
  wake something up.  The barrier occurs before the task state is cleared, and so
  sits between the STORE to indicate the event and the STORE to set TASK_RUNNING:
50fa610a3   David Howells   sched: Document m...
2086
2087
2088
2089
  
  	CPU 1				CPU 2
  	===============================	===============================
  	set_current_state();		STORE event_indicated
b92b8b35a   Peter Zijlstra   locking/arch: Ren...
2090
  	  smp_store_mb();		wake_up();
50fa610a3   David Howells   sched: Document m...
2091
2092
2093
  	    STORE current->state	  <write barrier>
  	    <general barrier>		  STORE current->state
  	LOAD event_indicated
5726ce06a   Paul E. McKenney   documentation: Cl...
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
  To repeat, this write memory barrier is present if and only if something
  is actually awakened.  To see this, consider the following sequence of
  events, where X and Y are both initially zero:
  
  	CPU 1				CPU 2
  	===============================	===============================
  	X = 1;				STORE event_indicated
  	smp_mb();			wake_up();
  	Y = 1;				wait_event(wq, Y == 1);
  	wake_up();			  load from Y sees 1, no memory barrier
  					load from X might see 0
  
  In contrast, if a wakeup does occur, CPU 2's load from X would be guaranteed
  to see 1.
50fa610a3   David Howells   sched: Document m...
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
  The available waker functions include:
  
  	complete();
  	wake_up();
  	wake_up_all();
  	wake_up_bit();
  	wake_up_interruptible();
  	wake_up_interruptible_all();
  	wake_up_interruptible_nr();
  	wake_up_interruptible_poll();
  	wake_up_interruptible_sync();
  	wake_up_interruptible_sync_poll();
  	wake_up_locked();
  	wake_up_locked_poll();
  	wake_up_nr();
  	wake_up_poll();
  	wake_up_process();
  
  
  [!] Note that the memory barriers implied by the sleeper and the waker do _not_
  order multiple stores before the wake-up with respect to loads of those stored
  values after the sleeper has called set_current_state().  For instance, if the
  sleeper does:
  
  	set_current_state(TASK_INTERRUPTIBLE);
  	if (event_indicated)
  		break;
  	__set_current_state(TASK_RUNNING);
  	do_something(my_data);
  
  and the waker does:
  
  	my_data = value;
  	event_indicated = 1;
  	wake_up(&event_wait_queue);
  
  there's no guarantee that the change to event_indicated will be perceived by
  the sleeper as coming after the change to my_data.  In such a circumstance, the
  code on both sides must interpolate its own memory barriers between the
  separate data accesses.  Thus the above sleeper ought to do:
  
  	set_current_state(TASK_INTERRUPTIBLE);
  	if (event_indicated) {
  		smp_rmb();
  		do_something(my_data);
  	}
  
  and the waker should do:
  
  	my_data = value;
  	smp_wmb();
  	event_indicated = 1;
  	wake_up(&event_wait_queue);
108b42b4b   David Howells   [PATCH] Document ...
2161
2162
2163
2164
2165
2166
  MISCELLANEOUS FUNCTIONS
  -----------------------
  
  Other functions that imply barriers:
  
   (*) schedule() and similar imply full memory barriers.
108b42b4b   David Howells   [PATCH] Document ...
2167

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2168
2169
2170
  ===================================
  INTER-CPU ACQUIRING BARRIER EFFECTS
  ===================================
108b42b4b   David Howells   [PATCH] Document ...
2171
2172
2173
2174
  
  On SMP systems locking primitives give a more substantial form of barrier: one
  that does affect memory access ordering on other CPUs, within the context of
  conflict on any particular lock.
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2175
2176
  ACQUIRES VS MEMORY ACCESSES
  ---------------------------
108b42b4b   David Howells   [PATCH] Document ...
2177

79afecfaa   Aneesh Kumar   [PATCH] Fix typos...
2178
  Consider the following: the system has a pair of spinlocks (M) and (Q), and
108b42b4b   David Howells   [PATCH] Document ...
2179
2180
2181
2182
  three CPUs; then should the following sequence of events occur:
  
  	CPU 1				CPU 2
  	===============================	===============================
9af194cef   Paul E. McKenney   documentation: Re...
2183
  	WRITE_ONCE(*A, a);		WRITE_ONCE(*E, e);
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2184
  	ACQUIRE M			ACQUIRE Q
9af194cef   Paul E. McKenney   documentation: Re...
2185
2186
  	WRITE_ONCE(*B, b);		WRITE_ONCE(*F, f);
  	WRITE_ONCE(*C, c);		WRITE_ONCE(*G, g);
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2187
  	RELEASE M			RELEASE Q
9af194cef   Paul E. McKenney   documentation: Re...
2188
  	WRITE_ONCE(*D, d);		WRITE_ONCE(*H, h);
108b42b4b   David Howells   [PATCH] Document ...
2189

81fc63235   Jarek Poplawski   Documentation/mem...
2190
  Then there is no guarantee as to what order CPU 3 will see the accesses to *A
108b42b4b   David Howells   [PATCH] Document ...
2191
  through *H occur in, other than the constraints imposed by the separate locks
0b6fa347d   SeongJae Park   locking/Documenta...
2192
  on the separate CPUs.  It might, for example, see:
108b42b4b   David Howells   [PATCH] Document ...
2193

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2194
  	*E, ACQUIRE M, ACQUIRE Q, *G, *C, *F, *A, *B, RELEASE Q, *D, *H, RELEASE M
108b42b4b   David Howells   [PATCH] Document ...
2195
2196
  
  But it won't see any of:
2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2197
2198
2199
2200
  	*B, *C or *D preceding ACQUIRE M
  	*A, *B or *C following RELEASE M
  	*F, *G or *H preceding ACQUIRE Q
  	*E, *F or *G following RELEASE Q
108b42b4b   David Howells   [PATCH] Document ...
2201

108b42b4b   David Howells   [PATCH] Document ...
2202

2e4f5382d   Peter Zijlstra   locking/doc: Rena...
2203
2204
  ACQUIRES VS I/O ACCESSES
  ------------------------
108b42b4b   David Howells   [PATCH] Document ...
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
  
  Under certain circumstances (especially involving NUMA), I/O accesses within
  two spinlocked sections on two different CPUs may be seen as interleaved by the
  PCI bridge, because the PCI bridge does not necessarily participate in the
  cache-coherence protocol, and is therefore incapable of issuing the required
  read memory barriers.
  
  For example:
  
  	CPU 1				CPU 2
  	===============================	===============================
  	spin_lock(Q)
  	writel(0, ADDR)
  	writel(1, DATA);
  	spin_unlock(Q);
  					spin_lock(Q);
  					writel(4, ADDR);
  					writel(5, DATA);
  					spin_unlock(Q);
  
  may be seen by the PCI bridge as follows:
  
  	STORE *ADDR = 0, STORE *ADDR = 4, STORE *DATA = 1, STORE *DATA = 5
  
  which would probably cause the hardware to malfunction.
  
  
  What is necessary here is to intervene with an mmiowb() before dropping the
  spinlock, for example:
  
  	CPU 1				CPU 2
  	===============================	===============================
  	spin_lock(Q)
  	writel(0, ADDR)
  	writel(1, DATA);
  	mmiowb();
  	spin_unlock(Q);
  					spin_lock(Q);
  					writel(4, ADDR);
  					writel(5, DATA);
  					mmiowb();
  					spin_unlock(Q);
81fc63235   Jarek Poplawski   Documentation/mem...
2247
2248
  this will ensure that the two stores issued on CPU 1 appear at the PCI bridge
  before either of the stores issued on CPU 2.
108b42b4b   David Howells   [PATCH] Document ...
2249

81fc63235   Jarek Poplawski   Documentation/mem...
2250
2251
  Furthermore, following a store by a load from the same device obviates the need
  for the mmiowb(), because the load forces the store to complete before the load
108b42b4b   David Howells   [PATCH] Document ...
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
  is performed:
  
  	CPU 1				CPU 2
  	===============================	===============================
  	spin_lock(Q)
  	writel(0, ADDR)
  	a = readl(DATA);
  	spin_unlock(Q);
  					spin_lock(Q);
  					writel(4, ADDR);
  					b = readl(DATA);
  					spin_unlock(Q);
0fe397f0c   Helmut Grohne   docs: update refe...
2264
  See Documentation/driver-api/device-io.rst for more information.
108b42b4b   David Howells   [PATCH] Document ...
2265
2266
2267
2268
2269
2270
2271
2272
  
  
  =================================
  WHERE ARE MEMORY BARRIERS NEEDED?
  =================================
  
  Under normal operation, memory operation reordering is generally not going to
  be a problem as a single-threaded linear piece of code will still appear to
50fa610a3   David Howells   sched: Document m...
2273
  work correctly, even if it's in an SMP kernel.  There are, however, four
108b42b4b   David Howells   [PATCH] Document ...
2274
2275
2276
2277
2278
  circumstances in which reordering definitely _could_ be a problem:
  
   (*) Interprocessor interaction.
  
   (*) Atomic operations.
81fc63235   Jarek Poplawski   Documentation/mem...
2279
   (*) Accessing devices.
108b42b4b   David Howells   [PATCH] Document ...
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
  
   (*) Interrupts.
  
  
  INTERPROCESSOR INTERACTION
  --------------------------
  
  When there's a system with more than one processor, more than one CPU in the
  system may be working on the same data set at the same time.  This can cause
  synchronisation problems, and the usual way of dealing with them is to use
  locks.  Locks, however, are quite expensive, and so it may be preferable to
  operate without the use of a lock if at all possible.  In such a case
  operations that affect both CPUs may have to be carefully ordered to prevent
  a malfunction.
  
  Consider, for example, the R/W semaphore slow path.  Here a waiting process is
  queued on the semaphore, by virtue of it having a piece of its stack linked to
  the semaphore's list of waiting processes:
  
  	struct rw_semaphore {
  		...
  		spinlock_t lock;
  		struct list_head waiters;
  	};
  
  	struct rwsem_waiter {
  		struct list_head list;
  		struct task_struct *task;
  	};
  
  To wake up a particular waiter, the up_read() or up_write() functions have to:
  
   (1) read the next pointer from this waiter's record to know as to where the
       next waiter record is;
81fc63235   Jarek Poplawski   Documentation/mem...
2314
   (2) read the pointer to the waiter's task structure;
108b42b4b   David Howells   [PATCH] Document ...
2315
2316
2317
2318
2319
2320
  
   (3) clear the task pointer to tell the waiter it has been given the semaphore;
  
   (4) call wake_up_process() on the task; and
  
   (5) release the reference held on the waiter's task struct.
81fc63235   Jarek Poplawski   Documentation/mem...
2321
  In other words, it has to perform this sequence of events:
108b42b4b   David Howells   [PATCH] Document ...
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
  
  	LOAD waiter->list.next;
  	LOAD waiter->task;
  	STORE waiter->task;
  	CALL wakeup
  	RELEASE task
  
  and if any of these steps occur out of order, then the whole thing may
  malfunction.
  
  Once it has queued itself and dropped the semaphore lock, the waiter does not
  get the lock again; it instead just waits for its task pointer to be cleared
  before proceeding.  Since the record is on the waiter's stack, this means that
  if the task pointer is cleared _before_ the next pointer in the list is read,
  another CPU might start processing the waiter and might clobber the waiter's
  stack before the up*() function has a chance to read the next pointer.
  
  Consider then what might happen to the above sequence of events:
  
  	CPU 1				CPU 2
  	===============================	===============================
  					down_xxx()
  					Queue waiter
  					Sleep
  	up_yyy()
  	LOAD waiter->task;
  	STORE waiter->task;
  					Woken up by other event
  	<preempt>
  					Resume processing
  					down_xxx() returns
  					call foo()
  					foo() clobbers *waiter
  	</preempt>
  	LOAD waiter->list.next;
  	--- OOPS ---
  
  This could be dealt with using the semaphore lock, but then the down_xxx()
  function has to needlessly get the spinlock again after being woken up.
  
  The way to deal with this is to insert a general SMP memory barrier:
  
  	LOAD waiter->list.next;
  	LOAD waiter->task;
  	smp_mb();
  	STORE waiter->task;
  	CALL wakeup
  	RELEASE task
  
  In this case, the barrier makes a guarantee that all memory accesses before the
  barrier will appear to happen before all the memory accesses after the barrier
  with respect to the other CPUs on the system.  It does _not_ guarantee that all
  the memory accesses before the barrier will be complete by the time the barrier
  instruction itself is complete.
  
  On a UP system - where this wouldn't be a problem - the smp_mb() is just a
  compiler barrier, thus making sure the compiler emits the instructions in the
6bc392741   David Howells   [PATCH] Correctio...
2379
2380
  right order without actually intervening in the CPU.  Since there's only one
  CPU, that CPU's dependency ordering logic will take care of everything else.
108b42b4b   David Howells   [PATCH] Document ...
2381
2382
2383
2384
  
  
  ATOMIC OPERATIONS
  -----------------
dbc8700e2   David Howells   [PATCH] Fix memor...
2385
2386
2387
2388
  Whilst they are technically interprocessor interaction considerations, atomic
  operations are noted specially as some of them imply full memory barriers and
  some don't, but they're very heavily relied on as a group throughout the
  kernel.
706eeb3e9   Peter Zijlstra   Documentation/loc...
2389
  See Documentation/atomic_t.txt for more information.
108b42b4b   David Howells   [PATCH] Document ...
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
  
  
  ACCESSING DEVICES
  -----------------
  
  Many devices can be memory mapped, and so appear to the CPU as if they're just
  a set of memory locations.  To control such a device, the driver usually has to
  make the right memory accesses in exactly the right order.
  
  However, having a clever CPU or a clever compiler creates a potential problem
  in that the carefully sequenced accesses in the driver code won't reach the
  device in the requisite order if the CPU or the compiler thinks it is more
  efficient to reorder, combine or merge accesses - something that would cause
  the device to malfunction.
  
  Inside of the Linux kernel, I/O should be done through the appropriate accessor
  routines - such as inb() or writel() - which know how to make such accesses
  appropriately sequential.  Whilst this, for the most part, renders the explicit
  use of memory barriers unnecessary, there are a couple of situations where they
  might be needed:
  
   (1) On some systems, I/O stores are not strongly ordered across all CPUs, and
       so for _all_ general drivers locks should be used and mmiowb() must be
       issued prior to unlocking the critical section.
  
   (2) If the accessor functions are used to refer to an I/O memory window with
       relaxed memory access properties, then _mandatory_ memory barriers are
       required to enforce ordering.
0fe397f0c   Helmut Grohne   docs: update refe...
2418
  See Documentation/driver-api/device-io.rst for more information.
108b42b4b   David Howells   [PATCH] Document ...
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
  
  
  INTERRUPTS
  ----------
  
  A driver may be interrupted by its own interrupt service routine, and thus the
  two parts of the driver may interfere with each other's attempts to control or
  access the device.
  
  This may be alleviated - at least in part - by disabling local interrupts (a
  form of locking), such that the critical operations are all contained within
  the interrupt-disabled section in the driver.  Whilst the driver's interrupt
  routine is executing, the driver's core may not run on the same CPU, and its
  interrupt is not permitted to happen again until the current interrupt has been
  handled, thus the interrupt handler does not need to lock against that.
  
  However, consider a driver that was talking to an ethernet card that sports an
  address register and a data register.  If that driver's core talks to the card
  under interrupt-disablement and then the driver's interrupt handler is invoked:
  
  	LOCAL IRQ DISABLE
  	writew(ADDR, 3);
  	writew(DATA, y);
  	LOCAL IRQ ENABLE
  	<interrupt>
  	writew(ADDR, 4);
  	q = readw(DATA);
  	</interrupt>
  
  The store to the data register might happen after the second store to the
  address register if ordering rules are sufficiently relaxed:
  
  	STORE *ADDR = 3, STORE *ADDR = 4, STORE *DATA = y, q = LOAD *DATA
  
  
  If ordering rules are relaxed, it must be assumed that accesses done inside an
  interrupt disabled section may leak outside of it and may interleave with
  accesses performed in an interrupt - and vice versa - unless implicit or
  explicit barriers are used.
  
  Normally this won't be a problem because the I/O accesses done inside such
  sections will include synchronous load operations on strictly ordered I/O
0b6fa347d   SeongJae Park   locking/Documenta...
2461
  registers that form implicit I/O barriers.  If this isn't sufficient then an
108b42b4b   David Howells   [PATCH] Document ...
2462
2463
2464
2465
  mmiowb() may need to be used explicitly.
  
  
  A similar situation may occur between an interrupt routine and two routines
0b6fa347d   SeongJae Park   locking/Documenta...
2466
  running on separate CPUs that communicate with each other.  If such a case is
108b42b4b   David Howells   [PATCH] Document ...
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
  likely, then interrupt-disabling locks should be used to guarantee ordering.
  
  
  ==========================
  KERNEL I/O BARRIER EFFECTS
  ==========================
  
  When accessing I/O memory, drivers should use the appropriate accessor
  functions:
  
   (*) inX(), outX():
  
       These are intended to talk to I/O space rather than memory space, but
0b6fa347d   SeongJae Park   locking/Documenta...
2480
2481
       that's primarily a CPU-specific concept.  The i386 and x86_64 processors
       do indeed have special I/O space access cycles and instructions, but many
108b42b4b   David Howells   [PATCH] Document ...
2482
       CPUs don't have such a concept.
81fc63235   Jarek Poplawski   Documentation/mem...
2483
2484
       The PCI bus, amongst others, defines an I/O space concept which - on such
       CPUs as i386 and x86_64 - readily maps to the CPU's concept of I/O
6bc392741   David Howells   [PATCH] Correctio...
2485
2486
2487
       space.  However, it may also be mapped as a virtual I/O space in the CPU's
       memory map, particularly on those CPUs that don't support alternate I/O
       spaces.
108b42b4b   David Howells   [PATCH] Document ...
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
  
       Accesses to this space may be fully synchronous (as on i386), but
       intermediary bridges (such as the PCI host bridge) may not fully honour
       that.
  
       They are guaranteed to be fully ordered with respect to each other.
  
       They are not guaranteed to be fully ordered with respect to other types of
       memory and I/O operation.
  
   (*) readX(), writeX():
  
       Whether these are guaranteed to be fully ordered and uncombined with
       respect to each other on the issuing CPU depends on the characteristics
0b6fa347d   SeongJae Park   locking/Documenta...
2502
       defined for the memory window through which they're accessing.  On later
108b42b4b   David Howells   [PATCH] Document ...
2503
2504
       i386 architecture machines, for example, this is controlled by way of the
       MTRR registers.
81fc63235   Jarek Poplawski   Documentation/mem...
2505
       Ordinarily, these will be guaranteed to be fully ordered and uncombined,
108b42b4b   David Howells   [PATCH] Document ...
2506
2507
2508
2509
2510
2511
2512
2513
       provided they're not accessing a prefetchable device.
  
       However, intermediary hardware (such as a PCI bridge) may indulge in
       deferral if it so wishes; to flush a store, a load from the same location
       is preferred[*], but a load from the same device or from configuration
       space should suffice for PCI.
  
       [*] NOTE! attempting to load from the same location as was written to may
e0edc78f2   Ingo Molnar   Documentation/mem...
2514
2515
  	 cause a malfunction - consider the 16550 Rx/Tx serial registers for
  	 example.
108b42b4b   David Howells   [PATCH] Document ...
2516
2517
2518
2519
2520
2521
  
       Used with prefetchable I/O memory, an mmiowb() barrier may be required to
       force stores to be ordered.
  
       Please refer to the PCI specification for more information on interactions
       between PCI transactions.
a8e0aead7   Will Deacon   documentation: me...
2522
2523
2524
   (*) readX_relaxed(), writeX_relaxed()
  
       These are similar to readX() and writeX(), but provide weaker memory
0b6fa347d   SeongJae Park   locking/Documenta...
2525
       ordering guarantees.  Specifically, they do not guarantee ordering with
a8e0aead7   Will Deacon   documentation: me...
2526
       respect to normal memory accesses (e.g. DMA buffers) nor do they guarantee
0b6fa347d   SeongJae Park   locking/Documenta...
2527
2528
       ordering with respect to LOCK or UNLOCK operations.  If the latter is
       required, an mmiowb() barrier can be used.  Note that relaxed accesses to
a8e0aead7   Will Deacon   documentation: me...
2529
2530
       the same peripheral are guaranteed to be ordered with respect to each
       other.
108b42b4b   David Howells   [PATCH] Document ...
2531
2532
  
   (*) ioreadX(), iowriteX()
81fc63235   Jarek Poplawski   Documentation/mem...
2533
       These will perform appropriately for the type of access they're actually
108b42b4b   David Howells   [PATCH] Document ...
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
       doing, be it inX()/outX() or readX()/writeX().
  
  
  ========================================
  ASSUMED MINIMUM EXECUTION ORDERING MODEL
  ========================================
  
  It has to be assumed that the conceptual CPU is weakly-ordered but that it will
  maintain the appearance of program causality with respect to itself.  Some CPUs
  (such as i386 or x86_64) are more constrained than others (such as powerpc or
  frv), and so the most relaxed case (namely DEC Alpha) must be assumed outside
  of arch-specific code.
  
  This means that it must be considered that the CPU will execute its instruction
  stream in any order it feels like - or even in parallel - provided that if an
81fc63235   Jarek Poplawski   Documentation/mem...
2549
  instruction in the stream depends on an earlier instruction, then that
108b42b4b   David Howells   [PATCH] Document ...
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
  earlier instruction must be sufficiently complete[*] before the later
  instruction may proceed; in other words: provided that the appearance of
  causality is maintained.
  
   [*] Some instructions have more than one effect - such as changing the
       condition codes, changing registers or changing memory - and different
       instructions may depend on different effects.
  
  A CPU may also discard any instruction sequence that winds up having no
  ultimate effect.  For example, if two adjacent instructions both load an
  immediate value into the same register, the first may be discarded.
  
  
  Similarly, it has to be assumed that compiler might reorder the instruction
  stream in any way it sees fit, again provided the appearance of causality is
  maintained.
  
  
  ============================
  THE EFFECTS OF THE CPU CACHE
  ============================
  
  The way cached memory operations are perceived across the system is affected to
  a certain extent by the caches that lie between CPUs and memory, and by the
  memory coherence system that maintains the consistency of state in the system.
  
  As far as the way a CPU interacts with another part of the system through the
  caches goes, the memory system has to include the CPU's caches, and memory
  barriers for the most part act at the interface between the CPU and its cache
  (memory barriers logically act on the dotted line in the following diagram):
  
  	    <--- CPU --->         :       <----------- Memory ----------->
  	                          :
  	+--------+    +--------+  :   +--------+    +-----------+
  	|        |    |        |  :   |        |    |           |    +--------+
e0edc78f2   Ingo Molnar   Documentation/mem...
2585
2586
  	|  CPU   |    | Memory |  :   | CPU    |    |           |    |        |
  	|  Core  |--->| Access |----->| Cache  |<-->|           |    |        |
108b42b4b   David Howells   [PATCH] Document ...
2587
  	|        |    | Queue  |  :   |        |    |           |--->| Memory |
e0edc78f2   Ingo Molnar   Documentation/mem...
2588
2589
  	|        |    |        |  :   |        |    |           |    |        |
  	+--------+    +--------+  :   +--------+    |           |    |        |
108b42b4b   David Howells   [PATCH] Document ...
2590
2591
2592
2593
2594
2595
  	                          :                 | Cache     |    +--------+
  	                          :                 | Coherency |
  	                          :                 | Mechanism |    +--------+
  	+--------+    +--------+  :   +--------+    |           |    |	      |
  	|        |    |        |  :   |        |    |           |    |        |
  	|  CPU   |    | Memory |  :   | CPU    |    |           |--->| Device |
e0edc78f2   Ingo Molnar   Documentation/mem...
2596
2597
  	|  Core  |--->| Access |----->| Cache  |<-->|           |    |        |
  	|        |    | Queue  |  :   |        |    |           |    |        |
108b42b4b   David Howells   [PATCH] Document ...
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
  	|        |    |        |  :   |        |    |           |    +--------+
  	+--------+    +--------+  :   +--------+    +-----------+
  	                          :
  	                          :
  
  Although any particular load or store may not actually appear outside of the
  CPU that issued it since it may have been satisfied within the CPU's own cache,
  it will still appear as if the full memory access had taken place as far as the
  other CPUs are concerned since the cache coherency mechanisms will migrate the
  cacheline over to the accessing CPU and propagate the effects upon conflict.
  
  The CPU core may execute instructions in any order it deems fit, provided the
  expected program causality appears to be maintained.  Some of the instructions
  generate load and store operations which then go into the queue of memory
  accesses to be performed.  The core may place these in the queue in any order
  it wishes, and continue execution until it is forced to wait for an instruction
  to complete.
  
  What memory barriers are concerned with is controlling the order in which
  accesses cross from the CPU side of things to the memory side of things, and
  the order in which the effects are perceived to happen by the other observers
  in the system.
  
  [!] Memory barriers are _not_ needed within a given CPU, as CPUs always see
  their own loads and stores as if they had happened in program order.
  
  [!] MMIO or other device accesses may bypass the cache system.  This depends on
  the properties of the memory window through which devices are accessed and/or
  the use of any special device communication instructions the CPU may have.
  
  
  CACHE COHERENCY
  ---------------
  
  Life isn't quite as simple as it may appear above, however: for while the
  caches are expected to be coherent, there's no guarantee that that coherency
  will be ordered.  This means that whilst changes made on one CPU will
  eventually become visible on all CPUs, there's no guarantee that they will
  become apparent in the same order on those other CPUs.
81fc63235   Jarek Poplawski   Documentation/mem...
2637
2638
  Consider dealing with a system that has a pair of CPUs (1 & 2), each of which
  has a pair of parallel data caches (CPU 1 has A/B, and CPU 2 has C/D):
108b42b4b   David Howells   [PATCH] Document ...
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
  
  	            :
  	            :                          +--------+
  	            :      +---------+         |        |
  	+--------+  : +--->| Cache A |<------->|        |
  	|        |  : |    +---------+         |        |
  	|  CPU 1 |<---+                        |        |
  	|        |  : |    +---------+         |        |
  	+--------+  : +--->| Cache B |<------->|        |
  	            :      +---------+         |        |
  	            :                          | Memory |
  	            :      +---------+         | System |
  	+--------+  : +--->| Cache C |<------->|        |
  	|        |  : |    +---------+         |        |
  	|  CPU 2 |<---+                        |        |
  	|        |  : |    +---------+         |        |
  	+--------+  : +--->| Cache D |<------->|        |
  	            :      +---------+         |        |
  	            :                          +--------+
  	            :
  
  Imagine the system has the following properties:
  
   (*) an odd-numbered cache line may be in cache A, cache C or it may still be
       resident in memory;
  
   (*) an even-numbered cache line may be in cache B, cache D or it may still be
       resident in memory;
  
   (*) whilst the CPU core is interrogating one cache, the other cache may be
       making use of the bus to access the rest of the system - perhaps to
       displace a dirty cacheline or to do a speculative load;
  
   (*) each cache has a queue of operations that need to be applied to that cache
       to maintain coherency with the rest of the system;
  
   (*) the coherency queue is not flushed by normal loads to lines already
       present in the cache, even though the contents of the queue may
81fc63235   Jarek Poplawski   Documentation/mem...
2677
       potentially affect those loads.
108b42b4b   David Howells   [PATCH] Document ...
2678
2679
2680
2681
2682
2683
2684
2685
2686
  
  Imagine, then, that two writes are made on the first CPU, with a write barrier
  between them to guarantee that they will appear to reach that CPU's caches in
  the requisite order:
  
  	CPU 1		CPU 2		COMMENT
  	===============	===============	=======================================
  					u == 0, v == 1 and p == &u, q == &u
  	v = 2;
81fc63235   Jarek Poplawski   Documentation/mem...
2687
  	smp_wmb();			Make sure change to v is visible before
108b42b4b   David Howells   [PATCH] Document ...
2688
2689
2690
2691
2692
2693
2694
  					 change to p
  	<A:modify v=2>			v is now in cache A exclusively
  	p = &v;
  	<B:modify p=&v>			p is now in cache B exclusively
  
  The write memory barrier forces the other CPUs in the system to perceive that
  the local CPU's caches have apparently been updated in the correct order.  But
81fc63235   Jarek Poplawski   Documentation/mem...
2695
  now imagine that the second CPU wants to read those values:
108b42b4b   David Howells   [PATCH] Document ...
2696
2697
2698
2699
2700
2701
  
  	CPU 1		CPU 2		COMMENT
  	===============	===============	=======================================
  	...
  			q = p;
  			x = *q;
81fc63235   Jarek Poplawski   Documentation/mem...
2702
  The above pair of reads may then fail to happen in the expected order, as the
108b42b4b   David Howells   [PATCH] Document ...
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
  cacheline holding p may get updated in one of the second CPU's caches whilst
  the update to the cacheline holding v is delayed in the other of the second
  CPU's caches by some other cache event:
  
  	CPU 1		CPU 2		COMMENT
  	===============	===============	=======================================
  					u == 0, v == 1 and p == &u, q == &u
  	v = 2;
  	smp_wmb();
  	<A:modify v=2>	<C:busy>
  			<C:queue v=2>
79afecfaa   Aneesh Kumar   [PATCH] Fix typos...
2714
  	p = &v;		q = p;
108b42b4b   David Howells   [PATCH] Document ...
2715
2716
  			<D:request p>
  	<B:modify p=&v>	<D:commit p=&v>
e0edc78f2   Ingo Molnar   Documentation/mem...
2717
  			<D:read p>
108b42b4b   David Howells   [PATCH] Document ...
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
  			x = *q;
  			<C:read *q>	Reads from v before v updated in cache
  			<C:unbusy>
  			<C:commit v=2>
  
  Basically, whilst both cachelines will be updated on CPU 2 eventually, there's
  no guarantee that, without intervention, the order of update will be the same
  as that committed on CPU 1.
  
  
  To intervene, we need to interpolate a data dependency barrier or a read
  barrier between the loads.  This will force the cache to commit its coherency
  queue before processing any further requests:
  
  	CPU 1		CPU 2		COMMENT
  	===============	===============	=======================================
  					u == 0, v == 1 and p == &u, q == &u
  	v = 2;
  	smp_wmb();
  	<A:modify v=2>	<C:busy>
  			<C:queue v=2>
3fda982c5   Paolo 'Blaisorblade' Giarrusso   [PATCH] fix typo ...
2739
  	p = &v;		q = p;
108b42b4b   David Howells   [PATCH] Document ...
2740
2741
  			<D:request p>
  	<B:modify p=&v>	<D:commit p=&v>
e0edc78f2   Ingo Molnar   Documentation/mem...
2742
  			<D:read p>
108b42b4b   David Howells   [PATCH] Document ...
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
  			smp_read_barrier_depends()
  			<C:unbusy>
  			<C:commit v=2>
  			x = *q;
  			<C:read *q>	Reads from v after v updated in cache
  
  
  This sort of problem can be encountered on DEC Alpha processors as they have a
  split cache that improves performance by making better use of the data bus.
  Whilst most CPUs do imply a data dependency barrier on the read when a memory
  access depends on a read, not all do, so it may not be relied on.
  
  Other CPUs may also have split caches, but must coordinate between the various
3f6dee9b2   Matt LaPlante   Fix some typos in...
2756
  cachelets for normal memory accesses.  The semantics of the Alpha removes the
81fc63235   Jarek Poplawski   Documentation/mem...
2757
  need for coordination in the absence of memory barriers.
108b42b4b   David Howells   [PATCH] Document ...
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
  
  
  CACHE COHERENCY VS DMA
  ----------------------
  
  Not all systems maintain cache coherency with respect to devices doing DMA.  In
  such cases, a device attempting DMA may obtain stale data from RAM because
  dirty cache lines may be resident in the caches of various CPUs, and may not
  have been written back to RAM yet.  To deal with this, the appropriate part of
  the kernel must flush the overlapping bits of cache on each CPU (and maybe
  invalidate them as well).
  
  In addition, the data DMA'd to RAM by a device may be overwritten by dirty
  cache lines being written back to RAM from a CPU's cache after the device has
81fc63235   Jarek Poplawski   Documentation/mem...
2772
2773
2774
2775
  installed its own data, or cache lines present in the CPU's cache may simply
  obscure the fact that RAM has been updated, until at such time as the cacheline
  is discarded from the CPU's cache and reloaded.  To deal with this, the
  appropriate part of the kernel must invalidate the overlapping bits of the
108b42b4b   David Howells   [PATCH] Document ...
2776
2777
2778
2779
2780
2781
2782
2783
2784
  cache on each CPU.
  
  See Documentation/cachetlb.txt for more information on cache management.
  
  
  CACHE COHERENCY VS MMIO
  -----------------------
  
  Memory mapped I/O usually takes place through memory locations that are part of
81fc63235   Jarek Poplawski   Documentation/mem...
2785
  a window in the CPU's memory space that has different properties assigned than
108b42b4b   David Howells   [PATCH] Document ...
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
  the usual RAM directed window.
  
  Amongst these properties is usually the fact that such accesses bypass the
  caching entirely and go directly to the device buses.  This means MMIO accesses
  may, in effect, overtake accesses to cached memory that were emitted earlier.
  A memory barrier isn't sufficient in such a case, but rather the cache must be
  flushed between the cached memory write and the MMIO access if the two are in
  any way dependent.
  
  
  =========================
  THE THINGS CPUS GET UP TO
  =========================
  
  A programmer might take it for granted that the CPU will perform memory
81fc63235   Jarek Poplawski   Documentation/mem...
2801
  operations in exactly the order specified, so that if the CPU is, for example,
108b42b4b   David Howells   [PATCH] Document ...
2802
  given the following piece of code to execute:
9af194cef   Paul E. McKenney   documentation: Re...
2803
2804
2805
2806
2807
  	a = READ_ONCE(*A);
  	WRITE_ONCE(*B, b);
  	c = READ_ONCE(*C);
  	d = READ_ONCE(*D);
  	WRITE_ONCE(*E, e);
108b42b4b   David Howells   [PATCH] Document ...
2808

81fc63235   Jarek Poplawski   Documentation/mem...
2809
  they would then expect that the CPU will complete the memory operation for each
108b42b4b   David Howells   [PATCH] Document ...
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
  instruction before moving on to the next one, leading to a definite sequence of
  operations as seen by external observers in the system:
  
  	LOAD *A, STORE *B, LOAD *C, LOAD *D, STORE *E.
  
  
  Reality is, of course, much messier.  With many CPUs and compilers, the above
  assumption doesn't hold because:
  
   (*) loads are more likely to need to be completed immediately to permit
       execution progress, whereas stores can often be deferred without a
       problem;
  
   (*) loads may be done speculatively, and the result discarded should it prove
       to have been unnecessary;
81fc63235   Jarek Poplawski   Documentation/mem...
2825
2826
   (*) loads may be done speculatively, leading to the result having been fetched
       at the wrong time in the expected sequence of events;
108b42b4b   David Howells   [PATCH] Document ...
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
  
   (*) the order of the memory accesses may be rearranged to promote better use
       of the CPU buses and caches;
  
   (*) loads and stores may be combined to improve performance when talking to
       memory or I/O hardware that can do batched accesses of adjacent locations,
       thus cutting down on transaction setup costs (memory and PCI devices may
       both be able to do this); and
  
   (*) the CPU's data cache may affect the ordering, and whilst cache-coherency
       mechanisms may alleviate this - once the store has actually hit the cache
       - there's no guarantee that the coherency management will be propagated in
       order to other CPUs.
  
  So what another CPU, say, might actually observe from the above piece of code
  is:
  
  	LOAD *A, ..., LOAD {*C,*D}, STORE *E, STORE *B
  
  	(Where "LOAD {*C,*D}" is a combined load)
  
  
  However, it is guaranteed that a CPU will be self-consistent: it will see its
  _own_ accesses appear to be correctly ordered, without the need for a memory
  barrier.  For instance with the following code:
9af194cef   Paul E. McKenney   documentation: Re...
2852
2853
2854
2855
2856
2857
  	U = READ_ONCE(*A);
  	WRITE_ONCE(*A, V);
  	WRITE_ONCE(*A, W);
  	X = READ_ONCE(*A);
  	WRITE_ONCE(*A, Y);
  	Z = READ_ONCE(*A);
108b42b4b   David Howells   [PATCH] Document ...
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
  
  and assuming no intervention by an external influence, it can be assumed that
  the final result will appear to be:
  
  	U == the original value of *A
  	X == W
  	Z == Y
  	*A == Y
  
  The code above may cause the CPU to generate the full sequence of memory
  accesses:
  
  	U=LOAD *A, STORE *A=V, STORE *A=W, X=LOAD *A, STORE *A=Y, Z=LOAD *A
  
  in that order, but, without intervention, the sequence may have almost any
9af194cef   Paul E. McKenney   documentation: Re...
2873
2874
2875
2876
2877
2878
2879
2880
  combination of elements combined or discarded, provided the program's view
  of the world remains consistent.  Note that READ_ONCE() and WRITE_ONCE()
  are -not- optional in the above example, as there are architectures
  where a given CPU might reorder successive loads to the same location.
  On such architectures, READ_ONCE() and WRITE_ONCE() do whatever is
  necessary to prevent this, for example, on Itanium the volatile casts
  used by READ_ONCE() and WRITE_ONCE() cause GCC to emit the special ld.acq
  and st.rel instructions (respectively) that prevent such reordering.
108b42b4b   David Howells   [PATCH] Document ...
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
  
  The compiler may also combine, discard or defer elements of the sequence before
  the CPU even sees them.
  
  For instance:
  
  	*A = V;
  	*A = W;
  
  may be reduced to:
  
  	*A = W;
9af194cef   Paul E. McKenney   documentation: Re...
2893
  since, without either a write barrier or an WRITE_ONCE(), it can be
2ecf81012   Paul E. McKenney   Documentation/mem...
2894
  assumed that the effect of the storage of V to *A is lost.  Similarly:
108b42b4b   David Howells   [PATCH] Document ...
2895
2896
2897
  
  	*A = Y;
  	Z = *A;
9af194cef   Paul E. McKenney   documentation: Re...
2898
2899
  may, without a memory barrier or an READ_ONCE() and WRITE_ONCE(), be
  reduced to:
108b42b4b   David Howells   [PATCH] Document ...
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
  
  	*A = Y;
  	Z = Y;
  
  and the LOAD operation never appear outside of the CPU.
  
  
  AND THEN THERE'S THE ALPHA
  --------------------------
  
  The DEC Alpha CPU is one of the most relaxed CPUs there is.  Not only that,
  some versions of the Alpha CPU have a split data cache, permitting them to have
81fc63235   Jarek Poplawski   Documentation/mem...
2912
  two semantically-related cache lines updated at separate times.  This is where
108b42b4b   David Howells   [PATCH] Document ...
2913
2914
2915
  the data dependency barrier really becomes necessary as this synchronises both
  caches with the memory coherence system, thus making it seem like pointer
  changes vs new data occur in the right order.
81fc63235   Jarek Poplawski   Documentation/mem...
2916
  The Alpha defines the Linux kernel's memory barrier model.
108b42b4b   David Howells   [PATCH] Document ...
2917
2918
  
  See the subsection on "Cache Coherency" above.
0b6fa347d   SeongJae Park   locking/Documenta...
2919

6a65d2638   Michael S. Tsirkin   asm-generic: impl...
2920
  VIRTUAL MACHINE GUESTS
3dbf0913f   SeongJae Park   locking/Documenta...
2921
  ----------------------
6a65d2638   Michael S. Tsirkin   asm-generic: impl...
2922
2923
2924
2925
2926
2927
2928
2929
  
  Guests running within virtual machines might be affected by SMP effects even if
  the guest itself is compiled without SMP support.  This is an artifact of
  interfacing with an SMP host while running an UP kernel.  Using mandatory
  barriers for this use-case would be possible but is often suboptimal.
  
  To handle this case optimally, low-level virt_mb() etc macros are available.
  These have the same effect as smp_mb() etc when SMP is enabled, but generate
0b6fa347d   SeongJae Park   locking/Documenta...
2930
  identical code for SMP and non-SMP systems.  For example, virtual machine guests
6a65d2638   Michael S. Tsirkin   asm-generic: impl...
2931
2932
2933
2934
2935
2936
  should use virt_mb() rather than smp_mb() when synchronizing against a
  (possibly SMP) host.
  
  These are equivalent to smp_mb() etc counterparts in all other respects,
  in particular, they do not control MMIO effects: to control
  MMIO effects, use mandatory barriers.
108b42b4b   David Howells   [PATCH] Document ...
2937

0b6fa347d   SeongJae Park   locking/Documenta...
2938

90fddabf5   David Howells   Document Linux's ...
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
  ============
  EXAMPLE USES
  ============
  
  CIRCULAR BUFFERS
  ----------------
  
  Memory barriers can be used to implement circular buffering without the need
  of a lock to serialise the producer with the consumer.  See:
  
  	Documentation/circular-buffers.txt
  
  for details.
108b42b4b   David Howells   [PATCH] Document ...
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
  ==========
  REFERENCES
  ==========
  
  Alpha AXP Architecture Reference Manual, Second Edition (Sites & Witek,
  Digital Press)
  	Chapter 5.2: Physical Address Space Characteristics
  	Chapter 5.4: Caches and Write Buffers
  	Chapter 5.5: Data Sharing
  	Chapter 5.6: Read/Write Ordering
  
  AMD64 Architecture Programmer's Manual Volume 2: System Programming
  	Chapter 7.1: Memory-Access Ordering
  	Chapter 7.4: Buffering and Combining Memory Writes
  
  IA-32 Intel Architecture Software Developer's Manual, Volume 3:
  System Programming Guide
  	Chapter 7.1: Locked Atomic Operations
  	Chapter 7.2: Memory Ordering
  	Chapter 7.4: Serializing Instructions
  
  The SPARC Architecture Manual, Version 9
  	Chapter 8: Memory Models
  	Appendix D: Formal Specification of the Memory Models
  	Appendix J: Programming with the Memory Models
  
  UltraSPARC Programmer Reference Manual
  	Chapter 5: Memory Accesses and Cacheability
  	Chapter 15: Sparc-V9 Memory Models
  
  UltraSPARC III Cu User's Manual
  	Chapter 9: Memory Models
  
  UltraSPARC IIIi Processor User's Manual
  	Chapter 8: Memory Models
  
  UltraSPARC Architecture 2005
  	Chapter 9: Memory
  	Appendix D: Formal Specifications of the Memory Models
  
  UltraSPARC T1 Supplement to the UltraSPARC Architecture 2005
  	Chapter 8: Memory Models
  	Appendix F: Caches and Cache Coherency
  
  Solaris Internals, Core Kernel Architecture, p63-68:
  	Chapter 3.3: Hardware Considerations for Locks and
  			Synchronization
  
  Unix Systems for Modern Architectures, Symmetric Multiprocessing and Caching
  for Kernel Programmers:
  	Chapter 13: Other Memory Models
  
  Intel Itanium Architecture Software Developer's Manual: Volume 1:
  	Section 2.6: Speculation
  	Section 4.4: Memory Access