2 * Read-Copy Update mechanism for mutual exclusion (tree-based version)
3 * Internal non-public definitions that provide either classic
4 * or preemptible semantics.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can access it online at
18 * http://www.gnu.org/licenses/gpl-2.0.html.
20 * Copyright Red Hat, 2009
21 * Copyright IBM Corporation, 2009
23 * Author: Ingo Molnar <mingo@elte.hu>
24 * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
27 #ifdef CONFIG_RCU_BOOST
29 #include "../locking/rtmutex_common.h"
31 #endif /* #ifdef CONFIG_RCU_BOOST */
34 * Control variables for per-CPU and per-rcu_node kthreads. These
35 * handle all flavors of RCU.
37 DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
38 DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
39 DEFINE_PER_CPU(char, rcu_cpu_has_work);
41 #ifdef CONFIG_RCU_NOCB_CPU
42 static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
43 static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
44 static bool __read_mostly rcu_nocb_poll; /* Offload kthread are to poll. */
45 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
48 * Check the RCU kernel configuration parameters and print informative
49 * messages about anything out of the ordinary. If you like #ifdef, you
50 * will love this function.
52 static void __init rcu_bootup_announce_oddness(void)
54 if (IS_ENABLED(CONFIG_RCU_TRACE))
55 pr_info("\tRCU debugfs-based tracing is enabled.\n");
56 if ((IS_ENABLED(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) ||
57 (!IS_ENABLED(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32))
58 pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
60 if (IS_ENABLED(CONFIG_RCU_FANOUT_EXACT))
61 pr_info("\tHierarchical RCU autobalancing is disabled.\n");
62 if (IS_ENABLED(CONFIG_RCU_FAST_NO_HZ))
63 pr_info("\tRCU dyntick-idle grace-period acceleration is enabled.\n");
64 if (IS_ENABLED(CONFIG_PROVE_RCU))
65 pr_info("\tRCU lockdep checking is enabled.\n");
66 if (IS_ENABLED(CONFIG_RCU_TORTURE_TEST_RUNNABLE))
67 pr_info("\tRCU torture testing starts during boot.\n");
68 if (IS_ENABLED(CONFIG_RCU_CPU_STALL_INFO))
69 pr_info("\tAdditional per-CPU info printed with stalls.\n");
70 if (NUM_RCU_LVL_4 != 0)
71 pr_info("\tFour-level hierarchy is enabled.\n");
72 if (CONFIG_RCU_FANOUT_LEAF != 16)
73 pr_info("\tBuild-time adjustment of leaf fanout to %d.\n",
74 CONFIG_RCU_FANOUT_LEAF);
75 if (rcu_fanout_leaf != CONFIG_RCU_FANOUT_LEAF)
76 pr_info("\tBoot-time adjustment of leaf fanout to %d.\n", rcu_fanout_leaf);
77 if (nr_cpu_ids != NR_CPUS)
78 pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%d.\n", NR_CPUS, nr_cpu_ids);
79 if (IS_ENABLED(CONFIG_RCU_BOOST))
80 pr_info("\tRCU kthread priority: %d.\n", kthread_prio);
83 #ifdef CONFIG_PREEMPT_RCU
85 RCU_STATE_INITIALIZER(rcu_preempt, 'p', call_rcu);
86 static struct rcu_state *rcu_state_p = &rcu_preempt_state;
88 static int rcu_preempted_readers_exp(struct rcu_node *rnp);
89 static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
93 * Tell them what RCU they are running.
95 static void __init rcu_bootup_announce(void)
97 pr_info("Preemptible hierarchical RCU implementation.\n");
98 rcu_bootup_announce_oddness();
102 * Record a preemptible-RCU quiescent state for the specified CPU. Note
103 * that this just means that the task currently running on the CPU is
104 * not in a quiescent state. There might be any number of tasks blocked
105 * while in an RCU read-side critical section.
107 * As with the other rcu_*_qs() functions, callers to this function
108 * must disable preemption.
110 static void rcu_preempt_qs(void)
112 if (!__this_cpu_read(rcu_preempt_data.passed_quiesce)) {
113 trace_rcu_grace_period(TPS("rcu_preempt"),
114 __this_cpu_read(rcu_preempt_data.gpnum),
116 __this_cpu_write(rcu_preempt_data.passed_quiesce, 1);
117 barrier(); /* Coordinate with rcu_preempt_check_callbacks(). */
118 current->rcu_read_unlock_special.b.need_qs = false;
123 * We have entered the scheduler, and the current task might soon be
124 * context-switched away from. If this task is in an RCU read-side
125 * critical section, we will no longer be able to rely on the CPU to
126 * record that fact, so we enqueue the task on the blkd_tasks list.
127 * The task will dequeue itself when it exits the outermost enclosing
128 * RCU read-side critical section. Therefore, the current grace period
129 * cannot be permitted to complete until the blkd_tasks list entries
130 * predating the current grace period drain, in other words, until
131 * rnp->gp_tasks becomes NULL.
133 * Caller must disable preemption.
135 static void rcu_preempt_note_context_switch(void)
137 struct task_struct *t = current;
139 struct rcu_data *rdp;
140 struct rcu_node *rnp;
142 if (t->rcu_read_lock_nesting > 0 &&
143 !t->rcu_read_unlock_special.b.blocked) {
145 /* Possibly blocking in an RCU read-side critical section. */
146 rdp = this_cpu_ptr(rcu_preempt_state.rda);
148 raw_spin_lock_irqsave(&rnp->lock, flags);
149 smp_mb__after_unlock_lock();
150 t->rcu_read_unlock_special.b.blocked = true;
151 t->rcu_blocked_node = rnp;
154 * If this CPU has already checked in, then this task
155 * will hold up the next grace period rather than the
156 * current grace period. Queue the task accordingly.
157 * If the task is queued for the current grace period
158 * (i.e., this CPU has not yet passed through a quiescent
159 * state for the current grace period), then as long
160 * as that task remains queued, the current grace period
161 * cannot end. Note that there is some uncertainty as
162 * to exactly when the current grace period started.
163 * We take a conservative approach, which can result
164 * in unnecessarily waiting on tasks that started very
165 * slightly after the current grace period began. C'est
168 * But first, note that the current CPU must still be
171 WARN_ON_ONCE((rdp->grpmask & rcu_rnp_online_cpus(rnp)) == 0);
172 WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
173 if ((rnp->qsmask & rdp->grpmask) && rnp->gp_tasks != NULL) {
174 list_add(&t->rcu_node_entry, rnp->gp_tasks->prev);
175 rnp->gp_tasks = &t->rcu_node_entry;
176 #ifdef CONFIG_RCU_BOOST
177 if (rnp->boost_tasks != NULL)
178 rnp->boost_tasks = rnp->gp_tasks;
179 #endif /* #ifdef CONFIG_RCU_BOOST */
181 list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
182 if (rnp->qsmask & rdp->grpmask)
183 rnp->gp_tasks = &t->rcu_node_entry;
185 trace_rcu_preempt_task(rdp->rsp->name,
187 (rnp->qsmask & rdp->grpmask)
190 raw_spin_unlock_irqrestore(&rnp->lock, flags);
191 } else if (t->rcu_read_lock_nesting < 0 &&
192 t->rcu_read_unlock_special.s) {
195 * Complete exit from RCU read-side critical section on
196 * behalf of preempted instance of __rcu_read_unlock().
198 rcu_read_unlock_special(t);
202 * Either we were not in an RCU read-side critical section to
203 * begin with, or we have now recorded that critical section
204 * globally. Either way, we can now note a quiescent state
205 * for this CPU. Again, if we were in an RCU read-side critical
206 * section, and if that critical section was blocking the current
207 * grace period, then the fact that the task has been enqueued
208 * means that we continue to block the current grace period.
214 * Check for preempted RCU readers blocking the current grace period
215 * for the specified rcu_node structure. If the caller needs a reliable
216 * answer, it must hold the rcu_node's ->lock.
218 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
220 return rnp->gp_tasks != NULL;
224 * Advance a ->blkd_tasks-list pointer to the next entry, instead
225 * returning NULL if at the end of the list.
227 static struct list_head *rcu_next_node_entry(struct task_struct *t,
228 struct rcu_node *rnp)
230 struct list_head *np;
232 np = t->rcu_node_entry.next;
233 if (np == &rnp->blkd_tasks)
239 * Return true if the specified rcu_node structure has tasks that were
240 * preempted within an RCU read-side critical section.
242 static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
244 return !list_empty(&rnp->blkd_tasks);
248 * Handle special cases during rcu_read_unlock(), such as needing to
249 * notify RCU core processing or task having blocked during the RCU
250 * read-side critical section.
252 void rcu_read_unlock_special(struct task_struct *t)
258 struct list_head *np;
259 #ifdef CONFIG_RCU_BOOST
260 bool drop_boost_mutex = false;
261 #endif /* #ifdef CONFIG_RCU_BOOST */
262 struct rcu_node *rnp;
263 union rcu_special special;
265 /* NMI handlers cannot block and cannot safely manipulate state. */
269 local_irq_save(flags);
272 * If RCU core is waiting for this CPU to exit critical section,
273 * let it know that we have done so. Because irqs are disabled,
274 * t->rcu_read_unlock_special cannot change.
276 special = t->rcu_read_unlock_special;
277 if (special.b.need_qs) {
279 t->rcu_read_unlock_special.b.need_qs = false;
280 if (!t->rcu_read_unlock_special.s) {
281 local_irq_restore(flags);
286 /* Hardware IRQ handlers cannot block, complain if they get here. */
287 if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
288 lockdep_rcu_suspicious(__FILE__, __LINE__,
289 "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
290 pr_alert("->rcu_read_unlock_special: %#x (b: %d, nq: %d)\n",
291 t->rcu_read_unlock_special.s,
292 t->rcu_read_unlock_special.b.blocked,
293 t->rcu_read_unlock_special.b.need_qs);
294 local_irq_restore(flags);
298 /* Clean up if blocked during RCU read-side critical section. */
299 if (special.b.blocked) {
300 t->rcu_read_unlock_special.b.blocked = false;
303 * Remove this task from the list it blocked on. The
304 * task can migrate while we acquire the lock, but at
305 * most one time. So at most two passes through loop.
308 rnp = t->rcu_blocked_node;
309 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
310 smp_mb__after_unlock_lock();
311 if (rnp == t->rcu_blocked_node)
313 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
315 empty_norm = !rcu_preempt_blocked_readers_cgp(rnp);
316 empty_exp = !rcu_preempted_readers_exp(rnp);
317 smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
318 np = rcu_next_node_entry(t, rnp);
319 list_del_init(&t->rcu_node_entry);
320 t->rcu_blocked_node = NULL;
321 trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
323 if (&t->rcu_node_entry == rnp->gp_tasks)
325 if (&t->rcu_node_entry == rnp->exp_tasks)
327 #ifdef CONFIG_RCU_BOOST
328 if (&t->rcu_node_entry == rnp->boost_tasks)
329 rnp->boost_tasks = np;
330 /* Snapshot ->boost_mtx ownership with rcu_node lock held. */
331 drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx) == t;
332 #endif /* #ifdef CONFIG_RCU_BOOST */
335 * If this was the last task on the current list, and if
336 * we aren't waiting on any CPUs, report the quiescent state.
337 * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
338 * so we must take a snapshot of the expedited state.
340 empty_exp_now = !rcu_preempted_readers_exp(rnp);
341 if (!empty_norm && !rcu_preempt_blocked_readers_cgp(rnp)) {
342 trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
349 rcu_report_unblock_qs_rnp(&rcu_preempt_state,
352 raw_spin_unlock_irqrestore(&rnp->lock, flags);
355 #ifdef CONFIG_RCU_BOOST
356 /* Unboost if we were boosted. */
357 if (drop_boost_mutex)
358 rt_mutex_unlock(&rnp->boost_mtx);
359 #endif /* #ifdef CONFIG_RCU_BOOST */
362 * If this was the last task on the expedited lists,
363 * then we need to report up the rcu_node hierarchy.
365 if (!empty_exp && empty_exp_now)
366 rcu_report_exp_rnp(&rcu_preempt_state, rnp, true);
368 local_irq_restore(flags);
373 * Dump detailed information for all tasks blocking the current RCU
374 * grace period on the specified rcu_node structure.
376 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
379 struct task_struct *t;
381 raw_spin_lock_irqsave(&rnp->lock, flags);
382 if (!rcu_preempt_blocked_readers_cgp(rnp)) {
383 raw_spin_unlock_irqrestore(&rnp->lock, flags);
386 t = list_entry(rnp->gp_tasks,
387 struct task_struct, rcu_node_entry);
388 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
390 raw_spin_unlock_irqrestore(&rnp->lock, flags);
394 * Dump detailed information for all tasks blocking the current RCU
397 static void rcu_print_detail_task_stall(struct rcu_state *rsp)
399 struct rcu_node *rnp = rcu_get_root(rsp);
401 rcu_print_detail_task_stall_rnp(rnp);
402 rcu_for_each_leaf_node(rsp, rnp)
403 rcu_print_detail_task_stall_rnp(rnp);
406 #ifdef CONFIG_RCU_CPU_STALL_INFO
408 static void rcu_print_task_stall_begin(struct rcu_node *rnp)
410 pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
411 rnp->level, rnp->grplo, rnp->grphi);
414 static void rcu_print_task_stall_end(void)
419 #else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
421 static void rcu_print_task_stall_begin(struct rcu_node *rnp)
425 static void rcu_print_task_stall_end(void)
429 #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
432 * Scan the current list of tasks blocked within RCU read-side critical
433 * sections, printing out the tid of each.
435 static int rcu_print_task_stall(struct rcu_node *rnp)
437 struct task_struct *t;
440 if (!rcu_preempt_blocked_readers_cgp(rnp))
442 rcu_print_task_stall_begin(rnp);
443 t = list_entry(rnp->gp_tasks,
444 struct task_struct, rcu_node_entry);
445 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
446 pr_cont(" P%d", t->pid);
449 rcu_print_task_stall_end();
454 * Check that the list of blocked tasks for the newly completed grace
455 * period is in fact empty. It is a serious bug to complete a grace
456 * period that still has RCU readers blocked! This function must be
457 * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
458 * must be held by the caller.
460 * Also, if there are blocked tasks on the list, they automatically
461 * block the newly created grace period, so set up ->gp_tasks accordingly.
463 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
465 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
466 if (rcu_preempt_has_tasks(rnp))
467 rnp->gp_tasks = rnp->blkd_tasks.next;
468 WARN_ON_ONCE(rnp->qsmask);
472 * Check for a quiescent state from the current CPU. When a task blocks,
473 * the task is recorded in the corresponding CPU's rcu_node structure,
474 * which is checked elsewhere.
476 * Caller must disable hard irqs.
478 static void rcu_preempt_check_callbacks(void)
480 struct task_struct *t = current;
482 if (t->rcu_read_lock_nesting == 0) {
486 if (t->rcu_read_lock_nesting > 0 &&
487 __this_cpu_read(rcu_preempt_data.qs_pending) &&
488 !__this_cpu_read(rcu_preempt_data.passed_quiesce))
489 t->rcu_read_unlock_special.b.need_qs = true;
493 * Queue a preemptible-RCU callback for invocation after a grace period.
495 void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
497 __call_rcu(head, func, &rcu_preempt_state, -1, 0);
499 EXPORT_SYMBOL_GPL(call_rcu);
502 * synchronize_rcu - wait until a grace period has elapsed.
504 * Control will return to the caller some time after a full grace
505 * period has elapsed, in other words after all currently executing RCU
506 * read-side critical sections have completed. Note, however, that
507 * upon return from synchronize_rcu(), the caller might well be executing
508 * concurrently with new RCU read-side critical sections that began while
509 * synchronize_rcu() was waiting. RCU read-side critical sections are
510 * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
512 * See the description of synchronize_sched() for more detailed information
513 * on memory ordering guarantees.
515 void synchronize_rcu(void)
517 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
518 !lock_is_held(&rcu_lock_map) &&
519 !lock_is_held(&rcu_sched_lock_map),
520 "Illegal synchronize_rcu() in RCU read-side critical section");
521 if (!rcu_scheduler_active)
523 if (rcu_gp_is_expedited())
524 synchronize_rcu_expedited();
526 wait_rcu_gp(call_rcu);
528 EXPORT_SYMBOL_GPL(synchronize_rcu);
530 static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
531 static unsigned long sync_rcu_preempt_exp_count;
532 static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
535 * Return non-zero if there are any tasks in RCU read-side critical
536 * sections blocking the current preemptible-RCU expedited grace period.
537 * If there is no preemptible-RCU expedited grace period currently in
538 * progress, returns zero unconditionally.
540 static int rcu_preempted_readers_exp(struct rcu_node *rnp)
542 return rnp->exp_tasks != NULL;
546 * return non-zero if there is no RCU expedited grace period in progress
547 * for the specified rcu_node structure, in other words, if all CPUs and
548 * tasks covered by the specified rcu_node structure have done their bit
549 * for the current expedited grace period. Works only for preemptible
550 * RCU -- other RCU implementation use other means.
552 * Caller must hold sync_rcu_preempt_exp_mutex.
554 static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
556 return !rcu_preempted_readers_exp(rnp) &&
557 ACCESS_ONCE(rnp->expmask) == 0;
561 * Report the exit from RCU read-side critical section for the last task
562 * that queued itself during or before the current expedited preemptible-RCU
563 * grace period. This event is reported either to the rcu_node structure on
564 * which the task was queued or to one of that rcu_node structure's ancestors,
565 * recursively up the tree. (Calm down, calm down, we do the recursion
568 * Caller must hold sync_rcu_preempt_exp_mutex.
570 static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
576 raw_spin_lock_irqsave(&rnp->lock, flags);
577 smp_mb__after_unlock_lock();
579 if (!sync_rcu_preempt_exp_done(rnp)) {
580 raw_spin_unlock_irqrestore(&rnp->lock, flags);
583 if (rnp->parent == NULL) {
584 raw_spin_unlock_irqrestore(&rnp->lock, flags);
586 smp_mb(); /* EGP done before wake_up(). */
587 wake_up(&sync_rcu_preempt_exp_wq);
592 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
594 raw_spin_lock(&rnp->lock); /* irqs already disabled */
595 smp_mb__after_unlock_lock();
596 rnp->expmask &= ~mask;
601 * Snapshot the tasks blocking the newly started preemptible-RCU expedited
602 * grace period for the specified rcu_node structure, phase 1. If there
603 * are such tasks, set the ->expmask bits up the rcu_node tree and also
604 * set the ->expmask bits on the leaf rcu_node structures to tell phase 2
605 * that work is needed here.
607 * Caller must hold sync_rcu_preempt_exp_mutex.
610 sync_rcu_preempt_exp_init1(struct rcu_state *rsp, struct rcu_node *rnp)
614 struct rcu_node *rnp_up;
616 raw_spin_lock_irqsave(&rnp->lock, flags);
617 smp_mb__after_unlock_lock();
618 WARN_ON_ONCE(rnp->expmask);
619 WARN_ON_ONCE(rnp->exp_tasks);
620 if (!rcu_preempt_has_tasks(rnp)) {
621 /* No blocked tasks, nothing to do. */
622 raw_spin_unlock_irqrestore(&rnp->lock, flags);
625 /* Call for Phase 2 and propagate ->expmask bits up the tree. */
628 while (rnp_up->parent) {
629 mask = rnp_up->grpmask;
630 rnp_up = rnp_up->parent;
631 if (rnp_up->expmask & mask)
633 raw_spin_lock(&rnp_up->lock); /* irqs already off */
634 smp_mb__after_unlock_lock();
635 rnp_up->expmask |= mask;
636 raw_spin_unlock(&rnp_up->lock); /* irqs still off */
638 raw_spin_unlock_irqrestore(&rnp->lock, flags);
642 * Snapshot the tasks blocking the newly started preemptible-RCU expedited
643 * grace period for the specified rcu_node structure, phase 2. If the
644 * leaf rcu_node structure has its ->expmask field set, check for tasks.
645 * If there are some, clear ->expmask and set ->exp_tasks accordingly,
646 * then initiate RCU priority boosting. Otherwise, clear ->expmask and
647 * invoke rcu_report_exp_rnp() to clear out the upper-level ->expmask bits,
648 * enabling rcu_read_unlock_special() to do the bit-clearing.
650 * Caller must hold sync_rcu_preempt_exp_mutex.
653 sync_rcu_preempt_exp_init2(struct rcu_state *rsp, struct rcu_node *rnp)
657 raw_spin_lock_irqsave(&rnp->lock, flags);
658 smp_mb__after_unlock_lock();
660 /* Phase 1 didn't do anything, so Phase 2 doesn't either. */
661 raw_spin_unlock_irqrestore(&rnp->lock, flags);
665 /* Phase 1 is over. */
669 * If there are still blocked tasks, set up ->exp_tasks so that
670 * rcu_read_unlock_special() will wake us and then boost them.
672 if (rcu_preempt_has_tasks(rnp)) {
673 rnp->exp_tasks = rnp->blkd_tasks.next;
674 rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
678 /* No longer any blocked tasks, so undo bit setting. */
679 raw_spin_unlock_irqrestore(&rnp->lock, flags);
680 rcu_report_exp_rnp(rsp, rnp, false);
684 * synchronize_rcu_expedited - Brute-force RCU grace period
686 * Wait for an RCU-preempt grace period, but expedite it. The basic
687 * idea is to invoke synchronize_sched_expedited() to push all the tasks to
688 * the ->blkd_tasks lists and wait for this list to drain. This consumes
689 * significant time on all CPUs and is unfriendly to real-time workloads,
690 * so is thus not recommended for any sort of common-case code.
691 * In fact, if you are using synchronize_rcu_expedited() in a loop,
692 * please restructure your code to batch your updates, and then Use a
693 * single synchronize_rcu() instead.
695 void synchronize_rcu_expedited(void)
697 struct rcu_node *rnp;
698 struct rcu_state *rsp = &rcu_preempt_state;
702 smp_mb(); /* Caller's modifications seen first by other CPUs. */
703 snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
704 smp_mb(); /* Above access cannot bleed into critical section. */
707 * Block CPU-hotplug operations. This means that any CPU-hotplug
708 * operation that finds an rcu_node structure with tasks in the
709 * process of being boosted will know that all tasks blocking
710 * this expedited grace period will already be in the process of
711 * being boosted. This simplifies the process of moving tasks
712 * from leaf to root rcu_node structures.
714 if (!try_get_online_cpus()) {
715 /* CPU-hotplug operation in flight, fall back to normal GP. */
716 wait_rcu_gp(call_rcu);
721 * Acquire lock, falling back to synchronize_rcu() if too many
722 * lock-acquisition failures. Of course, if someone does the
723 * expedited grace period for us, just leave.
725 while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
726 if (ULONG_CMP_LT(snap,
727 ACCESS_ONCE(sync_rcu_preempt_exp_count))) {
729 goto mb_ret; /* Others did our work for us. */
731 if (trycount++ < 10) {
732 udelay(trycount * num_online_cpus());
735 wait_rcu_gp(call_rcu);
739 if (ULONG_CMP_LT(snap, ACCESS_ONCE(sync_rcu_preempt_exp_count))) {
741 goto unlock_mb_ret; /* Others did our work for us. */
744 /* force all RCU readers onto ->blkd_tasks lists. */
745 synchronize_sched_expedited();
748 * Snapshot current state of ->blkd_tasks lists into ->expmask.
749 * Phase 1 sets bits and phase 2 permits rcu_read_unlock_special()
750 * to start clearing them. Doing this in one phase leads to
751 * strange races between setting and clearing bits, so just say "no"!
753 rcu_for_each_leaf_node(rsp, rnp)
754 sync_rcu_preempt_exp_init1(rsp, rnp);
755 rcu_for_each_leaf_node(rsp, rnp)
756 sync_rcu_preempt_exp_init2(rsp, rnp);
760 /* Wait for snapshotted ->blkd_tasks lists to drain. */
761 rnp = rcu_get_root(rsp);
762 wait_event(sync_rcu_preempt_exp_wq,
763 sync_rcu_preempt_exp_done(rnp));
765 /* Clean up and exit. */
766 smp_mb(); /* ensure expedited GP seen before counter increment. */
767 ACCESS_ONCE(sync_rcu_preempt_exp_count) =
768 sync_rcu_preempt_exp_count + 1;
770 mutex_unlock(&sync_rcu_preempt_exp_mutex);
772 smp_mb(); /* ensure subsequent action seen after grace period. */
774 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
777 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
779 * Note that this primitive does not necessarily wait for an RCU grace period
780 * to complete. For example, if there are no RCU callbacks queued anywhere
781 * in the system, then rcu_barrier() is within its rights to return
782 * immediately, without waiting for anything, much less an RCU grace period.
784 void rcu_barrier(void)
786 _rcu_barrier(&rcu_preempt_state);
788 EXPORT_SYMBOL_GPL(rcu_barrier);
791 * Initialize preemptible RCU's state structures.
793 static void __init __rcu_init_preempt(void)
795 rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
799 * Check for a task exiting while in a preemptible-RCU read-side
800 * critical section, clean up if so. No need to issue warnings,
801 * as debug_check_no_locks_held() already does this if lockdep
806 struct task_struct *t = current;
808 if (likely(list_empty(¤t->rcu_node_entry)))
810 t->rcu_read_lock_nesting = 1;
812 t->rcu_read_unlock_special.b.blocked = true;
816 #else /* #ifdef CONFIG_PREEMPT_RCU */
818 static struct rcu_state *rcu_state_p = &rcu_sched_state;
821 * Tell them what RCU they are running.
823 static void __init rcu_bootup_announce(void)
825 pr_info("Hierarchical RCU implementation.\n");
826 rcu_bootup_announce_oddness();
830 * Because preemptible RCU does not exist, we never have to check for
831 * CPUs being in quiescent states.
833 static void rcu_preempt_note_context_switch(void)
838 * Because preemptible RCU does not exist, there are never any preempted
841 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
847 * Because there is no preemptible RCU, there can be no readers blocked.
849 static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
855 * Because preemptible RCU does not exist, we never have to check for
856 * tasks blocked within RCU read-side critical sections.
858 static void rcu_print_detail_task_stall(struct rcu_state *rsp)
863 * Because preemptible RCU does not exist, we never have to check for
864 * tasks blocked within RCU read-side critical sections.
866 static int rcu_print_task_stall(struct rcu_node *rnp)
872 * Because there is no preemptible RCU, there can be no readers blocked,
873 * so there is no need to check for blocked tasks. So check only for
874 * bogus qsmask values.
876 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
878 WARN_ON_ONCE(rnp->qsmask);
882 * Because preemptible RCU does not exist, it never has any callbacks
885 static void rcu_preempt_check_callbacks(void)
890 * Wait for an rcu-preempt grace period, but make it happen quickly.
891 * But because preemptible RCU does not exist, map to rcu-sched.
893 void synchronize_rcu_expedited(void)
895 synchronize_sched_expedited();
897 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
900 * Because preemptible RCU does not exist, rcu_barrier() is just
901 * another name for rcu_barrier_sched().
903 void rcu_barrier(void)
907 EXPORT_SYMBOL_GPL(rcu_barrier);
910 * Because preemptible RCU does not exist, it need not be initialized.
912 static void __init __rcu_init_preempt(void)
917 * Because preemptible RCU does not exist, tasks cannot possibly exit
918 * while in preemptible RCU read-side critical sections.
924 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
927 * If boosting, set rcuc kthreads to realtime priority.
929 static void rcu_cpu_kthread_setup(unsigned int cpu)
931 #ifdef CONFIG_RCU_BOOST
932 struct sched_param sp;
934 sp.sched_priority = kthread_prio;
935 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
936 #endif /* #ifdef CONFIG_RCU_BOOST */
939 #ifdef CONFIG_RCU_BOOST
941 #include "../locking/rtmutex_common.h"
943 #ifdef CONFIG_RCU_TRACE
945 static void rcu_initiate_boost_trace(struct rcu_node *rnp)
947 if (!rcu_preempt_has_tasks(rnp))
948 rnp->n_balk_blkd_tasks++;
949 else if (rnp->exp_tasks == NULL && rnp->gp_tasks == NULL)
950 rnp->n_balk_exp_gp_tasks++;
951 else if (rnp->gp_tasks != NULL && rnp->boost_tasks != NULL)
952 rnp->n_balk_boost_tasks++;
953 else if (rnp->gp_tasks != NULL && rnp->qsmask != 0)
954 rnp->n_balk_notblocked++;
955 else if (rnp->gp_tasks != NULL &&
956 ULONG_CMP_LT(jiffies, rnp->boost_time))
957 rnp->n_balk_notyet++;
962 #else /* #ifdef CONFIG_RCU_TRACE */
964 static void rcu_initiate_boost_trace(struct rcu_node *rnp)
968 #endif /* #else #ifdef CONFIG_RCU_TRACE */
971 * Carry out RCU priority boosting on the task indicated by ->exp_tasks
972 * or ->boost_tasks, advancing the pointer to the next task in the
975 * Note that irqs must be enabled: boosting the task can block.
976 * Returns 1 if there are more tasks needing to be boosted.
978 static int rcu_boost(struct rcu_node *rnp)
981 struct task_struct *t;
982 struct list_head *tb;
984 if (ACCESS_ONCE(rnp->exp_tasks) == NULL &&
985 ACCESS_ONCE(rnp->boost_tasks) == NULL)
986 return 0; /* Nothing left to boost. */
988 raw_spin_lock_irqsave(&rnp->lock, flags);
989 smp_mb__after_unlock_lock();
992 * Recheck under the lock: all tasks in need of boosting
993 * might exit their RCU read-side critical sections on their own.
995 if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
996 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1001 * Preferentially boost tasks blocking expedited grace periods.
1002 * This cannot starve the normal grace periods because a second
1003 * expedited grace period must boost all blocked tasks, including
1004 * those blocking the pre-existing normal grace period.
1006 if (rnp->exp_tasks != NULL) {
1007 tb = rnp->exp_tasks;
1008 rnp->n_exp_boosts++;
1010 tb = rnp->boost_tasks;
1011 rnp->n_normal_boosts++;
1013 rnp->n_tasks_boosted++;
1016 * We boost task t by manufacturing an rt_mutex that appears to
1017 * be held by task t. We leave a pointer to that rt_mutex where
1018 * task t can find it, and task t will release the mutex when it
1019 * exits its outermost RCU read-side critical section. Then
1020 * simply acquiring this artificial rt_mutex will boost task
1021 * t's priority. (Thanks to tglx for suggesting this approach!)
1023 * Note that task t must acquire rnp->lock to remove itself from
1024 * the ->blkd_tasks list, which it will do from exit() if from
1025 * nowhere else. We therefore are guaranteed that task t will
1026 * stay around at least until we drop rnp->lock. Note that
1027 * rnp->lock also resolves races between our priority boosting
1028 * and task t's exiting its outermost RCU read-side critical
1031 t = container_of(tb, struct task_struct, rcu_node_entry);
1032 rt_mutex_init_proxy_locked(&rnp->boost_mtx, t);
1033 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1034 /* Lock only for side effect: boosts task t's priority. */
1035 rt_mutex_lock(&rnp->boost_mtx);
1036 rt_mutex_unlock(&rnp->boost_mtx); /* Then keep lockdep happy. */
1038 return ACCESS_ONCE(rnp->exp_tasks) != NULL ||
1039 ACCESS_ONCE(rnp->boost_tasks) != NULL;
1043 * Priority-boosting kthread. One per leaf rcu_node and one for the
1046 static int rcu_boost_kthread(void *arg)
1048 struct rcu_node *rnp = (struct rcu_node *)arg;
1052 trace_rcu_utilization(TPS("Start boost kthread@init"));
1054 rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
1055 trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
1056 rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
1057 trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
1058 rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
1059 more2boost = rcu_boost(rnp);
1065 rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
1066 trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
1067 schedule_timeout_interruptible(2);
1068 trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
1073 trace_rcu_utilization(TPS("End boost kthread@notreached"));
1078 * Check to see if it is time to start boosting RCU readers that are
1079 * blocking the current grace period, and, if so, tell the per-rcu_node
1080 * kthread to start boosting them. If there is an expedited grace
1081 * period in progress, it is always time to boost.
1083 * The caller must hold rnp->lock, which this function releases.
1084 * The ->boost_kthread_task is immortal, so we don't need to worry
1085 * about it going away.
1087 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
1088 __releases(rnp->lock)
1090 struct task_struct *t;
1092 if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
1093 rnp->n_balk_exp_gp_tasks++;
1094 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1097 if (rnp->exp_tasks != NULL ||
1098 (rnp->gp_tasks != NULL &&
1099 rnp->boost_tasks == NULL &&
1101 ULONG_CMP_GE(jiffies, rnp->boost_time))) {
1102 if (rnp->exp_tasks == NULL)
1103 rnp->boost_tasks = rnp->gp_tasks;
1104 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1105 t = rnp->boost_kthread_task;
1107 rcu_wake_cond(t, rnp->boost_kthread_status);
1109 rcu_initiate_boost_trace(rnp);
1110 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1115 * Is the current CPU running the RCU-callbacks kthread?
1116 * Caller must have preemption disabled.
1118 static bool rcu_is_callbacks_kthread(void)
1120 return __this_cpu_read(rcu_cpu_kthread_task) == current;
1123 #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
1126 * Do priority-boost accounting for the start of a new grace period.
1128 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1130 rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
1134 * Create an RCU-boost kthread for the specified node if one does not
1135 * already exist. We only create this kthread for preemptible RCU.
1136 * Returns zero if all is well, a negated errno otherwise.
1138 static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
1139 struct rcu_node *rnp)
1141 int rnp_index = rnp - &rsp->node[0];
1142 unsigned long flags;
1143 struct sched_param sp;
1144 struct task_struct *t;
1146 if (&rcu_preempt_state != rsp)
1149 if (!rcu_scheduler_fully_active || rcu_rnp_online_cpus(rnp) == 0)
1153 if (rnp->boost_kthread_task != NULL)
1155 t = kthread_create(rcu_boost_kthread, (void *)rnp,
1156 "rcub/%d", rnp_index);
1159 raw_spin_lock_irqsave(&rnp->lock, flags);
1160 smp_mb__after_unlock_lock();
1161 rnp->boost_kthread_task = t;
1162 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1163 sp.sched_priority = kthread_prio;
1164 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1165 wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
1170 * Set the per-rcu_node kthread's affinity to cover all CPUs that are
1171 * served by the rcu_node in question. The CPU hotplug lock is still
1172 * held, so the value of rnp->qsmaskinit will be stable.
1174 * We don't include outgoingcpu in the affinity set, use -1 if there is
1175 * no outgoing CPU. If there are no CPUs left in the affinity set,
1176 * this function allows the kthread to execute on any CPU.
1178 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1180 struct task_struct *t = rnp->boost_kthread_task;
1181 unsigned long mask = rcu_rnp_online_cpus(rnp);
1187 if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
1189 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1)
1190 if ((mask & 0x1) && cpu != outgoingcpu)
1191 cpumask_set_cpu(cpu, cm);
1192 if (cpumask_weight(cm) == 0)
1194 set_cpus_allowed_ptr(t, cm);
1195 free_cpumask_var(cm);
1199 * Spawn boost kthreads -- called as soon as the scheduler is running.
1201 static void __init rcu_spawn_boost_kthreads(void)
1203 struct rcu_node *rnp;
1204 rcu_for_each_leaf_node(rcu_state_p, rnp)
1205 (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
1208 static void rcu_prepare_kthreads(int cpu)
1210 struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
1211 struct rcu_node *rnp = rdp->mynode;
1213 /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
1214 if (rcu_scheduler_fully_active)
1215 (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
1218 #else /* #ifdef CONFIG_RCU_BOOST */
1220 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
1221 __releases(rnp->lock)
1223 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1226 static bool rcu_is_callbacks_kthread(void)
1231 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1235 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1239 static void __init rcu_spawn_boost_kthreads(void)
1243 static void rcu_prepare_kthreads(int cpu)
1247 #endif /* #else #ifdef CONFIG_RCU_BOOST */
1249 #if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
1252 * Check to see if any future RCU-related work will need to be done
1253 * by the current CPU, even if none need be done immediately, returning
1254 * 1 if so. This function is part of the RCU implementation; it is -not-
1255 * an exported member of the RCU API.
1257 * Because we not have RCU_FAST_NO_HZ, just check whether this CPU needs
1258 * any flavor of RCU.
1260 #ifndef CONFIG_RCU_NOCB_CPU_ALL
1261 int rcu_needs_cpu(unsigned long *delta_jiffies)
1263 *delta_jiffies = ULONG_MAX;
1264 return rcu_cpu_has_callbacks(NULL);
1266 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
1267 #endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
1269 #if !defined(CONFIG_RCU_FAST_NO_HZ)
1271 * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
1274 static void rcu_cleanup_after_idle(void)
1279 * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
1282 static void rcu_prepare_for_idle(void)
1287 * Don't bother keeping a running count of the number of RCU callbacks
1288 * posted because CONFIG_RCU_FAST_NO_HZ=n.
1290 static void rcu_idle_count_callbacks_posted(void)
1294 #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1297 * This code is invoked when a CPU goes idle, at which point we want
1298 * to have the CPU do everything required for RCU so that it can enter
1299 * the energy-efficient dyntick-idle mode. This is handled by a
1300 * state machine implemented by rcu_prepare_for_idle() below.
1302 * The following three proprocessor symbols control this state machine:
1304 * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
1305 * to sleep in dyntick-idle mode with RCU callbacks pending. This
1306 * is sized to be roughly one RCU grace period. Those energy-efficiency
1307 * benchmarkers who might otherwise be tempted to set this to a large
1308 * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
1309 * system. And if you are -that- concerned about energy efficiency,
1310 * just power the system down and be done with it!
1311 * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
1312 * permitted to sleep in dyntick-idle mode with only lazy RCU
1313 * callbacks pending. Setting this too high can OOM your system.
1315 * The values below work well in practice. If future workloads require
1316 * adjustment, they can be converted into kernel config parameters, though
1317 * making the state machine smarter might be a better option.
1319 #define RCU_IDLE_GP_DELAY 4 /* Roughly one grace period. */
1320 #define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
1322 static int rcu_idle_gp_delay = RCU_IDLE_GP_DELAY;
1323 module_param(rcu_idle_gp_delay, int, 0644);
1324 static int rcu_idle_lazy_gp_delay = RCU_IDLE_LAZY_GP_DELAY;
1325 module_param(rcu_idle_lazy_gp_delay, int, 0644);
1327 extern int tick_nohz_active;
1330 * Try to advance callbacks for all flavors of RCU on the current CPU, but
1331 * only if it has been awhile since the last time we did so. Afterwards,
1332 * if there are any callbacks ready for immediate invocation, return true.
1334 static bool __maybe_unused rcu_try_advance_all_cbs(void)
1336 bool cbs_ready = false;
1337 struct rcu_data *rdp;
1338 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
1339 struct rcu_node *rnp;
1340 struct rcu_state *rsp;
1342 /* Exit early if we advanced recently. */
1343 if (jiffies == rdtp->last_advance_all)
1345 rdtp->last_advance_all = jiffies;
1347 for_each_rcu_flavor(rsp) {
1348 rdp = this_cpu_ptr(rsp->rda);
1352 * Don't bother checking unless a grace period has
1353 * completed since we last checked and there are
1354 * callbacks not yet ready to invoke.
1356 if ((rdp->completed != rnp->completed ||
1357 unlikely(ACCESS_ONCE(rdp->gpwrap))) &&
1358 rdp->nxttail[RCU_DONE_TAIL] != rdp->nxttail[RCU_NEXT_TAIL])
1359 note_gp_changes(rsp, rdp);
1361 if (cpu_has_callbacks_ready_to_invoke(rdp))
1367 #ifndef CONFIG_PREEMPT_RT_FULL
1370 * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
1371 * to invoke. If the CPU has callbacks, try to advance them. Tell the
1372 * caller to set the timeout based on whether or not there are non-lazy
1375 * The caller must have disabled interrupts.
1377 #ifndef CONFIG_RCU_NOCB_CPU_ALL
1378 int rcu_needs_cpu(unsigned long *dj)
1380 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
1382 /* Snapshot to detect later posting of non-lazy callback. */
1383 rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
1385 /* If no callbacks, RCU doesn't need the CPU. */
1386 if (!rcu_cpu_has_callbacks(&rdtp->all_lazy)) {
1391 /* Attempt to advance callbacks. */
1392 if (rcu_try_advance_all_cbs()) {
1393 /* Some ready to invoke, so initiate later invocation. */
1397 rdtp->last_accelerate = jiffies;
1399 /* Request timer delay depending on laziness, and round. */
1400 if (!rdtp->all_lazy) {
1401 *dj = round_up(rcu_idle_gp_delay + jiffies,
1402 rcu_idle_gp_delay) - jiffies;
1404 *dj = round_jiffies(rcu_idle_lazy_gp_delay + jiffies) - jiffies;
1408 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
1409 #endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
1411 * Prepare a CPU for idle from an RCU perspective. The first major task
1412 * is to sense whether nohz mode has been enabled or disabled via sysfs.
1413 * The second major task is to check to see if a non-lazy callback has
1414 * arrived at a CPU that previously had only lazy callbacks. The third
1415 * major task is to accelerate (that is, assign grace-period numbers to)
1416 * any recently arrived callbacks.
1418 * The caller must have disabled interrupts.
1420 static void rcu_prepare_for_idle(void)
1422 #ifndef CONFIG_RCU_NOCB_CPU_ALL
1424 struct rcu_data *rdp;
1425 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
1426 struct rcu_node *rnp;
1427 struct rcu_state *rsp;
1430 /* Handle nohz enablement switches conservatively. */
1431 tne = ACCESS_ONCE(tick_nohz_active);
1432 if (tne != rdtp->tick_nohz_enabled_snap) {
1433 if (rcu_cpu_has_callbacks(NULL))
1434 invoke_rcu_core(); /* force nohz to see update. */
1435 rdtp->tick_nohz_enabled_snap = tne;
1441 /* If this is a no-CBs CPU, no callbacks, just return. */
1442 if (rcu_is_nocb_cpu(smp_processor_id()))
1446 * If a non-lazy callback arrived at a CPU having only lazy
1447 * callbacks, invoke RCU core for the side-effect of recalculating
1448 * idle duration on re-entry to idle.
1450 if (rdtp->all_lazy &&
1451 rdtp->nonlazy_posted != rdtp->nonlazy_posted_snap) {
1452 rdtp->all_lazy = false;
1453 rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
1459 * If we have not yet accelerated this jiffy, accelerate all
1460 * callbacks on this CPU.
1462 if (rdtp->last_accelerate == jiffies)
1464 rdtp->last_accelerate = jiffies;
1465 for_each_rcu_flavor(rsp) {
1466 rdp = this_cpu_ptr(rsp->rda);
1467 if (!*rdp->nxttail[RCU_DONE_TAIL])
1470 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
1471 smp_mb__after_unlock_lock();
1472 needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
1473 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1475 rcu_gp_kthread_wake(rsp);
1477 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
1481 * Clean up for exit from idle. Attempt to advance callbacks based on
1482 * any grace periods that elapsed while the CPU was idle, and if any
1483 * callbacks are now ready to invoke, initiate invocation.
1485 static void rcu_cleanup_after_idle(void)
1487 #ifndef CONFIG_RCU_NOCB_CPU_ALL
1488 if (rcu_is_nocb_cpu(smp_processor_id()))
1490 if (rcu_try_advance_all_cbs())
1492 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
1496 * Keep a running count of the number of non-lazy callbacks posted
1497 * on this CPU. This running counter (which is never decremented) allows
1498 * rcu_prepare_for_idle() to detect when something out of the idle loop
1499 * posts a callback, even if an equal number of callbacks are invoked.
1500 * Of course, callbacks should only be posted from within a trace event
1501 * designed to be called from idle or from within RCU_NONIDLE().
1503 static void rcu_idle_count_callbacks_posted(void)
1505 __this_cpu_add(rcu_dynticks.nonlazy_posted, 1);
1509 * Data for flushing lazy RCU callbacks at OOM time.
1511 static atomic_t oom_callback_count;
1512 static DECLARE_WAIT_QUEUE_HEAD(oom_callback_wq);
1515 * RCU OOM callback -- decrement the outstanding count and deliver the
1516 * wake-up if we are the last one.
1518 static void rcu_oom_callback(struct rcu_head *rhp)
1520 if (atomic_dec_and_test(&oom_callback_count))
1521 wake_up(&oom_callback_wq);
1525 * Post an rcu_oom_notify callback on the current CPU if it has at
1526 * least one lazy callback. This will unnecessarily post callbacks
1527 * to CPUs that already have a non-lazy callback at the end of their
1528 * callback list, but this is an infrequent operation, so accept some
1529 * extra overhead to keep things simple.
1531 static void rcu_oom_notify_cpu(void *unused)
1533 struct rcu_state *rsp;
1534 struct rcu_data *rdp;
1536 for_each_rcu_flavor(rsp) {
1537 rdp = raw_cpu_ptr(rsp->rda);
1538 if (rdp->qlen_lazy != 0) {
1539 atomic_inc(&oom_callback_count);
1540 rsp->call(&rdp->oom_head, rcu_oom_callback);
1546 * If low on memory, ensure that each CPU has a non-lazy callback.
1547 * This will wake up CPUs that have only lazy callbacks, in turn
1548 * ensuring that they free up the corresponding memory in a timely manner.
1549 * Because an uncertain amount of memory will be freed in some uncertain
1550 * timeframe, we do not claim to have freed anything.
1552 static int rcu_oom_notify(struct notifier_block *self,
1553 unsigned long notused, void *nfreed)
1557 /* Wait for callbacks from earlier instance to complete. */
1558 wait_event(oom_callback_wq, atomic_read(&oom_callback_count) == 0);
1559 smp_mb(); /* Ensure callback reuse happens after callback invocation. */
1562 * Prevent premature wakeup: ensure that all increments happen
1563 * before there is a chance of the counter reaching zero.
1565 atomic_set(&oom_callback_count, 1);
1568 for_each_online_cpu(cpu) {
1569 smp_call_function_single(cpu, rcu_oom_notify_cpu, NULL, 1);
1570 cond_resched_rcu_qs();
1574 /* Unconditionally decrement: no need to wake ourselves up. */
1575 atomic_dec(&oom_callback_count);
1580 static struct notifier_block rcu_oom_nb = {
1581 .notifier_call = rcu_oom_notify
1584 static int __init rcu_register_oom_notifier(void)
1586 register_oom_notifier(&rcu_oom_nb);
1589 early_initcall(rcu_register_oom_notifier);
1591 #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1593 #ifdef CONFIG_RCU_CPU_STALL_INFO
1595 #ifdef CONFIG_RCU_FAST_NO_HZ
1597 static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
1599 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
1600 unsigned long nlpd = rdtp->nonlazy_posted - rdtp->nonlazy_posted_snap;
1602 sprintf(cp, "last_accelerate: %04lx/%04lx, nonlazy_posted: %ld, %c%c",
1603 rdtp->last_accelerate & 0xffff, jiffies & 0xffff,
1605 rdtp->all_lazy ? 'L' : '.',
1606 rdtp->tick_nohz_enabled_snap ? '.' : 'D');
1609 #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
1611 static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
1616 #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
1618 /* Initiate the stall-info list. */
1619 static void print_cpu_stall_info_begin(void)
1625 * Print out diagnostic information for the specified stalled CPU.
1627 * If the specified CPU is aware of the current RCU grace period
1628 * (flavor specified by rsp), then print the number of scheduling
1629 * clock interrupts the CPU has taken during the time that it has
1630 * been aware. Otherwise, print the number of RCU grace periods
1631 * that this CPU is ignorant of, for example, "1" if the CPU was
1632 * aware of the previous grace period.
1634 * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
1636 static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
1638 char fast_no_hz[72];
1639 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
1640 struct rcu_dynticks *rdtp = rdp->dynticks;
1642 unsigned long ticks_value;
1644 if (rsp->gpnum == rdp->gpnum) {
1645 ticks_title = "ticks this GP";
1646 ticks_value = rdp->ticks_this_gp;
1648 ticks_title = "GPs behind";
1649 ticks_value = rsp->gpnum - rdp->gpnum;
1651 print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
1652 pr_err("\t%d: (%lu %s) idle=%03x/%llx/%d softirq=%u/%u fqs=%ld %s\n",
1653 cpu, ticks_value, ticks_title,
1654 atomic_read(&rdtp->dynticks) & 0xfff,
1655 rdtp->dynticks_nesting, rdtp->dynticks_nmi_nesting,
1656 rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
1657 ACCESS_ONCE(rsp->n_force_qs) - rsp->n_force_qs_gpstart,
1661 /* Terminate the stall-info list. */
1662 static void print_cpu_stall_info_end(void)
1667 /* Zero ->ticks_this_gp for all flavors of RCU. */
1668 static void zero_cpu_stall_ticks(struct rcu_data *rdp)
1670 rdp->ticks_this_gp = 0;
1671 rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
1674 /* Increment ->ticks_this_gp for all flavors of RCU. */
1675 static void increment_cpu_stall_ticks(void)
1677 struct rcu_state *rsp;
1679 for_each_rcu_flavor(rsp)
1680 raw_cpu_inc(rsp->rda->ticks_this_gp);
1683 #else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
1685 static void print_cpu_stall_info_begin(void)
1690 static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
1692 pr_cont(" %d", cpu);
1695 static void print_cpu_stall_info_end(void)
1700 static void zero_cpu_stall_ticks(struct rcu_data *rdp)
1704 static void increment_cpu_stall_ticks(void)
1708 #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
1710 #ifdef CONFIG_RCU_NOCB_CPU
1713 * Offload callback processing from the boot-time-specified set of CPUs
1714 * specified by rcu_nocb_mask. For each CPU in the set, there is a
1715 * kthread created that pulls the callbacks from the corresponding CPU,
1716 * waits for a grace period to elapse, and invokes the callbacks.
1717 * The no-CBs CPUs do a wake_up() on their kthread when they insert
1718 * a callback into any empty list, unless the rcu_nocb_poll boot parameter
1719 * has been specified, in which case each kthread actively polls its
1720 * CPU. (Which isn't so great for energy efficiency, but which does
1721 * reduce RCU's overhead on that CPU.)
1723 * This is intended to be used in conjunction with Frederic Weisbecker's
1724 * adaptive-idle work, which would seriously reduce OS jitter on CPUs
1725 * running CPU-bound user-mode computations.
1727 * Offloading of callback processing could also in theory be used as
1728 * an energy-efficiency measure because CPUs with no RCU callbacks
1729 * queued are more aggressive about entering dyntick-idle mode.
1733 /* Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters. */
1734 static int __init rcu_nocb_setup(char *str)
1736 alloc_bootmem_cpumask_var(&rcu_nocb_mask);
1737 have_rcu_nocb_mask = true;
1738 cpulist_parse(str, rcu_nocb_mask);
1741 __setup("rcu_nocbs=", rcu_nocb_setup);
1743 static int __init parse_rcu_nocb_poll(char *arg)
1748 early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
1751 * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
1754 static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
1756 swait_wake_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
1760 * Set the root rcu_node structure's ->need_future_gp field
1761 * based on the sum of those of all rcu_node structures. This does
1762 * double-count the root rcu_node structure's requests, but this
1763 * is necessary to handle the possibility of a rcu_nocb_kthread()
1764 * having awakened during the time that the rcu_node structures
1765 * were being updated for the end of the previous grace period.
1767 static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
1769 rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
1772 static void rcu_init_one_nocb(struct rcu_node *rnp)
1774 init_swait_head(&rnp->nocb_gp_wq[0]);
1775 init_swait_head(&rnp->nocb_gp_wq[1]);
1778 #ifndef CONFIG_RCU_NOCB_CPU_ALL
1779 /* Is the specified CPU a no-CBs CPU? */
1780 bool rcu_is_nocb_cpu(int cpu)
1782 if (have_rcu_nocb_mask)
1783 return cpumask_test_cpu(cpu, rcu_nocb_mask);
1786 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
1789 * Kick the leader kthread for this NOCB group.
1791 static void wake_nocb_leader(struct rcu_data *rdp, bool force)
1793 struct rcu_data *rdp_leader = rdp->nocb_leader;
1795 if (!ACCESS_ONCE(rdp_leader->nocb_kthread))
1797 if (ACCESS_ONCE(rdp_leader->nocb_leader_sleep) || force) {
1798 /* Prior smp_mb__after_atomic() orders against prior enqueue. */
1799 ACCESS_ONCE(rdp_leader->nocb_leader_sleep) = false;
1800 swait_wake(&rdp_leader->nocb_wq);
1805 * Does the specified CPU need an RCU callback for the specified flavor
1808 static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
1810 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
1812 #ifdef CONFIG_PROVE_RCU
1813 struct rcu_head *rhp;
1814 #endif /* #ifdef CONFIG_PROVE_RCU */
1817 * Check count of all no-CBs callbacks awaiting invocation.
1818 * There needs to be a barrier before this function is called,
1819 * but associated with a prior determination that no more
1820 * callbacks would be posted. In the worst case, the first
1821 * barrier in _rcu_barrier() suffices (but the caller cannot
1822 * necessarily rely on this, not a substitute for the caller
1823 * getting the concurrency design right!). There must also be
1824 * a barrier between the following load an posting of a callback
1825 * (if a callback is in fact needed). This is associated with an
1826 * atomic_inc() in the caller.
1828 ret = atomic_long_read(&rdp->nocb_q_count);
1830 #ifdef CONFIG_PROVE_RCU
1831 rhp = ACCESS_ONCE(rdp->nocb_head);
1833 rhp = ACCESS_ONCE(rdp->nocb_gp_head);
1835 rhp = ACCESS_ONCE(rdp->nocb_follower_head);
1837 /* Having no rcuo kthread but CBs after scheduler starts is bad! */
1838 if (!ACCESS_ONCE(rdp->nocb_kthread) && rhp &&
1839 rcu_scheduler_fully_active) {
1840 /* RCU callback enqueued before CPU first came online??? */
1841 pr_err("RCU: Never-onlined no-CBs CPU %d has CB %p\n",
1845 #endif /* #ifdef CONFIG_PROVE_RCU */
1851 * Enqueue the specified string of rcu_head structures onto the specified
1852 * CPU's no-CBs lists. The CPU is specified by rdp, the head of the
1853 * string by rhp, and the tail of the string by rhtp. The non-lazy/lazy
1854 * counts are supplied by rhcount and rhcount_lazy.
1856 * If warranted, also wake up the kthread servicing this CPUs queues.
1858 static void __call_rcu_nocb_enqueue(struct rcu_data *rdp,
1859 struct rcu_head *rhp,
1860 struct rcu_head **rhtp,
1861 int rhcount, int rhcount_lazy,
1862 unsigned long flags)
1865 struct rcu_head **old_rhpp;
1866 struct task_struct *t;
1868 /* Enqueue the callback on the nocb list and update counts. */
1869 atomic_long_add(rhcount, &rdp->nocb_q_count);
1870 /* rcu_barrier() relies on ->nocb_q_count add before xchg. */
1871 old_rhpp = xchg(&rdp->nocb_tail, rhtp);
1872 ACCESS_ONCE(*old_rhpp) = rhp;
1873 atomic_long_add(rhcount_lazy, &rdp->nocb_q_count_lazy);
1874 smp_mb__after_atomic(); /* Store *old_rhpp before _wake test. */
1876 /* If we are not being polled and there is a kthread, awaken it ... */
1877 t = ACCESS_ONCE(rdp->nocb_kthread);
1878 if (rcu_nocb_poll || !t) {
1879 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
1880 TPS("WakeNotPoll"));
1883 len = atomic_long_read(&rdp->nocb_q_count);
1884 if (old_rhpp == &rdp->nocb_head) {
1885 if (!irqs_disabled_flags(flags)) {
1886 /* ... if queue was empty ... */
1887 wake_nocb_leader(rdp, false);
1888 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
1891 rdp->nocb_defer_wakeup = RCU_NOGP_WAKE;
1892 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
1893 TPS("WakeEmptyIsDeferred"));
1895 rdp->qlen_last_fqs_check = 0;
1896 } else if (len > rdp->qlen_last_fqs_check + qhimark) {
1897 /* ... or if many callbacks queued. */
1898 if (!irqs_disabled_flags(flags)) {
1899 wake_nocb_leader(rdp, true);
1900 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
1903 rdp->nocb_defer_wakeup = RCU_NOGP_WAKE_FORCE;
1904 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
1905 TPS("WakeOvfIsDeferred"));
1907 rdp->qlen_last_fqs_check = LONG_MAX / 2;
1909 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WakeNot"));
1915 * This is a helper for __call_rcu(), which invokes this when the normal
1916 * callback queue is inoperable. If this is not a no-CBs CPU, this
1917 * function returns failure back to __call_rcu(), which can complain
1920 * Otherwise, this function queues the callback where the corresponding
1921 * "rcuo" kthread can find it.
1923 static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
1924 bool lazy, unsigned long flags)
1927 if (!rcu_is_nocb_cpu(rdp->cpu))
1929 __call_rcu_nocb_enqueue(rdp, rhp, &rhp->next, 1, lazy, flags);
1930 if (__is_kfree_rcu_offset((unsigned long)rhp->func))
1931 trace_rcu_kfree_callback(rdp->rsp->name, rhp,
1932 (unsigned long)rhp->func,
1933 -atomic_long_read(&rdp->nocb_q_count_lazy),
1934 -atomic_long_read(&rdp->nocb_q_count));
1936 trace_rcu_callback(rdp->rsp->name, rhp,
1937 -atomic_long_read(&rdp->nocb_q_count_lazy),
1938 -atomic_long_read(&rdp->nocb_q_count));
1941 * If called from an extended quiescent state with interrupts
1942 * disabled, invoke the RCU core in order to allow the idle-entry
1943 * deferred-wakeup check to function.
1945 if (irqs_disabled_flags(flags) &&
1946 !rcu_is_watching() &&
1947 cpu_online(smp_processor_id()))
1954 * Adopt orphaned callbacks on a no-CBs CPU, or return 0 if this is
1957 static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_state *rsp,
1958 struct rcu_data *rdp,
1959 unsigned long flags)
1961 long ql = rsp->qlen;
1962 long qll = rsp->qlen_lazy;
1964 /* If this is not a no-CBs CPU, tell the caller to do it the old way. */
1965 if (!rcu_is_nocb_cpu(smp_processor_id()))
1970 /* First, enqueue the donelist, if any. This preserves CB ordering. */
1971 if (rsp->orphan_donelist != NULL) {
1972 __call_rcu_nocb_enqueue(rdp, rsp->orphan_donelist,
1973 rsp->orphan_donetail, ql, qll, flags);
1975 rsp->orphan_donelist = NULL;
1976 rsp->orphan_donetail = &rsp->orphan_donelist;
1978 if (rsp->orphan_nxtlist != NULL) {
1979 __call_rcu_nocb_enqueue(rdp, rsp->orphan_nxtlist,
1980 rsp->orphan_nxttail, ql, qll, flags);
1982 rsp->orphan_nxtlist = NULL;
1983 rsp->orphan_nxttail = &rsp->orphan_nxtlist;
1989 * If necessary, kick off a new grace period, and either way wait
1990 * for a subsequent grace period to complete.
1992 static void rcu_nocb_wait_gp(struct rcu_data *rdp)
1996 unsigned long flags;
1998 struct rcu_node *rnp = rdp->mynode;
2000 raw_spin_lock_irqsave(&rnp->lock, flags);
2001 smp_mb__after_unlock_lock();
2002 needwake = rcu_start_future_gp(rnp, rdp, &c);
2003 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2005 rcu_gp_kthread_wake(rdp->rsp);
2008 * Wait for the grace period. Do so interruptibly to avoid messing
2009 * up the load average.
2011 trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
2013 swait_event_interruptible(
2014 rnp->nocb_gp_wq[c & 0x1],
2015 (d = ULONG_CMP_GE(ACCESS_ONCE(rnp->completed), c)));
2018 WARN_ON(signal_pending(current));
2019 trace_rcu_future_gp(rnp, rdp, c, TPS("ResumeWait"));
2021 trace_rcu_future_gp(rnp, rdp, c, TPS("EndWait"));
2022 smp_mb(); /* Ensure that CB invocation happens after GP end. */
2026 * Leaders come here to wait for additional callbacks to show up.
2027 * This function does not return until callbacks appear.
2029 static void nocb_leader_wait(struct rcu_data *my_rdp)
2031 bool firsttime = true;
2033 struct rcu_data *rdp;
2034 struct rcu_head **tail;
2038 /* Wait for callbacks to appear. */
2039 if (!rcu_nocb_poll) {
2040 trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
2041 swait_event_interruptible(my_rdp->nocb_wq,
2042 !ACCESS_ONCE(my_rdp->nocb_leader_sleep));
2043 /* Memory barrier handled by smp_mb() calls below and repoll. */
2044 } else if (firsttime) {
2045 firsttime = false; /* Don't drown trace log with "Poll"! */
2046 trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Poll");
2050 * Each pass through the following loop checks a follower for CBs.
2051 * We are our own first follower. Any CBs found are moved to
2052 * nocb_gp_head, where they await a grace period.
2055 for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
2056 rdp->nocb_gp_head = ACCESS_ONCE(rdp->nocb_head);
2057 if (!rdp->nocb_gp_head)
2058 continue; /* No CBs here, try next follower. */
2060 /* Move callbacks to wait-for-GP list, which is empty. */
2061 ACCESS_ONCE(rdp->nocb_head) = NULL;
2062 rdp->nocb_gp_tail = xchg(&rdp->nocb_tail, &rdp->nocb_head);
2067 * If there were no callbacks, sleep a bit, rescan after a
2068 * memory barrier, and go retry.
2070 if (unlikely(!gotcbs)) {
2072 trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu,
2074 WARN_ON(signal_pending(current));
2075 schedule_timeout_interruptible(1);
2077 /* Rescan in case we were a victim of memory ordering. */
2078 my_rdp->nocb_leader_sleep = true;
2079 smp_mb(); /* Ensure _sleep true before scan. */
2080 for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower)
2081 if (ACCESS_ONCE(rdp->nocb_head)) {
2082 /* Found CB, so short-circuit next wait. */
2083 my_rdp->nocb_leader_sleep = false;
2089 /* Wait for one grace period. */
2090 rcu_nocb_wait_gp(my_rdp);
2093 * We left ->nocb_leader_sleep unset to reduce cache thrashing.
2094 * We set it now, but recheck for new callbacks while
2095 * traversing our follower list.
2097 my_rdp->nocb_leader_sleep = true;
2098 smp_mb(); /* Ensure _sleep true before scan of ->nocb_head. */
2100 /* Each pass through the following loop wakes a follower, if needed. */
2101 for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
2102 if (ACCESS_ONCE(rdp->nocb_head))
2103 my_rdp->nocb_leader_sleep = false;/* No need to sleep.*/
2104 if (!rdp->nocb_gp_head)
2105 continue; /* No CBs, so no need to wake follower. */
2107 /* Append callbacks to follower's "done" list. */
2108 tail = xchg(&rdp->nocb_follower_tail, rdp->nocb_gp_tail);
2109 *tail = rdp->nocb_gp_head;
2110 smp_mb__after_atomic(); /* Store *tail before wakeup. */
2111 if (rdp != my_rdp && tail == &rdp->nocb_follower_head) {
2113 * List was empty, wake up the follower.
2114 * Memory barriers supplied by atomic_long_add().
2116 swait_wake(&rdp->nocb_wq);
2120 /* If we (the leader) don't have CBs, go wait some more. */
2121 if (!my_rdp->nocb_follower_head)
2126 * Followers come here to wait for additional callbacks to show up.
2127 * This function does not return until callbacks appear.
2129 static void nocb_follower_wait(struct rcu_data *rdp)
2131 bool firsttime = true;
2134 if (!rcu_nocb_poll) {
2135 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
2137 swait_event_interruptible(rdp->nocb_wq,
2138 ACCESS_ONCE(rdp->nocb_follower_head));
2139 } else if (firsttime) {
2140 /* Don't drown trace log with "Poll"! */
2142 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, "Poll");
2144 if (smp_load_acquire(&rdp->nocb_follower_head)) {
2145 /* ^^^ Ensure CB invocation follows _head test. */
2149 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
2151 WARN_ON(signal_pending(current));
2152 schedule_timeout_interruptible(1);
2157 * Per-rcu_data kthread, but only for no-CBs CPUs. Each kthread invokes
2158 * callbacks queued by the corresponding no-CBs CPU, however, there is
2159 * an optional leader-follower relationship so that the grace-period
2160 * kthreads don't have to do quite so many wakeups.
2162 static int rcu_nocb_kthread(void *arg)
2165 struct rcu_head *list;
2166 struct rcu_head *next;
2167 struct rcu_head **tail;
2168 struct rcu_data *rdp = arg;
2170 /* Each pass through this loop invokes one batch of callbacks */
2172 /* Wait for callbacks. */
2173 if (rdp->nocb_leader == rdp)
2174 nocb_leader_wait(rdp);
2176 nocb_follower_wait(rdp);
2178 /* Pull the ready-to-invoke callbacks onto local list. */
2179 list = ACCESS_ONCE(rdp->nocb_follower_head);
2181 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, "WokeNonEmpty");
2182 ACCESS_ONCE(rdp->nocb_follower_head) = NULL;
2183 tail = xchg(&rdp->nocb_follower_tail, &rdp->nocb_follower_head);
2185 /* Each pass through the following loop invokes a callback. */
2186 trace_rcu_batch_start(rdp->rsp->name,
2187 atomic_long_read(&rdp->nocb_q_count_lazy),
2188 atomic_long_read(&rdp->nocb_q_count), -1);
2192 /* Wait for enqueuing to complete, if needed. */
2193 while (next == NULL && &list->next != tail) {
2194 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
2196 schedule_timeout_interruptible(1);
2197 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
2201 debug_rcu_head_unqueue(list);
2203 if (__rcu_reclaim(rdp->rsp->name, list))
2209 trace_rcu_batch_end(rdp->rsp->name, c, !!list, 0, 0, 1);
2210 smp_mb__before_atomic(); /* _add after CB invocation. */
2211 atomic_long_add(-c, &rdp->nocb_q_count);
2212 atomic_long_add(-cl, &rdp->nocb_q_count_lazy);
2213 rdp->n_nocbs_invoked += c;
2218 /* Is a deferred wakeup of rcu_nocb_kthread() required? */
2219 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
2221 return ACCESS_ONCE(rdp->nocb_defer_wakeup);
2224 /* Do a deferred wakeup of rcu_nocb_kthread(). */
2225 static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
2229 if (!rcu_nocb_need_deferred_wakeup(rdp))
2231 ndw = ACCESS_ONCE(rdp->nocb_defer_wakeup);
2232 ACCESS_ONCE(rdp->nocb_defer_wakeup) = RCU_NOGP_WAKE_NOT;
2233 wake_nocb_leader(rdp, ndw == RCU_NOGP_WAKE_FORCE);
2234 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("DeferredWake"));
2237 void __init rcu_init_nohz(void)
2240 bool need_rcu_nocb_mask = true;
2241 struct rcu_state *rsp;
2243 #ifdef CONFIG_RCU_NOCB_CPU_NONE
2244 need_rcu_nocb_mask = false;
2245 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_NONE */
2247 #if defined(CONFIG_NO_HZ_FULL)
2248 if (tick_nohz_full_running && cpumask_weight(tick_nohz_full_mask))
2249 need_rcu_nocb_mask = true;
2250 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
2252 if (!have_rcu_nocb_mask && need_rcu_nocb_mask) {
2253 if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
2254 pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
2257 have_rcu_nocb_mask = true;
2259 if (!have_rcu_nocb_mask)
2262 #ifdef CONFIG_RCU_NOCB_CPU_ZERO
2263 pr_info("\tOffload RCU callbacks from CPU 0\n");
2264 cpumask_set_cpu(0, rcu_nocb_mask);
2265 #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ZERO */
2266 #ifdef CONFIG_RCU_NOCB_CPU_ALL
2267 pr_info("\tOffload RCU callbacks from all CPUs\n");
2268 cpumask_copy(rcu_nocb_mask, cpu_possible_mask);
2269 #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ALL */
2270 #if defined(CONFIG_NO_HZ_FULL)
2271 if (tick_nohz_full_running)
2272 cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
2273 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
2275 if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
2276 pr_info("\tNote: kernel parameter 'rcu_nocbs=' contains nonexistent CPUs.\n");
2277 cpumask_and(rcu_nocb_mask, cpu_possible_mask,
2280 pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
2281 cpumask_pr_args(rcu_nocb_mask));
2283 pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
2285 for_each_rcu_flavor(rsp) {
2286 for_each_cpu(cpu, rcu_nocb_mask)
2287 init_nocb_callback_list(per_cpu_ptr(rsp->rda, cpu));
2288 rcu_organize_nocb_kthreads(rsp);
2292 /* Initialize per-rcu_data variables for no-CBs CPUs. */
2293 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
2295 rdp->nocb_tail = &rdp->nocb_head;
2296 init_swait_head(&rdp->nocb_wq);
2297 rdp->nocb_follower_tail = &rdp->nocb_follower_head;
2301 * If the specified CPU is a no-CBs CPU that does not already have its
2302 * rcuo kthread for the specified RCU flavor, spawn it. If the CPUs are
2303 * brought online out of order, this can require re-organizing the
2304 * leader-follower relationships.
2306 static void rcu_spawn_one_nocb_kthread(struct rcu_state *rsp, int cpu)
2308 struct rcu_data *rdp;
2309 struct rcu_data *rdp_last;
2310 struct rcu_data *rdp_old_leader;
2311 struct rcu_data *rdp_spawn = per_cpu_ptr(rsp->rda, cpu);
2312 struct task_struct *t;
2315 * If this isn't a no-CBs CPU or if it already has an rcuo kthread,
2316 * then nothing to do.
2318 if (!rcu_is_nocb_cpu(cpu) || rdp_spawn->nocb_kthread)
2321 /* If we didn't spawn the leader first, reorganize! */
2322 rdp_old_leader = rdp_spawn->nocb_leader;
2323 if (rdp_old_leader != rdp_spawn && !rdp_old_leader->nocb_kthread) {
2325 rdp = rdp_old_leader;
2327 rdp->nocb_leader = rdp_spawn;
2328 if (rdp_last && rdp != rdp_spawn)
2329 rdp_last->nocb_next_follower = rdp;
2330 if (rdp == rdp_spawn) {
2331 rdp = rdp->nocb_next_follower;
2334 rdp = rdp->nocb_next_follower;
2335 rdp_last->nocb_next_follower = NULL;
2338 rdp_spawn->nocb_next_follower = rdp_old_leader;
2341 /* Spawn the kthread for this CPU and RCU flavor. */
2342 t = kthread_run(rcu_nocb_kthread, rdp_spawn,
2343 "rcuo%c/%d", rsp->abbr, cpu);
2345 ACCESS_ONCE(rdp_spawn->nocb_kthread) = t;
2349 * If the specified CPU is a no-CBs CPU that does not already have its
2350 * rcuo kthreads, spawn them.
2352 static void rcu_spawn_all_nocb_kthreads(int cpu)
2354 struct rcu_state *rsp;
2356 if (rcu_scheduler_fully_active)
2357 for_each_rcu_flavor(rsp)
2358 rcu_spawn_one_nocb_kthread(rsp, cpu);
2362 * Once the scheduler is running, spawn rcuo kthreads for all online
2363 * no-CBs CPUs. This assumes that the early_initcall()s happen before
2364 * non-boot CPUs come online -- if this changes, we will need to add
2365 * some mutual exclusion.
2367 static void __init rcu_spawn_nocb_kthreads(void)
2371 for_each_online_cpu(cpu)
2372 rcu_spawn_all_nocb_kthreads(cpu);
2375 /* How many follower CPU IDs per leader? Default of -1 for sqrt(nr_cpu_ids). */
2376 static int rcu_nocb_leader_stride = -1;
2377 module_param(rcu_nocb_leader_stride, int, 0444);
2380 * Initialize leader-follower relationships for all no-CBs CPU.
2382 static void __init rcu_organize_nocb_kthreads(struct rcu_state *rsp)
2385 int ls = rcu_nocb_leader_stride;
2386 int nl = 0; /* Next leader. */
2387 struct rcu_data *rdp;
2388 struct rcu_data *rdp_leader = NULL; /* Suppress misguided gcc warn. */
2389 struct rcu_data *rdp_prev = NULL;
2391 if (!have_rcu_nocb_mask)
2394 ls = int_sqrt(nr_cpu_ids);
2395 rcu_nocb_leader_stride = ls;
2399 * Each pass through this loop sets up one rcu_data structure and
2400 * spawns one rcu_nocb_kthread().
2402 for_each_cpu(cpu, rcu_nocb_mask) {
2403 rdp = per_cpu_ptr(rsp->rda, cpu);
2404 if (rdp->cpu >= nl) {
2405 /* New leader, set up for followers & next leader. */
2406 nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
2407 rdp->nocb_leader = rdp;
2410 /* Another follower, link to previous leader. */
2411 rdp->nocb_leader = rdp_leader;
2412 rdp_prev->nocb_next_follower = rdp;
2418 /* Prevent __call_rcu() from enqueuing callbacks on no-CBs CPUs */
2419 static bool init_nocb_callback_list(struct rcu_data *rdp)
2421 if (!rcu_is_nocb_cpu(rdp->cpu))
2424 /* If there are early-boot callbacks, move them to nocb lists. */
2426 rdp->nocb_head = rdp->nxtlist;
2427 rdp->nocb_tail = rdp->nxttail[RCU_NEXT_TAIL];
2428 atomic_long_set(&rdp->nocb_q_count, rdp->qlen);
2429 atomic_long_set(&rdp->nocb_q_count_lazy, rdp->qlen_lazy);
2430 rdp->nxtlist = NULL;
2434 rdp->nxttail[RCU_NEXT_TAIL] = NULL;
2438 #else /* #ifdef CONFIG_RCU_NOCB_CPU */
2440 static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
2442 WARN_ON_ONCE(1); /* Should be dead code. */
2446 static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
2450 static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
2454 static void rcu_init_one_nocb(struct rcu_node *rnp)
2458 static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
2459 bool lazy, unsigned long flags)
2464 static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_state *rsp,
2465 struct rcu_data *rdp,
2466 unsigned long flags)
2471 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
2475 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
2480 static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
2484 static void rcu_spawn_all_nocb_kthreads(int cpu)
2488 static void __init rcu_spawn_nocb_kthreads(void)
2492 static bool init_nocb_callback_list(struct rcu_data *rdp)
2497 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
2500 * An adaptive-ticks CPU can potentially execute in kernel mode for an
2501 * arbitrarily long period of time with the scheduling-clock tick turned
2502 * off. RCU will be paying attention to this CPU because it is in the
2503 * kernel, but the CPU cannot be guaranteed to be executing the RCU state
2504 * machine because the scheduling-clock tick has been disabled. Therefore,
2505 * if an adaptive-ticks CPU is failing to respond to the current grace
2506 * period and has not be idle from an RCU perspective, kick it.
2508 static void __maybe_unused rcu_kick_nohz_cpu(int cpu)
2510 #ifdef CONFIG_NO_HZ_FULL
2511 if (tick_nohz_full_cpu(cpu))
2512 smp_send_reschedule(cpu);
2513 #endif /* #ifdef CONFIG_NO_HZ_FULL */
2517 #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
2519 static int full_sysidle_state; /* Current system-idle state. */
2520 #define RCU_SYSIDLE_NOT 0 /* Some CPU is not idle. */
2521 #define RCU_SYSIDLE_SHORT 1 /* All CPUs idle for brief period. */
2522 #define RCU_SYSIDLE_LONG 2 /* All CPUs idle for long enough. */
2523 #define RCU_SYSIDLE_FULL 3 /* All CPUs idle, ready for sysidle. */
2524 #define RCU_SYSIDLE_FULL_NOTED 4 /* Actually entered sysidle state. */
2527 * Invoked to note exit from irq or task transition to idle. Note that
2528 * usermode execution does -not- count as idle here! After all, we want
2529 * to detect full-system idle states, not RCU quiescent states and grace
2530 * periods. The caller must have disabled interrupts.
2532 static void rcu_sysidle_enter(int irq)
2535 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
2537 /* If there are no nohz_full= CPUs, no need to track this. */
2538 if (!tick_nohz_full_enabled())
2541 /* Adjust nesting, check for fully idle. */
2543 rdtp->dynticks_idle_nesting--;
2544 WARN_ON_ONCE(rdtp->dynticks_idle_nesting < 0);
2545 if (rdtp->dynticks_idle_nesting != 0)
2546 return; /* Still not fully idle. */
2548 if ((rdtp->dynticks_idle_nesting & DYNTICK_TASK_NEST_MASK) ==
2549 DYNTICK_TASK_NEST_VALUE) {
2550 rdtp->dynticks_idle_nesting = 0;
2552 rdtp->dynticks_idle_nesting -= DYNTICK_TASK_NEST_VALUE;
2553 WARN_ON_ONCE(rdtp->dynticks_idle_nesting < 0);
2554 return; /* Still not fully idle. */
2558 /* Record start of fully idle period. */
2560 ACCESS_ONCE(rdtp->dynticks_idle_jiffies) = j;
2561 smp_mb__before_atomic();
2562 atomic_inc(&rdtp->dynticks_idle);
2563 smp_mb__after_atomic();
2564 WARN_ON_ONCE(atomic_read(&rdtp->dynticks_idle) & 0x1);
2568 * Unconditionally force exit from full system-idle state. This is
2569 * invoked when a normal CPU exits idle, but must be called separately
2570 * for the timekeeping CPU (tick_do_timer_cpu). The reason for this
2571 * is that the timekeeping CPU is permitted to take scheduling-clock
2572 * interrupts while the system is in system-idle state, and of course
2573 * rcu_sysidle_exit() has no way of distinguishing a scheduling-clock
2574 * interrupt from any other type of interrupt.
2576 void rcu_sysidle_force_exit(void)
2578 int oldstate = ACCESS_ONCE(full_sysidle_state);
2582 * Each pass through the following loop attempts to exit full
2583 * system-idle state. If contention proves to be a problem,
2584 * a trylock-based contention tree could be used here.
2586 while (oldstate > RCU_SYSIDLE_SHORT) {
2587 newoldstate = cmpxchg(&full_sysidle_state,
2588 oldstate, RCU_SYSIDLE_NOT);
2589 if (oldstate == newoldstate &&
2590 oldstate == RCU_SYSIDLE_FULL_NOTED) {
2591 rcu_kick_nohz_cpu(tick_do_timer_cpu);
2592 return; /* We cleared it, done! */
2594 oldstate = newoldstate;
2596 smp_mb(); /* Order initial oldstate fetch vs. later non-idle work. */
2600 * Invoked to note entry to irq or task transition from idle. Note that
2601 * usermode execution does -not- count as idle here! The caller must
2602 * have disabled interrupts.
2604 static void rcu_sysidle_exit(int irq)
2606 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
2608 /* If there are no nohz_full= CPUs, no need to track this. */
2609 if (!tick_nohz_full_enabled())
2612 /* Adjust nesting, check for already non-idle. */
2614 rdtp->dynticks_idle_nesting++;
2615 WARN_ON_ONCE(rdtp->dynticks_idle_nesting <= 0);
2616 if (rdtp->dynticks_idle_nesting != 1)
2617 return; /* Already non-idle. */
2620 * Allow for irq misnesting. Yes, it really is possible
2621 * to enter an irq handler then never leave it, and maybe
2622 * also vice versa. Handle both possibilities.
2624 if (rdtp->dynticks_idle_nesting & DYNTICK_TASK_NEST_MASK) {
2625 rdtp->dynticks_idle_nesting += DYNTICK_TASK_NEST_VALUE;
2626 WARN_ON_ONCE(rdtp->dynticks_idle_nesting <= 0);
2627 return; /* Already non-idle. */
2629 rdtp->dynticks_idle_nesting = DYNTICK_TASK_EXIT_IDLE;
2633 /* Record end of idle period. */
2634 smp_mb__before_atomic();
2635 atomic_inc(&rdtp->dynticks_idle);
2636 smp_mb__after_atomic();
2637 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks_idle) & 0x1));
2640 * If we are the timekeeping CPU, we are permitted to be non-idle
2641 * during a system-idle state. This must be the case, because
2642 * the timekeeping CPU has to take scheduling-clock interrupts
2643 * during the time that the system is transitioning to full
2644 * system-idle state. This means that the timekeeping CPU must
2645 * invoke rcu_sysidle_force_exit() directly if it does anything
2646 * more than take a scheduling-clock interrupt.
2648 if (smp_processor_id() == tick_do_timer_cpu)
2651 /* Update system-idle state: We are clearly no longer fully idle! */
2652 rcu_sysidle_force_exit();
2656 * Check to see if the current CPU is idle. Note that usermode execution
2657 * does not count as idle. The caller must have disabled interrupts,
2658 * and must be running on tick_do_timer_cpu.
2660 static void rcu_sysidle_check_cpu(struct rcu_data *rdp, bool *isidle,
2661 unsigned long *maxj)
2665 struct rcu_dynticks *rdtp = rdp->dynticks;
2667 /* If there are no nohz_full= CPUs, don't check system-wide idleness. */
2668 if (!tick_nohz_full_enabled())
2672 * If some other CPU has already reported non-idle, if this is
2673 * not the flavor of RCU that tracks sysidle state, or if this
2674 * is an offline or the timekeeping CPU, nothing to do.
2676 if (!*isidle || rdp->rsp != rcu_state_p ||
2677 cpu_is_offline(rdp->cpu) || rdp->cpu == tick_do_timer_cpu)
2679 /* Verify affinity of current kthread. */
2680 WARN_ON_ONCE(smp_processor_id() != tick_do_timer_cpu);
2682 /* Pick up current idle and NMI-nesting counter and check. */
2683 cur = atomic_read(&rdtp->dynticks_idle);
2685 *isidle = false; /* We are not idle! */
2688 smp_mb(); /* Read counters before timestamps. */
2690 /* Pick up timestamps. */
2691 j = ACCESS_ONCE(rdtp->dynticks_idle_jiffies);
2692 /* If this CPU entered idle more recently, update maxj timestamp. */
2693 if (ULONG_CMP_LT(*maxj, j))
2698 * Is this the flavor of RCU that is handling full-system idle?
2700 static bool is_sysidle_rcu_state(struct rcu_state *rsp)
2702 return rsp == rcu_state_p;
2706 * Return a delay in jiffies based on the number of CPUs, rcu_node
2707 * leaf fanout, and jiffies tick rate. The idea is to allow larger
2708 * systems more time to transition to full-idle state in order to
2709 * avoid the cache thrashing that otherwise occur on the state variable.
2710 * Really small systems (less than a couple of tens of CPUs) should
2711 * instead use a single global atomically incremented counter, and later
2712 * versions of this will automatically reconfigure themselves accordingly.
2714 static unsigned long rcu_sysidle_delay(void)
2716 if (nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL)
2718 return DIV_ROUND_UP(nr_cpu_ids * HZ, rcu_fanout_leaf * 1000);
2722 * Advance the full-system-idle state. This is invoked when all of
2723 * the non-timekeeping CPUs are idle.
2725 static void rcu_sysidle(unsigned long j)
2727 /* Check the current state. */
2728 switch (ACCESS_ONCE(full_sysidle_state)) {
2729 case RCU_SYSIDLE_NOT:
2731 /* First time all are idle, so note a short idle period. */
2732 ACCESS_ONCE(full_sysidle_state) = RCU_SYSIDLE_SHORT;
2735 case RCU_SYSIDLE_SHORT:
2738 * Idle for a bit, time to advance to next state?
2739 * cmpxchg failure means race with non-idle, let them win.
2741 if (ULONG_CMP_GE(jiffies, j + rcu_sysidle_delay()))
2742 (void)cmpxchg(&full_sysidle_state,
2743 RCU_SYSIDLE_SHORT, RCU_SYSIDLE_LONG);
2746 case RCU_SYSIDLE_LONG:
2749 * Do an additional check pass before advancing to full.
2750 * cmpxchg failure means race with non-idle, let them win.
2752 if (ULONG_CMP_GE(jiffies, j + rcu_sysidle_delay()))
2753 (void)cmpxchg(&full_sysidle_state,
2754 RCU_SYSIDLE_LONG, RCU_SYSIDLE_FULL);
2763 * Found a non-idle non-timekeeping CPU, so kick the system-idle state
2764 * back to the beginning.
2766 static void rcu_sysidle_cancel(void)
2769 if (full_sysidle_state > RCU_SYSIDLE_SHORT)
2770 ACCESS_ONCE(full_sysidle_state) = RCU_SYSIDLE_NOT;
2774 * Update the sysidle state based on the results of a force-quiescent-state
2775 * scan of the CPUs' dyntick-idle state.
2777 static void rcu_sysidle_report(struct rcu_state *rsp, int isidle,
2778 unsigned long maxj, bool gpkt)
2780 if (rsp != rcu_state_p)
2781 return; /* Wrong flavor, ignore. */
2782 if (gpkt && nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL)
2783 return; /* Running state machine from timekeeping CPU. */
2785 rcu_sysidle(maxj); /* More idle! */
2787 rcu_sysidle_cancel(); /* Idle is over. */
2791 * Wrapper for rcu_sysidle_report() when called from the grace-period
2792 * kthread's context.
2794 static void rcu_sysidle_report_gp(struct rcu_state *rsp, int isidle,
2797 /* If there are no nohz_full= CPUs, no need to track this. */
2798 if (!tick_nohz_full_enabled())
2801 rcu_sysidle_report(rsp, isidle, maxj, true);
2804 /* Callback and function for forcing an RCU grace period. */
2805 struct rcu_sysidle_head {
2810 static void rcu_sysidle_cb(struct rcu_head *rhp)
2812 struct rcu_sysidle_head *rshp;
2815 * The following memory barrier is needed to replace the
2816 * memory barriers that would normally be in the memory
2819 smp_mb(); /* grace period precedes setting inuse. */
2821 rshp = container_of(rhp, struct rcu_sysidle_head, rh);
2822 ACCESS_ONCE(rshp->inuse) = 0;
2826 * Check to see if the system is fully idle, other than the timekeeping CPU.
2827 * The caller must have disabled interrupts. This is not intended to be
2828 * called unless tick_nohz_full_enabled().
2830 bool rcu_sys_is_idle(void)
2832 static struct rcu_sysidle_head rsh;
2833 int rss = ACCESS_ONCE(full_sysidle_state);
2835 if (WARN_ON_ONCE(smp_processor_id() != tick_do_timer_cpu))
2838 /* Handle small-system case by doing a full scan of CPUs. */
2839 if (nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL) {
2840 int oldrss = rss - 1;
2843 * One pass to advance to each state up to _FULL.
2844 * Give up if any pass fails to advance the state.
2846 while (rss < RCU_SYSIDLE_FULL && oldrss < rss) {
2849 unsigned long maxj = jiffies - ULONG_MAX / 4;
2850 struct rcu_data *rdp;
2852 /* Scan all the CPUs looking for nonidle CPUs. */
2853 for_each_possible_cpu(cpu) {
2854 rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
2855 rcu_sysidle_check_cpu(rdp, &isidle, &maxj);
2859 rcu_sysidle_report(rcu_state_p, isidle, maxj, false);
2861 rss = ACCESS_ONCE(full_sysidle_state);
2865 /* If this is the first observation of an idle period, record it. */
2866 if (rss == RCU_SYSIDLE_FULL) {
2867 rss = cmpxchg(&full_sysidle_state,
2868 RCU_SYSIDLE_FULL, RCU_SYSIDLE_FULL_NOTED);
2869 return rss == RCU_SYSIDLE_FULL;
2872 smp_mb(); /* ensure rss load happens before later caller actions. */
2874 /* If already fully idle, tell the caller (in case of races). */
2875 if (rss == RCU_SYSIDLE_FULL_NOTED)
2879 * If we aren't there yet, and a grace period is not in flight,
2880 * initiate a grace period. Either way, tell the caller that
2881 * we are not there yet. We use an xchg() rather than an assignment
2882 * to make up for the memory barriers that would otherwise be
2883 * provided by the memory allocator.
2885 if (nr_cpu_ids > CONFIG_NO_HZ_FULL_SYSIDLE_SMALL &&
2886 !rcu_gp_in_progress(rcu_state_p) &&
2887 !rsh.inuse && xchg(&rsh.inuse, 1) == 0)
2888 call_rcu(&rsh.rh, rcu_sysidle_cb);
2893 * Initialize dynticks sysidle state for CPUs coming online.
2895 static void rcu_sysidle_init_percpu_data(struct rcu_dynticks *rdtp)
2897 rdtp->dynticks_idle_nesting = DYNTICK_TASK_NEST_VALUE;
2900 #else /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
2902 static void rcu_sysidle_enter(int irq)
2906 static void rcu_sysidle_exit(int irq)
2910 static void rcu_sysidle_check_cpu(struct rcu_data *rdp, bool *isidle,
2911 unsigned long *maxj)
2915 static bool is_sysidle_rcu_state(struct rcu_state *rsp)
2920 static void rcu_sysidle_report_gp(struct rcu_state *rsp, int isidle,
2925 static void rcu_sysidle_init_percpu_data(struct rcu_dynticks *rdtp)
2929 #endif /* #else #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
2932 * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
2933 * grace-period kthread will do force_quiescent_state() processing?
2934 * The idea is to avoid waking up RCU core processing on such a
2935 * CPU unless the grace period has extended for too long.
2937 * This code relies on the fact that all NO_HZ_FULL CPUs are also
2938 * CONFIG_RCU_NOCB_CPU CPUs.
2940 static bool rcu_nohz_full_cpu(struct rcu_state *rsp)
2942 #ifdef CONFIG_NO_HZ_FULL
2943 if (tick_nohz_full_cpu(smp_processor_id()) &&
2944 (!rcu_gp_in_progress(rsp) ||
2945 ULONG_CMP_LT(jiffies, ACCESS_ONCE(rsp->gp_start) + HZ)))
2947 #endif /* #ifdef CONFIG_NO_HZ_FULL */
2952 * Bind the grace-period kthread for the sysidle flavor of RCU to the
2955 static void rcu_bind_gp_kthread(void)
2957 int __maybe_unused cpu;
2959 if (!tick_nohz_full_enabled())
2961 #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
2962 cpu = tick_do_timer_cpu;
2963 if (cpu >= 0 && cpu < nr_cpu_ids)
2964 set_cpus_allowed_ptr(current, cpumask_of(cpu));
2965 #else /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
2966 housekeeping_affine(current);
2967 #endif /* #else #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
2970 /* Record the current task on dyntick-idle entry. */
2971 static void rcu_dynticks_task_enter(void)
2973 #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
2974 ACCESS_ONCE(current->rcu_tasks_idle_cpu) = smp_processor_id();
2975 #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
2978 /* Record no current task on dyntick-idle exit. */
2979 static void rcu_dynticks_task_exit(void)
2981 #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
2982 ACCESS_ONCE(current->rcu_tasks_idle_cpu) = -1;
2983 #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */