lock.cat 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * Generate coherence orders and handle lock operations
  8. *)
  9. include "cross.cat"
  10. (*
  11. * The lock-related events generated by herd7 are as follows:
  12. *
  13. * LKR Lock-Read: the read part of a spin_lock() or successful
  14. * spin_trylock() read-modify-write event pair
  15. * LKW Lock-Write: the write part of a spin_lock() or successful
  16. * spin_trylock() RMW event pair
  17. * UL Unlock: a spin_unlock() event
  18. * LF Lock-Fail: a failed spin_trylock() event
  19. * RL Read-Locked: a spin_is_locked() event which returns True
  20. * RU Read-Unlocked: a spin_is_locked() event which returns False
  21. *
  22. * LKR and LKW events always come paired, like all RMW event sequences.
  23. *
  24. * LKR, LF, RL, and RU are read events; LKR has Acquire ordering.
  25. * LKW and UL are write events; UL has Release ordering.
  26. * LKW, LF, RL, and RU have no ordering properties.
  27. *)
  28. (* Backward compatibility *)
  29. let RL = try RL with emptyset
  30. let RU = try RU with emptyset
  31. (* Treat RL as a kind of LF: a read with no ordering properties *)
  32. let LF = LF | RL
  33. (* There should be no ordinary R or W accesses to spinlocks or SRCU structs *)
  34. let ALL-LOCKS = LKR | LKW | UL | LF | RU | Srcu-lock | Srcu-unlock | Sync-srcu
  35. flag ~empty [M \ IW \ ALL-LOCKS] ; loc ; [ALL-LOCKS] as mixed-lock-accesses
  36. (* Link Lock-Reads to their RMW-partner Lock-Writes *)
  37. let lk-rmw = ([LKR] ; po-loc ; [LKW]) \ (po ; po)
  38. let rmw = rmw | lk-rmw
  39. (* The litmus test is invalid if an LKR/LKW event is not part of an RMW pair *)
  40. flag ~empty LKW \ range(lk-rmw) as unpaired-LKW
  41. flag ~empty LKR \ domain(lk-rmw) as unpaired-LKR
  42. (*
  43. * An LKR must always see an unlocked value; spin_lock() calls nested
  44. * inside a critical section (for the same lock) always deadlock.
  45. *)
  46. empty ([LKW] ; po-loc ; [LKR]) \ (po-loc ; [UL] ; po-loc) as lock-nest
  47. (*
  48. * In the same way, spin_is_locked() inside a critical section must always
  49. * return True (no RU events can be in a critical section for the same lock).
  50. *)
  51. empty ([LKW] ; po-loc ; [RU]) \ (po-loc ; [UL] ; po-loc) as nested-is-locked
  52. (* The final value of a spinlock should not be tested *)
  53. flag ~empty [FW] ; loc ; [ALL-LOCKS] as lock-final
  54. (*
  55. * Put lock operations in their appropriate classes, but leave UL out of W
  56. * until after the co relation has been generated.
  57. *)
  58. let R = R | LKR | LF | RU
  59. let W = W | LKW
  60. let Release = Release | UL
  61. let Acquire = Acquire | LKR
  62. (* Match LKW events to their corresponding UL events *)
  63. let critical = ([LKW] ; po-loc ; [UL]) \ (po-loc ; [LKW | UL] ; po-loc)
  64. flag ~empty UL \ range(critical) as unmatched-unlock
  65. (* Allow up to one unmatched LKW per location; more must deadlock *)
  66. let UNMATCHED-LKW = LKW \ domain(critical)
  67. empty ([UNMATCHED-LKW] ; loc ; [UNMATCHED-LKW]) \ id as unmatched-locks
  68. (* rfi for LF events: link each LKW to the LF events in its critical section *)
  69. let rfi-lf = ([LKW] ; po-loc ; [LF]) \ ([LKW] ; po-loc ; [UL] ; po-loc)
  70. (* Utility macro to convert a single pair to a single-edge relation *)
  71. let pair-to-relation p = p ++ 0
  72. (*
  73. * If a given LF event e is outside a critical section, it cannot read
  74. * internally but it may read from an LKW event in another thread.
  75. * Compute the relation containing these possible edges.
  76. *)
  77. let possible-rfe-noncrit-lf e = (LKW * {e}) & loc & ext
  78. (* Compute set of sets of possible rfe edges for LF events *)
  79. let all-possible-rfe-lf =
  80. (*
  81. * Convert the possible-rfe-noncrit-lf relation for e
  82. * to a set of single edges
  83. *)
  84. let set-of-singleton-rfe-lf e =
  85. map pair-to-relation (possible-rfe-noncrit-lf e)
  86. (* Do this for each LF event e that isn't in rfi-lf *)
  87. in map set-of-singleton-rfe-lf (LF \ range(rfi-lf))
  88. (* Generate all rf relations for LF events *)
  89. with rfe-lf from cross(all-possible-rfe-lf)
  90. let rf-lf = rfe-lf | rfi-lf
  91. (*
  92. * A given RU event e may read internally from the last po-previous UL,
  93. * or it may read from a UL event in another thread or the initial write.
  94. * Compute the relation containing these possible edges.
  95. *)
  96. let possible-rf-ru e = (((UL * {e}) & po-loc) \
  97. ([UL] ; po-loc ; [UL] ; po-loc)) |
  98. (((UL | IW) * {e}) & loc & ext)
  99. (* Compute set of sets of possible rf edges for RU events *)
  100. let all-possible-rf-ru =
  101. (* Convert the possible-rf-ru relation for e to a set of single edges *)
  102. let set-of-singleton-rf-ru e =
  103. map pair-to-relation (possible-rf-ru e)
  104. (* Do this for each RU event e *)
  105. in map set-of-singleton-rf-ru RU
  106. (* Generate all rf relations for RU events *)
  107. with rf-ru from cross(all-possible-rf-ru)
  108. (* Final rf relation *)
  109. let rf = rf | rf-lf | rf-ru
  110. (* Generate all co relations, including LKW events but not UL *)
  111. let co0 = co0 | ([IW] ; loc ; [LKW]) |
  112. (([LKW] ; loc ; [UNMATCHED-LKW]) \ [UNMATCHED-LKW])
  113. include "cos-opt.cat"
  114. let W = W | UL
  115. let M = R | W
  116. (* Merge UL events into co *)
  117. let co = (co | critical | (critical^-1 ; co))+
  118. let coe = co & ext
  119. let coi = co & int
  120. (* Merge LKR events into rf *)
  121. let rf = rf | ([IW | UL] ; singlestep(co) ; lk-rmw^-1)
  122. let rfe = rf & ext
  123. let rfi = rf & int
  124. let fr = rf^-1 ; co
  125. let fre = fr & ext
  126. let fri = fr & int
  127. show co,rf,fr