tools/memory-model: Remove duplicated code from lock.cat
[linux-2.6-block.git] / tools / memory-model / lock.cat
1 // SPDX-License-Identifier: GPL-2.0+
2 (*
3  * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria
4  * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu>
5  *)
6
7 (* Generate coherence orders and handle lock operations *)
8 (*
9  * Warning, crashes with herd7 versions strictly before 7.48.
10  * spin_islocked is functional from version 7.49.
11  *
12  *)
13 include "cross.cat"
14
15 (* From lock reads to their partner lock writes *)
16 let lk-rmw = ([LKR] ; po-loc ; [LKW]) \ (po ; po)
17 let rmw = rmw | lk-rmw
18
19 (*
20  * A paired LKR must always see an unlocked value; spin_lock() calls nested
21  * inside a critical section (for the same lock) always deadlock.
22  *)
23 empty ([LKW] ; po-loc ; [domain(lk-rmw)]) \ (po-loc ; [UL] ; po-loc)
24         as lock-nest
25
26 (* The litmus test is invalid if an LKW event is not part of an RMW pair *)
27 flag ~empty LKW \ range(lk-rmw) as unpaired-LKW
28
29 (* This will be allowed if we implement spin_is_locked() *)
30 flag ~empty LKR \ domain(lk-rmw) as unpaired-LKR
31
32 (* There should be no R or W accesses to spinlocks *)
33 let ALL-LOCKS = LKR | LKW | UL | LF
34 flag ~empty [M \ IW] ; loc ; [ALL-LOCKS] as mixed-lock-accesses
35
36 (* The final value of a spinlock should not be tested *)
37 flag ~empty [FW] ; loc ; [ALL-LOCKS] as lock-final
38
39 (*
40  * Backward compatibility
41  *)
42 let RL = try RL with emptyset (* defined herd7 >= 7.49 *)
43 let RU = try RU with emptyset (* defined herd7 >= 7.49 *)
44
45 (* Treat RL as a kind of LF: a read with no ordering properties *)
46 let LF = LF | RL
47
48 (*
49  * Put lock operations in their appropriate classes, but leave UL out of W
50  * until after the co relation has been generated.
51  *)
52 let R = R | LKR | LF | RU
53 let W = W | LKW
54
55 let Release = Release | UL
56 let Acquire = Acquire | LKR
57
58
59 (* Match LKW events to their corresponding UL events *)
60 let critical = ([LKW] ; po-loc ; [UL]) \ (po-loc ; [LKW | UL] ; po-loc)
61
62 flag ~empty UL \ range(critical) as unmatched-unlock
63
64 (* Allow up to one unmatched LKW per location; more must deadlock *)
65 let UNMATCHED-LKW = LKW \ domain(critical)
66 empty ([UNMATCHED-LKW] ; loc ; [UNMATCHED-LKW]) \ id as unmatched-locks
67
68
69 (* rfi for LF events: link each LKW to the LF events in its critical section *)
70 let rfi-lf = ([LKW] ; po-loc ; [LF]) \ ([LKW] ; po-loc ; [UL] ; po-loc)
71
72 (* rfe for LF events *)
73 let all-possible-rfe-lf =
74   (*
75    * Given an LF event r, compute the possible rfe edges for that event
76    * (all those starting from LKW events in other threads),
77    * and then convert that relation to a set of single-edge relations.
78    *)
79   let possible-rfe-lf r =
80     let pair-to-relation p = p ++ 0
81     in map pair-to-relation ((LKW * {r}) & loc & ext)
82   (* Do this for each LF event r that isn't in rfi-lf *)
83   in map possible-rfe-lf (LF \ range(rfi-lf))
84
85 (* Generate all rf relations for LF events *)
86 with rfe-lf from cross(all-possible-rfe-lf)
87 let rf-lf = rfe-lf | rfi-lf
88
89 (* Read from unlock, ie islocked returning false, slightly different *)
90
91 (* islocked returning false can read from the last po-previous unlock *)
92 let rfi-ru = ([UL] ; po-loc ; [RU]) \ ([UL] ; po-loc ; [LKW] ; po-loc)
93
94 (* any islocked returning false can read from any external unlock *)
95 let all-possible-rfe-ru =
96    let possible-rfe-ru r =
97      let pair-to-relation p = p ++ 0
98      in map pair-to-relation (((UL|IW) * {r}) & loc & ext)
99   in map possible-rfe-ru RU
100
101 with rfe-ru from cross(all-possible-rfe-ru)
102 let rf-ru = rfe-ru | rfi-ru
103
104 (* Final rf relation *)
105 let rf = rf | rf-lf | rf-ru
106
107 (* Generate all co relations, including LKW events but not UL *)
108 let co0 = co0 | ([IW] ; loc ; [LKW]) |
109         (([LKW] ; loc ; [UNMATCHED-LKW]) \ [UNMATCHED-LKW])
110 include "cos-opt.cat"
111 let W = W | UL
112 let M = R | W
113
114 (* Merge UL events into co *)
115 let co = (co | critical | (critical^-1 ; co))+
116 let coe = co & ext
117 let coi = co & int
118
119 (* Merge LKR events into rf *)
120 let rf = rf | ([IW | UL] ; singlestep(co) ; lk-rmw^-1)
121 let rfe = rf & ext
122 let rfi = rf & int
123
124 let fr = rf^-1 ; co
125 let fre = fr & ext
126 let fri = fr & int
127
128 show co,rf,fr