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/sched.h> 28 #include <linux/sched/debug.h> 29 #include <linux/nmi.h> 30 #include <linux/atomic.h> 31 #include <linux/bitops.h> 32 #include <linux/export.h> 33 #include <linux/completion.h> 34 #include <linux/moduleparam.h> 35 #include <linux/percpu.h> 36 #include <linux/notifier.h> 37 #include <linux/cpu.h> 38 #include <linux/mutex.h> 39 #include <linux/time.h> 40 #include <linux/kernel_stat.h> 41 #include <linux/wait.h> 42 #include <linux/kthread.h> 43 #include <uapi/linux/sched/types.h> 44 #include <linux/prefetch.h> 45 #include <linux/delay.h> 46 #include <linux/random.h> 47 #include <linux/trace_events.h> 48 #include <linux/suspend.h> 49 #include <linux/ftrace.h> 50 #include <linux/tick.h> 51 #include <linux/sysrq.h> 52 #include <linux/kprobes.h> 53 #include <linux/gfp.h> 54 #include <linux/oom.h> 55 #include <linux/smpboot.h> 56 #include <linux/jiffies.h> 57 #include <linux/slab.h> 58 #include <linux/sched/isolation.h> 59 #include <linux/sched/clock.h> 60 #include "../time/tick-internal.h" 61 62 #include "tree.h" 63 #include "rcu.h" 64 65 #ifdef MODULE_PARAM_PREFIX 66 #undef MODULE_PARAM_PREFIX 67 #endif 68 #define MODULE_PARAM_PREFIX "rcutree." 69 70 /* Data structures. */ 71 72 /* 73 * Steal a bit from the bottom of ->dynticks for idle entry/exit 74 * control. Initially this is for TLB flushing. 75 */ 76 #define RCU_DYNTICK_CTRL_MASK 0x1 77 #define RCU_DYNTICK_CTRL_CTR (RCU_DYNTICK_CTRL_MASK + 1) 78 #ifndef rcu_eqs_special_exit 79 #define rcu_eqs_special_exit() do { } while (0) 80 #endif 81 82 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = { 83 .dynticks_nesting = 1, 84 .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE, 85 .dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR), 86 }; 87 static struct rcu_state rcu_state = { 88 .level = { &rcu_state.node[0] }, 89 .gp_state = RCU_GP_IDLE, 90 .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT, 91 .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex), 92 .name = RCU_NAME, 93 .abbr = RCU_ABBR, 94 .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex), 95 .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex), 96 .ofl_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.ofl_lock), 97 }; 98 99 /* Dump rcu_node combining tree at boot to verify correct setup. */ 100 static bool dump_tree; 101 module_param(dump_tree, bool, 0444); 102 /* By default, use RCU_SOFTIRQ instead of rcuc kthreads. */ 103 static bool use_softirq = 1; 104 module_param(use_softirq, bool, 0444); 105 /* Control rcu_node-tree auto-balancing at boot time. */ 106 static bool rcu_fanout_exact; 107 module_param(rcu_fanout_exact, bool, 0444); 108 /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */ 109 static int rcu_fanout_leaf = RCU_FANOUT_LEAF; 110 module_param(rcu_fanout_leaf, int, 0444); 111 int rcu_num_lvls __read_mostly = RCU_NUM_LVLS; 112 /* Number of rcu_nodes at specified level. */ 113 int num_rcu_lvl[] = NUM_RCU_LVL_INIT; 114 int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */ 115 116 /* 117 * The rcu_scheduler_active variable is initialized to the value 118 * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the 119 * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE, 120 * RCU can assume that there is but one task, allowing RCU to (for example) 121 * optimize synchronize_rcu() to a simple barrier(). When this variable 122 * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required 123 * to detect real grace periods. This variable is also used to suppress 124 * boot-time false positives from lockdep-RCU error checking. Finally, it 125 * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU 126 * is fully initialized, including all of its kthreads having been spawned. 127 */ 128 int rcu_scheduler_active __read_mostly; 129 EXPORT_SYMBOL_GPL(rcu_scheduler_active); 130 131 /* 132 * The rcu_scheduler_fully_active variable transitions from zero to one 133 * during the early_initcall() processing, which is after the scheduler 134 * is capable of creating new tasks. So RCU processing (for example, 135 * creating tasks for RCU priority boosting) must be delayed until after 136 * rcu_scheduler_fully_active transitions from zero to one. We also 137 * currently delay invocation of any RCU callbacks until after this point. 138 * 139 * It might later prove better for people registering RCU callbacks during 140 * early boot to take responsibility for these callbacks, but one step at 141 * a time. 142 */ 143 static int rcu_scheduler_fully_active __read_mostly; 144 145 static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp, 146 unsigned long gps, unsigned long flags); 147 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf); 148 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf); 149 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu); 150 static void invoke_rcu_core(void); 151 static void rcu_report_exp_rdp(struct rcu_data *rdp); 152 static void sync_sched_exp_online_cleanup(int cpu); 153 static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp); 154 155 /* rcuc/rcub kthread realtime priority */ 156 static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0; 157 module_param(kthread_prio, int, 0444); 158 159 /* Delay in jiffies for grace-period initialization delays, debug only. */ 160 161 static int gp_preinit_delay; 162 module_param(gp_preinit_delay, int, 0444); 163 static int gp_init_delay; 164 module_param(gp_init_delay, int, 0444); 165 static int gp_cleanup_delay; 166 module_param(gp_cleanup_delay, int, 0444); 167 168 /* Retrieve RCU kthreads priority for rcutorture */ 169 int rcu_get_gp_kthreads_prio(void) 170 { 171 return kthread_prio; 172 } 173 EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio); 174 175 /* 176 * Number of grace periods between delays, normalized by the duration of 177 * the delay. The longer the delay, the more the grace periods between 178 * each delay. The reason for this normalization is that it means that, 179 * for non-zero delays, the overall slowdown of grace periods is constant 180 * regardless of the duration of the delay. This arrangement balances 181 * the need for long delays to increase some race probabilities with the 182 * need for fast grace periods to increase other race probabilities. 183 */ 184 #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */ 185 186 /* 187 * Compute the mask of online CPUs for the specified rcu_node structure. 188 * This will not be stable unless the rcu_node structure's ->lock is 189 * held, but the bit corresponding to the current CPU will be stable 190 * in most contexts. 191 */ 192 static unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp) 193 { 194 return READ_ONCE(rnp->qsmaskinitnext); 195 } 196 197 /* 198 * Return true if an RCU grace period is in progress. The READ_ONCE()s 199 * permit this function to be invoked without holding the root rcu_node 200 * structure's ->lock, but of course results can be subject to change. 201 */ 202 static int rcu_gp_in_progress(void) 203 { 204 return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq)); 205 } 206 207 /* 208 * Return the number of callbacks queued on the specified CPU. 209 * Handles both the nocbs and normal cases. 210 */ 211 static long rcu_get_n_cbs_cpu(int cpu) 212 { 213 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 214 215 if (rcu_segcblist_is_enabled(&rdp->cblist)) 216 return rcu_segcblist_n_cbs(&rdp->cblist); 217 return 0; 218 } 219 220 void rcu_softirq_qs(void) 221 { 222 rcu_qs(); 223 rcu_preempt_deferred_qs(current); 224 } 225 226 /* 227 * Record entry into an extended quiescent state. This is only to be 228 * called when not already in an extended quiescent state. 229 */ 230 static void rcu_dynticks_eqs_enter(void) 231 { 232 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 233 int seq; 234 235 /* 236 * CPUs seeing atomic_add_return() must see prior RCU read-side 237 * critical sections, and we also must force ordering with the 238 * next idle sojourn. 239 */ 240 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks); 241 /* Better be in an extended quiescent state! */ 242 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && 243 (seq & RCU_DYNTICK_CTRL_CTR)); 244 /* Better not have special action (TLB flush) pending! */ 245 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && 246 (seq & RCU_DYNTICK_CTRL_MASK)); 247 } 248 249 /* 250 * Record exit from an extended quiescent state. This is only to be 251 * called from an extended quiescent state. 252 */ 253 static void rcu_dynticks_eqs_exit(void) 254 { 255 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 256 int seq; 257 258 /* 259 * CPUs seeing atomic_add_return() must see prior idle sojourns, 260 * and we also must force ordering with the next RCU read-side 261 * critical section. 262 */ 263 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks); 264 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && 265 !(seq & RCU_DYNTICK_CTRL_CTR)); 266 if (seq & RCU_DYNTICK_CTRL_MASK) { 267 atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdp->dynticks); 268 smp_mb__after_atomic(); /* _exit after clearing mask. */ 269 /* Prefer duplicate flushes to losing a flush. */ 270 rcu_eqs_special_exit(); 271 } 272 } 273 274 /* 275 * Reset the current CPU's ->dynticks counter to indicate that the 276 * newly onlined CPU is no longer in an extended quiescent state. 277 * This will either leave the counter unchanged, or increment it 278 * to the next non-quiescent value. 279 * 280 * The non-atomic test/increment sequence works because the upper bits 281 * of the ->dynticks counter are manipulated only by the corresponding CPU, 282 * or when the corresponding CPU is offline. 283 */ 284 static void rcu_dynticks_eqs_online(void) 285 { 286 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 287 288 if (atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR) 289 return; 290 atomic_add(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks); 291 } 292 293 /* 294 * Is the current CPU in an extended quiescent state? 295 * 296 * No ordering, as we are sampling CPU-local information. 297 */ 298 static bool rcu_dynticks_curr_cpu_in_eqs(void) 299 { 300 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 301 302 return !(atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR); 303 } 304 305 /* 306 * Snapshot the ->dynticks counter with full ordering so as to allow 307 * stable comparison of this counter with past and future snapshots. 308 */ 309 static int rcu_dynticks_snap(struct rcu_data *rdp) 310 { 311 int snap = atomic_add_return(0, &rdp->dynticks); 312 313 return snap & ~RCU_DYNTICK_CTRL_MASK; 314 } 315 316 /* 317 * Return true if the snapshot returned from rcu_dynticks_snap() 318 * indicates that RCU is in an extended quiescent state. 319 */ 320 static bool rcu_dynticks_in_eqs(int snap) 321 { 322 return !(snap & RCU_DYNTICK_CTRL_CTR); 323 } 324 325 /* 326 * Return true if the CPU corresponding to the specified rcu_data 327 * structure has spent some time in an extended quiescent state since 328 * rcu_dynticks_snap() returned the specified snapshot. 329 */ 330 static bool rcu_dynticks_in_eqs_since(struct rcu_data *rdp, int snap) 331 { 332 return snap != rcu_dynticks_snap(rdp); 333 } 334 335 /* 336 * Set the special (bottom) bit of the specified CPU so that it 337 * will take special action (such as flushing its TLB) on the 338 * next exit from an extended quiescent state. Returns true if 339 * the bit was successfully set, or false if the CPU was not in 340 * an extended quiescent state. 341 */ 342 bool rcu_eqs_special_set(int cpu) 343 { 344 int old; 345 int new; 346 int new_old; 347 struct rcu_data *rdp = &per_cpu(rcu_data, cpu); 348 349 new_old = atomic_read(&rdp->dynticks); 350 do { 351 old = new_old; 352 if (old & RCU_DYNTICK_CTRL_CTR) 353 return false; 354 new = old | RCU_DYNTICK_CTRL_MASK; 355 new_old = atomic_cmpxchg(&rdp->dynticks, old, new); 356 } while (new_old != old); 357 return true; 358 } 359 360 /* 361 * Let the RCU core know that this CPU has gone through the scheduler, 362 * which is a quiescent state. This is called when the need for a 363 * quiescent state is urgent, so we burn an atomic operation and full 364 * memory barriers to let the RCU core know about it, regardless of what 365 * this CPU might (or might not) do in the near future. 366 * 367 * We inform the RCU core by emulating a zero-duration dyntick-idle period. 368 * 369 * The caller must have disabled interrupts and must not be idle. 370 */ 371 void rcu_momentary_dyntick_idle(void) 372 { 373 int special; 374 375 raw_cpu_write(rcu_data.rcu_need_heavy_qs, false); 376 special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR, 377 &this_cpu_ptr(&rcu_data)->dynticks); 378 /* It is illegal to call this from idle state. */ 379 WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR)); 380 rcu_preempt_deferred_qs(current); 381 } 382 EXPORT_SYMBOL_GPL(rcu_momentary_dyntick_idle); 383 384 /** 385 * rcu_is_cpu_rrupt_from_idle - see if interrupted from idle 386 * 387 * If the current CPU is idle and running at a first-level (not nested) 388 * interrupt from idle, return true. The caller must have at least 389 * disabled preemption. 390 */ 391 static int rcu_is_cpu_rrupt_from_idle(void) 392 { 393 /* Called only from within the scheduling-clock interrupt */ 394 lockdep_assert_in_irq(); 395 396 /* Check for counter underflows */ 397 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nesting) < 0, 398 "RCU dynticks_nesting counter underflow!"); 399 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nmi_nesting) <= 0, 400 "RCU dynticks_nmi_nesting counter underflow/zero!"); 401 402 /* Are we at first interrupt nesting level? */ 403 if (__this_cpu_read(rcu_data.dynticks_nmi_nesting) != 1) 404 return false; 405 406 /* Does CPU appear to be idle from an RCU standpoint? */ 407 return __this_cpu_read(rcu_data.dynticks_nesting) == 0; 408 } 409 410 #define DEFAULT_RCU_BLIMIT 10 /* Maximum callbacks per rcu_do_batch ... */ 411 #define DEFAULT_MAX_RCU_BLIMIT 10000 /* ... even during callback flood. */ 412 static long blimit = DEFAULT_RCU_BLIMIT; 413 #define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */ 414 static long qhimark = DEFAULT_RCU_QHIMARK; 415 #define DEFAULT_RCU_QLOMARK 100 /* Once only this many pending, use blimit. */ 416 static long qlowmark = DEFAULT_RCU_QLOMARK; 417 #define DEFAULT_RCU_QOVLD_MULT 2 418 #define DEFAULT_RCU_QOVLD (DEFAULT_RCU_QOVLD_MULT * DEFAULT_RCU_QHIMARK) 419 static long qovld = DEFAULT_RCU_QOVLD; /* If this many pending, hammer QS. */ 420 static long qovld_calc = -1; /* No pre-initialization lock acquisitions! */ 421 422 module_param(blimit, long, 0444); 423 module_param(qhimark, long, 0444); 424 module_param(qlowmark, long, 0444); 425 module_param(qovld, long, 0444); 426 427 static ulong jiffies_till_first_fqs = ULONG_MAX; 428 static ulong jiffies_till_next_fqs = ULONG_MAX; 429 static bool rcu_kick_kthreads; 430 static int rcu_divisor = 7; 431 module_param(rcu_divisor, int, 0644); 432 433 /* Force an exit from rcu_do_batch() after 3 milliseconds. */ 434 static long rcu_resched_ns = 3 * NSEC_PER_MSEC; 435 module_param(rcu_resched_ns, long, 0644); 436 437 /* 438 * How long the grace period must be before we start recruiting 439 * quiescent-state help from rcu_note_context_switch(). 440 */ 441 static ulong jiffies_till_sched_qs = ULONG_MAX; 442 module_param(jiffies_till_sched_qs, ulong, 0444); 443 static ulong jiffies_to_sched_qs; /* See adjust_jiffies_till_sched_qs(). */ 444 module_param(jiffies_to_sched_qs, ulong, 0444); /* Display only! */ 445 446 /* 447 * Make sure that we give the grace-period kthread time to detect any 448 * idle CPUs before taking active measures to force quiescent states. 449 * However, don't go below 100 milliseconds, adjusted upwards for really 450 * large systems. 451 */ 452 static void adjust_jiffies_till_sched_qs(void) 453 { 454 unsigned long j; 455 456 /* If jiffies_till_sched_qs was specified, respect the request. */ 457 if (jiffies_till_sched_qs != ULONG_MAX) { 458 WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs); 459 return; 460 } 461 /* Otherwise, set to third fqs scan, but bound below on large system. */ 462 j = READ_ONCE(jiffies_till_first_fqs) + 463 2 * READ_ONCE(jiffies_till_next_fqs); 464 if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV) 465 j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV; 466 pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j); 467 WRITE_ONCE(jiffies_to_sched_qs, j); 468 } 469 470 static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp) 471 { 472 ulong j; 473 int ret = kstrtoul(val, 0, &j); 474 475 if (!ret) { 476 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j); 477 adjust_jiffies_till_sched_qs(); 478 } 479 return ret; 480 } 481 482 static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp) 483 { 484 ulong j; 485 int ret = kstrtoul(val, 0, &j); 486 487 if (!ret) { 488 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1)); 489 adjust_jiffies_till_sched_qs(); 490 } 491 return ret; 492 } 493 494 static struct kernel_param_ops first_fqs_jiffies_ops = { 495 .set = param_set_first_fqs_jiffies, 496 .get = param_get_ulong, 497 }; 498 499 static struct kernel_param_ops next_fqs_jiffies_ops = { 500 .set = param_set_next_fqs_jiffies, 501 .get = param_get_ulong, 502 }; 503 504 module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644); 505 module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644); 506 module_param(rcu_kick_kthreads, bool, 0644); 507 508 static void force_qs_rnp(int (*f)(struct rcu_data *rdp)); 509 static int rcu_pending(int user); 510 511 /* 512 * Return the number of RCU GPs completed thus far for debug & stats. 513 */ 514 unsigned long rcu_get_gp_seq(void) 515 { 516 return READ_ONCE(rcu_state.gp_seq); 517 } 518 EXPORT_SYMBOL_GPL(rcu_get_gp_seq); 519 520 /* 521 * Return the number of RCU expedited batches completed thus far for 522 * debug & stats. Odd numbers mean that a batch is in progress, even 523 * numbers mean idle. The value returned will thus be roughly double 524 * the cumulative batches since boot. 525 */ 526 unsigned long rcu_exp_batches_completed(void) 527 { 528 return rcu_state.expedited_sequence; 529 } 530 EXPORT_SYMBOL_GPL(rcu_exp_batches_completed); 531 532 /* 533 * Return the root node of the rcu_state structure. 534 */ 535 static struct rcu_node *rcu_get_root(void) 536 { 537 return &rcu_state.node[0]; 538 } 539 540 /* 541 * Send along grace-period-related data for rcutorture diagnostics. 542 */ 543 void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags, 544 unsigned long *gp_seq) 545 { 546 switch (test_type) { 547 case RCU_FLAVOR: 548 *flags = READ_ONCE(rcu_state.gp_flags); 549 *gp_seq = rcu_seq_current(&rcu_state.gp_seq); 550 break; 551 default: 552 break; 553 } 554 } 555 EXPORT_SYMBOL_GPL(rcutorture_get_gp_data); 556 557 /* 558 * Enter an RCU extended quiescent state, which can be either the 559 * idle loop or adaptive-tickless usermode execution. 560 * 561 * We crowbar the ->dynticks_nmi_nesting field to zero to allow for 562 * the possibility of usermode upcalls having messed up our count 563 * of interrupt nesting level during the prior busy period. 564 */ 565 static void rcu_eqs_enter(bool user) 566 { 567 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 568 569 WARN_ON_ONCE(rdp->dynticks_nmi_nesting != DYNTICK_IRQ_NONIDLE); 570 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); 571 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && 572 rdp->dynticks_nesting == 0); 573 if (rdp->dynticks_nesting != 1) { 574 rdp->dynticks_nesting--; 575 return; 576 } 577 578 lockdep_assert_irqs_disabled(); 579 trace_rcu_dyntick(TPS("Start"), rdp->dynticks_nesting, 0, atomic_read(&rdp->dynticks)); 580 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current)); 581 rdp = this_cpu_ptr(&rcu_data); 582 do_nocb_deferred_wakeup(rdp); 583 rcu_prepare_for_idle(); 584 rcu_preempt_deferred_qs(current); 585 WRITE_ONCE(rdp->dynticks_nesting, 0); /* Avoid irq-access tearing. */ 586 rcu_dynticks_eqs_enter(); 587 rcu_dynticks_task_enter(); 588 } 589 590 /** 591 * rcu_idle_enter - inform RCU that current CPU is entering idle 592 * 593 * Enter idle mode, in other words, -leave- the mode in which RCU 594 * read-side critical sections can occur. (Though RCU read-side 595 * critical sections can occur in irq handlers in idle, a possibility 596 * handled by irq_enter() and irq_exit().) 597 * 598 * If you add or remove a call to rcu_idle_enter(), be sure to test with 599 * CONFIG_RCU_EQS_DEBUG=y. 600 */ 601 void rcu_idle_enter(void) 602 { 603 lockdep_assert_irqs_disabled(); 604 rcu_eqs_enter(false); 605 } 606 607 #ifdef CONFIG_NO_HZ_FULL 608 /** 609 * rcu_user_enter - inform RCU that we are resuming userspace. 610 * 611 * Enter RCU idle mode right before resuming userspace. No use of RCU 612 * is permitted between this call and rcu_user_exit(). This way the 613 * CPU doesn't need to maintain the tick for RCU maintenance purposes 614 * when the CPU runs in userspace. 615 * 616 * If you add or remove a call to rcu_user_enter(), be sure to test with 617 * CONFIG_RCU_EQS_DEBUG=y. 618 */ 619 void rcu_user_enter(void) 620 { 621 lockdep_assert_irqs_disabled(); 622 rcu_eqs_enter(true); 623 } 624 #endif /* CONFIG_NO_HZ_FULL */ 625 626 /* 627 * If we are returning from the outermost NMI handler that interrupted an 628 * RCU-idle period, update rdp->dynticks and rdp->dynticks_nmi_nesting 629 * to let the RCU grace-period handling know that the CPU is back to 630 * being RCU-idle. 631 * 632 * If you add or remove a call to rcu_nmi_exit_common(), be sure to test 633 * with CONFIG_RCU_EQS_DEBUG=y. 634 */ 635 static __always_inline void rcu_nmi_exit_common(bool irq) 636 { 637 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 638 639 /* 640 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks. 641 * (We are exiting an NMI handler, so RCU better be paying attention 642 * to us!) 643 */ 644 WARN_ON_ONCE(rdp->dynticks_nmi_nesting <= 0); 645 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs()); 646 647 /* 648 * If the nesting level is not 1, the CPU wasn't RCU-idle, so 649 * leave it in non-RCU-idle state. 650 */ 651 if (rdp->dynticks_nmi_nesting != 1) { 652 trace_rcu_dyntick(TPS("--="), rdp->dynticks_nmi_nesting, rdp->dynticks_nmi_nesting - 2, 653 atomic_read(&rdp->dynticks)); 654 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* No store tearing. */ 655 rdp->dynticks_nmi_nesting - 2); 656 return; 657 } 658 659 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */ 660 trace_rcu_dyntick(TPS("Startirq"), rdp->dynticks_nmi_nesting, 0, atomic_read(&rdp->dynticks)); 661 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); /* Avoid store tearing. */ 662 663 if (irq) 664 rcu_prepare_for_idle(); 665 666 rcu_dynticks_eqs_enter(); 667 668 if (irq) 669 rcu_dynticks_task_enter(); 670 } 671 672 /** 673 * rcu_nmi_exit - inform RCU of exit from NMI context 674 * 675 * If you add or remove a call to rcu_nmi_exit(), be sure to test 676 * with CONFIG_RCU_EQS_DEBUG=y. 677 */ 678 void rcu_nmi_exit(void) 679 { 680 rcu_nmi_exit_common(false); 681 } 682 683 /** 684 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle 685 * 686 * Exit from an interrupt handler, which might possibly result in entering 687 * idle mode, in other words, leaving the mode in which read-side critical 688 * sections can occur. The caller must have disabled interrupts. 689 * 690 * This code assumes that the idle loop never does anything that might 691 * result in unbalanced calls to irq_enter() and irq_exit(). If your 692 * architecture's idle loop violates this assumption, RCU will give you what 693 * you deserve, good and hard. But very infrequently and irreproducibly. 694 * 695 * Use things like work queues to work around this limitation. 696 * 697 * You have been warned. 698 * 699 * If you add or remove a call to rcu_irq_exit(), be sure to test with 700 * CONFIG_RCU_EQS_DEBUG=y. 701 */ 702 void rcu_irq_exit(void) 703 { 704 lockdep_assert_irqs_disabled(); 705 rcu_nmi_exit_common(true); 706 } 707 708 /* 709 * Wrapper for rcu_irq_exit() where interrupts are enabled. 710 * 711 * If you add or remove a call to rcu_irq_exit_irqson(), be sure to test 712 * with CONFIG_RCU_EQS_DEBUG=y. 713 */ 714 void rcu_irq_exit_irqson(void) 715 { 716 unsigned long flags; 717 718 local_irq_save(flags); 719 rcu_irq_exit(); 720 local_irq_restore(flags); 721 } 722 723 /* 724 * Exit an RCU extended quiescent state, which can be either the 725 * idle loop or adaptive-tickless usermode execution. 726 * 727 * We crowbar the ->dynticks_nmi_nesting field to DYNTICK_IRQ_NONIDLE to 728 * allow for the possibility of usermode upcalls messing up our count of 729 * interrupt nesting level during the busy period that is just now starting. 730 */ 731 static void rcu_eqs_exit(bool user) 732 { 733 struct rcu_data *rdp; 734 long oldval; 735 736 lockdep_assert_irqs_disabled(); 737 rdp = this_cpu_ptr(&rcu_data); 738 oldval = rdp->dynticks_nesting; 739 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0); 740 if (oldval) { 741 rdp->dynticks_nesting++; 742 return; 743 } 744 rcu_dynticks_task_exit(); 745 rcu_dynticks_eqs_exit(); 746 rcu_cleanup_after_idle(); 747 trace_rcu_dyntick(TPS("End"), rdp->dynticks_nesting, 1, atomic_read(&rdp->dynticks)); 748 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current)); 749 WRITE_ONCE(rdp->dynticks_nesting, 1); 750 WARN_ON_ONCE(rdp->dynticks_nmi_nesting); 751 WRITE_ONCE(rdp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE); 752 } 753 754 /** 755 * rcu_idle_exit - inform RCU that current CPU is leaving idle 756 * 757 * Exit idle mode, in other words, -enter- the mode in which RCU 758 * read-side critical sections can occur. 759 * 760 * If you add or remove a call to rcu_idle_exit(), be sure to test with 761 * CONFIG_RCU_EQS_DEBUG=y. 762 */ 763 void rcu_idle_exit(void) 764 { 765 unsigned long flags; 766 767 local_irq_save(flags); 768 rcu_eqs_exit(false); 769 local_irq_restore(flags); 770 } 771 772 #ifdef CONFIG_NO_HZ_FULL 773 /** 774 * rcu_user_exit - inform RCU that we are exiting userspace. 775 * 776 * Exit RCU idle mode while entering the kernel because it can 777 * run a RCU read side critical section anytime. 778 * 779 * If you add or remove a call to rcu_user_exit(), be sure to test with 780 * CONFIG_RCU_EQS_DEBUG=y. 781 */ 782 void rcu_user_exit(void) 783 { 784 rcu_eqs_exit(1); 785 } 786 #endif /* CONFIG_NO_HZ_FULL */ 787 788 /** 789 * rcu_nmi_enter_common - inform RCU of entry to NMI context 790 * @irq: Is this call from rcu_irq_enter? 791 * 792 * If the CPU was idle from RCU's viewpoint, update rdp->dynticks and 793 * rdp->dynticks_nmi_nesting to let the RCU grace-period handling know 794 * that the CPU is active. This implementation permits nested NMIs, as 795 * long as the nesting level does not overflow an int. (You will probably 796 * run out of stack space first.) 797 * 798 * If you add or remove a call to rcu_nmi_enter_common(), be sure to test 799 * with CONFIG_RCU_EQS_DEBUG=y. 800 */ 801 static __always_inline void rcu_nmi_enter_common(bool irq) 802 { 803 long incby = 2; 804 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 805 806 /* Complain about underflow. */ 807 WARN_ON_ONCE(rdp->dynticks_nmi_nesting < 0); 808 809 /* 810 * If idle from RCU viewpoint, atomically increment ->dynticks 811 * to mark non-idle and increment ->dynticks_nmi_nesting by one. 812 * Otherwise, increment ->dynticks_nmi_nesting by two. This means 813 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed 814 * to be in the outermost NMI handler that interrupted an RCU-idle 815 * period (observation due to Andy Lutomirski). 816 */ 817 if (rcu_dynticks_curr_cpu_in_eqs()) { 818 819 if (irq) 820 rcu_dynticks_task_exit(); 821 822 rcu_dynticks_eqs_exit(); 823 824 if (irq) 825 rcu_cleanup_after_idle(); 826 827 incby = 1; 828 } else if (tick_nohz_full_cpu(rdp->cpu) && 829 rdp->dynticks_nmi_nesting == DYNTICK_IRQ_NONIDLE && 830 READ_ONCE(rdp->rcu_urgent_qs) && 831 !READ_ONCE(rdp->rcu_forced_tick)) { 832 raw_spin_lock_rcu_node(rdp->mynode); 833 // Recheck under lock. 834 if (rdp->rcu_urgent_qs && !rdp->rcu_forced_tick) { 835 WRITE_ONCE(rdp->rcu_forced_tick, true); 836 tick_dep_set_cpu(rdp->cpu, TICK_DEP_BIT_RCU); 837 } 838 raw_spin_unlock_rcu_node(rdp->mynode); 839 } 840 trace_rcu_dyntick(incby == 1 ? TPS("Endirq") : TPS("++="), 841 rdp->dynticks_nmi_nesting, 842 rdp->dynticks_nmi_nesting + incby, atomic_read(&rdp->dynticks)); 843 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* Prevent store tearing. */ 844 rdp->dynticks_nmi_nesting + incby); 845 barrier(); 846 } 847 848 /** 849 * rcu_nmi_enter - inform RCU of entry to NMI context 850 */ 851 void rcu_nmi_enter(void) 852 { 853 rcu_nmi_enter_common(false); 854 } 855 NOKPROBE_SYMBOL(rcu_nmi_enter); 856 857 /** 858 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle 859 * 860 * Enter an interrupt handler, which might possibly result in exiting 861 * idle mode, in other words, entering the mode in which read-side critical 862 * sections can occur. The caller must have disabled interrupts. 863 * 864 * Note that the Linux kernel is fully capable of entering an interrupt 865 * handler that it never exits, for example when doing upcalls to user mode! 866 * This code assumes that the idle loop never does upcalls to user mode. 867 * If your architecture's idle loop does do upcalls to user mode (or does 868 * anything else that results in unbalanced calls to the irq_enter() and 869 * irq_exit() functions), RCU will give you what you deserve, good and hard. 870 * But very infrequently and irreproducibly. 871 * 872 * Use things like work queues to work around this limitation. 873 * 874 * You have been warned. 875 * 876 * If you add or remove a call to rcu_irq_enter(), be sure to test with 877 * CONFIG_RCU_EQS_DEBUG=y. 878 */ 879 void rcu_irq_enter(void) 880 { 881 lockdep_assert_irqs_disabled(); 882 rcu_nmi_enter_common(true); 883 } 884 885 /* 886 * Wrapper for rcu_irq_enter() where interrupts are enabled. 887 * 888 * If you add or remove a call to rcu_irq_enter_irqson(), be sure to test 889 * with CONFIG_RCU_EQS_DEBUG=y. 890 */ 891 void rcu_irq_enter_irqson(void) 892 { 893 unsigned long flags; 894 895 local_irq_save(flags); 896 rcu_irq_enter(); 897 local_irq_restore(flags); 898 } 899 900 /* 901 * If any sort of urgency was applied to the current CPU (for example, 902 * the scheduler-clock interrupt was enabled on a nohz_full CPU) in order 903 * to get to a quiescent state, disable it. 904 */ 905 static void rcu_disable_urgency_upon_qs(struct rcu_data *rdp) 906 { 907 raw_lockdep_assert_held_rcu_node(rdp->mynode); 908 WRITE_ONCE(rdp->rcu_urgent_qs, false); 909 WRITE_ONCE(rdp->rcu_need_heavy_qs, false); 910 if (tick_nohz_full_cpu(rdp->cpu) && rdp->rcu_forced_tick) { 911 tick_dep_clear_cpu(rdp->cpu, TICK_DEP_BIT_RCU); 912 WRITE_ONCE(rdp->rcu_forced_tick, false); 913 } 914 } 915 916 /** 917 * rcu_is_watching - see if RCU thinks that the current CPU is not idle 918 * 919 * Return true if RCU is watching the running CPU, which means that this 920 * CPU can safely enter RCU read-side critical sections. In other words, 921 * if the current CPU is not in its idle loop or is in an interrupt or 922 * NMI handler, return true. 923 */ 924 bool notrace rcu_is_watching(void) 925 { 926 bool ret; 927 928 preempt_disable_notrace(); 929 ret = !rcu_dynticks_curr_cpu_in_eqs(); 930 preempt_enable_notrace(); 931 return ret; 932 } 933 EXPORT_SYMBOL_GPL(rcu_is_watching); 934 935 /* 936 * If a holdout task is actually running, request an urgent quiescent 937 * state from its CPU. This is unsynchronized, so migrations can cause 938 * the request to go to the wrong CPU. Which is OK, all that will happen 939 * is that the CPU's next context switch will be a bit slower and next 940 * time around this task will generate another request. 941 */ 942 void rcu_request_urgent_qs_task(struct task_struct *t) 943 { 944 int cpu; 945 946 barrier(); 947 cpu = task_cpu(t); 948 if (!task_curr(t)) 949 return; /* This task is not running on that CPU. */ 950 smp_store_release(per_cpu_ptr(&rcu_data.rcu_urgent_qs, cpu), true); 951 } 952 953 #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) 954 955 /* 956 * Is the current CPU online as far as RCU is concerned? 957 * 958 * Disable preemption to avoid false positives that could otherwise 959 * happen due to the current CPU number being sampled, this task being 960 * preempted, its old CPU being taken offline, resuming on some other CPU, 961 * then determining that its old CPU is now offline. 962 * 963 * Disable checking if in an NMI handler because we cannot safely 964 * report errors from NMI handlers anyway. In addition, it is OK to use 965 * RCU on an offline processor during initial boot, hence the check for 966 * rcu_scheduler_fully_active. 967 */ 968 bool rcu_lockdep_current_cpu_online(void) 969 { 970 struct rcu_data *rdp; 971 struct rcu_node *rnp; 972 bool ret = false; 973 974 if (in_nmi() || !rcu_scheduler_fully_active) 975 return true; 976 preempt_disable(); 977 rdp = this_cpu_ptr(&rcu_data); 978 rnp = rdp->mynode; 979 if (rdp->grpmask & rcu_rnp_online_cpus(rnp)) 980 ret = true; 981 preempt_enable(); 982 return ret; 983 } 984 EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online); 985 986 #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */ 987 988 /* 989 * We are reporting a quiescent state on behalf of some other CPU, so 990 * it is our responsibility to check for and handle potential overflow 991 * of the rcu_node ->gp_seq counter with respect to the rcu_data counters. 992 * After all, the CPU might be in deep idle state, and thus executing no 993 * code whatsoever. 994 */ 995 static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp) 996 { 997 raw_lockdep_assert_held_rcu_node(rnp); 998 if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + ULONG_MAX / 4, 999 rnp->gp_seq)) 1000 WRITE_ONCE(rdp->gpwrap, true); 1001 if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq)) 1002 rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4; 1003 } 1004 1005 /* 1006 * Snapshot the specified CPU's dynticks counter so that we can later 1007 * credit them with an implicit quiescent state. Return 1 if this CPU 1008 * is in dynticks idle mode, which is an extended quiescent state. 1009 */ 1010 static int dyntick_save_progress_counter(struct rcu_data *rdp) 1011 { 1012 rdp->dynticks_snap = rcu_dynticks_snap(rdp); 1013 if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) { 1014 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti")); 1015 rcu_gpnum_ovf(rdp->mynode, rdp); 1016 return 1; 1017 } 1018 return 0; 1019 } 1020 1021 /* 1022 * Return true if the specified CPU has passed through a quiescent 1023 * state by virtue of being in or having passed through an dynticks 1024 * idle state since the last call to dyntick_save_progress_counter() 1025 * for this same CPU, or by virtue of having been offline. 1026 */ 1027 static int rcu_implicit_dynticks_qs(struct rcu_data *rdp) 1028 { 1029 unsigned long jtsq; 1030 bool *rnhqp; 1031 bool *ruqp; 1032 struct rcu_node *rnp = rdp->mynode; 1033 1034 /* 1035 * If the CPU passed through or entered a dynticks idle phase with 1036 * no active irq/NMI handlers, then we can safely pretend that the CPU 1037 * already acknowledged the request to pass through a quiescent 1038 * state. Either way, that CPU cannot possibly be in an RCU 1039 * read-side critical section that started before the beginning 1040 * of the current RCU grace period. 1041 */ 1042 if (rcu_dynticks_in_eqs_since(rdp, rdp->dynticks_snap)) { 1043 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti")); 1044 rcu_gpnum_ovf(rnp, rdp); 1045 return 1; 1046 } 1047 1048 /* If waiting too long on an offline CPU, complain. */ 1049 if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp)) && 1050 time_after(jiffies, rcu_state.gp_start + HZ)) { 1051 bool onl; 1052 struct rcu_node *rnp1; 1053 1054 WARN_ON(1); /* Offline CPUs are supposed to report QS! */ 1055 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n", 1056 __func__, rnp->grplo, rnp->grphi, rnp->level, 1057 (long)rnp->gp_seq, (long)rnp->completedqs); 1058 for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent) 1059 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n", 1060 __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask); 1061 onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp)); 1062 pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n", 1063 __func__, rdp->cpu, ".o"[onl], 1064 (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags, 1065 (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags); 1066 return 1; /* Break things loose after complaining. */ 1067 } 1068 1069 /* 1070 * A CPU running for an extended time within the kernel can 1071 * delay RCU grace periods: (1) At age jiffies_to_sched_qs, 1072 * set .rcu_urgent_qs, (2) At age 2*jiffies_to_sched_qs, set 1073 * both .rcu_need_heavy_qs and .rcu_urgent_qs. Note that the 1074 * unsynchronized assignments to the per-CPU rcu_need_heavy_qs 1075 * variable are safe because the assignments are repeated if this 1076 * CPU failed to pass through a quiescent state. This code 1077 * also checks .jiffies_resched in case jiffies_to_sched_qs 1078 * is set way high. 1079 */ 1080 jtsq = READ_ONCE(jiffies_to_sched_qs); 1081 ruqp = per_cpu_ptr(&rcu_data.rcu_urgent_qs, rdp->cpu); 1082 rnhqp = &per_cpu(rcu_data.rcu_need_heavy_qs, rdp->cpu); 1083 if (!READ_ONCE(*rnhqp) && 1084 (time_after(jiffies, rcu_state.gp_start + jtsq * 2) || 1085 time_after(jiffies, rcu_state.jiffies_resched) || 1086 rcu_state.cbovld)) { 1087 WRITE_ONCE(*rnhqp, true); 1088 /* Store rcu_need_heavy_qs before rcu_urgent_qs. */ 1089 smp_store_release(ruqp, true); 1090 } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) { 1091 WRITE_ONCE(*ruqp, true); 1092 } 1093 1094 /* 1095 * NO_HZ_FULL CPUs can run in-kernel without rcu_sched_clock_irq! 1096 * The above code handles this, but only for straight cond_resched(). 1097 * And some in-kernel loops check need_resched() before calling 1098 * cond_resched(), which defeats the above code for CPUs that are 1099 * running in-kernel with scheduling-clock interrupts disabled. 1100 * So hit them over the head with the resched_cpu() hammer! 1101 */ 1102 if (tick_nohz_full_cpu(rdp->cpu) && 1103 (time_after(jiffies, READ_ONCE(rdp->last_fqs_resched) + jtsq * 3) || 1104 rcu_state.cbovld)) { 1105 WRITE_ONCE(*ruqp, true); 1106 resched_cpu(rdp->cpu); 1107 WRITE_ONCE(rdp->last_fqs_resched, jiffies); 1108 } 1109 1110 /* 1111 * If more than halfway to RCU CPU stall-warning time, invoke 1112 * resched_cpu() more frequently to try to loosen things up a bit. 1113 * Also check to see if the CPU is getting hammered with interrupts, 1114 * but only once per grace period, just to keep the IPIs down to 1115 * a dull roar. 1116 */ 1117 if (time_after(jiffies, rcu_state.jiffies_resched)) { 1118 if (time_after(jiffies, 1119 READ_ONCE(rdp->last_fqs_resched) + jtsq)) { 1120 resched_cpu(rdp->cpu); 1121 WRITE_ONCE(rdp->last_fqs_resched, jiffies); 1122 } 1123 if (IS_ENABLED(CONFIG_IRQ_WORK) && 1124 !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq && 1125 (rnp->ffmask & rdp->grpmask)) { 1126 init_irq_work(&rdp->rcu_iw, rcu_iw_handler); 1127 rdp->rcu_iw_pending = true; 1128 rdp->rcu_iw_gp_seq = rnp->gp_seq; 1129 irq_work_queue_on(&rdp->rcu_iw, rdp->cpu); 1130 } 1131 } 1132 1133 return 0; 1134 } 1135 1136 /* Trace-event wrapper function for trace_rcu_future_grace_period. */ 1137 static void trace_rcu_this_gp(struct rcu_node *rnp, struct rcu_data *rdp, 1138 unsigned long gp_seq_req, const char *s) 1139 { 1140 trace_rcu_future_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq), 1141 gp_seq_req, rnp->level, 1142 rnp->grplo, rnp->grphi, s); 1143 } 1144 1145 /* 1146 * rcu_start_this_gp - Request the start of a particular grace period 1147 * @rnp_start: The leaf node of the CPU from which to start. 1148 * @rdp: The rcu_data corresponding to the CPU from which to start. 1149 * @gp_seq_req: The gp_seq of the grace period to start. 1150 * 1151 * Start the specified grace period, as needed to handle newly arrived 1152 * callbacks. The required future grace periods are recorded in each 1153 * rcu_node structure's ->gp_seq_needed field. Returns true if there 1154 * is reason to awaken the grace-period kthread. 1155 * 1156 * The caller must hold the specified rcu_node structure's ->lock, which 1157 * is why the caller is responsible for waking the grace-period kthread. 1158 * 1159 * Returns true if the GP thread needs to be awakened else false. 1160 */ 1161 static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp, 1162 unsigned long gp_seq_req) 1163 { 1164 bool ret = false; 1165 struct rcu_node *rnp; 1166 1167 /* 1168 * Use funnel locking to either acquire the root rcu_node 1169 * structure's lock or bail out if the need for this grace period 1170 * has already been recorded -- or if that grace period has in 1171 * fact already started. If there is already a grace period in 1172 * progress in a non-leaf node, no recording is needed because the 1173 * end of the grace period will scan the leaf rcu_node structures. 1174 * Note that rnp_start->lock must not be released. 1175 */ 1176 raw_lockdep_assert_held_rcu_node(rnp_start); 1177 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, TPS("Startleaf")); 1178 for (rnp = rnp_start; 1; rnp = rnp->parent) { 1179 if (rnp != rnp_start) 1180 raw_spin_lock_rcu_node(rnp); 1181 if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) || 1182 rcu_seq_started(&rnp->gp_seq, gp_seq_req) || 1183 (rnp != rnp_start && 1184 rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) { 1185 trace_rcu_this_gp(rnp, rdp, gp_seq_req, 1186 TPS("Prestarted")); 1187 goto unlock_out; 1188 } 1189 WRITE_ONCE(rnp->gp_seq_needed, gp_seq_req); 1190 if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) { 1191 /* 1192 * We just marked the leaf or internal node, and a 1193 * grace period is in progress, which means that 1194 * rcu_gp_cleanup() will see the marking. Bail to 1195 * reduce contention. 1196 */ 1197 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, 1198 TPS("Startedleaf")); 1199 goto unlock_out; 1200 } 1201 if (rnp != rnp_start && rnp->parent != NULL) 1202 raw_spin_unlock_rcu_node(rnp); 1203 if (!rnp->parent) 1204 break; /* At root, and perhaps also leaf. */ 1205 } 1206 1207 /* If GP already in progress, just leave, otherwise start one. */ 1208 if (rcu_gp_in_progress()) { 1209 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedleafroot")); 1210 goto unlock_out; 1211 } 1212 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedroot")); 1213 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT); 1214 WRITE_ONCE(rcu_state.gp_req_activity, jiffies); 1215 if (!READ_ONCE(rcu_state.gp_kthread)) { 1216 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("NoGPkthread")); 1217 goto unlock_out; 1218 } 1219 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("newreq")); 1220 ret = true; /* Caller must wake GP kthread. */ 1221 unlock_out: 1222 /* Push furthest requested GP to leaf node and rcu_data structure. */ 1223 if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) { 1224 WRITE_ONCE(rnp_start->gp_seq_needed, rnp->gp_seq_needed); 1225 WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed); 1226 } 1227 if (rnp != rnp_start) 1228 raw_spin_unlock_rcu_node(rnp); 1229 return ret; 1230 } 1231 1232 /* 1233 * Clean up any old requests for the just-ended grace period. Also return 1234 * whether any additional grace periods have been requested. 1235 */ 1236 static bool rcu_future_gp_cleanup(struct rcu_node *rnp) 1237 { 1238 bool needmore; 1239 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 1240 1241 needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed); 1242 if (!needmore) 1243 rnp->gp_seq_needed = rnp->gp_seq; /* Avoid counter wrap. */ 1244 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq, 1245 needmore ? TPS("CleanupMore") : TPS("Cleanup")); 1246 return needmore; 1247 } 1248 1249 /* 1250 * Awaken the grace-period kthread. Don't do a self-awaken (unless in an 1251 * interrupt or softirq handler, in which case we just might immediately 1252 * sleep upon return, resulting in a grace-period hang), and don't bother 1253 * awakening when there is nothing for the grace-period kthread to do 1254 * (as in several CPUs raced to awaken, we lost), and finally don't try 1255 * to awaken a kthread that has not yet been created. If all those checks 1256 * are passed, track some debug information and awaken. 1257 * 1258 * So why do the self-wakeup when in an interrupt or softirq handler 1259 * in the grace-period kthread's context? Because the kthread might have 1260 * been interrupted just as it was going to sleep, and just after the final 1261 * pre-sleep check of the awaken condition. In this case, a wakeup really 1262 * is required, and is therefore supplied. 1263 */ 1264 static void rcu_gp_kthread_wake(void) 1265 { 1266 struct task_struct *t = READ_ONCE(rcu_state.gp_kthread); 1267 1268 if ((current == t && !in_irq() && !in_serving_softirq()) || 1269 !READ_ONCE(rcu_state.gp_flags) || !t) 1270 return; 1271 WRITE_ONCE(rcu_state.gp_wake_time, jiffies); 1272 WRITE_ONCE(rcu_state.gp_wake_seq, READ_ONCE(rcu_state.gp_seq)); 1273 swake_up_one(&rcu_state.gp_wq); 1274 } 1275 1276 /* 1277 * If there is room, assign a ->gp_seq number to any callbacks on this 1278 * CPU that have not already been assigned. Also accelerate any callbacks 1279 * that were previously assigned a ->gp_seq number that has since proven 1280 * to be too conservative, which can happen if callbacks get assigned a 1281 * ->gp_seq number while RCU is idle, but with reference to a non-root 1282 * rcu_node structure. This function is idempotent, so it does not hurt 1283 * to call it repeatedly. Returns an flag saying that we should awaken 1284 * the RCU grace-period kthread. 1285 * 1286 * The caller must hold rnp->lock with interrupts disabled. 1287 */ 1288 static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp) 1289 { 1290 unsigned long gp_seq_req; 1291 bool ret = false; 1292 1293 rcu_lockdep_assert_cblist_protected(rdp); 1294 raw_lockdep_assert_held_rcu_node(rnp); 1295 1296 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */ 1297 if (!rcu_segcblist_pend_cbs(&rdp->cblist)) 1298 return false; 1299 1300 /* 1301 * Callbacks are often registered with incomplete grace-period 1302 * information. Something about the fact that getting exact 1303 * information requires acquiring a global lock... RCU therefore 1304 * makes a conservative estimate of the grace period number at which 1305 * a given callback will become ready to invoke. The following 1306 * code checks this estimate and improves it when possible, thus 1307 * accelerating callback invocation to an earlier grace-period 1308 * number. 1309 */ 1310 gp_seq_req = rcu_seq_snap(&rcu_state.gp_seq); 1311 if (rcu_segcblist_accelerate(&rdp->cblist, gp_seq_req)) 1312 ret = rcu_start_this_gp(rnp, rdp, gp_seq_req); 1313 1314 /* Trace depending on how much we were able to accelerate. */ 1315 if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL)) 1316 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccWaitCB")); 1317 else 1318 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccReadyCB")); 1319 return ret; 1320 } 1321 1322 /* 1323 * Similar to rcu_accelerate_cbs(), but does not require that the leaf 1324 * rcu_node structure's ->lock be held. It consults the cached value 1325 * of ->gp_seq_needed in the rcu_data structure, and if that indicates 1326 * that a new grace-period request be made, invokes rcu_accelerate_cbs() 1327 * while holding the leaf rcu_node structure's ->lock. 1328 */ 1329 static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp, 1330 struct rcu_data *rdp) 1331 { 1332 unsigned long c; 1333 bool needwake; 1334 1335 rcu_lockdep_assert_cblist_protected(rdp); 1336 c = rcu_seq_snap(&rcu_state.gp_seq); 1337 if (!READ_ONCE(rdp->gpwrap) && ULONG_CMP_GE(rdp->gp_seq_needed, c)) { 1338 /* Old request still live, so mark recent callbacks. */ 1339 (void)rcu_segcblist_accelerate(&rdp->cblist, c); 1340 return; 1341 } 1342 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */ 1343 needwake = rcu_accelerate_cbs(rnp, rdp); 1344 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */ 1345 if (needwake) 1346 rcu_gp_kthread_wake(); 1347 } 1348 1349 /* 1350 * Move any callbacks whose grace period has completed to the 1351 * RCU_DONE_TAIL sublist, then compact the remaining sublists and 1352 * assign ->gp_seq numbers to any callbacks in the RCU_NEXT_TAIL 1353 * sublist. This function is idempotent, so it does not hurt to 1354 * invoke it repeatedly. As long as it is not invoked -too- often... 1355 * Returns true if the RCU grace-period kthread needs to be awakened. 1356 * 1357 * The caller must hold rnp->lock with interrupts disabled. 1358 */ 1359 static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp) 1360 { 1361 rcu_lockdep_assert_cblist_protected(rdp); 1362 raw_lockdep_assert_held_rcu_node(rnp); 1363 1364 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */ 1365 if (!rcu_segcblist_pend_cbs(&rdp->cblist)) 1366 return false; 1367 1368 /* 1369 * Find all callbacks whose ->gp_seq numbers indicate that they 1370 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist. 1371 */ 1372 rcu_segcblist_advance(&rdp->cblist, rnp->gp_seq); 1373 1374 /* Classify any remaining callbacks. */ 1375 return rcu_accelerate_cbs(rnp, rdp); 1376 } 1377 1378 /* 1379 * Move and classify callbacks, but only if doing so won't require 1380 * that the RCU grace-period kthread be awakened. 1381 */ 1382 static void __maybe_unused rcu_advance_cbs_nowake(struct rcu_node *rnp, 1383 struct rcu_data *rdp) 1384 { 1385 rcu_lockdep_assert_cblist_protected(rdp); 1386 if (!rcu_seq_state(rcu_seq_current(&rnp->gp_seq)) || 1387 !raw_spin_trylock_rcu_node(rnp)) 1388 return; 1389 WARN_ON_ONCE(rcu_advance_cbs(rnp, rdp)); 1390 raw_spin_unlock_rcu_node(rnp); 1391 } 1392 1393 /* 1394 * Update CPU-local rcu_data state to record the beginnings and ends of 1395 * grace periods. The caller must hold the ->lock of the leaf rcu_node 1396 * structure corresponding to the current CPU, and must have irqs disabled. 1397 * Returns true if the grace-period kthread needs to be awakened. 1398 */ 1399 static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp) 1400 { 1401 bool ret = false; 1402 bool need_qs; 1403 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) && 1404 rcu_segcblist_is_offloaded(&rdp->cblist); 1405 1406 raw_lockdep_assert_held_rcu_node(rnp); 1407 1408 if (rdp->gp_seq == rnp->gp_seq) 1409 return false; /* Nothing to do. */ 1410 1411 /* Handle the ends of any preceding grace periods first. */ 1412 if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) || 1413 unlikely(READ_ONCE(rdp->gpwrap))) { 1414 if (!offloaded) 1415 ret = rcu_advance_cbs(rnp, rdp); /* Advance CBs. */ 1416 rdp->core_needs_qs = false; 1417 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend")); 1418 } else { 1419 if (!offloaded) 1420 ret = rcu_accelerate_cbs(rnp, rdp); /* Recent CBs. */ 1421 if (rdp->core_needs_qs) 1422 rdp->core_needs_qs = !!(rnp->qsmask & rdp->grpmask); 1423 } 1424 1425 /* Now handle the beginnings of any new-to-this-CPU grace periods. */ 1426 if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) || 1427 unlikely(READ_ONCE(rdp->gpwrap))) { 1428 /* 1429 * If the current grace period is waiting for this CPU, 1430 * set up to detect a quiescent state, otherwise don't 1431 * go looking for one. 1432 */ 1433 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart")); 1434 need_qs = !!(rnp->qsmask & rdp->grpmask); 1435 rdp->cpu_no_qs.b.norm = need_qs; 1436 rdp->core_needs_qs = need_qs; 1437 zero_cpu_stall_ticks(rdp); 1438 } 1439 rdp->gp_seq = rnp->gp_seq; /* Remember new grace-period state. */ 1440 if (ULONG_CMP_LT(rdp->gp_seq_needed, rnp->gp_seq_needed) || rdp->gpwrap) 1441 WRITE_ONCE(rdp->gp_seq_needed, rnp->gp_seq_needed); 1442 WRITE_ONCE(rdp->gpwrap, false); 1443 rcu_gpnum_ovf(rnp, rdp); 1444 return ret; 1445 } 1446 1447 static void note_gp_changes(struct rcu_data *rdp) 1448 { 1449 unsigned long flags; 1450 bool needwake; 1451 struct rcu_node *rnp; 1452 1453 local_irq_save(flags); 1454 rnp = rdp->mynode; 1455 if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) && 1456 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */ 1457 !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */ 1458 local_irq_restore(flags); 1459 return; 1460 } 1461 needwake = __note_gp_changes(rnp, rdp); 1462 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 1463 if (needwake) 1464 rcu_gp_kthread_wake(); 1465 } 1466 1467 static void rcu_gp_slow(int delay) 1468 { 1469 if (delay > 0 && 1470 !(rcu_seq_ctr(rcu_state.gp_seq) % 1471 (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay))) 1472 schedule_timeout_uninterruptible(delay); 1473 } 1474 1475 /* 1476 * Initialize a new grace period. Return false if no grace period required. 1477 */ 1478 static bool rcu_gp_init(void) 1479 { 1480 unsigned long flags; 1481 unsigned long oldmask; 1482 unsigned long mask; 1483 struct rcu_data *rdp; 1484 struct rcu_node *rnp = rcu_get_root(); 1485 1486 WRITE_ONCE(rcu_state.gp_activity, jiffies); 1487 raw_spin_lock_irq_rcu_node(rnp); 1488 if (!READ_ONCE(rcu_state.gp_flags)) { 1489 /* Spurious wakeup, tell caller to go back to sleep. */ 1490 raw_spin_unlock_irq_rcu_node(rnp); 1491 return false; 1492 } 1493 WRITE_ONCE(rcu_state.gp_flags, 0); /* Clear all flags: New GP. */ 1494 1495 if (WARN_ON_ONCE(rcu_gp_in_progress())) { 1496 /* 1497 * Grace period already in progress, don't start another. 1498 * Not supposed to be able to happen. 1499 */ 1500 raw_spin_unlock_irq_rcu_node(rnp); 1501 return false; 1502 } 1503 1504 /* Advance to a new grace period and initialize state. */ 1505 record_gp_stall_check_time(); 1506 /* Record GP times before starting GP, hence rcu_seq_start(). */ 1507 rcu_seq_start(&rcu_state.gp_seq); 1508 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start")); 1509 raw_spin_unlock_irq_rcu_node(rnp); 1510 1511 /* 1512 * Apply per-leaf buffered online and offline operations to the 1513 * rcu_node tree. Note that this new grace period need not wait 1514 * for subsequent online CPUs, and that quiescent-state forcing 1515 * will handle subsequent offline CPUs. 1516 */ 1517 rcu_state.gp_state = RCU_GP_ONOFF; 1518 rcu_for_each_leaf_node(rnp) { 1519 raw_spin_lock(&rcu_state.ofl_lock); 1520 raw_spin_lock_irq_rcu_node(rnp); 1521 if (rnp->qsmaskinit == rnp->qsmaskinitnext && 1522 !rnp->wait_blkd_tasks) { 1523 /* Nothing to do on this leaf rcu_node structure. */ 1524 raw_spin_unlock_irq_rcu_node(rnp); 1525 raw_spin_unlock(&rcu_state.ofl_lock); 1526 continue; 1527 } 1528 1529 /* Record old state, apply changes to ->qsmaskinit field. */ 1530 oldmask = rnp->qsmaskinit; 1531 rnp->qsmaskinit = rnp->qsmaskinitnext; 1532 1533 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */ 1534 if (!oldmask != !rnp->qsmaskinit) { 1535 if (!oldmask) { /* First online CPU for rcu_node. */ 1536 if (!rnp->wait_blkd_tasks) /* Ever offline? */ 1537 rcu_init_new_rnp(rnp); 1538 } else if (rcu_preempt_has_tasks(rnp)) { 1539 rnp->wait_blkd_tasks = true; /* blocked tasks */ 1540 } else { /* Last offline CPU and can propagate. */ 1541 rcu_cleanup_dead_rnp(rnp); 1542 } 1543 } 1544 1545 /* 1546 * If all waited-on tasks from prior grace period are 1547 * done, and if all this rcu_node structure's CPUs are 1548 * still offline, propagate up the rcu_node tree and 1549 * clear ->wait_blkd_tasks. Otherwise, if one of this 1550 * rcu_node structure's CPUs has since come back online, 1551 * simply clear ->wait_blkd_tasks. 1552 */ 1553 if (rnp->wait_blkd_tasks && 1554 (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) { 1555 rnp->wait_blkd_tasks = false; 1556 if (!rnp->qsmaskinit) 1557 rcu_cleanup_dead_rnp(rnp); 1558 } 1559 1560 raw_spin_unlock_irq_rcu_node(rnp); 1561 raw_spin_unlock(&rcu_state.ofl_lock); 1562 } 1563 rcu_gp_slow(gp_preinit_delay); /* Races with CPU hotplug. */ 1564 1565 /* 1566 * Set the quiescent-state-needed bits in all the rcu_node 1567 * structures for all currently online CPUs in breadth-first 1568 * order, starting from the root rcu_node structure, relying on the 1569 * layout of the tree within the rcu_state.node[] array. Note that 1570 * other CPUs will access only the leaves of the hierarchy, thus 1571 * seeing that no grace period is in progress, at least until the 1572 * corresponding leaf node has been initialized. 1573 * 1574 * The grace period cannot complete until the initialization 1575 * process finishes, because this kthread handles both. 1576 */ 1577 rcu_state.gp_state = RCU_GP_INIT; 1578 rcu_for_each_node_breadth_first(rnp) { 1579 rcu_gp_slow(gp_init_delay); 1580 raw_spin_lock_irqsave_rcu_node(rnp, flags); 1581 rdp = this_cpu_ptr(&rcu_data); 1582 rcu_preempt_check_blocked_tasks(rnp); 1583 rnp->qsmask = rnp->qsmaskinit; 1584 WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq); 1585 if (rnp == rdp->mynode) 1586 (void)__note_gp_changes(rnp, rdp); 1587 rcu_preempt_boost_start_gp(rnp); 1588 trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq, 1589 rnp->level, rnp->grplo, 1590 rnp->grphi, rnp->qsmask); 1591 /* Quiescent states for tasks on any now-offline CPUs. */ 1592 mask = rnp->qsmask & ~rnp->qsmaskinitnext; 1593 rnp->rcu_gp_init_mask = mask; 1594 if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp)) 1595 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags); 1596 else 1597 raw_spin_unlock_irq_rcu_node(rnp); 1598 cond_resched_tasks_rcu_qs(); 1599 WRITE_ONCE(rcu_state.gp_activity, jiffies); 1600 } 1601 1602 return true; 1603 } 1604 1605 /* 1606 * Helper function for swait_event_idle_exclusive() wakeup at force-quiescent-state 1607 * time. 1608 */ 1609 static bool rcu_gp_fqs_check_wake(int *gfp) 1610 { 1611 struct rcu_node *rnp = rcu_get_root(); 1612 1613 /* Someone like call_rcu() requested a force-quiescent-state scan. */ 1614 *gfp = READ_ONCE(rcu_state.gp_flags); 1615 if (*gfp & RCU_GP_FLAG_FQS) 1616 return true; 1617 1618 /* The current grace period has completed. */ 1619 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp)) 1620 return true; 1621 1622 return false; 1623 } 1624 1625 /* 1626 * Do one round of quiescent-state forcing. 1627 */ 1628 static void rcu_gp_fqs(bool first_time) 1629 { 1630 struct rcu_node *rnp = rcu_get_root(); 1631 1632 WRITE_ONCE(rcu_state.gp_activity, jiffies); 1633 rcu_state.n_force_qs++; 1634 if (first_time) { 1635 /* Collect dyntick-idle snapshots. */ 1636 force_qs_rnp(dyntick_save_progress_counter); 1637 } else { 1638 /* Handle dyntick-idle and offline CPUs. */ 1639 force_qs_rnp(rcu_implicit_dynticks_qs); 1640 } 1641 /* Clear flag to prevent immediate re-entry. */ 1642 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) { 1643 raw_spin_lock_irq_rcu_node(rnp); 1644 WRITE_ONCE(rcu_state.gp_flags, 1645 READ_ONCE(rcu_state.gp_flags) & ~RCU_GP_FLAG_FQS); 1646 raw_spin_unlock_irq_rcu_node(rnp); 1647 } 1648 } 1649 1650 /* 1651 * Loop doing repeated quiescent-state forcing until the grace period ends. 1652 */ 1653 static void rcu_gp_fqs_loop(void) 1654 { 1655 bool first_gp_fqs; 1656 int gf; 1657 unsigned long j; 1658 int ret; 1659 struct rcu_node *rnp = rcu_get_root(); 1660 1661 first_gp_fqs = true; 1662 j = READ_ONCE(jiffies_till_first_fqs); 1663 ret = 0; 1664 for (;;) { 1665 if (!ret) { 1666 rcu_state.jiffies_force_qs = jiffies + j; 1667 WRITE_ONCE(rcu_state.jiffies_kick_kthreads, 1668 jiffies + (j ? 3 * j : 2)); 1669 } 1670 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 1671 TPS("fqswait")); 1672 rcu_state.gp_state = RCU_GP_WAIT_FQS; 1673 ret = swait_event_idle_timeout_exclusive( 1674 rcu_state.gp_wq, rcu_gp_fqs_check_wake(&gf), j); 1675 rcu_state.gp_state = RCU_GP_DOING_FQS; 1676 /* Locking provides needed memory barriers. */ 1677 /* If grace period done, leave loop. */ 1678 if (!READ_ONCE(rnp->qsmask) && 1679 !rcu_preempt_blocked_readers_cgp(rnp)) 1680 break; 1681 /* If time for quiescent-state forcing, do it. */ 1682 if (ULONG_CMP_GE(jiffies, rcu_state.jiffies_force_qs) || 1683 (gf & RCU_GP_FLAG_FQS)) { 1684 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 1685 TPS("fqsstart")); 1686 rcu_gp_fqs(first_gp_fqs); 1687 first_gp_fqs = false; 1688 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 1689 TPS("fqsend")); 1690 cond_resched_tasks_rcu_qs(); 1691 WRITE_ONCE(rcu_state.gp_activity, jiffies); 1692 ret = 0; /* Force full wait till next FQS. */ 1693 j = READ_ONCE(jiffies_till_next_fqs); 1694 } else { 1695 /* Deal with stray signal. */ 1696 cond_resched_tasks_rcu_qs(); 1697 WRITE_ONCE(rcu_state.gp_activity, jiffies); 1698 WARN_ON(signal_pending(current)); 1699 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 1700 TPS("fqswaitsig")); 1701 ret = 1; /* Keep old FQS timing. */ 1702 j = jiffies; 1703 if (time_after(jiffies, rcu_state.jiffies_force_qs)) 1704 j = 1; 1705 else 1706 j = rcu_state.jiffies_force_qs - j; 1707 } 1708 } 1709 } 1710 1711 /* 1712 * Clean up after the old grace period. 1713 */ 1714 static void rcu_gp_cleanup(void) 1715 { 1716 int cpu; 1717 bool needgp = false; 1718 unsigned long gp_duration; 1719 unsigned long new_gp_seq; 1720 bool offloaded; 1721 struct rcu_data *rdp; 1722 struct rcu_node *rnp = rcu_get_root(); 1723 struct swait_queue_head *sq; 1724 1725 WRITE_ONCE(rcu_state.gp_activity, jiffies); 1726 raw_spin_lock_irq_rcu_node(rnp); 1727 rcu_state.gp_end = jiffies; 1728 gp_duration = rcu_state.gp_end - rcu_state.gp_start; 1729 if (gp_duration > rcu_state.gp_max) 1730 rcu_state.gp_max = gp_duration; 1731 1732 /* 1733 * We know the grace period is complete, but to everyone else 1734 * it appears to still be ongoing. But it is also the case 1735 * that to everyone else it looks like there is nothing that 1736 * they can do to advance the grace period. It is therefore 1737 * safe for us to drop the lock in order to mark the grace 1738 * period as completed in all of the rcu_node structures. 1739 */ 1740 raw_spin_unlock_irq_rcu_node(rnp); 1741 1742 /* 1743 * Propagate new ->gp_seq value to rcu_node structures so that 1744 * other CPUs don't have to wait until the start of the next grace 1745 * period to process their callbacks. This also avoids some nasty 1746 * RCU grace-period initialization races by forcing the end of 1747 * the current grace period to be completely recorded in all of 1748 * the rcu_node structures before the beginning of the next grace 1749 * period is recorded in any of the rcu_node structures. 1750 */ 1751 new_gp_seq = rcu_state.gp_seq; 1752 rcu_seq_end(&new_gp_seq); 1753 rcu_for_each_node_breadth_first(rnp) { 1754 raw_spin_lock_irq_rcu_node(rnp); 1755 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp))) 1756 dump_blkd_tasks(rnp, 10); 1757 WARN_ON_ONCE(rnp->qsmask); 1758 WRITE_ONCE(rnp->gp_seq, new_gp_seq); 1759 rdp = this_cpu_ptr(&rcu_data); 1760 if (rnp == rdp->mynode) 1761 needgp = __note_gp_changes(rnp, rdp) || needgp; 1762 /* smp_mb() provided by prior unlock-lock pair. */ 1763 needgp = rcu_future_gp_cleanup(rnp) || needgp; 1764 // Reset overload indication for CPUs no longer overloaded 1765 if (rcu_is_leaf_node(rnp)) 1766 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->cbovldmask) { 1767 rdp = per_cpu_ptr(&rcu_data, cpu); 1768 check_cb_ovld_locked(rdp, rnp); 1769 } 1770 sq = rcu_nocb_gp_get(rnp); 1771 raw_spin_unlock_irq_rcu_node(rnp); 1772 rcu_nocb_gp_cleanup(sq); 1773 cond_resched_tasks_rcu_qs(); 1774 WRITE_ONCE(rcu_state.gp_activity, jiffies); 1775 rcu_gp_slow(gp_cleanup_delay); 1776 } 1777 rnp = rcu_get_root(); 1778 raw_spin_lock_irq_rcu_node(rnp); /* GP before ->gp_seq update. */ 1779 1780 /* Declare grace period done, trace first to use old GP number. */ 1781 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end")); 1782 rcu_seq_end(&rcu_state.gp_seq); 1783 rcu_state.gp_state = RCU_GP_IDLE; 1784 /* Check for GP requests since above loop. */ 1785 rdp = this_cpu_ptr(&rcu_data); 1786 if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) { 1787 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq_needed, 1788 TPS("CleanupMore")); 1789 needgp = true; 1790 } 1791 /* Advance CBs to reduce false positives below. */ 1792 offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) && 1793 rcu_segcblist_is_offloaded(&rdp->cblist); 1794 if ((offloaded || !rcu_accelerate_cbs(rnp, rdp)) && needgp) { 1795 WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT); 1796 WRITE_ONCE(rcu_state.gp_req_activity, jiffies); 1797 trace_rcu_grace_period(rcu_state.name, 1798 rcu_state.gp_seq, 1799 TPS("newreq")); 1800 } else { 1801 WRITE_ONCE(rcu_state.gp_flags, 1802 rcu_state.gp_flags & RCU_GP_FLAG_INIT); 1803 } 1804 raw_spin_unlock_irq_rcu_node(rnp); 1805 } 1806 1807 /* 1808 * Body of kthread that handles grace periods. 1809 */ 1810 static int __noreturn rcu_gp_kthread(void *unused) 1811 { 1812 rcu_bind_gp_kthread(); 1813 for (;;) { 1814 1815 /* Handle grace-period start. */ 1816 for (;;) { 1817 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 1818 TPS("reqwait")); 1819 rcu_state.gp_state = RCU_GP_WAIT_GPS; 1820 swait_event_idle_exclusive(rcu_state.gp_wq, 1821 READ_ONCE(rcu_state.gp_flags) & 1822 RCU_GP_FLAG_INIT); 1823 rcu_state.gp_state = RCU_GP_DONE_GPS; 1824 /* Locking provides needed memory barrier. */ 1825 if (rcu_gp_init()) 1826 break; 1827 cond_resched_tasks_rcu_qs(); 1828 WRITE_ONCE(rcu_state.gp_activity, jiffies); 1829 WARN_ON(signal_pending(current)); 1830 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, 1831 TPS("reqwaitsig")); 1832 } 1833 1834 /* Handle quiescent-state forcing. */ 1835 rcu_gp_fqs_loop(); 1836 1837 /* Handle grace-period end. */ 1838 rcu_state.gp_state = RCU_GP_CLEANUP; 1839 rcu_gp_cleanup(); 1840 rcu_state.gp_state = RCU_GP_CLEANED; 1841 } 1842 } 1843 1844 /* 1845 * Report a full set of quiescent states to the rcu_state data structure. 1846 * Invoke rcu_gp_kthread_wake() to awaken the grace-period kthread if 1847 * another grace period is required. Whether we wake the grace-period 1848 * kthread or it awakens itself for the next round of quiescent-state 1849 * forcing, that kthread will clean up after the just-completed grace 1850 * period. Note that the caller must hold rnp->lock, which is released 1851 * before return. 1852 */ 1853 static void rcu_report_qs_rsp(unsigned long flags) 1854 __releases(rcu_get_root()->lock) 1855 { 1856 raw_lockdep_assert_held_rcu_node(rcu_get_root()); 1857 WARN_ON_ONCE(!rcu_gp_in_progress()); 1858 WRITE_ONCE(rcu_state.gp_flags, 1859 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS); 1860 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags); 1861 rcu_gp_kthread_wake(); 1862 } 1863 1864 /* 1865 * Similar to rcu_report_qs_rdp(), for which it is a helper function. 1866 * Allows quiescent states for a group of CPUs to be reported at one go 1867 * to the specified rcu_node structure, though all the CPUs in the group 1868 * must be represented by the same rcu_node structure (which need not be a 1869 * leaf rcu_node structure, though it often will be). The gps parameter 1870 * is the grace-period snapshot, which means that the quiescent states 1871 * are valid only if rnp->gp_seq is equal to gps. That structure's lock 1872 * must be held upon entry, and it is released before return. 1873 * 1874 * As a special case, if mask is zero, the bit-already-cleared check is 1875 * disabled. This allows propagating quiescent state due to resumed tasks 1876 * during grace-period initialization. 1877 */ 1878 static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp, 1879 unsigned long gps, unsigned long flags) 1880 __releases(rnp->lock) 1881 { 1882 unsigned long oldmask = 0; 1883 struct rcu_node *rnp_c; 1884 1885 raw_lockdep_assert_held_rcu_node(rnp); 1886 1887 /* Walk up the rcu_node hierarchy. */ 1888 for (;;) { 1889 if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) { 1890 1891 /* 1892 * Our bit has already been cleared, or the 1893 * relevant grace period is already over, so done. 1894 */ 1895 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 1896 return; 1897 } 1898 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */ 1899 WARN_ON_ONCE(!rcu_is_leaf_node(rnp) && 1900 rcu_preempt_blocked_readers_cgp(rnp)); 1901 WRITE_ONCE(rnp->qsmask, rnp->qsmask & ~mask); 1902 trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq, 1903 mask, rnp->qsmask, rnp->level, 1904 rnp->grplo, rnp->grphi, 1905 !!rnp->gp_tasks); 1906 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) { 1907 1908 /* Other bits still set at this level, so done. */ 1909 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 1910 return; 1911 } 1912 rnp->completedqs = rnp->gp_seq; 1913 mask = rnp->grpmask; 1914 if (rnp->parent == NULL) { 1915 1916 /* No more levels. Exit loop holding root lock. */ 1917 1918 break; 1919 } 1920 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 1921 rnp_c = rnp; 1922 rnp = rnp->parent; 1923 raw_spin_lock_irqsave_rcu_node(rnp, flags); 1924 oldmask = READ_ONCE(rnp_c->qsmask); 1925 } 1926 1927 /* 1928 * Get here if we are the last CPU to pass through a quiescent 1929 * state for this grace period. Invoke rcu_report_qs_rsp() 1930 * to clean up and start the next grace period if one is needed. 1931 */ 1932 rcu_report_qs_rsp(flags); /* releases rnp->lock. */ 1933 } 1934 1935 /* 1936 * Record a quiescent state for all tasks that were previously queued 1937 * on the specified rcu_node structure and that were blocking the current 1938 * RCU grace period. The caller must hold the corresponding rnp->lock with 1939 * irqs disabled, and this lock is released upon return, but irqs remain 1940 * disabled. 1941 */ 1942 static void __maybe_unused 1943 rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags) 1944 __releases(rnp->lock) 1945 { 1946 unsigned long gps; 1947 unsigned long mask; 1948 struct rcu_node *rnp_p; 1949 1950 raw_lockdep_assert_held_rcu_node(rnp); 1951 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT_RCU)) || 1952 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) || 1953 rnp->qsmask != 0) { 1954 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 1955 return; /* Still need more quiescent states! */ 1956 } 1957 1958 rnp->completedqs = rnp->gp_seq; 1959 rnp_p = rnp->parent; 1960 if (rnp_p == NULL) { 1961 /* 1962 * Only one rcu_node structure in the tree, so don't 1963 * try to report up to its nonexistent parent! 1964 */ 1965 rcu_report_qs_rsp(flags); 1966 return; 1967 } 1968 1969 /* Report up the rest of the hierarchy, tracking current ->gp_seq. */ 1970 gps = rnp->gp_seq; 1971 mask = rnp->grpmask; 1972 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */ 1973 raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */ 1974 rcu_report_qs_rnp(mask, rnp_p, gps, flags); 1975 } 1976 1977 /* 1978 * Record a quiescent state for the specified CPU to that CPU's rcu_data 1979 * structure. This must be called from the specified CPU. 1980 */ 1981 static void 1982 rcu_report_qs_rdp(int cpu, struct rcu_data *rdp) 1983 { 1984 unsigned long flags; 1985 unsigned long mask; 1986 bool needwake = false; 1987 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) && 1988 rcu_segcblist_is_offloaded(&rdp->cblist); 1989 struct rcu_node *rnp; 1990 1991 rnp = rdp->mynode; 1992 raw_spin_lock_irqsave_rcu_node(rnp, flags); 1993 if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq || 1994 rdp->gpwrap) { 1995 1996 /* 1997 * The grace period in which this quiescent state was 1998 * recorded has ended, so don't report it upwards. 1999 * We will instead need a new quiescent state that lies 2000 * within the current grace period. 2001 */ 2002 rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */ 2003 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2004 return; 2005 } 2006 mask = rdp->grpmask; 2007 if (rdp->cpu == smp_processor_id()) 2008 rdp->core_needs_qs = false; 2009 if ((rnp->qsmask & mask) == 0) { 2010 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2011 } else { 2012 /* 2013 * This GP can't end until cpu checks in, so all of our 2014 * callbacks can be processed during the next GP. 2015 */ 2016 if (!offloaded) 2017 needwake = rcu_accelerate_cbs(rnp, rdp); 2018 2019 rcu_disable_urgency_upon_qs(rdp); 2020 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags); 2021 /* ^^^ Released rnp->lock */ 2022 if (needwake) 2023 rcu_gp_kthread_wake(); 2024 } 2025 } 2026 2027 /* 2028 * Check to see if there is a new grace period of which this CPU 2029 * is not yet aware, and if so, set up local rcu_data state for it. 2030 * Otherwise, see if this CPU has just passed through its first 2031 * quiescent state for this grace period, and record that fact if so. 2032 */ 2033 static void 2034 rcu_check_quiescent_state(struct rcu_data *rdp) 2035 { 2036 /* Check for grace-period ends and beginnings. */ 2037 note_gp_changes(rdp); 2038 2039 /* 2040 * Does this CPU still need to do its part for current grace period? 2041 * If no, return and let the other CPUs do their part as well. 2042 */ 2043 if (!rdp->core_needs_qs) 2044 return; 2045 2046 /* 2047 * Was there a quiescent state since the beginning of the grace 2048 * period? If no, then exit and wait for the next call. 2049 */ 2050 if (rdp->cpu_no_qs.b.norm) 2051 return; 2052 2053 /* 2054 * Tell RCU we are done (but rcu_report_qs_rdp() will be the 2055 * judge of that). 2056 */ 2057 rcu_report_qs_rdp(rdp->cpu, rdp); 2058 } 2059 2060 /* 2061 * Near the end of the offline process. Trace the fact that this CPU 2062 * is going offline. 2063 */ 2064 int rcutree_dying_cpu(unsigned int cpu) 2065 { 2066 bool blkd; 2067 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 2068 struct rcu_node *rnp = rdp->mynode; 2069 2070 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU)) 2071 return 0; 2072 2073 blkd = !!(rnp->qsmask & rdp->grpmask); 2074 trace_rcu_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq), 2075 blkd ? TPS("cpuofl") : TPS("cpuofl-bgp")); 2076 return 0; 2077 } 2078 2079 /* 2080 * All CPUs for the specified rcu_node structure have gone offline, 2081 * and all tasks that were preempted within an RCU read-side critical 2082 * section while running on one of those CPUs have since exited their RCU 2083 * read-side critical section. Some other CPU is reporting this fact with 2084 * the specified rcu_node structure's ->lock held and interrupts disabled. 2085 * This function therefore goes up the tree of rcu_node structures, 2086 * clearing the corresponding bits in the ->qsmaskinit fields. Note that 2087 * the leaf rcu_node structure's ->qsmaskinit field has already been 2088 * updated. 2089 * 2090 * This function does check that the specified rcu_node structure has 2091 * all CPUs offline and no blocked tasks, so it is OK to invoke it 2092 * prematurely. That said, invoking it after the fact will cost you 2093 * a needless lock acquisition. So once it has done its work, don't 2094 * invoke it again. 2095 */ 2096 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf) 2097 { 2098 long mask; 2099 struct rcu_node *rnp = rnp_leaf; 2100 2101 raw_lockdep_assert_held_rcu_node(rnp_leaf); 2102 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) || 2103 WARN_ON_ONCE(rnp_leaf->qsmaskinit) || 2104 WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf))) 2105 return; 2106 for (;;) { 2107 mask = rnp->grpmask; 2108 rnp = rnp->parent; 2109 if (!rnp) 2110 break; 2111 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */ 2112 rnp->qsmaskinit &= ~mask; 2113 /* Between grace periods, so better already be zero! */ 2114 WARN_ON_ONCE(rnp->qsmask); 2115 if (rnp->qsmaskinit) { 2116 raw_spin_unlock_rcu_node(rnp); 2117 /* irqs remain disabled. */ 2118 return; 2119 } 2120 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */ 2121 } 2122 } 2123 2124 /* 2125 * The CPU has been completely removed, and some other CPU is reporting 2126 * this fact from process context. Do the remainder of the cleanup. 2127 * There can only be one CPU hotplug operation at a time, so no need for 2128 * explicit locking. 2129 */ 2130 int rcutree_dead_cpu(unsigned int cpu) 2131 { 2132 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 2133 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */ 2134 2135 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU)) 2136 return 0; 2137 2138 /* Adjust any no-longer-needed kthreads. */ 2139 rcu_boost_kthread_setaffinity(rnp, -1); 2140 /* Do any needed no-CB deferred wakeups from this CPU. */ 2141 do_nocb_deferred_wakeup(per_cpu_ptr(&rcu_data, cpu)); 2142 2143 // Stop-machine done, so allow nohz_full to disable tick. 2144 tick_dep_clear(TICK_DEP_BIT_RCU); 2145 return 0; 2146 } 2147 2148 /* 2149 * Invoke any RCU callbacks that have made it to the end of their grace 2150 * period. Thottle as specified by rdp->blimit. 2151 */ 2152 static void rcu_do_batch(struct rcu_data *rdp) 2153 { 2154 unsigned long flags; 2155 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) && 2156 rcu_segcblist_is_offloaded(&rdp->cblist); 2157 struct rcu_head *rhp; 2158 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl); 2159 long bl, count; 2160 long pending, tlimit = 0; 2161 2162 /* If no callbacks are ready, just return. */ 2163 if (!rcu_segcblist_ready_cbs(&rdp->cblist)) { 2164 trace_rcu_batch_start(rcu_state.name, 2165 rcu_segcblist_n_cbs(&rdp->cblist), 0); 2166 trace_rcu_batch_end(rcu_state.name, 0, 2167 !rcu_segcblist_empty(&rdp->cblist), 2168 need_resched(), is_idle_task(current), 2169 rcu_is_callbacks_kthread()); 2170 return; 2171 } 2172 2173 /* 2174 * Extract the list of ready callbacks, disabling to prevent 2175 * races with call_rcu() from interrupt handlers. Leave the 2176 * callback counts, as rcu_barrier() needs to be conservative. 2177 */ 2178 local_irq_save(flags); 2179 rcu_nocb_lock(rdp); 2180 WARN_ON_ONCE(cpu_is_offline(smp_processor_id())); 2181 pending = rcu_segcblist_n_cbs(&rdp->cblist); 2182 bl = max(rdp->blimit, pending >> rcu_divisor); 2183 if (unlikely(bl > 100)) 2184 tlimit = local_clock() + rcu_resched_ns; 2185 trace_rcu_batch_start(rcu_state.name, 2186 rcu_segcblist_n_cbs(&rdp->cblist), bl); 2187 rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl); 2188 if (offloaded) 2189 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist); 2190 rcu_nocb_unlock_irqrestore(rdp, flags); 2191 2192 /* Invoke callbacks. */ 2193 tick_dep_set_task(current, TICK_DEP_BIT_RCU); 2194 rhp = rcu_cblist_dequeue(&rcl); 2195 for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) { 2196 rcu_callback_t f; 2197 2198 debug_rcu_head_unqueue(rhp); 2199 2200 rcu_lock_acquire(&rcu_callback_map); 2201 trace_rcu_invoke_callback(rcu_state.name, rhp); 2202 2203 f = rhp->func; 2204 WRITE_ONCE(rhp->func, (rcu_callback_t)0L); 2205 f(rhp); 2206 2207 rcu_lock_release(&rcu_callback_map); 2208 2209 /* 2210 * Stop only if limit reached and CPU has something to do. 2211 * Note: The rcl structure counts down from zero. 2212 */ 2213 if (-rcl.len >= bl && !offloaded && 2214 (need_resched() || 2215 (!is_idle_task(current) && !rcu_is_callbacks_kthread()))) 2216 break; 2217 if (unlikely(tlimit)) { 2218 /* only call local_clock() every 32 callbacks */ 2219 if (likely((-rcl.len & 31) || local_clock() < tlimit)) 2220 continue; 2221 /* Exceeded the time limit, so leave. */ 2222 break; 2223 } 2224 if (offloaded) { 2225 WARN_ON_ONCE(in_serving_softirq()); 2226 local_bh_enable(); 2227 lockdep_assert_irqs_enabled(); 2228 cond_resched_tasks_rcu_qs(); 2229 lockdep_assert_irqs_enabled(); 2230 local_bh_disable(); 2231 } 2232 } 2233 2234 local_irq_save(flags); 2235 rcu_nocb_lock(rdp); 2236 count = -rcl.len; 2237 trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(), 2238 is_idle_task(current), rcu_is_callbacks_kthread()); 2239 2240 /* Update counts and requeue any remaining callbacks. */ 2241 rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl); 2242 smp_mb(); /* List handling before counting for rcu_barrier(). */ 2243 rcu_segcblist_insert_count(&rdp->cblist, &rcl); 2244 2245 /* Reinstate batch limit if we have worked down the excess. */ 2246 count = rcu_segcblist_n_cbs(&rdp->cblist); 2247 if (rdp->blimit >= DEFAULT_MAX_RCU_BLIMIT && count <= qlowmark) 2248 rdp->blimit = blimit; 2249 2250 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */ 2251 if (count == 0 && rdp->qlen_last_fqs_check != 0) { 2252 rdp->qlen_last_fqs_check = 0; 2253 rdp->n_force_qs_snap = rcu_state.n_force_qs; 2254 } else if (count < rdp->qlen_last_fqs_check - qhimark) 2255 rdp->qlen_last_fqs_check = count; 2256 2257 /* 2258 * The following usually indicates a double call_rcu(). To track 2259 * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. 2260 */ 2261 WARN_ON_ONCE(count == 0 && !rcu_segcblist_empty(&rdp->cblist)); 2262 WARN_ON_ONCE(!IS_ENABLED(CONFIG_RCU_NOCB_CPU) && 2263 count != 0 && rcu_segcblist_empty(&rdp->cblist)); 2264 2265 rcu_nocb_unlock_irqrestore(rdp, flags); 2266 2267 /* Re-invoke RCU core processing if there are callbacks remaining. */ 2268 if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist)) 2269 invoke_rcu_core(); 2270 tick_dep_clear_task(current, TICK_DEP_BIT_RCU); 2271 } 2272 2273 /* 2274 * This function is invoked from each scheduling-clock interrupt, 2275 * and checks to see if this CPU is in a non-context-switch quiescent 2276 * state, for example, user mode or idle loop. It also schedules RCU 2277 * core processing. If the current grace period has gone on too long, 2278 * it will ask the scheduler to manufacture a context switch for the sole 2279 * purpose of providing a providing the needed quiescent state. 2280 */ 2281 void rcu_sched_clock_irq(int user) 2282 { 2283 trace_rcu_utilization(TPS("Start scheduler-tick")); 2284 raw_cpu_inc(rcu_data.ticks_this_gp); 2285 /* The load-acquire pairs with the store-release setting to true. */ 2286 if (smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) { 2287 /* Idle and userspace execution already are quiescent states. */ 2288 if (!rcu_is_cpu_rrupt_from_idle() && !user) { 2289 set_tsk_need_resched(current); 2290 set_preempt_need_resched(); 2291 } 2292 __this_cpu_write(rcu_data.rcu_urgent_qs, false); 2293 } 2294 rcu_flavor_sched_clock_irq(user); 2295 if (rcu_pending(user)) 2296 invoke_rcu_core(); 2297 2298 trace_rcu_utilization(TPS("End scheduler-tick")); 2299 } 2300 2301 /* 2302 * Scan the leaf rcu_node structures. For each structure on which all 2303 * CPUs have reported a quiescent state and on which there are tasks 2304 * blocking the current grace period, initiate RCU priority boosting. 2305 * Otherwise, invoke the specified function to check dyntick state for 2306 * each CPU that has not yet reported a quiescent state. 2307 */ 2308 static void force_qs_rnp(int (*f)(struct rcu_data *rdp)) 2309 { 2310 int cpu; 2311 unsigned long flags; 2312 unsigned long mask; 2313 struct rcu_data *rdp; 2314 struct rcu_node *rnp; 2315 2316 rcu_state.cbovld = rcu_state.cbovldnext; 2317 rcu_state.cbovldnext = false; 2318 rcu_for_each_leaf_node(rnp) { 2319 cond_resched_tasks_rcu_qs(); 2320 mask = 0; 2321 raw_spin_lock_irqsave_rcu_node(rnp, flags); 2322 rcu_state.cbovldnext |= !!rnp->cbovldmask; 2323 if (rnp->qsmask == 0) { 2324 if (!IS_ENABLED(CONFIG_PREEMPT_RCU) || 2325 rcu_preempt_blocked_readers_cgp(rnp)) { 2326 /* 2327 * No point in scanning bits because they 2328 * are all zero. But we might need to 2329 * priority-boost blocked readers. 2330 */ 2331 rcu_initiate_boost(rnp, flags); 2332 /* rcu_initiate_boost() releases rnp->lock */ 2333 continue; 2334 } 2335 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2336 continue; 2337 } 2338 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->qsmask) { 2339 rdp = per_cpu_ptr(&rcu_data, cpu); 2340 if (f(rdp)) { 2341 mask |= rdp->grpmask; 2342 rcu_disable_urgency_upon_qs(rdp); 2343 } 2344 } 2345 if (mask != 0) { 2346 /* Idle/offline CPUs, report (releases rnp->lock). */ 2347 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags); 2348 } else { 2349 /* Nothing to do here, so just drop the lock. */ 2350 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2351 } 2352 } 2353 } 2354 2355 /* 2356 * Force quiescent states on reluctant CPUs, and also detect which 2357 * CPUs are in dyntick-idle mode. 2358 */ 2359 void rcu_force_quiescent_state(void) 2360 { 2361 unsigned long flags; 2362 bool ret; 2363 struct rcu_node *rnp; 2364 struct rcu_node *rnp_old = NULL; 2365 2366 /* Funnel through hierarchy to reduce memory contention. */ 2367 rnp = __this_cpu_read(rcu_data.mynode); 2368 for (; rnp != NULL; rnp = rnp->parent) { 2369 ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) || 2370 !raw_spin_trylock(&rnp->fqslock); 2371 if (rnp_old != NULL) 2372 raw_spin_unlock(&rnp_old->fqslock); 2373 if (ret) 2374 return; 2375 rnp_old = rnp; 2376 } 2377 /* rnp_old == rcu_get_root(), rnp == NULL. */ 2378 2379 /* Reached the root of the rcu_node tree, acquire lock. */ 2380 raw_spin_lock_irqsave_rcu_node(rnp_old, flags); 2381 raw_spin_unlock(&rnp_old->fqslock); 2382 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) { 2383 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags); 2384 return; /* Someone beat us to it. */ 2385 } 2386 WRITE_ONCE(rcu_state.gp_flags, 2387 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS); 2388 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags); 2389 rcu_gp_kthread_wake(); 2390 } 2391 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state); 2392 2393 /* Perform RCU core processing work for the current CPU. */ 2394 static __latent_entropy void rcu_core(void) 2395 { 2396 unsigned long flags; 2397 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data); 2398 struct rcu_node *rnp = rdp->mynode; 2399 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) && 2400 rcu_segcblist_is_offloaded(&rdp->cblist); 2401 2402 if (cpu_is_offline(smp_processor_id())) 2403 return; 2404 trace_rcu_utilization(TPS("Start RCU core")); 2405 WARN_ON_ONCE(!rdp->beenonline); 2406 2407 /* Report any deferred quiescent states if preemption enabled. */ 2408 if (!(preempt_count() & PREEMPT_MASK)) { 2409 rcu_preempt_deferred_qs(current); 2410 } else if (rcu_preempt_need_deferred_qs(current)) { 2411 set_tsk_need_resched(current); 2412 set_preempt_need_resched(); 2413 } 2414 2415 /* Update RCU state based on any recent quiescent states. */ 2416 rcu_check_quiescent_state(rdp); 2417 2418 /* No grace period and unregistered callbacks? */ 2419 if (!rcu_gp_in_progress() && 2420 rcu_segcblist_is_enabled(&rdp->cblist) && !offloaded) { 2421 local_irq_save(flags); 2422 if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL)) 2423 rcu_accelerate_cbs_unlocked(rnp, rdp); 2424 local_irq_restore(flags); 2425 } 2426 2427 rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check()); 2428 2429 /* If there are callbacks ready, invoke them. */ 2430 if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist) && 2431 likely(READ_ONCE(rcu_scheduler_fully_active))) 2432 rcu_do_batch(rdp); 2433 2434 /* Do any needed deferred wakeups of rcuo kthreads. */ 2435 do_nocb_deferred_wakeup(rdp); 2436 trace_rcu_utilization(TPS("End RCU core")); 2437 } 2438 2439 static void rcu_core_si(struct softirq_action *h) 2440 { 2441 rcu_core(); 2442 } 2443 2444 static void rcu_wake_cond(struct task_struct *t, int status) 2445 { 2446 /* 2447 * If the thread is yielding, only wake it when this 2448 * is invoked from idle 2449 */ 2450 if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current))) 2451 wake_up_process(t); 2452 } 2453 2454 static void invoke_rcu_core_kthread(void) 2455 { 2456 struct task_struct *t; 2457 unsigned long flags; 2458 2459 local_irq_save(flags); 2460 __this_cpu_write(rcu_data.rcu_cpu_has_work, 1); 2461 t = __this_cpu_read(rcu_data.rcu_cpu_kthread_task); 2462 if (t != NULL && t != current) 2463 rcu_wake_cond(t, __this_cpu_read(rcu_data.rcu_cpu_kthread_status)); 2464 local_irq_restore(flags); 2465 } 2466 2467 /* 2468 * Wake up this CPU's rcuc kthread to do RCU core processing. 2469 */ 2470 static void invoke_rcu_core(void) 2471 { 2472 if (!cpu_online(smp_processor_id())) 2473 return; 2474 if (use_softirq) 2475 raise_softirq(RCU_SOFTIRQ); 2476 else 2477 invoke_rcu_core_kthread(); 2478 } 2479 2480 static void rcu_cpu_kthread_park(unsigned int cpu) 2481 { 2482 per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU; 2483 } 2484 2485 static int rcu_cpu_kthread_should_run(unsigned int cpu) 2486 { 2487 return __this_cpu_read(rcu_data.rcu_cpu_has_work); 2488 } 2489 2490 /* 2491 * Per-CPU kernel thread that invokes RCU callbacks. This replaces 2492 * the RCU softirq used in configurations of RCU that do not support RCU 2493 * priority boosting. 2494 */ 2495 static void rcu_cpu_kthread(unsigned int cpu) 2496 { 2497 unsigned int *statusp = this_cpu_ptr(&rcu_data.rcu_cpu_kthread_status); 2498 char work, *workp = this_cpu_ptr(&rcu_data.rcu_cpu_has_work); 2499 int spincnt; 2500 2501 trace_rcu_utilization(TPS("Start CPU kthread@rcu_run")); 2502 for (spincnt = 0; spincnt < 10; spincnt++) { 2503 local_bh_disable(); 2504 *statusp = RCU_KTHREAD_RUNNING; 2505 local_irq_disable(); 2506 work = *workp; 2507 *workp = 0; 2508 local_irq_enable(); 2509 if (work) 2510 rcu_core(); 2511 local_bh_enable(); 2512 if (*workp == 0) { 2513 trace_rcu_utilization(TPS("End CPU kthread@rcu_wait")); 2514 *statusp = RCU_KTHREAD_WAITING; 2515 return; 2516 } 2517 } 2518 *statusp = RCU_KTHREAD_YIELDING; 2519 trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield")); 2520 schedule_timeout_interruptible(2); 2521 trace_rcu_utilization(TPS("End CPU kthread@rcu_yield")); 2522 *statusp = RCU_KTHREAD_WAITING; 2523 } 2524 2525 static struct smp_hotplug_thread rcu_cpu_thread_spec = { 2526 .store = &rcu_data.rcu_cpu_kthread_task, 2527 .thread_should_run = rcu_cpu_kthread_should_run, 2528 .thread_fn = rcu_cpu_kthread, 2529 .thread_comm = "rcuc/%u", 2530 .setup = rcu_cpu_kthread_setup, 2531 .park = rcu_cpu_kthread_park, 2532 }; 2533 2534 /* 2535 * Spawn per-CPU RCU core processing kthreads. 2536 */ 2537 static int __init rcu_spawn_core_kthreads(void) 2538 { 2539 int cpu; 2540 2541 for_each_possible_cpu(cpu) 2542 per_cpu(rcu_data.rcu_cpu_has_work, cpu) = 0; 2543 if (!IS_ENABLED(CONFIG_RCU_BOOST) && use_softirq) 2544 return 0; 2545 WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec), 2546 "%s: Could not start rcuc kthread, OOM is now expected behavior\n", __func__); 2547 return 0; 2548 } 2549 early_initcall(rcu_spawn_core_kthreads); 2550 2551 /* 2552 * Handle any core-RCU processing required by a call_rcu() invocation. 2553 */ 2554 static void __call_rcu_core(struct rcu_data *rdp, struct rcu_head *head, 2555 unsigned long flags) 2556 { 2557 /* 2558 * If called from an extended quiescent state, invoke the RCU 2559 * core in order to force a re-evaluation of RCU's idleness. 2560 */ 2561 if (!rcu_is_watching()) 2562 invoke_rcu_core(); 2563 2564 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */ 2565 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id())) 2566 return; 2567 2568 /* 2569 * Force the grace period if too many callbacks or too long waiting. 2570 * Enforce hysteresis, and don't invoke rcu_force_quiescent_state() 2571 * if some other CPU has recently done so. Also, don't bother 2572 * invoking rcu_force_quiescent_state() if the newly enqueued callback 2573 * is the only one waiting for a grace period to complete. 2574 */ 2575 if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) > 2576 rdp->qlen_last_fqs_check + qhimark)) { 2577 2578 /* Are we ignoring a completed grace period? */ 2579 note_gp_changes(rdp); 2580 2581 /* Start a new grace period if one not already started. */ 2582 if (!rcu_gp_in_progress()) { 2583 rcu_accelerate_cbs_unlocked(rdp->mynode, rdp); 2584 } else { 2585 /* Give the grace period a kick. */ 2586 rdp->blimit = DEFAULT_MAX_RCU_BLIMIT; 2587 if (rcu_state.n_force_qs == rdp->n_force_qs_snap && 2588 rcu_segcblist_first_pend_cb(&rdp->cblist) != head) 2589 rcu_force_quiescent_state(); 2590 rdp->n_force_qs_snap = rcu_state.n_force_qs; 2591 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist); 2592 } 2593 } 2594 } 2595 2596 /* 2597 * RCU callback function to leak a callback. 2598 */ 2599 static void rcu_leak_callback(struct rcu_head *rhp) 2600 { 2601 } 2602 2603 /* 2604 * Check and if necessary update the leaf rcu_node structure's 2605 * ->cbovldmask bit corresponding to the current CPU based on that CPU's 2606 * number of queued RCU callbacks. The caller must hold the leaf rcu_node 2607 * structure's ->lock. 2608 */ 2609 static void check_cb_ovld_locked(struct rcu_data *rdp, struct rcu_node *rnp) 2610 { 2611 raw_lockdep_assert_held_rcu_node(rnp); 2612 if (qovld_calc <= 0) 2613 return; // Early boot and wildcard value set. 2614 if (rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc) 2615 WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask | rdp->grpmask); 2616 else 2617 WRITE_ONCE(rnp->cbovldmask, rnp->cbovldmask & ~rdp->grpmask); 2618 } 2619 2620 /* 2621 * Check and if necessary update the leaf rcu_node structure's 2622 * ->cbovldmask bit corresponding to the current CPU based on that CPU's 2623 * number of queued RCU callbacks. No locks need be held, but the 2624 * caller must have disabled interrupts. 2625 * 2626 * Note that this function ignores the possibility that there are a lot 2627 * of callbacks all of which have already seen the end of their respective 2628 * grace periods. This omission is due to the need for no-CBs CPUs to 2629 * be holding ->nocb_lock to do this check, which is too heavy for a 2630 * common-case operation. 2631 */ 2632 static void check_cb_ovld(struct rcu_data *rdp) 2633 { 2634 struct rcu_node *const rnp = rdp->mynode; 2635 2636 if (qovld_calc <= 0 || 2637 ((rcu_segcblist_n_cbs(&rdp->cblist) >= qovld_calc) == 2638 !!(READ_ONCE(rnp->cbovldmask) & rdp->grpmask))) 2639 return; // Early boot wildcard value or already set correctly. 2640 raw_spin_lock_rcu_node(rnp); 2641 check_cb_ovld_locked(rdp, rnp); 2642 raw_spin_unlock_rcu_node(rnp); 2643 } 2644 2645 /* Helper function for call_rcu() and friends. */ 2646 static void 2647 __call_rcu(struct rcu_head *head, rcu_callback_t func) 2648 { 2649 unsigned long flags; 2650 struct rcu_data *rdp; 2651 bool was_alldone; 2652 2653 /* Misaligned rcu_head! */ 2654 WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1)); 2655 2656 if (debug_rcu_head_queue(head)) { 2657 /* 2658 * Probable double call_rcu(), so leak the callback. 2659 * Use rcu:rcu_callback trace event to find the previous 2660 * time callback was passed to __call_rcu(). 2661 */ 2662 WARN_ONCE(1, "__call_rcu(): Double-freed CB %p->%pS()!!!\n", 2663 head, head->func); 2664 WRITE_ONCE(head->func, rcu_leak_callback); 2665 return; 2666 } 2667 head->func = func; 2668 head->next = NULL; 2669 local_irq_save(flags); 2670 rdp = this_cpu_ptr(&rcu_data); 2671 2672 /* Add the callback to our list. */ 2673 if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist))) { 2674 // This can trigger due to call_rcu() from offline CPU: 2675 WARN_ON_ONCE(rcu_scheduler_active != RCU_SCHEDULER_INACTIVE); 2676 WARN_ON_ONCE(!rcu_is_watching()); 2677 // Very early boot, before rcu_init(). Initialize if needed 2678 // and then drop through to queue the callback. 2679 if (rcu_segcblist_empty(&rdp->cblist)) 2680 rcu_segcblist_init(&rdp->cblist); 2681 } 2682 2683 check_cb_ovld(rdp); 2684 if (rcu_nocb_try_bypass(rdp, head, &was_alldone, flags)) 2685 return; // Enqueued onto ->nocb_bypass, so just leave. 2686 // If no-CBs CPU gets here, rcu_nocb_try_bypass() acquired ->nocb_lock. 2687 rcu_segcblist_enqueue(&rdp->cblist, head); 2688 if (__is_kfree_rcu_offset((unsigned long)func)) 2689 trace_rcu_kfree_callback(rcu_state.name, head, 2690 (unsigned long)func, 2691 rcu_segcblist_n_cbs(&rdp->cblist)); 2692 else 2693 trace_rcu_callback(rcu_state.name, head, 2694 rcu_segcblist_n_cbs(&rdp->cblist)); 2695 2696 /* Go handle any RCU core processing required. */ 2697 if (IS_ENABLED(CONFIG_RCU_NOCB_CPU) && 2698 unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) { 2699 __call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */ 2700 } else { 2701 __call_rcu_core(rdp, head, flags); 2702 local_irq_restore(flags); 2703 } 2704 } 2705 2706 /** 2707 * call_rcu() - Queue an RCU callback for invocation after a grace period. 2708 * @head: structure to be used for queueing the RCU updates. 2709 * @func: actual callback function to be invoked after the grace period 2710 * 2711 * The callback function will be invoked some time after a full grace 2712 * period elapses, in other words after all pre-existing RCU read-side 2713 * critical sections have completed. However, the callback function 2714 * might well execute concurrently with RCU read-side critical sections 2715 * that started after call_rcu() was invoked. RCU read-side critical 2716 * sections are delimited by rcu_read_lock() and rcu_read_unlock(), and 2717 * may be nested. In addition, regions of code across which interrupts, 2718 * preemption, or softirqs have been disabled also serve as RCU read-side 2719 * critical sections. This includes hardware interrupt handlers, softirq 2720 * handlers, and NMI handlers. 2721 * 2722 * Note that all CPUs must agree that the grace period extended beyond 2723 * all pre-existing RCU read-side critical section. On systems with more 2724 * than one CPU, this means that when "func()" is invoked, each CPU is 2725 * guaranteed to have executed a full memory barrier since the end of its 2726 * last RCU read-side critical section whose beginning preceded the call 2727 * to call_rcu(). It also means that each CPU executing an RCU read-side 2728 * critical section that continues beyond the start of "func()" must have 2729 * executed a memory barrier after the call_rcu() but before the beginning 2730 * of that RCU read-side critical section. Note that these guarantees 2731 * include CPUs that are offline, idle, or executing in user mode, as 2732 * well as CPUs that are executing in the kernel. 2733 * 2734 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the 2735 * resulting RCU callback function "func()", then both CPU A and CPU B are 2736 * guaranteed to execute a full memory barrier during the time interval 2737 * between the call to call_rcu() and the invocation of "func()" -- even 2738 * if CPU A and CPU B are the same CPU (but again only if the system has 2739 * more than one CPU). 2740 */ 2741 void call_rcu(struct rcu_head *head, rcu_callback_t func) 2742 { 2743 __call_rcu(head, func); 2744 } 2745 EXPORT_SYMBOL_GPL(call_rcu); 2746 2747 2748 /* Maximum number of jiffies to wait before draining a batch. */ 2749 #define KFREE_DRAIN_JIFFIES (HZ / 50) 2750 #define KFREE_N_BATCHES 2 2751 2752 /* 2753 * This macro defines how many entries the "records" array 2754 * will contain. It is based on the fact that the size of 2755 * kfree_rcu_bulk_data structure becomes exactly one page. 2756 */ 2757 #define KFREE_BULK_MAX_ENTR ((PAGE_SIZE / sizeof(void *)) - 3) 2758 2759 /** 2760 * struct kfree_rcu_bulk_data - single block to store kfree_rcu() pointers 2761 * @nr_records: Number of active pointers in the array 2762 * @records: Array of the kfree_rcu() pointers 2763 * @next: Next bulk object in the block chain 2764 * @head_free_debug: For debug, when CONFIG_DEBUG_OBJECTS_RCU_HEAD is set 2765 */ 2766 struct kfree_rcu_bulk_data { 2767 unsigned long nr_records; 2768 void *records[KFREE_BULK_MAX_ENTR]; 2769 struct kfree_rcu_bulk_data *next; 2770 struct rcu_head *head_free_debug; 2771 }; 2772 2773 /** 2774 * struct kfree_rcu_cpu_work - single batch of kfree_rcu() requests 2775 * @rcu_work: Let queue_rcu_work() invoke workqueue handler after grace period 2776 * @head_free: List of kfree_rcu() objects waiting for a grace period 2777 * @bhead_free: Bulk-List of kfree_rcu() objects waiting for a grace period 2778 * @krcp: Pointer to @kfree_rcu_cpu structure 2779 */ 2780 2781 struct kfree_rcu_cpu_work { 2782 struct rcu_work rcu_work; 2783 struct rcu_head *head_free; 2784 struct kfree_rcu_bulk_data *bhead_free; 2785 struct kfree_rcu_cpu *krcp; 2786 }; 2787 2788 /** 2789 * struct kfree_rcu_cpu - batch up kfree_rcu() requests for RCU grace period 2790 * @head: List of kfree_rcu() objects not yet waiting for a grace period 2791 * @bhead: Bulk-List of kfree_rcu() objects not yet waiting for a grace period 2792 * @bcached: Keeps at most one object for later reuse when build chain blocks 2793 * @krw_arr: Array of batches of kfree_rcu() objects waiting for a grace period 2794 * @lock: Synchronize access to this structure 2795 * @monitor_work: Promote @head to @head_free after KFREE_DRAIN_JIFFIES 2796 * @monitor_todo: Tracks whether a @monitor_work delayed work is pending 2797 * @initialized: The @lock and @rcu_work fields have been initialized 2798 * 2799 * This is a per-CPU structure. The reason that it is not included in 2800 * the rcu_data structure is to permit this code to be extracted from 2801 * the RCU files. Such extraction could allow further optimization of 2802 * the interactions with the slab allocators. 2803 */ 2804 struct kfree_rcu_cpu { 2805 struct rcu_head *head; 2806 struct kfree_rcu_bulk_data *bhead; 2807 struct kfree_rcu_bulk_data *bcached; 2808 struct kfree_rcu_cpu_work krw_arr[KFREE_N_BATCHES]; 2809 spinlock_t lock; 2810 struct delayed_work monitor_work; 2811 bool monitor_todo; 2812 bool initialized; 2813 }; 2814 2815 static DEFINE_PER_CPU(struct kfree_rcu_cpu, krc); 2816 2817 static __always_inline void 2818 debug_rcu_head_unqueue_bulk(struct rcu_head *head) 2819 { 2820 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD 2821 for (; head; head = head->next) 2822 debug_rcu_head_unqueue(head); 2823 #endif 2824 } 2825 2826 /* 2827 * This function is invoked in workqueue context after a grace period. 2828 * It frees all the objects queued on ->bhead_free or ->head_free. 2829 */ 2830 static void kfree_rcu_work(struct work_struct *work) 2831 { 2832 unsigned long flags; 2833 struct rcu_head *head, *next; 2834 struct kfree_rcu_bulk_data *bhead, *bnext; 2835 struct kfree_rcu_cpu *krcp; 2836 struct kfree_rcu_cpu_work *krwp; 2837 2838 krwp = container_of(to_rcu_work(work), 2839 struct kfree_rcu_cpu_work, rcu_work); 2840 krcp = krwp->krcp; 2841 spin_lock_irqsave(&krcp->lock, flags); 2842 head = krwp->head_free; 2843 krwp->head_free = NULL; 2844 bhead = krwp->bhead_free; 2845 krwp->bhead_free = NULL; 2846 spin_unlock_irqrestore(&krcp->lock, flags); 2847 2848 /* "bhead" is now private, so traverse locklessly. */ 2849 for (; bhead; bhead = bnext) { 2850 bnext = bhead->next; 2851 2852 debug_rcu_head_unqueue_bulk(bhead->head_free_debug); 2853 2854 rcu_lock_acquire(&rcu_callback_map); 2855 trace_rcu_invoke_kfree_bulk_callback(rcu_state.name, 2856 bhead->nr_records, bhead->records); 2857 2858 kfree_bulk(bhead->nr_records, bhead->records); 2859 rcu_lock_release(&rcu_callback_map); 2860 2861 if (cmpxchg(&krcp->bcached, NULL, bhead)) 2862 free_page((unsigned long) bhead); 2863 2864 cond_resched_tasks_rcu_qs(); 2865 } 2866 2867 /* 2868 * Emergency case only. It can happen under low memory 2869 * condition when an allocation gets failed, so the "bulk" 2870 * path can not be temporary maintained. 2871 */ 2872 for (; head; head = next) { 2873 unsigned long offset = (unsigned long)head->func; 2874 2875 next = head->next; 2876 debug_rcu_head_unqueue(head); 2877 rcu_lock_acquire(&rcu_callback_map); 2878 trace_rcu_invoke_kfree_callback(rcu_state.name, head, offset); 2879 2880 if (!WARN_ON_ONCE(!__is_kfree_rcu_offset(offset))) 2881 kfree((void *)head - offset); 2882 2883 rcu_lock_release(&rcu_callback_map); 2884 cond_resched_tasks_rcu_qs(); 2885 } 2886 } 2887 2888 /* 2889 * Schedule the kfree batch RCU work to run in workqueue context after a GP. 2890 * 2891 * This function is invoked by kfree_rcu_monitor() when the KFREE_DRAIN_JIFFIES 2892 * timeout has been reached. 2893 */ 2894 static inline bool queue_kfree_rcu_work(struct kfree_rcu_cpu *krcp) 2895 { 2896 struct kfree_rcu_cpu_work *krwp; 2897 bool queued = false; 2898 int i; 2899 2900 lockdep_assert_held(&krcp->lock); 2901 2902 for (i = 0; i < KFREE_N_BATCHES; i++) { 2903 krwp = &(krcp->krw_arr[i]); 2904 2905 /* 2906 * Try to detach bhead or head and attach it over any 2907 * available corresponding free channel. It can be that 2908 * a previous RCU batch is in progress, it means that 2909 * immediately to queue another one is not possible so 2910 * return false to tell caller to retry. 2911 */ 2912 if ((krcp->bhead && !krwp->bhead_free) || 2913 (krcp->head && !krwp->head_free)) { 2914 /* Channel 1. */ 2915 if (!krwp->bhead_free) { 2916 krwp->bhead_free = krcp->bhead; 2917 krcp->bhead = NULL; 2918 } 2919 2920 /* Channel 2. */ 2921 if (!krwp->head_free) { 2922 krwp->head_free = krcp->head; 2923 krcp->head = NULL; 2924 } 2925 2926 /* 2927 * One work is per one batch, so there are two "free channels", 2928 * "bhead_free" and "head_free" the batch can handle. It can be 2929 * that the work is in the pending state when two channels have 2930 * been detached following each other, one by one. 2931 */ 2932 queue_rcu_work(system_wq, &krwp->rcu_work); 2933 queued = true; 2934 } 2935 } 2936 2937 return queued; 2938 } 2939 2940 static inline void kfree_rcu_drain_unlock(struct kfree_rcu_cpu *krcp, 2941 unsigned long flags) 2942 { 2943 // Attempt to start a new batch. 2944 krcp->monitor_todo = false; 2945 if (queue_kfree_rcu_work(krcp)) { 2946 // Success! Our job is done here. 2947 spin_unlock_irqrestore(&krcp->lock, flags); 2948 return; 2949 } 2950 2951 // Previous RCU batch still in progress, try again later. 2952 krcp->monitor_todo = true; 2953 schedule_delayed_work(&krcp->monitor_work, KFREE_DRAIN_JIFFIES); 2954 spin_unlock_irqrestore(&krcp->lock, flags); 2955 } 2956 2957 /* 2958 * This function is invoked after the KFREE_DRAIN_JIFFIES timeout. 2959 * It invokes kfree_rcu_drain_unlock() to attempt to start another batch. 2960 */ 2961 static void kfree_rcu_monitor(struct work_struct *work) 2962 { 2963 unsigned long flags; 2964 struct kfree_rcu_cpu *krcp = container_of(work, struct kfree_rcu_cpu, 2965 monitor_work.work); 2966 2967 spin_lock_irqsave(&krcp->lock, flags); 2968 if (krcp->monitor_todo) 2969 kfree_rcu_drain_unlock(krcp, flags); 2970 else 2971 spin_unlock_irqrestore(&krcp->lock, flags); 2972 } 2973 2974 static inline bool 2975 kfree_call_rcu_add_ptr_to_bulk(struct kfree_rcu_cpu *krcp, 2976 struct rcu_head *head, rcu_callback_t func) 2977 { 2978 struct kfree_rcu_bulk_data *bnode; 2979 2980 if (unlikely(!krcp->initialized)) 2981 return false; 2982 2983 lockdep_assert_held(&krcp->lock); 2984 2985 /* Check if a new block is required. */ 2986 if (!krcp->bhead || 2987 krcp->bhead->nr_records == KFREE_BULK_MAX_ENTR) { 2988 bnode = xchg(&krcp->bcached, NULL); 2989 if (!bnode) { 2990 WARN_ON_ONCE(sizeof(struct kfree_rcu_bulk_data) > PAGE_SIZE); 2991 2992 bnode = (struct kfree_rcu_bulk_data *) 2993 __get_free_page(GFP_NOWAIT | __GFP_NOWARN); 2994 } 2995 2996 /* Switch to emergency path. */ 2997 if (unlikely(!bnode)) 2998 return false; 2999 3000 /* Initialize the new block. */ 3001 bnode->nr_records = 0; 3002 bnode->next = krcp->bhead; 3003 bnode->head_free_debug = NULL; 3004 3005 /* Attach it to the head. */ 3006 krcp->bhead = bnode; 3007 } 3008 3009 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD 3010 head->func = func; 3011 head->next = krcp->bhead->head_free_debug; 3012 krcp->bhead->head_free_debug = head; 3013 #endif 3014 3015 /* Finally insert. */ 3016 krcp->bhead->records[krcp->bhead->nr_records++] = 3017 (void *) head - (unsigned long) func; 3018 3019 return true; 3020 } 3021 3022 /* 3023 * Queue a request for lazy invocation of kfree_bulk()/kfree() after a grace 3024 * period. Please note there are two paths are maintained, one is the main one 3025 * that uses kfree_bulk() interface and second one is emergency one, that is 3026 * used only when the main path can not be maintained temporary, due to memory 3027 * pressure. 3028 * 3029 * Each kfree_call_rcu() request is added to a batch. The batch will be drained 3030 * every KFREE_DRAIN_JIFFIES number of jiffies. All the objects in the batch will 3031 * be free'd in workqueue context. This allows us to: batch requests together to 3032 * reduce the number of grace periods during heavy kfree_rcu() load. 3033 */ 3034 void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func) 3035 { 3036 unsigned long flags; 3037 struct kfree_rcu_cpu *krcp; 3038 3039 local_irq_save(flags); // For safely calling this_cpu_ptr(). 3040 krcp = this_cpu_ptr(&krc); 3041 if (krcp->initialized) 3042 spin_lock(&krcp->lock); 3043 3044 // Queue the object but don't yet schedule the batch. 3045 if (debug_rcu_head_queue(head)) { 3046 // Probable double kfree_rcu(), just leak. 3047 WARN_ONCE(1, "%s(): Double-freed call. rcu_head %p\n", 3048 __func__, head); 3049 goto unlock_return; 3050 } 3051 3052 /* 3053 * Under high memory pressure GFP_NOWAIT can fail, 3054 * in that case the emergency path is maintained. 3055 */ 3056 if (unlikely(!kfree_call_rcu_add_ptr_to_bulk(krcp, head, func))) { 3057 head->func = func; 3058 head->next = krcp->head; 3059 krcp->head = head; 3060 } 3061 3062 // Set timer to drain after KFREE_DRAIN_JIFFIES. 3063 if (rcu_scheduler_active == RCU_SCHEDULER_RUNNING && 3064 !krcp->monitor_todo) { 3065 krcp->monitor_todo = true; 3066 schedule_delayed_work(&krcp->monitor_work, KFREE_DRAIN_JIFFIES); 3067 } 3068 3069 unlock_return: 3070 if (krcp->initialized) 3071 spin_unlock(&krcp->lock); 3072 local_irq_restore(flags); 3073 } 3074 EXPORT_SYMBOL_GPL(kfree_call_rcu); 3075 3076 void __init kfree_rcu_scheduler_running(void) 3077 { 3078 int cpu; 3079 unsigned long flags; 3080 3081 for_each_online_cpu(cpu) { 3082 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu); 3083 3084 spin_lock_irqsave(&krcp->lock, flags); 3085 if (!krcp->head || krcp->monitor_todo) { 3086 spin_unlock_irqrestore(&krcp->lock, flags); 3087 continue; 3088 } 3089 krcp->monitor_todo = true; 3090 schedule_delayed_work_on(cpu, &krcp->monitor_work, 3091 KFREE_DRAIN_JIFFIES); 3092 spin_unlock_irqrestore(&krcp->lock, flags); 3093 } 3094 } 3095 3096 /* 3097 * During early boot, any blocking grace-period wait automatically 3098 * implies a grace period. Later on, this is never the case for PREEMPTION. 3099 * 3100 * Howevr, because a context switch is a grace period for !PREEMPTION, any 3101 * blocking grace-period wait automatically implies a grace period if 3102 * there is only one CPU online at any point time during execution of 3103 * either synchronize_rcu() or synchronize_rcu_expedited(). It is OK to 3104 * occasionally incorrectly indicate that there are multiple CPUs online 3105 * when there was in fact only one the whole time, as this just adds some 3106 * overhead: RCU still operates correctly. 3107 */ 3108 static int rcu_blocking_is_gp(void) 3109 { 3110 int ret; 3111 3112 if (IS_ENABLED(CONFIG_PREEMPTION)) 3113 return rcu_scheduler_active == RCU_SCHEDULER_INACTIVE; 3114 might_sleep(); /* Check for RCU read-side critical section. */ 3115 preempt_disable(); 3116 ret = num_online_cpus() <= 1; 3117 preempt_enable(); 3118 return ret; 3119 } 3120 3121 /** 3122 * synchronize_rcu - wait until a grace period has elapsed. 3123 * 3124 * Control will return to the caller some time after a full grace 3125 * period has elapsed, in other words after all currently executing RCU 3126 * read-side critical sections have completed. Note, however, that 3127 * upon return from synchronize_rcu(), the caller might well be executing 3128 * concurrently with new RCU read-side critical sections that began while 3129 * synchronize_rcu() was waiting. RCU read-side critical sections are 3130 * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested. 3131 * In addition, regions of code across which interrupts, preemption, or 3132 * softirqs have been disabled also serve as RCU read-side critical 3133 * sections. This includes hardware interrupt handlers, softirq handlers, 3134 * and NMI handlers. 3135 * 3136 * Note that this guarantee implies further memory-ordering guarantees. 3137 * On systems with more than one CPU, when synchronize_rcu() returns, 3138 * each CPU is guaranteed to have executed a full memory barrier since 3139 * the end of its last RCU read-side critical section whose beginning 3140 * preceded the call to synchronize_rcu(). In addition, each CPU having 3141 * an RCU read-side critical section that extends beyond the return from 3142 * synchronize_rcu() is guaranteed to have executed a full memory barrier 3143 * after the beginning of synchronize_rcu() and before the beginning of 3144 * that RCU read-side critical section. Note that these guarantees include 3145 * CPUs that are offline, idle, or executing in user mode, as well as CPUs 3146 * that are executing in the kernel. 3147 * 3148 * Furthermore, if CPU A invoked synchronize_rcu(), which returned 3149 * to its caller on CPU B, then both CPU A and CPU B are guaranteed 3150 * to have executed a full memory barrier during the execution of 3151 * synchronize_rcu() -- even if CPU A and CPU B are the same CPU (but 3152 * again only if the system has more than one CPU). 3153 */ 3154 void synchronize_rcu(void) 3155 { 3156 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) || 3157 lock_is_held(&rcu_lock_map) || 3158 lock_is_held(&rcu_sched_lock_map), 3159 "Illegal synchronize_rcu() in RCU read-side critical section"); 3160 if (rcu_blocking_is_gp()) 3161 return; 3162 if (rcu_gp_is_expedited()) 3163 synchronize_rcu_expedited(); 3164 else 3165 wait_rcu_gp(call_rcu); 3166 } 3167 EXPORT_SYMBOL_GPL(synchronize_rcu); 3168 3169 /** 3170 * get_state_synchronize_rcu - Snapshot current RCU state 3171 * 3172 * Returns a cookie that is used by a later call to cond_synchronize_rcu() 3173 * to determine whether or not a full grace period has elapsed in the 3174 * meantime. 3175 */ 3176 unsigned long get_state_synchronize_rcu(void) 3177 { 3178 /* 3179 * Any prior manipulation of RCU-protected data must happen 3180 * before the load from ->gp_seq. 3181 */ 3182 smp_mb(); /* ^^^ */ 3183 return rcu_seq_snap(&rcu_state.gp_seq); 3184 } 3185 EXPORT_SYMBOL_GPL(get_state_synchronize_rcu); 3186 3187 /** 3188 * cond_synchronize_rcu - Conditionally wait for an RCU grace period 3189 * 3190 * @oldstate: return value from earlier call to get_state_synchronize_rcu() 3191 * 3192 * If a full RCU grace period has elapsed since the earlier call to 3193 * get_state_synchronize_rcu(), just return. Otherwise, invoke 3194 * synchronize_rcu() to wait for a full grace period. 3195 * 3196 * Yes, this function does not take counter wrap into account. But 3197 * counter wrap is harmless. If the counter wraps, we have waited for 3198 * more than 2 billion grace periods (and way more on a 64-bit system!), 3199 * so waiting for one additional grace period should be just fine. 3200 */ 3201 void cond_synchronize_rcu(unsigned long oldstate) 3202 { 3203 if (!rcu_seq_done(&rcu_state.gp_seq, oldstate)) 3204 synchronize_rcu(); 3205 else 3206 smp_mb(); /* Ensure GP ends before subsequent accesses. */ 3207 } 3208 EXPORT_SYMBOL_GPL(cond_synchronize_rcu); 3209 3210 /* 3211 * Check to see if there is any immediate RCU-related work to be done by 3212 * the current CPU, returning 1 if so and zero otherwise. The checks are 3213 * in order of increasing expense: checks that can be carried out against 3214 * CPU-local state are performed first. However, we must check for CPU 3215 * stalls first, else we might not get a chance. 3216 */ 3217 static int rcu_pending(int user) 3218 { 3219 bool gp_in_progress; 3220 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 3221 struct rcu_node *rnp = rdp->mynode; 3222 3223 /* Check for CPU stalls, if enabled. */ 3224 check_cpu_stall(rdp); 3225 3226 /* Does this CPU need a deferred NOCB wakeup? */ 3227 if (rcu_nocb_need_deferred_wakeup(rdp)) 3228 return 1; 3229 3230 /* Is this a nohz_full CPU in userspace or idle? (Ignore RCU if so.) */ 3231 if ((user || rcu_is_cpu_rrupt_from_idle()) && rcu_nohz_full_cpu()) 3232 return 0; 3233 3234 /* Is the RCU core waiting for a quiescent state from this CPU? */ 3235 gp_in_progress = rcu_gp_in_progress(); 3236 if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm && gp_in_progress) 3237 return 1; 3238 3239 /* Does this CPU have callbacks ready to invoke? */ 3240 if (rcu_segcblist_ready_cbs(&rdp->cblist)) 3241 return 1; 3242 3243 /* Has RCU gone idle with this CPU needing another grace period? */ 3244 if (!gp_in_progress && rcu_segcblist_is_enabled(&rdp->cblist) && 3245 (!IS_ENABLED(CONFIG_RCU_NOCB_CPU) || 3246 !rcu_segcblist_is_offloaded(&rdp->cblist)) && 3247 !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL)) 3248 return 1; 3249 3250 /* Have RCU grace period completed or started? */ 3251 if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq || 3252 unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */ 3253 return 1; 3254 3255 /* nothing to do */ 3256 return 0; 3257 } 3258 3259 /* 3260 * Helper function for rcu_barrier() tracing. If tracing is disabled, 3261 * the compiler is expected to optimize this away. 3262 */ 3263 static void rcu_barrier_trace(const char *s, int cpu, unsigned long done) 3264 { 3265 trace_rcu_barrier(rcu_state.name, s, cpu, 3266 atomic_read(&rcu_state.barrier_cpu_count), done); 3267 } 3268 3269 /* 3270 * RCU callback function for rcu_barrier(). If we are last, wake 3271 * up the task executing rcu_barrier(). 3272 * 3273 * Note that the value of rcu_state.barrier_sequence must be captured 3274 * before the atomic_dec_and_test(). Otherwise, if this CPU is not last, 3275 * other CPUs might count the value down to zero before this CPU gets 3276 * around to invoking rcu_barrier_trace(), which might result in bogus 3277 * data from the next instance of rcu_barrier(). 3278 */ 3279 static void rcu_barrier_callback(struct rcu_head *rhp) 3280 { 3281 unsigned long __maybe_unused s = rcu_state.barrier_sequence; 3282 3283 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) { 3284 rcu_barrier_trace(TPS("LastCB"), -1, s); 3285 complete(&rcu_state.barrier_completion); 3286 } else { 3287 rcu_barrier_trace(TPS("CB"), -1, s); 3288 } 3289 } 3290 3291 /* 3292 * Called with preemption disabled, and from cross-cpu IRQ context. 3293 */ 3294 static void rcu_barrier_func(void *cpu_in) 3295 { 3296 uintptr_t cpu = (uintptr_t)cpu_in; 3297 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 3298 3299 rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence); 3300 rdp->barrier_head.func = rcu_barrier_callback; 3301 debug_rcu_head_queue(&rdp->barrier_head); 3302 rcu_nocb_lock(rdp); 3303 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies)); 3304 if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head)) { 3305 atomic_inc(&rcu_state.barrier_cpu_count); 3306 } else { 3307 debug_rcu_head_unqueue(&rdp->barrier_head); 3308 rcu_barrier_trace(TPS("IRQNQ"), -1, 3309 rcu_state.barrier_sequence); 3310 } 3311 rcu_nocb_unlock(rdp); 3312 } 3313 3314 /** 3315 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete. 3316 * 3317 * Note that this primitive does not necessarily wait for an RCU grace period 3318 * to complete. For example, if there are no RCU callbacks queued anywhere 3319 * in the system, then rcu_barrier() is within its rights to return 3320 * immediately, without waiting for anything, much less an RCU grace period. 3321 */ 3322 void rcu_barrier(void) 3323 { 3324 uintptr_t cpu; 3325 struct rcu_data *rdp; 3326 unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence); 3327 3328 rcu_barrier_trace(TPS("Begin"), -1, s); 3329 3330 /* Take mutex to serialize concurrent rcu_barrier() requests. */ 3331 mutex_lock(&rcu_state.barrier_mutex); 3332 3333 /* Did someone else do our work for us? */ 3334 if (rcu_seq_done(&rcu_state.barrier_sequence, s)) { 3335 rcu_barrier_trace(TPS("EarlyExit"), -1, 3336 rcu_state.barrier_sequence); 3337 smp_mb(); /* caller's subsequent code after above check. */ 3338 mutex_unlock(&rcu_state.barrier_mutex); 3339 return; 3340 } 3341 3342 /* Mark the start of the barrier operation. */ 3343 rcu_seq_start(&rcu_state.barrier_sequence); 3344 rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence); 3345 3346 /* 3347 * Initialize the count to two rather than to zero in order 3348 * to avoid a too-soon return to zero in case of an immediate 3349 * invocation of the just-enqueued callback (or preemption of 3350 * this task). Exclude CPU-hotplug operations to ensure that no 3351 * offline non-offloaded CPU has callbacks queued. 3352 */ 3353 init_completion(&rcu_state.barrier_completion); 3354 atomic_set(&rcu_state.barrier_cpu_count, 2); 3355 get_online_cpus(); 3356 3357 /* 3358 * Force each CPU with callbacks to register a new callback. 3359 * When that callback is invoked, we will know that all of the 3360 * corresponding CPU's preceding callbacks have been invoked. 3361 */ 3362 for_each_possible_cpu(cpu) { 3363 rdp = per_cpu_ptr(&rcu_data, cpu); 3364 if (cpu_is_offline(cpu) && 3365 !rcu_segcblist_is_offloaded(&rdp->cblist)) 3366 continue; 3367 if (rcu_segcblist_n_cbs(&rdp->cblist) && cpu_online(cpu)) { 3368 rcu_barrier_trace(TPS("OnlineQ"), cpu, 3369 rcu_state.barrier_sequence); 3370 smp_call_function_single(cpu, rcu_barrier_func, (void *)cpu, 1); 3371 } else if (rcu_segcblist_n_cbs(&rdp->cblist) && 3372 cpu_is_offline(cpu)) { 3373 rcu_barrier_trace(TPS("OfflineNoCBQ"), cpu, 3374 rcu_state.barrier_sequence); 3375 local_irq_disable(); 3376 rcu_barrier_func((void *)cpu); 3377 local_irq_enable(); 3378 } else if (cpu_is_offline(cpu)) { 3379 rcu_barrier_trace(TPS("OfflineNoCBNoQ"), cpu, 3380 rcu_state.barrier_sequence); 3381 } else { 3382 rcu_barrier_trace(TPS("OnlineNQ"), cpu, 3383 rcu_state.barrier_sequence); 3384 } 3385 } 3386 put_online_cpus(); 3387 3388 /* 3389 * Now that we have an rcu_barrier_callback() callback on each 3390 * CPU, and thus each counted, remove the initial count. 3391 */ 3392 if (atomic_sub_and_test(2, &rcu_state.barrier_cpu_count)) 3393 complete(&rcu_state.barrier_completion); 3394 3395 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */ 3396 wait_for_completion(&rcu_state.barrier_completion); 3397 3398 /* Mark the end of the barrier operation. */ 3399 rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence); 3400 rcu_seq_end(&rcu_state.barrier_sequence); 3401 3402 /* Other rcu_barrier() invocations can now safely proceed. */ 3403 mutex_unlock(&rcu_state.barrier_mutex); 3404 } 3405 EXPORT_SYMBOL_GPL(rcu_barrier); 3406 3407 /* 3408 * Propagate ->qsinitmask bits up the rcu_node tree to account for the 3409 * first CPU in a given leaf rcu_node structure coming online. The caller 3410 * must hold the corresponding leaf rcu_node ->lock with interrrupts 3411 * disabled. 3412 */ 3413 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf) 3414 { 3415 long mask; 3416 long oldmask; 3417 struct rcu_node *rnp = rnp_leaf; 3418 3419 raw_lockdep_assert_held_rcu_node(rnp_leaf); 3420 WARN_ON_ONCE(rnp->wait_blkd_tasks); 3421 for (;;) { 3422 mask = rnp->grpmask; 3423 rnp = rnp->parent; 3424 if (rnp == NULL) 3425 return; 3426 raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */ 3427 oldmask = rnp->qsmaskinit; 3428 rnp->qsmaskinit |= mask; 3429 raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */ 3430 if (oldmask) 3431 return; 3432 } 3433 } 3434 3435 /* 3436 * Do boot-time initialization of a CPU's per-CPU RCU data. 3437 */ 3438 static void __init 3439 rcu_boot_init_percpu_data(int cpu) 3440 { 3441 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 3442 3443 /* Set up local state, ensuring consistent view of global state. */ 3444 rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu); 3445 WARN_ON_ONCE(rdp->dynticks_nesting != 1); 3446 WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp))); 3447 rdp->rcu_ofl_gp_seq = rcu_state.gp_seq; 3448 rdp->rcu_ofl_gp_flags = RCU_GP_CLEANED; 3449 rdp->rcu_onl_gp_seq = rcu_state.gp_seq; 3450 rdp->rcu_onl_gp_flags = RCU_GP_CLEANED; 3451 rdp->cpu = cpu; 3452 rcu_boot_init_nocb_percpu_data(rdp); 3453 } 3454 3455 /* 3456 * Invoked early in the CPU-online process, when pretty much all services 3457 * are available. The incoming CPU is not present. 3458 * 3459 * Initializes a CPU's per-CPU RCU data. Note that only one online or 3460 * offline event can be happening at a given time. Note also that we can 3461 * accept some slop in the rsp->gp_seq access due to the fact that this 3462 * CPU cannot possibly have any non-offloaded RCU callbacks in flight yet. 3463 * And any offloaded callbacks are being numbered elsewhere. 3464 */ 3465 int rcutree_prepare_cpu(unsigned int cpu) 3466 { 3467 unsigned long flags; 3468 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 3469 struct rcu_node *rnp = rcu_get_root(); 3470 3471 /* Set up local state, ensuring consistent view of global state. */ 3472 raw_spin_lock_irqsave_rcu_node(rnp, flags); 3473 rdp->qlen_last_fqs_check = 0; 3474 rdp->n_force_qs_snap = rcu_state.n_force_qs; 3475 rdp->blimit = blimit; 3476 if (rcu_segcblist_empty(&rdp->cblist) && /* No early-boot CBs? */ 3477 !rcu_segcblist_is_offloaded(&rdp->cblist)) 3478 rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */ 3479 rdp->dynticks_nesting = 1; /* CPU not up, no tearing. */ 3480 rcu_dynticks_eqs_online(); 3481 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */ 3482 3483 /* 3484 * Add CPU to leaf rcu_node pending-online bitmask. Any needed 3485 * propagation up the rcu_node tree will happen at the beginning 3486 * of the next grace period. 3487 */ 3488 rnp = rdp->mynode; 3489 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */ 3490 rdp->beenonline = true; /* We have now been online. */ 3491 rdp->gp_seq = READ_ONCE(rnp->gp_seq); 3492 rdp->gp_seq_needed = rdp->gp_seq; 3493 rdp->cpu_no_qs.b.norm = true; 3494 rdp->core_needs_qs = false; 3495 rdp->rcu_iw_pending = false; 3496 rdp->rcu_iw_gp_seq = rdp->gp_seq - 1; 3497 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl")); 3498 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 3499 rcu_prepare_kthreads(cpu); 3500 rcu_spawn_cpu_nocb_kthread(cpu); 3501 3502 return 0; 3503 } 3504 3505 /* 3506 * Update RCU priority boot kthread affinity for CPU-hotplug changes. 3507 */ 3508 static void rcutree_affinity_setting(unsigned int cpu, int outgoing) 3509 { 3510 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 3511 3512 rcu_boost_kthread_setaffinity(rdp->mynode, outgoing); 3513 } 3514 3515 /* 3516 * Near the end of the CPU-online process. Pretty much all services 3517 * enabled, and the CPU is now very much alive. 3518 */ 3519 int rcutree_online_cpu(unsigned int cpu) 3520 { 3521 unsigned long flags; 3522 struct rcu_data *rdp; 3523 struct rcu_node *rnp; 3524 3525 rdp = per_cpu_ptr(&rcu_data, cpu); 3526 rnp = rdp->mynode; 3527 raw_spin_lock_irqsave_rcu_node(rnp, flags); 3528 rnp->ffmask |= rdp->grpmask; 3529 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 3530 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE) 3531 return 0; /* Too early in boot for scheduler work. */ 3532 sync_sched_exp_online_cleanup(cpu); 3533 rcutree_affinity_setting(cpu, -1); 3534 3535 // Stop-machine done, so allow nohz_full to disable tick. 3536 tick_dep_clear(TICK_DEP_BIT_RCU); 3537 return 0; 3538 } 3539 3540 /* 3541 * Near the beginning of the process. The CPU is still very much alive 3542 * with pretty much all services enabled. 3543 */ 3544 int rcutree_offline_cpu(unsigned int cpu) 3545 { 3546 unsigned long flags; 3547 struct rcu_data *rdp; 3548 struct rcu_node *rnp; 3549 3550 rdp = per_cpu_ptr(&rcu_data, cpu); 3551 rnp = rdp->mynode; 3552 raw_spin_lock_irqsave_rcu_node(rnp, flags); 3553 rnp->ffmask &= ~rdp->grpmask; 3554 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 3555 3556 rcutree_affinity_setting(cpu, cpu); 3557 3558 // nohz_full CPUs need the tick for stop-machine to work quickly 3559 tick_dep_set(TICK_DEP_BIT_RCU); 3560 return 0; 3561 } 3562 3563 static DEFINE_PER_CPU(int, rcu_cpu_started); 3564 3565 /* 3566 * Mark the specified CPU as being online so that subsequent grace periods 3567 * (both expedited and normal) will wait on it. Note that this means that 3568 * incoming CPUs are not allowed to use RCU read-side critical sections 3569 * until this function is called. Failing to observe this restriction 3570 * will result in lockdep splats. 3571 * 3572 * Note that this function is special in that it is invoked directly 3573 * from the incoming CPU rather than from the cpuhp_step mechanism. 3574 * This is because this function must be invoked at a precise location. 3575 */ 3576 void rcu_cpu_starting(unsigned int cpu) 3577 { 3578 unsigned long flags; 3579 unsigned long mask; 3580 int nbits; 3581 unsigned long oldmask; 3582 struct rcu_data *rdp; 3583 struct rcu_node *rnp; 3584 3585 if (per_cpu(rcu_cpu_started, cpu)) 3586 return; 3587 3588 per_cpu(rcu_cpu_started, cpu) = 1; 3589 3590 rdp = per_cpu_ptr(&rcu_data, cpu); 3591 rnp = rdp->mynode; 3592 mask = rdp->grpmask; 3593 raw_spin_lock_irqsave_rcu_node(rnp, flags); 3594 WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext | mask); 3595 oldmask = rnp->expmaskinitnext; 3596 rnp->expmaskinitnext |= mask; 3597 oldmask ^= rnp->expmaskinitnext; 3598 nbits = bitmap_weight(&oldmask, BITS_PER_LONG); 3599 /* Allow lockless access for expedited grace periods. */ 3600 smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + nbits); /* ^^^ */ 3601 rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */ 3602 rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq); 3603 rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags); 3604 if (rnp->qsmask & mask) { /* RCU waiting on incoming CPU? */ 3605 rcu_disable_urgency_upon_qs(rdp); 3606 /* Report QS -after- changing ->qsmaskinitnext! */ 3607 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags); 3608 } else { 3609 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 3610 } 3611 smp_mb(); /* Ensure RCU read-side usage follows above initialization. */ 3612 } 3613 3614 #ifdef CONFIG_HOTPLUG_CPU 3615 /* 3616 * The outgoing function has no further need of RCU, so remove it from 3617 * the rcu_node tree's ->qsmaskinitnext bit masks. 3618 * 3619 * Note that this function is special in that it is invoked directly 3620 * from the outgoing CPU rather than from the cpuhp_step mechanism. 3621 * This is because this function must be invoked at a precise location. 3622 */ 3623 void rcu_report_dead(unsigned int cpu) 3624 { 3625 unsigned long flags; 3626 unsigned long mask; 3627 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 3628 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */ 3629 3630 /* QS for any half-done expedited grace period. */ 3631 preempt_disable(); 3632 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data)); 3633 preempt_enable(); 3634 rcu_preempt_deferred_qs(current); 3635 3636 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */ 3637 mask = rdp->grpmask; 3638 raw_spin_lock(&rcu_state.ofl_lock); 3639 raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */ 3640 rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq); 3641 rdp->rcu_ofl_gp_flags = READ_ONCE(rcu_state.gp_flags); 3642 if (rnp->qsmask & mask) { /* RCU waiting on outgoing CPU? */ 3643 /* Report quiescent state -before- changing ->qsmaskinitnext! */ 3644 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags); 3645 raw_spin_lock_irqsave_rcu_node(rnp, flags); 3646 } 3647 WRITE_ONCE(rnp->qsmaskinitnext, rnp->qsmaskinitnext & ~mask); 3648 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 3649 raw_spin_unlock(&rcu_state.ofl_lock); 3650 3651 per_cpu(rcu_cpu_started, cpu) = 0; 3652 } 3653 3654 /* 3655 * The outgoing CPU has just passed through the dying-idle state, and we 3656 * are being invoked from the CPU that was IPIed to continue the offline 3657 * operation. Migrate the outgoing CPU's callbacks to the current CPU. 3658 */ 3659 void rcutree_migrate_callbacks(int cpu) 3660 { 3661 unsigned long flags; 3662 struct rcu_data *my_rdp; 3663 struct rcu_node *my_rnp; 3664 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 3665 bool needwake; 3666 3667 if (rcu_segcblist_is_offloaded(&rdp->cblist) || 3668 rcu_segcblist_empty(&rdp->cblist)) 3669 return; /* No callbacks to migrate. */ 3670 3671 local_irq_save(flags); 3672 my_rdp = this_cpu_ptr(&rcu_data); 3673 my_rnp = my_rdp->mynode; 3674 rcu_nocb_lock(my_rdp); /* irqs already disabled. */ 3675 WARN_ON_ONCE(!rcu_nocb_flush_bypass(my_rdp, NULL, jiffies)); 3676 raw_spin_lock_rcu_node(my_rnp); /* irqs already disabled. */ 3677 /* Leverage recent GPs and set GP for new callbacks. */ 3678 needwake = rcu_advance_cbs(my_rnp, rdp) || 3679 rcu_advance_cbs(my_rnp, my_rdp); 3680 rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist); 3681 needwake = needwake || rcu_advance_cbs(my_rnp, my_rdp); 3682 rcu_segcblist_disable(&rdp->cblist); 3683 WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) != 3684 !rcu_segcblist_n_cbs(&my_rdp->cblist)); 3685 if (rcu_segcblist_is_offloaded(&my_rdp->cblist)) { 3686 raw_spin_unlock_rcu_node(my_rnp); /* irqs remain disabled. */ 3687 __call_rcu_nocb_wake(my_rdp, true, flags); 3688 } else { 3689 rcu_nocb_unlock(my_rdp); /* irqs remain disabled. */ 3690 raw_spin_unlock_irqrestore_rcu_node(my_rnp, flags); 3691 } 3692 if (needwake) 3693 rcu_gp_kthread_wake(); 3694 lockdep_assert_irqs_enabled(); 3695 WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 || 3696 !rcu_segcblist_empty(&rdp->cblist), 3697 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n", 3698 cpu, rcu_segcblist_n_cbs(&rdp->cblist), 3699 rcu_segcblist_first_cb(&rdp->cblist)); 3700 } 3701 #endif 3702 3703 /* 3704 * On non-huge systems, use expedited RCU grace periods to make suspend 3705 * and hibernation run faster. 3706 */ 3707 static int rcu_pm_notify(struct notifier_block *self, 3708 unsigned long action, void *hcpu) 3709 { 3710 switch (action) { 3711 case PM_HIBERNATION_PREPARE: 3712 case PM_SUSPEND_PREPARE: 3713 rcu_expedite_gp(); 3714 break; 3715 case PM_POST_HIBERNATION: 3716 case PM_POST_SUSPEND: 3717 rcu_unexpedite_gp(); 3718 break; 3719 default: 3720 break; 3721 } 3722 return NOTIFY_OK; 3723 } 3724 3725 /* 3726 * Spawn the kthreads that handle RCU's grace periods. 3727 */ 3728 static int __init rcu_spawn_gp_kthread(void) 3729 { 3730 unsigned long flags; 3731 int kthread_prio_in = kthread_prio; 3732 struct rcu_node *rnp; 3733 struct sched_param sp; 3734 struct task_struct *t; 3735 3736 /* Force priority into range. */ 3737 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2 3738 && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)) 3739 kthread_prio = 2; 3740 else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1) 3741 kthread_prio = 1; 3742 else if (kthread_prio < 0) 3743 kthread_prio = 0; 3744 else if (kthread_prio > 99) 3745 kthread_prio = 99; 3746 3747 if (kthread_prio != kthread_prio_in) 3748 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n", 3749 kthread_prio, kthread_prio_in); 3750 3751 rcu_scheduler_fully_active = 1; 3752 t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name); 3753 if (WARN_ONCE(IS_ERR(t), "%s: Could not start grace-period kthread, OOM is now expected behavior\n", __func__)) 3754 return 0; 3755 if (kthread_prio) { 3756 sp.sched_priority = kthread_prio; 3757 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp); 3758 } 3759 rnp = rcu_get_root(); 3760 raw_spin_lock_irqsave_rcu_node(rnp, flags); 3761 WRITE_ONCE(rcu_state.gp_activity, jiffies); 3762 WRITE_ONCE(rcu_state.gp_req_activity, jiffies); 3763 // Reset .gp_activity and .gp_req_activity before setting .gp_kthread. 3764 smp_store_release(&rcu_state.gp_kthread, t); /* ^^^ */ 3765 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 3766 wake_up_process(t); 3767 rcu_spawn_nocb_kthreads(); 3768 rcu_spawn_boost_kthreads(); 3769 return 0; 3770 } 3771 early_initcall(rcu_spawn_gp_kthread); 3772 3773 /* 3774 * This function is invoked towards the end of the scheduler's 3775 * initialization process. Before this is called, the idle task might 3776 * contain synchronous grace-period primitives (during which time, this idle 3777 * task is booting the system, and such primitives are no-ops). After this 3778 * function is called, any synchronous grace-period primitives are run as 3779 * expedited, with the requesting task driving the grace period forward. 3780 * A later core_initcall() rcu_set_runtime_mode() will switch to full 3781 * runtime RCU functionality. 3782 */ 3783 void rcu_scheduler_starting(void) 3784 { 3785 WARN_ON(num_online_cpus() != 1); 3786 WARN_ON(nr_context_switches() > 0); 3787 rcu_test_sync_prims(); 3788 rcu_scheduler_active = RCU_SCHEDULER_INIT; 3789 rcu_test_sync_prims(); 3790 } 3791 3792 /* 3793 * Helper function for rcu_init() that initializes the rcu_state structure. 3794 */ 3795 static void __init rcu_init_one(void) 3796 { 3797 static const char * const buf[] = RCU_NODE_NAME_INIT; 3798 static const char * const fqs[] = RCU_FQS_NAME_INIT; 3799 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS]; 3800 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS]; 3801 3802 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */ 3803 int cpustride = 1; 3804 int i; 3805 int j; 3806 struct rcu_node *rnp; 3807 3808 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */ 3809 3810 /* Silence gcc 4.8 false positive about array index out of range. */ 3811 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS) 3812 panic("rcu_init_one: rcu_num_lvls out of range"); 3813 3814 /* Initialize the level-tracking arrays. */ 3815 3816 for (i = 1; i < rcu_num_lvls; i++) 3817 rcu_state.level[i] = 3818 rcu_state.level[i - 1] + num_rcu_lvl[i - 1]; 3819 rcu_init_levelspread(levelspread, num_rcu_lvl); 3820 3821 /* Initialize the elements themselves, starting from the leaves. */ 3822 3823 for (i = rcu_num_lvls - 1; i >= 0; i--) { 3824 cpustride *= levelspread[i]; 3825 rnp = rcu_state.level[i]; 3826 for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) { 3827 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock)); 3828 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock), 3829 &rcu_node_class[i], buf[i]); 3830 raw_spin_lock_init(&rnp->fqslock); 3831 lockdep_set_class_and_name(&rnp->fqslock, 3832 &rcu_fqs_class[i], fqs[i]); 3833 rnp->gp_seq = rcu_state.gp_seq; 3834 rnp->gp_seq_needed = rcu_state.gp_seq; 3835 rnp->completedqs = rcu_state.gp_seq; 3836 rnp->qsmask = 0; 3837 rnp->qsmaskinit = 0; 3838 rnp->grplo = j * cpustride; 3839 rnp->grphi = (j + 1) * cpustride - 1; 3840 if (rnp->grphi >= nr_cpu_ids) 3841 rnp->grphi = nr_cpu_ids - 1; 3842 if (i == 0) { 3843 rnp->grpnum = 0; 3844 rnp->grpmask = 0; 3845 rnp->parent = NULL; 3846 } else { 3847 rnp->grpnum = j % levelspread[i - 1]; 3848 rnp->grpmask = BIT(rnp->grpnum); 3849 rnp->parent = rcu_state.level[i - 1] + 3850 j / levelspread[i - 1]; 3851 } 3852 rnp->level = i; 3853 INIT_LIST_HEAD(&rnp->blkd_tasks); 3854 rcu_init_one_nocb(rnp); 3855 init_waitqueue_head(&rnp->exp_wq[0]); 3856 init_waitqueue_head(&rnp->exp_wq[1]); 3857 init_waitqueue_head(&rnp->exp_wq[2]); 3858 init_waitqueue_head(&rnp->exp_wq[3]); 3859 spin_lock_init(&rnp->exp_lock); 3860 } 3861 } 3862 3863 init_swait_queue_head(&rcu_state.gp_wq); 3864 init_swait_queue_head(&rcu_state.expedited_wq); 3865 rnp = rcu_first_leaf_node(); 3866 for_each_possible_cpu(i) { 3867 while (i > rnp->grphi) 3868 rnp++; 3869 per_cpu_ptr(&rcu_data, i)->mynode = rnp; 3870 rcu_boot_init_percpu_data(i); 3871 } 3872 } 3873 3874 /* 3875 * Compute the rcu_node tree geometry from kernel parameters. This cannot 3876 * replace the definitions in tree.h because those are needed to size 3877 * the ->node array in the rcu_state structure. 3878 */ 3879 static void __init rcu_init_geometry(void) 3880 { 3881 ulong d; 3882 int i; 3883 int rcu_capacity[RCU_NUM_LVLS]; 3884 3885 /* 3886 * Initialize any unspecified boot parameters. 3887 * The default values of jiffies_till_first_fqs and 3888 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS 3889 * value, which is a function of HZ, then adding one for each 3890 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system. 3891 */ 3892 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV; 3893 if (jiffies_till_first_fqs == ULONG_MAX) 3894 jiffies_till_first_fqs = d; 3895 if (jiffies_till_next_fqs == ULONG_MAX) 3896 jiffies_till_next_fqs = d; 3897 adjust_jiffies_till_sched_qs(); 3898 3899 /* If the compile-time values are accurate, just leave. */ 3900 if (rcu_fanout_leaf == RCU_FANOUT_LEAF && 3901 nr_cpu_ids == NR_CPUS) 3902 return; 3903 pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n", 3904 rcu_fanout_leaf, nr_cpu_ids); 3905 3906 /* 3907 * The boot-time rcu_fanout_leaf parameter must be at least two 3908 * and cannot exceed the number of bits in the rcu_node masks. 3909 * Complain and fall back to the compile-time values if this 3910 * limit is exceeded. 3911 */ 3912 if (rcu_fanout_leaf < 2 || 3913 rcu_fanout_leaf > sizeof(unsigned long) * 8) { 3914 rcu_fanout_leaf = RCU_FANOUT_LEAF; 3915 WARN_ON(1); 3916 return; 3917 } 3918 3919 /* 3920 * Compute number of nodes that can be handled an rcu_node tree 3921 * with the given number of levels. 3922 */ 3923 rcu_capacity[0] = rcu_fanout_leaf; 3924 for (i = 1; i < RCU_NUM_LVLS; i++) 3925 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT; 3926 3927 /* 3928 * The tree must be able to accommodate the configured number of CPUs. 3929 * If this limit is exceeded, fall back to the compile-time values. 3930 */ 3931 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) { 3932 rcu_fanout_leaf = RCU_FANOUT_LEAF; 3933 WARN_ON(1); 3934 return; 3935 } 3936 3937 /* Calculate the number of levels in the tree. */ 3938 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) { 3939 } 3940 rcu_num_lvls = i + 1; 3941 3942 /* Calculate the number of rcu_nodes at each level of the tree. */ 3943 for (i = 0; i < rcu_num_lvls; i++) { 3944 int cap = rcu_capacity[(rcu_num_lvls - 1) - i]; 3945 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap); 3946 } 3947 3948 /* Calculate the total number of rcu_node structures. */ 3949 rcu_num_nodes = 0; 3950 for (i = 0; i < rcu_num_lvls; i++) 3951 rcu_num_nodes += num_rcu_lvl[i]; 3952 } 3953 3954 /* 3955 * Dump out the structure of the rcu_node combining tree associated 3956 * with the rcu_state structure. 3957 */ 3958 static void __init rcu_dump_rcu_node_tree(void) 3959 { 3960 int level = 0; 3961 struct rcu_node *rnp; 3962 3963 pr_info("rcu_node tree layout dump\n"); 3964 pr_info(" "); 3965 rcu_for_each_node_breadth_first(rnp) { 3966 if (rnp->level != level) { 3967 pr_cont("\n"); 3968 pr_info(" "); 3969 level = rnp->level; 3970 } 3971 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum); 3972 } 3973 pr_cont("\n"); 3974 } 3975 3976 struct workqueue_struct *rcu_gp_wq; 3977 struct workqueue_struct *rcu_par_gp_wq; 3978 3979 static void __init kfree_rcu_batch_init(void) 3980 { 3981 int cpu; 3982 int i; 3983 3984 for_each_possible_cpu(cpu) { 3985 struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu); 3986 3987 spin_lock_init(&krcp->lock); 3988 for (i = 0; i < KFREE_N_BATCHES; i++) { 3989 INIT_RCU_WORK(&krcp->krw_arr[i].rcu_work, kfree_rcu_work); 3990 krcp->krw_arr[i].krcp = krcp; 3991 } 3992 3993 INIT_DELAYED_WORK(&krcp->monitor_work, kfree_rcu_monitor); 3994 krcp->initialized = true; 3995 } 3996 } 3997 3998 void __init rcu_init(void) 3999 { 4000 int cpu; 4001 4002 rcu_early_boot_tests(); 4003 4004 kfree_rcu_batch_init(); 4005 rcu_bootup_announce(); 4006 rcu_init_geometry(); 4007 rcu_init_one(); 4008 if (dump_tree) 4009 rcu_dump_rcu_node_tree(); 4010 if (use_softirq) 4011 open_softirq(RCU_SOFTIRQ, rcu_core_si); 4012 4013 /* 4014 * We don't need protection against CPU-hotplug here because 4015 * this is called early in boot, before either interrupts 4016 * or the scheduler are operational. 4017 */ 4018 pm_notifier(rcu_pm_notify, 0); 4019 for_each_online_cpu(cpu) { 4020 rcutree_prepare_cpu(cpu); 4021 rcu_cpu_starting(cpu); 4022 rcutree_online_cpu(cpu); 4023 } 4024 4025 /* Create workqueue for expedited GPs and for Tree SRCU. */ 4026 rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0); 4027 WARN_ON(!rcu_gp_wq); 4028 rcu_par_gp_wq = alloc_workqueue("rcu_par_gp", WQ_MEM_RECLAIM, 0); 4029 WARN_ON(!rcu_par_gp_wq); 4030 srcu_init(); 4031 4032 /* Fill in default value for rcutree.qovld boot parameter. */ 4033 /* -After- the rcu_node ->lock fields are initialized! */ 4034 if (qovld < 0) 4035 qovld_calc = DEFAULT_RCU_QOVLD_MULT * qhimark; 4036 else 4037 qovld_calc = qovld; 4038 } 4039 4040 #include "tree_stall.h" 4041 #include "tree_exp.h" 4042 #include "tree_plugin.h" 4043