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