1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * Sleepable Read-Copy Update mechanism for mutual exclusion, 4 * tiny variant. 5 * 6 * Copyright (C) IBM Corporation, 2017 7 * 8 * Author: Paul McKenney <paulmck@linux.ibm.com> 9 */ 10 11 #ifndef _LINUX_SRCU_TINY_H 12 #define _LINUX_SRCU_TINY_H 13 14 #include <linux/irq_work_types.h> 15 #include <linux/llist.h> 16 #include <linux/swait.h> 17 18 struct srcu_struct { 19 short srcu_lock_nesting[2]; /* srcu_read_lock() nesting depth. */ 20 u8 srcu_gp_running; /* GP workqueue running? */ 21 u8 srcu_gp_waiting; /* GP waiting for readers? */ 22 u8 srcu_atomic_gp_flag; /* Serialize atomic GP work.*/ 23 unsigned long srcu_idx; /* Current reader array element in bit 0x2. */ 24 unsigned long srcu_idx_max; /* Furthest future srcu_idx request. */ 25 struct swait_queue_head srcu_wq; 26 /* Last srcu_read_unlock() wakes GP. */ 27 struct rcu_head *srcu_cb_head; /* Pending callbacks: Head. */ 28 struct rcu_head **srcu_cb_tail; /* Pending callbacks: Tail. */ 29 struct work_struct srcu_work; /* For driving grace periods. */ 30 struct irq_work srcu_irq_work; /* Defer schedule_work() to irq work. */ 31 struct llist_head defer_cbs; /* Callbacks deferred on re-entry. */ 32 struct irq_work defer_iw; /* Re-issues defer_cbs later. */ 33 #ifdef CONFIG_DEBUG_LOCK_ALLOC 34 struct lockdep_map dep_map; 35 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 36 }; 37 38 void srcu_drive_gp(struct work_struct *wp); 39 void srcu_tiny_irq_work(struct irq_work *irq_work); 40 void srcu_defer_drain(struct irq_work *irq_work); 41 42 #define __SRCU_STRUCT_INIT(name, __ignored, ___ignored, ____ignored) \ 43 { \ 44 .srcu_wq = __SWAIT_QUEUE_HEAD_INITIALIZER(name.srcu_wq), \ 45 .srcu_cb_tail = &name.srcu_cb_head, \ 46 .srcu_work = __WORK_INITIALIZER(name.srcu_work, srcu_drive_gp), \ 47 .srcu_irq_work = { .func = srcu_tiny_irq_work }, \ 48 .defer_cbs = LLIST_HEAD_INIT(name.defer_cbs), \ 49 .defer_iw = { .node = { .u_flags = IRQ_WORK_HARD_IRQ }, \ 50 .func = srcu_defer_drain }, \ 51 __SRCU_DEP_MAP_INIT(name) \ 52 } 53 54 /* 55 * This odd _STATIC_ arrangement is needed for API compatibility with 56 * Tree SRCU, which needs some per-CPU data. 57 */ 58 #define DEFINE_SRCU(name) \ 59 struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name) 60 #define DEFINE_STATIC_SRCU(name) \ 61 static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name) 62 #define DEFINE_SRCU_FAST(name) DEFINE_SRCU(name) 63 #define DEFINE_STATIC_SRCU_FAST(name) \ 64 static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name) 65 #define DEFINE_SRCU_FAST_UPDOWN(name) DEFINE_SRCU(name) 66 #define DEFINE_STATIC_SRCU_FAST_UPDOWN(name) \ 67 static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name) 68 #define DEFINE_SRCU_ATOMIC(name) DEFINE_SRCU(name) 69 #define DEFINE_STATIC_SRCU_ATOMIC(name) \ 70 static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name) 71 72 // Dummy structure for srcu_notifier_head. 73 struct srcu_usage { }; 74 #define __SRCU_USAGE_INIT(name) { } 75 #define __init_srcu_struct_fast __init_srcu_struct 76 #define __init_srcu_struct_fast_updown __init_srcu_struct 77 #define __init_srcu_struct_atomic __init_srcu_struct 78 #ifndef CONFIG_DEBUG_LOCK_ALLOC 79 #define init_srcu_struct_fast init_srcu_struct 80 #define init_srcu_struct_fast_updown init_srcu_struct 81 #define init_srcu_struct_atomic init_srcu_struct 82 #endif // #ifndef CONFIG_DEBUG_LOCK_ALLOC 83 84 void synchronize_srcu(struct srcu_struct *ssp); 85 86 /* 87 * Counts the new reader in the appropriate per-CPU element of the 88 * srcu_struct. Can be invoked from irq/bh handlers, but the matching 89 * __srcu_read_unlock() must be in the same handler instance. Returns an 90 * index that must be passed to the matching srcu_read_unlock(). 91 */ 92 static inline int __srcu_read_lock(struct srcu_struct *ssp) 93 __acquires_shared(ssp) 94 { 95 int idx; 96 97 preempt_disable(); // Needed for PREEMPT_LAZY 98 idx = ((READ_ONCE(ssp->srcu_idx) + 1) & 0x2) >> 1; 99 WRITE_ONCE(ssp->srcu_lock_nesting[idx], READ_ONCE(ssp->srcu_lock_nesting[idx]) + 1); 100 preempt_enable(); 101 __acquire_shared(ssp); 102 return idx; 103 } 104 105 struct srcu_ctr; 106 107 static inline bool __srcu_ptr_to_ctr(struct srcu_struct *ssp, struct srcu_ctr __percpu *scpp) 108 { 109 return (int)(intptr_t)(struct srcu_ctr __force __kernel *)scpp; 110 } 111 112 static inline struct srcu_ctr __percpu *__srcu_ctr_to_ptr(struct srcu_struct *ssp, int idx) 113 { 114 return (struct srcu_ctr __percpu *)(intptr_t)idx; 115 } 116 117 static inline struct srcu_ctr __percpu *__srcu_read_lock_fast(struct srcu_struct *ssp) 118 __acquires_shared(ssp) 119 { 120 return __srcu_ctr_to_ptr(ssp, __srcu_read_lock(ssp)); 121 } 122 123 static inline void __srcu_read_unlock_fast(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp) 124 __releases_shared(ssp) 125 { 126 __srcu_read_unlock(ssp, __srcu_ptr_to_ctr(ssp, scp)); 127 } 128 129 static inline struct srcu_ctr __percpu *__srcu_read_lock_fast_updown(struct srcu_struct *ssp) 130 __acquires_shared(ssp) 131 { 132 return __srcu_ctr_to_ptr(ssp, __srcu_read_lock(ssp)); 133 } 134 135 static inline 136 void __srcu_read_unlock_fast_updown(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp) 137 __releases_shared(ssp) 138 { 139 __srcu_read_unlock(ssp, __srcu_ptr_to_ctr(ssp, scp)); 140 } 141 142 static inline void synchronize_srcu_expedited(struct srcu_struct *ssp) 143 { 144 synchronize_srcu(ssp); 145 } 146 147 void srcu_barrier(struct srcu_struct *ssp); 148 149 static inline void srcu_expedite_current(struct srcu_struct *ssp) { } 150 #define srcu_check_read_flavor(ssp, read_flavor) do { } while (0) 151 152 /* Defined here to avoid size increase for non-torture kernels. */ 153 static inline void srcu_torture_stats_print(struct srcu_struct *ssp, 154 char *tt, char *tf) 155 { 156 int idx; 157 158 idx = ((data_race(READ_ONCE(ssp->srcu_idx)) + 1) & 0x2) >> 1; 159 pr_alert("%s%s Tiny SRCU per-CPU(idx=%d): (%hd,%hd) gp: %lu->%lu\n", 160 tt, tf, idx, 161 data_race(READ_ONCE(ssp->srcu_lock_nesting[!idx])), 162 data_race(READ_ONCE(ssp->srcu_lock_nesting[idx])), 163 data_race(READ_ONCE(ssp->srcu_idx)), 164 data_race(READ_ONCE(ssp->srcu_idx_max))); 165 } 166 167 /** 168 * srcu_readers_active - returns true if there are readers. and false otherwise. 169 * @ssp: which srcu_struct to count active readers (holding srcu_read_lock). 170 * 171 * Note that this is not an atomic primitive, and can therefore suffer 172 * severe errors when invoked on an active srcu_struct. That said, it 173 * can be useful as an error check at cleanup time. 174 */ 175 static inline bool srcu_readers_active(struct srcu_struct *ssp) 176 { 177 return READ_ONCE(ssp->srcu_lock_nesting[0]) || READ_ONCE(ssp->srcu_lock_nesting[1]); 178 } 179 180 #endif 181