1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Sleepable Read-Copy Update mechanism for mutual exclusion. 4 * 5 * Copyright (C) IBM Corporation, 2006 6 * Copyright (C) Fujitsu, 2012 7 * 8 * Authors: Paul McKenney <paulmck@linux.ibm.com> 9 * Lai Jiangshan <laijs@cn.fujitsu.com> 10 * 11 * For detailed explanation of Read-Copy Update mechanism see - 12 * Documentation/RCU/ *.txt 13 * 14 */ 15 16 #define pr_fmt(fmt) "rcu: " fmt 17 18 #include <linux/export.h> 19 #include <linux/mutex.h> 20 #include <linux/percpu.h> 21 #include <linux/preempt.h> 22 #include <linux/irq_work.h> 23 #include <linux/llist.h> 24 #include <linux/rcupdate_wait.h> 25 #include <linux/sched.h> 26 #include <linux/smp.h> 27 #include <linux/delay.h> 28 #include <linux/module.h> 29 #include <linux/slab.h> 30 #include <linux/srcu.h> 31 32 #include "rcu.h" 33 #include "rcu_segcblist.h" 34 35 /* Holdoff in nanoseconds for auto-expediting. */ 36 #define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000) 37 static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF; 38 module_param(exp_holdoff, ulong, 0444); 39 40 /* Overflow-check frequency. N bits roughly says every 2**N grace periods. */ 41 static ulong counter_wrap_check = (ULONG_MAX >> 2); 42 module_param(counter_wrap_check, ulong, 0444); 43 44 /* 45 * Control conversion to SRCU_SIZE_BIG: 46 * 0: Don't convert at all. 47 * 1: Convert at init_srcu_struct() time. 48 * 2: Convert when rcutorture invokes srcu_torture_stats_print(). 49 * 3: Decide at boot time based on system shape (default). 50 * 0x1x: Convert when excessive contention encountered. 51 */ 52 #define SRCU_SIZING_NONE 0 53 #define SRCU_SIZING_INIT 1 54 #define SRCU_SIZING_TORTURE 2 55 #define SRCU_SIZING_AUTO 3 56 #define SRCU_SIZING_CONTEND 0x10 57 #define SRCU_SIZING_IS(x) ((convert_to_big & ~SRCU_SIZING_CONTEND) == x) 58 #define SRCU_SIZING_IS_NONE() (SRCU_SIZING_IS(SRCU_SIZING_NONE)) 59 #define SRCU_SIZING_IS_INIT() (SRCU_SIZING_IS(SRCU_SIZING_INIT)) 60 #define SRCU_SIZING_IS_TORTURE() (SRCU_SIZING_IS(SRCU_SIZING_TORTURE)) 61 #define SRCU_SIZING_IS_CONTEND() (convert_to_big & SRCU_SIZING_CONTEND) 62 static int convert_to_big = SRCU_SIZING_AUTO; 63 module_param(convert_to_big, int, 0444); 64 65 /* Number of CPUs to trigger init_srcu_struct()-time transition to big. */ 66 static int big_cpu_lim __read_mostly = 128; 67 module_param(big_cpu_lim, int, 0444); 68 69 /* Contention events per jiffy to initiate transition to big. */ 70 static int small_contention_lim __read_mostly = 100; 71 module_param(small_contention_lim, int, 0444); 72 73 /* Early-boot callback-management, so early that no lock is required! */ 74 static LIST_HEAD(srcu_boot_list); 75 static bool __read_mostly srcu_init_done; 76 77 static void srcu_invoke_callbacks(struct work_struct *work); 78 static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay); 79 static void process_srcu(struct work_struct *work); 80 static void srcu_irq_work(struct irq_work *work); 81 static void srcu_delay_timer(struct timer_list *t); 82 83 struct srcu_defer; 84 static void srcu_defer_drain(struct irq_work *iw); 85 static void __srcu_defer_drain(struct srcu_defer *sndp); 86 87 /* 88 * Per-CPU call_srcu() deferral state, shared by every srcu_struct. A deferred 89 * callback is staged on its srcu_data's ->defer_cbs; that srcu_data is chained 90 * via ->defer_link onto ->list, which the irq_work walks. 91 */ 92 struct srcu_defer { 93 struct llist_head list; 94 struct irq_work iw; 95 raw_spinlock_t lock; 96 bool draining; 97 }; 98 99 static DEFINE_PER_CPU(struct srcu_defer, srcu_defer) = { 100 .lock = __RAW_SPIN_LOCK_UNLOCKED(srcu_defer.lock), 101 .iw = IRQ_WORK_INIT_HARD(srcu_defer_drain), 102 }; 103 104 /* 105 * Flush pending deferred callbacks so a following srcu_barrier() waits for them. 106 */ 107 static void srcu_defer_flush(void) 108 { 109 int cpu; 110 111 for_each_possible_cpu(cpu) 112 __srcu_defer_drain(&per_cpu(srcu_defer, cpu)); 113 } 114 115 /* 116 * Initialize SRCU per-CPU data. Note that statically allocated 117 * srcu_struct structures might already have srcu_read_lock() and 118 * srcu_read_unlock() running against them. So if the is_static 119 * parameter is set, don't initialize ->srcu_ctrs[].srcu_locks and 120 * ->srcu_ctrs[].srcu_unlocks. 121 */ 122 static void init_srcu_struct_data(struct srcu_struct *ssp) 123 { 124 int cpu; 125 struct srcu_data *sdp; 126 127 /* 128 * Initialize the per-CPU srcu_data array, which feeds into the 129 * leaves of the srcu_node tree. 130 */ 131 for_each_possible_cpu(cpu) { 132 sdp = per_cpu_ptr(ssp->sda, cpu); 133 raw_spin_lock_init(&ACCESS_PRIVATE(sdp, lock)); 134 rcu_segcblist_init(&sdp->srcu_cblist); 135 sdp->srcu_cblist_invoking = false; 136 sdp->srcu_gp_seq_needed = ssp->srcu_sup->srcu_gp_seq; 137 sdp->srcu_gp_seq_needed_exp = ssp->srcu_sup->srcu_gp_seq; 138 sdp->srcu_barrier_head.next = &sdp->srcu_barrier_head; 139 sdp->mynode = NULL; 140 sdp->cpu = cpu; 141 INIT_WORK(&sdp->work, srcu_invoke_callbacks); 142 timer_setup(&sdp->delay_work, srcu_delay_timer, 0); 143 /* 144 * ->defer_cbs and ->defer_link are valid when zeroed and are not 145 * reinitialized here: that would clobber callbacks a reentrant 146 * call_srcu() already staged. See __call_srcu(). 147 */ 148 sdp->ssp = ssp; 149 } 150 } 151 152 /* Invalid seq state, used during snp node initialization */ 153 #define SRCU_SNP_INIT_SEQ 0x2 154 155 /* 156 * Check whether sequence number corresponding to snp node, 157 * is invalid. 158 */ 159 static inline bool srcu_invl_snp_seq(unsigned long s) 160 { 161 return s == SRCU_SNP_INIT_SEQ; 162 } 163 164 /* 165 * Allocated and initialize SRCU combining tree. Returns @true if 166 * allocation succeeded and @false otherwise. 167 */ 168 static bool init_srcu_struct_nodes(struct srcu_struct *ssp, gfp_t gfp_flags) 169 { 170 int cpu; 171 int i; 172 int level = 0; 173 int levelspread[RCU_NUM_LVLS]; 174 struct srcu_data *sdp; 175 struct srcu_node *snp; 176 struct srcu_node *snp_first; 177 178 /* Initialize geometry if it has not already been initialized. */ 179 rcu_init_geometry(); 180 ssp->srcu_sup->node = kzalloc_objs(*ssp->srcu_sup->node, rcu_num_nodes, 181 gfp_flags); 182 if (!ssp->srcu_sup->node) 183 return false; 184 185 /* Work out the overall tree geometry. */ 186 ssp->srcu_sup->level[0] = &ssp->srcu_sup->node[0]; 187 for (i = 1; i < rcu_num_lvls; i++) 188 ssp->srcu_sup->level[i] = ssp->srcu_sup->level[i - 1] + num_rcu_lvl[i - 1]; 189 rcu_init_levelspread(levelspread, num_rcu_lvl); 190 191 /* Each pass through this loop initializes one srcu_node structure. */ 192 srcu_for_each_node_breadth_first(ssp, snp) { 193 raw_spin_lock_init(&ACCESS_PRIVATE(snp, lock)); 194 BUILD_BUG_ON(ARRAY_SIZE(snp->srcu_have_cbs) != 195 ARRAY_SIZE(snp->srcu_data_have_cbs)); 196 for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) { 197 snp->srcu_have_cbs[i] = SRCU_SNP_INIT_SEQ; 198 snp->srcu_data_have_cbs[i] = 0; 199 } 200 snp->srcu_gp_seq_needed_exp = SRCU_SNP_INIT_SEQ; 201 snp->grplo = -1; 202 snp->grphi = -1; 203 if (snp == &ssp->srcu_sup->node[0]) { 204 /* Root node, special case. */ 205 snp->srcu_parent = NULL; 206 continue; 207 } 208 209 /* Non-root node. */ 210 if (snp == ssp->srcu_sup->level[level + 1]) 211 level++; 212 snp->srcu_parent = ssp->srcu_sup->level[level - 1] + 213 (snp - ssp->srcu_sup->level[level]) / 214 levelspread[level - 1]; 215 } 216 217 /* 218 * Initialize the per-CPU srcu_data array, which feeds into the 219 * leaves of the srcu_node tree. 220 */ 221 level = rcu_num_lvls - 1; 222 snp_first = ssp->srcu_sup->level[level]; 223 for_each_possible_cpu(cpu) { 224 sdp = per_cpu_ptr(ssp->sda, cpu); 225 sdp->mynode = &snp_first[cpu / levelspread[level]]; 226 for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) { 227 if (snp->grplo < 0) 228 snp->grplo = cpu; 229 snp->grphi = cpu; 230 } 231 sdp->grpmask = 1UL << (cpu - sdp->mynode->grplo); 232 } 233 smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_WAIT_BARRIER); 234 return true; 235 } 236 237 /* 238 * Initialize non-compile-time initialized fields, including the 239 * associated srcu_node and srcu_data structures. The is_static parameter 240 * tells us that ->sda has already been wired up to srcu_data. 241 * The is_atomic parameter tells us that there is no reason to 242 * ever transition to big. 243 */ 244 static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static, bool is_atomic) 245 { 246 if (!is_static) 247 ssp->srcu_sup = kzalloc_obj(*ssp->srcu_sup); 248 if (!ssp->srcu_sup) 249 return -ENOMEM; 250 if (!is_static) 251 raw_spin_lock_init(&ACCESS_PRIVATE(ssp->srcu_sup, lock)); 252 ssp->srcu_sup->srcu_size_state = SRCU_SIZE_SMALL; 253 ssp->srcu_sup->node = NULL; 254 mutex_init(&ssp->srcu_sup->srcu_cb_mutex); 255 mutex_init(&ssp->srcu_sup->srcu_gp_mutex); 256 atomic_set(&ssp->srcu_sup->srcu_atomic_gp_flag, 0); 257 ssp->srcu_sup->srcu_gp_seq = SRCU_GP_SEQ_INITIAL_VAL; 258 ssp->srcu_sup->srcu_barrier_seq = 0; 259 mutex_init(&ssp->srcu_sup->srcu_barrier_mutex); 260 atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 0); 261 INIT_DELAYED_WORK(&ssp->srcu_sup->work, process_srcu); 262 /* 263 * trace events started on the command line require SRCU before 264 * the irq_work kthread starts. Since all it does is a simple 265 * wakeup, having it as a hard irq, even on PREEMPT_RT is fine. 266 */ 267 ssp->srcu_sup->irq_work = IRQ_WORK_INIT_HARD(srcu_irq_work); 268 ssp->srcu_sup->sda_is_static = is_static; 269 if (!is_static) { 270 ssp->sda = alloc_percpu(struct srcu_data); 271 ssp->srcu_ctrp = &ssp->sda->srcu_ctrs[0]; 272 } 273 if (!ssp->sda) 274 goto err_free_sup; 275 init_srcu_struct_data(ssp); 276 ssp->srcu_sup->srcu_gp_seq_needed_exp = SRCU_GP_SEQ_INITIAL_VAL; 277 ssp->srcu_sup->srcu_last_gp_end = ktime_get_mono_fast_ns(); 278 if (!is_atomic && 279 READ_ONCE(ssp->srcu_sup->srcu_size_state) == SRCU_SIZE_SMALL && SRCU_SIZING_IS_INIT()) { 280 if (!preemptible()) 281 WRITE_ONCE(ssp->srcu_sup->srcu_size_state, SRCU_SIZE_ALLOC); 282 else if (init_srcu_struct_nodes(ssp, GFP_KERNEL)) 283 WRITE_ONCE(ssp->srcu_sup->srcu_size_state, SRCU_SIZE_BIG); 284 else 285 goto err_free_sda; 286 } 287 ssp->srcu_sup->srcu_ssp = ssp; 288 smp_store_release(&ssp->srcu_sup->srcu_gp_seq_needed, 289 SRCU_GP_SEQ_INITIAL_VAL); /* Init done. */ 290 return 0; 291 292 err_free_sda: 293 if (!is_static) { 294 free_percpu(ssp->sda); 295 ssp->sda = NULL; 296 } 297 err_free_sup: 298 if (!is_static) { 299 kfree(ssp->srcu_sup); 300 ssp->srcu_sup = NULL; 301 } 302 return -ENOMEM; 303 } 304 305 #ifdef CONFIG_DEBUG_LOCK_ALLOC 306 307 static int 308 __init_srcu_struct_common(struct srcu_struct *ssp, const char *name, 309 struct lock_class_key *key, bool is_atomic) 310 { 311 /* Don't re-initialize a lock while it is held. */ 312 debug_check_no_locks_freed((void *)ssp, sizeof(*ssp)); 313 lockdep_init_map(&ssp->dep_map, name, key, 0); 314 return init_srcu_struct_fields(ssp, false, is_atomic); 315 } 316 317 int init_srcu_struct_lockdep(struct srcu_struct *ssp, const char *name, 318 struct lock_class_key *key) 319 { 320 ssp->srcu_reader_flavor = 0; 321 return __init_srcu_struct_common(ssp, name, key, false); 322 } 323 EXPORT_SYMBOL_GPL(init_srcu_struct_lockdep); 324 325 int __init_srcu_struct_fast(struct srcu_struct *ssp, const char *name, struct lock_class_key *key) 326 { 327 ssp->srcu_reader_flavor = SRCU_READ_FLAVOR_FAST; 328 return __init_srcu_struct_common(ssp, name, key, false); 329 } 330 EXPORT_SYMBOL_GPL(__init_srcu_struct_fast); 331 332 int __init_srcu_struct_fast_updown(struct srcu_struct *ssp, const char *name, 333 struct lock_class_key *key) 334 { 335 ssp->srcu_reader_flavor = SRCU_READ_FLAVOR_FAST_UPDOWN; 336 return __init_srcu_struct_common(ssp, name, key, false); 337 } 338 EXPORT_SYMBOL_GPL(__init_srcu_struct_fast_updown); 339 340 int __init_srcu_struct_atomic(struct srcu_struct *ssp, const char *name, struct lock_class_key *key) 341 { 342 ssp->srcu_reader_flavor = SRCU_READ_FLAVOR_ATOMIC; 343 return __init_srcu_struct_common(ssp, name, key, true); 344 } 345 EXPORT_SYMBOL_GPL(__init_srcu_struct_atomic); 346 347 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 348 349 /** 350 * init_srcu_struct_generic - initialize a sleep-RCU structure 351 * @ssp: structure to initialize. 352 * 353 * Use this in place of DEFINE_SRCU() and DEFINE_STATIC_SRCU() 354 * for non-static srcu_struct structures that are to be passed to 355 * srcu_read_lock(), srcu_read_lock_nmisafe(), and friends. It is necessary 356 * to invoke this on a given srcu_struct before passing that srcu_struct 357 * to any other function. Each srcu_struct represents a separate domain 358 * of SRCU protection. 359 */ 360 int init_srcu_struct_generic(struct srcu_struct *ssp) 361 { 362 ssp->srcu_reader_flavor = 0; 363 return init_srcu_struct_fields(ssp, false, false); 364 } 365 EXPORT_SYMBOL_GPL(init_srcu_struct_generic); 366 367 /** 368 * init_srcu_struct_fast - initialize a fast-reader sleep-RCU structure 369 * @ssp: structure to initialize. 370 * 371 * Use this in place of DEFINE_SRCU_FAST() and DEFINE_STATIC_SRCU_FAST() 372 * for non-static srcu_struct structures that are to be passed to 373 * srcu_read_lock_fast() and friends. It is necessary to invoke this on a 374 * given srcu_struct before passing that srcu_struct to any other function. 375 * Each srcu_struct represents a separate domain of SRCU protection. 376 */ 377 int init_srcu_struct_fast(struct srcu_struct *ssp) 378 { 379 ssp->srcu_reader_flavor = SRCU_READ_FLAVOR_FAST; 380 return init_srcu_struct_fields(ssp, false, false); 381 } 382 EXPORT_SYMBOL_GPL(init_srcu_struct_fast); 383 384 /** 385 * init_srcu_struct_fast_updown - initialize a fast-reader up/down sleep-RCU structure 386 * @ssp: structure to initialize. 387 * 388 * Use this function in place of DEFINE_SRCU_FAST_UPDOWN() and 389 * DEFINE_STATIC_SRCU_FAST_UPDOWN() for non-static srcu_struct 390 * structures that are to be passed to srcu_read_lock_fast_updown(), 391 * srcu_down_read_fast(), and friends. It is necessary to invoke this on a 392 * given srcu_struct before passing that srcu_struct to any other function. 393 * Each srcu_struct represents a separate domain of SRCU protection. 394 */ 395 int init_srcu_struct_fast_updown(struct srcu_struct *ssp) 396 { 397 ssp->srcu_reader_flavor = SRCU_READ_FLAVOR_FAST_UPDOWN; 398 return init_srcu_struct_fields(ssp, false, false); 399 } 400 EXPORT_SYMBOL_GPL(init_srcu_struct_fast_updown); 401 402 /** 403 * init_srcu_struct_atomic - initialize an atomic sleep-RCU structure 404 * @ssp: structure to initialize. 405 * 406 * Use this in place of DEFINE_SRCU_ATOMIC() and DEFINE_STATIC_SRCU_ATOMIC() 407 * for non-static srcu_struct structures that are to be passed to 408 * srcu_read_lock_atomic() and friends. It is necessary to invoke this on a 409 * given srcu_struct before passing that srcu_struct to any other function. 410 * Each srcu_struct represents a separate domain of SRCU protection. 411 * 412 * And yes, we really are defining a sleepable RCU implementation that 413 * cannot sleep. Strange universe we live in, isn't it? 414 */ 415 int init_srcu_struct_atomic(struct srcu_struct *ssp) 416 { 417 ssp->srcu_reader_flavor = SRCU_READ_FLAVOR_ATOMIC; 418 return init_srcu_struct_fields(ssp, false, true); 419 } 420 EXPORT_SYMBOL_GPL(init_srcu_struct_atomic); 421 422 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 423 424 /* 425 * Initiate a transition to SRCU_SIZE_BIG with lock held. 426 */ 427 static void __srcu_transition_to_big(struct srcu_struct *ssp) 428 { 429 lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock)); 430 smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_ALLOC); 431 } 432 433 /* 434 * Initiate an idempotent transition to SRCU_SIZE_BIG. 435 */ 436 static void srcu_transition_to_big(struct srcu_struct *ssp) 437 { 438 unsigned long flags; 439 440 /* Double-checked locking on ->srcu_size-state. */ 441 if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) != SRCU_SIZE_SMALL) 442 return; 443 raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags); 444 if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) != SRCU_SIZE_SMALL) { 445 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags); 446 return; 447 } 448 __srcu_transition_to_big(ssp); 449 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags); 450 } 451 452 /* 453 * Check to see if the just-encountered contention event justifies 454 * a transition to SRCU_SIZE_BIG. 455 */ 456 static void raw_spin_lock_irqsave_check_contention(struct srcu_struct *ssp) 457 { 458 unsigned long j; 459 460 if (!SRCU_SIZING_IS_CONTEND() || ssp->srcu_sup->srcu_size_state) 461 return; 462 j = jiffies; 463 if (ssp->srcu_sup->srcu_size_jiffies != j) { 464 ssp->srcu_sup->srcu_size_jiffies = j; 465 ssp->srcu_sup->srcu_n_lock_retries = 0; 466 } 467 if (++ssp->srcu_sup->srcu_n_lock_retries <= small_contention_lim) 468 return; 469 __srcu_transition_to_big(ssp); 470 } 471 472 /* 473 * Acquire the specified srcu_data structure's ->lock, but check for 474 * excessive contention, which results in initiation of a transition 475 * to SRCU_SIZE_BIG. But only if the srcutree.convert_to_big module 476 * parameter permits this. 477 */ 478 static void raw_spin_lock_irqsave_sdp_contention(struct srcu_data *sdp, unsigned long *flags) 479 { 480 struct srcu_struct *ssp = sdp->ssp; 481 482 if (raw_spin_trylock_irqsave_rcu_node(sdp, *flags)) 483 return; 484 raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, *flags); 485 raw_spin_lock_irqsave_check_contention(ssp); 486 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, *flags); 487 raw_spin_lock_irqsave_rcu_node(sdp, *flags); 488 } 489 490 /* 491 * Acquire the specified srcu_struct structure's ->lock, but check for 492 * excessive contention, which results in initiation of a transition 493 * to SRCU_SIZE_BIG. But only if the srcutree.convert_to_big module 494 * parameter permits this. 495 */ 496 static void raw_spin_lock_irqsave_ssp_contention(struct srcu_struct *ssp, unsigned long *flags) 497 { 498 if (raw_spin_trylock_irqsave_rcu_node(ssp->srcu_sup, *flags)) 499 return; 500 raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, *flags); 501 raw_spin_lock_irqsave_check_contention(ssp); 502 } 503 504 /* 505 * First-use initialization of statically allocated srcu_struct 506 * structure. Wiring up the combining tree is more than can be 507 * done with compile-time initialization, so this check is added 508 * to each update-side SRCU primitive. Use ssp->lock, which -is- 509 * compile-time initialized, to resolve races involving multiple 510 * CPUs trying to garner first-use privileges. The is_atomic 511 * parameter tells us that there will never be a reason to 512 * transition to big. 513 */ 514 static void check_init_srcu_struct(struct srcu_struct *ssp, bool is_atomic) 515 { 516 unsigned long flags; 517 518 /* The smp_load_acquire() pairs with the smp_store_release(). */ 519 if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed))) /*^^^*/ 520 return; /* Already initialized. */ 521 raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags); 522 if (!rcu_seq_state(ssp->srcu_sup->srcu_gp_seq_needed)) { 523 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags); 524 return; 525 } 526 init_srcu_struct_fields(ssp, true, is_atomic); 527 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags); 528 } 529 530 /* 531 * Is the current or any upcoming grace period to be expedited? 532 */ 533 static bool srcu_gp_is_expedited(struct srcu_struct *ssp) 534 { 535 struct srcu_usage *sup = ssp->srcu_sup; 536 537 return ULONG_CMP_LT(READ_ONCE(sup->srcu_gp_seq), READ_ONCE(sup->srcu_gp_seq_needed_exp)); 538 } 539 540 /* 541 * Computes approximate total of the readers' ->srcu_ctrs[].srcu_locks 542 * values for the rank of per-CPU counters specified by idx, and returns 543 * true if the caller did the proper barrier (gp), and if the count of 544 * the locks matches that of the unlocks passed in. 545 */ 546 static bool srcu_readers_lock_idx(struct srcu_struct *ssp, int idx, bool gp, unsigned long unlocks) 547 { 548 int cpu; 549 unsigned long mask = 0; 550 unsigned long sum = 0; 551 552 for_each_possible_cpu(cpu) { 553 struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu); 554 555 sum += atomic_long_read(&sdp->srcu_ctrs[idx].srcu_locks); 556 if (IS_ENABLED(CONFIG_PROVE_RCU)) 557 mask = mask | READ_ONCE(sdp->srcu_reader_flavor); 558 } 559 WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && (mask & (mask - 1)), 560 "Mixed reader flavors for srcu_struct at %ps.\n", ssp); 561 if (mask & SRCU_READ_FLAVOR_SLOWGP && !gp) 562 return false; 563 return sum == unlocks; 564 } 565 566 /* 567 * Returns approximate total of the readers' ->srcu_ctrs[].srcu_unlocks 568 * values for the rank of per-CPU counters specified by idx. 569 */ 570 static unsigned long srcu_readers_unlock_idx(struct srcu_struct *ssp, int idx, unsigned long *rdm) 571 { 572 int cpu; 573 unsigned long mask = ssp->srcu_reader_flavor; 574 unsigned long sum = 0; 575 576 for_each_possible_cpu(cpu) { 577 struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu); 578 579 sum += atomic_long_read(&sdp->srcu_ctrs[idx].srcu_unlocks); 580 mask = mask | READ_ONCE(sdp->srcu_reader_flavor); 581 } 582 WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && (mask & (mask - 1)), 583 "Mixed reader flavors for srcu_struct at %ps.\n", ssp); 584 *rdm = mask; 585 return sum; 586 } 587 588 /* 589 * Return true if the number of pre-existing readers is determined to 590 * be zero. 591 */ 592 static bool srcu_readers_active_idx_check(struct srcu_struct *ssp, int idx) 593 { 594 bool did_gp; 595 unsigned long rdm; 596 unsigned long unlocks; 597 598 unlocks = srcu_readers_unlock_idx(ssp, idx, &rdm); 599 did_gp = !!(rdm & SRCU_READ_FLAVOR_SLOWGP); 600 601 /* 602 * Make sure that a lock is always counted if the corresponding 603 * unlock is counted. Needs to be a smp_mb() as the read side may 604 * contain a read from a variable that is written to before the 605 * synchronize_srcu() in the write side. In this case smp_mb()s 606 * A and B (or X and Y) act like the store buffering pattern. 607 * 608 * This smp_mb() also pairs with smp_mb() C (or, in the case of X, 609 * Z) to prevent accesses after the synchronize_srcu() from being 610 * executed before the grace period ends. 611 */ 612 if (!did_gp) 613 smp_mb(); /* A */ 614 else if (srcu_gp_is_expedited(ssp)) 615 synchronize_rcu_expedited(); /* X */ 616 else 617 synchronize_rcu(); /* X */ 618 619 /* 620 * If the locks are the same as the unlocks, then there must have 621 * been no readers on this index at some point in this function. 622 * But there might be more readers, as a task might have read 623 * the current ->srcu_ctrp but not yet have incremented its CPU's 624 * ->srcu_ctrs[idx].srcu_locks counter. In fact, it is possible 625 * that most of the tasks have been preempted between fetching 626 * ->srcu_ctrp and incrementing ->srcu_ctrs[idx].srcu_locks. And 627 * there could be almost (ULONG_MAX / sizeof(struct task_struct)) 628 * tasks in a system whose address space was fully populated 629 * with memory. Call this quantity Nt. 630 * 631 * So suppose that the updater is preempted at this 632 * point in the code for a long time. That now-preempted 633 * updater has already flipped ->srcu_ctrp (possibly during 634 * the preceding grace period), done an smp_mb() (again, 635 * possibly during the preceding grace period), and summed up 636 * the ->srcu_ctrs[idx].srcu_unlocks counters. How many times 637 * can a given one of the aforementioned Nt tasks increment the 638 * old ->srcu_ctrp value's ->srcu_ctrs[idx].srcu_locks counter, 639 * in the absence of nesting? 640 * 641 * It can clearly do so once, given that it has already fetched 642 * the old value of ->srcu_ctrp and is just about to use that 643 * value to index its increment of ->srcu_ctrs[idx].srcu_locks. 644 * But as soon as it leaves that SRCU read-side critical section, 645 * it will increment ->srcu_ctrs[idx].srcu_unlocks, which must 646 * follow the updater's above read from that same value. Thus, 647 as soon the reading task does an smp_mb() and a later fetch from 648 * ->srcu_ctrp, that task will be guaranteed to get the new index. 649 * Except that the increment of ->srcu_ctrs[idx].srcu_unlocks 650 * in __srcu_read_unlock() is after the smp_mb(), and the fetch 651 * from ->srcu_ctrp in __srcu_read_lock() is before the smp_mb(). 652 * Thus, that task might not see the new value of ->srcu_ctrp until 653 * the -second- __srcu_read_lock(), which in turn means that this 654 * task might well increment ->srcu_ctrs[idx].srcu_locks for the 655 * old value of ->srcu_ctrp twice, not just once. 656 * 657 * However, it is important to note that a given smp_mb() takes 658 * effect not just for the task executing it, but also for any 659 * later task running on that same CPU. 660 * 661 * That is, there can be almost Nt + Nc further increments 662 * of ->srcu_ctrs[idx].srcu_locks for the old index, where Nc 663 * is the number of CPUs. But this is OK because the size of 664 * the task_struct structure limits the value of Nt and current 665 * systems limit Nc to a few thousand. 666 * 667 * OK, but what about nesting? This does impose a limit on 668 * nesting of half of the size of the task_struct structure 669 * (measured in bytes), which should be sufficient. A late 2022 670 * TREE01 rcutorture run reported this size to be no less than 671 * 9408 bytes, allowing up to 4704 levels of nesting, which is 672 * comfortably beyond excessive. Especially on 64-bit systems, 673 * which are unlikely to be configured with an address space fully 674 * populated with memory, at least not anytime soon. 675 */ 676 return srcu_readers_lock_idx(ssp, idx, did_gp, unlocks); 677 } 678 679 /* 680 * We use an adaptive strategy for synchronize_srcu() and especially for 681 * synchronize_srcu_expedited(). We spin for a fixed time period 682 * (defined below, boot time configurable) to allow SRCU readers to exit 683 * their read-side critical sections. If there are still some readers 684 * after one jiffy, we repeatedly block for one jiffy time periods. 685 * The blocking time is increased as the grace-period age increases, 686 * with max blocking time capped at 10 jiffies. 687 */ 688 #define SRCU_DEFAULT_RETRY_CHECK_DELAY 5 689 690 static ulong srcu_retry_check_delay = SRCU_DEFAULT_RETRY_CHECK_DELAY; 691 module_param(srcu_retry_check_delay, ulong, 0444); 692 693 #define SRCU_INTERVAL 1 // Base delay if no expedited GPs pending. 694 #define SRCU_MAX_INTERVAL 10 // Maximum incremental delay from slow readers. 695 696 #define SRCU_DEFAULT_MAX_NODELAY_PHASE_LO 3UL // Lowmark on default per-GP-phase 697 // no-delay instances. 698 #define SRCU_DEFAULT_MAX_NODELAY_PHASE_HI 1000UL // Highmark on default per-GP-phase 699 // no-delay instances. 700 701 #define SRCU_UL_CLAMP_LO(val, low) ((val) > (low) ? (val) : (low)) 702 #define SRCU_UL_CLAMP_HI(val, high) ((val) < (high) ? (val) : (high)) 703 #define SRCU_UL_CLAMP(val, low, high) SRCU_UL_CLAMP_HI(SRCU_UL_CLAMP_LO((val), (low)), (high)) 704 // per-GP-phase no-delay instances adjusted to allow non-sleeping poll up to 705 // one jiffies time duration. Mult by 2 is done to factor in the srcu_get_delay() 706 // called from process_srcu(). 707 #define SRCU_DEFAULT_MAX_NODELAY_PHASE_ADJUSTED \ 708 (2UL * USEC_PER_SEC / HZ / SRCU_DEFAULT_RETRY_CHECK_DELAY) 709 710 // Maximum per-GP-phase consecutive no-delay instances. 711 #define SRCU_DEFAULT_MAX_NODELAY_PHASE \ 712 SRCU_UL_CLAMP(SRCU_DEFAULT_MAX_NODELAY_PHASE_ADJUSTED, \ 713 SRCU_DEFAULT_MAX_NODELAY_PHASE_LO, \ 714 SRCU_DEFAULT_MAX_NODELAY_PHASE_HI) 715 716 static ulong srcu_max_nodelay_phase = SRCU_DEFAULT_MAX_NODELAY_PHASE; 717 module_param(srcu_max_nodelay_phase, ulong, 0444); 718 719 // Maximum consecutive no-delay instances. 720 #define SRCU_DEFAULT_MAX_NODELAY (SRCU_DEFAULT_MAX_NODELAY_PHASE > 100 ? \ 721 SRCU_DEFAULT_MAX_NODELAY_PHASE : 100) 722 723 static ulong srcu_max_nodelay = SRCU_DEFAULT_MAX_NODELAY; 724 module_param(srcu_max_nodelay, ulong, 0444); 725 726 /* 727 * Return grace-period delay, zero if there are expedited grace 728 * periods pending, SRCU_INTERVAL otherwise. 729 */ 730 static unsigned long srcu_get_delay(struct srcu_struct *ssp) 731 { 732 unsigned long gpstart; 733 unsigned long j; 734 unsigned long jbase = SRCU_INTERVAL; 735 struct srcu_usage *sup = ssp->srcu_sup; 736 737 lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock)); 738 if (srcu_gp_is_expedited(ssp)) 739 jbase = 0; 740 if (rcu_seq_state(READ_ONCE(sup->srcu_gp_seq))) { 741 j = jiffies - 1; 742 gpstart = READ_ONCE(sup->srcu_gp_start); 743 if (time_after(j, gpstart)) 744 jbase += j - gpstart; 745 if (!jbase) { 746 ASSERT_EXCLUSIVE_WRITER(sup->srcu_n_exp_nodelay); 747 WRITE_ONCE(sup->srcu_n_exp_nodelay, READ_ONCE(sup->srcu_n_exp_nodelay) + 1); 748 if (READ_ONCE(sup->srcu_n_exp_nodelay) > srcu_max_nodelay_phase) 749 jbase = 1; 750 } 751 } 752 return jbase > SRCU_MAX_INTERVAL ? SRCU_MAX_INTERVAL : jbase; 753 } 754 755 /** 756 * cleanup_srcu_struct - deconstruct a sleep-RCU structure 757 * @ssp: structure to clean up. 758 * 759 * Must invoke this after you are finished using a given srcu_struct that 760 * was initialized via init_srcu_struct(), else you leak memory. 761 */ 762 void cleanup_srcu_struct(struct srcu_struct *ssp) 763 { 764 int cpu; 765 unsigned long delay; 766 struct srcu_usage *sup = ssp->srcu_sup; 767 768 /* 769 * Drain before the early returns below: they leak the srcu_struct, but 770 * srcu_module_going() frees ->sda regardless, and a staged srcu_data 771 * left chained on a per-CPU list would then dangle. Draining first also 772 * has to precede the ->irq_work sync, since re-issuing a callback can 773 * start a grace period and re-queue ->irq_work, which schedules ->work. 774 */ 775 srcu_defer_flush(); 776 raw_spin_lock_irq_rcu_node(ssp->srcu_sup); 777 delay = srcu_get_delay(ssp); 778 raw_spin_unlock_irq_rcu_node(ssp->srcu_sup); 779 if (WARN_ON(!delay)) 780 return; /* Just leak it! */ 781 if (WARN_ON(srcu_readers_active(ssp))) 782 return; /* Just leak it! */ 783 irq_work_sync(&sup->irq_work); 784 flush_delayed_work(&sup->work); 785 for_each_possible_cpu(cpu) { 786 struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu); 787 788 // Call srcu_barrier() before this cleanup_srcu_struct() 789 // to avoid triggering this WARN_ON(). 790 if (WARN_ON(timer_delete_sync(&sdp->delay_work) && 791 !rcu_segcblist_empty(&sdp->srcu_cblist)) && 792 rcu_cpu_beenfullyonline(sdp->cpu)) 793 queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work); 794 flush_work(&sdp->work); 795 if (WARN_ON(rcu_segcblist_n_cbs(&sdp->srcu_cblist))) 796 return; /* Forgot srcu_barrier(), so just leak it! */ 797 } 798 if (WARN_ON(rcu_seq_state(READ_ONCE(sup->srcu_gp_seq)) != SRCU_STATE_IDLE) || 799 WARN_ON(rcu_seq_current(&sup->srcu_gp_seq) != sup->srcu_gp_seq_needed) || 800 WARN_ON(srcu_readers_active(ssp))) { 801 pr_info("%s: Active srcu_struct %p read state: %d gp state: %lu/%lu\n", 802 __func__, ssp, rcu_seq_state(READ_ONCE(sup->srcu_gp_seq)), 803 rcu_seq_current(&sup->srcu_gp_seq), sup->srcu_gp_seq_needed); 804 return; // Caller forgot to stop doing call_srcu()? 805 // Or caller invoked start_poll_synchronize_srcu() 806 // and then cleanup_srcu_struct() before that grace 807 // period ended? 808 } 809 kfree(sup->node); 810 sup->node = NULL; 811 sup->srcu_size_state = SRCU_SIZE_SMALL; 812 if (!sup->sda_is_static) { 813 free_percpu(ssp->sda); 814 ssp->sda = NULL; 815 kfree(sup); 816 ssp->srcu_sup = NULL; 817 } 818 } 819 EXPORT_SYMBOL_GPL(cleanup_srcu_struct); 820 821 /* 822 * Check for consistent reader flavor. 823 */ 824 void __srcu_check_read_flavor(struct srcu_struct *ssp, int read_flavor) 825 { 826 int old_read_flavor; 827 struct srcu_data *sdp; 828 829 /* NMI-unsafe use in NMI is a bad sign, as is multi-bit read_flavor values. */ 830 WARN_ON_ONCE(read_flavor != SRCU_READ_FLAVOR_NMI && 831 read_flavor != SRCU_READ_FLAVOR_FAST && in_nmi()); 832 WARN_ON_ONCE(read_flavor & (read_flavor - 1)); 833 834 sdp = raw_cpu_ptr(ssp->sda); 835 old_read_flavor = READ_ONCE(sdp->srcu_reader_flavor); 836 WARN_ON_ONCE(ssp->srcu_reader_flavor && read_flavor != ssp->srcu_reader_flavor); 837 WARN_ON_ONCE(old_read_flavor && ssp->srcu_reader_flavor && 838 old_read_flavor != ssp->srcu_reader_flavor); 839 WARN_ON_ONCE(!!(read_flavor & SRCU_READ_FLAVOR_PREDEF) && 840 read_flavor != ssp->srcu_reader_flavor); 841 WARN_ON_ONCE(!!(ssp->srcu_reader_flavor & SRCU_READ_FLAVOR_PREDEF) && 842 read_flavor != ssp->srcu_reader_flavor); 843 if (!old_read_flavor) { 844 old_read_flavor = cmpxchg(&sdp->srcu_reader_flavor, 0, read_flavor); 845 if (!old_read_flavor) 846 return; 847 } 848 WARN_ONCE(old_read_flavor != read_flavor, "CPU %d old state %d new state %d\n", sdp->cpu, old_read_flavor, read_flavor); 849 } 850 EXPORT_SYMBOL_GPL(__srcu_check_read_flavor); 851 852 /* 853 * Counts the new reader in the appropriate per-CPU element of the 854 * srcu_struct. 855 * Returns a guaranteed non-negative index that must be passed to the 856 * matching __srcu_read_unlock(). 857 */ 858 int __srcu_read_lock(struct srcu_struct *ssp) 859 { 860 struct srcu_ctr __percpu *scp = READ_ONCE(ssp->srcu_ctrp); 861 862 this_cpu_inc(scp->srcu_locks.counter); 863 smp_mb(); /* B */ /* Avoid leaking the critical section. */ 864 return __srcu_ptr_to_ctr(ssp, scp); 865 } 866 EXPORT_SYMBOL_GPL(__srcu_read_lock); 867 868 /* 869 * Removes the count for the old reader from the appropriate per-CPU 870 * element of the srcu_struct. Note that this may well be a different 871 * CPU than that which was incremented by the corresponding srcu_read_lock(). 872 */ 873 void __srcu_read_unlock(struct srcu_struct *ssp, int idx) 874 { 875 smp_mb(); /* C */ /* Avoid leaking the critical section. */ 876 this_cpu_inc(__srcu_ctr_to_ptr(ssp, idx)->srcu_unlocks.counter); 877 } 878 EXPORT_SYMBOL_GPL(__srcu_read_unlock); 879 880 #ifdef CONFIG_NEED_SRCU_NMI_SAFE 881 882 /* 883 * Counts the new reader in the appropriate per-CPU element of the 884 * srcu_struct, but in an NMI-safe manner using RMW atomics. 885 * Returns an index that must be passed to the matching srcu_read_unlock(). 886 */ 887 int __srcu_read_lock_nmisafe(struct srcu_struct *ssp) 888 { 889 struct srcu_ctr __percpu *scpp = READ_ONCE(ssp->srcu_ctrp); 890 struct srcu_ctr *scp = raw_cpu_ptr(scpp); 891 892 atomic_long_inc(&scp->srcu_locks); 893 smp_mb__after_atomic(); /* B */ /* Avoid leaking the critical section. */ 894 return __srcu_ptr_to_ctr(ssp, scpp); 895 } 896 EXPORT_SYMBOL_GPL(__srcu_read_lock_nmisafe); 897 898 /* 899 * Removes the count for the old reader from the appropriate per-CPU 900 * element of the srcu_struct. Note that this may well be a different 901 * CPU than that which was incremented by the corresponding srcu_read_lock(). 902 */ 903 void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx) 904 { 905 smp_mb__before_atomic(); /* C */ /* Avoid leaking the critical section. */ 906 atomic_long_inc(&raw_cpu_ptr(__srcu_ctr_to_ptr(ssp, idx))->srcu_unlocks); 907 } 908 EXPORT_SYMBOL_GPL(__srcu_read_unlock_nmisafe); 909 910 #endif // CONFIG_NEED_SRCU_NMI_SAFE 911 912 /* 913 * Start an SRCU grace period. 914 */ 915 static void srcu_gp_start(struct srcu_struct *ssp) 916 { 917 int state; 918 919 lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock)); 920 WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed)); 921 WRITE_ONCE(ssp->srcu_sup->srcu_gp_start, jiffies); 922 WRITE_ONCE(ssp->srcu_sup->srcu_n_exp_nodelay, 0); 923 smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */ 924 rcu_seq_start(&ssp->srcu_sup->srcu_gp_seq); 925 state = rcu_seq_state(ssp->srcu_sup->srcu_gp_seq); 926 WARN_ON_ONCE(state != SRCU_STATE_SCAN1); 927 } 928 929 930 static void srcu_delay_timer(struct timer_list *t) 931 { 932 struct srcu_data *sdp = container_of(t, struct srcu_data, delay_work); 933 934 queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work); 935 } 936 937 static void srcu_queue_delayed_work_on(struct srcu_data *sdp, 938 unsigned long delay) 939 { 940 if (!delay) { 941 queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work); 942 return; 943 } 944 945 timer_reduce(&sdp->delay_work, jiffies + delay); 946 } 947 948 /* 949 * Schedule callback invocation for the specified srcu_data structure, 950 * if possible, on the corresponding CPU. 951 */ 952 static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay) 953 { 954 srcu_queue_delayed_work_on(sdp, delay); 955 } 956 957 /* 958 * Schedule callback invocation for all srcu_data structures associated 959 * with the specified srcu_node structure that have callbacks for the 960 * just-completed grace period, the one corresponding to idx. If possible, 961 * schedule this invocation on the corresponding CPUs. 962 */ 963 static void srcu_schedule_cbs_snp(struct srcu_struct *ssp, struct srcu_node *snp, 964 unsigned long mask, unsigned long delay) 965 { 966 int cpu; 967 968 for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) 969 if ((mask & (1UL << (cpu - snp->grplo))) && rcu_cpu_beenfullyonline(cpu)) 970 srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, cpu), delay); 971 } 972 973 /* 974 * Note the end of an SRCU grace period. Initiates callback invocation 975 * and starts a new grace period if needed. 976 * 977 * The ->srcu_cb_mutex acquisition does not protect any data, but 978 * instead prevents more than one grace period from starting while we 979 * are initiating callback invocation. This allows the ->srcu_have_cbs[] 980 * array to have a finite number of elements. 981 */ 982 static void srcu_gp_end(struct srcu_struct *ssp, bool is_atomic) 983 { 984 unsigned long cbdelay = 1; 985 bool cbs; 986 bool last_lvl; 987 int cpu; 988 unsigned long flags; 989 unsigned long gpseq; 990 int idx; 991 unsigned long mask; 992 struct srcu_data *sdp; 993 unsigned long sgsne; 994 struct srcu_node *snp; 995 int ss_state; 996 struct srcu_usage *sup = ssp->srcu_sup; 997 998 /* Prevent more than one additional grace period. */ 999 if (!is_atomic) 1000 mutex_lock(&sup->srcu_cb_mutex); 1001 1002 /* End the current grace period. */ 1003 raw_spin_lock_irqsave_rcu_node(sup, flags); 1004 idx = rcu_seq_state(sup->srcu_gp_seq); 1005 WARN_ON_ONCE(idx != SRCU_STATE_SCAN2); 1006 if (srcu_gp_is_expedited(ssp)) 1007 cbdelay = 0; 1008 1009 WRITE_ONCE(sup->srcu_last_gp_end, ktime_get_mono_fast_ns()); 1010 rcu_seq_end(&sup->srcu_gp_seq); 1011 gpseq = rcu_seq_current(&sup->srcu_gp_seq); 1012 if (ULONG_CMP_LT(sup->srcu_gp_seq_needed_exp, gpseq)) 1013 WRITE_ONCE(sup->srcu_gp_seq_needed_exp, gpseq); 1014 raw_spin_unlock_irqrestore_rcu_node(sup, flags); 1015 if (!is_atomic) 1016 mutex_unlock(&sup->srcu_gp_mutex); 1017 /* A new grace period can start at this point. But only one. */ 1018 1019 /* Initiate callback invocation as needed. */ 1020 ss_state = smp_load_acquire(&sup->srcu_size_state); 1021 if (!is_atomic && ss_state < SRCU_SIZE_WAIT_BARRIER) { 1022 srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, get_boot_cpu_id()), 1023 cbdelay); 1024 } else if (!is_atomic) { 1025 idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs); 1026 srcu_for_each_node_breadth_first(ssp, snp) { 1027 raw_spin_lock_irq_rcu_node(snp); 1028 cbs = false; 1029 last_lvl = snp >= sup->level[rcu_num_lvls - 1]; 1030 if (last_lvl) 1031 cbs = ss_state < SRCU_SIZE_BIG || snp->srcu_have_cbs[idx] == gpseq; 1032 snp->srcu_have_cbs[idx] = gpseq; 1033 rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1); 1034 sgsne = snp->srcu_gp_seq_needed_exp; 1035 if (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, gpseq)) 1036 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, gpseq); 1037 if (ss_state < SRCU_SIZE_BIG) 1038 mask = ~0; 1039 else 1040 mask = snp->srcu_data_have_cbs[idx]; 1041 snp->srcu_data_have_cbs[idx] = 0; 1042 raw_spin_unlock_irq_rcu_node(snp); 1043 if (cbs) 1044 srcu_schedule_cbs_snp(ssp, snp, mask, cbdelay); 1045 } 1046 } 1047 1048 /* Occasionally prevent srcu_data counter wrap. */ 1049 if (!(gpseq & counter_wrap_check)) 1050 for_each_possible_cpu(cpu) { 1051 sdp = per_cpu_ptr(ssp->sda, cpu); 1052 raw_spin_lock_irqsave_rcu_node(sdp, flags); 1053 if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed + 100)) 1054 sdp->srcu_gp_seq_needed = gpseq; 1055 if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed_exp + 100)) 1056 sdp->srcu_gp_seq_needed_exp = gpseq; 1057 raw_spin_unlock_irqrestore_rcu_node(sdp, flags); 1058 } 1059 1060 /* Callback initiation done, allow grace periods after next. */ 1061 if (!is_atomic) 1062 mutex_unlock(&sup->srcu_cb_mutex); 1063 1064 /* Start a new grace period if needed. */ 1065 raw_spin_lock_irqsave_rcu_node(sup, flags); 1066 gpseq = rcu_seq_current(&sup->srcu_gp_seq); 1067 if (!rcu_seq_state(gpseq) && 1068 ULONG_CMP_LT(gpseq, sup->srcu_gp_seq_needed)) { 1069 WARN_ON_ONCE(ssp->srcu_reader_flavor & SRCU_READ_FLAVOR_ATOMIC); 1070 srcu_gp_start(ssp); 1071 raw_spin_unlock_irqrestore_rcu_node(sup, flags); 1072 srcu_reschedule(ssp, 0); 1073 } else { 1074 raw_spin_unlock_irqrestore_rcu_node(sup, flags); 1075 } 1076 1077 /* Transition to big if needed, but never for atomic SRCU. */ 1078 if (ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC && ss_state != SRCU_SIZE_SMALL) { 1079 WARN_ON_ONCE(1); 1080 } else if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) { 1081 if (ss_state == SRCU_SIZE_ALLOC) 1082 init_srcu_struct_nodes(ssp, GFP_KERNEL); 1083 else 1084 smp_store_release(&sup->srcu_size_state, ss_state + 1); 1085 } 1086 } 1087 1088 /* 1089 * Funnel-locking scheme to scalably mediate many concurrent expedited 1090 * grace-period requests. This function is invoked for the first known 1091 * expedited request for a grace period that has already been requested, 1092 * but without expediting. To start a completely new grace period, 1093 * whether expedited or not, use srcu_funnel_gp_start() instead. 1094 */ 1095 static void srcu_funnel_exp_start(struct srcu_struct *ssp, struct srcu_node *snp, 1096 unsigned long s) 1097 { 1098 unsigned long flags; 1099 unsigned long sgsne; 1100 1101 if (snp) 1102 for (; snp != NULL; snp = snp->srcu_parent) { 1103 sgsne = READ_ONCE(snp->srcu_gp_seq_needed_exp); 1104 if (WARN_ON_ONCE(rcu_seq_done(&ssp->srcu_sup->srcu_gp_seq, s)) || 1105 (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s))) 1106 return; 1107 raw_spin_lock_irqsave_rcu_node(snp, flags); 1108 sgsne = snp->srcu_gp_seq_needed_exp; 1109 if (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)) { 1110 raw_spin_unlock_irqrestore_rcu_node(snp, flags); 1111 return; 1112 } 1113 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s); 1114 raw_spin_unlock_irqrestore_rcu_node(snp, flags); 1115 } 1116 raw_spin_lock_irqsave_ssp_contention(ssp, &flags); 1117 if (ULONG_CMP_LT(ssp->srcu_sup->srcu_gp_seq_needed_exp, s)) 1118 WRITE_ONCE(ssp->srcu_sup->srcu_gp_seq_needed_exp, s); 1119 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags); 1120 } 1121 1122 /* 1123 * Funnel-locking scheme to scalably mediate many concurrent grace-period 1124 * requests. The winner has to do the work of actually starting grace 1125 * period s. Losers must either ensure that their desired grace-period 1126 * number is recorded on at least their leaf srcu_node structure, or they 1127 * must take steps to invoke their own callbacks. 1128 * 1129 * Note that this function also does the work of srcu_funnel_exp_start(), 1130 * in some cases by directly invoking it. 1131 * 1132 * The srcu read lock should be hold around this function. And s is a seq snap 1133 * after holding that lock. 1134 */ 1135 static void srcu_funnel_gp_start(struct srcu_struct *ssp, struct srcu_data *sdp, 1136 unsigned long s, bool do_norm) 1137 { 1138 unsigned long flags; 1139 int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs); 1140 unsigned long sgsne; 1141 struct srcu_node *snp; 1142 struct srcu_node *snp_leaf; 1143 unsigned long snp_seq; 1144 struct srcu_usage *sup = ssp->srcu_sup; 1145 1146 /* Ensure that snp node tree is fully initialized before traversing it */ 1147 if (smp_load_acquire(&sup->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER) 1148 snp_leaf = NULL; 1149 else 1150 snp_leaf = sdp->mynode; 1151 1152 if (snp_leaf) 1153 /* Each pass through the loop does one level of the srcu_node tree. */ 1154 for (snp = snp_leaf; snp != NULL; snp = snp->srcu_parent) { 1155 if (WARN_ON_ONCE(rcu_seq_done(&sup->srcu_gp_seq, s)) && snp != snp_leaf) 1156 return; /* GP already done and CBs recorded. */ 1157 raw_spin_lock_irqsave_rcu_node(snp, flags); 1158 snp_seq = snp->srcu_have_cbs[idx]; 1159 if (!srcu_invl_snp_seq(snp_seq) && ULONG_CMP_GE(snp_seq, s)) { 1160 if (snp == snp_leaf && snp_seq == s) 1161 snp->srcu_data_have_cbs[idx] |= sdp->grpmask; 1162 raw_spin_unlock_irqrestore_rcu_node(snp, flags); 1163 if (snp == snp_leaf && snp_seq != s) { 1164 srcu_schedule_cbs_sdp(sdp, do_norm ? SRCU_INTERVAL : 0); 1165 return; 1166 } 1167 if (!do_norm) 1168 srcu_funnel_exp_start(ssp, snp, s); 1169 return; 1170 } 1171 snp->srcu_have_cbs[idx] = s; 1172 if (snp == snp_leaf) 1173 snp->srcu_data_have_cbs[idx] |= sdp->grpmask; 1174 sgsne = snp->srcu_gp_seq_needed_exp; 1175 if (!do_norm && (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, s))) 1176 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s); 1177 raw_spin_unlock_irqrestore_rcu_node(snp, flags); 1178 } 1179 1180 /* Top of tree, must ensure the grace period will be started. */ 1181 raw_spin_lock_irqsave_ssp_contention(ssp, &flags); 1182 if (ULONG_CMP_LT(sup->srcu_gp_seq_needed, s)) { 1183 /* 1184 * Record need for grace period s. Pair with load 1185 * acquire setting up for initialization. 1186 */ 1187 smp_store_release(&sup->srcu_gp_seq_needed, s); /*^^^*/ 1188 } 1189 if (!do_norm && ULONG_CMP_LT(sup->srcu_gp_seq_needed_exp, s)) 1190 WRITE_ONCE(sup->srcu_gp_seq_needed_exp, s); 1191 1192 /* If grace period not already in progress, start it. */ 1193 if (!WARN_ON_ONCE(rcu_seq_done(&sup->srcu_gp_seq, s)) && 1194 rcu_seq_state(sup->srcu_gp_seq) == SRCU_STATE_IDLE) { 1195 WARN_ON_ONCE(ssp->srcu_reader_flavor & SRCU_READ_FLAVOR_ATOMIC); 1196 srcu_gp_start(ssp); 1197 1198 // And how can that list_add() in the "else" clause 1199 // possibly be safe for concurrent execution? Well, 1200 // it isn't. And it does not have to be. After all, it 1201 // can only be executed during early boot when there is only 1202 // the one boot CPU running with interrupts still disabled. 1203 // 1204 // Use an irq_work here to avoid acquiring runqueue lock with 1205 // srcu rcu_node::lock held. BPF instrument could introduce the 1206 // opposite dependency, hence we need to break the possible 1207 // locking dependency here. 1208 if (likely(srcu_init_done)) 1209 irq_work_queue(&sup->irq_work); 1210 else if (list_empty(&sup->work.work.entry)) 1211 list_add(&sup->work.work.entry, &srcu_boot_list); 1212 } 1213 raw_spin_unlock_irqrestore_rcu_node(sup, flags); 1214 } 1215 1216 /* 1217 * Wait until all readers counted by array index idx complete, but 1218 * loop an additional time if there is an expedited grace period pending. 1219 * The caller must ensure that ->srcu_ctrp is not changed while checking. 1220 */ 1221 static bool try_check_zero(struct srcu_struct *ssp, int idx, int trycount) 1222 { 1223 unsigned long curdelay; 1224 unsigned long flags; 1225 1226 raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags); 1227 curdelay = !srcu_get_delay(ssp); 1228 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags); 1229 1230 for (;;) { 1231 if (srcu_readers_active_idx_check(ssp, idx)) 1232 return true; 1233 if ((--trycount + curdelay) <= 0) 1234 return false; 1235 udelay(srcu_retry_check_delay); 1236 } 1237 } 1238 1239 /* 1240 * Increment the ->srcu_ctrp counter so that future SRCU readers will 1241 * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows 1242 * us to wait for pre-existing readers in a starvation-free manner. 1243 */ 1244 static void srcu_flip(struct srcu_struct *ssp) 1245 { 1246 /* 1247 * Because the flip of ->srcu_ctrp is executed only if the 1248 * preceding call to srcu_readers_active_idx_check() found that 1249 * the ->srcu_ctrs[].srcu_unlocks and ->srcu_ctrs[].srcu_locks sums 1250 * matched and because that summing uses atomic_long_read(), 1251 * there is ordering due to a control dependency between that 1252 * summing and the WRITE_ONCE() in this call to srcu_flip(). 1253 * This ordering ensures that if this updater saw a given reader's 1254 * increment from __srcu_read_lock(), that reader was using a value 1255 * of ->srcu_ctrp from before the previous call to srcu_flip(), 1256 * which should be quite rare. This ordering thus helps forward 1257 * progress because the grace period could otherwise be delayed 1258 * by additional calls to __srcu_read_lock() using that old (soon 1259 * to be new) value of ->srcu_ctrp. 1260 * 1261 * This sum-equality check and ordering also ensures that if 1262 * a given call to __srcu_read_lock() uses the new value of 1263 * ->srcu_ctrp, this updater's earlier scans cannot have seen 1264 * that reader's increments, which is all to the good, because 1265 * this grace period need not wait on that reader. After all, 1266 * if those earlier scans had seen that reader, there would have 1267 * been a sum mismatch and this code would not be reached. 1268 * 1269 * This means that the following smp_mb() is redundant, but 1270 * it stays until either (1) Compilers learn about this sort of 1271 * control dependency or (2) Some production workload running on 1272 * a production system is unduly delayed by this slowpath smp_mb(). 1273 * Except for _lite() readers, where it is inoperative, which 1274 * means that it is a good thing that it is redundant. 1275 */ 1276 smp_mb(); /* E */ /* Pairs with B and C. */ 1277 1278 WRITE_ONCE(ssp->srcu_ctrp, 1279 &ssp->sda->srcu_ctrs[!(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0])]); 1280 1281 /* 1282 * Ensure that if the updater misses an __srcu_read_unlock() 1283 * increment, that task's __srcu_read_lock() following its next 1284 * __srcu_read_lock() or __srcu_read_unlock() will see the above 1285 * counter update. Note that both this memory barrier and the 1286 * one in srcu_readers_active_idx_check() provide the guarantee 1287 * for __srcu_read_lock(). 1288 * 1289 * Note that this is a performance optimization, in which we spend 1290 * an otherwise unnecessary smp_mb() in order to reduce the number 1291 * of full per-CPU-variable scans in srcu_readers_lock_idx() and 1292 * srcu_readers_unlock_idx(). But this performance optimization 1293 * is not so optimal for SRCU-fast, where we would be spending 1294 * not smp_mb(), but rather synchronize_rcu(). At the same time, 1295 * the overhead of the smp_mb() is in the noise, so there is no 1296 * point in omitting it in the SRCU-fast case. So the same code 1297 * is executed either way. 1298 */ 1299 smp_mb(); /* D */ /* Pairs with C. */ 1300 } 1301 1302 /* 1303 * If SRCU is likely idle, in other words, the next SRCU grace period 1304 * should be expedited, return true, otherwise return false. Except that 1305 * in the presence of _lite() readers, always return false. 1306 * 1307 * Note that it is OK for several current from-idle requests for a new 1308 * grace period from idle to specify expediting because they will all end 1309 * up requesting the same grace period anyhow. So no loss. 1310 * 1311 * Note also that if any CPU (including the current one) is still invoking 1312 * callbacks, this function will nevertheless say "idle". This is not 1313 * ideal, but the overhead of checking all CPUs' callback lists is even 1314 * less ideal, especially on large systems. Furthermore, the wakeup 1315 * can happen before the callback is fully removed, so we have no choice 1316 * but to accept this type of error. 1317 * 1318 * This function is also subject to counter-wrap errors, but let's face 1319 * it, if this function was preempted for enough time for the counters 1320 * to wrap, it really doesn't matter whether or not we expedite the grace 1321 * period. The extra overhead of a needlessly expedited grace period is 1322 * negligible when amortized over that time period, and the extra latency 1323 * of a needlessly non-expedited grace period is similarly negligible. 1324 */ 1325 static bool srcu_should_expedite(struct srcu_struct *ssp) 1326 { 1327 unsigned long curseq; 1328 unsigned long flags; 1329 struct srcu_data *sdp; 1330 unsigned long t; 1331 unsigned long tlast; 1332 1333 check_init_srcu_struct(ssp, false); 1334 /* If _lite() readers, don't do unsolicited expediting. */ 1335 if (this_cpu_read(ssp->sda->srcu_reader_flavor) & SRCU_READ_FLAVOR_SLOWGP) 1336 return false; 1337 /* If the local srcu_data structure has callbacks, not idle. */ 1338 sdp = raw_cpu_ptr(ssp->sda); 1339 raw_spin_lock_irqsave_rcu_node(sdp, flags); 1340 if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) { 1341 raw_spin_unlock_irqrestore_rcu_node(sdp, flags); 1342 return false; /* Callbacks already present, so not idle. */ 1343 } 1344 raw_spin_unlock_irqrestore_rcu_node(sdp, flags); 1345 1346 /* 1347 * No local callbacks, so probabilistically probe global state. 1348 * Exact information would require acquiring locks, which would 1349 * kill scalability, hence the probabilistic nature of the probe. 1350 */ 1351 1352 /* First, see if enough time has passed since the last GP. */ 1353 t = ktime_get_mono_fast_ns(); 1354 tlast = READ_ONCE(ssp->srcu_sup->srcu_last_gp_end); 1355 if (exp_holdoff == 0 || 1356 time_in_range_open(t, tlast, tlast + exp_holdoff)) 1357 return false; /* Too soon after last GP. */ 1358 1359 /* Next, check for probable idleness. */ 1360 curseq = rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq); 1361 smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */ 1362 if (ULONG_CMP_LT(curseq, READ_ONCE(ssp->srcu_sup->srcu_gp_seq_needed))) 1363 return false; /* Grace period in progress, so not idle. */ 1364 smp_mb(); /* Order ->srcu_gp_seq with prior access. */ 1365 if (curseq != rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq)) 1366 return false; /* GP # changed, so not idle. */ 1367 return true; /* With reasonable probability, idle! */ 1368 } 1369 1370 /* 1371 * SRCU callback function to leak a callback. 1372 */ 1373 static void srcu_leak_callback(struct rcu_head *rhp) 1374 { 1375 } 1376 1377 /* 1378 * Start an SRCU grace period, and also queue the callback if non-NULL. 1379 */ 1380 static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp, 1381 struct rcu_head *rhp, bool do_norm) 1382 { 1383 unsigned long flags; 1384 int idx; 1385 bool needexp = false; 1386 bool needgp = false; 1387 unsigned long s; 1388 struct srcu_data *sdp; 1389 struct srcu_node *sdp_mynode; 1390 int ss_state; 1391 1392 check_init_srcu_struct(ssp, false); 1393 /* 1394 * While starting a new grace period, make sure we are in an 1395 * SRCU read-side critical section so that the grace-period 1396 * sequence number cannot wrap around in the meantime. 1397 */ 1398 idx = __srcu_read_lock_nmisafe(ssp); 1399 ss_state = smp_load_acquire(&ssp->srcu_sup->srcu_size_state); 1400 // If !rcu_cpu_beenfullyonline(), interrupts are still disabled, 1401 // so no migration is possible in either direction from this CPU. 1402 if (ss_state < SRCU_SIZE_WAIT_CALL || !rcu_cpu_beenfullyonline(raw_smp_processor_id())) 1403 sdp = per_cpu_ptr(ssp->sda, get_boot_cpu_id()); 1404 else 1405 sdp = raw_cpu_ptr(ssp->sda); 1406 raw_spin_lock_irqsave_sdp_contention(sdp, &flags); 1407 if (rhp) 1408 rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp); 1409 /* 1410 * It's crucial to capture the snapshot 's' for acceleration before 1411 * reading the current gp_seq that is used for advancing. This is 1412 * essential because if the acceleration snapshot is taken after a 1413 * failed advancement attempt, there's a risk that a grace period may 1414 * conclude and a new one may start in the interim. If the snapshot is 1415 * captured after this sequence of events, the acceleration snapshot 's' 1416 * could be excessively advanced, leading to acceleration failure. 1417 * In such a scenario, an 'acceleration leak' can occur, where new 1418 * callbacks become indefinitely stuck in the RCU_NEXT_TAIL segment. 1419 * Also note that encountering advancing failures is a normal 1420 * occurrence when the grace period for RCU_WAIT_TAIL is in progress. 1421 * 1422 * To see this, consider the following events which occur if 1423 * rcu_seq_snap() were to be called after advance: 1424 * 1425 * 1) The RCU_WAIT_TAIL segment has callbacks (gp_num = X + 4) and the 1426 * RCU_NEXT_READY_TAIL also has callbacks (gp_num = X + 8). 1427 * 1428 * 2) The grace period for RCU_WAIT_TAIL is seen as started but not 1429 * completed so rcu_seq_current() returns X + SRCU_STATE_SCAN1. 1430 * 1431 * 3) This value is passed to srcu_segcblist_advance() which can't move 1432 * any segment forward and fails. 1433 * 1434 * 4) srcu_gp_start_if_needed() still proceeds with callback acceleration. 1435 * But then the call to rcu_seq_snap() observes the grace period for the 1436 * RCU_WAIT_TAIL segment as completed and the subsequent one for the 1437 * RCU_NEXT_READY_TAIL segment as started (ie: X + 4 + SRCU_STATE_SCAN1) 1438 * so it returns a snapshot of the next grace period, which is X + 12. 1439 * 1440 * 5) The value of X + 12 is passed to srcu_segcblist_accelerate() but the 1441 * freshly enqueued callback in RCU_NEXT_TAIL can't move to 1442 * RCU_NEXT_READY_TAIL which already has callbacks for a previous grace 1443 * period (gp_num = X + 8). So acceleration fails. 1444 */ 1445 s = rcu_seq_snap(&ssp->srcu_sup->srcu_gp_seq); 1446 if (rhp) { 1447 srcu_segcblist_advance(&sdp->srcu_cblist, 1448 rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq)); 1449 /* 1450 * Acceleration can never fail because the base current gp_seq 1451 * used for acceleration is <= the value of gp_seq used for 1452 * advancing. This means that RCU_NEXT_TAIL segment will 1453 * always be able to be emptied by the acceleration into the 1454 * RCU_NEXT_READY_TAIL or RCU_WAIT_TAIL segments. 1455 */ 1456 WARN_ON_ONCE(!srcu_segcblist_accelerate(&sdp->srcu_cblist, s)); 1457 } 1458 if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) { 1459 sdp->srcu_gp_seq_needed = s; 1460 needgp = true; 1461 } 1462 if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) { 1463 sdp->srcu_gp_seq_needed_exp = s; 1464 needexp = true; 1465 } 1466 raw_spin_unlock_irqrestore_rcu_node(sdp, flags); 1467 1468 /* Ensure that snp node tree is fully initialized before traversing it */ 1469 if (ss_state < SRCU_SIZE_WAIT_BARRIER) 1470 sdp_mynode = NULL; 1471 else 1472 sdp_mynode = sdp->mynode; 1473 1474 if (needgp) 1475 srcu_funnel_gp_start(ssp, sdp, s, do_norm); 1476 else if (needexp) 1477 srcu_funnel_exp_start(ssp, sdp_mynode, s); 1478 __srcu_read_unlock_nmisafe(ssp, idx); 1479 return s; 1480 } 1481 1482 /* 1483 * Enqueue an SRCU callback on the srcu_data structure associated with 1484 * the current CPU and the specified srcu_struct structure, initiating 1485 * grace-period processing if it is not already running. 1486 * 1487 * Note that all CPUs must agree that the grace period extended beyond 1488 * all pre-existing SRCU read-side critical section. On systems with 1489 * more than one CPU, this means that when "func()" is invoked, each CPU 1490 * is guaranteed to have executed a full memory barrier since the end of 1491 * its last corresponding SRCU read-side critical section whose beginning 1492 * preceded the call to call_srcu(). It also means that each CPU executing 1493 * an SRCU read-side critical section that continues beyond the start of 1494 * "func()" must have executed a memory barrier after the call_srcu() 1495 * but before the beginning of that SRCU read-side critical section. 1496 * Note that these guarantees include CPUs that are offline, idle, or 1497 * executing in user mode, as well as CPUs that are executing in the kernel. 1498 * 1499 * Furthermore, if CPU A invoked call_srcu() and CPU B invoked the 1500 * resulting SRCU callback function "func()", then both CPU A and CPU 1501 * B are guaranteed to execute a full memory barrier during the time 1502 * interval between the call to call_srcu() and the invocation of "func()". 1503 * This guarantee applies even if CPU A and CPU B are the same CPU (but 1504 * again only if the system has more than one CPU). 1505 * 1506 * Of course, these guarantees apply only for invocations of call_srcu(), 1507 * srcu_read_lock(), and srcu_read_unlock() that are all passed the same 1508 * srcu_struct structure. 1509 */ 1510 static void srcu_do_enqueue(struct srcu_struct *ssp, struct rcu_head *rhp, 1511 rcu_callback_t func, bool do_norm) 1512 { 1513 if (debug_rcu_head_queue(rhp)) { 1514 /* Probable double call_srcu(), so leak the callback. */ 1515 WRITE_ONCE(rhp->func, srcu_leak_callback); 1516 WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n"); 1517 return; 1518 } 1519 rhp->func = func; 1520 (void)srcu_gp_start_if_needed(ssp, rhp, do_norm); 1521 } 1522 1523 /* 1524 * The srcu_cblist and srcu_node tree are only accessed with interrupts 1525 * disabled, so defer when interrupts are already off rather than enqueue into 1526 * an operation that may be in flight on this CPU. 1527 */ 1528 static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp, 1529 rcu_callback_t func, bool do_norm) 1530 { 1531 if (WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC)) 1532 return; // Leak the callback rather than corrupt SRCU state. 1533 if (should_rcu_defer()) { 1534 struct srcu_defer *sndp = this_cpu_ptr(&srcu_defer); 1535 struct srcu_data *sdp; 1536 1537 /* 1538 * Instrumentation on the enqueue path can re-enter here from 1539 * inside the drain. Re-queuing would livelock it, so drop the 1540 * callback; an NMI cannot loop, so let it through. 1541 */ 1542 if (READ_ONCE(sndp->draining) && !in_nmi()) { 1543 WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU), 1544 "call_srcu() re-entered during callback drain; leaking callback\n"); 1545 return; 1546 } 1547 sdp = this_cpu_ptr(ssp->sda); 1548 rhp->func = func; 1549 if (llist_add((struct llist_node *)rhp, &sdp->defer_cbs)) { 1550 /* 1551 * Chain this srcu_data for the drain. ->ssp must be 1552 * published here: deferral skips 1553 * check_init_srcu_struct(), so on a never-initialized 1554 * static srcu_struct the srcu_data are still zeroed and 1555 * the drain would read a NULL ->ssp. 1556 */ 1557 sdp->ssp = ssp; 1558 if (llist_add(&sdp->defer_link, &sndp->list)) 1559 irq_work_queue(&sndp->iw); 1560 } 1561 return; 1562 } 1563 1564 /* 1565 * Only reachable from an NMI when deferral is off: before the scheduler 1566 * is up, or with CONFIG_RCU_DEFER=n. The enqueue can then race. 1567 */ 1568 WARN_ON_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && in_nmi()); 1569 1570 srcu_do_enqueue(ssp, rhp, func, do_norm); 1571 } 1572 1573 /* 1574 * Re-issue deferred callbacks straight to srcu_do_enqueue() so they cannot defer 1575 * again. ->lock serializes the drainers: the irq_work, srcu_defer_flush() and 1576 * srcu_offline_drain(). 1577 */ 1578 static void __srcu_defer_drain(struct srcu_defer *sndp) 1579 { 1580 struct llist_node *snode, *snext; 1581 unsigned long flags; 1582 1583 if (!IS_ENABLED(CONFIG_RCU_DEFER)) 1584 return; 1585 1586 raw_spin_lock_irqsave(&sndp->lock, flags); 1587 llist_for_each_safe(snode, snext, llist_del_all(&sndp->list)) { 1588 struct srcu_data *sdp = container_of(snode, struct srcu_data, defer_link); 1589 struct srcu_struct *ssp = sdp->ssp; 1590 struct llist_node *cnode, *cnext; 1591 1592 cnode = llist_del_all(&sdp->defer_cbs); 1593 llist_for_each_safe(cnode, cnext, cnode) { 1594 struct rcu_head *rhp = (struct rcu_head *)cnode; 1595 1596 /* Bounds a node self-linked by a double call_srcu(). */ 1597 rhp->next = NULL; 1598 srcu_do_enqueue(ssp, rhp, rhp->func, true); 1599 } 1600 } 1601 raw_spin_unlock_irqrestore(&sndp->lock, flags); 1602 } 1603 1604 /* 1605 * Only the irq_work drain can be re-fed by its own re-issue, so only it sets 1606 * ->draining. A direct drain re-issues onto this CPU, and anything staged 1607 * during it is picked up by that CPU's own irq_work. 1608 */ 1609 static void srcu_defer_drain(struct irq_work *iw) 1610 { 1611 struct srcu_defer *sndp = container_of(iw, struct srcu_defer, iw); 1612 1613 WRITE_ONCE(sndp->draining, true); 1614 __srcu_defer_drain(sndp); 1615 WRITE_ONCE(sndp->draining, false); 1616 } 1617 1618 /* 1619 * Drain @cpu's deferred call_srcu() callbacks once @cpu is dead. One pass 1620 * covers every srcu_struct; the re-issue lands on the current CPU. 1621 */ 1622 void srcu_offline_drain(int cpu) 1623 { 1624 __srcu_defer_drain(&per_cpu(srcu_defer, cpu)); 1625 } 1626 1627 /** 1628 * call_srcu() - Queue a callback for invocation after an SRCU grace period 1629 * @ssp: srcu_struct in queue the callback 1630 * @rhp: structure to be used for queueing the SRCU callback. 1631 * @func: function to be invoked after the SRCU grace period 1632 * 1633 * The callback function will be invoked some time after a full SRCU 1634 * grace period elapses, in other words after all pre-existing SRCU 1635 * read-side critical sections have completed. However, the callback 1636 * function might well execute concurrently with other SRCU read-side 1637 * critical sections that started after call_srcu() was invoked. SRCU 1638 * read-side critical sections are delimited by srcu_read_lock() and 1639 * srcu_read_unlock(), and may be nested. 1640 * 1641 * The callback will be invoked from process context, but with bh 1642 * disabled. The callback function must therefore be fast and must 1643 * not block. 1644 * 1645 * See the description of call_rcu() for more detailed information on 1646 * memory ordering guarantees. 1647 */ 1648 void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp, 1649 rcu_callback_t func) 1650 { 1651 __call_srcu(ssp, rhp, func, true); 1652 } 1653 EXPORT_SYMBOL_GPL(call_srcu); 1654 1655 /* 1656 * Helper function for synchronize_srcu() and synchronize_srcu_expedited(). 1657 */ 1658 static void __synchronize_srcu(struct srcu_struct *ssp, bool do_norm) 1659 { 1660 struct rcu_synchronize rcu; 1661 1662 srcu_lock_sync(&ssp->dep_map); 1663 1664 RCU_LOCKDEP_WARN(lockdep_is_held(ssp) || 1665 lock_is_held(&rcu_bh_lock_map) || 1666 lock_is_held(&rcu_lock_map) || 1667 lock_is_held(&rcu_sched_lock_map), 1668 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section"); 1669 1670 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE) 1671 return; 1672 if (WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC)) { 1673 // This works, and exposes a possible bug. 1674 synchronize_srcu_atomic(ssp); 1675 return; 1676 } 1677 might_sleep(); 1678 check_init_srcu_struct(ssp, false); 1679 init_completion(&rcu.completion); 1680 init_rcu_head_on_stack(&rcu.head); 1681 __call_srcu(ssp, &rcu.head, wakeme_after_rcu, do_norm); 1682 wait_for_completion(&rcu.completion); 1683 destroy_rcu_head_on_stack(&rcu.head); 1684 1685 /* 1686 * Make sure that later code is ordered after the SRCU grace 1687 * period. This pairs with the raw_spin_lock_irq_rcu_node() 1688 * in srcu_invoke_callbacks(). Unlike Tree RCU, this is needed 1689 * because the current CPU might have been totally uninvolved with 1690 * (and thus unordered against) that grace period. 1691 */ 1692 smp_mb(); 1693 } 1694 1695 /** 1696 * synchronize_srcu_expedited - Brute-force SRCU grace period 1697 * @ssp: srcu_struct with which to synchronize. 1698 * 1699 * Wait for an SRCU grace period to elapse, but be more aggressive about 1700 * spinning rather than blocking when waiting. 1701 * 1702 * Note that synchronize_srcu_expedited() has the same deadlock and 1703 * memory-ordering properties as does synchronize_srcu(). 1704 */ 1705 void synchronize_srcu_expedited(struct srcu_struct *ssp) 1706 { 1707 __synchronize_srcu(ssp, rcu_gp_is_normal()); 1708 } 1709 EXPORT_SYMBOL_GPL(synchronize_srcu_expedited); 1710 1711 /** 1712 * synchronize_srcu - wait for prior SRCU read-side critical-section completion 1713 * @ssp: srcu_struct with which to synchronize. 1714 * 1715 * Wait for the count to drain to zero of both indexes. To avoid the 1716 * possible starvation of synchronize_srcu(), it waits for the count of 1717 * the index=!(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0]) to drain to zero 1718 * at first, and then flip the ->srcu_ctrp and wait for the count of the 1719 * other index. 1720 * 1721 * Can block; must be called from process context. 1722 * 1723 * Note that it is illegal to call synchronize_srcu() from the corresponding 1724 * SRCU read-side critical section; doing so will result in deadlock. 1725 * However, it is perfectly legal to call synchronize_srcu() on one 1726 * srcu_struct from some other srcu_struct's read-side critical section, 1727 * as long as the resulting graph of srcu_structs is acyclic. 1728 * 1729 * There are memory-ordering constraints implied by synchronize_srcu(). 1730 * On systems with more than one CPU, when synchronize_srcu() returns, 1731 * each CPU is guaranteed to have executed a full memory barrier since 1732 * the end of its last corresponding SRCU read-side critical section 1733 * whose beginning preceded the call to synchronize_srcu(). In addition, 1734 * each CPU having an SRCU read-side critical section that extends beyond 1735 * the return from synchronize_srcu() is guaranteed to have executed a 1736 * full memory barrier after the beginning of synchronize_srcu() and before 1737 * the beginning of that SRCU read-side critical section. Note that these 1738 * guarantees include CPUs that are offline, idle, or executing in user mode, 1739 * as well as CPUs that are executing in the kernel. 1740 * 1741 * Furthermore, if CPU A invoked synchronize_srcu(), which returned 1742 * to its caller on CPU B, then both CPU A and CPU B are guaranteed 1743 * to have executed a full memory barrier during the execution of 1744 * synchronize_srcu(). This guarantee applies even if CPU A and CPU B 1745 * are the same CPU, but again only if the system has more than one CPU. 1746 * 1747 * Of course, these memory-ordering guarantees apply only when 1748 * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are 1749 * passed the same srcu_struct structure. 1750 * 1751 * Implementation of these memory-ordering guarantees is similar to 1752 * that of synchronize_rcu(). 1753 * 1754 * If SRCU is likely idle as determined by srcu_should_expedite(), 1755 * expedite the first request. This semantic was provided by Classic SRCU, 1756 * and is relied upon by its users, so TREE SRCU must also provide it. 1757 * Note that detecting idleness is heuristic and subject to both false 1758 * positives and negatives. 1759 */ 1760 void synchronize_srcu(struct srcu_struct *ssp) 1761 { 1762 if (srcu_should_expedite(ssp) || rcu_gp_is_expedited()) 1763 synchronize_srcu_expedited(ssp); 1764 else 1765 __synchronize_srcu(ssp, true); 1766 } 1767 EXPORT_SYMBOL_GPL(synchronize_srcu); 1768 1769 /** 1770 * get_state_synchronize_srcu - Provide an end-of-grace-period cookie 1771 * @ssp: srcu_struct to provide cookie for. 1772 * 1773 * This function returns a cookie that can be passed to 1774 * poll_state_synchronize_srcu(), which will return true if a full grace 1775 * period has elapsed in the meantime. It is the caller's responsibility 1776 * to make sure that grace period happens, for example, by invoking 1777 * call_srcu() after return from get_state_synchronize_srcu(). 1778 */ 1779 unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp) 1780 { 1781 // Any prior manipulation of SRCU-protected data must happen 1782 // before the load from ->srcu_gp_seq. 1783 smp_mb(); 1784 return rcu_seq_snap(&ssp->srcu_sup->srcu_gp_seq); 1785 } 1786 EXPORT_SYMBOL_GPL(get_state_synchronize_srcu); 1787 1788 /** 1789 * start_poll_synchronize_srcu - Provide cookie and start grace period 1790 * @ssp: srcu_struct to provide cookie for. 1791 * 1792 * This function returns a cookie that can be passed to 1793 * poll_state_synchronize_srcu(), which will return true if a full grace 1794 * period has elapsed in the meantime. Unlike get_state_synchronize_srcu(), 1795 * this function also ensures that any needed SRCU grace period will be 1796 * started. This convenience does come at a cost in terms of CPU overhead. 1797 * 1798 * This function cannot be used with atomic SRCU, which only has 1799 * atomic grace periods. Give a warning if someone tries, and return 1800 * the same cookie that would have been returned, but refrain from 1801 * messing up state by starting a grace period. If someone somewhere 1802 * somehow invokes synchronize_srcu_atomic(), passing this cookie to 1803 * poll_state_synchronize_srcu() will return true. If no one ever invokes 1804 * synchronize_srcu_atomic(), too bad. 1805 */ 1806 unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp) 1807 { 1808 if (WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC)) 1809 return get_state_synchronize_srcu(ssp); 1810 else 1811 return srcu_gp_start_if_needed(ssp, NULL, true); 1812 } 1813 EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu); 1814 1815 /** 1816 * poll_state_synchronize_srcu - Has cookie's grace period ended? 1817 * @ssp: srcu_struct to provide cookie for. 1818 * @cookie: Return value from get_state_synchronize_srcu() or start_poll_synchronize_srcu(). 1819 * 1820 * This function takes the cookie that was returned from either 1821 * get_state_synchronize_srcu() or start_poll_synchronize_srcu(), and 1822 * returns @true if an SRCU grace period elapsed since the time that the 1823 * cookie was created. 1824 * 1825 * Because cookies are finite in size, wrapping/overflow is possible. 1826 * This is more pronounced on 32-bit systems where cookies are 32 bits, 1827 * where in theory wrapping could happen in about 14 hours assuming 1828 * 25-microsecond expedited SRCU grace periods. However, a more likely 1829 * overflow lower bound is on the order of 24 days in the case of 1830 * one-millisecond SRCU grace periods. Of course, wrapping in a 64-bit 1831 * system requires geologic timespans, as in more than seven million years 1832 * even for expedited SRCU grace periods. 1833 * 1834 * Wrapping/overflow is much more of an issue for CONFIG_SMP=n systems 1835 * that also have CONFIG_PREEMPTION=n, which selects Tiny SRCU. This uses 1836 * a 16-bit cookie, which rcutorture routinely wraps in a matter of a 1837 * few minutes. If this proves to be a problem, this counter will be 1838 * expanded to the same size as for Tree SRCU. 1839 */ 1840 bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie) 1841 { 1842 if (cookie != SRCU_GET_STATE_COMPLETED && 1843 !rcu_seq_done_exact(&ssp->srcu_sup->srcu_gp_seq, cookie)) 1844 return false; 1845 // Ensure that the end of the SRCU grace period happens before 1846 // any subsequent code that the caller might execute. 1847 smp_mb(); // ^^^ 1848 return true; 1849 } 1850 EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu); 1851 1852 /* 1853 * Callback function for srcu_barrier() use. 1854 */ 1855 static void srcu_barrier_cb(struct rcu_head *rhp) 1856 { 1857 struct srcu_data *sdp; 1858 struct srcu_struct *ssp; 1859 1860 rhp->next = rhp; // Mark the callback as having been invoked. 1861 sdp = container_of(rhp, struct srcu_data, srcu_barrier_head); 1862 ssp = sdp->ssp; 1863 if (atomic_dec_and_test(&ssp->srcu_sup->srcu_barrier_cpu_cnt)) 1864 complete(&ssp->srcu_sup->srcu_barrier_completion); 1865 } 1866 1867 /* 1868 * Enqueue an srcu_barrier() callback on the specified srcu_data 1869 * structure's ->cblist. but only if that ->cblist already has at least one 1870 * callback enqueued. Note that if a CPU already has callbacks enqueue, 1871 * it must have already registered the need for a future grace period, 1872 * so all we need do is enqueue a callback that will use the same grace 1873 * period as the last callback already in the queue. 1874 */ 1875 static void srcu_barrier_one_cpu(struct srcu_struct *ssp, struct srcu_data *sdp) 1876 { 1877 raw_spin_lock_irq_rcu_node(sdp); 1878 atomic_inc(&ssp->srcu_sup->srcu_barrier_cpu_cnt); 1879 sdp->srcu_barrier_head.func = srcu_barrier_cb; 1880 debug_rcu_head_queue(&sdp->srcu_barrier_head); 1881 if (!rcu_segcblist_entrain(&sdp->srcu_cblist, 1882 &sdp->srcu_barrier_head)) { 1883 debug_rcu_head_unqueue(&sdp->srcu_barrier_head); 1884 atomic_dec(&ssp->srcu_sup->srcu_barrier_cpu_cnt); 1885 } 1886 raw_spin_unlock_irq_rcu_node(sdp); 1887 } 1888 1889 /** 1890 * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete. 1891 * @ssp: srcu_struct on which to wait for in-flight callbacks. 1892 */ 1893 void srcu_barrier(struct srcu_struct *ssp) 1894 { 1895 int cpu; 1896 int idx; 1897 unsigned long s; 1898 1899 check_init_srcu_struct(ssp, false); 1900 // Atomic SRCU has no callbacks, so there is nothing to wait on. 1901 if (WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC)) 1902 return; 1903 1904 /* 1905 * Register any deferred callbacks before snapshotting the sequence. The 1906 * staging list is per-CPU, not per-srcu_struct, so this also drains 1907 * other srcu_structs'. 1908 */ 1909 srcu_defer_flush(); 1910 1911 s = rcu_seq_snap(&ssp->srcu_sup->srcu_barrier_seq); 1912 mutex_lock(&ssp->srcu_sup->srcu_barrier_mutex); 1913 if (rcu_seq_done(&ssp->srcu_sup->srcu_barrier_seq, s)) { 1914 smp_mb(); /* Force ordering following return. */ 1915 mutex_unlock(&ssp->srcu_sup->srcu_barrier_mutex); 1916 return; /* Someone else did our work for us. */ 1917 } 1918 rcu_seq_start(&ssp->srcu_sup->srcu_barrier_seq); 1919 init_completion(&ssp->srcu_sup->srcu_barrier_completion); 1920 1921 /* Initial count prevents reaching zero until all CBs are posted. */ 1922 atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 1); 1923 1924 idx = __srcu_read_lock_nmisafe(ssp); 1925 if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER) 1926 srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, get_boot_cpu_id())); 1927 else 1928 for_each_possible_cpu(cpu) 1929 srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, cpu)); 1930 __srcu_read_unlock_nmisafe(ssp, idx); 1931 1932 /* Remove the initial count, at which point reaching zero can happen. */ 1933 if (atomic_dec_and_test(&ssp->srcu_sup->srcu_barrier_cpu_cnt)) 1934 complete(&ssp->srcu_sup->srcu_barrier_completion); 1935 wait_for_completion(&ssp->srcu_sup->srcu_barrier_completion); 1936 1937 rcu_seq_end(&ssp->srcu_sup->srcu_barrier_seq); 1938 mutex_unlock(&ssp->srcu_sup->srcu_barrier_mutex); 1939 } 1940 EXPORT_SYMBOL_GPL(srcu_barrier); 1941 1942 /* Callback for srcu_expedite_current() usage. */ 1943 static void srcu_expedite_current_cb(struct rcu_head *rhp) 1944 { 1945 unsigned long flags; 1946 bool needcb = false; 1947 struct srcu_data *sdp = container_of(rhp, struct srcu_data, srcu_ec_head); 1948 1949 raw_spin_lock_irqsave_sdp_contention(sdp, &flags); 1950 if (sdp->srcu_ec_state == SRCU_EC_IDLE) { 1951 WARN_ON_ONCE(1); 1952 } else if (sdp->srcu_ec_state == SRCU_EC_PENDING) { 1953 sdp->srcu_ec_state = SRCU_EC_IDLE; 1954 } else { 1955 WARN_ON_ONCE(sdp->srcu_ec_state != SRCU_EC_REPOST); 1956 sdp->srcu_ec_state = SRCU_EC_PENDING; 1957 needcb = true; 1958 } 1959 raw_spin_unlock_irqrestore_rcu_node(sdp, flags); 1960 // If needed, requeue ourselves as an expedited SRCU callback. 1961 if (needcb) 1962 __call_srcu(sdp->ssp, &sdp->srcu_ec_head, srcu_expedite_current_cb, false); 1963 } 1964 1965 /** 1966 * srcu_expedite_current - Expedite the current SRCU grace period 1967 * @ssp: srcu_struct to expedite. 1968 * 1969 * Cause the current SRCU grace period to become expedited. The grace 1970 * period following the current one might also be expedited. If there is 1971 * no current grace period, one might be created. If the current grace 1972 * period is currently sleeping, that sleep will complete before expediting 1973 * will take effect. 1974 * 1975 * This function must not be invoked on srcu_struct structures that are 1976 * used with srcu_read_lock_atomic() and synchronize_srcu_atomic(). 1977 */ 1978 void srcu_expedite_current(struct srcu_struct *ssp) 1979 { 1980 unsigned long flags; 1981 bool needcb = false; 1982 struct srcu_data *sdp; 1983 1984 // Atomic SRCU has no callbacks, so there is nothing to expedite. 1985 if (WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC)) 1986 return; 1987 migrate_disable(); 1988 sdp = this_cpu_ptr(ssp->sda); 1989 raw_spin_lock_irqsave_sdp_contention(sdp, &flags); 1990 if (sdp->srcu_ec_state == SRCU_EC_IDLE) { 1991 sdp->srcu_ec_state = SRCU_EC_PENDING; 1992 needcb = true; 1993 } else if (sdp->srcu_ec_state == SRCU_EC_PENDING) { 1994 sdp->srcu_ec_state = SRCU_EC_REPOST; 1995 } else { 1996 WARN_ON_ONCE(sdp->srcu_ec_state != SRCU_EC_REPOST); 1997 } 1998 raw_spin_unlock_irqrestore_rcu_node(sdp, flags); 1999 // If needed, queue an expedited SRCU callback. 2000 if (needcb) 2001 __call_srcu(ssp, &sdp->srcu_ec_head, srcu_expedite_current_cb, false); 2002 migrate_enable(); 2003 } 2004 EXPORT_SYMBOL_GPL(srcu_expedite_current); 2005 2006 /** 2007 * srcu_batches_completed - return batches completed. 2008 * @ssp: srcu_struct on which to report batch completion. 2009 * 2010 * Report the number of batches, correlated with, but not necessarily 2011 * precisely the same as, the number of grace periods that have elapsed. 2012 */ 2013 unsigned long srcu_batches_completed(struct srcu_struct *ssp) 2014 { 2015 return READ_ONCE(ssp->srcu_sup->srcu_gp_seq); 2016 } 2017 EXPORT_SYMBOL_GPL(srcu_batches_completed); 2018 2019 /* 2020 * Core SRCU state machine. Push state bits of ->srcu_gp_seq 2021 * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has 2022 * completed in that state. Set is_atomic to indicate that 2023 * the caller is excluding other calls so that ->srcu_gp_mutex 2024 * is not needed, and to indicate that blocking is forbidden. 2025 */ 2026 static void srcu_advance_state(struct srcu_struct *ssp, bool is_atomic) 2027 { 2028 unsigned long flags; 2029 int idx; 2030 2031 if (!is_atomic) 2032 mutex_lock(&ssp->srcu_sup->srcu_gp_mutex); 2033 2034 /* 2035 * Because readers might be delayed for an extended period after 2036 * fetching ->srcu_ctrp for their index, at any point in time there 2037 * might well be readers using both idx=0 and idx=1. We therefore 2038 * need to wait for readers to clear from both index values before 2039 * invoking a callback. 2040 * 2041 * The load-acquire ensures that we see the accesses performed 2042 * by the prior grace period. 2043 */ 2044 idx = rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq)); /* ^^^ */ 2045 if (idx == SRCU_STATE_IDLE) { 2046 raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags); 2047 if (ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed)) { 2048 WARN_ON_ONCE(rcu_seq_state(ssp->srcu_sup->srcu_gp_seq)); 2049 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags); 2050 if (!is_atomic) 2051 mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex); 2052 return; 2053 } 2054 idx = rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq)); 2055 if (idx == SRCU_STATE_IDLE) { 2056 WARN_ON_ONCE(ssp->srcu_reader_flavor & SRCU_READ_FLAVOR_ATOMIC); 2057 srcu_gp_start(ssp); 2058 } 2059 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags); 2060 if (idx != SRCU_STATE_IDLE) { 2061 if (!is_atomic) 2062 mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex); 2063 return; /* Someone else started the grace period. */ 2064 } 2065 } 2066 2067 if (rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq)) == SRCU_STATE_SCAN1) { 2068 idx = !(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0]); 2069 if (!try_check_zero(ssp, idx, 1)) { 2070 if (!is_atomic) 2071 mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex); 2072 return; /* readers present, retry later. */ 2073 } 2074 srcu_flip(ssp); 2075 raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags); 2076 rcu_seq_set_state(&ssp->srcu_sup->srcu_gp_seq, SRCU_STATE_SCAN2); 2077 ssp->srcu_sup->srcu_n_exp_nodelay = 0; 2078 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags); 2079 } 2080 2081 if (rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq)) == SRCU_STATE_SCAN2) { 2082 2083 /* 2084 * SRCU read-side critical sections are normally short, 2085 * so check at least twice in quick succession after a flip. 2086 */ 2087 idx = !(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0]); 2088 if (!try_check_zero(ssp, idx, 2)) { 2089 if (!is_atomic) 2090 mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex); 2091 return; /* readers present, retry later. */ 2092 } 2093 ssp->srcu_sup->srcu_n_exp_nodelay = 0; 2094 srcu_gp_end(ssp, is_atomic); /* Releases ->srcu_gp_mutex. */ 2095 } 2096 } 2097 2098 /** 2099 * synchronize_srcu_atomic - spin for prior SRCU read-side critical-section completion 2100 * @ssp: srcu_struct with which to synchronize. 2101 * 2102 * Similar to synchronize_srcu(), but spins rather than blocking. 2103 * Use only with srcu_read_lock_atomic() and srcu_read_unlock_atomic(), 2104 * which are forbidden from voluntarily context switching. If 2105 * synchronize_srcu_atomic() is invoked from a more restrictive context 2106 * (for example, interrupts disabled) for a given srcu_struct structure, 2107 * then for that structure, all calls to both srcu_read_lock_atomic() 2108 * and srcu_read_unlock_atomic() must be invoked from that same context, 2109 * or one that is even more strict. 2110 * 2111 * If synchronize_srcu_atomic() is invoked on a given srcu_struct 2112 * structure, then none of call_srcu(), synchronize_srcu(), 2113 * synchronize_srcu_expedited(), start_poll_synchronize_srcu(), 2114 * srcu_barrier(), or srcu_expedite_current() may be invoked on that 2115 * same structure. 2116 * 2117 * Because synchronize_srcu_atomic() is even more expedited than is 2118 * synchronize_srcu_expedited(), there is no expedited counterpart to 2119 * this function. 2120 */ 2121 void synchronize_srcu_atomic(struct srcu_struct *ssp) 2122 { 2123 unsigned long srcu_state; 2124 struct srcu_usage *sup = ssp->srcu_sup; 2125 unsigned long flags; 2126 unsigned long rdm0, rdm1; 2127 unsigned long unlocks0, unlocks1; 2128 2129 srcu_lock_sync(&ssp->dep_map); 2130 2131 RCU_LOCKDEP_WARN(lockdep_is_held(ssp), 2132 "Illegal synchronize_srcu_atomic() in same-type SRCU read-side critical section"); 2133 2134 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE) 2135 return; 2136 2137 // Initialize. Either init_srcu_struct() was invoked or 2138 // DEFINE_SRCU() or similar was used. Therefore, no allocation 2139 // will be done here. 2140 check_init_srcu_struct(ssp, true); 2141 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_ATOMIC); 2142 2143 // Perhaps others will do our work for us. 2144 srcu_state = get_state_synchronize_srcu(ssp); 2145 preempt_disable(); 2146 while (atomic_read(&sup->srcu_atomic_gp_flag) || 2147 atomic_xchg(&sup->srcu_atomic_gp_flag, 1)) { 2148 preempt_enable(); 2149 if (poll_state_synchronize_srcu(ssp, srcu_state)) 2150 return; 2151 cpu_relax(); 2152 preempt_disable(); 2153 } 2154 2155 // One last check for others doing our work for us under the lock. 2156 raw_spin_lock_irqsave_rcu_node(sup, flags); 2157 if (poll_state_synchronize_srcu(ssp, srcu_state)) { 2158 raw_spin_unlock_irqrestore_rcu_node(sup, flags); 2159 atomic_set(&sup->srcu_atomic_gp_flag, 0); 2160 preempt_enable(); 2161 return; 2162 } 2163 2164 // OK, we really have to do it ourselves. Start the grace period. 2165 non_block_start(); // We must not voluntarily block! 2166 smp_store_release(&sup->srcu_gp_seq_needed, srcu_state); // See srcu_funnel_gp_start(). 2167 ASSERT_EXCLUSIVE_WRITER(ssp->srcu_sup->srcu_gp_seq); 2168 srcu_gp_start(ssp); 2169 raw_spin_unlock_irqrestore_rcu_node(sup, flags); 2170 2171 // 2172 // Fastpath: If there are no readers at all, neither grace-period 2173 // scan need wait, so both can be satisfied at once without doing 2174 // the index flip. The counter-sum proof is the same as that of 2175 // srcu_readers_active_idx_check(), but spanning both indices. 2176 // Atomic SRCU guarantees that all readers are of 2177 // SRCU_READ_FLAVOR_ATOMIC, so the SLOWGP check never triggers and 2178 // the ->srcu_reader_flavor masks returned by 2179 // srcu_readers_unlock_idx() are unused. 2180 // 2181 // This proof must follow the grace-period anchor written by the 2182 // srcu_gp_start() above, never precede it. With the anchor first, 2183 // a reader whose lock increment is missed by the sums below cannot 2184 // have incremented its lock counter before the anchor, and therefore 2185 // cannot be a pre-existing reader of this grace period. Placing the 2186 // proof before the anchor would let this grace period miss a 2187 // pre-existing reader and return without waiting for it. 2188 // 2189 // The smp_mb() pairs with the smp_mb() in __srcu_read_lock() 2190 // (store-buffering pattern), which guarantees that a lock is always 2191 // counted if the corresponding unlock is counted, the same 2192 // memory-ordering guarantee as is provided by 2193 // srcu_readers_active_idx_check(). 2194 // 2195 unlocks0 = srcu_readers_unlock_idx(ssp, 0, &rdm0); 2196 unlocks1 = srcu_readers_unlock_idx(ssp, 1, &rdm1); 2197 smp_mb(); /* A */ 2198 if (srcu_readers_lock_idx(ssp, 0, false, unlocks0) && 2199 srcu_readers_lock_idx(ssp, 1, false, unlocks1)) { 2200 // No readers, so end this grace period manually, skipping 2201 // the index flip. Advancing the sequence number via 2202 // rcu_seq_start() in srcu_gp_start() above and rcu_seq_end() 2203 // below keeps get_state_synchronize_srcu() and 2204 // poll_state_synchronize_srcu() working, all under ->lock 2205 // and ->srcu_atomic_gp_flag, which excludes concurrent 2206 // sequence-number updates. 2207 raw_spin_lock_irqsave_rcu_node(sup, flags); 2208 rcu_seq_end(&sup->srcu_gp_seq); 2209 raw_spin_unlock_irqrestore_rcu_node(sup, flags); 2210 WARN_ON_ONCE(!poll_state_synchronize_srcu(ssp, srcu_state)); 2211 atomic_set_release(&sup->srcu_atomic_gp_flag, 0); 2212 preempt_enable(); 2213 non_block_end(); 2214 return; 2215 } 2216 2217 // Wait for it to complete, helping it along. 2218 while (!poll_state_synchronize_srcu(ssp, srcu_state)) { 2219 cpu_relax(); 2220 srcu_advance_state(ssp, true); 2221 } 2222 ASSERT_EXCLUSIVE_WRITER(sup->srcu_atomic_gp_flag); 2223 atomic_set_release(&sup->srcu_atomic_gp_flag, 0); 2224 preempt_enable(); 2225 non_block_end(); 2226 } 2227 EXPORT_SYMBOL_GPL(synchronize_srcu_atomic); 2228 2229 /* 2230 * Invoke a limited number of SRCU callbacks that have passed through 2231 * their grace period. If there are more to do, SRCU will reschedule 2232 * the workqueue. Note that needed memory barriers have been executed 2233 * in this task's context by srcu_readers_active_idx_check(). 2234 */ 2235 static void srcu_invoke_callbacks(struct work_struct *work) 2236 { 2237 long len; 2238 bool more; 2239 struct rcu_cblist ready_cbs; 2240 struct rcu_head *rhp; 2241 struct srcu_data *sdp; 2242 struct srcu_struct *ssp; 2243 2244 sdp = container_of(work, struct srcu_data, work); 2245 2246 ssp = sdp->ssp; 2247 rcu_cblist_init(&ready_cbs); 2248 raw_spin_lock_irq_rcu_node(sdp); 2249 WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL)); 2250 srcu_segcblist_advance(&sdp->srcu_cblist, 2251 rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq)); 2252 /* 2253 * Although this function is theoretically re-entrant, concurrent 2254 * callbacks invocation is disallowed to avoid executing an SRCU barrier 2255 * too early. 2256 */ 2257 if (sdp->srcu_cblist_invoking || 2258 !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) { 2259 raw_spin_unlock_irq_rcu_node(sdp); 2260 return; /* Someone else on the job or nothing to do. */ 2261 } 2262 2263 /* We are on the job! Extract and invoke ready callbacks. */ 2264 sdp->srcu_cblist_invoking = true; 2265 rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs); 2266 len = ready_cbs.len; 2267 raw_spin_unlock_irq_rcu_node(sdp); 2268 rhp = rcu_cblist_dequeue(&ready_cbs); 2269 for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) { 2270 debug_rcu_head_unqueue(rhp); 2271 debug_rcu_head_callback(rhp); 2272 local_bh_disable(); 2273 rhp->func(rhp); 2274 local_bh_enable(); 2275 } 2276 WARN_ON_ONCE(ready_cbs.len); 2277 2278 /* 2279 * Update counts, accelerate new callbacks, and if needed, 2280 * schedule another round of callback invocation. 2281 */ 2282 raw_spin_lock_irq_rcu_node(sdp); 2283 rcu_segcblist_add_len(&sdp->srcu_cblist, -len); 2284 sdp->srcu_cblist_invoking = false; 2285 more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist); 2286 raw_spin_unlock_irq_rcu_node(sdp); 2287 /* An SRCU barrier or callbacks from previous nesting work pending */ 2288 if (more) 2289 srcu_schedule_cbs_sdp(sdp, 0); 2290 } 2291 2292 /* 2293 * Finished one round of SRCU grace period. Start another if there are 2294 * more SRCU callbacks queued, otherwise put SRCU into not-running state. 2295 */ 2296 static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay) 2297 { 2298 bool pushgp = true; 2299 2300 raw_spin_lock_irq_rcu_node(ssp->srcu_sup); 2301 if (ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed)) { 2302 if (!WARN_ON_ONCE(rcu_seq_state(ssp->srcu_sup->srcu_gp_seq))) { 2303 /* All requests fulfilled, time to go idle. */ 2304 pushgp = false; 2305 } 2306 } else if (!rcu_seq_state(ssp->srcu_sup->srcu_gp_seq)) { 2307 /* Outstanding request and no GP. Start one. */ 2308 WARN_ON_ONCE(ssp->srcu_reader_flavor & SRCU_READ_FLAVOR_ATOMIC); 2309 srcu_gp_start(ssp); 2310 } 2311 raw_spin_unlock_irq_rcu_node(ssp->srcu_sup); 2312 2313 if (pushgp) 2314 queue_delayed_work(rcu_gp_wq, &ssp->srcu_sup->work, delay); 2315 } 2316 2317 /* 2318 * This is the work-queue function that handles SRCU grace periods. 2319 */ 2320 static void process_srcu(struct work_struct *work) 2321 { 2322 unsigned long curdelay; 2323 unsigned long j; 2324 struct srcu_struct *ssp; 2325 struct srcu_usage *sup; 2326 2327 sup = container_of(work, struct srcu_usage, work.work); 2328 ssp = sup->srcu_ssp; 2329 2330 srcu_advance_state(ssp, false); 2331 raw_spin_lock_irq_rcu_node(ssp->srcu_sup); 2332 curdelay = srcu_get_delay(ssp); 2333 raw_spin_unlock_irq_rcu_node(ssp->srcu_sup); 2334 if (curdelay) { 2335 WRITE_ONCE(sup->reschedule_count, 0); 2336 } else { 2337 j = jiffies; 2338 if (READ_ONCE(sup->reschedule_jiffies) == j) { 2339 ASSERT_EXCLUSIVE_WRITER(sup->reschedule_count); 2340 WRITE_ONCE(sup->reschedule_count, READ_ONCE(sup->reschedule_count) + 1); 2341 if (READ_ONCE(sup->reschedule_count) > srcu_max_nodelay) 2342 curdelay = 1; 2343 } else { 2344 WRITE_ONCE(sup->reschedule_count, 1); 2345 WRITE_ONCE(sup->reschedule_jiffies, j); 2346 } 2347 } 2348 srcu_reschedule(ssp, curdelay); 2349 } 2350 2351 static void srcu_irq_work(struct irq_work *work) 2352 { 2353 struct srcu_struct *ssp; 2354 struct srcu_usage *sup; 2355 unsigned long delay; 2356 unsigned long flags; 2357 2358 sup = container_of(work, struct srcu_usage, irq_work); 2359 ssp = sup->srcu_ssp; 2360 2361 raw_spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags); 2362 delay = srcu_get_delay(ssp); 2363 raw_spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags); 2364 2365 queue_delayed_work(rcu_gp_wq, &sup->work, !!delay); 2366 } 2367 2368 void srcutorture_get_gp_data(struct srcu_struct *ssp, int *flags, 2369 unsigned long *gp_seq) 2370 { 2371 *flags = 0; 2372 *gp_seq = rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq); 2373 } 2374 EXPORT_SYMBOL_GPL(srcutorture_get_gp_data); 2375 2376 static const char * const srcu_size_state_name[] = { 2377 "SRCU_SIZE_SMALL", 2378 "SRCU_SIZE_ALLOC", 2379 "SRCU_SIZE_WAIT_BARRIER", 2380 "SRCU_SIZE_WAIT_CALL", 2381 "SRCU_SIZE_WAIT_CBS1", 2382 "SRCU_SIZE_WAIT_CBS2", 2383 "SRCU_SIZE_WAIT_CBS3", 2384 "SRCU_SIZE_WAIT_CBS4", 2385 "SRCU_SIZE_BIG", 2386 "SRCU_SIZE_???", 2387 }; 2388 2389 void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf) 2390 { 2391 int cpu; 2392 int idx; 2393 unsigned long s0 = 0, s1 = 0; 2394 int ss_state = READ_ONCE(ssp->srcu_sup->srcu_size_state); 2395 int ss_state_idx = ss_state; 2396 2397 idx = ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0]; 2398 if (ss_state < 0 || ss_state >= ARRAY_SIZE(srcu_size_state_name)) 2399 ss_state_idx = ARRAY_SIZE(srcu_size_state_name) - 1; 2400 pr_alert("%s%s Tree SRCU g%ld state %d (%s)", 2401 tt, tf, rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq), ss_state, 2402 srcu_size_state_name[ss_state_idx]); 2403 if (!ssp->sda) { 2404 // Called after cleanup_srcu_struct(), perhaps. 2405 pr_cont(" No per-CPU srcu_data structures (->sda == NULL).\n"); 2406 } else { 2407 pr_cont(" per-CPU(idx=%d):", idx); 2408 for_each_possible_cpu(cpu) { 2409 unsigned long l0, l1; 2410 unsigned long u0, u1; 2411 long c0, c1; 2412 struct srcu_data *sdp; 2413 2414 sdp = per_cpu_ptr(ssp->sda, cpu); 2415 u0 = data_race(atomic_long_read(&sdp->srcu_ctrs[!idx].srcu_unlocks)); 2416 u1 = data_race(atomic_long_read(&sdp->srcu_ctrs[idx].srcu_unlocks)); 2417 2418 /* 2419 * Make sure that a lock is always counted if the corresponding 2420 * unlock is counted. 2421 */ 2422 smp_rmb(); 2423 2424 l0 = data_race(atomic_long_read(&sdp->srcu_ctrs[!idx].srcu_locks)); 2425 l1 = data_race(atomic_long_read(&sdp->srcu_ctrs[idx].srcu_locks)); 2426 2427 c0 = l0 - u0; 2428 c1 = l1 - u1; 2429 pr_cont(" %d(%ld,%ld %c)", 2430 cpu, c0, c1, 2431 "C."[rcu_segcblist_empty(&sdp->srcu_cblist)]); 2432 s0 += c0; 2433 s1 += c1; 2434 } 2435 pr_cont(" T(%ld,%ld)\n", s0, s1); 2436 } 2437 if (SRCU_SIZING_IS_TORTURE()) { 2438 if (!WARN_ON_ONCE(ssp->srcu_reader_flavor == SRCU_READ_FLAVOR_ATOMIC)) 2439 srcu_transition_to_big(ssp); 2440 } 2441 } 2442 EXPORT_SYMBOL_GPL(srcu_torture_stats_print); 2443 2444 static int __init srcu_bootup_announce(void) 2445 { 2446 pr_info("Hierarchical SRCU implementation.\n"); 2447 if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF) 2448 pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff); 2449 if (srcu_retry_check_delay != SRCU_DEFAULT_RETRY_CHECK_DELAY) 2450 pr_info("\tNon-default retry check delay of %lu us.\n", srcu_retry_check_delay); 2451 if (srcu_max_nodelay != SRCU_DEFAULT_MAX_NODELAY) 2452 pr_info("\tNon-default max no-delay of %lu.\n", srcu_max_nodelay); 2453 pr_info("\tMax phase no-delay instances is %lu.\n", srcu_max_nodelay_phase); 2454 return 0; 2455 } 2456 early_initcall(srcu_bootup_announce); 2457 2458 void __init srcu_init(void) 2459 { 2460 struct srcu_usage *sup; 2461 2462 /* Decide on srcu_struct-size strategy. */ 2463 if (SRCU_SIZING_IS(SRCU_SIZING_AUTO)) { 2464 if (nr_cpu_ids >= big_cpu_lim) { 2465 convert_to_big = SRCU_SIZING_INIT; // Don't bother waiting for contention. 2466 pr_info("%s: Setting srcu_struct sizes to big.\n", __func__); 2467 } else { 2468 convert_to_big = SRCU_SIZING_NONE | SRCU_SIZING_CONTEND; 2469 pr_info("%s: Setting srcu_struct sizes based on contention.\n", __func__); 2470 } 2471 } 2472 2473 /* 2474 * Once that is set, call_srcu() can follow the normal path and 2475 * queue delayed work. This must follow RCU workqueues creation 2476 * and timers initialization. 2477 */ 2478 srcu_init_done = true; 2479 while (!list_empty(&srcu_boot_list)) { 2480 sup = list_first_entry(&srcu_boot_list, struct srcu_usage, 2481 work.work.entry); 2482 list_del_init(&sup->work.work.entry); 2483 if (SRCU_SIZING_IS(SRCU_SIZING_INIT) && 2484 sup->srcu_size_state == SRCU_SIZE_SMALL) 2485 sup->srcu_size_state = SRCU_SIZE_ALLOC; 2486 queue_work(rcu_gp_wq, &sup->work.work); 2487 } 2488 } 2489 2490 #ifdef CONFIG_MODULES 2491 2492 /* Initialize any global-scope srcu_struct structures used by this module. */ 2493 static int srcu_module_coming(struct module *mod) 2494 { 2495 int i; 2496 struct srcu_struct *ssp; 2497 struct srcu_struct **sspp = mod->srcu_struct_ptrs; 2498 2499 for (i = 0; i < mod->num_srcu_structs; i++) { 2500 ssp = *(sspp++); 2501 ssp->sda = alloc_percpu(struct srcu_data); 2502 if (WARN_ON_ONCE(!ssp->sda)) 2503 return -ENOMEM; 2504 ssp->srcu_ctrp = &ssp->sda->srcu_ctrs[0]; 2505 } 2506 return 0; 2507 } 2508 2509 /* Clean up any global-scope srcu_struct structures used by this module. */ 2510 static void srcu_module_going(struct module *mod) 2511 { 2512 int i; 2513 struct srcu_struct *ssp; 2514 struct srcu_struct **sspp = mod->srcu_struct_ptrs; 2515 2516 /* 2517 * Deferral skips check_init_srcu_struct(), so cleanup_srcu_struct() 2518 * below can be skipped for an srcu_struct that has staged callbacks. 2519 * Drain them before any ->sda is freed. 2520 */ 2521 srcu_defer_flush(); 2522 for (i = 0; i < mod->num_srcu_structs; i++) { 2523 ssp = *(sspp++); 2524 if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed)) && 2525 !WARN_ON_ONCE(!ssp->srcu_sup->sda_is_static)) 2526 cleanup_srcu_struct(ssp); 2527 if (!WARN_ON(srcu_readers_active(ssp))) 2528 free_percpu(ssp->sda); 2529 } 2530 } 2531 2532 /* Handle one module, either coming or going. */ 2533 static int srcu_module_notify(struct notifier_block *self, 2534 unsigned long val, void *data) 2535 { 2536 struct module *mod = data; 2537 int ret = 0; 2538 2539 switch (val) { 2540 case MODULE_STATE_COMING: 2541 ret = srcu_module_coming(mod); 2542 break; 2543 case MODULE_STATE_GOING: 2544 srcu_module_going(mod); 2545 break; 2546 default: 2547 break; 2548 } 2549 return ret; 2550 } 2551 2552 static struct notifier_block srcu_module_nb = { 2553 .notifier_call = srcu_module_notify, 2554 .priority = 0, 2555 }; 2556 2557 static __init int init_srcu_module_notifier(void) 2558 { 2559 int ret; 2560 2561 ret = register_module_notifier(&srcu_module_nb); 2562 if (ret) 2563 pr_warn("Failed to register srcu module notifier\n"); 2564 return ret; 2565 } 2566 late_initcall(init_srcu_module_notifier); 2567 2568 #endif /* #ifdef CONFIG_MODULES */ 2569