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