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-rf-lock-po = po ; [UL] ; 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
47
48(**********************************)
49(* Fundamental coherence ordering *)
50(**********************************)
51
52(* Sequential Consistency Per Variable *)
53let com = rf | co | fr
54acyclic po-loc | com as coherence
55
56(* Atomic Read-Modify-Write *)
57empty rmw & (fre ; coe) as atomic
58
59(**********************************)
60(* Instruction execution ordering *)
61(**********************************)
62
63(* Preserved Program Order *)
64let dep = addr | data
65let rwdep = (dep | ctrl) ; [W]
66let overwrite = co | fr
67let to-w = rwdep | (overwrite & int)
68let to-r = addr | (dep ; [Marked] ; rfi)
69let ppo = to-r | to-w | fence | (po-unlock-rf-lock-po & int)
70
71(* Propagation: Ordering from release operations and strong fences. *)
72let A-cumul(r) = (rfe ; [Marked])? ; r
73let cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb |
74	po-unlock-rf-lock-po) ; [Marked]
75let prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ;
76	[Marked] ; rfe? ; [Marked]
77
78(*
79 * Happens Before: Ordering from the passage of time.
80 * No fences needed here for prop because relation confined to one process.
81 *)
82let hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked]
83acyclic hb as happens-before
84
85(****************************************)
86(* Write and fence propagation ordering *)
87(****************************************)
88
89(* Propagation: Each non-rf link needs a strong fence. *)
90let pb = prop ; strong-fence ; hb* ; [Marked]
91acyclic pb as propagation
92
93(*******)
94(* RCU *)
95(*******)
96
97(*
98 * Effects of read-side critical sections proceed from the rcu_read_unlock()
99 * or srcu_read_unlock() backwards on the one hand, and from the
100 * rcu_read_lock() or srcu_read_lock() forwards on the other hand.
101 *
102 * In the definition of rcu-fence below, the po term at the left-hand side
103 * of each disjunct and the po? term at the right-hand end have been factored
104 * out.  They have been moved into the definitions of rcu-link and rb.
105 * This was necessary in order to apply the "& loc" tests correctly.
106 *)
107let rcu-gp = [Sync-rcu]		(* Compare with gp *)
108let srcu-gp = [Sync-srcu]
109let rcu-rscsi = rcu-rscs^-1
110let srcu-rscsi = srcu-rscs^-1
111
112(*
113 * The synchronize_rcu() strong fence is special in that it can order not
114 * one but two non-rf relations, but only in conjunction with an RCU
115 * read-side critical section.
116 *)
117let rcu-link = po? ; hb* ; pb* ; prop ; po
118
119(*
120 * Any sequence containing at least as many grace periods as RCU read-side
121 * critical sections (joined by rcu-link) acts as a generalized strong fence.
122 * Likewise for SRCU grace periods and read-side critical sections, provided
123 * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same
124 * struct srcu_struct location.
125 *)
126let rec rcu-fence = rcu-gp | srcu-gp |
127	(rcu-gp ; rcu-link ; rcu-rscsi) |
128	((srcu-gp ; rcu-link ; srcu-rscsi) & loc) |
129	(rcu-rscsi ; rcu-link ; rcu-gp) |
130	((srcu-rscsi ; rcu-link ; srcu-gp) & loc) |
131	(rcu-gp ; rcu-link ; rcu-fence ; rcu-link ; rcu-rscsi) |
132	((srcu-gp ; rcu-link ; rcu-fence ; rcu-link ; srcu-rscsi) & loc) |
133	(rcu-rscsi ; rcu-link ; rcu-fence ; rcu-link ; rcu-gp) |
134	((srcu-rscsi ; rcu-link ; rcu-fence ; rcu-link ; srcu-gp) & loc) |
135	(rcu-fence ; rcu-link ; rcu-fence)
136
137(* rb orders instructions just as pb does *)
138let rb = prop ; po ; rcu-fence ; po? ; hb* ; pb* ; [Marked]
139
140irreflexive rb as rcu
141
142(*
143 * The happens-before, propagation, and rcu constraints are all
144 * expressions of temporal ordering.  They could be replaced by
145 * a single constraint on an "executes-before" relation, xb:
146 *
147 * let xb = hb | pb | rb
148 * acyclic xb as executes-before
149 *)
150