Keywords: perfbook_html

### 14.2.4.3 Pair-Wise Memory Barriers

Pair-wise memory barriers provide conditional ordering semantics. For example, in the following set of operations, CPU 1's access to A does not unconditionally precede its access to B from the viewpoint of an external logic analyzer (see Appendix for examples). (the system is only to act as if the accesses are in order; it is not necessarily required to actually force them to be in order). However, if CPU 2's access to B sees the result of CPU 1's access to B, then CPU 2's access to A is guaranteed to see the result of CPU 1's access to A. Although some CPUs' memory barriers do in fact provide stronger, unconditional ordering guarantees, portable code may rely only on this weaker if-then conditional ordering guarantee.

`CPU 1` | `CPU 2`
`access(A);` | `access(B);`
`smp_mb();` | `smp_mb();`
`access(B);` | `access(A);`

Quick Quiz 14.6: But if the memory barriers do not unconditionally force ordering, how the heck can a device driver reliably execute sequences of loads and stores to MMIO registers? End Quick Quiz

Of course, accesses must be either loads or stores, and these do have different properties. Table shows all possible combinations of loads and stores from a pair of CPUs. Of course, to enforce conditional ordering, there must be a memory barrier between each CPU's pair of operations.

Table: Memory-Barrier Combinations
| CPU 1 | CPU 2 | Description
0 | load(A) | load(B) | load(B) | load(A) | Ears to ears.
1 | load(A) | load(B) | load(B) | store(A) | Only one store.
2 | load(A) | load(B) | store(B) | load(A) | Only one store.
3 | load(A) | load(B) | store(B) | store(A) | Pairing 1.
4 | load(A) | store(B) | load(B) | load(A) | Only one store.
5 | load(A) | store(B) | load(B) | store(A) | Pairing 2.
6 | load(A) | store(B) | store(B) | load(A) | Mouth to mouth, ear to ear.
7 | load(A) | store(B) | store(B) | store(A) | Pairing 3.
8 | store(A) | load(B) | load(B) | load(A) | Only one store.
9 | store(A) | load(B) | load(B) | store(A) | Mouth to mouth, ear to ear.
A | store(A) | load(B) | store(B) | load(A) | Ears to mouths.
B | store(A) | load(B) | store(B) | store(A) | Stores ``pass in the night''.
C | store(A) | store(B) | load(B) | load(A) | Pairing 1.
D | store(A) | store(B) | load(B) | store(A) | Pairing 3.
E | store(A) | store(B) | store(B) | load(A) | Stores ``pass in the night''.
F | store(A) | store(B) | store(B) | store(A) | Stores ``pass in the night''.