xref: /linux/kernel/rcu/tree_plugin.h (revision 142d106d5e62ff2cf0dfd2dfe1adfcaff1c2ed85)
1 /*
2  * Read-Copy Update mechanism for mutual exclusion (tree-based version)
3  * Internal non-public definitions that provide either classic
4  * or preemptible semantics.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you can access it online at
18  * http://www.gnu.org/licenses/gpl-2.0.html.
19  *
20  * Copyright Red Hat, 2009
21  * Copyright IBM Corporation, 2009
22  *
23  * Author: Ingo Molnar <mingo@elte.hu>
24  *	   Paul E. McKenney <paulmck@linux.vnet.ibm.com>
25  */
26 
27 #include <linux/delay.h>
28 #include <linux/gfp.h>
29 #include <linux/oom.h>
30 #include <linux/sched/debug.h>
31 #include <linux/smpboot.h>
32 #include <linux/sched/isolation.h>
33 #include <uapi/linux/sched/types.h>
34 #include "../time/tick-internal.h"
35 
36 #ifdef CONFIG_RCU_BOOST
37 
38 #include "../locking/rtmutex_common.h"
39 
40 /*
41  * Control variables for per-CPU and per-rcu_node kthreads.
42  */
43 static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
44 DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
45 DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
46 DEFINE_PER_CPU(char, rcu_cpu_has_work);
47 
48 #else /* #ifdef CONFIG_RCU_BOOST */
49 
50 /*
51  * Some architectures do not define rt_mutexes, but if !CONFIG_RCU_BOOST,
52  * all uses are in dead code.  Provide a definition to keep the compiler
53  * happy, but add WARN_ON_ONCE() to complain if used in the wrong place.
54  * This probably needs to be excluded from -rt builds.
55  */
56 #define rt_mutex_owner(a) ({ WARN_ON_ONCE(1); NULL; })
57 #define rt_mutex_futex_unlock(x) WARN_ON_ONCE(1)
58 
59 #endif /* #else #ifdef CONFIG_RCU_BOOST */
60 
61 #ifdef CONFIG_RCU_NOCB_CPU
62 static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
63 static bool __read_mostly rcu_nocb_poll;    /* Offload kthread are to poll. */
64 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
65 
66 /*
67  * Check the RCU kernel configuration parameters and print informative
68  * messages about anything out of the ordinary.
69  */
70 static void __init rcu_bootup_announce_oddness(void)
71 {
72 	if (IS_ENABLED(CONFIG_RCU_TRACE))
73 		pr_info("\tRCU event tracing is enabled.\n");
74 	if ((IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 64) ||
75 	    (!IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 32))
76 		pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d.\n",
77 			RCU_FANOUT);
78 	if (rcu_fanout_exact)
79 		pr_info("\tHierarchical RCU autobalancing is disabled.\n");
80 	if (IS_ENABLED(CONFIG_RCU_FAST_NO_HZ))
81 		pr_info("\tRCU dyntick-idle grace-period acceleration is enabled.\n");
82 	if (IS_ENABLED(CONFIG_PROVE_RCU))
83 		pr_info("\tRCU lockdep checking is enabled.\n");
84 	if (RCU_NUM_LVLS >= 4)
85 		pr_info("\tFour(or more)-level hierarchy is enabled.\n");
86 	if (RCU_FANOUT_LEAF != 16)
87 		pr_info("\tBuild-time adjustment of leaf fanout to %d.\n",
88 			RCU_FANOUT_LEAF);
89 	if (rcu_fanout_leaf != RCU_FANOUT_LEAF)
90 		pr_info("\tBoot-time adjustment of leaf fanout to %d.\n",
91 			rcu_fanout_leaf);
92 	if (nr_cpu_ids != NR_CPUS)
93 		pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%u.\n", NR_CPUS, nr_cpu_ids);
94 #ifdef CONFIG_RCU_BOOST
95 	pr_info("\tRCU priority boosting: priority %d delay %d ms.\n",
96 		kthread_prio, CONFIG_RCU_BOOST_DELAY);
97 #endif
98 	if (blimit != DEFAULT_RCU_BLIMIT)
99 		pr_info("\tBoot-time adjustment of callback invocation limit to %ld.\n", blimit);
100 	if (qhimark != DEFAULT_RCU_QHIMARK)
101 		pr_info("\tBoot-time adjustment of callback high-water mark to %ld.\n", qhimark);
102 	if (qlowmark != DEFAULT_RCU_QLOMARK)
103 		pr_info("\tBoot-time adjustment of callback low-water mark to %ld.\n", qlowmark);
104 	if (jiffies_till_first_fqs != ULONG_MAX)
105 		pr_info("\tBoot-time adjustment of first FQS scan delay to %ld jiffies.\n", jiffies_till_first_fqs);
106 	if (jiffies_till_next_fqs != ULONG_MAX)
107 		pr_info("\tBoot-time adjustment of subsequent FQS scan delay to %ld jiffies.\n", jiffies_till_next_fqs);
108 	if (jiffies_till_sched_qs != ULONG_MAX)
109 		pr_info("\tBoot-time adjustment of scheduler-enlistment delay to %ld jiffies.\n", jiffies_till_sched_qs);
110 	if (rcu_kick_kthreads)
111 		pr_info("\tKick kthreads if too-long grace period.\n");
112 	if (IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD))
113 		pr_info("\tRCU callback double-/use-after-free debug enabled.\n");
114 	if (gp_preinit_delay)
115 		pr_info("\tRCU debug GP pre-init slowdown %d jiffies.\n", gp_preinit_delay);
116 	if (gp_init_delay)
117 		pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_init_delay);
118 	if (gp_cleanup_delay)
119 		pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_cleanup_delay);
120 	if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG))
121 		pr_info("\tRCU debug extended QS entry/exit.\n");
122 	rcupdate_announce_bootup_oddness();
123 }
124 
125 #ifdef CONFIG_PREEMPT_RCU
126 
127 static void rcu_report_exp_rnp(struct rcu_node *rnp, bool wake);
128 static void rcu_read_unlock_special(struct task_struct *t);
129 
130 /*
131  * Tell them what RCU they are running.
132  */
133 static void __init rcu_bootup_announce(void)
134 {
135 	pr_info("Preemptible hierarchical RCU implementation.\n");
136 	rcu_bootup_announce_oddness();
137 }
138 
139 /* Flags for rcu_preempt_ctxt_queue() decision table. */
140 #define RCU_GP_TASKS	0x8
141 #define RCU_EXP_TASKS	0x4
142 #define RCU_GP_BLKD	0x2
143 #define RCU_EXP_BLKD	0x1
144 
145 /*
146  * Queues a task preempted within an RCU-preempt read-side critical
147  * section into the appropriate location within the ->blkd_tasks list,
148  * depending on the states of any ongoing normal and expedited grace
149  * periods.  The ->gp_tasks pointer indicates which element the normal
150  * grace period is waiting on (NULL if none), and the ->exp_tasks pointer
151  * indicates which element the expedited grace period is waiting on (again,
152  * NULL if none).  If a grace period is waiting on a given element in the
153  * ->blkd_tasks list, it also waits on all subsequent elements.  Thus,
154  * adding a task to the tail of the list blocks any grace period that is
155  * already waiting on one of the elements.  In contrast, adding a task
156  * to the head of the list won't block any grace period that is already
157  * waiting on one of the elements.
158  *
159  * This queuing is imprecise, and can sometimes make an ongoing grace
160  * period wait for a task that is not strictly speaking blocking it.
161  * Given the choice, we needlessly block a normal grace period rather than
162  * blocking an expedited grace period.
163  *
164  * Note that an endless sequence of expedited grace periods still cannot
165  * indefinitely postpone a normal grace period.  Eventually, all of the
166  * fixed number of preempted tasks blocking the normal grace period that are
167  * not also blocking the expedited grace period will resume and complete
168  * their RCU read-side critical sections.  At that point, the ->gp_tasks
169  * pointer will equal the ->exp_tasks pointer, at which point the end of
170  * the corresponding expedited grace period will also be the end of the
171  * normal grace period.
172  */
173 static void rcu_preempt_ctxt_queue(struct rcu_node *rnp, struct rcu_data *rdp)
174 	__releases(rnp->lock) /* But leaves rrupts disabled. */
175 {
176 	int blkd_state = (rnp->gp_tasks ? RCU_GP_TASKS : 0) +
177 			 (rnp->exp_tasks ? RCU_EXP_TASKS : 0) +
178 			 (rnp->qsmask & rdp->grpmask ? RCU_GP_BLKD : 0) +
179 			 (rnp->expmask & rdp->grpmask ? RCU_EXP_BLKD : 0);
180 	struct task_struct *t = current;
181 
182 	raw_lockdep_assert_held_rcu_node(rnp);
183 	WARN_ON_ONCE(rdp->mynode != rnp);
184 	WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
185 	/* RCU better not be waiting on newly onlined CPUs! */
186 	WARN_ON_ONCE(rnp->qsmaskinitnext & ~rnp->qsmaskinit & rnp->qsmask &
187 		     rdp->grpmask);
188 
189 	/*
190 	 * Decide where to queue the newly blocked task.  In theory,
191 	 * this could be an if-statement.  In practice, when I tried
192 	 * that, it was quite messy.
193 	 */
194 	switch (blkd_state) {
195 	case 0:
196 	case                RCU_EXP_TASKS:
197 	case                RCU_EXP_TASKS + RCU_GP_BLKD:
198 	case RCU_GP_TASKS:
199 	case RCU_GP_TASKS + RCU_EXP_TASKS:
200 
201 		/*
202 		 * Blocking neither GP, or first task blocking the normal
203 		 * GP but not blocking the already-waiting expedited GP.
204 		 * Queue at the head of the list to avoid unnecessarily
205 		 * blocking the already-waiting GPs.
206 		 */
207 		list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
208 		break;
209 
210 	case                                              RCU_EXP_BLKD:
211 	case                                RCU_GP_BLKD:
212 	case                                RCU_GP_BLKD + RCU_EXP_BLKD:
213 	case RCU_GP_TASKS +                               RCU_EXP_BLKD:
214 	case RCU_GP_TASKS +                 RCU_GP_BLKD + RCU_EXP_BLKD:
215 	case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
216 
217 		/*
218 		 * First task arriving that blocks either GP, or first task
219 		 * arriving that blocks the expedited GP (with the normal
220 		 * GP already waiting), or a task arriving that blocks
221 		 * both GPs with both GPs already waiting.  Queue at the
222 		 * tail of the list to avoid any GP waiting on any of the
223 		 * already queued tasks that are not blocking it.
224 		 */
225 		list_add_tail(&t->rcu_node_entry, &rnp->blkd_tasks);
226 		break;
227 
228 	case                RCU_EXP_TASKS +               RCU_EXP_BLKD:
229 	case                RCU_EXP_TASKS + RCU_GP_BLKD + RCU_EXP_BLKD:
230 	case RCU_GP_TASKS + RCU_EXP_TASKS +               RCU_EXP_BLKD:
231 
232 		/*
233 		 * Second or subsequent task blocking the expedited GP.
234 		 * The task either does not block the normal GP, or is the
235 		 * first task blocking the normal GP.  Queue just after
236 		 * the first task blocking the expedited GP.
237 		 */
238 		list_add(&t->rcu_node_entry, rnp->exp_tasks);
239 		break;
240 
241 	case RCU_GP_TASKS +                 RCU_GP_BLKD:
242 	case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD:
243 
244 		/*
245 		 * Second or subsequent task blocking the normal GP.
246 		 * The task does not block the expedited GP. Queue just
247 		 * after the first task blocking the normal GP.
248 		 */
249 		list_add(&t->rcu_node_entry, rnp->gp_tasks);
250 		break;
251 
252 	default:
253 
254 		/* Yet another exercise in excessive paranoia. */
255 		WARN_ON_ONCE(1);
256 		break;
257 	}
258 
259 	/*
260 	 * We have now queued the task.  If it was the first one to
261 	 * block either grace period, update the ->gp_tasks and/or
262 	 * ->exp_tasks pointers, respectively, to reference the newly
263 	 * blocked tasks.
264 	 */
265 	if (!rnp->gp_tasks && (blkd_state & RCU_GP_BLKD)) {
266 		rnp->gp_tasks = &t->rcu_node_entry;
267 		WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq);
268 	}
269 	if (!rnp->exp_tasks && (blkd_state & RCU_EXP_BLKD))
270 		rnp->exp_tasks = &t->rcu_node_entry;
271 	WARN_ON_ONCE(!(blkd_state & RCU_GP_BLKD) !=
272 		     !(rnp->qsmask & rdp->grpmask));
273 	WARN_ON_ONCE(!(blkd_state & RCU_EXP_BLKD) !=
274 		     !(rnp->expmask & rdp->grpmask));
275 	raw_spin_unlock_rcu_node(rnp); /* interrupts remain disabled. */
276 
277 	/*
278 	 * Report the quiescent state for the expedited GP.  This expedited
279 	 * GP should not be able to end until we report, so there should be
280 	 * no need to check for a subsequent expedited GP.  (Though we are
281 	 * still in a quiescent state in any case.)
282 	 */
283 	if (blkd_state & RCU_EXP_BLKD && rdp->deferred_qs)
284 		rcu_report_exp_rdp(rdp);
285 	else
286 		WARN_ON_ONCE(rdp->deferred_qs);
287 }
288 
289 /*
290  * Record a preemptible-RCU quiescent state for the specified CPU.
291  * Note that this does not necessarily mean that the task currently running
292  * on the CPU is in a quiescent state:  Instead, it means that the current
293  * grace period need not wait on any RCU read-side critical section that
294  * starts later on this CPU.  It also means that if the current task is
295  * in an RCU read-side critical section, it has already added itself to
296  * some leaf rcu_node structure's ->blkd_tasks list.  In addition to the
297  * current task, there might be any number of other tasks blocked while
298  * in an RCU read-side critical section.
299  *
300  * Callers to this function must disable preemption.
301  */
302 static void rcu_qs(void)
303 {
304 	RCU_LOCKDEP_WARN(preemptible(), "rcu_qs() invoked with preemption enabled!!!\n");
305 	if (__this_cpu_read(rcu_data.cpu_no_qs.s)) {
306 		trace_rcu_grace_period(TPS("rcu_preempt"),
307 				       __this_cpu_read(rcu_data.gp_seq),
308 				       TPS("cpuqs"));
309 		__this_cpu_write(rcu_data.cpu_no_qs.b.norm, false);
310 		barrier(); /* Coordinate with rcu_flavor_check_callbacks(). */
311 		current->rcu_read_unlock_special.b.need_qs = false;
312 	}
313 }
314 
315 /*
316  * We have entered the scheduler, and the current task might soon be
317  * context-switched away from.  If this task is in an RCU read-side
318  * critical section, we will no longer be able to rely on the CPU to
319  * record that fact, so we enqueue the task on the blkd_tasks list.
320  * The task will dequeue itself when it exits the outermost enclosing
321  * RCU read-side critical section.  Therefore, the current grace period
322  * cannot be permitted to complete until the blkd_tasks list entries
323  * predating the current grace period drain, in other words, until
324  * rnp->gp_tasks becomes NULL.
325  *
326  * Caller must disable interrupts.
327  */
328 void rcu_note_context_switch(bool preempt)
329 {
330 	struct task_struct *t = current;
331 	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
332 	struct rcu_node *rnp;
333 
334 	barrier(); /* Avoid RCU read-side critical sections leaking down. */
335 	trace_rcu_utilization(TPS("Start context switch"));
336 	lockdep_assert_irqs_disabled();
337 	WARN_ON_ONCE(!preempt && t->rcu_read_lock_nesting > 0);
338 	if (t->rcu_read_lock_nesting > 0 &&
339 	    !t->rcu_read_unlock_special.b.blocked) {
340 
341 		/* Possibly blocking in an RCU read-side critical section. */
342 		rnp = rdp->mynode;
343 		raw_spin_lock_rcu_node(rnp);
344 		t->rcu_read_unlock_special.b.blocked = true;
345 		t->rcu_blocked_node = rnp;
346 
347 		/*
348 		 * Verify the CPU's sanity, trace the preemption, and
349 		 * then queue the task as required based on the states
350 		 * of any ongoing and expedited grace periods.
351 		 */
352 		WARN_ON_ONCE((rdp->grpmask & rcu_rnp_online_cpus(rnp)) == 0);
353 		WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
354 		trace_rcu_preempt_task(rcu_state.name,
355 				       t->pid,
356 				       (rnp->qsmask & rdp->grpmask)
357 				       ? rnp->gp_seq
358 				       : rcu_seq_snap(&rnp->gp_seq));
359 		rcu_preempt_ctxt_queue(rnp, rdp);
360 	} else if (t->rcu_read_lock_nesting < 0 &&
361 		   t->rcu_read_unlock_special.s) {
362 
363 		/*
364 		 * Complete exit from RCU read-side critical section on
365 		 * behalf of preempted instance of __rcu_read_unlock().
366 		 */
367 		rcu_read_unlock_special(t);
368 		rcu_preempt_deferred_qs(t);
369 	} else {
370 		rcu_preempt_deferred_qs(t);
371 	}
372 
373 	/*
374 	 * Either we were not in an RCU read-side critical section to
375 	 * begin with, or we have now recorded that critical section
376 	 * globally.  Either way, we can now note a quiescent state
377 	 * for this CPU.  Again, if we were in an RCU read-side critical
378 	 * section, and if that critical section was blocking the current
379 	 * grace period, then the fact that the task has been enqueued
380 	 * means that we continue to block the current grace period.
381 	 */
382 	rcu_qs();
383 	if (rdp->deferred_qs)
384 		rcu_report_exp_rdp(rdp);
385 	trace_rcu_utilization(TPS("End context switch"));
386 	barrier(); /* Avoid RCU read-side critical sections leaking up. */
387 }
388 EXPORT_SYMBOL_GPL(rcu_note_context_switch);
389 
390 /*
391  * Check for preempted RCU readers blocking the current grace period
392  * for the specified rcu_node structure.  If the caller needs a reliable
393  * answer, it must hold the rcu_node's ->lock.
394  */
395 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
396 {
397 	return rnp->gp_tasks != NULL;
398 }
399 
400 /* Bias and limit values for ->rcu_read_lock_nesting. */
401 #define RCU_NEST_BIAS INT_MAX
402 #define RCU_NEST_NMAX (-INT_MAX / 2)
403 #define RCU_NEST_PMAX (INT_MAX / 2)
404 
405 /*
406  * Preemptible RCU implementation for rcu_read_lock().
407  * Just increment ->rcu_read_lock_nesting, shared state will be updated
408  * if we block.
409  */
410 void __rcu_read_lock(void)
411 {
412 	current->rcu_read_lock_nesting++;
413 	if (IS_ENABLED(CONFIG_PROVE_LOCKING))
414 		WARN_ON_ONCE(current->rcu_read_lock_nesting > RCU_NEST_PMAX);
415 	barrier();  /* critical section after entry code. */
416 }
417 EXPORT_SYMBOL_GPL(__rcu_read_lock);
418 
419 /*
420  * Preemptible RCU implementation for rcu_read_unlock().
421  * Decrement ->rcu_read_lock_nesting.  If the result is zero (outermost
422  * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
423  * invoke rcu_read_unlock_special() to clean up after a context switch
424  * in an RCU read-side critical section and other special cases.
425  */
426 void __rcu_read_unlock(void)
427 {
428 	struct task_struct *t = current;
429 
430 	if (t->rcu_read_lock_nesting != 1) {
431 		--t->rcu_read_lock_nesting;
432 	} else {
433 		barrier();  /* critical section before exit code. */
434 		t->rcu_read_lock_nesting = -RCU_NEST_BIAS;
435 		barrier();  /* assign before ->rcu_read_unlock_special load */
436 		if (unlikely(READ_ONCE(t->rcu_read_unlock_special.s)))
437 			rcu_read_unlock_special(t);
438 		barrier();  /* ->rcu_read_unlock_special load before assign */
439 		t->rcu_read_lock_nesting = 0;
440 	}
441 	if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
442 		int rrln = t->rcu_read_lock_nesting;
443 
444 		WARN_ON_ONCE(rrln < 0 && rrln > RCU_NEST_NMAX);
445 	}
446 }
447 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
448 
449 /*
450  * Advance a ->blkd_tasks-list pointer to the next entry, instead
451  * returning NULL if at the end of the list.
452  */
453 static struct list_head *rcu_next_node_entry(struct task_struct *t,
454 					     struct rcu_node *rnp)
455 {
456 	struct list_head *np;
457 
458 	np = t->rcu_node_entry.next;
459 	if (np == &rnp->blkd_tasks)
460 		np = NULL;
461 	return np;
462 }
463 
464 /*
465  * Return true if the specified rcu_node structure has tasks that were
466  * preempted within an RCU read-side critical section.
467  */
468 static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
469 {
470 	return !list_empty(&rnp->blkd_tasks);
471 }
472 
473 /*
474  * Report deferred quiescent states.  The deferral time can
475  * be quite short, for example, in the case of the call from
476  * rcu_read_unlock_special().
477  */
478 static void
479 rcu_preempt_deferred_qs_irqrestore(struct task_struct *t, unsigned long flags)
480 {
481 	bool empty_exp;
482 	bool empty_norm;
483 	bool empty_exp_now;
484 	struct list_head *np;
485 	bool drop_boost_mutex = false;
486 	struct rcu_data *rdp;
487 	struct rcu_node *rnp;
488 	union rcu_special special;
489 
490 	/*
491 	 * If RCU core is waiting for this CPU to exit its critical section,
492 	 * report the fact that it has exited.  Because irqs are disabled,
493 	 * t->rcu_read_unlock_special cannot change.
494 	 */
495 	special = t->rcu_read_unlock_special;
496 	rdp = this_cpu_ptr(&rcu_data);
497 	if (!special.s && !rdp->deferred_qs) {
498 		local_irq_restore(flags);
499 		return;
500 	}
501 	if (special.b.need_qs) {
502 		rcu_qs();
503 		t->rcu_read_unlock_special.b.need_qs = false;
504 		if (!t->rcu_read_unlock_special.s && !rdp->deferred_qs) {
505 			local_irq_restore(flags);
506 			return;
507 		}
508 	}
509 
510 	/*
511 	 * Respond to a request by an expedited grace period for a
512 	 * quiescent state from this CPU.  Note that requests from
513 	 * tasks are handled when removing the task from the
514 	 * blocked-tasks list below.
515 	 */
516 	if (rdp->deferred_qs) {
517 		rcu_report_exp_rdp(rdp);
518 		if (!t->rcu_read_unlock_special.s) {
519 			local_irq_restore(flags);
520 			return;
521 		}
522 	}
523 
524 	/* Clean up if blocked during RCU read-side critical section. */
525 	if (special.b.blocked) {
526 		t->rcu_read_unlock_special.b.blocked = false;
527 
528 		/*
529 		 * Remove this task from the list it blocked on.  The task
530 		 * now remains queued on the rcu_node corresponding to the
531 		 * CPU it first blocked on, so there is no longer any need
532 		 * to loop.  Retain a WARN_ON_ONCE() out of sheer paranoia.
533 		 */
534 		rnp = t->rcu_blocked_node;
535 		raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
536 		WARN_ON_ONCE(rnp != t->rcu_blocked_node);
537 		WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
538 		empty_norm = !rcu_preempt_blocked_readers_cgp(rnp);
539 		WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq &&
540 			     (!empty_norm || rnp->qsmask));
541 		empty_exp = sync_rcu_preempt_exp_done(rnp);
542 		smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
543 		np = rcu_next_node_entry(t, rnp);
544 		list_del_init(&t->rcu_node_entry);
545 		t->rcu_blocked_node = NULL;
546 		trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
547 						rnp->gp_seq, t->pid);
548 		if (&t->rcu_node_entry == rnp->gp_tasks)
549 			rnp->gp_tasks = np;
550 		if (&t->rcu_node_entry == rnp->exp_tasks)
551 			rnp->exp_tasks = np;
552 		if (IS_ENABLED(CONFIG_RCU_BOOST)) {
553 			/* Snapshot ->boost_mtx ownership w/rnp->lock held. */
554 			drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx) == t;
555 			if (&t->rcu_node_entry == rnp->boost_tasks)
556 				rnp->boost_tasks = np;
557 		}
558 
559 		/*
560 		 * If this was the last task on the current list, and if
561 		 * we aren't waiting on any CPUs, report the quiescent state.
562 		 * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
563 		 * so we must take a snapshot of the expedited state.
564 		 */
565 		empty_exp_now = sync_rcu_preempt_exp_done(rnp);
566 		if (!empty_norm && !rcu_preempt_blocked_readers_cgp(rnp)) {
567 			trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
568 							 rnp->gp_seq,
569 							 0, rnp->qsmask,
570 							 rnp->level,
571 							 rnp->grplo,
572 							 rnp->grphi,
573 							 !!rnp->gp_tasks);
574 			rcu_report_unblock_qs_rnp(rnp, flags);
575 		} else {
576 			raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
577 		}
578 
579 		/* Unboost if we were boosted. */
580 		if (IS_ENABLED(CONFIG_RCU_BOOST) && drop_boost_mutex)
581 			rt_mutex_futex_unlock(&rnp->boost_mtx);
582 
583 		/*
584 		 * If this was the last task on the expedited lists,
585 		 * then we need to report up the rcu_node hierarchy.
586 		 */
587 		if (!empty_exp && empty_exp_now)
588 			rcu_report_exp_rnp(rnp, true);
589 	} else {
590 		local_irq_restore(flags);
591 	}
592 }
593 
594 /*
595  * Is a deferred quiescent-state pending, and are we also not in
596  * an RCU read-side critical section?  It is the caller's responsibility
597  * to ensure it is otherwise safe to report any deferred quiescent
598  * states.  The reason for this is that it is safe to report a
599  * quiescent state during context switch even though preemption
600  * is disabled.  This function cannot be expected to understand these
601  * nuances, so the caller must handle them.
602  */
603 static bool rcu_preempt_need_deferred_qs(struct task_struct *t)
604 {
605 	return (__this_cpu_read(rcu_data.deferred_qs) ||
606 		READ_ONCE(t->rcu_read_unlock_special.s)) &&
607 	       t->rcu_read_lock_nesting <= 0;
608 }
609 
610 /*
611  * Report a deferred quiescent state if needed and safe to do so.
612  * As with rcu_preempt_need_deferred_qs(), "safe" involves only
613  * not being in an RCU read-side critical section.  The caller must
614  * evaluate safety in terms of interrupt, softirq, and preemption
615  * disabling.
616  */
617 static void rcu_preempt_deferred_qs(struct task_struct *t)
618 {
619 	unsigned long flags;
620 	bool couldrecurse = t->rcu_read_lock_nesting >= 0;
621 
622 	if (!rcu_preempt_need_deferred_qs(t))
623 		return;
624 	if (couldrecurse)
625 		t->rcu_read_lock_nesting -= RCU_NEST_BIAS;
626 	local_irq_save(flags);
627 	rcu_preempt_deferred_qs_irqrestore(t, flags);
628 	if (couldrecurse)
629 		t->rcu_read_lock_nesting += RCU_NEST_BIAS;
630 }
631 
632 /*
633  * Handle special cases during rcu_read_unlock(), such as needing to
634  * notify RCU core processing or task having blocked during the RCU
635  * read-side critical section.
636  */
637 static void rcu_read_unlock_special(struct task_struct *t)
638 {
639 	unsigned long flags;
640 	bool preempt_bh_were_disabled =
641 			!!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK));
642 	bool irqs_were_disabled;
643 
644 	/* NMI handlers cannot block and cannot safely manipulate state. */
645 	if (in_nmi())
646 		return;
647 
648 	local_irq_save(flags);
649 	irqs_were_disabled = irqs_disabled_flags(flags);
650 	if (preempt_bh_were_disabled || irqs_were_disabled) {
651 		WRITE_ONCE(t->rcu_read_unlock_special.b.exp_hint, false);
652 		/* Need to defer quiescent state until everything is enabled. */
653 		if (irqs_were_disabled) {
654 			/* Enabling irqs does not reschedule, so... */
655 			raise_softirq_irqoff(RCU_SOFTIRQ);
656 		} else {
657 			/* Enabling BH or preempt does reschedule, so... */
658 			set_tsk_need_resched(current);
659 			set_preempt_need_resched();
660 		}
661 		local_irq_restore(flags);
662 		return;
663 	}
664 	WRITE_ONCE(t->rcu_read_unlock_special.b.exp_hint, false);
665 	rcu_preempt_deferred_qs_irqrestore(t, flags);
666 }
667 
668 /*
669  * Dump detailed information for all tasks blocking the current RCU
670  * grace period on the specified rcu_node structure.
671  */
672 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
673 {
674 	unsigned long flags;
675 	struct task_struct *t;
676 
677 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
678 	if (!rcu_preempt_blocked_readers_cgp(rnp)) {
679 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
680 		return;
681 	}
682 	t = list_entry(rnp->gp_tasks->prev,
683 		       struct task_struct, rcu_node_entry);
684 	list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
685 		/*
686 		 * We could be printing a lot while holding a spinlock.
687 		 * Avoid triggering hard lockup.
688 		 */
689 		touch_nmi_watchdog();
690 		sched_show_task(t);
691 	}
692 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
693 }
694 
695 /*
696  * Dump detailed information for all tasks blocking the current RCU
697  * grace period.
698  */
699 static void rcu_print_detail_task_stall(void)
700 {
701 	struct rcu_node *rnp = rcu_get_root();
702 
703 	rcu_print_detail_task_stall_rnp(rnp);
704 	rcu_for_each_leaf_node(rnp)
705 		rcu_print_detail_task_stall_rnp(rnp);
706 }
707 
708 static void rcu_print_task_stall_begin(struct rcu_node *rnp)
709 {
710 	pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
711 	       rnp->level, rnp->grplo, rnp->grphi);
712 }
713 
714 static void rcu_print_task_stall_end(void)
715 {
716 	pr_cont("\n");
717 }
718 
719 /*
720  * Scan the current list of tasks blocked within RCU read-side critical
721  * sections, printing out the tid of each.
722  */
723 static int rcu_print_task_stall(struct rcu_node *rnp)
724 {
725 	struct task_struct *t;
726 	int ndetected = 0;
727 
728 	if (!rcu_preempt_blocked_readers_cgp(rnp))
729 		return 0;
730 	rcu_print_task_stall_begin(rnp);
731 	t = list_entry(rnp->gp_tasks->prev,
732 		       struct task_struct, rcu_node_entry);
733 	list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
734 		pr_cont(" P%d", t->pid);
735 		ndetected++;
736 	}
737 	rcu_print_task_stall_end();
738 	return ndetected;
739 }
740 
741 /*
742  * Scan the current list of tasks blocked within RCU read-side critical
743  * sections, printing out the tid of each that is blocking the current
744  * expedited grace period.
745  */
746 static int rcu_print_task_exp_stall(struct rcu_node *rnp)
747 {
748 	struct task_struct *t;
749 	int ndetected = 0;
750 
751 	if (!rnp->exp_tasks)
752 		return 0;
753 	t = list_entry(rnp->exp_tasks->prev,
754 		       struct task_struct, rcu_node_entry);
755 	list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
756 		pr_cont(" P%d", t->pid);
757 		ndetected++;
758 	}
759 	return ndetected;
760 }
761 
762 /*
763  * Check that the list of blocked tasks for the newly completed grace
764  * period is in fact empty.  It is a serious bug to complete a grace
765  * period that still has RCU readers blocked!  This function must be
766  * invoked -before- updating this rnp's ->gp_seq, and the rnp's ->lock
767  * must be held by the caller.
768  *
769  * Also, if there are blocked tasks on the list, they automatically
770  * block the newly created grace period, so set up ->gp_tasks accordingly.
771  */
772 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
773 {
774 	struct task_struct *t;
775 
776 	RCU_LOCKDEP_WARN(preemptible(), "rcu_preempt_check_blocked_tasks() invoked with preemption enabled!!!\n");
777 	if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
778 		dump_blkd_tasks(rnp, 10);
779 	if (rcu_preempt_has_tasks(rnp) &&
780 	    (rnp->qsmaskinit || rnp->wait_blkd_tasks)) {
781 		rnp->gp_tasks = rnp->blkd_tasks.next;
782 		t = container_of(rnp->gp_tasks, struct task_struct,
783 				 rcu_node_entry);
784 		trace_rcu_unlock_preempted_task(TPS("rcu_preempt-GPS"),
785 						rnp->gp_seq, t->pid);
786 	}
787 	WARN_ON_ONCE(rnp->qsmask);
788 }
789 
790 /*
791  * Check for a quiescent state from the current CPU.  When a task blocks,
792  * the task is recorded in the corresponding CPU's rcu_node structure,
793  * which is checked elsewhere.
794  *
795  * Caller must disable hard irqs.
796  */
797 static void rcu_flavor_check_callbacks(int user)
798 {
799 	struct task_struct *t = current;
800 
801 	if (user || rcu_is_cpu_rrupt_from_idle()) {
802 		rcu_note_voluntary_context_switch(current);
803 	}
804 	if (t->rcu_read_lock_nesting > 0 ||
805 	    (preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK))) {
806 		/* No QS, force context switch if deferred. */
807 		if (rcu_preempt_need_deferred_qs(t)) {
808 			set_tsk_need_resched(t);
809 			set_preempt_need_resched();
810 		}
811 	} else if (rcu_preempt_need_deferred_qs(t)) {
812 		rcu_preempt_deferred_qs(t); /* Report deferred QS. */
813 		return;
814 	} else if (!t->rcu_read_lock_nesting) {
815 		rcu_qs(); /* Report immediate QS. */
816 		return;
817 	}
818 
819 	/* If GP is oldish, ask for help from rcu_read_unlock_special(). */
820 	if (t->rcu_read_lock_nesting > 0 &&
821 	    __this_cpu_read(rcu_data.core_needs_qs) &&
822 	    __this_cpu_read(rcu_data.cpu_no_qs.b.norm) &&
823 	    !t->rcu_read_unlock_special.b.need_qs &&
824 	    time_after(jiffies, rcu_state.gp_start + HZ))
825 		t->rcu_read_unlock_special.b.need_qs = true;
826 }
827 
828 /**
829  * synchronize_rcu - wait until a grace period has elapsed.
830  *
831  * Control will return to the caller some time after a full grace
832  * period has elapsed, in other words after all currently executing RCU
833  * read-side critical sections have completed.  Note, however, that
834  * upon return from synchronize_rcu(), the caller might well be executing
835  * concurrently with new RCU read-side critical sections that began while
836  * synchronize_rcu() was waiting.  RCU read-side critical sections are
837  * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
838  * In addition, regions of code across which interrupts, preemption, or
839  * softirqs have been disabled also serve as RCU read-side critical
840  * sections.  This includes hardware interrupt handlers, softirq handlers,
841  * and NMI handlers.
842  *
843  * Note that this guarantee implies further memory-ordering guarantees.
844  * On systems with more than one CPU, when synchronize_rcu() returns,
845  * each CPU is guaranteed to have executed a full memory barrier since
846  * the end of its last RCU read-side critical section whose beginning
847  * preceded the call to synchronize_rcu().  In addition, each CPU having
848  * an RCU read-side critical section that extends beyond the return from
849  * synchronize_rcu() is guaranteed to have executed a full memory barrier
850  * after the beginning of synchronize_rcu() and before the beginning of
851  * that RCU read-side critical section.  Note that these guarantees include
852  * CPUs that are offline, idle, or executing in user mode, as well as CPUs
853  * that are executing in the kernel.
854  *
855  * Furthermore, if CPU A invoked synchronize_rcu(), which returned
856  * to its caller on CPU B, then both CPU A and CPU B are guaranteed
857  * to have executed a full memory barrier during the execution of
858  * synchronize_rcu() -- even if CPU A and CPU B are the same CPU (but
859  * again only if the system has more than one CPU).
860  */
861 void synchronize_rcu(void)
862 {
863 	RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
864 			 lock_is_held(&rcu_lock_map) ||
865 			 lock_is_held(&rcu_sched_lock_map),
866 			 "Illegal synchronize_rcu() in RCU read-side critical section");
867 	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
868 		return;
869 	if (rcu_gp_is_expedited())
870 		synchronize_rcu_expedited();
871 	else
872 		wait_rcu_gp(call_rcu);
873 }
874 EXPORT_SYMBOL_GPL(synchronize_rcu);
875 
876 /*
877  * Check for a task exiting while in a preemptible-RCU read-side
878  * critical section, clean up if so.  No need to issue warnings,
879  * as debug_check_no_locks_held() already does this if lockdep
880  * is enabled.
881  */
882 void exit_rcu(void)
883 {
884 	struct task_struct *t = current;
885 
886 	if (likely(list_empty(&current->rcu_node_entry)))
887 		return;
888 	t->rcu_read_lock_nesting = 1;
889 	barrier();
890 	t->rcu_read_unlock_special.b.blocked = true;
891 	__rcu_read_unlock();
892 	rcu_preempt_deferred_qs(current);
893 }
894 
895 /*
896  * Dump the blocked-tasks state, but limit the list dump to the
897  * specified number of elements.
898  */
899 static void
900 dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
901 {
902 	int cpu;
903 	int i;
904 	struct list_head *lhp;
905 	bool onl;
906 	struct rcu_data *rdp;
907 	struct rcu_node *rnp1;
908 
909 	raw_lockdep_assert_held_rcu_node(rnp);
910 	pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
911 		__func__, rnp->grplo, rnp->grphi, rnp->level,
912 		(long)rnp->gp_seq, (long)rnp->completedqs);
913 	for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
914 		pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx\n",
915 			__func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext);
916 	pr_info("%s: ->gp_tasks %p ->boost_tasks %p ->exp_tasks %p\n",
917 		__func__, rnp->gp_tasks, rnp->boost_tasks, rnp->exp_tasks);
918 	pr_info("%s: ->blkd_tasks", __func__);
919 	i = 0;
920 	list_for_each(lhp, &rnp->blkd_tasks) {
921 		pr_cont(" %p", lhp);
922 		if (++i >= 10)
923 			break;
924 	}
925 	pr_cont("\n");
926 	for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++) {
927 		rdp = per_cpu_ptr(&rcu_data, cpu);
928 		onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
929 		pr_info("\t%d: %c online: %ld(%d) offline: %ld(%d)\n",
930 			cpu, ".o"[onl],
931 			(long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
932 			(long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
933 	}
934 }
935 
936 #else /* #ifdef CONFIG_PREEMPT_RCU */
937 
938 /*
939  * Tell them what RCU they are running.
940  */
941 static void __init rcu_bootup_announce(void)
942 {
943 	pr_info("Hierarchical RCU implementation.\n");
944 	rcu_bootup_announce_oddness();
945 }
946 
947 /*
948  * Note a quiescent state for PREEMPT=n.  Because we do not need to know
949  * how many quiescent states passed, just if there was at least one since
950  * the start of the grace period, this just sets a flag.  The caller must
951  * have disabled preemption.
952  */
953 static void rcu_qs(void)
954 {
955 	RCU_LOCKDEP_WARN(preemptible(), "rcu_qs() invoked with preemption enabled!!!");
956 	if (!__this_cpu_read(rcu_data.cpu_no_qs.s))
957 		return;
958 	trace_rcu_grace_period(TPS("rcu_sched"),
959 			       __this_cpu_read(rcu_data.gp_seq), TPS("cpuqs"));
960 	__this_cpu_write(rcu_data.cpu_no_qs.b.norm, false);
961 	if (!__this_cpu_read(rcu_data.cpu_no_qs.b.exp))
962 		return;
963 	__this_cpu_write(rcu_data.cpu_no_qs.b.exp, false);
964 	rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
965 }
966 
967 /*
968  * Register an urgently needed quiescent state.  If there is an
969  * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
970  * dyntick-idle quiescent state visible to other CPUs, which will in
971  * some cases serve for expedited as well as normal grace periods.
972  * Either way, register a lightweight quiescent state.
973  *
974  * The barrier() calls are redundant in the common case when this is
975  * called externally, but just in case this is called from within this
976  * file.
977  *
978  */
979 void rcu_all_qs(void)
980 {
981 	unsigned long flags;
982 
983 	if (!raw_cpu_read(rcu_data.rcu_urgent_qs))
984 		return;
985 	preempt_disable();
986 	/* Load rcu_urgent_qs before other flags. */
987 	if (!smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
988 		preempt_enable();
989 		return;
990 	}
991 	this_cpu_write(rcu_data.rcu_urgent_qs, false);
992 	barrier(); /* Avoid RCU read-side critical sections leaking down. */
993 	if (unlikely(raw_cpu_read(rcu_data.rcu_need_heavy_qs))) {
994 		local_irq_save(flags);
995 		rcu_momentary_dyntick_idle();
996 		local_irq_restore(flags);
997 	}
998 	rcu_qs();
999 	barrier(); /* Avoid RCU read-side critical sections leaking up. */
1000 	preempt_enable();
1001 }
1002 EXPORT_SYMBOL_GPL(rcu_all_qs);
1003 
1004 /*
1005  * Note a PREEMPT=n context switch.  The caller must have disabled interrupts.
1006  */
1007 void rcu_note_context_switch(bool preempt)
1008 {
1009 	barrier(); /* Avoid RCU read-side critical sections leaking down. */
1010 	trace_rcu_utilization(TPS("Start context switch"));
1011 	rcu_qs();
1012 	/* Load rcu_urgent_qs before other flags. */
1013 	if (!smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs)))
1014 		goto out;
1015 	this_cpu_write(rcu_data.rcu_urgent_qs, false);
1016 	if (unlikely(raw_cpu_read(rcu_data.rcu_need_heavy_qs)))
1017 		rcu_momentary_dyntick_idle();
1018 	if (!preempt)
1019 		rcu_tasks_qs(current);
1020 out:
1021 	trace_rcu_utilization(TPS("End context switch"));
1022 	barrier(); /* Avoid RCU read-side critical sections leaking up. */
1023 }
1024 EXPORT_SYMBOL_GPL(rcu_note_context_switch);
1025 
1026 /*
1027  * Because preemptible RCU does not exist, there are never any preempted
1028  * RCU readers.
1029  */
1030 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
1031 {
1032 	return 0;
1033 }
1034 
1035 /*
1036  * Because there is no preemptible RCU, there can be no readers blocked.
1037  */
1038 static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
1039 {
1040 	return false;
1041 }
1042 
1043 /*
1044  * Because there is no preemptible RCU, there can be no deferred quiescent
1045  * states.
1046  */
1047 static bool rcu_preempt_need_deferred_qs(struct task_struct *t)
1048 {
1049 	return false;
1050 }
1051 static void rcu_preempt_deferred_qs(struct task_struct *t) { }
1052 
1053 /*
1054  * Because preemptible RCU does not exist, we never have to check for
1055  * tasks blocked within RCU read-side critical sections.
1056  */
1057 static void rcu_print_detail_task_stall(void)
1058 {
1059 }
1060 
1061 /*
1062  * Because preemptible RCU does not exist, we never have to check for
1063  * tasks blocked within RCU read-side critical sections.
1064  */
1065 static int rcu_print_task_stall(struct rcu_node *rnp)
1066 {
1067 	return 0;
1068 }
1069 
1070 /*
1071  * Because preemptible RCU does not exist, we never have to check for
1072  * tasks blocked within RCU read-side critical sections that are
1073  * blocking the current expedited grace period.
1074  */
1075 static int rcu_print_task_exp_stall(struct rcu_node *rnp)
1076 {
1077 	return 0;
1078 }
1079 
1080 /*
1081  * Because there is no preemptible RCU, there can be no readers blocked,
1082  * so there is no need to check for blocked tasks.  So check only for
1083  * bogus qsmask values.
1084  */
1085 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
1086 {
1087 	WARN_ON_ONCE(rnp->qsmask);
1088 }
1089 
1090 /*
1091  * Check to see if this CPU is in a non-context-switch quiescent state
1092  * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
1093  * Also schedule RCU core processing.
1094  *
1095  * This function must be called from hardirq context.  It is normally
1096  * invoked from the scheduling-clock interrupt.
1097  */
1098 static void rcu_flavor_check_callbacks(int user)
1099 {
1100 	if (user || rcu_is_cpu_rrupt_from_idle()) {
1101 
1102 		/*
1103 		 * Get here if this CPU took its interrupt from user
1104 		 * mode or from the idle loop, and if this is not a
1105 		 * nested interrupt.  In this case, the CPU is in
1106 		 * a quiescent state, so note it.
1107 		 *
1108 		 * No memory barrier is required here because rcu_qs()
1109 		 * references only CPU-local variables that other CPUs
1110 		 * neither access nor modify, at least not while the
1111 		 * corresponding CPU is online.
1112 		 */
1113 
1114 		rcu_qs();
1115 	}
1116 }
1117 
1118 /* PREEMPT=n implementation of synchronize_rcu(). */
1119 void synchronize_rcu(void)
1120 {
1121 	RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
1122 			 lock_is_held(&rcu_lock_map) ||
1123 			 lock_is_held(&rcu_sched_lock_map),
1124 			 "Illegal synchronize_rcu() in RCU read-side critical section");
1125 	if (rcu_blocking_is_gp())
1126 		return;
1127 	if (rcu_gp_is_expedited())
1128 		synchronize_rcu_expedited();
1129 	else
1130 		wait_rcu_gp(call_rcu);
1131 }
1132 EXPORT_SYMBOL_GPL(synchronize_rcu);
1133 
1134 /*
1135  * Because preemptible RCU does not exist, tasks cannot possibly exit
1136  * while in preemptible RCU read-side critical sections.
1137  */
1138 void exit_rcu(void)
1139 {
1140 }
1141 
1142 /*
1143  * Dump the guaranteed-empty blocked-tasks state.  Trust but verify.
1144  */
1145 static void
1146 dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
1147 {
1148 	WARN_ON_ONCE(!list_empty(&rnp->blkd_tasks));
1149 }
1150 
1151 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
1152 
1153 #ifdef CONFIG_RCU_BOOST
1154 
1155 static void rcu_wake_cond(struct task_struct *t, int status)
1156 {
1157 	/*
1158 	 * If the thread is yielding, only wake it when this
1159 	 * is invoked from idle
1160 	 */
1161 	if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
1162 		wake_up_process(t);
1163 }
1164 
1165 /*
1166  * Carry out RCU priority boosting on the task indicated by ->exp_tasks
1167  * or ->boost_tasks, advancing the pointer to the next task in the
1168  * ->blkd_tasks list.
1169  *
1170  * Note that irqs must be enabled: boosting the task can block.
1171  * Returns 1 if there are more tasks needing to be boosted.
1172  */
1173 static int rcu_boost(struct rcu_node *rnp)
1174 {
1175 	unsigned long flags;
1176 	struct task_struct *t;
1177 	struct list_head *tb;
1178 
1179 	if (READ_ONCE(rnp->exp_tasks) == NULL &&
1180 	    READ_ONCE(rnp->boost_tasks) == NULL)
1181 		return 0;  /* Nothing left to boost. */
1182 
1183 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
1184 
1185 	/*
1186 	 * Recheck under the lock: all tasks in need of boosting
1187 	 * might exit their RCU read-side critical sections on their own.
1188 	 */
1189 	if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
1190 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1191 		return 0;
1192 	}
1193 
1194 	/*
1195 	 * Preferentially boost tasks blocking expedited grace periods.
1196 	 * This cannot starve the normal grace periods because a second
1197 	 * expedited grace period must boost all blocked tasks, including
1198 	 * those blocking the pre-existing normal grace period.
1199 	 */
1200 	if (rnp->exp_tasks != NULL)
1201 		tb = rnp->exp_tasks;
1202 	else
1203 		tb = rnp->boost_tasks;
1204 
1205 	/*
1206 	 * We boost task t by manufacturing an rt_mutex that appears to
1207 	 * be held by task t.  We leave a pointer to that rt_mutex where
1208 	 * task t can find it, and task t will release the mutex when it
1209 	 * exits its outermost RCU read-side critical section.  Then
1210 	 * simply acquiring this artificial rt_mutex will boost task
1211 	 * t's priority.  (Thanks to tglx for suggesting this approach!)
1212 	 *
1213 	 * Note that task t must acquire rnp->lock to remove itself from
1214 	 * the ->blkd_tasks list, which it will do from exit() if from
1215 	 * nowhere else.  We therefore are guaranteed that task t will
1216 	 * stay around at least until we drop rnp->lock.  Note that
1217 	 * rnp->lock also resolves races between our priority boosting
1218 	 * and task t's exiting its outermost RCU read-side critical
1219 	 * section.
1220 	 */
1221 	t = container_of(tb, struct task_struct, rcu_node_entry);
1222 	rt_mutex_init_proxy_locked(&rnp->boost_mtx, t);
1223 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1224 	/* Lock only for side effect: boosts task t's priority. */
1225 	rt_mutex_lock(&rnp->boost_mtx);
1226 	rt_mutex_unlock(&rnp->boost_mtx);  /* Then keep lockdep happy. */
1227 
1228 	return READ_ONCE(rnp->exp_tasks) != NULL ||
1229 	       READ_ONCE(rnp->boost_tasks) != NULL;
1230 }
1231 
1232 /*
1233  * Priority-boosting kthread, one per leaf rcu_node.
1234  */
1235 static int rcu_boost_kthread(void *arg)
1236 {
1237 	struct rcu_node *rnp = (struct rcu_node *)arg;
1238 	int spincnt = 0;
1239 	int more2boost;
1240 
1241 	trace_rcu_utilization(TPS("Start boost kthread@init"));
1242 	for (;;) {
1243 		rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
1244 		trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
1245 		rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
1246 		trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
1247 		rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
1248 		more2boost = rcu_boost(rnp);
1249 		if (more2boost)
1250 			spincnt++;
1251 		else
1252 			spincnt = 0;
1253 		if (spincnt > 10) {
1254 			rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
1255 			trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
1256 			schedule_timeout_interruptible(2);
1257 			trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
1258 			spincnt = 0;
1259 		}
1260 	}
1261 	/* NOTREACHED */
1262 	trace_rcu_utilization(TPS("End boost kthread@notreached"));
1263 	return 0;
1264 }
1265 
1266 /*
1267  * Check to see if it is time to start boosting RCU readers that are
1268  * blocking the current grace period, and, if so, tell the per-rcu_node
1269  * kthread to start boosting them.  If there is an expedited grace
1270  * period in progress, it is always time to boost.
1271  *
1272  * The caller must hold rnp->lock, which this function releases.
1273  * The ->boost_kthread_task is immortal, so we don't need to worry
1274  * about it going away.
1275  */
1276 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
1277 	__releases(rnp->lock)
1278 {
1279 	struct task_struct *t;
1280 
1281 	raw_lockdep_assert_held_rcu_node(rnp);
1282 	if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
1283 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1284 		return;
1285 	}
1286 	if (rnp->exp_tasks != NULL ||
1287 	    (rnp->gp_tasks != NULL &&
1288 	     rnp->boost_tasks == NULL &&
1289 	     rnp->qsmask == 0 &&
1290 	     ULONG_CMP_GE(jiffies, rnp->boost_time))) {
1291 		if (rnp->exp_tasks == NULL)
1292 			rnp->boost_tasks = rnp->gp_tasks;
1293 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1294 		t = rnp->boost_kthread_task;
1295 		if (t)
1296 			rcu_wake_cond(t, rnp->boost_kthread_status);
1297 	} else {
1298 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1299 	}
1300 }
1301 
1302 /*
1303  * Wake up the per-CPU kthread to invoke RCU callbacks.
1304  */
1305 static void invoke_rcu_callbacks_kthread(void)
1306 {
1307 	unsigned long flags;
1308 
1309 	local_irq_save(flags);
1310 	__this_cpu_write(rcu_cpu_has_work, 1);
1311 	if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
1312 	    current != __this_cpu_read(rcu_cpu_kthread_task)) {
1313 		rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
1314 			      __this_cpu_read(rcu_cpu_kthread_status));
1315 	}
1316 	local_irq_restore(flags);
1317 }
1318 
1319 /*
1320  * Is the current CPU running the RCU-callbacks kthread?
1321  * Caller must have preemption disabled.
1322  */
1323 static bool rcu_is_callbacks_kthread(void)
1324 {
1325 	return __this_cpu_read(rcu_cpu_kthread_task) == current;
1326 }
1327 
1328 #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
1329 
1330 /*
1331  * Do priority-boost accounting for the start of a new grace period.
1332  */
1333 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1334 {
1335 	rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
1336 }
1337 
1338 /*
1339  * Create an RCU-boost kthread for the specified node if one does not
1340  * already exist.  We only create this kthread for preemptible RCU.
1341  * Returns zero if all is well, a negated errno otherwise.
1342  */
1343 static int rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
1344 {
1345 	int rnp_index = rnp - rcu_get_root();
1346 	unsigned long flags;
1347 	struct sched_param sp;
1348 	struct task_struct *t;
1349 
1350 	if (!IS_ENABLED(CONFIG_PREEMPT_RCU))
1351 		return 0;
1352 
1353 	if (!rcu_scheduler_fully_active || rcu_rnp_online_cpus(rnp) == 0)
1354 		return 0;
1355 
1356 	rcu_state.boost = 1;
1357 	if (rnp->boost_kthread_task != NULL)
1358 		return 0;
1359 	t = kthread_create(rcu_boost_kthread, (void *)rnp,
1360 			   "rcub/%d", rnp_index);
1361 	if (IS_ERR(t))
1362 		return PTR_ERR(t);
1363 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
1364 	rnp->boost_kthread_task = t;
1365 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1366 	sp.sched_priority = kthread_prio;
1367 	sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1368 	wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
1369 	return 0;
1370 }
1371 
1372 static void rcu_cpu_kthread_setup(unsigned int cpu)
1373 {
1374 	struct sched_param sp;
1375 
1376 	sp.sched_priority = kthread_prio;
1377 	sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
1378 }
1379 
1380 static void rcu_cpu_kthread_park(unsigned int cpu)
1381 {
1382 	per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
1383 }
1384 
1385 static int rcu_cpu_kthread_should_run(unsigned int cpu)
1386 {
1387 	return __this_cpu_read(rcu_cpu_has_work);
1388 }
1389 
1390 /*
1391  * Per-CPU kernel thread that invokes RCU callbacks.  This replaces
1392  * the RCU softirq used in configurations of RCU that do not support RCU
1393  * priority boosting.
1394  */
1395 static void rcu_cpu_kthread(unsigned int cpu)
1396 {
1397 	unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
1398 	char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
1399 	int spincnt;
1400 
1401 	for (spincnt = 0; spincnt < 10; spincnt++) {
1402 		trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
1403 		local_bh_disable();
1404 		*statusp = RCU_KTHREAD_RUNNING;
1405 		this_cpu_inc(rcu_cpu_kthread_loops);
1406 		local_irq_disable();
1407 		work = *workp;
1408 		*workp = 0;
1409 		local_irq_enable();
1410 		if (work)
1411 			rcu_do_batch(this_cpu_ptr(&rcu_data));
1412 		local_bh_enable();
1413 		if (*workp == 0) {
1414 			trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
1415 			*statusp = RCU_KTHREAD_WAITING;
1416 			return;
1417 		}
1418 	}
1419 	*statusp = RCU_KTHREAD_YIELDING;
1420 	trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
1421 	schedule_timeout_interruptible(2);
1422 	trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
1423 	*statusp = RCU_KTHREAD_WAITING;
1424 }
1425 
1426 /*
1427  * Set the per-rcu_node kthread's affinity to cover all CPUs that are
1428  * served by the rcu_node in question.  The CPU hotplug lock is still
1429  * held, so the value of rnp->qsmaskinit will be stable.
1430  *
1431  * We don't include outgoingcpu in the affinity set, use -1 if there is
1432  * no outgoing CPU.  If there are no CPUs left in the affinity set,
1433  * this function allows the kthread to execute on any CPU.
1434  */
1435 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1436 {
1437 	struct task_struct *t = rnp->boost_kthread_task;
1438 	unsigned long mask = rcu_rnp_online_cpus(rnp);
1439 	cpumask_var_t cm;
1440 	int cpu;
1441 
1442 	if (!t)
1443 		return;
1444 	if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
1445 		return;
1446 	for_each_leaf_node_possible_cpu(rnp, cpu)
1447 		if ((mask & leaf_node_cpu_bit(rnp, cpu)) &&
1448 		    cpu != outgoingcpu)
1449 			cpumask_set_cpu(cpu, cm);
1450 	if (cpumask_weight(cm) == 0)
1451 		cpumask_setall(cm);
1452 	set_cpus_allowed_ptr(t, cm);
1453 	free_cpumask_var(cm);
1454 }
1455 
1456 static struct smp_hotplug_thread rcu_cpu_thread_spec = {
1457 	.store			= &rcu_cpu_kthread_task,
1458 	.thread_should_run	= rcu_cpu_kthread_should_run,
1459 	.thread_fn		= rcu_cpu_kthread,
1460 	.thread_comm		= "rcuc/%u",
1461 	.setup			= rcu_cpu_kthread_setup,
1462 	.park			= rcu_cpu_kthread_park,
1463 };
1464 
1465 /*
1466  * Spawn boost kthreads -- called as soon as the scheduler is running.
1467  */
1468 static void __init rcu_spawn_boost_kthreads(void)
1469 {
1470 	struct rcu_node *rnp;
1471 	int cpu;
1472 
1473 	for_each_possible_cpu(cpu)
1474 		per_cpu(rcu_cpu_has_work, cpu) = 0;
1475 	if (WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec), "%s: Could not start rcub kthread, OOM is now expected behavior\n", __func__))
1476 		return;
1477 	rcu_for_each_leaf_node(rnp)
1478 		(void)rcu_spawn_one_boost_kthread(rnp);
1479 }
1480 
1481 static void rcu_prepare_kthreads(int cpu)
1482 {
1483 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1484 	struct rcu_node *rnp = rdp->mynode;
1485 
1486 	/* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
1487 	if (rcu_scheduler_fully_active)
1488 		(void)rcu_spawn_one_boost_kthread(rnp);
1489 }
1490 
1491 #else /* #ifdef CONFIG_RCU_BOOST */
1492 
1493 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
1494 	__releases(rnp->lock)
1495 {
1496 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1497 }
1498 
1499 static void invoke_rcu_callbacks_kthread(void)
1500 {
1501 	WARN_ON_ONCE(1);
1502 }
1503 
1504 static bool rcu_is_callbacks_kthread(void)
1505 {
1506 	return false;
1507 }
1508 
1509 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1510 {
1511 }
1512 
1513 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1514 {
1515 }
1516 
1517 static void __init rcu_spawn_boost_kthreads(void)
1518 {
1519 }
1520 
1521 static void rcu_prepare_kthreads(int cpu)
1522 {
1523 }
1524 
1525 #endif /* #else #ifdef CONFIG_RCU_BOOST */
1526 
1527 #if !defined(CONFIG_RCU_FAST_NO_HZ)
1528 
1529 /*
1530  * Check to see if any future RCU-related work will need to be done
1531  * by the current CPU, even if none need be done immediately, returning
1532  * 1 if so.  This function is part of the RCU implementation; it is -not-
1533  * an exported member of the RCU API.
1534  *
1535  * Because we not have RCU_FAST_NO_HZ, just check whether or not this
1536  * CPU has RCU callbacks queued.
1537  */
1538 int rcu_needs_cpu(u64 basemono, u64 *nextevt)
1539 {
1540 	*nextevt = KTIME_MAX;
1541 	return rcu_cpu_has_callbacks(NULL);
1542 }
1543 
1544 /*
1545  * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
1546  * after it.
1547  */
1548 static void rcu_cleanup_after_idle(void)
1549 {
1550 }
1551 
1552 /*
1553  * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
1554  * is nothing.
1555  */
1556 static void rcu_prepare_for_idle(void)
1557 {
1558 }
1559 
1560 /*
1561  * Don't bother keeping a running count of the number of RCU callbacks
1562  * posted because CONFIG_RCU_FAST_NO_HZ=n.
1563  */
1564 static void rcu_idle_count_callbacks_posted(void)
1565 {
1566 }
1567 
1568 #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1569 
1570 /*
1571  * This code is invoked when a CPU goes idle, at which point we want
1572  * to have the CPU do everything required for RCU so that it can enter
1573  * the energy-efficient dyntick-idle mode.  This is handled by a
1574  * state machine implemented by rcu_prepare_for_idle() below.
1575  *
1576  * The following three proprocessor symbols control this state machine:
1577  *
1578  * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
1579  *	to sleep in dyntick-idle mode with RCU callbacks pending.  This
1580  *	is sized to be roughly one RCU grace period.  Those energy-efficiency
1581  *	benchmarkers who might otherwise be tempted to set this to a large
1582  *	number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
1583  *	system.  And if you are -that- concerned about energy efficiency,
1584  *	just power the system down and be done with it!
1585  * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
1586  *	permitted to sleep in dyntick-idle mode with only lazy RCU
1587  *	callbacks pending.  Setting this too high can OOM your system.
1588  *
1589  * The values below work well in practice.  If future workloads require
1590  * adjustment, they can be converted into kernel config parameters, though
1591  * making the state machine smarter might be a better option.
1592  */
1593 #define RCU_IDLE_GP_DELAY 4		/* Roughly one grace period. */
1594 #define RCU_IDLE_LAZY_GP_DELAY (6 * HZ)	/* Roughly six seconds. */
1595 
1596 static int rcu_idle_gp_delay = RCU_IDLE_GP_DELAY;
1597 module_param(rcu_idle_gp_delay, int, 0644);
1598 static int rcu_idle_lazy_gp_delay = RCU_IDLE_LAZY_GP_DELAY;
1599 module_param(rcu_idle_lazy_gp_delay, int, 0644);
1600 
1601 /*
1602  * Try to advance callbacks on the current CPU, but only if it has been
1603  * awhile since the last time we did so.  Afterwards, if there are any
1604  * callbacks ready for immediate invocation, return true.
1605  */
1606 static bool __maybe_unused rcu_try_advance_all_cbs(void)
1607 {
1608 	bool cbs_ready = false;
1609 	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
1610 	struct rcu_node *rnp;
1611 
1612 	/* Exit early if we advanced recently. */
1613 	if (jiffies == rdp->last_advance_all)
1614 		return false;
1615 	rdp->last_advance_all = jiffies;
1616 
1617 	rnp = rdp->mynode;
1618 
1619 	/*
1620 	 * Don't bother checking unless a grace period has
1621 	 * completed since we last checked and there are
1622 	 * callbacks not yet ready to invoke.
1623 	 */
1624 	if ((rcu_seq_completed_gp(rdp->gp_seq,
1625 				  rcu_seq_current(&rnp->gp_seq)) ||
1626 	     unlikely(READ_ONCE(rdp->gpwrap))) &&
1627 	    rcu_segcblist_pend_cbs(&rdp->cblist))
1628 		note_gp_changes(rdp);
1629 
1630 	if (rcu_segcblist_ready_cbs(&rdp->cblist))
1631 		cbs_ready = true;
1632 	return cbs_ready;
1633 }
1634 
1635 /*
1636  * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
1637  * to invoke.  If the CPU has callbacks, try to advance them.  Tell the
1638  * caller to set the timeout based on whether or not there are non-lazy
1639  * callbacks.
1640  *
1641  * The caller must have disabled interrupts.
1642  */
1643 int rcu_needs_cpu(u64 basemono, u64 *nextevt)
1644 {
1645 	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
1646 	unsigned long dj;
1647 
1648 	lockdep_assert_irqs_disabled();
1649 
1650 	/* Snapshot to detect later posting of non-lazy callback. */
1651 	rdp->nonlazy_posted_snap = rdp->nonlazy_posted;
1652 
1653 	/* If no callbacks, RCU doesn't need the CPU. */
1654 	if (!rcu_cpu_has_callbacks(&rdp->all_lazy)) {
1655 		*nextevt = KTIME_MAX;
1656 		return 0;
1657 	}
1658 
1659 	/* Attempt to advance callbacks. */
1660 	if (rcu_try_advance_all_cbs()) {
1661 		/* Some ready to invoke, so initiate later invocation. */
1662 		invoke_rcu_core();
1663 		return 1;
1664 	}
1665 	rdp->last_accelerate = jiffies;
1666 
1667 	/* Request timer delay depending on laziness, and round. */
1668 	if (!rdp->all_lazy) {
1669 		dj = round_up(rcu_idle_gp_delay + jiffies,
1670 			       rcu_idle_gp_delay) - jiffies;
1671 	} else {
1672 		dj = round_jiffies(rcu_idle_lazy_gp_delay + jiffies) - jiffies;
1673 	}
1674 	*nextevt = basemono + dj * TICK_NSEC;
1675 	return 0;
1676 }
1677 
1678 /*
1679  * Prepare a CPU for idle from an RCU perspective.  The first major task
1680  * is to sense whether nohz mode has been enabled or disabled via sysfs.
1681  * The second major task is to check to see if a non-lazy callback has
1682  * arrived at a CPU that previously had only lazy callbacks.  The third
1683  * major task is to accelerate (that is, assign grace-period numbers to)
1684  * any recently arrived callbacks.
1685  *
1686  * The caller must have disabled interrupts.
1687  */
1688 static void rcu_prepare_for_idle(void)
1689 {
1690 	bool needwake;
1691 	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
1692 	struct rcu_node *rnp;
1693 	int tne;
1694 
1695 	lockdep_assert_irqs_disabled();
1696 	if (rcu_is_nocb_cpu(smp_processor_id()))
1697 		return;
1698 
1699 	/* Handle nohz enablement switches conservatively. */
1700 	tne = READ_ONCE(tick_nohz_active);
1701 	if (tne != rdp->tick_nohz_enabled_snap) {
1702 		if (rcu_cpu_has_callbacks(NULL))
1703 			invoke_rcu_core(); /* force nohz to see update. */
1704 		rdp->tick_nohz_enabled_snap = tne;
1705 		return;
1706 	}
1707 	if (!tne)
1708 		return;
1709 
1710 	/*
1711 	 * If a non-lazy callback arrived at a CPU having only lazy
1712 	 * callbacks, invoke RCU core for the side-effect of recalculating
1713 	 * idle duration on re-entry to idle.
1714 	 */
1715 	if (rdp->all_lazy &&
1716 	    rdp->nonlazy_posted != rdp->nonlazy_posted_snap) {
1717 		rdp->all_lazy = false;
1718 		rdp->nonlazy_posted_snap = rdp->nonlazy_posted;
1719 		invoke_rcu_core();
1720 		return;
1721 	}
1722 
1723 	/*
1724 	 * If we have not yet accelerated this jiffy, accelerate all
1725 	 * callbacks on this CPU.
1726 	 */
1727 	if (rdp->last_accelerate == jiffies)
1728 		return;
1729 	rdp->last_accelerate = jiffies;
1730 	if (rcu_segcblist_pend_cbs(&rdp->cblist)) {
1731 		rnp = rdp->mynode;
1732 		raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
1733 		needwake = rcu_accelerate_cbs(rnp, rdp);
1734 		raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
1735 		if (needwake)
1736 			rcu_gp_kthread_wake();
1737 	}
1738 }
1739 
1740 /*
1741  * Clean up for exit from idle.  Attempt to advance callbacks based on
1742  * any grace periods that elapsed while the CPU was idle, and if any
1743  * callbacks are now ready to invoke, initiate invocation.
1744  */
1745 static void rcu_cleanup_after_idle(void)
1746 {
1747 	lockdep_assert_irqs_disabled();
1748 	if (rcu_is_nocb_cpu(smp_processor_id()))
1749 		return;
1750 	if (rcu_try_advance_all_cbs())
1751 		invoke_rcu_core();
1752 }
1753 
1754 /*
1755  * Keep a running count of the number of non-lazy callbacks posted
1756  * on this CPU.  This running counter (which is never decremented) allows
1757  * rcu_prepare_for_idle() to detect when something out of the idle loop
1758  * posts a callback, even if an equal number of callbacks are invoked.
1759  * Of course, callbacks should only be posted from within a trace event
1760  * designed to be called from idle or from within RCU_NONIDLE().
1761  */
1762 static void rcu_idle_count_callbacks_posted(void)
1763 {
1764 	__this_cpu_add(rcu_data.nonlazy_posted, 1);
1765 }
1766 
1767 #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1768 
1769 #ifdef CONFIG_RCU_FAST_NO_HZ
1770 
1771 static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
1772 {
1773 	struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
1774 	unsigned long nlpd = rdp->nonlazy_posted - rdp->nonlazy_posted_snap;
1775 
1776 	sprintf(cp, "last_accelerate: %04lx/%04lx, nonlazy_posted: %ld, %c%c",
1777 		rdp->last_accelerate & 0xffff, jiffies & 0xffff,
1778 		ulong2long(nlpd),
1779 		rdp->all_lazy ? 'L' : '.',
1780 		rdp->tick_nohz_enabled_snap ? '.' : 'D');
1781 }
1782 
1783 #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
1784 
1785 static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
1786 {
1787 	*cp = '\0';
1788 }
1789 
1790 #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
1791 
1792 /* Initiate the stall-info list. */
1793 static void print_cpu_stall_info_begin(void)
1794 {
1795 	pr_cont("\n");
1796 }
1797 
1798 /*
1799  * Print out diagnostic information for the specified stalled CPU.
1800  *
1801  * If the specified CPU is aware of the current RCU grace period, then
1802  * print the number of scheduling clock interrupts the CPU has taken
1803  * during the time that it has been aware.  Otherwise, print the number
1804  * of RCU grace periods that this CPU is ignorant of, for example, "1"
1805  * if the CPU was aware of the previous grace period.
1806  *
1807  * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
1808  */
1809 static void print_cpu_stall_info(int cpu)
1810 {
1811 	unsigned long delta;
1812 	char fast_no_hz[72];
1813 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1814 	char *ticks_title;
1815 	unsigned long ticks_value;
1816 
1817 	/*
1818 	 * We could be printing a lot while holding a spinlock.  Avoid
1819 	 * triggering hard lockup.
1820 	 */
1821 	touch_nmi_watchdog();
1822 
1823 	ticks_value = rcu_seq_ctr(rcu_state.gp_seq - rdp->gp_seq);
1824 	if (ticks_value) {
1825 		ticks_title = "GPs behind";
1826 	} else {
1827 		ticks_title = "ticks this GP";
1828 		ticks_value = rdp->ticks_this_gp;
1829 	}
1830 	print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
1831 	delta = rcu_seq_ctr(rdp->mynode->gp_seq - rdp->rcu_iw_gp_seq);
1832 	pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%03x/%ld/%#lx softirq=%u/%u fqs=%ld %s\n",
1833 	       cpu,
1834 	       "O."[!!cpu_online(cpu)],
1835 	       "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
1836 	       "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
1837 	       !IS_ENABLED(CONFIG_IRQ_WORK) ? '?' :
1838 			rdp->rcu_iw_pending ? (int)min(delta, 9UL) + '0' :
1839 				"!."[!delta],
1840 	       ticks_value, ticks_title,
1841 	       rcu_dynticks_snap(rdp) & 0xfff,
1842 	       rdp->dynticks_nesting, rdp->dynticks_nmi_nesting,
1843 	       rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
1844 	       READ_ONCE(rcu_state.n_force_qs) - rcu_state.n_force_qs_gpstart,
1845 	       fast_no_hz);
1846 }
1847 
1848 /* Terminate the stall-info list. */
1849 static void print_cpu_stall_info_end(void)
1850 {
1851 	pr_err("\t");
1852 }
1853 
1854 /* Zero ->ticks_this_gp and snapshot the number of RCU softirq handlers. */
1855 static void zero_cpu_stall_ticks(struct rcu_data *rdp)
1856 {
1857 	rdp->ticks_this_gp = 0;
1858 	rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
1859 	WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1860 }
1861 
1862 #ifdef CONFIG_RCU_NOCB_CPU
1863 
1864 /*
1865  * Offload callback processing from the boot-time-specified set of CPUs
1866  * specified by rcu_nocb_mask.  For each CPU in the set, there is a
1867  * kthread created that pulls the callbacks from the corresponding CPU,
1868  * waits for a grace period to elapse, and invokes the callbacks.
1869  * The no-CBs CPUs do a wake_up() on their kthread when they insert
1870  * a callback into any empty list, unless the rcu_nocb_poll boot parameter
1871  * has been specified, in which case each kthread actively polls its
1872  * CPU.  (Which isn't so great for energy efficiency, but which does
1873  * reduce RCU's overhead on that CPU.)
1874  *
1875  * This is intended to be used in conjunction with Frederic Weisbecker's
1876  * adaptive-idle work, which would seriously reduce OS jitter on CPUs
1877  * running CPU-bound user-mode computations.
1878  *
1879  * Offloading of callback processing could also in theory be used as
1880  * an energy-efficiency measure because CPUs with no RCU callbacks
1881  * queued are more aggressive about entering dyntick-idle mode.
1882  */
1883 
1884 
1885 /* Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters. */
1886 static int __init rcu_nocb_setup(char *str)
1887 {
1888 	alloc_bootmem_cpumask_var(&rcu_nocb_mask);
1889 	cpulist_parse(str, rcu_nocb_mask);
1890 	return 1;
1891 }
1892 __setup("rcu_nocbs=", rcu_nocb_setup);
1893 
1894 static int __init parse_rcu_nocb_poll(char *arg)
1895 {
1896 	rcu_nocb_poll = true;
1897 	return 0;
1898 }
1899 early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
1900 
1901 /*
1902  * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
1903  * grace period.
1904  */
1905 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
1906 {
1907 	swake_up_all(sq);
1908 }
1909 
1910 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
1911 {
1912 	return &rnp->nocb_gp_wq[rcu_seq_ctr(rnp->gp_seq) & 0x1];
1913 }
1914 
1915 static void rcu_init_one_nocb(struct rcu_node *rnp)
1916 {
1917 	init_swait_queue_head(&rnp->nocb_gp_wq[0]);
1918 	init_swait_queue_head(&rnp->nocb_gp_wq[1]);
1919 }
1920 
1921 /* Is the specified CPU a no-CBs CPU? */
1922 bool rcu_is_nocb_cpu(int cpu)
1923 {
1924 	if (cpumask_available(rcu_nocb_mask))
1925 		return cpumask_test_cpu(cpu, rcu_nocb_mask);
1926 	return false;
1927 }
1928 
1929 /*
1930  * Kick the leader kthread for this NOCB group.  Caller holds ->nocb_lock
1931  * and this function releases it.
1932  */
1933 static void __wake_nocb_leader(struct rcu_data *rdp, bool force,
1934 			       unsigned long flags)
1935 	__releases(rdp->nocb_lock)
1936 {
1937 	struct rcu_data *rdp_leader = rdp->nocb_leader;
1938 
1939 	lockdep_assert_held(&rdp->nocb_lock);
1940 	if (!READ_ONCE(rdp_leader->nocb_kthread)) {
1941 		raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
1942 		return;
1943 	}
1944 	if (rdp_leader->nocb_leader_sleep || force) {
1945 		/* Prior smp_mb__after_atomic() orders against prior enqueue. */
1946 		WRITE_ONCE(rdp_leader->nocb_leader_sleep, false);
1947 		del_timer(&rdp->nocb_timer);
1948 		raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
1949 		smp_mb(); /* ->nocb_leader_sleep before swake_up_one(). */
1950 		swake_up_one(&rdp_leader->nocb_wq);
1951 	} else {
1952 		raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
1953 	}
1954 }
1955 
1956 /*
1957  * Kick the leader kthread for this NOCB group, but caller has not
1958  * acquired locks.
1959  */
1960 static void wake_nocb_leader(struct rcu_data *rdp, bool force)
1961 {
1962 	unsigned long flags;
1963 
1964 	raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
1965 	__wake_nocb_leader(rdp, force, flags);
1966 }
1967 
1968 /*
1969  * Arrange to wake the leader kthread for this NOCB group at some
1970  * future time when it is safe to do so.
1971  */
1972 static void wake_nocb_leader_defer(struct rcu_data *rdp, int waketype,
1973 				   const char *reason)
1974 {
1975 	unsigned long flags;
1976 
1977 	raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
1978 	if (rdp->nocb_defer_wakeup == RCU_NOCB_WAKE_NOT)
1979 		mod_timer(&rdp->nocb_timer, jiffies + 1);
1980 	WRITE_ONCE(rdp->nocb_defer_wakeup, waketype);
1981 	trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, reason);
1982 	raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
1983 }
1984 
1985 /*
1986  * Does the specified CPU need an RCU callback for this invocation
1987  * of rcu_barrier()?
1988  */
1989 static bool rcu_nocb_cpu_needs_barrier(int cpu)
1990 {
1991 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1992 	unsigned long ret;
1993 #ifdef CONFIG_PROVE_RCU
1994 	struct rcu_head *rhp;
1995 #endif /* #ifdef CONFIG_PROVE_RCU */
1996 
1997 	/*
1998 	 * Check count of all no-CBs callbacks awaiting invocation.
1999 	 * There needs to be a barrier before this function is called,
2000 	 * but associated with a prior determination that no more
2001 	 * callbacks would be posted.  In the worst case, the first
2002 	 * barrier in rcu_barrier() suffices (but the caller cannot
2003 	 * necessarily rely on this, not a substitute for the caller
2004 	 * getting the concurrency design right!).  There must also be
2005 	 * a barrier between the following load an posting of a callback
2006 	 * (if a callback is in fact needed).  This is associated with an
2007 	 * atomic_inc() in the caller.
2008 	 */
2009 	ret = rcu_get_n_cbs_nocb_cpu(rdp);
2010 
2011 #ifdef CONFIG_PROVE_RCU
2012 	rhp = READ_ONCE(rdp->nocb_head);
2013 	if (!rhp)
2014 		rhp = READ_ONCE(rdp->nocb_gp_head);
2015 	if (!rhp)
2016 		rhp = READ_ONCE(rdp->nocb_follower_head);
2017 
2018 	/* Having no rcuo kthread but CBs after scheduler starts is bad! */
2019 	if (!READ_ONCE(rdp->nocb_kthread) && rhp &&
2020 	    rcu_scheduler_fully_active) {
2021 		/* RCU callback enqueued before CPU first came online??? */
2022 		pr_err("RCU: Never-onlined no-CBs CPU %d has CB %p\n",
2023 		       cpu, rhp->func);
2024 		WARN_ON_ONCE(1);
2025 	}
2026 #endif /* #ifdef CONFIG_PROVE_RCU */
2027 
2028 	return !!ret;
2029 }
2030 
2031 /*
2032  * Enqueue the specified string of rcu_head structures onto the specified
2033  * CPU's no-CBs lists.  The CPU is specified by rdp, the head of the
2034  * string by rhp, and the tail of the string by rhtp.  The non-lazy/lazy
2035  * counts are supplied by rhcount and rhcount_lazy.
2036  *
2037  * If warranted, also wake up the kthread servicing this CPUs queues.
2038  */
2039 static void __call_rcu_nocb_enqueue(struct rcu_data *rdp,
2040 				    struct rcu_head *rhp,
2041 				    struct rcu_head **rhtp,
2042 				    int rhcount, int rhcount_lazy,
2043 				    unsigned long flags)
2044 {
2045 	int len;
2046 	struct rcu_head **old_rhpp;
2047 	struct task_struct *t;
2048 
2049 	/* Enqueue the callback on the nocb list and update counts. */
2050 	atomic_long_add(rhcount, &rdp->nocb_q_count);
2051 	/* rcu_barrier() relies on ->nocb_q_count add before xchg. */
2052 	old_rhpp = xchg(&rdp->nocb_tail, rhtp);
2053 	WRITE_ONCE(*old_rhpp, rhp);
2054 	atomic_long_add(rhcount_lazy, &rdp->nocb_q_count_lazy);
2055 	smp_mb__after_atomic(); /* Store *old_rhpp before _wake test. */
2056 
2057 	/* If we are not being polled and there is a kthread, awaken it ... */
2058 	t = READ_ONCE(rdp->nocb_kthread);
2059 	if (rcu_nocb_poll || !t) {
2060 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
2061 				    TPS("WakeNotPoll"));
2062 		return;
2063 	}
2064 	len = rcu_get_n_cbs_nocb_cpu(rdp);
2065 	if (old_rhpp == &rdp->nocb_head) {
2066 		if (!irqs_disabled_flags(flags)) {
2067 			/* ... if queue was empty ... */
2068 			wake_nocb_leader(rdp, false);
2069 			trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
2070 					    TPS("WakeEmpty"));
2071 		} else {
2072 			wake_nocb_leader_defer(rdp, RCU_NOCB_WAKE,
2073 					       TPS("WakeEmptyIsDeferred"));
2074 		}
2075 		rdp->qlen_last_fqs_check = 0;
2076 	} else if (len > rdp->qlen_last_fqs_check + qhimark) {
2077 		/* ... or if many callbacks queued. */
2078 		if (!irqs_disabled_flags(flags)) {
2079 			wake_nocb_leader(rdp, true);
2080 			trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
2081 					    TPS("WakeOvf"));
2082 		} else {
2083 			wake_nocb_leader_defer(rdp, RCU_NOCB_WAKE_FORCE,
2084 					       TPS("WakeOvfIsDeferred"));
2085 		}
2086 		rdp->qlen_last_fqs_check = LONG_MAX / 2;
2087 	} else {
2088 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
2089 	}
2090 	return;
2091 }
2092 
2093 /*
2094  * This is a helper for __call_rcu(), which invokes this when the normal
2095  * callback queue is inoperable.  If this is not a no-CBs CPU, this
2096  * function returns failure back to __call_rcu(), which can complain
2097  * appropriately.
2098  *
2099  * Otherwise, this function queues the callback where the corresponding
2100  * "rcuo" kthread can find it.
2101  */
2102 static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
2103 			    bool lazy, unsigned long flags)
2104 {
2105 
2106 	if (!rcu_is_nocb_cpu(rdp->cpu))
2107 		return false;
2108 	__call_rcu_nocb_enqueue(rdp, rhp, &rhp->next, 1, lazy, flags);
2109 	if (__is_kfree_rcu_offset((unsigned long)rhp->func))
2110 		trace_rcu_kfree_callback(rcu_state.name, rhp,
2111 					 (unsigned long)rhp->func,
2112 					 -atomic_long_read(&rdp->nocb_q_count_lazy),
2113 					 -rcu_get_n_cbs_nocb_cpu(rdp));
2114 	else
2115 		trace_rcu_callback(rcu_state.name, rhp,
2116 				   -atomic_long_read(&rdp->nocb_q_count_lazy),
2117 				   -rcu_get_n_cbs_nocb_cpu(rdp));
2118 
2119 	/*
2120 	 * If called from an extended quiescent state with interrupts
2121 	 * disabled, invoke the RCU core in order to allow the idle-entry
2122 	 * deferred-wakeup check to function.
2123 	 */
2124 	if (irqs_disabled_flags(flags) &&
2125 	    !rcu_is_watching() &&
2126 	    cpu_online(smp_processor_id()))
2127 		invoke_rcu_core();
2128 
2129 	return true;
2130 }
2131 
2132 /*
2133  * Adopt orphaned callbacks on a no-CBs CPU, or return 0 if this is
2134  * not a no-CBs CPU.
2135  */
2136 static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp,
2137 						     struct rcu_data *rdp,
2138 						     unsigned long flags)
2139 {
2140 	lockdep_assert_irqs_disabled();
2141 	if (!rcu_is_nocb_cpu(smp_processor_id()))
2142 		return false; /* Not NOCBs CPU, caller must migrate CBs. */
2143 	__call_rcu_nocb_enqueue(my_rdp, rcu_segcblist_head(&rdp->cblist),
2144 				rcu_segcblist_tail(&rdp->cblist),
2145 				rcu_segcblist_n_cbs(&rdp->cblist),
2146 				rcu_segcblist_n_lazy_cbs(&rdp->cblist), flags);
2147 	rcu_segcblist_init(&rdp->cblist);
2148 	rcu_segcblist_disable(&rdp->cblist);
2149 	return true;
2150 }
2151 
2152 /*
2153  * If necessary, kick off a new grace period, and either way wait
2154  * for a subsequent grace period to complete.
2155  */
2156 static void rcu_nocb_wait_gp(struct rcu_data *rdp)
2157 {
2158 	unsigned long c;
2159 	bool d;
2160 	unsigned long flags;
2161 	bool needwake;
2162 	struct rcu_node *rnp = rdp->mynode;
2163 
2164 	local_irq_save(flags);
2165 	c = rcu_seq_snap(&rcu_state.gp_seq);
2166 	if (!rdp->gpwrap && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
2167 		local_irq_restore(flags);
2168 	} else {
2169 		raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
2170 		needwake = rcu_start_this_gp(rnp, rdp, c);
2171 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2172 		if (needwake)
2173 			rcu_gp_kthread_wake();
2174 	}
2175 
2176 	/*
2177 	 * Wait for the grace period.  Do so interruptibly to avoid messing
2178 	 * up the load average.
2179 	 */
2180 	trace_rcu_this_gp(rnp, rdp, c, TPS("StartWait"));
2181 	for (;;) {
2182 		swait_event_interruptible_exclusive(
2183 			rnp->nocb_gp_wq[rcu_seq_ctr(c) & 0x1],
2184 			(d = rcu_seq_done(&rnp->gp_seq, c)));
2185 		if (likely(d))
2186 			break;
2187 		WARN_ON(signal_pending(current));
2188 		trace_rcu_this_gp(rnp, rdp, c, TPS("ResumeWait"));
2189 	}
2190 	trace_rcu_this_gp(rnp, rdp, c, TPS("EndWait"));
2191 	smp_mb(); /* Ensure that CB invocation happens after GP end. */
2192 }
2193 
2194 /*
2195  * Leaders come here to wait for additional callbacks to show up.
2196  * This function does not return until callbacks appear.
2197  */
2198 static void nocb_leader_wait(struct rcu_data *my_rdp)
2199 {
2200 	bool firsttime = true;
2201 	unsigned long flags;
2202 	bool gotcbs;
2203 	struct rcu_data *rdp;
2204 	struct rcu_head **tail;
2205 
2206 wait_again:
2207 
2208 	/* Wait for callbacks to appear. */
2209 	if (!rcu_nocb_poll) {
2210 		trace_rcu_nocb_wake(rcu_state.name, my_rdp->cpu, TPS("Sleep"));
2211 		swait_event_interruptible_exclusive(my_rdp->nocb_wq,
2212 				!READ_ONCE(my_rdp->nocb_leader_sleep));
2213 		raw_spin_lock_irqsave(&my_rdp->nocb_lock, flags);
2214 		my_rdp->nocb_leader_sleep = true;
2215 		WRITE_ONCE(my_rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
2216 		del_timer(&my_rdp->nocb_timer);
2217 		raw_spin_unlock_irqrestore(&my_rdp->nocb_lock, flags);
2218 	} else if (firsttime) {
2219 		firsttime = false; /* Don't drown trace log with "Poll"! */
2220 		trace_rcu_nocb_wake(rcu_state.name, my_rdp->cpu, TPS("Poll"));
2221 	}
2222 
2223 	/*
2224 	 * Each pass through the following loop checks a follower for CBs.
2225 	 * We are our own first follower.  Any CBs found are moved to
2226 	 * nocb_gp_head, where they await a grace period.
2227 	 */
2228 	gotcbs = false;
2229 	smp_mb(); /* wakeup and _sleep before ->nocb_head reads. */
2230 	for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
2231 		rdp->nocb_gp_head = READ_ONCE(rdp->nocb_head);
2232 		if (!rdp->nocb_gp_head)
2233 			continue;  /* No CBs here, try next follower. */
2234 
2235 		/* Move callbacks to wait-for-GP list, which is empty. */
2236 		WRITE_ONCE(rdp->nocb_head, NULL);
2237 		rdp->nocb_gp_tail = xchg(&rdp->nocb_tail, &rdp->nocb_head);
2238 		gotcbs = true;
2239 	}
2240 
2241 	/* No callbacks?  Sleep a bit if polling, and go retry.  */
2242 	if (unlikely(!gotcbs)) {
2243 		WARN_ON(signal_pending(current));
2244 		if (rcu_nocb_poll) {
2245 			schedule_timeout_interruptible(1);
2246 		} else {
2247 			trace_rcu_nocb_wake(rcu_state.name, my_rdp->cpu,
2248 					    TPS("WokeEmpty"));
2249 		}
2250 		goto wait_again;
2251 	}
2252 
2253 	/* Wait for one grace period. */
2254 	rcu_nocb_wait_gp(my_rdp);
2255 
2256 	/* Each pass through the following loop wakes a follower, if needed. */
2257 	for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
2258 		if (!rcu_nocb_poll &&
2259 		    READ_ONCE(rdp->nocb_head) &&
2260 		    READ_ONCE(my_rdp->nocb_leader_sleep)) {
2261 			raw_spin_lock_irqsave(&my_rdp->nocb_lock, flags);
2262 			my_rdp->nocb_leader_sleep = false;/* No need to sleep.*/
2263 			raw_spin_unlock_irqrestore(&my_rdp->nocb_lock, flags);
2264 		}
2265 		if (!rdp->nocb_gp_head)
2266 			continue; /* No CBs, so no need to wake follower. */
2267 
2268 		/* Append callbacks to follower's "done" list. */
2269 		raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
2270 		tail = rdp->nocb_follower_tail;
2271 		rdp->nocb_follower_tail = rdp->nocb_gp_tail;
2272 		*tail = rdp->nocb_gp_head;
2273 		raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
2274 		if (rdp != my_rdp && tail == &rdp->nocb_follower_head) {
2275 			/* List was empty, so wake up the follower.  */
2276 			swake_up_one(&rdp->nocb_wq);
2277 		}
2278 	}
2279 
2280 	/* If we (the leader) don't have CBs, go wait some more. */
2281 	if (!my_rdp->nocb_follower_head)
2282 		goto wait_again;
2283 }
2284 
2285 /*
2286  * Followers come here to wait for additional callbacks to show up.
2287  * This function does not return until callbacks appear.
2288  */
2289 static void nocb_follower_wait(struct rcu_data *rdp)
2290 {
2291 	for (;;) {
2292 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("FollowerSleep"));
2293 		swait_event_interruptible_exclusive(rdp->nocb_wq,
2294 					 READ_ONCE(rdp->nocb_follower_head));
2295 		if (smp_load_acquire(&rdp->nocb_follower_head)) {
2296 			/* ^^^ Ensure CB invocation follows _head test. */
2297 			return;
2298 		}
2299 		WARN_ON(signal_pending(current));
2300 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeEmpty"));
2301 	}
2302 }
2303 
2304 /*
2305  * Per-rcu_data kthread, but only for no-CBs CPUs.  Each kthread invokes
2306  * callbacks queued by the corresponding no-CBs CPU, however, there is
2307  * an optional leader-follower relationship so that the grace-period
2308  * kthreads don't have to do quite so many wakeups.
2309  */
2310 static int rcu_nocb_kthread(void *arg)
2311 {
2312 	int c, cl;
2313 	unsigned long flags;
2314 	struct rcu_head *list;
2315 	struct rcu_head *next;
2316 	struct rcu_head **tail;
2317 	struct rcu_data *rdp = arg;
2318 
2319 	/* Each pass through this loop invokes one batch of callbacks */
2320 	for (;;) {
2321 		/* Wait for callbacks. */
2322 		if (rdp->nocb_leader == rdp)
2323 			nocb_leader_wait(rdp);
2324 		else
2325 			nocb_follower_wait(rdp);
2326 
2327 		/* Pull the ready-to-invoke callbacks onto local list. */
2328 		raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
2329 		list = rdp->nocb_follower_head;
2330 		rdp->nocb_follower_head = NULL;
2331 		tail = rdp->nocb_follower_tail;
2332 		rdp->nocb_follower_tail = &rdp->nocb_follower_head;
2333 		raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
2334 		if (WARN_ON_ONCE(!list))
2335 			continue;
2336 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeNonEmpty"));
2337 
2338 		/* Each pass through the following loop invokes a callback. */
2339 		trace_rcu_batch_start(rcu_state.name,
2340 				      atomic_long_read(&rdp->nocb_q_count_lazy),
2341 				      rcu_get_n_cbs_nocb_cpu(rdp), -1);
2342 		c = cl = 0;
2343 		while (list) {
2344 			next = list->next;
2345 			/* Wait for enqueuing to complete, if needed. */
2346 			while (next == NULL && &list->next != tail) {
2347 				trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
2348 						    TPS("WaitQueue"));
2349 				schedule_timeout_interruptible(1);
2350 				trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
2351 						    TPS("WokeQueue"));
2352 				next = list->next;
2353 			}
2354 			debug_rcu_head_unqueue(list);
2355 			local_bh_disable();
2356 			if (__rcu_reclaim(rcu_state.name, list))
2357 				cl++;
2358 			c++;
2359 			local_bh_enable();
2360 			cond_resched_tasks_rcu_qs();
2361 			list = next;
2362 		}
2363 		trace_rcu_batch_end(rcu_state.name, c, !!list, 0, 0, 1);
2364 		smp_mb__before_atomic();  /* _add after CB invocation. */
2365 		atomic_long_add(-c, &rdp->nocb_q_count);
2366 		atomic_long_add(-cl, &rdp->nocb_q_count_lazy);
2367 	}
2368 	return 0;
2369 }
2370 
2371 /* Is a deferred wakeup of rcu_nocb_kthread() required? */
2372 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
2373 {
2374 	return READ_ONCE(rdp->nocb_defer_wakeup);
2375 }
2376 
2377 /* Do a deferred wakeup of rcu_nocb_kthread(). */
2378 static void do_nocb_deferred_wakeup_common(struct rcu_data *rdp)
2379 {
2380 	unsigned long flags;
2381 	int ndw;
2382 
2383 	raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
2384 	if (!rcu_nocb_need_deferred_wakeup(rdp)) {
2385 		raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
2386 		return;
2387 	}
2388 	ndw = READ_ONCE(rdp->nocb_defer_wakeup);
2389 	WRITE_ONCE(rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
2390 	__wake_nocb_leader(rdp, ndw == RCU_NOCB_WAKE_FORCE, flags);
2391 	trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DeferredWake"));
2392 }
2393 
2394 /* Do a deferred wakeup of rcu_nocb_kthread() from a timer handler. */
2395 static void do_nocb_deferred_wakeup_timer(struct timer_list *t)
2396 {
2397 	struct rcu_data *rdp = from_timer(rdp, t, nocb_timer);
2398 
2399 	do_nocb_deferred_wakeup_common(rdp);
2400 }
2401 
2402 /*
2403  * Do a deferred wakeup of rcu_nocb_kthread() from fastpath.
2404  * This means we do an inexact common-case check.  Note that if
2405  * we miss, ->nocb_timer will eventually clean things up.
2406  */
2407 static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
2408 {
2409 	if (rcu_nocb_need_deferred_wakeup(rdp))
2410 		do_nocb_deferred_wakeup_common(rdp);
2411 }
2412 
2413 void __init rcu_init_nohz(void)
2414 {
2415 	int cpu;
2416 	bool need_rcu_nocb_mask = false;
2417 
2418 #if defined(CONFIG_NO_HZ_FULL)
2419 	if (tick_nohz_full_running && cpumask_weight(tick_nohz_full_mask))
2420 		need_rcu_nocb_mask = true;
2421 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
2422 
2423 	if (!cpumask_available(rcu_nocb_mask) && need_rcu_nocb_mask) {
2424 		if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
2425 			pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
2426 			return;
2427 		}
2428 	}
2429 	if (!cpumask_available(rcu_nocb_mask))
2430 		return;
2431 
2432 #if defined(CONFIG_NO_HZ_FULL)
2433 	if (tick_nohz_full_running)
2434 		cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
2435 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
2436 
2437 	if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
2438 		pr_info("\tNote: kernel parameter 'rcu_nocbs=', 'nohz_full', or 'isolcpus=' contains nonexistent CPUs.\n");
2439 		cpumask_and(rcu_nocb_mask, cpu_possible_mask,
2440 			    rcu_nocb_mask);
2441 	}
2442 	if (cpumask_empty(rcu_nocb_mask))
2443 		pr_info("\tOffload RCU callbacks from CPUs: (none).\n");
2444 	else
2445 		pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
2446 			cpumask_pr_args(rcu_nocb_mask));
2447 	if (rcu_nocb_poll)
2448 		pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
2449 
2450 	for_each_cpu(cpu, rcu_nocb_mask)
2451 		init_nocb_callback_list(per_cpu_ptr(&rcu_data, cpu));
2452 	rcu_organize_nocb_kthreads();
2453 }
2454 
2455 /* Initialize per-rcu_data variables for no-CBs CPUs. */
2456 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
2457 {
2458 	rdp->nocb_tail = &rdp->nocb_head;
2459 	init_swait_queue_head(&rdp->nocb_wq);
2460 	rdp->nocb_follower_tail = &rdp->nocb_follower_head;
2461 	raw_spin_lock_init(&rdp->nocb_lock);
2462 	timer_setup(&rdp->nocb_timer, do_nocb_deferred_wakeup_timer, 0);
2463 }
2464 
2465 /*
2466  * If the specified CPU is a no-CBs CPU that does not already have its
2467  * rcuo kthread, spawn it.  If the CPUs are brought online out of order,
2468  * this can require re-organizing the leader-follower relationships.
2469  */
2470 static void rcu_spawn_one_nocb_kthread(int cpu)
2471 {
2472 	struct rcu_data *rdp;
2473 	struct rcu_data *rdp_last;
2474 	struct rcu_data *rdp_old_leader;
2475 	struct rcu_data *rdp_spawn = per_cpu_ptr(&rcu_data, cpu);
2476 	struct task_struct *t;
2477 
2478 	/*
2479 	 * If this isn't a no-CBs CPU or if it already has an rcuo kthread,
2480 	 * then nothing to do.
2481 	 */
2482 	if (!rcu_is_nocb_cpu(cpu) || rdp_spawn->nocb_kthread)
2483 		return;
2484 
2485 	/* If we didn't spawn the leader first, reorganize! */
2486 	rdp_old_leader = rdp_spawn->nocb_leader;
2487 	if (rdp_old_leader != rdp_spawn && !rdp_old_leader->nocb_kthread) {
2488 		rdp_last = NULL;
2489 		rdp = rdp_old_leader;
2490 		do {
2491 			rdp->nocb_leader = rdp_spawn;
2492 			if (rdp_last && rdp != rdp_spawn)
2493 				rdp_last->nocb_next_follower = rdp;
2494 			if (rdp == rdp_spawn) {
2495 				rdp = rdp->nocb_next_follower;
2496 			} else {
2497 				rdp_last = rdp;
2498 				rdp = rdp->nocb_next_follower;
2499 				rdp_last->nocb_next_follower = NULL;
2500 			}
2501 		} while (rdp);
2502 		rdp_spawn->nocb_next_follower = rdp_old_leader;
2503 	}
2504 
2505 	/* Spawn the kthread for this CPU. */
2506 	t = kthread_run(rcu_nocb_kthread, rdp_spawn,
2507 			"rcuo%c/%d", rcu_state.abbr, cpu);
2508 	if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo kthread, OOM is now expected behavior\n", __func__))
2509 		return;
2510 	WRITE_ONCE(rdp_spawn->nocb_kthread, t);
2511 }
2512 
2513 /*
2514  * If the specified CPU is a no-CBs CPU that does not already have its
2515  * rcuo kthread, spawn it.
2516  */
2517 static void rcu_spawn_cpu_nocb_kthread(int cpu)
2518 {
2519 	if (rcu_scheduler_fully_active)
2520 		rcu_spawn_one_nocb_kthread(cpu);
2521 }
2522 
2523 /*
2524  * Once the scheduler is running, spawn rcuo kthreads for all online
2525  * no-CBs CPUs.  This assumes that the early_initcall()s happen before
2526  * non-boot CPUs come online -- if this changes, we will need to add
2527  * some mutual exclusion.
2528  */
2529 static void __init rcu_spawn_nocb_kthreads(void)
2530 {
2531 	int cpu;
2532 
2533 	for_each_online_cpu(cpu)
2534 		rcu_spawn_cpu_nocb_kthread(cpu);
2535 }
2536 
2537 /* How many follower CPU IDs per leader?  Default of -1 for sqrt(nr_cpu_ids). */
2538 static int rcu_nocb_leader_stride = -1;
2539 module_param(rcu_nocb_leader_stride, int, 0444);
2540 
2541 /*
2542  * Initialize leader-follower relationships for all no-CBs CPU.
2543  */
2544 static void __init rcu_organize_nocb_kthreads(void)
2545 {
2546 	int cpu;
2547 	int ls = rcu_nocb_leader_stride;
2548 	int nl = 0;  /* Next leader. */
2549 	struct rcu_data *rdp;
2550 	struct rcu_data *rdp_leader = NULL;  /* Suppress misguided gcc warn. */
2551 	struct rcu_data *rdp_prev = NULL;
2552 
2553 	if (!cpumask_available(rcu_nocb_mask))
2554 		return;
2555 	if (ls == -1) {
2556 		ls = int_sqrt(nr_cpu_ids);
2557 		rcu_nocb_leader_stride = ls;
2558 	}
2559 
2560 	/*
2561 	 * Each pass through this loop sets up one rcu_data structure.
2562 	 * Should the corresponding CPU come online in the future, then
2563 	 * we will spawn the needed set of rcu_nocb_kthread() kthreads.
2564 	 */
2565 	for_each_cpu(cpu, rcu_nocb_mask) {
2566 		rdp = per_cpu_ptr(&rcu_data, cpu);
2567 		if (rdp->cpu >= nl) {
2568 			/* New leader, set up for followers & next leader. */
2569 			nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
2570 			rdp->nocb_leader = rdp;
2571 			rdp_leader = rdp;
2572 		} else {
2573 			/* Another follower, link to previous leader. */
2574 			rdp->nocb_leader = rdp_leader;
2575 			rdp_prev->nocb_next_follower = rdp;
2576 		}
2577 		rdp_prev = rdp;
2578 	}
2579 }
2580 
2581 /* Prevent __call_rcu() from enqueuing callbacks on no-CBs CPUs */
2582 static bool init_nocb_callback_list(struct rcu_data *rdp)
2583 {
2584 	if (!rcu_is_nocb_cpu(rdp->cpu))
2585 		return false;
2586 
2587 	/* If there are early-boot callbacks, move them to nocb lists. */
2588 	if (!rcu_segcblist_empty(&rdp->cblist)) {
2589 		rdp->nocb_head = rcu_segcblist_head(&rdp->cblist);
2590 		rdp->nocb_tail = rcu_segcblist_tail(&rdp->cblist);
2591 		atomic_long_set(&rdp->nocb_q_count,
2592 				rcu_segcblist_n_cbs(&rdp->cblist));
2593 		atomic_long_set(&rdp->nocb_q_count_lazy,
2594 				rcu_segcblist_n_lazy_cbs(&rdp->cblist));
2595 		rcu_segcblist_init(&rdp->cblist);
2596 	}
2597 	rcu_segcblist_disable(&rdp->cblist);
2598 	return true;
2599 }
2600 
2601 /*
2602  * Bind the current task to the offloaded CPUs.  If there are no offloaded
2603  * CPUs, leave the task unbound.  Splat if the bind attempt fails.
2604  */
2605 void rcu_bind_current_to_nocb(void)
2606 {
2607 	if (cpumask_available(rcu_nocb_mask) && cpumask_weight(rcu_nocb_mask))
2608 		WARN_ON(sched_setaffinity(current->pid, rcu_nocb_mask));
2609 }
2610 EXPORT_SYMBOL_GPL(rcu_bind_current_to_nocb);
2611 
2612 /*
2613  * Return the number of RCU callbacks still queued from the specified
2614  * CPU, which must be a nocbs CPU.
2615  */
2616 static unsigned long rcu_get_n_cbs_nocb_cpu(struct rcu_data *rdp)
2617 {
2618 	return atomic_long_read(&rdp->nocb_q_count);
2619 }
2620 
2621 #else /* #ifdef CONFIG_RCU_NOCB_CPU */
2622 
2623 static bool rcu_nocb_cpu_needs_barrier(int cpu)
2624 {
2625 	WARN_ON_ONCE(1); /* Should be dead code. */
2626 	return false;
2627 }
2628 
2629 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
2630 {
2631 }
2632 
2633 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
2634 {
2635 	return NULL;
2636 }
2637 
2638 static void rcu_init_one_nocb(struct rcu_node *rnp)
2639 {
2640 }
2641 
2642 static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
2643 			    bool lazy, unsigned long flags)
2644 {
2645 	return false;
2646 }
2647 
2648 static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp,
2649 						     struct rcu_data *rdp,
2650 						     unsigned long flags)
2651 {
2652 	return false;
2653 }
2654 
2655 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
2656 {
2657 }
2658 
2659 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
2660 {
2661 	return false;
2662 }
2663 
2664 static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
2665 {
2666 }
2667 
2668 static void rcu_spawn_cpu_nocb_kthread(int cpu)
2669 {
2670 }
2671 
2672 static void __init rcu_spawn_nocb_kthreads(void)
2673 {
2674 }
2675 
2676 static bool init_nocb_callback_list(struct rcu_data *rdp)
2677 {
2678 	return false;
2679 }
2680 
2681 static unsigned long rcu_get_n_cbs_nocb_cpu(struct rcu_data *rdp)
2682 {
2683 	return 0;
2684 }
2685 
2686 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
2687 
2688 /*
2689  * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
2690  * grace-period kthread will do force_quiescent_state() processing?
2691  * The idea is to avoid waking up RCU core processing on such a
2692  * CPU unless the grace period has extended for too long.
2693  *
2694  * This code relies on the fact that all NO_HZ_FULL CPUs are also
2695  * CONFIG_RCU_NOCB_CPU CPUs.
2696  */
2697 static bool rcu_nohz_full_cpu(void)
2698 {
2699 #ifdef CONFIG_NO_HZ_FULL
2700 	if (tick_nohz_full_cpu(smp_processor_id()) &&
2701 	    (!rcu_gp_in_progress() ||
2702 	     ULONG_CMP_LT(jiffies, READ_ONCE(rcu_state.gp_start) + HZ)))
2703 		return true;
2704 #endif /* #ifdef CONFIG_NO_HZ_FULL */
2705 	return false;
2706 }
2707 
2708 /*
2709  * Bind the RCU grace-period kthreads to the housekeeping CPU.
2710  */
2711 static void rcu_bind_gp_kthread(void)
2712 {
2713 	if (!tick_nohz_full_enabled())
2714 		return;
2715 	housekeeping_affine(current, HK_FLAG_RCU);
2716 }
2717 
2718 /* Record the current task on dyntick-idle entry. */
2719 static void rcu_dynticks_task_enter(void)
2720 {
2721 #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
2722 	WRITE_ONCE(current->rcu_tasks_idle_cpu, smp_processor_id());
2723 #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
2724 }
2725 
2726 /* Record no current task on dyntick-idle exit. */
2727 static void rcu_dynticks_task_exit(void)
2728 {
2729 #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
2730 	WRITE_ONCE(current->rcu_tasks_idle_cpu, -1);
2731 #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
2732 }
2733