1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Sleepable Read-Copy Update mechanism for mutual exclusion, 4 * tiny version for non-preemptible single-CPU use. 5 * 6 * Copyright (C) IBM Corporation, 2017 7 * 8 * Author: Paul McKenney <paulmck@linux.ibm.com> 9 */ 10 11 #include <linux/export.h> 12 #include <linux/mutex.h> 13 #include <linux/preempt.h> 14 #include <linux/rcupdate_wait.h> 15 #include <linux/sched.h> 16 #include <linux/delay.h> 17 #include <linux/srcu.h> 18 19 #include <linux/rcu_node_tree.h> 20 #include "rcu_segcblist.h" 21 #include "rcu.h" 22 23 #ifndef CONFIG_TREE_RCU 24 int rcu_scheduler_active __read_mostly; 25 #else // #ifndef CONFIG_TREE_RCU 26 extern int rcu_scheduler_active; 27 #endif // #else // #ifndef CONFIG_TREE_RCU 28 static LIST_HEAD(srcu_boot_list); 29 static bool srcu_init_done; 30 31 static int init_srcu_struct_fields(struct srcu_struct *ssp) 32 { 33 ssp->srcu_lock_nesting[0] = 0; 34 ssp->srcu_lock_nesting[1] = 0; 35 init_swait_queue_head(&ssp->srcu_wq); 36 ssp->srcu_cb_head = NULL; 37 ssp->srcu_cb_tail = &ssp->srcu_cb_head; 38 ssp->srcu_gp_running = false; 39 ssp->srcu_gp_waiting = false; 40 ssp->srcu_idx = 0; 41 ssp->srcu_idx_max = 0; 42 INIT_WORK(&ssp->srcu_work, srcu_drive_gp); 43 INIT_LIST_HEAD(&ssp->srcu_work.entry); 44 return 0; 45 } 46 47 #ifdef CONFIG_DEBUG_LOCK_ALLOC 48 49 int __init_srcu_struct(struct srcu_struct *ssp, const char *name, 50 struct lock_class_key *key) 51 { 52 /* Don't re-initialize a lock while it is held. */ 53 debug_check_no_locks_freed((void *)ssp, sizeof(*ssp)); 54 lockdep_init_map(&ssp->dep_map, name, key, 0); 55 return init_srcu_struct_fields(ssp); 56 } 57 EXPORT_SYMBOL_GPL(__init_srcu_struct); 58 59 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 60 61 /* 62 * init_srcu_struct - initialize a sleep-RCU structure 63 * @ssp: structure to initialize. 64 * 65 * Must invoke this on a given srcu_struct before passing that srcu_struct 66 * to any other function. Each srcu_struct represents a separate domain 67 * of SRCU protection. 68 */ 69 int init_srcu_struct(struct srcu_struct *ssp) 70 { 71 return init_srcu_struct_fields(ssp); 72 } 73 EXPORT_SYMBOL_GPL(init_srcu_struct); 74 75 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 76 77 /* 78 * cleanup_srcu_struct - deconstruct a sleep-RCU structure 79 * @ssp: structure to clean up. 80 * 81 * Must invoke this after you are finished using a given srcu_struct that 82 * was initialized via init_srcu_struct(), else you leak memory. 83 */ 84 void cleanup_srcu_struct(struct srcu_struct *ssp) 85 { 86 WARN_ON(ssp->srcu_lock_nesting[0] || ssp->srcu_lock_nesting[1]); 87 flush_work(&ssp->srcu_work); 88 WARN_ON(ssp->srcu_gp_running); 89 WARN_ON(ssp->srcu_gp_waiting); 90 WARN_ON(ssp->srcu_cb_head); 91 WARN_ON(&ssp->srcu_cb_head != ssp->srcu_cb_tail); 92 WARN_ON(ssp->srcu_idx != ssp->srcu_idx_max); 93 WARN_ON(ssp->srcu_idx & 0x1); 94 } 95 EXPORT_SYMBOL_GPL(cleanup_srcu_struct); 96 97 /* 98 * Removes the count for the old reader from the appropriate element of 99 * the srcu_struct. 100 */ 101 void __srcu_read_unlock(struct srcu_struct *ssp, int idx) 102 { 103 int newval; 104 105 preempt_disable(); // Needed for PREEMPT_LAZY 106 newval = READ_ONCE(ssp->srcu_lock_nesting[idx]) - 1; 107 WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval); 108 preempt_enable(); 109 if (!newval && READ_ONCE(ssp->srcu_gp_waiting) && in_task()) 110 swake_up_one(&ssp->srcu_wq); 111 } 112 EXPORT_SYMBOL_GPL(__srcu_read_unlock); 113 114 /* 115 * Workqueue handler to drive one grace period and invoke any callbacks 116 * that become ready as a result. Single-CPU and !PREEMPTION operation 117 * means that we get away with murder on synchronization. ;-) 118 */ 119 void srcu_drive_gp(struct work_struct *wp) 120 { 121 int idx; 122 struct rcu_head *lh; 123 struct rcu_head *rhp; 124 struct srcu_struct *ssp; 125 126 ssp = container_of(wp, struct srcu_struct, srcu_work); 127 preempt_disable(); // Needed for PREEMPT_LAZY 128 if (ssp->srcu_gp_running || ULONG_CMP_GE(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max))) { 129 preempt_enable(); 130 return; /* Already running or nothing to do. */ 131 } 132 133 /* Remove recently arrived callbacks and wait for readers. */ 134 WRITE_ONCE(ssp->srcu_gp_running, true); 135 local_irq_disable(); 136 lh = ssp->srcu_cb_head; 137 ssp->srcu_cb_head = NULL; 138 ssp->srcu_cb_tail = &ssp->srcu_cb_head; 139 local_irq_enable(); 140 idx = (ssp->srcu_idx & 0x2) / 2; 141 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); 142 WRITE_ONCE(ssp->srcu_gp_waiting, true); /* srcu_read_unlock() wakes! */ 143 preempt_enable(); 144 swait_event_exclusive(ssp->srcu_wq, !READ_ONCE(ssp->srcu_lock_nesting[idx])); 145 preempt_disable(); // Needed for PREEMPT_LAZY 146 WRITE_ONCE(ssp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */ 147 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); 148 preempt_enable(); 149 150 /* Invoke the callbacks we removed above. */ 151 while (lh) { 152 rhp = lh; 153 lh = lh->next; 154 debug_rcu_head_callback(rhp); 155 local_bh_disable(); 156 rhp->func(rhp); 157 local_bh_enable(); 158 } 159 160 /* 161 * Enable rescheduling, and if there are more callbacks, 162 * reschedule ourselves. This can race with a call_srcu() 163 * at interrupt level, but the ->srcu_gp_running checks will 164 * straighten that out. 165 */ 166 preempt_disable(); // Needed for PREEMPT_LAZY 167 WRITE_ONCE(ssp->srcu_gp_running, false); 168 idx = ULONG_CMP_LT(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max)); 169 preempt_enable(); 170 if (idx) 171 schedule_work(&ssp->srcu_work); 172 } 173 EXPORT_SYMBOL_GPL(srcu_drive_gp); 174 175 static void srcu_gp_start_if_needed(struct srcu_struct *ssp) 176 { 177 unsigned long cookie; 178 179 lockdep_assert_preemption_disabled(); // Needed for PREEMPT_LAZY 180 cookie = get_state_synchronize_srcu(ssp); 181 if (ULONG_CMP_GE(READ_ONCE(ssp->srcu_idx_max), cookie)) { 182 return; 183 } 184 WRITE_ONCE(ssp->srcu_idx_max, cookie); 185 if (!READ_ONCE(ssp->srcu_gp_running)) { 186 if (likely(srcu_init_done)) 187 schedule_work(&ssp->srcu_work); 188 else if (list_empty(&ssp->srcu_work.entry)) 189 list_add(&ssp->srcu_work.entry, &srcu_boot_list); 190 } 191 } 192 193 /* 194 * Enqueue an SRCU callback on the specified srcu_struct structure, 195 * initiating grace-period processing if it is not already running. 196 */ 197 void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp, 198 rcu_callback_t func) 199 { 200 unsigned long flags; 201 202 rhp->func = func; 203 rhp->next = NULL; 204 preempt_disable(); // Needed for PREEMPT_LAZY 205 local_irq_save(flags); 206 *ssp->srcu_cb_tail = rhp; 207 ssp->srcu_cb_tail = &rhp->next; 208 local_irq_restore(flags); 209 srcu_gp_start_if_needed(ssp); 210 preempt_enable(); 211 } 212 EXPORT_SYMBOL_GPL(call_srcu); 213 214 /* 215 * synchronize_srcu - wait for prior SRCU read-side critical-section completion 216 */ 217 void synchronize_srcu(struct srcu_struct *ssp) 218 { 219 struct rcu_synchronize rs; 220 221 srcu_lock_sync(&ssp->dep_map); 222 223 RCU_LOCKDEP_WARN(lockdep_is_held(ssp) || 224 lock_is_held(&rcu_bh_lock_map) || 225 lock_is_held(&rcu_lock_map) || 226 lock_is_held(&rcu_sched_lock_map), 227 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section"); 228 229 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE) 230 return; 231 232 might_sleep(); 233 init_rcu_head_on_stack(&rs.head); 234 init_completion(&rs.completion); 235 call_srcu(ssp, &rs.head, wakeme_after_rcu); 236 wait_for_completion(&rs.completion); 237 destroy_rcu_head_on_stack(&rs.head); 238 } 239 EXPORT_SYMBOL_GPL(synchronize_srcu); 240 241 /* 242 * get_state_synchronize_srcu - Provide an end-of-grace-period cookie 243 */ 244 unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp) 245 { 246 unsigned long ret; 247 248 barrier(); 249 ret = (READ_ONCE(ssp->srcu_idx) + 3) & ~0x1; 250 barrier(); 251 return ret; 252 } 253 EXPORT_SYMBOL_GPL(get_state_synchronize_srcu); 254 255 /* 256 * start_poll_synchronize_srcu - Provide cookie and start grace period 257 * 258 * The difference between this and get_state_synchronize_srcu() is that 259 * this function ensures that the poll_state_synchronize_srcu() will 260 * eventually return the value true. 261 */ 262 unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp) 263 { 264 unsigned long ret; 265 266 preempt_disable(); // Needed for PREEMPT_LAZY 267 ret = get_state_synchronize_srcu(ssp); 268 srcu_gp_start_if_needed(ssp); 269 preempt_enable(); 270 return ret; 271 } 272 EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu); 273 274 /* 275 * poll_state_synchronize_srcu - Has cookie's grace period ended? 276 */ 277 bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie) 278 { 279 unsigned long cur_s = READ_ONCE(ssp->srcu_idx); 280 281 barrier(); 282 return cookie == SRCU_GET_STATE_COMPLETED || 283 ULONG_CMP_GE(cur_s, cookie) || ULONG_CMP_LT(cur_s, cookie - 3); 284 } 285 EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu); 286 287 #ifndef CONFIG_TREE_RCU 288 /* Lockdep diagnostics. */ 289 void __init rcu_scheduler_starting(void) 290 { 291 rcu_scheduler_active = RCU_SCHEDULER_RUNNING; 292 } 293 #endif // #ifndef CONFIG_TREE_RCU 294 295 /* 296 * Queue work for srcu_struct structures with early boot callbacks. 297 * The work won't actually execute until the workqueue initialization 298 * phase that takes place after the scheduler starts. 299 */ 300 void __init srcu_init(void) 301 { 302 struct srcu_struct *ssp; 303 304 srcu_init_done = true; 305 while (!list_empty(&srcu_boot_list)) { 306 ssp = list_first_entry(&srcu_boot_list, 307 struct srcu_struct, srcu_work.entry); 308 list_del_init(&ssp->srcu_work.entry); 309 schedule_work(&ssp->srcu_work); 310 } 311 } 312