0001 // SPDX-License-Identifier: GPL-2.0+
0002 (*
0003 * Copyright (C) 2015 Jade Alglave <j.alglave@ucl.ac.uk>,
0004 * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria
0005 * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu>,
0006 * Andrea Parri <parri.andrea@gmail.com>
0007 *
0008 * An earlier version of this file appeared in the companion webpage for
0009 * "Frightening small children and disconcerting grown-ups: Concurrency
0010 * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern,
0011 * which appeared in ASPLOS 2018.
0012 *)
0013
0014 "Linux-kernel memory consistency model"
0015
0016 (*
0017 * File "lock.cat" handles locks and is experimental.
0018 * It can be replaced by include "cos.cat" for tests that do not use locks.
0019 *)
0020
0021 include "lock.cat"
0022
0023 (*******************)
0024 (* Basic relations *)
0025 (*******************)
0026
0027 (* Release Acquire *)
0028 let acq-po = [Acquire] ; po ; [M]
0029 let po-rel = [M] ; po ; [Release]
0030 let po-unlock-lock-po = po ; [UL] ; (po|rf) ; [LKR] ; po
0031
0032 (* Fences *)
0033 let R4rmb = R \ Noreturn (* Reads for which rmb works *)
0034 let rmb = [R4rmb] ; fencerel(Rmb) ; [R4rmb]
0035 let wmb = [W] ; fencerel(Wmb) ; [W]
0036 let mb = ([M] ; fencerel(Mb) ; [M]) |
0037 ([M] ; fencerel(Before-atomic) ; [RMW] ; po? ; [M]) |
0038 ([M] ; po? ; [RMW] ; fencerel(After-atomic) ; [M]) |
0039 ([M] ; po? ; [LKW] ; fencerel(After-spinlock) ; [M]) |
0040 ([M] ; po ; [UL] ; (co | po) ; [LKW] ;
0041 fencerel(After-unlock-lock) ; [M])
0042 let gp = po ; [Sync-rcu | Sync-srcu] ; po?
0043 let strong-fence = mb | gp
0044
0045 let nonrw-fence = strong-fence | po-rel | acq-po
0046 let fence = nonrw-fence | wmb | rmb
0047 let barrier = fencerel(Barrier | Rmb | Wmb | Mb | Sync-rcu | Sync-srcu |
0048 Before-atomic | After-atomic | Acquire | Release |
0049 Rcu-lock | Rcu-unlock | Srcu-lock | Srcu-unlock) |
0050 (po ; [Release]) | ([Acquire] ; po)
0051
0052 (**********************************)
0053 (* Fundamental coherence ordering *)
0054 (**********************************)
0055
0056 (* Sequential Consistency Per Variable *)
0057 let com = rf | co | fr
0058 acyclic po-loc | com as coherence
0059
0060 (* Atomic Read-Modify-Write *)
0061 empty rmw & (fre ; coe) as atomic
0062
0063 (**********************************)
0064 (* Instruction execution ordering *)
0065 (**********************************)
0066
0067 (* Preserved Program Order *)
0068 let dep = addr | data
0069 let rwdep = (dep | ctrl) ; [W]
0070 let overwrite = co | fr
0071 let to-w = rwdep | (overwrite & int) | (addr ; [Plain] ; wmb)
0072 let to-r = addr | (dep ; [Marked] ; rfi)
0073 let ppo = to-r | to-w | fence | (po-unlock-lock-po & int)
0074
0075 (* Propagation: Ordering from release operations and strong fences. *)
0076 let A-cumul(r) = (rfe ; [Marked])? ; r
0077 let cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb |
0078 po-unlock-lock-po) ; [Marked]
0079 let prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ;
0080 [Marked] ; rfe? ; [Marked]
0081
0082 (*
0083 * Happens Before: Ordering from the passage of time.
0084 * No fences needed here for prop because relation confined to one process.
0085 *)
0086 let hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked]
0087 acyclic hb as happens-before
0088
0089 (****************************************)
0090 (* Write and fence propagation ordering *)
0091 (****************************************)
0092
0093 (* Propagation: Each non-rf link needs a strong fence. *)
0094 let pb = prop ; strong-fence ; hb* ; [Marked]
0095 acyclic pb as propagation
0096
0097 (*******)
0098 (* RCU *)
0099 (*******)
0100
0101 (*
0102 * Effects of read-side critical sections proceed from the rcu_read_unlock()
0103 * or srcu_read_unlock() backwards on the one hand, and from the
0104 * rcu_read_lock() or srcu_read_lock() forwards on the other hand.
0105 *
0106 * In the definition of rcu-fence below, the po term at the left-hand side
0107 * of each disjunct and the po? term at the right-hand end have been factored
0108 * out. They have been moved into the definitions of rcu-link and rb.
0109 * This was necessary in order to apply the "& loc" tests correctly.
0110 *)
0111 let rcu-gp = [Sync-rcu] (* Compare with gp *)
0112 let srcu-gp = [Sync-srcu]
0113 let rcu-rscsi = rcu-rscs^-1
0114 let srcu-rscsi = srcu-rscs^-1
0115
0116 (*
0117 * The synchronize_rcu() strong fence is special in that it can order not
0118 * one but two non-rf relations, but only in conjunction with an RCU
0119 * read-side critical section.
0120 *)
0121 let rcu-link = po? ; hb* ; pb* ; prop ; po
0122
0123 (*
0124 * Any sequence containing at least as many grace periods as RCU read-side
0125 * critical sections (joined by rcu-link) induces order like a generalized
0126 * inter-CPU strong fence.
0127 * Likewise for SRCU grace periods and read-side critical sections, provided
0128 * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same
0129 * struct srcu_struct location.
0130 *)
0131 let rec rcu-order = rcu-gp | srcu-gp |
0132 (rcu-gp ; rcu-link ; rcu-rscsi) |
0133 ((srcu-gp ; rcu-link ; srcu-rscsi) & loc) |
0134 (rcu-rscsi ; rcu-link ; rcu-gp) |
0135 ((srcu-rscsi ; rcu-link ; srcu-gp) & loc) |
0136 (rcu-gp ; rcu-link ; rcu-order ; rcu-link ; rcu-rscsi) |
0137 ((srcu-gp ; rcu-link ; rcu-order ; rcu-link ; srcu-rscsi) & loc) |
0138 (rcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; rcu-gp) |
0139 ((srcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; srcu-gp) & loc) |
0140 (rcu-order ; rcu-link ; rcu-order)
0141 let rcu-fence = po ; rcu-order ; po?
0142 let fence = fence | rcu-fence
0143 let strong-fence = strong-fence | rcu-fence
0144
0145 (* rb orders instructions just as pb does *)
0146 let rb = prop ; rcu-fence ; hb* ; pb* ; [Marked]
0147
0148 irreflexive rb as rcu
0149
0150 (*
0151 * The happens-before, propagation, and rcu constraints are all
0152 * expressions of temporal ordering. They could be replaced by
0153 * a single constraint on an "executes-before" relation, xb:
0154 *
0155 * let xb = hb | pb | rb
0156 * acyclic xb as executes-before
0157 *)
0158
0159 (*********************************)
0160 (* Plain accesses and data races *)
0161 (*********************************)
0162
0163 (* Warn about plain writes and marked accesses in the same region *)
0164 let mixed-accesses = ([Plain & W] ; (po-loc \ barrier) ; [Marked]) |
0165 ([Marked] ; (po-loc \ barrier) ; [Plain & W])
0166 flag ~empty mixed-accesses as mixed-accesses
0167
0168 (* Executes-before and visibility *)
0169 let xbstar = (hb | pb | rb)*
0170 let vis = cumul-fence* ; rfe? ; [Marked] ;
0171 ((strong-fence ; [Marked] ; xbstar) | (xbstar & int))
0172
0173 (* Boundaries for lifetimes of plain accesses *)
0174 let w-pre-bounded = [Marked] ; (addr | fence)?
0175 let r-pre-bounded = [Marked] ; (addr | nonrw-fence |
0176 ([R4rmb] ; fencerel(Rmb) ; [~Noreturn]))?
0177 let w-post-bounded = fence? ; [Marked]
0178 let r-post-bounded = (nonrw-fence | ([~Noreturn] ; fencerel(Rmb) ; [R4rmb]))? ;
0179 [Marked]
0180
0181 (* Visibility and executes-before for plain accesses *)
0182 let ww-vis = fence | (strong-fence ; xbstar ; w-pre-bounded) |
0183 (w-post-bounded ; vis ; w-pre-bounded)
0184 let wr-vis = fence | (strong-fence ; xbstar ; r-pre-bounded) |
0185 (w-post-bounded ; vis ; r-pre-bounded)
0186 let rw-xbstar = fence | (r-post-bounded ; xbstar ; w-pre-bounded)
0187
0188 (* Potential races *)
0189 let pre-race = ext & ((Plain * M) | ((M \ IW) * Plain))
0190
0191 (* Coherence requirements for plain accesses *)
0192 let wr-incoh = pre-race & rf & rw-xbstar^-1
0193 let rw-incoh = pre-race & fr & wr-vis^-1
0194 let ww-incoh = pre-race & co & ww-vis^-1
0195 empty (wr-incoh | rw-incoh | ww-incoh) as plain-coherence
0196
0197 (* Actual races *)
0198 let ww-nonrace = ww-vis & ((Marked * W) | rw-xbstar) & ((W * Marked) | wr-vis)
0199 let ww-race = (pre-race & co) \ ww-nonrace
0200 let wr-race = (pre-race & (co? ; rf)) \ wr-vis \ rw-xbstar^-1
0201 let rw-race = (pre-race & fr) \ rw-xbstar
0202
0203 flag ~empty (ww-race | wr-race | rw-race) as data-race