Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 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 | * |
Andrea Parri | 1a00b455 | 2018-05-14 16:33:56 -0700 | [diff] [blame] | 8 | * An earlier version of this file appeared in the companion webpage for |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 9 | * "Frightening small children and disconcerting grown-ups: Concurrency |
| 10 | * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern, |
Andrea Parri | 1a00b455 | 2018-05-14 16:33:56 -0700 | [diff] [blame] | 11 | * which appeared in ASPLOS 2018. |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 12 | *) |
| 13 | |
Andrea Parri | 48d44d4 | 2018-02-20 15:25:01 -0800 | [diff] [blame] | 14 | "Linux-kernel memory consistency model" |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 15 | |
| 16 | (* |
| 17 | * File "lock.cat" handles locks and is experimental. |
| 18 | * It can be replaced by include "cos.cat" for tests that do not use locks. |
| 19 | *) |
| 20 | |
| 21 | include "lock.cat" |
| 22 | |
| 23 | (*******************) |
| 24 | (* Basic relations *) |
| 25 | (*******************) |
| 26 | |
| 27 | (* Fences *) |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 28 | let rmb = [R \ Noreturn] ; fencerel(Rmb) ; [R \ Noreturn] |
| 29 | let wmb = [W] ; fencerel(Wmb) ; [W] |
| 30 | let mb = ([M] ; fencerel(Mb) ; [M]) | |
Paul E. McKenney | cac79a39 | 2018-02-20 15:25:11 -0800 | [diff] [blame] | 31 | ([M] ; fencerel(Before-atomic) ; [RMW] ; po? ; [M]) | |
| 32 | ([M] ; po? ; [RMW] ; fencerel(After-atomic) ; [M]) | |
Andrea Parri | 5b735eb | 2018-12-03 15:04:49 -0800 | [diff] [blame^] | 33 | ([M] ; po? ; [LKW] ; fencerel(After-spinlock) ; [M]) | |
| 34 | ([M] ; po ; [UL] ; (co | po) ; [LKW] ; |
| 35 | fencerel(After-unlock-lock) ; [M]) |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 36 | let gp = po ; [Sync-rcu] ; po? |
| 37 | |
| 38 | let strong-fence = mb | gp |
| 39 | |
| 40 | (* Release Acquire *) |
| 41 | let acq-po = [Acquire] ; po ; [M] |
| 42 | let po-rel = [M] ; po ; [Release] |
Alan Stern | 6e89e83 | 2018-09-26 11:29:17 -0700 | [diff] [blame] | 43 | let po-unlock-rf-lock-po = po ; [UL] ; rf ; [LKR] ; po |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 44 | |
| 45 | (**********************************) |
| 46 | (* Fundamental coherence ordering *) |
| 47 | (**********************************) |
| 48 | |
| 49 | (* Sequential Consistency Per Variable *) |
| 50 | let com = rf | co | fr |
| 51 | acyclic po-loc | com as coherence |
| 52 | |
| 53 | (* Atomic Read-Modify-Write *) |
| 54 | empty rmw & (fre ; coe) as atomic |
| 55 | |
| 56 | (**********************************) |
| 57 | (* Instruction execution ordering *) |
| 58 | (**********************************) |
| 59 | |
| 60 | (* Preserved Program Order *) |
| 61 | let dep = addr | data |
| 62 | let rwdep = (dep | ctrl) ; [W] |
| 63 | let overwrite = co | fr |
| 64 | let to-w = rwdep | (overwrite & int) |
Alan Stern | 6e89e83 | 2018-09-26 11:29:17 -0700 | [diff] [blame] | 65 | let to-r = addr | (dep ; rfi) |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 66 | let fence = strong-fence | wmb | po-rel | rmb | acq-po |
Alan Stern | 6e89e83 | 2018-09-26 11:29:17 -0700 | [diff] [blame] | 67 | let ppo = to-r | to-w | fence | (po-unlock-rf-lock-po & int) |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 68 | |
| 69 | (* Propagation: Ordering from release operations and strong fences. *) |
| 70 | let A-cumul(r) = rfe? ; r |
Alan Stern | 6e89e83 | 2018-09-26 11:29:17 -0700 | [diff] [blame] | 71 | let cumul-fence = A-cumul(strong-fence | po-rel) | wmb | po-unlock-rf-lock-po |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 72 | let prop = (overwrite & ext)? ; cumul-fence* ; rfe? |
| 73 | |
| 74 | (* |
| 75 | * Happens Before: Ordering from the passage of time. |
| 76 | * No fences needed here for prop because relation confined to one process. |
| 77 | *) |
| 78 | let hb = ppo | rfe | ((prop \ id) & int) |
| 79 | acyclic hb as happens-before |
| 80 | |
| 81 | (****************************************) |
| 82 | (* Write and fence propagation ordering *) |
| 83 | (****************************************) |
| 84 | |
| 85 | (* Propagation: Each non-rf link needs a strong fence. *) |
| 86 | let pb = prop ; strong-fence ; hb* |
| 87 | acyclic pb as propagation |
| 88 | |
| 89 | (*******) |
| 90 | (* RCU *) |
| 91 | (*******) |
| 92 | |
| 93 | (* |
| 94 | * Effect of read-side critical section proceeds from the rcu_read_lock() |
| 95 | * onward on the one hand and from the rcu_read_unlock() backwards on the |
| 96 | * other hand. |
| 97 | *) |
| 98 | let rscs = po ; crit^-1 ; po? |
| 99 | |
| 100 | (* |
| 101 | * The synchronize_rcu() strong fence is special in that it can order not |
| 102 | * one but two non-rf relations, but only in conjunction with an RCU |
| 103 | * read-side critical section. |
| 104 | *) |
Alan Stern | 1ee2da5 | 2018-05-14 16:33:39 -0700 | [diff] [blame] | 105 | let rcu-link = hb* ; pb* ; prop |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 106 | |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 107 | (* |
Alan Stern | 9d03688 | 2018-05-14 16:33:40 -0700 | [diff] [blame] | 108 | * Any sequence containing at least as many grace periods as RCU read-side |
| 109 | * critical sections (joined by rcu-link) acts as a generalized strong fence. |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 110 | *) |
Alan Stern | 9d03688 | 2018-05-14 16:33:40 -0700 | [diff] [blame] | 111 | let rec rcu-fence = gp | |
| 112 | (gp ; rcu-link ; rscs) | |
| 113 | (rscs ; rcu-link ; gp) | |
| 114 | (gp ; rcu-link ; rcu-fence ; rcu-link ; rscs) | |
| 115 | (rscs ; rcu-link ; rcu-fence ; rcu-link ; gp) | |
| 116 | (rcu-fence ; rcu-link ; rcu-fence) |
| 117 | |
| 118 | (* rb orders instructions just as pb does *) |
| 119 | let rb = prop ; rcu-fence ; hb* ; pb* |
Paul E. McKenney | 1c27b64 | 2018-01-18 19:58:55 -0800 | [diff] [blame] | 120 | |
Alan Stern | 1ee2da5 | 2018-05-14 16:33:39 -0700 | [diff] [blame] | 121 | irreflexive rb as rcu |
Alan Stern | 9d03688 | 2018-05-14 16:33:40 -0700 | [diff] [blame] | 122 | |
| 123 | (* |
| 124 | * The happens-before, propagation, and rcu constraints are all |
| 125 | * expressions of temporal ordering. They could be replaced by |
| 126 | * a single constraint on an "executes-before" relation, xb: |
| 127 | * |
| 128 | * let xb = hb | pb | rb |
| 129 | * acyclic xb as executes-before |
| 130 | *) |