xref: /openbmc/linux/tools/memory-model/lock.cat (revision 30b795df11a1a9dd7fc50c1ff4677343b67cb379)
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 * Warning: spin_is_locked() crashes herd7 versions strictly before 7.48.
11fd0359dbSAlan Stern * spin_is_locked() is functional from herd7 version 7.49.
1215553dcbSLuc Maranget *)
13fd0359dbSAlan Stern
141c27b644SPaul E. McKenneyinclude "cross.cat"
151c27b644SPaul E. McKenney
16fd0359dbSAlan Stern(*
17fd0359dbSAlan Stern * The lock-related events generated by herd are as follows:
18fd0359dbSAlan Stern *
19fd0359dbSAlan Stern * LKR		Lock-Read: the read part of a spin_lock() or successful
20fd0359dbSAlan Stern *			spin_trylock() read-modify-write event pair
21fd0359dbSAlan Stern * LKW		Lock-Write: the write part of a spin_lock() or successful
22fd0359dbSAlan Stern *			spin_trylock() RMW event pair
23fd0359dbSAlan Stern * UL		Unlock: a spin_unlock() event
24fd0359dbSAlan Stern * LF		Lock-Fail: a failed spin_trylock() event
25fd0359dbSAlan Stern * RL		Read-Locked: a spin_is_locked() event which returns True
26fd0359dbSAlan Stern * RU		Read-Unlocked: a spin_is_locked() event which returns False
27fd0359dbSAlan Stern *
28fd0359dbSAlan Stern * LKR and LKW events always come paired, like all RMW event sequences.
29fd0359dbSAlan Stern *
30fd0359dbSAlan Stern * LKR, LF, RL, and RU are read events; LKR has Acquire ordering.
31fd0359dbSAlan Stern * LKW and UL are write events; UL has Release ordering.
32fd0359dbSAlan Stern * LKW, LF, RL, and RU have no ordering properties.
33fd0359dbSAlan Stern *)
34fd0359dbSAlan Stern
35*30b795dfSAlan Stern(* Backward compatibility *)
36*30b795dfSAlan Sternlet RL = try RL with emptyset
37*30b795dfSAlan Sternlet RU = try RU with emptyset
38*30b795dfSAlan Stern
39*30b795dfSAlan Stern(* Treat RL as a kind of LF: a read with no ordering properties *)
40*30b795dfSAlan Sternlet LF = LF | RL
41*30b795dfSAlan Stern
42*30b795dfSAlan Stern(* There should be no ordinary R or W accesses to spinlocks *)
43*30b795dfSAlan Sternlet ALL-LOCKS = LKR | LKW | UL | LF | RU
44*30b795dfSAlan Sternflag ~empty [M \ IW] ; loc ; [ALL-LOCKS] as mixed-lock-accesses
45*30b795dfSAlan Stern
46fd0359dbSAlan Stern(* Link Lock-Reads to their RMW-partner Lock-Writes *)
471c27b644SPaul E. McKenneylet lk-rmw = ([LKR] ; po-loc ; [LKW]) \ (po ; po)
481c27b644SPaul E. McKenneylet rmw = rmw | lk-rmw
491c27b644SPaul E. McKenney
501c27b644SPaul E. McKenney(*
511c27b644SPaul E. McKenney * A paired LKR must always see an unlocked value; spin_lock() calls nested
521c27b644SPaul E. McKenney * inside a critical section (for the same lock) always deadlock.
531c27b644SPaul E. McKenney *)
541c27b644SPaul E. McKenneyempty ([LKW] ; po-loc ; [domain(lk-rmw)]) \ (po-loc ; [UL] ; po-loc)
551c27b644SPaul E. McKenney	as lock-nest
561c27b644SPaul E. McKenney
571c27b644SPaul E. McKenney(* The litmus test is invalid if an LKW event is not part of an RMW pair *)
581c27b644SPaul E. McKenneyflag ~empty LKW \ range(lk-rmw) as unpaired-LKW
591c27b644SPaul E. McKenney
601c27b644SPaul E. McKenney(* This will be allowed if we implement spin_is_locked() *)
611c27b644SPaul E. McKenneyflag ~empty LKR \ domain(lk-rmw) as unpaired-LKR
621c27b644SPaul E. McKenney
631c27b644SPaul E. McKenney(* The final value of a spinlock should not be tested *)
641c27b644SPaul E. McKenneyflag ~empty [FW] ; loc ; [ALL-LOCKS] as lock-final
651c27b644SPaul E. McKenney
661c27b644SPaul E. McKenney(*
671c27b644SPaul E. McKenney * Put lock operations in their appropriate classes, but leave UL out of W
681c27b644SPaul E. McKenney * until after the co relation has been generated.
691c27b644SPaul E. McKenney *)
708559183cSAlan Sternlet R = R | LKR | LF | RU
711c27b644SPaul E. McKenneylet W = W | LKW
721c27b644SPaul E. McKenney
731c27b644SPaul E. McKenneylet Release = Release | UL
741c27b644SPaul E. McKenneylet Acquire = Acquire | LKR
751c27b644SPaul E. McKenney
761c27b644SPaul E. McKenney(* Match LKW events to their corresponding UL events *)
771c27b644SPaul E. McKenneylet critical = ([LKW] ; po-loc ; [UL]) \ (po-loc ; [LKW | UL] ; po-loc)
781c27b644SPaul E. McKenney
791c27b644SPaul E. McKenneyflag ~empty UL \ range(critical) as unmatched-unlock
801c27b644SPaul E. McKenney
811c27b644SPaul E. McKenney(* Allow up to one unmatched LKW per location; more must deadlock *)
821c27b644SPaul E. McKenneylet UNMATCHED-LKW = LKW \ domain(critical)
831c27b644SPaul E. McKenneyempty ([UNMATCHED-LKW] ; loc ; [UNMATCHED-LKW]) \ id as unmatched-locks
841c27b644SPaul E. McKenney
851c27b644SPaul E. McKenney(* rfi for LF events: link each LKW to the LF events in its critical section *)
861c27b644SPaul E. McKenneylet rfi-lf = ([LKW] ; po-loc ; [LF]) \ ([LKW] ; po-loc ; [UL] ; po-loc)
871c27b644SPaul E. McKenney
881c27b644SPaul E. McKenney(* rfe for LF events *)
891c27b644SPaul E. McKenneylet all-possible-rfe-lf =
901c27b644SPaul E. McKenney  (*
911c27b644SPaul E. McKenney   * Given an LF event r, compute the possible rfe edges for that event
921c27b644SPaul E. McKenney   * (all those starting from LKW events in other threads),
931c27b644SPaul E. McKenney   * and then convert that relation to a set of single-edge relations.
941c27b644SPaul E. McKenney   *)
951c27b644SPaul E. McKenney  let possible-rfe-lf r =
961c27b644SPaul E. McKenney    let pair-to-relation p = p ++ 0
971c27b644SPaul E. McKenney    in map pair-to-relation ((LKW * {r}) & loc & ext)
981c27b644SPaul E. McKenney  (* Do this for each LF event r that isn't in rfi-lf *)
991c27b644SPaul E. McKenney  in map possible-rfe-lf (LF \ range(rfi-lf))
1001c27b644SPaul E. McKenney
1011c27b644SPaul E. McKenney(* Generate all rf relations for LF events *)
1021c27b644SPaul E. McKenneywith rfe-lf from cross(all-possible-rfe-lf)
10315553dcbSLuc Marangetlet rf-lf = rfe-lf | rfi-lf
10415553dcbSLuc Maranget
105fd0359dbSAlan Stern(*
106fd0359dbSAlan Stern * RU, i.e., spin_is_locked() returning False, is slightly different.
107fd0359dbSAlan Stern * We rely on the memory model to rule out cases where spin_is_locked()
108fd0359dbSAlan Stern * within one of the lock's critical sections returns False.
109fd0359dbSAlan Stern *)
11015553dcbSLuc Maranget
111fd0359dbSAlan Stern(* rfi for RU events: an RU may read from the last po-previous UL *)
11215553dcbSLuc Marangetlet rfi-ru = ([UL] ; po-loc ; [RU]) \ ([UL] ; po-loc ; [LKW] ; po-loc)
11315553dcbSLuc Maranget
114fd0359dbSAlan Stern(* rfe for RU events: an RU may read from an external UL or the initial write *)
11515553dcbSLuc Marangetlet all-possible-rfe-ru =
11615553dcbSLuc Maranget   let possible-rfe-ru r =
11715553dcbSLuc Maranget     let pair-to-relation p = p ++ 0
11815553dcbSLuc Maranget     in map pair-to-relation (((UL|IW) * {r}) & loc & ext)
11915553dcbSLuc Maranget  in map possible-rfe-ru RU
12015553dcbSLuc Maranget
121fd0359dbSAlan Stern(* Generate all rf relations for RU events *)
12215553dcbSLuc Marangetwith rfe-ru from cross(all-possible-rfe-ru)
12315553dcbSLuc Marangetlet rf-ru = rfe-ru | rfi-ru
12415553dcbSLuc Maranget
12515553dcbSLuc Maranget(* Final rf relation *)
1268559183cSAlan Sternlet rf = rf | rf-lf | rf-ru
1271c27b644SPaul E. McKenney
1281c27b644SPaul E. McKenney(* Generate all co relations, including LKW events but not UL *)
1291c27b644SPaul E. McKenneylet co0 = co0 | ([IW] ; loc ; [LKW]) |
1301c27b644SPaul E. McKenney	(([LKW] ; loc ; [UNMATCHED-LKW]) \ [UNMATCHED-LKW])
1311c27b644SPaul E. McKenneyinclude "cos-opt.cat"
1321c27b644SPaul E. McKenneylet W = W | UL
1331c27b644SPaul E. McKenneylet M = R | W
1341c27b644SPaul E. McKenney
1351c27b644SPaul E. McKenney(* Merge UL events into co *)
1361c27b644SPaul E. McKenneylet co = (co | critical | (critical^-1 ; co))+
1371c27b644SPaul E. McKenneylet coe = co & ext
1381c27b644SPaul E. McKenneylet coi = co & int
1391c27b644SPaul E. McKenney
1401c27b644SPaul E. McKenney(* Merge LKR events into rf *)
1411c27b644SPaul E. McKenneylet rf = rf | ([IW | UL] ; singlestep(co) ; lk-rmw^-1)
1421c27b644SPaul E. McKenneylet rfe = rf & ext
1431c27b644SPaul E. McKenneylet rfi = rf & int
1441c27b644SPaul E. McKenney
1451c27b644SPaul E. McKenneylet fr = rf^-1 ; co
1461c27b644SPaul E. McKenneylet fre = fr & ext
1471c27b644SPaul E. McKenneylet fri = fr & int
1481c27b644SPaul E. McKenney
1491c27b644SPaul E. McKenneyshow co,rf,fr
150