]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - tools/memory-model/linux-kernel.cat
tools/memory-model: Do not use "herd" to refer to "herd7"
[mirror_ubuntu-jammy-kernel.git] / tools / memory-model / linux-kernel.cat
CommitLineData
1c27b644
PM
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 *
1a00b455 8 * An earlier version of this file appeared in the companion webpage for
1c27b644
PM
9 * "Frightening small children and disconcerting grown-ups: Concurrency
10 * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern,
1a00b455 11 * which appeared in ASPLOS 2018.
1c27b644
PM
12 *)
13
48d44d4e 14"Linux-kernel memory consistency model"
1c27b644
PM
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
4494dd58
AS
27(* Release Acquire *)
28let acq-po = [Acquire] ; po ; [M]
29let po-rel = [M] ; po ; [Release]
30let po-unlock-rf-lock-po = po ; [UL] ; rf ; [LKR] ; po
31
1c27b644 32(* Fences *)
4494dd58
AS
33let R4rmb = R \ Noreturn (* Reads for which rmb works *)
34let rmb = [R4rmb] ; fencerel(Rmb) ; [R4rmb]
1c27b644
PM
35let wmb = [W] ; fencerel(Wmb) ; [W]
36let mb = ([M] ; fencerel(Mb) ; [M]) |
cac79a39
PM
37 ([M] ; fencerel(Before-atomic) ; [RMW] ; po? ; [M]) |
38 ([M] ; po? ; [RMW] ; fencerel(After-atomic) ; [M]) |
5b735eb1
AP
39 ([M] ; po? ; [LKW] ; fencerel(After-spinlock) ; [M]) |
40 ([M] ; po ; [UL] ; (co | po) ; [LKW] ;
41 fencerel(After-unlock-lock) ; [M])
a3f600d9 42let gp = po ; [Sync-rcu | Sync-srcu] ; po?
1c27b644
PM
43let strong-fence = mb | gp
44
4494dd58
AS
45let nonrw-fence = strong-fence | po-rel | acq-po
46let fence = nonrw-fence | wmb | rmb
0031e38a
AS
47let barrier = fencerel(Barrier | Rmb | Wmb | Mb | Sync-rcu | Sync-srcu |
48 Before-atomic | After-atomic | Acquire | Release) |
49 (po ; [Release]) | ([Acquire] ; po)
1c27b644
PM
50
51(**********************************)
52(* Fundamental coherence ordering *)
53(**********************************)
54
55(* Sequential Consistency Per Variable *)
56let com = rf | co | fr
57acyclic po-loc | com as coherence
58
59(* Atomic Read-Modify-Write *)
60empty rmw & (fre ; coe) as atomic
61
62(**********************************)
63(* Instruction execution ordering *)
64(**********************************)
65
66(* Preserved Program Order *)
67let dep = addr | data
68let rwdep = (dep | ctrl) ; [W]
69let overwrite = co | fr
0031e38a 70let to-w = rwdep | (overwrite & int) | (addr ; [Plain] ; wmb)
d1a84ab1 71let to-r = addr | (dep ; [Marked] ; rfi)
6e89e831 72let ppo = to-r | to-w | fence | (po-unlock-rf-lock-po & int)
1c27b644
PM
73
74(* Propagation: Ordering from release operations and strong fences. *)
d1a84ab1
AS
75let A-cumul(r) = (rfe ; [Marked])? ; r
76let cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb |
77 po-unlock-rf-lock-po) ; [Marked]
78let prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ;
79 [Marked] ; rfe? ; [Marked]
1c27b644
PM
80
81(*
82 * Happens Before: Ordering from the passage of time.
83 * No fences needed here for prop because relation confined to one process.
84 *)
d1a84ab1 85let hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked]
1c27b644
PM
86acyclic hb as happens-before
87
88(****************************************)
89(* Write and fence propagation ordering *)
90(****************************************)
91
92(* Propagation: Each non-rf link needs a strong fence. *)
d1a84ab1 93let pb = prop ; strong-fence ; hb* ; [Marked]
1c27b644
PM
94acyclic pb as propagation
95
96(*******)
97(* RCU *)
98(*******)
99
100(*
284749b0 101 * Effects of read-side critical sections proceed from the rcu_read_unlock()
a3f600d9
AS
102 * or srcu_read_unlock() backwards on the one hand, and from the
103 * rcu_read_lock() or srcu_read_lock() forwards on the other hand.
284749b0
AS
104 *
105 * In the definition of rcu-fence below, the po term at the left-hand side
106 * of each disjunct and the po? term at the right-hand end have been factored
107 * out. They have been moved into the definitions of rcu-link and rb.
a3f600d9 108 * This was necessary in order to apply the "& loc" tests correctly.
1c27b644 109 *)
284749b0 110let rcu-gp = [Sync-rcu] (* Compare with gp *)
a3f600d9 111let srcu-gp = [Sync-srcu]
284749b0 112let rcu-rscsi = rcu-rscs^-1
a3f600d9 113let srcu-rscsi = srcu-rscs^-1
1c27b644
PM
114
115(*
116 * The synchronize_rcu() strong fence is special in that it can order not
117 * one but two non-rf relations, but only in conjunction with an RCU
118 * read-side critical section.
119 *)
284749b0 120let rcu-link = po? ; hb* ; pb* ; prop ; po
1c27b644 121
1c27b644 122(*
9d036883
AS
123 * Any sequence containing at least as many grace periods as RCU read-side
124 * critical sections (joined by rcu-link) acts as a generalized strong fence.
a3f600d9
AS
125 * Likewise for SRCU grace periods and read-side critical sections, provided
126 * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same
127 * struct srcu_struct location.
1c27b644 128 *)
a3f600d9 129let rec rcu-fence = rcu-gp | srcu-gp |
284749b0 130 (rcu-gp ; rcu-link ; rcu-rscsi) |
a3f600d9 131 ((srcu-gp ; rcu-link ; srcu-rscsi) & loc) |
284749b0 132 (rcu-rscsi ; rcu-link ; rcu-gp) |
a3f600d9 133 ((srcu-rscsi ; rcu-link ; srcu-gp) & loc) |
284749b0 134 (rcu-gp ; rcu-link ; rcu-fence ; rcu-link ; rcu-rscsi) |
a3f600d9 135 ((srcu-gp ; rcu-link ; rcu-fence ; rcu-link ; srcu-rscsi) & loc) |
284749b0 136 (rcu-rscsi ; rcu-link ; rcu-fence ; rcu-link ; rcu-gp) |
a3f600d9 137 ((srcu-rscsi ; rcu-link ; rcu-fence ; rcu-link ; srcu-gp) & loc) |
9d036883
AS
138 (rcu-fence ; rcu-link ; rcu-fence)
139
140(* rb orders instructions just as pb does *)
d1a84ab1 141let rb = prop ; po ; rcu-fence ; po? ; hb* ; pb* ; [Marked]
1c27b644 142
1ee2da5f 143irreflexive rb as rcu
9d036883
AS
144
145(*
146 * The happens-before, propagation, and rcu constraints are all
147 * expressions of temporal ordering. They could be replaced by
148 * a single constraint on an "executes-before" relation, xb:
149 *
150 * let xb = hb | pb | rb
151 * acyclic xb as executes-before
152 *)
0031e38a
AS
153
154(*********************************)
155(* Plain accesses and data races *)
156(*********************************)
157
158(* Warn about plain writes and marked accesses in the same region *)
159let mixed-accesses = ([Plain & W] ; (po-loc \ barrier) ; [Marked]) |
160 ([Marked] ; (po-loc \ barrier) ; [Plain & W])
161flag ~empty mixed-accesses as mixed-accesses
162
163(* Executes-before and visibility *)
164let xbstar = (hb | pb | rb)*
165let full-fence = strong-fence | (po ; rcu-fence ; po?)
166let vis = cumul-fence* ; rfe? ; [Marked] ;
167 ((full-fence ; [Marked] ; xbstar) | (xbstar & int))
168
169(* Boundaries for lifetimes of plain accesses *)
170let w-pre-bounded = [Marked] ; (addr | fence)?
171let r-pre-bounded = [Marked] ; (addr | nonrw-fence |
172 ([R4rmb] ; fencerel(Rmb) ; [~Noreturn]))?
173let w-post-bounded = fence? ; [Marked]
174let r-post-bounded = (nonrw-fence | ([~Noreturn] ; fencerel(Rmb) ; [R4rmb]))? ;
175 [Marked]
176
177(* Visibility and executes-before for plain accesses *)
178let ww-vis = w-post-bounded ; vis ; w-pre-bounded
179let wr-vis = w-post-bounded ; vis ; r-pre-bounded
180let rw-xbstar = r-post-bounded ; xbstar ; w-pre-bounded
181
182(* Potential races *)
183let pre-race = ext & ((Plain * M) | ((M \ IW) * Plain))
184
185(* Coherence requirements for plain accesses *)
186let wr-incoh = pre-race & rf & rw-xbstar^-1
187let rw-incoh = pre-race & fr & wr-vis^-1
188let ww-incoh = pre-race & co & ww-vis^-1
189empty (wr-incoh | rw-incoh | ww-incoh) as plain-coherence
190
191(* Actual races *)
192let ww-nonrace = ww-vis & ((Marked * W) | rw-xbstar) & ((W * Marked) | wr-vis)
193let ww-race = (pre-race & co) \ ww-nonrace
194let wr-race = (pre-race & (co? ; rf)) \ wr-vis
195let rw-race = (pre-race & fr) \ rw-xbstar
196
197flag ~empty (ww-race | wr-race | rw-race) as data-race