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