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