linux-kernel.cat 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0+
  2. (*
  3. * Copyright (C) 2015 Jade Alglave <j.alglave@ucl.ac.uk>,
  4. * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria
  5. * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu>,
  6. * Andrea Parri <parri.andrea@gmail.com>
  7. *
  8. * An earlier version of this file appeared in the companion webpage for
  9. * "Frightening small children and disconcerting grown-ups: Concurrency
  10. * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern,
  11. * which appeared in ASPLOS 2018.
  12. *)
  13. "Linux-kernel memory consistency model"
  14. (*
  15. * File "lock.cat" handles locks and is experimental.
  16. * It can be replaced by include "cos.cat" for tests that do not use locks.
  17. *)
  18. include "lock.cat"
  19. (*******************)
  20. (* Basic relations *)
  21. (*******************)
  22. (* Release Acquire *)
  23. let acq-po = [Acquire] ; po ; [M]
  24. let po-rel = [M] ; po ; [Release]
  25. let po-unlock-rf-lock-po = po ; [UL] ; rf ; [LKR] ; po
  26. (* Fences *)
  27. let R4rmb = R \ Noreturn (* Reads for which rmb works *)
  28. let rmb = [R4rmb] ; fencerel(Rmb) ; [R4rmb]
  29. let wmb = [W] ; fencerel(Wmb) ; [W]
  30. let mb = ([M] ; fencerel(Mb) ; [M]) |
  31. ([M] ; fencerel(Before-atomic) ; [RMW] ; po? ; [M]) |
  32. ([M] ; po? ; [RMW] ; fencerel(After-atomic) ; [M]) |
  33. ([M] ; po? ; [LKW] ; fencerel(After-spinlock) ; [M]) |
  34. ([M] ; po ; [UL] ; (co | po) ; [LKW] ;
  35. fencerel(After-unlock-lock) ; [M])
  36. let gp = po ; [Sync-rcu | Sync-srcu] ; po?
  37. let strong-fence = mb | gp
  38. let nonrw-fence = strong-fence | po-rel | acq-po
  39. let fence = nonrw-fence | wmb | rmb
  40. let barrier = fencerel(Barrier | Rmb | Wmb | Mb | Sync-rcu | Sync-srcu |
  41. Before-atomic | After-atomic | Acquire | Release |
  42. Rcu-lock | Rcu-unlock | Srcu-lock | Srcu-unlock) |
  43. (po ; [Release]) | ([Acquire] ; po)
  44. (**********************************)
  45. (* Fundamental coherence ordering *)
  46. (**********************************)
  47. (* Sequential Consistency Per Variable *)
  48. let com = rf | co | fr
  49. acyclic po-loc | com as coherence
  50. (* Atomic Read-Modify-Write *)
  51. empty rmw & (fre ; coe) as atomic
  52. (**********************************)
  53. (* Instruction execution ordering *)
  54. (**********************************)
  55. (* Preserved Program Order *)
  56. let dep = addr | data
  57. let rwdep = (dep | ctrl) ; [W]
  58. let overwrite = co | fr
  59. let to-w = rwdep | (overwrite & int) | (addr ; [Plain] ; wmb)
  60. let to-r = addr | (dep ; [Marked] ; rfi)
  61. let ppo = to-r | to-w | fence | (po-unlock-rf-lock-po & int)
  62. (* Propagation: Ordering from release operations and strong fences. *)
  63. let A-cumul(r) = (rfe ; [Marked])? ; r
  64. let cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb |
  65. po-unlock-rf-lock-po) ; [Marked]
  66. let prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ;
  67. [Marked] ; rfe? ; [Marked]
  68. (*
  69. * Happens Before: Ordering from the passage of time.
  70. * No fences needed here for prop because relation confined to one process.
  71. *)
  72. let hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked]
  73. acyclic hb as happens-before
  74. (****************************************)
  75. (* Write and fence propagation ordering *)
  76. (****************************************)
  77. (* Propagation: Each non-rf link needs a strong fence. *)
  78. let pb = prop ; strong-fence ; hb* ; [Marked]
  79. acyclic pb as propagation
  80. (*******)
  81. (* RCU *)
  82. (*******)
  83. (*
  84. * Effects of read-side critical sections proceed from the rcu_read_unlock()
  85. * or srcu_read_unlock() backwards on the one hand, and from the
  86. * rcu_read_lock() or srcu_read_lock() forwards on the other hand.
  87. *
  88. * In the definition of rcu-fence below, the po term at the left-hand side
  89. * of each disjunct and the po? term at the right-hand end have been factored
  90. * out. They have been moved into the definitions of rcu-link and rb.
  91. * This was necessary in order to apply the "& loc" tests correctly.
  92. *)
  93. let rcu-gp = [Sync-rcu] (* Compare with gp *)
  94. let srcu-gp = [Sync-srcu]
  95. let rcu-rscsi = rcu-rscs^-1
  96. let srcu-rscsi = srcu-rscs^-1
  97. (*
  98. * The synchronize_rcu() strong fence is special in that it can order not
  99. * one but two non-rf relations, but only in conjunction with an RCU
  100. * read-side critical section.
  101. *)
  102. let rcu-link = po? ; hb* ; pb* ; prop ; po
  103. (*
  104. * Any sequence containing at least as many grace periods as RCU read-side
  105. * critical sections (joined by rcu-link) induces order like a generalized
  106. * inter-CPU strong fence.
  107. * Likewise for SRCU grace periods and read-side critical sections, provided
  108. * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same
  109. * struct srcu_struct location.
  110. *)
  111. let rec rcu-order = rcu-gp | srcu-gp |
  112. (rcu-gp ; rcu-link ; rcu-rscsi) |
  113. ((srcu-gp ; rcu-link ; srcu-rscsi) & loc) |
  114. (rcu-rscsi ; rcu-link ; rcu-gp) |
  115. ((srcu-rscsi ; rcu-link ; srcu-gp) & loc) |
  116. (rcu-gp ; rcu-link ; rcu-order ; rcu-link ; rcu-rscsi) |
  117. ((srcu-gp ; rcu-link ; rcu-order ; rcu-link ; srcu-rscsi) & loc) |
  118. (rcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; rcu-gp) |
  119. ((srcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; srcu-gp) & loc) |
  120. (rcu-order ; rcu-link ; rcu-order)
  121. let rcu-fence = po ; rcu-order ; po?
  122. let fence = fence | rcu-fence
  123. let strong-fence = strong-fence | rcu-fence
  124. (* rb orders instructions just as pb does *)
  125. let rb = prop ; rcu-fence ; hb* ; pb* ; [Marked]
  126. irreflexive rb as rcu
  127. (*
  128. * The happens-before, propagation, and rcu constraints are all
  129. * expressions of temporal ordering. They could be replaced by
  130. * a single constraint on an "executes-before" relation, xb:
  131. *
  132. * let xb = hb | pb | rb
  133. * acyclic xb as executes-before
  134. *)
  135. (*********************************)
  136. (* Plain accesses and data races *)
  137. (*********************************)
  138. (* Warn about plain writes and marked accesses in the same region *)
  139. let mixed-accesses = ([Plain & W] ; (po-loc \ barrier) ; [Marked]) |
  140. ([Marked] ; (po-loc \ barrier) ; [Plain & W])
  141. flag ~empty mixed-accesses as mixed-accesses
  142. (* Executes-before and visibility *)
  143. let xbstar = (hb | pb | rb)*
  144. let vis = cumul-fence* ; rfe? ; [Marked] ;
  145. ((strong-fence ; [Marked] ; xbstar) | (xbstar & int))
  146. (* Boundaries for lifetimes of plain accesses *)
  147. let w-pre-bounded = [Marked] ; (addr | fence)?
  148. let r-pre-bounded = [Marked] ; (addr | nonrw-fence |
  149. ([R4rmb] ; fencerel(Rmb) ; [~Noreturn]))?
  150. let w-post-bounded = fence? ; [Marked]
  151. let r-post-bounded = (nonrw-fence | ([~Noreturn] ; fencerel(Rmb) ; [R4rmb]))? ;
  152. [Marked]
  153. (* Visibility and executes-before for plain accesses *)
  154. let ww-vis = fence | (strong-fence ; xbstar ; w-pre-bounded) |
  155. (w-post-bounded ; vis ; w-pre-bounded)
  156. let wr-vis = fence | (strong-fence ; xbstar ; r-pre-bounded) |
  157. (w-post-bounded ; vis ; r-pre-bounded)
  158. let rw-xbstar = fence | (r-post-bounded ; xbstar ; w-pre-bounded)
  159. (* Potential races *)
  160. let pre-race = ext & ((Plain * M) | ((M \ IW) * Plain))
  161. (* Coherence requirements for plain accesses *)
  162. let wr-incoh = pre-race & rf & rw-xbstar^-1
  163. let rw-incoh = pre-race & fr & wr-vis^-1
  164. let ww-incoh = pre-race & co & ww-vis^-1
  165. empty (wr-incoh | rw-incoh | ww-incoh) as plain-coherence
  166. (* Actual races *)
  167. let ww-nonrace = ww-vis & ((Marked * W) | rw-xbstar) & ((W * Marked) | wr-vis)
  168. let ww-race = (pre-race & co) \ ww-nonrace
  169. let wr-race = (pre-race & (co? ; rf)) \ wr-vis \ rw-xbstar^-1
  170. let rw-race = (pre-race & fr) \ rw-xbstar
  171. flag ~empty (ww-race | wr-race | rw-race) as data-race