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