1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Read-Copy Update mechanism for mutual exclusion (tree-based version)
4 * Internal non-public definitions that provide either classic
5 * or preemptible semantics.
7 * Copyright Red Hat, 2009
8 * Copyright IBM Corporation, 2009
11 * Author: Ingo Molnar <mingo@elte.hu>
12 * Paul E. McKenney <paulmck@linux.ibm.com>
13 * Frederic Weisbecker <frederic@kernel.org>
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)
21 return lockdep_is_held(&rdp->nocb_lock);
24 static inline bool rcu_current_is_nocb_kthread(struct rcu_data *rdp)
26 /* Race on early boot between thread creation and assignment */
27 if (!rdp->nocb_cb_kthread || !rdp->nocb_gp_kthread)
30 if (current == rdp->nocb_cb_kthread || current == rdp->nocb_gp_kthread)
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.)
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.
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.
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.
64 static bool rcu_nocb_is_setup;
66 static int __init rcu_nocb_setup(char *str)
68 alloc_bootmem_cpumask_var(&rcu_nocb_mask);
70 if (cpulist_parse(++str, rcu_nocb_mask)) {
71 pr_warn("rcu_nocbs= bad CPU range, all CPUs set\n");
72 cpumask_setall(rcu_nocb_mask);
75 rcu_nocb_is_setup = true;
78 __setup("rcu_nocbs", rcu_nocb_setup);
80 static int __init parse_rcu_nocb_poll(char *arg)
85 early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
88 * Don't bother bypassing ->cblist if the call_rcu() rate is low.
89 * After all, the main point of bypassing is to avoid lock contention
90 * on ->nocb_lock, which only can happen at high call_rcu() rates.
92 static int nocb_nobypass_lim_per_jiffy = 16 * 1000 / HZ;
93 module_param(nocb_nobypass_lim_per_jiffy, int, 0);
96 * Acquire the specified rcu_data structure's ->nocb_bypass_lock. If the
97 * lock isn't immediately available, increment ->nocb_lock_contended to
98 * flag the contention.
100 static void rcu_nocb_bypass_lock(struct rcu_data *rdp)
101 __acquires(&rdp->nocb_bypass_lock)
103 lockdep_assert_irqs_disabled();
104 if (raw_spin_trylock(&rdp->nocb_bypass_lock))
106 atomic_inc(&rdp->nocb_lock_contended);
107 WARN_ON_ONCE(smp_processor_id() != rdp->cpu);
108 smp_mb__after_atomic(); /* atomic_inc() before lock. */
109 raw_spin_lock(&rdp->nocb_bypass_lock);
110 smp_mb__before_atomic(); /* atomic_dec() after lock. */
111 atomic_dec(&rdp->nocb_lock_contended);
115 * Spinwait until the specified rcu_data structure's ->nocb_lock is
116 * not contended. Please note that this is extremely special-purpose,
117 * relying on the fact that at most two kthreads and one CPU contend for
118 * this lock, and also that the two kthreads are guaranteed to have frequent
119 * grace-period-duration time intervals between successive acquisitions
120 * of the lock. This allows us to use an extremely simple throttling
121 * mechanism, and further to apply it only to the CPU doing floods of
122 * call_rcu() invocations. Don't try this at home!
124 static void rcu_nocb_wait_contended(struct rcu_data *rdp)
126 WARN_ON_ONCE(smp_processor_id() != rdp->cpu);
127 while (WARN_ON_ONCE(atomic_read(&rdp->nocb_lock_contended)))
132 * Conditionally acquire the specified rcu_data structure's
133 * ->nocb_bypass_lock.
135 static bool rcu_nocb_bypass_trylock(struct rcu_data *rdp)
137 lockdep_assert_irqs_disabled();
138 return raw_spin_trylock(&rdp->nocb_bypass_lock);
142 * Release the specified rcu_data structure's ->nocb_bypass_lock.
144 static void rcu_nocb_bypass_unlock(struct rcu_data *rdp)
145 __releases(&rdp->nocb_bypass_lock)
147 lockdep_assert_irqs_disabled();
148 raw_spin_unlock(&rdp->nocb_bypass_lock);
152 * Acquire the specified rcu_data structure's ->nocb_lock, but only
153 * if it corresponds to a no-CBs CPU.
155 static void rcu_nocb_lock(struct rcu_data *rdp)
157 lockdep_assert_irqs_disabled();
158 if (!rcu_rdp_is_offloaded(rdp))
160 raw_spin_lock(&rdp->nocb_lock);
164 * Release the specified rcu_data structure's ->nocb_lock, but only
165 * if it corresponds to a no-CBs CPU.
167 static void rcu_nocb_unlock(struct rcu_data *rdp)
169 if (rcu_rdp_is_offloaded(rdp)) {
170 lockdep_assert_irqs_disabled();
171 raw_spin_unlock(&rdp->nocb_lock);
176 * Release the specified rcu_data structure's ->nocb_lock and restore
177 * interrupts, but only if it corresponds to a no-CBs CPU.
179 static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
182 if (rcu_rdp_is_offloaded(rdp)) {
183 lockdep_assert_irqs_disabled();
184 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
186 local_irq_restore(flags);
190 /* Lockdep check that ->cblist may be safely accessed. */
191 static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp)
193 lockdep_assert_irqs_disabled();
194 if (rcu_rdp_is_offloaded(rdp))
195 lockdep_assert_held(&rdp->nocb_lock);
199 * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
202 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
207 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
209 return &rnp->nocb_gp_wq[rcu_seq_ctr(rnp->gp_seq) & 0x1];
212 static void rcu_init_one_nocb(struct rcu_node *rnp)
214 init_swait_queue_head(&rnp->nocb_gp_wq[0]);
215 init_swait_queue_head(&rnp->nocb_gp_wq[1]);
218 static bool __wake_nocb_gp(struct rcu_data *rdp_gp,
219 struct rcu_data *rdp,
220 bool force, unsigned long flags)
221 __releases(rdp_gp->nocb_gp_lock)
223 bool needwake = false;
225 if (!READ_ONCE(rdp_gp->nocb_gp_kthread)) {
226 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
227 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
228 TPS("AlreadyAwake"));
232 if (rdp_gp->nocb_defer_wakeup > RCU_NOCB_WAKE_NOT) {
233 WRITE_ONCE(rdp_gp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
234 del_timer(&rdp_gp->nocb_timer);
237 if (force || READ_ONCE(rdp_gp->nocb_gp_sleep)) {
238 WRITE_ONCE(rdp_gp->nocb_gp_sleep, false);
241 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
243 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DoWake"));
244 wake_up_process(rdp_gp->nocb_gp_kthread);
251 * Kick the GP kthread for this NOCB group.
253 static bool wake_nocb_gp(struct rcu_data *rdp, bool force)
256 struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
258 raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
259 return __wake_nocb_gp(rdp_gp, rdp, force, flags);
263 * Arrange to wake the GP kthread for this NOCB group at some future
264 * time when it is safe to do so.
266 static void wake_nocb_gp_defer(struct rcu_data *rdp, int waketype,
270 struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
272 raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
275 * Bypass wakeup overrides previous deferments. In case
276 * of callback storm, no need to wake up too early.
278 if (waketype == RCU_NOCB_WAKE_BYPASS) {
279 mod_timer(&rdp_gp->nocb_timer, jiffies + 2);
280 WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
282 if (rdp_gp->nocb_defer_wakeup < RCU_NOCB_WAKE)
283 mod_timer(&rdp_gp->nocb_timer, jiffies + 1);
284 if (rdp_gp->nocb_defer_wakeup < waketype)
285 WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
288 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
290 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, reason);
294 * Flush the ->nocb_bypass queue into ->cblist, enqueuing rhp if non-NULL.
295 * However, if there is a callback to be enqueued and if ->nocb_bypass
296 * proves to be initially empty, just return false because the no-CB GP
297 * kthread may need to be awakened in this case.
299 * Note that this function always returns true if rhp is NULL.
301 static bool rcu_nocb_do_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
304 struct rcu_cblist rcl;
306 WARN_ON_ONCE(!rcu_rdp_is_offloaded(rdp));
307 rcu_lockdep_assert_cblist_protected(rdp);
308 lockdep_assert_held(&rdp->nocb_bypass_lock);
309 if (rhp && !rcu_cblist_n_cbs(&rdp->nocb_bypass)) {
310 raw_spin_unlock(&rdp->nocb_bypass_lock);
313 /* Note: ->cblist.len already accounts for ->nocb_bypass contents. */
315 rcu_segcblist_inc_len(&rdp->cblist); /* Must precede enqueue. */
316 rcu_cblist_flush_enqueue(&rcl, &rdp->nocb_bypass, rhp);
317 rcu_segcblist_insert_pend_cbs(&rdp->cblist, &rcl);
318 WRITE_ONCE(rdp->nocb_bypass_first, j);
319 rcu_nocb_bypass_unlock(rdp);
324 * Flush the ->nocb_bypass queue into ->cblist, enqueuing rhp if non-NULL.
325 * However, if there is a callback to be enqueued and if ->nocb_bypass
326 * proves to be initially empty, just return false because the no-CB GP
327 * kthread may need to be awakened in this case.
329 * Note that this function always returns true if rhp is NULL.
331 static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
334 if (!rcu_rdp_is_offloaded(rdp))
336 rcu_lockdep_assert_cblist_protected(rdp);
337 rcu_nocb_bypass_lock(rdp);
338 return rcu_nocb_do_flush_bypass(rdp, rhp, j);
342 * If the ->nocb_bypass_lock is immediately available, flush the
343 * ->nocb_bypass queue into ->cblist.
345 static void rcu_nocb_try_flush_bypass(struct rcu_data *rdp, unsigned long j)
347 rcu_lockdep_assert_cblist_protected(rdp);
348 if (!rcu_rdp_is_offloaded(rdp) ||
349 !rcu_nocb_bypass_trylock(rdp))
351 WARN_ON_ONCE(!rcu_nocb_do_flush_bypass(rdp, NULL, j));
355 * See whether it is appropriate to use the ->nocb_bypass list in order
356 * to control contention on ->nocb_lock. A limited number of direct
357 * enqueues are permitted into ->cblist per jiffy. If ->nocb_bypass
358 * is non-empty, further callbacks must be placed into ->nocb_bypass,
359 * otherwise rcu_barrier() breaks. Use rcu_nocb_flush_bypass() to switch
360 * back to direct use of ->cblist. However, ->nocb_bypass should not be
361 * used if ->cblist is empty, because otherwise callbacks can be stranded
362 * on ->nocb_bypass because we cannot count on the current CPU ever again
363 * invoking call_rcu(). The general rule is that if ->nocb_bypass is
364 * non-empty, the corresponding no-CBs grace-period kthread must not be
365 * in an indefinite sleep state.
367 * Finally, it is not permitted to use the bypass during early boot,
368 * as doing so would confuse the auto-initialization code. Besides
369 * which, there is no point in worrying about lock contention while
370 * there is only one CPU in operation.
372 static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
373 bool *was_alldone, unsigned long flags)
376 unsigned long cur_gp_seq;
377 unsigned long j = jiffies;
378 long ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
380 lockdep_assert_irqs_disabled();
382 // Pure softirq/rcuc based processing: no bypassing, no
384 if (!rcu_rdp_is_offloaded(rdp)) {
385 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
389 // In the process of (de-)offloading: no bypassing, but
391 if (!rcu_segcblist_completely_offloaded(&rdp->cblist)) {
393 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
394 return false; /* Not offloaded, no bypassing. */
397 // Don't use ->nocb_bypass during early boot.
398 if (rcu_scheduler_active != RCU_SCHEDULER_RUNNING) {
400 WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
401 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
405 // If we have advanced to a new jiffy, reset counts to allow
406 // moving back from ->nocb_bypass to ->cblist.
407 if (j == rdp->nocb_nobypass_last) {
408 c = rdp->nocb_nobypass_count + 1;
410 WRITE_ONCE(rdp->nocb_nobypass_last, j);
411 c = rdp->nocb_nobypass_count - nocb_nobypass_lim_per_jiffy;
412 if (ULONG_CMP_LT(rdp->nocb_nobypass_count,
413 nocb_nobypass_lim_per_jiffy))
415 else if (c > nocb_nobypass_lim_per_jiffy)
416 c = nocb_nobypass_lim_per_jiffy;
418 WRITE_ONCE(rdp->nocb_nobypass_count, c);
420 // If there hasn't yet been all that many ->cblist enqueues
421 // this jiffy, tell the caller to enqueue onto ->cblist. But flush
422 // ->nocb_bypass first.
423 if (rdp->nocb_nobypass_count < nocb_nobypass_lim_per_jiffy) {
425 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
427 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
429 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, j));
430 WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
431 return false; // Caller must enqueue the callback.
434 // If ->nocb_bypass has been used too long or is too full,
435 // flush ->nocb_bypass to ->cblist.
436 if ((ncbs && j != READ_ONCE(rdp->nocb_bypass_first)) ||
439 if (!rcu_nocb_flush_bypass(rdp, rhp, j)) {
440 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
442 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
444 WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
445 return false; // Caller must enqueue the callback.
447 if (j != rdp->nocb_gp_adv_time &&
448 rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
449 rcu_seq_done(&rdp->mynode->gp_seq, cur_gp_seq)) {
450 rcu_advance_cbs_nowake(rdp->mynode, rdp);
451 rdp->nocb_gp_adv_time = j;
453 rcu_nocb_unlock_irqrestore(rdp, flags);
454 return true; // Callback already enqueued.
457 // We need to use the bypass.
458 rcu_nocb_wait_contended(rdp);
459 rcu_nocb_bypass_lock(rdp);
460 ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
461 rcu_segcblist_inc_len(&rdp->cblist); /* Must precede enqueue. */
462 rcu_cblist_enqueue(&rdp->nocb_bypass, rhp);
464 WRITE_ONCE(rdp->nocb_bypass_first, j);
465 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("FirstBQ"));
467 rcu_nocb_bypass_unlock(rdp);
468 smp_mb(); /* Order enqueue before wake. */
470 local_irq_restore(flags);
472 // No-CBs GP kthread might be indefinitely asleep, if so, wake.
473 rcu_nocb_lock(rdp); // Rare during call_rcu() flood.
474 if (!rcu_segcblist_pend_cbs(&rdp->cblist)) {
475 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
477 __call_rcu_nocb_wake(rdp, true, flags);
479 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
480 TPS("FirstBQnoWake"));
481 rcu_nocb_unlock_irqrestore(rdp, flags);
484 return true; // Callback already enqueued.
488 * Awaken the no-CBs grace-period kthread if needed, either due to it
489 * legitimately being asleep or due to overload conditions.
491 * If warranted, also wake up the kthread servicing this CPUs queues.
493 static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone,
495 __releases(rdp->nocb_lock)
497 unsigned long cur_gp_seq;
500 struct task_struct *t;
502 // If we are being polled or there is no kthread, just leave.
503 t = READ_ONCE(rdp->nocb_gp_kthread);
504 if (rcu_nocb_poll || !t) {
505 rcu_nocb_unlock_irqrestore(rdp, flags);
506 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
510 // Need to actually to a wakeup.
511 len = rcu_segcblist_n_cbs(&rdp->cblist);
513 rdp->qlen_last_fqs_check = len;
514 if (!irqs_disabled_flags(flags)) {
515 /* ... if queue was empty ... */
516 rcu_nocb_unlock_irqrestore(rdp, flags);
517 wake_nocb_gp(rdp, false);
518 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
521 rcu_nocb_unlock_irqrestore(rdp, flags);
522 wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE,
523 TPS("WakeEmptyIsDeferred"));
525 } else if (len > rdp->qlen_last_fqs_check + qhimark) {
526 /* ... or if many callbacks queued. */
527 rdp->qlen_last_fqs_check = len;
529 if (j != rdp->nocb_gp_adv_time &&
530 rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
531 rcu_seq_done(&rdp->mynode->gp_seq, cur_gp_seq)) {
532 rcu_advance_cbs_nowake(rdp->mynode, rdp);
533 rdp->nocb_gp_adv_time = j;
535 smp_mb(); /* Enqueue before timer_pending(). */
536 if ((rdp->nocb_cb_sleep ||
537 !rcu_segcblist_ready_cbs(&rdp->cblist)) &&
538 !timer_pending(&rdp->nocb_timer)) {
539 rcu_nocb_unlock_irqrestore(rdp, flags);
540 wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE_FORCE,
541 TPS("WakeOvfIsDeferred"));
543 rcu_nocb_unlock_irqrestore(rdp, flags);
544 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
547 rcu_nocb_unlock_irqrestore(rdp, flags);
548 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
553 * Check if we ignore this rdp.
555 * We check that without holding the nocb lock but
556 * we make sure not to miss a freshly offloaded rdp
557 * with the current ordering:
559 * rdp_offload_toggle() nocb_gp_enabled_cb()
560 * ------------------------- ----------------------------
561 * WRITE flags LOCK nocb_gp_lock
562 * LOCK nocb_gp_lock READ/WRITE nocb_gp_sleep
563 * READ/WRITE nocb_gp_sleep UNLOCK nocb_gp_lock
564 * UNLOCK nocb_gp_lock READ flags
566 static inline bool nocb_gp_enabled_cb(struct rcu_data *rdp)
568 u8 flags = SEGCBLIST_OFFLOADED | SEGCBLIST_KTHREAD_GP;
570 return rcu_segcblist_test_flags(&rdp->cblist, flags);
573 static inline bool nocb_gp_update_state_deoffloading(struct rcu_data *rdp,
574 bool *needwake_state)
576 struct rcu_segcblist *cblist = &rdp->cblist;
578 if (rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED)) {
579 if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP)) {
580 rcu_segcblist_set_flags(cblist, SEGCBLIST_KTHREAD_GP);
581 if (rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB))
582 *needwake_state = true;
588 * De-offloading. Clear our flag and notify the de-offload worker.
589 * We will ignore this rdp until it ever gets re-offloaded.
591 WARN_ON_ONCE(!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP));
592 rcu_segcblist_clear_flags(cblist, SEGCBLIST_KTHREAD_GP);
593 if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB))
594 *needwake_state = true;
600 * No-CBs GP kthreads come here to wait for additional callbacks to show up
601 * or for grace periods to end.
603 static void nocb_gp_wait(struct rcu_data *my_rdp)
607 int __maybe_unused cpu = my_rdp->cpu;
608 unsigned long cur_gp_seq;
611 unsigned long j = jiffies;
612 bool needwait_gp = false; // This prevents actual uninitialized use.
615 struct rcu_data *rdp;
616 struct rcu_node *rnp;
617 unsigned long wait_gp_seq = 0; // Suppress "use uninitialized" warning.
618 bool wasempty = false;
621 * Each pass through the following loop checks for CBs and for the
622 * nearest grace period (if any) to wait for next. The CB kthreads
623 * and the global grace-period kthread are awakened if needed.
625 WARN_ON_ONCE(my_rdp->nocb_gp_rdp != my_rdp);
627 * An rcu_data structure is removed from the list after its
628 * CPU is de-offloaded and added to the list before that CPU is
629 * (re-)offloaded. If the following loop happens to be referencing
630 * that rcu_data structure during the time that the corresponding
631 * CPU is de-offloaded and then immediately re-offloaded, this
632 * loop's rdp pointer will be carried to the end of the list by
633 * the resulting pair of list operations. This can cause the loop
634 * to skip over some of the rcu_data structures that were supposed
635 * to have been scanned. Fortunately a new iteration through the
636 * entire loop is forced after a given CPU's rcu_data structure
637 * is added to the list, so the skipped-over rcu_data structures
638 * won't be ignored for long.
640 list_for_each_entry_rcu(rdp, &my_rdp->nocb_head_rdp, nocb_entry_rdp, 1) {
641 bool needwake_state = false;
643 if (!nocb_gp_enabled_cb(rdp))
645 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Check"));
646 rcu_nocb_lock_irqsave(rdp, flags);
647 if (nocb_gp_update_state_deoffloading(rdp, &needwake_state)) {
648 rcu_nocb_unlock_irqrestore(rdp, flags);
650 swake_up_one(&rdp->nocb_state_wq);
653 bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
655 (time_after(j, READ_ONCE(rdp->nocb_bypass_first) + 1) ||
656 bypass_ncbs > 2 * qhimark)) {
657 // Bypass full or old, so flush it.
658 (void)rcu_nocb_try_flush_bypass(rdp, j);
659 bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
660 } else if (!bypass_ncbs && rcu_segcblist_empty(&rdp->cblist)) {
661 rcu_nocb_unlock_irqrestore(rdp, flags);
663 swake_up_one(&rdp->nocb_state_wq);
664 continue; /* No callbacks here, try next. */
667 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
673 // Advance callbacks if helpful and low contention.
675 if (!rcu_segcblist_restempty(&rdp->cblist,
676 RCU_NEXT_READY_TAIL) ||
677 (rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
678 rcu_seq_done(&rnp->gp_seq, cur_gp_seq))) {
679 raw_spin_lock_rcu_node(rnp); /* irqs disabled. */
680 needwake_gp = rcu_advance_cbs(rnp, rdp);
681 wasempty = rcu_segcblist_restempty(&rdp->cblist,
682 RCU_NEXT_READY_TAIL);
683 raw_spin_unlock_rcu_node(rnp); /* irqs disabled. */
685 // Need to wait on some grace period?
686 WARN_ON_ONCE(wasempty &&
687 !rcu_segcblist_restempty(&rdp->cblist,
688 RCU_NEXT_READY_TAIL));
689 if (rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq)) {
691 ULONG_CMP_LT(cur_gp_seq, wait_gp_seq))
692 wait_gp_seq = cur_gp_seq;
694 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
697 if (rcu_segcblist_ready_cbs(&rdp->cblist)) {
698 needwake = rdp->nocb_cb_sleep;
699 WRITE_ONCE(rdp->nocb_cb_sleep, false);
700 smp_mb(); /* CB invocation -after- GP end. */
704 rcu_nocb_unlock_irqrestore(rdp, flags);
706 swake_up_one(&rdp->nocb_cb_wq);
710 rcu_gp_kthread_wake();
712 swake_up_one(&rdp->nocb_state_wq);
715 my_rdp->nocb_gp_bypass = bypass;
716 my_rdp->nocb_gp_gp = needwait_gp;
717 my_rdp->nocb_gp_seq = needwait_gp ? wait_gp_seq : 0;
719 if (bypass && !rcu_nocb_poll) {
720 // At least one child with non-empty ->nocb_bypass, so set
721 // timer in order to avoid stranding its callbacks.
722 wake_nocb_gp_defer(my_rdp, RCU_NOCB_WAKE_BYPASS,
723 TPS("WakeBypassIsDeferred"));
726 /* Polling, so trace if first poll in the series. */
728 trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("Poll"));
729 schedule_timeout_idle(1);
730 } else if (!needwait_gp) {
731 /* Wait for callbacks to appear. */
732 trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("Sleep"));
733 swait_event_interruptible_exclusive(my_rdp->nocb_gp_wq,
734 !READ_ONCE(my_rdp->nocb_gp_sleep));
735 trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("EndSleep"));
737 rnp = my_rdp->mynode;
738 trace_rcu_this_gp(rnp, my_rdp, wait_gp_seq, TPS("StartWait"));
739 swait_event_interruptible_exclusive(
740 rnp->nocb_gp_wq[rcu_seq_ctr(wait_gp_seq) & 0x1],
741 rcu_seq_done(&rnp->gp_seq, wait_gp_seq) ||
742 !READ_ONCE(my_rdp->nocb_gp_sleep));
743 trace_rcu_this_gp(rnp, my_rdp, wait_gp_seq, TPS("EndWait"));
745 if (!rcu_nocb_poll) {
746 raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
747 if (my_rdp->nocb_defer_wakeup > RCU_NOCB_WAKE_NOT) {
748 WRITE_ONCE(my_rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
749 del_timer(&my_rdp->nocb_timer);
751 WRITE_ONCE(my_rdp->nocb_gp_sleep, true);
752 raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
754 my_rdp->nocb_gp_seq = -1;
755 WARN_ON(signal_pending(current));
759 * No-CBs grace-period-wait kthread. There is one of these per group
760 * of CPUs, but only once at least one CPU in that group has come online
761 * at least once since boot. This kthread checks for newly posted
762 * callbacks from any of the CPUs it is responsible for, waits for a
763 * grace period, then awakens all of the rcu_nocb_cb_kthread() instances
764 * that then have callback-invocation work to do.
766 static int rcu_nocb_gp_kthread(void *arg)
768 struct rcu_data *rdp = arg;
771 WRITE_ONCE(rdp->nocb_gp_loops, rdp->nocb_gp_loops + 1);
773 cond_resched_tasks_rcu_qs();
778 static inline bool nocb_cb_can_run(struct rcu_data *rdp)
780 u8 flags = SEGCBLIST_OFFLOADED | SEGCBLIST_KTHREAD_CB;
782 return rcu_segcblist_test_flags(&rdp->cblist, flags);
785 static inline bool nocb_cb_wait_cond(struct rcu_data *rdp)
787 return nocb_cb_can_run(rdp) && !READ_ONCE(rdp->nocb_cb_sleep);
791 * Invoke any ready callbacks from the corresponding no-CBs CPU,
792 * then, if there are no more, wait for more to appear.
794 static void nocb_cb_wait(struct rcu_data *rdp)
796 struct rcu_segcblist *cblist = &rdp->cblist;
797 unsigned long cur_gp_seq;
799 bool needwake_state = false;
800 bool needwake_gp = false;
801 bool can_sleep = true;
802 struct rcu_node *rnp = rdp->mynode;
805 swait_event_interruptible_exclusive(rdp->nocb_cb_wq,
806 nocb_cb_wait_cond(rdp));
808 // VVV Ensure CB invocation follows _sleep test.
809 if (smp_load_acquire(&rdp->nocb_cb_sleep)) { // ^^^
810 WARN_ON(signal_pending(current));
811 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeEmpty"));
813 } while (!nocb_cb_can_run(rdp));
816 local_irq_save(flags);
817 rcu_momentary_dyntick_idle();
818 local_irq_restore(flags);
820 * Disable BH to provide the expected environment. Also, when
821 * transitioning to/from NOCB mode, a self-requeuing callback might
822 * be invoked from softirq. A short grace period could cause both
823 * instances of this callback would execute concurrently.
828 lockdep_assert_irqs_enabled();
829 rcu_nocb_lock_irqsave(rdp, flags);
830 if (rcu_segcblist_nextgp(cblist, &cur_gp_seq) &&
831 rcu_seq_done(&rnp->gp_seq, cur_gp_seq) &&
832 raw_spin_trylock_rcu_node(rnp)) { /* irqs already disabled. */
833 needwake_gp = rcu_advance_cbs(rdp->mynode, rdp);
834 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
837 if (rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED)) {
838 if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB)) {
839 rcu_segcblist_set_flags(cblist, SEGCBLIST_KTHREAD_CB);
840 if (rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP))
841 needwake_state = true;
843 if (rcu_segcblist_ready_cbs(cblist))
847 * De-offloading. Clear our flag and notify the de-offload worker.
848 * We won't touch the callbacks and keep sleeping until we ever
851 WARN_ON_ONCE(!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB));
852 rcu_segcblist_clear_flags(cblist, SEGCBLIST_KTHREAD_CB);
853 if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP))
854 needwake_state = true;
857 WRITE_ONCE(rdp->nocb_cb_sleep, can_sleep);
859 if (rdp->nocb_cb_sleep)
860 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("CBSleep"));
862 rcu_nocb_unlock_irqrestore(rdp, flags);
864 rcu_gp_kthread_wake();
867 swake_up_one(&rdp->nocb_state_wq);
871 * Per-rcu_data kthread, but only for no-CBs CPUs. Repeatedly invoke
872 * nocb_cb_wait() to do the dirty work.
874 static int rcu_nocb_cb_kthread(void *arg)
876 struct rcu_data *rdp = arg;
878 // Each pass through this loop does one callback batch, and,
879 // if there are no more ready callbacks, waits for them.
882 cond_resched_tasks_rcu_qs();
887 /* Is a deferred wakeup of rcu_nocb_kthread() required? */
888 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level)
890 return READ_ONCE(rdp->nocb_defer_wakeup) >= level;
893 /* Do a deferred wakeup of rcu_nocb_kthread(). */
894 static bool do_nocb_deferred_wakeup_common(struct rcu_data *rdp_gp,
895 struct rcu_data *rdp, int level,
897 __releases(rdp_gp->nocb_gp_lock)
902 if (!rcu_nocb_need_deferred_wakeup(rdp_gp, level)) {
903 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
907 ndw = rdp_gp->nocb_defer_wakeup;
908 ret = __wake_nocb_gp(rdp_gp, rdp, ndw == RCU_NOCB_WAKE_FORCE, flags);
909 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DeferredWake"));
914 /* Do a deferred wakeup of rcu_nocb_kthread() from a timer handler. */
915 static void do_nocb_deferred_wakeup_timer(struct timer_list *t)
918 struct rcu_data *rdp = from_timer(rdp, t, nocb_timer);
920 WARN_ON_ONCE(rdp->nocb_gp_rdp != rdp);
921 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Timer"));
923 raw_spin_lock_irqsave(&rdp->nocb_gp_lock, flags);
924 smp_mb__after_spinlock(); /* Timer expire before wakeup. */
925 do_nocb_deferred_wakeup_common(rdp, rdp, RCU_NOCB_WAKE_BYPASS, flags);
929 * Do a deferred wakeup of rcu_nocb_kthread() from fastpath.
930 * This means we do an inexact common-case check. Note that if
931 * we miss, ->nocb_timer will eventually clean things up.
933 static bool do_nocb_deferred_wakeup(struct rcu_data *rdp)
936 struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
938 if (!rdp_gp || !rcu_nocb_need_deferred_wakeup(rdp_gp, RCU_NOCB_WAKE))
941 raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
942 return do_nocb_deferred_wakeup_common(rdp_gp, rdp, RCU_NOCB_WAKE, flags);
945 void rcu_nocb_flush_deferred_wakeup(void)
947 do_nocb_deferred_wakeup(this_cpu_ptr(&rcu_data));
949 EXPORT_SYMBOL_GPL(rcu_nocb_flush_deferred_wakeup);
951 static int rdp_offload_toggle(struct rcu_data *rdp,
952 bool offload, unsigned long flags)
953 __releases(rdp->nocb_lock)
955 struct rcu_segcblist *cblist = &rdp->cblist;
956 struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
957 bool wake_gp = false;
959 rcu_segcblist_offload(cblist, offload);
961 if (rdp->nocb_cb_sleep)
962 rdp->nocb_cb_sleep = false;
963 rcu_nocb_unlock_irqrestore(rdp, flags);
966 * Ignore former value of nocb_cb_sleep and force wake up as it could
967 * have been spuriously set to false already.
969 swake_up_one(&rdp->nocb_cb_wq);
971 raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
972 if (rdp_gp->nocb_gp_sleep) {
973 rdp_gp->nocb_gp_sleep = false;
976 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
979 wake_up_process(rdp_gp->nocb_gp_kthread);
984 static long rcu_nocb_rdp_deoffload(void *arg)
986 struct rcu_data *rdp = arg;
987 struct rcu_segcblist *cblist = &rdp->cblist;
991 WARN_ON_ONCE(rdp->cpu != raw_smp_processor_id());
993 pr_info("De-offloading %d\n", rdp->cpu);
995 rcu_nocb_lock_irqsave(rdp, flags);
997 * Flush once and for all now. This suffices because we are
998 * running on the target CPU holding ->nocb_lock (thus having
999 * interrupts disabled), and because rdp_offload_toggle()
1000 * invokes rcu_segcblist_offload(), which clears SEGCBLIST_OFFLOADED.
1001 * Thus future calls to rcu_segcblist_completely_offloaded() will
1002 * return false, which means that future calls to rcu_nocb_try_bypass()
1003 * will refuse to put anything into the bypass.
1005 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies));
1007 * Start with invoking rcu_core() early. This way if the current thread
1008 * happens to preempt an ongoing call to rcu_core() in the middle,
1009 * leaving some work dismissed because rcu_core() still thinks the rdp is
1010 * completely offloaded, we are guaranteed a nearby future instance of
1011 * rcu_core() to catch up.
1013 rcu_segcblist_set_flags(cblist, SEGCBLIST_RCU_CORE);
1015 ret = rdp_offload_toggle(rdp, false, flags);
1016 swait_event_exclusive(rdp->nocb_state_wq,
1017 !rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB |
1018 SEGCBLIST_KTHREAD_GP));
1019 /* Stop nocb_gp_wait() from iterating over this structure. */
1020 list_del_rcu(&rdp->nocb_entry_rdp);
1022 * Lock one last time to acquire latest callback updates from kthreads
1023 * so we can later handle callbacks locally without locking.
1025 rcu_nocb_lock_irqsave(rdp, flags);
1027 * Theoretically we could clear SEGCBLIST_LOCKING after the nocb
1028 * lock is released but how about being paranoid for once?
1030 rcu_segcblist_clear_flags(cblist, SEGCBLIST_LOCKING);
1032 * Without SEGCBLIST_LOCKING, we can't use
1033 * rcu_nocb_unlock_irqrestore() anymore.
1035 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
1038 WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
1044 int rcu_nocb_cpu_deoffload(int cpu)
1046 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1049 mutex_lock(&rcu_state.barrier_mutex);
1051 if (rcu_rdp_is_offloaded(rdp)) {
1052 if (cpu_online(cpu)) {
1053 ret = work_on_cpu(cpu, rcu_nocb_rdp_deoffload, rdp);
1055 cpumask_clear_cpu(cpu, rcu_nocb_mask);
1057 pr_info("NOCB: Can't CB-deoffload an offline CPU\n");
1062 mutex_unlock(&rcu_state.barrier_mutex);
1066 EXPORT_SYMBOL_GPL(rcu_nocb_cpu_deoffload);
1068 static long rcu_nocb_rdp_offload(void *arg)
1070 struct rcu_data *rdp = arg;
1071 struct rcu_segcblist *cblist = &rdp->cblist;
1072 unsigned long flags;
1075 WARN_ON_ONCE(rdp->cpu != raw_smp_processor_id());
1077 * For now we only support re-offload, ie: the rdp must have been
1078 * offloaded on boot first.
1080 if (!rdp->nocb_gp_rdp)
1083 pr_info("Offloading %d\n", rdp->cpu);
1086 * Cause future nocb_gp_wait() invocations to iterate over
1087 * structure, resetting ->nocb_gp_sleep and waking up the related
1088 * "rcuog". Since nocb_gp_wait() in turn locks ->nocb_gp_lock
1089 * before setting ->nocb_gp_sleep again, we are guaranteed to
1090 * iterate this newly added structure before "rcuog" goes to
1093 list_add_tail_rcu(&rdp->nocb_entry_rdp, &rdp->nocb_gp_rdp->nocb_head_rdp);
1096 * Can't use rcu_nocb_lock_irqsave() before SEGCBLIST_LOCKING
1099 raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
1102 * We didn't take the nocb lock while working on the
1103 * rdp->cblist with SEGCBLIST_LOCKING cleared (pure softirq/rcuc mode).
1104 * Every modifications that have been done previously on
1105 * rdp->cblist must be visible remotely by the nocb kthreads
1106 * upon wake up after reading the cblist flags.
1108 * The layout against nocb_lock enforces that ordering:
1110 * __rcu_nocb_rdp_offload() nocb_cb_wait()/nocb_gp_wait()
1111 * ------------------------- ----------------------------
1112 * WRITE callbacks rcu_nocb_lock()
1113 * rcu_nocb_lock() READ flags
1114 * WRITE flags READ callbacks
1115 * rcu_nocb_unlock() rcu_nocb_unlock()
1117 ret = rdp_offload_toggle(rdp, true, flags);
1118 swait_event_exclusive(rdp->nocb_state_wq,
1119 rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB) &&
1120 rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP));
1123 * All kthreads are ready to work, we can finally relieve rcu_core() and
1124 * enable nocb bypass.
1126 rcu_nocb_lock_irqsave(rdp, flags);
1127 rcu_segcblist_clear_flags(cblist, SEGCBLIST_RCU_CORE);
1128 rcu_nocb_unlock_irqrestore(rdp, flags);
1133 int rcu_nocb_cpu_offload(int cpu)
1135 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1138 mutex_lock(&rcu_state.barrier_mutex);
1140 if (!rcu_rdp_is_offloaded(rdp)) {
1141 if (cpu_online(cpu)) {
1142 ret = work_on_cpu(cpu, rcu_nocb_rdp_offload, rdp);
1144 cpumask_set_cpu(cpu, rcu_nocb_mask);
1146 pr_info("NOCB: Can't CB-offload an offline CPU\n");
1151 mutex_unlock(&rcu_state.barrier_mutex);
1155 EXPORT_SYMBOL_GPL(rcu_nocb_cpu_offload);
1157 void __init rcu_init_nohz(void)
1160 bool need_rcu_nocb_mask = false;
1161 struct rcu_data *rdp;
1163 #if defined(CONFIG_NO_HZ_FULL)
1164 if (tick_nohz_full_running && !cpumask_empty(tick_nohz_full_mask))
1165 need_rcu_nocb_mask = true;
1166 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
1168 if (need_rcu_nocb_mask) {
1169 if (!cpumask_available(rcu_nocb_mask)) {
1170 if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
1171 pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
1175 rcu_nocb_is_setup = true;
1178 if (!rcu_nocb_is_setup)
1181 #if defined(CONFIG_NO_HZ_FULL)
1182 if (tick_nohz_full_running)
1183 cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
1184 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
1186 if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
1187 pr_info("\tNote: kernel parameter 'rcu_nocbs=', 'nohz_full', or 'isolcpus=' contains nonexistent CPUs.\n");
1188 cpumask_and(rcu_nocb_mask, cpu_possible_mask,
1191 if (cpumask_empty(rcu_nocb_mask))
1192 pr_info("\tOffload RCU callbacks from CPUs: (none).\n");
1194 pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
1195 cpumask_pr_args(rcu_nocb_mask));
1197 pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
1199 for_each_cpu(cpu, rcu_nocb_mask) {
1200 rdp = per_cpu_ptr(&rcu_data, cpu);
1201 if (rcu_segcblist_empty(&rdp->cblist))
1202 rcu_segcblist_init(&rdp->cblist);
1203 rcu_segcblist_offload(&rdp->cblist, true);
1204 rcu_segcblist_set_flags(&rdp->cblist, SEGCBLIST_KTHREAD_CB | SEGCBLIST_KTHREAD_GP);
1205 rcu_segcblist_clear_flags(&rdp->cblist, SEGCBLIST_RCU_CORE);
1207 rcu_organize_nocb_kthreads();
1210 /* Initialize per-rcu_data variables for no-CBs CPUs. */
1211 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
1213 init_swait_queue_head(&rdp->nocb_cb_wq);
1214 init_swait_queue_head(&rdp->nocb_gp_wq);
1215 init_swait_queue_head(&rdp->nocb_state_wq);
1216 raw_spin_lock_init(&rdp->nocb_lock);
1217 raw_spin_lock_init(&rdp->nocb_bypass_lock);
1218 raw_spin_lock_init(&rdp->nocb_gp_lock);
1219 timer_setup(&rdp->nocb_timer, do_nocb_deferred_wakeup_timer, 0);
1220 rcu_cblist_init(&rdp->nocb_bypass);
1221 mutex_init(&rdp->nocb_gp_kthread_mutex);
1225 * If the specified CPU is a no-CBs CPU that does not already have its
1226 * rcuo CB kthread, spawn it. Additionally, if the rcuo GP kthread
1227 * for this CPU's group has not yet been created, spawn it as well.
1229 static void rcu_spawn_cpu_nocb_kthread(int cpu)
1231 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1232 struct rcu_data *rdp_gp;
1233 struct task_struct *t;
1234 struct sched_param sp;
1236 if (!rcu_scheduler_fully_active || !rcu_nocb_is_setup)
1239 /* If there already is an rcuo kthread, then nothing to do. */
1240 if (rdp->nocb_cb_kthread)
1243 /* If we didn't spawn the GP kthread first, reorganize! */
1244 sp.sched_priority = kthread_prio;
1245 rdp_gp = rdp->nocb_gp_rdp;
1246 mutex_lock(&rdp_gp->nocb_gp_kthread_mutex);
1247 if (!rdp_gp->nocb_gp_kthread) {
1248 t = kthread_run(rcu_nocb_gp_kthread, rdp_gp,
1249 "rcuog/%d", rdp_gp->cpu);
1250 if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo GP kthread, OOM is now expected behavior\n", __func__)) {
1251 mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
1254 WRITE_ONCE(rdp_gp->nocb_gp_kthread, t);
1256 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1258 mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
1260 /* Spawn the kthread for this CPU. */
1261 t = kthread_run(rcu_nocb_cb_kthread, rdp,
1262 "rcuo%c/%d", rcu_state.abbr, cpu);
1263 if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo CB kthread, OOM is now expected behavior\n", __func__))
1267 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1268 WRITE_ONCE(rdp->nocb_cb_kthread, t);
1269 WRITE_ONCE(rdp->nocb_gp_kthread, rdp_gp->nocb_gp_kthread);
1273 * Once the scheduler is running, spawn rcuo kthreads for all online
1274 * no-CBs CPUs. This assumes that the early_initcall()s happen before
1275 * non-boot CPUs come online -- if this changes, we will need to add
1276 * some mutual exclusion.
1278 static void __init rcu_spawn_nocb_kthreads(void)
1282 if (rcu_nocb_is_setup) {
1283 for_each_online_cpu(cpu)
1284 rcu_spawn_cpu_nocb_kthread(cpu);
1288 /* How many CB CPU IDs per GP kthread? Default of -1 for sqrt(nr_cpu_ids). */
1289 static int rcu_nocb_gp_stride = -1;
1290 module_param(rcu_nocb_gp_stride, int, 0444);
1293 * Initialize GP-CB relationships for all no-CBs CPU.
1295 static void __init rcu_organize_nocb_kthreads(void)
1298 bool firsttime = true;
1299 bool gotnocbs = false;
1300 bool gotnocbscbs = true;
1301 int ls = rcu_nocb_gp_stride;
1302 int nl = 0; /* Next GP kthread. */
1303 struct rcu_data *rdp;
1304 struct rcu_data *rdp_gp = NULL; /* Suppress misguided gcc warn. */
1306 if (!cpumask_available(rcu_nocb_mask))
1309 ls = nr_cpu_ids / int_sqrt(nr_cpu_ids);
1310 rcu_nocb_gp_stride = ls;
1314 * Each pass through this loop sets up one rcu_data structure.
1315 * Should the corresponding CPU come online in the future, then
1316 * we will spawn the needed set of rcu_nocb_kthread() kthreads.
1318 for_each_possible_cpu(cpu) {
1319 rdp = per_cpu_ptr(&rcu_data, cpu);
1320 if (rdp->cpu >= nl) {
1321 /* New GP kthread, set up for CBs & next GP. */
1323 nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
1325 INIT_LIST_HEAD(&rdp->nocb_head_rdp);
1328 pr_cont("%s\n", gotnocbscbs
1329 ? "" : " (self only)");
1330 gotnocbscbs = false;
1332 pr_alert("%s: No-CB GP kthread CPU %d:",
1336 /* Another CB kthread, link to previous GP kthread. */
1339 pr_cont(" %d", cpu);
1341 rdp->nocb_gp_rdp = rdp_gp;
1342 if (cpumask_test_cpu(cpu, rcu_nocb_mask))
1343 list_add_tail(&rdp->nocb_entry_rdp, &rdp_gp->nocb_head_rdp);
1345 if (gotnocbs && dump_tree)
1346 pr_cont("%s\n", gotnocbscbs ? "" : " (self only)");
1350 * Bind the current task to the offloaded CPUs. If there are no offloaded
1351 * CPUs, leave the task unbound. Splat if the bind attempt fails.
1353 void rcu_bind_current_to_nocb(void)
1355 if (cpumask_available(rcu_nocb_mask) && !cpumask_empty(rcu_nocb_mask))
1356 WARN_ON(sched_setaffinity(current->pid, rcu_nocb_mask));
1358 EXPORT_SYMBOL_GPL(rcu_bind_current_to_nocb);
1360 // The ->on_cpu field is available only in CONFIG_SMP=y, so...
1362 static char *show_rcu_should_be_on_cpu(struct task_struct *tsp)
1364 return tsp && task_is_running(tsp) && !tsp->on_cpu ? "!" : "";
1366 #else // #ifdef CONFIG_SMP
1367 static char *show_rcu_should_be_on_cpu(struct task_struct *tsp)
1371 #endif // #else #ifdef CONFIG_SMP
1374 * Dump out nocb grace-period kthread state for the specified rcu_data
1377 static void show_rcu_nocb_gp_state(struct rcu_data *rdp)
1379 struct rcu_node *rnp = rdp->mynode;
1381 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",
1383 "kK"[!!rdp->nocb_gp_kthread],
1384 "lL"[raw_spin_is_locked(&rdp->nocb_gp_lock)],
1385 "dD"[!!rdp->nocb_defer_wakeup],
1386 "tT"[timer_pending(&rdp->nocb_timer)],
1387 "sS"[!!rdp->nocb_gp_sleep],
1388 ".W"[swait_active(&rdp->nocb_gp_wq)],
1389 ".W"[swait_active(&rnp->nocb_gp_wq[0])],
1390 ".W"[swait_active(&rnp->nocb_gp_wq[1])],
1391 ".B"[!!rdp->nocb_gp_bypass],
1392 ".G"[!!rdp->nocb_gp_gp],
1393 (long)rdp->nocb_gp_seq,
1394 rnp->grplo, rnp->grphi, READ_ONCE(rdp->nocb_gp_loops),
1395 rdp->nocb_gp_kthread ? task_state_to_char(rdp->nocb_gp_kthread) : '.',
1396 rdp->nocb_cb_kthread ? (int)task_cpu(rdp->nocb_gp_kthread) : -1,
1397 show_rcu_should_be_on_cpu(rdp->nocb_cb_kthread));
1400 /* Dump out nocb kthread state for the specified rcu_data structure. */
1401 static void show_rcu_nocb_state(struct rcu_data *rdp)
1405 struct rcu_data *nocb_next_rdp;
1406 struct rcu_segcblist *rsclp = &rdp->cblist;
1410 if (rdp->nocb_gp_rdp == rdp)
1411 show_rcu_nocb_gp_state(rdp);
1413 nocb_next_rdp = list_next_or_null_rcu(&rdp->nocb_gp_rdp->nocb_head_rdp,
1414 &rdp->nocb_entry_rdp,
1418 sprintf(bufw, "%ld", rsclp->gp_seq[RCU_WAIT_TAIL]);
1419 sprintf(bufr, "%ld", rsclp->gp_seq[RCU_NEXT_READY_TAIL]);
1420 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",
1421 rdp->cpu, rdp->nocb_gp_rdp->cpu,
1422 nocb_next_rdp ? nocb_next_rdp->cpu : -1,
1423 "kK"[!!rdp->nocb_cb_kthread],
1424 "bB"[raw_spin_is_locked(&rdp->nocb_bypass_lock)],
1425 "cC"[!!atomic_read(&rdp->nocb_lock_contended)],
1426 "lL"[raw_spin_is_locked(&rdp->nocb_lock)],
1427 "sS"[!!rdp->nocb_cb_sleep],
1428 ".W"[swait_active(&rdp->nocb_cb_wq)],
1429 jiffies - rdp->nocb_bypass_first,
1430 jiffies - rdp->nocb_nobypass_last,
1431 rdp->nocb_nobypass_count,
1432 ".D"[rcu_segcblist_ready_cbs(rsclp)],
1433 ".W"[!rcu_segcblist_segempty(rsclp, RCU_WAIT_TAIL)],
1434 rcu_segcblist_segempty(rsclp, RCU_WAIT_TAIL) ? "" : bufw,
1435 ".R"[!rcu_segcblist_segempty(rsclp, RCU_NEXT_READY_TAIL)],
1436 rcu_segcblist_segempty(rsclp, RCU_NEXT_READY_TAIL) ? "" : bufr,
1437 ".N"[!rcu_segcblist_segempty(rsclp, RCU_NEXT_TAIL)],
1438 ".B"[!!rcu_cblist_n_cbs(&rdp->nocb_bypass)],
1439 rcu_segcblist_n_cbs(&rdp->cblist),
1440 rdp->nocb_cb_kthread ? task_state_to_char(rdp->nocb_cb_kthread) : '.',
1441 rdp->nocb_cb_kthread ? (int)task_cpu(rdp->nocb_gp_kthread) : -1,
1442 show_rcu_should_be_on_cpu(rdp->nocb_cb_kthread));
1444 /* It is OK for GP kthreads to have GP state. */
1445 if (rdp->nocb_gp_rdp == rdp)
1448 waslocked = raw_spin_is_locked(&rdp->nocb_gp_lock);
1449 wassleep = swait_active(&rdp->nocb_gp_wq);
1450 if (!rdp->nocb_gp_sleep && !waslocked && !wassleep)
1451 return; /* Nothing untoward. */
1453 pr_info(" nocb GP activity on CB-only CPU!!! %c%c%c %c\n",
1455 "dD"[!!rdp->nocb_defer_wakeup],
1456 "sS"[!!rdp->nocb_gp_sleep],
1460 #else /* #ifdef CONFIG_RCU_NOCB_CPU */
1462 static inline int rcu_lockdep_is_held_nocb(struct rcu_data *rdp)
1467 static inline bool rcu_current_is_nocb_kthread(struct rcu_data *rdp)
1472 /* No ->nocb_lock to acquire. */
1473 static void rcu_nocb_lock(struct rcu_data *rdp)
1477 /* No ->nocb_lock to release. */
1478 static void rcu_nocb_unlock(struct rcu_data *rdp)
1482 /* No ->nocb_lock to release. */
1483 static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
1484 unsigned long flags)
1486 local_irq_restore(flags);
1489 /* Lockdep check that ->cblist may be safely accessed. */
1490 static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp)
1492 lockdep_assert_irqs_disabled();
1495 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
1499 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
1504 static void rcu_init_one_nocb(struct rcu_node *rnp)
1508 static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
1514 static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
1515 bool *was_alldone, unsigned long flags)
1520 static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_empty,
1521 unsigned long flags)
1523 WARN_ON_ONCE(1); /* Should be dead code! */
1526 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
1530 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level)
1535 static bool do_nocb_deferred_wakeup(struct rcu_data *rdp)
1540 static void rcu_spawn_cpu_nocb_kthread(int cpu)
1544 static void __init rcu_spawn_nocb_kthreads(void)
1548 static void show_rcu_nocb_state(struct rcu_data *rdp)
1552 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */