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