2 * Read-Copy Update mechanism for mutual exclusion
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright IBM Corporation, 2008
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
22 * Paul E. McKenney <paulmck@linux.vnet.ibm.com> Hierarchical version
24 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
25 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
27 * For detailed explanation of Read-Copy Update mechanism see -
30 #include <linux/types.h>
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/spinlock.h>
34 #include <linux/smp.h>
35 #include <linux/rcupdate.h>
36 #include <linux/interrupt.h>
37 #include <linux/sched.h>
38 #include <linux/nmi.h>
39 #include <linux/atomic.h>
40 #include <linux/bitops.h>
41 #include <linux/export.h>
42 #include <linux/completion.h>
43 #include <linux/moduleparam.h>
44 #include <linux/percpu.h>
45 #include <linux/notifier.h>
46 #include <linux/cpu.h>
47 #include <linux/mutex.h>
48 #include <linux/time.h>
49 #include <linux/kernel_stat.h>
50 #include <linux/wait.h>
51 #include <linux/kthread.h>
52 #include <linux/prefetch.h>
53 #include <linux/delay.h>
54 #include <linux/stop_machine.h>
55 #include <linux/random.h>
58 #include <trace/events/rcu.h>
62 /* Data structures. */
64 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
65 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
67 #define RCU_STATE_INITIALIZER(sname, cr) { \
68 .level = { &sname##_state.node[0] }, \
70 .fqs_state = RCU_GP_IDLE, \
73 .onofflock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.onofflock), \
74 .orphan_nxttail = &sname##_state.orphan_nxtlist, \
75 .orphan_donetail = &sname##_state.orphan_donelist, \
76 .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
80 struct rcu_state rcu_sched_state =
81 RCU_STATE_INITIALIZER(rcu_sched, call_rcu_sched);
82 DEFINE_PER_CPU(struct rcu_data, rcu_sched_data);
84 struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh, call_rcu_bh);
85 DEFINE_PER_CPU(struct rcu_data, rcu_bh_data);
87 static struct rcu_state *rcu_state;
88 LIST_HEAD(rcu_struct_flavors);
90 /* Increase (but not decrease) the CONFIG_RCU_FANOUT_LEAF at boot time. */
91 static int rcu_fanout_leaf = CONFIG_RCU_FANOUT_LEAF;
92 module_param(rcu_fanout_leaf, int, 0444);
93 int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
94 static int num_rcu_lvl[] = { /* Number of rcu_nodes at specified level. */
101 int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
104 * The rcu_scheduler_active variable transitions from zero to one just
105 * before the first task is spawned. So when this variable is zero, RCU
106 * can assume that there is but one task, allowing RCU to (for example)
107 * optimized synchronize_sched() to a simple barrier(). When this variable
108 * is one, RCU must actually do all the hard work required to detect real
109 * grace periods. This variable is also used to suppress boot-time false
110 * positives from lockdep-RCU error checking.
112 int rcu_scheduler_active __read_mostly;
113 EXPORT_SYMBOL_GPL(rcu_scheduler_active);
116 * The rcu_scheduler_fully_active variable transitions from zero to one
117 * during the early_initcall() processing, which is after the scheduler
118 * is capable of creating new tasks. So RCU processing (for example,
119 * creating tasks for RCU priority boosting) must be delayed until after
120 * rcu_scheduler_fully_active transitions from zero to one. We also
121 * currently delay invocation of any RCU callbacks until after this point.
123 * It might later prove better for people registering RCU callbacks during
124 * early boot to take responsibility for these callbacks, but one step at
127 static int rcu_scheduler_fully_active __read_mostly;
129 #ifdef CONFIG_RCU_BOOST
132 * Control variables for per-CPU and per-rcu_node kthreads. These
133 * handle all flavors of RCU.
135 static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
136 DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
137 DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
138 DEFINE_PER_CPU(char, rcu_cpu_has_work);
140 #endif /* #ifdef CONFIG_RCU_BOOST */
142 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
143 static void invoke_rcu_core(void);
144 static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
147 * Track the rcutorture test sequence number and the update version
148 * number within a given test. The rcutorture_testseq is incremented
149 * on every rcutorture module load and unload, so has an odd value
150 * when a test is running. The rcutorture_vernum is set to zero
151 * when rcutorture starts and is incremented on each rcutorture update.
152 * These variables enable correlating rcutorture output with the
153 * RCU tracing information.
155 unsigned long rcutorture_testseq;
156 unsigned long rcutorture_vernum;
159 * Return true if an RCU grace period is in progress. The ACCESS_ONCE()s
160 * permit this function to be invoked without holding the root rcu_node
161 * structure's ->lock, but of course results can be subject to change.
163 static int rcu_gp_in_progress(struct rcu_state *rsp)
165 return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
169 * Note a quiescent state. Because we do not need to know
170 * how many quiescent states passed, just if there was at least
171 * one since the start of the grace period, this just sets a flag.
172 * The caller must have disabled preemption.
174 void rcu_sched_qs(int cpu)
176 struct rcu_data *rdp = &per_cpu(rcu_sched_data, cpu);
178 if (rdp->passed_quiesce == 0)
179 trace_rcu_grace_period("rcu_sched", rdp->gpnum, "cpuqs");
180 rdp->passed_quiesce = 1;
183 void rcu_bh_qs(int cpu)
185 struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
187 if (rdp->passed_quiesce == 0)
188 trace_rcu_grace_period("rcu_bh", rdp->gpnum, "cpuqs");
189 rdp->passed_quiesce = 1;
193 * Note a context switch. This is a quiescent state for RCU-sched,
194 * and requires special handling for preemptible RCU.
195 * The caller must have disabled preemption.
197 void rcu_note_context_switch(int cpu)
199 trace_rcu_utilization("Start context switch");
201 rcu_preempt_note_context_switch(cpu);
202 trace_rcu_utilization("End context switch");
204 EXPORT_SYMBOL_GPL(rcu_note_context_switch);
206 DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
207 .dynticks_nesting = DYNTICK_TASK_EXIT_IDLE,
208 .dynticks = ATOMIC_INIT(1),
209 #if defined(CONFIG_RCU_USER_QS) && !defined(CONFIG_RCU_USER_QS_FORCE)
210 .ignore_user_qs = true,
214 static int blimit = 10; /* Maximum callbacks per rcu_do_batch. */
215 static int qhimark = 10000; /* If this many pending, ignore blimit. */
216 static int qlowmark = 100; /* Once only this many pending, use blimit. */
218 module_param(blimit, int, 0444);
219 module_param(qhimark, int, 0444);
220 module_param(qlowmark, int, 0444);
222 int rcu_cpu_stall_suppress __read_mostly; /* 1 = suppress stall warnings. */
223 int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
225 module_param(rcu_cpu_stall_suppress, int, 0644);
226 module_param(rcu_cpu_stall_timeout, int, 0644);
228 static ulong jiffies_till_first_fqs = RCU_JIFFIES_TILL_FORCE_QS;
229 static ulong jiffies_till_next_fqs = RCU_JIFFIES_TILL_FORCE_QS;
231 module_param(jiffies_till_first_fqs, ulong, 0644);
232 module_param(jiffies_till_next_fqs, ulong, 0644);
234 static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *));
235 static void force_quiescent_state(struct rcu_state *rsp);
236 static int rcu_pending(int cpu);
239 * Return the number of RCU-sched batches processed thus far for debug & stats.
241 long rcu_batches_completed_sched(void)
243 return rcu_sched_state.completed;
245 EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
248 * Return the number of RCU BH batches processed thus far for debug & stats.
250 long rcu_batches_completed_bh(void)
252 return rcu_bh_state.completed;
254 EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
257 * Force a quiescent state for RCU BH.
259 void rcu_bh_force_quiescent_state(void)
261 force_quiescent_state(&rcu_bh_state);
263 EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
266 * Record the number of times rcutorture tests have been initiated and
267 * terminated. This information allows the debugfs tracing stats to be
268 * correlated to the rcutorture messages, even when the rcutorture module
269 * is being repeatedly loaded and unloaded. In other words, we cannot
270 * store this state in rcutorture itself.
272 void rcutorture_record_test_transition(void)
274 rcutorture_testseq++;
275 rcutorture_vernum = 0;
277 EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
280 * Record the number of writer passes through the current rcutorture test.
281 * This is also used to correlate debugfs tracing stats with the rcutorture
284 void rcutorture_record_progress(unsigned long vernum)
288 EXPORT_SYMBOL_GPL(rcutorture_record_progress);
291 * Force a quiescent state for RCU-sched.
293 void rcu_sched_force_quiescent_state(void)
295 force_quiescent_state(&rcu_sched_state);
297 EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
300 * Does the CPU have callbacks ready to be invoked?
303 cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
305 return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
309 * Does the current CPU require a yet-as-unscheduled grace period?
312 cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
314 return *rdp->nxttail[RCU_DONE_TAIL] && !rcu_gp_in_progress(rsp);
318 * Return the root node of the specified rcu_state structure.
320 static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
322 return &rsp->node[0];
326 * rcu_eqs_enter_common - current CPU is moving towards extended quiescent state
328 * If the new value of the ->dynticks_nesting counter now is zero,
329 * we really have entered idle, and must do the appropriate accounting.
330 * The caller must have disabled interrupts.
332 static void rcu_eqs_enter_common(struct rcu_dynticks *rdtp, long long oldval,
335 trace_rcu_dyntick("Start", oldval, 0);
336 if (!is_idle_task(current) && !user) {
337 struct task_struct *idle = idle_task(smp_processor_id());
339 trace_rcu_dyntick("Error on entry: not idle task", oldval, 0);
340 ftrace_dump(DUMP_ORIG);
341 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
342 current->pid, current->comm,
343 idle->pid, idle->comm); /* must be idle task! */
345 rcu_prepare_for_idle(smp_processor_id());
346 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
347 smp_mb__before_atomic_inc(); /* See above. */
348 atomic_inc(&rdtp->dynticks);
349 smp_mb__after_atomic_inc(); /* Force ordering with next sojourn. */
350 WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
353 * It is illegal to enter an extended quiescent state while
354 * in an RCU read-side critical section.
356 rcu_lockdep_assert(!lock_is_held(&rcu_lock_map),
357 "Illegal idle entry in RCU read-side critical section.");
358 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map),
359 "Illegal idle entry in RCU-bh read-side critical section.");
360 rcu_lockdep_assert(!lock_is_held(&rcu_sched_lock_map),
361 "Illegal idle entry in RCU-sched read-side critical section.");
365 * Enter an RCU extended quiescent state, which can be either the
366 * idle loop or adaptive-tickless usermode execution.
368 static void rcu_eqs_enter(bool user)
371 struct rcu_dynticks *rdtp;
373 rdtp = &__get_cpu_var(rcu_dynticks);
374 oldval = rdtp->dynticks_nesting;
375 WARN_ON_ONCE((oldval & DYNTICK_TASK_NEST_MASK) == 0);
376 if ((oldval & DYNTICK_TASK_NEST_MASK) == DYNTICK_TASK_NEST_VALUE)
377 rdtp->dynticks_nesting = 0;
379 rdtp->dynticks_nesting -= DYNTICK_TASK_NEST_VALUE;
380 rcu_eqs_enter_common(rdtp, oldval, user);
384 * rcu_idle_enter - inform RCU that current CPU is entering idle
386 * Enter idle mode, in other words, -leave- the mode in which RCU
387 * read-side critical sections can occur. (Though RCU read-side
388 * critical sections can occur in irq handlers in idle, a possibility
389 * handled by irq_enter() and irq_exit().)
391 * We crowbar the ->dynticks_nesting field to zero to allow for
392 * the possibility of usermode upcalls having messed up our count
393 * of interrupt nesting level during the prior busy period.
395 void rcu_idle_enter(void)
399 local_irq_save(flags);
401 local_irq_restore(flags);
403 EXPORT_SYMBOL_GPL(rcu_idle_enter);
405 #ifdef CONFIG_RCU_USER_QS
407 * rcu_user_enter - inform RCU that we are resuming userspace.
409 * Enter RCU idle mode right before resuming userspace. No use of RCU
410 * is permitted between this call and rcu_user_exit(). This way the
411 * CPU doesn't need to maintain the tick for RCU maintenance purposes
412 * when the CPU runs in userspace.
414 void rcu_user_enter(void)
417 struct rcu_dynticks *rdtp;
419 WARN_ON_ONCE(!current->mm);
421 local_irq_save(flags);
422 rdtp = &__get_cpu_var(rcu_dynticks);
423 if (!rdtp->ignore_user_qs && !rdtp->in_user) {
424 rdtp->in_user = true;
427 local_irq_restore(flags);
429 EXPORT_SYMBOL_GPL(rcu_user_enter);
432 * rcu_user_enter_irq - inform RCU that we are going to resume userspace
433 * after the current irq returns.
435 * This is similar to rcu_user_enter() but in the context of a non-nesting
436 * irq. After this call, RCU enters into idle mode when the interrupt
439 void rcu_user_enter_irq(void)
442 struct rcu_dynticks *rdtp;
444 local_irq_save(flags);
445 rdtp = &__get_cpu_var(rcu_dynticks);
446 /* Ensure this irq is interrupting a non-idle RCU state. */
447 WARN_ON_ONCE(!(rdtp->dynticks_nesting & DYNTICK_TASK_MASK));
448 rdtp->dynticks_nesting = 1;
449 local_irq_restore(flags);
454 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
456 * Exit from an interrupt handler, which might possibly result in entering
457 * idle mode, in other words, leaving the mode in which read-side critical
458 * sections can occur.
460 * This code assumes that the idle loop never does anything that might
461 * result in unbalanced calls to irq_enter() and irq_exit(). If your
462 * architecture violates this assumption, RCU will give you what you
463 * deserve, good and hard. But very infrequently and irreproducibly.
465 * Use things like work queues to work around this limitation.
467 * You have been warned.
469 void rcu_irq_exit(void)
473 struct rcu_dynticks *rdtp;
475 local_irq_save(flags);
476 rdtp = &__get_cpu_var(rcu_dynticks);
477 oldval = rdtp->dynticks_nesting;
478 rdtp->dynticks_nesting--;
479 WARN_ON_ONCE(rdtp->dynticks_nesting < 0);
480 if (rdtp->dynticks_nesting)
481 trace_rcu_dyntick("--=", oldval, rdtp->dynticks_nesting);
483 rcu_eqs_enter_common(rdtp, oldval, 1);
484 local_irq_restore(flags);
486 EXPORT_SYMBOL_GPL(rcu_irq_exit);
489 * rcu_eqs_exit_common - current CPU moving away from extended quiescent state
491 * If the new value of the ->dynticks_nesting counter was previously zero,
492 * we really have exited idle, and must do the appropriate accounting.
493 * The caller must have disabled interrupts.
495 static void rcu_eqs_exit_common(struct rcu_dynticks *rdtp, long long oldval,
498 smp_mb__before_atomic_inc(); /* Force ordering w/previous sojourn. */
499 atomic_inc(&rdtp->dynticks);
500 /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
501 smp_mb__after_atomic_inc(); /* See above. */
502 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
503 rcu_cleanup_after_idle(smp_processor_id());
504 trace_rcu_dyntick("End", oldval, rdtp->dynticks_nesting);
505 if (!is_idle_task(current) && !user) {
506 struct task_struct *idle = idle_task(smp_processor_id());
508 trace_rcu_dyntick("Error on exit: not idle task",
509 oldval, rdtp->dynticks_nesting);
510 ftrace_dump(DUMP_ORIG);
511 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
512 current->pid, current->comm,
513 idle->pid, idle->comm); /* must be idle task! */
518 * Exit an RCU extended quiescent state, which can be either the
519 * idle loop or adaptive-tickless usermode execution.
521 static void rcu_eqs_exit(bool user)
523 struct rcu_dynticks *rdtp;
526 rdtp = &__get_cpu_var(rcu_dynticks);
527 oldval = rdtp->dynticks_nesting;
528 WARN_ON_ONCE(oldval < 0);
529 if (oldval & DYNTICK_TASK_NEST_MASK)
530 rdtp->dynticks_nesting += DYNTICK_TASK_NEST_VALUE;
532 rdtp->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
533 rcu_eqs_exit_common(rdtp, oldval, user);
537 * rcu_idle_exit - inform RCU that current CPU is leaving idle
539 * Exit idle mode, in other words, -enter- the mode in which RCU
540 * read-side critical sections can occur.
542 * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to
543 * allow for the possibility of usermode upcalls messing up our count
544 * of interrupt nesting level during the busy period that is just
547 void rcu_idle_exit(void)
551 local_irq_save(flags);
553 local_irq_restore(flags);
555 EXPORT_SYMBOL_GPL(rcu_idle_exit);
557 #ifdef CONFIG_RCU_USER_QS
559 * rcu_user_exit - inform RCU that we are exiting userspace.
561 * Exit RCU idle mode while entering the kernel because it can
562 * run a RCU read side critical section anytime.
564 void rcu_user_exit(void)
567 struct rcu_dynticks *rdtp;
569 local_irq_save(flags);
570 rdtp = &__get_cpu_var(rcu_dynticks);
572 rdtp->in_user = false;
575 local_irq_restore(flags);
577 EXPORT_SYMBOL_GPL(rcu_user_exit);
580 * rcu_user_exit_irq - inform RCU that we won't resume to userspace
581 * idle mode after the current non-nesting irq returns.
583 * This is similar to rcu_user_exit() but in the context of an irq.
584 * This is called when the irq has interrupted a userspace RCU idle mode
585 * context. When the current non-nesting interrupt returns after this call,
586 * the CPU won't restore the RCU idle mode.
588 void rcu_user_exit_irq(void)
591 struct rcu_dynticks *rdtp;
593 local_irq_save(flags);
594 rdtp = &__get_cpu_var(rcu_dynticks);
595 /* Ensure we are interrupting an RCU idle mode. */
596 WARN_ON_ONCE(rdtp->dynticks_nesting & DYNTICK_TASK_NEST_MASK);
597 rdtp->dynticks_nesting += DYNTICK_TASK_EXIT_IDLE;
598 local_irq_restore(flags);
603 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
605 * Enter an interrupt handler, which might possibly result in exiting
606 * idle mode, in other words, entering the mode in which read-side critical
607 * sections can occur.
609 * Note that the Linux kernel is fully capable of entering an interrupt
610 * handler that it never exits, for example when doing upcalls to
611 * user mode! This code assumes that the idle loop never does upcalls to
612 * user mode. If your architecture does do upcalls from the idle loop (or
613 * does anything else that results in unbalanced calls to the irq_enter()
614 * and irq_exit() functions), RCU will give you what you deserve, good
615 * and hard. But very infrequently and irreproducibly.
617 * Use things like work queues to work around this limitation.
619 * You have been warned.
621 void rcu_irq_enter(void)
624 struct rcu_dynticks *rdtp;
627 local_irq_save(flags);
628 rdtp = &__get_cpu_var(rcu_dynticks);
629 oldval = rdtp->dynticks_nesting;
630 rdtp->dynticks_nesting++;
631 WARN_ON_ONCE(rdtp->dynticks_nesting == 0);
633 trace_rcu_dyntick("++=", oldval, rdtp->dynticks_nesting);
635 rcu_eqs_exit_common(rdtp, oldval, 1);
636 local_irq_restore(flags);
638 EXPORT_SYMBOL_GPL(rcu_irq_enter);
641 * rcu_nmi_enter - inform RCU of entry to NMI context
643 * If the CPU was idle with dynamic ticks active, and there is no
644 * irq handler running, this updates rdtp->dynticks_nmi to let the
645 * RCU grace-period handling know that the CPU is active.
647 void rcu_nmi_enter(void)
649 struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
651 if (rdtp->dynticks_nmi_nesting == 0 &&
652 (atomic_read(&rdtp->dynticks) & 0x1))
654 rdtp->dynticks_nmi_nesting++;
655 smp_mb__before_atomic_inc(); /* Force delay from prior write. */
656 atomic_inc(&rdtp->dynticks);
657 /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
658 smp_mb__after_atomic_inc(); /* See above. */
659 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
663 * rcu_nmi_exit - inform RCU of exit from NMI context
665 * If the CPU was idle with dynamic ticks active, and there is no
666 * irq handler running, this updates rdtp->dynticks_nmi to let the
667 * RCU grace-period handling know that the CPU is no longer active.
669 void rcu_nmi_exit(void)
671 struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
673 if (rdtp->dynticks_nmi_nesting == 0 ||
674 --rdtp->dynticks_nmi_nesting != 0)
676 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
677 smp_mb__before_atomic_inc(); /* See above. */
678 atomic_inc(&rdtp->dynticks);
679 smp_mb__after_atomic_inc(); /* Force delay to next write. */
680 WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
684 * rcu_is_cpu_idle - see if RCU thinks that the current CPU is idle
686 * If the current CPU is in its idle loop and is neither in an interrupt
687 * or NMI handler, return true.
689 int rcu_is_cpu_idle(void)
694 ret = (atomic_read(&__get_cpu_var(rcu_dynticks).dynticks) & 0x1) == 0;
698 EXPORT_SYMBOL(rcu_is_cpu_idle);
700 #ifdef CONFIG_RCU_USER_QS
701 void rcu_user_hooks_switch(struct task_struct *prev,
702 struct task_struct *next)
704 struct rcu_dynticks *rdtp;
706 /* Interrupts are disabled in context switch */
707 rdtp = &__get_cpu_var(rcu_dynticks);
708 if (!rdtp->ignore_user_qs) {
709 clear_tsk_thread_flag(prev, TIF_NOHZ);
710 set_tsk_thread_flag(next, TIF_NOHZ);
713 #endif /* #ifdef CONFIG_RCU_USER_QS */
715 #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
718 * Is the current CPU online? Disable preemption to avoid false positives
719 * that could otherwise happen due to the current CPU number being sampled,
720 * this task being preempted, its old CPU being taken offline, resuming
721 * on some other CPU, then determining that its old CPU is now offline.
722 * It is OK to use RCU on an offline processor during initial boot, hence
723 * the check for rcu_scheduler_fully_active. Note also that it is OK
724 * for a CPU coming online to use RCU for one jiffy prior to marking itself
725 * online in the cpu_online_mask. Similarly, it is OK for a CPU going
726 * offline to continue to use RCU for one jiffy after marking itself
727 * offline in the cpu_online_mask. This leniency is necessary given the
728 * non-atomic nature of the online and offline processing, for example,
729 * the fact that a CPU enters the scheduler after completing the CPU_DYING
732 * This is also why RCU internally marks CPUs online during the
733 * CPU_UP_PREPARE phase and offline during the CPU_DEAD phase.
735 * Disable checking if in an NMI handler because we cannot safely report
736 * errors from NMI handlers anyway.
738 bool rcu_lockdep_current_cpu_online(void)
740 struct rcu_data *rdp;
741 struct rcu_node *rnp;
747 rdp = &__get_cpu_var(rcu_sched_data);
749 ret = (rdp->grpmask & rnp->qsmaskinit) ||
750 !rcu_scheduler_fully_active;
754 EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
756 #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
759 * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
761 * If the current CPU is idle or running at a first-level (not nested)
762 * interrupt from idle, return true. The caller must have at least
763 * disabled preemption.
765 int rcu_is_cpu_rrupt_from_idle(void)
767 return __get_cpu_var(rcu_dynticks).dynticks_nesting <= 1;
771 * Snapshot the specified CPU's dynticks counter so that we can later
772 * credit them with an implicit quiescent state. Return 1 if this CPU
773 * is in dynticks idle mode, which is an extended quiescent state.
775 static int dyntick_save_progress_counter(struct rcu_data *rdp)
777 rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
778 return (rdp->dynticks_snap & 0x1) == 0;
782 * Return true if the specified CPU has passed through a quiescent
783 * state by virtue of being in or having passed through an dynticks
784 * idle state since the last call to dyntick_save_progress_counter()
785 * for this same CPU, or by virtue of having been offline.
787 static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
792 curr = (unsigned int)atomic_add_return(0, &rdp->dynticks->dynticks);
793 snap = (unsigned int)rdp->dynticks_snap;
796 * If the CPU passed through or entered a dynticks idle phase with
797 * no active irq/NMI handlers, then we can safely pretend that the CPU
798 * already acknowledged the request to pass through a quiescent
799 * state. Either way, that CPU cannot possibly be in an RCU
800 * read-side critical section that started before the beginning
801 * of the current RCU grace period.
803 if ((curr & 0x1) == 0 || UINT_CMP_GE(curr, snap + 2)) {
804 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, "dti");
810 * Check for the CPU being offline, but only if the grace period
811 * is old enough. We don't need to worry about the CPU changing
812 * state: If we see it offline even once, it has been through a
815 * The reason for insisting that the grace period be at least
816 * one jiffy old is that CPUs that are not quite online and that
817 * have just gone offline can still execute RCU read-side critical
820 if (ULONG_CMP_GE(rdp->rsp->gp_start + 2, jiffies))
821 return 0; /* Grace period is not old enough. */
823 if (cpu_is_offline(rdp->cpu)) {
824 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, "ofl");
831 static int jiffies_till_stall_check(void)
833 int till_stall_check = ACCESS_ONCE(rcu_cpu_stall_timeout);
836 * Limit check must be consistent with the Kconfig limits
837 * for CONFIG_RCU_CPU_STALL_TIMEOUT.
839 if (till_stall_check < 3) {
840 ACCESS_ONCE(rcu_cpu_stall_timeout) = 3;
841 till_stall_check = 3;
842 } else if (till_stall_check > 300) {
843 ACCESS_ONCE(rcu_cpu_stall_timeout) = 300;
844 till_stall_check = 300;
846 return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
849 static void record_gp_stall_check_time(struct rcu_state *rsp)
851 rsp->gp_start = jiffies;
852 rsp->jiffies_stall = jiffies + jiffies_till_stall_check();
855 static void print_other_cpu_stall(struct rcu_state *rsp)
861 struct rcu_node *rnp = rcu_get_root(rsp);
863 /* Only let one CPU complain about others per time interval. */
865 raw_spin_lock_irqsave(&rnp->lock, flags);
866 delta = jiffies - rsp->jiffies_stall;
867 if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
868 raw_spin_unlock_irqrestore(&rnp->lock, flags);
871 rsp->jiffies_stall = jiffies + 3 * jiffies_till_stall_check() + 3;
872 raw_spin_unlock_irqrestore(&rnp->lock, flags);
875 * OK, time to rat on our buddy...
876 * See Documentation/RCU/stallwarn.txt for info on how to debug
877 * RCU CPU stall warnings.
879 printk(KERN_ERR "INFO: %s detected stalls on CPUs/tasks:",
881 print_cpu_stall_info_begin();
882 rcu_for_each_leaf_node(rsp, rnp) {
883 raw_spin_lock_irqsave(&rnp->lock, flags);
884 ndetected += rcu_print_task_stall(rnp);
885 if (rnp->qsmask == 0) {
886 raw_spin_unlock_irqrestore(&rnp->lock, flags);
889 for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
890 if (rnp->qsmask & (1UL << cpu)) {
891 print_cpu_stall_info(rsp, rnp->grplo + cpu);
894 raw_spin_unlock_irqrestore(&rnp->lock, flags);
898 * Now rat on any tasks that got kicked up to the root rcu_node
899 * due to CPU offlining.
901 rnp = rcu_get_root(rsp);
902 raw_spin_lock_irqsave(&rnp->lock, flags);
903 ndetected += rcu_print_task_stall(rnp);
904 raw_spin_unlock_irqrestore(&rnp->lock, flags);
906 print_cpu_stall_info_end();
907 printk(KERN_CONT "(detected by %d, t=%ld jiffies)\n",
908 smp_processor_id(), (long)(jiffies - rsp->gp_start));
910 printk(KERN_ERR "INFO: Stall ended before state dump start\n");
911 else if (!trigger_all_cpu_backtrace())
914 /* Complain about tasks blocking the grace period. */
916 rcu_print_detail_task_stall(rsp);
918 force_quiescent_state(rsp); /* Kick them all. */
921 static void print_cpu_stall(struct rcu_state *rsp)
924 struct rcu_node *rnp = rcu_get_root(rsp);
927 * OK, time to rat on ourselves...
928 * See Documentation/RCU/stallwarn.txt for info on how to debug
929 * RCU CPU stall warnings.
931 printk(KERN_ERR "INFO: %s self-detected stall on CPU", rsp->name);
932 print_cpu_stall_info_begin();
933 print_cpu_stall_info(rsp, smp_processor_id());
934 print_cpu_stall_info_end();
935 printk(KERN_CONT " (t=%lu jiffies)\n", jiffies - rsp->gp_start);
936 if (!trigger_all_cpu_backtrace())
939 raw_spin_lock_irqsave(&rnp->lock, flags);
940 if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall))
941 rsp->jiffies_stall = jiffies +
942 3 * jiffies_till_stall_check() + 3;
943 raw_spin_unlock_irqrestore(&rnp->lock, flags);
945 set_need_resched(); /* kick ourselves to get things going. */
948 static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
952 struct rcu_node *rnp;
954 if (rcu_cpu_stall_suppress)
956 j = ACCESS_ONCE(jiffies);
957 js = ACCESS_ONCE(rsp->jiffies_stall);
959 if (rcu_gp_in_progress(rsp) &&
960 (ACCESS_ONCE(rnp->qsmask) & rdp->grpmask) && ULONG_CMP_GE(j, js)) {
962 /* We haven't checked in, so go dump stack. */
963 print_cpu_stall(rsp);
965 } else if (rcu_gp_in_progress(rsp) &&
966 ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
968 /* They had a few time units to dump stack, so complain. */
969 print_other_cpu_stall(rsp);
973 static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
975 rcu_cpu_stall_suppress = 1;
980 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
982 * Set the stall-warning timeout way off into the future, thus preventing
983 * any RCU CPU stall-warning messages from appearing in the current set of
986 * The caller must disable hard irqs.
988 void rcu_cpu_stall_reset(void)
990 struct rcu_state *rsp;
992 for_each_rcu_flavor(rsp)
993 rsp->jiffies_stall = jiffies + ULONG_MAX / 2;
996 static struct notifier_block rcu_panic_block = {
997 .notifier_call = rcu_panic,
1000 static void __init check_cpu_stall_init(void)
1002 atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
1006 * Update CPU-local rcu_data state to record the newly noticed grace period.
1007 * This is used both when we started the grace period and when we notice
1008 * that someone else started the grace period. The caller must hold the
1009 * ->lock of the leaf rcu_node structure corresponding to the current CPU,
1010 * and must have irqs disabled.
1012 static void __note_new_gpnum(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
1014 if (rdp->gpnum != rnp->gpnum) {
1016 * If the current grace period is waiting for this CPU,
1017 * set up to detect a quiescent state, otherwise don't
1018 * go looking for one.
1020 rdp->gpnum = rnp->gpnum;
1021 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpustart");
1022 rdp->passed_quiesce = 0;
1023 rdp->qs_pending = !!(rnp->qsmask & rdp->grpmask);
1024 zero_cpu_stall_ticks(rdp);
1028 static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
1030 unsigned long flags;
1031 struct rcu_node *rnp;
1033 local_irq_save(flags);
1035 if (rdp->gpnum == ACCESS_ONCE(rnp->gpnum) || /* outside lock. */
1036 !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
1037 local_irq_restore(flags);
1040 __note_new_gpnum(rsp, rnp, rdp);
1041 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1045 * Did someone else start a new RCU grace period start since we last
1046 * checked? Update local state appropriately if so. Must be called
1047 * on the CPU corresponding to rdp.
1050 check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
1052 unsigned long flags;
1055 local_irq_save(flags);
1056 if (rdp->gpnum != rsp->gpnum) {
1057 note_new_gpnum(rsp, rdp);
1060 local_irq_restore(flags);
1065 * Initialize the specified rcu_data structure's callback list to empty.
1067 static void init_callback_list(struct rcu_data *rdp)
1071 rdp->nxtlist = NULL;
1072 for (i = 0; i < RCU_NEXT_SIZE; i++)
1073 rdp->nxttail[i] = &rdp->nxtlist;
1077 * Advance this CPU's callbacks, but only if the current grace period
1078 * has ended. This may be called only from the CPU to whom the rdp
1079 * belongs. In addition, the corresponding leaf rcu_node structure's
1080 * ->lock must be held by the caller, with irqs disabled.
1083 __rcu_process_gp_end(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
1085 /* Did another grace period end? */
1086 if (rdp->completed != rnp->completed) {
1088 /* Advance callbacks. No harm if list empty. */
1089 rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
1090 rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
1091 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
1093 /* Remember that we saw this grace-period completion. */
1094 rdp->completed = rnp->completed;
1095 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpuend");
1098 * If we were in an extended quiescent state, we may have
1099 * missed some grace periods that others CPUs handled on
1100 * our behalf. Catch up with this state to avoid noting
1101 * spurious new grace periods. If another grace period
1102 * has started, then rnp->gpnum will have advanced, so
1103 * we will detect this later on. Of course, any quiescent
1104 * states we found for the old GP are now invalid.
1106 if (ULONG_CMP_LT(rdp->gpnum, rdp->completed)) {
1107 rdp->gpnum = rdp->completed;
1108 rdp->passed_quiesce = 0;
1112 * If RCU does not need a quiescent state from this CPU,
1113 * then make sure that this CPU doesn't go looking for one.
1115 if ((rnp->qsmask & rdp->grpmask) == 0)
1116 rdp->qs_pending = 0;
1121 * Advance this CPU's callbacks, but only if the current grace period
1122 * has ended. This may be called only from the CPU to whom the rdp
1126 rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
1128 unsigned long flags;
1129 struct rcu_node *rnp;
1131 local_irq_save(flags);
1133 if (rdp->completed == ACCESS_ONCE(rnp->completed) || /* outside lock. */
1134 !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
1135 local_irq_restore(flags);
1138 __rcu_process_gp_end(rsp, rnp, rdp);
1139 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1143 * Do per-CPU grace-period initialization for running CPU. The caller
1144 * must hold the lock of the leaf rcu_node structure corresponding to
1148 rcu_start_gp_per_cpu(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
1150 /* Prior grace period ended, so advance callbacks for current CPU. */
1151 __rcu_process_gp_end(rsp, rnp, rdp);
1153 /* Set state so that this CPU will detect the next quiescent state. */
1154 __note_new_gpnum(rsp, rnp, rdp);
1158 * Initialize a new grace period.
1160 static int rcu_gp_init(struct rcu_state *rsp)
1162 unsigned long flags;
1163 struct rcu_data *rdp;
1164 struct rcu_node *rnp = rcu_get_root(rsp);
1166 raw_spin_lock_irqsave(&rnp->lock, flags);
1167 rsp->gp_flags = 0; /* Clear all flags: New grace period. */
1169 if (rcu_gp_in_progress(rsp)) {
1170 /* Grace period already in progress, don't start another. */
1171 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1175 /* Advance to a new grace period and initialize state. */
1177 trace_rcu_grace_period(rsp->name, rsp->gpnum, "start");
1178 record_gp_stall_check_time(rsp);
1179 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1181 /* Exclude any concurrent CPU-hotplug operations. */
1185 * Set the quiescent-state-needed bits in all the rcu_node
1186 * structures for all currently online CPUs in breadth-first order,
1187 * starting from the root rcu_node structure, relying on the layout
1188 * of the tree within the rsp->node[] array. Note that other CPUs
1189 * access only the leaves of the hierarchy, thus seeing that no
1190 * grace period is in progress, at least until the corresponding
1191 * leaf node has been initialized. In addition, we have excluded
1192 * CPU-hotplug operations.
1194 * The grace period cannot complete until the initialization
1195 * process finishes, because this kthread handles both.
1197 rcu_for_each_node_breadth_first(rsp, rnp) {
1198 raw_spin_lock_irqsave(&rnp->lock, flags);
1199 rdp = this_cpu_ptr(rsp->rda);
1200 rcu_preempt_check_blocked_tasks(rnp);
1201 rnp->qsmask = rnp->qsmaskinit;
1202 rnp->gpnum = rsp->gpnum;
1203 WARN_ON_ONCE(rnp->completed != rsp->completed);
1204 rnp->completed = rsp->completed;
1205 if (rnp == rdp->mynode)
1206 rcu_start_gp_per_cpu(rsp, rnp, rdp);
1207 rcu_preempt_boost_start_gp(rnp);
1208 trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
1209 rnp->level, rnp->grplo,
1210 rnp->grphi, rnp->qsmask);
1211 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1212 #ifdef CONFIG_PROVE_RCU_DELAY
1213 if ((random32() % (rcu_num_nodes * 8)) == 0)
1214 schedule_timeout_uninterruptible(2);
1215 #endif /* #ifdef CONFIG_PROVE_RCU_DELAY */
1224 * Do one round of quiescent-state forcing.
1226 int rcu_gp_fqs(struct rcu_state *rsp, int fqs_state_in)
1228 unsigned long flags;
1229 int fqs_state = fqs_state_in;
1230 struct rcu_node *rnp = rcu_get_root(rsp);
1233 if (fqs_state == RCU_SAVE_DYNTICK) {
1234 /* Collect dyntick-idle snapshots. */
1235 force_qs_rnp(rsp, dyntick_save_progress_counter);
1236 fqs_state = RCU_FORCE_QS;
1238 /* Handle dyntick-idle and offline CPUs. */
1239 force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
1241 /* Clear flag to prevent immediate re-entry. */
1242 if (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
1243 raw_spin_lock_irqsave(&rnp->lock, flags);
1244 rsp->gp_flags &= ~RCU_GP_FLAG_FQS;
1245 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1251 * Clean up after the old grace period.
1253 static void rcu_gp_cleanup(struct rcu_state *rsp)
1255 unsigned long flags;
1256 unsigned long gp_duration;
1257 struct rcu_data *rdp;
1258 struct rcu_node *rnp = rcu_get_root(rsp);
1260 raw_spin_lock_irqsave(&rnp->lock, flags);
1261 gp_duration = jiffies - rsp->gp_start;
1262 if (gp_duration > rsp->gp_max)
1263 rsp->gp_max = gp_duration;
1266 * We know the grace period is complete, but to everyone else
1267 * it appears to still be ongoing. But it is also the case
1268 * that to everyone else it looks like there is nothing that
1269 * they can do to advance the grace period. It is therefore
1270 * safe for us to drop the lock in order to mark the grace
1271 * period as completed in all of the rcu_node structures.
1273 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1276 * Propagate new ->completed value to rcu_node structures so
1277 * that other CPUs don't have to wait until the start of the next
1278 * grace period to process their callbacks. This also avoids
1279 * some nasty RCU grace-period initialization races.
1281 rcu_for_each_node_breadth_first(rsp, rnp) {
1282 raw_spin_lock_irqsave(&rnp->lock, flags);
1283 rnp->completed = rsp->gpnum;
1284 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1287 rnp = rcu_get_root(rsp);
1288 raw_spin_lock_irqsave(&rnp->lock, flags);
1290 rsp->completed = rsp->gpnum; /* Declare grace period done. */
1291 trace_rcu_grace_period(rsp->name, rsp->completed, "end");
1292 rsp->fqs_state = RCU_GP_IDLE;
1293 rdp = this_cpu_ptr(rsp->rda);
1294 if (cpu_needs_another_gp(rsp, rdp))
1296 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1300 * Body of kthread that handles grace periods.
1302 static int rcu_gp_kthread(void *arg)
1307 struct rcu_state *rsp = arg;
1308 struct rcu_node *rnp = rcu_get_root(rsp);
1312 /* Handle grace-period start. */
1314 wait_event_interruptible(rsp->gp_wq,
1317 if ((rsp->gp_flags & RCU_GP_FLAG_INIT) &&
1321 flush_signals(current);
1324 /* Handle quiescent-state forcing. */
1325 fqs_state = RCU_SAVE_DYNTICK;
1326 j = jiffies_till_first_fqs;
1329 jiffies_till_first_fqs = HZ;
1332 rsp->jiffies_force_qs = jiffies + j;
1333 ret = wait_event_interruptible_timeout(rsp->gp_wq,
1334 (rsp->gp_flags & RCU_GP_FLAG_FQS) ||
1335 (!ACCESS_ONCE(rnp->qsmask) &&
1336 !rcu_preempt_blocked_readers_cgp(rnp)),
1338 /* If grace period done, leave loop. */
1339 if (!ACCESS_ONCE(rnp->qsmask) &&
1340 !rcu_preempt_blocked_readers_cgp(rnp))
1342 /* If time for quiescent-state forcing, do it. */
1343 if (ret == 0 || (rsp->gp_flags & RCU_GP_FLAG_FQS)) {
1344 fqs_state = rcu_gp_fqs(rsp, fqs_state);
1347 /* Deal with stray signal. */
1349 flush_signals(current);
1351 j = jiffies_till_next_fqs;
1354 jiffies_till_next_fqs = HZ;
1357 jiffies_till_next_fqs = 1;
1361 /* Handle grace-period end. */
1362 rcu_gp_cleanup(rsp);
1368 * Start a new RCU grace period if warranted, re-initializing the hierarchy
1369 * in preparation for detecting the next grace period. The caller must hold
1370 * the root node's ->lock, which is released before return. Hard irqs must
1373 * Note that it is legal for a dying CPU (which is marked as offline) to
1374 * invoke this function. This can happen when the dying CPU reports its
1378 rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
1379 __releases(rcu_get_root(rsp)->lock)
1381 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
1382 struct rcu_node *rnp = rcu_get_root(rsp);
1384 if (!rsp->gp_kthread ||
1385 !cpu_needs_another_gp(rsp, rdp)) {
1387 * Either we have not yet spawned the grace-period
1388 * task or this CPU does not need another grace period.
1389 * Either way, don't start a new grace period.
1391 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1396 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1397 wake_up(&rsp->gp_wq);
1401 * Report a full set of quiescent states to the specified rcu_state
1402 * data structure. This involves cleaning up after the prior grace
1403 * period and letting rcu_start_gp() start up the next grace period
1404 * if one is needed. Note that the caller must hold rnp->lock, as
1405 * required by rcu_start_gp(), which will release it.
1407 static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
1408 __releases(rcu_get_root(rsp)->lock)
1410 WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
1411 raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
1412 wake_up(&rsp->gp_wq); /* Memory barrier implied by wake_up() path. */
1416 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
1417 * Allows quiescent states for a group of CPUs to be reported at one go
1418 * to the specified rcu_node structure, though all the CPUs in the group
1419 * must be represented by the same rcu_node structure (which need not be
1420 * a leaf rcu_node structure, though it often will be). That structure's
1421 * lock must be held upon entry, and it is released before return.
1424 rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
1425 struct rcu_node *rnp, unsigned long flags)
1426 __releases(rnp->lock)
1428 struct rcu_node *rnp_c;
1430 /* Walk up the rcu_node hierarchy. */
1432 if (!(rnp->qsmask & mask)) {
1434 /* Our bit has already been cleared, so done. */
1435 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1438 rnp->qsmask &= ~mask;
1439 trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
1440 mask, rnp->qsmask, rnp->level,
1441 rnp->grplo, rnp->grphi,
1443 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
1445 /* Other bits still set at this level, so done. */
1446 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1449 mask = rnp->grpmask;
1450 if (rnp->parent == NULL) {
1452 /* No more levels. Exit loop holding root lock. */
1456 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1459 raw_spin_lock_irqsave(&rnp->lock, flags);
1460 WARN_ON_ONCE(rnp_c->qsmask);
1464 * Get here if we are the last CPU to pass through a quiescent
1465 * state for this grace period. Invoke rcu_report_qs_rsp()
1466 * to clean up and start the next grace period if one is needed.
1468 rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
1472 * Record a quiescent state for the specified CPU to that CPU's rcu_data
1473 * structure. This must be either called from the specified CPU, or
1474 * called when the specified CPU is known to be offline (and when it is
1475 * also known that no other CPU is concurrently trying to help the offline
1476 * CPU). The lastcomp argument is used to make sure we are still in the
1477 * grace period of interest. We don't want to end the current grace period
1478 * based on quiescent states detected in an earlier grace period!
1481 rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp)
1483 unsigned long flags;
1485 struct rcu_node *rnp;
1488 raw_spin_lock_irqsave(&rnp->lock, flags);
1489 if (rdp->passed_quiesce == 0 || rdp->gpnum != rnp->gpnum ||
1490 rnp->completed == rnp->gpnum) {
1493 * The grace period in which this quiescent state was
1494 * recorded has ended, so don't report it upwards.
1495 * We will instead need a new quiescent state that lies
1496 * within the current grace period.
1498 rdp->passed_quiesce = 0; /* need qs for new gp. */
1499 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1502 mask = rdp->grpmask;
1503 if ((rnp->qsmask & mask) == 0) {
1504 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1506 rdp->qs_pending = 0;
1509 * This GP can't end until cpu checks in, so all of our
1510 * callbacks can be processed during the next GP.
1512 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
1514 rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */
1519 * Check to see if there is a new grace period of which this CPU
1520 * is not yet aware, and if so, set up local rcu_data state for it.
1521 * Otherwise, see if this CPU has just passed through its first
1522 * quiescent state for this grace period, and record that fact if so.
1525 rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
1527 /* If there is now a new grace period, record and return. */
1528 if (check_for_new_grace_period(rsp, rdp))
1532 * Does this CPU still need to do its part for current grace period?
1533 * If no, return and let the other CPUs do their part as well.
1535 if (!rdp->qs_pending)
1539 * Was there a quiescent state since the beginning of the grace
1540 * period? If no, then exit and wait for the next call.
1542 if (!rdp->passed_quiesce)
1546 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
1549 rcu_report_qs_rdp(rdp->cpu, rsp, rdp);
1552 #ifdef CONFIG_HOTPLUG_CPU
1555 * Send the specified CPU's RCU callbacks to the orphanage. The
1556 * specified CPU must be offline, and the caller must hold the
1560 rcu_send_cbs_to_orphanage(int cpu, struct rcu_state *rsp,
1561 struct rcu_node *rnp, struct rcu_data *rdp)
1564 * Orphan the callbacks. First adjust the counts. This is safe
1565 * because ->onofflock excludes _rcu_barrier()'s adoption of
1566 * the callbacks, thus no memory barrier is required.
1568 if (rdp->nxtlist != NULL) {
1569 rsp->qlen_lazy += rdp->qlen_lazy;
1570 rsp->qlen += rdp->qlen;
1571 rdp->n_cbs_orphaned += rdp->qlen;
1573 ACCESS_ONCE(rdp->qlen) = 0;
1577 * Next, move those callbacks still needing a grace period to
1578 * the orphanage, where some other CPU will pick them up.
1579 * Some of the callbacks might have gone partway through a grace
1580 * period, but that is too bad. They get to start over because we
1581 * cannot assume that grace periods are synchronized across CPUs.
1582 * We don't bother updating the ->nxttail[] array yet, instead
1583 * we just reset the whole thing later on.
1585 if (*rdp->nxttail[RCU_DONE_TAIL] != NULL) {
1586 *rsp->orphan_nxttail = *rdp->nxttail[RCU_DONE_TAIL];
1587 rsp->orphan_nxttail = rdp->nxttail[RCU_NEXT_TAIL];
1588 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
1592 * Then move the ready-to-invoke callbacks to the orphanage,
1593 * where some other CPU will pick them up. These will not be
1594 * required to pass though another grace period: They are done.
1596 if (rdp->nxtlist != NULL) {
1597 *rsp->orphan_donetail = rdp->nxtlist;
1598 rsp->orphan_donetail = rdp->nxttail[RCU_DONE_TAIL];
1601 /* Finally, initialize the rcu_data structure's list to empty. */
1602 init_callback_list(rdp);
1606 * Adopt the RCU callbacks from the specified rcu_state structure's
1607 * orphanage. The caller must hold the ->onofflock.
1609 static void rcu_adopt_orphan_cbs(struct rcu_state *rsp)
1612 struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
1614 /* Do the accounting first. */
1615 rdp->qlen_lazy += rsp->qlen_lazy;
1616 rdp->qlen += rsp->qlen;
1617 rdp->n_cbs_adopted += rsp->qlen;
1618 if (rsp->qlen_lazy != rsp->qlen)
1619 rcu_idle_count_callbacks_posted();
1624 * We do not need a memory barrier here because the only way we
1625 * can get here if there is an rcu_barrier() in flight is if
1626 * we are the task doing the rcu_barrier().
1629 /* First adopt the ready-to-invoke callbacks. */
1630 if (rsp->orphan_donelist != NULL) {
1631 *rsp->orphan_donetail = *rdp->nxttail[RCU_DONE_TAIL];
1632 *rdp->nxttail[RCU_DONE_TAIL] = rsp->orphan_donelist;
1633 for (i = RCU_NEXT_SIZE - 1; i >= RCU_DONE_TAIL; i--)
1634 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
1635 rdp->nxttail[i] = rsp->orphan_donetail;
1636 rsp->orphan_donelist = NULL;
1637 rsp->orphan_donetail = &rsp->orphan_donelist;
1640 /* And then adopt the callbacks that still need a grace period. */
1641 if (rsp->orphan_nxtlist != NULL) {
1642 *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxtlist;
1643 rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxttail;
1644 rsp->orphan_nxtlist = NULL;
1645 rsp->orphan_nxttail = &rsp->orphan_nxtlist;
1650 * Trace the fact that this CPU is going offline.
1652 static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
1654 RCU_TRACE(unsigned long mask);
1655 RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda));
1656 RCU_TRACE(struct rcu_node *rnp = rdp->mynode);
1658 RCU_TRACE(mask = rdp->grpmask);
1659 trace_rcu_grace_period(rsp->name,
1660 rnp->gpnum + 1 - !!(rnp->qsmask & mask),
1665 * The CPU has been completely removed, and some other CPU is reporting
1666 * this fact from process context. Do the remainder of the cleanup,
1667 * including orphaning the outgoing CPU's RCU callbacks, and also
1668 * adopting them. There can only be one CPU hotplug operation at a time,
1669 * so no other CPU can be attempting to update rcu_cpu_kthread_task.
1671 static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
1673 unsigned long flags;
1675 int need_report = 0;
1676 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
1677 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
1679 /* Adjust any no-longer-needed kthreads. */
1680 rcu_boost_kthread_setaffinity(rnp, -1);
1682 /* Remove the dead CPU from the bitmasks in the rcu_node hierarchy. */
1684 /* Exclude any attempts to start a new grace period. */
1685 raw_spin_lock_irqsave(&rsp->onofflock, flags);
1687 /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */
1688 rcu_send_cbs_to_orphanage(cpu, rsp, rnp, rdp);
1689 rcu_adopt_orphan_cbs(rsp);
1691 /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
1692 mask = rdp->grpmask; /* rnp->grplo is constant. */
1694 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
1695 rnp->qsmaskinit &= ~mask;
1696 if (rnp->qsmaskinit != 0) {
1697 if (rnp != rdp->mynode)
1698 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1701 if (rnp == rdp->mynode)
1702 need_report = rcu_preempt_offline_tasks(rsp, rnp, rdp);
1704 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1705 mask = rnp->grpmask;
1707 } while (rnp != NULL);
1710 * We still hold the leaf rcu_node structure lock here, and
1711 * irqs are still disabled. The reason for this subterfuge is
1712 * because invoking rcu_report_unblock_qs_rnp() with ->onofflock
1713 * held leads to deadlock.
1715 raw_spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
1717 if (need_report & RCU_OFL_TASKS_NORM_GP)
1718 rcu_report_unblock_qs_rnp(rnp, flags);
1720 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1721 if (need_report & RCU_OFL_TASKS_EXP_GP)
1722 rcu_report_exp_rnp(rsp, rnp, true);
1723 WARN_ONCE(rdp->qlen != 0 || rdp->nxtlist != NULL,
1724 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n",
1725 cpu, rdp->qlen, rdp->nxtlist);
1726 init_callback_list(rdp);
1727 /* Disallow further callbacks on this CPU. */
1728 rdp->nxttail[RCU_NEXT_TAIL] = NULL;
1731 #else /* #ifdef CONFIG_HOTPLUG_CPU */
1733 static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
1737 static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
1741 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1744 * Invoke any RCU callbacks that have made it to the end of their grace
1745 * period. Thottle as specified by rdp->blimit.
1747 static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
1749 unsigned long flags;
1750 struct rcu_head *next, *list, **tail;
1751 int bl, count, count_lazy, i;
1753 /* If no callbacks are ready, just return.*/
1754 if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
1755 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0);
1756 trace_rcu_batch_end(rsp->name, 0, !!ACCESS_ONCE(rdp->nxtlist),
1757 need_resched(), is_idle_task(current),
1758 rcu_is_callbacks_kthread());
1763 * Extract the list of ready callbacks, disabling to prevent
1764 * races with call_rcu() from interrupt handlers.
1766 local_irq_save(flags);
1767 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
1769 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl);
1770 list = rdp->nxtlist;
1771 rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
1772 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
1773 tail = rdp->nxttail[RCU_DONE_TAIL];
1774 for (i = RCU_NEXT_SIZE - 1; i >= 0; i--)
1775 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
1776 rdp->nxttail[i] = &rdp->nxtlist;
1777 local_irq_restore(flags);
1779 /* Invoke callbacks. */
1780 count = count_lazy = 0;
1784 debug_rcu_head_unqueue(list);
1785 if (__rcu_reclaim(rsp->name, list))
1788 /* Stop only if limit reached and CPU has something to do. */
1789 if (++count >= bl &&
1791 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
1795 local_irq_save(flags);
1796 trace_rcu_batch_end(rsp->name, count, !!list, need_resched(),
1797 is_idle_task(current),
1798 rcu_is_callbacks_kthread());
1800 /* Update count, and requeue any remaining callbacks. */
1802 *tail = rdp->nxtlist;
1803 rdp->nxtlist = list;
1804 for (i = 0; i < RCU_NEXT_SIZE; i++)
1805 if (&rdp->nxtlist == rdp->nxttail[i])
1806 rdp->nxttail[i] = tail;
1810 smp_mb(); /* List handling before counting for rcu_barrier(). */
1811 rdp->qlen_lazy -= count_lazy;
1812 ACCESS_ONCE(rdp->qlen) -= count;
1813 rdp->n_cbs_invoked += count;
1815 /* Reinstate batch limit if we have worked down the excess. */
1816 if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
1817 rdp->blimit = blimit;
1819 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
1820 if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
1821 rdp->qlen_last_fqs_check = 0;
1822 rdp->n_force_qs_snap = rsp->n_force_qs;
1823 } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
1824 rdp->qlen_last_fqs_check = rdp->qlen;
1825 WARN_ON_ONCE((rdp->nxtlist == NULL) != (rdp->qlen == 0));
1827 local_irq_restore(flags);
1829 /* Re-invoke RCU core processing if there are callbacks remaining. */
1830 if (cpu_has_callbacks_ready_to_invoke(rdp))
1835 * Check to see if this CPU is in a non-context-switch quiescent state
1836 * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
1837 * Also schedule RCU core processing.
1839 * This function must be called from hardirq context. It is normally
1840 * invoked from the scheduling-clock interrupt. If rcu_pending returns
1841 * false, there is no point in invoking rcu_check_callbacks().
1843 void rcu_check_callbacks(int cpu, int user)
1845 trace_rcu_utilization("Start scheduler-tick");
1846 increment_cpu_stall_ticks();
1847 if (user || rcu_is_cpu_rrupt_from_idle()) {
1850 * Get here if this CPU took its interrupt from user
1851 * mode or from the idle loop, and if this is not a
1852 * nested interrupt. In this case, the CPU is in
1853 * a quiescent state, so note it.
1855 * No memory barrier is required here because both
1856 * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
1857 * variables that other CPUs neither access nor modify,
1858 * at least not while the corresponding CPU is online.
1864 } else if (!in_softirq()) {
1867 * Get here if this CPU did not take its interrupt from
1868 * softirq, in other words, if it is not interrupting
1869 * a rcu_bh read-side critical section. This is an _bh
1870 * critical section, so note it.
1875 rcu_preempt_check_callbacks(cpu);
1876 if (rcu_pending(cpu))
1878 trace_rcu_utilization("End scheduler-tick");
1882 * Scan the leaf rcu_node structures, processing dyntick state for any that
1883 * have not yet encountered a quiescent state, using the function specified.
1884 * Also initiate boosting for any threads blocked on the root rcu_node.
1886 * The caller must have suppressed start of new grace periods.
1888 static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *))
1892 unsigned long flags;
1894 struct rcu_node *rnp;
1896 rcu_for_each_leaf_node(rsp, rnp) {
1899 raw_spin_lock_irqsave(&rnp->lock, flags);
1900 if (!rcu_gp_in_progress(rsp)) {
1901 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1904 if (rnp->qsmask == 0) {
1905 rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
1910 for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
1911 if ((rnp->qsmask & bit) != 0 &&
1912 f(per_cpu_ptr(rsp->rda, cpu)))
1917 /* rcu_report_qs_rnp() releases rnp->lock. */
1918 rcu_report_qs_rnp(mask, rsp, rnp, flags);
1921 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1923 rnp = rcu_get_root(rsp);
1924 if (rnp->qsmask == 0) {
1925 raw_spin_lock_irqsave(&rnp->lock, flags);
1926 rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
1931 * Force quiescent states on reluctant CPUs, and also detect which
1932 * CPUs are in dyntick-idle mode.
1934 static void force_quiescent_state(struct rcu_state *rsp)
1936 unsigned long flags;
1938 struct rcu_node *rnp;
1939 struct rcu_node *rnp_old = NULL;
1941 /* Funnel through hierarchy to reduce memory contention. */
1942 rnp = per_cpu_ptr(rsp->rda, raw_smp_processor_id())->mynode;
1943 for (; rnp != NULL; rnp = rnp->parent) {
1944 ret = (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
1945 !raw_spin_trylock(&rnp->fqslock);
1946 if (rnp_old != NULL)
1947 raw_spin_unlock(&rnp_old->fqslock);
1949 rsp->n_force_qs_lh++;
1954 /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
1956 /* Reached the root of the rcu_node tree, acquire lock. */
1957 raw_spin_lock_irqsave(&rnp_old->lock, flags);
1958 raw_spin_unlock(&rnp_old->fqslock);
1959 if (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
1960 rsp->n_force_qs_lh++;
1961 raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
1962 return; /* Someone beat us to it. */
1964 rsp->gp_flags |= RCU_GP_FLAG_FQS;
1965 raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
1966 wake_up(&rsp->gp_wq); /* Memory barrier implied by wake_up() path. */
1970 * This does the RCU core processing work for the specified rcu_state
1971 * and rcu_data structures. This may be called only from the CPU to
1972 * whom the rdp belongs.
1975 __rcu_process_callbacks(struct rcu_state *rsp)
1977 unsigned long flags;
1978 struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
1980 WARN_ON_ONCE(rdp->beenonline == 0);
1983 * Advance callbacks in response to end of earlier grace
1984 * period that some other CPU ended.
1986 rcu_process_gp_end(rsp, rdp);
1988 /* Update RCU state based on any recent quiescent states. */
1989 rcu_check_quiescent_state(rsp, rdp);
1991 /* Does this CPU require a not-yet-started grace period? */
1992 if (cpu_needs_another_gp(rsp, rdp)) {
1993 raw_spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
1994 rcu_start_gp(rsp, flags); /* releases above lock */
1997 /* If there are callbacks ready, invoke them. */
1998 if (cpu_has_callbacks_ready_to_invoke(rdp))
1999 invoke_rcu_callbacks(rsp, rdp);
2003 * Do RCU core processing for the current CPU.
2005 static void rcu_process_callbacks(struct softirq_action *unused)
2007 struct rcu_state *rsp;
2009 if (cpu_is_offline(smp_processor_id()))
2011 trace_rcu_utilization("Start RCU core");
2012 for_each_rcu_flavor(rsp)
2013 __rcu_process_callbacks(rsp);
2014 trace_rcu_utilization("End RCU core");
2018 * Schedule RCU callback invocation. If the specified type of RCU
2019 * does not support RCU priority boosting, just do a direct call,
2020 * otherwise wake up the per-CPU kernel kthread. Note that because we
2021 * are running on the current CPU with interrupts disabled, the
2022 * rcu_cpu_kthread_task cannot disappear out from under us.
2024 static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
2026 if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active)))
2028 if (likely(!rsp->boost)) {
2029 rcu_do_batch(rsp, rdp);
2032 invoke_rcu_callbacks_kthread();
2035 static void invoke_rcu_core(void)
2037 raise_softirq(RCU_SOFTIRQ);
2041 * Handle any core-RCU processing required by a call_rcu() invocation.
2043 static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
2044 struct rcu_head *head, unsigned long flags)
2047 * If called from an extended quiescent state, invoke the RCU
2048 * core in order to force a re-evaluation of RCU's idleness.
2050 if (rcu_is_cpu_idle() && cpu_online(smp_processor_id()))
2053 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
2054 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
2058 * Force the grace period if too many callbacks or too long waiting.
2059 * Enforce hysteresis, and don't invoke force_quiescent_state()
2060 * if some other CPU has recently done so. Also, don't bother
2061 * invoking force_quiescent_state() if the newly enqueued callback
2062 * is the only one waiting for a grace period to complete.
2064 if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
2066 /* Are we ignoring a completed grace period? */
2067 rcu_process_gp_end(rsp, rdp);
2068 check_for_new_grace_period(rsp, rdp);
2070 /* Start a new grace period if one not already started. */
2071 if (!rcu_gp_in_progress(rsp)) {
2072 unsigned long nestflag;
2073 struct rcu_node *rnp_root = rcu_get_root(rsp);
2075 raw_spin_lock_irqsave(&rnp_root->lock, nestflag);
2076 rcu_start_gp(rsp, nestflag); /* rlses rnp_root->lock */
2078 /* Give the grace period a kick. */
2079 rdp->blimit = LONG_MAX;
2080 if (rsp->n_force_qs == rdp->n_force_qs_snap &&
2081 *rdp->nxttail[RCU_DONE_TAIL] != head)
2082 force_quiescent_state(rsp);
2083 rdp->n_force_qs_snap = rsp->n_force_qs;
2084 rdp->qlen_last_fqs_check = rdp->qlen;
2090 __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
2091 struct rcu_state *rsp, bool lazy)
2093 unsigned long flags;
2094 struct rcu_data *rdp;
2096 WARN_ON_ONCE((unsigned long)head & 0x3); /* Misaligned rcu_head! */
2097 debug_rcu_head_queue(head);
2102 * Opportunistically note grace-period endings and beginnings.
2103 * Note that we might see a beginning right after we see an
2104 * end, but never vice versa, since this CPU has to pass through
2105 * a quiescent state betweentimes.
2107 local_irq_save(flags);
2108 rdp = this_cpu_ptr(rsp->rda);
2110 /* Add the callback to our list. */
2111 if (unlikely(rdp->nxttail[RCU_NEXT_TAIL] == NULL)) {
2112 /* _call_rcu() is illegal on offline CPU; leak the callback. */
2114 local_irq_restore(flags);
2117 ACCESS_ONCE(rdp->qlen)++;
2121 rcu_idle_count_callbacks_posted();
2122 smp_mb(); /* Count before adding callback for rcu_barrier(). */
2123 *rdp->nxttail[RCU_NEXT_TAIL] = head;
2124 rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
2126 if (__is_kfree_rcu_offset((unsigned long)func))
2127 trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
2128 rdp->qlen_lazy, rdp->qlen);
2130 trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen);
2132 /* Go handle any RCU core processing required. */
2133 __call_rcu_core(rsp, rdp, head, flags);
2134 local_irq_restore(flags);
2138 * Queue an RCU-sched callback for invocation after a grace period.
2140 void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
2142 __call_rcu(head, func, &rcu_sched_state, 0);
2144 EXPORT_SYMBOL_GPL(call_rcu_sched);
2147 * Queue an RCU callback for invocation after a quicker grace period.
2149 void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
2151 __call_rcu(head, func, &rcu_bh_state, 0);
2153 EXPORT_SYMBOL_GPL(call_rcu_bh);
2156 * Because a context switch is a grace period for RCU-sched and RCU-bh,
2157 * any blocking grace-period wait automatically implies a grace period
2158 * if there is only one CPU online at any point time during execution
2159 * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
2160 * occasionally incorrectly indicate that there are multiple CPUs online
2161 * when there was in fact only one the whole time, as this just adds
2162 * some overhead: RCU still operates correctly.
2164 static inline int rcu_blocking_is_gp(void)
2168 might_sleep(); /* Check for RCU read-side critical section. */
2170 ret = num_online_cpus() <= 1;
2176 * synchronize_sched - wait until an rcu-sched grace period has elapsed.
2178 * Control will return to the caller some time after a full rcu-sched
2179 * grace period has elapsed, in other words after all currently executing
2180 * rcu-sched read-side critical sections have completed. These read-side
2181 * critical sections are delimited by rcu_read_lock_sched() and
2182 * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
2183 * local_irq_disable(), and so on may be used in place of
2184 * rcu_read_lock_sched().
2186 * This means that all preempt_disable code sequences, including NMI and
2187 * hardware-interrupt handlers, in progress on entry will have completed
2188 * before this primitive returns. However, this does not guarantee that
2189 * softirq handlers will have completed, since in some kernels, these
2190 * handlers can run in process context, and can block.
2192 * This primitive provides the guarantees made by the (now removed)
2193 * synchronize_kernel() API. In contrast, synchronize_rcu() only
2194 * guarantees that rcu_read_lock() sections will have completed.
2195 * In "classic RCU", these two guarantees happen to be one and
2196 * the same, but can differ in realtime RCU implementations.
2198 void synchronize_sched(void)
2200 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
2201 !lock_is_held(&rcu_lock_map) &&
2202 !lock_is_held(&rcu_sched_lock_map),
2203 "Illegal synchronize_sched() in RCU-sched read-side critical section");
2204 if (rcu_blocking_is_gp())
2206 wait_rcu_gp(call_rcu_sched);
2208 EXPORT_SYMBOL_GPL(synchronize_sched);
2211 * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
2213 * Control will return to the caller some time after a full rcu_bh grace
2214 * period has elapsed, in other words after all currently executing rcu_bh
2215 * read-side critical sections have completed. RCU read-side critical
2216 * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
2217 * and may be nested.
2219 void synchronize_rcu_bh(void)
2221 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
2222 !lock_is_held(&rcu_lock_map) &&
2223 !lock_is_held(&rcu_sched_lock_map),
2224 "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
2225 if (rcu_blocking_is_gp())
2227 wait_rcu_gp(call_rcu_bh);
2229 EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
2231 static atomic_t sync_sched_expedited_started = ATOMIC_INIT(0);
2232 static atomic_t sync_sched_expedited_done = ATOMIC_INIT(0);
2234 static int synchronize_sched_expedited_cpu_stop(void *data)
2237 * There must be a full memory barrier on each affected CPU
2238 * between the time that try_stop_cpus() is called and the
2239 * time that it returns.
2241 * In the current initial implementation of cpu_stop, the
2242 * above condition is already met when the control reaches
2243 * this point and the following smp_mb() is not strictly
2244 * necessary. Do smp_mb() anyway for documentation and
2245 * robustness against future implementation changes.
2247 smp_mb(); /* See above comment block. */
2252 * synchronize_sched_expedited - Brute-force RCU-sched grace period
2254 * Wait for an RCU-sched grace period to elapse, but use a "big hammer"
2255 * approach to force the grace period to end quickly. This consumes
2256 * significant time on all CPUs and is unfriendly to real-time workloads,
2257 * so is thus not recommended for any sort of common-case code. In fact,
2258 * if you are using synchronize_sched_expedited() in a loop, please
2259 * restructure your code to batch your updates, and then use a single
2260 * synchronize_sched() instead.
2262 * Note that it is illegal to call this function while holding any lock
2263 * that is acquired by a CPU-hotplug notifier. And yes, it is also illegal
2264 * to call this function from a CPU-hotplug notifier. Failing to observe
2265 * these restriction will result in deadlock.
2267 * This implementation can be thought of as an application of ticket
2268 * locking to RCU, with sync_sched_expedited_started and
2269 * sync_sched_expedited_done taking on the roles of the halves
2270 * of the ticket-lock word. Each task atomically increments
2271 * sync_sched_expedited_started upon entry, snapshotting the old value,
2272 * then attempts to stop all the CPUs. If this succeeds, then each
2273 * CPU will have executed a context switch, resulting in an RCU-sched
2274 * grace period. We are then done, so we use atomic_cmpxchg() to
2275 * update sync_sched_expedited_done to match our snapshot -- but
2276 * only if someone else has not already advanced past our snapshot.
2278 * On the other hand, if try_stop_cpus() fails, we check the value
2279 * of sync_sched_expedited_done. If it has advanced past our
2280 * initial snapshot, then someone else must have forced a grace period
2281 * some time after we took our snapshot. In this case, our work is
2282 * done for us, and we can simply return. Otherwise, we try again,
2283 * but keep our initial snapshot for purposes of checking for someone
2284 * doing our work for us.
2286 * If we fail too many times in a row, we fall back to synchronize_sched().
2288 void synchronize_sched_expedited(void)
2290 int firstsnap, s, snap, trycount = 0;
2292 /* Note that atomic_inc_return() implies full memory barrier. */
2293 firstsnap = snap = atomic_inc_return(&sync_sched_expedited_started);
2295 WARN_ON_ONCE(cpu_is_offline(raw_smp_processor_id()));
2298 * Each pass through the following loop attempts to force a
2299 * context switch on each CPU.
2301 while (try_stop_cpus(cpu_online_mask,
2302 synchronize_sched_expedited_cpu_stop,
2306 /* No joy, try again later. Or just synchronize_sched(). */
2307 if (trycount++ < 10) {
2308 udelay(trycount * num_online_cpus());
2310 synchronize_sched();
2314 /* Check to see if someone else did our work for us. */
2315 s = atomic_read(&sync_sched_expedited_done);
2316 if (UINT_CMP_GE((unsigned)s, (unsigned)firstsnap)) {
2317 smp_mb(); /* ensure test happens before caller kfree */
2322 * Refetching sync_sched_expedited_started allows later
2323 * callers to piggyback on our grace period. We subtract
2324 * 1 to get the same token that the last incrementer got.
2325 * We retry after they started, so our grace period works
2326 * for them, and they started after our first try, so their
2327 * grace period works for us.
2330 snap = atomic_read(&sync_sched_expedited_started);
2331 smp_mb(); /* ensure read is before try_stop_cpus(). */
2335 * Everyone up to our most recent fetch is covered by our grace
2336 * period. Update the counter, but only if our work is still
2337 * relevant -- which it won't be if someone who started later
2338 * than we did beat us to the punch.
2341 s = atomic_read(&sync_sched_expedited_done);
2342 if (UINT_CMP_GE((unsigned)s, (unsigned)snap)) {
2343 smp_mb(); /* ensure test happens before caller kfree */
2346 } while (atomic_cmpxchg(&sync_sched_expedited_done, s, snap) != s);
2350 EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
2353 * Check to see if there is any immediate RCU-related work to be done
2354 * by the current CPU, for the specified type of RCU, returning 1 if so.
2355 * The checks are in order of increasing expense: checks that can be
2356 * carried out against CPU-local state are performed first. However,
2357 * we must check for CPU stalls first, else we might not get a chance.
2359 static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
2361 struct rcu_node *rnp = rdp->mynode;
2363 rdp->n_rcu_pending++;
2365 /* Check for CPU stalls, if enabled. */
2366 check_cpu_stall(rsp, rdp);
2368 /* Is the RCU core waiting for a quiescent state from this CPU? */
2369 if (rcu_scheduler_fully_active &&
2370 rdp->qs_pending && !rdp->passed_quiesce) {
2371 rdp->n_rp_qs_pending++;
2372 } else if (rdp->qs_pending && rdp->passed_quiesce) {
2373 rdp->n_rp_report_qs++;
2377 /* Does this CPU have callbacks ready to invoke? */
2378 if (cpu_has_callbacks_ready_to_invoke(rdp)) {
2379 rdp->n_rp_cb_ready++;
2383 /* Has RCU gone idle with this CPU needing another grace period? */
2384 if (cpu_needs_another_gp(rsp, rdp)) {
2385 rdp->n_rp_cpu_needs_gp++;
2389 /* Has another RCU grace period completed? */
2390 if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
2391 rdp->n_rp_gp_completed++;
2395 /* Has a new RCU grace period started? */
2396 if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */
2397 rdp->n_rp_gp_started++;
2402 rdp->n_rp_need_nothing++;
2407 * Check to see if there is any immediate RCU-related work to be done
2408 * by the current CPU, returning 1 if so. This function is part of the
2409 * RCU implementation; it is -not- an exported member of the RCU API.
2411 static int rcu_pending(int cpu)
2413 struct rcu_state *rsp;
2415 for_each_rcu_flavor(rsp)
2416 if (__rcu_pending(rsp, per_cpu_ptr(rsp->rda, cpu)))
2422 * Check to see if any future RCU-related work will need to be done
2423 * by the current CPU, even if none need be done immediately, returning
2426 static int rcu_cpu_has_callbacks(int cpu)
2428 struct rcu_state *rsp;
2430 /* RCU callbacks either ready or pending? */
2431 for_each_rcu_flavor(rsp)
2432 if (per_cpu_ptr(rsp->rda, cpu)->nxtlist)
2438 * Helper function for _rcu_barrier() tracing. If tracing is disabled,
2439 * the compiler is expected to optimize this away.
2441 static void _rcu_barrier_trace(struct rcu_state *rsp, char *s,
2442 int cpu, unsigned long done)
2444 trace_rcu_barrier(rsp->name, s, cpu,
2445 atomic_read(&rsp->barrier_cpu_count), done);
2449 * RCU callback function for _rcu_barrier(). If we are last, wake
2450 * up the task executing _rcu_barrier().
2452 static void rcu_barrier_callback(struct rcu_head *rhp)
2454 struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
2455 struct rcu_state *rsp = rdp->rsp;
2457 if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
2458 _rcu_barrier_trace(rsp, "LastCB", -1, rsp->n_barrier_done);
2459 complete(&rsp->barrier_completion);
2461 _rcu_barrier_trace(rsp, "CB", -1, rsp->n_barrier_done);
2466 * Called with preemption disabled, and from cross-cpu IRQ context.
2468 static void rcu_barrier_func(void *type)
2470 struct rcu_state *rsp = type;
2471 struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
2473 _rcu_barrier_trace(rsp, "IRQ", -1, rsp->n_barrier_done);
2474 atomic_inc(&rsp->barrier_cpu_count);
2475 rsp->call(&rdp->barrier_head, rcu_barrier_callback);
2479 * Orchestrate the specified type of RCU barrier, waiting for all
2480 * RCU callbacks of the specified type to complete.
2482 static void _rcu_barrier(struct rcu_state *rsp)
2485 struct rcu_data *rdp;
2486 unsigned long snap = ACCESS_ONCE(rsp->n_barrier_done);
2487 unsigned long snap_done;
2489 _rcu_barrier_trace(rsp, "Begin", -1, snap);
2491 /* Take mutex to serialize concurrent rcu_barrier() requests. */
2492 mutex_lock(&rsp->barrier_mutex);
2495 * Ensure that all prior references, including to ->n_barrier_done,
2496 * are ordered before the _rcu_barrier() machinery.
2498 smp_mb(); /* See above block comment. */
2501 * Recheck ->n_barrier_done to see if others did our work for us.
2502 * This means checking ->n_barrier_done for an even-to-odd-to-even
2503 * transition. The "if" expression below therefore rounds the old
2504 * value up to the next even number and adds two before comparing.
2506 snap_done = ACCESS_ONCE(rsp->n_barrier_done);
2507 _rcu_barrier_trace(rsp, "Check", -1, snap_done);
2508 if (ULONG_CMP_GE(snap_done, ((snap + 1) & ~0x1) + 2)) {
2509 _rcu_barrier_trace(rsp, "EarlyExit", -1, snap_done);
2510 smp_mb(); /* caller's subsequent code after above check. */
2511 mutex_unlock(&rsp->barrier_mutex);
2516 * Increment ->n_barrier_done to avoid duplicate work. Use
2517 * ACCESS_ONCE() to prevent the compiler from speculating
2518 * the increment to precede the early-exit check.
2520 ACCESS_ONCE(rsp->n_barrier_done)++;
2521 WARN_ON_ONCE((rsp->n_barrier_done & 0x1) != 1);
2522 _rcu_barrier_trace(rsp, "Inc1", -1, rsp->n_barrier_done);
2523 smp_mb(); /* Order ->n_barrier_done increment with below mechanism. */
2526 * Initialize the count to one rather than to zero in order to
2527 * avoid a too-soon return to zero in case of a short grace period
2528 * (or preemption of this task). Exclude CPU-hotplug operations
2529 * to ensure that no offline CPU has callbacks queued.
2531 init_completion(&rsp->barrier_completion);
2532 atomic_set(&rsp->barrier_cpu_count, 1);
2536 * Force each CPU with callbacks to register a new callback.
2537 * When that callback is invoked, we will know that all of the
2538 * corresponding CPU's preceding callbacks have been invoked.
2540 for_each_online_cpu(cpu) {
2541 rdp = per_cpu_ptr(rsp->rda, cpu);
2542 if (ACCESS_ONCE(rdp->qlen)) {
2543 _rcu_barrier_trace(rsp, "OnlineQ", cpu,
2544 rsp->n_barrier_done);
2545 smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
2547 _rcu_barrier_trace(rsp, "OnlineNQ", cpu,
2548 rsp->n_barrier_done);
2554 * Now that we have an rcu_barrier_callback() callback on each
2555 * CPU, and thus each counted, remove the initial count.
2557 if (atomic_dec_and_test(&rsp->barrier_cpu_count))
2558 complete(&rsp->barrier_completion);
2560 /* Increment ->n_barrier_done to prevent duplicate work. */
2561 smp_mb(); /* Keep increment after above mechanism. */
2562 ACCESS_ONCE(rsp->n_barrier_done)++;
2563 WARN_ON_ONCE((rsp->n_barrier_done & 0x1) != 0);
2564 _rcu_barrier_trace(rsp, "Inc2", -1, rsp->n_barrier_done);
2565 smp_mb(); /* Keep increment before caller's subsequent code. */
2567 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
2568 wait_for_completion(&rsp->barrier_completion);
2570 /* Other rcu_barrier() invocations can now safely proceed. */
2571 mutex_unlock(&rsp->barrier_mutex);
2575 * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
2577 void rcu_barrier_bh(void)
2579 _rcu_barrier(&rcu_bh_state);
2581 EXPORT_SYMBOL_GPL(rcu_barrier_bh);
2584 * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
2586 void rcu_barrier_sched(void)
2588 _rcu_barrier(&rcu_sched_state);
2590 EXPORT_SYMBOL_GPL(rcu_barrier_sched);
2593 * Do boot-time initialization of a CPU's per-CPU RCU data.
2596 rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
2598 unsigned long flags;
2599 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
2600 struct rcu_node *rnp = rcu_get_root(rsp);
2602 /* Set up local state, ensuring consistent view of global state. */
2603 raw_spin_lock_irqsave(&rnp->lock, flags);
2604 rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
2605 init_callback_list(rdp);
2607 ACCESS_ONCE(rdp->qlen) = 0;
2608 rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
2609 WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_EXIT_IDLE);
2610 WARN_ON_ONCE(atomic_read(&rdp->dynticks->dynticks) != 1);
2611 #ifdef CONFIG_RCU_USER_QS
2612 WARN_ON_ONCE(rdp->dynticks->in_user);
2616 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2620 * Initialize a CPU's per-CPU RCU data. Note that only one online or
2621 * offline event can be happening at a given time. Note also that we
2622 * can accept some slop in the rsp->completed access due to the fact
2623 * that this CPU cannot possibly have any RCU callbacks in flight yet.
2625 static void __cpuinit
2626 rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptible)
2628 unsigned long flags;
2630 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
2631 struct rcu_node *rnp = rcu_get_root(rsp);
2633 /* Set up local state, ensuring consistent view of global state. */
2634 raw_spin_lock_irqsave(&rnp->lock, flags);
2635 rdp->beenonline = 1; /* We have now been online. */
2636 rdp->preemptible = preemptible;
2637 rdp->qlen_last_fqs_check = 0;
2638 rdp->n_force_qs_snap = rsp->n_force_qs;
2639 rdp->blimit = blimit;
2640 init_callback_list(rdp); /* Re-enable callbacks on this CPU. */
2641 rdp->dynticks->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
2642 atomic_set(&rdp->dynticks->dynticks,
2643 (atomic_read(&rdp->dynticks->dynticks) & ~0x1) + 1);
2644 rcu_prepare_for_idle_init(cpu);
2645 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
2648 * A new grace period might start here. If so, we won't be part
2649 * of it, but that is OK, as we are currently in a quiescent state.
2652 /* Exclude any attempts to start a new GP on large systems. */
2653 raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
2655 /* Add CPU to rcu_node bitmasks. */
2657 mask = rdp->grpmask;
2659 /* Exclude any attempts to start a new GP on small systems. */
2660 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
2661 rnp->qsmaskinit |= mask;
2662 mask = rnp->grpmask;
2663 if (rnp == rdp->mynode) {
2665 * If there is a grace period in progress, we will
2666 * set up to wait for it next time we run the
2669 rdp->gpnum = rnp->completed;
2670 rdp->completed = rnp->completed;
2671 rdp->passed_quiesce = 0;
2672 rdp->qs_pending = 0;
2673 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpuonl");
2675 raw_spin_unlock(&rnp->lock); /* irqs already disabled. */
2677 } while (rnp != NULL && !(rnp->qsmaskinit & mask));
2679 raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
2682 static void __cpuinit rcu_prepare_cpu(int cpu)
2684 struct rcu_state *rsp;
2686 for_each_rcu_flavor(rsp)
2687 rcu_init_percpu_data(cpu, rsp,
2688 strcmp(rsp->name, "rcu_preempt") == 0);
2692 * Handle CPU online/offline notification events.
2694 static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
2695 unsigned long action, void *hcpu)
2697 long cpu = (long)hcpu;
2698 struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
2699 struct rcu_node *rnp = rdp->mynode;
2700 struct rcu_state *rsp;
2702 trace_rcu_utilization("Start CPU hotplug");
2704 case CPU_UP_PREPARE:
2705 case CPU_UP_PREPARE_FROZEN:
2706 rcu_prepare_cpu(cpu);
2707 rcu_prepare_kthreads(cpu);
2710 case CPU_DOWN_FAILED:
2711 rcu_boost_kthread_setaffinity(rnp, -1);
2713 case CPU_DOWN_PREPARE:
2714 rcu_boost_kthread_setaffinity(rnp, cpu);
2717 case CPU_DYING_FROZEN:
2719 * The whole machine is "stopped" except this CPU, so we can
2720 * touch any data without introducing corruption. We send the
2721 * dying CPU's callbacks to an arbitrarily chosen online CPU.
2723 for_each_rcu_flavor(rsp)
2724 rcu_cleanup_dying_cpu(rsp);
2725 rcu_cleanup_after_idle(cpu);
2728 case CPU_DEAD_FROZEN:
2729 case CPU_UP_CANCELED:
2730 case CPU_UP_CANCELED_FROZEN:
2731 for_each_rcu_flavor(rsp)
2732 rcu_cleanup_dead_cpu(cpu, rsp);
2737 trace_rcu_utilization("End CPU hotplug");
2742 * Spawn the kthread that handles this RCU flavor's grace periods.
2744 static int __init rcu_spawn_gp_kthread(void)
2746 unsigned long flags;
2747 struct rcu_node *rnp;
2748 struct rcu_state *rsp;
2749 struct task_struct *t;
2751 for_each_rcu_flavor(rsp) {
2752 t = kthread_run(rcu_gp_kthread, rsp, rsp->name);
2754 rnp = rcu_get_root(rsp);
2755 raw_spin_lock_irqsave(&rnp->lock, flags);
2756 rsp->gp_kthread = t;
2757 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2761 early_initcall(rcu_spawn_gp_kthread);
2764 * This function is invoked towards the end of the scheduler's initialization
2765 * process. Before this is called, the idle task might contain
2766 * RCU read-side critical sections (during which time, this idle
2767 * task is booting the system). After this function is called, the
2768 * idle tasks are prohibited from containing RCU read-side critical
2769 * sections. This function also enables RCU lockdep checking.
2771 void rcu_scheduler_starting(void)
2773 WARN_ON(num_online_cpus() != 1);
2774 WARN_ON(nr_context_switches() > 0);
2775 rcu_scheduler_active = 1;
2779 * Compute the per-level fanout, either using the exact fanout specified
2780 * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
2782 #ifdef CONFIG_RCU_FANOUT_EXACT
2783 static void __init rcu_init_levelspread(struct rcu_state *rsp)
2787 for (i = rcu_num_lvls - 1; i > 0; i--)
2788 rsp->levelspread[i] = CONFIG_RCU_FANOUT;
2789 rsp->levelspread[0] = rcu_fanout_leaf;
2791 #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
2792 static void __init rcu_init_levelspread(struct rcu_state *rsp)
2799 for (i = rcu_num_lvls - 1; i >= 0; i--) {
2800 ccur = rsp->levelcnt[i];
2801 rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
2805 #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
2808 * Helper function for rcu_init() that initializes one rcu_state structure.
2810 static void __init rcu_init_one(struct rcu_state *rsp,
2811 struct rcu_data __percpu *rda)
2813 static char *buf[] = { "rcu_node_0",
2816 "rcu_node_3" }; /* Match MAX_RCU_LVLS */
2817 static char *fqs[] = { "rcu_node_fqs_0",
2820 "rcu_node_fqs_3" }; /* Match MAX_RCU_LVLS */
2824 struct rcu_node *rnp;
2826 BUILD_BUG_ON(MAX_RCU_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
2828 /* Initialize the level-tracking arrays. */
2830 for (i = 0; i < rcu_num_lvls; i++)
2831 rsp->levelcnt[i] = num_rcu_lvl[i];
2832 for (i = 1; i < rcu_num_lvls; i++)
2833 rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
2834 rcu_init_levelspread(rsp);
2836 /* Initialize the elements themselves, starting from the leaves. */
2838 for (i = rcu_num_lvls - 1; i >= 0; i--) {
2839 cpustride *= rsp->levelspread[i];
2840 rnp = rsp->level[i];
2841 for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
2842 raw_spin_lock_init(&rnp->lock);
2843 lockdep_set_class_and_name(&rnp->lock,
2844 &rcu_node_class[i], buf[i]);
2845 raw_spin_lock_init(&rnp->fqslock);
2846 lockdep_set_class_and_name(&rnp->fqslock,
2847 &rcu_fqs_class[i], fqs[i]);
2848 rnp->gpnum = rsp->gpnum;
2849 rnp->completed = rsp->completed;
2851 rnp->qsmaskinit = 0;
2852 rnp->grplo = j * cpustride;
2853 rnp->grphi = (j + 1) * cpustride - 1;
2854 if (rnp->grphi >= NR_CPUS)
2855 rnp->grphi = NR_CPUS - 1;
2861 rnp->grpnum = j % rsp->levelspread[i - 1];
2862 rnp->grpmask = 1UL << rnp->grpnum;
2863 rnp->parent = rsp->level[i - 1] +
2864 j / rsp->levelspread[i - 1];
2867 INIT_LIST_HEAD(&rnp->blkd_tasks);
2872 init_waitqueue_head(&rsp->gp_wq);
2873 rnp = rsp->level[rcu_num_lvls - 1];
2874 for_each_possible_cpu(i) {
2875 while (i > rnp->grphi)
2877 per_cpu_ptr(rsp->rda, i)->mynode = rnp;
2878 rcu_boot_init_percpu_data(i, rsp);
2880 list_add(&rsp->flavors, &rcu_struct_flavors);
2884 * Compute the rcu_node tree geometry from kernel parameters. This cannot
2885 * replace the definitions in rcutree.h because those are needed to size
2886 * the ->node array in the rcu_state structure.
2888 static void __init rcu_init_geometry(void)
2893 int rcu_capacity[MAX_RCU_LVLS + 1];
2895 /* If the compile-time values are accurate, just leave. */
2896 if (rcu_fanout_leaf == CONFIG_RCU_FANOUT_LEAF)
2900 * Compute number of nodes that can be handled an rcu_node tree
2901 * with the given number of levels. Setting rcu_capacity[0] makes
2902 * some of the arithmetic easier.
2904 rcu_capacity[0] = 1;
2905 rcu_capacity[1] = rcu_fanout_leaf;
2906 for (i = 2; i <= MAX_RCU_LVLS; i++)
2907 rcu_capacity[i] = rcu_capacity[i - 1] * CONFIG_RCU_FANOUT;
2910 * The boot-time rcu_fanout_leaf parameter is only permitted
2911 * to increase the leaf-level fanout, not decrease it. Of course,
2912 * the leaf-level fanout cannot exceed the number of bits in
2913 * the rcu_node masks. Finally, the tree must be able to accommodate
2914 * the configured number of CPUs. Complain and fall back to the
2915 * compile-time values if these limits are exceeded.
2917 if (rcu_fanout_leaf < CONFIG_RCU_FANOUT_LEAF ||
2918 rcu_fanout_leaf > sizeof(unsigned long) * 8 ||
2919 n > rcu_capacity[MAX_RCU_LVLS]) {
2924 /* Calculate the number of rcu_nodes at each level of the tree. */
2925 for (i = 1; i <= MAX_RCU_LVLS; i++)
2926 if (n <= rcu_capacity[i]) {
2927 for (j = 0; j <= i; j++)
2929 DIV_ROUND_UP(n, rcu_capacity[i - j]);
2931 for (j = i + 1; j <= MAX_RCU_LVLS; j++)
2936 /* Calculate the total number of rcu_node structures. */
2938 for (i = 0; i <= MAX_RCU_LVLS; i++)
2939 rcu_num_nodes += num_rcu_lvl[i];
2943 void __init rcu_init(void)
2947 rcu_bootup_announce();
2948 rcu_init_geometry();
2949 rcu_init_one(&rcu_sched_state, &rcu_sched_data);
2950 rcu_init_one(&rcu_bh_state, &rcu_bh_data);
2951 __rcu_init_preempt();
2952 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
2955 * We don't need protection against CPU-hotplug here because
2956 * this is called early in boot, before either interrupts
2957 * or the scheduler are operational.
2959 cpu_notifier(rcu_cpu_notify, 0);
2960 for_each_online_cpu(cpu)
2961 rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
2962 check_cpu_stall_init();
2965 #include "rcutree_plugin.h"