11c27b644SPaul E. McKenney// SPDX-License-Identifier: GPL-2.0+ 21c27b644SPaul E. McKenney(* 31c27b644SPaul E. McKenney * Copyright (C) 2015 Jade Alglave <j.alglave@ucl.ac.uk>, 41c27b644SPaul E. McKenney * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria 51c27b644SPaul E. McKenney * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu>, 61c27b644SPaul E. McKenney * Andrea Parri <parri.andrea@gmail.com> 71c27b644SPaul E. McKenney * 81a00b455SAndrea Parri * An earlier version of this file appeared in the companion webpage for 91c27b644SPaul E. McKenney * "Frightening small children and disconcerting grown-ups: Concurrency 101c27b644SPaul E. McKenney * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern, 111a00b455SAndrea Parri * which appeared in ASPLOS 2018. 121c27b644SPaul E. McKenney *) 131c27b644SPaul E. McKenney 1448d44d4eSAndrea Parri"Linux-kernel memory consistency model" 151c27b644SPaul E. McKenney 161c27b644SPaul E. McKenney(* 171c27b644SPaul E. McKenney * File "lock.cat" handles locks and is experimental. 181c27b644SPaul E. McKenney * It can be replaced by include "cos.cat" for tests that do not use locks. 191c27b644SPaul E. McKenney *) 201c27b644SPaul E. McKenney 211c27b644SPaul E. McKenneyinclude "lock.cat" 221c27b644SPaul E. McKenney 231c27b644SPaul E. McKenney(*******************) 241c27b644SPaul E. McKenney(* Basic relations *) 251c27b644SPaul E. McKenney(*******************) 261c27b644SPaul E. McKenney 274494dd58SAlan Stern(* Release Acquire *) 284494dd58SAlan Sternlet acq-po = [Acquire] ; po ; [M] 294494dd58SAlan Sternlet po-rel = [M] ; po ; [Release] 30ddfe1294SBoqun Fenglet po-unlock-lock-po = po ; [UL] ; (po|rf) ; [LKR] ; po 314494dd58SAlan Stern 321c27b644SPaul E. McKenney(* Fences *) 334494dd58SAlan Sternlet R4rmb = R \ Noreturn (* Reads for which rmb works *) 344494dd58SAlan Sternlet rmb = [R4rmb] ; fencerel(Rmb) ; [R4rmb] 351c27b644SPaul E. McKenneylet wmb = [W] ; fencerel(Wmb) ; [W] 361c27b644SPaul E. McKenneylet mb = ([M] ; fencerel(Mb) ; [M]) | 37cac79a39SPaul E. McKenney ([M] ; fencerel(Before-atomic) ; [RMW] ; po? ; [M]) | 38cac79a39SPaul E. McKenney ([M] ; po? ; [RMW] ; fencerel(After-atomic) ; [M]) | 395b735eb1SAndrea Parri ([M] ; po? ; [LKW] ; fencerel(After-spinlock) ; [M]) | 40dd409de2SJonas Oberhauser(* 41dd409de2SJonas Oberhauser * Note: The po-unlock-lock-po relation only passes the lock to the direct 42dd409de2SJonas Oberhauser * successor, perhaps giving the impression that the ordering of the 43dd409de2SJonas Oberhauser * smp_mb__after_unlock_lock() fence only affects a single lock handover. 44dd409de2SJonas Oberhauser * However, in a longer sequence of lock handovers, the implicit 45dd409de2SJonas Oberhauser * A-cumulative release fences of lock-release ensure that any stores that 46dd409de2SJonas Oberhauser * propagate to one of the involved CPUs before it hands over the lock to 47dd409de2SJonas Oberhauser * the next CPU will also propagate to the final CPU handing over the lock 48dd409de2SJonas Oberhauser * to the CPU that executes the fence. Therefore, all those stores are 49dd409de2SJonas Oberhauser * also affected by the fence. 50dd409de2SJonas Oberhauser *) 51dd409de2SJonas Oberhauser ([M] ; po-unlock-lock-po ; 5202bae7a2SPaul E. McKenney [After-unlock-lock] ; po ; [M]) | 5302bae7a2SPaul E. McKenney ([M] ; po? ; [Srcu-unlock] ; fencerel(After-srcu-read-unlock) ; [M]) 54a3f600d9SAlan Sternlet gp = po ; [Sync-rcu | Sync-srcu] ; po? 551c27b644SPaul E. McKenneylet strong-fence = mb | gp 561c27b644SPaul E. McKenney 574494dd58SAlan Sternlet nonrw-fence = strong-fence | po-rel | acq-po 584494dd58SAlan Sternlet fence = nonrw-fence | wmb | rmb 590031e38aSAlan Sternlet barrier = fencerel(Barrier | Rmb | Wmb | Mb | Sync-rcu | Sync-srcu | 60f9de4171SAlan Stern Before-atomic | After-atomic | Acquire | Release | 61f9de4171SAlan Stern Rcu-lock | Rcu-unlock | Srcu-lock | Srcu-unlock) | 620031e38aSAlan Stern (po ; [Release]) | ([Acquire] ; po) 631c27b644SPaul E. McKenney 641c27b644SPaul E. McKenney(**********************************) 651c27b644SPaul E. McKenney(* Fundamental coherence ordering *) 661c27b644SPaul E. McKenney(**********************************) 671c27b644SPaul E. McKenney 681c27b644SPaul E. McKenney(* Sequential Consistency Per Variable *) 691c27b644SPaul E. McKenneylet com = rf | co | fr 701c27b644SPaul E. McKenneyacyclic po-loc | com as coherence 711c27b644SPaul E. McKenney 721c27b644SPaul E. McKenney(* Atomic Read-Modify-Write *) 731c27b644SPaul E. McKenneyempty rmw & (fre ; coe) as atomic 741c27b644SPaul E. McKenney 751c27b644SPaul E. McKenney(**********************************) 761c27b644SPaul E. McKenney(* Instruction execution ordering *) 771c27b644SPaul E. McKenney(**********************************) 781c27b644SPaul E. McKenney 791c27b644SPaul E. McKenney(* Preserved Program Order *) 801c27b644SPaul E. McKenneylet dep = addr | data 811c27b644SPaul E. McKenneylet rwdep = (dep | ctrl) ; [W] 821c27b644SPaul E. McKenneylet overwrite = co | fr 830031e38aSAlan Sternlet to-w = rwdep | (overwrite & int) | (addr ; [Plain] ; wmb) 84aa568c26SJoel Fernandes (Google)let to-r = (addr ; [R]) | (dep ; [Marked] ; rfi) 85*762e9357SJonas Oberhauserlet ppo = to-r | to-w | (fence & int) | (po-unlock-lock-po & int) 861c27b644SPaul E. McKenney 871c27b644SPaul E. McKenney(* Propagation: Ordering from release operations and strong fences. *) 88d1a84ab1SAlan Sternlet A-cumul(r) = (rfe ; [Marked])? ; r 89ebd50e29SAlan Sternlet rmw-sequence = (rf ; rmw)* 90d1a84ab1SAlan Sternlet cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb | 91ebd50e29SAlan Stern po-unlock-lock-po) ; [Marked] ; rmw-sequence 92d1a84ab1SAlan Sternlet prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ; 93d1a84ab1SAlan Stern [Marked] ; rfe? ; [Marked] 941c27b644SPaul E. McKenney 951c27b644SPaul E. McKenney(* 961c27b644SPaul E. McKenney * Happens Before: Ordering from the passage of time. 971c27b644SPaul E. McKenney * No fences needed here for prop because relation confined to one process. 981c27b644SPaul E. McKenney *) 99d1a84ab1SAlan Sternlet hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked] 1001c27b644SPaul E. McKenneyacyclic hb as happens-before 1011c27b644SPaul E. McKenney 1021c27b644SPaul E. McKenney(****************************************) 1031c27b644SPaul E. McKenney(* Write and fence propagation ordering *) 1041c27b644SPaul E. McKenney(****************************************) 1051c27b644SPaul E. McKenney 1061c27b644SPaul E. McKenney(* Propagation: Each non-rf link needs a strong fence. *) 107d1a84ab1SAlan Sternlet pb = prop ; strong-fence ; hb* ; [Marked] 1081c27b644SPaul E. McKenneyacyclic pb as propagation 1091c27b644SPaul E. McKenney 1101c27b644SPaul E. McKenney(*******) 1111c27b644SPaul E. McKenney(* RCU *) 1121c27b644SPaul E. McKenney(*******) 1131c27b644SPaul E. McKenney 1141c27b644SPaul E. McKenney(* 115284749b0SAlan Stern * Effects of read-side critical sections proceed from the rcu_read_unlock() 116a3f600d9SAlan Stern * or srcu_read_unlock() backwards on the one hand, and from the 117a3f600d9SAlan Stern * rcu_read_lock() or srcu_read_lock() forwards on the other hand. 118284749b0SAlan Stern * 119284749b0SAlan Stern * In the definition of rcu-fence below, the po term at the left-hand side 120284749b0SAlan Stern * of each disjunct and the po? term at the right-hand end have been factored 121284749b0SAlan Stern * out. They have been moved into the definitions of rcu-link and rb. 122a3f600d9SAlan Stern * This was necessary in order to apply the "& loc" tests correctly. 1231c27b644SPaul E. McKenney *) 124284749b0SAlan Sternlet rcu-gp = [Sync-rcu] (* Compare with gp *) 125a3f600d9SAlan Sternlet srcu-gp = [Sync-srcu] 126284749b0SAlan Sternlet rcu-rscsi = rcu-rscs^-1 127a3f600d9SAlan Sternlet srcu-rscsi = srcu-rscs^-1 1281c27b644SPaul E. McKenney 1291c27b644SPaul E. McKenney(* 1301c27b644SPaul E. McKenney * The synchronize_rcu() strong fence is special in that it can order not 1311c27b644SPaul E. McKenney * one but two non-rf relations, but only in conjunction with an RCU 1321c27b644SPaul E. McKenney * read-side critical section. 1331c27b644SPaul E. McKenney *) 134284749b0SAlan Sternlet rcu-link = po? ; hb* ; pb* ; prop ; po 1351c27b644SPaul E. McKenney 1361c27b644SPaul E. McKenney(* 1379d036883SAlan Stern * Any sequence containing at least as many grace periods as RCU read-side 13815aa25cbSAlan Stern * critical sections (joined by rcu-link) induces order like a generalized 13915aa25cbSAlan Stern * inter-CPU strong fence. 140a3f600d9SAlan Stern * Likewise for SRCU grace periods and read-side critical sections, provided 141a3f600d9SAlan Stern * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same 142a3f600d9SAlan Stern * struct srcu_struct location. 1431c27b644SPaul E. McKenney *) 14415aa25cbSAlan Sternlet rec rcu-order = rcu-gp | srcu-gp | 145284749b0SAlan Stern (rcu-gp ; rcu-link ; rcu-rscsi) | 146a3f600d9SAlan Stern ((srcu-gp ; rcu-link ; srcu-rscsi) & loc) | 147284749b0SAlan Stern (rcu-rscsi ; rcu-link ; rcu-gp) | 148a3f600d9SAlan Stern ((srcu-rscsi ; rcu-link ; srcu-gp) & loc) | 14915aa25cbSAlan Stern (rcu-gp ; rcu-link ; rcu-order ; rcu-link ; rcu-rscsi) | 15015aa25cbSAlan Stern ((srcu-gp ; rcu-link ; rcu-order ; rcu-link ; srcu-rscsi) & loc) | 15115aa25cbSAlan Stern (rcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; rcu-gp) | 15215aa25cbSAlan Stern ((srcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; srcu-gp) & loc) | 15315aa25cbSAlan Stern (rcu-order ; rcu-link ; rcu-order) 15415aa25cbSAlan Sternlet rcu-fence = po ; rcu-order ; po? 15515aa25cbSAlan Sternlet fence = fence | rcu-fence 15615aa25cbSAlan Sternlet strong-fence = strong-fence | rcu-fence 1579d036883SAlan Stern 1589d036883SAlan Stern(* rb orders instructions just as pb does *) 15915aa25cbSAlan Sternlet rb = prop ; rcu-fence ; hb* ; pb* ; [Marked] 1601c27b644SPaul E. McKenney 1611ee2da5fSAlan Sternirreflexive rb as rcu 1629d036883SAlan Stern 1639d036883SAlan Stern(* 1649d036883SAlan Stern * The happens-before, propagation, and rcu constraints are all 1659d036883SAlan Stern * expressions of temporal ordering. They could be replaced by 1669d036883SAlan Stern * a single constraint on an "executes-before" relation, xb: 1679d036883SAlan Stern * 1689d036883SAlan Stern * let xb = hb | pb | rb 1699d036883SAlan Stern * acyclic xb as executes-before 1709d036883SAlan Stern *) 1710031e38aSAlan Stern 1720031e38aSAlan Stern(*********************************) 1730031e38aSAlan Stern(* Plain accesses and data races *) 1740031e38aSAlan Stern(*********************************) 1750031e38aSAlan Stern 1760031e38aSAlan Stern(* Warn about plain writes and marked accesses in the same region *) 1770031e38aSAlan Sternlet mixed-accesses = ([Plain & W] ; (po-loc \ barrier) ; [Marked]) | 1780031e38aSAlan Stern ([Marked] ; (po-loc \ barrier) ; [Plain & W]) 1790031e38aSAlan Sternflag ~empty mixed-accesses as mixed-accesses 1800031e38aSAlan Stern 1810031e38aSAlan Stern(* Executes-before and visibility *) 1820031e38aSAlan Sternlet xbstar = (hb | pb | rb)* 1830031e38aSAlan Sternlet vis = cumul-fence* ; rfe? ; [Marked] ; 18415aa25cbSAlan Stern ((strong-fence ; [Marked] ; xbstar) | (xbstar & int)) 1850031e38aSAlan Stern 1860031e38aSAlan Stern(* Boundaries for lifetimes of plain accesses *) 1870031e38aSAlan Sternlet w-pre-bounded = [Marked] ; (addr | fence)? 1880031e38aSAlan Sternlet r-pre-bounded = [Marked] ; (addr | nonrw-fence | 1890031e38aSAlan Stern ([R4rmb] ; fencerel(Rmb) ; [~Noreturn]))? 190ebd50e29SAlan Sternlet w-post-bounded = fence? ; [Marked] ; rmw-sequence 1910031e38aSAlan Sternlet r-post-bounded = (nonrw-fence | ([~Noreturn] ; fencerel(Rmb) ; [R4rmb]))? ; 1920031e38aSAlan Stern [Marked] 1930031e38aSAlan Stern 1940031e38aSAlan Stern(* Visibility and executes-before for plain accesses *) 1954289ee7dSAlan Sternlet ww-vis = fence | (strong-fence ; xbstar ; w-pre-bounded) | 1964289ee7dSAlan Stern (w-post-bounded ; vis ; w-pre-bounded) 1974289ee7dSAlan Sternlet wr-vis = fence | (strong-fence ; xbstar ; r-pre-bounded) | 1984289ee7dSAlan Stern (w-post-bounded ; vis ; r-pre-bounded) 1994289ee7dSAlan Sternlet rw-xbstar = fence | (r-post-bounded ; xbstar ; w-pre-bounded) 2000031e38aSAlan Stern 2010031e38aSAlan Stern(* Potential races *) 2020031e38aSAlan Sternlet pre-race = ext & ((Plain * M) | ((M \ IW) * Plain)) 2030031e38aSAlan Stern 2040031e38aSAlan Stern(* Coherence requirements for plain accesses *) 2050031e38aSAlan Sternlet wr-incoh = pre-race & rf & rw-xbstar^-1 2060031e38aSAlan Sternlet rw-incoh = pre-race & fr & wr-vis^-1 2070031e38aSAlan Sternlet ww-incoh = pre-race & co & ww-vis^-1 2080031e38aSAlan Sternempty (wr-incoh | rw-incoh | ww-incoh) as plain-coherence 2090031e38aSAlan Stern 2100031e38aSAlan Stern(* Actual races *) 2110031e38aSAlan Sternlet ww-nonrace = ww-vis & ((Marked * W) | rw-xbstar) & ((W * Marked) | wr-vis) 2120031e38aSAlan Sternlet ww-race = (pre-race & co) \ ww-nonrace 213daebf24aSAlan Sternlet wr-race = (pre-race & (co? ; rf)) \ wr-vis \ rw-xbstar^-1 2140031e38aSAlan Sternlet rw-race = (pre-race & fr) \ rw-xbstar 2150031e38aSAlan Stern 2160031e38aSAlan Sternflag ~empty (ww-race | wr-race | rw-race) as data-race 217