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