1// SPDX-License-Identifier: GPL-2.0+
2(*
3 * Requires herd version 7.51+6 or higher.
4 *
5 * Copyright (C) 2015 Jade Alglave <[email protected]>,
6 * Copyright (C) 2016 Luc Maranget <[email protected]> for Inria
7 * Copyright (C) 2017 Alan Stern <[email protected]>,
8 *                    Andrea Parri <[email protected]>
9 *
10 * An earlier version of this file appeared in the companion webpage for
11 * "Frightening small children and disconcerting grown-ups: Concurrency
12 * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern,
13 * which appeared in ASPLOS 2018.
14 *)
15
16"Linux-kernel memory consistency model"
17
18(*
19 * File "lock.cat" handles locks and is experimental.
20 * It can be replaced by include "cos.cat" for tests that do not use locks.
21 *)
22
23include "lock.cat"
24
25(*******************)
26(* Basic relations *)
27(*******************)
28
29(* Fences *)
30let rmb = [R \ Noreturn] ; fencerel(Rmb) ; [R \ Noreturn]
31let wmb = [W] ; fencerel(Wmb) ; [W]
32let mb = ([M] ; fencerel(Mb) ; [M]) |
33	([M] ; fencerel(Before-atomic) ; [RMW] ; po? ; [M]) |
34	([M] ; po? ; [RMW] ; fencerel(After-atomic) ; [M]) |
35	([M] ; po? ; [LKW] ; fencerel(After-spinlock) ; [M]) |
36	([M] ; po ; [UL] ; (co | po) ; [LKW] ;
37		fencerel(After-unlock-lock) ; [M])
38let gp = po ; [Sync-rcu | Sync-srcu] ; po?
39
40let strong-fence = mb | gp
41
42(* Release Acquire *)
43let acq-po = [Acquire] ; po ; [M]
44let po-rel = [M] ; po ; [Release]
45let po-unlock-rf-lock-po = po ; [UL] ; rf ; [LKR] ; po
46
47(**********************************)
48(* Fundamental coherence ordering *)
49(**********************************)
50
51(* Sequential Consistency Per Variable *)
52let com = rf | co | fr
53acyclic po-loc | com as coherence
54
55(* Atomic Read-Modify-Write *)
56empty rmw & (fre ; coe) as atomic
57
58(**********************************)
59(* Instruction execution ordering *)
60(**********************************)
61
62(* Preserved Program Order *)
63let dep = addr | data
64let rwdep = (dep | ctrl) ; [W]
65let overwrite = co | fr
66let to-w = rwdep | (overwrite & int)
67let to-r = addr | (dep ; rfi)
68let fence = strong-fence | wmb | po-rel | rmb | acq-po
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? ; r
73let cumul-fence = A-cumul(strong-fence | po-rel) | wmb | po-unlock-rf-lock-po
74let prop = (overwrite & ext)? ; cumul-fence* ; rfe?
75
76(*
77 * Happens Before: Ordering from the passage of time.
78 * No fences needed here for prop because relation confined to one process.
79 *)
80let hb = ppo | rfe | ((prop \ id) & int)
81acyclic hb as happens-before
82
83(****************************************)
84(* Write and fence propagation ordering *)
85(****************************************)
86
87(* Propagation: Each non-rf link needs a strong fence. *)
88let pb = prop ; strong-fence ; hb*
89acyclic pb as propagation
90
91(*******)
92(* RCU *)
93(*******)
94
95(*
96 * Effects of read-side critical sections proceed from the rcu_read_unlock()
97 * or srcu_read_unlock() backwards on the one hand, and from the
98 * rcu_read_lock() or srcu_read_lock() forwards on the other hand.
99 *
100 * In the definition of rcu-fence below, the po term at the left-hand side
101 * of each disjunct and the po? term at the right-hand end have been factored
102 * out.  They have been moved into the definitions of rcu-link and rb.
103 * This was necessary in order to apply the "& loc" tests correctly.
104 *)
105let rcu-gp = [Sync-rcu]		(* Compare with gp *)
106let srcu-gp = [Sync-srcu]
107let rcu-rscsi = rcu-rscs^-1
108let srcu-rscsi = srcu-rscs^-1
109
110(*
111 * The synchronize_rcu() strong fence is special in that it can order not
112 * one but two non-rf relations, but only in conjunction with an RCU
113 * read-side critical section.
114 *)
115let rcu-link = po? ; hb* ; pb* ; prop ; po
116
117(*
118 * Any sequence containing at least as many grace periods as RCU read-side
119 * critical sections (joined by rcu-link) acts as a generalized strong fence.
120 * Likewise for SRCU grace periods and read-side critical sections, provided
121 * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same
122 * struct srcu_struct location.
123 *)
124let rec rcu-fence = rcu-gp | srcu-gp |
125	(rcu-gp ; rcu-link ; rcu-rscsi) |
126	((srcu-gp ; rcu-link ; srcu-rscsi) & loc) |
127	(rcu-rscsi ; rcu-link ; rcu-gp) |
128	((srcu-rscsi ; rcu-link ; srcu-gp) & loc) |
129	(rcu-gp ; rcu-link ; rcu-fence ; rcu-link ; rcu-rscsi) |
130	((srcu-gp ; rcu-link ; rcu-fence ; rcu-link ; srcu-rscsi) & loc) |
131	(rcu-rscsi ; rcu-link ; rcu-fence ; rcu-link ; rcu-gp) |
132	((srcu-rscsi ; rcu-link ; rcu-fence ; rcu-link ; srcu-gp) & loc) |
133	(rcu-fence ; rcu-link ; rcu-fence)
134
135(* rb orders instructions just as pb does *)
136let rb = prop ; po ; rcu-fence ; po? ; hb* ; pb*
137
138irreflexive rb as rcu
139
140(*
141 * The happens-before, propagation, and rcu constraints are all
142 * expressions of temporal ordering.  They could be replaced by
143 * a single constraint on an "executes-before" relation, xb:
144 *
145 * let xb = hb | pb | rb
146 * acyclic xb as executes-before
147 *)
148