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