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