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