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