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 | * |
| 8 | * An earlier version of this file appears in the companion webpage for |
| 9 | * "Frightening small children and disconcerting grown-ups: Concurrency |
| 10 | * in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern, |
| 11 | * which is to appear in ASPLOS 2018. |
| 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 *) |
| 28 | let rb-dep = [R] ; fencerel(Rb_dep) ; [R] |
| 29 | let rmb = [R \ Noreturn] ; fencerel(Rmb) ; [R \ Noreturn] |
| 30 | let wmb = [W] ; fencerel(Wmb) ; [W] |
| 31 | let mb = ([M] ; fencerel(Mb) ; [M]) | |
| 32 | ([M] ; fencerel(Before_atomic) ; [RMW] ; po? ; [M]) | |
| 33 | ([M] ; po? ; [RMW] ; fencerel(After_atomic) ; [M]) | |
| 34 | ([M] ; po? ; [LKW] ; fencerel(After_spinlock) ; [M]) |
| 35 | let gp = po ; [Sync-rcu] ; po? |
| 36 | |
| 37 | let strong-fence = mb | gp |
| 38 | |
| 39 | (* Release Acquire *) |
| 40 | let acq-po = [Acquire] ; po ; [M] |
| 41 | let po-rel = [M] ; po ; [Release] |
| 42 | let rfi-rel-acq = [Release] ; rfi ; [Acquire] |
| 43 | |
| 44 | (**********************************) |
| 45 | (* Fundamental coherence ordering *) |
| 46 | (**********************************) |
| 47 | |
| 48 | (* Sequential Consistency Per Variable *) |
| 49 | let com = rf | co | fr |
| 50 | acyclic po-loc | com as coherence |
| 51 | |
| 52 | (* Atomic Read-Modify-Write *) |
| 53 | empty rmw & (fre ; coe) as atomic |
| 54 | |
| 55 | (**********************************) |
| 56 | (* Instruction execution ordering *) |
| 57 | (**********************************) |
| 58 | |
| 59 | (* Preserved Program Order *) |
| 60 | let dep = addr | data |
| 61 | let rwdep = (dep | ctrl) ; [W] |
| 62 | let overwrite = co | fr |
| 63 | let to-w = rwdep | (overwrite & int) |
| 64 | let rrdep = addr | (dep ; rfi) |
| 65 | let strong-rrdep = rrdep+ & rb-dep |
| 66 | let to-r = strong-rrdep | rfi-rel-acq |
| 67 | let fence = strong-fence | wmb | po-rel | rmb | acq-po |
| 68 | let ppo = rrdep* ; (to-r | to-w | fence) |
| 69 | |
| 70 | (* Propagation: Ordering from release operations and strong fences. *) |
| 71 | let A-cumul(r) = rfe? ; r |
| 72 | let cumul-fence = A-cumul(strong-fence | po-rel) | wmb |
| 73 | let prop = (overwrite & ext)? ; cumul-fence* ; rfe? |
| 74 | |
| 75 | (* |
| 76 | * Happens Before: Ordering from the passage of time. |
| 77 | * No fences needed here for prop because relation confined to one process. |
| 78 | *) |
| 79 | let hb = ppo | rfe | ((prop \ id) & int) |
| 80 | acyclic hb as happens-before |
| 81 | |
| 82 | (****************************************) |
| 83 | (* Write and fence propagation ordering *) |
| 84 | (****************************************) |
| 85 | |
| 86 | (* Propagation: Each non-rf link needs a strong fence. *) |
| 87 | let pb = prop ; strong-fence ; hb* |
| 88 | acyclic pb as propagation |
| 89 | |
| 90 | (*******) |
| 91 | (* RCU *) |
| 92 | (*******) |
| 93 | |
| 94 | (* |
| 95 | * Effect of read-side critical section proceeds from the rcu_read_lock() |
| 96 | * onward on the one hand and from the rcu_read_unlock() backwards on the |
| 97 | * other hand. |
| 98 | *) |
| 99 | let rscs = po ; crit^-1 ; po? |
| 100 | |
| 101 | (* |
| 102 | * The synchronize_rcu() strong fence is special in that it can order not |
| 103 | * one but two non-rf relations, but only in conjunction with an RCU |
| 104 | * read-side critical section. |
| 105 | *) |
| 106 | let link = hb* ; pb* ; prop |
| 107 | |
| 108 | (* Chains that affect the RCU grace-period guarantee *) |
| 109 | let gp-link = gp ; link |
| 110 | let rscs-link = rscs ; link |
| 111 | |
| 112 | (* |
| 113 | * A cycle containing at least as many grace periods as RCU read-side |
| 114 | * critical sections is forbidden. |
| 115 | *) |
| 116 | let rec rcu-path = |
| 117 | gp-link | |
| 118 | (gp-link ; rscs-link) | |
| 119 | (rscs-link ; gp-link) | |
| 120 | (rcu-path ; rcu-path) | |
| 121 | (gp-link ; rcu-path ; rscs-link) | |
| 122 | (rscs-link ; rcu-path ; gp-link) |
| 123 | |
| 124 | irreflexive rcu-path as rcu |