lock.cat 4.7 KB

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