2 * Read-Copy Update mechanism for mutual exclusion
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
18 * Copyright IBM Corporation, 2008
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
22 * Paul E. McKenney <paulmck@linux.vnet.ibm.com> Hierarchical version
24 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
25 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
27 * For detailed explanation of Read-Copy Update mechanism see -
31 #define pr_fmt(fmt) "rcu: " fmt
33 #include <linux/types.h>
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/spinlock.h>
37 #include <linux/smp.h>
38 #include <linux/rcupdate_wait.h>
39 #include <linux/interrupt.h>
40 #include <linux/sched.h>
41 #include <linux/sched/debug.h>
42 #include <linux/nmi.h>
43 #include <linux/atomic.h>
44 #include <linux/bitops.h>
45 #include <linux/export.h>
46 #include <linux/completion.h>
47 #include <linux/moduleparam.h>
48 #include <linux/percpu.h>
49 #include <linux/notifier.h>
50 #include <linux/cpu.h>
51 #include <linux/mutex.h>
52 #include <linux/time.h>
53 #include <linux/kernel_stat.h>
54 #include <linux/wait.h>
55 #include <linux/kthread.h>
56 #include <uapi/linux/sched/types.h>
57 #include <linux/prefetch.h>
58 #include <linux/delay.h>
59 #include <linux/stop_machine.h>
60 #include <linux/random.h>
61 #include <linux/trace_events.h>
62 #include <linux/suspend.h>
63 #include <linux/ftrace.h>
68 #ifdef MODULE_PARAM_PREFIX
69 #undef MODULE_PARAM_PREFIX
71 #define MODULE_PARAM_PREFIX "rcutree."
73 /* Data structures. */
75 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data);
76 struct rcu_state rcu_state = {
77 .level = { &rcu_state.node[0] },
78 .gp_state = RCU_GP_IDLE,
79 .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT,
80 .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex),
83 .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
84 .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
85 .ofl_lock = __SPIN_LOCK_UNLOCKED(rcu_state.ofl_lock),
88 /* Dump rcu_node combining tree at boot to verify correct setup. */
89 static bool dump_tree;
90 module_param(dump_tree, bool, 0444);
91 /* Control rcu_node-tree auto-balancing at boot time. */
92 static bool rcu_fanout_exact;
93 module_param(rcu_fanout_exact, bool, 0444);
94 /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
95 static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
96 module_param(rcu_fanout_leaf, int, 0444);
97 int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
98 /* Number of rcu_nodes at specified level. */
99 int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
100 int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
101 /* panic() on RCU Stall sysctl. */
102 int sysctl_panic_on_rcu_stall __read_mostly;
105 * The rcu_scheduler_active variable is initialized to the value
106 * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
107 * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
108 * RCU can assume that there is but one task, allowing RCU to (for example)
109 * optimize synchronize_rcu() to a simple barrier(). When this variable
110 * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
111 * to detect real grace periods. This variable is also used to suppress
112 * boot-time false positives from lockdep-RCU error checking. Finally, it
113 * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
114 * is fully initialized, including all of its kthreads having been spawned.
116 int rcu_scheduler_active __read_mostly;
117 EXPORT_SYMBOL_GPL(rcu_scheduler_active);
120 * The rcu_scheduler_fully_active variable transitions from zero to one
121 * during the early_initcall() processing, which is after the scheduler
122 * is capable of creating new tasks. So RCU processing (for example,
123 * creating tasks for RCU priority boosting) must be delayed until after
124 * rcu_scheduler_fully_active transitions from zero to one. We also
125 * currently delay invocation of any RCU callbacks until after this point.
127 * It might later prove better for people registering RCU callbacks during
128 * early boot to take responsibility for these callbacks, but one step at
131 static int rcu_scheduler_fully_active __read_mostly;
133 static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
134 unsigned long gps, unsigned long flags);
135 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
136 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
137 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
138 static void invoke_rcu_core(void);
139 static void invoke_rcu_callbacks(struct rcu_data *rdp);
140 static void rcu_report_exp_rdp(struct rcu_data *rdp);
141 static void sync_sched_exp_online_cleanup(int cpu);
143 /* rcuc/rcub kthread realtime priority */
144 static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
145 module_param(kthread_prio, int, 0644);
147 /* Delay in jiffies for grace-period initialization delays, debug only. */
149 static int gp_preinit_delay;
150 module_param(gp_preinit_delay, int, 0444);
151 static int gp_init_delay;
152 module_param(gp_init_delay, int, 0444);
153 static int gp_cleanup_delay;
154 module_param(gp_cleanup_delay, int, 0444);
156 /* Retrieve RCU kthreads priority for rcutorture */
157 int rcu_get_gp_kthreads_prio(void)
161 EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio);
164 * Number of grace periods between delays, normalized by the duration of
165 * the delay. The longer the delay, the more the grace periods between
166 * each delay. The reason for this normalization is that it means that,
167 * for non-zero delays, the overall slowdown of grace periods is constant
168 * regardless of the duration of the delay. This arrangement balances
169 * the need for long delays to increase some race probabilities with the
170 * need for fast grace periods to increase other race probabilities.
172 #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
175 * Compute the mask of online CPUs for the specified rcu_node structure.
176 * This will not be stable unless the rcu_node structure's ->lock is
177 * held, but the bit corresponding to the current CPU will be stable
180 unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
182 return READ_ONCE(rnp->qsmaskinitnext);
186 * Return true if an RCU grace period is in progress. The READ_ONCE()s
187 * permit this function to be invoked without holding the root rcu_node
188 * structure's ->lock, but of course results can be subject to change.
190 static int rcu_gp_in_progress(void)
192 return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq));
195 void rcu_softirq_qs(void)
198 rcu_preempt_deferred_qs(current);
202 * Steal a bit from the bottom of ->dynticks for idle entry/exit
203 * control. Initially this is for TLB flushing.
205 #define RCU_DYNTICK_CTRL_MASK 0x1
206 #define RCU_DYNTICK_CTRL_CTR (RCU_DYNTICK_CTRL_MASK + 1)
207 #ifndef rcu_eqs_special_exit
208 #define rcu_eqs_special_exit() do { } while (0)
211 static DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
212 .dynticks_nesting = 1,
213 .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,
214 .dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),
218 * Record entry into an extended quiescent state. This is only to be
219 * called when not already in an extended quiescent state.
221 static void rcu_dynticks_eqs_enter(void)
223 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
227 * CPUs seeing atomic_add_return() must see prior RCU read-side
228 * critical sections, and we also must force ordering with the
231 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdtp->dynticks);
232 /* Better be in an extended quiescent state! */
233 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
234 (seq & RCU_DYNTICK_CTRL_CTR));
235 /* Better not have special action (TLB flush) pending! */
236 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
237 (seq & RCU_DYNTICK_CTRL_MASK));
241 * Record exit from an extended quiescent state. This is only to be
242 * called from an extended quiescent state.
244 static void rcu_dynticks_eqs_exit(void)
246 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
250 * CPUs seeing atomic_add_return() must see prior idle sojourns,
251 * and we also must force ordering with the next RCU read-side
254 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdtp->dynticks);
255 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
256 !(seq & RCU_DYNTICK_CTRL_CTR));
257 if (seq & RCU_DYNTICK_CTRL_MASK) {
258 atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdtp->dynticks);
259 smp_mb__after_atomic(); /* _exit after clearing mask. */
260 /* Prefer duplicate flushes to losing a flush. */
261 rcu_eqs_special_exit();
266 * Reset the current CPU's ->dynticks counter to indicate that the
267 * newly onlined CPU is no longer in an extended quiescent state.
268 * This will either leave the counter unchanged, or increment it
269 * to the next non-quiescent value.
271 * The non-atomic test/increment sequence works because the upper bits
272 * of the ->dynticks counter are manipulated only by the corresponding CPU,
273 * or when the corresponding CPU is offline.
275 static void rcu_dynticks_eqs_online(void)
277 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
279 if (atomic_read(&rdtp->dynticks) & RCU_DYNTICK_CTRL_CTR)
281 atomic_add(RCU_DYNTICK_CTRL_CTR, &rdtp->dynticks);
285 * Is the current CPU in an extended quiescent state?
287 * No ordering, as we are sampling CPU-local information.
289 bool rcu_dynticks_curr_cpu_in_eqs(void)
291 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
293 return !(atomic_read(&rdtp->dynticks) & RCU_DYNTICK_CTRL_CTR);
297 * Snapshot the ->dynticks counter with full ordering so as to allow
298 * stable comparison of this counter with past and future snapshots.
300 int rcu_dynticks_snap(struct rcu_dynticks *rdtp)
302 int snap = atomic_add_return(0, &rdtp->dynticks);
304 return snap & ~RCU_DYNTICK_CTRL_MASK;
308 * Return true if the snapshot returned from rcu_dynticks_snap()
309 * indicates that RCU is in an extended quiescent state.
311 static bool rcu_dynticks_in_eqs(int snap)
313 return !(snap & RCU_DYNTICK_CTRL_CTR);
317 * Return true if the CPU corresponding to the specified rcu_dynticks
318 * structure has spent some time in an extended quiescent state since
319 * rcu_dynticks_snap() returned the specified snapshot.
321 static bool rcu_dynticks_in_eqs_since(struct rcu_dynticks *rdtp, int snap)
323 return snap != rcu_dynticks_snap(rdtp);
327 * Set the special (bottom) bit of the specified CPU so that it
328 * will take special action (such as flushing its TLB) on the
329 * next exit from an extended quiescent state. Returns true if
330 * the bit was successfully set, or false if the CPU was not in
331 * an extended quiescent state.
333 bool rcu_eqs_special_set(int cpu)
337 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
340 old = atomic_read(&rdtp->dynticks);
341 if (old & RCU_DYNTICK_CTRL_CTR)
343 new = old | RCU_DYNTICK_CTRL_MASK;
344 } while (atomic_cmpxchg(&rdtp->dynticks, old, new) != old);
349 * Let the RCU core know that this CPU has gone through the scheduler,
350 * which is a quiescent state. This is called when the need for a
351 * quiescent state is urgent, so we burn an atomic operation and full
352 * memory barriers to let the RCU core know about it, regardless of what
353 * this CPU might (or might not) do in the near future.
355 * We inform the RCU core by emulating a zero-duration dyntick-idle period.
357 * The caller must have disabled interrupts and must not be idle.
359 static void __maybe_unused rcu_momentary_dyntick_idle(void)
361 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
364 raw_cpu_write(rcu_dynticks.rcu_need_heavy_qs, false);
365 special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR, &rdtp->dynticks);
366 /* It is illegal to call this from idle state. */
367 WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR));
368 rcu_preempt_deferred_qs(current);
372 * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
374 * If the current CPU is idle or running at a first-level (not nested)
375 * interrupt from idle, return true. The caller must have at least
376 * disabled preemption.
378 static int rcu_is_cpu_rrupt_from_idle(void)
380 return __this_cpu_read(rcu_dynticks.dynticks_nesting) <= 0 &&
381 __this_cpu_read(rcu_dynticks.dynticks_nmi_nesting) <= 1;
384 #define DEFAULT_RCU_BLIMIT 10 /* Maximum callbacks per rcu_do_batch. */
385 static long blimit = DEFAULT_RCU_BLIMIT;
386 #define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */
387 static long qhimark = DEFAULT_RCU_QHIMARK;
388 #define DEFAULT_RCU_QLOMARK 100 /* Once only this many pending, use blimit. */
389 static long qlowmark = DEFAULT_RCU_QLOMARK;
391 module_param(blimit, long, 0444);
392 module_param(qhimark, long, 0444);
393 module_param(qlowmark, long, 0444);
395 static ulong jiffies_till_first_fqs = ULONG_MAX;
396 static ulong jiffies_till_next_fqs = ULONG_MAX;
397 static bool rcu_kick_kthreads;
399 static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp)
402 int ret = kstrtoul(val, 0, &j);
405 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j);
409 static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp)
412 int ret = kstrtoul(val, 0, &j);
415 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1));
419 static struct kernel_param_ops first_fqs_jiffies_ops = {
420 .set = param_set_first_fqs_jiffies,
421 .get = param_get_ulong,
424 static struct kernel_param_ops next_fqs_jiffies_ops = {
425 .set = param_set_next_fqs_jiffies,
426 .get = param_get_ulong,
429 module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644);
430 module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644);
431 module_param(rcu_kick_kthreads, bool, 0644);
434 * How long the grace period must be before we start recruiting
435 * quiescent-state help from rcu_note_context_switch().
437 static ulong jiffies_till_sched_qs = HZ / 10;
438 module_param(jiffies_till_sched_qs, ulong, 0444);
440 static void force_qs_rnp(int (*f)(struct rcu_data *rdp));
441 static void force_quiescent_state(void);
442 static int rcu_pending(void);
445 * Return the number of RCU GPs completed thus far for debug & stats.
447 unsigned long rcu_get_gp_seq(void)
449 return READ_ONCE(rcu_state.gp_seq);
451 EXPORT_SYMBOL_GPL(rcu_get_gp_seq);
454 * Return the number of RCU expedited batches completed thus far for
455 * debug & stats. Odd numbers mean that a batch is in progress, even
456 * numbers mean idle. The value returned will thus be roughly double
457 * the cumulative batches since boot.
459 unsigned long rcu_exp_batches_completed(void)
461 return rcu_state.expedited_sequence;
463 EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
466 * Force a quiescent state.
468 void rcu_force_quiescent_state(void)
470 force_quiescent_state();
472 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
475 * Show the state of the grace-period kthreads.
477 void show_rcu_gp_kthreads(void)
480 struct rcu_data *rdp;
481 struct rcu_node *rnp;
483 pr_info("%s: wait state: %d ->state: %#lx\n", rcu_state.name,
484 rcu_state.gp_state, rcu_state.gp_kthread->state);
485 rcu_for_each_node_breadth_first(rnp) {
486 if (ULONG_CMP_GE(rcu_state.gp_seq, rnp->gp_seq_needed))
488 pr_info("\trcu_node %d:%d ->gp_seq %lu ->gp_seq_needed %lu\n",
489 rnp->grplo, rnp->grphi, rnp->gp_seq,
491 if (!rcu_is_leaf_node(rnp))
493 for_each_leaf_node_possible_cpu(rnp, cpu) {
494 rdp = per_cpu_ptr(&rcu_data, cpu);
496 ULONG_CMP_GE(rcu_state.gp_seq,
499 pr_info("\tcpu %d ->gp_seq_needed %lu\n",
500 cpu, rdp->gp_seq_needed);
503 /* sched_show_task(rcu_state.gp_kthread); */
505 EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
508 * Send along grace-period-related data for rcutorture diagnostics.
510 void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
511 unsigned long *gp_seq)
516 case RCU_SCHED_FLAVOR:
517 *flags = READ_ONCE(rcu_state.gp_flags);
518 *gp_seq = rcu_seq_current(&rcu_state.gp_seq);
524 EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
527 * Return the root node of the rcu_state structure.
529 static struct rcu_node *rcu_get_root(void)
531 return &rcu_state.node[0];
535 * Enter an RCU extended quiescent state, which can be either the
536 * idle loop or adaptive-tickless usermode execution.
538 * We crowbar the ->dynticks_nmi_nesting field to zero to allow for
539 * the possibility of usermode upcalls having messed up our count
540 * of interrupt nesting level during the prior busy period.
542 static void rcu_eqs_enter(bool user)
544 struct rcu_data *rdp;
545 struct rcu_dynticks *rdtp;
547 rdtp = this_cpu_ptr(&rcu_dynticks);
548 WARN_ON_ONCE(rdtp->dynticks_nmi_nesting != DYNTICK_IRQ_NONIDLE);
549 WRITE_ONCE(rdtp->dynticks_nmi_nesting, 0);
550 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
551 rdtp->dynticks_nesting == 0);
552 if (rdtp->dynticks_nesting != 1) {
553 rdtp->dynticks_nesting--;
557 lockdep_assert_irqs_disabled();
558 trace_rcu_dyntick(TPS("Start"), rdtp->dynticks_nesting, 0, rdtp->dynticks);
559 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
560 rdp = this_cpu_ptr(&rcu_data);
561 do_nocb_deferred_wakeup(rdp);
562 rcu_prepare_for_idle();
563 rcu_preempt_deferred_qs(current);
564 WRITE_ONCE(rdtp->dynticks_nesting, 0); /* Avoid irq-access tearing. */
565 rcu_dynticks_eqs_enter();
566 rcu_dynticks_task_enter();
570 * rcu_idle_enter - inform RCU that current CPU is entering idle
572 * Enter idle mode, in other words, -leave- the mode in which RCU
573 * read-side critical sections can occur. (Though RCU read-side
574 * critical sections can occur in irq handlers in idle, a possibility
575 * handled by irq_enter() and irq_exit().)
577 * If you add or remove a call to rcu_idle_enter(), be sure to test with
578 * CONFIG_RCU_EQS_DEBUG=y.
580 void rcu_idle_enter(void)
582 lockdep_assert_irqs_disabled();
583 rcu_eqs_enter(false);
586 #ifdef CONFIG_NO_HZ_FULL
588 * rcu_user_enter - inform RCU that we are resuming userspace.
590 * Enter RCU idle mode right before resuming userspace. No use of RCU
591 * is permitted between this call and rcu_user_exit(). This way the
592 * CPU doesn't need to maintain the tick for RCU maintenance purposes
593 * when the CPU runs in userspace.
595 * If you add or remove a call to rcu_user_enter(), be sure to test with
596 * CONFIG_RCU_EQS_DEBUG=y.
598 void rcu_user_enter(void)
600 lockdep_assert_irqs_disabled();
603 #endif /* CONFIG_NO_HZ_FULL */
606 * If we are returning from the outermost NMI handler that interrupted an
607 * RCU-idle period, update rdtp->dynticks and rdtp->dynticks_nmi_nesting
608 * to let the RCU grace-period handling know that the CPU is back to
611 * If you add or remove a call to rcu_nmi_exit_common(), be sure to test
612 * with CONFIG_RCU_EQS_DEBUG=y.
614 static __always_inline void rcu_nmi_exit_common(bool irq)
616 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
619 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
620 * (We are exiting an NMI handler, so RCU better be paying attention
623 WARN_ON_ONCE(rdtp->dynticks_nmi_nesting <= 0);
624 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
627 * If the nesting level is not 1, the CPU wasn't RCU-idle, so
628 * leave it in non-RCU-idle state.
630 if (rdtp->dynticks_nmi_nesting != 1) {
631 trace_rcu_dyntick(TPS("--="), rdtp->dynticks_nmi_nesting, rdtp->dynticks_nmi_nesting - 2, rdtp->dynticks);
632 WRITE_ONCE(rdtp->dynticks_nmi_nesting, /* No store tearing. */
633 rdtp->dynticks_nmi_nesting - 2);
637 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
638 trace_rcu_dyntick(TPS("Startirq"), rdtp->dynticks_nmi_nesting, 0, rdtp->dynticks);
639 WRITE_ONCE(rdtp->dynticks_nmi_nesting, 0); /* Avoid store tearing. */
642 rcu_prepare_for_idle();
644 rcu_dynticks_eqs_enter();
647 rcu_dynticks_task_enter();
651 * rcu_nmi_exit - inform RCU of exit from NMI context
652 * @irq: Is this call from rcu_irq_exit?
654 * If you add or remove a call to rcu_nmi_exit(), be sure to test
655 * with CONFIG_RCU_EQS_DEBUG=y.
657 void rcu_nmi_exit(void)
659 rcu_nmi_exit_common(false);
663 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
665 * Exit from an interrupt handler, which might possibly result in entering
666 * idle mode, in other words, leaving the mode in which read-side critical
667 * sections can occur. The caller must have disabled interrupts.
669 * This code assumes that the idle loop never does anything that might
670 * result in unbalanced calls to irq_enter() and irq_exit(). If your
671 * architecture's idle loop violates this assumption, RCU will give you what
672 * you deserve, good and hard. But very infrequently and irreproducibly.
674 * Use things like work queues to work around this limitation.
676 * You have been warned.
678 * If you add or remove a call to rcu_irq_exit(), be sure to test with
679 * CONFIG_RCU_EQS_DEBUG=y.
681 void rcu_irq_exit(void)
683 lockdep_assert_irqs_disabled();
684 rcu_nmi_exit_common(true);
688 * Wrapper for rcu_irq_exit() where interrupts are enabled.
690 * If you add or remove a call to rcu_irq_exit_irqson(), be sure to test
691 * with CONFIG_RCU_EQS_DEBUG=y.
693 void rcu_irq_exit_irqson(void)
697 local_irq_save(flags);
699 local_irq_restore(flags);
703 * Exit an RCU extended quiescent state, which can be either the
704 * idle loop or adaptive-tickless usermode execution.
706 * We crowbar the ->dynticks_nmi_nesting field to DYNTICK_IRQ_NONIDLE to
707 * allow for the possibility of usermode upcalls messing up our count of
708 * interrupt nesting level during the busy period that is just now starting.
710 static void rcu_eqs_exit(bool user)
712 struct rcu_dynticks *rdtp;
715 lockdep_assert_irqs_disabled();
716 rdtp = this_cpu_ptr(&rcu_dynticks);
717 oldval = rdtp->dynticks_nesting;
718 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
720 rdtp->dynticks_nesting++;
723 rcu_dynticks_task_exit();
724 rcu_dynticks_eqs_exit();
725 rcu_cleanup_after_idle();
726 trace_rcu_dyntick(TPS("End"), rdtp->dynticks_nesting, 1, rdtp->dynticks);
727 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
728 WRITE_ONCE(rdtp->dynticks_nesting, 1);
729 WARN_ON_ONCE(rdtp->dynticks_nmi_nesting);
730 WRITE_ONCE(rdtp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE);
734 * rcu_idle_exit - inform RCU that current CPU is leaving idle
736 * Exit idle mode, in other words, -enter- the mode in which RCU
737 * read-side critical sections can occur.
739 * If you add or remove a call to rcu_idle_exit(), be sure to test with
740 * CONFIG_RCU_EQS_DEBUG=y.
742 void rcu_idle_exit(void)
746 local_irq_save(flags);
748 local_irq_restore(flags);
751 #ifdef CONFIG_NO_HZ_FULL
753 * rcu_user_exit - inform RCU that we are exiting userspace.
755 * Exit RCU idle mode while entering the kernel because it can
756 * run a RCU read side critical section anytime.
758 * If you add or remove a call to rcu_user_exit(), be sure to test with
759 * CONFIG_RCU_EQS_DEBUG=y.
761 void rcu_user_exit(void)
765 #endif /* CONFIG_NO_HZ_FULL */
768 * rcu_nmi_enter_common - inform RCU of entry to NMI context
769 * @irq: Is this call from rcu_irq_enter?
771 * If the CPU was idle from RCU's viewpoint, update rdtp->dynticks and
772 * rdtp->dynticks_nmi_nesting to let the RCU grace-period handling know
773 * that the CPU is active. This implementation permits nested NMIs, as
774 * long as the nesting level does not overflow an int. (You will probably
775 * run out of stack space first.)
777 * If you add or remove a call to rcu_nmi_enter_common(), be sure to test
778 * with CONFIG_RCU_EQS_DEBUG=y.
780 static __always_inline void rcu_nmi_enter_common(bool irq)
782 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
785 /* Complain about underflow. */
786 WARN_ON_ONCE(rdtp->dynticks_nmi_nesting < 0);
789 * If idle from RCU viewpoint, atomically increment ->dynticks
790 * to mark non-idle and increment ->dynticks_nmi_nesting by one.
791 * Otherwise, increment ->dynticks_nmi_nesting by two. This means
792 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
793 * to be in the outermost NMI handler that interrupted an RCU-idle
794 * period (observation due to Andy Lutomirski).
796 if (rcu_dynticks_curr_cpu_in_eqs()) {
799 rcu_dynticks_task_exit();
801 rcu_dynticks_eqs_exit();
804 rcu_cleanup_after_idle();
808 trace_rcu_dyntick(incby == 1 ? TPS("Endirq") : TPS("++="),
809 rdtp->dynticks_nmi_nesting,
810 rdtp->dynticks_nmi_nesting + incby, rdtp->dynticks);
811 WRITE_ONCE(rdtp->dynticks_nmi_nesting, /* Prevent store tearing. */
812 rdtp->dynticks_nmi_nesting + incby);
817 * rcu_nmi_enter - inform RCU of entry to NMI context
819 void rcu_nmi_enter(void)
821 rcu_nmi_enter_common(false);
825 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
827 * Enter an interrupt handler, which might possibly result in exiting
828 * idle mode, in other words, entering the mode in which read-side critical
829 * sections can occur. The caller must have disabled interrupts.
831 * Note that the Linux kernel is fully capable of entering an interrupt
832 * handler that it never exits, for example when doing upcalls to user mode!
833 * This code assumes that the idle loop never does upcalls to user mode.
834 * If your architecture's idle loop does do upcalls to user mode (or does
835 * anything else that results in unbalanced calls to the irq_enter() and
836 * irq_exit() functions), RCU will give you what you deserve, good and hard.
837 * But very infrequently and irreproducibly.
839 * Use things like work queues to work around this limitation.
841 * You have been warned.
843 * If you add or remove a call to rcu_irq_enter(), be sure to test with
844 * CONFIG_RCU_EQS_DEBUG=y.
846 void rcu_irq_enter(void)
848 lockdep_assert_irqs_disabled();
849 rcu_nmi_enter_common(true);
853 * Wrapper for rcu_irq_enter() where interrupts are enabled.
855 * If you add or remove a call to rcu_irq_enter_irqson(), be sure to test
856 * with CONFIG_RCU_EQS_DEBUG=y.
858 void rcu_irq_enter_irqson(void)
862 local_irq_save(flags);
864 local_irq_restore(flags);
868 * rcu_is_watching - see if RCU thinks that the current CPU is idle
870 * Return true if RCU is watching the running CPU, which means that this
871 * CPU can safely enter RCU read-side critical sections. In other words,
872 * if the current CPU is in its idle loop and is neither in an interrupt
873 * or NMI handler, return true.
875 bool notrace rcu_is_watching(void)
879 preempt_disable_notrace();
880 ret = !rcu_dynticks_curr_cpu_in_eqs();
881 preempt_enable_notrace();
884 EXPORT_SYMBOL_GPL(rcu_is_watching);
887 * If a holdout task is actually running, request an urgent quiescent
888 * state from its CPU. This is unsynchronized, so migrations can cause
889 * the request to go to the wrong CPU. Which is OK, all that will happen
890 * is that the CPU's next context switch will be a bit slower and next
891 * time around this task will generate another request.
893 void rcu_request_urgent_qs_task(struct task_struct *t)
900 return; /* This task is not running on that CPU. */
901 smp_store_release(per_cpu_ptr(&rcu_dynticks.rcu_urgent_qs, cpu), true);
904 #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
907 * Is the current CPU online as far as RCU is concerned?
909 * Disable preemption to avoid false positives that could otherwise
910 * happen due to the current CPU number being sampled, this task being
911 * preempted, its old CPU being taken offline, resuming on some other CPU,
912 * then determining that its old CPU is now offline.
914 * Disable checking if in an NMI handler because we cannot safely
915 * report errors from NMI handlers anyway. In addition, it is OK to use
916 * RCU on an offline processor during initial boot, hence the check for
917 * rcu_scheduler_fully_active.
919 bool rcu_lockdep_current_cpu_online(void)
921 struct rcu_data *rdp;
922 struct rcu_node *rnp;
925 if (in_nmi() || !rcu_scheduler_fully_active)
928 rdp = this_cpu_ptr(&rcu_data);
930 if (rdp->grpmask & rcu_rnp_online_cpus(rnp))
935 EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
937 #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
940 * We are reporting a quiescent state on behalf of some other CPU, so
941 * it is our responsibility to check for and handle potential overflow
942 * of the rcu_node ->gp_seq counter with respect to the rcu_data counters.
943 * After all, the CPU might be in deep idle state, and thus executing no
946 static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
948 raw_lockdep_assert_held_rcu_node(rnp);
949 if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + ULONG_MAX / 4,
951 WRITE_ONCE(rdp->gpwrap, true);
952 if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq))
953 rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4;
957 * Snapshot the specified CPU's dynticks counter so that we can later
958 * credit them with an implicit quiescent state. Return 1 if this CPU
959 * is in dynticks idle mode, which is an extended quiescent state.
961 static int dyntick_save_progress_counter(struct rcu_data *rdp)
963 rdp->dynticks_snap = rcu_dynticks_snap(rdp->dynticks);
964 if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
965 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
966 rcu_gpnum_ovf(rdp->mynode, rdp);
973 * Handler for the irq_work request posted when a grace period has
974 * gone on for too long, but not yet long enough for an RCU CPU
975 * stall warning. Set state appropriately, but just complain if
976 * there is unexpected state on entry.
978 static void rcu_iw_handler(struct irq_work *iwp)
980 struct rcu_data *rdp;
981 struct rcu_node *rnp;
983 rdp = container_of(iwp, struct rcu_data, rcu_iw);
985 raw_spin_lock_rcu_node(rnp);
986 if (!WARN_ON_ONCE(!rdp->rcu_iw_pending)) {
987 rdp->rcu_iw_gp_seq = rnp->gp_seq;
988 rdp->rcu_iw_pending = false;
990 raw_spin_unlock_rcu_node(rnp);
994 * Return true if the specified CPU has passed through a quiescent
995 * state by virtue of being in or having passed through an dynticks
996 * idle state since the last call to dyntick_save_progress_counter()
997 * for this same CPU, or by virtue of having been offline.
999 static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
1004 struct rcu_node *rnp = rdp->mynode;
1007 * If the CPU passed through or entered a dynticks idle phase with
1008 * no active irq/NMI handlers, then we can safely pretend that the CPU
1009 * already acknowledged the request to pass through a quiescent
1010 * state. Either way, that CPU cannot possibly be in an RCU
1011 * read-side critical section that started before the beginning
1012 * of the current RCU grace period.
1014 if (rcu_dynticks_in_eqs_since(rdp->dynticks, rdp->dynticks_snap)) {
1015 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
1016 rdp->dynticks_fqs++;
1017 rcu_gpnum_ovf(rnp, rdp);
1022 * Has this CPU encountered a cond_resched() since the beginning
1023 * of the grace period? For this to be the case, the CPU has to
1024 * have noticed the current grace period. This might not be the
1025 * case for nohz_full CPUs looping in the kernel.
1027 jtsq = jiffies_till_sched_qs;
1028 ruqp = per_cpu_ptr(&rcu_dynticks.rcu_urgent_qs, rdp->cpu);
1029 if (time_after(jiffies, rcu_state.gp_start + jtsq) &&
1030 READ_ONCE(rdp->rcu_qs_ctr_snap) != per_cpu(rcu_dynticks.rcu_qs_ctr, rdp->cpu) &&
1031 rcu_seq_current(&rdp->gp_seq) == rnp->gp_seq && !rdp->gpwrap) {
1032 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("rqc"));
1033 rcu_gpnum_ovf(rnp, rdp);
1035 } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) {
1036 /* Load rcu_qs_ctr before store to rcu_urgent_qs. */
1037 smp_store_release(ruqp, true);
1040 /* If waiting too long on an offline CPU, complain. */
1041 if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp)) &&
1042 time_after(jiffies, rcu_state.gp_start + HZ)) {
1044 struct rcu_node *rnp1;
1046 WARN_ON(1); /* Offline CPUs are supposed to report QS! */
1047 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
1048 __func__, rnp->grplo, rnp->grphi, rnp->level,
1049 (long)rnp->gp_seq, (long)rnp->completedqs);
1050 for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
1051 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n",
1052 __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask);
1053 onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
1054 pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n",
1055 __func__, rdp->cpu, ".o"[onl],
1056 (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
1057 (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
1058 return 1; /* Break things loose after complaining. */
1062 * A CPU running for an extended time within the kernel can
1063 * delay RCU grace periods. When the CPU is in NO_HZ_FULL mode,
1064 * even context-switching back and forth between a pair of
1065 * in-kernel CPU-bound tasks cannot advance grace periods.
1066 * So if the grace period is old enough, make the CPU pay attention.
1067 * Note that the unsynchronized assignments to the per-CPU
1068 * rcu_need_heavy_qs variable are safe. Yes, setting of
1069 * bits can be lost, but they will be set again on the next
1070 * force-quiescent-state pass. So lost bit sets do not result
1071 * in incorrect behavior, merely in a grace period lasting
1072 * a few jiffies longer than it might otherwise. Because
1073 * there are at most four threads involved, and because the
1074 * updates are only once every few jiffies, the probability of
1075 * lossage (and thus of slight grace-period extension) is
1078 rnhqp = &per_cpu(rcu_dynticks.rcu_need_heavy_qs, rdp->cpu);
1079 if (!READ_ONCE(*rnhqp) &&
1080 (time_after(jiffies, rcu_state.gp_start + jtsq) ||
1081 time_after(jiffies, rcu_state.jiffies_resched))) {
1082 WRITE_ONCE(*rnhqp, true);
1083 /* Store rcu_need_heavy_qs before rcu_urgent_qs. */
1084 smp_store_release(ruqp, true);
1085 rcu_state.jiffies_resched += jtsq; /* Re-enable beating. */
1089 * If more than halfway to RCU CPU stall-warning time, do a
1090 * resched_cpu() to try to loosen things up a bit. Also check to
1091 * see if the CPU is getting hammered with interrupts, but only
1092 * once per grace period, just to keep the IPIs down to a dull roar.
1094 if (jiffies - rcu_state.gp_start > rcu_jiffies_till_stall_check() / 2) {
1095 resched_cpu(rdp->cpu);
1096 if (IS_ENABLED(CONFIG_IRQ_WORK) &&
1097 !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq &&
1098 (rnp->ffmask & rdp->grpmask)) {
1099 init_irq_work(&rdp->rcu_iw, rcu_iw_handler);
1100 rdp->rcu_iw_pending = true;
1101 rdp->rcu_iw_gp_seq = rnp->gp_seq;
1102 irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
1109 static void record_gp_stall_check_time(void)
1111 unsigned long j = jiffies;
1114 rcu_state.gp_start = j;
1115 j1 = rcu_jiffies_till_stall_check();
1116 /* Record ->gp_start before ->jiffies_stall. */
1117 smp_store_release(&rcu_state.jiffies_stall, j + j1); /* ^^^ */
1118 rcu_state.jiffies_resched = j + j1 / 2;
1119 rcu_state.n_force_qs_gpstart = READ_ONCE(rcu_state.n_force_qs);
1123 * Convert a ->gp_state value to a character string.
1125 static const char *gp_state_getname(short gs)
1127 if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
1129 return gp_state_names[gs];
1133 * Complain about starvation of grace-period kthread.
1135 static void rcu_check_gp_kthread_starvation(void)
1137 struct task_struct *gpk = rcu_state.gp_kthread;
1140 j = jiffies - READ_ONCE(rcu_state.gp_activity);
1142 pr_err("%s kthread starved for %ld jiffies! g%ld f%#x %s(%d) ->state=%#lx ->cpu=%d\n",
1144 (long)rcu_seq_current(&rcu_state.gp_seq),
1146 gp_state_getname(rcu_state.gp_state), rcu_state.gp_state,
1147 gpk ? gpk->state : ~0, gpk ? task_cpu(gpk) : -1);
1149 pr_err("RCU grace-period kthread stack dump:\n");
1150 sched_show_task(gpk);
1151 wake_up_process(gpk);
1157 * Dump stacks of all tasks running on stalled CPUs. First try using
1158 * NMIs, but fall back to manual remote stack tracing on architectures
1159 * that don't support NMI-based stack dumps. The NMI-triggered stack
1160 * traces are more accurate because they are printed by the target CPU.
1162 static void rcu_dump_cpu_stacks(void)
1165 unsigned long flags;
1166 struct rcu_node *rnp;
1168 rcu_for_each_leaf_node(rnp) {
1169 raw_spin_lock_irqsave_rcu_node(rnp, flags);
1170 for_each_leaf_node_possible_cpu(rnp, cpu)
1171 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu))
1172 if (!trigger_single_cpu_backtrace(cpu))
1174 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1179 * If too much time has passed in the current grace period, and if
1180 * so configured, go kick the relevant kthreads.
1182 static void rcu_stall_kick_kthreads(void)
1186 if (!rcu_kick_kthreads)
1188 j = READ_ONCE(rcu_state.jiffies_kick_kthreads);
1189 if (time_after(jiffies, j) && rcu_state.gp_kthread &&
1190 (rcu_gp_in_progress() || READ_ONCE(rcu_state.gp_flags))) {
1191 WARN_ONCE(1, "Kicking %s grace-period kthread\n",
1193 rcu_ftrace_dump(DUMP_ALL);
1194 wake_up_process(rcu_state.gp_kthread);
1195 WRITE_ONCE(rcu_state.jiffies_kick_kthreads, j + HZ);
1199 static void panic_on_rcu_stall(void)
1201 if (sysctl_panic_on_rcu_stall)
1202 panic("RCU Stall\n");
1205 static void print_other_cpu_stall(unsigned long gp_seq)
1208 unsigned long flags;
1212 struct rcu_node *rnp = rcu_get_root();
1215 /* Kick and suppress, if so configured. */
1216 rcu_stall_kick_kthreads();
1217 if (rcu_cpu_stall_suppress)
1221 * OK, time to rat on our buddy...
1222 * See Documentation/RCU/stallwarn.txt for info on how to debug
1223 * RCU CPU stall warnings.
1225 pr_err("INFO: %s detected stalls on CPUs/tasks:", rcu_state.name);
1226 print_cpu_stall_info_begin();
1227 rcu_for_each_leaf_node(rnp) {
1228 raw_spin_lock_irqsave_rcu_node(rnp, flags);
1229 ndetected += rcu_print_task_stall(rnp);
1230 if (rnp->qsmask != 0) {
1231 for_each_leaf_node_possible_cpu(rnp, cpu)
1232 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
1233 print_cpu_stall_info(cpu);
1237 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1240 print_cpu_stall_info_end();
1241 for_each_possible_cpu(cpu)
1242 totqlen += rcu_segcblist_n_cbs(&per_cpu_ptr(&rcu_data,
1244 pr_cont("(detected by %d, t=%ld jiffies, g=%ld, q=%lu)\n",
1245 smp_processor_id(), (long)(jiffies - rcu_state.gp_start),
1246 (long)rcu_seq_current(&rcu_state.gp_seq), totqlen);
1248 rcu_dump_cpu_stacks();
1250 /* Complain about tasks blocking the grace period. */
1251 rcu_print_detail_task_stall();
1253 if (rcu_seq_current(&rcu_state.gp_seq) != gp_seq) {
1254 pr_err("INFO: Stall ended before state dump start\n");
1257 gpa = READ_ONCE(rcu_state.gp_activity);
1258 pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
1259 rcu_state.name, j - gpa, j, gpa,
1260 jiffies_till_next_fqs,
1261 rcu_get_root()->qsmask);
1262 /* In this case, the current CPU might be at fault. */
1263 sched_show_task(current);
1266 /* Rewrite if needed in case of slow consoles. */
1267 if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
1268 WRITE_ONCE(rcu_state.jiffies_stall,
1269 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
1271 rcu_check_gp_kthread_starvation();
1273 panic_on_rcu_stall();
1275 force_quiescent_state(); /* Kick them all. */
1278 static void print_cpu_stall(void)
1281 unsigned long flags;
1282 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
1283 struct rcu_node *rnp = rcu_get_root();
1286 /* Kick and suppress, if so configured. */
1287 rcu_stall_kick_kthreads();
1288 if (rcu_cpu_stall_suppress)
1292 * OK, time to rat on ourselves...
1293 * See Documentation/RCU/stallwarn.txt for info on how to debug
1294 * RCU CPU stall warnings.
1296 pr_err("INFO: %s self-detected stall on CPU", rcu_state.name);
1297 print_cpu_stall_info_begin();
1298 raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
1299 print_cpu_stall_info(smp_processor_id());
1300 raw_spin_unlock_irqrestore_rcu_node(rdp->mynode, flags);
1301 print_cpu_stall_info_end();
1302 for_each_possible_cpu(cpu)
1303 totqlen += rcu_segcblist_n_cbs(&per_cpu_ptr(&rcu_data,
1305 pr_cont(" (t=%lu jiffies g=%ld q=%lu)\n",
1306 jiffies - rcu_state.gp_start,
1307 (long)rcu_seq_current(&rcu_state.gp_seq), totqlen);
1309 rcu_check_gp_kthread_starvation();
1311 rcu_dump_cpu_stacks();
1313 raw_spin_lock_irqsave_rcu_node(rnp, flags);
1314 /* Rewrite if needed in case of slow consoles. */
1315 if (ULONG_CMP_GE(jiffies, READ_ONCE(rcu_state.jiffies_stall)))
1316 WRITE_ONCE(rcu_state.jiffies_stall,
1317 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
1318 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1320 panic_on_rcu_stall();
1323 * Attempt to revive the RCU machinery by forcing a context switch.
1325 * A context switch would normally allow the RCU state machine to make
1326 * progress and it could be we're stuck in kernel space without context
1327 * switches for an entirely unreasonable amount of time.
1329 resched_cpu(smp_processor_id());
1332 static void check_cpu_stall(struct rcu_data *rdp)
1340 struct rcu_node *rnp;
1342 if ((rcu_cpu_stall_suppress && !rcu_kick_kthreads) ||
1343 !rcu_gp_in_progress())
1345 rcu_stall_kick_kthreads();
1349 * Lots of memory barriers to reject false positives.
1351 * The idea is to pick up rcu_state.gp_seq, then
1352 * rcu_state.jiffies_stall, then rcu_state.gp_start, and finally
1353 * another copy of rcu_state.gp_seq. These values are updated in
1354 * the opposite order with memory barriers (or equivalent) during
1355 * grace-period initialization and cleanup. Now, a false positive
1356 * can occur if we get an new value of rcu_state.gp_start and a old
1357 * value of rcu_state.jiffies_stall. But given the memory barriers,
1358 * the only way that this can happen is if one grace period ends
1359 * and another starts between these two fetches. This is detected
1360 * by comparing the second fetch of rcu_state.gp_seq with the
1361 * previous fetch from rcu_state.gp_seq.
1363 * Given this check, comparisons of jiffies, rcu_state.jiffies_stall,
1364 * and rcu_state.gp_start suffice to forestall false positives.
1366 gs1 = READ_ONCE(rcu_state.gp_seq);
1367 smp_rmb(); /* Pick up ->gp_seq first... */
1368 js = READ_ONCE(rcu_state.jiffies_stall);
1369 smp_rmb(); /* ...then ->jiffies_stall before the rest... */
1370 gps = READ_ONCE(rcu_state.gp_start);
1371 smp_rmb(); /* ...and finally ->gp_start before ->gp_seq again. */
1372 gs2 = READ_ONCE(rcu_state.gp_seq);
1374 ULONG_CMP_LT(j, js) ||
1375 ULONG_CMP_GE(gps, js))
1376 return; /* No stall or GP completed since entering function. */
1378 jn = jiffies + 3 * rcu_jiffies_till_stall_check() + 3;
1379 if (rcu_gp_in_progress() &&
1380 (READ_ONCE(rnp->qsmask) & rdp->grpmask) &&
1381 cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
1383 /* We haven't checked in, so go dump stack. */
1386 } else if (rcu_gp_in_progress() &&
1387 ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY) &&
1388 cmpxchg(&rcu_state.jiffies_stall, js, jn) == js) {
1390 /* They had a few time units to dump stack, so complain. */
1391 print_other_cpu_stall(gs2);
1396 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
1398 * Set the stall-warning timeout way off into the future, thus preventing
1399 * any RCU CPU stall-warning messages from appearing in the current set of
1400 * RCU grace periods.
1402 * The caller must disable hard irqs.
1404 void rcu_cpu_stall_reset(void)
1406 WRITE_ONCE(rcu_state.jiffies_stall, jiffies + ULONG_MAX / 2);
1409 /* Trace-event wrapper function for trace_rcu_future_grace_period. */
1410 static void trace_rcu_this_gp(struct rcu_node *rnp, struct rcu_data *rdp,
1411 unsigned long gp_seq_req, const char *s)
1413 trace_rcu_future_grace_period(rcu_state.name, rnp->gp_seq, gp_seq_req,
1414 rnp->level, rnp->grplo, rnp->grphi, s);
1418 * rcu_start_this_gp - Request the start of a particular grace period
1419 * @rnp_start: The leaf node of the CPU from which to start.
1420 * @rdp: The rcu_data corresponding to the CPU from which to start.
1421 * @gp_seq_req: The gp_seq of the grace period to start.
1423 * Start the specified grace period, as needed to handle newly arrived
1424 * callbacks. The required future grace periods are recorded in each
1425 * rcu_node structure's ->gp_seq_needed field. Returns true if there
1426 * is reason to awaken the grace-period kthread.
1428 * The caller must hold the specified rcu_node structure's ->lock, which
1429 * is why the caller is responsible for waking the grace-period kthread.
1431 * Returns true if the GP thread needs to be awakened else false.
1433 static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp,
1434 unsigned long gp_seq_req)
1437 struct rcu_node *rnp;
1440 * Use funnel locking to either acquire the root rcu_node
1441 * structure's lock or bail out if the need for this grace period
1442 * has already been recorded -- or if that grace period has in
1443 * fact already started. If there is already a grace period in
1444 * progress in a non-leaf node, no recording is needed because the
1445 * end of the grace period will scan the leaf rcu_node structures.
1446 * Note that rnp_start->lock must not be released.
1448 raw_lockdep_assert_held_rcu_node(rnp_start);
1449 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, TPS("Startleaf"));
1450 for (rnp = rnp_start; 1; rnp = rnp->parent) {
1451 if (rnp != rnp_start)
1452 raw_spin_lock_rcu_node(rnp);
1453 if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) ||
1454 rcu_seq_started(&rnp->gp_seq, gp_seq_req) ||
1455 (rnp != rnp_start &&
1456 rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) {
1457 trace_rcu_this_gp(rnp, rdp, gp_seq_req,
1461 rnp->gp_seq_needed = gp_seq_req;
1462 if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) {
1464 * We just marked the leaf or internal node, and a
1465 * grace period is in progress, which means that
1466 * rcu_gp_cleanup() will see the marking. Bail to
1467 * reduce contention.
1469 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req,
1470 TPS("Startedleaf"));
1473 if (rnp != rnp_start && rnp->parent != NULL)
1474 raw_spin_unlock_rcu_node(rnp);
1476 break; /* At root, and perhaps also leaf. */
1479 /* If GP already in progress, just leave, otherwise start one. */
1480 if (rcu_gp_in_progress()) {
1481 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedleafroot"));
1484 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedroot"));
1485 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT);
1486 rcu_state.gp_req_activity = jiffies;
1487 if (!rcu_state.gp_kthread) {
1488 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("NoGPkthread"));
1491 trace_rcu_grace_period(rcu_state.name, READ_ONCE(rcu_state.gp_seq), TPS("newreq"));
1492 ret = true; /* Caller must wake GP kthread. */
1494 /* Push furthest requested GP to leaf node and rcu_data structure. */
1495 if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) {
1496 rnp_start->gp_seq_needed = rnp->gp_seq_needed;
1497 rdp->gp_seq_needed = rnp->gp_seq_needed;
1499 if (rnp != rnp_start)
1500 raw_spin_unlock_rcu_node(rnp);
1505 * Clean up any old requests for the just-ended grace period. Also return
1506 * whether any additional grace periods have been requested.
1508 static bool rcu_future_gp_cleanup(struct rcu_node *rnp)
1511 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
1513 needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed);
1515 rnp->gp_seq_needed = rnp->gp_seq; /* Avoid counter wrap. */
1516 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq,
1517 needmore ? TPS("CleanupMore") : TPS("Cleanup"));
1522 * Awaken the grace-period kthread. Don't do a self-awaken, and don't
1523 * bother awakening when there is nothing for the grace-period kthread
1524 * to do (as in several CPUs raced to awaken, and we lost), and finally
1525 * don't try to awaken a kthread that has not yet been created.
1527 static void rcu_gp_kthread_wake(void)
1529 if (current == rcu_state.gp_kthread ||
1530 !READ_ONCE(rcu_state.gp_flags) ||
1531 !rcu_state.gp_kthread)
1533 swake_up_one(&rcu_state.gp_wq);
1537 * If there is room, assign a ->gp_seq number to any callbacks on this
1538 * CPU that have not already been assigned. Also accelerate any callbacks
1539 * that were previously assigned a ->gp_seq number that has since proven
1540 * to be too conservative, which can happen if callbacks get assigned a
1541 * ->gp_seq number while RCU is idle, but with reference to a non-root
1542 * rcu_node structure. This function is idempotent, so it does not hurt
1543 * to call it repeatedly. Returns an flag saying that we should awaken
1544 * the RCU grace-period kthread.
1546 * The caller must hold rnp->lock with interrupts disabled.
1548 static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
1550 unsigned long gp_seq_req;
1553 raw_lockdep_assert_held_rcu_node(rnp);
1555 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1556 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
1560 * Callbacks are often registered with incomplete grace-period
1561 * information. Something about the fact that getting exact
1562 * information requires acquiring a global lock... RCU therefore
1563 * makes a conservative estimate of the grace period number at which
1564 * a given callback will become ready to invoke. The following
1565 * code checks this estimate and improves it when possible, thus
1566 * accelerating callback invocation to an earlier grace-period
1569 gp_seq_req = rcu_seq_snap(&rcu_state.gp_seq);
1570 if (rcu_segcblist_accelerate(&rdp->cblist, gp_seq_req))
1571 ret = rcu_start_this_gp(rnp, rdp, gp_seq_req);
1573 /* Trace depending on how much we were able to accelerate. */
1574 if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
1575 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccWaitCB"));
1577 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccReadyCB"));
1582 * Similar to rcu_accelerate_cbs(), but does not require that the leaf
1583 * rcu_node structure's ->lock be held. It consults the cached value
1584 * of ->gp_seq_needed in the rcu_data structure, and if that indicates
1585 * that a new grace-period request be made, invokes rcu_accelerate_cbs()
1586 * while holding the leaf rcu_node structure's ->lock.
1588 static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp,
1589 struct rcu_data *rdp)
1594 lockdep_assert_irqs_disabled();
1595 c = rcu_seq_snap(&rcu_state.gp_seq);
1596 if (!rdp->gpwrap && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
1597 /* Old request still live, so mark recent callbacks. */
1598 (void)rcu_segcblist_accelerate(&rdp->cblist, c);
1601 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
1602 needwake = rcu_accelerate_cbs(rnp, rdp);
1603 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
1605 rcu_gp_kthread_wake();
1609 * Move any callbacks whose grace period has completed to the
1610 * RCU_DONE_TAIL sublist, then compact the remaining sublists and
1611 * assign ->gp_seq numbers to any callbacks in the RCU_NEXT_TAIL
1612 * sublist. This function is idempotent, so it does not hurt to
1613 * invoke it repeatedly. As long as it is not invoked -too- often...
1614 * Returns true if the RCU grace-period kthread needs to be awakened.
1616 * The caller must hold rnp->lock with interrupts disabled.
1618 static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
1620 raw_lockdep_assert_held_rcu_node(rnp);
1622 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1623 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
1627 * Find all callbacks whose ->gp_seq numbers indicate that they
1628 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
1630 rcu_segcblist_advance(&rdp->cblist, rnp->gp_seq);
1632 /* Classify any remaining callbacks. */
1633 return rcu_accelerate_cbs(rnp, rdp);
1637 * Update CPU-local rcu_data state to record the beginnings and ends of
1638 * grace periods. The caller must hold the ->lock of the leaf rcu_node
1639 * structure corresponding to the current CPU, and must have irqs disabled.
1640 * Returns true if the grace-period kthread needs to be awakened.
1642 static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp)
1647 raw_lockdep_assert_held_rcu_node(rnp);
1649 if (rdp->gp_seq == rnp->gp_seq)
1650 return false; /* Nothing to do. */
1652 /* Handle the ends of any preceding grace periods first. */
1653 if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) ||
1654 unlikely(READ_ONCE(rdp->gpwrap))) {
1655 ret = rcu_advance_cbs(rnp, rdp); /* Advance callbacks. */
1656 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend"));
1658 ret = rcu_accelerate_cbs(rnp, rdp); /* Recent callbacks. */
1661 /* Now handle the beginnings of any new-to-this-CPU grace periods. */
1662 if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) ||
1663 unlikely(READ_ONCE(rdp->gpwrap))) {
1665 * If the current grace period is waiting for this CPU,
1666 * set up to detect a quiescent state, otherwise don't
1667 * go looking for one.
1669 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart"));
1670 need_gp = !!(rnp->qsmask & rdp->grpmask);
1671 rdp->cpu_no_qs.b.norm = need_gp;
1672 rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_dynticks.rcu_qs_ctr);
1673 rdp->core_needs_qs = need_gp;
1674 zero_cpu_stall_ticks(rdp);
1676 rdp->gp_seq = rnp->gp_seq; /* Remember new grace-period state. */
1677 if (ULONG_CMP_GE(rnp->gp_seq_needed, rdp->gp_seq_needed) || rdp->gpwrap)
1678 rdp->gp_seq_needed = rnp->gp_seq_needed;
1679 WRITE_ONCE(rdp->gpwrap, false);
1680 rcu_gpnum_ovf(rnp, rdp);
1684 static void note_gp_changes(struct rcu_data *rdp)
1686 unsigned long flags;
1688 struct rcu_node *rnp;
1690 local_irq_save(flags);
1692 if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) &&
1693 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
1694 !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
1695 local_irq_restore(flags);
1698 needwake = __note_gp_changes(rnp, rdp);
1699 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1701 rcu_gp_kthread_wake();
1704 static void rcu_gp_slow(int delay)
1707 !(rcu_seq_ctr(rcu_state.gp_seq) %
1708 (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
1709 schedule_timeout_uninterruptible(delay);
1713 * Initialize a new grace period. Return false if no grace period required.
1715 static bool rcu_gp_init(void)
1717 unsigned long flags;
1718 unsigned long oldmask;
1720 struct rcu_data *rdp;
1721 struct rcu_node *rnp = rcu_get_root();
1723 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1724 raw_spin_lock_irq_rcu_node(rnp);
1725 if (!READ_ONCE(rcu_state.gp_flags)) {
1726 /* Spurious wakeup, tell caller to go back to sleep. */
1727 raw_spin_unlock_irq_rcu_node(rnp);
1730 WRITE_ONCE(rcu_state.gp_flags, 0); /* Clear all flags: New GP. */
1732 if (WARN_ON_ONCE(rcu_gp_in_progress())) {
1734 * Grace period already in progress, don't start another.
1735 * Not supposed to be able to happen.
1737 raw_spin_unlock_irq_rcu_node(rnp);
1741 /* Advance to a new grace period and initialize state. */
1742 record_gp_stall_check_time();
1743 /* Record GP times before starting GP, hence rcu_seq_start(). */
1744 rcu_seq_start(&rcu_state.gp_seq);
1745 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
1746 raw_spin_unlock_irq_rcu_node(rnp);
1749 * Apply per-leaf buffered online and offline operations to the
1750 * rcu_node tree. Note that this new grace period need not wait
1751 * for subsequent online CPUs, and that quiescent-state forcing
1752 * will handle subsequent offline CPUs.
1754 rcu_state.gp_state = RCU_GP_ONOFF;
1755 rcu_for_each_leaf_node(rnp) {
1756 spin_lock(&rcu_state.ofl_lock);
1757 raw_spin_lock_irq_rcu_node(rnp);
1758 if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
1759 !rnp->wait_blkd_tasks) {
1760 /* Nothing to do on this leaf rcu_node structure. */
1761 raw_spin_unlock_irq_rcu_node(rnp);
1762 spin_unlock(&rcu_state.ofl_lock);
1766 /* Record old state, apply changes to ->qsmaskinit field. */
1767 oldmask = rnp->qsmaskinit;
1768 rnp->qsmaskinit = rnp->qsmaskinitnext;
1770 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
1771 if (!oldmask != !rnp->qsmaskinit) {
1772 if (!oldmask) { /* First online CPU for rcu_node. */
1773 if (!rnp->wait_blkd_tasks) /* Ever offline? */
1774 rcu_init_new_rnp(rnp);
1775 } else if (rcu_preempt_has_tasks(rnp)) {
1776 rnp->wait_blkd_tasks = true; /* blocked tasks */
1777 } else { /* Last offline CPU and can propagate. */
1778 rcu_cleanup_dead_rnp(rnp);
1783 * If all waited-on tasks from prior grace period are
1784 * done, and if all this rcu_node structure's CPUs are
1785 * still offline, propagate up the rcu_node tree and
1786 * clear ->wait_blkd_tasks. Otherwise, if one of this
1787 * rcu_node structure's CPUs has since come back online,
1788 * simply clear ->wait_blkd_tasks.
1790 if (rnp->wait_blkd_tasks &&
1791 (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) {
1792 rnp->wait_blkd_tasks = false;
1793 if (!rnp->qsmaskinit)
1794 rcu_cleanup_dead_rnp(rnp);
1797 raw_spin_unlock_irq_rcu_node(rnp);
1798 spin_unlock(&rcu_state.ofl_lock);
1800 rcu_gp_slow(gp_preinit_delay); /* Races with CPU hotplug. */
1803 * Set the quiescent-state-needed bits in all the rcu_node
1804 * structures for all currently online CPUs in breadth-first
1805 * order, starting from the root rcu_node structure, relying on the
1806 * layout of the tree within the rcu_state.node[] array. Note that
1807 * other CPUs will access only the leaves of the hierarchy, thus
1808 * seeing that no grace period is in progress, at least until the
1809 * corresponding leaf node has been initialized.
1811 * The grace period cannot complete until the initialization
1812 * process finishes, because this kthread handles both.
1814 rcu_state.gp_state = RCU_GP_INIT;
1815 rcu_for_each_node_breadth_first(rnp) {
1816 rcu_gp_slow(gp_init_delay);
1817 raw_spin_lock_irqsave_rcu_node(rnp, flags);
1818 rdp = this_cpu_ptr(&rcu_data);
1819 rcu_preempt_check_blocked_tasks(rnp);
1820 rnp->qsmask = rnp->qsmaskinit;
1821 WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);
1822 if (rnp == rdp->mynode)
1823 (void)__note_gp_changes(rnp, rdp);
1824 rcu_preempt_boost_start_gp(rnp);
1825 trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq,
1826 rnp->level, rnp->grplo,
1827 rnp->grphi, rnp->qsmask);
1828 /* Quiescent states for tasks on any now-offline CPUs. */
1829 mask = rnp->qsmask & ~rnp->qsmaskinitnext;
1830 rnp->rcu_gp_init_mask = mask;
1831 if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp))
1832 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
1834 raw_spin_unlock_irq_rcu_node(rnp);
1835 cond_resched_tasks_rcu_qs();
1836 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1843 * Helper function for swait_event_idle_exclusive() wakeup at force-quiescent-state
1846 static bool rcu_gp_fqs_check_wake(int *gfp)
1848 struct rcu_node *rnp = rcu_get_root();
1850 /* Someone like call_rcu() requested a force-quiescent-state scan. */
1851 *gfp = READ_ONCE(rcu_state.gp_flags);
1852 if (*gfp & RCU_GP_FLAG_FQS)
1855 /* The current grace period has completed. */
1856 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
1863 * Do one round of quiescent-state forcing.
1865 static void rcu_gp_fqs(bool first_time)
1867 struct rcu_node *rnp = rcu_get_root();
1869 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1870 rcu_state.n_force_qs++;
1872 /* Collect dyntick-idle snapshots. */
1873 force_qs_rnp(dyntick_save_progress_counter);
1875 /* Handle dyntick-idle and offline CPUs. */
1876 force_qs_rnp(rcu_implicit_dynticks_qs);
1878 /* Clear flag to prevent immediate re-entry. */
1879 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
1880 raw_spin_lock_irq_rcu_node(rnp);
1881 WRITE_ONCE(rcu_state.gp_flags,
1882 READ_ONCE(rcu_state.gp_flags) & ~RCU_GP_FLAG_FQS);
1883 raw_spin_unlock_irq_rcu_node(rnp);
1888 * Loop doing repeated quiescent-state forcing until the grace period ends.
1890 static void rcu_gp_fqs_loop(void)
1896 struct rcu_node *rnp = rcu_get_root();
1898 first_gp_fqs = true;
1899 j = jiffies_till_first_fqs;
1903 rcu_state.jiffies_force_qs = jiffies + j;
1904 WRITE_ONCE(rcu_state.jiffies_kick_kthreads,
1907 trace_rcu_grace_period(rcu_state.name,
1908 READ_ONCE(rcu_state.gp_seq),
1910 rcu_state.gp_state = RCU_GP_WAIT_FQS;
1911 ret = swait_event_idle_timeout_exclusive(
1912 rcu_state.gp_wq, rcu_gp_fqs_check_wake(&gf), j);
1913 rcu_state.gp_state = RCU_GP_DOING_FQS;
1914 /* Locking provides needed memory barriers. */
1915 /* If grace period done, leave loop. */
1916 if (!READ_ONCE(rnp->qsmask) &&
1917 !rcu_preempt_blocked_readers_cgp(rnp))
1919 /* If time for quiescent-state forcing, do it. */
1920 if (ULONG_CMP_GE(jiffies, rcu_state.jiffies_force_qs) ||
1921 (gf & RCU_GP_FLAG_FQS)) {
1922 trace_rcu_grace_period(rcu_state.name,
1923 READ_ONCE(rcu_state.gp_seq),
1925 rcu_gp_fqs(first_gp_fqs);
1926 first_gp_fqs = false;
1927 trace_rcu_grace_period(rcu_state.name,
1928 READ_ONCE(rcu_state.gp_seq),
1930 cond_resched_tasks_rcu_qs();
1931 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1932 ret = 0; /* Force full wait till next FQS. */
1933 j = jiffies_till_next_fqs;
1935 /* Deal with stray signal. */
1936 cond_resched_tasks_rcu_qs();
1937 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1938 WARN_ON(signal_pending(current));
1939 trace_rcu_grace_period(rcu_state.name,
1940 READ_ONCE(rcu_state.gp_seq),
1942 ret = 1; /* Keep old FQS timing. */
1944 if (time_after(jiffies, rcu_state.jiffies_force_qs))
1947 j = rcu_state.jiffies_force_qs - j;
1953 * Clean up after the old grace period.
1955 static void rcu_gp_cleanup(void)
1957 unsigned long gp_duration;
1958 bool needgp = false;
1959 unsigned long new_gp_seq;
1960 struct rcu_data *rdp;
1961 struct rcu_node *rnp = rcu_get_root();
1962 struct swait_queue_head *sq;
1964 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1965 raw_spin_lock_irq_rcu_node(rnp);
1966 gp_duration = jiffies - rcu_state.gp_start;
1967 if (gp_duration > rcu_state.gp_max)
1968 rcu_state.gp_max = gp_duration;
1971 * We know the grace period is complete, but to everyone else
1972 * it appears to still be ongoing. But it is also the case
1973 * that to everyone else it looks like there is nothing that
1974 * they can do to advance the grace period. It is therefore
1975 * safe for us to drop the lock in order to mark the grace
1976 * period as completed in all of the rcu_node structures.
1978 raw_spin_unlock_irq_rcu_node(rnp);
1981 * Propagate new ->gp_seq value to rcu_node structures so that
1982 * other CPUs don't have to wait until the start of the next grace
1983 * period to process their callbacks. This also avoids some nasty
1984 * RCU grace-period initialization races by forcing the end of
1985 * the current grace period to be completely recorded in all of
1986 * the rcu_node structures before the beginning of the next grace
1987 * period is recorded in any of the rcu_node structures.
1989 new_gp_seq = rcu_state.gp_seq;
1990 rcu_seq_end(&new_gp_seq);
1991 rcu_for_each_node_breadth_first(rnp) {
1992 raw_spin_lock_irq_rcu_node(rnp);
1993 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
1994 dump_blkd_tasks(rnp, 10);
1995 WARN_ON_ONCE(rnp->qsmask);
1996 WRITE_ONCE(rnp->gp_seq, new_gp_seq);
1997 rdp = this_cpu_ptr(&rcu_data);
1998 if (rnp == rdp->mynode)
1999 needgp = __note_gp_changes(rnp, rdp) || needgp;
2000 /* smp_mb() provided by prior unlock-lock pair. */
2001 needgp = rcu_future_gp_cleanup(rnp) || needgp;
2002 sq = rcu_nocb_gp_get(rnp);
2003 raw_spin_unlock_irq_rcu_node(rnp);
2004 rcu_nocb_gp_cleanup(sq);
2005 cond_resched_tasks_rcu_qs();
2006 WRITE_ONCE(rcu_state.gp_activity, jiffies);
2007 rcu_gp_slow(gp_cleanup_delay);
2009 rnp = rcu_get_root();
2010 raw_spin_lock_irq_rcu_node(rnp); /* GP before ->gp_seq update. */
2012 /* Declare grace period done. */
2013 rcu_seq_end(&rcu_state.gp_seq);
2014 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end"));
2015 rcu_state.gp_state = RCU_GP_IDLE;
2016 /* Check for GP requests since above loop. */
2017 rdp = this_cpu_ptr(&rcu_data);
2018 if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) {
2019 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq_needed,
2020 TPS("CleanupMore"));
2023 /* Advance CBs to reduce false positives below. */
2024 if (!rcu_accelerate_cbs(rnp, rdp) && needgp) {
2025 WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT);
2026 rcu_state.gp_req_activity = jiffies;
2027 trace_rcu_grace_period(rcu_state.name,
2028 READ_ONCE(rcu_state.gp_seq),
2031 WRITE_ONCE(rcu_state.gp_flags,
2032 rcu_state.gp_flags & RCU_GP_FLAG_INIT);
2034 raw_spin_unlock_irq_rcu_node(rnp);
2038 * Body of kthread that handles grace periods.
2040 static int __noreturn rcu_gp_kthread(void *unused)
2042 rcu_bind_gp_kthread();
2045 /* Handle grace-period start. */
2047 trace_rcu_grace_period(rcu_state.name,
2048 READ_ONCE(rcu_state.gp_seq),
2050 rcu_state.gp_state = RCU_GP_WAIT_GPS;
2051 swait_event_idle_exclusive(rcu_state.gp_wq,
2052 READ_ONCE(rcu_state.gp_flags) &
2054 rcu_state.gp_state = RCU_GP_DONE_GPS;
2055 /* Locking provides needed memory barrier. */
2058 cond_resched_tasks_rcu_qs();
2059 WRITE_ONCE(rcu_state.gp_activity, jiffies);
2060 WARN_ON(signal_pending(current));
2061 trace_rcu_grace_period(rcu_state.name,
2062 READ_ONCE(rcu_state.gp_seq),
2066 /* Handle quiescent-state forcing. */
2069 /* Handle grace-period end. */
2070 rcu_state.gp_state = RCU_GP_CLEANUP;
2072 rcu_state.gp_state = RCU_GP_CLEANED;
2077 * Report a full set of quiescent states to the rcu_state data structure.
2078 * Invoke rcu_gp_kthread_wake() to awaken the grace-period kthread if
2079 * another grace period is required. Whether we wake the grace-period
2080 * kthread or it awakens itself for the next round of quiescent-state
2081 * forcing, that kthread will clean up after the just-completed grace
2082 * period. Note that the caller must hold rnp->lock, which is released
2085 static void rcu_report_qs_rsp(unsigned long flags)
2086 __releases(rcu_get_root()->lock)
2088 raw_lockdep_assert_held_rcu_node(rcu_get_root());
2089 WARN_ON_ONCE(!rcu_gp_in_progress());
2090 WRITE_ONCE(rcu_state.gp_flags,
2091 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
2092 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags);
2093 rcu_gp_kthread_wake();
2097 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
2098 * Allows quiescent states for a group of CPUs to be reported at one go
2099 * to the specified rcu_node structure, though all the CPUs in the group
2100 * must be represented by the same rcu_node structure (which need not be a
2101 * leaf rcu_node structure, though it often will be). The gps parameter
2102 * is the grace-period snapshot, which means that the quiescent states
2103 * are valid only if rnp->gp_seq is equal to gps. That structure's lock
2104 * must be held upon entry, and it is released before return.
2106 * As a special case, if mask is zero, the bit-already-cleared check is
2107 * disabled. This allows propagating quiescent state due to resumed tasks
2108 * during grace-period initialization.
2110 static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
2111 unsigned long gps, unsigned long flags)
2112 __releases(rnp->lock)
2114 unsigned long oldmask = 0;
2115 struct rcu_node *rnp_c;
2117 raw_lockdep_assert_held_rcu_node(rnp);
2119 /* Walk up the rcu_node hierarchy. */
2121 if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) {
2124 * Our bit has already been cleared, or the
2125 * relevant grace period is already over, so done.
2127 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2130 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
2131 WARN_ON_ONCE(!rcu_is_leaf_node(rnp) &&
2132 rcu_preempt_blocked_readers_cgp(rnp));
2133 rnp->qsmask &= ~mask;
2134 trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq,
2135 mask, rnp->qsmask, rnp->level,
2136 rnp->grplo, rnp->grphi,
2138 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
2140 /* Other bits still set at this level, so done. */
2141 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2144 rnp->completedqs = rnp->gp_seq;
2145 mask = rnp->grpmask;
2146 if (rnp->parent == NULL) {
2148 /* No more levels. Exit loop holding root lock. */
2152 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2155 raw_spin_lock_irqsave_rcu_node(rnp, flags);
2156 oldmask = rnp_c->qsmask;
2160 * Get here if we are the last CPU to pass through a quiescent
2161 * state for this grace period. Invoke rcu_report_qs_rsp()
2162 * to clean up and start the next grace period if one is needed.
2164 rcu_report_qs_rsp(flags); /* releases rnp->lock. */
2168 * Record a quiescent state for all tasks that were previously queued
2169 * on the specified rcu_node structure and that were blocking the current
2170 * RCU grace period. The caller must hold the corresponding rnp->lock with
2171 * irqs disabled, and this lock is released upon return, but irqs remain
2174 static void __maybe_unused
2175 rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
2176 __releases(rnp->lock)
2180 struct rcu_node *rnp_p;
2182 raw_lockdep_assert_held_rcu_node(rnp);
2183 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT)) ||
2184 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) ||
2186 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2187 return; /* Still need more quiescent states! */
2190 rnp->completedqs = rnp->gp_seq;
2191 rnp_p = rnp->parent;
2192 if (rnp_p == NULL) {
2194 * Only one rcu_node structure in the tree, so don't
2195 * try to report up to its nonexistent parent!
2197 rcu_report_qs_rsp(flags);
2201 /* Report up the rest of the hierarchy, tracking current ->gp_seq. */
2203 mask = rnp->grpmask;
2204 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
2205 raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
2206 rcu_report_qs_rnp(mask, rnp_p, gps, flags);
2210 * Record a quiescent state for the specified CPU to that CPU's rcu_data
2211 * structure. This must be called from the specified CPU.
2214 rcu_report_qs_rdp(int cpu, struct rcu_data *rdp)
2216 unsigned long flags;
2219 struct rcu_node *rnp;
2222 raw_spin_lock_irqsave_rcu_node(rnp, flags);
2223 if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq ||
2227 * The grace period in which this quiescent state was
2228 * recorded has ended, so don't report it upwards.
2229 * We will instead need a new quiescent state that lies
2230 * within the current grace period.
2232 rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
2233 rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_dynticks.rcu_qs_ctr);
2234 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2237 mask = rdp->grpmask;
2238 if ((rnp->qsmask & mask) == 0) {
2239 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2241 rdp->core_needs_qs = false;
2244 * This GP can't end until cpu checks in, so all of our
2245 * callbacks can be processed during the next GP.
2247 needwake = rcu_accelerate_cbs(rnp, rdp);
2249 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
2250 /* ^^^ Released rnp->lock */
2252 rcu_gp_kthread_wake();
2257 * Check to see if there is a new grace period of which this CPU
2258 * is not yet aware, and if so, set up local rcu_data state for it.
2259 * Otherwise, see if this CPU has just passed through its first
2260 * quiescent state for this grace period, and record that fact if so.
2263 rcu_check_quiescent_state(struct rcu_data *rdp)
2265 /* Check for grace-period ends and beginnings. */
2266 note_gp_changes(rdp);
2269 * Does this CPU still need to do its part for current grace period?
2270 * If no, return and let the other CPUs do their part as well.
2272 if (!rdp->core_needs_qs)
2276 * Was there a quiescent state since the beginning of the grace
2277 * period? If no, then exit and wait for the next call.
2279 if (rdp->cpu_no_qs.b.norm)
2283 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
2286 rcu_report_qs_rdp(rdp->cpu, rdp);
2290 * Near the end of the offline process. Trace the fact that this CPU
2293 int rcutree_dying_cpu(unsigned int cpu)
2295 RCU_TRACE(bool blkd;)
2296 RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(&rcu_data);)
2297 RCU_TRACE(struct rcu_node *rnp = rdp->mynode;)
2299 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2302 RCU_TRACE(blkd = !!(rnp->qsmask & rdp->grpmask);)
2303 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq,
2304 blkd ? TPS("cpuofl") : TPS("cpuofl-bgp"));
2309 * All CPUs for the specified rcu_node structure have gone offline,
2310 * and all tasks that were preempted within an RCU read-side critical
2311 * section while running on one of those CPUs have since exited their RCU
2312 * read-side critical section. Some other CPU is reporting this fact with
2313 * the specified rcu_node structure's ->lock held and interrupts disabled.
2314 * This function therefore goes up the tree of rcu_node structures,
2315 * clearing the corresponding bits in the ->qsmaskinit fields. Note that
2316 * the leaf rcu_node structure's ->qsmaskinit field has already been
2319 * This function does check that the specified rcu_node structure has
2320 * all CPUs offline and no blocked tasks, so it is OK to invoke it
2321 * prematurely. That said, invoking it after the fact will cost you
2322 * a needless lock acquisition. So once it has done its work, don't
2325 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
2328 struct rcu_node *rnp = rnp_leaf;
2330 raw_lockdep_assert_held_rcu_node(rnp_leaf);
2331 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
2332 WARN_ON_ONCE(rnp_leaf->qsmaskinit) ||
2333 WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf)))
2336 mask = rnp->grpmask;
2340 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
2341 rnp->qsmaskinit &= ~mask;
2342 /* Between grace periods, so better already be zero! */
2343 WARN_ON_ONCE(rnp->qsmask);
2344 if (rnp->qsmaskinit) {
2345 raw_spin_unlock_rcu_node(rnp);
2346 /* irqs remain disabled. */
2349 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
2354 * The CPU has been completely removed, and some other CPU is reporting
2355 * this fact from process context. Do the remainder of the cleanup.
2356 * There can only be one CPU hotplug operation at a time, so no need for
2359 int rcutree_dead_cpu(unsigned int cpu)
2361 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
2362 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
2364 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2367 /* Adjust any no-longer-needed kthreads. */
2368 rcu_boost_kthread_setaffinity(rnp, -1);
2369 /* Do any needed no-CB deferred wakeups from this CPU. */
2370 do_nocb_deferred_wakeup(per_cpu_ptr(&rcu_data, cpu));
2375 * Invoke any RCU callbacks that have made it to the end of their grace
2376 * period. Thottle as specified by rdp->blimit.
2378 static void rcu_do_batch(struct rcu_data *rdp)
2380 unsigned long flags;
2381 struct rcu_head *rhp;
2382 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
2385 /* If no callbacks are ready, just return. */
2386 if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
2387 trace_rcu_batch_start(rcu_state.name,
2388 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2389 rcu_segcblist_n_cbs(&rdp->cblist), 0);
2390 trace_rcu_batch_end(rcu_state.name, 0,
2391 !rcu_segcblist_empty(&rdp->cblist),
2392 need_resched(), is_idle_task(current),
2393 rcu_is_callbacks_kthread());
2398 * Extract the list of ready callbacks, disabling to prevent
2399 * races with call_rcu() from interrupt handlers. Leave the
2400 * callback counts, as rcu_barrier() needs to be conservative.
2402 local_irq_save(flags);
2403 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
2405 trace_rcu_batch_start(rcu_state.name,
2406 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2407 rcu_segcblist_n_cbs(&rdp->cblist), bl);
2408 rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
2409 local_irq_restore(flags);
2411 /* Invoke callbacks. */
2412 rhp = rcu_cblist_dequeue(&rcl);
2413 for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
2414 debug_rcu_head_unqueue(rhp);
2415 if (__rcu_reclaim(rcu_state.name, rhp))
2416 rcu_cblist_dequeued_lazy(&rcl);
2418 * Stop only if limit reached and CPU has something to do.
2419 * Note: The rcl structure counts down from zero.
2421 if (-rcl.len >= bl &&
2423 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
2427 local_irq_save(flags);
2429 trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
2430 is_idle_task(current), rcu_is_callbacks_kthread());
2432 /* Update counts and requeue any remaining callbacks. */
2433 rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
2434 smp_mb(); /* List handling before counting for rcu_barrier(). */
2435 rcu_segcblist_insert_count(&rdp->cblist, &rcl);
2437 /* Reinstate batch limit if we have worked down the excess. */
2438 count = rcu_segcblist_n_cbs(&rdp->cblist);
2439 if (rdp->blimit == LONG_MAX && count <= qlowmark)
2440 rdp->blimit = blimit;
2442 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
2443 if (count == 0 && rdp->qlen_last_fqs_check != 0) {
2444 rdp->qlen_last_fqs_check = 0;
2445 rdp->n_force_qs_snap = rcu_state.n_force_qs;
2446 } else if (count < rdp->qlen_last_fqs_check - qhimark)
2447 rdp->qlen_last_fqs_check = count;
2450 * The following usually indicates a double call_rcu(). To track
2451 * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.
2453 WARN_ON_ONCE(rcu_segcblist_empty(&rdp->cblist) != (count == 0));
2455 local_irq_restore(flags);
2457 /* Re-invoke RCU core processing if there are callbacks remaining. */
2458 if (rcu_segcblist_ready_cbs(&rdp->cblist))
2463 * Check to see if this CPU is in a non-context-switch quiescent state
2464 * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
2465 * Also schedule RCU core processing.
2467 * This function must be called from hardirq context. It is normally
2468 * invoked from the scheduling-clock interrupt.
2470 void rcu_check_callbacks(int user)
2472 trace_rcu_utilization(TPS("Start scheduler-tick"));
2473 raw_cpu_inc(rcu_data.ticks_this_gp);
2474 rcu_flavor_check_callbacks(user);
2478 trace_rcu_utilization(TPS("End scheduler-tick"));
2482 * Scan the leaf rcu_node structures, processing dyntick state for any that
2483 * have not yet encountered a quiescent state, using the function specified.
2484 * Also initiate boosting for any threads blocked on the root rcu_node.
2486 * The caller must have suppressed start of new grace periods.
2488 static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
2491 unsigned long flags;
2493 struct rcu_node *rnp;
2495 rcu_for_each_leaf_node(rnp) {
2496 cond_resched_tasks_rcu_qs();
2498 raw_spin_lock_irqsave_rcu_node(rnp, flags);
2499 if (rnp->qsmask == 0) {
2500 if (!IS_ENABLED(CONFIG_PREEMPT) ||
2501 rcu_preempt_blocked_readers_cgp(rnp)) {
2503 * No point in scanning bits because they
2504 * are all zero. But we might need to
2505 * priority-boost blocked readers.
2507 rcu_initiate_boost(rnp, flags);
2508 /* rcu_initiate_boost() releases rnp->lock */
2511 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2514 for_each_leaf_node_possible_cpu(rnp, cpu) {
2515 unsigned long bit = leaf_node_cpu_bit(rnp, cpu);
2516 if ((rnp->qsmask & bit) != 0) {
2517 if (f(per_cpu_ptr(&rcu_data, cpu)))
2522 /* Idle/offline CPUs, report (releases rnp->lock). */
2523 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
2525 /* Nothing to do here, so just drop the lock. */
2526 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2532 * Force quiescent states on reluctant CPUs, and also detect which
2533 * CPUs are in dyntick-idle mode.
2535 static void force_quiescent_state(void)
2537 unsigned long flags;
2539 struct rcu_node *rnp;
2540 struct rcu_node *rnp_old = NULL;
2542 /* Funnel through hierarchy to reduce memory contention. */
2543 rnp = __this_cpu_read(rcu_data.mynode);
2544 for (; rnp != NULL; rnp = rnp->parent) {
2545 ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) ||
2546 !raw_spin_trylock(&rnp->fqslock);
2547 if (rnp_old != NULL)
2548 raw_spin_unlock(&rnp_old->fqslock);
2553 /* rnp_old == rcu_get_root(), rnp == NULL. */
2555 /* Reached the root of the rcu_node tree, acquire lock. */
2556 raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
2557 raw_spin_unlock(&rnp_old->fqslock);
2558 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
2559 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
2560 return; /* Someone beat us to it. */
2562 WRITE_ONCE(rcu_state.gp_flags,
2563 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
2564 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
2565 rcu_gp_kthread_wake();
2569 * This function checks for grace-period requests that fail to motivate
2570 * RCU to come out of its idle mode.
2573 rcu_check_gp_start_stall(struct rcu_node *rnp, struct rcu_data *rdp)
2575 const unsigned long gpssdelay = rcu_jiffies_till_stall_check() * HZ;
2576 unsigned long flags;
2578 struct rcu_node *rnp_root = rcu_get_root();
2579 static atomic_t warned = ATOMIC_INIT(0);
2581 if (!IS_ENABLED(CONFIG_PROVE_RCU) || rcu_gp_in_progress() ||
2582 ULONG_CMP_GE(rnp_root->gp_seq, rnp_root->gp_seq_needed))
2584 j = jiffies; /* Expensive access, and in common case don't get here. */
2585 if (time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
2586 time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
2587 atomic_read(&warned))
2590 raw_spin_lock_irqsave_rcu_node(rnp, flags);
2592 if (rcu_gp_in_progress() ||
2593 ULONG_CMP_GE(rnp_root->gp_seq, rnp_root->gp_seq_needed) ||
2594 time_before(j, READ_ONCE(rcu_state.gp_req_activity) + gpssdelay) ||
2595 time_before(j, READ_ONCE(rcu_state.gp_activity) + gpssdelay) ||
2596 atomic_read(&warned)) {
2597 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2600 /* Hold onto the leaf lock to make others see warned==1. */
2602 if (rnp_root != rnp)
2603 raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
2605 if (rcu_gp_in_progress() ||
2606 ULONG_CMP_GE(rnp_root->gp_seq, rnp_root->gp_seq_needed) ||
2607 time_before(j, rcu_state.gp_req_activity + gpssdelay) ||
2608 time_before(j, rcu_state.gp_activity + gpssdelay) ||
2609 atomic_xchg(&warned, 1)) {
2610 raw_spin_unlock_rcu_node(rnp_root); /* irqs remain disabled. */
2611 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2614 pr_alert("%s: g%ld->%ld gar:%lu ga:%lu f%#x gs:%d %s->state:%#lx\n",
2615 __func__, (long)READ_ONCE(rcu_state.gp_seq),
2616 (long)READ_ONCE(rnp_root->gp_seq_needed),
2617 j - rcu_state.gp_req_activity, j - rcu_state.gp_activity,
2618 rcu_state.gp_flags, rcu_state.gp_state, rcu_state.name,
2619 rcu_state.gp_kthread ? rcu_state.gp_kthread->state : 0x1ffffL);
2621 if (rnp_root != rnp)
2622 raw_spin_unlock_rcu_node(rnp_root);
2623 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2627 * This does the RCU core processing work for the specified rcu_data
2628 * structures. This may be called only from the CPU to whom the rdp
2631 static __latent_entropy void rcu_process_callbacks(struct softirq_action *unused)
2633 unsigned long flags;
2634 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
2635 struct rcu_node *rnp = rdp->mynode;
2637 if (cpu_is_offline(smp_processor_id()))
2639 trace_rcu_utilization(TPS("Start RCU core"));
2640 WARN_ON_ONCE(!rdp->beenonline);
2642 /* Report any deferred quiescent states if preemption enabled. */
2643 if (!(preempt_count() & PREEMPT_MASK))
2644 rcu_preempt_deferred_qs(current);
2645 else if (rcu_preempt_need_deferred_qs(current))
2646 resched_cpu(rdp->cpu); /* Provoke future context switch. */
2648 /* Update RCU state based on any recent quiescent states. */
2649 rcu_check_quiescent_state(rdp);
2651 /* No grace period and unregistered callbacks? */
2652 if (!rcu_gp_in_progress() &&
2653 rcu_segcblist_is_enabled(&rdp->cblist)) {
2654 local_irq_save(flags);
2655 if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
2656 rcu_accelerate_cbs_unlocked(rnp, rdp);
2657 local_irq_restore(flags);
2660 rcu_check_gp_start_stall(rnp, rdp);
2662 /* If there are callbacks ready, invoke them. */
2663 if (rcu_segcblist_ready_cbs(&rdp->cblist))
2664 invoke_rcu_callbacks(rdp);
2666 /* Do any needed deferred wakeups of rcuo kthreads. */
2667 do_nocb_deferred_wakeup(rdp);
2668 trace_rcu_utilization(TPS("End RCU core"));
2672 * Schedule RCU callback invocation. If the running implementation of RCU
2673 * does not support RCU priority boosting, just do a direct call, otherwise
2674 * wake up the per-CPU kernel kthread. Note that because we are running
2675 * on the current CPU with softirqs disabled, the rcu_cpu_kthread_task
2676 * cannot disappear out from under us.
2678 static void invoke_rcu_callbacks(struct rcu_data *rdp)
2680 if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
2682 if (likely(!rcu_state.boost)) {
2686 invoke_rcu_callbacks_kthread();
2689 static void invoke_rcu_core(void)
2691 if (cpu_online(smp_processor_id()))
2692 raise_softirq(RCU_SOFTIRQ);
2696 * Handle any core-RCU processing required by a call_rcu() invocation.
2698 static void __call_rcu_core(struct rcu_data *rdp, struct rcu_head *head,
2699 unsigned long flags)
2702 * If called from an extended quiescent state, invoke the RCU
2703 * core in order to force a re-evaluation of RCU's idleness.
2705 if (!rcu_is_watching())
2708 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
2709 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
2713 * Force the grace period if too many callbacks or too long waiting.
2714 * Enforce hysteresis, and don't invoke force_quiescent_state()
2715 * if some other CPU has recently done so. Also, don't bother
2716 * invoking force_quiescent_state() if the newly enqueued callback
2717 * is the only one waiting for a grace period to complete.
2719 if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
2720 rdp->qlen_last_fqs_check + qhimark)) {
2722 /* Are we ignoring a completed grace period? */
2723 note_gp_changes(rdp);
2725 /* Start a new grace period if one not already started. */
2726 if (!rcu_gp_in_progress()) {
2727 rcu_accelerate_cbs_unlocked(rdp->mynode, rdp);
2729 /* Give the grace period a kick. */
2730 rdp->blimit = LONG_MAX;
2731 if (rcu_state.n_force_qs == rdp->n_force_qs_snap &&
2732 rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
2733 force_quiescent_state();
2734 rdp->n_force_qs_snap = rcu_state.n_force_qs;
2735 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
2741 * RCU callback function to leak a callback.
2743 static void rcu_leak_callback(struct rcu_head *rhp)
2748 * Helper function for call_rcu() and friends. The cpu argument will
2749 * normally be -1, indicating "currently running CPU". It may specify
2750 * a CPU only if that CPU is a no-CBs CPU. Currently, only _rcu_barrier()
2751 * is expected to specify a CPU.
2754 __call_rcu(struct rcu_head *head, rcu_callback_t func, int cpu, bool lazy)
2756 unsigned long flags;
2757 struct rcu_data *rdp;
2759 /* Misaligned rcu_head! */
2760 WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
2762 if (debug_rcu_head_queue(head)) {
2764 * Probable double call_rcu(), so leak the callback.
2765 * Use rcu:rcu_callback trace event to find the previous
2766 * time callback was passed to __call_rcu().
2768 WARN_ONCE(1, "__call_rcu(): Double-freed CB %p->%pF()!!!\n",
2770 WRITE_ONCE(head->func, rcu_leak_callback);
2775 local_irq_save(flags);
2776 rdp = this_cpu_ptr(&rcu_data);
2778 /* Add the callback to our list. */
2779 if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist)) || cpu != -1) {
2783 rdp = per_cpu_ptr(&rcu_data, cpu);
2784 if (likely(rdp->mynode)) {
2785 /* Post-boot, so this should be for a no-CBs CPU. */
2786 offline = !__call_rcu_nocb(rdp, head, lazy, flags);
2787 WARN_ON_ONCE(offline);
2788 /* Offline CPU, _call_rcu() illegal, leak callback. */
2789 local_irq_restore(flags);
2793 * Very early boot, before rcu_init(). Initialize if needed
2794 * and then drop through to queue the callback.
2797 WARN_ON_ONCE(!rcu_is_watching());
2798 if (rcu_segcblist_empty(&rdp->cblist))
2799 rcu_segcblist_init(&rdp->cblist);
2801 rcu_segcblist_enqueue(&rdp->cblist, head, lazy);
2803 rcu_idle_count_callbacks_posted();
2805 if (__is_kfree_rcu_offset((unsigned long)func))
2806 trace_rcu_kfree_callback(rcu_state.name, head,
2807 (unsigned long)func,
2808 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2809 rcu_segcblist_n_cbs(&rdp->cblist));
2811 trace_rcu_callback(rcu_state.name, head,
2812 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2813 rcu_segcblist_n_cbs(&rdp->cblist));
2815 /* Go handle any RCU core processing required. */
2816 __call_rcu_core(rdp, head, flags);
2817 local_irq_restore(flags);
2821 * call_rcu() - Queue an RCU callback for invocation after a grace period.
2822 * @head: structure to be used for queueing the RCU updates.
2823 * @func: actual callback function to be invoked after the grace period
2825 * The callback function will be invoked some time after a full grace
2826 * period elapses, in other words after all pre-existing RCU read-side
2827 * critical sections have completed. However, the callback function
2828 * might well execute concurrently with RCU read-side critical sections
2829 * that started after call_rcu() was invoked. RCU read-side critical
2830 * sections are delimited by rcu_read_lock() and rcu_read_unlock(), and
2831 * may be nested. In addition, regions of code across which interrupts,
2832 * preemption, or softirqs have been disabled also serve as RCU read-side
2833 * critical sections. This includes hardware interrupt handlers, softirq
2834 * handlers, and NMI handlers.
2836 * Note that all CPUs must agree that the grace period extended beyond
2837 * all pre-existing RCU read-side critical section. On systems with more
2838 * than one CPU, this means that when "func()" is invoked, each CPU is
2839 * guaranteed to have executed a full memory barrier since the end of its
2840 * last RCU read-side critical section whose beginning preceded the call
2841 * to call_rcu(). It also means that each CPU executing an RCU read-side
2842 * critical section that continues beyond the start of "func()" must have
2843 * executed a memory barrier after the call_rcu() but before the beginning
2844 * of that RCU read-side critical section. Note that these guarantees
2845 * include CPUs that are offline, idle, or executing in user mode, as
2846 * well as CPUs that are executing in the kernel.
2848 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
2849 * resulting RCU callback function "func()", then both CPU A and CPU B are
2850 * guaranteed to execute a full memory barrier during the time interval
2851 * between the call to call_rcu() and the invocation of "func()" -- even
2852 * if CPU A and CPU B are the same CPU (but again only if the system has
2853 * more than one CPU).
2855 void call_rcu(struct rcu_head *head, rcu_callback_t func)
2857 __call_rcu(head, func, -1, 0);
2859 EXPORT_SYMBOL_GPL(call_rcu);
2862 * Queue an RCU callback for lazy invocation after a grace period.
2863 * This will likely be later named something like "call_rcu_lazy()",
2864 * but this change will require some way of tagging the lazy RCU
2865 * callbacks in the list of pending callbacks. Until then, this
2866 * function may only be called from __kfree_rcu().
2868 void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
2870 __call_rcu(head, func, -1, 1);
2872 EXPORT_SYMBOL_GPL(kfree_call_rcu);
2875 * get_state_synchronize_rcu - Snapshot current RCU state
2877 * Returns a cookie that is used by a later call to cond_synchronize_rcu()
2878 * to determine whether or not a full grace period has elapsed in the
2881 unsigned long get_state_synchronize_rcu(void)
2884 * Any prior manipulation of RCU-protected data must happen
2885 * before the load from ->gp_seq.
2888 return rcu_seq_snap(&rcu_state.gp_seq);
2890 EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
2893 * cond_synchronize_rcu - Conditionally wait for an RCU grace period
2895 * @oldstate: return value from earlier call to get_state_synchronize_rcu()
2897 * If a full RCU grace period has elapsed since the earlier call to
2898 * get_state_synchronize_rcu(), just return. Otherwise, invoke
2899 * synchronize_rcu() to wait for a full grace period.
2901 * Yes, this function does not take counter wrap into account. But
2902 * counter wrap is harmless. If the counter wraps, we have waited for
2903 * more than 2 billion grace periods (and way more on a 64-bit system!),
2904 * so waiting for one additional grace period should be just fine.
2906 void cond_synchronize_rcu(unsigned long oldstate)
2908 if (!rcu_seq_done(&rcu_state.gp_seq, oldstate))
2911 smp_mb(); /* Ensure GP ends before subsequent accesses. */
2913 EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
2916 * Check to see if there is any immediate RCU-related work to be done by
2917 * the current CPU, returning 1 if so and zero otherwise. The checks are
2918 * in order of increasing expense: checks that can be carried out against
2919 * CPU-local state are performed first. However, we must check for CPU
2920 * stalls first, else we might not get a chance.
2922 static int rcu_pending(void)
2924 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
2925 struct rcu_node *rnp = rdp->mynode;
2927 /* Check for CPU stalls, if enabled. */
2928 check_cpu_stall(rdp);
2930 /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
2931 if (rcu_nohz_full_cpu())
2934 /* Is the RCU core waiting for a quiescent state from this CPU? */
2935 if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm)
2938 /* Does this CPU have callbacks ready to invoke? */
2939 if (rcu_segcblist_ready_cbs(&rdp->cblist))
2942 /* Has RCU gone idle with this CPU needing another grace period? */
2943 if (!rcu_gp_in_progress() &&
2944 rcu_segcblist_is_enabled(&rdp->cblist) &&
2945 !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
2948 /* Have RCU grace period completed or started? */
2949 if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq ||
2950 unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */
2953 /* Does this CPU need a deferred NOCB wakeup? */
2954 if (rcu_nocb_need_deferred_wakeup(rdp))
2962 * Return true if the specified CPU has any callback. If all_lazy is
2963 * non-NULL, store an indication of whether all callbacks are lazy.
2964 * (If there are no callbacks, all of them are deemed to be lazy.)
2966 static bool rcu_cpu_has_callbacks(bool *all_lazy)
2970 struct rcu_data *rdp;
2972 rdp = this_cpu_ptr(&rcu_data);
2973 if (!rcu_segcblist_empty(&rdp->cblist)) {
2975 if (rcu_segcblist_n_nonlazy_cbs(&rdp->cblist))
2984 * Helper function for _rcu_barrier() tracing. If tracing is disabled,
2985 * the compiler is expected to optimize this away.
2987 static void _rcu_barrier_trace(const char *s, int cpu, unsigned long done)
2989 trace_rcu_barrier(rcu_state.name, s, cpu,
2990 atomic_read(&rcu_state.barrier_cpu_count), done);
2994 * RCU callback function for _rcu_barrier(). If we are last, wake
2995 * up the task executing _rcu_barrier().
2997 static void rcu_barrier_callback(struct rcu_head *rhp)
2999 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) {
3000 _rcu_barrier_trace(TPS("LastCB"), -1,
3001 rcu_state.barrier_sequence);
3002 complete(&rcu_state.barrier_completion);
3004 _rcu_barrier_trace(TPS("CB"), -1, rcu_state.barrier_sequence);
3009 * Called with preemption disabled, and from cross-cpu IRQ context.
3011 static void rcu_barrier_func(void *unused)
3013 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
3015 _rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence);
3016 rdp->barrier_head.func = rcu_barrier_callback;
3017 debug_rcu_head_queue(&rdp->barrier_head);
3018 if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head, 0)) {
3019 atomic_inc(&rcu_state.barrier_cpu_count);
3021 debug_rcu_head_unqueue(&rdp->barrier_head);
3022 _rcu_barrier_trace(TPS("IRQNQ"), -1,
3023 rcu_state.barrier_sequence);
3027 /* Orchestrate an RCU barrier, waiting for all RCU callbacks to complete. */
3028 static void _rcu_barrier(void)
3031 struct rcu_data *rdp;
3032 unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence);
3034 _rcu_barrier_trace(TPS("Begin"), -1, s);
3036 /* Take mutex to serialize concurrent rcu_barrier() requests. */
3037 mutex_lock(&rcu_state.barrier_mutex);
3039 /* Did someone else do our work for us? */
3040 if (rcu_seq_done(&rcu_state.barrier_sequence, s)) {
3041 _rcu_barrier_trace(TPS("EarlyExit"), -1,
3042 rcu_state.barrier_sequence);
3043 smp_mb(); /* caller's subsequent code after above check. */
3044 mutex_unlock(&rcu_state.barrier_mutex);
3048 /* Mark the start of the barrier operation. */
3049 rcu_seq_start(&rcu_state.barrier_sequence);
3050 _rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence);
3053 * Initialize the count to one rather than to zero in order to
3054 * avoid a too-soon return to zero in case of a short grace period
3055 * (or preemption of this task). Exclude CPU-hotplug operations
3056 * to ensure that no offline CPU has callbacks queued.
3058 init_completion(&rcu_state.barrier_completion);
3059 atomic_set(&rcu_state.barrier_cpu_count, 1);
3063 * Force each CPU with callbacks to register a new callback.
3064 * When that callback is invoked, we will know that all of the
3065 * corresponding CPU's preceding callbacks have been invoked.
3067 for_each_possible_cpu(cpu) {
3068 if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
3070 rdp = per_cpu_ptr(&rcu_data, cpu);
3071 if (rcu_is_nocb_cpu(cpu)) {
3072 if (!rcu_nocb_cpu_needs_barrier(cpu)) {
3073 _rcu_barrier_trace(TPS("OfflineNoCB"), cpu,
3074 rcu_state.barrier_sequence);
3076 _rcu_barrier_trace(TPS("OnlineNoCB"), cpu,
3077 rcu_state.barrier_sequence);
3078 smp_mb__before_atomic();
3079 atomic_inc(&rcu_state.barrier_cpu_count);
3080 __call_rcu(&rdp->barrier_head,
3081 rcu_barrier_callback, cpu, 0);
3083 } else if (rcu_segcblist_n_cbs(&rdp->cblist)) {
3084 _rcu_barrier_trace(TPS("OnlineQ"), cpu,
3085 rcu_state.barrier_sequence);
3086 smp_call_function_single(cpu, rcu_barrier_func, NULL, 1);
3088 _rcu_barrier_trace(TPS("OnlineNQ"), cpu,
3089 rcu_state.barrier_sequence);
3095 * Now that we have an rcu_barrier_callback() callback on each
3096 * CPU, and thus each counted, remove the initial count.
3098 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count))
3099 complete(&rcu_state.barrier_completion);
3101 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
3102 wait_for_completion(&rcu_state.barrier_completion);
3104 /* Mark the end of the barrier operation. */
3105 _rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence);
3106 rcu_seq_end(&rcu_state.barrier_sequence);
3108 /* Other rcu_barrier() invocations can now safely proceed. */
3109 mutex_unlock(&rcu_state.barrier_mutex);
3113 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
3115 * Note that this primitive does not necessarily wait for an RCU grace period
3116 * to complete. For example, if there are no RCU callbacks queued anywhere
3117 * in the system, then rcu_barrier() is within its rights to return
3118 * immediately, without waiting for anything, much less an RCU grace period.
3120 void rcu_barrier(void)
3124 EXPORT_SYMBOL_GPL(rcu_barrier);
3127 * Propagate ->qsinitmask bits up the rcu_node tree to account for the
3128 * first CPU in a given leaf rcu_node structure coming online. The caller
3129 * must hold the corresponding leaf rcu_node ->lock with interrrupts
3132 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
3136 struct rcu_node *rnp = rnp_leaf;
3138 raw_lockdep_assert_held_rcu_node(rnp_leaf);
3139 WARN_ON_ONCE(rnp->wait_blkd_tasks);
3141 mask = rnp->grpmask;
3145 raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
3146 oldmask = rnp->qsmaskinit;
3147 rnp->qsmaskinit |= mask;
3148 raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
3155 * Do boot-time initialization of a CPU's per-CPU RCU data.
3158 rcu_boot_init_percpu_data(int cpu)
3160 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3162 /* Set up local state, ensuring consistent view of global state. */
3163 rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
3164 rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
3165 WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != 1);
3166 WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp->dynticks)));
3167 rdp->rcu_ofl_gp_seq = rcu_state.gp_seq;
3168 rdp->rcu_ofl_gp_flags = RCU_GP_CLEANED;
3169 rdp->rcu_onl_gp_seq = rcu_state.gp_seq;
3170 rdp->rcu_onl_gp_flags = RCU_GP_CLEANED;
3172 rcu_boot_init_nocb_percpu_data(rdp);
3176 * Invoked early in the CPU-online process, when pretty much all services
3177 * are available. The incoming CPU is not present.
3179 * Initializes a CPU's per-CPU RCU data. Note that only one online or
3180 * offline event can be happening at a given time. Note also that we can
3181 * accept some slop in the rsp->gp_seq access due to the fact that this
3182 * CPU cannot possibly have any RCU callbacks in flight yet.
3184 int rcutree_prepare_cpu(unsigned int cpu)
3186 unsigned long flags;
3187 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3188 struct rcu_node *rnp = rcu_get_root();
3190 /* Set up local state, ensuring consistent view of global state. */
3191 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3192 rdp->qlen_last_fqs_check = 0;
3193 rdp->n_force_qs_snap = rcu_state.n_force_qs;
3194 rdp->blimit = blimit;
3195 if (rcu_segcblist_empty(&rdp->cblist) && /* No early-boot CBs? */
3196 !init_nocb_callback_list(rdp))
3197 rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */
3198 rdp->dynticks->dynticks_nesting = 1; /* CPU not up, no tearing. */
3199 rcu_dynticks_eqs_online();
3200 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
3203 * Add CPU to leaf rcu_node pending-online bitmask. Any needed
3204 * propagation up the rcu_node tree will happen at the beginning
3205 * of the next grace period.
3208 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
3209 rdp->beenonline = true; /* We have now been online. */
3210 rdp->gp_seq = rnp->gp_seq;
3211 rdp->gp_seq_needed = rnp->gp_seq;
3212 rdp->cpu_no_qs.b.norm = true;
3213 rdp->rcu_qs_ctr_snap = per_cpu(rcu_dynticks.rcu_qs_ctr, cpu);
3214 rdp->core_needs_qs = false;
3215 rdp->rcu_iw_pending = false;
3216 rdp->rcu_iw_gp_seq = rnp->gp_seq - 1;
3217 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl"));
3218 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3219 rcu_prepare_kthreads(cpu);
3220 rcu_spawn_all_nocb_kthreads(cpu);
3226 * Update RCU priority boot kthread affinity for CPU-hotplug changes.
3228 static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
3230 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3232 rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
3236 * Near the end of the CPU-online process. Pretty much all services
3237 * enabled, and the CPU is now very much alive.
3239 int rcutree_online_cpu(unsigned int cpu)
3241 unsigned long flags;
3242 struct rcu_data *rdp;
3243 struct rcu_node *rnp;
3245 rdp = per_cpu_ptr(&rcu_data, cpu);
3247 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3248 rnp->ffmask |= rdp->grpmask;
3249 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3250 if (IS_ENABLED(CONFIG_TREE_SRCU))
3251 srcu_online_cpu(cpu);
3252 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
3253 return 0; /* Too early in boot for scheduler work. */
3254 sync_sched_exp_online_cleanup(cpu);
3255 rcutree_affinity_setting(cpu, -1);
3260 * Near the beginning of the process. The CPU is still very much alive
3261 * with pretty much all services enabled.
3263 int rcutree_offline_cpu(unsigned int cpu)
3265 unsigned long flags;
3266 struct rcu_data *rdp;
3267 struct rcu_node *rnp;
3269 rdp = per_cpu_ptr(&rcu_data, cpu);
3271 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3272 rnp->ffmask &= ~rdp->grpmask;
3273 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3275 rcutree_affinity_setting(cpu, cpu);
3276 if (IS_ENABLED(CONFIG_TREE_SRCU))
3277 srcu_offline_cpu(cpu);
3281 static DEFINE_PER_CPU(int, rcu_cpu_started);
3284 * Mark the specified CPU as being online so that subsequent grace periods
3285 * (both expedited and normal) will wait on it. Note that this means that
3286 * incoming CPUs are not allowed to use RCU read-side critical sections
3287 * until this function is called. Failing to observe this restriction
3288 * will result in lockdep splats.
3290 * Note that this function is special in that it is invoked directly
3291 * from the incoming CPU rather than from the cpuhp_step mechanism.
3292 * This is because this function must be invoked at a precise location.
3294 void rcu_cpu_starting(unsigned int cpu)
3296 unsigned long flags;
3299 unsigned long oldmask;
3300 struct rcu_data *rdp;
3301 struct rcu_node *rnp;
3303 if (per_cpu(rcu_cpu_started, cpu))
3306 per_cpu(rcu_cpu_started, cpu) = 1;
3308 rdp = per_cpu_ptr(&rcu_data, cpu);
3310 mask = rdp->grpmask;
3311 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3312 rnp->qsmaskinitnext |= mask;
3313 oldmask = rnp->expmaskinitnext;
3314 rnp->expmaskinitnext |= mask;
3315 oldmask ^= rnp->expmaskinitnext;
3316 nbits = bitmap_weight(&oldmask, BITS_PER_LONG);
3317 /* Allow lockless access for expedited grace periods. */
3318 smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + nbits); /* ^^^ */
3319 rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */
3320 rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3321 rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags);
3322 if (rnp->qsmask & mask) { /* RCU waiting on incoming CPU? */
3323 /* Report QS -after- changing ->qsmaskinitnext! */
3324 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3326 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3328 smp_mb(); /* Ensure RCU read-side usage follows above initialization. */
3331 #ifdef CONFIG_HOTPLUG_CPU
3333 * The outgoing function has no further need of RCU, so remove it from
3334 * the rcu_node tree's ->qsmaskinitnext bit masks.
3336 * Note that this function is special in that it is invoked directly
3337 * from the outgoing CPU rather than from the cpuhp_step mechanism.
3338 * This is because this function must be invoked at a precise location.
3340 void rcu_report_dead(unsigned int cpu)
3342 unsigned long flags;
3344 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3345 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
3347 /* QS for any half-done expedited grace period. */
3349 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
3351 rcu_preempt_deferred_qs(current);
3353 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
3354 mask = rdp->grpmask;
3355 spin_lock(&rcu_state.ofl_lock);
3356 raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
3357 rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3358 rdp->rcu_ofl_gp_flags = READ_ONCE(rcu_state.gp_flags);
3359 if (rnp->qsmask & mask) { /* RCU waiting on outgoing CPU? */
3360 /* Report quiescent state -before- changing ->qsmaskinitnext! */
3361 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3362 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3364 rnp->qsmaskinitnext &= ~mask;
3365 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3366 spin_unlock(&rcu_state.ofl_lock);
3368 per_cpu(rcu_cpu_started, cpu) = 0;
3372 * The outgoing CPU has just passed through the dying-idle state, and we
3373 * are being invoked from the CPU that was IPIed to continue the offline
3374 * operation. Migrate the outgoing CPU's callbacks to the current CPU.
3376 void rcutree_migrate_callbacks(int cpu)
3378 unsigned long flags;
3379 struct rcu_data *my_rdp;
3380 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3381 struct rcu_node *rnp_root = rcu_get_root();
3384 if (rcu_is_nocb_cpu(cpu) || rcu_segcblist_empty(&rdp->cblist))
3385 return; /* No callbacks to migrate. */
3387 local_irq_save(flags);
3388 my_rdp = this_cpu_ptr(&rcu_data);
3389 if (rcu_nocb_adopt_orphan_cbs(my_rdp, rdp, flags)) {
3390 local_irq_restore(flags);
3393 raw_spin_lock_rcu_node(rnp_root); /* irqs already disabled. */
3394 /* Leverage recent GPs and set GP for new callbacks. */
3395 needwake = rcu_advance_cbs(rnp_root, rdp) ||
3396 rcu_advance_cbs(rnp_root, my_rdp);
3397 rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
3398 WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) !=
3399 !rcu_segcblist_n_cbs(&my_rdp->cblist));
3400 raw_spin_unlock_irqrestore_rcu_node(rnp_root, flags);
3402 rcu_gp_kthread_wake();
3403 WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
3404 !rcu_segcblist_empty(&rdp->cblist),
3405 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
3406 cpu, rcu_segcblist_n_cbs(&rdp->cblist),
3407 rcu_segcblist_first_cb(&rdp->cblist));
3412 * On non-huge systems, use expedited RCU grace periods to make suspend
3413 * and hibernation run faster.
3415 static int rcu_pm_notify(struct notifier_block *self,
3416 unsigned long action, void *hcpu)
3419 case PM_HIBERNATION_PREPARE:
3420 case PM_SUSPEND_PREPARE:
3421 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
3424 case PM_POST_HIBERNATION:
3425 case PM_POST_SUSPEND:
3426 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
3427 rcu_unexpedite_gp();
3436 * Spawn the kthreads that handle RCU's grace periods.
3438 static int __init rcu_spawn_gp_kthread(void)
3440 unsigned long flags;
3441 int kthread_prio_in = kthread_prio;
3442 struct rcu_node *rnp;
3443 struct sched_param sp;
3444 struct task_struct *t;
3446 /* Force priority into range. */
3447 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2
3448 && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST))
3450 else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
3452 else if (kthread_prio < 0)
3454 else if (kthread_prio > 99)
3457 if (kthread_prio != kthread_prio_in)
3458 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
3459 kthread_prio, kthread_prio_in);
3461 rcu_scheduler_fully_active = 1;
3462 t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name);
3464 rnp = rcu_get_root();
3465 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3466 rcu_state.gp_kthread = t;
3468 sp.sched_priority = kthread_prio;
3469 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
3471 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3473 rcu_spawn_nocb_kthreads();
3474 rcu_spawn_boost_kthreads();
3477 early_initcall(rcu_spawn_gp_kthread);
3480 * This function is invoked towards the end of the scheduler's
3481 * initialization process. Before this is called, the idle task might
3482 * contain synchronous grace-period primitives (during which time, this idle
3483 * task is booting the system, and such primitives are no-ops). After this
3484 * function is called, any synchronous grace-period primitives are run as
3485 * expedited, with the requesting task driving the grace period forward.
3486 * A later core_initcall() rcu_set_runtime_mode() will switch to full
3487 * runtime RCU functionality.
3489 void rcu_scheduler_starting(void)
3491 WARN_ON(num_online_cpus() != 1);
3492 WARN_ON(nr_context_switches() > 0);
3493 rcu_test_sync_prims();
3494 rcu_scheduler_active = RCU_SCHEDULER_INIT;
3495 rcu_test_sync_prims();
3499 * Helper function for rcu_init() that initializes the rcu_state structure.
3501 static void __init rcu_init_one(void)
3503 static const char * const buf[] = RCU_NODE_NAME_INIT;
3504 static const char * const fqs[] = RCU_FQS_NAME_INIT;
3505 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
3506 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
3508 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
3512 struct rcu_node *rnp;
3514 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
3516 /* Silence gcc 4.8 false positive about array index out of range. */
3517 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
3518 panic("rcu_init_one: rcu_num_lvls out of range");
3520 /* Initialize the level-tracking arrays. */
3522 for (i = 1; i < rcu_num_lvls; i++)
3523 rcu_state.level[i] =
3524 rcu_state.level[i - 1] + num_rcu_lvl[i - 1];
3525 rcu_init_levelspread(levelspread, num_rcu_lvl);
3527 /* Initialize the elements themselves, starting from the leaves. */
3529 for (i = rcu_num_lvls - 1; i >= 0; i--) {
3530 cpustride *= levelspread[i];
3531 rnp = rcu_state.level[i];
3532 for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
3533 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
3534 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
3535 &rcu_node_class[i], buf[i]);
3536 raw_spin_lock_init(&rnp->fqslock);
3537 lockdep_set_class_and_name(&rnp->fqslock,
3538 &rcu_fqs_class[i], fqs[i]);
3539 rnp->gp_seq = rcu_state.gp_seq;
3540 rnp->gp_seq_needed = rcu_state.gp_seq;
3541 rnp->completedqs = rcu_state.gp_seq;
3543 rnp->qsmaskinit = 0;
3544 rnp->grplo = j * cpustride;
3545 rnp->grphi = (j + 1) * cpustride - 1;
3546 if (rnp->grphi >= nr_cpu_ids)
3547 rnp->grphi = nr_cpu_ids - 1;
3553 rnp->grpnum = j % levelspread[i - 1];
3554 rnp->grpmask = 1UL << rnp->grpnum;
3555 rnp->parent = rcu_state.level[i - 1] +
3556 j / levelspread[i - 1];
3559 INIT_LIST_HEAD(&rnp->blkd_tasks);
3560 rcu_init_one_nocb(rnp);
3561 init_waitqueue_head(&rnp->exp_wq[0]);
3562 init_waitqueue_head(&rnp->exp_wq[1]);
3563 init_waitqueue_head(&rnp->exp_wq[2]);
3564 init_waitqueue_head(&rnp->exp_wq[3]);
3565 spin_lock_init(&rnp->exp_lock);
3569 init_swait_queue_head(&rcu_state.gp_wq);
3570 init_swait_queue_head(&rcu_state.expedited_wq);
3571 rnp = rcu_first_leaf_node();
3572 for_each_possible_cpu(i) {
3573 while (i > rnp->grphi)
3575 per_cpu_ptr(&rcu_data, i)->mynode = rnp;
3576 rcu_boot_init_percpu_data(i);
3581 * Compute the rcu_node tree geometry from kernel parameters. This cannot
3582 * replace the definitions in tree.h because those are needed to size
3583 * the ->node array in the rcu_state structure.
3585 static void __init rcu_init_geometry(void)
3589 int rcu_capacity[RCU_NUM_LVLS];
3592 * Initialize any unspecified boot parameters.
3593 * The default values of jiffies_till_first_fqs and
3594 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
3595 * value, which is a function of HZ, then adding one for each
3596 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
3598 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
3599 if (jiffies_till_first_fqs == ULONG_MAX)
3600 jiffies_till_first_fqs = d;
3601 if (jiffies_till_next_fqs == ULONG_MAX)
3602 jiffies_till_next_fqs = d;
3604 /* If the compile-time values are accurate, just leave. */
3605 if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
3606 nr_cpu_ids == NR_CPUS)
3608 pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
3609 rcu_fanout_leaf, nr_cpu_ids);
3612 * The boot-time rcu_fanout_leaf parameter must be at least two
3613 * and cannot exceed the number of bits in the rcu_node masks.
3614 * Complain and fall back to the compile-time values if this
3615 * limit is exceeded.
3617 if (rcu_fanout_leaf < 2 ||
3618 rcu_fanout_leaf > sizeof(unsigned long) * 8) {
3619 rcu_fanout_leaf = RCU_FANOUT_LEAF;
3625 * Compute number of nodes that can be handled an rcu_node tree
3626 * with the given number of levels.
3628 rcu_capacity[0] = rcu_fanout_leaf;
3629 for (i = 1; i < RCU_NUM_LVLS; i++)
3630 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
3633 * The tree must be able to accommodate the configured number of CPUs.
3634 * If this limit is exceeded, fall back to the compile-time values.
3636 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
3637 rcu_fanout_leaf = RCU_FANOUT_LEAF;
3642 /* Calculate the number of levels in the tree. */
3643 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
3645 rcu_num_lvls = i + 1;
3647 /* Calculate the number of rcu_nodes at each level of the tree. */
3648 for (i = 0; i < rcu_num_lvls; i++) {
3649 int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
3650 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
3653 /* Calculate the total number of rcu_node structures. */
3655 for (i = 0; i < rcu_num_lvls; i++)
3656 rcu_num_nodes += num_rcu_lvl[i];
3660 * Dump out the structure of the rcu_node combining tree associated
3661 * with the rcu_state structure.
3663 static void __init rcu_dump_rcu_node_tree(void)
3666 struct rcu_node *rnp;
3668 pr_info("rcu_node tree layout dump\n");
3670 rcu_for_each_node_breadth_first(rnp) {
3671 if (rnp->level != level) {
3676 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
3681 struct workqueue_struct *rcu_gp_wq;
3682 struct workqueue_struct *rcu_par_gp_wq;
3684 void __init rcu_init(void)
3688 rcu_early_boot_tests();
3690 rcu_bootup_announce();
3691 rcu_init_geometry();
3694 rcu_dump_rcu_node_tree();
3695 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
3698 * We don't need protection against CPU-hotplug here because
3699 * this is called early in boot, before either interrupts
3700 * or the scheduler are operational.
3702 pm_notifier(rcu_pm_notify, 0);
3703 for_each_online_cpu(cpu) {
3704 rcutree_prepare_cpu(cpu);
3705 rcu_cpu_starting(cpu);
3706 rcutree_online_cpu(cpu);
3709 /* Create workqueue for expedited GPs and for Tree SRCU. */
3710 rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
3711 WARN_ON(!rcu_gp_wq);
3712 rcu_par_gp_wq = alloc_workqueue("rcu_par_gp", WQ_MEM_RECLAIM, 0);
3713 WARN_ON(!rcu_par_gp_wq);
3716 #include "tree_exp.h"
3717 #include "tree_plugin.h"