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 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 16enum 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(),...*) 21instructions R[Accesses] 22instructions W[Accesses] 23instructions RMW[Accesses] 24 25enum 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*) 37instructions 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 *) 44let FailedRMW = RMW \ (domain(rmw) | range(rmw)) 45let Acquire = ACQUIRE \ W \ FailedRMW 46let Release = RELEASE \ R \ FailedRMW 47let Mb = MB \ FailedRMW 48let Noreturn = NORETURN \ W 49 50(* SRCU *) 51enum SRCU = 'srcu-lock || 'srcu-unlock || 'sync-srcu 52instructions SRCU[SRCU] 53(* All srcu events *) 54let Srcu = Srcu-lock | Srcu-unlock | Sync-srcu 55 56(* Compute matching pairs of nested Rcu-lock and Rcu-unlock *) 57let 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 *) 69flag ~empty Rcu-lock \ domain(rcu-rscs) as unmatched-rcu-lock 70flag ~empty Rcu-unlock \ range(rcu-rscs) as unmatched-rcu-unlock 71 72(* Compute matching pairs of nested Srcu-lock and Srcu-unlock *) 73let carry-srcu-data = (data ; [~ Srcu-unlock] ; rf)* 74let srcu-rscs = ([Srcu-lock] ; carry-srcu-data ; data ; [Srcu-unlock]) & loc 75 76(* Validate nesting *) 77flag ~empty Srcu-lock \ domain(srcu-rscs) as unmatched-srcu-lock 78flag ~empty Srcu-unlock \ range(srcu-rscs) as unmatched-srcu-unlock 79flag ~empty (srcu-rscs^-1 ; srcu-rscs) \ id as multiple-srcu-matches 80 81(* Check for use of synchronize_srcu() inside an RCU critical section *) 82flag ~empty rcu-rscs & (po ; [Sync-srcu] ; po) as invalid-sleep 83 84(* Validate SRCU dynamic match *) 85flag ~empty different-values(srcu-rscs) as srcu-bad-value-match 86 87(* Compute marked and plain memory accesses *) 88let Marked = (~M) | IW | ONCE | RELEASE | ACQUIRE | MB | RMW | 89 LKR | LKW | UL | LF | RL | RU | Srcu-lock | Srcu-unlock 90let Plain = M \ Marked 91 92(* Redefine dependencies to include those carried through plain accesses *) 93let carry-dep = (data ; [~ Srcu-unlock] ; rfi)* 94let addr = carry-dep ; addr 95let ctrl = carry-dep ; ctrl 96let data = carry-dep ; data 97 98flag ~empty (if "lkmmv2" then 0 else _) 99 as this-model-requires-variant-higher-than-lkmmv1 100