]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/memory-model/lock.cat
df74de2148f6de35175c74b3facad96bda36c701
[linux.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 (*
8  * Generate coherence orders and handle lock operations
9  *
10  * Warning: spin_is_locked() crashes herd7 versions strictly before 7.48.
11  * spin_is_locked() is functional from herd7 version 7.49.
12  *)
13
14 include "cross.cat"
15
16 (*
17  * The lock-related events generated by herd are as follows:
18  *
19  * LKR          Lock-Read: the read part of a spin_lock() or successful
20  *                      spin_trylock() read-modify-write event pair
21  * LKW          Lock-Write: the write part of a spin_lock() or successful
22  *                      spin_trylock() RMW event pair
23  * UL           Unlock: a spin_unlock() event
24  * LF           Lock-Fail: a failed spin_trylock() event
25  * RL           Read-Locked: a spin_is_locked() event which returns True
26  * RU           Read-Unlocked: a spin_is_locked() event which returns False
27  *
28  * LKR and LKW events always come paired, like all RMW event sequences.
29  *
30  * LKR, LF, RL, and RU are read events; LKR has Acquire ordering.
31  * LKW and UL are write events; UL has Release ordering.
32  * LKW, LF, RL, and RU have no ordering properties.
33  *)
34
35 (* Link Lock-Reads to their RMW-partner Lock-Writes *)
36 let lk-rmw = ([LKR] ; po-loc ; [LKW]) \ (po ; po)
37 let rmw = rmw | lk-rmw
38
39 (*
40  * A paired LKR must always see an unlocked value; spin_lock() calls nested
41  * inside a critical section (for the same lock) always deadlock.
42  *)
43 empty ([LKW] ; po-loc ; [domain(lk-rmw)]) \ (po-loc ; [UL] ; po-loc)
44         as lock-nest
45
46 (* The litmus test is invalid if an LKW event is not part of an RMW pair *)
47 flag ~empty LKW \ range(lk-rmw) as unpaired-LKW
48
49 (* This will be allowed if we implement spin_is_locked() *)
50 flag ~empty LKR \ domain(lk-rmw) as unpaired-LKR
51
52 (* There should be no ordinary R or W accesses to spinlocks *)
53 let ALL-LOCKS = LKR | LKW | UL | LF
54 flag ~empty [M \ IW] ; loc ; [ALL-LOCKS] as mixed-lock-accesses
55
56 (* The final value of a spinlock should not be tested *)
57 flag ~empty [FW] ; loc ; [ALL-LOCKS] as lock-final
58
59 (* Backward compatibility *)
60 let RL = try RL with emptyset
61 let RU = try RU with emptyset
62
63 (* Treat RL as a kind of LF: a read with no ordering properties *)
64 let LF = LF | RL
65
66 (*
67  * Put lock operations in their appropriate classes, but leave UL out of W
68  * until after the co relation has been generated.
69  *)
70 let R = R | LKR | LF | RU
71 let W = W | LKW
72
73 let Release = Release | UL
74 let Acquire = Acquire | LKR
75
76 (* Match LKW events to their corresponding UL events *)
77 let critical = ([LKW] ; po-loc ; [UL]) \ (po-loc ; [LKW | UL] ; po-loc)
78
79 flag ~empty UL \ range(critical) as unmatched-unlock
80
81 (* Allow up to one unmatched LKW per location; more must deadlock *)
82 let UNMATCHED-LKW = LKW \ domain(critical)
83 empty ([UNMATCHED-LKW] ; loc ; [UNMATCHED-LKW]) \ id as unmatched-locks
84
85 (* rfi for LF events: link each LKW to the LF events in its critical section *)
86 let rfi-lf = ([LKW] ; po-loc ; [LF]) \ ([LKW] ; po-loc ; [UL] ; po-loc)
87
88 (* rfe for LF events *)
89 let all-possible-rfe-lf =
90   (*
91    * Given an LF event r, compute the possible rfe edges for that event
92    * (all those starting from LKW events in other threads),
93    * and then convert that relation to a set of single-edge relations.
94    *)
95   let possible-rfe-lf r =
96     let pair-to-relation p = p ++ 0
97     in map pair-to-relation ((LKW * {r}) & loc & ext)
98   (* Do this for each LF event r that isn't in rfi-lf *)
99   in map possible-rfe-lf (LF \ range(rfi-lf))
100
101 (* Generate all rf relations for LF events *)
102 with rfe-lf from cross(all-possible-rfe-lf)
103 let rf-lf = rfe-lf | rfi-lf
104
105 (*
106  * RU, i.e., spin_is_locked() returning False, is slightly different.
107  * We rely on the memory model to rule out cases where spin_is_locked()
108  * within one of the lock's critical sections returns False.
109  *)
110
111 (* rfi for RU events: an RU may read from the last po-previous UL *)
112 let rfi-ru = ([UL] ; po-loc ; [RU]) \ ([UL] ; po-loc ; [LKW] ; po-loc)
113
114 (* rfe for RU events: an RU may read from an external UL or the initial write *)
115 let all-possible-rfe-ru =
116    let possible-rfe-ru r =
117      let pair-to-relation p = p ++ 0
118      in map pair-to-relation (((UL|IW) * {r}) & loc & ext)
119   in map possible-rfe-ru RU
120
121 (* Generate all rf relations for RU events *)
122 with rfe-ru from cross(all-possible-rfe-ru)
123 let rf-ru = rfe-ru | rfi-ru
124
125 (* Final rf relation *)
126 let rf = rf | rf-lf | rf-ru
127
128 (* Generate all co relations, including LKW events but not UL *)
129 let co0 = co0 | ([IW] ; loc ; [LKW]) |
130         (([LKW] ; loc ; [UNMATCHED-LKW]) \ [UNMATCHED-LKW])
131 include "cos-opt.cat"
132 let W = W | UL
133 let M = R | W
134
135 (* Merge UL events into co *)
136 let co = (co | critical | (critical^-1 ; co))+
137 let coe = co & ext
138 let coi = co & int
139
140 (* Merge LKR events into rf *)
141 let rf = rf | ([IW | UL] ; singlestep(co) ; lk-rmw^-1)
142 let rfe = rf & ext
143 let rfi = rf & int
144
145 let fr = rf^-1 ; co
146 let fre = fr & ext
147 let fri = fr & int
148
149 show co,rf,fr