rcu: Fix day-zero grace-period initialization/cleanup race
[deliverable/linux.git] / kernel / rcutree.c
CommitLineData
64db4cff
PM
1/*
2 * Read-Copy Update mechanism for mutual exclusion
3 *
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.
8 *
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.
13 *
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.
17 *
18 * Copyright IBM Corporation, 2008
19 *
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
23 *
24 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
25 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
26 *
27 * For detailed explanation of Read-Copy Update mechanism see -
a71fca58 28 * Documentation/RCU
64db4cff
PM
29 */
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>
c1dc0b9c 38#include <linux/nmi.h>
8826f3b0 39#include <linux/atomic.h>
64db4cff 40#include <linux/bitops.h>
9984de1a 41#include <linux/export.h>
64db4cff
PM
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>
bbad9379 49#include <linux/kernel_stat.h>
a26ac245
PM
50#include <linux/wait.h>
51#include <linux/kthread.h>
268bb0ce 52#include <linux/prefetch.h>
3d3b7db0
PM
53#include <linux/delay.h>
54#include <linux/stop_machine.h>
64db4cff 55
9f77da9f 56#include "rcutree.h"
29c00b4a
PM
57#include <trace/events/rcu.h>
58
59#include "rcu.h"
9f77da9f 60
64db4cff
PM
61/* Data structures. */
62
f885b7f2 63static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
394f2769 64static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
88b91c7c 65
037b64ed 66#define RCU_STATE_INITIALIZER(sname, cr) { \
6c90cc7b 67 .level = { &sname##_state.node[0] }, \
037b64ed 68 .call = cr, \
af446b70 69 .fqs_state = RCU_GP_IDLE, \
64db4cff
PM
70 .gpnum = -300, \
71 .completed = -300, \
6c90cc7b
PM
72 .onofflock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.onofflock), \
73 .orphan_nxttail = &sname##_state.orphan_nxtlist, \
74 .orphan_donetail = &sname##_state.orphan_donelist, \
7be7f0be 75 .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
6c90cc7b 76 .name = #sname, \
64db4cff
PM
77}
78
037b64ed
PM
79struct rcu_state rcu_sched_state =
80 RCU_STATE_INITIALIZER(rcu_sched, call_rcu_sched);
d6714c22 81DEFINE_PER_CPU(struct rcu_data, rcu_sched_data);
64db4cff 82
037b64ed 83struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh, call_rcu_bh);
6258c4fb 84DEFINE_PER_CPU(struct rcu_data, rcu_bh_data);
b1f77b05 85
27f4d280 86static struct rcu_state *rcu_state;
6ce75a23 87LIST_HEAD(rcu_struct_flavors);
27f4d280 88
f885b7f2
PM
89/* Increase (but not decrease) the CONFIG_RCU_FANOUT_LEAF at boot time. */
90static int rcu_fanout_leaf = CONFIG_RCU_FANOUT_LEAF;
7e5c2dfb 91module_param(rcu_fanout_leaf, int, 0444);
f885b7f2
PM
92int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
93static int num_rcu_lvl[] = { /* Number of rcu_nodes at specified level. */
94 NUM_RCU_LVL_0,
95 NUM_RCU_LVL_1,
96 NUM_RCU_LVL_2,
97 NUM_RCU_LVL_3,
98 NUM_RCU_LVL_4,
99};
100int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
101
b0d30417
PM
102/*
103 * The rcu_scheduler_active variable transitions from zero to one just
104 * before the first task is spawned. So when this variable is zero, RCU
105 * can assume that there is but one task, allowing RCU to (for example)
106 * optimized synchronize_sched() to a simple barrier(). When this variable
107 * is one, RCU must actually do all the hard work required to detect real
108 * grace periods. This variable is also used to suppress boot-time false
109 * positives from lockdep-RCU error checking.
110 */
bbad9379
PM
111int rcu_scheduler_active __read_mostly;
112EXPORT_SYMBOL_GPL(rcu_scheduler_active);
113
b0d30417
PM
114/*
115 * The rcu_scheduler_fully_active variable transitions from zero to one
116 * during the early_initcall() processing, which is after the scheduler
117 * is capable of creating new tasks. So RCU processing (for example,
118 * creating tasks for RCU priority boosting) must be delayed until after
119 * rcu_scheduler_fully_active transitions from zero to one. We also
120 * currently delay invocation of any RCU callbacks until after this point.
121 *
122 * It might later prove better for people registering RCU callbacks during
123 * early boot to take responsibility for these callbacks, but one step at
124 * a time.
125 */
126static int rcu_scheduler_fully_active __read_mostly;
127
a46e0899
PM
128#ifdef CONFIG_RCU_BOOST
129
a26ac245
PM
130/*
131 * Control variables for per-CPU and per-rcu_node kthreads. These
132 * handle all flavors of RCU.
133 */
134static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
d71df90e 135DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
15ba0ba8 136DEFINE_PER_CPU(int, rcu_cpu_kthread_cpu);
5ece5bab 137DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
d71df90e 138DEFINE_PER_CPU(char, rcu_cpu_has_work);
a26ac245 139
a46e0899
PM
140#endif /* #ifdef CONFIG_RCU_BOOST */
141
0f962a5e 142static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
a46e0899
PM
143static void invoke_rcu_core(void);
144static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
a26ac245 145
4a298656
PM
146/*
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.
154 */
155unsigned long rcutorture_testseq;
156unsigned long rcutorture_vernum;
157
fc2219d4
PM
158/*
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.
162 */
163static int rcu_gp_in_progress(struct rcu_state *rsp)
164{
165 return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
166}
167
b1f77b05 168/*
d6714c22 169 * Note a quiescent state. Because we do not need to know
b1f77b05 170 * how many quiescent states passed, just if there was at least
d6714c22 171 * one since the start of the grace period, this just sets a flag.
e4cc1f22 172 * The caller must have disabled preemption.
b1f77b05 173 */
d6714c22 174void rcu_sched_qs(int cpu)
b1f77b05 175{
25502a6c 176 struct rcu_data *rdp = &per_cpu(rcu_sched_data, cpu);
f41d911f 177
e4cc1f22 178 rdp->passed_quiesce_gpnum = rdp->gpnum;
c3422bea 179 barrier();
e4cc1f22 180 if (rdp->passed_quiesce == 0)
d4c08f2a 181 trace_rcu_grace_period("rcu_sched", rdp->gpnum, "cpuqs");
e4cc1f22 182 rdp->passed_quiesce = 1;
b1f77b05
IM
183}
184
d6714c22 185void rcu_bh_qs(int cpu)
b1f77b05 186{
25502a6c 187 struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
f41d911f 188
e4cc1f22 189 rdp->passed_quiesce_gpnum = rdp->gpnum;
c3422bea 190 barrier();
e4cc1f22 191 if (rdp->passed_quiesce == 0)
d4c08f2a 192 trace_rcu_grace_period("rcu_bh", rdp->gpnum, "cpuqs");
e4cc1f22 193 rdp->passed_quiesce = 1;
b1f77b05 194}
64db4cff 195
25502a6c
PM
196/*
197 * Note a context switch. This is a quiescent state for RCU-sched,
198 * and requires special handling for preemptible RCU.
e4cc1f22 199 * The caller must have disabled preemption.
25502a6c
PM
200 */
201void rcu_note_context_switch(int cpu)
202{
300df91c 203 trace_rcu_utilization("Start context switch");
25502a6c 204 rcu_sched_qs(cpu);
cba6d0d6 205 rcu_preempt_note_context_switch(cpu);
300df91c 206 trace_rcu_utilization("End context switch");
25502a6c 207}
29ce8310 208EXPORT_SYMBOL_GPL(rcu_note_context_switch);
25502a6c 209
90a4d2c0 210DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
29e37d81 211 .dynticks_nesting = DYNTICK_TASK_EXIT_IDLE,
23b5c8fa 212 .dynticks = ATOMIC_INIT(1),
90a4d2c0 213};
64db4cff 214
e0f23060 215static int blimit = 10; /* Maximum callbacks per rcu_do_batch. */
64db4cff
PM
216static int qhimark = 10000; /* If this many pending, ignore blimit. */
217static int qlowmark = 100; /* Once only this many pending, use blimit. */
218
7e5c2dfb
PM
219module_param(blimit, int, 0444);
220module_param(qhimark, int, 0444);
221module_param(qlowmark, int, 0444);
3d76c082 222
13cfcca0
PM
223int rcu_cpu_stall_suppress __read_mostly; /* 1 = suppress stall warnings. */
224int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
225
f2e0dd70 226module_param(rcu_cpu_stall_suppress, int, 0644);
13cfcca0 227module_param(rcu_cpu_stall_timeout, int, 0644);
742734ee 228
d40011f6
PM
229static ulong jiffies_till_first_fqs = RCU_JIFFIES_TILL_FORCE_QS;
230static ulong jiffies_till_next_fqs = RCU_JIFFIES_TILL_FORCE_QS;
231
232module_param(jiffies_till_first_fqs, ulong, 0644);
233module_param(jiffies_till_next_fqs, ulong, 0644);
234
4cdfc175
PM
235static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *));
236static void force_quiescent_state(struct rcu_state *rsp);
a157229c 237static int rcu_pending(int cpu);
64db4cff
PM
238
239/*
d6714c22 240 * Return the number of RCU-sched batches processed thus far for debug & stats.
64db4cff 241 */
d6714c22 242long rcu_batches_completed_sched(void)
64db4cff 243{
d6714c22 244 return rcu_sched_state.completed;
64db4cff 245}
d6714c22 246EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
64db4cff
PM
247
248/*
249 * Return the number of RCU BH batches processed thus far for debug & stats.
250 */
251long rcu_batches_completed_bh(void)
252{
253 return rcu_bh_state.completed;
254}
255EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
256
bf66f18e
PM
257/*
258 * Force a quiescent state for RCU BH.
259 */
260void rcu_bh_force_quiescent_state(void)
261{
4cdfc175 262 force_quiescent_state(&rcu_bh_state);
bf66f18e
PM
263}
264EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
265
4a298656
PM
266/*
267 * Record the number of times rcutorture tests have been initiated and
268 * terminated. This information allows the debugfs tracing stats to be
269 * correlated to the rcutorture messages, even when the rcutorture module
270 * is being repeatedly loaded and unloaded. In other words, we cannot
271 * store this state in rcutorture itself.
272 */
273void rcutorture_record_test_transition(void)
274{
275 rcutorture_testseq++;
276 rcutorture_vernum = 0;
277}
278EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
279
280/*
281 * Record the number of writer passes through the current rcutorture test.
282 * This is also used to correlate debugfs tracing stats with the rcutorture
283 * messages.
284 */
285void rcutorture_record_progress(unsigned long vernum)
286{
287 rcutorture_vernum++;
288}
289EXPORT_SYMBOL_GPL(rcutorture_record_progress);
290
bf66f18e
PM
291/*
292 * Force a quiescent state for RCU-sched.
293 */
294void rcu_sched_force_quiescent_state(void)
295{
4cdfc175 296 force_quiescent_state(&rcu_sched_state);
bf66f18e
PM
297}
298EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
299
64db4cff
PM
300/*
301 * Does the CPU have callbacks ready to be invoked?
302 */
303static int
304cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
305{
306 return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
307}
308
309/*
310 * Does the current CPU require a yet-as-unscheduled grace period?
311 */
312static int
313cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
314{
a10d206e
PM
315 return *rdp->nxttail[RCU_DONE_TAIL +
316 ACCESS_ONCE(rsp->completed) != rdp->completed] &&
317 !rcu_gp_in_progress(rsp);
64db4cff
PM
318}
319
320/*
321 * Return the root node of the specified rcu_state structure.
322 */
323static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
324{
325 return &rsp->node[0];
326}
327
64db4cff
PM
328/*
329 * If the specified CPU is offline, tell the caller that it is in
330 * a quiescent state. Otherwise, whack it with a reschedule IPI.
331 * Grace periods can end up waiting on an offline CPU when that
332 * CPU is in the process of coming online -- it will be added to the
333 * rcu_node bitmasks before it actually makes it online. The same thing
334 * can happen while a CPU is in the process of coming online. Because this
335 * race is quite rare, we check for it after detecting that the grace
336 * period has been delayed rather than checking each and every CPU
337 * each and every time we start a new grace period.
338 */
339static int rcu_implicit_offline_qs(struct rcu_data *rdp)
340{
341 /*
2036d94a
PM
342 * If the CPU is offline for more than a jiffy, it is in a quiescent
343 * state. We can trust its state not to change because interrupts
344 * are disabled. The reason for the jiffy's worth of slack is to
345 * handle CPUs initializing on the way up and finding their way
346 * to the idle loop on the way down.
64db4cff 347 */
2036d94a
PM
348 if (cpu_is_offline(rdp->cpu) &&
349 ULONG_CMP_LT(rdp->rsp->gp_start + 2, jiffies)) {
d4c08f2a 350 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, "ofl");
64db4cff
PM
351 rdp->offline_fqs++;
352 return 1;
353 }
64db4cff
PM
354 return 0;
355}
356
9b2e4f18
PM
357/*
358 * rcu_idle_enter_common - inform RCU that current CPU is moving towards idle
359 *
360 * If the new value of the ->dynticks_nesting counter now is zero,
361 * we really have entered idle, and must do the appropriate accounting.
362 * The caller must have disabled interrupts.
363 */
4145fa7f 364static void rcu_idle_enter_common(struct rcu_dynticks *rdtp, long long oldval)
9b2e4f18 365{
facc4e15 366 trace_rcu_dyntick("Start", oldval, 0);
99745b6a 367 if (!is_idle_task(current)) {
0989cb46
PM
368 struct task_struct *idle = idle_task(smp_processor_id());
369
facc4e15 370 trace_rcu_dyntick("Error on entry: not idle task", oldval, 0);
bf1304e9 371 ftrace_dump(DUMP_ORIG);
0989cb46
PM
372 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
373 current->pid, current->comm,
374 idle->pid, idle->comm); /* must be idle task! */
9b2e4f18 375 }
aea1b35e 376 rcu_prepare_for_idle(smp_processor_id());
9b2e4f18
PM
377 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
378 smp_mb__before_atomic_inc(); /* See above. */
379 atomic_inc(&rdtp->dynticks);
380 smp_mb__after_atomic_inc(); /* Force ordering with next sojourn. */
381 WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
c44e2cdd
PM
382
383 /*
384 * The idle task is not permitted to enter the idle loop while
385 * in an RCU read-side critical section.
386 */
387 rcu_lockdep_assert(!lock_is_held(&rcu_lock_map),
388 "Illegal idle entry in RCU read-side critical section.");
389 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map),
390 "Illegal idle entry in RCU-bh read-side critical section.");
391 rcu_lockdep_assert(!lock_is_held(&rcu_sched_lock_map),
392 "Illegal idle entry in RCU-sched read-side critical section.");
9b2e4f18 393}
64db4cff
PM
394
395/**
9b2e4f18 396 * rcu_idle_enter - inform RCU that current CPU is entering idle
64db4cff 397 *
9b2e4f18 398 * Enter idle mode, in other words, -leave- the mode in which RCU
64db4cff 399 * read-side critical sections can occur. (Though RCU read-side
9b2e4f18
PM
400 * critical sections can occur in irq handlers in idle, a possibility
401 * handled by irq_enter() and irq_exit().)
402 *
403 * We crowbar the ->dynticks_nesting field to zero to allow for
404 * the possibility of usermode upcalls having messed up our count
405 * of interrupt nesting level during the prior busy period.
64db4cff 406 */
9b2e4f18 407void rcu_idle_enter(void)
64db4cff
PM
408{
409 unsigned long flags;
4145fa7f 410 long long oldval;
64db4cff
PM
411 struct rcu_dynticks *rdtp;
412
64db4cff
PM
413 local_irq_save(flags);
414 rdtp = &__get_cpu_var(rcu_dynticks);
4145fa7f 415 oldval = rdtp->dynticks_nesting;
29e37d81
PM
416 WARN_ON_ONCE((oldval & DYNTICK_TASK_NEST_MASK) == 0);
417 if ((oldval & DYNTICK_TASK_NEST_MASK) == DYNTICK_TASK_NEST_VALUE)
418 rdtp->dynticks_nesting = 0;
419 else
420 rdtp->dynticks_nesting -= DYNTICK_TASK_NEST_VALUE;
4145fa7f 421 rcu_idle_enter_common(rdtp, oldval);
64db4cff
PM
422 local_irq_restore(flags);
423}
8a2ecf47 424EXPORT_SYMBOL_GPL(rcu_idle_enter);
64db4cff 425
9b2e4f18
PM
426/**
427 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
428 *
429 * Exit from an interrupt handler, which might possibly result in entering
430 * idle mode, in other words, leaving the mode in which read-side critical
431 * sections can occur.
64db4cff 432 *
9b2e4f18
PM
433 * This code assumes that the idle loop never does anything that might
434 * result in unbalanced calls to irq_enter() and irq_exit(). If your
435 * architecture violates this assumption, RCU will give you what you
436 * deserve, good and hard. But very infrequently and irreproducibly.
437 *
438 * Use things like work queues to work around this limitation.
439 *
440 * You have been warned.
64db4cff 441 */
9b2e4f18 442void rcu_irq_exit(void)
64db4cff
PM
443{
444 unsigned long flags;
4145fa7f 445 long long oldval;
64db4cff
PM
446 struct rcu_dynticks *rdtp;
447
448 local_irq_save(flags);
449 rdtp = &__get_cpu_var(rcu_dynticks);
4145fa7f 450 oldval = rdtp->dynticks_nesting;
9b2e4f18
PM
451 rdtp->dynticks_nesting--;
452 WARN_ON_ONCE(rdtp->dynticks_nesting < 0);
b6fc6020
FW
453 if (rdtp->dynticks_nesting)
454 trace_rcu_dyntick("--=", oldval, rdtp->dynticks_nesting);
455 else
456 rcu_idle_enter_common(rdtp, oldval);
9b2e4f18
PM
457 local_irq_restore(flags);
458}
459
460/*
461 * rcu_idle_exit_common - inform RCU that current CPU is moving away from idle
462 *
463 * If the new value of the ->dynticks_nesting counter was previously zero,
464 * we really have exited idle, and must do the appropriate accounting.
465 * The caller must have disabled interrupts.
466 */
467static void rcu_idle_exit_common(struct rcu_dynticks *rdtp, long long oldval)
468{
23b5c8fa
PM
469 smp_mb__before_atomic_inc(); /* Force ordering w/previous sojourn. */
470 atomic_inc(&rdtp->dynticks);
471 /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
472 smp_mb__after_atomic_inc(); /* See above. */
473 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
7cb92499 474 rcu_cleanup_after_idle(smp_processor_id());
4145fa7f 475 trace_rcu_dyntick("End", oldval, rdtp->dynticks_nesting);
99745b6a 476 if (!is_idle_task(current)) {
0989cb46
PM
477 struct task_struct *idle = idle_task(smp_processor_id());
478
4145fa7f
PM
479 trace_rcu_dyntick("Error on exit: not idle task",
480 oldval, rdtp->dynticks_nesting);
bf1304e9 481 ftrace_dump(DUMP_ORIG);
0989cb46
PM
482 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
483 current->pid, current->comm,
484 idle->pid, idle->comm); /* must be idle task! */
9b2e4f18
PM
485 }
486}
487
488/**
489 * rcu_idle_exit - inform RCU that current CPU is leaving idle
490 *
491 * Exit idle mode, in other words, -enter- the mode in which RCU
492 * read-side critical sections can occur.
493 *
29e37d81 494 * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to
4145fa7f 495 * allow for the possibility of usermode upcalls messing up our count
9b2e4f18
PM
496 * of interrupt nesting level during the busy period that is just
497 * now starting.
498 */
499void rcu_idle_exit(void)
500{
501 unsigned long flags;
502 struct rcu_dynticks *rdtp;
503 long long oldval;
504
505 local_irq_save(flags);
506 rdtp = &__get_cpu_var(rcu_dynticks);
507 oldval = rdtp->dynticks_nesting;
29e37d81
PM
508 WARN_ON_ONCE(oldval < 0);
509 if (oldval & DYNTICK_TASK_NEST_MASK)
510 rdtp->dynticks_nesting += DYNTICK_TASK_NEST_VALUE;
511 else
512 rdtp->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
9b2e4f18
PM
513 rcu_idle_exit_common(rdtp, oldval);
514 local_irq_restore(flags);
515}
8a2ecf47 516EXPORT_SYMBOL_GPL(rcu_idle_exit);
9b2e4f18
PM
517
518/**
519 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
520 *
521 * Enter an interrupt handler, which might possibly result in exiting
522 * idle mode, in other words, entering the mode in which read-side critical
523 * sections can occur.
524 *
525 * Note that the Linux kernel is fully capable of entering an interrupt
526 * handler that it never exits, for example when doing upcalls to
527 * user mode! This code assumes that the idle loop never does upcalls to
528 * user mode. If your architecture does do upcalls from the idle loop (or
529 * does anything else that results in unbalanced calls to the irq_enter()
530 * and irq_exit() functions), RCU will give you what you deserve, good
531 * and hard. But very infrequently and irreproducibly.
532 *
533 * Use things like work queues to work around this limitation.
534 *
535 * You have been warned.
536 */
537void rcu_irq_enter(void)
538{
539 unsigned long flags;
540 struct rcu_dynticks *rdtp;
541 long long oldval;
542
543 local_irq_save(flags);
544 rdtp = &__get_cpu_var(rcu_dynticks);
545 oldval = rdtp->dynticks_nesting;
546 rdtp->dynticks_nesting++;
547 WARN_ON_ONCE(rdtp->dynticks_nesting == 0);
b6fc6020
FW
548 if (oldval)
549 trace_rcu_dyntick("++=", oldval, rdtp->dynticks_nesting);
550 else
551 rcu_idle_exit_common(rdtp, oldval);
64db4cff 552 local_irq_restore(flags);
64db4cff
PM
553}
554
555/**
556 * rcu_nmi_enter - inform RCU of entry to NMI context
557 *
558 * If the CPU was idle with dynamic ticks active, and there is no
559 * irq handler running, this updates rdtp->dynticks_nmi to let the
560 * RCU grace-period handling know that the CPU is active.
561 */
562void rcu_nmi_enter(void)
563{
564 struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
565
23b5c8fa
PM
566 if (rdtp->dynticks_nmi_nesting == 0 &&
567 (atomic_read(&rdtp->dynticks) & 0x1))
64db4cff 568 return;
23b5c8fa
PM
569 rdtp->dynticks_nmi_nesting++;
570 smp_mb__before_atomic_inc(); /* Force delay from prior write. */
571 atomic_inc(&rdtp->dynticks);
572 /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
573 smp_mb__after_atomic_inc(); /* See above. */
574 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
64db4cff
PM
575}
576
577/**
578 * rcu_nmi_exit - inform RCU of exit from NMI context
579 *
580 * If the CPU was idle with dynamic ticks active, and there is no
581 * irq handler running, this updates rdtp->dynticks_nmi to let the
582 * RCU grace-period handling know that the CPU is no longer active.
583 */
584void rcu_nmi_exit(void)
585{
586 struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
587
23b5c8fa
PM
588 if (rdtp->dynticks_nmi_nesting == 0 ||
589 --rdtp->dynticks_nmi_nesting != 0)
64db4cff 590 return;
23b5c8fa
PM
591 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
592 smp_mb__before_atomic_inc(); /* See above. */
593 atomic_inc(&rdtp->dynticks);
594 smp_mb__after_atomic_inc(); /* Force delay to next write. */
595 WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
64db4cff
PM
596}
597
598/**
9b2e4f18 599 * rcu_is_cpu_idle - see if RCU thinks that the current CPU is idle
64db4cff 600 *
9b2e4f18 601 * If the current CPU is in its idle loop and is neither in an interrupt
34240697 602 * or NMI handler, return true.
64db4cff 603 */
9b2e4f18 604int rcu_is_cpu_idle(void)
64db4cff 605{
34240697
PM
606 int ret;
607
608 preempt_disable();
609 ret = (atomic_read(&__get_cpu_var(rcu_dynticks).dynticks) & 0x1) == 0;
610 preempt_enable();
611 return ret;
64db4cff 612}
e6b80a3b 613EXPORT_SYMBOL(rcu_is_cpu_idle);
64db4cff 614
62fde6ed 615#if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
c0d6d01b
PM
616
617/*
618 * Is the current CPU online? Disable preemption to avoid false positives
619 * that could otherwise happen due to the current CPU number being sampled,
620 * this task being preempted, its old CPU being taken offline, resuming
621 * on some other CPU, then determining that its old CPU is now offline.
622 * It is OK to use RCU on an offline processor during initial boot, hence
2036d94a
PM
623 * the check for rcu_scheduler_fully_active. Note also that it is OK
624 * for a CPU coming online to use RCU for one jiffy prior to marking itself
625 * online in the cpu_online_mask. Similarly, it is OK for a CPU going
626 * offline to continue to use RCU for one jiffy after marking itself
627 * offline in the cpu_online_mask. This leniency is necessary given the
628 * non-atomic nature of the online and offline processing, for example,
629 * the fact that a CPU enters the scheduler after completing the CPU_DYING
630 * notifiers.
631 *
632 * This is also why RCU internally marks CPUs online during the
633 * CPU_UP_PREPARE phase and offline during the CPU_DEAD phase.
c0d6d01b
PM
634 *
635 * Disable checking if in an NMI handler because we cannot safely report
636 * errors from NMI handlers anyway.
637 */
638bool rcu_lockdep_current_cpu_online(void)
639{
2036d94a
PM
640 struct rcu_data *rdp;
641 struct rcu_node *rnp;
c0d6d01b
PM
642 bool ret;
643
644 if (in_nmi())
645 return 1;
646 preempt_disable();
2036d94a
PM
647 rdp = &__get_cpu_var(rcu_sched_data);
648 rnp = rdp->mynode;
649 ret = (rdp->grpmask & rnp->qsmaskinit) ||
c0d6d01b
PM
650 !rcu_scheduler_fully_active;
651 preempt_enable();
652 return ret;
653}
654EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
655
62fde6ed 656#endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
9b2e4f18 657
64db4cff 658/**
9b2e4f18 659 * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
64db4cff 660 *
9b2e4f18
PM
661 * If the current CPU is idle or running at a first-level (not nested)
662 * interrupt from idle, return true. The caller must have at least
663 * disabled preemption.
64db4cff 664 */
9b2e4f18 665int rcu_is_cpu_rrupt_from_idle(void)
64db4cff 666{
9b2e4f18 667 return __get_cpu_var(rcu_dynticks).dynticks_nesting <= 1;
64db4cff
PM
668}
669
64db4cff
PM
670/*
671 * Snapshot the specified CPU's dynticks counter so that we can later
672 * credit them with an implicit quiescent state. Return 1 if this CPU
1eba8f84 673 * is in dynticks idle mode, which is an extended quiescent state.
64db4cff
PM
674 */
675static int dyntick_save_progress_counter(struct rcu_data *rdp)
676{
23b5c8fa 677 rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
f0e7c19d 678 return (rdp->dynticks_snap & 0x1) == 0;
64db4cff
PM
679}
680
681/*
682 * Return true if the specified CPU has passed through a quiescent
683 * state by virtue of being in or having passed through an dynticks
684 * idle state since the last call to dyntick_save_progress_counter()
685 * for this same CPU.
686 */
687static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
688{
7eb4f455
PM
689 unsigned int curr;
690 unsigned int snap;
64db4cff 691
7eb4f455
PM
692 curr = (unsigned int)atomic_add_return(0, &rdp->dynticks->dynticks);
693 snap = (unsigned int)rdp->dynticks_snap;
64db4cff
PM
694
695 /*
696 * If the CPU passed through or entered a dynticks idle phase with
697 * no active irq/NMI handlers, then we can safely pretend that the CPU
698 * already acknowledged the request to pass through a quiescent
699 * state. Either way, that CPU cannot possibly be in an RCU
700 * read-side critical section that started before the beginning
701 * of the current RCU grace period.
702 */
7eb4f455 703 if ((curr & 0x1) == 0 || UINT_CMP_GE(curr, snap + 2)) {
d4c08f2a 704 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, "dti");
64db4cff
PM
705 rdp->dynticks_fqs++;
706 return 1;
707 }
708
709 /* Go check for the CPU being offline. */
710 return rcu_implicit_offline_qs(rdp);
711}
712
13cfcca0
PM
713static int jiffies_till_stall_check(void)
714{
715 int till_stall_check = ACCESS_ONCE(rcu_cpu_stall_timeout);
716
717 /*
718 * Limit check must be consistent with the Kconfig limits
719 * for CONFIG_RCU_CPU_STALL_TIMEOUT.
720 */
721 if (till_stall_check < 3) {
722 ACCESS_ONCE(rcu_cpu_stall_timeout) = 3;
723 till_stall_check = 3;
724 } else if (till_stall_check > 300) {
725 ACCESS_ONCE(rcu_cpu_stall_timeout) = 300;
726 till_stall_check = 300;
727 }
728 return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
729}
730
64db4cff
PM
731static void record_gp_stall_check_time(struct rcu_state *rsp)
732{
733 rsp->gp_start = jiffies;
13cfcca0 734 rsp->jiffies_stall = jiffies + jiffies_till_stall_check();
64db4cff
PM
735}
736
737static void print_other_cpu_stall(struct rcu_state *rsp)
738{
739 int cpu;
740 long delta;
741 unsigned long flags;
285fe294 742 int ndetected = 0;
64db4cff 743 struct rcu_node *rnp = rcu_get_root(rsp);
64db4cff
PM
744
745 /* Only let one CPU complain about others per time interval. */
746
1304afb2 747 raw_spin_lock_irqsave(&rnp->lock, flags);
64db4cff 748 delta = jiffies - rsp->jiffies_stall;
fc2219d4 749 if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
1304afb2 750 raw_spin_unlock_irqrestore(&rnp->lock, flags);
64db4cff
PM
751 return;
752 }
13cfcca0 753 rsp->jiffies_stall = jiffies + 3 * jiffies_till_stall_check() + 3;
1304afb2 754 raw_spin_unlock_irqrestore(&rnp->lock, flags);
64db4cff 755
8cdd32a9
PM
756 /*
757 * OK, time to rat on our buddy...
758 * See Documentation/RCU/stallwarn.txt for info on how to debug
759 * RCU CPU stall warnings.
760 */
a858af28 761 printk(KERN_ERR "INFO: %s detected stalls on CPUs/tasks:",
4300aa64 762 rsp->name);
a858af28 763 print_cpu_stall_info_begin();
a0b6c9a7 764 rcu_for_each_leaf_node(rsp, rnp) {
3acd9eb3 765 raw_spin_lock_irqsave(&rnp->lock, flags);
9bc8b558 766 ndetected += rcu_print_task_stall(rnp);
3acd9eb3 767 raw_spin_unlock_irqrestore(&rnp->lock, flags);
a0b6c9a7 768 if (rnp->qsmask == 0)
64db4cff 769 continue;
a0b6c9a7 770 for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
9bc8b558 771 if (rnp->qsmask & (1UL << cpu)) {
a858af28 772 print_cpu_stall_info(rsp, rnp->grplo + cpu);
9bc8b558
PM
773 ndetected++;
774 }
64db4cff 775 }
a858af28
PM
776
777 /*
778 * Now rat on any tasks that got kicked up to the root rcu_node
779 * due to CPU offlining.
780 */
781 rnp = rcu_get_root(rsp);
782 raw_spin_lock_irqsave(&rnp->lock, flags);
285fe294 783 ndetected += rcu_print_task_stall(rnp);
a858af28
PM
784 raw_spin_unlock_irqrestore(&rnp->lock, flags);
785
786 print_cpu_stall_info_end();
787 printk(KERN_CONT "(detected by %d, t=%ld jiffies)\n",
64db4cff 788 smp_processor_id(), (long)(jiffies - rsp->gp_start));
9bc8b558
PM
789 if (ndetected == 0)
790 printk(KERN_ERR "INFO: Stall ended before state dump start\n");
791 else if (!trigger_all_cpu_backtrace())
4627e240 792 dump_stack();
c1dc0b9c 793
4cdfc175 794 /* Complain about tasks blocking the grace period. */
1ed509a2
PM
795
796 rcu_print_detail_task_stall(rsp);
797
4cdfc175 798 force_quiescent_state(rsp); /* Kick them all. */
64db4cff
PM
799}
800
801static void print_cpu_stall(struct rcu_state *rsp)
802{
803 unsigned long flags;
804 struct rcu_node *rnp = rcu_get_root(rsp);
805
8cdd32a9
PM
806 /*
807 * OK, time to rat on ourselves...
808 * See Documentation/RCU/stallwarn.txt for info on how to debug
809 * RCU CPU stall warnings.
810 */
a858af28
PM
811 printk(KERN_ERR "INFO: %s self-detected stall on CPU", rsp->name);
812 print_cpu_stall_info_begin();
813 print_cpu_stall_info(rsp, smp_processor_id());
814 print_cpu_stall_info_end();
815 printk(KERN_CONT " (t=%lu jiffies)\n", jiffies - rsp->gp_start);
4627e240
PM
816 if (!trigger_all_cpu_backtrace())
817 dump_stack();
c1dc0b9c 818
1304afb2 819 raw_spin_lock_irqsave(&rnp->lock, flags);
20133cfc 820 if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall))
13cfcca0
PM
821 rsp->jiffies_stall = jiffies +
822 3 * jiffies_till_stall_check() + 3;
1304afb2 823 raw_spin_unlock_irqrestore(&rnp->lock, flags);
c1dc0b9c 824
64db4cff
PM
825 set_need_resched(); /* kick ourselves to get things going. */
826}
827
828static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
829{
bad6e139
PM
830 unsigned long j;
831 unsigned long js;
64db4cff
PM
832 struct rcu_node *rnp;
833
742734ee 834 if (rcu_cpu_stall_suppress)
c68de209 835 return;
bad6e139
PM
836 j = ACCESS_ONCE(jiffies);
837 js = ACCESS_ONCE(rsp->jiffies_stall);
64db4cff 838 rnp = rdp->mynode;
bad6e139 839 if ((ACCESS_ONCE(rnp->qsmask) & rdp->grpmask) && ULONG_CMP_GE(j, js)) {
64db4cff
PM
840
841 /* We haven't checked in, so go dump stack. */
842 print_cpu_stall(rsp);
843
bad6e139
PM
844 } else if (rcu_gp_in_progress(rsp) &&
845 ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
64db4cff 846
bad6e139 847 /* They had a few time units to dump stack, so complain. */
64db4cff
PM
848 print_other_cpu_stall(rsp);
849 }
850}
851
c68de209
PM
852static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
853{
742734ee 854 rcu_cpu_stall_suppress = 1;
c68de209
PM
855 return NOTIFY_DONE;
856}
857
53d84e00
PM
858/**
859 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
860 *
861 * Set the stall-warning timeout way off into the future, thus preventing
862 * any RCU CPU stall-warning messages from appearing in the current set of
863 * RCU grace periods.
864 *
865 * The caller must disable hard irqs.
866 */
867void rcu_cpu_stall_reset(void)
868{
6ce75a23
PM
869 struct rcu_state *rsp;
870
871 for_each_rcu_flavor(rsp)
872 rsp->jiffies_stall = jiffies + ULONG_MAX / 2;
53d84e00
PM
873}
874
c68de209
PM
875static struct notifier_block rcu_panic_block = {
876 .notifier_call = rcu_panic,
877};
878
879static void __init check_cpu_stall_init(void)
880{
881 atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
882}
883
64db4cff
PM
884/*
885 * Update CPU-local rcu_data state to record the newly noticed grace period.
886 * This is used both when we started the grace period and when we notice
9160306e
PM
887 * that someone else started the grace period. The caller must hold the
888 * ->lock of the leaf rcu_node structure corresponding to the current CPU,
889 * and must have irqs disabled.
64db4cff 890 */
9160306e
PM
891static void __note_new_gpnum(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
892{
893 if (rdp->gpnum != rnp->gpnum) {
121dfc4b
PM
894 /*
895 * If the current grace period is waiting for this CPU,
896 * set up to detect a quiescent state, otherwise don't
897 * go looking for one.
898 */
9160306e 899 rdp->gpnum = rnp->gpnum;
d4c08f2a 900 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpustart");
121dfc4b
PM
901 if (rnp->qsmask & rdp->grpmask) {
902 rdp->qs_pending = 1;
e4cc1f22 903 rdp->passed_quiesce = 0;
c701d5d9 904 } else {
121dfc4b 905 rdp->qs_pending = 0;
c701d5d9 906 }
a858af28 907 zero_cpu_stall_ticks(rdp);
9160306e
PM
908 }
909}
910
64db4cff
PM
911static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
912{
9160306e
PM
913 unsigned long flags;
914 struct rcu_node *rnp;
915
916 local_irq_save(flags);
917 rnp = rdp->mynode;
918 if (rdp->gpnum == ACCESS_ONCE(rnp->gpnum) || /* outside lock. */
1304afb2 919 !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
9160306e
PM
920 local_irq_restore(flags);
921 return;
922 }
923 __note_new_gpnum(rsp, rnp, rdp);
1304afb2 924 raw_spin_unlock_irqrestore(&rnp->lock, flags);
64db4cff
PM
925}
926
927/*
928 * Did someone else start a new RCU grace period start since we last
929 * checked? Update local state appropriately if so. Must be called
930 * on the CPU corresponding to rdp.
931 */
932static int
933check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
934{
935 unsigned long flags;
936 int ret = 0;
937
938 local_irq_save(flags);
939 if (rdp->gpnum != rsp->gpnum) {
940 note_new_gpnum(rsp, rdp);
941 ret = 1;
942 }
943 local_irq_restore(flags);
944 return ret;
945}
946
3f5d3ea6
PM
947/*
948 * Initialize the specified rcu_data structure's callback list to empty.
949 */
950static void init_callback_list(struct rcu_data *rdp)
951{
952 int i;
953
954 rdp->nxtlist = NULL;
955 for (i = 0; i < RCU_NEXT_SIZE; i++)
956 rdp->nxttail[i] = &rdp->nxtlist;
957}
958
d09b62df
PM
959/*
960 * Advance this CPU's callbacks, but only if the current grace period
961 * has ended. This may be called only from the CPU to whom the rdp
962 * belongs. In addition, the corresponding leaf rcu_node structure's
963 * ->lock must be held by the caller, with irqs disabled.
964 */
965static void
966__rcu_process_gp_end(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
967{
968 /* Did another grace period end? */
969 if (rdp->completed != rnp->completed) {
970
971 /* Advance callbacks. No harm if list empty. */
972 rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
973 rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
974 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
975
976 /* Remember that we saw this grace-period completion. */
977 rdp->completed = rnp->completed;
d4c08f2a 978 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpuend");
20377f32 979
5ff8e6f0
FW
980 /*
981 * If we were in an extended quiescent state, we may have
121dfc4b 982 * missed some grace periods that others CPUs handled on
5ff8e6f0 983 * our behalf. Catch up with this state to avoid noting
121dfc4b
PM
984 * spurious new grace periods. If another grace period
985 * has started, then rnp->gpnum will have advanced, so
986 * we will detect this later on.
5ff8e6f0 987 */
121dfc4b 988 if (ULONG_CMP_LT(rdp->gpnum, rdp->completed))
5ff8e6f0
FW
989 rdp->gpnum = rdp->completed;
990
20377f32 991 /*
121dfc4b
PM
992 * If RCU does not need a quiescent state from this CPU,
993 * then make sure that this CPU doesn't go looking for one.
20377f32 994 */
121dfc4b 995 if ((rnp->qsmask & rdp->grpmask) == 0)
20377f32 996 rdp->qs_pending = 0;
d09b62df
PM
997 }
998}
999
1000/*
1001 * Advance this CPU's callbacks, but only if the current grace period
1002 * has ended. This may be called only from the CPU to whom the rdp
1003 * belongs.
1004 */
1005static void
1006rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
1007{
1008 unsigned long flags;
1009 struct rcu_node *rnp;
1010
1011 local_irq_save(flags);
1012 rnp = rdp->mynode;
1013 if (rdp->completed == ACCESS_ONCE(rnp->completed) || /* outside lock. */
1304afb2 1014 !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
d09b62df
PM
1015 local_irq_restore(flags);
1016 return;
1017 }
1018 __rcu_process_gp_end(rsp, rnp, rdp);
1304afb2 1019 raw_spin_unlock_irqrestore(&rnp->lock, flags);
d09b62df
PM
1020}
1021
1022/*
1023 * Do per-CPU grace-period initialization for running CPU. The caller
1024 * must hold the lock of the leaf rcu_node structure corresponding to
1025 * this CPU.
1026 */
1027static void
1028rcu_start_gp_per_cpu(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
1029{
1030 /* Prior grace period ended, so advance callbacks for current CPU. */
1031 __rcu_process_gp_end(rsp, rnp, rdp);
1032
9160306e
PM
1033 /* Set state so that this CPU will detect the next quiescent state. */
1034 __note_new_gpnum(rsp, rnp, rdp);
d09b62df
PM
1035}
1036
b3dbec76 1037/*
7fdefc10 1038 * Initialize a new grace period.
b3dbec76 1039 */
7fdefc10 1040static int rcu_gp_init(struct rcu_state *rsp)
b3dbec76
PM
1041{
1042 struct rcu_data *rdp;
7fdefc10 1043 struct rcu_node *rnp = rcu_get_root(rsp);
b3dbec76 1044
7fdefc10 1045 raw_spin_lock_irq(&rnp->lock);
4cdfc175 1046 rsp->gp_flags = 0; /* Clear all flags: New grace period. */
b3dbec76 1047
7fdefc10
PM
1048 if (rcu_gp_in_progress(rsp)) {
1049 /* Grace period already in progress, don't start another. */
1050 raw_spin_unlock_irq(&rnp->lock);
1051 return 0;
1052 }
1053
7fdefc10
PM
1054 /* Advance to a new grace period and initialize state. */
1055 rsp->gpnum++;
1056 trace_rcu_grace_period(rsp->name, rsp->gpnum, "start");
7fdefc10
PM
1057 record_gp_stall_check_time(rsp);
1058 raw_spin_unlock_irq(&rnp->lock);
1059
1060 /* Exclude any concurrent CPU-hotplug operations. */
1061 get_online_cpus();
1062
1063 /*
1064 * Set the quiescent-state-needed bits in all the rcu_node
1065 * structures for all currently online CPUs in breadth-first order,
1066 * starting from the root rcu_node structure, relying on the layout
1067 * of the tree within the rsp->node[] array. Note that other CPUs
1068 * will access only the leaves of the hierarchy, thus seeing that no
1069 * grace period is in progress, at least until the corresponding
1070 * leaf node has been initialized. In addition, we have excluded
1071 * CPU-hotplug operations.
1072 *
1073 * The grace period cannot complete until the initialization
1074 * process finishes, because this kthread handles both.
1075 */
1076 rcu_for_each_node_breadth_first(rsp, rnp) {
b3dbec76 1077 raw_spin_lock_irq(&rnp->lock);
b3dbec76 1078 rdp = this_cpu_ptr(rsp->rda);
7fdefc10
PM
1079 rcu_preempt_check_blocked_tasks(rnp);
1080 rnp->qsmask = rnp->qsmaskinit;
1081 rnp->gpnum = rsp->gpnum;
1082 rnp->completed = rsp->completed;
1083 if (rnp == rdp->mynode)
1084 rcu_start_gp_per_cpu(rsp, rnp, rdp);
1085 rcu_preempt_boost_start_gp(rnp);
1086 trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
1087 rnp->level, rnp->grplo,
1088 rnp->grphi, rnp->qsmask);
1089 raw_spin_unlock_irq(&rnp->lock);
1090 cond_resched();
1091 }
b3dbec76 1092
7fdefc10
PM
1093 put_online_cpus();
1094 return 1;
1095}
b3dbec76 1096
4cdfc175
PM
1097/*
1098 * Do one round of quiescent-state forcing.
1099 */
1100int rcu_gp_fqs(struct rcu_state *rsp, int fqs_state_in)
1101{
1102 int fqs_state = fqs_state_in;
1103 struct rcu_node *rnp = rcu_get_root(rsp);
1104
1105 rsp->n_force_qs++;
1106 if (fqs_state == RCU_SAVE_DYNTICK) {
1107 /* Collect dyntick-idle snapshots. */
1108 force_qs_rnp(rsp, dyntick_save_progress_counter);
1109 fqs_state = RCU_FORCE_QS;
1110 } else {
1111 /* Handle dyntick-idle and offline CPUs. */
1112 force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
1113 }
1114 /* Clear flag to prevent immediate re-entry. */
1115 if (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
1116 raw_spin_lock_irq(&rnp->lock);
1117 rsp->gp_flags &= ~RCU_GP_FLAG_FQS;
1118 raw_spin_unlock_irq(&rnp->lock);
1119 }
1120 return fqs_state;
1121}
1122
7fdefc10
PM
1123/*
1124 * Clean up after the old grace period.
1125 */
4cdfc175 1126static void rcu_gp_cleanup(struct rcu_state *rsp)
7fdefc10
PM
1127{
1128 unsigned long gp_duration;
1129 struct rcu_data *rdp;
1130 struct rcu_node *rnp = rcu_get_root(rsp);
b3dbec76 1131
7fdefc10
PM
1132 raw_spin_lock_irq(&rnp->lock);
1133 gp_duration = jiffies - rsp->gp_start;
1134 if (gp_duration > rsp->gp_max)
1135 rsp->gp_max = gp_duration;
b3dbec76 1136
7fdefc10
PM
1137 /*
1138 * We know the grace period is complete, but to everyone else
1139 * it appears to still be ongoing. But it is also the case
1140 * that to everyone else it looks like there is nothing that
1141 * they can do to advance the grace period. It is therefore
1142 * safe for us to drop the lock in order to mark the grace
1143 * period as completed in all of the rcu_node structures.
7fdefc10 1144 */
5d4b8659 1145 raw_spin_unlock_irq(&rnp->lock);
b3dbec76 1146
5d4b8659
PM
1147 /*
1148 * Propagate new ->completed value to rcu_node structures so
1149 * that other CPUs don't have to wait until the start of the next
1150 * grace period to process their callbacks. This also avoids
1151 * some nasty RCU grace-period initialization races by forcing
1152 * the end of the current grace period to be completely recorded in
1153 * all of the rcu_node structures before the beginning of the next
1154 * grace period is recorded in any of the rcu_node structures.
1155 */
1156 rcu_for_each_node_breadth_first(rsp, rnp) {
755609a9 1157 raw_spin_lock_irq(&rnp->lock);
5d4b8659
PM
1158 rnp->completed = rsp->gpnum;
1159 raw_spin_unlock_irq(&rnp->lock);
1160 cond_resched();
7fdefc10 1161 }
5d4b8659
PM
1162 rnp = rcu_get_root(rsp);
1163 raw_spin_lock_irq(&rnp->lock);
7fdefc10
PM
1164
1165 rsp->completed = rsp->gpnum; /* Declare grace period done. */
1166 trace_rcu_grace_period(rsp->name, rsp->completed, "end");
1167 rsp->fqs_state = RCU_GP_IDLE;
5d4b8659 1168 rdp = this_cpu_ptr(rsp->rda);
7fdefc10
PM
1169 if (cpu_needs_another_gp(rsp, rdp))
1170 rsp->gp_flags = 1;
1171 raw_spin_unlock_irq(&rnp->lock);
7fdefc10
PM
1172}
1173
1174/*
1175 * Body of kthread that handles grace periods.
1176 */
1177static int __noreturn rcu_gp_kthread(void *arg)
1178{
4cdfc175 1179 int fqs_state;
d40011f6 1180 unsigned long j;
4cdfc175 1181 int ret;
7fdefc10
PM
1182 struct rcu_state *rsp = arg;
1183 struct rcu_node *rnp = rcu_get_root(rsp);
1184
1185 for (;;) {
1186
1187 /* Handle grace-period start. */
1188 for (;;) {
4cdfc175
PM
1189 wait_event_interruptible(rsp->gp_wq,
1190 rsp->gp_flags &
1191 RCU_GP_FLAG_INIT);
1192 if ((rsp->gp_flags & RCU_GP_FLAG_INIT) &&
1193 rcu_gp_init(rsp))
7fdefc10
PM
1194 break;
1195 cond_resched();
1196 flush_signals(current);
1197 }
cabc49c1 1198
4cdfc175
PM
1199 /* Handle quiescent-state forcing. */
1200 fqs_state = RCU_SAVE_DYNTICK;
d40011f6
PM
1201 j = jiffies_till_first_fqs;
1202 if (j > HZ) {
1203 j = HZ;
1204 jiffies_till_first_fqs = HZ;
1205 }
cabc49c1 1206 for (;;) {
d40011f6 1207 rsp->jiffies_force_qs = jiffies + j;
4cdfc175
PM
1208 ret = wait_event_interruptible_timeout(rsp->gp_wq,
1209 (rsp->gp_flags & RCU_GP_FLAG_FQS) ||
1210 (!ACCESS_ONCE(rnp->qsmask) &&
1211 !rcu_preempt_blocked_readers_cgp(rnp)),
d40011f6 1212 j);
4cdfc175 1213 /* If grace period done, leave loop. */
cabc49c1 1214 if (!ACCESS_ONCE(rnp->qsmask) &&
4cdfc175 1215 !rcu_preempt_blocked_readers_cgp(rnp))
cabc49c1 1216 break;
4cdfc175
PM
1217 /* If time for quiescent-state forcing, do it. */
1218 if (ret == 0 || (rsp->gp_flags & RCU_GP_FLAG_FQS)) {
1219 fqs_state = rcu_gp_fqs(rsp, fqs_state);
1220 cond_resched();
1221 } else {
1222 /* Deal with stray signal. */
1223 cond_resched();
1224 flush_signals(current);
1225 }
d40011f6
PM
1226 j = jiffies_till_next_fqs;
1227 if (j > HZ) {
1228 j = HZ;
1229 jiffies_till_next_fqs = HZ;
1230 } else if (j < 1) {
1231 j = 1;
1232 jiffies_till_next_fqs = 1;
1233 }
cabc49c1 1234 }
4cdfc175
PM
1235
1236 /* Handle grace-period end. */
1237 rcu_gp_cleanup(rsp);
b3dbec76 1238 }
b3dbec76
PM
1239}
1240
64db4cff
PM
1241/*
1242 * Start a new RCU grace period if warranted, re-initializing the hierarchy
1243 * in preparation for detecting the next grace period. The caller must hold
1244 * the root node's ->lock, which is released before return. Hard irqs must
1245 * be disabled.
e5601400
PM
1246 *
1247 * Note that it is legal for a dying CPU (which is marked as offline) to
1248 * invoke this function. This can happen when the dying CPU reports its
1249 * quiescent state.
64db4cff
PM
1250 */
1251static void
1252rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
1253 __releases(rcu_get_root(rsp)->lock)
1254{
394f99a9 1255 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
64db4cff 1256 struct rcu_node *rnp = rcu_get_root(rsp);
64db4cff 1257
b3dbec76 1258 if (!rsp->gp_kthread ||
afe24b12
PM
1259 !cpu_needs_another_gp(rsp, rdp)) {
1260 /*
b3dbec76
PM
1261 * Either we have not yet spawned the grace-period
1262 * task or this CPU does not need another grace period.
1263 * Either way, don't start a new grace period.
afe24b12
PM
1264 */
1265 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1266 return;
1267 }
b32e9eb6 1268
4cdfc175 1269 rsp->gp_flags = RCU_GP_FLAG_INIT;
b3dbec76
PM
1270 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1271 wake_up(&rsp->gp_wq);
64db4cff
PM
1272}
1273
f41d911f 1274/*
d3f6bad3
PM
1275 * Report a full set of quiescent states to the specified rcu_state
1276 * data structure. This involves cleaning up after the prior grace
1277 * period and letting rcu_start_gp() start up the next grace period
1278 * if one is needed. Note that the caller must hold rnp->lock, as
1279 * required by rcu_start_gp(), which will release it.
f41d911f 1280 */
d3f6bad3 1281static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
fc2219d4 1282 __releases(rcu_get_root(rsp)->lock)
f41d911f 1283{
fc2219d4 1284 WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
cabc49c1
PM
1285 raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
1286 wake_up(&rsp->gp_wq); /* Memory barrier implied by wake_up() path. */
f41d911f
PM
1287}
1288
64db4cff 1289/*
d3f6bad3
PM
1290 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
1291 * Allows quiescent states for a group of CPUs to be reported at one go
1292 * to the specified rcu_node structure, though all the CPUs in the group
1293 * must be represented by the same rcu_node structure (which need not be
1294 * a leaf rcu_node structure, though it often will be). That structure's
1295 * lock must be held upon entry, and it is released before return.
64db4cff
PM
1296 */
1297static void
d3f6bad3
PM
1298rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
1299 struct rcu_node *rnp, unsigned long flags)
64db4cff
PM
1300 __releases(rnp->lock)
1301{
28ecd580
PM
1302 struct rcu_node *rnp_c;
1303
64db4cff
PM
1304 /* Walk up the rcu_node hierarchy. */
1305 for (;;) {
1306 if (!(rnp->qsmask & mask)) {
1307
1308 /* Our bit has already been cleared, so done. */
1304afb2 1309 raw_spin_unlock_irqrestore(&rnp->lock, flags);
64db4cff
PM
1310 return;
1311 }
1312 rnp->qsmask &= ~mask;
d4c08f2a
PM
1313 trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
1314 mask, rnp->qsmask, rnp->level,
1315 rnp->grplo, rnp->grphi,
1316 !!rnp->gp_tasks);
27f4d280 1317 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
64db4cff
PM
1318
1319 /* Other bits still set at this level, so done. */
1304afb2 1320 raw_spin_unlock_irqrestore(&rnp->lock, flags);
64db4cff
PM
1321 return;
1322 }
1323 mask = rnp->grpmask;
1324 if (rnp->parent == NULL) {
1325
1326 /* No more levels. Exit loop holding root lock. */
1327
1328 break;
1329 }
1304afb2 1330 raw_spin_unlock_irqrestore(&rnp->lock, flags);
28ecd580 1331 rnp_c = rnp;
64db4cff 1332 rnp = rnp->parent;
1304afb2 1333 raw_spin_lock_irqsave(&rnp->lock, flags);
28ecd580 1334 WARN_ON_ONCE(rnp_c->qsmask);
64db4cff
PM
1335 }
1336
1337 /*
1338 * Get here if we are the last CPU to pass through a quiescent
d3f6bad3 1339 * state for this grace period. Invoke rcu_report_qs_rsp()
f41d911f 1340 * to clean up and start the next grace period if one is needed.
64db4cff 1341 */
d3f6bad3 1342 rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
64db4cff
PM
1343}
1344
1345/*
d3f6bad3
PM
1346 * Record a quiescent state for the specified CPU to that CPU's rcu_data
1347 * structure. This must be either called from the specified CPU, or
1348 * called when the specified CPU is known to be offline (and when it is
1349 * also known that no other CPU is concurrently trying to help the offline
1350 * CPU). The lastcomp argument is used to make sure we are still in the
1351 * grace period of interest. We don't want to end the current grace period
1352 * based on quiescent states detected in an earlier grace period!
64db4cff
PM
1353 */
1354static void
e4cc1f22 1355rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp, long lastgp)
64db4cff
PM
1356{
1357 unsigned long flags;
1358 unsigned long mask;
1359 struct rcu_node *rnp;
1360
1361 rnp = rdp->mynode;
1304afb2 1362 raw_spin_lock_irqsave(&rnp->lock, flags);
e4cc1f22 1363 if (lastgp != rnp->gpnum || rnp->completed == rnp->gpnum) {
64db4cff
PM
1364
1365 /*
e4cc1f22
PM
1366 * The grace period in which this quiescent state was
1367 * recorded has ended, so don't report it upwards.
1368 * We will instead need a new quiescent state that lies
1369 * within the current grace period.
64db4cff 1370 */
e4cc1f22 1371 rdp->passed_quiesce = 0; /* need qs for new gp. */
1304afb2 1372 raw_spin_unlock_irqrestore(&rnp->lock, flags);
64db4cff
PM
1373 return;
1374 }
1375 mask = rdp->grpmask;
1376 if ((rnp->qsmask & mask) == 0) {
1304afb2 1377 raw_spin_unlock_irqrestore(&rnp->lock, flags);
64db4cff
PM
1378 } else {
1379 rdp->qs_pending = 0;
1380
1381 /*
1382 * This GP can't end until cpu checks in, so all of our
1383 * callbacks can be processed during the next GP.
1384 */
64db4cff
PM
1385 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
1386
d3f6bad3 1387 rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */
64db4cff
PM
1388 }
1389}
1390
1391/*
1392 * Check to see if there is a new grace period of which this CPU
1393 * is not yet aware, and if so, set up local rcu_data state for it.
1394 * Otherwise, see if this CPU has just passed through its first
1395 * quiescent state for this grace period, and record that fact if so.
1396 */
1397static void
1398rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
1399{
1400 /* If there is now a new grace period, record and return. */
1401 if (check_for_new_grace_period(rsp, rdp))
1402 return;
1403
1404 /*
1405 * Does this CPU still need to do its part for current grace period?
1406 * If no, return and let the other CPUs do their part as well.
1407 */
1408 if (!rdp->qs_pending)
1409 return;
1410
1411 /*
1412 * Was there a quiescent state since the beginning of the grace
1413 * period? If no, then exit and wait for the next call.
1414 */
e4cc1f22 1415 if (!rdp->passed_quiesce)
64db4cff
PM
1416 return;
1417
d3f6bad3
PM
1418 /*
1419 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
1420 * judge of that).
1421 */
e4cc1f22 1422 rcu_report_qs_rdp(rdp->cpu, rsp, rdp, rdp->passed_quiesce_gpnum);
64db4cff
PM
1423}
1424
1425#ifdef CONFIG_HOTPLUG_CPU
1426
e74f4c45 1427/*
b1420f1c
PM
1428 * Send the specified CPU's RCU callbacks to the orphanage. The
1429 * specified CPU must be offline, and the caller must hold the
1430 * ->onofflock.
e74f4c45 1431 */
b1420f1c
PM
1432static void
1433rcu_send_cbs_to_orphanage(int cpu, struct rcu_state *rsp,
1434 struct rcu_node *rnp, struct rcu_data *rdp)
e74f4c45 1435{
b1420f1c
PM
1436 /*
1437 * Orphan the callbacks. First adjust the counts. This is safe
1438 * because ->onofflock excludes _rcu_barrier()'s adoption of
1439 * the callbacks, thus no memory barrier is required.
1440 */
a50c3af9 1441 if (rdp->nxtlist != NULL) {
b1420f1c
PM
1442 rsp->qlen_lazy += rdp->qlen_lazy;
1443 rsp->qlen += rdp->qlen;
1444 rdp->n_cbs_orphaned += rdp->qlen;
a50c3af9 1445 rdp->qlen_lazy = 0;
1d1fb395 1446 ACCESS_ONCE(rdp->qlen) = 0;
a50c3af9
PM
1447 }
1448
1449 /*
b1420f1c
PM
1450 * Next, move those callbacks still needing a grace period to
1451 * the orphanage, where some other CPU will pick them up.
1452 * Some of the callbacks might have gone partway through a grace
1453 * period, but that is too bad. They get to start over because we
1454 * cannot assume that grace periods are synchronized across CPUs.
1455 * We don't bother updating the ->nxttail[] array yet, instead
1456 * we just reset the whole thing later on.
a50c3af9 1457 */
b1420f1c
PM
1458 if (*rdp->nxttail[RCU_DONE_TAIL] != NULL) {
1459 *rsp->orphan_nxttail = *rdp->nxttail[RCU_DONE_TAIL];
1460 rsp->orphan_nxttail = rdp->nxttail[RCU_NEXT_TAIL];
1461 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
a50c3af9
PM
1462 }
1463
1464 /*
b1420f1c
PM
1465 * Then move the ready-to-invoke callbacks to the orphanage,
1466 * where some other CPU will pick them up. These will not be
1467 * required to pass though another grace period: They are done.
a50c3af9 1468 */
e5601400 1469 if (rdp->nxtlist != NULL) {
b1420f1c
PM
1470 *rsp->orphan_donetail = rdp->nxtlist;
1471 rsp->orphan_donetail = rdp->nxttail[RCU_DONE_TAIL];
e5601400 1472 }
e74f4c45 1473
b1420f1c 1474 /* Finally, initialize the rcu_data structure's list to empty. */
3f5d3ea6 1475 init_callback_list(rdp);
b1420f1c
PM
1476}
1477
1478/*
1479 * Adopt the RCU callbacks from the specified rcu_state structure's
1480 * orphanage. The caller must hold the ->onofflock.
1481 */
1482static void rcu_adopt_orphan_cbs(struct rcu_state *rsp)
1483{
1484 int i;
1485 struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
1486
a50c3af9 1487 /*
b1420f1c
PM
1488 * If there is an rcu_barrier() operation in progress, then
1489 * only the task doing that operation is permitted to adopt
1490 * callbacks. To do otherwise breaks rcu_barrier() and friends
1491 * by causing them to fail to wait for the callbacks in the
1492 * orphanage.
a50c3af9 1493 */
b1420f1c
PM
1494 if (rsp->rcu_barrier_in_progress &&
1495 rsp->rcu_barrier_in_progress != current)
1496 return;
1497
1498 /* Do the accounting first. */
1499 rdp->qlen_lazy += rsp->qlen_lazy;
1500 rdp->qlen += rsp->qlen;
1501 rdp->n_cbs_adopted += rsp->qlen;
8f5af6f1
PM
1502 if (rsp->qlen_lazy != rsp->qlen)
1503 rcu_idle_count_callbacks_posted();
b1420f1c
PM
1504 rsp->qlen_lazy = 0;
1505 rsp->qlen = 0;
1506
1507 /*
1508 * We do not need a memory barrier here because the only way we
1509 * can get here if there is an rcu_barrier() in flight is if
1510 * we are the task doing the rcu_barrier().
1511 */
1512
1513 /* First adopt the ready-to-invoke callbacks. */
1514 if (rsp->orphan_donelist != NULL) {
1515 *rsp->orphan_donetail = *rdp->nxttail[RCU_DONE_TAIL];
1516 *rdp->nxttail[RCU_DONE_TAIL] = rsp->orphan_donelist;
1517 for (i = RCU_NEXT_SIZE - 1; i >= RCU_DONE_TAIL; i--)
1518 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
1519 rdp->nxttail[i] = rsp->orphan_donetail;
1520 rsp->orphan_donelist = NULL;
1521 rsp->orphan_donetail = &rsp->orphan_donelist;
1522 }
1523
1524 /* And then adopt the callbacks that still need a grace period. */
1525 if (rsp->orphan_nxtlist != NULL) {
1526 *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxtlist;
1527 rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxttail;
1528 rsp->orphan_nxtlist = NULL;
1529 rsp->orphan_nxttail = &rsp->orphan_nxtlist;
1530 }
1531}
1532
1533/*
1534 * Trace the fact that this CPU is going offline.
1535 */
1536static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
1537{
1538 RCU_TRACE(unsigned long mask);
1539 RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda));
1540 RCU_TRACE(struct rcu_node *rnp = rdp->mynode);
1541
1542 RCU_TRACE(mask = rdp->grpmask);
e5601400
PM
1543 trace_rcu_grace_period(rsp->name,
1544 rnp->gpnum + 1 - !!(rnp->qsmask & mask),
1545 "cpuofl");
64db4cff
PM
1546}
1547
1548/*
e5601400 1549 * The CPU has been completely removed, and some other CPU is reporting
b1420f1c
PM
1550 * this fact from process context. Do the remainder of the cleanup,
1551 * including orphaning the outgoing CPU's RCU callbacks, and also
1552 * adopting them, if there is no _rcu_barrier() instance running.
e5601400
PM
1553 * There can only be one CPU hotplug operation at a time, so no other
1554 * CPU can be attempting to update rcu_cpu_kthread_task.
64db4cff 1555 */
e5601400 1556static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
64db4cff 1557{
2036d94a
PM
1558 unsigned long flags;
1559 unsigned long mask;
1560 int need_report = 0;
e5601400 1561 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
b1420f1c 1562 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
e5601400 1563
2036d94a 1564 /* Adjust any no-longer-needed kthreads. */
e5601400
PM
1565 rcu_stop_cpu_kthread(cpu);
1566 rcu_node_kthread_setaffinity(rnp, -1);
2036d94a 1567
b1420f1c 1568 /* Remove the dead CPU from the bitmasks in the rcu_node hierarchy. */
2036d94a
PM
1569
1570 /* Exclude any attempts to start a new grace period. */
1571 raw_spin_lock_irqsave(&rsp->onofflock, flags);
1572
b1420f1c
PM
1573 /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */
1574 rcu_send_cbs_to_orphanage(cpu, rsp, rnp, rdp);
1575 rcu_adopt_orphan_cbs(rsp);
1576
2036d94a
PM
1577 /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
1578 mask = rdp->grpmask; /* rnp->grplo is constant. */
1579 do {
1580 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
1581 rnp->qsmaskinit &= ~mask;
1582 if (rnp->qsmaskinit != 0) {
1583 if (rnp != rdp->mynode)
1584 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1585 break;
1586 }
1587 if (rnp == rdp->mynode)
1588 need_report = rcu_preempt_offline_tasks(rsp, rnp, rdp);
1589 else
1590 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1591 mask = rnp->grpmask;
1592 rnp = rnp->parent;
1593 } while (rnp != NULL);
1594
1595 /*
1596 * We still hold the leaf rcu_node structure lock here, and
1597 * irqs are still disabled. The reason for this subterfuge is
1598 * because invoking rcu_report_unblock_qs_rnp() with ->onofflock
1599 * held leads to deadlock.
1600 */
1601 raw_spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
1602 rnp = rdp->mynode;
1603 if (need_report & RCU_OFL_TASKS_NORM_GP)
1604 rcu_report_unblock_qs_rnp(rnp, flags);
1605 else
1606 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1607 if (need_report & RCU_OFL_TASKS_EXP_GP)
1608 rcu_report_exp_rnp(rsp, rnp, true);
cf01537e
PM
1609 WARN_ONCE(rdp->qlen != 0 || rdp->nxtlist != NULL,
1610 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n",
1611 cpu, rdp->qlen, rdp->nxtlist);
64db4cff
PM
1612}
1613
1614#else /* #ifdef CONFIG_HOTPLUG_CPU */
1615
b1420f1c
PM
1616static void rcu_adopt_orphan_cbs(struct rcu_state *rsp)
1617{
1618}
1619
e5601400 1620static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
e74f4c45
PM
1621{
1622}
1623
e5601400 1624static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
64db4cff
PM
1625{
1626}
1627
1628#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1629
1630/*
1631 * Invoke any RCU callbacks that have made it to the end of their grace
1632 * period. Thottle as specified by rdp->blimit.
1633 */
37c72e56 1634static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
64db4cff
PM
1635{
1636 unsigned long flags;
1637 struct rcu_head *next, *list, **tail;
b41772ab 1638 int bl, count, count_lazy, i;
64db4cff
PM
1639
1640 /* If no callbacks are ready, just return.*/
29c00b4a 1641 if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
486e2593 1642 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0);
4968c300
PM
1643 trace_rcu_batch_end(rsp->name, 0, !!ACCESS_ONCE(rdp->nxtlist),
1644 need_resched(), is_idle_task(current),
1645 rcu_is_callbacks_kthread());
64db4cff 1646 return;
29c00b4a 1647 }
64db4cff
PM
1648
1649 /*
1650 * Extract the list of ready callbacks, disabling to prevent
1651 * races with call_rcu() from interrupt handlers.
1652 */
1653 local_irq_save(flags);
8146c4e2 1654 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
29c00b4a 1655 bl = rdp->blimit;
486e2593 1656 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl);
64db4cff
PM
1657 list = rdp->nxtlist;
1658 rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
1659 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
1660 tail = rdp->nxttail[RCU_DONE_TAIL];
b41772ab
PM
1661 for (i = RCU_NEXT_SIZE - 1; i >= 0; i--)
1662 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
1663 rdp->nxttail[i] = &rdp->nxtlist;
64db4cff
PM
1664 local_irq_restore(flags);
1665
1666 /* Invoke callbacks. */
486e2593 1667 count = count_lazy = 0;
64db4cff
PM
1668 while (list) {
1669 next = list->next;
1670 prefetch(next);
551d55a9 1671 debug_rcu_head_unqueue(list);
486e2593
PM
1672 if (__rcu_reclaim(rsp->name, list))
1673 count_lazy++;
64db4cff 1674 list = next;
dff1672d
PM
1675 /* Stop only if limit reached and CPU has something to do. */
1676 if (++count >= bl &&
1677 (need_resched() ||
1678 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
64db4cff
PM
1679 break;
1680 }
1681
1682 local_irq_save(flags);
4968c300
PM
1683 trace_rcu_batch_end(rsp->name, count, !!list, need_resched(),
1684 is_idle_task(current),
1685 rcu_is_callbacks_kthread());
64db4cff
PM
1686
1687 /* Update count, and requeue any remaining callbacks. */
64db4cff
PM
1688 if (list != NULL) {
1689 *tail = rdp->nxtlist;
1690 rdp->nxtlist = list;
b41772ab
PM
1691 for (i = 0; i < RCU_NEXT_SIZE; i++)
1692 if (&rdp->nxtlist == rdp->nxttail[i])
1693 rdp->nxttail[i] = tail;
64db4cff
PM
1694 else
1695 break;
1696 }
b1420f1c
PM
1697 smp_mb(); /* List handling before counting for rcu_barrier(). */
1698 rdp->qlen_lazy -= count_lazy;
1d1fb395 1699 ACCESS_ONCE(rdp->qlen) -= count;
b1420f1c 1700 rdp->n_cbs_invoked += count;
64db4cff
PM
1701
1702 /* Reinstate batch limit if we have worked down the excess. */
1703 if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
1704 rdp->blimit = blimit;
1705
37c72e56
PM
1706 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
1707 if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
1708 rdp->qlen_last_fqs_check = 0;
1709 rdp->n_force_qs_snap = rsp->n_force_qs;
1710 } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
1711 rdp->qlen_last_fqs_check = rdp->qlen;
cfca9279 1712 WARN_ON_ONCE((rdp->nxtlist == NULL) != (rdp->qlen == 0));
37c72e56 1713
64db4cff
PM
1714 local_irq_restore(flags);
1715
e0f23060 1716 /* Re-invoke RCU core processing if there are callbacks remaining. */
64db4cff 1717 if (cpu_has_callbacks_ready_to_invoke(rdp))
a46e0899 1718 invoke_rcu_core();
64db4cff
PM
1719}
1720
1721/*
1722 * Check to see if this CPU is in a non-context-switch quiescent state
1723 * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
e0f23060 1724 * Also schedule RCU core processing.
64db4cff 1725 *
9b2e4f18 1726 * This function must be called from hardirq context. It is normally
64db4cff
PM
1727 * invoked from the scheduling-clock interrupt. If rcu_pending returns
1728 * false, there is no point in invoking rcu_check_callbacks().
1729 */
1730void rcu_check_callbacks(int cpu, int user)
1731{
300df91c 1732 trace_rcu_utilization("Start scheduler-tick");
a858af28 1733 increment_cpu_stall_ticks();
9b2e4f18 1734 if (user || rcu_is_cpu_rrupt_from_idle()) {
64db4cff
PM
1735
1736 /*
1737 * Get here if this CPU took its interrupt from user
1738 * mode or from the idle loop, and if this is not a
1739 * nested interrupt. In this case, the CPU is in
d6714c22 1740 * a quiescent state, so note it.
64db4cff
PM
1741 *
1742 * No memory barrier is required here because both
d6714c22
PM
1743 * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
1744 * variables that other CPUs neither access nor modify,
1745 * at least not while the corresponding CPU is online.
64db4cff
PM
1746 */
1747
d6714c22
PM
1748 rcu_sched_qs(cpu);
1749 rcu_bh_qs(cpu);
64db4cff
PM
1750
1751 } else if (!in_softirq()) {
1752
1753 /*
1754 * Get here if this CPU did not take its interrupt from
1755 * softirq, in other words, if it is not interrupting
1756 * a rcu_bh read-side critical section. This is an _bh
d6714c22 1757 * critical section, so note it.
64db4cff
PM
1758 */
1759
d6714c22 1760 rcu_bh_qs(cpu);
64db4cff 1761 }
f41d911f 1762 rcu_preempt_check_callbacks(cpu);
d21670ac 1763 if (rcu_pending(cpu))
a46e0899 1764 invoke_rcu_core();
300df91c 1765 trace_rcu_utilization("End scheduler-tick");
64db4cff
PM
1766}
1767
64db4cff
PM
1768/*
1769 * Scan the leaf rcu_node structures, processing dyntick state for any that
1770 * have not yet encountered a quiescent state, using the function specified.
27f4d280
PM
1771 * Also initiate boosting for any threads blocked on the root rcu_node.
1772 *
ee47eb9f 1773 * The caller must have suppressed start of new grace periods.
64db4cff 1774 */
45f014c5 1775static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *))
64db4cff
PM
1776{
1777 unsigned long bit;
1778 int cpu;
1779 unsigned long flags;
1780 unsigned long mask;
a0b6c9a7 1781 struct rcu_node *rnp;
64db4cff 1782
a0b6c9a7 1783 rcu_for_each_leaf_node(rsp, rnp) {
b4be093f 1784 cond_resched();
64db4cff 1785 mask = 0;
1304afb2 1786 raw_spin_lock_irqsave(&rnp->lock, flags);
ee47eb9f 1787 if (!rcu_gp_in_progress(rsp)) {
1304afb2 1788 raw_spin_unlock_irqrestore(&rnp->lock, flags);
0f10dc82 1789 return;
64db4cff 1790 }
a0b6c9a7 1791 if (rnp->qsmask == 0) {
1217ed1b 1792 rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
64db4cff
PM
1793 continue;
1794 }
a0b6c9a7 1795 cpu = rnp->grplo;
64db4cff 1796 bit = 1;
a0b6c9a7 1797 for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
394f99a9
LJ
1798 if ((rnp->qsmask & bit) != 0 &&
1799 f(per_cpu_ptr(rsp->rda, cpu)))
64db4cff
PM
1800 mask |= bit;
1801 }
45f014c5 1802 if (mask != 0) {
64db4cff 1803
d3f6bad3
PM
1804 /* rcu_report_qs_rnp() releases rnp->lock. */
1805 rcu_report_qs_rnp(mask, rsp, rnp, flags);
64db4cff
PM
1806 continue;
1807 }
1304afb2 1808 raw_spin_unlock_irqrestore(&rnp->lock, flags);
64db4cff 1809 }
27f4d280 1810 rnp = rcu_get_root(rsp);
1217ed1b
PM
1811 if (rnp->qsmask == 0) {
1812 raw_spin_lock_irqsave(&rnp->lock, flags);
1813 rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
1814 }
64db4cff
PM
1815}
1816
1817/*
1818 * Force quiescent states on reluctant CPUs, and also detect which
1819 * CPUs are in dyntick-idle mode.
1820 */
4cdfc175 1821static void force_quiescent_state(struct rcu_state *rsp)
64db4cff
PM
1822{
1823 unsigned long flags;
394f2769
PM
1824 bool ret;
1825 struct rcu_node *rnp;
1826 struct rcu_node *rnp_old = NULL;
1827
1828 /* Funnel through hierarchy to reduce memory contention. */
1829 rnp = per_cpu_ptr(rsp->rda, raw_smp_processor_id())->mynode;
1830 for (; rnp != NULL; rnp = rnp->parent) {
1831 ret = (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
1832 !raw_spin_trylock(&rnp->fqslock);
1833 if (rnp_old != NULL)
1834 raw_spin_unlock(&rnp_old->fqslock);
1835 if (ret) {
1836 rsp->n_force_qs_lh++;
1837 return;
1838 }
1839 rnp_old = rnp;
1840 }
1841 /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
64db4cff 1842
394f2769
PM
1843 /* Reached the root of the rcu_node tree, acquire lock. */
1844 raw_spin_lock_irqsave(&rnp_old->lock, flags);
1845 raw_spin_unlock(&rnp_old->fqslock);
1846 if (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
1847 rsp->n_force_qs_lh++;
1848 raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
4cdfc175 1849 return; /* Someone beat us to it. */
46a1e34e 1850 }
4cdfc175 1851 rsp->gp_flags |= RCU_GP_FLAG_FQS;
394f2769 1852 raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
4cdfc175 1853 wake_up(&rsp->gp_wq); /* Memory barrier implied by wake_up() path. */
64db4cff
PM
1854}
1855
64db4cff 1856/*
e0f23060
PM
1857 * This does the RCU core processing work for the specified rcu_state
1858 * and rcu_data structures. This may be called only from the CPU to
1859 * whom the rdp belongs.
64db4cff
PM
1860 */
1861static void
1bca8cf1 1862__rcu_process_callbacks(struct rcu_state *rsp)
64db4cff
PM
1863{
1864 unsigned long flags;
1bca8cf1 1865 struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
64db4cff 1866
2e597558
PM
1867 WARN_ON_ONCE(rdp->beenonline == 0);
1868
64db4cff
PM
1869 /*
1870 * Advance callbacks in response to end of earlier grace
1871 * period that some other CPU ended.
1872 */
1873 rcu_process_gp_end(rsp, rdp);
1874
1875 /* Update RCU state based on any recent quiescent states. */
1876 rcu_check_quiescent_state(rsp, rdp);
1877
1878 /* Does this CPU require a not-yet-started grace period? */
1879 if (cpu_needs_another_gp(rsp, rdp)) {
1304afb2 1880 raw_spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
64db4cff
PM
1881 rcu_start_gp(rsp, flags); /* releases above lock */
1882 }
1883
1884 /* If there are callbacks ready, invoke them. */
09223371 1885 if (cpu_has_callbacks_ready_to_invoke(rdp))
a46e0899 1886 invoke_rcu_callbacks(rsp, rdp);
09223371
SL
1887}
1888
64db4cff 1889/*
e0f23060 1890 * Do RCU core processing for the current CPU.
64db4cff 1891 */
09223371 1892static void rcu_process_callbacks(struct softirq_action *unused)
64db4cff 1893{
6ce75a23
PM
1894 struct rcu_state *rsp;
1895
bfa00b4c
PM
1896 if (cpu_is_offline(smp_processor_id()))
1897 return;
300df91c 1898 trace_rcu_utilization("Start RCU core");
6ce75a23
PM
1899 for_each_rcu_flavor(rsp)
1900 __rcu_process_callbacks(rsp);
300df91c 1901 trace_rcu_utilization("End RCU core");
64db4cff
PM
1902}
1903
a26ac245 1904/*
e0f23060
PM
1905 * Schedule RCU callback invocation. If the specified type of RCU
1906 * does not support RCU priority boosting, just do a direct call,
1907 * otherwise wake up the per-CPU kernel kthread. Note that because we
1908 * are running on the current CPU with interrupts disabled, the
1909 * rcu_cpu_kthread_task cannot disappear out from under us.
a26ac245 1910 */
a46e0899 1911static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
a26ac245 1912{
b0d30417
PM
1913 if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active)))
1914 return;
a46e0899
PM
1915 if (likely(!rsp->boost)) {
1916 rcu_do_batch(rsp, rdp);
a26ac245
PM
1917 return;
1918 }
a46e0899 1919 invoke_rcu_callbacks_kthread();
a26ac245
PM
1920}
1921
a46e0899 1922static void invoke_rcu_core(void)
09223371
SL
1923{
1924 raise_softirq(RCU_SOFTIRQ);
1925}
1926
29154c57
PM
1927/*
1928 * Handle any core-RCU processing required by a call_rcu() invocation.
1929 */
1930static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
1931 struct rcu_head *head, unsigned long flags)
64db4cff 1932{
62fde6ed
PM
1933 /*
1934 * If called from an extended quiescent state, invoke the RCU
1935 * core in order to force a re-evaluation of RCU's idleness.
1936 */
a16b7a69 1937 if (rcu_is_cpu_idle() && cpu_online(smp_processor_id()))
62fde6ed
PM
1938 invoke_rcu_core();
1939
a16b7a69 1940 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
29154c57 1941 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
2655d57e 1942 return;
64db4cff 1943
37c72e56
PM
1944 /*
1945 * Force the grace period if too many callbacks or too long waiting.
1946 * Enforce hysteresis, and don't invoke force_quiescent_state()
1947 * if some other CPU has recently done so. Also, don't bother
1948 * invoking force_quiescent_state() if the newly enqueued callback
1949 * is the only one waiting for a grace period to complete.
1950 */
2655d57e 1951 if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
b52573d2
PM
1952
1953 /* Are we ignoring a completed grace period? */
1954 rcu_process_gp_end(rsp, rdp);
1955 check_for_new_grace_period(rsp, rdp);
1956
1957 /* Start a new grace period if one not already started. */
1958 if (!rcu_gp_in_progress(rsp)) {
1959 unsigned long nestflag;
1960 struct rcu_node *rnp_root = rcu_get_root(rsp);
1961
1962 raw_spin_lock_irqsave(&rnp_root->lock, nestflag);
1963 rcu_start_gp(rsp, nestflag); /* rlses rnp_root->lock */
1964 } else {
1965 /* Give the grace period a kick. */
1966 rdp->blimit = LONG_MAX;
1967 if (rsp->n_force_qs == rdp->n_force_qs_snap &&
1968 *rdp->nxttail[RCU_DONE_TAIL] != head)
4cdfc175 1969 force_quiescent_state(rsp);
b52573d2
PM
1970 rdp->n_force_qs_snap = rsp->n_force_qs;
1971 rdp->qlen_last_fqs_check = rdp->qlen;
1972 }
4cdfc175 1973 }
29154c57
PM
1974}
1975
64db4cff
PM
1976static void
1977__call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
486e2593 1978 struct rcu_state *rsp, bool lazy)
64db4cff
PM
1979{
1980 unsigned long flags;
1981 struct rcu_data *rdp;
1982
0bb7b59d 1983 WARN_ON_ONCE((unsigned long)head & 0x3); /* Misaligned rcu_head! */
551d55a9 1984 debug_rcu_head_queue(head);
64db4cff
PM
1985 head->func = func;
1986 head->next = NULL;
1987
1988 smp_mb(); /* Ensure RCU update seen before callback registry. */
1989
1990 /*
1991 * Opportunistically note grace-period endings and beginnings.
1992 * Note that we might see a beginning right after we see an
1993 * end, but never vice versa, since this CPU has to pass through
1994 * a quiescent state betweentimes.
1995 */
1996 local_irq_save(flags);
394f99a9 1997 rdp = this_cpu_ptr(rsp->rda);
64db4cff
PM
1998
1999 /* Add the callback to our list. */
29154c57 2000 ACCESS_ONCE(rdp->qlen)++;
486e2593
PM
2001 if (lazy)
2002 rdp->qlen_lazy++;
c57afe80
PM
2003 else
2004 rcu_idle_count_callbacks_posted();
b1420f1c
PM
2005 smp_mb(); /* Count before adding callback for rcu_barrier(). */
2006 *rdp->nxttail[RCU_NEXT_TAIL] = head;
2007 rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
2655d57e 2008
d4c08f2a
PM
2009 if (__is_kfree_rcu_offset((unsigned long)func))
2010 trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
486e2593 2011 rdp->qlen_lazy, rdp->qlen);
d4c08f2a 2012 else
486e2593 2013 trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen);
d4c08f2a 2014
29154c57
PM
2015 /* Go handle any RCU core processing required. */
2016 __call_rcu_core(rsp, rdp, head, flags);
64db4cff
PM
2017 local_irq_restore(flags);
2018}
2019
2020/*
d6714c22 2021 * Queue an RCU-sched callback for invocation after a grace period.
64db4cff 2022 */
d6714c22 2023void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
64db4cff 2024{
486e2593 2025 __call_rcu(head, func, &rcu_sched_state, 0);
64db4cff 2026}
d6714c22 2027EXPORT_SYMBOL_GPL(call_rcu_sched);
64db4cff
PM
2028
2029/*
486e2593 2030 * Queue an RCU callback for invocation after a quicker grace period.
64db4cff
PM
2031 */
2032void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
2033{
486e2593 2034 __call_rcu(head, func, &rcu_bh_state, 0);
64db4cff
PM
2035}
2036EXPORT_SYMBOL_GPL(call_rcu_bh);
2037
6d813391
PM
2038/*
2039 * Because a context switch is a grace period for RCU-sched and RCU-bh,
2040 * any blocking grace-period wait automatically implies a grace period
2041 * if there is only one CPU online at any point time during execution
2042 * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
2043 * occasionally incorrectly indicate that there are multiple CPUs online
2044 * when there was in fact only one the whole time, as this just adds
2045 * some overhead: RCU still operates correctly.
6d813391
PM
2046 */
2047static inline int rcu_blocking_is_gp(void)
2048{
95f0c1de
PM
2049 int ret;
2050
6d813391 2051 might_sleep(); /* Check for RCU read-side critical section. */
95f0c1de
PM
2052 preempt_disable();
2053 ret = num_online_cpus() <= 1;
2054 preempt_enable();
2055 return ret;
6d813391
PM
2056}
2057
6ebb237b
PM
2058/**
2059 * synchronize_sched - wait until an rcu-sched grace period has elapsed.
2060 *
2061 * Control will return to the caller some time after a full rcu-sched
2062 * grace period has elapsed, in other words after all currently executing
2063 * rcu-sched read-side critical sections have completed. These read-side
2064 * critical sections are delimited by rcu_read_lock_sched() and
2065 * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
2066 * local_irq_disable(), and so on may be used in place of
2067 * rcu_read_lock_sched().
2068 *
2069 * This means that all preempt_disable code sequences, including NMI and
2070 * hardware-interrupt handlers, in progress on entry will have completed
2071 * before this primitive returns. However, this does not guarantee that
2072 * softirq handlers will have completed, since in some kernels, these
2073 * handlers can run in process context, and can block.
2074 *
2075 * This primitive provides the guarantees made by the (now removed)
2076 * synchronize_kernel() API. In contrast, synchronize_rcu() only
2077 * guarantees that rcu_read_lock() sections will have completed.
2078 * In "classic RCU", these two guarantees happen to be one and
2079 * the same, but can differ in realtime RCU implementations.
2080 */
2081void synchronize_sched(void)
2082{
fe15d706
PM
2083 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
2084 !lock_is_held(&rcu_lock_map) &&
2085 !lock_is_held(&rcu_sched_lock_map),
2086 "Illegal synchronize_sched() in RCU-sched read-side critical section");
6ebb237b
PM
2087 if (rcu_blocking_is_gp())
2088 return;
2c42818e 2089 wait_rcu_gp(call_rcu_sched);
6ebb237b
PM
2090}
2091EXPORT_SYMBOL_GPL(synchronize_sched);
2092
2093/**
2094 * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
2095 *
2096 * Control will return to the caller some time after a full rcu_bh grace
2097 * period has elapsed, in other words after all currently executing rcu_bh
2098 * read-side critical sections have completed. RCU read-side critical
2099 * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
2100 * and may be nested.
2101 */
2102void synchronize_rcu_bh(void)
2103{
fe15d706
PM
2104 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
2105 !lock_is_held(&rcu_lock_map) &&
2106 !lock_is_held(&rcu_sched_lock_map),
2107 "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
6ebb237b
PM
2108 if (rcu_blocking_is_gp())
2109 return;
2c42818e 2110 wait_rcu_gp(call_rcu_bh);
6ebb237b
PM
2111}
2112EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
2113
3d3b7db0
PM
2114static atomic_t sync_sched_expedited_started = ATOMIC_INIT(0);
2115static atomic_t sync_sched_expedited_done = ATOMIC_INIT(0);
2116
2117static int synchronize_sched_expedited_cpu_stop(void *data)
2118{
2119 /*
2120 * There must be a full memory barrier on each affected CPU
2121 * between the time that try_stop_cpus() is called and the
2122 * time that it returns.
2123 *
2124 * In the current initial implementation of cpu_stop, the
2125 * above condition is already met when the control reaches
2126 * this point and the following smp_mb() is not strictly
2127 * necessary. Do smp_mb() anyway for documentation and
2128 * robustness against future implementation changes.
2129 */
2130 smp_mb(); /* See above comment block. */
2131 return 0;
2132}
2133
236fefaf
PM
2134/**
2135 * synchronize_sched_expedited - Brute-force RCU-sched grace period
2136 *
2137 * Wait for an RCU-sched grace period to elapse, but use a "big hammer"
2138 * approach to force the grace period to end quickly. This consumes
2139 * significant time on all CPUs and is unfriendly to real-time workloads,
2140 * so is thus not recommended for any sort of common-case code. In fact,
2141 * if you are using synchronize_sched_expedited() in a loop, please
2142 * restructure your code to batch your updates, and then use a single
2143 * synchronize_sched() instead.
3d3b7db0 2144 *
236fefaf
PM
2145 * Note that it is illegal to call this function while holding any lock
2146 * that is acquired by a CPU-hotplug notifier. And yes, it is also illegal
2147 * to call this function from a CPU-hotplug notifier. Failing to observe
2148 * these restriction will result in deadlock.
3d3b7db0
PM
2149 *
2150 * This implementation can be thought of as an application of ticket
2151 * locking to RCU, with sync_sched_expedited_started and
2152 * sync_sched_expedited_done taking on the roles of the halves
2153 * of the ticket-lock word. Each task atomically increments
2154 * sync_sched_expedited_started upon entry, snapshotting the old value,
2155 * then attempts to stop all the CPUs. If this succeeds, then each
2156 * CPU will have executed a context switch, resulting in an RCU-sched
2157 * grace period. We are then done, so we use atomic_cmpxchg() to
2158 * update sync_sched_expedited_done to match our snapshot -- but
2159 * only if someone else has not already advanced past our snapshot.
2160 *
2161 * On the other hand, if try_stop_cpus() fails, we check the value
2162 * of sync_sched_expedited_done. If it has advanced past our
2163 * initial snapshot, then someone else must have forced a grace period
2164 * some time after we took our snapshot. In this case, our work is
2165 * done for us, and we can simply return. Otherwise, we try again,
2166 * but keep our initial snapshot for purposes of checking for someone
2167 * doing our work for us.
2168 *
2169 * If we fail too many times in a row, we fall back to synchronize_sched().
2170 */
2171void synchronize_sched_expedited(void)
2172{
2173 int firstsnap, s, snap, trycount = 0;
2174
2175 /* Note that atomic_inc_return() implies full memory barrier. */
2176 firstsnap = snap = atomic_inc_return(&sync_sched_expedited_started);
2177 get_online_cpus();
1cc85961 2178 WARN_ON_ONCE(cpu_is_offline(raw_smp_processor_id()));
3d3b7db0
PM
2179
2180 /*
2181 * Each pass through the following loop attempts to force a
2182 * context switch on each CPU.
2183 */
2184 while (try_stop_cpus(cpu_online_mask,
2185 synchronize_sched_expedited_cpu_stop,
2186 NULL) == -EAGAIN) {
2187 put_online_cpus();
2188
2189 /* No joy, try again later. Or just synchronize_sched(). */
c701d5d9 2190 if (trycount++ < 10) {
3d3b7db0 2191 udelay(trycount * num_online_cpus());
c701d5d9 2192 } else {
3d3b7db0
PM
2193 synchronize_sched();
2194 return;
2195 }
2196
2197 /* Check to see if someone else did our work for us. */
2198 s = atomic_read(&sync_sched_expedited_done);
2199 if (UINT_CMP_GE((unsigned)s, (unsigned)firstsnap)) {
2200 smp_mb(); /* ensure test happens before caller kfree */
2201 return;
2202 }
2203
2204 /*
2205 * Refetching sync_sched_expedited_started allows later
2206 * callers to piggyback on our grace period. We subtract
2207 * 1 to get the same token that the last incrementer got.
2208 * We retry after they started, so our grace period works
2209 * for them, and they started after our first try, so their
2210 * grace period works for us.
2211 */
2212 get_online_cpus();
2213 snap = atomic_read(&sync_sched_expedited_started);
2214 smp_mb(); /* ensure read is before try_stop_cpus(). */
2215 }
2216
2217 /*
2218 * Everyone up to our most recent fetch is covered by our grace
2219 * period. Update the counter, but only if our work is still
2220 * relevant -- which it won't be if someone who started later
2221 * than we did beat us to the punch.
2222 */
2223 do {
2224 s = atomic_read(&sync_sched_expedited_done);
2225 if (UINT_CMP_GE((unsigned)s, (unsigned)snap)) {
2226 smp_mb(); /* ensure test happens before caller kfree */
2227 break;
2228 }
2229 } while (atomic_cmpxchg(&sync_sched_expedited_done, s, snap) != s);
2230
2231 put_online_cpus();
2232}
2233EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
2234
64db4cff
PM
2235/*
2236 * Check to see if there is any immediate RCU-related work to be done
2237 * by the current CPU, for the specified type of RCU, returning 1 if so.
2238 * The checks are in order of increasing expense: checks that can be
2239 * carried out against CPU-local state are performed first. However,
2240 * we must check for CPU stalls first, else we might not get a chance.
2241 */
2242static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
2243{
2f51f988
PM
2244 struct rcu_node *rnp = rdp->mynode;
2245
64db4cff
PM
2246 rdp->n_rcu_pending++;
2247
2248 /* Check for CPU stalls, if enabled. */
2249 check_cpu_stall(rsp, rdp);
2250
2251 /* Is the RCU core waiting for a quiescent state from this CPU? */
5c51dd73
PM
2252 if (rcu_scheduler_fully_active &&
2253 rdp->qs_pending && !rdp->passed_quiesce) {
d21670ac 2254 rdp->n_rp_qs_pending++;
e4cc1f22 2255 } else if (rdp->qs_pending && rdp->passed_quiesce) {
d21670ac 2256 rdp->n_rp_report_qs++;
64db4cff 2257 return 1;
7ba5c840 2258 }
64db4cff
PM
2259
2260 /* Does this CPU have callbacks ready to invoke? */
7ba5c840
PM
2261 if (cpu_has_callbacks_ready_to_invoke(rdp)) {
2262 rdp->n_rp_cb_ready++;
64db4cff 2263 return 1;
7ba5c840 2264 }
64db4cff
PM
2265
2266 /* Has RCU gone idle with this CPU needing another grace period? */
7ba5c840
PM
2267 if (cpu_needs_another_gp(rsp, rdp)) {
2268 rdp->n_rp_cpu_needs_gp++;
64db4cff 2269 return 1;
7ba5c840 2270 }
64db4cff
PM
2271
2272 /* Has another RCU grace period completed? */
2f51f988 2273 if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
7ba5c840 2274 rdp->n_rp_gp_completed++;
64db4cff 2275 return 1;
7ba5c840 2276 }
64db4cff
PM
2277
2278 /* Has a new RCU grace period started? */
2f51f988 2279 if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */
7ba5c840 2280 rdp->n_rp_gp_started++;
64db4cff 2281 return 1;
7ba5c840 2282 }
64db4cff 2283
64db4cff 2284 /* nothing to do */
7ba5c840 2285 rdp->n_rp_need_nothing++;
64db4cff
PM
2286 return 0;
2287}
2288
2289/*
2290 * Check to see if there is any immediate RCU-related work to be done
2291 * by the current CPU, returning 1 if so. This function is part of the
2292 * RCU implementation; it is -not- an exported member of the RCU API.
2293 */
a157229c 2294static int rcu_pending(int cpu)
64db4cff 2295{
6ce75a23
PM
2296 struct rcu_state *rsp;
2297
2298 for_each_rcu_flavor(rsp)
2299 if (__rcu_pending(rsp, per_cpu_ptr(rsp->rda, cpu)))
2300 return 1;
2301 return 0;
64db4cff
PM
2302}
2303
2304/*
2305 * Check to see if any future RCU-related work will need to be done
2306 * by the current CPU, even if none need be done immediately, returning
8bd93a2c 2307 * 1 if so.
64db4cff 2308 */
aea1b35e 2309static int rcu_cpu_has_callbacks(int cpu)
64db4cff 2310{
6ce75a23
PM
2311 struct rcu_state *rsp;
2312
64db4cff 2313 /* RCU callbacks either ready or pending? */
6ce75a23
PM
2314 for_each_rcu_flavor(rsp)
2315 if (per_cpu_ptr(rsp->rda, cpu)->nxtlist)
2316 return 1;
2317 return 0;
64db4cff
PM
2318}
2319
a83eff0a
PM
2320/*
2321 * Helper function for _rcu_barrier() tracing. If tracing is disabled,
2322 * the compiler is expected to optimize this away.
2323 */
2324static void _rcu_barrier_trace(struct rcu_state *rsp, char *s,
2325 int cpu, unsigned long done)
2326{
2327 trace_rcu_barrier(rsp->name, s, cpu,
2328 atomic_read(&rsp->barrier_cpu_count), done);
2329}
2330
b1420f1c
PM
2331/*
2332 * RCU callback function for _rcu_barrier(). If we are last, wake
2333 * up the task executing _rcu_barrier().
2334 */
24ebbca8 2335static void rcu_barrier_callback(struct rcu_head *rhp)
d0ec774c 2336{
24ebbca8
PM
2337 struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
2338 struct rcu_state *rsp = rdp->rsp;
2339
a83eff0a
PM
2340 if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
2341 _rcu_barrier_trace(rsp, "LastCB", -1, rsp->n_barrier_done);
7db74df8 2342 complete(&rsp->barrier_completion);
a83eff0a
PM
2343 } else {
2344 _rcu_barrier_trace(rsp, "CB", -1, rsp->n_barrier_done);
2345 }
d0ec774c
PM
2346}
2347
2348/*
2349 * Called with preemption disabled, and from cross-cpu IRQ context.
2350 */
2351static void rcu_barrier_func(void *type)
2352{
037b64ed 2353 struct rcu_state *rsp = type;
06668efa 2354 struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
d0ec774c 2355
a83eff0a 2356 _rcu_barrier_trace(rsp, "IRQ", -1, rsp->n_barrier_done);
24ebbca8 2357 atomic_inc(&rsp->barrier_cpu_count);
06668efa 2358 rsp->call(&rdp->barrier_head, rcu_barrier_callback);
d0ec774c
PM
2359}
2360
d0ec774c
PM
2361/*
2362 * Orchestrate the specified type of RCU barrier, waiting for all
2363 * RCU callbacks of the specified type to complete.
2364 */
037b64ed 2365static void _rcu_barrier(struct rcu_state *rsp)
d0ec774c 2366{
b1420f1c
PM
2367 int cpu;
2368 unsigned long flags;
2369 struct rcu_data *rdp;
24ebbca8 2370 struct rcu_data rd;
cf3a9c48
PM
2371 unsigned long snap = ACCESS_ONCE(rsp->n_barrier_done);
2372 unsigned long snap_done;
b1420f1c 2373
24ebbca8 2374 init_rcu_head_on_stack(&rd.barrier_head);
a83eff0a 2375 _rcu_barrier_trace(rsp, "Begin", -1, snap);
b1420f1c 2376
e74f4c45 2377 /* Take mutex to serialize concurrent rcu_barrier() requests. */
7be7f0be 2378 mutex_lock(&rsp->barrier_mutex);
b1420f1c 2379
cf3a9c48
PM
2380 /*
2381 * Ensure that all prior references, including to ->n_barrier_done,
2382 * are ordered before the _rcu_barrier() machinery.
2383 */
2384 smp_mb(); /* See above block comment. */
2385
2386 /*
2387 * Recheck ->n_barrier_done to see if others did our work for us.
2388 * This means checking ->n_barrier_done for an even-to-odd-to-even
2389 * transition. The "if" expression below therefore rounds the old
2390 * value up to the next even number and adds two before comparing.
2391 */
2392 snap_done = ACCESS_ONCE(rsp->n_barrier_done);
a83eff0a 2393 _rcu_barrier_trace(rsp, "Check", -1, snap_done);
cf3a9c48 2394 if (ULONG_CMP_GE(snap_done, ((snap + 1) & ~0x1) + 2)) {
a83eff0a 2395 _rcu_barrier_trace(rsp, "EarlyExit", -1, snap_done);
cf3a9c48
PM
2396 smp_mb(); /* caller's subsequent code after above check. */
2397 mutex_unlock(&rsp->barrier_mutex);
2398 return;
2399 }
2400
2401 /*
2402 * Increment ->n_barrier_done to avoid duplicate work. Use
2403 * ACCESS_ONCE() to prevent the compiler from speculating
2404 * the increment to precede the early-exit check.
2405 */
2406 ACCESS_ONCE(rsp->n_barrier_done)++;
2407 WARN_ON_ONCE((rsp->n_barrier_done & 0x1) != 1);
a83eff0a 2408 _rcu_barrier_trace(rsp, "Inc1", -1, rsp->n_barrier_done);
cf3a9c48 2409 smp_mb(); /* Order ->n_barrier_done increment with below mechanism. */
b1420f1c 2410
d0ec774c 2411 /*
b1420f1c
PM
2412 * Initialize the count to one rather than to zero in order to
2413 * avoid a too-soon return to zero in case of a short grace period
2414 * (or preemption of this task). Also flag this task as doing
2415 * an rcu_barrier(). This will prevent anyone else from adopting
2416 * orphaned callbacks, which could cause otherwise failure if a
2417 * CPU went offline and quickly came back online. To see this,
2418 * consider the following sequence of events:
2419 *
2420 * 1. We cause CPU 0 to post an rcu_barrier_callback() callback.
2421 * 2. CPU 1 goes offline, orphaning its callbacks.
2422 * 3. CPU 0 adopts CPU 1's orphaned callbacks.
2423 * 4. CPU 1 comes back online.
2424 * 5. We cause CPU 1 to post an rcu_barrier_callback() callback.
2425 * 6. Both rcu_barrier_callback() callbacks are invoked, awakening
2426 * us -- but before CPU 1's orphaned callbacks are invoked!!!
d0ec774c 2427 */
7db74df8 2428 init_completion(&rsp->barrier_completion);
24ebbca8 2429 atomic_set(&rsp->barrier_cpu_count, 1);
b1420f1c
PM
2430 raw_spin_lock_irqsave(&rsp->onofflock, flags);
2431 rsp->rcu_barrier_in_progress = current;
2432 raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
2433
2434 /*
2435 * Force every CPU with callbacks to register a new callback
2436 * that will tell us when all the preceding callbacks have
2437 * been invoked. If an offline CPU has callbacks, wait for
2438 * it to either come back online or to finish orphaning those
2439 * callbacks.
2440 */
2441 for_each_possible_cpu(cpu) {
2442 preempt_disable();
2443 rdp = per_cpu_ptr(rsp->rda, cpu);
2444 if (cpu_is_offline(cpu)) {
a83eff0a
PM
2445 _rcu_barrier_trace(rsp, "Offline", cpu,
2446 rsp->n_barrier_done);
b1420f1c
PM
2447 preempt_enable();
2448 while (cpu_is_offline(cpu) && ACCESS_ONCE(rdp->qlen))
2449 schedule_timeout_interruptible(1);
2450 } else if (ACCESS_ONCE(rdp->qlen)) {
a83eff0a
PM
2451 _rcu_barrier_trace(rsp, "OnlineQ", cpu,
2452 rsp->n_barrier_done);
037b64ed 2453 smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
b1420f1c
PM
2454 preempt_enable();
2455 } else {
a83eff0a
PM
2456 _rcu_barrier_trace(rsp, "OnlineNQ", cpu,
2457 rsp->n_barrier_done);
b1420f1c
PM
2458 preempt_enable();
2459 }
2460 }
2461
2462 /*
2463 * Now that all online CPUs have rcu_barrier_callback() callbacks
2464 * posted, we can adopt all of the orphaned callbacks and place
2465 * an rcu_barrier_callback() callback after them. When that is done,
2466 * we are guaranteed to have an rcu_barrier_callback() callback
2467 * following every callback that could possibly have been
2468 * registered before _rcu_barrier() was called.
2469 */
2470 raw_spin_lock_irqsave(&rsp->onofflock, flags);
2471 rcu_adopt_orphan_cbs(rsp);
2472 rsp->rcu_barrier_in_progress = NULL;
2473 raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
24ebbca8 2474 atomic_inc(&rsp->barrier_cpu_count);
b1420f1c 2475 smp_mb__after_atomic_inc(); /* Ensure atomic_inc() before callback. */
24ebbca8
PM
2476 rd.rsp = rsp;
2477 rsp->call(&rd.barrier_head, rcu_barrier_callback);
b1420f1c
PM
2478
2479 /*
2480 * Now that we have an rcu_barrier_callback() callback on each
2481 * CPU, and thus each counted, remove the initial count.
2482 */
24ebbca8 2483 if (atomic_dec_and_test(&rsp->barrier_cpu_count))
7db74df8 2484 complete(&rsp->barrier_completion);
b1420f1c 2485
cf3a9c48
PM
2486 /* Increment ->n_barrier_done to prevent duplicate work. */
2487 smp_mb(); /* Keep increment after above mechanism. */
2488 ACCESS_ONCE(rsp->n_barrier_done)++;
2489 WARN_ON_ONCE((rsp->n_barrier_done & 0x1) != 0);
a83eff0a 2490 _rcu_barrier_trace(rsp, "Inc2", -1, rsp->n_barrier_done);
cf3a9c48
PM
2491 smp_mb(); /* Keep increment before caller's subsequent code. */
2492
b1420f1c 2493 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
7db74df8 2494 wait_for_completion(&rsp->barrier_completion);
b1420f1c
PM
2495
2496 /* Other rcu_barrier() invocations can now safely proceed. */
7be7f0be 2497 mutex_unlock(&rsp->barrier_mutex);
b1420f1c 2498
24ebbca8 2499 destroy_rcu_head_on_stack(&rd.barrier_head);
d0ec774c 2500}
d0ec774c
PM
2501
2502/**
2503 * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
2504 */
2505void rcu_barrier_bh(void)
2506{
037b64ed 2507 _rcu_barrier(&rcu_bh_state);
d0ec774c
PM
2508}
2509EXPORT_SYMBOL_GPL(rcu_barrier_bh);
2510
2511/**
2512 * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
2513 */
2514void rcu_barrier_sched(void)
2515{
037b64ed 2516 _rcu_barrier(&rcu_sched_state);
d0ec774c
PM
2517}
2518EXPORT_SYMBOL_GPL(rcu_barrier_sched);
2519
64db4cff 2520/*
27569620 2521 * Do boot-time initialization of a CPU's per-CPU RCU data.
64db4cff 2522 */
27569620
PM
2523static void __init
2524rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
64db4cff
PM
2525{
2526 unsigned long flags;
394f99a9 2527 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
27569620
PM
2528 struct rcu_node *rnp = rcu_get_root(rsp);
2529
2530 /* Set up local state, ensuring consistent view of global state. */
1304afb2 2531 raw_spin_lock_irqsave(&rnp->lock, flags);
27569620 2532 rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
3f5d3ea6 2533 init_callback_list(rdp);
486e2593 2534 rdp->qlen_lazy = 0;
1d1fb395 2535 ACCESS_ONCE(rdp->qlen) = 0;
27569620 2536 rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
29e37d81 2537 WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_EXIT_IDLE);
9b2e4f18 2538 WARN_ON_ONCE(atomic_read(&rdp->dynticks->dynticks) != 1);
27569620 2539 rdp->cpu = cpu;
d4c08f2a 2540 rdp->rsp = rsp;
1304afb2 2541 raw_spin_unlock_irqrestore(&rnp->lock, flags);
27569620
PM
2542}
2543
2544/*
2545 * Initialize a CPU's per-CPU RCU data. Note that only one online or
2546 * offline event can be happening at a given time. Note also that we
2547 * can accept some slop in the rsp->completed access due to the fact
2548 * that this CPU cannot possibly have any RCU callbacks in flight yet.
64db4cff 2549 */
e4fa4c97 2550static void __cpuinit
6cc68793 2551rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptible)
64db4cff
PM
2552{
2553 unsigned long flags;
64db4cff 2554 unsigned long mask;
394f99a9 2555 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
64db4cff
PM
2556 struct rcu_node *rnp = rcu_get_root(rsp);
2557
2558 /* Set up local state, ensuring consistent view of global state. */
1304afb2 2559 raw_spin_lock_irqsave(&rnp->lock, flags);
64db4cff 2560 rdp->beenonline = 1; /* We have now been online. */
6cc68793 2561 rdp->preemptible = preemptible;
37c72e56
PM
2562 rdp->qlen_last_fqs_check = 0;
2563 rdp->n_force_qs_snap = rsp->n_force_qs;
64db4cff 2564 rdp->blimit = blimit;
29e37d81 2565 rdp->dynticks->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
c92b131b
PM
2566 atomic_set(&rdp->dynticks->dynticks,
2567 (atomic_read(&rdp->dynticks->dynticks) & ~0x1) + 1);
7cb92499 2568 rcu_prepare_for_idle_init(cpu);
1304afb2 2569 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
64db4cff
PM
2570
2571 /*
2572 * A new grace period might start here. If so, we won't be part
2573 * of it, but that is OK, as we are currently in a quiescent state.
2574 */
2575
2576 /* Exclude any attempts to start a new GP on large systems. */
1304afb2 2577 raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */
64db4cff
PM
2578
2579 /* Add CPU to rcu_node bitmasks. */
2580 rnp = rdp->mynode;
2581 mask = rdp->grpmask;
2582 do {
2583 /* Exclude any attempts to start a new GP on small systems. */
1304afb2 2584 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
64db4cff
PM
2585 rnp->qsmaskinit |= mask;
2586 mask = rnp->grpmask;
d09b62df 2587 if (rnp == rdp->mynode) {
06ae115a
PM
2588 /*
2589 * If there is a grace period in progress, we will
2590 * set up to wait for it next time we run the
2591 * RCU core code.
2592 */
2593 rdp->gpnum = rnp->completed;
d09b62df 2594 rdp->completed = rnp->completed;
06ae115a
PM
2595 rdp->passed_quiesce = 0;
2596 rdp->qs_pending = 0;
e4cc1f22 2597 rdp->passed_quiesce_gpnum = rnp->gpnum - 1;
d4c08f2a 2598 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpuonl");
d09b62df 2599 }
1304afb2 2600 raw_spin_unlock(&rnp->lock); /* irqs already disabled. */
64db4cff
PM
2601 rnp = rnp->parent;
2602 } while (rnp != NULL && !(rnp->qsmaskinit & mask));
2603
1304afb2 2604 raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
64db4cff
PM
2605}
2606
d72bce0e 2607static void __cpuinit rcu_prepare_cpu(int cpu)
64db4cff 2608{
6ce75a23
PM
2609 struct rcu_state *rsp;
2610
2611 for_each_rcu_flavor(rsp)
2612 rcu_init_percpu_data(cpu, rsp,
2613 strcmp(rsp->name, "rcu_preempt") == 0);
64db4cff
PM
2614}
2615
2616/*
f41d911f 2617 * Handle CPU online/offline notification events.
64db4cff 2618 */
9f680ab4
PM
2619static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
2620 unsigned long action, void *hcpu)
64db4cff
PM
2621{
2622 long cpu = (long)hcpu;
27f4d280 2623 struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
a26ac245 2624 struct rcu_node *rnp = rdp->mynode;
6ce75a23 2625 struct rcu_state *rsp;
64db4cff 2626
300df91c 2627 trace_rcu_utilization("Start CPU hotplug");
64db4cff
PM
2628 switch (action) {
2629 case CPU_UP_PREPARE:
2630 case CPU_UP_PREPARE_FROZEN:
d72bce0e
PZ
2631 rcu_prepare_cpu(cpu);
2632 rcu_prepare_kthreads(cpu);
a26ac245
PM
2633 break;
2634 case CPU_ONLINE:
0f962a5e
PM
2635 case CPU_DOWN_FAILED:
2636 rcu_node_kthread_setaffinity(rnp, -1);
e3995a25 2637 rcu_cpu_kthread_setrt(cpu, 1);
0f962a5e
PM
2638 break;
2639 case CPU_DOWN_PREPARE:
2640 rcu_node_kthread_setaffinity(rnp, cpu);
e3995a25 2641 rcu_cpu_kthread_setrt(cpu, 0);
64db4cff 2642 break;
d0ec774c
PM
2643 case CPU_DYING:
2644 case CPU_DYING_FROZEN:
2645 /*
2d999e03
PM
2646 * The whole machine is "stopped" except this CPU, so we can
2647 * touch any data without introducing corruption. We send the
2648 * dying CPU's callbacks to an arbitrarily chosen online CPU.
d0ec774c 2649 */
6ce75a23
PM
2650 for_each_rcu_flavor(rsp)
2651 rcu_cleanup_dying_cpu(rsp);
7cb92499 2652 rcu_cleanup_after_idle(cpu);
d0ec774c 2653 break;
64db4cff
PM
2654 case CPU_DEAD:
2655 case CPU_DEAD_FROZEN:
2656 case CPU_UP_CANCELED:
2657 case CPU_UP_CANCELED_FROZEN:
6ce75a23
PM
2658 for_each_rcu_flavor(rsp)
2659 rcu_cleanup_dead_cpu(cpu, rsp);
64db4cff
PM
2660 break;
2661 default:
2662 break;
2663 }
300df91c 2664 trace_rcu_utilization("End CPU hotplug");
64db4cff
PM
2665 return NOTIFY_OK;
2666}
2667
b3dbec76
PM
2668/*
2669 * Spawn the kthread that handles this RCU flavor's grace periods.
2670 */
2671static int __init rcu_spawn_gp_kthread(void)
2672{
2673 unsigned long flags;
2674 struct rcu_node *rnp;
2675 struct rcu_state *rsp;
2676 struct task_struct *t;
2677
2678 for_each_rcu_flavor(rsp) {
2679 t = kthread_run(rcu_gp_kthread, rsp, rsp->name);
2680 BUG_ON(IS_ERR(t));
2681 rnp = rcu_get_root(rsp);
2682 raw_spin_lock_irqsave(&rnp->lock, flags);
2683 rsp->gp_kthread = t;
2684 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2685 }
2686 return 0;
2687}
2688early_initcall(rcu_spawn_gp_kthread);
2689
bbad9379
PM
2690/*
2691 * This function is invoked towards the end of the scheduler's initialization
2692 * process. Before this is called, the idle task might contain
2693 * RCU read-side critical sections (during which time, this idle
2694 * task is booting the system). After this function is called, the
2695 * idle tasks are prohibited from containing RCU read-side critical
2696 * sections. This function also enables RCU lockdep checking.
2697 */
2698void rcu_scheduler_starting(void)
2699{
2700 WARN_ON(num_online_cpus() != 1);
2701 WARN_ON(nr_context_switches() > 0);
2702 rcu_scheduler_active = 1;
2703}
2704
64db4cff
PM
2705/*
2706 * Compute the per-level fanout, either using the exact fanout specified
2707 * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
2708 */
2709#ifdef CONFIG_RCU_FANOUT_EXACT
2710static void __init rcu_init_levelspread(struct rcu_state *rsp)
2711{
2712 int i;
2713
f885b7f2 2714 for (i = rcu_num_lvls - 1; i > 0; i--)
64db4cff 2715 rsp->levelspread[i] = CONFIG_RCU_FANOUT;
f885b7f2 2716 rsp->levelspread[0] = rcu_fanout_leaf;
64db4cff
PM
2717}
2718#else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
2719static void __init rcu_init_levelspread(struct rcu_state *rsp)
2720{
2721 int ccur;
2722 int cprv;
2723 int i;
2724
2725 cprv = NR_CPUS;
f885b7f2 2726 for (i = rcu_num_lvls - 1; i >= 0; i--) {
64db4cff
PM
2727 ccur = rsp->levelcnt[i];
2728 rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
2729 cprv = ccur;
2730 }
2731}
2732#endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
2733
2734/*
2735 * Helper function for rcu_init() that initializes one rcu_state structure.
2736 */
394f99a9
LJ
2737static void __init rcu_init_one(struct rcu_state *rsp,
2738 struct rcu_data __percpu *rda)
64db4cff 2739{
394f2769
PM
2740 static char *buf[] = { "rcu_node_0",
2741 "rcu_node_1",
2742 "rcu_node_2",
2743 "rcu_node_3" }; /* Match MAX_RCU_LVLS */
2744 static char *fqs[] = { "rcu_node_fqs_0",
2745 "rcu_node_fqs_1",
2746 "rcu_node_fqs_2",
2747 "rcu_node_fqs_3" }; /* Match MAX_RCU_LVLS */
64db4cff
PM
2748 int cpustride = 1;
2749 int i;
2750 int j;
2751 struct rcu_node *rnp;
2752
b6407e86
PM
2753 BUILD_BUG_ON(MAX_RCU_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
2754
64db4cff
PM
2755 /* Initialize the level-tracking arrays. */
2756
f885b7f2
PM
2757 for (i = 0; i < rcu_num_lvls; i++)
2758 rsp->levelcnt[i] = num_rcu_lvl[i];
2759 for (i = 1; i < rcu_num_lvls; i++)
64db4cff
PM
2760 rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
2761 rcu_init_levelspread(rsp);
2762
2763 /* Initialize the elements themselves, starting from the leaves. */
2764
f885b7f2 2765 for (i = rcu_num_lvls - 1; i >= 0; i--) {
64db4cff
PM
2766 cpustride *= rsp->levelspread[i];
2767 rnp = rsp->level[i];
2768 for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
1304afb2 2769 raw_spin_lock_init(&rnp->lock);
b6407e86
PM
2770 lockdep_set_class_and_name(&rnp->lock,
2771 &rcu_node_class[i], buf[i]);
394f2769
PM
2772 raw_spin_lock_init(&rnp->fqslock);
2773 lockdep_set_class_and_name(&rnp->fqslock,
2774 &rcu_fqs_class[i], fqs[i]);
f41d911f 2775 rnp->gpnum = 0;
64db4cff
PM
2776 rnp->qsmask = 0;
2777 rnp->qsmaskinit = 0;
2778 rnp->grplo = j * cpustride;
2779 rnp->grphi = (j + 1) * cpustride - 1;
2780 if (rnp->grphi >= NR_CPUS)
2781 rnp->grphi = NR_CPUS - 1;
2782 if (i == 0) {
2783 rnp->grpnum = 0;
2784 rnp->grpmask = 0;
2785 rnp->parent = NULL;
2786 } else {
2787 rnp->grpnum = j % rsp->levelspread[i - 1];
2788 rnp->grpmask = 1UL << rnp->grpnum;
2789 rnp->parent = rsp->level[i - 1] +
2790 j / rsp->levelspread[i - 1];
2791 }
2792 rnp->level = i;
12f5f524 2793 INIT_LIST_HEAD(&rnp->blkd_tasks);
64db4cff
PM
2794 }
2795 }
0c34029a 2796
394f99a9 2797 rsp->rda = rda;
b3dbec76 2798 init_waitqueue_head(&rsp->gp_wq);
f885b7f2 2799 rnp = rsp->level[rcu_num_lvls - 1];
0c34029a 2800 for_each_possible_cpu(i) {
4a90a068 2801 while (i > rnp->grphi)
0c34029a 2802 rnp++;
394f99a9 2803 per_cpu_ptr(rsp->rda, i)->mynode = rnp;
0c34029a
LJ
2804 rcu_boot_init_percpu_data(i, rsp);
2805 }
6ce75a23 2806 list_add(&rsp->flavors, &rcu_struct_flavors);
64db4cff
PM
2807}
2808
f885b7f2
PM
2809/*
2810 * Compute the rcu_node tree geometry from kernel parameters. This cannot
2811 * replace the definitions in rcutree.h because those are needed to size
2812 * the ->node array in the rcu_state structure.
2813 */
2814static void __init rcu_init_geometry(void)
2815{
2816 int i;
2817 int j;
cca6f393 2818 int n = nr_cpu_ids;
f885b7f2
PM
2819 int rcu_capacity[MAX_RCU_LVLS + 1];
2820
2821 /* If the compile-time values are accurate, just leave. */
2822 if (rcu_fanout_leaf == CONFIG_RCU_FANOUT_LEAF)
2823 return;
2824
2825 /*
2826 * Compute number of nodes that can be handled an rcu_node tree
2827 * with the given number of levels. Setting rcu_capacity[0] makes
2828 * some of the arithmetic easier.
2829 */
2830 rcu_capacity[0] = 1;
2831 rcu_capacity[1] = rcu_fanout_leaf;
2832 for (i = 2; i <= MAX_RCU_LVLS; i++)
2833 rcu_capacity[i] = rcu_capacity[i - 1] * CONFIG_RCU_FANOUT;
2834
2835 /*
2836 * The boot-time rcu_fanout_leaf parameter is only permitted
2837 * to increase the leaf-level fanout, not decrease it. Of course,
2838 * the leaf-level fanout cannot exceed the number of bits in
2839 * the rcu_node masks. Finally, the tree must be able to accommodate
2840 * the configured number of CPUs. Complain and fall back to the
2841 * compile-time values if these limits are exceeded.
2842 */
2843 if (rcu_fanout_leaf < CONFIG_RCU_FANOUT_LEAF ||
2844 rcu_fanout_leaf > sizeof(unsigned long) * 8 ||
2845 n > rcu_capacity[MAX_RCU_LVLS]) {
2846 WARN_ON(1);
2847 return;
2848 }
2849
2850 /* Calculate the number of rcu_nodes at each level of the tree. */
2851 for (i = 1; i <= MAX_RCU_LVLS; i++)
2852 if (n <= rcu_capacity[i]) {
2853 for (j = 0; j <= i; j++)
2854 num_rcu_lvl[j] =
2855 DIV_ROUND_UP(n, rcu_capacity[i - j]);
2856 rcu_num_lvls = i;
2857 for (j = i + 1; j <= MAX_RCU_LVLS; j++)
2858 num_rcu_lvl[j] = 0;
2859 break;
2860 }
2861
2862 /* Calculate the total number of rcu_node structures. */
2863 rcu_num_nodes = 0;
2864 for (i = 0; i <= MAX_RCU_LVLS; i++)
2865 rcu_num_nodes += num_rcu_lvl[i];
2866 rcu_num_nodes -= n;
2867}
2868
9f680ab4 2869void __init rcu_init(void)
64db4cff 2870{
017c4261 2871 int cpu;
9f680ab4 2872
f41d911f 2873 rcu_bootup_announce();
f885b7f2 2874 rcu_init_geometry();
394f99a9
LJ
2875 rcu_init_one(&rcu_sched_state, &rcu_sched_data);
2876 rcu_init_one(&rcu_bh_state, &rcu_bh_data);
f41d911f 2877 __rcu_init_preempt();
09223371 2878 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
9f680ab4
PM
2879
2880 /*
2881 * We don't need protection against CPU-hotplug here because
2882 * this is called early in boot, before either interrupts
2883 * or the scheduler are operational.
2884 */
2885 cpu_notifier(rcu_cpu_notify, 0);
017c4261
PM
2886 for_each_online_cpu(cpu)
2887 rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
c68de209 2888 check_cpu_stall_init();
64db4cff
PM
2889}
2890
1eba8f84 2891#include "rcutree_plugin.h"
This page took 0.439579 seconds and 5 git commands to generate.