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