1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Read-Copy Update mechanism for mutual exclusion (tree-based version) 4 * 5 * Copyright IBM Corporation, 2008 6 * 7 * Authors: Dipankar Sarma <dipankar@in.ibm.com> 8 * Manfred Spraul <manfred@colorfullife.com> 9 * Paul E. McKenney <paulmck@linux.ibm.com> 10 * 11 * Based on the original work by Paul McKenney <paulmck@linux.ibm.com> 12 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen. 13 * 14 * For detailed explanation of Read-Copy Update mechanism see - 15 * Documentation/RCU 16 */ 17 18 #define pr_fmt(fmt) "rcu: " fmt 19 20 #include <linux/types.h> 21 #include <linux/kernel.h> 22 #include <linux/init.h> 23 #include <linux/spinlock.h> 24 #include <linux/smp.h> 25 #include <linux/rcupdate_wait.h> 26 #include <linux/interrupt.h> 27 #include <linux/llist.h> 28 #include <linux/sched.h> 29 #include <linux/sched/debug.h> 30 #include <linux/nmi.h> 31 #include <linux/atomic.h> 32 #include <linux/bitops.h> 33 #include <linux/export.h> 34 #include <linux/completion.h> 35 #include <linux/kmemleak.h> 36 #include <linux/moduleparam.h> 37 #include <linux/panic.h> 38 #include <linux/panic_notifier.h> 39 #include <linux/percpu.h> 40 #include <linux/notifier.h> 41 #include <linux/cpu.h> 42 #include <linux/mutex.h> 43 #include <linux/time.h> 44 #include <linux/kernel_stat.h> 45 #include <linux/wait.h> 46 #include <linux/kthread.h> 47 #include <uapi/linux/sched/types.h> 48 #include <linux/prefetch.h> 49 #include <linux/delay.h> 50 #include <linux/random.h> 51 #include <linux/trace_events.h> 52 #include <linux/suspend.h> 53 #include <linux/ftrace.h> 54 #include <linux/tick.h> 55 #include <linux/sysrq.h> 56 #include <linux/kprobes.h> 57 #include <linux/gfp.h> 58 #include <linux/oom.h> 59 #include <linux/smpboot.h> 60 #include <linux/jiffies.h> 61 #include <linux/slab.h> 62 #include <linux/sched/isolation.h> 63 #include <linux/sched/clock.h> 64 #include <linux/vmalloc.h> 65 #include <linux/mm.h> 66 #include <linux/kasan.h> 67 #include <linux/context_tracking.h> 68 69 #include "tree.h" 70 #include "rcu.h" 71 72 #ifdef MODULE_PARAM_PREFIX 73 #undef MODULE_PARAM_PREFIX 74 #endif 75 #define MODULE_PARAM_PREFIX "rcutree." 76 77 /* Data structures. */ 78 static void rcu_sr_normal_gp_cleanup_work(struct work_struct *); 79 80 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = { 81 .gpwrap = true, 82 }; 83 84 int rcu_get_gpwrap_count(int cpu) 85 { 86 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 87 88 return READ_ONCE(rdp->gpwrap_count); 89 } 90 EXPORT_SYMBOL_GPL(rcu_get_gpwrap_count); 91 92 static struct rcu_state rcu_state = { 93 .level = { &rcu_state.node[0] }, 94 .gp_state = RCU_GP_IDLE, 95 .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT, 96 .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex), 97 .barrier_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.barrier_lock), 98 .name = RCU_NAME, 99 .abbr = RCU_ABBR, 100 .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex), 101 .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex), 102 .ofl_lock = __ARCH_SPIN_LOCK_UNLOCKED, 103 .srs_cleanup_work = __WORK_INITIALIZER(rcu_state.srs_cleanup_work, 104 rcu_sr_normal_gp_cleanup_work), 105 .srs_cleanups_pending = ATOMIC_INIT(0), 106 #ifdef CONFIG_RCU_NOCB_CPU 107 .nocb_mutex = __MUTEX_INITIALIZER(rcu_state.nocb_mutex), 108 #endif 109 }; 110 111 /* Dump rcu_node combining tree at boot to verify correct setup. */ 112 static bool dump_tree; 113 module_param(dump_tree, bool, 0444); 114 /* By default, use RCU_SOFTIRQ instead of rcuc kthreads. */ 115 static bool use_softirq = !IS_ENABLED(CONFIG_PREEMPT_RT); 116 #ifndef CONFIG_PREEMPT_RT 117 module_param(use_softirq, bool, 0444); 118 #endif 119 /* Control rcu_node-tree auto-balancing at boot time. */ 120 static bool rcu_fanout_exact; 121 module_param(rcu_fanout_exact, bool, 0444); 122 /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */ 123 static int rcu_fanout_leaf = RCU_FANOUT_LEAF; 124 module_param(rcu_fanout_leaf, int, 0444); 125 int rcu_num_lvls __read_mostly = RCU_NUM_LVLS; 126 /* Number of rcu_nodes at specified level. */ 127 int num_rcu_lvl[] = NUM_RCU_LVL_INIT; 128 int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */ 129 130 /* 131 * The rcu_scheduler_active variable is initialized to the value 132 * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the 133 * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE, 134 * RCU can assume that there is but one task, allowing RCU to (for example) 135 * optimize synchronize_rcu() to a simple barrier(). When this variable 136 * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required 137 * to detect real grace periods. This variable is also used to suppress 138 * boot-time false positives from lockdep-RCU error checking. Finally, it 139 * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU 140 * is fully initialized, including all of its kthreads having been spawned. 141 */ 142 int rcu_scheduler_active __read_mostly; 143 EXPORT_SYMBOL_GPL(rcu_scheduler_active); 144 145 /* 146 * The rcu_scheduler_fully_active variable transitions from zero to one 147 * during the early_initcall() processing, which is after the scheduler 148 * is capable of creating new tasks. So RCU processing (for example, 149 * creating tasks for RCU priority boosting) must be delayed until after 150 * rcu_scheduler_fully_active transitions from zero to one. We also 151 * currently delay invocation of any RCU callbacks until after this point. 152 * 153 * It might later prove better for people registering RCU callbacks during 154 * early boot to take responsibility for these callbacks, but one step at 155 * a time. 156 */ 157 static int rcu_scheduler_fully_active __read_mostly; 158 159 static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp, 160 unsigned long gps, unsigned long flags); 161 static void invoke_rcu_core(void); 162 static void rcu_report_exp_rdp(struct rcu_data *rdp); 163 static void rcu_report_qs_rdp(struct rcu_data *rdp); 164 static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp); 165 static bool rcu_rdp_is_offloaded(struct rcu_data *rdp); 166 static bool rcu_rdp_cpu_online(struct rcu_data *rdp); 167 static bool rcu_init_invoked(void); 168 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf); 169 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf); 170 171 /* 172 * rcuc/rcub/rcuop kthread realtime priority. The "rcuop" 173 * real-time priority(enabling/disabling) is controlled by 174 * the extra CONFIG_RCU_NOCB_CPU_CB_BOOST configuration. 175 */ 176 static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0; 177 module_param(kthread_prio, int, 0444); 178 179 /* Delay in jiffies for grace-period initialization delays, debug only. */ 180 181 static int gp_preinit_delay; 182 module_param(gp_preinit_delay, int, 0444); 183 static int gp_init_delay; 184 module_param(gp_init_delay, int, 0444); 185 static int gp_cleanup_delay; 186 module_param(gp_cleanup_delay, int, 0444); 187 static int nohz_full_patience_delay; 188 module_param(nohz_full_patience_delay, int, 0444); 189 static int nohz_full_patience_delay_jiffies; 190 191 // Add delay to rcu_read_unlock() for strict grace periods. 192 static int rcu_unlock_delay; 193 #ifdef CONFIG_RCU_STRICT_GRACE_PERIOD 194 module_param(rcu_unlock_delay, int, 0444); 195 #endif 196 197 /* Retrieve RCU kthreads priority for rcutorture */ 198 int rcu_get_gp_kthreads_prio(void) 199 { 200 return kthread_prio; 201 } 202 EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio); 203 204 /* 205 * Number of grace periods between delays, normalized by the duration of 206 * the delay. The longer the delay, the more the grace periods between 207 * each delay. The reason for this normalization is that it means that, 208 * for non-zero delays, the overall slowdown of grace periods is constant 209 * regardless of the duration of the delay. This arrangement balances 210 * the need for long delays to increase some race probabilities with the 211 * need for fast grace periods to increase other race probabilities. 212 */ 213 #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays for debugging. */ 214 215 /* 216 * Return true if an RCU grace period is in progress. The READ_ONCE()s 217 * permit this function to be invoked without holding the root rcu_node 218 * structure's ->lock, but of course results can be subject to change. 219 */ 220 static int rcu_gp_in_progress(void) 221 { 222 return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq)); 223 } 224 225 /* 226 * Return the number of callbacks queued on the specified CPU. 227 * Handles both the nocbs and normal cases. 228 */ 229 static long rcu_get_n_cbs_cpu(int cpu) 230 { 231 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 232 233 if (rcu_segcblist_is_enabled(&rdp->cblist)) 234 return rcu_segcblist_n_cbs(&rdp->cblist); 235 return 0; 236 } 237 238 /** 239 * rcu_softirq_qs - Provide a set of RCU quiescent states in softirq processing 240 * 241 * Mark a quiescent state for RCU, Tasks RCU, and Tasks Trace RCU. 242 * This is a special-purpose function to be used in the softirq 243 * infrastructure and perhaps the occasional long-running softirq 244 * handler. 245 * 246 * Note that from RCU's viewpoint, a call to rcu_softirq_qs() is 247 * equivalent to momentarily completely enabling preemption. For 248 * example, given this code:: 249 * 250 * local_bh_disable(); 251 * do_something(); 252 * rcu_softirq_qs(); // A 253 * do_something_else(); 254 * local_bh_enable(); // B 255 * 256 * A call to synchronize_rcu() that began concurrently with the 257 * call to do_something() would be guaranteed to wait only until 258 * execution reached statement A. Without that rcu_softirq_qs(), 259 * that same synchronize_rcu() would instead be guaranteed to wait 260 * until execution reached statement B. 261 */ 262 void rcu_softirq_qs(void) 263 { 264 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) || 265 lock_is_held(&rcu_lock_map) || 266 lock_is_held(&rcu_sched_lock_map), 267 "Illegal rcu_softirq_qs() in RCU read-side critical section"); 268 rcu_qs(); 269 rcu_preempt_deferred_qs(current); 270 rcu_tasks_qs(current, false); 271 } 272 273 /* 274 * Reset the current CPU's RCU_WATCHING counter to indicate that the 275 * newly onlined CPU is no longer in an extended quiescent state. 276 * This will either leave the counter unchanged, or increment it 277 * to the next non-quiescent value. 278 * 279 * The non-atomic test/increment sequence works because the upper bits 280 * of the ->state variable are manipulated only by the corresponding CPU, 281 * or when the corresponding CPU is offline. 282 */ 283 static void rcu_watching_online(void) 284 { 285 if (ct_rcu_watching() & CT_RCU_WATCHING) 286 return; 287 ct_state_inc(CT_RCU_WATCHING); 288 } 289 290 /* 291 * Return true if the snapshot returned from ct_rcu_watching() 292 * indicates that RCU is in an extended quiescent state. 293 */ 294 static bool rcu_watching_snap_in_eqs(int snap) 295 { 296 return !(snap & CT_RCU_WATCHING); 297 } 298 299 /** 300 * rcu_watching_snap_stopped_since() - Has RCU stopped watching a given CPU 301 * since the specified @snap? 302 * 303 * @rdp: The rcu_data corresponding to the CPU for which to check EQS. 304 * @snap: rcu_watching snapshot taken when the CPU wasn't in an EQS. 305 * 306 * Returns true if the CPU corresponding to @rdp has spent some time in an 307 * extended quiescent state since @snap. Note that this doesn't check if it 308 * /still/ is in an EQS, just that it went through one since @snap. 309 * 310 * This is meant to be used in a loop waiting for a CPU to go through an EQS. 311 */ 312 static bool rcu_watching_snap_stopped_since(struct rcu_data *rdp, int snap) 313 { 314 /* 315 * The first failing snapshot is already ordered against the accesses 316 * performed by the remote CPU after it exits idle. 317 * 318 * The second snapshot therefore only needs to order against accesses 319 * performed by the remote CPU prior to entering idle and therefore can 320 * rely solely on acquire semantics. 321 */ 322 if (WARN_ON_ONCE(rcu_watching_snap_in_eqs(snap))) 323 return true; 324 325 return snap != ct_rcu_watching_cpu_acquire(rdp->cpu); 326 } 327 328 /* 329 * Return true if the referenced integer is zero while the specified 330 * CPU remains within a single extended quiescent state. 331 */ 332 bool rcu_watching_zero_in_eqs(int cpu, int *vp) 333 { 334 int snap; 335 336 // If not quiescent, force back to earlier extended quiescent state. 337 snap = ct_rcu_watching_cpu(cpu) & ~CT_RCU_WATCHING; 338 smp_rmb(); // Order CT state and *vp reads. 339 if (READ_ONCE(*vp)) 340 return false; // Non-zero, so report failure; 341 smp_rmb(); // Order *vp read and CT state re-read. 342 343 // If still in the same extended quiescent state, we are good! 344 return snap == ct_rcu_watching_cpu(cpu); 345 } 346 347 /* 348 * Let the RCU core know that this CPU has gone through the scheduler, 349 * which is a quiescent state. This is called when the need for a 350 * quiescent state is urgent, so we burn an atomic operation and full 351 * memory barriers to let the RCU core know about it, regardless of what 352 * this CPU might (or might not) do in the near future. 353 * 354 * We inform the RCU core by emulating a zero-duration dyntick-idle period. 355 * 356 * The caller must have disabled interrupts and must not be idle. 357 */ 358 notrace void rcu_momentary_eqs(void) 359 { 360 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 361 int seq; 362 363 WRITE_ONCE(rdp->rcu_need_heavy_qs, false); 364 seq = ct_state_inc(2 * CT_RCU_WATCHING); 365 /* It is illegal to call this from idle state. */ 366 WARN_ON_ONCE(!(seq & CT_RCU_WATCHING)); 367 rcu_preempt_deferred_qs(current); 368 } 369 EXPORT_SYMBOL_GPL(rcu_momentary_eqs); 370 371 /** 372 * rcu_is_cpu_rrupt_from_idle - see if 'interrupted' from idle 373 * 374 * If the current CPU is idle and running at a first-level (not nested) 375 * interrupt, or directly, from idle, return true. 376 * 377 * The caller must have at least disabled IRQs. 378 */ 379 static int rcu_is_cpu_rrupt_from_idle(void) 380 { 381 long nmi_nesting = ct_nmi_nesting(); 382 383 /* 384 * Usually called from the tick; but also used from smp_function_call() 385 * for expedited grace periods. This latter can result in running from 386 * the idle task, instead of an actual IPI. 387 */ 388 lockdep_assert_irqs_disabled(); 389 390 /* Check for counter underflows */ 391 RCU_LOCKDEP_WARN(ct_nesting() < 0, 392 "RCU nesting counter underflow!"); 393 394 /* Non-idle interrupt or nested idle interrupt */ 395 if (nmi_nesting > 1) 396 return false; 397 398 /* 399 * Non nested idle interrupt (interrupting section where RCU 400 * wasn't watching). 401 */ 402 if (nmi_nesting == 1) 403 return true; 404 405 /* Not in an interrupt */ 406 if (!nmi_nesting) { 407 RCU_LOCKDEP_WARN(!in_task() || !is_idle_task(current), 408 "RCU nmi_nesting counter not in idle task!"); 409 return !rcu_is_watching_curr_cpu(); 410 } 411 412 RCU_LOCKDEP_WARN(1, "RCU nmi_nesting counter underflow/zero!"); 413 414 return false; 415 } 416 417 #define DEFAULT_RCU_BLIMIT (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) ? 1000 : 10) 418 // Maximum callbacks per rcu_do_batch ... 419 #define DEFAULT_MAX_RCU_BLIMIT 10000 // ... even during callback flood. 420 static long blimit = DEFAULT_RCU_BLIMIT; 421 #define DEFAULT_RCU_QHIMARK 10000 // If this many pending, ignore blimit. 422 static long qhimark = DEFAULT_RCU_QHIMARK; 423 #define DEFAULT_RCU_QLOMARK 100 // Once only this many pending, use blimit. 424 static long qlowmark = DEFAULT_RCU_QLOMARK; 425 #define DEFAULT_RCU_QOVLD_MULT 2 426 #define DEFAULT_RCU_QOVLD (DEFAULT_RCU_QOVLD_MULT * DEFAULT_RCU_QHIMARK) 427 static long qovld = DEFAULT_RCU_QOVLD; // If this many pending, hammer QS. 428 static long qovld_calc = -1; // No pre-initialization lock acquisitions! 429 430 module_param(blimit, long, 0444); 431 module_param(qhimark, long, 0444); 432 module_param(qlowmark, long, 0444); 433 module_param(qovld, long, 0444); 434 435 static ulong jiffies_till_first_fqs = IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) ? 0 : ULONG_MAX; 436 static ulong jiffies_till_next_fqs = ULONG_MAX; 437 static bool rcu_kick_kthreads; 438 static int rcu_divisor = 7; 439 module_param(rcu_divisor, int, 0644); 440 441 /* Force an exit from rcu_do_batch() after 3 milliseconds. */ 442 static long rcu_resched_ns = 3 * NSEC_PER_MSEC; 443 module_param(rcu_resched_ns, long, 0644); 444 445 /* 446 * How long the grace period must be before we start recruiting 447 * quiescent-state help from rcu_note_context_switch(). 448 */ 449 static ulong jiffies_till_sched_qs = ULONG_MAX; 450 module_param(jiffies_till_sched_qs, ulong, 0444); 451 static ulong jiffies_to_sched_qs; /* See adjust_jiffies_till_sched_qs(). */ 452 module_param(jiffies_to_sched_qs, ulong, 0444); /* Display only! */ 453 454 /* 455 * Make sure that we give the grace-period kthread time to detect any 456 * idle CPUs before taking active measures to force quiescent states. 457 * However, don't go below 100 milliseconds, adjusted upwards for really 458 * large systems. 459 */ 460 static void adjust_jiffies_till_sched_qs(void) 461 { 462 unsigned long j; 463 464 /* If jiffies_till_sched_qs was specified, respect the request. */ 465 if (jiffies_till_sched_qs != ULONG_MAX) { 466 WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs); 467 return; 468 } 469 /* Otherwise, set to third fqs scan, but bound below on large system. */ 470 j = READ_ONCE(jiffies_till_first_fqs) + 471 2 * READ_ONCE(jiffies_till_next_fqs); 472 if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV) 473 j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV; 474 pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j); 475 WRITE_ONCE(jiffies_to_sched_qs, j); 476 } 477 478 static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp) 479 { 480 ulong j; 481 int ret = kstrtoul(val, 0, &j); 482 483 if (!ret) { 484 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j); 485 adjust_jiffies_till_sched_qs(); 486 } 487 return ret; 488 } 489 490 static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp) 491 { 492 ulong j; 493 int ret = kstrtoul(val, 0, &j); 494 495 if (!ret) { 496 WRITE_ONCE(*(ulong *)kp->arg, clamp_val(j, 1, HZ)); 497 adjust_jiffies_till_sched_qs(); 498 } 499 return ret; 500 } 501 502 static const struct kernel_param_ops first_fqs_jiffies_ops = { 503 .set = param_set_first_fqs_jiffies, 504 .get = param_get_ulong, 505 }; 506 507 static const struct kernel_param_ops next_fqs_jiffies_ops = { 508 .set = param_set_next_fqs_jiffies, 509 .get = param_get_ulong, 510 }; 511 512 module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644); 513 module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644); 514 module_param(rcu_kick_kthreads, bool, 0644); 515 516 static void force_qs_rnp(int (*f)(struct rcu_data *rdp)); 517 static int rcu_pending(int user); 518 519 /* 520 * Return the number of RCU GPs completed thus far for debug & stats. 521 */ 522 unsigned long rcu_get_gp_seq(void) 523 { 524 return READ_ONCE(rcu_state.gp_seq); 525 } 526 EXPORT_SYMBOL_GPL(rcu_get_gp_seq); 527 528 /* 529 * Return the number of RCU expedited batches completed thus far for 530 * debug & stats. Odd numbers mean that a batch is in progress, even 531 * numbers mean idle. The value returned will thus be roughly double 532 * the cumulative batches since boot. 533 */ 534 unsigned long rcu_exp_batches_completed(void) 535 { 536 return rcu_state.expedited_sequence; 537 } 538 EXPORT_SYMBOL_GPL(rcu_exp_batches_completed); 539 540 /* 541 * Return the root node of the rcu_state structure. 542 */ 543 static struct rcu_node *rcu_get_root(void) 544 { 545 return &rcu_state.node[0]; 546 } 547 548 /* 549 * Send along grace-period-related data for rcutorture diagnostics. 550 */ 551 void rcutorture_get_gp_data(int *flags, unsigned long *gp_seq) 552 { 553 *flags = READ_ONCE(rcu_state.gp_flags); 554 *gp_seq = rcu_seq_current(&rcu_state.gp_seq); 555 } 556 EXPORT_SYMBOL_GPL(rcutorture_get_gp_data); 557 558 /* Gather grace-period sequence numbers for rcutorture diagnostics. */ 559 unsigned long long rcutorture_gather_gp_seqs(void) 560 { 561 return ((READ_ONCE(rcu_state.gp_seq) & 0xffffULL) << 40) | 562 ((READ_ONCE(rcu_state.expedited_sequence) & 0xffffffULL) << 16) | 563 (READ_ONCE(rcu_state.gp_seq_polled) & 0xffffULL); 564 } 565 EXPORT_SYMBOL_GPL(rcutorture_gather_gp_seqs); 566 567 /* Format grace-period sequence numbers for rcutorture diagnostics. */ 568 void rcutorture_format_gp_seqs(unsigned long long seqs, char *cp, size_t len) 569 { 570 unsigned int egp = (seqs >> 16) & 0xffffffULL; 571 unsigned int ggp = (seqs >> 40) & 0xffffULL; 572 unsigned int pgp = seqs & 0xffffULL; 573 574 snprintf(cp, len, "g%04x:e%06x:p%04x", ggp, egp, pgp); 575 } 576 EXPORT_SYMBOL_GPL(rcutorture_format_gp_seqs); 577 578 #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_VIRT_XFER_TO_GUEST_WORK)) 579 /* 580 * An empty function that will trigger a reschedule on 581 * IRQ tail once IRQs get re-enabled on userspace/guest resume. 582 */ 583 static void late_wakeup_func(struct irq_work *work) 584 { 585 } 586 587 static DEFINE_PER_CPU(struct irq_work, late_wakeup_work) = 588 IRQ_WORK_INIT(late_wakeup_func); 589 590 /* 591 * If either: 592 * 593 * 1) the task is about to enter in guest mode and $ARCH doesn't support KVM generic work 594 * 2) the task is about to enter in user mode and $ARCH doesn't support generic entry. 595 * 596 * In these cases the late RCU wake ups aren't supported in the resched loops and our 597 * last resort is to fire a local irq_work that will trigger a reschedule once IRQs 598 * get re-enabled again. 599 */ 600 noinstr void rcu_irq_work_resched(void) 601 { 602 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 603 604 if (IS_ENABLED(CONFIG_GENERIC_ENTRY) && !(current->flags & PF_VCPU)) 605 return; 606 607 if (IS_ENABLED(CONFIG_VIRT_XFER_TO_GUEST_WORK) && (current->flags & PF_VCPU)) 608 return; 609 610 instrumentation_begin(); 611 if (do_nocb_deferred_wakeup(rdp) && need_resched()) { 612 irq_work_queue(this_cpu_ptr(&late_wakeup_work)); 613 } 614 instrumentation_end(); 615 } 616 #endif /* #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_VIRT_XFER_TO_GUEST_WORK)) */ 617 618 #ifdef CONFIG_PROVE_RCU 619 /** 620 * rcu_irq_exit_check_preempt - Validate that scheduling is possible 621 */ 622 void rcu_irq_exit_check_preempt(void) 623 { 624 lockdep_assert_irqs_disabled(); 625 626 RCU_LOCKDEP_WARN(ct_nesting() <= 0, 627 "RCU nesting counter underflow/zero!"); 628 RCU_LOCKDEP_WARN(ct_nmi_nesting() != 629 CT_NESTING_IRQ_NONIDLE, 630 "Bad RCU nmi_nesting counter\n"); 631 RCU_LOCKDEP_WARN(!rcu_is_watching_curr_cpu(), 632 "RCU in extended quiescent state!"); 633 } 634 #endif /* #ifdef CONFIG_PROVE_RCU */ 635 636 #ifdef CONFIG_NO_HZ_FULL 637 /** 638 * __rcu_irq_enter_check_tick - Enable scheduler tick on CPU if RCU needs it. 639 * 640 * The scheduler tick is not normally enabled when CPUs enter the kernel 641 * from nohz_full userspace execution. After all, nohz_full userspace 642 * execution is an RCU quiescent state and the time executing in the kernel 643 * is quite short. Except of course when it isn't. And it is not hard to 644 * cause a large system to spend tens of seconds or even minutes looping 645 * in the kernel, which can cause a number of problems, include RCU CPU 646 * stall warnings. 647 * 648 * Therefore, if a nohz_full CPU fails to report a quiescent state 649 * in a timely manner, the RCU grace-period kthread sets that CPU's 650 * ->rcu_urgent_qs flag with the expectation that the next interrupt or 651 * exception will invoke this function, which will turn on the scheduler 652 * tick, which will enable RCU to detect that CPU's quiescent states, 653 * for example, due to cond_resched() calls in CONFIG_PREEMPT=n kernels. 654 * The tick will be disabled once a quiescent state is reported for 655 * this CPU. 656 * 657 * Of course, in carefully tuned systems, there might never be an 658 * interrupt or exception. In that case, the RCU grace-period kthread 659 * will eventually cause one to happen. However, in less carefully 660 * controlled environments, this function allows RCU to get what it 661 * needs without creating otherwise useless interruptions. 662 */ 663 void __rcu_irq_enter_check_tick(void) 664 { 665 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 666 667 // If we're here from NMI there's nothing to do. 668 if (in_nmi()) 669 return; 670 671 RCU_LOCKDEP_WARN(!rcu_is_watching_curr_cpu(), 672 "Illegal rcu_irq_enter_check_tick() from extended quiescent state"); 673 674 if (!tick_nohz_full_cpu(rdp->cpu) || 675 !READ_ONCE(rdp->rcu_urgent_qs) || 676 READ_ONCE(rdp->rcu_forced_tick)) { 677 // RCU doesn't need nohz_full help from this CPU, or it is 678 // already getting that help. 679 return; 680 } 681 682 // We get here only when not in an extended quiescent state and 683 // from interrupts (as opposed to NMIs). Therefore, (1) RCU is 684 // already watching and (2) The fact that we are in an interrupt 685 // handler and that the rcu_node lock is an irq-disabled lock 686 // prevents self-deadlock. So we can safely recheck under the lock. 687 // Note that the nohz_full state currently cannot change. 688 raw_spin_lock_rcu_node(rdp->mynode); 689 if (READ_ONCE(rdp->rcu_urgent_qs) && !rdp->rcu_forced_tick) { 690 // A nohz_full CPU is in the kernel and RCU needs a 691 // quiescent state. Turn on the tick! 692 WRITE_ONCE(rdp->rcu_forced_tick, true); 693 tick_dep_set_cpu(rdp->cpu, TICK_DEP_BIT_RCU); 694 } 695 raw_spin_unlock_rcu_node(rdp->mynode); 696 } 697 NOKPROBE_SYMBOL(__rcu_irq_enter_check_tick); 698 #endif /* CONFIG_NO_HZ_FULL */ 699 700 /* 701 * Check to see if any future non-offloaded RCU-related work will need 702 * to be done by the current CPU, even if none need be done immediately, 703 * returning 1 if so. This function is part of the RCU implementation; 704 * it is -not- an exported member of the RCU API. This is used by 705 * the idle-entry code to figure out whether it is safe to disable the 706 * scheduler-clock interrupt. 707 * 708 * Just check whether or not this CPU has non-offloaded RCU callbacks 709 * queued. 710 */ 711 int rcu_needs_cpu(void) 712 { 713 return !rcu_segcblist_empty(&this_cpu_ptr(&rcu_data)->cblist) && 714 !rcu_rdp_is_offloaded(this_cpu_ptr(&rcu_data)); 715 } 716 717 /* 718 * If any sort of urgency was applied to the current CPU (for example, 719 * the scheduler-clock interrupt was enabled on a nohz_full CPU) in order 720 * to get to a quiescent state, disable it. 721 */ 722 static void rcu_disable_urgency_upon_qs(struct rcu_data *rdp) 723 { 724 raw_lockdep_assert_held_rcu_node(rdp->mynode); 725 WRITE_ONCE(rdp->rcu_urgent_qs, false); 726 WRITE_ONCE(rdp->rcu_need_heavy_qs, false); 727 if (tick_nohz_full_cpu(rdp->cpu) && rdp->rcu_forced_tick) { 728 tick_dep_clear_cpu(rdp->cpu, TICK_DEP_BIT_RCU); 729 WRITE_ONCE(rdp->rcu_forced_tick, false); 730 } 731 } 732 733 /** 734 * rcu_is_watching - RCU read-side critical sections permitted on current CPU? 735 * 736 * Return @true if RCU is watching the running CPU and @false otherwise. 737 * An @true return means that this CPU can safely enter RCU read-side 738 * critical sections. 739 * 740 * Although calls to rcu_is_watching() from most parts of the kernel 741 * will return @true, there are important exceptions. For example, if the 742 * current CPU is deep within its idle loop, in kernel entry/exit code, 743 * or offline, rcu_is_watching() will return @false. 744 * 745 * Make notrace because it can be called by the internal functions of 746 * ftrace, and making this notrace removes unnecessary recursion calls. 747 */ 748 notrace bool rcu_is_watching(void) 749 { 750 bool ret; 751 752 preempt_disable_notrace(); 753 ret = rcu_is_watching_curr_cpu(); 754 preempt_enable_notrace(); 755 return ret; 756 } 757 EXPORT_SYMBOL_GPL(rcu_is_watching); 758 759 /* 760 * If a holdout task is actually running, request an urgent quiescent 761 * state from its CPU. This is unsynchronized, so migrations can cause 762 * the request to go to the wrong CPU. Which is OK, all that will happen 763 * is that the CPU's next context switch will be a bit slower and next 764 * time around this task will generate another request. 765 */ 766 void rcu_request_urgent_qs_task(struct task_struct *t) 767 { 768 int cpu; 769 770 barrier(); 771 cpu = task_cpu(t); 772 if (!task_curr(t)) 773 return; /* This task is not running on that CPU. */ 774 smp_store_release(per_cpu_ptr(&rcu_data.rcu_urgent_qs, cpu), true); 775 } 776 777 static unsigned long seq_gpwrap_lag = ULONG_MAX / 4; 778 779 /** 780 * rcu_set_gpwrap_lag - Set RCU GP sequence overflow lag value. 781 * @lag_gps: Set overflow lag to this many grace period worth of counters 782 * which is used by rcutorture to quickly force a gpwrap situation. 783 * @lag_gps = 0 means we reset it back to the boot-time value. 784 */ 785 void rcu_set_gpwrap_lag(unsigned long lag_gps) 786 { 787 unsigned long lag_seq_count; 788 789 lag_seq_count = (lag_gps == 0) 790 ? ULONG_MAX / 4 791 : lag_gps << RCU_SEQ_CTR_SHIFT; 792 WRITE_ONCE(seq_gpwrap_lag, lag_seq_count); 793 } 794 EXPORT_SYMBOL_GPL(rcu_set_gpwrap_lag); 795 796 /* 797 * When trying to report a quiescent state on behalf of some other CPU, 798 * it is our responsibility to check for and handle potential overflow 799 * of the rcu_node ->gp_seq counter with respect to the rcu_data counters. 800 * After all, the CPU might be in deep idle state, and thus executing no 801 * code whatsoever. 802 */ 803 static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp) 804 { 805 raw_lockdep_assert_held_rcu_node(rnp); 806 if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + seq_gpwrap_lag, 807 rnp->gp_seq)) { 808 WRITE_ONCE(rdp->gpwrap, true); 809 WRITE_ONCE(rdp->gpwrap_count, READ_ONCE(rdp->gpwrap_count) + 1); 810 } 811 if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq)) 812 rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4; 813 } 814 815 /* 816 * Snapshot the specified CPU's RCU_WATCHING counter so that we can later 817 * credit them with an implicit quiescent state. Return 1 if this CPU 818 * is in dynticks idle mode, which is an extended quiescent state. 819 */ 820 static int rcu_watching_snap_save(struct rcu_data *rdp) 821 { 822 /* 823 * Full ordering between remote CPU's post idle accesses and updater's 824 * accesses prior to current GP (and also the started GP sequence number) 825 * is enforced by rcu_seq_start() implicit barrier and even further by 826 * smp_mb__after_unlock_lock() barriers chained all the way throughout the 827 * rnp locking tree since rcu_gp_init() and up to the current leaf rnp 828 * locking. 829 * 830 * Ordering between remote CPU's pre idle accesses and post grace period 831 * updater's accesses is enforced by the below acquire semantic. 832 */ 833 rdp->watching_snap = ct_rcu_watching_cpu_acquire(rdp->cpu); 834 if (rcu_watching_snap_in_eqs(rdp->watching_snap)) { 835 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti")); 836 rcu_gpnum_ovf(rdp->mynode, rdp); 837 return 1; 838 } 839 return 0; 840 } 841 842 #ifndef arch_irq_stat_cpu 843 #define arch_irq_stat_cpu(cpu) 0 844 #endif 845 846 /* 847 * Returns positive if the specified CPU has passed through a quiescent state 848 * by virtue of being in or having passed through an dynticks idle state since 849 * the last call to rcu_watching_snap_save() for this same CPU, or by 850 * virtue of having been offline. 851 * 852 * Returns negative if the specified CPU needs a force resched. 853 * 854 * Returns zero otherwise. 855 */ 856 static int rcu_watching_snap_recheck(struct rcu_data *rdp) 857 { 858 unsigned long jtsq; 859 int ret = 0; 860 struct rcu_node *rnp = rdp->mynode; 861 862 /* 863 * If the CPU passed through or entered a dynticks idle phase with 864 * no active irq/NMI handlers, then we can safely pretend that the CPU 865 * already acknowledged the request to pass through a quiescent 866 * state. Either way, that CPU cannot possibly be in an RCU 867 * read-side critical section that started before the beginning 868 * of the current RCU grace period. 869 */ 870 if (rcu_watching_snap_stopped_since(rdp, rdp->watching_snap)) { 871 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti")); 872 rcu_gpnum_ovf(rnp, rdp); 873 return 1; 874 } 875 876 /* 877 * Complain if a CPU that is considered to be offline from RCU's 878 * perspective has not yet reported a quiescent state. After all, 879 * the offline CPU should have reported a quiescent state during 880 * the CPU-offline process, or, failing that, by rcu_gp_init() 881 * if it ran concurrently with either the CPU going offline or the 882 * last task on a leaf rcu_node structure exiting its RCU read-side 883 * critical section while all CPUs corresponding to that structure 884 * are offline. This added warning detects bugs in any of these 885 * code paths. 886 * 887 * The rcu_node structure's ->lock is held here, which excludes 888 * the relevant portions the CPU-hotplug code, the grace-period 889 * initialization code, and the rcu_read_unlock() code paths. 890 * 891 * For more detail, please refer to the "Hotplug CPU" section 892 * of RCU's Requirements documentation. 893 */ 894 if (WARN_ON_ONCE(!rcu_rdp_cpu_online(rdp))) { 895 struct rcu_node *rnp1; 896 897 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n", 898 __func__, rnp->grplo, rnp->grphi, rnp->level, 899 (long)rnp->gp_seq, (long)rnp->completedqs); 900 for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent) 901 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n", 902 __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask); 903 pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n", 904 __func__, rdp->cpu, ".o"[rcu_rdp_cpu_online(rdp)], 905 (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_state, 906 (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_state); 907 return 1; /* Break things loose after complaining. */ 908 } 909 910 /* 911 * A CPU running for an extended time within the kernel can 912 * delay RCU grace periods: (1) At age jiffies_to_sched_qs, 913 * set .rcu_urgent_qs, (2) At age 2*jiffies_to_sched_qs, set 914 * both .rcu_need_heavy_qs and .rcu_urgent_qs. Note that the 915 * unsynchronized assignments to the per-CPU rcu_need_heavy_qs 916 * variable are safe because the assignments are repeated if this 917 * CPU failed to pass through a quiescent state. This code 918 * also checks .jiffies_resched in case jiffies_to_sched_qs 919 * is set way high. 920 */ 921 jtsq = READ_ONCE(jiffies_to_sched_qs); 922 if (!READ_ONCE(rdp->rcu_need_heavy_qs) && 923 (time_after(jiffies, rcu_state.gp_start + jtsq * 2) || 924 time_after(jiffies, rcu_state.jiffies_resched) || 925 rcu_state.cbovld)) { 926 WRITE_ONCE(rdp->rcu_need_heavy_qs, true); 927 /* Store rcu_need_heavy_qs before rcu_urgent_qs. */ 928 smp_store_release(&rdp->rcu_urgent_qs, true); 929 } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) { 930 WRITE_ONCE(rdp->rcu_urgent_qs, true); 931 } 932 933 /* 934 * NO_HZ_FULL CPUs can run in-kernel without rcu_sched_clock_irq! 935 * The above code handles this, but only for straight cond_resched(). 936 * And some in-kernel loops check need_resched() before calling 937 * cond_resched(), which defeats the above code for CPUs that are 938 * running in-kernel with scheduling-clock interrupts disabled. 939 * So hit them over the head with the resched_cpu() hammer! 940 */ 941 if (tick_nohz_full_cpu(rdp->cpu) && 942 (time_after(jiffies, READ_ONCE(rdp->last_fqs_resched) + jtsq * 3) || 943 rcu_state.cbovld)) { 944 WRITE_ONCE(rdp->rcu_urgent_qs, true); 945 WRITE_ONCE(rdp->last_fqs_resched, jiffies); 946 ret = -1; 947 } 948 949 /* 950 * If more than halfway to RCU CPU stall-warning time, invoke 951 * resched_cpu() more frequently to try to loosen things up a bit. 952 * Also check to see if the CPU is getting hammered with interrupts, 953 * but only once per grace period, just to keep the IPIs down to 954 * a dull roar. 955 */ 956 if (time_after(jiffies, rcu_state.jiffies_resched)) { 957 if (time_after(jiffies, 958 READ_ONCE(rdp->last_fqs_resched) + jtsq)) { 959 WRITE_ONCE(rdp->last_fqs_resched, jiffies); 960 ret = -1; 961 } 962 if (IS_ENABLED(CONFIG_IRQ_WORK) && 963 !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq && 964 (rnp->ffmask & rdp->grpmask)) { 965 rdp->rcu_iw_pending = true; 966 rdp->rcu_iw_gp_seq = rnp->gp_seq; 967 irq_work_queue_on(&rdp->rcu_iw, rdp->cpu); 968 } 969 970 if (rcu_cpu_stall_cputime && rdp->snap_record.gp_seq != rdp->gp_seq) { 971 int cpu = rdp->cpu; 972 struct rcu_snap_record *rsrp; 973 974 rsrp = &rdp->snap_record; 975 rsrp->cputime_irq = kcpustat_field(CPUTIME_IRQ, cpu); 976 rsrp->cputime_softirq = kcpustat_field(CPUTIME_SOFTIRQ, cpu); 977 rsrp->cputime_system = kcpustat_field(CPUTIME_SYSTEM, cpu); 978 rsrp->nr_hardirqs = kstat_cpu_irqs_sum(cpu) + arch_irq_stat_cpu(cpu); 979 rsrp->nr_softirqs = kstat_cpu_softirqs_sum(cpu); 980 rsrp->nr_csw = nr_context_switches_cpu(cpu); 981 rsrp->jiffies = jiffies; 982 rsrp->gp_seq = rdp->gp_seq; 983 } 984 } 985 986 return ret; 987 } 988 989 /* Trace-event wrapper function for trace_rcu_future_grace_period. */ 990 static void trace_rcu_this_gp(struct rcu_node *rnp, unsigned long gp_seq_req, 991 const char *s) 992 { 993 trace_rcu_future_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq), 994 gp_seq_req, rnp->level, 995 rnp->grplo, rnp->grphi, s); 996 } 997 998 /* 999 * rcu_start_this_gp - Request the start of a particular grace period 1000 * @rnp_start: The leaf node of the CPU from which to start. 1001 * @rdp: The rcu_data corresponding to the CPU from which to start. 1002 * @gp_seq_req: The gp_seq of the grace period to start. 1003 * 1004 * Start the specified grace period, as needed to handle newly arrived 1005 * callbacks. The required future grace periods are recorded in each 1006 * rcu_node structure's ->gp_seq_needed field. Returns true if there 1007 * is reason to awaken the grace-period kthread. 1008 * 1009 * The caller must hold the specified rcu_node structure's ->lock, which 1010 * is why the caller is responsible for waking the grace-period kthread. 1011 * 1012 * Returns true if the GP thread needs to be awakened else false. 1013 */ 1014 static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp, 1015 unsigned long gp_seq_req) 1016 { 1017 bool ret = false; 1018 struct rcu_node *rnp; 1019 1020 /* 1021 * Use funnel locking to either acquire the root rcu_node 1022 * structure's lock or bail out if the need for this grace period 1023 * has already been recorded -- or if that grace period has in 1024 * fact already started. If there is already a grace period in 1025 * progress in a non-leaf node, no recording is needed because the 1026 * end of the grace period will scan the leaf rcu_node structures. 1027 * Note that rnp_start->lock must not be released. 1028 */ 1029 raw_lockdep_assert_held_rcu_node(rnp_start); 1030 trace_rcu_this_gp(rnp_start, gp_seq_req, TPS("Startleaf")); 1031 for (rnp = rnp_start; 1; rnp = rnp->parent) { 1032 if (rnp != rnp_start) 1033 raw_spin_lock_rcu_node(rnp); 1034 if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) || 1035 rcu_seq_started(&rnp->gp_seq, gp_seq_req) || 1036 (rnp != rnp_start && 1037 rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) { 1038 trace_rcu_this_gp(rnp, gp_seq_req, TPS("Prestarted")); 1039 goto unlock_out; 1040 } 1041 WRITE_ONCE(rnp->gp_seq_needed, gp_seq_req); 1042 if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) { 1043 /* 1044 * We just marked the leaf or internal node, and a 1045 * grace period is in progress, which means that 1046 * rcu_gp_cleanup() will see the marking. Bail to 1047 * reduce contention. 1048 */ 1049 trace_rcu_this_gp(rnp_start, gp_seq_req, 1050 TPS("Startedleaf")); 1051 goto unlock_out; 1052 } 1053 if (rnp != rnp_start && rnp->parent != NULL) 1054 raw_spin_unlock_rcu_node(rnp); 1055 if (!rnp->parent) 1056 break; /* At root, and perhaps also leaf. */ 1057 } 1058 1059 /* If GP already in progress, just leave, otherwise start one. */ 1060 if (rcu_gp_in_progress()) { 1061 trace_rcu_this_gp(rnp, gp_seq_req, TPS("Startedleafroot")); 1062 goto unlock_out; 1063 } 1064 trace_rcu_this_gp(rnp, gp_seq_req, TPS("Startedroot")); 1065 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT); 1066 WRITE_ONCE(rcu_state.gp_req_activity, jiffies); 1067 if (!READ_ONCE(rcu_state.gp_kthread)) { 1068 trace_rcu_this_gp(rnp, gp_seq_req, TPS("NoGPkthread")); 1069 goto unlock_out; 1070 } 1071 trace_rcu_grace_period(rcu_state.name, data_race(rcu_state.gp_seq), TPS("newreq")); 1072 ret = true; /* Caller must wake GP kthread. */ 1073 unlock_out: 1074 /* Push furthest requested GP to leaf node and rcu_data structure. */ 1075 if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) { 1076 WRITE_ONCE(rnp_start->gp_seq_needed, rnp->gp_seq_needed); 1077 WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed); 1078 } 1079 if (rnp != rnp_start) 1080 raw_spin_unlock_rcu_node(rnp); 1081 return ret; 1082 } 1083 1084 /* 1085 * Clean up any old requests for the just-ended grace period. Also return 1086 * whether any additional grace periods have been requested. 1087 */ 1088 static bool rcu_future_gp_cleanup(struct rcu_node *rnp) 1089 { 1090 bool needmore; 1091 1092 needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed); 1093 if (!needmore) 1094 rnp->gp_seq_needed = rnp->gp_seq; /* Avoid counter wrap. */ 1095 trace_rcu_this_gp(rnp, rnp->gp_seq, 1096 needmore ? TPS("CleanupMore") : TPS("Cleanup")); 1097 return needmore; 1098 } 1099 1100 /* 1101 * Awaken the grace-period kthread. Don't do a self-awaken (unless in an 1102 * interrupt or softirq handler, in which case we just might immediately 1103 * sleep upon return, resulting in a grace-period hang), and don't bother 1104 * awakening when there is nothing for the grace-period kthread to do 1105 * (as in several CPUs raced to awaken, we lost), and finally don't try 1106 * to awaken a kthread that has not yet been created. If all those checks 1107 * are passed, track some debug information and awaken. 1108 * 1109 * So why do the self-wakeup when in an interrupt or softirq handler 1110 * in the grace-period kthread's context? Because the kthread might have 1111 * been interrupted just as it was going to sleep, and just after the final 1112 * pre-sleep check of the awaken condition. In this case, a wakeup really 1113 * is required, and is therefore supplied. 1114 */ 1115 static void rcu_gp_kthread_wake(void) 1116 { 1117 struct task_struct *t = READ_ONCE(rcu_state.gp_kthread); 1118 1119 if ((current == t && !in_hardirq() && !in_serving_softirq()) || 1120 !READ_ONCE(rcu_state.gp_flags) || !t) 1121 return; 1122 WRITE_ONCE(rcu_state.gp_wake_time, jiffies); 1123 WRITE_ONCE(rcu_state.gp_wake_seq, READ_ONCE(rcu_state.gp_seq)); 1124 swake_up_one(&rcu_state.gp_wq); 1125 } 1126 1127 /* 1128 * If there is room, assign a ->gp_seq number to any callbacks on this 1129 * CPU that have not already been assigned. Also accelerate any callbacks 1130 * that were previously assigned a ->gp_seq number that has since proven 1131 * to be too conservative, which can happen if callbacks get assigned a 1132 * ->gp_seq number while RCU is idle, but with reference to a non-root 1133 * rcu_node structure. This function is idempotent, so it does not hurt 1134 * to call it repeatedly. Returns an flag saying that we should awaken 1135 * the RCU grace-period kthread. 1136 * 1137 * The caller must hold rnp->lock with interrupts disabled. 1138 */ 1139 static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp) 1140 { 1141 struct rcu_gp_seq gs; 1142 bool ret = false; 1143 1144 rcu_lockdep_assert_cblist_protected(rdp); 1145 raw_lockdep_assert_held_rcu_node(rnp); 1146 1147 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */ 1148 if (!rcu_segcblist_pend_cbs(&rdp->cblist)) 1149 return false; 1150 1151 trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPreAcc")); 1152 1153 /* 1154 * Callbacks are often registered with incomplete grace-period 1155 * information. Something about the fact that getting exact 1156 * information requires acquiring a global lock... RCU therefore 1157 * makes a conservative estimate of the grace period number at which 1158 * a given callback will become ready to invoke. The following 1159 * code checks this estimate and improves it when possible, thus 1160 * accelerating callback invocation to an earlier grace-period 1161 * number. 1162 */ 1163 get_state_synchronize_rcu_full(&gs); 1164 if (rcu_segcblist_accelerate(&rdp->cblist, &gs)) 1165 ret = rcu_start_this_gp(rnp, rdp, gs.norm); 1166 1167 /* Trace depending on how much we were able to accelerate. */ 1168 if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL)) 1169 trace_rcu_grace_period(rcu_state.name, gs.norm, TPS("AccWaitCB")); 1170 else 1171 trace_rcu_grace_period(rcu_state.name, gs.norm, TPS("AccReadyCB")); 1172 1173 trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbPostAcc")); 1174 1175 return ret; 1176 } 1177 1178 /* 1179 * Similar to rcu_accelerate_cbs(), but does not require that the leaf 1180 * rcu_node structure's ->lock be held. It consults the cached value 1181 * of ->gp_seq_needed in the rcu_data structure, and if that indicates 1182 * that a new grace-period request be made, invokes rcu_accelerate_cbs() 1183 * while holding the leaf rcu_node structure's ->lock. 1184 */ 1185 static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp, 1186 struct rcu_data *rdp) 1187 { 1188 struct rcu_gp_seq gs; 1189 bool needwake; 1190 1191 rcu_lockdep_assert_cblist_protected(rdp); 1192 get_state_synchronize_rcu_full(&gs); 1193 if (!READ_ONCE(rdp->gpwrap) && ULONG_CMP_GE(rdp->gp_seq_needed, gs.norm)) { 1194 /* Old request still live, so mark recent callbacks. */ 1195 (void)rcu_segcblist_accelerate(&rdp->cblist, &gs); 1196 return; 1197 } 1198 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */ 1199 needwake = rcu_accelerate_cbs(rnp, rdp); 1200 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */ 1201 if (needwake) 1202 rcu_gp_kthread_wake(); 1203 } 1204 1205 /* 1206 * Move any callbacks whose grace period has completed to the 1207 * RCU_DONE_TAIL sublist, then compact the remaining sublists and 1208 * assign ->gp_seq[] state to any callbacks in the RCU_NEXT_TAIL 1209 * sublist. This function is idempotent, so it does not hurt to 1210 * invoke it repeatedly. As long as it is not invoked -too- often... 1211 * Returns true if the RCU grace-period kthread needs to be awakened. 1212 * 1213 * The caller must hold rnp->lock with interrupts disabled. 1214 */ 1215 static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp) 1216 { 1217 rcu_lockdep_assert_cblist_protected(rdp); 1218 raw_lockdep_assert_held_rcu_node(rnp); 1219 1220 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */ 1221 if (!rcu_segcblist_pend_cbs(&rdp->cblist)) 1222 return false; 1223 1224 /* 1225 * Find all callbacks whose grace periods have completed (either 1226 * normal or expedited) and put them into the RCU_DONE_TAIL sublist. 1227 */ 1228 rcu_segcblist_advance(&rdp->cblist); 1229 1230 /* Classify any remaining callbacks. */ 1231 return rcu_accelerate_cbs(rnp, rdp); 1232 } 1233 1234 /* 1235 * Move and classify callbacks, but only if doing so won't require 1236 * that the RCU grace-period kthread be awakened. 1237 */ 1238 static void __maybe_unused rcu_advance_cbs_nowake(struct rcu_node *rnp, 1239 struct rcu_data *rdp) 1240 { 1241 rcu_lockdep_assert_cblist_protected(rdp); 1242 if (!rcu_seq_state(rcu_seq_current(&rnp->gp_seq)) || !raw_spin_trylock_rcu_node(rnp)) 1243 return; 1244 // The grace period cannot end while we hold the rcu_node lock. 1245 if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) 1246 WARN_ON_ONCE(rcu_advance_cbs(rnp, rdp)); 1247 raw_spin_unlock_rcu_node(rnp); 1248 } 1249 1250 /* 1251 * In CONFIG_RCU_STRICT_GRACE_PERIOD=y kernels, attempt to generate a 1252 * quiescent state. This is intended to be invoked when the CPU notices 1253 * a new grace period. 1254 */ 1255 static void rcu_strict_gp_check_qs(void) 1256 { 1257 if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) { 1258 rcu_read_lock(); 1259 rcu_read_unlock(); 1260 } 1261 } 1262 1263 /* 1264 * Update CPU-local rcu_data state to record the beginnings and ends of 1265 * grace periods. The caller must hold the ->lock of the leaf rcu_node 1266 * structure corresponding to the current CPU, and must have irqs disabled. 1267 * Returns true if the grace-period kthread needs to be awakened. 1268 */ 1269 static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp) 1270 { 1271 bool ret = false; 1272 bool need_qs; 1273 const bool offloaded = rcu_rdp_is_offloaded(rdp); 1274 1275 raw_lockdep_assert_held_rcu_node(rnp); 1276 1277 if (rdp->gp_seq == rnp->gp_seq) 1278 return false; /* Nothing to do. */ 1279 1280 /* Handle the ends of any preceding grace periods first. */ 1281 if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) || 1282 unlikely(rdp->gpwrap)) { 1283 if (!offloaded) 1284 ret = rcu_advance_cbs(rnp, rdp); /* Advance CBs. */ 1285 rdp->core_needs_qs = false; 1286 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend")); 1287 } else { 1288 if (!offloaded) 1289 ret = rcu_accelerate_cbs(rnp, rdp); /* Recent CBs. */ 1290 if (rdp->core_needs_qs) 1291 rdp->core_needs_qs = !!(rnp->qsmask & rdp->grpmask); 1292 } 1293 1294 /* Now handle the beginnings of any new-to-this-CPU grace periods. */ 1295 if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) || 1296 unlikely(rdp->gpwrap)) { 1297 /* 1298 * If the current grace period is waiting for this CPU, 1299 * set up to detect a quiescent state, otherwise don't 1300 * go looking for one. 1301 */ 1302 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart")); 1303 need_qs = !!(rnp->qsmask & rdp->grpmask); 1304 rdp->cpu_no_qs.b.norm = need_qs; 1305 rdp->core_needs_qs = need_qs; 1306 zero_cpu_stall_ticks(rdp); 1307 } 1308 rdp->gp_seq = rnp->gp_seq; /* Remember new grace-period state. */ 1309 if (ULONG_CMP_LT(rdp->gp_seq_needed, rnp->gp_seq_needed) || rdp->gpwrap) 1310 WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed); 1311 if (IS_ENABLED(CONFIG_PROVE_RCU) && rdp->gpwrap) 1312 WRITE_ONCE(rdp->last_sched_clock, jiffies); 1313 WRITE_ONCE(rdp->gpwrap, false); 1314 rcu_gpnum_ovf(rnp, rdp); 1315 return ret; 1316 } 1317 1318 static void note_gp_changes(struct rcu_data *rdp) 1319 { 1320 unsigned long flags; 1321 bool needwake; 1322 struct rcu_node *rnp; 1323 1324 local_irq_save(flags); 1325 rnp = rdp->mynode; 1326 if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) && 1327 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */ 1328 !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */ 1329 local_irq_restore(flags); 1330 return; 1331 } 1332 needwake = __note_gp_changes(rnp, rdp); 1333 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 1334 rcu_strict_gp_check_qs(); 1335 if (needwake) 1336 rcu_gp_kthread_wake(); 1337 } 1338 1339 static atomic_t *rcu_gp_slow_suppress; 1340 1341 /* Register a counter to suppress debugging grace-period delays. */ 1342 void rcu_gp_slow_register(atomic_t *rgssp) 1343 { 1344 WARN_ON_ONCE(rcu_gp_slow_suppress); 1345 1346 WRITE_ONCE(rcu_gp_slow_suppress, rgssp); 1347 } 1348 EXPORT_SYMBOL_GPL(rcu_gp_slow_register); 1349 1350 /* Unregister a counter, with NULL for not caring which. */ 1351 void rcu_gp_slow_unregister(atomic_t *rgssp) 1352 { 1353 WARN_ON_ONCE(rgssp && rgssp != rcu_gp_slow_suppress && rcu_gp_slow_suppress != NULL); 1354 1355 WRITE_ONCE(rcu_gp_slow_suppress, NULL); 1356 } 1357 EXPORT_SYMBOL_GPL(rcu_gp_slow_unregister); 1358 1359 static bool rcu_gp_slow_is_suppressed(void) 1360 { 1361 atomic_t *rgssp = READ_ONCE(rcu_gp_slow_suppress); 1362 1363 return rgssp && atomic_read(rgssp); 1364 } 1365 1366 static void rcu_gp_slow(int delay) 1367 { 1368 if (!rcu_gp_slow_is_suppressed() && delay > 0 && 1369 !(rcu_seq_ctr(rcu_state.gp_seq) % (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay))) 1370 schedule_timeout_idle(delay); 1371 } 1372 1373 static unsigned long sleep_duration; 1374 1375 /* Allow rcutorture to stall the grace-period kthread. */ 1376 void rcu_gp_set_torture_wait(int duration) 1377 { 1378 if (IS_ENABLED(CONFIG_RCU_TORTURE_TEST) && duration > 0) 1379 WRITE_ONCE(sleep_duration, duration); 1380 } 1381 EXPORT_SYMBOL_GPL(rcu_gp_set_torture_wait); 1382 1383 /* Actually implement the aforementioned wait. */ 1384 static void rcu_gp_torture_wait(void) 1385 { 1386 unsigned long duration; 1387 1388 if (!IS_ENABLED(CONFIG_RCU_TORTURE_TEST)) 1389 return; 1390 duration = xchg(&sleep_duration, 0UL); 1391 if (duration > 0) { 1392 pr_alert("%s: Waiting %lu jiffies\n", __func__, duration); 1393 schedule_timeout_idle(duration); 1394 pr_alert("%s: Wait complete\n", __func__); 1395 } 1396 } 1397 1398 /* 1399 * Handler for on_each_cpu() to invoke the target CPU's RCU core 1400 * processing. 1401 */ 1402 static void rcu_strict_gp_boundary(void *unused) 1403 { 1404 invoke_rcu_core(); 1405 } 1406 1407 // Make the polled API aware of the beginning of a grace period. 1408 static void rcu_poll_gp_seq_start(unsigned long *snap) 1409 { 1410 struct rcu_node *rnp = rcu_get_root(); 1411 1412 if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE) 1413 raw_lockdep_assert_held_rcu_node(rnp); 1414 1415 // If RCU was idle, note beginning of GP. 1416 if (!rcu_seq_state(rcu_state.gp_seq_polled)) 1417 rcu_seq_start(&rcu_state.gp_seq_polled); 1418 1419 // Either way, record current state. 1420 *snap = rcu_state.gp_seq_polled; 1421 } 1422 1423 // Make the polled API aware of the end of a grace period. 1424 static void rcu_poll_gp_seq_end(unsigned long *snap) 1425 { 1426 struct rcu_node *rnp = rcu_get_root(); 1427 1428 if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE) 1429 raw_lockdep_assert_held_rcu_node(rnp); 1430 1431 // If the previously noted GP is still in effect, record the 1432 // end of that GP. Either way, zero counter to avoid counter-wrap 1433 // problems. 1434 if (*snap && *snap == rcu_state.gp_seq_polled) { 1435 rcu_seq_end(&rcu_state.gp_seq_polled); 1436 rcu_state.gp_seq_polled_snap = 0; 1437 rcu_state.gp_seq_polled_exp_snap = 0; 1438 } else { 1439 *snap = 0; 1440 } 1441 } 1442 1443 // Make the polled API aware of the beginning of a grace period, but 1444 // where caller does not hold the root rcu_node structure's lock. 1445 static void rcu_poll_gp_seq_start_unlocked(unsigned long *snap) 1446 { 1447 unsigned long flags; 1448 struct rcu_node *rnp = rcu_get_root(); 1449 1450 if (rcu_init_invoked()) { 1451 if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE) 1452 lockdep_assert_irqs_enabled(); 1453 raw_spin_lock_irqsave_rcu_node(rnp, flags); 1454 } 1455 rcu_poll_gp_seq_start(snap); 1456 if (rcu_init_invoked()) 1457 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 1458 } 1459 1460 // Make the polled API aware of the end of a grace period, but where 1461 // caller does not hold the root rcu_node structure's lock. 1462 static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) 1463 { 1464 unsigned long flags; 1465 struct rcu_node *rnp = rcu_get_root(); 1466 1467 if (rcu_init_invoked()) { 1468 if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE) 1469 lockdep_assert_irqs_enabled(); 1470 raw_spin_lock_irqsave_rcu_node(rnp, flags); 1471 } 1472 rcu_poll_gp_seq_end(snap); 1473 if (rcu_init_invoked()) 1474 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 1475 } 1476 1477 /* 1478 * There is a single llist, which is used for handling 1479 * synchronize_rcu() users' enqueued rcu_synchronize nodes. 1480 * Within this llist, there are two tail pointers: 1481 * 1482 * wait tail: Tracks the set of nodes, which need to 1483 * wait for the current GP to complete. 1484 * done tail: Tracks the set of nodes, for which grace 1485 * period has elapsed. These nodes processing 1486 * will be done as part of the cleanup work 1487 * execution by a kworker. 1488 * 1489 * At every grace period init, a new wait node is added 1490 * to the llist. This wait node is used as wait tail 1491 * for this new grace period. Given that there are a fixed 1492 * number of wait nodes, if all wait nodes are in use 1493 * (which can happen when kworker callback processing 1494 * is delayed) and additional grace period is requested. 1495 * This means, a system is slow in processing callbacks. 1496 * 1497 * TODO: If a slow processing is detected, a first node 1498 * in the llist should be used as a wait-tail for this 1499 * grace period, therefore users which should wait due 1500 * to a slow process are handled by _this_ grace period 1501 * and not next. 1502 * 1503 * Below is an illustration of how the done and wait 1504 * tail pointers move from one set of rcu_synchronize nodes 1505 * to the other, as grace periods start and finish and 1506 * nodes are processed by kworker. 1507 * 1508 * 1509 * a. Initial llist callbacks list: 1510 * 1511 * +----------+ +--------+ +-------+ 1512 * | | | | | | 1513 * | head |---------> | cb2 |--------->| cb1 | 1514 * | | | | | | 1515 * +----------+ +--------+ +-------+ 1516 * 1517 * 1518 * 1519 * b. New GP1 Start: 1520 * 1521 * WAIT TAIL 1522 * | 1523 * | 1524 * v 1525 * +----------+ +--------+ +--------+ +-------+ 1526 * | | | | | | | | 1527 * | head ------> wait |------> cb2 |------> | cb1 | 1528 * | | | head1 | | | | | 1529 * +----------+ +--------+ +--------+ +-------+ 1530 * 1531 * 1532 * 1533 * c. GP completion: 1534 * 1535 * WAIT_TAIL == DONE_TAIL 1536 * 1537 * DONE TAIL 1538 * | 1539 * | 1540 * v 1541 * +----------+ +--------+ +--------+ +-------+ 1542 * | | | | | | | | 1543 * | head ------> wait |------> cb2 |------> | cb1 | 1544 * | | | head1 | | | | | 1545 * +----------+ +--------+ +--------+ +-------+ 1546 * 1547 * 1548 * 1549 * d. New callbacks and GP2 start: 1550 * 1551 * WAIT TAIL DONE TAIL 1552 * | | 1553 * | | 1554 * v v 1555 * +----------+ +------+ +------+ +------+ +-----+ +-----+ +-----+ 1556 * | | | | | | | | | | | | | | 1557 * | head ------> wait |--->| cb4 |--->| cb3 |--->|wait |--->| cb2 |--->| cb1 | 1558 * | | | head2| | | | | |head1| | | | | 1559 * +----------+ +------+ +------+ +------+ +-----+ +-----+ +-----+ 1560 * 1561 * 1562 * 1563 * e. GP2 completion: 1564 * 1565 * WAIT_TAIL == DONE_TAIL 1566 * DONE TAIL 1567 * | 1568 * | 1569 * v 1570 * +----------+ +------+ +------+ +------+ +-----+ +-----+ +-----+ 1571 * | | | | | | | | | | | | | | 1572 * | head ------> wait |--->| cb4 |--->| cb3 |--->|wait |--->| cb2 |--->| cb1 | 1573 * | | | head2| | | | | |head1| | | | | 1574 * +----------+ +------+ +------+ +------+ +-----+ +-----+ +-----+ 1575 * 1576 * 1577 * While the llist state transitions from d to e, a kworker 1578 * can start executing rcu_sr_normal_gp_cleanup_work() and 1579 * can observe either the old done tail (@c) or the new 1580 * done tail (@e). So, done tail updates and reads need 1581 * to use the rel-acq semantics. If the concurrent kworker 1582 * observes the old done tail, the newly queued work 1583 * execution will process the updated done tail. If the 1584 * concurrent kworker observes the new done tail, then 1585 * the newly queued work will skip processing the done 1586 * tail, as workqueue semantics guarantees that the new 1587 * work is executed only after the previous one completes. 1588 * 1589 * f. kworker callbacks processing complete: 1590 * 1591 * 1592 * DONE TAIL 1593 * | 1594 * | 1595 * v 1596 * +----------+ +--------+ 1597 * | | | | 1598 * | head ------> wait | 1599 * | | | head2 | 1600 * +----------+ +--------+ 1601 * 1602 */ 1603 static bool rcu_sr_is_wait_head(struct llist_node *node) 1604 { 1605 return &(rcu_state.srs_wait_nodes)[0].node <= node && 1606 node <= &(rcu_state.srs_wait_nodes)[SR_NORMAL_GP_WAIT_HEAD_MAX - 1].node; 1607 } 1608 1609 static struct llist_node *rcu_sr_get_wait_head(void) 1610 { 1611 struct sr_wait_node *sr_wn; 1612 int i; 1613 1614 for (i = 0; i < SR_NORMAL_GP_WAIT_HEAD_MAX; i++) { 1615 sr_wn = &(rcu_state.srs_wait_nodes)[i]; 1616 1617 if (!atomic_cmpxchg_acquire(&sr_wn->inuse, 0, 1)) 1618 return &sr_wn->node; 1619 } 1620 1621 return NULL; 1622 } 1623 1624 static void rcu_sr_put_wait_head(struct llist_node *node) 1625 { 1626 struct sr_wait_node *sr_wn = container_of(node, struct sr_wait_node, node); 1627 1628 atomic_set_release(&sr_wn->inuse, 0); 1629 } 1630 1631 static int rcu_normal_wake_from_gp = 1; 1632 module_param(rcu_normal_wake_from_gp, int, 0644); 1633 static struct workqueue_struct *sync_wq; 1634 1635 #define RCU_SR_NORMAL_LATCH_THR 64 1636 1637 /* Number of in-flight synchronize_rcu() calls queued on srs_next. */ 1638 static atomic_long_t rcu_sr_normal_count; 1639 static int rcu_sr_normal_latched; /* 0/1 */ 1640 1641 static void rcu_sr_normal_complete(struct llist_node *node) 1642 { 1643 struct rcu_synchronize *rs = container_of( 1644 (struct rcu_head *) node, struct rcu_synchronize, head); 1645 long nr; 1646 1647 WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && 1648 !poll_state_synchronize_rcu_full(&rs->oldstate), 1649 "A full grace period is not passed yet!\n"); 1650 1651 /* Finally. */ 1652 complete(&rs->completion); 1653 nr = atomic_long_dec_return(&rcu_sr_normal_count); 1654 WARN_ON_ONCE(nr < 0); 1655 1656 /* 1657 * Unlatch: switch back to normal path when fully 1658 * drained and if it has been latched. 1659 */ 1660 if (nr == 0) 1661 (void)cmpxchg_relaxed(&rcu_sr_normal_latched, 1, 0); 1662 } 1663 1664 static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work) 1665 { 1666 struct llist_node *done, *rcu, *next, *head; 1667 1668 /* 1669 * This work execution can potentially execute 1670 * while a new done tail is being updated by 1671 * grace period kthread in rcu_sr_normal_gp_cleanup(). 1672 * So, read and updates of done tail need to 1673 * follow acq-rel semantics. 1674 * 1675 * Given that wq semantics guarantees that a single work 1676 * cannot execute concurrently by multiple kworkers, 1677 * the done tail list manipulations are protected here. 1678 */ 1679 done = smp_load_acquire(&rcu_state.srs_done_tail); 1680 if (WARN_ON_ONCE(!done)) 1681 return; 1682 1683 WARN_ON_ONCE(!rcu_sr_is_wait_head(done)); 1684 head = done->next; 1685 done->next = NULL; 1686 1687 /* 1688 * The dummy node, which is pointed to by the 1689 * done tail which is acq-read above is not removed 1690 * here. This allows lockless additions of new 1691 * rcu_synchronize nodes in rcu_sr_normal_add_req(), 1692 * while the cleanup work executes. The dummy 1693 * nodes is removed, in next round of cleanup 1694 * work execution. 1695 */ 1696 llist_for_each_safe(rcu, next, head) { 1697 if (!rcu_sr_is_wait_head(rcu)) { 1698 rcu_sr_normal_complete(rcu); 1699 continue; 1700 } 1701 1702 rcu_sr_put_wait_head(rcu); 1703 } 1704 1705 /* Order list manipulations with atomic access. */ 1706 atomic_dec_return_release(&rcu_state.srs_cleanups_pending); 1707 } 1708 1709 /* 1710 * Helper function for rcu_gp_cleanup(). 1711 */ 1712 static void rcu_sr_normal_gp_cleanup(void) 1713 { 1714 struct llist_node *wait_tail, *next = NULL, *rcu = NULL; 1715 int done = 0; 1716 1717 wait_tail = rcu_state.srs_wait_tail; 1718 if (wait_tail == NULL) 1719 return; 1720 1721 rcu_state.srs_wait_tail = NULL; 1722 ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_wait_tail); 1723 WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail)); 1724 1725 /* 1726 * Process (a) and (d) cases. See an illustration. 1727 */ 1728 llist_for_each_safe(rcu, next, wait_tail->next) { 1729 if (rcu_sr_is_wait_head(rcu)) 1730 break; 1731 1732 rcu_sr_normal_complete(rcu); 1733 // It can be last, update a next on this step. 1734 wait_tail->next = next; 1735 1736 if (++done == SR_MAX_USERS_WAKE_FROM_GP) 1737 break; 1738 } 1739 1740 /* 1741 * Fast path, no more users to process except putting the second last 1742 * wait head if no inflight-workers. If there are in-flight workers, 1743 * they will remove the last wait head. 1744 * 1745 * Note that the ACQUIRE orders atomic access with list manipulation. 1746 */ 1747 if (wait_tail->next && wait_tail->next->next == NULL && 1748 rcu_sr_is_wait_head(wait_tail->next) && 1749 !atomic_read_acquire(&rcu_state.srs_cleanups_pending)) { 1750 rcu_sr_put_wait_head(wait_tail->next); 1751 wait_tail->next = NULL; 1752 } 1753 1754 /* Concurrent sr_normal_gp_cleanup work might observe this update. */ 1755 ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_done_tail); 1756 smp_store_release(&rcu_state.srs_done_tail, wait_tail); 1757 1758 /* 1759 * We schedule a work in order to perform a final processing 1760 * of outstanding users(if still left) and releasing wait-heads 1761 * added by rcu_sr_normal_gp_init() call. 1762 */ 1763 if (wait_tail->next) { 1764 atomic_inc(&rcu_state.srs_cleanups_pending); 1765 if (!queue_work(sync_wq, &rcu_state.srs_cleanup_work)) 1766 atomic_dec(&rcu_state.srs_cleanups_pending); 1767 } 1768 } 1769 1770 /* 1771 * Helper function for rcu_gp_init(). 1772 */ 1773 static bool rcu_sr_normal_gp_init(void) 1774 { 1775 struct llist_node *first; 1776 struct llist_node *wait_head; 1777 bool start_new_poll = false; 1778 1779 first = READ_ONCE(rcu_state.srs_next.first); 1780 if (!first || rcu_sr_is_wait_head(first)) 1781 return start_new_poll; 1782 1783 wait_head = rcu_sr_get_wait_head(); 1784 if (!wait_head) { 1785 // Kick another GP to retry. 1786 start_new_poll = true; 1787 return start_new_poll; 1788 } 1789 1790 /* Inject a wait-dummy-node. */ 1791 llist_add(wait_head, &rcu_state.srs_next); 1792 1793 /* 1794 * A waiting list of rcu_synchronize nodes should be empty on 1795 * this step, since a GP-kthread, rcu_gp_init() -> gp_cleanup(), 1796 * rolls it over. If not, it is a BUG, warn a user. 1797 */ 1798 WARN_ON_ONCE(rcu_state.srs_wait_tail != NULL); 1799 rcu_state.srs_wait_tail = wait_head; 1800 ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_wait_tail); 1801 1802 return start_new_poll; 1803 } 1804 1805 static void rcu_sr_normal_add_req(struct rcu_synchronize *rs) 1806 { 1807 /* 1808 * Increment before publish to avoid a complete 1809 * vs enqueue race on latch. 1810 */ 1811 long nr = atomic_long_inc_return(&rcu_sr_normal_count); 1812 1813 /* 1814 * Latch when threshold is reached. Checking for an exact match 1815 * restricts cmpxchg() to a single context. 1816 * 1817 * This latch is intentionally relaxed and best-effort. Concurrent 1818 * set/clear can race and temporarily lose the latch, which is OK 1819 * because it only selects between the fast and fallback paths. 1820 */ 1821 if (nr == RCU_SR_NORMAL_LATCH_THR) 1822 (void)cmpxchg_relaxed(&rcu_sr_normal_latched, 0, 1); 1823 1824 /* Publish for the GP kthread/worker. */ 1825 llist_add((struct llist_node *) &rs->head, &rcu_state.srs_next); 1826 } 1827 1828 /* 1829 * Initialize a new grace period. Return false if no grace period required. 1830 */ 1831 static noinline_for_stack bool rcu_gp_init(void) 1832 { 1833 unsigned long flags; 1834 unsigned long oldmask; 1835 unsigned long mask; 1836 struct rcu_data *rdp; 1837 struct rcu_node *rnp = rcu_get_root(); 1838 bool start_new_poll; 1839 unsigned long old_gp_seq; 1840 1841 WRITE_ONCE(rcu_state.gp_activity, jiffies); 1842 raw_spin_lock_irq_rcu_node(rnp); 1843 if (!rcu_state.gp_flags) { 1844 /* Spurious wakeup, tell caller to go back to sleep. */ 1845 raw_spin_unlock_irq_rcu_node(rnp); 1846 return false; 1847 } 1848 WRITE_ONCE(rcu_state.gp_flags, 0); /* Clear all flags: New GP. */ 1849 1850 if (WARN_ON_ONCE(rcu_gp_in_progress())) { 1851 /* 1852 * Grace period already in progress, don't start another. 1853 * Not supposed to be able to happen. 1854 */ 1855 raw_spin_unlock_irq_rcu_node(rnp); 1856 return false; 1857 } 1858 1859 /* Advance to a new grace period and initialize state. */ 1860 record_gp_stall_check_time(); 1861 /* 1862 * A new wait segment must be started before gp_seq advanced, so 1863 * that previous gp waiters won't observe the new gp_seq. 1864 */ 1865 start_new_poll = rcu_sr_normal_gp_init(); 1866 /* Record GP times before starting GP, hence rcu_seq_start(). */ 1867 old_gp_seq = rcu_state.gp_seq; 1868 /* 1869 * Critical ordering: rcu_seq_start() must happen BEFORE the CPU hotplug 1870 * scan below. Otherwise we risk a race where a newly onlining CPU could 1871 * be missed by the current grace period, potentially leading to 1872 * use-after-free errors. For a detailed explanation of this race, see 1873 * Documentation/RCU/Design/Requirements/Requirements.rst in the 1874 * "Hotplug CPU" section. 1875 * 1876 * Also note that the root rnp's gp_seq is kept separate from, and lags, 1877 * the rcu_state's gp_seq, for a reason. See the Quick-Quiz on 1878 * Single-node systems for more details (in Data-Structures.rst). 1879 */ 1880 rcu_seq_start(&rcu_state.gp_seq); 1881 /* Ensure that rcu_seq_done_exact() guardband doesn't give false positives. */ 1882 WARN_ON_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && 1883 rcu_seq_done_exact(&old_gp_seq, rcu_seq_snap(&rcu_state.gp_seq))); 1884 1885 ASSERT_EXCLUSIVE_WRITER(rcu_state.gp_seq); 1886 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start")); 1887 rcu_poll_gp_seq_start(&rcu_state.gp_seq_polled_snap); 1888 raw_spin_unlock_irq_rcu_node(rnp); 1889 1890 /* 1891 * The "start_new_poll" is set to true, only when this GP is not able 1892 * to handle anything and there are outstanding users. It happens when 1893 * the rcu_sr_normal_gp_init() function was not able to insert a dummy 1894 * separator to the llist, because there were no left any dummy-nodes. 1895 * 1896 * Number of dummy-nodes is fixed, it could be that we are run out of 1897 * them, if so we start a new pool request to repeat a try. It is rare 1898 * and it means that a system is doing a slow processing of callbacks. 1899 */ 1900 if (start_new_poll) 1901 (void) start_poll_synchronize_rcu(); 1902 1903 /* 1904 * Apply per-leaf buffered online and offline operations to 1905 * the rcu_node tree. Note that this new grace period need not 1906 * wait for subsequent online CPUs, and that RCU hooks in the CPU 1907 * offlining path, when combined with checks in this function, 1908 * will handle CPUs that are currently going offline or that will 1909 * go offline later. Please also refer to "Hotplug CPU" section 1910 * of RCU's Requirements documentation. 1911 */ 1912 WRITE_ONCE(rcu_state.gp_state, RCU_GP_ONOFF); 1913 /* Exclude CPU hotplug operations. */ 1914 rcu_for_each_leaf_node(rnp) { 1915 local_irq_disable(); 1916 /* 1917 * Serialize with CPU offline. See Requirements.rst > Hotplug CPU > 1918 * Concurrent Quiescent State Reporting for Offline CPUs. 1919 */ 1920 arch_spin_lock(&rcu_state.ofl_lock); 1921 raw_spin_lock_rcu_node(rnp); 1922 if (rnp->qsmaskinit == rnp->qsmaskinitnext && 1923 !rnp->wait_blkd_tasks) { 1924 /* Nothing to do on this leaf rcu_node structure. */ 1925 raw_spin_unlock_rcu_node(rnp); 1926 arch_spin_unlock(&rcu_state.ofl_lock); 1927 local_irq_enable(); 1928 continue; 1929 } 1930 1931 /* Record old state, apply changes to ->qsmaskinit field. */ 1932 oldmask = rnp->qsmaskinit; 1933 rnp->qsmaskinit = rnp->qsmaskinitnext; 1934 1935 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */ 1936 if (!oldmask != !rnp->qsmaskinit) { 1937 if (!oldmask) { /* First online CPU for rcu_node. */ 1938 if (!rnp->wait_blkd_tasks) /* Ever offline? */ 1939 rcu_init_new_rnp(rnp); 1940 } else if (rcu_preempt_has_tasks(rnp)) { 1941 rnp->wait_blkd_tasks = true; /* blocked tasks */ 1942 } else { /* Last offline CPU and can propagate. */ 1943 rcu_cleanup_dead_rnp(rnp); 1944 } 1945 } 1946 1947 /* 1948 * If all waited-on tasks from prior grace period are 1949 * done, and if all this rcu_node structure's CPUs are 1950 * still offline, propagate up the rcu_node tree and 1951 * clear ->wait_blkd_tasks. Otherwise, if one of this 1952 * rcu_node structure's CPUs has since come back online, 1953 * simply clear ->wait_blkd_tasks. 1954 */ 1955 if (rnp->wait_blkd_tasks && 1956 (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) { 1957 rnp->wait_blkd_tasks = false; 1958 if (!rnp->qsmaskinit) 1959 rcu_cleanup_dead_rnp(rnp); 1960 } 1961 1962 raw_spin_unlock_rcu_node(rnp); 1963 arch_spin_unlock(&rcu_state.ofl_lock); 1964 local_irq_enable(); 1965 } 1966 rcu_gp_slow(gp_preinit_delay); /* Races with CPU hotplug. */ 1967 1968 /* 1969 * Set the quiescent-state-needed bits in all the rcu_node 1970 * structures for all currently online CPUs in breadth-first 1971 * order, starting from the root rcu_node structure, relying on the 1972 * layout of the tree within the rcu_state.node[] array. Note that 1973 * other CPUs will access only the leaves of the hierarchy, thus 1974 * seeing that no grace period is in progress, at least until the 1975 * corresponding leaf node has been initialized. 1976 * 1977 * The grace period cannot complete until the initialization 1978 * process finishes, because this kthread handles both. 1979 */ 1980 WRITE_ONCE(rcu_state.gp_state, RCU_GP_INIT); 1981 rcu_for_each_node_breadth_first(rnp) { 1982 rcu_gp_slow(gp_init_delay); 1983 raw_spin_lock_irqsave_rcu_node(rnp, flags); 1984 rdp = this_cpu_ptr(&rcu_data); 1985 rcu_preempt_check_blocked_tasks(rnp); 1986 rnp->qsmask = rnp->qsmaskinit; 1987 WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq); 1988 if (rnp == rdp->mynode) 1989 (void)__note_gp_changes(rnp, rdp); 1990 rcu_preempt_boost_start_gp(rnp); 1991 trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq, 1992 rnp->level, rnp->grplo, 1993 rnp->grphi, rnp->qsmask); 1994 /* 1995 * Quiescent states for tasks on any now-offline CPUs. Since we 1996 * released the ofl and rnp lock before this loop, CPUs might 1997 * have gone offline and we have to report QS on their behalf. 1998 * See Requirements.rst > Hotplug CPU > Concurrent QS Reporting. 1999 */ 2000 mask = rnp->qsmask & ~rnp->qsmaskinitnext; 2001 rnp->rcu_gp_init_mask = mask; 2002 if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp)) 2003 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags); 2004 else 2005 raw_spin_unlock_irq_rcu_node(rnp); 2006 cond_resched_tasks_rcu_qs(); 2007 WRITE_ONCE(rcu_state.gp_activity, jiffies); 2008 } 2009 2010 // If strict, make all CPUs aware of new grace period. 2011 if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) 2012 on_each_cpu(rcu_strict_gp_boundary, NULL, 0); 2013 2014 /* 2015 * Immediately report QS for the GP kthread's CPU. The GP kthread 2016 * cannot be in an RCU read-side critical section while running 2017 * the FQS scan. This eliminates the need for a second FQS wait 2018 * when all CPUs are idle. 2019 */ 2020 preempt_disable(); 2021 rcu_qs(); 2022 rcu_report_qs_rdp(this_cpu_ptr(&rcu_data)); 2023 preempt_enable(); 2024 2025 return true; 2026 } 2027 2028 /* 2029 * Helper function for swait_event_idle_exclusive() wakeup at force-quiescent-state 2030 * time. 2031 */ 2032 static bool rcu_gp_fqs_check_wake(int *gfp) 2033 { 2034 struct rcu_node *rnp = rcu_get_root(); 2035 2036 // If under overload conditions, force an immediate FQS scan. 2037 if (*gfp & RCU_GP_FLAG_OVLD) 2038 return true; 2039 2040 // Someone like call_rcu() requested a force-quiescent-state scan. 2041 *gfp = READ_ONCE(rcu_state.gp_flags); 2042 if (*gfp & RCU_GP_FLAG_FQS) 2043 return true; 2044 2045 // The current grace period has completed. 2046 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp)) 2047 return true; 2048 2049 return false; 2050 } 2051 2052 /* 2053 * Do one round of quiescent-state forcing. 2054 */ 2055 static void rcu_gp_fqs(bool first_time) 2056 { 2057 int nr_fqs = READ_ONCE(rcu_state.nr_fqs_jiffies_stall); 2058 struct rcu_node *rnp = rcu_get_root(); 2059 2060 WRITE_ONCE(rcu_state.gp_activity, jiffies); 2061 WRITE_ONCE(rcu_state.n_force_qs, rcu_state.n_force_qs + 1); 2062 2063 WARN_ON_ONCE(nr_fqs > 3); 2064 /* Only countdown nr_fqs for stall purposes if jiffies moves. */ 2065 if (nr_fqs) { 2066 if (nr_fqs == 1) { 2067 WRITE_ONCE(rcu_state.jiffies_stall, 2068 jiffies + rcu_jiffies_till_stall_check()); 2069 } 2070 WRITE_ONCE(rcu_state.nr_fqs_jiffies_stall, --nr_fqs); 2071 } 2072 2073 if (first_time) { 2074 /* Collect dyntick-idle snapshots. */ 2075 force_qs_rnp(rcu_watching_snap_save); 2076 } else { 2077 /* Handle dyntick-idle and offline CPUs. */ 2078 force_qs_rnp(rcu_watching_snap_recheck); 2079 } 2080 /* Clear flag to prevent immediate re-entry. */ 2081 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) { 2082 raw_spin_lock_irq_rcu_node(rnp); 2083 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags & ~RCU_GP_FLAG_FQS); 2084 raw_spin_unlock_irq_rcu_node(rnp); 2085 } 2086 } 2087 2088 /* 2089 * Loop doing repeated quiescent-state forcing until the grace period ends. 2090 */ 2091 static noinline_for_stack void rcu_gp_fqs_loop(void) 2092 { 2093 bool first_gp_fqs = true; 2094 int gf = 0; 2095 unsigned long j; 2096 int ret; 2097 struct rcu_node *rnp = rcu_get_root(); 2098 2099 j = READ_ONCE(jiffies_till_first_fqs); 2100 if (rcu_state.cbovld) 2101 gf = RCU_GP_FLAG_OVLD; 2102 ret = 0; 2103 for (;;) { 2104 if (rcu_state.cbovld) { 2105 j = (j + 2) / 3; 2106 if (j <= 0) 2107 j = 1; 2108 } 2109 if (!ret || time_before(jiffies + j, rcu_state.jiffies_force_qs)) { 2110 WRITE_ONCE(rcu_state.jiffies_force_qs, jiffies + j); 2111 /* 2112 * jiffies_force_qs before RCU_GP_WAIT_FQS state 2113 * update; required for stall checks. 2114 */ 2115 smp_wmb(); 2116 WRITE_ONCE(rcu_state.jiffies_kick_kthreads, 2117 jiffies + (j ? 3 * j : 2)); 2118 } 2119 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 2120 TPS("fqswait")); 2121 WRITE_ONCE(rcu_state.gp_state, RCU_GP_WAIT_FQS); 2122 (void)swait_event_idle_timeout_exclusive(rcu_state.gp_wq, 2123 rcu_gp_fqs_check_wake(&gf), j); 2124 rcu_gp_torture_wait(); 2125 WRITE_ONCE(rcu_state.gp_state, RCU_GP_DOING_FQS); 2126 /* Locking provides needed memory barriers. */ 2127 /* 2128 * Exit the loop if the root rcu_node structure indicates that the grace period 2129 * has ended, leave the loop. The rcu_preempt_blocked_readers_cgp(rnp) check 2130 * is required only for single-node rcu_node trees because readers blocking 2131 * the current grace period are queued only on leaf rcu_node structures. 2132 * For multi-node trees, checking the root node's ->qsmask suffices, because a 2133 * given root node's ->qsmask bit is cleared only when all CPUs and tasks from 2134 * the corresponding leaf nodes have passed through their quiescent state. 2135 */ 2136 if (!READ_ONCE(rnp->qsmask) && 2137 !rcu_preempt_blocked_readers_cgp(rnp)) 2138 break; 2139 /* If time for quiescent-state forcing, do it. */ 2140 if (!time_after(rcu_state.jiffies_force_qs, jiffies) || 2141 (gf & (RCU_GP_FLAG_FQS | RCU_GP_FLAG_OVLD))) { 2142 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 2143 TPS("fqsstart")); 2144 rcu_gp_fqs(first_gp_fqs); 2145 gf = 0; 2146 if (first_gp_fqs) { 2147 first_gp_fqs = false; 2148 gf = rcu_state.cbovld ? RCU_GP_FLAG_OVLD : 0; 2149 } 2150 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 2151 TPS("fqsend")); 2152 cond_resched_tasks_rcu_qs(); 2153 WRITE_ONCE(rcu_state.gp_activity, jiffies); 2154 ret = 0; /* Force full wait till next FQS. */ 2155 j = READ_ONCE(jiffies_till_next_fqs); 2156 } else { 2157 /* Deal with stray signal. */ 2158 cond_resched_tasks_rcu_qs(); 2159 WRITE_ONCE(rcu_state.gp_activity, jiffies); 2160 WARN_ON(signal_pending(current)); 2161 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 2162 TPS("fqswaitsig")); 2163 ret = 1; /* Keep old FQS timing. */ 2164 j = jiffies; 2165 if (time_after(jiffies, rcu_state.jiffies_force_qs)) 2166 j = 1; 2167 else 2168 j = rcu_state.jiffies_force_qs - j; 2169 gf = 0; 2170 } 2171 } 2172 } 2173 2174 /* 2175 * Clean up after the old grace period. 2176 */ 2177 static noinline void rcu_gp_cleanup(void) 2178 { 2179 int cpu; 2180 bool needgp = false; 2181 unsigned long gp_duration; 2182 unsigned long new_gp_seq; 2183 bool offloaded; 2184 struct rcu_data *rdp; 2185 struct rcu_node *rnp = rcu_get_root(); 2186 struct swait_queue_head *sq; 2187 2188 WRITE_ONCE(rcu_state.gp_activity, jiffies); 2189 raw_spin_lock_irq_rcu_node(rnp); 2190 rcu_state.gp_end = jiffies; 2191 gp_duration = rcu_state.gp_end - rcu_state.gp_start; 2192 if (gp_duration > rcu_state.gp_max) 2193 rcu_state.gp_max = gp_duration; 2194 2195 /* 2196 * We know the grace period is complete, but to everyone else 2197 * it appears to still be ongoing. But it is also the case 2198 * that to everyone else it looks like there is nothing that 2199 * they can do to advance the grace period. It is therefore 2200 * safe for us to drop the lock in order to mark the grace 2201 * period as completed in all of the rcu_node structures. 2202 */ 2203 rcu_poll_gp_seq_end(&rcu_state.gp_seq_polled_snap); 2204 raw_spin_unlock_irq_rcu_node(rnp); 2205 2206 /* 2207 * Propagate new ->gp_seq value to rcu_node structures so that 2208 * other CPUs don't have to wait until the start of the next grace 2209 * period to process their callbacks. This also avoids some nasty 2210 * RCU grace-period initialization races by forcing the end of 2211 * the current grace period to be completely recorded in all of 2212 * the rcu_node structures before the beginning of the next grace 2213 * period is recorded in any of the rcu_node structures. 2214 */ 2215 new_gp_seq = rcu_state.gp_seq; 2216 rcu_seq_end(&new_gp_seq); 2217 rcu_for_each_node_breadth_first(rnp) { 2218 raw_spin_lock_irq_rcu_node(rnp); 2219 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp))) 2220 dump_blkd_tasks(rnp, 10); 2221 WARN_ON_ONCE(rnp->qsmask); 2222 WRITE_ONCE(rnp->gp_seq, new_gp_seq); 2223 if (!rnp->parent) { 2224 /* 2225 * Order against failing poll_state_synchronize_rcu_full(), 2226 * and also against rcu_nocb_gp_cleanup() -> swait_active(), 2227 * which relies on this barrier to observe a waiter that 2228 * enqueued before re-checking the grace-period state. 2229 */ 2230 smp_mb(); 2231 } 2232 rdp = this_cpu_ptr(&rcu_data); 2233 if (rnp == rdp->mynode) 2234 needgp = __note_gp_changes(rnp, rdp) || needgp; 2235 /* smp_mb() provided by prior unlock-lock pair. */ 2236 needgp = rcu_future_gp_cleanup(rnp) || needgp; 2237 // Reset overload indication for CPUs no longer overloaded 2238 if (rcu_is_leaf_node(rnp)) 2239 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->cbovldmask) { 2240 rdp = per_cpu_ptr(&rcu_data, cpu); 2241 check_cb_ovld_locked(rdp, rnp); 2242 } 2243 sq = rcu_nocb_gp_get(rnp); 2244 raw_spin_unlock_irq_rcu_node(rnp); 2245 rcu_nocb_gp_cleanup(sq); 2246 cond_resched_tasks_rcu_qs(); 2247 WRITE_ONCE(rcu_state.gp_activity, jiffies); 2248 rcu_gp_slow(gp_cleanup_delay); 2249 } 2250 rnp = rcu_get_root(); 2251 raw_spin_lock_irq_rcu_node(rnp); /* GP before ->gp_seq update. */ 2252 2253 /* Declare grace period done, trace first to use old GP number. */ 2254 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end")); 2255 rcu_seq_end(&rcu_state.gp_seq); 2256 ASSERT_EXCLUSIVE_WRITER(rcu_state.gp_seq); 2257 WRITE_ONCE(rcu_state.gp_state, RCU_GP_IDLE); 2258 /* Check for GP requests since above loop. */ 2259 rdp = this_cpu_ptr(&rcu_data); 2260 if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) { 2261 trace_rcu_this_gp(rnp, rnp->gp_seq_needed, TPS("CleanupMore")); 2262 needgp = true; 2263 } 2264 /* Advance CBs to reduce false positives below. */ 2265 offloaded = rcu_rdp_is_offloaded(rdp); 2266 if ((offloaded || !rcu_accelerate_cbs(rnp, rdp)) && needgp) { 2267 2268 // We get here if a grace period was needed (“needgp”) 2269 // and the above call to rcu_accelerate_cbs() did not set 2270 // the RCU_GP_FLAG_INIT bit in ->gp_state (which records 2271 // the need for another grace period). The purpose 2272 // of the “offloaded” check is to avoid invoking 2273 // rcu_accelerate_cbs() on an offloaded CPU because we do not 2274 // hold the ->nocb_lock needed to safely access an offloaded 2275 // ->cblist. We do not want to acquire that lock because 2276 // it can be heavily contended during callback floods. 2277 2278 WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT); 2279 WRITE_ONCE(rcu_state.gp_req_activity, jiffies); 2280 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("newreq")); 2281 } else { 2282 2283 // We get here either if there is no need for an 2284 // additional grace period or if rcu_accelerate_cbs() has 2285 // already set the RCU_GP_FLAG_INIT bit in ->gp_flags. 2286 // So all we need to do is to clear all of the other 2287 // ->gp_flags bits. 2288 2289 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags & RCU_GP_FLAG_INIT); 2290 } 2291 raw_spin_unlock_irq_rcu_node(rnp); 2292 2293 // Make synchronize_rcu() users aware of the end of old grace period. 2294 rcu_sr_normal_gp_cleanup(); 2295 2296 // If strict, make all CPUs aware of the end of the old grace period. 2297 if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) 2298 on_each_cpu(rcu_strict_gp_boundary, NULL, 0); 2299 } 2300 2301 /* 2302 * Body of kthread that handles grace periods. 2303 */ 2304 static int __noreturn rcu_gp_kthread(void *unused) 2305 { 2306 rcu_bind_gp_kthread(); 2307 for (;;) { 2308 2309 /* Handle grace-period start. */ 2310 for (;;) { 2311 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 2312 TPS("reqwait")); 2313 WRITE_ONCE(rcu_state.gp_state, RCU_GP_WAIT_GPS); 2314 swait_event_idle_exclusive(rcu_state.gp_wq, 2315 READ_ONCE(rcu_state.gp_flags) & 2316 RCU_GP_FLAG_INIT); 2317 rcu_gp_torture_wait(); 2318 WRITE_ONCE(rcu_state.gp_state, RCU_GP_DONE_GPS); 2319 /* Locking provides needed memory barrier. */ 2320 if (rcu_gp_init()) 2321 break; 2322 cond_resched_tasks_rcu_qs(); 2323 WRITE_ONCE(rcu_state.gp_activity, jiffies); 2324 WARN_ON(signal_pending(current)); 2325 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 2326 TPS("reqwaitsig")); 2327 } 2328 2329 /* Handle quiescent-state forcing. */ 2330 rcu_gp_fqs_loop(); 2331 2332 /* Handle grace-period end. */ 2333 WRITE_ONCE(rcu_state.gp_state, RCU_GP_CLEANUP); 2334 rcu_gp_cleanup(); 2335 WRITE_ONCE(rcu_state.gp_state, RCU_GP_CLEANED); 2336 } 2337 } 2338 2339 /* 2340 * Report a full set of quiescent states to the rcu_state data structure. 2341 * Invoke rcu_gp_kthread_wake() to awaken the grace-period kthread if 2342 * another grace period is required. Whether we wake the grace-period 2343 * kthread or it awakens itself for the next round of quiescent-state 2344 * forcing, that kthread will clean up after the just-completed grace 2345 * period. Note that the caller must hold rnp->lock, which is released 2346 * before return. 2347 */ 2348 static void rcu_report_qs_rsp(unsigned long flags) 2349 __releases(rcu_get_root()->lock) 2350 { 2351 raw_lockdep_assert_held_rcu_node(rcu_get_root()); 2352 WARN_ON_ONCE(!rcu_gp_in_progress()); 2353 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_FQS); 2354 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags); 2355 rcu_gp_kthread_wake(); 2356 } 2357 2358 /* 2359 * Similar to rcu_report_qs_rdp(), for which it is a helper function. 2360 * Allows quiescent states for a group of CPUs to be reported at one go 2361 * to the specified rcu_node structure, though all the CPUs in the group 2362 * must be represented by the same rcu_node structure (which need not be a 2363 * leaf rcu_node structure, though it often will be). The gps parameter 2364 * is the grace-period snapshot, which means that the quiescent states 2365 * are valid only if rnp->gp_seq is equal to gps. That structure's lock 2366 * must be held upon entry, and it is released before return. 2367 * 2368 * As a special case, if mask is zero, the bit-already-cleared check is 2369 * disabled. This allows propagating quiescent state due to resumed tasks 2370 * during grace-period initialization. 2371 */ 2372 static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp, 2373 unsigned long gps, unsigned long flags) 2374 __releases(rnp->lock) 2375 { 2376 unsigned long oldmask = 0; 2377 struct rcu_node *rnp_c; 2378 2379 raw_lockdep_assert_held_rcu_node(rnp); 2380 2381 /* Walk up the rcu_node hierarchy. */ 2382 for (;;) { 2383 if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) { 2384 2385 /* 2386 * Our bit has already been cleared, or the 2387 * relevant grace period is already over, so done. 2388 */ 2389 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2390 return; 2391 } 2392 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */ 2393 WARN_ON_ONCE(!rcu_is_leaf_node(rnp) && 2394 rcu_preempt_blocked_readers_cgp(rnp)); 2395 WRITE_ONCE(rnp->qsmask, rnp->qsmask & ~mask); 2396 trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq, 2397 mask, rnp->qsmask, rnp->level, 2398 rnp->grplo, rnp->grphi, 2399 !!rnp->gp_tasks); 2400 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) { 2401 2402 /* Other bits still set at this level, so done. */ 2403 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2404 return; 2405 } 2406 rnp->completedqs = rnp->gp_seq; 2407 mask = rnp->grpmask; 2408 if (rnp->parent == NULL) { 2409 2410 /* No more levels. Exit loop holding root lock. */ 2411 2412 break; 2413 } 2414 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2415 rnp_c = rnp; 2416 rnp = rnp->parent; 2417 raw_spin_lock_irqsave_rcu_node(rnp, flags); 2418 oldmask = READ_ONCE(rnp_c->qsmask); 2419 } 2420 2421 /* 2422 * Get here if we are the last CPU to pass through a quiescent 2423 * state for this grace period. Invoke rcu_report_qs_rsp() 2424 * to clean up and start the next grace period if one is needed. 2425 */ 2426 rcu_report_qs_rsp(flags); /* releases rnp->lock. */ 2427 } 2428 2429 /* 2430 * Record a quiescent state for all tasks that were previously queued 2431 * on the specified rcu_node structure and that were blocking the current 2432 * RCU grace period. The caller must hold the corresponding rnp->lock with 2433 * irqs disabled, and this lock is released upon return, but irqs remain 2434 * disabled. 2435 */ 2436 static void __maybe_unused 2437 rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags) 2438 __releases(rnp->lock) 2439 { 2440 unsigned long gps; 2441 unsigned long mask; 2442 struct rcu_node *rnp_p; 2443 2444 raw_lockdep_assert_held_rcu_node(rnp); 2445 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT_RCU)) || 2446 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) || 2447 rnp->qsmask != 0) { 2448 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2449 return; /* Still need more quiescent states! */ 2450 } 2451 2452 rnp->completedqs = rnp->gp_seq; 2453 rnp_p = rnp->parent; 2454 if (rnp_p == NULL) { 2455 /* 2456 * Only one rcu_node structure in the tree, so don't 2457 * try to report up to its nonexistent parent! 2458 */ 2459 rcu_report_qs_rsp(flags); 2460 return; 2461 } 2462 2463 /* Report up the rest of the hierarchy, tracking current ->gp_seq. */ 2464 gps = rnp->gp_seq; 2465 mask = rnp->grpmask; 2466 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */ 2467 raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */ 2468 rcu_report_qs_rnp(mask, rnp_p, gps, flags); 2469 } 2470 2471 /* 2472 * Record a quiescent state for the specified CPU to that CPU's rcu_data 2473 * structure. This must be called from the specified CPU. 2474 */ 2475 static void 2476 rcu_report_qs_rdp(struct rcu_data *rdp) 2477 { 2478 unsigned long flags; 2479 unsigned long mask; 2480 struct rcu_node *rnp; 2481 2482 WARN_ON_ONCE(rdp->cpu != smp_processor_id()); 2483 rnp = rdp->mynode; 2484 raw_spin_lock_irqsave_rcu_node(rnp, flags); 2485 if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq || 2486 rdp->gpwrap) { 2487 2488 /* 2489 * The grace period in which this quiescent state was 2490 * recorded has ended, so don't report it upwards. 2491 * We will instead need a new quiescent state that lies 2492 * within the current grace period. 2493 */ 2494 rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */ 2495 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2496 return; 2497 } 2498 mask = rdp->grpmask; 2499 rdp->core_needs_qs = false; 2500 if ((rnp->qsmask & mask) == 0) { 2501 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2502 } else { 2503 /* 2504 * This GP can't end until cpu checks in, so all of our 2505 * callbacks can be processed during the next GP. 2506 * 2507 * NOCB kthreads have their own way to deal with that... 2508 */ 2509 if (!rcu_rdp_is_offloaded(rdp)) { 2510 /* 2511 * The current GP has not yet ended, so it 2512 * should not be possible for rcu_accelerate_cbs() 2513 * to return true. So complain, but don't awaken. 2514 */ 2515 WARN_ON_ONCE(rcu_accelerate_cbs(rnp, rdp)); 2516 } 2517 2518 rcu_disable_urgency_upon_qs(rdp); 2519 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags); 2520 /* ^^^ Released rnp->lock */ 2521 } 2522 } 2523 2524 /* 2525 * Check to see if there is a new grace period of which this CPU 2526 * is not yet aware, and if so, set up local rcu_data state for it. 2527 * Otherwise, see if this CPU has just passed through its first 2528 * quiescent state for this grace period, and record that fact if so. 2529 */ 2530 static void 2531 rcu_check_quiescent_state(struct rcu_data *rdp) 2532 { 2533 /* Check for grace-period ends and beginnings. */ 2534 note_gp_changes(rdp); 2535 2536 /* 2537 * Does this CPU still need to do its part for current grace period? 2538 * If no, return and let the other CPUs do their part as well. 2539 */ 2540 if (!rdp->core_needs_qs) 2541 return; 2542 2543 /* 2544 * Was there a quiescent state since the beginning of the grace 2545 * period? If no, then exit and wait for the next call. 2546 */ 2547 if (READ_ONCE(rdp->cpu_no_qs.b.norm)) 2548 return; 2549 2550 /* 2551 * Tell RCU we are done (but rcu_report_qs_rdp() will be the 2552 * judge of that). 2553 */ 2554 rcu_report_qs_rdp(rdp); 2555 } 2556 2557 /* Return true if callback-invocation time limit exceeded. */ 2558 static bool rcu_do_batch_check_time(long count, long tlimit, 2559 bool jlimit_check, unsigned long jlimit) 2560 { 2561 // Invoke local_clock() only once per 32 consecutive callbacks. 2562 return unlikely(tlimit) && 2563 (!likely(count & 31) || 2564 (IS_ENABLED(CONFIG_RCU_DOUBLE_CHECK_CB_TIME) && 2565 jlimit_check && time_after(jiffies, jlimit))) && 2566 local_clock() >= tlimit; 2567 } 2568 2569 /* 2570 * Invoke any RCU callbacks that have made it to the end of their grace 2571 * period. Throttle as specified by rdp->blimit. 2572 */ 2573 static void rcu_do_batch(struct rcu_data *rdp) 2574 { 2575 long bl; 2576 long count = 0; 2577 int div; 2578 bool __maybe_unused empty; 2579 unsigned long flags; 2580 unsigned long jlimit; 2581 bool jlimit_check = false; 2582 long pending; 2583 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl); 2584 struct rcu_head *rhp; 2585 long tlimit = 0; 2586 2587 /* If no callbacks are ready, just return. */ 2588 if (!rcu_segcblist_ready_cbs(&rdp->cblist)) { 2589 trace_rcu_batch_start(rcu_state.name, 2590 rcu_segcblist_n_cbs(&rdp->cblist), 0); 2591 trace_rcu_batch_end(rcu_state.name, 0, 2592 !rcu_segcblist_empty(&rdp->cblist), 2593 need_resched(), is_idle_task(current), 2594 rcu_is_callbacks_kthread(rdp)); 2595 return; 2596 } 2597 2598 /* 2599 * Extract the list of ready callbacks, disabling IRQs to prevent 2600 * races with call_rcu() from interrupt handlers. Leave the 2601 * callback counts, as rcu_barrier() needs to be conservative. 2602 * 2603 * Callbacks execution is fully ordered against preceding grace period 2604 * completion (materialized by rnp->gp_seq update) thanks to the 2605 * smp_mb__after_unlock_lock() upon node locking required for callbacks 2606 * advancing. In NOCB mode this ordering is then further relayed through 2607 * the nocb locking that protects both callbacks advancing and extraction. 2608 */ 2609 rcu_nocb_lock_irqsave(rdp, flags); 2610 WARN_ON_ONCE(cpu_is_offline(smp_processor_id())); 2611 pending = rcu_segcblist_get_seglen(&rdp->cblist, RCU_DONE_TAIL); 2612 div = READ_ONCE(rcu_divisor); 2613 div = div < 0 ? 7 : div > sizeof(long) * 8 - 2 ? sizeof(long) * 8 - 2 : div; 2614 bl = max(rdp->blimit, pending >> div); 2615 if ((in_serving_softirq() || rdp->rcu_cpu_kthread_status == RCU_KTHREAD_RUNNING) && 2616 (IS_ENABLED(CONFIG_RCU_DOUBLE_CHECK_CB_TIME) || unlikely(bl > 100))) { 2617 const long npj = NSEC_PER_SEC / HZ; 2618 long rrn = READ_ONCE(rcu_resched_ns); 2619 2620 rrn = clamp(rrn, NSEC_PER_MSEC, NSEC_PER_SEC); 2621 tlimit = local_clock() + rrn; 2622 jlimit = jiffies + (rrn + npj + 1) / npj; 2623 jlimit_check = true; 2624 } 2625 trace_rcu_batch_start(rcu_state.name, 2626 rcu_segcblist_n_cbs(&rdp->cblist), bl); 2627 rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl); 2628 if (rcu_rdp_is_offloaded(rdp)) 2629 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist); 2630 2631 trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCbDequeued")); 2632 rcu_nocb_unlock_irqrestore(rdp, flags); 2633 2634 /* Invoke callbacks. */ 2635 tick_dep_set_task(current, TICK_DEP_BIT_RCU); 2636 rhp = rcu_cblist_dequeue(&rcl); 2637 2638 for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) { 2639 rcu_callback_t f; 2640 2641 count++; 2642 debug_rcu_head_unqueue(rhp); 2643 2644 rcu_lock_acquire(&rcu_callback_map); 2645 trace_rcu_invoke_callback(rcu_state.name, rhp); 2646 2647 f = rhp->func; 2648 debug_rcu_head_callback(rhp); 2649 WRITE_ONCE(rhp->func, (rcu_callback_t)0L); 2650 f(rhp); 2651 2652 rcu_lock_release(&rcu_callback_map); 2653 2654 /* 2655 * Stop only if limit reached and CPU has something to do. 2656 */ 2657 if (in_serving_softirq()) { 2658 if (count >= bl && (need_resched() || !is_idle_task(current))) 2659 break; 2660 /* 2661 * Make sure we don't spend too much time here and deprive other 2662 * softirq vectors of CPU cycles. 2663 */ 2664 if (rcu_do_batch_check_time(count, tlimit, jlimit_check, jlimit)) 2665 break; 2666 } else { 2667 // In rcuc/rcuoc context, so no worries about 2668 // depriving other softirq vectors of CPU cycles. 2669 local_bh_enable(); 2670 lockdep_assert_irqs_enabled(); 2671 cond_resched_tasks_rcu_qs(); 2672 lockdep_assert_irqs_enabled(); 2673 local_bh_disable(); 2674 // But rcuc kthreads can delay quiescent-state 2675 // reporting, so check time limits for them. 2676 if (rdp->rcu_cpu_kthread_status == RCU_KTHREAD_RUNNING && 2677 rcu_do_batch_check_time(count, tlimit, jlimit_check, jlimit)) { 2678 WRITE_ONCE(rdp->rcu_cpu_has_work, 1); 2679 break; 2680 } 2681 } 2682 } 2683 2684 rcu_nocb_lock_irqsave(rdp, flags); 2685 rdp->n_cbs_invoked += count; 2686 trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(), 2687 is_idle_task(current), rcu_is_callbacks_kthread(rdp)); 2688 2689 /* Update counts and requeue any remaining callbacks. */ 2690 rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl); 2691 rcu_segcblist_add_len(&rdp->cblist, -count); 2692 2693 /* Reinstate batch limit if we have worked down the excess. */ 2694 count = rcu_segcblist_n_cbs(&rdp->cblist); 2695 if (rdp->blimit >= DEFAULT_MAX_RCU_BLIMIT && count <= qlowmark) 2696 rdp->blimit = blimit; 2697 2698 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */ 2699 if (count == 0 && rdp->qlen_last_fqs_check != 0) { 2700 rdp->qlen_last_fqs_check = 0; 2701 rdp->n_force_qs_snap = READ_ONCE(rcu_state.n_force_qs); 2702 } else if (count < rdp->qlen_last_fqs_check - qhimark) 2703 rdp->qlen_last_fqs_check = count; 2704 2705 /* 2706 * The following usually indicates a double call_rcu(). To track 2707 * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. 2708 */ 2709 empty = rcu_segcblist_empty(&rdp->cblist); 2710 WARN_ON_ONCE(count == 0 && !empty); 2711 WARN_ON_ONCE(!IS_ENABLED(CONFIG_RCU_NOCB_CPU) && 2712 count != 0 && empty); 2713 WARN_ON_ONCE(count == 0 && rcu_segcblist_n_segment_cbs(&rdp->cblist) != 0); 2714 WARN_ON_ONCE(!empty && rcu_segcblist_n_segment_cbs(&rdp->cblist) == 0); 2715 2716 rcu_nocb_unlock_irqrestore(rdp, flags); 2717 2718 tick_dep_clear_task(current, TICK_DEP_BIT_RCU); 2719 } 2720 2721 /* 2722 * This function is invoked from each scheduling-clock interrupt, 2723 * and checks to see if this CPU is in a non-context-switch quiescent 2724 * state, for example, user mode or idle loop. It also schedules RCU 2725 * core processing. If the current grace period has gone on too long, 2726 * it will ask the scheduler to manufacture a context switch for the sole 2727 * purpose of providing the needed quiescent state. 2728 */ 2729 void rcu_sched_clock_irq(int user) 2730 { 2731 unsigned long j; 2732 2733 if (IS_ENABLED(CONFIG_PROVE_RCU)) { 2734 j = jiffies; 2735 WARN_ON_ONCE(time_before(j, __this_cpu_read(rcu_data.last_sched_clock))); 2736 __this_cpu_write(rcu_data.last_sched_clock, j); 2737 } 2738 trace_rcu_utilization(TPS("Start scheduler-tick")); 2739 lockdep_assert_irqs_disabled(); 2740 raw_cpu_inc(rcu_data.ticks_this_gp); 2741 /* The load-acquire pairs with the store-release setting to true. */ 2742 if (smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) { 2743 /* Idle and userspace execution already are quiescent states. */ 2744 if (!rcu_is_cpu_rrupt_from_idle() && !user) 2745 set_need_resched_current(); 2746 __this_cpu_write(rcu_data.rcu_urgent_qs, false); 2747 } 2748 rcu_flavor_sched_clock_irq(user); 2749 if (rcu_pending(user)) 2750 invoke_rcu_core(); 2751 if (user || rcu_is_cpu_rrupt_from_idle()) 2752 rcu_note_voluntary_context_switch(current); 2753 lockdep_assert_irqs_disabled(); 2754 2755 trace_rcu_utilization(TPS("End scheduler-tick")); 2756 } 2757 2758 /* 2759 * Scan the leaf rcu_node structures. For each structure on which all 2760 * CPUs have reported a quiescent state and on which there are tasks 2761 * blocking the current grace period, initiate RCU priority boosting. 2762 * Otherwise, invoke the specified function to check dyntick state for 2763 * each CPU that has not yet reported a quiescent state. 2764 */ 2765 static void force_qs_rnp(int (*f)(struct rcu_data *rdp)) 2766 { 2767 int cpu; 2768 unsigned long flags; 2769 struct rcu_node *rnp; 2770 2771 rcu_state.cbovld = rcu_state.cbovldnext; 2772 rcu_state.cbovldnext = false; 2773 rcu_for_each_leaf_node(rnp) { 2774 unsigned long mask = 0; 2775 unsigned long rsmask = 0; 2776 2777 cond_resched_tasks_rcu_qs(); 2778 raw_spin_lock_irqsave_rcu_node(rnp, flags); 2779 rcu_state.cbovldnext |= !!rnp->cbovldmask; 2780 if (rnp->qsmask == 0) { 2781 if (rcu_preempt_blocked_readers_cgp(rnp)) { 2782 /* 2783 * No point in scanning bits because they 2784 * are all zero. But we might need to 2785 * priority-boost blocked readers. 2786 */ 2787 rcu_initiate_boost(rnp, flags); 2788 /* rcu_initiate_boost() releases rnp->lock */ 2789 continue; 2790 } 2791 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2792 continue; 2793 } 2794 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->qsmask) { 2795 struct rcu_data *rdp; 2796 int ret; 2797 2798 rdp = per_cpu_ptr(&rcu_data, cpu); 2799 ret = f(rdp); 2800 if (ret > 0) { 2801 mask |= rdp->grpmask; 2802 rcu_disable_urgency_upon_qs(rdp); 2803 } 2804 if (ret < 0) 2805 rsmask |= rdp->grpmask; 2806 } 2807 if (mask != 0) { 2808 /* Idle/offline CPUs, report (releases rnp->lock). */ 2809 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags); 2810 } else { 2811 /* Nothing to do here, so just drop the lock. */ 2812 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2813 } 2814 2815 for_each_leaf_node_cpu_mask(rnp, cpu, rsmask) 2816 resched_cpu(cpu); 2817 } 2818 } 2819 2820 /* 2821 * Force quiescent states on reluctant CPUs, and also detect which 2822 * CPUs are in dyntick-idle mode. 2823 */ 2824 void rcu_force_quiescent_state(void) 2825 { 2826 unsigned long flags; 2827 bool ret; 2828 struct rcu_node *rnp; 2829 struct rcu_node *rnp_old = NULL; 2830 2831 if (!rcu_gp_in_progress()) 2832 return; 2833 /* Funnel through hierarchy to reduce memory contention. */ 2834 rnp = raw_cpu_read(rcu_data.mynode); 2835 for (; rnp != NULL; rnp = rnp->parent) { 2836 ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) || 2837 !raw_spin_trylock(&rnp->fqslock); 2838 if (rnp_old != NULL) 2839 raw_spin_unlock(&rnp_old->fqslock); 2840 if (ret) 2841 return; 2842 rnp_old = rnp; 2843 } 2844 /* rnp_old == rcu_get_root(), rnp == NULL. */ 2845 2846 /* Reached the root of the rcu_node tree, acquire lock. */ 2847 raw_spin_lock_irqsave_rcu_node(rnp_old, flags); 2848 raw_spin_unlock(&rnp_old->fqslock); 2849 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) { 2850 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags); 2851 return; /* Someone beat us to it. */ 2852 } 2853 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_FQS); 2854 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags); 2855 rcu_gp_kthread_wake(); 2856 } 2857 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state); 2858 2859 // Workqueue handler for an RCU reader for kernels enforcing struct RCU 2860 // grace periods. 2861 static void strict_work_handler(struct work_struct *work) 2862 { 2863 rcu_read_lock(); 2864 rcu_read_unlock(); 2865 } 2866 2867 /* Perform RCU core processing work for the current CPU. */ 2868 static __latent_entropy void rcu_core(void) 2869 { 2870 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data); 2871 struct rcu_node *rnp = rdp->mynode; 2872 2873 if (cpu_is_offline(smp_processor_id())) 2874 return; 2875 trace_rcu_utilization(TPS("Start RCU core")); 2876 WARN_ON_ONCE(!rdp->beenonline); 2877 2878 /* Report any deferred quiescent states if preemption enabled. */ 2879 if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && (!(preempt_count() & PREEMPT_MASK))) { 2880 rcu_preempt_deferred_qs(current); 2881 } else if (rcu_preempt_need_deferred_qs(current)) { 2882 guard(irqsave)(); 2883 set_need_resched_current(); 2884 } 2885 2886 /* Update RCU state based on any recent quiescent states. */ 2887 rcu_check_quiescent_state(rdp); 2888 2889 /* Advance callbacks if an expedited GP has completed. */ 2890 if (!rcu_rdp_is_offloaded(rdp) && rcu_segcblist_is_enabled(&rdp->cblist)) { 2891 struct rcu_gp_seq gp_state; 2892 2893 if (rcu_segcblist_nextgp(&rdp->cblist, &gp_state) && 2894 poll_state_synchronize_rcu_full(&gp_state)) { 2895 guard(irqsave)(); 2896 if (raw_spin_trylock_rcu_node(rnp)) { 2897 bool needwake = rcu_advance_cbs(rnp, rdp); 2898 2899 raw_spin_unlock_rcu_node(rnp); 2900 if (needwake) 2901 rcu_gp_kthread_wake(); 2902 } 2903 } 2904 } 2905 2906 /* No grace period and unregistered callbacks? */ 2907 if (!rcu_gp_in_progress() && 2908 rcu_segcblist_is_enabled(&rdp->cblist) && !rcu_rdp_is_offloaded(rdp)) { 2909 guard(irqsave)(); 2910 if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL)) 2911 rcu_accelerate_cbs_unlocked(rnp, rdp); 2912 } 2913 2914 rcu_check_gp_start_stall(rnp, rcu_jiffies_till_stall_check()); 2915 2916 /* If there are callbacks ready, invoke them. */ 2917 if (!rcu_rdp_is_offloaded(rdp) && rcu_segcblist_ready_cbs(&rdp->cblist) && 2918 likely(READ_ONCE(rcu_scheduler_fully_active))) { 2919 rcu_do_batch(rdp); 2920 /* Re-invoke RCU core processing if there are callbacks remaining. */ 2921 if (rcu_segcblist_ready_cbs(&rdp->cblist)) 2922 invoke_rcu_core(); 2923 } 2924 2925 /* Do any needed deferred wakeups of rcuo kthreads. */ 2926 do_nocb_deferred_wakeup(rdp); 2927 trace_rcu_utilization(TPS("End RCU core")); 2928 2929 // If strict GPs, schedule an RCU reader in a clean environment. 2930 if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) 2931 queue_work_on(rdp->cpu, rcu_gp_wq, &rdp->strict_work); 2932 } 2933 2934 static void rcu_core_si(void) 2935 { 2936 rcu_core(); 2937 } 2938 2939 static void rcu_wake_cond(struct task_struct *t, int status) 2940 { 2941 /* 2942 * If the thread is yielding, only wake it when this 2943 * is invoked from idle 2944 */ 2945 if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current))) 2946 wake_up_process(t); 2947 } 2948 2949 static void invoke_rcu_core_kthread(void) 2950 { 2951 struct task_struct *t; 2952 unsigned long flags; 2953 2954 local_irq_save(flags); 2955 this_cpu_write(rcu_data.rcu_cpu_has_work, 1); 2956 t = __this_cpu_read(rcu_data.rcu_cpu_kthread_task); 2957 if (t != NULL && t != current) 2958 rcu_wake_cond(t, __this_cpu_read(rcu_data.rcu_cpu_kthread_status)); 2959 local_irq_restore(flags); 2960 } 2961 2962 /* 2963 * Wake up this CPU's rcuc kthread to do RCU core processing. 2964 */ 2965 static void invoke_rcu_core(void) 2966 { 2967 if (!cpu_online(smp_processor_id())) 2968 return; 2969 if (use_softirq) 2970 raise_softirq(RCU_SOFTIRQ); 2971 else 2972 invoke_rcu_core_kthread(); 2973 } 2974 2975 static void rcu_cpu_kthread_park(unsigned int cpu) 2976 { 2977 per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU; 2978 } 2979 2980 static int rcu_cpu_kthread_should_run(unsigned int cpu) 2981 { 2982 return this_cpu_read(rcu_data.rcu_cpu_has_work); 2983 } 2984 2985 /* 2986 * Per-CPU kernel thread that invokes RCU callbacks. This replaces 2987 * the RCU softirq used in configurations of RCU that do not support RCU 2988 * priority boosting. 2989 */ 2990 static void rcu_cpu_kthread(unsigned int cpu) 2991 { 2992 unsigned int *statusp = this_cpu_ptr(&rcu_data.rcu_cpu_kthread_status); 2993 char work, *workp = this_cpu_ptr(&rcu_data.rcu_cpu_has_work); 2994 unsigned long *j = this_cpu_ptr(&rcu_data.rcuc_activity); 2995 int spincnt; 2996 2997 trace_rcu_utilization(TPS("Start CPU kthread@rcu_run")); 2998 for (spincnt = 0; spincnt < 10; spincnt++) { 2999 WRITE_ONCE(*j, jiffies); 3000 local_bh_disable(); 3001 *statusp = RCU_KTHREAD_RUNNING; 3002 local_irq_disable(); 3003 work = READ_ONCE(*workp); 3004 WRITE_ONCE(*workp, 0); 3005 local_irq_enable(); 3006 if (work) 3007 rcu_core(); 3008 local_bh_enable(); 3009 if (!READ_ONCE(*workp)) { 3010 trace_rcu_utilization(TPS("End CPU kthread@rcu_wait")); 3011 *statusp = RCU_KTHREAD_WAITING; 3012 return; 3013 } 3014 } 3015 *statusp = RCU_KTHREAD_YIELDING; 3016 trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield")); 3017 schedule_timeout_idle(2); 3018 trace_rcu_utilization(TPS("End CPU kthread@rcu_yield")); 3019 *statusp = RCU_KTHREAD_WAITING; 3020 WRITE_ONCE(*j, jiffies); 3021 } 3022 3023 static struct smp_hotplug_thread rcu_cpu_thread_spec = { 3024 .store = &rcu_data.rcu_cpu_kthread_task, 3025 .thread_should_run = rcu_cpu_kthread_should_run, 3026 .thread_fn = rcu_cpu_kthread, 3027 .thread_comm = "rcuc/%u", 3028 .setup = rcu_cpu_kthread_setup, 3029 .park = rcu_cpu_kthread_park, 3030 }; 3031 3032 /* 3033 * Spawn per-CPU RCU core processing kthreads. 3034 */ 3035 static int __init rcu_spawn_core_kthreads(void) 3036 { 3037 int cpu; 3038 3039 for_each_possible_cpu(cpu) 3040 per_cpu(rcu_data.rcu_cpu_has_work, cpu) = 0; 3041 if (use_softirq) 3042 return 0; 3043 WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec), 3044 "%s: Could not start rcuc kthread, OOM is now expected behavior\n", __func__); 3045 return 0; 3046 } 3047 3048 static void rcutree_enqueue(struct rcu_data *rdp, struct rcu_head *head) 3049 { 3050 rcu_segcblist_enqueue(&rdp->cblist, head); 3051 trace_rcu_callback(rcu_state.name, head, 3052 rcu_segcblist_n_cbs(&rdp->cblist)); 3053 trace_rcu_segcb_stats(&rdp->cblist, TPS("SegCBQueued")); 3054 } 3055 3056 /* 3057 * Handle any core-RCU processing required by a call_rcu() invocation. 3058 */ 3059 static void call_rcu_core(struct rcu_data *rdp, struct rcu_head *head, 3060 unsigned long flags) 3061 { 3062 rcutree_enqueue(rdp, head); 3063 /* 3064 * If called from an extended quiescent state, invoke the RCU 3065 * core in order to force a re-evaluation of RCU's idleness. 3066 */ 3067 if (!rcu_is_watching()) 3068 invoke_rcu_core(); 3069 3070 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */ 3071 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id())) 3072 return; 3073 3074 /* 3075 * Force the grace period if too many callbacks or too long waiting. 3076 * Enforce hysteresis, and don't invoke rcu_force_quiescent_state() 3077 * if some other CPU has recently done so. Also, don't bother 3078 * invoking rcu_force_quiescent_state() if the newly enqueued callback 3079 * is the only one waiting for a grace period to complete. 3080 */ 3081 if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) > 3082 rdp->qlen_last_fqs_check + qhimark)) { 3083 3084 /* Are we ignoring a completed grace period? */ 3085 note_gp_changes(rdp); 3086 3087 /* Start a new grace period if one not already started. */ 3088 if (!rcu_gp_in_progress()) { 3089 rcu_accelerate_cbs_unlocked(rdp->mynode, rdp); 3090 } else { 3091 /* Give the grace period a kick. */ 3092 rdp->blimit = DEFAULT_MAX_RCU_BLIMIT; 3093 if (READ_ONCE(rcu_state.n_force_qs) == rdp->n_force_qs_snap && 3094 rcu_segcblist_first_pend_cb(&rdp->cblist) != head) 3095 rcu_force_quiescent_state(); 3096 rdp->n_force_qs_snap = READ_ONCE(rcu_state.n_force_qs); 3097 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist); 3098 } 3099 } 3100 } 3101 3102 /* 3103 * RCU callback function to leak a callback. 3104 */ 3105 static void rcu_leak_callback(struct rcu_head *rhp) 3106 { 3107 } 3108 3109 /* 3110 * Check and if necessary update the leaf rcu_node structure's 3111 * ->cbovldmask bit corresponding to the current CPU based on that CPU's 3112 * number of queued RCU callbacks. The caller must hold the leaf rcu_node 3113 * structure's ->lock. 3114 */ 3115 static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp) 3116 { 3117 raw_lockdep_assert_held_rcu_node(rnp); 3118 if (qovld_calc <= 0) 3119 return; // Early boot and wildcard value set. 3120 if (rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc) 3121 WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask | rdp->grpmask); 3122 else 3123 WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask & ~rdp->grpmask); 3124 } 3125 3126 /* 3127 * Check and if necessary update the leaf rcu_node structure's 3128 * ->cbovldmask bit corresponding to the current CPU based on that CPU's 3129 * number of queued RCU callbacks. No locks need be held, but the 3130 * caller must have disabled interrupts. 3131 * 3132 * Note that this function ignores the possibility that there are a lot 3133 * of callbacks all of which have already seen the end of their respective 3134 * grace periods. This omission is due to the need for no-CBs CPUs to 3135 * be holding ->nocb_lock to do this check, which is too heavy for a 3136 * common-case operation. 3137 */ 3138 static void check_cb_ovld(struct rcu_data *rdp) 3139 { 3140 struct rcu_node *const rnp = rdp->mynode; 3141 3142 if (qovld_calc <= 0 || 3143 ((rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc) == 3144 !!(READ_ONCE(rnp->cbovldmask) & rdp->grpmask))) 3145 return; // Early boot wildcard value or already set correctly. 3146 raw_spin_lock_rcu_node(rnp); 3147 check_cb_ovld_locked(rdp, rnp); 3148 raw_spin_unlock_rcu_node(rnp); 3149 } 3150 3151 /* 3152 * Also called by __rcu_defer_drain() to re-issue a deferred callback, so it 3153 * must not re-check the deferral condition. Either caller may have interrupts 3154 * already disabled, and a drain of a remote CPU re-issues onto the draining 3155 * CPU. 3156 */ 3157 static void rcu_do_enqueue(struct rcu_head *head, rcu_callback_t func, bool lazy_in) 3158 { 3159 static atomic_t doublefrees; 3160 unsigned long flags; 3161 bool lazy; 3162 struct rcu_data *rdp; 3163 3164 if (debug_rcu_head_queue(head)) { 3165 /* 3166 * Probable double call_rcu(), so leak the callback. 3167 * Use rcu:rcu_callback trace event to find the previous 3168 * time callback was passed to call_rcu(). 3169 */ 3170 if (atomic_inc_return(&doublefrees) < 4) { 3171 pr_err("%s(): Double-freed CB %p->%pS()!!! ", __func__, head, head->func); 3172 mem_dump_obj(head); 3173 } 3174 WRITE_ONCE(head->func, rcu_leak_callback); 3175 return; 3176 } 3177 head->func = func; 3178 head->next = NULL; 3179 3180 local_irq_save(flags); 3181 rdp = this_cpu_ptr(&rcu_data); 3182 RCU_LOCKDEP_WARN(!rcu_rdp_cpu_online(rdp), "Callback enqueued on offline CPU!"); 3183 3184 lazy = lazy_in && !rcu_async_should_hurry(); 3185 3186 /* Add the callback to our list. */ 3187 if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist))) { 3188 // This can trigger due to call_rcu() from offline CPU: 3189 WARN_ON_ONCE(rcu_scheduler_active != RCU_SCHEDULER_INACTIVE); 3190 WARN_ON_ONCE(!rcu_is_watching()); 3191 // Very early boot, before rcu_init(). Initialize if needed 3192 // and then drop through to queue the callback. 3193 if (rcu_segcblist_empty(&rdp->cblist)) 3194 rcu_segcblist_init(&rdp->cblist); 3195 } 3196 3197 check_cb_ovld(rdp); 3198 3199 if (unlikely(rcu_rdp_is_offloaded(rdp))) 3200 call_rcu_nocb(rdp, head, flags, lazy); 3201 else 3202 call_rcu_core(rdp, head, flags); 3203 local_irq_restore(flags); 3204 } 3205 3206 /* 3207 * Re-issue deferred callbacks straight to the enqueue so they cannot defer 3208 * again. ->defer_lock serializes the drainers: this CPU's irq_work, 3209 * rcu_defer_flush() and rcutree_migrate_callbacks(). 3210 */ 3211 static void __rcu_defer_drain(struct rcu_data *rdp) 3212 { 3213 struct llist_node *node, *next; 3214 unsigned long flags; 3215 3216 if (!IS_ENABLED(CONFIG_RCU_DEFER)) 3217 return; 3218 3219 raw_spin_lock_irqsave(&rdp->defer_lock, flags); 3220 llist_for_each_safe(node, next, llist_del_all(&rdp->defer_head)) { 3221 struct rcu_head *head = (struct rcu_head *)node; 3222 3223 /* Bounds a node self-linked by a double call_rcu(). */ 3224 head->next = NULL; 3225 rcu_do_enqueue(head, head->func, false); 3226 } 3227 raw_spin_unlock_irqrestore(&rdp->defer_lock, flags); 3228 } 3229 3230 /* 3231 * Only the irq_work drain can be re-fed by its own re-issue, so only it sets 3232 * ->defer_draining. Anything staged during a direct drain is picked up by the 3233 * staging CPU's own irq_work. Every caller of irq_work_run_list() has 3234 * interrupts disabled, so the flag is never visible with them enabled. 3235 */ 3236 static void rcu_defer_drain(struct irq_work *iw) 3237 { 3238 struct rcu_data *rdp = container_of(iw, struct rcu_data, defer_work); 3239 3240 WRITE_ONCE(rdp->defer_draining, true); 3241 __rcu_defer_drain(rdp); 3242 WRITE_ONCE(rdp->defer_draining, false); 3243 } 3244 3245 /* 3246 * Stage @head for this CPU's irq_work to re-issue once interrupts are on. Only 3247 * the drain side takes a lock, so this stays safe from NMI. 3248 */ 3249 static void call_rcu_defer(struct rcu_head *head, rcu_callback_t func) 3250 { 3251 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 3252 3253 /* 3254 * Instrumentation on the enqueue path can re-enter here from inside the 3255 * drain. Re-queuing would livelock it, so drop the callback; an NMI 3256 * cannot loop, so let it through. 3257 */ 3258 if (READ_ONCE(rdp->defer_draining) && !in_nmi()) { 3259 WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU), 3260 "call_rcu() re-entered during callback drain; leaking callback\n"); 3261 return; 3262 } 3263 head->func = func; 3264 if (llist_add((struct llist_node *)head, &rdp->defer_head)) 3265 irq_work_queue(&rdp->defer_work); 3266 } 3267 3268 static void rcu_defer_flush(void) 3269 { 3270 int cpu; 3271 3272 for_each_possible_cpu(cpu) 3273 __rcu_defer_drain(per_cpu_ptr(&rcu_data, cpu)); 3274 } 3275 3276 static void 3277 __call_rcu_common(struct rcu_head *head, rcu_callback_t func, bool lazy_in) 3278 { 3279 /* Misaligned rcu_head! */ 3280 WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1)); 3281 3282 /* Avoid NULL dereference if callback is NULL. */ 3283 if (WARN_ON_ONCE(!func)) 3284 return; 3285 3286 /* Record the caller: the irq_work's stack says nothing about it. */ 3287 kasan_record_aux_stack(head); 3288 3289 if (should_rcu_defer()) { 3290 call_rcu_defer(head, func); 3291 return; 3292 } 3293 3294 /* 3295 * Only reachable from an NMI when deferral is off: before the scheduler 3296 * is up, or with CONFIG_RCU_DEFER=n. The enqueue can then race. 3297 */ 3298 WARN_ON_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && in_nmi()); 3299 3300 rcu_do_enqueue(head, func, lazy_in); 3301 } 3302 3303 #ifdef CONFIG_RCU_LAZY 3304 static bool enable_rcu_lazy __read_mostly = !IS_ENABLED(CONFIG_RCU_LAZY_DEFAULT_OFF); 3305 module_param(enable_rcu_lazy, bool, 0444); 3306 3307 /** 3308 * call_rcu_hurry() - Queue RCU callback for invocation after grace period, and 3309 * flush all lazy callbacks (including the new one) to the main ->cblist while 3310 * doing so. 3311 * 3312 * @head: structure to be used for queueing the RCU updates. 3313 * @func: actual callback function to be invoked after the grace period 3314 * 3315 * The callback function will be invoked some time after a full grace 3316 * period elapses, in other words after all pre-existing RCU read-side 3317 * critical sections have completed. 3318 * 3319 * Use this API instead of call_rcu() if you don't want the callback to be 3320 * delayed for very long periods of time, which can happen on systems without 3321 * memory pressure and on systems which are lightly loaded or mostly idle. 3322 * This function will cause callbacks to be invoked sooner than later at the 3323 * expense of extra power. Other than that, this function is identical to, and 3324 * reuses call_rcu()'s logic. Refer to call_rcu() for more details about memory 3325 * ordering and other functionality. 3326 */ 3327 void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func) 3328 { 3329 __call_rcu_common(head, func, false); 3330 } 3331 EXPORT_SYMBOL_GPL(call_rcu_hurry); 3332 #else 3333 #define enable_rcu_lazy false 3334 #endif 3335 3336 /** 3337 * call_rcu() - Queue an RCU callback for invocation after a grace period. 3338 * By default the callbacks are 'lazy' and are kept hidden from the main 3339 * ->cblist to prevent starting of grace periods too soon. 3340 * If you desire grace periods to start very soon, use call_rcu_hurry(). 3341 * 3342 * @head: structure to be used for queueing the RCU updates. 3343 * @func: actual callback function to be invoked after the grace period 3344 * 3345 * The callback function will be invoked some time after a full grace 3346 * period elapses, in other words after all pre-existing RCU read-side 3347 * critical sections have completed. However, the callback function 3348 * might well execute concurrently with RCU read-side critical sections 3349 * that started after call_rcu() was invoked. 3350 * 3351 * It is perfectly legal to repost an RCU callback, potentially with 3352 * a different callback function, from within its callback function. 3353 * The specified function will be invoked after another full grace period 3354 * has elapsed. This use case is similar in form to the common practice 3355 * of reposting a timer from within its own handler. 3356 * 3357 * RCU read-side critical sections are delimited by rcu_read_lock() 3358 * and rcu_read_unlock(), and may be nested. In addition, but only in 3359 * v5.0 and later, regions of code across which interrupts, preemption, 3360 * or softirqs have been disabled also serve as RCU read-side critical 3361 * sections. This includes hardware interrupt handlers, softirq handlers, 3362 * and NMI handlers. 3363 * 3364 * Note that all CPUs must agree that the grace period extended beyond 3365 * all pre-existing RCU read-side critical section. On systems with more 3366 * than one CPU, this means that when "func()" is invoked, each CPU is 3367 * guaranteed to have executed a full memory barrier since the end of its 3368 * last RCU read-side critical section whose beginning preceded the call 3369 * to call_rcu(). It also means that each CPU executing an RCU read-side 3370 * critical section that continues beyond the start of "func()" must have 3371 * executed a memory barrier after the call_rcu() but before the beginning 3372 * of that RCU read-side critical section. Note that these guarantees 3373 * include CPUs that are offline, idle, or executing in user mode, as 3374 * well as CPUs that are executing in the kernel. 3375 * 3376 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the 3377 * resulting RCU callback function "func()", then both CPU A and CPU B are 3378 * guaranteed to execute a full memory barrier during the time interval 3379 * between the call to call_rcu() and the invocation of "func()" -- even 3380 * if CPU A and CPU B are the same CPU (but again only if the system has 3381 * more than one CPU). 3382 * 3383 * Implementation of these memory-ordering guarantees is described here: 3384 * Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.rst. 3385 * 3386 * Specific to call_rcu() (as opposed to the other call_rcu*() functions), 3387 * in kernels built with CONFIG_RCU_LAZY=y, call_rcu() might delay for many 3388 * seconds before starting the grace period needed by the corresponding 3389 * callback. This delay can significantly improve energy-efficiency 3390 * on low-utilization battery-powered devices. To avoid this delay, 3391 * in latency-sensitive kernel code, use call_rcu_hurry(). 3392 */ 3393 void call_rcu(struct rcu_head *head, rcu_callback_t func) 3394 { 3395 __call_rcu_common(head, func, enable_rcu_lazy); 3396 } 3397 EXPORT_SYMBOL_GPL(call_rcu); 3398 3399 /* 3400 * During early boot, any blocking grace-period wait automatically 3401 * implies a grace period. 3402 * 3403 * Later on, this could in theory be the case for kernels built with 3404 * CONFIG_SMP=y && CONFIG_PREEMPTION=y running on a single CPU, but this 3405 * is not a common case. Furthermore, this optimization would cause 3406 * the rcu_gp_seq structure to expand by 50%, so this potential 3407 * grace-period optimization is ignored once the scheduler is running. 3408 */ 3409 static int rcu_blocking_is_gp(void) 3410 { 3411 if (rcu_scheduler_active != RCU_SCHEDULER_INACTIVE) { 3412 might_sleep(); 3413 return false; 3414 } 3415 return true; 3416 } 3417 3418 /* 3419 * Helper function for the synchronize_rcu() API. 3420 */ 3421 static void synchronize_rcu_normal(void) 3422 { 3423 struct rcu_synchronize rs; 3424 3425 init_rcu_head_on_stack(&rs.head); 3426 trace_rcu_sr_normal(rcu_state.name, &rs.head, TPS("request")); 3427 3428 if (READ_ONCE(rcu_normal_wake_from_gp) < 1 || 3429 READ_ONCE(rcu_sr_normal_latched)) { 3430 wait_rcu_gp(call_rcu_hurry); 3431 goto trace_complete_out; 3432 } 3433 3434 init_completion(&rs.completion); 3435 3436 /* 3437 * This code might be preempted, therefore take a GP 3438 * snapshot before adding a request. 3439 */ 3440 if (IS_ENABLED(CONFIG_PROVE_RCU)) 3441 get_state_synchronize_rcu_full(&rs.oldstate); 3442 3443 rcu_sr_normal_add_req(&rs); 3444 3445 /* Kick a GP and start waiting. */ 3446 (void) start_poll_synchronize_rcu(); 3447 3448 /* Now we can wait. */ 3449 wait_for_completion(&rs.completion); 3450 3451 trace_complete_out: 3452 trace_rcu_sr_normal(rcu_state.name, &rs.head, TPS("complete")); 3453 destroy_rcu_head_on_stack(&rs.head); 3454 } 3455 3456 /** 3457 * synchronize_rcu - wait until a grace period has elapsed. 3458 * 3459 * Control will return to the caller some time after a full grace 3460 * period has elapsed, in other words after all currently executing RCU 3461 * read-side critical sections have completed. Note, however, that 3462 * upon return from synchronize_rcu(), the caller might well be executing 3463 * concurrently with new RCU read-side critical sections that began while 3464 * synchronize_rcu() was waiting. 3465 * 3466 * RCU read-side critical sections are delimited by rcu_read_lock() 3467 * and rcu_read_unlock(), and may be nested. In addition, but only in 3468 * v5.0 and later, regions of code across which interrupts, preemption, 3469 * or softirqs have been disabled also serve as RCU read-side critical 3470 * sections. This includes hardware interrupt handlers, softirq handlers, 3471 * and NMI handlers. 3472 * 3473 * Note that this guarantee implies further memory-ordering guarantees. 3474 * On systems with more than one CPU, when synchronize_rcu() returns, 3475 * each CPU is guaranteed to have executed a full memory barrier since 3476 * the end of its last RCU read-side critical section whose beginning 3477 * preceded the call to synchronize_rcu(). In addition, each CPU having 3478 * an RCU read-side critical section that extends beyond the return from 3479 * synchronize_rcu() is guaranteed to have executed a full memory barrier 3480 * after the beginning of synchronize_rcu() and before the beginning of 3481 * that RCU read-side critical section. Note that these guarantees include 3482 * CPUs that are offline, idle, or executing in user mode, as well as CPUs 3483 * that are executing in the kernel. 3484 * 3485 * Furthermore, if CPU A invoked synchronize_rcu(), which returned 3486 * to its caller on CPU B, then both CPU A and CPU B are guaranteed 3487 * to have executed a full memory barrier during the execution of 3488 * synchronize_rcu() -- even if CPU A and CPU B are the same CPU (but 3489 * again only if the system has more than one CPU). 3490 * 3491 * Implementation of these memory-ordering guarantees is described here: 3492 * Documentation/RCU/Design/Memory-Ordering/Tree-RCU-Memory-Ordering.rst. 3493 */ 3494 void synchronize_rcu(void) 3495 { 3496 unsigned long flags; 3497 struct rcu_node *rnp; 3498 3499 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) || 3500 lock_is_held(&rcu_lock_map) || 3501 lock_is_held(&rcu_sched_lock_map), 3502 "Illegal synchronize_rcu() in RCU read-side critical section"); 3503 if (!rcu_blocking_is_gp()) { 3504 if (rcu_gp_is_expedited()) 3505 synchronize_rcu_expedited(); 3506 else 3507 synchronize_rcu_normal(); 3508 return; 3509 } 3510 3511 // Context allows vacuous grace periods. 3512 // Note well that this code runs with !PREEMPT && !SMP. 3513 // In addition, all code that advances grace periods runs at 3514 // process level. Therefore, this normal GP overlaps with other 3515 // normal GPs only by being fully nested within them, which allows 3516 // reuse of ->gp_seq_polled_snap. 3517 rcu_poll_gp_seq_start_unlocked(&rcu_state.gp_seq_polled_snap); 3518 rcu_poll_gp_seq_end_unlocked(&rcu_state.gp_seq_polled_snap); 3519 3520 // Update the normal grace-period counters to record 3521 // this grace period, but only those used by the boot CPU. 3522 // The rcu_scheduler_starting() will take care of the rest of 3523 // these counters. 3524 local_irq_save(flags); 3525 WARN_ON_ONCE(num_online_cpus() > 1); 3526 rcu_state.gp_seq += (1 << RCU_SEQ_CTR_SHIFT); 3527 for (rnp = this_cpu_ptr(&rcu_data)->mynode; rnp; rnp = rnp->parent) 3528 rnp->gp_seq_needed = rnp->gp_seq = rcu_state.gp_seq; 3529 local_irq_restore(flags); 3530 } 3531 EXPORT_SYMBOL_GPL(synchronize_rcu); 3532 3533 /** 3534 * get_completed_synchronize_rcu_full - Return a full pre-completed polled state cookie 3535 * @gsp: Place to put state cookie 3536 * 3537 * Stores into @gsp a value that will always be treated by functions 3538 * like poll_state_synchronize_rcu_full() as a cookie whose grace period 3539 * has already completed. 3540 */ 3541 void get_completed_synchronize_rcu_full(struct rcu_gp_seq *gsp) 3542 { 3543 gsp->norm = RCU_GET_STATE_COMPLETED; 3544 gsp->exp = RCU_GET_STATE_COMPLETED; 3545 } 3546 EXPORT_SYMBOL_GPL(get_completed_synchronize_rcu_full); 3547 3548 /** 3549 * get_state_synchronize_rcu - Snapshot current RCU state 3550 * 3551 * Returns a cookie that is used by a later call to cond_synchronize_rcu() 3552 * or poll_state_synchronize_rcu() to determine whether or not a full 3553 * grace period has elapsed in the meantime. 3554 */ 3555 unsigned long get_state_synchronize_rcu(void) 3556 { 3557 /* 3558 * Any prior manipulation of RCU-protected data must happen 3559 * before the load from ->gp_seq. 3560 */ 3561 smp_mb(); /* ^^^ */ 3562 return rcu_seq_snap(&rcu_state.gp_seq_polled); 3563 } 3564 EXPORT_SYMBOL_GPL(get_state_synchronize_rcu); 3565 3566 /** 3567 * get_state_synchronize_rcu_full - Snapshot RCU state, both normal and expedited 3568 * @gsp: location to place combined normal/expedited grace-period state 3569 * 3570 * Places the normal and expedited grace-period states in @gsp. This 3571 * state value can be passed to a later call to cond_synchronize_rcu_full() 3572 * or poll_state_synchronize_rcu_full() to determine whether or not a 3573 * grace period (whether normal or expedited) has elapsed in the meantime. 3574 * The rcu_gp_seq structure takes up twice the memory of an unsigned 3575 * long, but is guaranteed to see all grace periods. In contrast, the 3576 * combined state occupies less memory, but can sometimes fail to take 3577 * grace periods into account. 3578 * 3579 * This does not guarantee that the needed grace period will actually 3580 * start. 3581 */ 3582 void get_state_synchronize_rcu_full(struct rcu_gp_seq *gsp) 3583 { 3584 /* 3585 * Any prior manipulation of RCU-protected data must happen 3586 * before the loads from ->gp_seq and ->expedited_sequence. 3587 */ 3588 smp_mb(); /* ^^^ */ 3589 3590 // Yes, rcu_state.gp_seq, not rnp_root->gp_seq, the latter's use 3591 // in poll_state_synchronize_rcu_full() notwithstanding. Use of 3592 // the latter here would result in too-short grace periods due to 3593 // interactions with newly onlined CPUs. 3594 gsp->norm = rcu_seq_snap(&rcu_state.gp_seq); 3595 gsp->exp = rcu_seq_snap(&rcu_state.expedited_sequence); 3596 } 3597 EXPORT_SYMBOL_GPL(get_state_synchronize_rcu_full); 3598 3599 /* 3600 * Helper function for start_poll_synchronize_rcu() and 3601 * start_poll_synchronize_rcu_full(). 3602 */ 3603 static void start_poll_synchronize_rcu_common(void) 3604 { 3605 unsigned long flags; 3606 bool needwake; 3607 struct rcu_data *rdp; 3608 struct rcu_node *rnp; 3609 3610 local_irq_save(flags); 3611 rdp = this_cpu_ptr(&rcu_data); 3612 rnp = rdp->mynode; 3613 raw_spin_lock_rcu_node(rnp); // irqs already disabled. 3614 // Note it is possible for a grace period to have elapsed between 3615 // the above call to get_state_synchronize_rcu() and the below call 3616 // to rcu_seq_snap. This is OK, the worst that happens is that we 3617 // get a grace period that no one needed. These accesses are ordered 3618 // by smp_mb(), and we are accessing them in the opposite order 3619 // from which they are updated at grace-period start, as required. 3620 needwake = rcu_start_this_gp(rnp, rdp, rcu_seq_snap(&rcu_state.gp_seq)); 3621 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 3622 if (needwake) 3623 rcu_gp_kthread_wake(); 3624 } 3625 3626 /** 3627 * start_poll_synchronize_rcu - Snapshot and start RCU grace period 3628 * 3629 * Returns a cookie that is used by a later call to cond_synchronize_rcu() 3630 * or poll_state_synchronize_rcu() to determine whether or not a full 3631 * grace period has elapsed in the meantime. If the needed grace period 3632 * is not already slated to start, notifies RCU core of the need for that 3633 * grace period. 3634 */ 3635 unsigned long start_poll_synchronize_rcu(void) 3636 { 3637 unsigned long gp_seq = get_state_synchronize_rcu(); 3638 3639 start_poll_synchronize_rcu_common(); 3640 return gp_seq; 3641 } 3642 EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu); 3643 3644 /** 3645 * start_poll_synchronize_rcu_full - Take a full snapshot and start RCU grace period 3646 * @gsp: value from get_state_synchronize_rcu_full() or start_poll_synchronize_rcu_full() 3647 * 3648 * Places the normal and expedited grace-period states in *@gs. This 3649 * state value can be passed to a later call to cond_synchronize_rcu_full() 3650 * or poll_state_synchronize_rcu_full() to determine whether or not a 3651 * grace period (whether normal or expedited) has elapsed in the meantime. 3652 * If the needed grace period is not already slated to start, notifies 3653 * RCU core of the need for that grace period. 3654 */ 3655 void start_poll_synchronize_rcu_full(struct rcu_gp_seq *gsp) 3656 { 3657 get_state_synchronize_rcu_full(gsp); 3658 3659 start_poll_synchronize_rcu_common(); 3660 } 3661 EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu_full); 3662 3663 /** 3664 * poll_state_synchronize_rcu - Has the specified RCU grace period completed? 3665 * @oldstate: value from get_state_synchronize_rcu() or start_poll_synchronize_rcu() 3666 * 3667 * If a full RCU grace period has elapsed since the earlier call from 3668 * which @oldstate was obtained, return @true, otherwise return @false. 3669 * If @false is returned, it is the caller's responsibility to invoke this 3670 * function later on until it does return @true. Alternatively, the caller 3671 * can explicitly wait for a grace period, for example, by passing @oldstate 3672 * to either cond_synchronize_rcu() or cond_synchronize_rcu_expedited() 3673 * on the one hand or by directly invoking either synchronize_rcu() or 3674 * synchronize_rcu_expedited() on the other. 3675 * 3676 * Yes, this function does not take counter wrap into account. 3677 * But counter wrap is harmless. If the counter wraps, we have waited for 3678 * more than a billion grace periods (and way more on a 64-bit system!). 3679 * Those needing to keep old state values for very long time periods 3680 * (many hours even on 32-bit systems) should check them occasionally and 3681 * either refresh them or set a flag indicating that the grace period has 3682 * completed. Alternatively, they can use get_completed_synchronize_rcu() 3683 * to get a guaranteed-completed grace-period state. 3684 * 3685 * In addition, because oldstate compresses the grace-period state for 3686 * both normal and expedited grace periods into a single unsigned long, 3687 * it can miss a grace period when synchronize_rcu() runs concurrently 3688 * with synchronize_rcu_expedited(). If this is unacceptable, please 3689 * instead use the _full() variant of these polling APIs. 3690 * 3691 * This function provides the same memory-ordering guarantees that 3692 * would be provided by a synchronize_rcu() that was invoked at the call 3693 * to the function that provided @oldstate, and that returned at the end 3694 * of this function. 3695 */ 3696 bool poll_state_synchronize_rcu(unsigned long oldstate) 3697 { 3698 if (oldstate == RCU_GET_STATE_COMPLETED || 3699 rcu_seq_done_exact(&rcu_state.gp_seq_polled, oldstate)) { 3700 smp_mb(); /* Ensure GP ends before subsequent accesses. */ 3701 return true; 3702 } 3703 return false; 3704 } 3705 EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu); 3706 3707 /* 3708 * Racy, memory-ordering-free test of whether the normal or expedited grace 3709 * period recorded in *gsp has completed. Callers that need the full 3710 * memory-ordering guarantees must use poll_state_synchronize_rcu_full(); 3711 * this variant is only a hint (e.g. for rcu_pending()) and leaves any 3712 * required ordering to a subsequent ordered check. 3713 */ 3714 static bool poll_state_synchronize_rcu_full_unordered(struct rcu_gp_seq *gsp) 3715 { 3716 struct rcu_node *rnp = rcu_get_root(); 3717 3718 return gsp->norm == RCU_GET_STATE_COMPLETED || 3719 rcu_seq_done_exact(&rnp->gp_seq, gsp->norm) || 3720 gsp->exp == RCU_GET_STATE_COMPLETED || 3721 (gsp->exp != RCU_GET_STATE_NOT_TRACKED && 3722 rcu_seq_done_exact(&rcu_state.expedited_sequence, gsp->exp)); 3723 } 3724 3725 /** 3726 * poll_state_synchronize_rcu_full - Has the specified RCU grace period completed? 3727 * @gsp: value from get_state_synchronize_rcu_full() or start_poll_synchronize_rcu_full() 3728 * 3729 * If a full RCU grace period has elapsed since the earlier call from 3730 * which *gsp was obtained, return @true, otherwise return @false. 3731 * If @false is returned, it is the caller's responsibility to invoke this 3732 * function later on until it does return @true. Alternatively, the caller 3733 * can explicitly wait for a grace period, for example, by passing @gsp 3734 * to cond_synchronize_rcu() or by directly invoking synchronize_rcu(). 3735 * 3736 * Yes, this function does not take counter wrap into account. 3737 * But counter wrap is harmless. If the counter wraps, we have waited 3738 * for more than a billion grace periods (and way more on a 64-bit 3739 * system!). Those needing to keep rcu_gp_seq values for very 3740 * long time periods (many hours even on 32-bit systems) should check 3741 * them occasionally and either refresh them or set a flag indicating 3742 * that the grace period has completed. Alternatively, they can use 3743 * get_completed_synchronize_rcu_full() to get a guaranteed-completed 3744 * grace-period state. 3745 * 3746 * This function provides the same memory-ordering guarantees that would 3747 * be provided by a synchronize_rcu() that was invoked at the call to 3748 * the function that provided @gsp, and that returned at the end of this 3749 * function. And this guarantee requires that the root rcu_node structure's 3750 * ->gp_seq field be checked instead of that of the rcu_state structure. 3751 * The problem is that the just-ending grace-period's callbacks can be 3752 * invoked between the time that the root rcu_node structure's ->gp_seq 3753 * field is updated and the time that the rcu_state structure's ->gp_seq 3754 * field is updated. Therefore, if a single synchronize_rcu() is to 3755 * cause a subsequent poll_state_synchronize_rcu_full() to return @true, 3756 * then the root rcu_node structure is the one that needs to be polled. 3757 */ 3758 bool poll_state_synchronize_rcu_full(struct rcu_gp_seq *gsp) 3759 { 3760 smp_mb(); // Order against root rcu_node structure grace-period cleanup. 3761 if (poll_state_synchronize_rcu_full_unordered(gsp)) { 3762 smp_mb(); /* Ensure GP ends before subsequent accesses. */ 3763 return true; 3764 } 3765 return false; 3766 } 3767 EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu_full); 3768 3769 /** 3770 * cond_synchronize_rcu - Conditionally wait for an RCU grace period 3771 * @oldstate: value from get_state_synchronize_rcu(), start_poll_synchronize_rcu(), or start_poll_synchronize_rcu_expedited() 3772 * 3773 * If a full RCU grace period has elapsed since the earlier call to 3774 * get_state_synchronize_rcu() or start_poll_synchronize_rcu(), just return. 3775 * Otherwise, invoke synchronize_rcu() to wait for a full grace period. 3776 * 3777 * Yes, this function does not take counter wrap into account. 3778 * But counter wrap is harmless. If the counter wraps, we have waited for 3779 * more than 2 billion grace periods (and way more on a 64-bit system!), 3780 * so waiting for a couple of additional grace periods should be just fine. 3781 * 3782 * This function provides the same memory-ordering guarantees that 3783 * would be provided by a synchronize_rcu() that was invoked at the call 3784 * to the function that provided @oldstate and that returned at the end 3785 * of this function. 3786 */ 3787 void cond_synchronize_rcu(unsigned long oldstate) 3788 { 3789 if (!poll_state_synchronize_rcu(oldstate)) 3790 synchronize_rcu(); 3791 } 3792 EXPORT_SYMBOL_GPL(cond_synchronize_rcu); 3793 3794 /** 3795 * cond_synchronize_rcu_full - Conditionally wait for an RCU grace period 3796 * @gsp: value from get_state_synchronize_rcu_full(), start_poll_synchronize_rcu_full(), or start_poll_synchronize_rcu_expedited_full() 3797 * 3798 * If a full RCU grace period has elapsed since the call to 3799 * get_state_synchronize_rcu_full(), start_poll_synchronize_rcu_full(), 3800 * or start_poll_synchronize_rcu_expedited_full() from which @gsp was 3801 * obtained, just return. Otherwise, invoke synchronize_rcu() to wait 3802 * for a full grace period. 3803 * 3804 * Yes, this function does not take counter wrap into account. 3805 * But counter wrap is harmless. If the counter wraps, we have waited for 3806 * more than 2 billion grace periods (and way more on a 64-bit system!), 3807 * so waiting for a couple of additional grace periods should be just fine. 3808 * 3809 * This function provides the same memory-ordering guarantees that 3810 * would be provided by a synchronize_rcu() that was invoked at the call 3811 * to the function that provided @gsp and that returned at the end of 3812 * this function. 3813 */ 3814 void cond_synchronize_rcu_full(struct rcu_gp_seq *gsp) 3815 { 3816 if (!poll_state_synchronize_rcu_full(gsp)) 3817 synchronize_rcu(); 3818 } 3819 EXPORT_SYMBOL_GPL(cond_synchronize_rcu_full); 3820 3821 /* 3822 * Check to see if there is any immediate RCU-related work to be done by 3823 * the current CPU, returning 1 if so and zero otherwise. The checks are 3824 * in order of increasing expense: checks that can be carried out against 3825 * CPU-local state are performed first. However, we must check for CPU 3826 * stalls first, else we might not get a chance. 3827 */ 3828 static int rcu_pending(int user) 3829 { 3830 bool gp_in_progress; 3831 struct rcu_gp_seq gp_state; 3832 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 3833 struct rcu_node *rnp = rdp->mynode; 3834 3835 lockdep_assert_irqs_disabled(); 3836 3837 /* Check for CPU stalls, if enabled. */ 3838 check_cpu_stall(rdp); 3839 3840 /* Does this CPU need a deferred NOCB wakeup? */ 3841 if (rcu_nocb_need_deferred_wakeup(rdp, RCU_NOCB_WAKE)) 3842 return 1; 3843 3844 /* Is this a nohz_full CPU in userspace or idle? (Ignore RCU if so.) */ 3845 gp_in_progress = rcu_gp_in_progress(); 3846 if ((user || rcu_is_cpu_rrupt_from_idle() || 3847 (gp_in_progress && 3848 time_before(jiffies, READ_ONCE(rcu_state.gp_start) + 3849 nohz_full_patience_delay_jiffies))) && 3850 rcu_nohz_full_cpu()) 3851 return 0; 3852 3853 /* Is the RCU core waiting for a quiescent state from this CPU? */ 3854 if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm && gp_in_progress) 3855 return 1; 3856 3857 /* Does this CPU have callbacks ready to invoke? */ 3858 if (!rcu_rdp_is_offloaded(rdp) && 3859 rcu_segcblist_ready_cbs(&rdp->cblist)) 3860 return 1; 3861 3862 /* 3863 * Has a GP (normal or expedited) completed for pending callbacks? 3864 * This is only a racy hint to decide whether to run rcu_core(); the 3865 * ordered re-check and callback advancement happen there, so the 3866 * unordered test avoids paying for memory barriers on every tick. 3867 */ 3868 if (!rcu_rdp_is_offloaded(rdp) && 3869 rcu_segcblist_nextgp(&rdp->cblist, &gp_state) && 3870 poll_state_synchronize_rcu_full_unordered(&gp_state)) 3871 return 1; 3872 3873 /* Has RCU gone idle with this CPU needing another grace period? */ 3874 if (!gp_in_progress && rcu_segcblist_is_enabled(&rdp->cblist) && 3875 !rcu_rdp_is_offloaded(rdp) && 3876 !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL)) 3877 return 1; 3878 3879 /* Have RCU grace period completed or started? */ 3880 if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq || 3881 unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */ 3882 return 1; 3883 3884 /* nothing to do */ 3885 return 0; 3886 } 3887 3888 /* 3889 * Helper function for rcu_barrier() tracing. If tracing is disabled, 3890 * the compiler is expected to optimize this away. 3891 */ 3892 static void rcu_barrier_trace(const char *s, int cpu, unsigned long done) 3893 { 3894 trace_rcu_barrier(rcu_state.name, s, cpu, 3895 atomic_read(&rcu_state.barrier_cpu_count), done); 3896 } 3897 3898 /* 3899 * RCU callback function for rcu_barrier(). If we are last, wake 3900 * up the task executing rcu_barrier(). 3901 * 3902 * Note that the value of rcu_state.barrier_sequence must be captured 3903 * before the atomic_dec_and_test(). Otherwise, if this CPU is not last, 3904 * other CPUs might count the value down to zero before this CPU gets 3905 * around to invoking rcu_barrier_trace(), which might result in bogus 3906 * data from the next instance of rcu_barrier(). 3907 */ 3908 static void rcu_barrier_callback(struct rcu_head *rhp) 3909 { 3910 unsigned long __maybe_unused s = rcu_state.barrier_sequence; 3911 3912 rhp->next = rhp; // Mark the callback as having been invoked. 3913 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) { 3914 rcu_barrier_trace(TPS("LastCB"), -1, s); 3915 complete(&rcu_state.barrier_completion); 3916 } else { 3917 rcu_barrier_trace(TPS("CB"), -1, s); 3918 } 3919 } 3920 3921 /* 3922 * If needed, entrain an rcu_barrier() callback on rdp->cblist. 3923 */ 3924 static void rcu_barrier_entrain(struct rcu_data *rdp) 3925 { 3926 unsigned long gseq = READ_ONCE(rcu_state.barrier_sequence); 3927 unsigned long lseq = READ_ONCE(rdp->barrier_seq_snap); 3928 bool wake_nocb = false; 3929 bool was_alldone = false; 3930 3931 lockdep_assert_held(&rcu_state.barrier_lock); 3932 if (rcu_seq_state(lseq) || !rcu_seq_state(gseq) || rcu_seq_ctr(lseq) != rcu_seq_ctr(gseq)) 3933 return; 3934 rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence); 3935 rdp->barrier_head.func = rcu_barrier_callback; 3936 debug_rcu_head_queue(&rdp->barrier_head); 3937 rcu_nocb_lock(rdp); 3938 /* 3939 * Flush bypass and wakeup rcuog if we add callbacks to an empty regular 3940 * queue. This way we don't wait for bypass timer that can reach seconds 3941 * if it's fully lazy. 3942 */ 3943 was_alldone = rcu_rdp_is_offloaded(rdp) && !rcu_segcblist_pend_cbs(&rdp->cblist); 3944 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies, false)); 3945 wake_nocb = was_alldone && rcu_segcblist_pend_cbs(&rdp->cblist); 3946 if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head)) { 3947 atomic_inc(&rcu_state.barrier_cpu_count); 3948 } else { 3949 debug_rcu_head_unqueue(&rdp->barrier_head); 3950 rcu_barrier_trace(TPS("IRQNQ"), -1, rcu_state.barrier_sequence); 3951 } 3952 rcu_nocb_unlock(rdp); 3953 if (wake_nocb) 3954 wake_nocb_gp(rdp); 3955 smp_store_release(&rdp->barrier_seq_snap, gseq); 3956 } 3957 3958 /* 3959 * Called with preemption disabled, and from cross-cpu IRQ context. 3960 */ 3961 static void rcu_barrier_handler(void *cpu_in) 3962 { 3963 uintptr_t cpu = (uintptr_t)cpu_in; 3964 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 3965 3966 lockdep_assert_irqs_disabled(); 3967 WARN_ON_ONCE(cpu != rdp->cpu); 3968 WARN_ON_ONCE(cpu != smp_processor_id()); 3969 raw_spin_lock(&rcu_state.barrier_lock); 3970 rcu_barrier_entrain(rdp); 3971 raw_spin_unlock(&rcu_state.barrier_lock); 3972 } 3973 3974 /** 3975 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete. 3976 * 3977 * Note that this primitive does not necessarily wait for an RCU grace period 3978 * to complete. For example, if there are no RCU callbacks queued anywhere 3979 * in the system, then rcu_barrier() is within its rights to return 3980 * immediately, without waiting for anything, much less an RCU grace period. 3981 * In fact, rcu_barrier() will normally not result in any RCU grace periods 3982 * beyond those that were already destined to be executed. 3983 * 3984 * In kernels built with CONFIG_RCU_LAZY=y, this function also hurries all 3985 * pending lazy RCU callbacks. 3986 */ 3987 void rcu_barrier(void) 3988 { 3989 uintptr_t cpu; 3990 unsigned long flags; 3991 unsigned long gseq; 3992 struct rcu_data *rdp; 3993 unsigned long s; 3994 3995 /* Register any deferred callbacks before snapshotting the sequence. */ 3996 rcu_defer_flush(); 3997 3998 s = rcu_seq_snap(&rcu_state.barrier_sequence); 3999 rcu_barrier_trace(TPS("Begin"), -1, s); 4000 4001 /* Take mutex to serialize concurrent rcu_barrier() requests. */ 4002 mutex_lock(&rcu_state.barrier_mutex); 4003 4004 /* Did someone else do our work for us? */ 4005 if (rcu_seq_done(&rcu_state.barrier_sequence, s)) { 4006 rcu_barrier_trace(TPS("EarlyExit"), -1, rcu_state.barrier_sequence); 4007 smp_mb(); /* caller's subsequent code after above check. */ 4008 mutex_unlock(&rcu_state.barrier_mutex); 4009 return; 4010 } 4011 4012 /* Mark the start of the barrier operation. */ 4013 raw_spin_lock_irqsave(&rcu_state.barrier_lock, flags); 4014 rcu_seq_start(&rcu_state.barrier_sequence); 4015 gseq = rcu_state.barrier_sequence; 4016 rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence); 4017 4018 /* 4019 * Initialize the count to two rather than to zero in order 4020 * to avoid a too-soon return to zero in case of an immediate 4021 * invocation of the just-enqueued callback (or preemption of 4022 * this task). Exclude CPU-hotplug operations to ensure that no 4023 * offline non-offloaded CPU has callbacks queued. 4024 */ 4025 init_completion(&rcu_state.barrier_completion); 4026 atomic_set(&rcu_state.barrier_cpu_count, 2); 4027 raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags); 4028 4029 /* 4030 * Force each CPU with callbacks to register a new callback. 4031 * When that callback is invoked, we will know that all of the 4032 * corresponding CPU's preceding callbacks have been invoked. 4033 */ 4034 for_each_possible_cpu(cpu) { 4035 rdp = per_cpu_ptr(&rcu_data, cpu); 4036 retry: 4037 if (smp_load_acquire(&rdp->barrier_seq_snap) == gseq) 4038 continue; 4039 raw_spin_lock_irqsave(&rcu_state.barrier_lock, flags); 4040 if (!rcu_segcblist_n_cbs(&rdp->cblist)) { 4041 WRITE_ONCE(rdp->barrier_seq_snap, gseq); 4042 raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags); 4043 rcu_barrier_trace(TPS("NQ"), cpu, rcu_state.barrier_sequence); 4044 continue; 4045 } 4046 if (!rcu_rdp_cpu_online(rdp)) { 4047 rcu_barrier_entrain(rdp); 4048 WARN_ON_ONCE(READ_ONCE(rdp->barrier_seq_snap) != gseq); 4049 raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags); 4050 rcu_barrier_trace(TPS("OfflineNoCBQ"), cpu, rcu_state.barrier_sequence); 4051 continue; 4052 } 4053 raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags); 4054 if (smp_call_function_single(cpu, rcu_barrier_handler, (void *)cpu, 1)) { 4055 schedule_timeout_uninterruptible(1); 4056 goto retry; 4057 } 4058 WARN_ON_ONCE(READ_ONCE(rdp->barrier_seq_snap) != gseq); 4059 rcu_barrier_trace(TPS("OnlineQ"), cpu, rcu_state.barrier_sequence); 4060 } 4061 4062 /* 4063 * Now that we have an rcu_barrier_callback() callback on each 4064 * CPU, and thus each counted, remove the initial count. 4065 */ 4066 if (atomic_sub_and_test(2, &rcu_state.barrier_cpu_count)) 4067 complete(&rcu_state.barrier_completion); 4068 4069 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */ 4070 wait_for_completion(&rcu_state.barrier_completion); 4071 4072 /* Mark the end of the barrier operation. */ 4073 rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence); 4074 rcu_seq_end(&rcu_state.barrier_sequence); 4075 gseq = rcu_state.barrier_sequence; 4076 for_each_possible_cpu(cpu) { 4077 rdp = per_cpu_ptr(&rcu_data, cpu); 4078 4079 WRITE_ONCE(rdp->barrier_seq_snap, gseq); 4080 } 4081 4082 /* Other rcu_barrier() invocations can now safely proceed. */ 4083 mutex_unlock(&rcu_state.barrier_mutex); 4084 } 4085 EXPORT_SYMBOL_GPL(rcu_barrier); 4086 4087 static unsigned long rcu_barrier_last_throttle; 4088 4089 /** 4090 * rcu_barrier_throttled - Drain deferred RCU frees, but rate-limit starts 4091 * 4092 * This can be thought of as guard rails around the deferred-free barriers 4093 * that permit unrestricted userspace use, at least assuming the hardware's 4094 * try_cmpxchg() is robust. There will be at most one drain operation started 4095 * per sixteenth of a second from use of this function, which means that 4096 * callers might needlessly wait a second or three. 4097 * 4098 * This is intended for use by test suites to avoid OOM by flushing RCU 4099 * callbacks from the previous test before starting the next. See the 4100 * rcutree.do_rcu_barrier module parameter for more information. 4101 * 4102 * Why not simply make rcu_barrier() more scalable? That might be 4103 * the eventual endpoint, but let's keep it simple for the time being. 4104 * Note that the module parameter infrastructure serializes calls to a 4105 * given .set() function, but should concurrent .set() invocation ever be 4106 * possible, we are ready! 4107 */ 4108 static void rcu_barrier_throttled(void) 4109 { 4110 unsigned long j = jiffies; 4111 unsigned long old = READ_ONCE(rcu_barrier_last_throttle); 4112 unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence); 4113 4114 while (time_in_range(j, old, old + HZ / 16) || 4115 !try_cmpxchg(&rcu_barrier_last_throttle, &old, j)) { 4116 schedule_timeout_idle(HZ / 16); 4117 j = jiffies; 4118 old = READ_ONCE(rcu_barrier_last_throttle); 4119 } 4120 /* 4121 * kfree_rcu() can retain objects outside the ordinary callback lists in 4122 * per-CPU SLUB sheaves and kvfree_rcu batches. Always drain those queues: 4123 * an ordinary barrier does not establish that this work was drained. 4124 */ 4125 kvfree_rcu_barrier(); 4126 /* 4127 * A completed barrier can still cover ordinary callbacks queued before 4128 * our entry snapshot. Otherwise, retain an explicit ordinary barrier 4129 * without depending on the implementation of kvfree_rcu_barrier(). 4130 */ 4131 if (rcu_seq_done(&rcu_state.barrier_sequence, s)) 4132 smp_mb(); /* caller's subsequent code after above check. */ 4133 else 4134 rcu_barrier(); 4135 } 4136 4137 /* 4138 * Invoke rcu_barrier_throttled() when a rcutree.do_rcu_barrier 4139 * request arrives. We insist on a true value to allow for possible 4140 * future expansion. 4141 */ 4142 static int param_set_do_rcu_barrier(const char *val, const struct kernel_param *kp) 4143 { 4144 bool b; 4145 int ret; 4146 4147 if (rcu_scheduler_active != RCU_SCHEDULER_RUNNING) 4148 return -EAGAIN; 4149 ret = kstrtobool(val, &b); 4150 if (!ret && b) { 4151 atomic_inc((atomic_t *)kp->arg); 4152 rcu_barrier_throttled(); 4153 atomic_dec((atomic_t *)kp->arg); 4154 } 4155 return ret; 4156 } 4157 4158 /* 4159 * Output the number of outstanding rcutree.do_rcu_barrier requests. 4160 */ 4161 static int param_get_do_rcu_barrier(char *buffer, const struct kernel_param *kp) 4162 { 4163 return sprintf(buffer, "%d\n", atomic_read((atomic_t *)kp->arg)); 4164 } 4165 4166 static const struct kernel_param_ops do_rcu_barrier_ops = { 4167 .set = param_set_do_rcu_barrier, 4168 .get = param_get_do_rcu_barrier, 4169 }; 4170 static atomic_t do_rcu_barrier; 4171 module_param_cb(do_rcu_barrier, &do_rcu_barrier_ops, &do_rcu_barrier, 0644); 4172 4173 /* 4174 * Compute the mask of online CPUs for the specified rcu_node structure. 4175 * This will not be stable unless the rcu_node structure's ->lock is 4176 * held, but the bit corresponding to the current CPU will be stable 4177 * in most contexts. 4178 */ 4179 static unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp) 4180 { 4181 return READ_ONCE(rnp->qsmaskinitnext); 4182 } 4183 4184 /* 4185 * Is the CPU corresponding to the specified rcu_data structure online 4186 * from RCU's perspective? This perspective is given by that structure's 4187 * ->qsmaskinitnext field rather than by the global cpu_online_mask. 4188 */ 4189 static bool rcu_rdp_cpu_online(struct rcu_data *rdp) 4190 { 4191 return !!(rdp->grpmask & rcu_rnp_online_cpus(rdp->mynode)); 4192 } 4193 4194 bool rcu_cpu_online(int cpu) 4195 { 4196 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 4197 4198 return rcu_rdp_cpu_online(rdp); 4199 } 4200 4201 #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) 4202 4203 /* 4204 * Is the current CPU online as far as RCU is concerned? 4205 * 4206 * Disable preemption to avoid false positives that could otherwise 4207 * happen due to the current CPU number being sampled, this task being 4208 * preempted, its old CPU being taken offline, resuming on some other CPU, 4209 * then determining that its old CPU is now offline. 4210 * 4211 * Disable checking if in an NMI handler because we cannot safely 4212 * report errors from NMI handlers anyway. In addition, it is OK to use 4213 * RCU on an offline processor during initial boot, hence the check for 4214 * rcu_scheduler_fully_active. 4215 */ 4216 bool notrace rcu_lockdep_current_cpu_online(void) 4217 { 4218 struct rcu_data *rdp; 4219 bool ret = false; 4220 4221 if (in_nmi() || !rcu_scheduler_fully_active) 4222 return true; 4223 preempt_disable_notrace(); 4224 rdp = this_cpu_ptr(&rcu_data); 4225 /* 4226 * Strictly, we care here about the case where the current CPU is 4227 * in rcutree_report_cpu_starting() and thus has an excuse for rdp->grpmask 4228 * not being up to date. So arch_spin_is_locked() might have a 4229 * false positive if it's held by some *other* CPU, but that's 4230 * OK because that just means a false *negative* on the warning. 4231 */ 4232 if (rcu_rdp_cpu_online(rdp) || arch_spin_is_locked(&rcu_state.ofl_lock)) 4233 ret = true; 4234 preempt_enable_notrace(); 4235 return ret; 4236 } 4237 EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online); 4238 4239 #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */ 4240 4241 // Has rcu_init() been invoked? This is used (for example) to determine 4242 // whether spinlocks may be acquired safely. 4243 static bool rcu_init_invoked(void) 4244 { 4245 return !!READ_ONCE(rcu_state.n_online_cpus); 4246 } 4247 4248 /* 4249 * All CPUs for the specified rcu_node structure have gone offline, 4250 * and all tasks that were preempted within an RCU read-side critical 4251 * section while running on one of those CPUs have since exited their RCU 4252 * read-side critical section. Some other CPU is reporting this fact with 4253 * the specified rcu_node structure's ->lock held and interrupts disabled. 4254 * This function therefore goes up the tree of rcu_node structures, 4255 * clearing the corresponding bits in the ->qsmaskinit fields. Note that 4256 * the leaf rcu_node structure's ->qsmaskinit field has already been 4257 * updated. 4258 * 4259 * This function does check that the specified rcu_node structure has 4260 * all CPUs offline and no blocked tasks, so it is OK to invoke it 4261 * prematurely. That said, invoking it after the fact will cost you 4262 * a needless lock acquisition. So once it has done its work, don't 4263 * invoke it again. 4264 */ 4265 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf) 4266 { 4267 long mask; 4268 struct rcu_node *rnp = rnp_leaf; 4269 4270 raw_lockdep_assert_held_rcu_node(rnp_leaf); 4271 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) || 4272 WARN_ON_ONCE(rnp_leaf->qsmaskinit) || 4273 WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf))) 4274 return; 4275 for (;;) { 4276 mask = rnp->grpmask; 4277 rnp = rnp->parent; 4278 if (!rnp) 4279 break; 4280 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */ 4281 rnp->qsmaskinit &= ~mask; 4282 /* Between grace periods, so better already be zero! */ 4283 WARN_ON_ONCE(rnp->qsmask); 4284 if (rnp->qsmaskinit) { 4285 raw_spin_unlock_rcu_node(rnp); 4286 /* irqs remain disabled. */ 4287 return; 4288 } 4289 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */ 4290 } 4291 } 4292 4293 /* 4294 * Propagate ->qsinitmask bits up the rcu_node tree to account for the 4295 * first CPU in a given leaf rcu_node structure coming online. The caller 4296 * must hold the corresponding leaf rcu_node ->lock with interrupts 4297 * disabled. 4298 */ 4299 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf) 4300 { 4301 long mask; 4302 long oldmask; 4303 struct rcu_node *rnp = rnp_leaf; 4304 4305 raw_lockdep_assert_held_rcu_node(rnp_leaf); 4306 WARN_ON_ONCE(rnp->wait_blkd_tasks); 4307 for (;;) { 4308 mask = rnp->grpmask; 4309 rnp = rnp->parent; 4310 if (rnp == NULL) 4311 return; 4312 raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */ 4313 oldmask = rnp->qsmaskinit; 4314 rnp->qsmaskinit |= mask; 4315 raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */ 4316 if (oldmask) 4317 return; 4318 } 4319 } 4320 4321 /* 4322 * Do boot-time initialization of a CPU's per-CPU RCU data. 4323 */ 4324 static void __init 4325 rcu_boot_init_percpu_data(int cpu) 4326 { 4327 struct context_tracking *ct = this_cpu_ptr(&context_tracking); 4328 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 4329 4330 /* Set up local state, ensuring consistent view of global state. */ 4331 rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu); 4332 INIT_WORK(&rdp->strict_work, strict_work_handler); 4333 WARN_ON_ONCE(ct->nesting != 1); 4334 WARN_ON_ONCE(rcu_watching_snap_in_eqs(ct_rcu_watching_cpu(cpu))); 4335 rdp->barrier_seq_snap = rcu_state.barrier_sequence; 4336 rdp->rcu_ofl_gp_seq = rcu_state.gp_seq; 4337 rdp->rcu_ofl_gp_state = RCU_GP_CLEANED; 4338 rdp->rcu_onl_gp_seq = rcu_state.gp_seq; 4339 rdp->rcu_onl_gp_state = RCU_GP_CLEANED; 4340 rdp->last_sched_clock = jiffies; 4341 rdp->cpu = cpu; 4342 init_llist_head(&rdp->defer_head); 4343 raw_spin_lock_init(&rdp->defer_lock); 4344 rdp->defer_work = IRQ_WORK_INIT_HARD(rcu_defer_drain); 4345 rcu_boot_init_nocb_percpu_data(rdp); 4346 } 4347 4348 static void rcu_thread_affine_rnp(struct task_struct *t, struct rcu_node *rnp) 4349 { 4350 cpumask_var_t affinity; 4351 int cpu; 4352 4353 if (!zalloc_cpumask_var(&affinity, GFP_KERNEL)) 4354 return; 4355 4356 for_each_leaf_node_possible_cpu(rnp, cpu) 4357 cpumask_set_cpu(cpu, affinity); 4358 4359 kthread_affine_preferred(t, affinity); 4360 4361 free_cpumask_var(affinity); 4362 } 4363 4364 struct kthread_worker *rcu_exp_gp_kworker; 4365 4366 static void rcu_spawn_exp_par_gp_kworker(struct rcu_node *rnp) 4367 { 4368 struct kthread_worker *kworker; 4369 const char *name = "rcu_exp_par_gp_kthread_worker/%d"; 4370 struct sched_param param = { .sched_priority = kthread_prio }; 4371 int rnp_index = rnp - rcu_get_root(); 4372 4373 if (rnp->exp_kworker) 4374 return; 4375 4376 kworker = kthread_create_worker(0, name, rnp_index); 4377 if (IS_ERR_OR_NULL(kworker)) { 4378 pr_err("Failed to create par gp kworker on %d/%d\n", 4379 rnp->grplo, rnp->grphi); 4380 return; 4381 } 4382 WRITE_ONCE(rnp->exp_kworker, kworker); 4383 4384 if (IS_ENABLED(CONFIG_RCU_EXP_KTHREAD)) 4385 sched_setscheduler_nocheck(kworker->task, SCHED_FIFO, ¶m); 4386 4387 rcu_thread_affine_rnp(kworker->task, rnp); 4388 wake_up_process(kworker->task); 4389 } 4390 4391 static void __init rcu_start_exp_gp_kworker(void) 4392 { 4393 const char *name = "rcu_exp_gp_kthread_worker"; 4394 struct sched_param param = { .sched_priority = kthread_prio }; 4395 4396 rcu_exp_gp_kworker = kthread_run_worker(0, name); 4397 if (IS_ERR_OR_NULL(rcu_exp_gp_kworker)) { 4398 pr_err("Failed to create %s!\n", name); 4399 rcu_exp_gp_kworker = NULL; 4400 return; 4401 } 4402 4403 if (IS_ENABLED(CONFIG_RCU_EXP_KTHREAD)) 4404 sched_setscheduler_nocheck(rcu_exp_gp_kworker->task, SCHED_FIFO, ¶m); 4405 } 4406 4407 static void rcu_spawn_rnp_kthreads(struct rcu_node *rnp) 4408 { 4409 if (rcu_scheduler_fully_active) { 4410 mutex_lock(&rnp->kthread_mutex); 4411 rcu_spawn_one_boost_kthread(rnp); 4412 rcu_spawn_exp_par_gp_kworker(rnp); 4413 mutex_unlock(&rnp->kthread_mutex); 4414 } 4415 } 4416 4417 /* 4418 * Invoked early in the CPU-online process, when pretty much all services 4419 * are available. The incoming CPU is not present. 4420 * 4421 * Initializes a CPU's per-CPU RCU data. Note that only one online or 4422 * offline event can be happening at a given time. Note also that we can 4423 * accept some slop in the rsp->gp_seq access due to the fact that this 4424 * CPU cannot possibly have any non-offloaded RCU callbacks in flight yet. 4425 * And any offloaded callbacks are being numbered elsewhere. 4426 */ 4427 int rcutree_prepare_cpu(unsigned int cpu) 4428 { 4429 unsigned long flags; 4430 struct context_tracking *ct = per_cpu_ptr(&context_tracking, cpu); 4431 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 4432 struct rcu_node *rnp = rcu_get_root(); 4433 4434 /* Set up local state, ensuring consistent view of global state. */ 4435 raw_spin_lock_irqsave_rcu_node(rnp, flags); 4436 rdp->qlen_last_fqs_check = 0; 4437 rdp->n_force_qs_snap = READ_ONCE(rcu_state.n_force_qs); 4438 rdp->blimit = blimit; 4439 ct->nesting = 1; /* CPU not up, no tearing. */ 4440 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */ 4441 4442 /* 4443 * Only non-NOCB CPUs that didn't have early-boot callbacks need to be 4444 * (re-)initialized. 4445 */ 4446 if (!rcu_segcblist_is_enabled(&rdp->cblist)) 4447 rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */ 4448 4449 /* 4450 * Add CPU to leaf rcu_node pending-online bitmask. Any needed 4451 * propagation up the rcu_node tree will happen at the beginning 4452 * of the next grace period. 4453 */ 4454 rnp = rdp->mynode; 4455 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */ 4456 rdp->gp_seq = READ_ONCE(rnp->gp_seq); 4457 rdp->gp_seq_needed = rdp->gp_seq; 4458 rdp->cpu_no_qs.b.norm = true; 4459 rdp->core_needs_qs = false; 4460 rdp->rcu_iw_pending = false; 4461 rdp->rcu_iw = IRQ_WORK_INIT_HARD(rcu_iw_handler); 4462 rdp->rcu_iw_gp_seq = rdp->gp_seq - 1; 4463 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl")); 4464 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 4465 4466 rcu_preempt_deferred_qs_init(rdp); 4467 rcu_spawn_rnp_kthreads(rnp); 4468 rcu_spawn_cpu_nocb_kthread(cpu); 4469 ASSERT_EXCLUSIVE_WRITER(rcu_state.n_online_cpus); 4470 WRITE_ONCE(rcu_state.n_online_cpus, rcu_state.n_online_cpus + 1); 4471 4472 return 0; 4473 } 4474 4475 /* 4476 * Has the specified (known valid) CPU ever been fully online? 4477 */ 4478 bool rcu_cpu_beenfullyonline(int cpu) 4479 { 4480 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 4481 4482 return smp_load_acquire(&rdp->beenonline); 4483 } 4484 4485 /* 4486 * Near the end of the CPU-online process. Pretty much all services 4487 * enabled, and the CPU is now very much alive. 4488 */ 4489 int rcutree_online_cpu(unsigned int cpu) 4490 { 4491 unsigned long flags; 4492 struct rcu_data *rdp; 4493 struct rcu_node *rnp; 4494 4495 rdp = per_cpu_ptr(&rcu_data, cpu); 4496 rnp = rdp->mynode; 4497 raw_spin_lock_irqsave_rcu_node(rnp, flags); 4498 rnp->ffmask |= rdp->grpmask; 4499 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 4500 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE) 4501 return 0; /* Too early in boot for scheduler work. */ 4502 4503 // Stop-machine done, so allow nohz_full to disable tick. 4504 tick_dep_clear(TICK_DEP_BIT_RCU); 4505 return 0; 4506 } 4507 4508 /* 4509 * Mark the specified CPU as being online so that subsequent grace periods 4510 * (both expedited and normal) will wait on it. Note that this means that 4511 * incoming CPUs are not allowed to use RCU read-side critical sections 4512 * until this function is called. Failing to observe this restriction 4513 * will result in lockdep splats. 4514 * 4515 * Note that this function is special in that it is invoked directly 4516 * from the incoming CPU rather than from the cpuhp_step mechanism. 4517 * This is because this function must be invoked at a precise location. 4518 * This incoming CPU must not have enabled interrupts yet. 4519 * 4520 * This mirrors the effects of rcutree_report_cpu_dead(). 4521 */ 4522 void rcutree_report_cpu_starting(unsigned int cpu) 4523 { 4524 unsigned long mask; 4525 struct rcu_data *rdp; 4526 struct rcu_node *rnp; 4527 bool newcpu; 4528 4529 lockdep_assert_irqs_disabled(); 4530 rdp = per_cpu_ptr(&rcu_data, cpu); 4531 if (rdp->cpu_started) 4532 return; 4533 rdp->cpu_started = true; 4534 4535 rnp = rdp->mynode; 4536 mask = rdp->grpmask; 4537 arch_spin_lock(&rcu_state.ofl_lock); 4538 rcu_watching_online(); 4539 raw_spin_lock(&rcu_state.barrier_lock); 4540 raw_spin_lock_rcu_node(rnp); 4541 WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext | mask); 4542 raw_spin_unlock(&rcu_state.barrier_lock); 4543 newcpu = !(rnp->expmaskinitnext & mask); 4544 rnp->expmaskinitnext |= mask; 4545 /* Allow lockless access for expedited grace periods. */ 4546 smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + newcpu); /* ^^^ */ 4547 ASSERT_EXCLUSIVE_WRITER(rcu_state.ncpus); 4548 rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */ 4549 rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq); 4550 rdp->rcu_onl_gp_state = READ_ONCE(rcu_state.gp_state); 4551 4552 /* An incoming CPU should never be blocking a grace period. */ 4553 if (WARN_ON_ONCE(rnp->qsmask & mask)) { /* RCU waiting on incoming CPU? */ 4554 /* rcu_report_qs_rnp() *really* wants some flags to restore */ 4555 unsigned long flags; 4556 4557 local_irq_save(flags); 4558 rcu_disable_urgency_upon_qs(rdp); 4559 /* Report QS -after- changing ->qsmaskinitnext! */ 4560 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags); 4561 } else { 4562 raw_spin_unlock_rcu_node(rnp); 4563 } 4564 arch_spin_unlock(&rcu_state.ofl_lock); 4565 smp_store_release(&rdp->beenonline, true); 4566 smp_mb(); /* Ensure RCU read-side usage follows above initialization. */ 4567 } 4568 4569 /* 4570 * The outgoing function has no further need of RCU, so remove it from 4571 * the rcu_node tree's ->qsmaskinitnext bit masks. 4572 * 4573 * Note that this function is special in that it is invoked directly 4574 * from the outgoing CPU rather than from the cpuhp_step mechanism. 4575 * This is because this function must be invoked at a precise location. 4576 * 4577 * This mirrors the effect of rcutree_report_cpu_starting(). 4578 */ 4579 void rcutree_report_cpu_dead(void) 4580 { 4581 unsigned long flags; 4582 unsigned long mask; 4583 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 4584 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */ 4585 4586 /* 4587 * IRQS must be disabled from now on and until the CPU dies, or an interrupt 4588 * may introduce a new READ-side while it is actually off the QS masks. 4589 */ 4590 lockdep_assert_irqs_disabled(); 4591 /* 4592 * CPUHP_AP_SMPCFD_DYING was the last call for rcu_exp_handler() execution. 4593 * The requested QS must have been reported on the last context switch 4594 * from stop machine to idle. 4595 */ 4596 WARN_ON_ONCE(rdp->cpu_no_qs.b.exp); 4597 // Do any dangling deferred wakeups. 4598 do_nocb_deferred_wakeup(rdp); 4599 4600 rcu_preempt_deferred_qs(current); 4601 4602 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */ 4603 mask = rdp->grpmask; 4604 4605 /* 4606 * Hold the ofl_lock and rnp lock to avoid races between CPU going 4607 * offline and doing a QS report (as below), versus rcu_gp_init(). 4608 * See Requirements.rst > Hotplug CPU > Concurrent QS Reporting section 4609 * for more details. 4610 */ 4611 arch_spin_lock(&rcu_state.ofl_lock); 4612 raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */ 4613 rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq); 4614 rdp->rcu_ofl_gp_state = READ_ONCE(rcu_state.gp_state); 4615 if (rnp->qsmask & mask) { /* RCU waiting on outgoing CPU? */ 4616 /* Report quiescent state -before- changing ->qsmaskinitnext! */ 4617 rcu_disable_urgency_upon_qs(rdp); 4618 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags); 4619 raw_spin_lock_irqsave_rcu_node(rnp, flags); 4620 } 4621 /* Clear from ->qsmaskinitnext to mark offline. */ 4622 WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext & ~mask); 4623 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 4624 arch_spin_unlock(&rcu_state.ofl_lock); 4625 rdp->cpu_started = false; 4626 } 4627 4628 #ifdef CONFIG_HOTPLUG_CPU 4629 /* 4630 * The outgoing CPU has just passed through the dying-idle state, and we 4631 * are being invoked from the CPU that was IPIed to continue the offline 4632 * operation. Migrate the outgoing CPU's callbacks to the current CPU. 4633 */ 4634 void rcutree_migrate_callbacks(int cpu) 4635 { 4636 unsigned long flags; 4637 struct rcu_data *my_rdp; 4638 struct rcu_node *my_rnp; 4639 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 4640 bool needwake; 4641 4642 /* 4643 * Callbacks deferred past the point the outgoing CPU's irq_work can run 4644 * sit on ->defer_head, which the ->cblist migration below does not 4645 * cover. Drain them here, before the early returns. 4646 */ 4647 __rcu_defer_drain(rdp); 4648 /* Likewise for the outgoing CPU's deferred call_srcu() callbacks. */ 4649 srcu_offline_drain(cpu); 4650 4651 if (rcu_rdp_is_offloaded(rdp)) 4652 return; 4653 4654 raw_spin_lock_irqsave(&rcu_state.barrier_lock, flags); 4655 if (rcu_segcblist_empty(&rdp->cblist)) { 4656 raw_spin_unlock_irqrestore(&rcu_state.barrier_lock, flags); 4657 return; /* No callbacks to migrate. */ 4658 } 4659 4660 WARN_ON_ONCE(rcu_rdp_cpu_online(rdp)); 4661 rcu_barrier_entrain(rdp); 4662 my_rdp = this_cpu_ptr(&rcu_data); 4663 my_rnp = my_rdp->mynode; 4664 rcu_nocb_lock(my_rdp); /* irqs already disabled. */ 4665 WARN_ON_ONCE(!rcu_nocb_flush_bypass(my_rdp, NULL, jiffies, false)); 4666 raw_spin_lock_rcu_node(my_rnp); /* irqs already disabled. */ 4667 /* Leverage recent GPs and set GP for new callbacks. */ 4668 needwake = rcu_advance_cbs(my_rnp, rdp) || 4669 rcu_advance_cbs(my_rnp, my_rdp); 4670 rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist); 4671 raw_spin_unlock(&rcu_state.barrier_lock); /* irqs remain disabled. */ 4672 needwake = needwake || rcu_advance_cbs(my_rnp, my_rdp); 4673 rcu_segcblist_disable(&rdp->cblist); 4674 WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) != !rcu_segcblist_n_cbs(&my_rdp->cblist)); 4675 check_cb_ovld_locked(my_rdp, my_rnp); 4676 if (rcu_rdp_is_offloaded(my_rdp)) { 4677 raw_spin_unlock_rcu_node(my_rnp); /* irqs remain disabled. */ 4678 __call_rcu_nocb_wake(my_rdp, true, flags); 4679 } else { 4680 rcu_nocb_unlock(my_rdp); /* irqs remain disabled. */ 4681 raw_spin_unlock_rcu_node(my_rnp); /* irqs remain disabled. */ 4682 } 4683 local_irq_restore(flags); 4684 if (needwake) 4685 rcu_gp_kthread_wake(); 4686 lockdep_assert_irqs_enabled(); 4687 WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 || 4688 !rcu_segcblist_empty(&rdp->cblist), 4689 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n", 4690 cpu, rcu_segcblist_n_cbs(&rdp->cblist), 4691 rcu_segcblist_first_cb(&rdp->cblist)); 4692 } 4693 4694 /* 4695 * The CPU has been completely removed, and some other CPU is reporting 4696 * this fact from process context. Do the remainder of the cleanup. 4697 * There can only be one CPU hotplug operation at a time, so no need for 4698 * explicit locking. 4699 */ 4700 int rcutree_dead_cpu(unsigned int cpu) 4701 { 4702 ASSERT_EXCLUSIVE_WRITER(rcu_state.n_online_cpus); 4703 WRITE_ONCE(rcu_state.n_online_cpus, rcu_state.n_online_cpus - 1); 4704 // Stop-machine done, so allow nohz_full to disable tick. 4705 tick_dep_clear(TICK_DEP_BIT_RCU); 4706 return 0; 4707 } 4708 4709 /* 4710 * Near the end of the offline process. Trace the fact that this CPU 4711 * is going offline. 4712 */ 4713 int rcutree_dying_cpu(unsigned int cpu) 4714 { 4715 bool blkd; 4716 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 4717 struct rcu_node *rnp = rdp->mynode; 4718 4719 blkd = !!(READ_ONCE(rnp->qsmask) & rdp->grpmask); 4720 trace_rcu_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq), 4721 blkd ? TPS("cpuofl-bgp") : TPS("cpuofl")); 4722 return 0; 4723 } 4724 4725 /* 4726 * Near the beginning of the process. The CPU is still very much alive 4727 * with pretty much all services enabled. 4728 */ 4729 int rcutree_offline_cpu(unsigned int cpu) 4730 { 4731 unsigned long flags; 4732 struct rcu_data *rdp; 4733 struct rcu_node *rnp; 4734 4735 rdp = per_cpu_ptr(&rcu_data, cpu); 4736 rnp = rdp->mynode; 4737 raw_spin_lock_irqsave_rcu_node(rnp, flags); 4738 rnp->ffmask &= ~rdp->grpmask; 4739 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 4740 4741 // nohz_full CPUs need the tick for stop-machine to work quickly 4742 tick_dep_set(TICK_DEP_BIT_RCU); 4743 return 0; 4744 } 4745 #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 4746 4747 /* 4748 * On non-huge systems, use expedited RCU grace periods to make suspend 4749 * and hibernation run faster. 4750 */ 4751 static int rcu_pm_notify(struct notifier_block *self, 4752 unsigned long action, void *hcpu) 4753 { 4754 switch (action) { 4755 case PM_HIBERNATION_PREPARE: 4756 case PM_SUSPEND_PREPARE: 4757 rcu_async_hurry(); 4758 rcu_expedite_gp(); 4759 break; 4760 case PM_POST_HIBERNATION: 4761 case PM_POST_SUSPEND: 4762 rcu_unexpedite_gp(); 4763 rcu_async_relax(); 4764 break; 4765 default: 4766 break; 4767 } 4768 return NOTIFY_OK; 4769 } 4770 4771 /* 4772 * Spawn the kthreads that handle RCU's grace periods. 4773 */ 4774 static int __init rcu_spawn_gp_kthread(void) 4775 { 4776 unsigned long flags; 4777 struct rcu_node *rnp; 4778 struct sched_param sp; 4779 struct task_struct *t; 4780 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 4781 4782 rcu_scheduler_fully_active = 1; 4783 t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name); 4784 if (WARN_ONCE(IS_ERR(t), "%s: Could not start grace-period kthread, OOM is now expected behavior\n", __func__)) 4785 return 0; 4786 if (kthread_prio) { 4787 sp.sched_priority = kthread_prio; 4788 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp); 4789 } 4790 rnp = rcu_get_root(); 4791 raw_spin_lock_irqsave_rcu_node(rnp, flags); 4792 WRITE_ONCE(rcu_state.gp_activity, jiffies); 4793 WRITE_ONCE(rcu_state.gp_req_activity, jiffies); 4794 // Reset .gp_activity and .gp_req_activity before setting .gp_kthread. 4795 smp_store_release(&rcu_state.gp_kthread, t); /* ^^^ */ 4796 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 4797 wake_up_process(t); 4798 /* This is a pre-SMP initcall, we expect a single CPU */ 4799 WARN_ON(num_online_cpus() > 1); 4800 /* 4801 * Those kthreads couldn't be created on rcu_init() -> rcutree_prepare_cpu() 4802 * due to rcu_scheduler_fully_active. 4803 */ 4804 rcu_spawn_cpu_nocb_kthread(smp_processor_id()); 4805 rcu_spawn_rnp_kthreads(rdp->mynode); 4806 rcu_spawn_core_kthreads(); 4807 /* Create kthread worker for expedited GPs */ 4808 rcu_start_exp_gp_kworker(); 4809 return 0; 4810 } 4811 early_initcall(rcu_spawn_gp_kthread); 4812 4813 /* 4814 * This function is invoked towards the end of the scheduler's 4815 * initialization process. Before this is called, the idle task might 4816 * contain synchronous grace-period primitives (during which time, this idle 4817 * task is booting the system, and such primitives are no-ops). After this 4818 * function is called, any synchronous grace-period primitives are run as 4819 * expedited, with the requesting task driving the grace period forward. 4820 * A later core_initcall() rcu_set_runtime_mode() will switch to full 4821 * runtime RCU functionality. 4822 */ 4823 void rcu_scheduler_starting(void) 4824 { 4825 unsigned long flags; 4826 struct rcu_node *rnp; 4827 4828 WARN_ON(num_online_cpus() != 1); 4829 WARN_ON(nr_context_switches() > 0); 4830 rcu_test_sync_prims(); 4831 4832 // Fix up the ->gp_seq counters. 4833 local_irq_save(flags); 4834 rcu_for_each_node_breadth_first(rnp) 4835 rnp->gp_seq_needed = rnp->gp_seq = rcu_state.gp_seq; 4836 local_irq_restore(flags); 4837 4838 // Switch out of early boot mode. 4839 rcu_scheduler_active = RCU_SCHEDULER_INIT; 4840 rcu_test_sync_prims(); 4841 } 4842 4843 /* 4844 * Helper function for rcu_init() that initializes the rcu_state structure. 4845 */ 4846 static void __init rcu_init_one(void) 4847 { 4848 static const char * const buf[] = RCU_NODE_NAME_INIT; 4849 static const char * const fqs[] = RCU_FQS_NAME_INIT; 4850 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS]; 4851 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS]; 4852 4853 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */ 4854 int cpustride = 1; 4855 int i; 4856 int j; 4857 struct rcu_node *rnp; 4858 4859 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */ 4860 4861 /* Silence gcc 4.8 false positive about array index out of range. */ 4862 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS) 4863 panic("rcu_init_one: rcu_num_lvls out of range"); 4864 4865 /* Initialize the level-tracking arrays. */ 4866 4867 for (i = 1; i < rcu_num_lvls; i++) 4868 rcu_state.level[i] = 4869 rcu_state.level[i - 1] + num_rcu_lvl[i - 1]; 4870 rcu_init_levelspread(levelspread, num_rcu_lvl); 4871 4872 /* Initialize the elements themselves, starting from the leaves. */ 4873 4874 for (i = rcu_num_lvls - 1; i >= 0; i--) { 4875 cpustride *= levelspread[i]; 4876 rnp = rcu_state.level[i]; 4877 for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) { 4878 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock)); 4879 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock), 4880 &rcu_node_class[i], buf[i]); 4881 raw_spin_lock_init(&rnp->fqslock); 4882 lockdep_set_class_and_name(&rnp->fqslock, 4883 &rcu_fqs_class[i], fqs[i]); 4884 rnp->gp_seq = rcu_state.gp_seq; 4885 rnp->gp_seq_needed = rcu_state.gp_seq; 4886 rnp->completedqs = rcu_state.gp_seq; 4887 rnp->qsmask = 0; 4888 rnp->qsmaskinit = 0; 4889 rnp->grplo = j * cpustride; 4890 rnp->grphi = (j + 1) * cpustride - 1; 4891 if (rnp->grphi >= nr_cpu_ids) 4892 rnp->grphi = nr_cpu_ids - 1; 4893 if (i == 0) { 4894 rnp->grpnum = 0; 4895 rnp->grpmask = 0; 4896 rnp->parent = NULL; 4897 } else { 4898 rnp->grpnum = j % levelspread[i - 1]; 4899 rnp->grpmask = BIT(rnp->grpnum); 4900 rnp->parent = rcu_state.level[i - 1] + 4901 j / levelspread[i - 1]; 4902 } 4903 rnp->level = i; 4904 INIT_LIST_HEAD(&rnp->blkd_tasks); 4905 rcu_init_one_nocb(rnp); 4906 init_waitqueue_head(&rnp->exp_wq[0]); 4907 init_waitqueue_head(&rnp->exp_wq[1]); 4908 init_waitqueue_head(&rnp->exp_wq[2]); 4909 init_waitqueue_head(&rnp->exp_wq[3]); 4910 spin_lock_init(&rnp->exp_lock); 4911 mutex_init(&rnp->kthread_mutex); 4912 raw_spin_lock_init(&rnp->exp_poll_lock); 4913 rnp->exp_seq_poll_rq = RCU_GET_STATE_COMPLETED; 4914 INIT_WORK(&rnp->exp_poll_wq, sync_rcu_do_polled_gp); 4915 } 4916 } 4917 4918 init_swait_queue_head(&rcu_state.gp_wq); 4919 init_swait_queue_head(&rcu_state.expedited_wq); 4920 rnp = rcu_first_leaf_node(); 4921 for_each_possible_cpu(i) { 4922 while (i > rnp->grphi) 4923 rnp++; 4924 per_cpu_ptr(&rcu_data, i)->mynode = rnp; 4925 per_cpu_ptr(&rcu_data, i)->barrier_head.next = 4926 &per_cpu_ptr(&rcu_data, i)->barrier_head; 4927 rcu_boot_init_percpu_data(i); 4928 } 4929 } 4930 4931 /* 4932 * Force priority from the kernel command-line into range. 4933 */ 4934 static void __init sanitize_kthread_prio(void) 4935 { 4936 int kthread_prio_in = kthread_prio; 4937 4938 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2 4939 && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)) 4940 kthread_prio = 2; 4941 else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1) 4942 kthread_prio = 1; 4943 else if (kthread_prio < 0) 4944 kthread_prio = 0; 4945 else if (kthread_prio > 99) 4946 kthread_prio = 99; 4947 4948 if (kthread_prio != kthread_prio_in) 4949 pr_alert("%s: Limited prio to %d from %d\n", 4950 __func__, kthread_prio, kthread_prio_in); 4951 } 4952 4953 /* 4954 * Compute the rcu_node tree geometry from kernel parameters. This cannot 4955 * replace the definitions in tree.h because those are needed to size 4956 * the ->node array in the rcu_state structure. 4957 */ 4958 void rcu_init_geometry(void) 4959 { 4960 ulong d; 4961 int i; 4962 static unsigned long old_nr_cpu_ids; 4963 int rcu_capacity[RCU_NUM_LVLS]; 4964 static bool initialized; 4965 4966 if (initialized) { 4967 /* 4968 * Warn if setup_nr_cpu_ids() had not yet been invoked, 4969 * unless nr_cpus_ids == NR_CPUS, in which case who cares? 4970 */ 4971 WARN_ON_ONCE(old_nr_cpu_ids != nr_cpu_ids); 4972 return; 4973 } 4974 4975 old_nr_cpu_ids = nr_cpu_ids; 4976 initialized = true; 4977 4978 /* 4979 * Initialize any unspecified boot parameters. 4980 * The default values of jiffies_till_first_fqs and 4981 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS 4982 * value, which is a function of HZ, then adding one for each 4983 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system. 4984 */ 4985 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV; 4986 if (jiffies_till_first_fqs == ULONG_MAX) 4987 jiffies_till_first_fqs = d; 4988 if (jiffies_till_next_fqs == ULONG_MAX) 4989 jiffies_till_next_fqs = d; 4990 adjust_jiffies_till_sched_qs(); 4991 4992 /* If the compile-time values are accurate, just leave. */ 4993 if (rcu_fanout_leaf == RCU_FANOUT_LEAF && 4994 nr_cpu_ids == NR_CPUS) 4995 return; 4996 pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n", 4997 rcu_fanout_leaf, nr_cpu_ids); 4998 4999 /* 5000 * The boot-time rcu_fanout_leaf parameter must be at least two 5001 * and cannot exceed the number of bits in the rcu_node masks. 5002 * Complain and fall back to the compile-time values if this 5003 * limit is exceeded. 5004 */ 5005 if (rcu_fanout_leaf < 2 || rcu_fanout_leaf > BITS_PER_LONG) { 5006 rcu_fanout_leaf = RCU_FANOUT_LEAF; 5007 WARN_ON(1); 5008 return; 5009 } 5010 5011 /* 5012 * Compute number of nodes that can be handled an rcu_node tree 5013 * with the given number of levels. 5014 */ 5015 rcu_capacity[0] = rcu_fanout_leaf; 5016 for (i = 1; i < RCU_NUM_LVLS; i++) 5017 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT; 5018 5019 /* 5020 * The tree must be able to accommodate the configured number of CPUs. 5021 * If this limit is exceeded, fall back to the compile-time values. 5022 */ 5023 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) { 5024 rcu_fanout_leaf = RCU_FANOUT_LEAF; 5025 WARN_ON(1); 5026 return; 5027 } 5028 5029 /* Calculate the number of levels in the tree. */ 5030 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) { 5031 } 5032 rcu_num_lvls = i + 1; 5033 5034 /* Calculate the number of rcu_nodes at each level of the tree. */ 5035 for (i = 0; i < rcu_num_lvls; i++) { 5036 int cap = rcu_capacity[(rcu_num_lvls - 1) - i]; 5037 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap); 5038 } 5039 5040 /* Calculate the total number of rcu_node structures. */ 5041 rcu_num_nodes = 0; 5042 for (i = 0; i < rcu_num_lvls; i++) 5043 rcu_num_nodes += num_rcu_lvl[i]; 5044 } 5045 5046 /* 5047 * Dump out the structure of the rcu_node combining tree associated 5048 * with the rcu_state structure. 5049 */ 5050 static void __init rcu_dump_rcu_node_tree(void) 5051 { 5052 int level = 0; 5053 struct rcu_node *rnp; 5054 5055 pr_info("rcu_node tree layout dump\n"); 5056 pr_info(" "); 5057 rcu_for_each_node_breadth_first(rnp) { 5058 if (rnp->level != level) { 5059 pr_cont("\n"); 5060 pr_info(" "); 5061 level = rnp->level; 5062 } 5063 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum); 5064 } 5065 pr_cont("\n"); 5066 } 5067 5068 struct workqueue_struct *rcu_gp_wq; 5069 5070 static struct cpumask rcu_stall_cpumask; 5071 static struct cpumask rcu_exp_stall_cpumask; 5072 5073 void __init rcu_init(void) 5074 { 5075 int cpu = smp_processor_id(); 5076 5077 rcu_early_boot_tests(); 5078 5079 rcu_bootup_announce(); 5080 sanitize_kthread_prio(); 5081 rcu_init_geometry(); 5082 rcu_init_one(); 5083 if (dump_tree) 5084 rcu_dump_rcu_node_tree(); 5085 if (use_softirq) 5086 open_softirq(RCU_SOFTIRQ, rcu_core_si); 5087 5088 /* 5089 * We don't need protection against CPU-hotplug here because 5090 * this is called early in boot, before either interrupts 5091 * or the scheduler are operational. 5092 */ 5093 pm_notifier(rcu_pm_notify, 0); 5094 WARN_ON(num_online_cpus() > 1); // Only one CPU this early in boot. 5095 rcutree_prepare_cpu(cpu); 5096 rcutree_report_cpu_starting(cpu); 5097 rcutree_online_cpu(cpu); 5098 5099 /* Create workqueue for Tree SRCU and for expedited GPs. */ 5100 rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM | WQ_PERCPU, 0); 5101 WARN_ON(!rcu_gp_wq); 5102 5103 sync_wq = alloc_workqueue("sync_wq", WQ_MEM_RECLAIM | WQ_UNBOUND, 0); 5104 WARN_ON(!sync_wq); 5105 5106 /* Fill in default value for rcutree.qovld boot parameter. */ 5107 /* -After- the rcu_node ->lock fields are initialized! */ 5108 if (qovld < 0) 5109 qovld_calc = DEFAULT_RCU_QOVLD_MULT * qhimark; 5110 else 5111 qovld_calc = qovld; 5112 5113 // Kick-start in case any polled grace periods started early. 5114 (void)start_poll_synchronize_rcu_expedited(); 5115 5116 rcu_test_sync_prims(); 5117 5118 tasks_cblist_init_generic(); 5119 } 5120 5121 #include "tree_stall.h" 5122 #include "tree_exp.h" 5123 #include "tree_nocb.h" 5124 #include "tree_plugin.h" 5125