xref: /openbmc/linux/tools/memory-model/lock.cat (revision 7e24a55b2122746c2eef192296fc84624354f895)
11c27b644SPaul E. McKenney// SPDX-License-Identifier: GPL-2.0+
21c27b644SPaul E. McKenney(*
31c27b644SPaul E. McKenney * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria
41c27b644SPaul E. McKenney * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu>
51c27b644SPaul E. McKenney *)
61c27b644SPaul E. McKenney
715553dcbSLuc Maranget(*
8fd0359dbSAlan Stern * Generate coherence orders and handle lock operations
915553dcbSLuc Maranget *)
10fd0359dbSAlan Stern
111c27b644SPaul E. McKenneyinclude "cross.cat"
121c27b644SPaul E. McKenney
13fd0359dbSAlan Stern(*
1437c600a3SAndrea Parri * The lock-related events generated by herd7 are as follows:
15fd0359dbSAlan Stern *
16fd0359dbSAlan Stern * LKR		Lock-Read: the read part of a spin_lock() or successful
17fd0359dbSAlan Stern *			spin_trylock() read-modify-write event pair
18fd0359dbSAlan Stern * LKW		Lock-Write: the write part of a spin_lock() or successful
19fd0359dbSAlan Stern *			spin_trylock() RMW event pair
20fd0359dbSAlan Stern * UL		Unlock: a spin_unlock() event
21fd0359dbSAlan Stern * LF		Lock-Fail: a failed spin_trylock() event
22fd0359dbSAlan Stern * RL		Read-Locked: a spin_is_locked() event which returns True
23fd0359dbSAlan Stern * RU		Read-Unlocked: a spin_is_locked() event which returns False
24fd0359dbSAlan Stern *
25fd0359dbSAlan Stern * LKR and LKW events always come paired, like all RMW event sequences.
26fd0359dbSAlan Stern *
27fd0359dbSAlan Stern * LKR, LF, RL, and RU are read events; LKR has Acquire ordering.
28fd0359dbSAlan Stern * LKW and UL are write events; UL has Release ordering.
29fd0359dbSAlan Stern * LKW, LF, RL, and RU have no ordering properties.
30fd0359dbSAlan Stern *)
31fd0359dbSAlan Stern
3230b795dfSAlan Stern(* Backward compatibility *)
3330b795dfSAlan Sternlet RL = try RL with emptyset
3430b795dfSAlan Sternlet RU = try RU with emptyset
3530b795dfSAlan Stern
3630b795dfSAlan Stern(* Treat RL as a kind of LF: a read with no ordering properties *)
3730b795dfSAlan Sternlet LF = LF | RL
3830b795dfSAlan Stern
39614e40faSAlan Stern(* There should be no ordinary R or W accesses to spinlocks or SRCU structs *)
40614e40faSAlan Sternlet ALL-LOCKS = LKR | LKW | UL | LF | RU | Srcu-lock | Srcu-unlock | Sync-srcu
41614e40faSAlan Sternflag ~empty [M \ IW \ ALL-LOCKS] ; loc ; [ALL-LOCKS] as mixed-lock-accesses
4230b795dfSAlan Stern
43fd0359dbSAlan Stern(* Link Lock-Reads to their RMW-partner Lock-Writes *)
441c27b644SPaul E. McKenneylet lk-rmw = ([LKR] ; po-loc ; [LKW]) \ (po ; po)
451c27b644SPaul E. McKenneylet rmw = rmw | lk-rmw
461c27b644SPaul E. McKenney
47cee0321aSAlan Stern(* The litmus test is invalid if an LKR/LKW event is not part of an RMW pair *)
48cee0321aSAlan Sternflag ~empty LKW \ range(lk-rmw) as unpaired-LKW
49cee0321aSAlan Sternflag ~empty LKR \ domain(lk-rmw) as unpaired-LKR
50cee0321aSAlan Stern
511c27b644SPaul E. McKenney(*
52cee0321aSAlan Stern * An LKR must always see an unlocked value; spin_lock() calls nested
531c27b644SPaul E. McKenney * inside a critical section (for the same lock) always deadlock.
541c27b644SPaul E. McKenney *)
55cee0321aSAlan Sternempty ([LKW] ; po-loc ; [LKR]) \ (po-loc ; [UL] ; po-loc) as lock-nest
561c27b644SPaul E. McKenney
571c27b644SPaul E. McKenney(* The final value of a spinlock should not be tested *)
581c27b644SPaul E. McKenneyflag ~empty [FW] ; loc ; [ALL-LOCKS] as lock-final
591c27b644SPaul E. McKenney
601c27b644SPaul E. McKenney(*
611c27b644SPaul E. McKenney * Put lock operations in their appropriate classes, but leave UL out of W
621c27b644SPaul E. McKenney * until after the co relation has been generated.
631c27b644SPaul E. McKenney *)
648559183cSAlan Sternlet R = R | LKR | LF | RU
651c27b644SPaul E. McKenneylet W = W | LKW
661c27b644SPaul E. McKenney
671c27b644SPaul E. McKenneylet Release = Release | UL
681c27b644SPaul E. McKenneylet Acquire = Acquire | LKR
691c27b644SPaul E. McKenney
701c27b644SPaul E. McKenney(* Match LKW events to their corresponding UL events *)
711c27b644SPaul E. McKenneylet critical = ([LKW] ; po-loc ; [UL]) \ (po-loc ; [LKW | UL] ; po-loc)
721c27b644SPaul E. McKenney
731c27b644SPaul E. McKenneyflag ~empty UL \ range(critical) as unmatched-unlock
741c27b644SPaul E. McKenney
751c27b644SPaul E. McKenney(* Allow up to one unmatched LKW per location; more must deadlock *)
761c27b644SPaul E. McKenneylet UNMATCHED-LKW = LKW \ domain(critical)
771c27b644SPaul E. McKenneyempty ([UNMATCHED-LKW] ; loc ; [UNMATCHED-LKW]) \ id as unmatched-locks
781c27b644SPaul E. McKenney
791c27b644SPaul E. McKenney(* rfi for LF events: link each LKW to the LF events in its critical section *)
801c27b644SPaul E. McKenneylet rfi-lf = ([LKW] ; po-loc ; [LF]) \ ([LKW] ; po-loc ; [UL] ; po-loc)
811c27b644SPaul E. McKenney
821c27b644SPaul E. McKenney(* rfe for LF events *)
831c27b644SPaul E. McKenneylet all-possible-rfe-lf =
841c27b644SPaul E. McKenney	(*
851c27b644SPaul E. McKenney	 * Given an LF event r, compute the possible rfe edges for that event
861c27b644SPaul E. McKenney	 * (all those starting from LKW events in other threads),
871c27b644SPaul E. McKenney	 * and then convert that relation to a set of single-edge relations.
881c27b644SPaul E. McKenney	 *)
891c27b644SPaul E. McKenney	let possible-rfe-lf r =
901c27b644SPaul E. McKenney		let pair-to-relation p = p ++ 0
911c27b644SPaul E. McKenney		in map pair-to-relation ((LKW * {r}) & loc & ext)
921c27b644SPaul E. McKenney	(* Do this for each LF event r that isn't in rfi-lf *)
931c27b644SPaul E. McKenney	in map possible-rfe-lf (LF \ range(rfi-lf))
941c27b644SPaul E. McKenney
951c27b644SPaul E. McKenney(* Generate all rf relations for LF events *)
961c27b644SPaul E. McKenneywith rfe-lf from cross(all-possible-rfe-lf)
9715553dcbSLuc Marangetlet rf-lf = rfe-lf | rfi-lf
9815553dcbSLuc Maranget
99fd0359dbSAlan Stern(*
100fd0359dbSAlan Stern * RU, i.e., spin_is_locked() returning False, is slightly different.
101fd0359dbSAlan Stern * We rely on the memory model to rule out cases where spin_is_locked()
102fd0359dbSAlan Stern * within one of the lock's critical sections returns False.
103fd0359dbSAlan Stern *)
10415553dcbSLuc Maranget
105*8d70d611SAlan Stern(*
106*8d70d611SAlan Stern * rf for RU events: an RU may read from an external UL or the initial write,
107*8d70d611SAlan Stern * or from the last po-previous UL
108*8d70d611SAlan Stern *)
109*8d70d611SAlan Sternlet all-possible-rf-ru =
110*8d70d611SAlan Stern	let possible-rf-ru r =
11115553dcbSLuc Maranget		let pair-to-relation p = p ++ 0
112*8d70d611SAlan Stern		in map pair-to-relation ((((UL | IW) * {r}) & loc & ext) |
113*8d70d611SAlan Stern			(((UL * {r}) & po-loc) \ ([UL] ; po-loc ; [LKW] ; po-loc)))
114*8d70d611SAlan Stern	in map possible-rf-ru RU
11515553dcbSLuc Maranget
116fd0359dbSAlan Stern(* Generate all rf relations for RU events *)
117*8d70d611SAlan Sternwith rf-ru from cross(all-possible-rf-ru)
11815553dcbSLuc Maranget
11915553dcbSLuc Maranget(* Final rf relation *)
1208559183cSAlan Sternlet rf = rf | rf-lf | rf-ru
1211c27b644SPaul E. McKenney
1221c27b644SPaul E. McKenney(* Generate all co relations, including LKW events but not UL *)
1231c27b644SPaul E. McKenneylet co0 = co0 | ([IW] ; loc ; [LKW]) |
1241c27b644SPaul E. McKenney	(([LKW] ; loc ; [UNMATCHED-LKW]) \ [UNMATCHED-LKW])
1251c27b644SPaul E. McKenneyinclude "cos-opt.cat"
1261c27b644SPaul E. McKenneylet W = W | UL
1271c27b644SPaul E. McKenneylet M = R | W
1281c27b644SPaul E. McKenney
1291c27b644SPaul E. McKenney(* Merge UL events into co *)
1301c27b644SPaul E. McKenneylet co = (co | critical | (critical^-1 ; co))+
1311c27b644SPaul E. McKenneylet coe = co & ext
1321c27b644SPaul E. McKenneylet coi = co & int
1331c27b644SPaul E. McKenney
1341c27b644SPaul E. McKenney(* Merge LKR events into rf *)
1351c27b644SPaul E. McKenneylet rf = rf | ([IW | UL] ; singlestep(co) ; lk-rmw^-1)
1361c27b644SPaul E. McKenneylet rfe = rf & ext
1371c27b644SPaul E. McKenneylet rfi = rf & int
1381c27b644SPaul E. McKenney
1391c27b644SPaul E. McKenneylet fr = rf^-1 ; co
1401c27b644SPaul E. McKenneylet fre = fr & ext
1411c27b644SPaul E. McKenneylet fri = fr & int
1421c27b644SPaul E. McKenney
1431c27b644SPaul E. McKenneyshow co,rf,fr
144