xref: /openbmc/linux/kernel/rcu/tree_nocb.h (revision 2ae1beb3)
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  * Copyright SUSE, 2021
10  *
11  * Author: Ingo Molnar <mingo@elte.hu>
12  *	   Paul E. McKenney <paulmck@linux.ibm.com>
13  *	   Frederic Weisbecker <frederic@kernel.org>
14  */
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 static inline int rcu_lockdep_is_held_nocb(struct rcu_data *rdp)
20 {
21 	return lockdep_is_held(&rdp->nocb_lock);
22 }
23 
24 static inline bool rcu_current_is_nocb_kthread(struct rcu_data *rdp)
25 {
26 	/* Race on early boot between thread creation and assignment */
27 	if (!rdp->nocb_cb_kthread || !rdp->nocb_gp_kthread)
28 		return true;
29 
30 	if (current == rdp->nocb_cb_kthread || current == rdp->nocb_gp_kthread)
31 		if (in_task())
32 			return true;
33 	return false;
34 }
35 
36 /*
37  * Offload callback processing from the boot-time-specified set of CPUs
38  * specified by rcu_nocb_mask.  For the CPUs in the set, there are kthreads
39  * created that pull the callbacks from the corresponding CPU, wait for
40  * a grace period to elapse, and invoke the callbacks.  These kthreads
41  * are organized into GP kthreads, which manage incoming callbacks, wait for
42  * grace periods, and awaken CB kthreads, and the CB kthreads, which only
43  * invoke callbacks.  Each GP kthread invokes its own CBs.  The no-CBs CPUs
44  * do a wake_up() on their GP kthread when they insert a callback into any
45  * empty list, unless the rcu_nocb_poll boot parameter has been specified,
46  * in which case each kthread actively polls its CPU.  (Which isn't so great
47  * for energy efficiency, but which does reduce RCU's overhead on that CPU.)
48  *
49  * This is intended to be used in conjunction with Frederic Weisbecker's
50  * adaptive-idle work, which would seriously reduce OS jitter on CPUs
51  * running CPU-bound user-mode computations.
52  *
53  * Offloading of callbacks can also be used as an energy-efficiency
54  * measure because CPUs with no RCU callbacks queued are more aggressive
55  * about entering dyntick-idle mode.
56  */
57 
58 
59 /*
60  * Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters.
61  * If the list is invalid, a warning is emitted and all CPUs are offloaded.
62  */
63 static int __init rcu_nocb_setup(char *str)
64 {
65 	alloc_bootmem_cpumask_var(&rcu_nocb_mask);
66 	if (*str == '=') {
67 		if (cpulist_parse(++str, rcu_nocb_mask)) {
68 			pr_warn("rcu_nocbs= bad CPU range, all CPUs set\n");
69 			cpumask_setall(rcu_nocb_mask);
70 		}
71 	}
72 	rcu_state.nocb_is_setup = true;
73 	return 1;
74 }
75 __setup("rcu_nocbs", rcu_nocb_setup);
76 
77 static int __init parse_rcu_nocb_poll(char *arg)
78 {
79 	rcu_nocb_poll = true;
80 	return 1;
81 }
82 __setup("rcu_nocb_poll", parse_rcu_nocb_poll);
83 
84 /*
85  * Don't bother bypassing ->cblist if the call_rcu() rate is low.
86  * After all, the main point of bypassing is to avoid lock contention
87  * on ->nocb_lock, which only can happen at high call_rcu() rates.
88  */
89 static int nocb_nobypass_lim_per_jiffy = 16 * 1000 / HZ;
90 module_param(nocb_nobypass_lim_per_jiffy, int, 0);
91 
92 /*
93  * Acquire the specified rcu_data structure's ->nocb_bypass_lock.  If the
94  * lock isn't immediately available, perform minimal sanity check.
95  */
96 static void rcu_nocb_bypass_lock(struct rcu_data *rdp)
97 	__acquires(&rdp->nocb_bypass_lock)
98 {
99 	lockdep_assert_irqs_disabled();
100 	if (raw_spin_trylock(&rdp->nocb_bypass_lock))
101 		return;
102 	/*
103 	 * Contention expected only when local enqueue collide with
104 	 * remote flush from kthreads.
105 	 */
106 	WARN_ON_ONCE(smp_processor_id() != rdp->cpu);
107 	raw_spin_lock(&rdp->nocb_bypass_lock);
108 }
109 
110 /*
111  * Conditionally acquire the specified rcu_data structure's
112  * ->nocb_bypass_lock.
113  */
114 static bool rcu_nocb_bypass_trylock(struct rcu_data *rdp)
115 {
116 	lockdep_assert_irqs_disabled();
117 	return raw_spin_trylock(&rdp->nocb_bypass_lock);
118 }
119 
120 /*
121  * Release the specified rcu_data structure's ->nocb_bypass_lock.
122  */
123 static void rcu_nocb_bypass_unlock(struct rcu_data *rdp)
124 	__releases(&rdp->nocb_bypass_lock)
125 {
126 	lockdep_assert_irqs_disabled();
127 	raw_spin_unlock(&rdp->nocb_bypass_lock);
128 }
129 
130 /*
131  * Acquire the specified rcu_data structure's ->nocb_lock, but only
132  * if it corresponds to a no-CBs CPU.
133  */
134 static void rcu_nocb_lock(struct rcu_data *rdp)
135 {
136 	lockdep_assert_irqs_disabled();
137 	if (!rcu_rdp_is_offloaded(rdp))
138 		return;
139 	raw_spin_lock(&rdp->nocb_lock);
140 }
141 
142 /*
143  * Release the specified rcu_data structure's ->nocb_lock, but only
144  * if it corresponds to a no-CBs CPU.
145  */
146 static void rcu_nocb_unlock(struct rcu_data *rdp)
147 {
148 	if (rcu_rdp_is_offloaded(rdp)) {
149 		lockdep_assert_irqs_disabled();
150 		raw_spin_unlock(&rdp->nocb_lock);
151 	}
152 }
153 
154 /*
155  * Release the specified rcu_data structure's ->nocb_lock and restore
156  * interrupts, but only if it corresponds to a no-CBs CPU.
157  */
158 static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
159 				       unsigned long flags)
160 {
161 	if (rcu_rdp_is_offloaded(rdp)) {
162 		lockdep_assert_irqs_disabled();
163 		raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
164 	} else {
165 		local_irq_restore(flags);
166 	}
167 }
168 
169 /* Lockdep check that ->cblist may be safely accessed. */
170 static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp)
171 {
172 	lockdep_assert_irqs_disabled();
173 	if (rcu_rdp_is_offloaded(rdp))
174 		lockdep_assert_held(&rdp->nocb_lock);
175 }
176 
177 /*
178  * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
179  * grace period.
180  */
181 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
182 {
183 	swake_up_all(sq);
184 }
185 
186 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
187 {
188 	return &rnp->nocb_gp_wq[rcu_seq_ctr(rnp->gp_seq) & 0x1];
189 }
190 
191 static void rcu_init_one_nocb(struct rcu_node *rnp)
192 {
193 	init_swait_queue_head(&rnp->nocb_gp_wq[0]);
194 	init_swait_queue_head(&rnp->nocb_gp_wq[1]);
195 }
196 
197 static bool __wake_nocb_gp(struct rcu_data *rdp_gp,
198 			   struct rcu_data *rdp,
199 			   bool force, unsigned long flags)
200 	__releases(rdp_gp->nocb_gp_lock)
201 {
202 	bool needwake = false;
203 
204 	if (!READ_ONCE(rdp_gp->nocb_gp_kthread)) {
205 		raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
206 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
207 				    TPS("AlreadyAwake"));
208 		return false;
209 	}
210 
211 	if (rdp_gp->nocb_defer_wakeup > RCU_NOCB_WAKE_NOT) {
212 		WRITE_ONCE(rdp_gp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
213 		del_timer(&rdp_gp->nocb_timer);
214 	}
215 
216 	if (force || READ_ONCE(rdp_gp->nocb_gp_sleep)) {
217 		WRITE_ONCE(rdp_gp->nocb_gp_sleep, false);
218 		needwake = true;
219 	}
220 	raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
221 	if (needwake) {
222 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DoWake"));
223 		wake_up_process(rdp_gp->nocb_gp_kthread);
224 	}
225 
226 	return needwake;
227 }
228 
229 /*
230  * Kick the GP kthread for this NOCB group.
231  */
232 static bool wake_nocb_gp(struct rcu_data *rdp, bool force)
233 {
234 	unsigned long flags;
235 	struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
236 
237 	raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
238 	return __wake_nocb_gp(rdp_gp, rdp, force, flags);
239 }
240 
241 /*
242  * LAZY_FLUSH_JIFFIES decides the maximum amount of time that
243  * can elapse before lazy callbacks are flushed. Lazy callbacks
244  * could be flushed much earlier for a number of other reasons
245  * however, LAZY_FLUSH_JIFFIES will ensure no lazy callbacks are
246  * left unsubmitted to RCU after those many jiffies.
247  */
248 #define LAZY_FLUSH_JIFFIES (10 * HZ)
249 static unsigned long jiffies_till_flush = LAZY_FLUSH_JIFFIES;
250 
251 #ifdef CONFIG_RCU_LAZY
252 // To be called only from test code.
253 void rcu_lazy_set_jiffies_till_flush(unsigned long jif)
254 {
255 	jiffies_till_flush = jif;
256 }
257 EXPORT_SYMBOL(rcu_lazy_set_jiffies_till_flush);
258 
259 unsigned long rcu_lazy_get_jiffies_till_flush(void)
260 {
261 	return jiffies_till_flush;
262 }
263 EXPORT_SYMBOL(rcu_lazy_get_jiffies_till_flush);
264 #endif
265 
266 /*
267  * Arrange to wake the GP kthread for this NOCB group at some future
268  * time when it is safe to do so.
269  */
270 static void wake_nocb_gp_defer(struct rcu_data *rdp, int waketype,
271 			       const char *reason)
272 {
273 	unsigned long flags;
274 	struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
275 
276 	raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
277 
278 	/*
279 	 * Bypass wakeup overrides previous deferments. In case of
280 	 * callback storms, no need to wake up too early.
281 	 */
282 	if (waketype == RCU_NOCB_WAKE_LAZY &&
283 	    rdp->nocb_defer_wakeup == RCU_NOCB_WAKE_NOT) {
284 		mod_timer(&rdp_gp->nocb_timer, jiffies + jiffies_till_flush);
285 		WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
286 	} else if (waketype == RCU_NOCB_WAKE_BYPASS) {
287 		mod_timer(&rdp_gp->nocb_timer, jiffies + 2);
288 		WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
289 	} else {
290 		if (rdp_gp->nocb_defer_wakeup < RCU_NOCB_WAKE)
291 			mod_timer(&rdp_gp->nocb_timer, jiffies + 1);
292 		if (rdp_gp->nocb_defer_wakeup < waketype)
293 			WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
294 	}
295 
296 	raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
297 
298 	trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, reason);
299 }
300 
301 /*
302  * Flush the ->nocb_bypass queue into ->cblist, enqueuing rhp if non-NULL.
303  * However, if there is a callback to be enqueued and if ->nocb_bypass
304  * proves to be initially empty, just return false because the no-CB GP
305  * kthread may need to be awakened in this case.
306  *
307  * Return true if there was something to be flushed and it succeeded, otherwise
308  * false.
309  *
310  * Note that this function always returns true if rhp is NULL.
311  */
312 static bool rcu_nocb_do_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp_in,
313 				     unsigned long j, bool lazy)
314 {
315 	struct rcu_cblist rcl;
316 	struct rcu_head *rhp = rhp_in;
317 
318 	WARN_ON_ONCE(!rcu_rdp_is_offloaded(rdp));
319 	rcu_lockdep_assert_cblist_protected(rdp);
320 	lockdep_assert_held(&rdp->nocb_bypass_lock);
321 	if (rhp && !rcu_cblist_n_cbs(&rdp->nocb_bypass)) {
322 		raw_spin_unlock(&rdp->nocb_bypass_lock);
323 		return false;
324 	}
325 	/* Note: ->cblist.len already accounts for ->nocb_bypass contents. */
326 	if (rhp)
327 		rcu_segcblist_inc_len(&rdp->cblist); /* Must precede enqueue. */
328 
329 	/*
330 	 * If the new CB requested was a lazy one, queue it onto the main
331 	 * ->cblist so that we can take advantage of the grace-period that will
332 	 * happen regardless. But queue it onto the bypass list first so that
333 	 * the lazy CB is ordered with the existing CBs in the bypass list.
334 	 */
335 	if (lazy && rhp) {
336 		rcu_cblist_enqueue(&rdp->nocb_bypass, rhp);
337 		rhp = NULL;
338 	}
339 	rcu_cblist_flush_enqueue(&rcl, &rdp->nocb_bypass, rhp);
340 	WRITE_ONCE(rdp->lazy_len, 0);
341 
342 	rcu_segcblist_insert_pend_cbs(&rdp->cblist, &rcl);
343 	WRITE_ONCE(rdp->nocb_bypass_first, j);
344 	rcu_nocb_bypass_unlock(rdp);
345 	return true;
346 }
347 
348 /*
349  * Flush the ->nocb_bypass queue into ->cblist, enqueuing rhp if non-NULL.
350  * However, if there is a callback to be enqueued and if ->nocb_bypass
351  * proves to be initially empty, just return false because the no-CB GP
352  * kthread may need to be awakened in this case.
353  *
354  * Note that this function always returns true if rhp is NULL.
355  */
356 static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
357 				  unsigned long j, bool lazy)
358 {
359 	if (!rcu_rdp_is_offloaded(rdp))
360 		return true;
361 	rcu_lockdep_assert_cblist_protected(rdp);
362 	rcu_nocb_bypass_lock(rdp);
363 	return rcu_nocb_do_flush_bypass(rdp, rhp, j, lazy);
364 }
365 
366 /*
367  * If the ->nocb_bypass_lock is immediately available, flush the
368  * ->nocb_bypass queue into ->cblist.
369  */
370 static void rcu_nocb_try_flush_bypass(struct rcu_data *rdp, unsigned long j)
371 {
372 	rcu_lockdep_assert_cblist_protected(rdp);
373 	if (!rcu_rdp_is_offloaded(rdp) ||
374 	    !rcu_nocb_bypass_trylock(rdp))
375 		return;
376 	WARN_ON_ONCE(!rcu_nocb_do_flush_bypass(rdp, NULL, j, false));
377 }
378 
379 /*
380  * See whether it is appropriate to use the ->nocb_bypass list in order
381  * to control contention on ->nocb_lock.  A limited number of direct
382  * enqueues are permitted into ->cblist per jiffy.  If ->nocb_bypass
383  * is non-empty, further callbacks must be placed into ->nocb_bypass,
384  * otherwise rcu_barrier() breaks.  Use rcu_nocb_flush_bypass() to switch
385  * back to direct use of ->cblist.  However, ->nocb_bypass should not be
386  * used if ->cblist is empty, because otherwise callbacks can be stranded
387  * on ->nocb_bypass because we cannot count on the current CPU ever again
388  * invoking call_rcu().  The general rule is that if ->nocb_bypass is
389  * non-empty, the corresponding no-CBs grace-period kthread must not be
390  * in an indefinite sleep state.
391  *
392  * Finally, it is not permitted to use the bypass during early boot,
393  * as doing so would confuse the auto-initialization code.  Besides
394  * which, there is no point in worrying about lock contention while
395  * there is only one CPU in operation.
396  */
397 static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
398 				bool *was_alldone, unsigned long flags,
399 				bool lazy)
400 {
401 	unsigned long c;
402 	unsigned long cur_gp_seq;
403 	unsigned long j = jiffies;
404 	long ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
405 	bool bypass_is_lazy = (ncbs == READ_ONCE(rdp->lazy_len));
406 
407 	lockdep_assert_irqs_disabled();
408 
409 	// Pure softirq/rcuc based processing: no bypassing, no
410 	// locking.
411 	if (!rcu_rdp_is_offloaded(rdp)) {
412 		*was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
413 		return false;
414 	}
415 
416 	// In the process of (de-)offloading: no bypassing, but
417 	// locking.
418 	if (!rcu_segcblist_completely_offloaded(&rdp->cblist)) {
419 		rcu_nocb_lock(rdp);
420 		*was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
421 		return false; /* Not offloaded, no bypassing. */
422 	}
423 
424 	// Don't use ->nocb_bypass during early boot.
425 	if (rcu_scheduler_active != RCU_SCHEDULER_RUNNING) {
426 		rcu_nocb_lock(rdp);
427 		WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
428 		*was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
429 		return false;
430 	}
431 
432 	// If we have advanced to a new jiffy, reset counts to allow
433 	// moving back from ->nocb_bypass to ->cblist.
434 	if (j == rdp->nocb_nobypass_last) {
435 		c = rdp->nocb_nobypass_count + 1;
436 	} else {
437 		WRITE_ONCE(rdp->nocb_nobypass_last, j);
438 		c = rdp->nocb_nobypass_count - nocb_nobypass_lim_per_jiffy;
439 		if (ULONG_CMP_LT(rdp->nocb_nobypass_count,
440 				 nocb_nobypass_lim_per_jiffy))
441 			c = 0;
442 		else if (c > nocb_nobypass_lim_per_jiffy)
443 			c = nocb_nobypass_lim_per_jiffy;
444 	}
445 	WRITE_ONCE(rdp->nocb_nobypass_count, c);
446 
447 	// If there hasn't yet been all that many ->cblist enqueues
448 	// this jiffy, tell the caller to enqueue onto ->cblist.  But flush
449 	// ->nocb_bypass first.
450 	// Lazy CBs throttle this back and do immediate bypass queuing.
451 	if (rdp->nocb_nobypass_count < nocb_nobypass_lim_per_jiffy && !lazy) {
452 		rcu_nocb_lock(rdp);
453 		*was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
454 		if (*was_alldone)
455 			trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
456 					    TPS("FirstQ"));
457 
458 		WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, j, false));
459 		WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
460 		return false; // Caller must enqueue the callback.
461 	}
462 
463 	// If ->nocb_bypass has been used too long or is too full,
464 	// flush ->nocb_bypass to ->cblist.
465 	if ((ncbs && !bypass_is_lazy && j != READ_ONCE(rdp->nocb_bypass_first)) ||
466 	    (ncbs &&  bypass_is_lazy &&
467 	     (time_after(j, READ_ONCE(rdp->nocb_bypass_first) + jiffies_till_flush))) ||
468 	    ncbs >= qhimark) {
469 		rcu_nocb_lock(rdp);
470 		*was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
471 
472 		if (!rcu_nocb_flush_bypass(rdp, rhp, j, lazy)) {
473 			if (*was_alldone)
474 				trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
475 						    TPS("FirstQ"));
476 			WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
477 			return false; // Caller must enqueue the callback.
478 		}
479 		if (j != rdp->nocb_gp_adv_time &&
480 		    rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
481 		    rcu_seq_done(&rdp->mynode->gp_seq, cur_gp_seq)) {
482 			rcu_advance_cbs_nowake(rdp->mynode, rdp);
483 			rdp->nocb_gp_adv_time = j;
484 		}
485 
486 		// The flush succeeded and we moved CBs into the regular list.
487 		// Don't wait for the wake up timer as it may be too far ahead.
488 		// Wake up the GP thread now instead, if the cblist was empty.
489 		__call_rcu_nocb_wake(rdp, *was_alldone, flags);
490 
491 		return true; // Callback already enqueued.
492 	}
493 
494 	// We need to use the bypass.
495 	rcu_nocb_bypass_lock(rdp);
496 	ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
497 	rcu_segcblist_inc_len(&rdp->cblist); /* Must precede enqueue. */
498 	rcu_cblist_enqueue(&rdp->nocb_bypass, rhp);
499 
500 	if (lazy)
501 		WRITE_ONCE(rdp->lazy_len, rdp->lazy_len + 1);
502 
503 	if (!ncbs) {
504 		WRITE_ONCE(rdp->nocb_bypass_first, j);
505 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("FirstBQ"));
506 	}
507 	rcu_nocb_bypass_unlock(rdp);
508 	smp_mb(); /* Order enqueue before wake. */
509 	// A wake up of the grace period kthread or timer adjustment
510 	// needs to be done only if:
511 	// 1. Bypass list was fully empty before (this is the first
512 	//    bypass list entry), or:
513 	// 2. Both of these conditions are met:
514 	//    a. The bypass list previously had only lazy CBs, and:
515 	//    b. The new CB is non-lazy.
516 	if (ncbs && (!bypass_is_lazy || lazy)) {
517 		local_irq_restore(flags);
518 	} else {
519 		// No-CBs GP kthread might be indefinitely asleep, if so, wake.
520 		rcu_nocb_lock(rdp); // Rare during call_rcu() flood.
521 		if (!rcu_segcblist_pend_cbs(&rdp->cblist)) {
522 			trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
523 					    TPS("FirstBQwake"));
524 			__call_rcu_nocb_wake(rdp, true, flags);
525 		} else {
526 			trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
527 					    TPS("FirstBQnoWake"));
528 			rcu_nocb_unlock_irqrestore(rdp, flags);
529 		}
530 	}
531 	return true; // Callback already enqueued.
532 }
533 
534 /*
535  * Awaken the no-CBs grace-period kthread if needed, either due to it
536  * legitimately being asleep or due to overload conditions.
537  *
538  * If warranted, also wake up the kthread servicing this CPUs queues.
539  */
540 static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone,
541 				 unsigned long flags)
542 				 __releases(rdp->nocb_lock)
543 {
544 	long bypass_len;
545 	unsigned long cur_gp_seq;
546 	unsigned long j;
547 	long lazy_len;
548 	long len;
549 	struct task_struct *t;
550 
551 	// If we are being polled or there is no kthread, just leave.
552 	t = READ_ONCE(rdp->nocb_gp_kthread);
553 	if (rcu_nocb_poll || !t) {
554 		rcu_nocb_unlock_irqrestore(rdp, flags);
555 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
556 				    TPS("WakeNotPoll"));
557 		return;
558 	}
559 	// Need to actually to a wakeup.
560 	len = rcu_segcblist_n_cbs(&rdp->cblist);
561 	bypass_len = rcu_cblist_n_cbs(&rdp->nocb_bypass);
562 	lazy_len = READ_ONCE(rdp->lazy_len);
563 	if (was_alldone) {
564 		rdp->qlen_last_fqs_check = len;
565 		// Only lazy CBs in bypass list
566 		if (lazy_len && bypass_len == lazy_len) {
567 			rcu_nocb_unlock_irqrestore(rdp, flags);
568 			wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE_LAZY,
569 					   TPS("WakeLazy"));
570 		} else if (!irqs_disabled_flags(flags)) {
571 			/* ... if queue was empty ... */
572 			rcu_nocb_unlock_irqrestore(rdp, flags);
573 			wake_nocb_gp(rdp, false);
574 			trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
575 					    TPS("WakeEmpty"));
576 		} else {
577 			rcu_nocb_unlock_irqrestore(rdp, flags);
578 			wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE,
579 					   TPS("WakeEmptyIsDeferred"));
580 		}
581 	} else if (len > rdp->qlen_last_fqs_check + qhimark) {
582 		/* ... or if many callbacks queued. */
583 		rdp->qlen_last_fqs_check = len;
584 		j = jiffies;
585 		if (j != rdp->nocb_gp_adv_time &&
586 		    rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
587 		    rcu_seq_done(&rdp->mynode->gp_seq, cur_gp_seq)) {
588 			rcu_advance_cbs_nowake(rdp->mynode, rdp);
589 			rdp->nocb_gp_adv_time = j;
590 		}
591 		smp_mb(); /* Enqueue before timer_pending(). */
592 		if ((rdp->nocb_cb_sleep ||
593 		     !rcu_segcblist_ready_cbs(&rdp->cblist)) &&
594 		    !timer_pending(&rdp->nocb_timer)) {
595 			rcu_nocb_unlock_irqrestore(rdp, flags);
596 			wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE_FORCE,
597 					   TPS("WakeOvfIsDeferred"));
598 		} else {
599 			rcu_nocb_unlock_irqrestore(rdp, flags);
600 			trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
601 		}
602 	} else {
603 		rcu_nocb_unlock_irqrestore(rdp, flags);
604 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
605 	}
606 }
607 
608 static int nocb_gp_toggle_rdp(struct rcu_data *rdp,
609 			       bool *wake_state)
610 {
611 	struct rcu_segcblist *cblist = &rdp->cblist;
612 	unsigned long flags;
613 	int ret;
614 
615 	rcu_nocb_lock_irqsave(rdp, flags);
616 	if (rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED) &&
617 	    !rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP)) {
618 		/*
619 		 * Offloading. Set our flag and notify the offload worker.
620 		 * We will handle this rdp until it ever gets de-offloaded.
621 		 */
622 		rcu_segcblist_set_flags(cblist, SEGCBLIST_KTHREAD_GP);
623 		if (rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB))
624 			*wake_state = true;
625 		ret = 1;
626 	} else if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED) &&
627 		   rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP)) {
628 		/*
629 		 * De-offloading. Clear our flag and notify the de-offload worker.
630 		 * We will ignore this rdp until it ever gets re-offloaded.
631 		 */
632 		rcu_segcblist_clear_flags(cblist, SEGCBLIST_KTHREAD_GP);
633 		if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB))
634 			*wake_state = true;
635 		ret = 0;
636 	} else {
637 		WARN_ON_ONCE(1);
638 		ret = -1;
639 	}
640 
641 	rcu_nocb_unlock_irqrestore(rdp, flags);
642 
643 	return ret;
644 }
645 
646 static void nocb_gp_sleep(struct rcu_data *my_rdp, int cpu)
647 {
648 	trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("Sleep"));
649 	swait_event_interruptible_exclusive(my_rdp->nocb_gp_wq,
650 					!READ_ONCE(my_rdp->nocb_gp_sleep));
651 	trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("EndSleep"));
652 }
653 
654 /*
655  * No-CBs GP kthreads come here to wait for additional callbacks to show up
656  * or for grace periods to end.
657  */
658 static void nocb_gp_wait(struct rcu_data *my_rdp)
659 {
660 	bool bypass = false;
661 	int __maybe_unused cpu = my_rdp->cpu;
662 	unsigned long cur_gp_seq;
663 	unsigned long flags;
664 	bool gotcbs = false;
665 	unsigned long j = jiffies;
666 	bool lazy = false;
667 	bool needwait_gp = false; // This prevents actual uninitialized use.
668 	bool needwake;
669 	bool needwake_gp;
670 	struct rcu_data *rdp, *rdp_toggling = NULL;
671 	struct rcu_node *rnp;
672 	unsigned long wait_gp_seq = 0; // Suppress "use uninitialized" warning.
673 	bool wasempty = false;
674 
675 	/*
676 	 * Each pass through the following loop checks for CBs and for the
677 	 * nearest grace period (if any) to wait for next.  The CB kthreads
678 	 * and the global grace-period kthread are awakened if needed.
679 	 */
680 	WARN_ON_ONCE(my_rdp->nocb_gp_rdp != my_rdp);
681 	/*
682 	 * An rcu_data structure is removed from the list after its
683 	 * CPU is de-offloaded and added to the list before that CPU is
684 	 * (re-)offloaded.  If the following loop happens to be referencing
685 	 * that rcu_data structure during the time that the corresponding
686 	 * CPU is de-offloaded and then immediately re-offloaded, this
687 	 * loop's rdp pointer will be carried to the end of the list by
688 	 * the resulting pair of list operations.  This can cause the loop
689 	 * to skip over some of the rcu_data structures that were supposed
690 	 * to have been scanned.  Fortunately a new iteration through the
691 	 * entire loop is forced after a given CPU's rcu_data structure
692 	 * is added to the list, so the skipped-over rcu_data structures
693 	 * won't be ignored for long.
694 	 */
695 	list_for_each_entry(rdp, &my_rdp->nocb_head_rdp, nocb_entry_rdp) {
696 		long bypass_ncbs;
697 		bool flush_bypass = false;
698 		long lazy_ncbs;
699 
700 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Check"));
701 		rcu_nocb_lock_irqsave(rdp, flags);
702 		lockdep_assert_held(&rdp->nocb_lock);
703 		bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
704 		lazy_ncbs = READ_ONCE(rdp->lazy_len);
705 
706 		if (bypass_ncbs && (lazy_ncbs == bypass_ncbs) &&
707 		    (time_after(j, READ_ONCE(rdp->nocb_bypass_first) + jiffies_till_flush) ||
708 		     bypass_ncbs > 2 * qhimark)) {
709 			flush_bypass = true;
710 		} else if (bypass_ncbs && (lazy_ncbs != bypass_ncbs) &&
711 		    (time_after(j, READ_ONCE(rdp->nocb_bypass_first) + 1) ||
712 		     bypass_ncbs > 2 * qhimark)) {
713 			flush_bypass = true;
714 		} else if (!bypass_ncbs && rcu_segcblist_empty(&rdp->cblist)) {
715 			rcu_nocb_unlock_irqrestore(rdp, flags);
716 			continue; /* No callbacks here, try next. */
717 		}
718 
719 		if (flush_bypass) {
720 			// Bypass full or old, so flush it.
721 			(void)rcu_nocb_try_flush_bypass(rdp, j);
722 			bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
723 			lazy_ncbs = READ_ONCE(rdp->lazy_len);
724 		}
725 
726 		if (bypass_ncbs) {
727 			trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
728 					    bypass_ncbs == lazy_ncbs ? TPS("Lazy") : TPS("Bypass"));
729 			if (bypass_ncbs == lazy_ncbs)
730 				lazy = true;
731 			else
732 				bypass = true;
733 		}
734 		rnp = rdp->mynode;
735 
736 		// Advance callbacks if helpful and low contention.
737 		needwake_gp = false;
738 		if (!rcu_segcblist_restempty(&rdp->cblist,
739 					     RCU_NEXT_READY_TAIL) ||
740 		    (rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
741 		     rcu_seq_done(&rnp->gp_seq, cur_gp_seq))) {
742 			raw_spin_lock_rcu_node(rnp); /* irqs disabled. */
743 			needwake_gp = rcu_advance_cbs(rnp, rdp);
744 			wasempty = rcu_segcblist_restempty(&rdp->cblist,
745 							   RCU_NEXT_READY_TAIL);
746 			raw_spin_unlock_rcu_node(rnp); /* irqs disabled. */
747 		}
748 		// Need to wait on some grace period?
749 		WARN_ON_ONCE(wasempty &&
750 			     !rcu_segcblist_restempty(&rdp->cblist,
751 						      RCU_NEXT_READY_TAIL));
752 		if (rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq)) {
753 			if (!needwait_gp ||
754 			    ULONG_CMP_LT(cur_gp_seq, wait_gp_seq))
755 				wait_gp_seq = cur_gp_seq;
756 			needwait_gp = true;
757 			trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
758 					    TPS("NeedWaitGP"));
759 		}
760 		if (rcu_segcblist_ready_cbs(&rdp->cblist)) {
761 			needwake = rdp->nocb_cb_sleep;
762 			WRITE_ONCE(rdp->nocb_cb_sleep, false);
763 			smp_mb(); /* CB invocation -after- GP end. */
764 		} else {
765 			needwake = false;
766 		}
767 		rcu_nocb_unlock_irqrestore(rdp, flags);
768 		if (needwake) {
769 			swake_up_one(&rdp->nocb_cb_wq);
770 			gotcbs = true;
771 		}
772 		if (needwake_gp)
773 			rcu_gp_kthread_wake();
774 	}
775 
776 	my_rdp->nocb_gp_bypass = bypass;
777 	my_rdp->nocb_gp_gp = needwait_gp;
778 	my_rdp->nocb_gp_seq = needwait_gp ? wait_gp_seq : 0;
779 
780 	// At least one child with non-empty ->nocb_bypass, so set
781 	// timer in order to avoid stranding its callbacks.
782 	if (!rcu_nocb_poll) {
783 		// If bypass list only has lazy CBs. Add a deferred lazy wake up.
784 		if (lazy && !bypass) {
785 			wake_nocb_gp_defer(my_rdp, RCU_NOCB_WAKE_LAZY,
786 					TPS("WakeLazyIsDeferred"));
787 		// Otherwise add a deferred bypass wake up.
788 		} else if (bypass) {
789 			wake_nocb_gp_defer(my_rdp, RCU_NOCB_WAKE_BYPASS,
790 					TPS("WakeBypassIsDeferred"));
791 		}
792 	}
793 
794 	if (rcu_nocb_poll) {
795 		/* Polling, so trace if first poll in the series. */
796 		if (gotcbs)
797 			trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("Poll"));
798 		if (list_empty(&my_rdp->nocb_head_rdp)) {
799 			raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
800 			if (!my_rdp->nocb_toggling_rdp)
801 				WRITE_ONCE(my_rdp->nocb_gp_sleep, true);
802 			raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
803 			/* Wait for any offloading rdp */
804 			nocb_gp_sleep(my_rdp, cpu);
805 		} else {
806 			schedule_timeout_idle(1);
807 		}
808 	} else if (!needwait_gp) {
809 		/* Wait for callbacks to appear. */
810 		nocb_gp_sleep(my_rdp, cpu);
811 	} else {
812 		rnp = my_rdp->mynode;
813 		trace_rcu_this_gp(rnp, my_rdp, wait_gp_seq, TPS("StartWait"));
814 		swait_event_interruptible_exclusive(
815 			rnp->nocb_gp_wq[rcu_seq_ctr(wait_gp_seq) & 0x1],
816 			rcu_seq_done(&rnp->gp_seq, wait_gp_seq) ||
817 			!READ_ONCE(my_rdp->nocb_gp_sleep));
818 		trace_rcu_this_gp(rnp, my_rdp, wait_gp_seq, TPS("EndWait"));
819 	}
820 
821 	if (!rcu_nocb_poll) {
822 		raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
823 		// (De-)queue an rdp to/from the group if its nocb state is changing
824 		rdp_toggling = my_rdp->nocb_toggling_rdp;
825 		if (rdp_toggling)
826 			my_rdp->nocb_toggling_rdp = NULL;
827 
828 		if (my_rdp->nocb_defer_wakeup > RCU_NOCB_WAKE_NOT) {
829 			WRITE_ONCE(my_rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
830 			del_timer(&my_rdp->nocb_timer);
831 		}
832 		WRITE_ONCE(my_rdp->nocb_gp_sleep, true);
833 		raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
834 	} else {
835 		rdp_toggling = READ_ONCE(my_rdp->nocb_toggling_rdp);
836 		if (rdp_toggling) {
837 			/*
838 			 * Paranoid locking to make sure nocb_toggling_rdp is well
839 			 * reset *before* we (re)set SEGCBLIST_KTHREAD_GP or we could
840 			 * race with another round of nocb toggling for this rdp.
841 			 * Nocb locking should prevent from that already but we stick
842 			 * to paranoia, especially in rare path.
843 			 */
844 			raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
845 			my_rdp->nocb_toggling_rdp = NULL;
846 			raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
847 		}
848 	}
849 
850 	if (rdp_toggling) {
851 		bool wake_state = false;
852 		int ret;
853 
854 		ret = nocb_gp_toggle_rdp(rdp_toggling, &wake_state);
855 		if (ret == 1)
856 			list_add_tail(&rdp_toggling->nocb_entry_rdp, &my_rdp->nocb_head_rdp);
857 		else if (ret == 0)
858 			list_del(&rdp_toggling->nocb_entry_rdp);
859 		if (wake_state)
860 			swake_up_one(&rdp_toggling->nocb_state_wq);
861 	}
862 
863 	my_rdp->nocb_gp_seq = -1;
864 	WARN_ON(signal_pending(current));
865 }
866 
867 /*
868  * No-CBs grace-period-wait kthread.  There is one of these per group
869  * of CPUs, but only once at least one CPU in that group has come online
870  * at least once since boot.  This kthread checks for newly posted
871  * callbacks from any of the CPUs it is responsible for, waits for a
872  * grace period, then awakens all of the rcu_nocb_cb_kthread() instances
873  * that then have callback-invocation work to do.
874  */
875 static int rcu_nocb_gp_kthread(void *arg)
876 {
877 	struct rcu_data *rdp = arg;
878 
879 	for (;;) {
880 		WRITE_ONCE(rdp->nocb_gp_loops, rdp->nocb_gp_loops + 1);
881 		nocb_gp_wait(rdp);
882 		cond_resched_tasks_rcu_qs();
883 	}
884 	return 0;
885 }
886 
887 static inline bool nocb_cb_can_run(struct rcu_data *rdp)
888 {
889 	u8 flags = SEGCBLIST_OFFLOADED | SEGCBLIST_KTHREAD_CB;
890 
891 	return rcu_segcblist_test_flags(&rdp->cblist, flags);
892 }
893 
894 static inline bool nocb_cb_wait_cond(struct rcu_data *rdp)
895 {
896 	return nocb_cb_can_run(rdp) && !READ_ONCE(rdp->nocb_cb_sleep);
897 }
898 
899 /*
900  * Invoke any ready callbacks from the corresponding no-CBs CPU,
901  * then, if there are no more, wait for more to appear.
902  */
903 static void nocb_cb_wait(struct rcu_data *rdp)
904 {
905 	struct rcu_segcblist *cblist = &rdp->cblist;
906 	unsigned long cur_gp_seq;
907 	unsigned long flags;
908 	bool needwake_state = false;
909 	bool needwake_gp = false;
910 	bool can_sleep = true;
911 	struct rcu_node *rnp = rdp->mynode;
912 
913 	do {
914 		swait_event_interruptible_exclusive(rdp->nocb_cb_wq,
915 						    nocb_cb_wait_cond(rdp));
916 
917 		// VVV Ensure CB invocation follows _sleep test.
918 		if (smp_load_acquire(&rdp->nocb_cb_sleep)) { // ^^^
919 			WARN_ON(signal_pending(current));
920 			trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeEmpty"));
921 		}
922 	} while (!nocb_cb_can_run(rdp));
923 
924 
925 	local_irq_save(flags);
926 	rcu_momentary_dyntick_idle();
927 	local_irq_restore(flags);
928 	/*
929 	 * Disable BH to provide the expected environment.  Also, when
930 	 * transitioning to/from NOCB mode, a self-requeuing callback might
931 	 * be invoked from softirq.  A short grace period could cause both
932 	 * instances of this callback would execute concurrently.
933 	 */
934 	local_bh_disable();
935 	rcu_do_batch(rdp);
936 	local_bh_enable();
937 	lockdep_assert_irqs_enabled();
938 	rcu_nocb_lock_irqsave(rdp, flags);
939 	if (rcu_segcblist_nextgp(cblist, &cur_gp_seq) &&
940 	    rcu_seq_done(&rnp->gp_seq, cur_gp_seq) &&
941 	    raw_spin_trylock_rcu_node(rnp)) { /* irqs already disabled. */
942 		needwake_gp = rcu_advance_cbs(rdp->mynode, rdp);
943 		raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
944 	}
945 
946 	if (rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED)) {
947 		if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB)) {
948 			rcu_segcblist_set_flags(cblist, SEGCBLIST_KTHREAD_CB);
949 			if (rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP))
950 				needwake_state = true;
951 		}
952 		if (rcu_segcblist_ready_cbs(cblist))
953 			can_sleep = false;
954 	} else {
955 		/*
956 		 * De-offloading. Clear our flag and notify the de-offload worker.
957 		 * We won't touch the callbacks and keep sleeping until we ever
958 		 * get re-offloaded.
959 		 */
960 		WARN_ON_ONCE(!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB));
961 		rcu_segcblist_clear_flags(cblist, SEGCBLIST_KTHREAD_CB);
962 		if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP))
963 			needwake_state = true;
964 	}
965 
966 	WRITE_ONCE(rdp->nocb_cb_sleep, can_sleep);
967 
968 	if (rdp->nocb_cb_sleep)
969 		trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("CBSleep"));
970 
971 	rcu_nocb_unlock_irqrestore(rdp, flags);
972 	if (needwake_gp)
973 		rcu_gp_kthread_wake();
974 
975 	if (needwake_state)
976 		swake_up_one(&rdp->nocb_state_wq);
977 }
978 
979 /*
980  * Per-rcu_data kthread, but only for no-CBs CPUs.  Repeatedly invoke
981  * nocb_cb_wait() to do the dirty work.
982  */
983 static int rcu_nocb_cb_kthread(void *arg)
984 {
985 	struct rcu_data *rdp = arg;
986 
987 	// Each pass through this loop does one callback batch, and,
988 	// if there are no more ready callbacks, waits for them.
989 	for (;;) {
990 		nocb_cb_wait(rdp);
991 		cond_resched_tasks_rcu_qs();
992 	}
993 	return 0;
994 }
995 
996 /* Is a deferred wakeup of rcu_nocb_kthread() required? */
997 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level)
998 {
999 	return READ_ONCE(rdp->nocb_defer_wakeup) >= level;
1000 }
1001 
1002 /* Do a deferred wakeup of rcu_nocb_kthread(). */
1003 static bool do_nocb_deferred_wakeup_common(struct rcu_data *rdp_gp,
1004 					   struct rcu_data *rdp, int level,
1005 					   unsigned long flags)
1006 	__releases(rdp_gp->nocb_gp_lock)
1007 {
1008 	int ndw;
1009 	int ret;
1010 
1011 	if (!rcu_nocb_need_deferred_wakeup(rdp_gp, level)) {
1012 		raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
1013 		return false;
1014 	}
1015 
1016 	ndw = rdp_gp->nocb_defer_wakeup;
1017 	ret = __wake_nocb_gp(rdp_gp, rdp, ndw == RCU_NOCB_WAKE_FORCE, flags);
1018 	trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DeferredWake"));
1019 
1020 	return ret;
1021 }
1022 
1023 /* Do a deferred wakeup of rcu_nocb_kthread() from a timer handler. */
1024 static void do_nocb_deferred_wakeup_timer(struct timer_list *t)
1025 {
1026 	unsigned long flags;
1027 	struct rcu_data *rdp = from_timer(rdp, t, nocb_timer);
1028 
1029 	WARN_ON_ONCE(rdp->nocb_gp_rdp != rdp);
1030 	trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Timer"));
1031 
1032 	raw_spin_lock_irqsave(&rdp->nocb_gp_lock, flags);
1033 	smp_mb__after_spinlock(); /* Timer expire before wakeup. */
1034 	do_nocb_deferred_wakeup_common(rdp, rdp, RCU_NOCB_WAKE_BYPASS, flags);
1035 }
1036 
1037 /*
1038  * Do a deferred wakeup of rcu_nocb_kthread() from fastpath.
1039  * This means we do an inexact common-case check.  Note that if
1040  * we miss, ->nocb_timer will eventually clean things up.
1041  */
1042 static bool do_nocb_deferred_wakeup(struct rcu_data *rdp)
1043 {
1044 	unsigned long flags;
1045 	struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
1046 
1047 	if (!rdp_gp || !rcu_nocb_need_deferred_wakeup(rdp_gp, RCU_NOCB_WAKE))
1048 		return false;
1049 
1050 	raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
1051 	return do_nocb_deferred_wakeup_common(rdp_gp, rdp, RCU_NOCB_WAKE, flags);
1052 }
1053 
1054 void rcu_nocb_flush_deferred_wakeup(void)
1055 {
1056 	do_nocb_deferred_wakeup(this_cpu_ptr(&rcu_data));
1057 }
1058 EXPORT_SYMBOL_GPL(rcu_nocb_flush_deferred_wakeup);
1059 
1060 static int rdp_offload_toggle(struct rcu_data *rdp,
1061 			       bool offload, unsigned long flags)
1062 	__releases(rdp->nocb_lock)
1063 {
1064 	struct rcu_segcblist *cblist = &rdp->cblist;
1065 	struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
1066 	bool wake_gp = false;
1067 
1068 	rcu_segcblist_offload(cblist, offload);
1069 
1070 	if (rdp->nocb_cb_sleep)
1071 		rdp->nocb_cb_sleep = false;
1072 	rcu_nocb_unlock_irqrestore(rdp, flags);
1073 
1074 	/*
1075 	 * Ignore former value of nocb_cb_sleep and force wake up as it could
1076 	 * have been spuriously set to false already.
1077 	 */
1078 	swake_up_one(&rdp->nocb_cb_wq);
1079 
1080 	raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
1081 	// Queue this rdp for add/del to/from the list to iterate on rcuog
1082 	WRITE_ONCE(rdp_gp->nocb_toggling_rdp, rdp);
1083 	if (rdp_gp->nocb_gp_sleep) {
1084 		rdp_gp->nocb_gp_sleep = false;
1085 		wake_gp = true;
1086 	}
1087 	raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
1088 
1089 	return wake_gp;
1090 }
1091 
1092 static long rcu_nocb_rdp_deoffload(void *arg)
1093 {
1094 	struct rcu_data *rdp = arg;
1095 	struct rcu_segcblist *cblist = &rdp->cblist;
1096 	unsigned long flags;
1097 	int wake_gp;
1098 	struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
1099 
1100 	/*
1101 	 * rcu_nocb_rdp_deoffload() may be called directly if
1102 	 * rcuog/o[p] spawn failed, because at this time the rdp->cpu
1103 	 * is not online yet.
1104 	 */
1105 	WARN_ON_ONCE((rdp->cpu != raw_smp_processor_id()) && cpu_online(rdp->cpu));
1106 
1107 	pr_info("De-offloading %d\n", rdp->cpu);
1108 
1109 	rcu_nocb_lock_irqsave(rdp, flags);
1110 	/*
1111 	 * Flush once and for all now. This suffices because we are
1112 	 * running on the target CPU holding ->nocb_lock (thus having
1113 	 * interrupts disabled), and because rdp_offload_toggle()
1114 	 * invokes rcu_segcblist_offload(), which clears SEGCBLIST_OFFLOADED.
1115 	 * Thus future calls to rcu_segcblist_completely_offloaded() will
1116 	 * return false, which means that future calls to rcu_nocb_try_bypass()
1117 	 * will refuse to put anything into the bypass.
1118 	 */
1119 	WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies, false));
1120 	/*
1121 	 * Start with invoking rcu_core() early. This way if the current thread
1122 	 * happens to preempt an ongoing call to rcu_core() in the middle,
1123 	 * leaving some work dismissed because rcu_core() still thinks the rdp is
1124 	 * completely offloaded, we are guaranteed a nearby future instance of
1125 	 * rcu_core() to catch up.
1126 	 */
1127 	rcu_segcblist_set_flags(cblist, SEGCBLIST_RCU_CORE);
1128 	invoke_rcu_core();
1129 	wake_gp = rdp_offload_toggle(rdp, false, flags);
1130 
1131 	mutex_lock(&rdp_gp->nocb_gp_kthread_mutex);
1132 	if (rdp_gp->nocb_gp_kthread) {
1133 		if (wake_gp)
1134 			wake_up_process(rdp_gp->nocb_gp_kthread);
1135 
1136 		/*
1137 		 * If rcuo[p] kthread spawn failed, directly remove SEGCBLIST_KTHREAD_CB.
1138 		 * Just wait SEGCBLIST_KTHREAD_GP to be cleared by rcuog.
1139 		 */
1140 		if (!rdp->nocb_cb_kthread) {
1141 			rcu_nocb_lock_irqsave(rdp, flags);
1142 			rcu_segcblist_clear_flags(&rdp->cblist, SEGCBLIST_KTHREAD_CB);
1143 			rcu_nocb_unlock_irqrestore(rdp, flags);
1144 		}
1145 
1146 		swait_event_exclusive(rdp->nocb_state_wq,
1147 					!rcu_segcblist_test_flags(cblist,
1148 					  SEGCBLIST_KTHREAD_CB | SEGCBLIST_KTHREAD_GP));
1149 	} else {
1150 		/*
1151 		 * No kthread to clear the flags for us or remove the rdp from the nocb list
1152 		 * to iterate. Do it here instead. Locking doesn't look stricly necessary
1153 		 * but we stick to paranoia in this rare path.
1154 		 */
1155 		rcu_nocb_lock_irqsave(rdp, flags);
1156 		rcu_segcblist_clear_flags(&rdp->cblist,
1157 				SEGCBLIST_KTHREAD_CB | SEGCBLIST_KTHREAD_GP);
1158 		rcu_nocb_unlock_irqrestore(rdp, flags);
1159 
1160 		list_del(&rdp->nocb_entry_rdp);
1161 	}
1162 	mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
1163 
1164 	/*
1165 	 * Lock one last time to acquire latest callback updates from kthreads
1166 	 * so we can later handle callbacks locally without locking.
1167 	 */
1168 	rcu_nocb_lock_irqsave(rdp, flags);
1169 	/*
1170 	 * Theoretically we could clear SEGCBLIST_LOCKING after the nocb
1171 	 * lock is released but how about being paranoid for once?
1172 	 */
1173 	rcu_segcblist_clear_flags(cblist, SEGCBLIST_LOCKING);
1174 	/*
1175 	 * Without SEGCBLIST_LOCKING, we can't use
1176 	 * rcu_nocb_unlock_irqrestore() anymore.
1177 	 */
1178 	raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
1179 
1180 	/* Sanity check */
1181 	WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
1182 
1183 
1184 	return 0;
1185 }
1186 
1187 int rcu_nocb_cpu_deoffload(int cpu)
1188 {
1189 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1190 	int ret = 0;
1191 
1192 	cpus_read_lock();
1193 	mutex_lock(&rcu_state.barrier_mutex);
1194 	if (rcu_rdp_is_offloaded(rdp)) {
1195 		if (cpu_online(cpu)) {
1196 			ret = work_on_cpu(cpu, rcu_nocb_rdp_deoffload, rdp);
1197 			if (!ret)
1198 				cpumask_clear_cpu(cpu, rcu_nocb_mask);
1199 		} else {
1200 			pr_info("NOCB: Cannot CB-deoffload offline CPU %d\n", rdp->cpu);
1201 			ret = -EINVAL;
1202 		}
1203 	}
1204 	mutex_unlock(&rcu_state.barrier_mutex);
1205 	cpus_read_unlock();
1206 
1207 	return ret;
1208 }
1209 EXPORT_SYMBOL_GPL(rcu_nocb_cpu_deoffload);
1210 
1211 static long rcu_nocb_rdp_offload(void *arg)
1212 {
1213 	struct rcu_data *rdp = arg;
1214 	struct rcu_segcblist *cblist = &rdp->cblist;
1215 	unsigned long flags;
1216 	int wake_gp;
1217 	struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
1218 
1219 	WARN_ON_ONCE(rdp->cpu != raw_smp_processor_id());
1220 	/*
1221 	 * For now we only support re-offload, ie: the rdp must have been
1222 	 * offloaded on boot first.
1223 	 */
1224 	if (!rdp->nocb_gp_rdp)
1225 		return -EINVAL;
1226 
1227 	if (WARN_ON_ONCE(!rdp_gp->nocb_gp_kthread))
1228 		return -EINVAL;
1229 
1230 	pr_info("Offloading %d\n", rdp->cpu);
1231 
1232 	/*
1233 	 * Can't use rcu_nocb_lock_irqsave() before SEGCBLIST_LOCKING
1234 	 * is set.
1235 	 */
1236 	raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
1237 
1238 	/*
1239 	 * We didn't take the nocb lock while working on the
1240 	 * rdp->cblist with SEGCBLIST_LOCKING cleared (pure softirq/rcuc mode).
1241 	 * Every modifications that have been done previously on
1242 	 * rdp->cblist must be visible remotely by the nocb kthreads
1243 	 * upon wake up after reading the cblist flags.
1244 	 *
1245 	 * The layout against nocb_lock enforces that ordering:
1246 	 *
1247 	 *  __rcu_nocb_rdp_offload()   nocb_cb_wait()/nocb_gp_wait()
1248 	 * -------------------------   ----------------------------
1249 	 *      WRITE callbacks           rcu_nocb_lock()
1250 	 *      rcu_nocb_lock()           READ flags
1251 	 *      WRITE flags               READ callbacks
1252 	 *      rcu_nocb_unlock()         rcu_nocb_unlock()
1253 	 */
1254 	wake_gp = rdp_offload_toggle(rdp, true, flags);
1255 	if (wake_gp)
1256 		wake_up_process(rdp_gp->nocb_gp_kthread);
1257 	swait_event_exclusive(rdp->nocb_state_wq,
1258 			      rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB) &&
1259 			      rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP));
1260 
1261 	/*
1262 	 * All kthreads are ready to work, we can finally relieve rcu_core() and
1263 	 * enable nocb bypass.
1264 	 */
1265 	rcu_nocb_lock_irqsave(rdp, flags);
1266 	rcu_segcblist_clear_flags(cblist, SEGCBLIST_RCU_CORE);
1267 	rcu_nocb_unlock_irqrestore(rdp, flags);
1268 
1269 	return 0;
1270 }
1271 
1272 int rcu_nocb_cpu_offload(int cpu)
1273 {
1274 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1275 	int ret = 0;
1276 
1277 	cpus_read_lock();
1278 	mutex_lock(&rcu_state.barrier_mutex);
1279 	if (!rcu_rdp_is_offloaded(rdp)) {
1280 		if (cpu_online(cpu)) {
1281 			ret = work_on_cpu(cpu, rcu_nocb_rdp_offload, rdp);
1282 			if (!ret)
1283 				cpumask_set_cpu(cpu, rcu_nocb_mask);
1284 		} else {
1285 			pr_info("NOCB: Cannot CB-offload offline CPU %d\n", rdp->cpu);
1286 			ret = -EINVAL;
1287 		}
1288 	}
1289 	mutex_unlock(&rcu_state.barrier_mutex);
1290 	cpus_read_unlock();
1291 
1292 	return ret;
1293 }
1294 EXPORT_SYMBOL_GPL(rcu_nocb_cpu_offload);
1295 
1296 #ifdef CONFIG_RCU_LAZY
1297 static unsigned long
1298 lazy_rcu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1299 {
1300 	int cpu;
1301 	unsigned long count = 0;
1302 
1303 	if (WARN_ON_ONCE(!cpumask_available(rcu_nocb_mask)))
1304 		return 0;
1305 
1306 	/*  Protect rcu_nocb_mask against concurrent (de-)offloading. */
1307 	if (!mutex_trylock(&rcu_state.barrier_mutex))
1308 		return 0;
1309 
1310 	/* Snapshot count of all CPUs */
1311 	for_each_cpu(cpu, rcu_nocb_mask) {
1312 		struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1313 
1314 		count +=  READ_ONCE(rdp->lazy_len);
1315 	}
1316 
1317 	mutex_unlock(&rcu_state.barrier_mutex);
1318 
1319 	return count ? count : SHRINK_EMPTY;
1320 }
1321 
1322 static unsigned long
1323 lazy_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1324 {
1325 	int cpu;
1326 	unsigned long flags;
1327 	unsigned long count = 0;
1328 
1329 	if (WARN_ON_ONCE(!cpumask_available(rcu_nocb_mask)))
1330 		return 0;
1331 	/*
1332 	 * Protect against concurrent (de-)offloading. Otherwise nocb locking
1333 	 * may be ignored or imbalanced.
1334 	 */
1335 	if (!mutex_trylock(&rcu_state.barrier_mutex)) {
1336 		/*
1337 		 * But really don't insist if barrier_mutex is contended since we
1338 		 * can't guarantee that it will never engage in a dependency
1339 		 * chain involving memory allocation. The lock is seldom contended
1340 		 * anyway.
1341 		 */
1342 		return 0;
1343 	}
1344 
1345 	/* Snapshot count of all CPUs */
1346 	for_each_cpu(cpu, rcu_nocb_mask) {
1347 		struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1348 		int _count;
1349 
1350 		if (WARN_ON_ONCE(!rcu_rdp_is_offloaded(rdp)))
1351 			continue;
1352 
1353 		if (!READ_ONCE(rdp->lazy_len))
1354 			continue;
1355 
1356 		rcu_nocb_lock_irqsave(rdp, flags);
1357 		/*
1358 		 * Recheck under the nocb lock. Since we are not holding the bypass
1359 		 * lock we may still race with increments from the enqueuer but still
1360 		 * we know for sure if there is at least one lazy callback.
1361 		 */
1362 		_count = READ_ONCE(rdp->lazy_len);
1363 		if (!_count) {
1364 			rcu_nocb_unlock_irqrestore(rdp, flags);
1365 			continue;
1366 		}
1367 		rcu_nocb_try_flush_bypass(rdp, jiffies);
1368 		rcu_nocb_unlock_irqrestore(rdp, flags);
1369 		wake_nocb_gp(rdp, false);
1370 		sc->nr_to_scan -= _count;
1371 		count += _count;
1372 		if (sc->nr_to_scan <= 0)
1373 			break;
1374 	}
1375 
1376 	mutex_unlock(&rcu_state.barrier_mutex);
1377 
1378 	return count ? count : SHRINK_STOP;
1379 }
1380 
1381 static struct shrinker lazy_rcu_shrinker = {
1382 	.count_objects = lazy_rcu_shrink_count,
1383 	.scan_objects = lazy_rcu_shrink_scan,
1384 	.batch = 0,
1385 	.seeks = DEFAULT_SEEKS,
1386 };
1387 #endif // #ifdef CONFIG_RCU_LAZY
1388 
1389 void __init rcu_init_nohz(void)
1390 {
1391 	int cpu;
1392 	struct rcu_data *rdp;
1393 	const struct cpumask *cpumask = NULL;
1394 
1395 #if defined(CONFIG_NO_HZ_FULL)
1396 	if (tick_nohz_full_running && !cpumask_empty(tick_nohz_full_mask))
1397 		cpumask = tick_nohz_full_mask;
1398 #endif
1399 
1400 	if (IS_ENABLED(CONFIG_RCU_NOCB_CPU_DEFAULT_ALL) &&
1401 	    !rcu_state.nocb_is_setup && !cpumask)
1402 		cpumask = cpu_possible_mask;
1403 
1404 	if (cpumask) {
1405 		if (!cpumask_available(rcu_nocb_mask)) {
1406 			if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
1407 				pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
1408 				return;
1409 			}
1410 		}
1411 
1412 		cpumask_or(rcu_nocb_mask, rcu_nocb_mask, cpumask);
1413 		rcu_state.nocb_is_setup = true;
1414 	}
1415 
1416 	if (!rcu_state.nocb_is_setup)
1417 		return;
1418 
1419 #ifdef CONFIG_RCU_LAZY
1420 	if (register_shrinker(&lazy_rcu_shrinker, "rcu-lazy"))
1421 		pr_err("Failed to register lazy_rcu shrinker!\n");
1422 #endif // #ifdef CONFIG_RCU_LAZY
1423 
1424 	if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
1425 		pr_info("\tNote: kernel parameter 'rcu_nocbs=', 'nohz_full', or 'isolcpus=' contains nonexistent CPUs.\n");
1426 		cpumask_and(rcu_nocb_mask, cpu_possible_mask,
1427 			    rcu_nocb_mask);
1428 	}
1429 	if (cpumask_empty(rcu_nocb_mask))
1430 		pr_info("\tOffload RCU callbacks from CPUs: (none).\n");
1431 	else
1432 		pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
1433 			cpumask_pr_args(rcu_nocb_mask));
1434 	if (rcu_nocb_poll)
1435 		pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
1436 
1437 	for_each_cpu(cpu, rcu_nocb_mask) {
1438 		rdp = per_cpu_ptr(&rcu_data, cpu);
1439 		if (rcu_segcblist_empty(&rdp->cblist))
1440 			rcu_segcblist_init(&rdp->cblist);
1441 		rcu_segcblist_offload(&rdp->cblist, true);
1442 		rcu_segcblist_set_flags(&rdp->cblist, SEGCBLIST_KTHREAD_CB | SEGCBLIST_KTHREAD_GP);
1443 		rcu_segcblist_clear_flags(&rdp->cblist, SEGCBLIST_RCU_CORE);
1444 	}
1445 	rcu_organize_nocb_kthreads();
1446 }
1447 
1448 /* Initialize per-rcu_data variables for no-CBs CPUs. */
1449 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
1450 {
1451 	init_swait_queue_head(&rdp->nocb_cb_wq);
1452 	init_swait_queue_head(&rdp->nocb_gp_wq);
1453 	init_swait_queue_head(&rdp->nocb_state_wq);
1454 	raw_spin_lock_init(&rdp->nocb_lock);
1455 	raw_spin_lock_init(&rdp->nocb_bypass_lock);
1456 	raw_spin_lock_init(&rdp->nocb_gp_lock);
1457 	timer_setup(&rdp->nocb_timer, do_nocb_deferred_wakeup_timer, 0);
1458 	rcu_cblist_init(&rdp->nocb_bypass);
1459 	WRITE_ONCE(rdp->lazy_len, 0);
1460 	mutex_init(&rdp->nocb_gp_kthread_mutex);
1461 }
1462 
1463 /*
1464  * If the specified CPU is a no-CBs CPU that does not already have its
1465  * rcuo CB kthread, spawn it.  Additionally, if the rcuo GP kthread
1466  * for this CPU's group has not yet been created, spawn it as well.
1467  */
1468 static void rcu_spawn_cpu_nocb_kthread(int cpu)
1469 {
1470 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1471 	struct rcu_data *rdp_gp;
1472 	struct task_struct *t;
1473 	struct sched_param sp;
1474 
1475 	if (!rcu_scheduler_fully_active || !rcu_state.nocb_is_setup)
1476 		return;
1477 
1478 	/* If there already is an rcuo kthread, then nothing to do. */
1479 	if (rdp->nocb_cb_kthread)
1480 		return;
1481 
1482 	/* If we didn't spawn the GP kthread first, reorganize! */
1483 	sp.sched_priority = kthread_prio;
1484 	rdp_gp = rdp->nocb_gp_rdp;
1485 	mutex_lock(&rdp_gp->nocb_gp_kthread_mutex);
1486 	if (!rdp_gp->nocb_gp_kthread) {
1487 		t = kthread_run(rcu_nocb_gp_kthread, rdp_gp,
1488 				"rcuog/%d", rdp_gp->cpu);
1489 		if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo GP kthread, OOM is now expected behavior\n", __func__)) {
1490 			mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
1491 			goto end;
1492 		}
1493 		WRITE_ONCE(rdp_gp->nocb_gp_kthread, t);
1494 		if (kthread_prio)
1495 			sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1496 	}
1497 	mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
1498 
1499 	/* Spawn the kthread for this CPU. */
1500 	t = kthread_run(rcu_nocb_cb_kthread, rdp,
1501 			"rcuo%c/%d", rcu_state.abbr, cpu);
1502 	if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo CB kthread, OOM is now expected behavior\n", __func__))
1503 		goto end;
1504 
1505 	if (IS_ENABLED(CONFIG_RCU_NOCB_CPU_CB_BOOST) && kthread_prio)
1506 		sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1507 
1508 	WRITE_ONCE(rdp->nocb_cb_kthread, t);
1509 	WRITE_ONCE(rdp->nocb_gp_kthread, rdp_gp->nocb_gp_kthread);
1510 	return;
1511 end:
1512 	mutex_lock(&rcu_state.barrier_mutex);
1513 	if (rcu_rdp_is_offloaded(rdp)) {
1514 		rcu_nocb_rdp_deoffload(rdp);
1515 		cpumask_clear_cpu(cpu, rcu_nocb_mask);
1516 	}
1517 	mutex_unlock(&rcu_state.barrier_mutex);
1518 }
1519 
1520 /* How many CB CPU IDs per GP kthread?  Default of -1 for sqrt(nr_cpu_ids). */
1521 static int rcu_nocb_gp_stride = -1;
1522 module_param(rcu_nocb_gp_stride, int, 0444);
1523 
1524 /*
1525  * Initialize GP-CB relationships for all no-CBs CPU.
1526  */
1527 static void __init rcu_organize_nocb_kthreads(void)
1528 {
1529 	int cpu;
1530 	bool firsttime = true;
1531 	bool gotnocbs = false;
1532 	bool gotnocbscbs = true;
1533 	int ls = rcu_nocb_gp_stride;
1534 	int nl = 0;  /* Next GP kthread. */
1535 	struct rcu_data *rdp;
1536 	struct rcu_data *rdp_gp = NULL;  /* Suppress misguided gcc warn. */
1537 
1538 	if (!cpumask_available(rcu_nocb_mask))
1539 		return;
1540 	if (ls == -1) {
1541 		ls = nr_cpu_ids / int_sqrt(nr_cpu_ids);
1542 		rcu_nocb_gp_stride = ls;
1543 	}
1544 
1545 	/*
1546 	 * Each pass through this loop sets up one rcu_data structure.
1547 	 * Should the corresponding CPU come online in the future, then
1548 	 * we will spawn the needed set of rcu_nocb_kthread() kthreads.
1549 	 */
1550 	for_each_possible_cpu(cpu) {
1551 		rdp = per_cpu_ptr(&rcu_data, cpu);
1552 		if (rdp->cpu >= nl) {
1553 			/* New GP kthread, set up for CBs & next GP. */
1554 			gotnocbs = true;
1555 			nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
1556 			rdp_gp = rdp;
1557 			INIT_LIST_HEAD(&rdp->nocb_head_rdp);
1558 			if (dump_tree) {
1559 				if (!firsttime)
1560 					pr_cont("%s\n", gotnocbscbs
1561 							? "" : " (self only)");
1562 				gotnocbscbs = false;
1563 				firsttime = false;
1564 				pr_alert("%s: No-CB GP kthread CPU %d:",
1565 					 __func__, cpu);
1566 			}
1567 		} else {
1568 			/* Another CB kthread, link to previous GP kthread. */
1569 			gotnocbscbs = true;
1570 			if (dump_tree)
1571 				pr_cont(" %d", cpu);
1572 		}
1573 		rdp->nocb_gp_rdp = rdp_gp;
1574 		if (cpumask_test_cpu(cpu, rcu_nocb_mask))
1575 			list_add_tail(&rdp->nocb_entry_rdp, &rdp_gp->nocb_head_rdp);
1576 	}
1577 	if (gotnocbs && dump_tree)
1578 		pr_cont("%s\n", gotnocbscbs ? "" : " (self only)");
1579 }
1580 
1581 /*
1582  * Bind the current task to the offloaded CPUs.  If there are no offloaded
1583  * CPUs, leave the task unbound.  Splat if the bind attempt fails.
1584  */
1585 void rcu_bind_current_to_nocb(void)
1586 {
1587 	if (cpumask_available(rcu_nocb_mask) && !cpumask_empty(rcu_nocb_mask))
1588 		WARN_ON(sched_setaffinity(current->pid, rcu_nocb_mask));
1589 }
1590 EXPORT_SYMBOL_GPL(rcu_bind_current_to_nocb);
1591 
1592 // The ->on_cpu field is available only in CONFIG_SMP=y, so...
1593 #ifdef CONFIG_SMP
1594 static char *show_rcu_should_be_on_cpu(struct task_struct *tsp)
1595 {
1596 	return tsp && task_is_running(tsp) && !tsp->on_cpu ? "!" : "";
1597 }
1598 #else // #ifdef CONFIG_SMP
1599 static char *show_rcu_should_be_on_cpu(struct task_struct *tsp)
1600 {
1601 	return "";
1602 }
1603 #endif // #else #ifdef CONFIG_SMP
1604 
1605 /*
1606  * Dump out nocb grace-period kthread state for the specified rcu_data
1607  * structure.
1608  */
1609 static void show_rcu_nocb_gp_state(struct rcu_data *rdp)
1610 {
1611 	struct rcu_node *rnp = rdp->mynode;
1612 
1613 	pr_info("nocb GP %d %c%c%c%c%c %c[%c%c] %c%c:%ld rnp %d:%d %lu %c CPU %d%s\n",
1614 		rdp->cpu,
1615 		"kK"[!!rdp->nocb_gp_kthread],
1616 		"lL"[raw_spin_is_locked(&rdp->nocb_gp_lock)],
1617 		"dD"[!!rdp->nocb_defer_wakeup],
1618 		"tT"[timer_pending(&rdp->nocb_timer)],
1619 		"sS"[!!rdp->nocb_gp_sleep],
1620 		".W"[swait_active(&rdp->nocb_gp_wq)],
1621 		".W"[swait_active(&rnp->nocb_gp_wq[0])],
1622 		".W"[swait_active(&rnp->nocb_gp_wq[1])],
1623 		".B"[!!rdp->nocb_gp_bypass],
1624 		".G"[!!rdp->nocb_gp_gp],
1625 		(long)rdp->nocb_gp_seq,
1626 		rnp->grplo, rnp->grphi, READ_ONCE(rdp->nocb_gp_loops),
1627 		rdp->nocb_gp_kthread ? task_state_to_char(rdp->nocb_gp_kthread) : '.',
1628 		rdp->nocb_gp_kthread ? (int)task_cpu(rdp->nocb_gp_kthread) : -1,
1629 		show_rcu_should_be_on_cpu(rdp->nocb_gp_kthread));
1630 }
1631 
1632 /* Dump out nocb kthread state for the specified rcu_data structure. */
1633 static void show_rcu_nocb_state(struct rcu_data *rdp)
1634 {
1635 	char bufw[20];
1636 	char bufr[20];
1637 	struct rcu_data *nocb_next_rdp;
1638 	struct rcu_segcblist *rsclp = &rdp->cblist;
1639 	bool waslocked;
1640 	bool wassleep;
1641 
1642 	if (rdp->nocb_gp_rdp == rdp)
1643 		show_rcu_nocb_gp_state(rdp);
1644 
1645 	nocb_next_rdp = list_next_or_null_rcu(&rdp->nocb_gp_rdp->nocb_head_rdp,
1646 					      &rdp->nocb_entry_rdp,
1647 					      typeof(*rdp),
1648 					      nocb_entry_rdp);
1649 
1650 	sprintf(bufw, "%ld", rsclp->gp_seq[RCU_WAIT_TAIL]);
1651 	sprintf(bufr, "%ld", rsclp->gp_seq[RCU_NEXT_READY_TAIL]);
1652 	pr_info("   CB %d^%d->%d %c%c%c%c%c F%ld L%ld C%d %c%c%s%c%s%c%c q%ld %c CPU %d%s\n",
1653 		rdp->cpu, rdp->nocb_gp_rdp->cpu,
1654 		nocb_next_rdp ? nocb_next_rdp->cpu : -1,
1655 		"kK"[!!rdp->nocb_cb_kthread],
1656 		"bB"[raw_spin_is_locked(&rdp->nocb_bypass_lock)],
1657 		"lL"[raw_spin_is_locked(&rdp->nocb_lock)],
1658 		"sS"[!!rdp->nocb_cb_sleep],
1659 		".W"[swait_active(&rdp->nocb_cb_wq)],
1660 		jiffies - rdp->nocb_bypass_first,
1661 		jiffies - rdp->nocb_nobypass_last,
1662 		rdp->nocb_nobypass_count,
1663 		".D"[rcu_segcblist_ready_cbs(rsclp)],
1664 		".W"[!rcu_segcblist_segempty(rsclp, RCU_WAIT_TAIL)],
1665 		rcu_segcblist_segempty(rsclp, RCU_WAIT_TAIL) ? "" : bufw,
1666 		".R"[!rcu_segcblist_segempty(rsclp, RCU_NEXT_READY_TAIL)],
1667 		rcu_segcblist_segempty(rsclp, RCU_NEXT_READY_TAIL) ? "" : bufr,
1668 		".N"[!rcu_segcblist_segempty(rsclp, RCU_NEXT_TAIL)],
1669 		".B"[!!rcu_cblist_n_cbs(&rdp->nocb_bypass)],
1670 		rcu_segcblist_n_cbs(&rdp->cblist),
1671 		rdp->nocb_cb_kthread ? task_state_to_char(rdp->nocb_cb_kthread) : '.',
1672 		rdp->nocb_cb_kthread ? (int)task_cpu(rdp->nocb_cb_kthread) : -1,
1673 		show_rcu_should_be_on_cpu(rdp->nocb_cb_kthread));
1674 
1675 	/* It is OK for GP kthreads to have GP state. */
1676 	if (rdp->nocb_gp_rdp == rdp)
1677 		return;
1678 
1679 	waslocked = raw_spin_is_locked(&rdp->nocb_gp_lock);
1680 	wassleep = swait_active(&rdp->nocb_gp_wq);
1681 	if (!rdp->nocb_gp_sleep && !waslocked && !wassleep)
1682 		return;  /* Nothing untoward. */
1683 
1684 	pr_info("   nocb GP activity on CB-only CPU!!! %c%c%c %c\n",
1685 		"lL"[waslocked],
1686 		"dD"[!!rdp->nocb_defer_wakeup],
1687 		"sS"[!!rdp->nocb_gp_sleep],
1688 		".W"[wassleep]);
1689 }
1690 
1691 #else /* #ifdef CONFIG_RCU_NOCB_CPU */
1692 
1693 static inline int rcu_lockdep_is_held_nocb(struct rcu_data *rdp)
1694 {
1695 	return 0;
1696 }
1697 
1698 static inline bool rcu_current_is_nocb_kthread(struct rcu_data *rdp)
1699 {
1700 	return false;
1701 }
1702 
1703 /* No ->nocb_lock to acquire.  */
1704 static void rcu_nocb_lock(struct rcu_data *rdp)
1705 {
1706 }
1707 
1708 /* No ->nocb_lock to release.  */
1709 static void rcu_nocb_unlock(struct rcu_data *rdp)
1710 {
1711 }
1712 
1713 /* No ->nocb_lock to release.  */
1714 static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
1715 				       unsigned long flags)
1716 {
1717 	local_irq_restore(flags);
1718 }
1719 
1720 /* Lockdep check that ->cblist may be safely accessed. */
1721 static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp)
1722 {
1723 	lockdep_assert_irqs_disabled();
1724 }
1725 
1726 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
1727 {
1728 }
1729 
1730 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
1731 {
1732 	return NULL;
1733 }
1734 
1735 static void rcu_init_one_nocb(struct rcu_node *rnp)
1736 {
1737 }
1738 
1739 static bool wake_nocb_gp(struct rcu_data *rdp, bool force)
1740 {
1741 	return false;
1742 }
1743 
1744 static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
1745 				  unsigned long j, bool lazy)
1746 {
1747 	return true;
1748 }
1749 
1750 static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
1751 				bool *was_alldone, unsigned long flags, bool lazy)
1752 {
1753 	return false;
1754 }
1755 
1756 static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_empty,
1757 				 unsigned long flags)
1758 {
1759 	WARN_ON_ONCE(1);  /* Should be dead code! */
1760 }
1761 
1762 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
1763 {
1764 }
1765 
1766 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level)
1767 {
1768 	return false;
1769 }
1770 
1771 static bool do_nocb_deferred_wakeup(struct rcu_data *rdp)
1772 {
1773 	return false;
1774 }
1775 
1776 static void rcu_spawn_cpu_nocb_kthread(int cpu)
1777 {
1778 }
1779 
1780 static void show_rcu_nocb_state(struct rcu_data *rdp)
1781 {
1782 }
1783 
1784 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
1785