162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 262306a36Sopenharmony_ci(* 362306a36Sopenharmony_ci * Copyright (C) 2015 Jade Alglave <j.alglave@ucl.ac.uk>, 462306a36Sopenharmony_ci * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria 562306a36Sopenharmony_ci * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu>, 662306a36Sopenharmony_ci * Andrea Parri <parri.andrea@gmail.com> 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * An earlier version of this file appeared in the companion webpage for 962306a36Sopenharmony_ci * "Frightening small children and disconcerting grown-ups: Concurrency 1062306a36Sopenharmony_ci * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern, 1162306a36Sopenharmony_ci * which appeared in ASPLOS 2018. 1262306a36Sopenharmony_ci *) 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci"Linux-kernel memory consistency model" 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci(* 1762306a36Sopenharmony_ci * File "lock.cat" handles locks and is experimental. 1862306a36Sopenharmony_ci * It can be replaced by include "cos.cat" for tests that do not use locks. 1962306a36Sopenharmony_ci *) 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciinclude "lock.cat" 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci(*******************) 2462306a36Sopenharmony_ci(* Basic relations *) 2562306a36Sopenharmony_ci(*******************) 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci(* Release Acquire *) 2862306a36Sopenharmony_cilet acq-po = [Acquire] ; po ; [M] 2962306a36Sopenharmony_cilet po-rel = [M] ; po ; [Release] 3062306a36Sopenharmony_cilet po-unlock-lock-po = po ; [UL] ; (po|rf) ; [LKR] ; po 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci(* Fences *) 3362306a36Sopenharmony_cilet R4rmb = R \ Noreturn (* Reads for which rmb works *) 3462306a36Sopenharmony_cilet rmb = [R4rmb] ; fencerel(Rmb) ; [R4rmb] 3562306a36Sopenharmony_cilet wmb = [W] ; fencerel(Wmb) ; [W] 3662306a36Sopenharmony_cilet mb = ([M] ; fencerel(Mb) ; [M]) | 3762306a36Sopenharmony_ci ([M] ; fencerel(Before-atomic) ; [RMW] ; po? ; [M]) | 3862306a36Sopenharmony_ci ([M] ; po? ; [RMW] ; fencerel(After-atomic) ; [M]) | 3962306a36Sopenharmony_ci ([M] ; po? ; [LKW] ; fencerel(After-spinlock) ; [M]) | 4062306a36Sopenharmony_ci(* 4162306a36Sopenharmony_ci * Note: The po-unlock-lock-po relation only passes the lock to the direct 4262306a36Sopenharmony_ci * successor, perhaps giving the impression that the ordering of the 4362306a36Sopenharmony_ci * smp_mb__after_unlock_lock() fence only affects a single lock handover. 4462306a36Sopenharmony_ci * However, in a longer sequence of lock handovers, the implicit 4562306a36Sopenharmony_ci * A-cumulative release fences of lock-release ensure that any stores that 4662306a36Sopenharmony_ci * propagate to one of the involved CPUs before it hands over the lock to 4762306a36Sopenharmony_ci * the next CPU will also propagate to the final CPU handing over the lock 4862306a36Sopenharmony_ci * to the CPU that executes the fence. Therefore, all those stores are 4962306a36Sopenharmony_ci * also affected by the fence. 5062306a36Sopenharmony_ci *) 5162306a36Sopenharmony_ci ([M] ; po-unlock-lock-po ; 5262306a36Sopenharmony_ci [After-unlock-lock] ; po ; [M]) | 5362306a36Sopenharmony_ci ([M] ; po? ; [Srcu-unlock] ; fencerel(After-srcu-read-unlock) ; [M]) 5462306a36Sopenharmony_cilet gp = po ; [Sync-rcu | Sync-srcu] ; po? 5562306a36Sopenharmony_cilet strong-fence = mb | gp 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_cilet nonrw-fence = strong-fence | po-rel | acq-po 5862306a36Sopenharmony_cilet fence = nonrw-fence | wmb | rmb 5962306a36Sopenharmony_cilet barrier = fencerel(Barrier | Rmb | Wmb | Mb | Sync-rcu | Sync-srcu | 6062306a36Sopenharmony_ci Before-atomic | After-atomic | Acquire | Release | 6162306a36Sopenharmony_ci Rcu-lock | Rcu-unlock | Srcu-lock | Srcu-unlock) | 6262306a36Sopenharmony_ci (po ; [Release]) | ([Acquire] ; po) 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci(**********************************) 6562306a36Sopenharmony_ci(* Fundamental coherence ordering *) 6662306a36Sopenharmony_ci(**********************************) 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci(* Sequential Consistency Per Variable *) 6962306a36Sopenharmony_cilet com = rf | co | fr 7062306a36Sopenharmony_ciacyclic po-loc | com as coherence 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci(* Atomic Read-Modify-Write *) 7362306a36Sopenharmony_ciempty rmw & (fre ; coe) as atomic 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci(**********************************) 7662306a36Sopenharmony_ci(* Instruction execution ordering *) 7762306a36Sopenharmony_ci(**********************************) 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci(* Preserved Program Order *) 8062306a36Sopenharmony_cilet dep = addr | data 8162306a36Sopenharmony_cilet rwdep = (dep | ctrl) ; [W] 8262306a36Sopenharmony_cilet overwrite = co | fr 8362306a36Sopenharmony_cilet to-w = rwdep | (overwrite & int) | (addr ; [Plain] ; wmb) 8462306a36Sopenharmony_cilet to-r = (addr ; [R]) | (dep ; [Marked] ; rfi) 8562306a36Sopenharmony_cilet ppo = to-r | to-w | (fence & int) | (po-unlock-lock-po & int) 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci(* Propagation: Ordering from release operations and strong fences. *) 8862306a36Sopenharmony_cilet A-cumul(r) = (rfe ; [Marked])? ; r 8962306a36Sopenharmony_cilet rmw-sequence = (rf ; rmw)* 9062306a36Sopenharmony_cilet cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb | 9162306a36Sopenharmony_ci po-unlock-lock-po) ; [Marked] ; rmw-sequence 9262306a36Sopenharmony_cilet prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ; 9362306a36Sopenharmony_ci [Marked] ; rfe? ; [Marked] 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci(* 9662306a36Sopenharmony_ci * Happens Before: Ordering from the passage of time. 9762306a36Sopenharmony_ci * No fences needed here for prop because relation confined to one process. 9862306a36Sopenharmony_ci *) 9962306a36Sopenharmony_cilet hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked] 10062306a36Sopenharmony_ciacyclic hb as happens-before 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci(****************************************) 10362306a36Sopenharmony_ci(* Write and fence propagation ordering *) 10462306a36Sopenharmony_ci(****************************************) 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci(* Propagation: Each non-rf link needs a strong fence. *) 10762306a36Sopenharmony_cilet pb = prop ; strong-fence ; hb* ; [Marked] 10862306a36Sopenharmony_ciacyclic pb as propagation 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci(*******) 11162306a36Sopenharmony_ci(* RCU *) 11262306a36Sopenharmony_ci(*******) 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci(* 11562306a36Sopenharmony_ci * Effects of read-side critical sections proceed from the rcu_read_unlock() 11662306a36Sopenharmony_ci * or srcu_read_unlock() backwards on the one hand, and from the 11762306a36Sopenharmony_ci * rcu_read_lock() or srcu_read_lock() forwards on the other hand. 11862306a36Sopenharmony_ci * 11962306a36Sopenharmony_ci * In the definition of rcu-fence below, the po term at the left-hand side 12062306a36Sopenharmony_ci * of each disjunct and the po? term at the right-hand end have been factored 12162306a36Sopenharmony_ci * out. They have been moved into the definitions of rcu-link and rb. 12262306a36Sopenharmony_ci * This was necessary in order to apply the "& loc" tests correctly. 12362306a36Sopenharmony_ci *) 12462306a36Sopenharmony_cilet rcu-gp = [Sync-rcu] (* Compare with gp *) 12562306a36Sopenharmony_cilet srcu-gp = [Sync-srcu] 12662306a36Sopenharmony_cilet rcu-rscsi = rcu-rscs^-1 12762306a36Sopenharmony_cilet srcu-rscsi = srcu-rscs^-1 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci(* 13062306a36Sopenharmony_ci * The synchronize_rcu() strong fence is special in that it can order not 13162306a36Sopenharmony_ci * one but two non-rf relations, but only in conjunction with an RCU 13262306a36Sopenharmony_ci * read-side critical section. 13362306a36Sopenharmony_ci *) 13462306a36Sopenharmony_cilet rcu-link = po? ; hb* ; pb* ; prop ; po 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci(* 13762306a36Sopenharmony_ci * Any sequence containing at least as many grace periods as RCU read-side 13862306a36Sopenharmony_ci * critical sections (joined by rcu-link) induces order like a generalized 13962306a36Sopenharmony_ci * inter-CPU strong fence. 14062306a36Sopenharmony_ci * Likewise for SRCU grace periods and read-side critical sections, provided 14162306a36Sopenharmony_ci * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same 14262306a36Sopenharmony_ci * struct srcu_struct location. 14362306a36Sopenharmony_ci *) 14462306a36Sopenharmony_cilet rec rcu-order = rcu-gp | srcu-gp | 14562306a36Sopenharmony_ci (rcu-gp ; rcu-link ; rcu-rscsi) | 14662306a36Sopenharmony_ci ((srcu-gp ; rcu-link ; srcu-rscsi) & loc) | 14762306a36Sopenharmony_ci (rcu-rscsi ; rcu-link ; rcu-gp) | 14862306a36Sopenharmony_ci ((srcu-rscsi ; rcu-link ; srcu-gp) & loc) | 14962306a36Sopenharmony_ci (rcu-gp ; rcu-link ; rcu-order ; rcu-link ; rcu-rscsi) | 15062306a36Sopenharmony_ci ((srcu-gp ; rcu-link ; rcu-order ; rcu-link ; srcu-rscsi) & loc) | 15162306a36Sopenharmony_ci (rcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; rcu-gp) | 15262306a36Sopenharmony_ci ((srcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; srcu-gp) & loc) | 15362306a36Sopenharmony_ci (rcu-order ; rcu-link ; rcu-order) 15462306a36Sopenharmony_cilet rcu-fence = po ; rcu-order ; po? 15562306a36Sopenharmony_cilet fence = fence | rcu-fence 15662306a36Sopenharmony_cilet strong-fence = strong-fence | rcu-fence 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci(* rb orders instructions just as pb does *) 15962306a36Sopenharmony_cilet rb = prop ; rcu-fence ; hb* ; pb* ; [Marked] 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ciirreflexive rb as rcu 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci(* 16462306a36Sopenharmony_ci * The happens-before, propagation, and rcu constraints are all 16562306a36Sopenharmony_ci * expressions of temporal ordering. They could be replaced by 16662306a36Sopenharmony_ci * a single constraint on an "executes-before" relation, xb: 16762306a36Sopenharmony_ci * 16862306a36Sopenharmony_ci * let xb = hb | pb | rb 16962306a36Sopenharmony_ci * acyclic xb as executes-before 17062306a36Sopenharmony_ci *) 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci(*********************************) 17362306a36Sopenharmony_ci(* Plain accesses and data races *) 17462306a36Sopenharmony_ci(*********************************) 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci(* Warn about plain writes and marked accesses in the same region *) 17762306a36Sopenharmony_cilet mixed-accesses = ([Plain & W] ; (po-loc \ barrier) ; [Marked]) | 17862306a36Sopenharmony_ci ([Marked] ; (po-loc \ barrier) ; [Plain & W]) 17962306a36Sopenharmony_ciflag ~empty mixed-accesses as mixed-accesses 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci(* Executes-before and visibility *) 18262306a36Sopenharmony_cilet xbstar = (hb | pb | rb)* 18362306a36Sopenharmony_cilet vis = cumul-fence* ; rfe? ; [Marked] ; 18462306a36Sopenharmony_ci ((strong-fence ; [Marked] ; xbstar) | (xbstar & int)) 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci(* Boundaries for lifetimes of plain accesses *) 18762306a36Sopenharmony_cilet w-pre-bounded = [Marked] ; (addr | fence)? 18862306a36Sopenharmony_cilet r-pre-bounded = [Marked] ; (addr | nonrw-fence | 18962306a36Sopenharmony_ci ([R4rmb] ; fencerel(Rmb) ; [~Noreturn]))? 19062306a36Sopenharmony_cilet w-post-bounded = fence? ; [Marked] ; rmw-sequence 19162306a36Sopenharmony_cilet r-post-bounded = (nonrw-fence | ([~Noreturn] ; fencerel(Rmb) ; [R4rmb]))? ; 19262306a36Sopenharmony_ci [Marked] 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci(* Visibility and executes-before for plain accesses *) 19562306a36Sopenharmony_cilet ww-vis = fence | (strong-fence ; xbstar ; w-pre-bounded) | 19662306a36Sopenharmony_ci (w-post-bounded ; vis ; w-pre-bounded) 19762306a36Sopenharmony_cilet wr-vis = fence | (strong-fence ; xbstar ; r-pre-bounded) | 19862306a36Sopenharmony_ci (w-post-bounded ; vis ; r-pre-bounded) 19962306a36Sopenharmony_cilet rw-xbstar = fence | (r-post-bounded ; xbstar ; w-pre-bounded) 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci(* Potential races *) 20262306a36Sopenharmony_cilet pre-race = ext & ((Plain * M) | ((M \ IW) * Plain)) 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci(* Coherence requirements for plain accesses *) 20562306a36Sopenharmony_cilet wr-incoh = pre-race & rf & rw-xbstar^-1 20662306a36Sopenharmony_cilet rw-incoh = pre-race & fr & wr-vis^-1 20762306a36Sopenharmony_cilet ww-incoh = pre-race & co & ww-vis^-1 20862306a36Sopenharmony_ciempty (wr-incoh | rw-incoh | ww-incoh) as plain-coherence 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci(* Actual races *) 21162306a36Sopenharmony_cilet ww-nonrace = ww-vis & ((Marked * W) | rw-xbstar) & ((W * Marked) | wr-vis) 21262306a36Sopenharmony_cilet ww-race = (pre-race & co) \ ww-nonrace 21362306a36Sopenharmony_cilet wr-race = (pre-race & (co? ; rf)) \ wr-vis \ rw-xbstar^-1 21462306a36Sopenharmony_cilet rw-race = (pre-race & fr) \ rw-xbstar 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ciflag ~empty (ww-race | wr-race | rw-race) as data-race 217