1// SPDX-License-Identifier: GPL-2.0+
2(*
3 * Copyright (C) 2015 Jade Alglave <j.alglave@ucl.ac.uk>,
4 * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria
5 * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu>,
6 *                    Andrea Parri <parri.andrea@gmail.com>
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	([M] ; po ; [UL] ; (co | po) ; [LKW] ;
41		fencerel(After-unlock-lock) ; [M])
42let gp = po ; [Sync-rcu | Sync-srcu] ; po?
43let strong-fence = mb | gp
44
45let nonrw-fence = strong-fence | po-rel | acq-po
46let fence = nonrw-fence | wmb | rmb
47let barrier = fencerel(Barrier | Rmb | Wmb | Mb | Sync-rcu | Sync-srcu |
48		Before-atomic | After-atomic | Acquire | Release |
49		Rcu-lock | Rcu-unlock | Srcu-lock | Srcu-unlock) |
50	(po ; [Release]) | ([Acquire] ; po)
51
52(**********************************)
53(* Fundamental coherence ordering *)
54(**********************************)
55
56(* Sequential Consistency Per Variable *)
57let com = rf | co | fr
58acyclic po-loc | com as coherence
59
60(* Atomic Read-Modify-Write *)
61empty rmw & (fre ; coe) as atomic
62
63(**********************************)
64(* Instruction execution ordering *)
65(**********************************)
66
67(* Preserved Program Order *)
68let dep = addr | data
69let rwdep = (dep | ctrl) ; [W]
70let overwrite = co | fr
71let to-w = rwdep | (overwrite & int) | (addr ; [Plain] ; wmb)
72let to-r = addr | (dep ; [Marked] ; rfi)
73let ppo = to-r | to-w | fence | (po-unlock-lock-po & int)
74
75(* Propagation: Ordering from release operations and strong fences. *)
76let A-cumul(r) = (rfe ; [Marked])? ; r
77let rmw-sequence = (rf ; rmw)*
78let cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb |
79	po-unlock-lock-po) ; [Marked] ; rmw-sequence
80let prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ;
81	[Marked] ; rfe? ; [Marked]
82
83(*
84 * Happens Before: Ordering from the passage of time.
85 * No fences needed here for prop because relation confined to one process.
86 *)
87let hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked]
88acyclic hb as happens-before
89
90(****************************************)
91(* Write and fence propagation ordering *)
92(****************************************)
93
94(* Propagation: Each non-rf link needs a strong fence. *)
95let pb = prop ; strong-fence ; hb* ; [Marked]
96acyclic pb as propagation
97
98(*******)
99(* RCU *)
100(*******)
101
102(*
103 * Effects of read-side critical sections proceed from the rcu_read_unlock()
104 * or srcu_read_unlock() backwards on the one hand, and from the
105 * rcu_read_lock() or srcu_read_lock() forwards on the other hand.
106 *
107 * In the definition of rcu-fence below, the po term at the left-hand side
108 * of each disjunct and the po? term at the right-hand end have been factored
109 * out.  They have been moved into the definitions of rcu-link and rb.
110 * This was necessary in order to apply the "& loc" tests correctly.
111 *)
112let rcu-gp = [Sync-rcu]		(* Compare with gp *)
113let srcu-gp = [Sync-srcu]
114let rcu-rscsi = rcu-rscs^-1
115let srcu-rscsi = srcu-rscs^-1
116
117(*
118 * The synchronize_rcu() strong fence is special in that it can order not
119 * one but two non-rf relations, but only in conjunction with an RCU
120 * read-side critical section.
121 *)
122let rcu-link = po? ; hb* ; pb* ; prop ; po
123
124(*
125 * Any sequence containing at least as many grace periods as RCU read-side
126 * critical sections (joined by rcu-link) induces order like a generalized
127 * inter-CPU strong fence.
128 * Likewise for SRCU grace periods and read-side critical sections, provided
129 * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same
130 * struct srcu_struct location.
131 *)
132let rec rcu-order = rcu-gp | srcu-gp |
133	(rcu-gp ; rcu-link ; rcu-rscsi) |
134	((srcu-gp ; rcu-link ; srcu-rscsi) & loc) |
135	(rcu-rscsi ; rcu-link ; rcu-gp) |
136	((srcu-rscsi ; rcu-link ; srcu-gp) & loc) |
137	(rcu-gp ; rcu-link ; rcu-order ; rcu-link ; rcu-rscsi) |
138	((srcu-gp ; rcu-link ; rcu-order ; rcu-link ; srcu-rscsi) & loc) |
139	(rcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; rcu-gp) |
140	((srcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; srcu-gp) & loc) |
141	(rcu-order ; rcu-link ; rcu-order)
142let rcu-fence = po ; rcu-order ; po?
143let fence = fence | rcu-fence
144let strong-fence = strong-fence | rcu-fence
145
146(* rb orders instructions just as pb does *)
147let rb = prop ; rcu-fence ; hb* ; pb* ; [Marked]
148
149irreflexive rb as rcu
150
151(*
152 * The happens-before, propagation, and rcu constraints are all
153 * expressions of temporal ordering.  They could be replaced by
154 * a single constraint on an "executes-before" relation, xb:
155 *
156 * let xb = hb | pb | rb
157 * acyclic xb as executes-before
158 *)
159
160(*********************************)
161(* Plain accesses and data races *)
162(*********************************)
163
164(* Warn about plain writes and marked accesses in the same region *)
165let mixed-accesses = ([Plain & W] ; (po-loc \ barrier) ; [Marked]) |
166	([Marked] ; (po-loc \ barrier) ; [Plain & W])
167flag ~empty mixed-accesses as mixed-accesses
168
169(* Executes-before and visibility *)
170let xbstar = (hb | pb | rb)*
171let vis = cumul-fence* ; rfe? ; [Marked] ;
172	((strong-fence ; [Marked] ; xbstar) | (xbstar & int))
173
174(* Boundaries for lifetimes of plain accesses *)
175let w-pre-bounded = [Marked] ; (addr | fence)?
176let r-pre-bounded = [Marked] ; (addr | nonrw-fence |
177	([R4rmb] ; fencerel(Rmb) ; [~Noreturn]))?
178let w-post-bounded = fence? ; [Marked] ; rmw-sequence
179let r-post-bounded = (nonrw-fence | ([~Noreturn] ; fencerel(Rmb) ; [R4rmb]))? ;
180	[Marked]
181
182(* Visibility and executes-before for plain accesses *)
183let ww-vis = fence | (strong-fence ; xbstar ; w-pre-bounded) |
184	(w-post-bounded ; vis ; w-pre-bounded)
185let wr-vis = fence | (strong-fence ; xbstar ; r-pre-bounded) |
186	(w-post-bounded ; vis ; r-pre-bounded)
187let rw-xbstar = fence | (r-post-bounded ; xbstar ; w-pre-bounded)
188
189(* Potential races *)
190let pre-race = ext & ((Plain * M) | ((M \ IW) * Plain))
191
192(* Coherence requirements for plain accesses *)
193let wr-incoh = pre-race & rf & rw-xbstar^-1
194let rw-incoh = pre-race & fr & wr-vis^-1
195let ww-incoh = pre-race & co & ww-vis^-1
196empty (wr-incoh | rw-incoh | ww-incoh) as plain-coherence
197
198(* Actual races *)
199let ww-nonrace = ww-vis & ((Marked * W) | rw-xbstar) & ((W * Marked) | wr-vis)
200let ww-race = (pre-race & co) \ ww-nonrace
201let wr-race = (pre-race & (co? ; rf)) \ wr-vis \ rw-xbstar^-1
202let rw-race = (pre-race & fr) \ rw-xbstar
203
204flag ~empty (ww-race | wr-race | rw-race) as data-race
205