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