blob: d70315fddef6e000e57a25e2cb0a297326abc398 [file] [log] [blame]
Paul E. McKenney1c27b642018-01-18 19:58:55 -08001// 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 *
Andrea Parri1a00b4552018-05-14 16:33:56 -07008 * An earlier version of this file appeared in the companion webpage for
Paul E. McKenney1c27b642018-01-18 19:58:55 -08009 * "Frightening small children and disconcerting grown-ups: Concurrency
10 * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern,
Andrea Parri1a00b4552018-05-14 16:33:56 -070011 * which appeared in ASPLOS 2018.
Paul E. McKenney1c27b642018-01-18 19:58:55 -080012 *)
13
Andrea Parri48d44d42018-02-20 15:25:01 -080014"Linux-kernel memory consistency model"
Paul E. McKenney1c27b642018-01-18 19:58:55 -080015
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
Alan Stern4494dd52019-04-22 12:17:45 -040027(* Release Acquire *)
28let acq-po = [Acquire] ; po ; [M]
29let po-rel = [M] ; po ; [Release]
Boqun Fengddfe1292021-10-25 22:54:14 +080030let po-unlock-lock-po = po ; [UL] ; (po|rf) ; [LKR] ; po
Alan Stern4494dd52019-04-22 12:17:45 -040031
Paul E. McKenney1c27b642018-01-18 19:58:55 -080032(* Fences *)
Alan Stern4494dd52019-04-22 12:17:45 -040033let R4rmb = R \ Noreturn (* Reads for which rmb works *)
34let rmb = [R4rmb] ; fencerel(Rmb) ; [R4rmb]
Paul E. McKenney1c27b642018-01-18 19:58:55 -080035let wmb = [W] ; fencerel(Wmb) ; [W]
36let mb = ([M] ; fencerel(Mb) ; [M]) |
Paul E. McKenneycac79a392018-02-20 15:25:11 -080037 ([M] ; fencerel(Before-atomic) ; [RMW] ; po? ; [M]) |
38 ([M] ; po? ; [RMW] ; fencerel(After-atomic) ; [M]) |
Andrea Parri5b735eb2018-12-03 15:04:49 -080039 ([M] ; po? ; [LKW] ; fencerel(After-spinlock) ; [M]) |
40 ([M] ; po ; [UL] ; (co | po) ; [LKW] ;
41 fencerel(After-unlock-lock) ; [M])
Alan Sterna3f600d2018-11-15 11:20:37 -050042let gp = po ; [Sync-rcu | Sync-srcu] ; po?
Paul E. McKenney1c27b642018-01-18 19:58:55 -080043let strong-fence = mb | gp
44
Alan Stern4494dd52019-04-22 12:17:45 -040045let nonrw-fence = strong-fence | po-rel | acq-po
46let fence = nonrw-fence | wmb | rmb
Alan Stern0031e382019-04-22 12:18:09 -040047let barrier = fencerel(Barrier | Rmb | Wmb | Mb | Sync-rcu | Sync-srcu |
Alan Sternf9de4172019-06-20 11:55:36 -040048 Before-atomic | After-atomic | Acquire | Release |
49 Rcu-lock | Rcu-unlock | Srcu-lock | Srcu-unlock) |
Alan Stern0031e382019-04-22 12:18:09 -040050 (po ; [Release]) | ([Acquire] ; po)
Paul E. McKenney1c27b642018-01-18 19:58:55 -080051
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
Alan Stern0031e382019-04-22 12:18:09 -040071let to-w = rwdep | (overwrite & int) | (addr ; [Plain] ; wmb)
Alan Sternd1a84ab2019-04-22 12:17:58 -040072let to-r = addr | (dep ; [Marked] ; rfi)
Boqun Fengddfe1292021-10-25 22:54:14 +080073let ppo = to-r | to-w | fence | (po-unlock-lock-po & int)
Paul E. McKenney1c27b642018-01-18 19:58:55 -080074
75(* Propagation: Ordering from release operations and strong fences. *)
Alan Sternd1a84ab2019-04-22 12:17:58 -040076let A-cumul(r) = (rfe ; [Marked])? ; r
77let cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb |
Boqun Fengddfe1292021-10-25 22:54:14 +080078 po-unlock-lock-po) ; [Marked]
Alan Sternd1a84ab2019-04-22 12:17:58 -040079let prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ;
80 [Marked] ; rfe? ; [Marked]
Paul E. McKenney1c27b642018-01-18 19:58:55 -080081
82(*
83 * Happens Before: Ordering from the passage of time.
84 * No fences needed here for prop because relation confined to one process.
85 *)
Alan Sternd1a84ab2019-04-22 12:17:58 -040086let hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked]
Paul E. McKenney1c27b642018-01-18 19:58:55 -080087acyclic hb as happens-before
88
89(****************************************)
90(* Write and fence propagation ordering *)
91(****************************************)
92
93(* Propagation: Each non-rf link needs a strong fence. *)
Alan Sternd1a84ab2019-04-22 12:17:58 -040094let pb = prop ; strong-fence ; hb* ; [Marked]
Paul E. McKenney1c27b642018-01-18 19:58:55 -080095acyclic pb as propagation
96
97(*******)
98(* RCU *)
99(*******)
100
101(*
Alan Stern284749b2018-11-15 11:19:58 -0500102 * Effects of read-side critical sections proceed from the rcu_read_unlock()
Alan Sterna3f600d2018-11-15 11:20:37 -0500103 * or srcu_read_unlock() backwards on the one hand, and from the
104 * rcu_read_lock() or srcu_read_lock() forwards on the other hand.
Alan Stern284749b2018-11-15 11:19:58 -0500105 *
106 * In the definition of rcu-fence below, the po term at the left-hand side
107 * of each disjunct and the po? term at the right-hand end have been factored
108 * out. They have been moved into the definitions of rcu-link and rb.
Alan Sterna3f600d2018-11-15 11:20:37 -0500109 * This was necessary in order to apply the "& loc" tests correctly.
Paul E. McKenney1c27b642018-01-18 19:58:55 -0800110 *)
Alan Stern284749b2018-11-15 11:19:58 -0500111let rcu-gp = [Sync-rcu] (* Compare with gp *)
Alan Sterna3f600d2018-11-15 11:20:37 -0500112let srcu-gp = [Sync-srcu]
Alan Stern284749b2018-11-15 11:19:58 -0500113let rcu-rscsi = rcu-rscs^-1
Alan Sterna3f600d2018-11-15 11:20:37 -0500114let srcu-rscsi = srcu-rscs^-1
Paul E. McKenney1c27b642018-01-18 19:58:55 -0800115
116(*
117 * The synchronize_rcu() strong fence is special in that it can order not
118 * one but two non-rf relations, but only in conjunction with an RCU
119 * read-side critical section.
120 *)
Alan Stern284749b2018-11-15 11:19:58 -0500121let rcu-link = po? ; hb* ; pb* ; prop ; po
Paul E. McKenney1c27b642018-01-18 19:58:55 -0800122
Paul E. McKenney1c27b642018-01-18 19:58:55 -0800123(*
Alan Stern9d036882018-05-14 16:33:40 -0700124 * Any sequence containing at least as many grace periods as RCU read-side
Alan Stern15aa25c2019-06-20 11:55:46 -0400125 * critical sections (joined by rcu-link) induces order like a generalized
126 * inter-CPU strong fence.
Alan Sterna3f600d2018-11-15 11:20:37 -0500127 * Likewise for SRCU grace periods and read-side critical sections, provided
128 * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same
129 * struct srcu_struct location.
Paul E. McKenney1c27b642018-01-18 19:58:55 -0800130 *)
Alan Stern15aa25c2019-06-20 11:55:46 -0400131let rec rcu-order = rcu-gp | srcu-gp |
Alan Stern284749b2018-11-15 11:19:58 -0500132 (rcu-gp ; rcu-link ; rcu-rscsi) |
Alan Sterna3f600d2018-11-15 11:20:37 -0500133 ((srcu-gp ; rcu-link ; srcu-rscsi) & loc) |
Alan Stern284749b2018-11-15 11:19:58 -0500134 (rcu-rscsi ; rcu-link ; rcu-gp) |
Alan Sterna3f600d2018-11-15 11:20:37 -0500135 ((srcu-rscsi ; rcu-link ; srcu-gp) & loc) |
Alan Stern15aa25c2019-06-20 11:55:46 -0400136 (rcu-gp ; rcu-link ; rcu-order ; rcu-link ; rcu-rscsi) |
137 ((srcu-gp ; rcu-link ; rcu-order ; rcu-link ; srcu-rscsi) & loc) |
138 (rcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; rcu-gp) |
139 ((srcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; srcu-gp) & loc) |
140 (rcu-order ; rcu-link ; rcu-order)
141let rcu-fence = po ; rcu-order ; po?
142let fence = fence | rcu-fence
143let strong-fence = strong-fence | rcu-fence
Alan Stern9d036882018-05-14 16:33:40 -0700144
145(* rb orders instructions just as pb does *)
Alan Stern15aa25c2019-06-20 11:55:46 -0400146let rb = prop ; rcu-fence ; hb* ; pb* ; [Marked]
Paul E. McKenney1c27b642018-01-18 19:58:55 -0800147
Alan Stern1ee2da52018-05-14 16:33:39 -0700148irreflexive rb as rcu
Alan Stern9d036882018-05-14 16:33:40 -0700149
150(*
151 * The happens-before, propagation, and rcu constraints are all
152 * expressions of temporal ordering. They could be replaced by
153 * a single constraint on an "executes-before" relation, xb:
154 *
155 * let xb = hb | pb | rb
156 * acyclic xb as executes-before
157 *)
Alan Stern0031e382019-04-22 12:18:09 -0400158
159(*********************************)
160(* Plain accesses and data races *)
161(*********************************)
162
163(* Warn about plain writes and marked accesses in the same region *)
164let mixed-accesses = ([Plain & W] ; (po-loc \ barrier) ; [Marked]) |
165 ([Marked] ; (po-loc \ barrier) ; [Plain & W])
166flag ~empty mixed-accesses as mixed-accesses
167
168(* Executes-before and visibility *)
169let xbstar = (hb | pb | rb)*
Alan Stern0031e382019-04-22 12:18:09 -0400170let vis = cumul-fence* ; rfe? ; [Marked] ;
Alan Stern15aa25c2019-06-20 11:55:46 -0400171 ((strong-fence ; [Marked] ; xbstar) | (xbstar & int))
Alan Stern0031e382019-04-22 12:18:09 -0400172
173(* Boundaries for lifetimes of plain accesses *)
174let w-pre-bounded = [Marked] ; (addr | fence)?
175let r-pre-bounded = [Marked] ; (addr | nonrw-fence |
176 ([R4rmb] ; fencerel(Rmb) ; [~Noreturn]))?
177let w-post-bounded = fence? ; [Marked]
178let r-post-bounded = (nonrw-fence | ([~Noreturn] ; fencerel(Rmb) ; [R4rmb]))? ;
179 [Marked]
180
181(* Visibility and executes-before for plain accesses *)
Alan Stern4289ee72019-06-20 11:55:58 -0400182let ww-vis = fence | (strong-fence ; xbstar ; w-pre-bounded) |
183 (w-post-bounded ; vis ; w-pre-bounded)
184let wr-vis = fence | (strong-fence ; xbstar ; r-pre-bounded) |
185 (w-post-bounded ; vis ; r-pre-bounded)
186let rw-xbstar = fence | (r-post-bounded ; xbstar ; w-pre-bounded)
Alan Stern0031e382019-04-22 12:18:09 -0400187
188(* Potential races *)
189let pre-race = ext & ((Plain * M) | ((M \ IW) * Plain))
190
191(* Coherence requirements for plain accesses *)
192let wr-incoh = pre-race & rf & rw-xbstar^-1
193let rw-incoh = pre-race & fr & wr-vis^-1
194let ww-incoh = pre-race & co & ww-vis^-1
195empty (wr-incoh | rw-incoh | ww-incoh) as plain-coherence
196
197(* Actual races *)
198let ww-nonrace = ww-vis & ((Marked * W) | rw-xbstar) & ((W * Marked) | wr-vis)
199let ww-race = (pre-race & co) \ ww-nonrace
Alan Sterndaebf242019-09-06 16:57:22 -0400200let wr-race = (pre-race & (co? ; rf)) \ wr-vis \ rw-xbstar^-1
Alan Stern0031e382019-04-22 12:18:09 -0400201let rw-race = (pre-race & fr) \ rw-xbstar
202
203flag ~empty (ww-race | wr-race | rw-race) as data-race