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