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