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