18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci(*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Jade Alglave <j.alglave@ucl.ac.uk>,
48c2ecf20Sopenharmony_ci * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria
58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu>,
68c2ecf20Sopenharmony_ci *                    Andrea Parri <parri.andrea@gmail.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * An earlier version of this file appeared in the companion webpage for
98c2ecf20Sopenharmony_ci * "Frightening small children and disconcerting grown-ups: Concurrency
108c2ecf20Sopenharmony_ci * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern,
118c2ecf20Sopenharmony_ci * which appeared in ASPLOS 2018.
128c2ecf20Sopenharmony_ci *)
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci"Linux-kernel memory consistency model"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci(*
178c2ecf20Sopenharmony_ci * File "lock.cat" handles locks and is experimental.
188c2ecf20Sopenharmony_ci * It can be replaced by include "cos.cat" for tests that do not use locks.
198c2ecf20Sopenharmony_ci *)
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciinclude "lock.cat"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci(*******************)
248c2ecf20Sopenharmony_ci(* Basic relations *)
258c2ecf20Sopenharmony_ci(*******************)
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci(* Release Acquire *)
288c2ecf20Sopenharmony_cilet acq-po = [Acquire] ; po ; [M]
298c2ecf20Sopenharmony_cilet po-rel = [M] ; po ; [Release]
308c2ecf20Sopenharmony_cilet po-unlock-rf-lock-po = po ; [UL] ; rf ; [LKR] ; po
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci(* Fences *)
338c2ecf20Sopenharmony_cilet R4rmb = R \ Noreturn	(* Reads for which rmb works *)
348c2ecf20Sopenharmony_cilet rmb = [R4rmb] ; fencerel(Rmb) ; [R4rmb]
358c2ecf20Sopenharmony_cilet wmb = [W] ; fencerel(Wmb) ; [W]
368c2ecf20Sopenharmony_cilet mb = ([M] ; fencerel(Mb) ; [M]) |
378c2ecf20Sopenharmony_ci	([M] ; fencerel(Before-atomic) ; [RMW] ; po? ; [M]) |
388c2ecf20Sopenharmony_ci	([M] ; po? ; [RMW] ; fencerel(After-atomic) ; [M]) |
398c2ecf20Sopenharmony_ci	([M] ; po? ; [LKW] ; fencerel(After-spinlock) ; [M]) |
408c2ecf20Sopenharmony_ci	([M] ; po ; [UL] ; (co | po) ; [LKW] ;
418c2ecf20Sopenharmony_ci		fencerel(After-unlock-lock) ; [M])
428c2ecf20Sopenharmony_cilet gp = po ; [Sync-rcu | Sync-srcu] ; po?
438c2ecf20Sopenharmony_cilet strong-fence = mb | gp
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cilet nonrw-fence = strong-fence | po-rel | acq-po
468c2ecf20Sopenharmony_cilet fence = nonrw-fence | wmb | rmb
478c2ecf20Sopenharmony_cilet barrier = fencerel(Barrier | Rmb | Wmb | Mb | Sync-rcu | Sync-srcu |
488c2ecf20Sopenharmony_ci		Before-atomic | After-atomic | Acquire | Release |
498c2ecf20Sopenharmony_ci		Rcu-lock | Rcu-unlock | Srcu-lock | Srcu-unlock) |
508c2ecf20Sopenharmony_ci	(po ; [Release]) | ([Acquire] ; po)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci(**********************************)
538c2ecf20Sopenharmony_ci(* Fundamental coherence ordering *)
548c2ecf20Sopenharmony_ci(**********************************)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci(* Sequential Consistency Per Variable *)
578c2ecf20Sopenharmony_cilet com = rf | co | fr
588c2ecf20Sopenharmony_ciacyclic po-loc | com as coherence
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci(* Atomic Read-Modify-Write *)
618c2ecf20Sopenharmony_ciempty rmw & (fre ; coe) as atomic
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci(**********************************)
648c2ecf20Sopenharmony_ci(* Instruction execution ordering *)
658c2ecf20Sopenharmony_ci(**********************************)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci(* Preserved Program Order *)
688c2ecf20Sopenharmony_cilet dep = addr | data
698c2ecf20Sopenharmony_cilet rwdep = (dep | ctrl) ; [W]
708c2ecf20Sopenharmony_cilet overwrite = co | fr
718c2ecf20Sopenharmony_cilet to-w = rwdep | (overwrite & int) | (addr ; [Plain] ; wmb)
728c2ecf20Sopenharmony_cilet to-r = addr | (dep ; [Marked] ; rfi)
738c2ecf20Sopenharmony_cilet ppo = to-r | to-w | fence | (po-unlock-rf-lock-po & int)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci(* Propagation: Ordering from release operations and strong fences. *)
768c2ecf20Sopenharmony_cilet A-cumul(r) = (rfe ; [Marked])? ; r
778c2ecf20Sopenharmony_cilet cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb |
788c2ecf20Sopenharmony_ci	po-unlock-rf-lock-po) ; [Marked]
798c2ecf20Sopenharmony_cilet prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ;
808c2ecf20Sopenharmony_ci	[Marked] ; rfe? ; [Marked]
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci(*
838c2ecf20Sopenharmony_ci * Happens Before: Ordering from the passage of time.
848c2ecf20Sopenharmony_ci * No fences needed here for prop because relation confined to one process.
858c2ecf20Sopenharmony_ci *)
868c2ecf20Sopenharmony_cilet hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked]
878c2ecf20Sopenharmony_ciacyclic hb as happens-before
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci(****************************************)
908c2ecf20Sopenharmony_ci(* Write and fence propagation ordering *)
918c2ecf20Sopenharmony_ci(****************************************)
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci(* Propagation: Each non-rf link needs a strong fence. *)
948c2ecf20Sopenharmony_cilet pb = prop ; strong-fence ; hb* ; [Marked]
958c2ecf20Sopenharmony_ciacyclic pb as propagation
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci(*******)
988c2ecf20Sopenharmony_ci(* RCU *)
998c2ecf20Sopenharmony_ci(*******)
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci(*
1028c2ecf20Sopenharmony_ci * Effects of read-side critical sections proceed from the rcu_read_unlock()
1038c2ecf20Sopenharmony_ci * or srcu_read_unlock() backwards on the one hand, and from the
1048c2ecf20Sopenharmony_ci * rcu_read_lock() or srcu_read_lock() forwards on the other hand.
1058c2ecf20Sopenharmony_ci *
1068c2ecf20Sopenharmony_ci * In the definition of rcu-fence below, the po term at the left-hand side
1078c2ecf20Sopenharmony_ci * of each disjunct and the po? term at the right-hand end have been factored
1088c2ecf20Sopenharmony_ci * out.  They have been moved into the definitions of rcu-link and rb.
1098c2ecf20Sopenharmony_ci * This was necessary in order to apply the "& loc" tests correctly.
1108c2ecf20Sopenharmony_ci *)
1118c2ecf20Sopenharmony_cilet rcu-gp = [Sync-rcu]		(* Compare with gp *)
1128c2ecf20Sopenharmony_cilet srcu-gp = [Sync-srcu]
1138c2ecf20Sopenharmony_cilet rcu-rscsi = rcu-rscs^-1
1148c2ecf20Sopenharmony_cilet srcu-rscsi = srcu-rscs^-1
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci(*
1178c2ecf20Sopenharmony_ci * The synchronize_rcu() strong fence is special in that it can order not
1188c2ecf20Sopenharmony_ci * one but two non-rf relations, but only in conjunction with an RCU
1198c2ecf20Sopenharmony_ci * read-side critical section.
1208c2ecf20Sopenharmony_ci *)
1218c2ecf20Sopenharmony_cilet rcu-link = po? ; hb* ; pb* ; prop ; po
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci(*
1248c2ecf20Sopenharmony_ci * Any sequence containing at least as many grace periods as RCU read-side
1258c2ecf20Sopenharmony_ci * critical sections (joined by rcu-link) induces order like a generalized
1268c2ecf20Sopenharmony_ci * inter-CPU strong fence.
1278c2ecf20Sopenharmony_ci * Likewise for SRCU grace periods and read-side critical sections, provided
1288c2ecf20Sopenharmony_ci * the synchronize_srcu() and srcu_read_[un]lock() calls refer to the same
1298c2ecf20Sopenharmony_ci * struct srcu_struct location.
1308c2ecf20Sopenharmony_ci *)
1318c2ecf20Sopenharmony_cilet rec rcu-order = rcu-gp | srcu-gp |
1328c2ecf20Sopenharmony_ci	(rcu-gp ; rcu-link ; rcu-rscsi) |
1338c2ecf20Sopenharmony_ci	((srcu-gp ; rcu-link ; srcu-rscsi) & loc) |
1348c2ecf20Sopenharmony_ci	(rcu-rscsi ; rcu-link ; rcu-gp) |
1358c2ecf20Sopenharmony_ci	((srcu-rscsi ; rcu-link ; srcu-gp) & loc) |
1368c2ecf20Sopenharmony_ci	(rcu-gp ; rcu-link ; rcu-order ; rcu-link ; rcu-rscsi) |
1378c2ecf20Sopenharmony_ci	((srcu-gp ; rcu-link ; rcu-order ; rcu-link ; srcu-rscsi) & loc) |
1388c2ecf20Sopenharmony_ci	(rcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; rcu-gp) |
1398c2ecf20Sopenharmony_ci	((srcu-rscsi ; rcu-link ; rcu-order ; rcu-link ; srcu-gp) & loc) |
1408c2ecf20Sopenharmony_ci	(rcu-order ; rcu-link ; rcu-order)
1418c2ecf20Sopenharmony_cilet rcu-fence = po ; rcu-order ; po?
1428c2ecf20Sopenharmony_cilet fence = fence | rcu-fence
1438c2ecf20Sopenharmony_cilet strong-fence = strong-fence | rcu-fence
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci(* rb orders instructions just as pb does *)
1468c2ecf20Sopenharmony_cilet rb = prop ; rcu-fence ; hb* ; pb* ; [Marked]
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ciirreflexive rb as rcu
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci(*
1518c2ecf20Sopenharmony_ci * The happens-before, propagation, and rcu constraints are all
1528c2ecf20Sopenharmony_ci * expressions of temporal ordering.  They could be replaced by
1538c2ecf20Sopenharmony_ci * a single constraint on an "executes-before" relation, xb:
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * let xb = hb | pb | rb
1568c2ecf20Sopenharmony_ci * acyclic xb as executes-before
1578c2ecf20Sopenharmony_ci *)
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci(*********************************)
1608c2ecf20Sopenharmony_ci(* Plain accesses and data races *)
1618c2ecf20Sopenharmony_ci(*********************************)
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci(* Warn about plain writes and marked accesses in the same region *)
1648c2ecf20Sopenharmony_cilet mixed-accesses = ([Plain & W] ; (po-loc \ barrier) ; [Marked]) |
1658c2ecf20Sopenharmony_ci	([Marked] ; (po-loc \ barrier) ; [Plain & W])
1668c2ecf20Sopenharmony_ciflag ~empty mixed-accesses as mixed-accesses
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci(* Executes-before and visibility *)
1698c2ecf20Sopenharmony_cilet xbstar = (hb | pb | rb)*
1708c2ecf20Sopenharmony_cilet vis = cumul-fence* ; rfe? ; [Marked] ;
1718c2ecf20Sopenharmony_ci	((strong-fence ; [Marked] ; xbstar) | (xbstar & int))
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci(* Boundaries for lifetimes of plain accesses *)
1748c2ecf20Sopenharmony_cilet w-pre-bounded = [Marked] ; (addr | fence)?
1758c2ecf20Sopenharmony_cilet r-pre-bounded = [Marked] ; (addr | nonrw-fence |
1768c2ecf20Sopenharmony_ci	([R4rmb] ; fencerel(Rmb) ; [~Noreturn]))?
1778c2ecf20Sopenharmony_cilet w-post-bounded = fence? ; [Marked]
1788c2ecf20Sopenharmony_cilet r-post-bounded = (nonrw-fence | ([~Noreturn] ; fencerel(Rmb) ; [R4rmb]))? ;
1798c2ecf20Sopenharmony_ci	[Marked]
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci(* Visibility and executes-before for plain accesses *)
1828c2ecf20Sopenharmony_cilet ww-vis = fence | (strong-fence ; xbstar ; w-pre-bounded) |
1838c2ecf20Sopenharmony_ci	(w-post-bounded ; vis ; w-pre-bounded)
1848c2ecf20Sopenharmony_cilet wr-vis = fence | (strong-fence ; xbstar ; r-pre-bounded) |
1858c2ecf20Sopenharmony_ci	(w-post-bounded ; vis ; r-pre-bounded)
1868c2ecf20Sopenharmony_cilet rw-xbstar = fence | (r-post-bounded ; xbstar ; w-pre-bounded)
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci(* Potential races *)
1898c2ecf20Sopenharmony_cilet pre-race = ext & ((Plain * M) | ((M \ IW) * Plain))
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci(* Coherence requirements for plain accesses *)
1928c2ecf20Sopenharmony_cilet wr-incoh = pre-race & rf & rw-xbstar^-1
1938c2ecf20Sopenharmony_cilet rw-incoh = pre-race & fr & wr-vis^-1
1948c2ecf20Sopenharmony_cilet ww-incoh = pre-race & co & ww-vis^-1
1958c2ecf20Sopenharmony_ciempty (wr-incoh | rw-incoh | ww-incoh) as plain-coherence
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci(* Actual races *)
1988c2ecf20Sopenharmony_cilet ww-nonrace = ww-vis & ((Marked * W) | rw-xbstar) & ((W * Marked) | wr-vis)
1998c2ecf20Sopenharmony_cilet ww-race = (pre-race & co) \ ww-nonrace
2008c2ecf20Sopenharmony_cilet wr-race = (pre-race & (co? ; rf)) \ wr-vis \ rw-xbstar^-1
2018c2ecf20Sopenharmony_cilet rw-race = (pre-race & fr) \ rw-xbstar
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ciflag ~empty (ww-race | wr-race | rw-race) as data-race
204