xref: /openbmc/linux/tools/memory-model/lock.cat (revision fd0359dbac3df00d1c6c22769e7d647b16b920cc)
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(*
8*fd0359dbSAlan Stern * Generate coherence orders and handle lock operations
915553dcbSLuc Maranget *
10*fd0359dbSAlan Stern * Warning: spin_is_locked() crashes herd7 versions strictly before 7.48.
11*fd0359dbSAlan Stern * spin_is_locked() is functional from herd7 version 7.49.
1215553dcbSLuc Maranget *)
13*fd0359dbSAlan Stern
141c27b644SPaul E. McKenneyinclude "cross.cat"
151c27b644SPaul E. McKenney
16*fd0359dbSAlan Stern(*
17*fd0359dbSAlan Stern * The lock-related events generated by herd are as follows:
18*fd0359dbSAlan Stern *
19*fd0359dbSAlan Stern * LKR		Lock-Read: the read part of a spin_lock() or successful
20*fd0359dbSAlan Stern *			spin_trylock() read-modify-write event pair
21*fd0359dbSAlan Stern * LKW		Lock-Write: the write part of a spin_lock() or successful
22*fd0359dbSAlan Stern *			spin_trylock() RMW event pair
23*fd0359dbSAlan Stern * UL		Unlock: a spin_unlock() event
24*fd0359dbSAlan Stern * LF		Lock-Fail: a failed spin_trylock() event
25*fd0359dbSAlan Stern * RL		Read-Locked: a spin_is_locked() event which returns True
26*fd0359dbSAlan Stern * RU		Read-Unlocked: a spin_is_locked() event which returns False
27*fd0359dbSAlan Stern *
28*fd0359dbSAlan Stern * LKR and LKW events always come paired, like all RMW event sequences.
29*fd0359dbSAlan Stern *
30*fd0359dbSAlan Stern * LKR, LF, RL, and RU are read events; LKR has Acquire ordering.
31*fd0359dbSAlan Stern * LKW and UL are write events; UL has Release ordering.
32*fd0359dbSAlan Stern * LKW, LF, RL, and RU have no ordering properties.
33*fd0359dbSAlan Stern *)
34*fd0359dbSAlan Stern
35*fd0359dbSAlan Stern(* Link Lock-Reads to their RMW-partner Lock-Writes *)
361c27b644SPaul E. McKenneylet lk-rmw = ([LKR] ; po-loc ; [LKW]) \ (po ; po)
371c27b644SPaul E. McKenneylet rmw = rmw | lk-rmw
381c27b644SPaul E. McKenney
391c27b644SPaul E. McKenney(*
401c27b644SPaul E. McKenney * A paired LKR must always see an unlocked value; spin_lock() calls nested
411c27b644SPaul E. McKenney * inside a critical section (for the same lock) always deadlock.
421c27b644SPaul E. McKenney *)
431c27b644SPaul E. McKenneyempty ([LKW] ; po-loc ; [domain(lk-rmw)]) \ (po-loc ; [UL] ; po-loc)
441c27b644SPaul E. McKenney	as lock-nest
451c27b644SPaul E. McKenney
461c27b644SPaul E. McKenney(* The litmus test is invalid if an LKW event is not part of an RMW pair *)
471c27b644SPaul E. McKenneyflag ~empty LKW \ range(lk-rmw) as unpaired-LKW
481c27b644SPaul E. McKenney
491c27b644SPaul E. McKenney(* This will be allowed if we implement spin_is_locked() *)
501c27b644SPaul E. McKenneyflag ~empty LKR \ domain(lk-rmw) as unpaired-LKR
511c27b644SPaul E. McKenney
52*fd0359dbSAlan Stern(* There should be no ordinary R or W accesses to spinlocks *)
531c27b644SPaul E. McKenneylet ALL-LOCKS = LKR | LKW | UL | LF
541c27b644SPaul E. McKenneyflag ~empty [M \ IW] ; loc ; [ALL-LOCKS] as mixed-lock-accesses
551c27b644SPaul E. McKenney
561c27b644SPaul E. McKenney(* The final value of a spinlock should not be tested *)
571c27b644SPaul E. McKenneyflag ~empty [FW] ; loc ; [ALL-LOCKS] as lock-final
581c27b644SPaul E. McKenney
59*fd0359dbSAlan Stern(* Backward compatibility *)
60*fd0359dbSAlan Sternlet RL = try RL with emptyset
61*fd0359dbSAlan Sternlet RU = try RU with emptyset
628559183cSAlan Stern
638559183cSAlan Stern(* Treat RL as a kind of LF: a read with no ordering properties *)
648559183cSAlan Sternlet LF = LF | RL
658559183cSAlan Stern
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
105*fd0359dbSAlan Stern(*
106*fd0359dbSAlan Stern * RU, i.e., spin_is_locked() returning False, is slightly different.
107*fd0359dbSAlan Stern * We rely on the memory model to rule out cases where spin_is_locked()
108*fd0359dbSAlan Stern * within one of the lock's critical sections returns False.
109*fd0359dbSAlan Stern *)
11015553dcbSLuc Maranget
111*fd0359dbSAlan 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
114*fd0359dbSAlan 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
121*fd0359dbSAlan 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