1// SPDX-License-Identifier: GPL-2.0+
2(*
3 * Copyright (C) 2015 Jade Alglave <[email protected]>,
4 * Copyright (C) 2016 Luc Maranget <[email protected]> for Inria
5 * Copyright (C) 2017 Alan Stern <[email protected]>,
6 *                    Andrea Parri <[email protected]>
7 *
8 * An earlier version of this file appeared in the companion webpage for
9 * "Frightening small children and disconcerting grown-ups: Concurrency
10 * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern,
11 * which appeared in ASPLOS 2018.
12 *)
13
14"Linux-kernel memory consistency model"
15
16(*
17 * File "lock.cat" handles locks and is experimental.
18 * It can be replaced by include "cos.cat" for tests that do not use locks.
19 *)
20
21include "lock.cat"
22
23(*******************)
24(* Basic relations *)
25(*******************)
26
27(* Release Acquire *)
28let acq-po = [Acquire] ; po ; [M]
29let po-rel = [M] ; po ; [Release]
30let po-unlock-lock-po = po ; [UL] ; (po|rf) ; [LKR] ; po
31
32(* Fences *)
33let R4rmb = R \ Noreturn	(* Reads for which rmb works *)
34let rmb = [R4rmb] ; fencerel(Rmb) ; [R4rmb]
35let wmb = [W] ; fencerel(Wmb) ; [W]
36let mb = ([M] ; fencerel(Mb) ; [M]) |
37	([M] ; fencerel(Before-atomic) ; [RMW] ; po? ; [M]) |
38	([M] ; po? ; [RMW] ; fencerel(After-atomic) ; [M]) |
39	([M] ; po? ; [LKW] ; fencerel(After-spinlock) ; [M]) |
40(*
41 * Note: The po-unlock-lock-po relation only passes the lock to the direct
42 * successor, perhaps giving the impression that the ordering of the
43 * smp_mb__after_unlock_lock() fence only affects a single lock handover.
44 * However, in a longer sequence of lock handovers, the implicit
45 * A-cumulative release fences of lock-release ensure that any stores that
46 * propagate to one of the involved CPUs before it hands over the lock to
47 * the next CPU will also propagate to the final CPU handing over the lock
48 * to the CPU that executes the fence.  Therefore, all those stores are
49 * also affected by the fence.
50 *)
51	([M] ; po-unlock-lock-po ;
52		[After-unlock-lock] ; po ; [M])
53let gp = po ; [Sync-rcu | Sync-srcu] ; po?
54let strong-fence = mb | gp
55
56let nonrw-fence = strong-fence | po-rel | acq-po
57let fence = nonrw-fence | wmb | rmb
58let barrier = fencerel(Barrier | Rmb | Wmb | Mb | Sync-rcu | Sync-srcu |
59		Before-atomic | After-atomic | Acquire | Release |
60		Rcu-lock | Rcu-unlock | Srcu-lock | Srcu-unlock) |
61	(po ; [Release]) | ([Acquire] ; po)
62
63(**********************************)
64(* Fundamental coherence ordering *)
65(**********************************)
66
67(* Sequential Consistency Per Variable *)
68let com = rf | co | fr
69acyclic po-loc | com as coherence
70
71(* Atomic Read-Modify-Write *)
72empty rmw & (fre ; coe) as atomic
73
74(**********************************)
75(* Instruction execution ordering *)
76(**********************************)
77
78(* Preserved Program Order *)
79let dep = addr | data
80let rwdep = (dep | ctrl) ; [W]
81let overwrite = co | fr
82let to-w = rwdep | (overwrite & int) | (addr ; [Plain] ; wmb)
83let to-r = addr | (dep ; [Marked] ; rfi)
84let ppo = to-r | to-w | fence | (po-unlock-lock-po & int)
85
86(* Propagation: Ordering from release operations and strong fences. *)
87let A-cumul(r) = (rfe ; [Marked])? ; r
88let rmw-sequence = (rf ; rmw)*
89let cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb |
90	po-unlock-lock-po) ; [Marked] ; rmw-sequence
91let prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ;
92	[Marked] ; rfe? ; [Marked]
93
94(*
95 * Happens Before: Ordering from the passage of time.
96 * No fences needed here for prop because relation confined to one process.
97 *)
98let hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked]
99acyclic hb as happens-before
100
101(****************************************)
102(* Write and fence propagation ordering *)
103(****************************************)
104
105(* Propagation: Each non-rf link needs a strong fence. *)
106let pb = prop ; strong-fence ; hb* ; [Marked]
107acyclic pb as propagation
108
109(*******)
110(* RCU *)
111(*******)
112
113(*
114 * Effects of read-side critical sections proceed from the rcu_read_unlock()
115 * or srcu_read_unlock() backwards on the one hand, and from the
116 * rcu_read_lock() or srcu_read_lock() forwards on the other hand.
117 *
118 * In the definition of rcu-fence below, the po term at the left-hand side
119 * of each disjunct and the po? term at the right-hand end have been factored
120 * out.  They have been moved into the definitions of rcu-link and rb.
121 * This was necessary in order to apply the "& loc" tests correctly.
122 *)
123let rcu-gp = [Sync-rcu]		(* Compare with gp *)
124let srcu-gp = [Sync-srcu]
125let rcu-rscsi = rcu-rscs^-1
126let srcu-rscsi = srcu-rscs^-1
127
128(*
129 * The synchronize_rcu() strong fence is special in that it can order not
130 * one but two non-rf relations, but only in conjunction with an RCU
131 * read-side critical section.
132 *)
133let rcu-link = po? ; hb* ; pb* ; prop ; po
134
135(*
136 * Any sequence containing at least as many grace periods as RCU read-side
137 * critical sections (joined by rcu-link) induces order like a generalized
138 * inter-CPU strong fence.
139 * Likewise for SRCU grace periods and read-side critical sections, provided
140 * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same
141 * struct srcu_struct location.
142 *)
143let rec rcu-order = rcu-gp | srcu-gp |
144	(rcu-gp ; rcu-link ; rcu-rscsi) |
145	((srcu-gp ; rcu-link ; srcu-rscsi) & loc) |
146	(rcu-rscsi ; rcu-link ; rcu-gp) |
147	((srcu-rscsi ; rcu-link ; srcu-gp) & loc) |
148	(rcu-gp ; rcu-link ; rcu-order ; rcu-link ; rcu-rscsi) |
149	((srcu-gp ; rcu-link ; rcu-order ; rcu-link ; srcu-rscsi) & loc) |
150	(rcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; rcu-gp) |
151	((srcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; srcu-gp) & loc) |
152	(rcu-order ; rcu-link ; rcu-order)
153let rcu-fence = po ; rcu-order ; po?
154let fence = fence | rcu-fence
155let strong-fence = strong-fence | rcu-fence
156
157(* rb orders instructions just as pb does *)
158let rb = prop ; rcu-fence ; hb* ; pb* ; [Marked]
159
160irreflexive rb as rcu
161
162(*
163 * The happens-before, propagation, and rcu constraints are all
164 * expressions of temporal ordering.  They could be replaced by
165 * a single constraint on an "executes-before" relation, xb:
166 *
167 * let xb = hb | pb | rb
168 * acyclic xb as executes-before
169 *)
170
171(*********************************)
172(* Plain accesses and data races *)
173(*********************************)
174
175(* Warn about plain writes and marked accesses in the same region *)
176let mixed-accesses = ([Plain & W] ; (po-loc \ barrier) ; [Marked]) |
177	([Marked] ; (po-loc \ barrier) ; [Plain & W])
178flag ~empty mixed-accesses as mixed-accesses
179
180(* Executes-before and visibility *)
181let xbstar = (hb | pb | rb)*
182let vis = cumul-fence* ; rfe? ; [Marked] ;
183	((strong-fence ; [Marked] ; xbstar) | (xbstar & int))
184
185(* Boundaries for lifetimes of plain accesses *)
186let w-pre-bounded = [Marked] ; (addr | fence)?
187let r-pre-bounded = [Marked] ; (addr | nonrw-fence |
188	([R4rmb] ; fencerel(Rmb) ; [~Noreturn]))?
189let w-post-bounded = fence? ; [Marked] ; rmw-sequence
190let r-post-bounded = (nonrw-fence | ([~Noreturn] ; fencerel(Rmb) ; [R4rmb]))? ;
191	[Marked]
192
193(* Visibility and executes-before for plain accesses *)
194let ww-vis = fence | (strong-fence ; xbstar ; w-pre-bounded) |
195	(w-post-bounded ; vis ; w-pre-bounded)
196let wr-vis = fence | (strong-fence ; xbstar ; r-pre-bounded) |
197	(w-post-bounded ; vis ; r-pre-bounded)
198let rw-xbstar = fence | (r-post-bounded ; xbstar ; w-pre-bounded)
199
200(* Potential races *)
201let pre-race = ext & ((Plain * M) | ((M \ IW) * Plain))
202
203(* Coherence requirements for plain accesses *)
204let wr-incoh = pre-race & rf & rw-xbstar^-1
205let rw-incoh = pre-race & fr & wr-vis^-1
206let ww-incoh = pre-race & co & ww-vis^-1
207empty (wr-incoh | rw-incoh | ww-incoh) as plain-coherence
208
209(* Actual races *)
210let ww-nonrace = ww-vis & ((Marked * W) | rw-xbstar) & ((W * Marked) | wr-vis)
211let ww-race = (pre-race & co) \ ww-nonrace
212let wr-race = (pre-race & (co? ; rf)) \ wr-vis \ rw-xbstar^-1
213let rw-race = (pre-race & fr) \ rw-xbstar
214
215flag ~empty (ww-race | wr-race | rw-race) as data-race
216