| 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | (* |
| 3 | * Copyright (C) 2015 Jade Alglave <[email protected]>, |
| 4 | * Copyright (C) 2016 Luc Maranget <[email protected]> for Inria |
| 5 | * Copyright (C) 2017 Alan Stern <[email protected]>, |
| 6 | * Andrea Parri <[email protected]> |
| 7 | * |
| 8 | * An earlier version of this file appeared 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 appeared in ASPLOS 2018. |
| 12 | *) |
| 13 | |
| 14 | "Linux-kernel memory consistency model" |
| 15 | |
| 16 | enum Accesses = 'ONCE (*READ_ONCE,WRITE_ONCE*) || |
| 17 | 'RELEASE (*smp_store_release*) || |
| 18 | 'ACQUIRE (*smp_load_acquire*) || |
| 19 | 'NORETURN (* R of non-return RMW *) || |
| 20 | 'MB (*xchg(),cmpxchg(),...*) |
| 21 | instructions R[Accesses] |
| 22 | instructions W[Accesses] |
| 23 | instructions RMW[Accesses] |
| 24 | |
| 25 | enum Barriers = 'wmb (*smp_wmb*) || |
| 26 | 'rmb (*smp_rmb*) || |
| 27 | 'MB (*smp_mb*) || |
| 28 | 'barrier (*barrier*) || |
| 29 | 'rcu-lock (*rcu_read_lock*) || |
| 30 | 'rcu-unlock (*rcu_read_unlock*) || |
| 31 | 'sync-rcu (*synchronize_rcu*) || |
| 32 | 'before-atomic (*smp_mb__before_atomic*) || |
| 33 | 'after-atomic (*smp_mb__after_atomic*) || |
| 34 | 'after-spinlock (*smp_mb__after_spinlock*) || |
| 35 | 'after-unlock-lock (*smp_mb__after_unlock_lock*) || |
| 36 | 'after-srcu-read-unlock (*smp_mb__after_srcu_read_unlock*) |
| 37 | instructions F[Barriers] |
| 38 | |
| 39 | |
| 40 | (* |
| 41 | * Filter out syntactic annotations that do not provide the corresponding |
| 42 | * semantic ordering, such as Acquire on a store or Mb on a failed RMW. |
| 43 | *) |
| 44 | let FailedRMW = RMW \ (domain(rmw) | range(rmw)) |
| 45 | let Acquire = ACQUIRE \ W \ FailedRMW |
| 46 | let Release = RELEASE \ R \ FailedRMW |
| 47 | let Mb = MB \ FailedRMW |
| 48 | let Noreturn = NORETURN \ W |
| 49 | |
| 50 | (* SRCU *) |
| 51 | enum SRCU = 'srcu-lock || 'srcu-unlock || 'sync-srcu |
| 52 | instructions SRCU[SRCU] |
| 53 | (* All srcu events *) |
| 54 | let Srcu = Srcu-lock | Srcu-unlock | Sync-srcu |
| 55 | |
| 56 | (* Compute matching pairs of nested Rcu-lock and Rcu-unlock *) |
| 57 | let rcu-rscs = let rec |
| 58 | unmatched-locks = Rcu-lock \ domain(matched) |
| 59 | and unmatched-unlocks = Rcu-unlock \ range(matched) |
| 60 | and unmatched = unmatched-locks | unmatched-unlocks |
| 61 | and unmatched-po = [unmatched] ; po ; [unmatched] |
| 62 | and unmatched-locks-to-unlocks = |
| 63 | [unmatched-locks] ; po ; [unmatched-unlocks] |
| 64 | and matched = matched | (unmatched-locks-to-unlocks \ |
| 65 | (unmatched-po ; unmatched-po)) |
| 66 | in matched |
| 67 | |
| 68 | (* Validate nesting *) |
| 69 | flag ~empty Rcu-lock \ domain(rcu-rscs) as unmatched-rcu-lock |
| 70 | flag ~empty Rcu-unlock \ range(rcu-rscs) as unmatched-rcu-unlock |
| 71 | |
| 72 | (* Compute matching pairs of nested Srcu-lock and Srcu-unlock *) |
| 73 | let carry-srcu-data = (data ; [~ Srcu-unlock] ; rf)* |
| 74 | let srcu-rscs = ([Srcu-lock] ; carry-srcu-data ; data ; [Srcu-unlock]) & loc |
| 75 | |
| 76 | (* Validate nesting *) |
| 77 | flag ~empty Srcu-lock \ domain(srcu-rscs) as unmatched-srcu-lock |
| 78 | flag ~empty Srcu-unlock \ range(srcu-rscs) as unmatched-srcu-unlock |
| 79 | flag ~empty (srcu-rscs^-1 ; srcu-rscs) \ id as multiple-srcu-matches |
| 80 | |
| 81 | (* Check for use of synchronize_srcu() inside an RCU critical section *) |
| 82 | flag ~empty rcu-rscs & (po ; [Sync-srcu] ; po) as invalid-sleep |
| 83 | |
| 84 | (* Validate SRCU dynamic match *) |
| 85 | flag ~empty different-values(srcu-rscs) as srcu-bad-value-match |
| 86 | |
| 87 | (* Compute marked and plain memory accesses *) |
| 88 | let Marked = (~M) | IW | ONCE | RELEASE | ACQUIRE | MB | RMW | |
| 89 | LKR | LKW | UL | LF | RL | RU | Srcu-lock | Srcu-unlock |
| 90 | let Plain = M \ Marked |
| 91 | |
| 92 | (* Redefine dependencies to include those carried through plain accesses *) |
| 93 | let carry-dep = (data ; [~ Srcu-unlock] ; rfi)* |
| 94 | let addr = carry-dep ; addr |
| 95 | let ctrl = carry-dep ; ctrl |
| 96 | let data = carry-dep ; data |
| 97 | |
| 98 | flag ~empty (if "lkmmv2" then 0 else _) |
| 99 | as this-model-requires-variant-higher-than-lkmmv1 |