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