rcu: Abstract extended quiescent state determination
[linux-2.6-block.git] / kernel / rcu / tree.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
87de1cfd
PM
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
64db4cff
PM
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>
af658dca 56#include <linux/trace_events.h>
d1d74d14 57#include <linux/suspend.h>
64db4cff 58
4102adab 59#include "tree.h"
29c00b4a 60#include "rcu.h"
9f77da9f 61
4102adab
PM
62#ifdef MODULE_PARAM_PREFIX
63#undef MODULE_PARAM_PREFIX
64#endif
65#define MODULE_PARAM_PREFIX "rcutree."
66
64db4cff
PM
67/* Data structures. */
68
f7f7bac9
SRRH
69/*
70 * In order to export the rcu_state name to the tracing tools, it
71 * needs to be added in the __tracepoint_string section.
72 * This requires defining a separate variable tp_<sname>_varname
73 * that points to the string being used, and this will allow
74 * the tracing userspace tools to be able to decipher the string
75 * address to the matching string.
76 */
a8a29b3b
AB
77#ifdef CONFIG_TRACING
78# define DEFINE_RCU_TPS(sname) \
f7f7bac9 79static char sname##_varname[] = #sname; \
a8a29b3b
AB
80static const char *tp_##sname##_varname __used __tracepoint_string = sname##_varname;
81# define RCU_STATE_NAME(sname) sname##_varname
82#else
83# define DEFINE_RCU_TPS(sname)
84# define RCU_STATE_NAME(sname) __stringify(sname)
85#endif
86
87#define RCU_STATE_INITIALIZER(sname, sabbr, cr) \
88DEFINE_RCU_TPS(sname) \
c92fb057 89static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, sname##_data); \
a41bfeb2 90struct rcu_state sname##_state = { \
6c90cc7b 91 .level = { &sname##_state.node[0] }, \
2723249a 92 .rda = &sname##_data, \
037b64ed 93 .call = cr, \
77f81fe0 94 .gp_state = RCU_GP_IDLE, \
42c3533e
PM
95 .gpnum = 0UL - 300UL, \
96 .completed = 0UL - 300UL, \
7b2e6011 97 .orphan_lock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.orphan_lock), \
6c90cc7b
PM
98 .orphan_nxttail = &sname##_state.orphan_nxtlist, \
99 .orphan_donetail = &sname##_state.orphan_donelist, \
7be7f0be 100 .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
a8a29b3b 101 .name = RCU_STATE_NAME(sname), \
a4889858 102 .abbr = sabbr, \
f6a12f34 103 .exp_mutex = __MUTEX_INITIALIZER(sname##_state.exp_mutex), \
3b5f668e 104 .exp_wake_mutex = __MUTEX_INITIALIZER(sname##_state.exp_wake_mutex), \
2723249a 105}
64db4cff 106
a41bfeb2
SRRH
107RCU_STATE_INITIALIZER(rcu_sched, 's', call_rcu_sched);
108RCU_STATE_INITIALIZER(rcu_bh, 'b', call_rcu_bh);
b1f77b05 109
b28a7c01 110static struct rcu_state *const rcu_state_p;
6ce75a23 111LIST_HEAD(rcu_struct_flavors);
27f4d280 112
a3dc2948
PM
113/* Dump rcu_node combining tree at boot to verify correct setup. */
114static bool dump_tree;
115module_param(dump_tree, bool, 0444);
7fa27001
PM
116/* Control rcu_node-tree auto-balancing at boot time. */
117static bool rcu_fanout_exact;
118module_param(rcu_fanout_exact, bool, 0444);
47d631af
PM
119/* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
120static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
7e5c2dfb 121module_param(rcu_fanout_leaf, int, 0444);
f885b7f2 122int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
cb007102
AG
123/* Number of rcu_nodes at specified level. */
124static int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
f885b7f2 125int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
088e9d25
DBO
126/* panic() on RCU Stall sysctl. */
127int sysctl_panic_on_rcu_stall __read_mostly;
f885b7f2 128
b0d30417 129/*
52d7e48b
PM
130 * The rcu_scheduler_active variable is initialized to the value
131 * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
132 * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
133 * RCU can assume that there is but one task, allowing RCU to (for example)
0d95092c 134 * optimize synchronize_rcu() to a simple barrier(). When this variable
52d7e48b
PM
135 * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
136 * to detect real grace periods. This variable is also used to suppress
137 * boot-time false positives from lockdep-RCU error checking. Finally, it
138 * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
139 * is fully initialized, including all of its kthreads having been spawned.
b0d30417 140 */
bbad9379
PM
141int rcu_scheduler_active __read_mostly;
142EXPORT_SYMBOL_GPL(rcu_scheduler_active);
143
b0d30417
PM
144/*
145 * The rcu_scheduler_fully_active variable transitions from zero to one
146 * during the early_initcall() processing, which is after the scheduler
147 * is capable of creating new tasks. So RCU processing (for example,
148 * creating tasks for RCU priority boosting) must be delayed until after
149 * rcu_scheduler_fully_active transitions from zero to one. We also
150 * currently delay invocation of any RCU callbacks until after this point.
151 *
152 * It might later prove better for people registering RCU callbacks during
153 * early boot to take responsibility for these callbacks, but one step at
154 * a time.
155 */
156static int rcu_scheduler_fully_active __read_mostly;
157
0aa04b05
PM
158static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
159static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
5d01bbd1 160static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
a46e0899
PM
161static void invoke_rcu_core(void);
162static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
6587a23b
PM
163static void rcu_report_exp_rdp(struct rcu_state *rsp,
164 struct rcu_data *rdp, bool wake);
3549c2bc 165static void sync_sched_exp_online_cleanup(int cpu);
a26ac245 166
a94844b2 167/* rcuc/rcub kthread realtime priority */
26730f55 168#ifdef CONFIG_RCU_KTHREAD_PRIO
a94844b2 169static int kthread_prio = CONFIG_RCU_KTHREAD_PRIO;
26730f55
PM
170#else /* #ifdef CONFIG_RCU_KTHREAD_PRIO */
171static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
172#endif /* #else #ifdef CONFIG_RCU_KTHREAD_PRIO */
a94844b2
PM
173module_param(kthread_prio, int, 0644);
174
8d7dc928 175/* Delay in jiffies for grace-period initialization delays, debug only. */
0f41c0dd
PM
176
177#ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT
178static int gp_preinit_delay = CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT_DELAY;
179module_param(gp_preinit_delay, int, 0644);
180#else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
181static const int gp_preinit_delay;
182#endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
183
8d7dc928
PM
184#ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT
185static int gp_init_delay = CONFIG_RCU_TORTURE_TEST_SLOW_INIT_DELAY;
37745d28 186module_param(gp_init_delay, int, 0644);
8d7dc928
PM
187#else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
188static const int gp_init_delay;
189#endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
eab128e8 190
0f41c0dd
PM
191#ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP
192static int gp_cleanup_delay = CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP_DELAY;
193module_param(gp_cleanup_delay, int, 0644);
194#else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
195static const int gp_cleanup_delay;
196#endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
197
eab128e8
PM
198/*
199 * Number of grace periods between delays, normalized by the duration of
200 * the delay. The longer the the delay, the more the grace periods between
201 * each delay. The reason for this normalization is that it means that,
202 * for non-zero delays, the overall slowdown of grace periods is constant
203 * regardless of the duration of the delay. This arrangement balances
204 * the need for long delays to increase some race probabilities with the
205 * need for fast grace periods to increase other race probabilities.
206 */
207#define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
37745d28 208
4a298656
PM
209/*
210 * Track the rcutorture test sequence number and the update version
211 * number within a given test. The rcutorture_testseq is incremented
212 * on every rcutorture module load and unload, so has an odd value
213 * when a test is running. The rcutorture_vernum is set to zero
214 * when rcutorture starts and is incremented on each rcutorture update.
215 * These variables enable correlating rcutorture output with the
216 * RCU tracing information.
217 */
218unsigned long rcutorture_testseq;
219unsigned long rcutorture_vernum;
220
0aa04b05
PM
221/*
222 * Compute the mask of online CPUs for the specified rcu_node structure.
223 * This will not be stable unless the rcu_node structure's ->lock is
224 * held, but the bit corresponding to the current CPU will be stable
225 * in most contexts.
226 */
227unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
228{
7d0ae808 229 return READ_ONCE(rnp->qsmaskinitnext);
0aa04b05
PM
230}
231
fc2219d4 232/*
7d0ae808 233 * Return true if an RCU grace period is in progress. The READ_ONCE()s
fc2219d4
PM
234 * permit this function to be invoked without holding the root rcu_node
235 * structure's ->lock, but of course results can be subject to change.
236 */
237static int rcu_gp_in_progress(struct rcu_state *rsp)
238{
7d0ae808 239 return READ_ONCE(rsp->completed) != READ_ONCE(rsp->gpnum);
fc2219d4
PM
240}
241
b1f77b05 242/*
d6714c22 243 * Note a quiescent state. Because we do not need to know
b1f77b05 244 * how many quiescent states passed, just if there was at least
d6714c22 245 * one since the start of the grace period, this just sets a flag.
e4cc1f22 246 * The caller must have disabled preemption.
b1f77b05 247 */
284a8c93 248void rcu_sched_qs(void)
b1f77b05 249{
fecbf6f0
PM
250 if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.s))
251 return;
252 trace_rcu_grace_period(TPS("rcu_sched"),
253 __this_cpu_read(rcu_sched_data.gpnum),
254 TPS("cpuqs"));
255 __this_cpu_write(rcu_sched_data.cpu_no_qs.b.norm, false);
256 if (!__this_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))
257 return;
46a5d164
PM
258 __this_cpu_write(rcu_sched_data.cpu_no_qs.b.exp, false);
259 rcu_report_exp_rdp(&rcu_sched_state,
260 this_cpu_ptr(&rcu_sched_data), true);
b1f77b05
IM
261}
262
284a8c93 263void rcu_bh_qs(void)
b1f77b05 264{
5b74c458 265 if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
284a8c93
PM
266 trace_rcu_grace_period(TPS("rcu_bh"),
267 __this_cpu_read(rcu_bh_data.gpnum),
268 TPS("cpuqs"));
5b74c458 269 __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
284a8c93 270 }
b1f77b05 271}
64db4cff 272
4a81e832
PM
273static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
274
275static DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
276 .dynticks_nesting = DYNTICK_TASK_EXIT_IDLE,
277 .dynticks = ATOMIC_INIT(1),
278#ifdef CONFIG_NO_HZ_FULL_SYSIDLE
279 .dynticks_idle_nesting = DYNTICK_TASK_NEST_VALUE,
280 .dynticks_idle = ATOMIC_INIT(1),
281#endif /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
282};
283
2625d469
PM
284/*
285 * Record entry into an extended quiescent state. This is only to be
286 * called when not already in an extended quiescent state.
287 */
288static void rcu_dynticks_eqs_enter(void)
289{
290 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
291 int special;
292
293 /*
294 * CPUs seeing atomic_inc_return() must see prior RCU read-side
295 * critical sections, and we also must force ordering with the
296 * next idle sojourn.
297 */
298 special = atomic_inc_return(&rdtp->dynticks);
299 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && special & 0x1);
300}
301
302/*
303 * Record exit from an extended quiescent state. This is only to be
304 * called from an extended quiescent state.
305 */
306static void rcu_dynticks_eqs_exit(void)
307{
308 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
309 int special;
310
311 /*
312 * CPUs seeing atomic_inc_return() must see prior idle sojourns,
313 * and we also must force ordering with the next RCU read-side
314 * critical section.
315 */
316 special = atomic_inc_return(&rdtp->dynticks);
317 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !(special & 0x1));
318}
319
320/*
321 * Reset the current CPU's ->dynticks counter to indicate that the
322 * newly onlined CPU is no longer in an extended quiescent state.
323 * This will either leave the counter unchanged, or increment it
324 * to the next non-quiescent value.
325 *
326 * The non-atomic test/increment sequence works because the upper bits
327 * of the ->dynticks counter are manipulated only by the corresponding CPU,
328 * or when the corresponding CPU is offline.
329 */
330static void rcu_dynticks_eqs_online(void)
331{
332 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
333
334 if (atomic_read(&rdtp->dynticks) & 0x1)
335 return;
336 atomic_add(0x1, &rdtp->dynticks);
337}
338
02a5c550
PM
339/*
340 * Is the current CPU in an extended quiescent state?
341 *
342 * No ordering, as we are sampling CPU-local information.
343 */
344bool rcu_dynticks_curr_cpu_in_eqs(void)
345{
346 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
347
348 return !(atomic_read(&rdtp->dynticks) & 0x1);
349}
350
8b2f63ab
PM
351/*
352 * Snapshot the ->dynticks counter with full ordering so as to allow
353 * stable comparison of this counter with past and future snapshots.
354 */
02a5c550 355int rcu_dynticks_snap(struct rcu_dynticks *rdtp)
8b2f63ab
PM
356{
357 int snap = atomic_add_return(0, &rdtp->dynticks);
358
359 return snap;
360}
361
02a5c550
PM
362/*
363 * Return true if the snapshot returned from rcu_dynticks_snap()
364 * indicates that RCU is in an extended quiescent state.
365 */
366static bool rcu_dynticks_in_eqs(int snap)
367{
368 return !(snap & 0x1);
369}
370
371/*
372 * Return true if the CPU corresponding to the specified rcu_dynticks
373 * structure has spent some time in an extended quiescent state since
374 * rcu_dynticks_snap() returned the specified snapshot.
375 */
376static bool rcu_dynticks_in_eqs_since(struct rcu_dynticks *rdtp, int snap)
377{
378 return snap != rcu_dynticks_snap(rdtp);
379}
380
6563de9d
PM
381/*
382 * Do a double-increment of the ->dynticks counter to emulate a
383 * momentary idle-CPU quiescent state.
384 */
385static void rcu_dynticks_momentary_idle(void)
386{
387 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
388 int special = atomic_add_return(2, &rdtp->dynticks);
389
390 /* It is illegal to call this from idle state. */
391 WARN_ON_ONCE(!(special & 0x1));
392}
393
5cd37193
PM
394DEFINE_PER_CPU_SHARED_ALIGNED(unsigned long, rcu_qs_ctr);
395EXPORT_PER_CPU_SYMBOL_GPL(rcu_qs_ctr);
396
4a81e832
PM
397/*
398 * Let the RCU core know that this CPU has gone through the scheduler,
399 * which is a quiescent state. This is called when the need for a
400 * quiescent state is urgent, so we burn an atomic operation and full
401 * memory barriers to let the RCU core know about it, regardless of what
402 * this CPU might (or might not) do in the near future.
403 *
404 * We inform the RCU core by emulating a zero-duration dyntick-idle
405 * period, which we in turn do by incrementing the ->dynticks counter
406 * by two.
46a5d164
PM
407 *
408 * The caller must have disabled interrupts.
4a81e832
PM
409 */
410static void rcu_momentary_dyntick_idle(void)
411{
4a81e832 412 struct rcu_data *rdp;
4a81e832
PM
413 int resched_mask;
414 struct rcu_state *rsp;
415
4a81e832
PM
416 /*
417 * Yes, we can lose flag-setting operations. This is OK, because
418 * the flag will be set again after some delay.
419 */
420 resched_mask = raw_cpu_read(rcu_sched_qs_mask);
421 raw_cpu_write(rcu_sched_qs_mask, 0);
422
423 /* Find the flavor that needs a quiescent state. */
424 for_each_rcu_flavor(rsp) {
425 rdp = raw_cpu_ptr(rsp->rda);
426 if (!(resched_mask & rsp->flavor_mask))
427 continue;
428 smp_mb(); /* rcu_sched_qs_mask before cond_resched_completed. */
7d0ae808
PM
429 if (READ_ONCE(rdp->mynode->completed) !=
430 READ_ONCE(rdp->cond_resched_completed))
4a81e832
PM
431 continue;
432
433 /*
434 * Pretend to be momentarily idle for the quiescent state.
435 * This allows the grace-period kthread to record the
436 * quiescent state, with no need for this CPU to do anything
437 * further.
438 */
6563de9d 439 rcu_dynticks_momentary_idle();
4a81e832
PM
440 break;
441 }
4a81e832
PM
442}
443
25502a6c
PM
444/*
445 * Note a context switch. This is a quiescent state for RCU-sched,
446 * and requires special handling for preemptible RCU.
46a5d164 447 * The caller must have disabled interrupts.
25502a6c 448 */
38200cf2 449void rcu_note_context_switch(void)
25502a6c 450{
bb73c52b 451 barrier(); /* Avoid RCU read-side critical sections leaking down. */
f7f7bac9 452 trace_rcu_utilization(TPS("Start context switch"));
284a8c93 453 rcu_sched_qs();
38200cf2 454 rcu_preempt_note_context_switch();
4a81e832
PM
455 if (unlikely(raw_cpu_read(rcu_sched_qs_mask)))
456 rcu_momentary_dyntick_idle();
f7f7bac9 457 trace_rcu_utilization(TPS("End context switch"));
bb73c52b 458 barrier(); /* Avoid RCU read-side critical sections leaking up. */
25502a6c 459}
29ce8310 460EXPORT_SYMBOL_GPL(rcu_note_context_switch);
25502a6c 461
5cd37193 462/*
1925d196 463 * Register a quiescent state for all RCU flavors. If there is an
5cd37193
PM
464 * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
465 * dyntick-idle quiescent state visible to other CPUs (but only for those
1925d196 466 * RCU flavors in desperate need of a quiescent state, which will normally
5cd37193
PM
467 * be none of them). Either way, do a lightweight quiescent state for
468 * all RCU flavors.
bb73c52b
BF
469 *
470 * The barrier() calls are redundant in the common case when this is
471 * called externally, but just in case this is called from within this
472 * file.
473 *
5cd37193
PM
474 */
475void rcu_all_qs(void)
476{
46a5d164
PM
477 unsigned long flags;
478
bb73c52b 479 barrier(); /* Avoid RCU read-side critical sections leaking down. */
46a5d164
PM
480 if (unlikely(raw_cpu_read(rcu_sched_qs_mask))) {
481 local_irq_save(flags);
5cd37193 482 rcu_momentary_dyntick_idle();
46a5d164
PM
483 local_irq_restore(flags);
484 }
a1e12248
PM
485 if (unlikely(raw_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))) {
486 /*
487 * Yes, we just checked a per-CPU variable with preemption
488 * enabled, so we might be migrated to some other CPU at
489 * this point. That is OK because in that case, the
490 * migration will supply the needed quiescent state.
491 * We might end up needlessly disabling preemption and
492 * invoking rcu_sched_qs() on the destination CPU, but
493 * the probability and cost are both quite low, so this
494 * should not be a problem in practice.
495 */
496 preempt_disable();
497 rcu_sched_qs();
498 preempt_enable();
499 }
5cd37193 500 this_cpu_inc(rcu_qs_ctr);
bb73c52b 501 barrier(); /* Avoid RCU read-side critical sections leaking up. */
5cd37193
PM
502}
503EXPORT_SYMBOL_GPL(rcu_all_qs);
504
878d7439
ED
505static long blimit = 10; /* Maximum callbacks per rcu_do_batch. */
506static long qhimark = 10000; /* If this many pending, ignore blimit. */
507static long qlowmark = 100; /* Once only this many pending, use blimit. */
64db4cff 508
878d7439
ED
509module_param(blimit, long, 0444);
510module_param(qhimark, long, 0444);
511module_param(qlowmark, long, 0444);
3d76c082 512
026ad283
PM
513static ulong jiffies_till_first_fqs = ULONG_MAX;
514static ulong jiffies_till_next_fqs = ULONG_MAX;
8c7c4829 515static bool rcu_kick_kthreads;
d40011f6
PM
516
517module_param(jiffies_till_first_fqs, ulong, 0644);
518module_param(jiffies_till_next_fqs, ulong, 0644);
8c7c4829 519module_param(rcu_kick_kthreads, bool, 0644);
d40011f6 520
4a81e832
PM
521/*
522 * How long the grace period must be before we start recruiting
523 * quiescent-state help from rcu_note_context_switch().
524 */
525static ulong jiffies_till_sched_qs = HZ / 20;
526module_param(jiffies_till_sched_qs, ulong, 0644);
527
48a7639c 528static bool rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
910ee45d 529 struct rcu_data *rdp);
217af2a2
PM
530static void force_qs_rnp(struct rcu_state *rsp,
531 int (*f)(struct rcu_data *rsp, bool *isidle,
532 unsigned long *maxj),
533 bool *isidle, unsigned long *maxj);
4cdfc175 534static void force_quiescent_state(struct rcu_state *rsp);
e3950ecd 535static int rcu_pending(void);
64db4cff
PM
536
537/*
917963d0 538 * Return the number of RCU batches started thus far for debug & stats.
64db4cff 539 */
917963d0
PM
540unsigned long rcu_batches_started(void)
541{
542 return rcu_state_p->gpnum;
543}
544EXPORT_SYMBOL_GPL(rcu_batches_started);
545
546/*
547 * Return the number of RCU-sched batches started thus far for debug & stats.
64db4cff 548 */
917963d0
PM
549unsigned long rcu_batches_started_sched(void)
550{
551 return rcu_sched_state.gpnum;
552}
553EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
554
555/*
556 * Return the number of RCU BH batches started thus far for debug & stats.
557 */
558unsigned long rcu_batches_started_bh(void)
559{
560 return rcu_bh_state.gpnum;
561}
562EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
563
564/*
565 * Return the number of RCU batches completed thus far for debug & stats.
566 */
567unsigned long rcu_batches_completed(void)
568{
569 return rcu_state_p->completed;
570}
571EXPORT_SYMBOL_GPL(rcu_batches_completed);
572
573/*
574 * Return the number of RCU-sched batches completed thus far for debug & stats.
64db4cff 575 */
9733e4f0 576unsigned long rcu_batches_completed_sched(void)
64db4cff 577{
d6714c22 578 return rcu_sched_state.completed;
64db4cff 579}
d6714c22 580EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
64db4cff
PM
581
582/*
917963d0 583 * Return the number of RCU BH batches completed thus far for debug & stats.
64db4cff 584 */
9733e4f0 585unsigned long rcu_batches_completed_bh(void)
64db4cff
PM
586{
587 return rcu_bh_state.completed;
588}
589EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
590
291783b8
PM
591/*
592 * Return the number of RCU expedited batches completed thus far for
593 * debug & stats. Odd numbers mean that a batch is in progress, even
594 * numbers mean idle. The value returned will thus be roughly double
595 * the cumulative batches since boot.
596 */
597unsigned long rcu_exp_batches_completed(void)
598{
599 return rcu_state_p->expedited_sequence;
600}
601EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
602
603/*
604 * Return the number of RCU-sched expedited batches completed thus far
605 * for debug & stats. Similar to rcu_exp_batches_completed().
606 */
607unsigned long rcu_exp_batches_completed_sched(void)
608{
609 return rcu_sched_state.expedited_sequence;
610}
611EXPORT_SYMBOL_GPL(rcu_exp_batches_completed_sched);
612
a381d757
ACB
613/*
614 * Force a quiescent state.
615 */
616void rcu_force_quiescent_state(void)
617{
e534165b 618 force_quiescent_state(rcu_state_p);
a381d757
ACB
619}
620EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
621
bf66f18e
PM
622/*
623 * Force a quiescent state for RCU BH.
624 */
625void rcu_bh_force_quiescent_state(void)
626{
4cdfc175 627 force_quiescent_state(&rcu_bh_state);
bf66f18e
PM
628}
629EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
630
e7580f33
PM
631/*
632 * Force a quiescent state for RCU-sched.
633 */
634void rcu_sched_force_quiescent_state(void)
635{
636 force_quiescent_state(&rcu_sched_state);
637}
638EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
639
afea227f
PM
640/*
641 * Show the state of the grace-period kthreads.
642 */
643void show_rcu_gp_kthreads(void)
644{
645 struct rcu_state *rsp;
646
647 for_each_rcu_flavor(rsp) {
648 pr_info("%s: wait state: %d ->state: %#lx\n",
649 rsp->name, rsp->gp_state, rsp->gp_kthread->state);
650 /* sched_show_task(rsp->gp_kthread); */
651 }
652}
653EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
654
4a298656
PM
655/*
656 * Record the number of times rcutorture tests have been initiated and
657 * terminated. This information allows the debugfs tracing stats to be
658 * correlated to the rcutorture messages, even when the rcutorture module
659 * is being repeatedly loaded and unloaded. In other words, we cannot
660 * store this state in rcutorture itself.
661 */
662void rcutorture_record_test_transition(void)
663{
664 rcutorture_testseq++;
665 rcutorture_vernum = 0;
666}
667EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
668
ad0dc7f9
PM
669/*
670 * Send along grace-period-related data for rcutorture diagnostics.
671 */
672void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
673 unsigned long *gpnum, unsigned long *completed)
674{
675 struct rcu_state *rsp = NULL;
676
677 switch (test_type) {
678 case RCU_FLAVOR:
e534165b 679 rsp = rcu_state_p;
ad0dc7f9
PM
680 break;
681 case RCU_BH_FLAVOR:
682 rsp = &rcu_bh_state;
683 break;
684 case RCU_SCHED_FLAVOR:
685 rsp = &rcu_sched_state;
686 break;
687 default:
688 break;
689 }
690 if (rsp != NULL) {
7d0ae808
PM
691 *flags = READ_ONCE(rsp->gp_flags);
692 *gpnum = READ_ONCE(rsp->gpnum);
693 *completed = READ_ONCE(rsp->completed);
ad0dc7f9
PM
694 return;
695 }
696 *flags = 0;
697 *gpnum = 0;
698 *completed = 0;
699}
700EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
701
4a298656
PM
702/*
703 * Record the number of writer passes through the current rcutorture test.
704 * This is also used to correlate debugfs tracing stats with the rcutorture
705 * messages.
706 */
707void rcutorture_record_progress(unsigned long vernum)
708{
709 rcutorture_vernum++;
710}
711EXPORT_SYMBOL_GPL(rcutorture_record_progress);
712
64db4cff
PM
713/*
714 * Does the CPU have callbacks ready to be invoked?
715 */
716static int
717cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
718{
3fbfbf7a
PM
719 return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL] &&
720 rdp->nxttail[RCU_DONE_TAIL] != NULL;
64db4cff
PM
721}
722
365187fb
PM
723/*
724 * Return the root node of the specified rcu_state structure.
725 */
726static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
727{
728 return &rsp->node[0];
729}
730
731/*
732 * Is there any need for future grace periods?
733 * Interrupts must be disabled. If the caller does not hold the root
734 * rnp_node structure's ->lock, the results are advisory only.
735 */
736static int rcu_future_needs_gp(struct rcu_state *rsp)
737{
738 struct rcu_node *rnp = rcu_get_root(rsp);
7d0ae808 739 int idx = (READ_ONCE(rnp->completed) + 1) & 0x1;
365187fb
PM
740 int *fp = &rnp->need_future_gp[idx];
741
7d0ae808 742 return READ_ONCE(*fp);
365187fb
PM
743}
744
64db4cff 745/*
dc35c893
PM
746 * Does the current CPU require a not-yet-started grace period?
747 * The caller must have disabled interrupts to prevent races with
748 * normal callback registry.
64db4cff 749 */
d117c8aa 750static bool
64db4cff
PM
751cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
752{
dc35c893 753 int i;
3fbfbf7a 754
dc35c893 755 if (rcu_gp_in_progress(rsp))
d117c8aa 756 return false; /* No, a grace period is already in progress. */
365187fb 757 if (rcu_future_needs_gp(rsp))
d117c8aa 758 return true; /* Yes, a no-CBs CPU needs one. */
dc35c893 759 if (!rdp->nxttail[RCU_NEXT_TAIL])
d117c8aa 760 return false; /* No, this is a no-CBs (or offline) CPU. */
dc35c893 761 if (*rdp->nxttail[RCU_NEXT_READY_TAIL])
d117c8aa 762 return true; /* Yes, CPU has newly registered callbacks. */
dc35c893
PM
763 for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++)
764 if (rdp->nxttail[i - 1] != rdp->nxttail[i] &&
7d0ae808 765 ULONG_CMP_LT(READ_ONCE(rsp->completed),
dc35c893 766 rdp->nxtcompleted[i]))
d117c8aa
PM
767 return true; /* Yes, CBs for future grace period. */
768 return false; /* No grace period needed. */
64db4cff
PM
769}
770
9b2e4f18 771/*
adf5091e 772 * rcu_eqs_enter_common - current CPU is moving towards extended quiescent state
9b2e4f18
PM
773 *
774 * If the new value of the ->dynticks_nesting counter now is zero,
775 * we really have entered idle, and must do the appropriate accounting.
776 * The caller must have disabled interrupts.
777 */
28ced795 778static void rcu_eqs_enter_common(long long oldval, bool user)
9b2e4f18 779{
96d3fd0d
PM
780 struct rcu_state *rsp;
781 struct rcu_data *rdp;
2625d469 782 RCU_TRACE(struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);)
96d3fd0d 783
f7f7bac9 784 trace_rcu_dyntick(TPS("Start"), oldval, rdtp->dynticks_nesting);
1ce46ee5
PM
785 if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
786 !user && !is_idle_task(current)) {
289828e6
PM
787 struct task_struct *idle __maybe_unused =
788 idle_task(smp_processor_id());
0989cb46 789
f7f7bac9 790 trace_rcu_dyntick(TPS("Error on entry: not idle task"), oldval, 0);
274529ba 791 rcu_ftrace_dump(DUMP_ORIG);
0989cb46
PM
792 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
793 current->pid, current->comm,
794 idle->pid, idle->comm); /* must be idle task! */
9b2e4f18 795 }
96d3fd0d
PM
796 for_each_rcu_flavor(rsp) {
797 rdp = this_cpu_ptr(rsp->rda);
798 do_nocb_deferred_wakeup(rdp);
799 }
198bbf81 800 rcu_prepare_for_idle();
2625d469 801 rcu_dynticks_eqs_enter();
176f8f7a 802 rcu_dynticks_task_enter();
c44e2cdd
PM
803
804 /*
adf5091e 805 * It is illegal to enter an extended quiescent state while
c44e2cdd
PM
806 * in an RCU read-side critical section.
807 */
f78f5b90
PM
808 RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
809 "Illegal idle entry in RCU read-side critical section.");
810 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map),
811 "Illegal idle entry in RCU-bh read-side critical section.");
812 RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map),
813 "Illegal idle entry in RCU-sched read-side critical section.");
9b2e4f18 814}
64db4cff 815
adf5091e
FW
816/*
817 * Enter an RCU extended quiescent state, which can be either the
818 * idle loop or adaptive-tickless usermode execution.
64db4cff 819 */
adf5091e 820static void rcu_eqs_enter(bool user)
64db4cff 821{
4145fa7f 822 long long oldval;
64db4cff
PM
823 struct rcu_dynticks *rdtp;
824
c9d4b0af 825 rdtp = this_cpu_ptr(&rcu_dynticks);
4145fa7f 826 oldval = rdtp->dynticks_nesting;
1ce46ee5
PM
827 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
828 (oldval & DYNTICK_TASK_NEST_MASK) == 0);
3a592405 829 if ((oldval & DYNTICK_TASK_NEST_MASK) == DYNTICK_TASK_NEST_VALUE) {
29e37d81 830 rdtp->dynticks_nesting = 0;
28ced795 831 rcu_eqs_enter_common(oldval, user);
3a592405 832 } else {
29e37d81 833 rdtp->dynticks_nesting -= DYNTICK_TASK_NEST_VALUE;
3a592405 834 }
64db4cff 835}
adf5091e
FW
836
837/**
838 * rcu_idle_enter - inform RCU that current CPU is entering idle
839 *
840 * Enter idle mode, in other words, -leave- the mode in which RCU
841 * read-side critical sections can occur. (Though RCU read-side
842 * critical sections can occur in irq handlers in idle, a possibility
843 * handled by irq_enter() and irq_exit().)
844 *
845 * We crowbar the ->dynticks_nesting field to zero to allow for
846 * the possibility of usermode upcalls having messed up our count
847 * of interrupt nesting level during the prior busy period.
848 */
849void rcu_idle_enter(void)
850{
c5d900bf
FW
851 unsigned long flags;
852
853 local_irq_save(flags);
cb349ca9 854 rcu_eqs_enter(false);
28ced795 855 rcu_sysidle_enter(0);
c5d900bf 856 local_irq_restore(flags);
adf5091e 857}
8a2ecf47 858EXPORT_SYMBOL_GPL(rcu_idle_enter);
64db4cff 859
d1ec4c34 860#ifdef CONFIG_NO_HZ_FULL
adf5091e
FW
861/**
862 * rcu_user_enter - inform RCU that we are resuming userspace.
863 *
864 * Enter RCU idle mode right before resuming userspace. No use of RCU
865 * is permitted between this call and rcu_user_exit(). This way the
866 * CPU doesn't need to maintain the tick for RCU maintenance purposes
867 * when the CPU runs in userspace.
868 */
869void rcu_user_enter(void)
870{
91d1aa43 871 rcu_eqs_enter(1);
adf5091e 872}
d1ec4c34 873#endif /* CONFIG_NO_HZ_FULL */
19dd1591 874
9b2e4f18
PM
875/**
876 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
877 *
878 * Exit from an interrupt handler, which might possibly result in entering
879 * idle mode, in other words, leaving the mode in which read-side critical
7c9906ca 880 * sections can occur. The caller must have disabled interrupts.
64db4cff 881 *
9b2e4f18
PM
882 * This code assumes that the idle loop never does anything that might
883 * result in unbalanced calls to irq_enter() and irq_exit(). If your
884 * architecture violates this assumption, RCU will give you what you
885 * deserve, good and hard. But very infrequently and irreproducibly.
886 *
887 * Use things like work queues to work around this limitation.
888 *
889 * You have been warned.
64db4cff 890 */
9b2e4f18 891void rcu_irq_exit(void)
64db4cff 892{
4145fa7f 893 long long oldval;
64db4cff
PM
894 struct rcu_dynticks *rdtp;
895
7c9906ca 896 RCU_LOCKDEP_WARN(!irqs_disabled(), "rcu_irq_exit() invoked with irqs enabled!!!");
c9d4b0af 897 rdtp = this_cpu_ptr(&rcu_dynticks);
4145fa7f 898 oldval = rdtp->dynticks_nesting;
9b2e4f18 899 rdtp->dynticks_nesting--;
1ce46ee5
PM
900 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
901 rdtp->dynticks_nesting < 0);
b6fc6020 902 if (rdtp->dynticks_nesting)
f7f7bac9 903 trace_rcu_dyntick(TPS("--="), oldval, rdtp->dynticks_nesting);
b6fc6020 904 else
28ced795
CL
905 rcu_eqs_enter_common(oldval, true);
906 rcu_sysidle_enter(1);
7c9906ca
PM
907}
908
909/*
910 * Wrapper for rcu_irq_exit() where interrupts are enabled.
911 */
912void rcu_irq_exit_irqson(void)
913{
914 unsigned long flags;
915
916 local_irq_save(flags);
917 rcu_irq_exit();
9b2e4f18
PM
918 local_irq_restore(flags);
919}
920
921/*
adf5091e 922 * rcu_eqs_exit_common - current CPU moving away from extended quiescent state
9b2e4f18
PM
923 *
924 * If the new value of the ->dynticks_nesting counter was previously zero,
925 * we really have exited idle, and must do the appropriate accounting.
926 * The caller must have disabled interrupts.
927 */
28ced795 928static void rcu_eqs_exit_common(long long oldval, int user)
9b2e4f18 929{
2625d469 930 RCU_TRACE(struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);)
28ced795 931
176f8f7a 932 rcu_dynticks_task_exit();
2625d469 933 rcu_dynticks_eqs_exit();
8fa7845d 934 rcu_cleanup_after_idle();
f7f7bac9 935 trace_rcu_dyntick(TPS("End"), oldval, rdtp->dynticks_nesting);
1ce46ee5
PM
936 if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
937 !user && !is_idle_task(current)) {
289828e6
PM
938 struct task_struct *idle __maybe_unused =
939 idle_task(smp_processor_id());
0989cb46 940
f7f7bac9 941 trace_rcu_dyntick(TPS("Error on exit: not idle task"),
4145fa7f 942 oldval, rdtp->dynticks_nesting);
274529ba 943 rcu_ftrace_dump(DUMP_ORIG);
0989cb46
PM
944 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
945 current->pid, current->comm,
946 idle->pid, idle->comm); /* must be idle task! */
9b2e4f18
PM
947 }
948}
949
adf5091e
FW
950/*
951 * Exit an RCU extended quiescent state, which can be either the
952 * idle loop or adaptive-tickless usermode execution.
9b2e4f18 953 */
adf5091e 954static void rcu_eqs_exit(bool user)
9b2e4f18 955{
9b2e4f18
PM
956 struct rcu_dynticks *rdtp;
957 long long oldval;
958
c9d4b0af 959 rdtp = this_cpu_ptr(&rcu_dynticks);
9b2e4f18 960 oldval = rdtp->dynticks_nesting;
1ce46ee5 961 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
3a592405 962 if (oldval & DYNTICK_TASK_NEST_MASK) {
29e37d81 963 rdtp->dynticks_nesting += DYNTICK_TASK_NEST_VALUE;
3a592405 964 } else {
29e37d81 965 rdtp->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
28ced795 966 rcu_eqs_exit_common(oldval, user);
3a592405 967 }
9b2e4f18 968}
adf5091e
FW
969
970/**
971 * rcu_idle_exit - inform RCU that current CPU is leaving idle
972 *
973 * Exit idle mode, in other words, -enter- the mode in which RCU
974 * read-side critical sections can occur.
975 *
976 * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to
977 * allow for the possibility of usermode upcalls messing up our count
978 * of interrupt nesting level during the busy period that is just
979 * now starting.
980 */
981void rcu_idle_exit(void)
982{
c5d900bf
FW
983 unsigned long flags;
984
985 local_irq_save(flags);
cb349ca9 986 rcu_eqs_exit(false);
28ced795 987 rcu_sysidle_exit(0);
c5d900bf 988 local_irq_restore(flags);
adf5091e 989}
8a2ecf47 990EXPORT_SYMBOL_GPL(rcu_idle_exit);
9b2e4f18 991
d1ec4c34 992#ifdef CONFIG_NO_HZ_FULL
adf5091e
FW
993/**
994 * rcu_user_exit - inform RCU that we are exiting userspace.
995 *
996 * Exit RCU idle mode while entering the kernel because it can
997 * run a RCU read side critical section anytime.
998 */
999void rcu_user_exit(void)
1000{
91d1aa43 1001 rcu_eqs_exit(1);
adf5091e 1002}
d1ec4c34 1003#endif /* CONFIG_NO_HZ_FULL */
19dd1591 1004
9b2e4f18
PM
1005/**
1006 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
1007 *
1008 * Enter an interrupt handler, which might possibly result in exiting
1009 * idle mode, in other words, entering the mode in which read-side critical
7c9906ca 1010 * sections can occur. The caller must have disabled interrupts.
9b2e4f18
PM
1011 *
1012 * Note that the Linux kernel is fully capable of entering an interrupt
1013 * handler that it never exits, for example when doing upcalls to
1014 * user mode! This code assumes that the idle loop never does upcalls to
1015 * user mode. If your architecture does do upcalls from the idle loop (or
1016 * does anything else that results in unbalanced calls to the irq_enter()
1017 * and irq_exit() functions), RCU will give you what you deserve, good
1018 * and hard. But very infrequently and irreproducibly.
1019 *
1020 * Use things like work queues to work around this limitation.
1021 *
1022 * You have been warned.
1023 */
1024void rcu_irq_enter(void)
1025{
9b2e4f18
PM
1026 struct rcu_dynticks *rdtp;
1027 long long oldval;
1028
7c9906ca 1029 RCU_LOCKDEP_WARN(!irqs_disabled(), "rcu_irq_enter() invoked with irqs enabled!!!");
c9d4b0af 1030 rdtp = this_cpu_ptr(&rcu_dynticks);
9b2e4f18
PM
1031 oldval = rdtp->dynticks_nesting;
1032 rdtp->dynticks_nesting++;
1ce46ee5
PM
1033 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
1034 rdtp->dynticks_nesting == 0);
b6fc6020 1035 if (oldval)
f7f7bac9 1036 trace_rcu_dyntick(TPS("++="), oldval, rdtp->dynticks_nesting);
b6fc6020 1037 else
28ced795
CL
1038 rcu_eqs_exit_common(oldval, true);
1039 rcu_sysidle_exit(1);
7c9906ca
PM
1040}
1041
1042/*
1043 * Wrapper for rcu_irq_enter() where interrupts are enabled.
1044 */
1045void rcu_irq_enter_irqson(void)
1046{
1047 unsigned long flags;
1048
1049 local_irq_save(flags);
1050 rcu_irq_enter();
64db4cff 1051 local_irq_restore(flags);
64db4cff
PM
1052}
1053
1054/**
1055 * rcu_nmi_enter - inform RCU of entry to NMI context
1056 *
734d1680
PM
1057 * If the CPU was idle from RCU's viewpoint, update rdtp->dynticks and
1058 * rdtp->dynticks_nmi_nesting to let the RCU grace-period handling know
1059 * that the CPU is active. This implementation permits nested NMIs, as
1060 * long as the nesting level does not overflow an int. (You will probably
1061 * run out of stack space first.)
64db4cff
PM
1062 */
1063void rcu_nmi_enter(void)
1064{
c9d4b0af 1065 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
734d1680 1066 int incby = 2;
64db4cff 1067
734d1680
PM
1068 /* Complain about underflow. */
1069 WARN_ON_ONCE(rdtp->dynticks_nmi_nesting < 0);
1070
1071 /*
1072 * If idle from RCU viewpoint, atomically increment ->dynticks
1073 * to mark non-idle and increment ->dynticks_nmi_nesting by one.
1074 * Otherwise, increment ->dynticks_nmi_nesting by two. This means
1075 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
1076 * to be in the outermost NMI handler that interrupted an RCU-idle
1077 * period (observation due to Andy Lutomirski).
1078 */
02a5c550 1079 if (rcu_dynticks_curr_cpu_in_eqs()) {
2625d469 1080 rcu_dynticks_eqs_exit();
734d1680
PM
1081 incby = 1;
1082 }
1083 rdtp->dynticks_nmi_nesting += incby;
1084 barrier();
64db4cff
PM
1085}
1086
1087/**
1088 * rcu_nmi_exit - inform RCU of exit from NMI context
1089 *
734d1680
PM
1090 * If we are returning from the outermost NMI handler that interrupted an
1091 * RCU-idle period, update rdtp->dynticks and rdtp->dynticks_nmi_nesting
1092 * to let the RCU grace-period handling know that the CPU is back to
1093 * being RCU-idle.
64db4cff
PM
1094 */
1095void rcu_nmi_exit(void)
1096{
c9d4b0af 1097 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
64db4cff 1098
734d1680
PM
1099 /*
1100 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
1101 * (We are exiting an NMI handler, so RCU better be paying attention
1102 * to us!)
1103 */
1104 WARN_ON_ONCE(rdtp->dynticks_nmi_nesting <= 0);
02a5c550 1105 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
734d1680
PM
1106
1107 /*
1108 * If the nesting level is not 1, the CPU wasn't RCU-idle, so
1109 * leave it in non-RCU-idle state.
1110 */
1111 if (rdtp->dynticks_nmi_nesting != 1) {
1112 rdtp->dynticks_nmi_nesting -= 2;
64db4cff 1113 return;
734d1680
PM
1114 }
1115
1116 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
1117 rdtp->dynticks_nmi_nesting = 0;
2625d469 1118 rcu_dynticks_eqs_enter();
64db4cff
PM
1119}
1120
1121/**
5c173eb8
PM
1122 * __rcu_is_watching - are RCU read-side critical sections safe?
1123 *
1124 * Return true if RCU is watching the running CPU, which means that
1125 * this CPU can safely enter RCU read-side critical sections. Unlike
1126 * rcu_is_watching(), the caller of __rcu_is_watching() must have at
1127 * least disabled preemption.
1128 */
9418fb20 1129bool notrace __rcu_is_watching(void)
5c173eb8 1130{
02a5c550 1131 return !rcu_dynticks_curr_cpu_in_eqs();
5c173eb8
PM
1132}
1133
1134/**
1135 * rcu_is_watching - see if RCU thinks that the current CPU is idle
64db4cff 1136 *
9b2e4f18 1137 * If the current CPU is in its idle loop and is neither in an interrupt
34240697 1138 * or NMI handler, return true.
64db4cff 1139 */
9418fb20 1140bool notrace rcu_is_watching(void)
64db4cff 1141{
f534ed1f 1142 bool ret;
34240697 1143
46f00d18 1144 preempt_disable_notrace();
5c173eb8 1145 ret = __rcu_is_watching();
46f00d18 1146 preempt_enable_notrace();
34240697 1147 return ret;
64db4cff 1148}
5c173eb8 1149EXPORT_SYMBOL_GPL(rcu_is_watching);
64db4cff 1150
62fde6ed 1151#if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
c0d6d01b
PM
1152
1153/*
1154 * Is the current CPU online? Disable preemption to avoid false positives
1155 * that could otherwise happen due to the current CPU number being sampled,
1156 * this task being preempted, its old CPU being taken offline, resuming
1157 * on some other CPU, then determining that its old CPU is now offline.
1158 * It is OK to use RCU on an offline processor during initial boot, hence
2036d94a
PM
1159 * the check for rcu_scheduler_fully_active. Note also that it is OK
1160 * for a CPU coming online to use RCU for one jiffy prior to marking itself
1161 * online in the cpu_online_mask. Similarly, it is OK for a CPU going
1162 * offline to continue to use RCU for one jiffy after marking itself
1163 * offline in the cpu_online_mask. This leniency is necessary given the
1164 * non-atomic nature of the online and offline processing, for example,
4df83742
TG
1165 * the fact that a CPU enters the scheduler after completing the teardown
1166 * of the CPU.
2036d94a 1167 *
4df83742
TG
1168 * This is also why RCU internally marks CPUs online during in the
1169 * preparation phase and offline after the CPU has been taken down.
c0d6d01b
PM
1170 *
1171 * Disable checking if in an NMI handler because we cannot safely report
1172 * errors from NMI handlers anyway.
1173 */
1174bool rcu_lockdep_current_cpu_online(void)
1175{
2036d94a
PM
1176 struct rcu_data *rdp;
1177 struct rcu_node *rnp;
c0d6d01b
PM
1178 bool ret;
1179
1180 if (in_nmi())
f6f7ee9a 1181 return true;
c0d6d01b 1182 preempt_disable();
c9d4b0af 1183 rdp = this_cpu_ptr(&rcu_sched_data);
2036d94a 1184 rnp = rdp->mynode;
0aa04b05 1185 ret = (rdp->grpmask & rcu_rnp_online_cpus(rnp)) ||
c0d6d01b
PM
1186 !rcu_scheduler_fully_active;
1187 preempt_enable();
1188 return ret;
1189}
1190EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
1191
62fde6ed 1192#endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
9b2e4f18 1193
64db4cff 1194/**
9b2e4f18 1195 * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
64db4cff 1196 *
9b2e4f18
PM
1197 * If the current CPU is idle or running at a first-level (not nested)
1198 * interrupt from idle, return true. The caller must have at least
1199 * disabled preemption.
64db4cff 1200 */
62e3cb14 1201static int rcu_is_cpu_rrupt_from_idle(void)
64db4cff 1202{
c9d4b0af 1203 return __this_cpu_read(rcu_dynticks.dynticks_nesting) <= 1;
64db4cff
PM
1204}
1205
64db4cff
PM
1206/*
1207 * Snapshot the specified CPU's dynticks counter so that we can later
1208 * credit them with an implicit quiescent state. Return 1 if this CPU
1eba8f84 1209 * is in dynticks idle mode, which is an extended quiescent state.
64db4cff 1210 */
217af2a2
PM
1211static int dyntick_save_progress_counter(struct rcu_data *rdp,
1212 bool *isidle, unsigned long *maxj)
64db4cff 1213{
8b2f63ab 1214 rdp->dynticks_snap = rcu_dynticks_snap(rdp->dynticks);
0edd1b17 1215 rcu_sysidle_check_cpu(rdp, isidle, maxj);
02a5c550 1216 if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
7941dbde 1217 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
7d0ae808 1218 if (ULONG_CMP_LT(READ_ONCE(rdp->gpnum) + ULONG_MAX / 4,
e3663b10 1219 rdp->mynode->gpnum))
7d0ae808 1220 WRITE_ONCE(rdp->gpwrap, true);
23a9bacd 1221 return 1;
7941dbde 1222 }
23a9bacd 1223 return 0;
64db4cff
PM
1224}
1225
1226/*
1227 * Return true if the specified CPU has passed through a quiescent
1228 * state by virtue of being in or having passed through an dynticks
1229 * idle state since the last call to dyntick_save_progress_counter()
a82dcc76 1230 * for this same CPU, or by virtue of having been offline.
64db4cff 1231 */
217af2a2
PM
1232static int rcu_implicit_dynticks_qs(struct rcu_data *rdp,
1233 bool *isidle, unsigned long *maxj)
64db4cff 1234{
4a81e832 1235 int *rcrmp;
64db4cff
PM
1236
1237 /*
1238 * If the CPU passed through or entered a dynticks idle phase with
1239 * no active irq/NMI handlers, then we can safely pretend that the CPU
1240 * already acknowledged the request to pass through a quiescent
1241 * state. Either way, that CPU cannot possibly be in an RCU
1242 * read-side critical section that started before the beginning
1243 * of the current RCU grace period.
1244 */
02a5c550 1245 if (rcu_dynticks_in_eqs_since(rdp->dynticks, rdp->dynticks_snap)) {
f7f7bac9 1246 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
64db4cff
PM
1247 rdp->dynticks_fqs++;
1248 return 1;
1249 }
1250
a82dcc76
PM
1251 /*
1252 * Check for the CPU being offline, but only if the grace period
1253 * is old enough. We don't need to worry about the CPU changing
1254 * state: If we see it offline even once, it has been through a
1255 * quiescent state.
1256 *
1257 * The reason for insisting that the grace period be at least
1258 * one jiffy old is that CPUs that are not quite online and that
1259 * have just gone offline can still execute RCU read-side critical
1260 * sections.
1261 */
1262 if (ULONG_CMP_GE(rdp->rsp->gp_start + 2, jiffies))
1263 return 0; /* Grace period is not old enough. */
1264 barrier();
1265 if (cpu_is_offline(rdp->cpu)) {
f7f7bac9 1266 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("ofl"));
a82dcc76
PM
1267 rdp->offline_fqs++;
1268 return 1;
1269 }
65d798f0
PM
1270
1271 /*
4a81e832
PM
1272 * A CPU running for an extended time within the kernel can
1273 * delay RCU grace periods. When the CPU is in NO_HZ_FULL mode,
1274 * even context-switching back and forth between a pair of
1275 * in-kernel CPU-bound tasks cannot advance grace periods.
1276 * So if the grace period is old enough, make the CPU pay attention.
1277 * Note that the unsynchronized assignments to the per-CPU
1278 * rcu_sched_qs_mask variable are safe. Yes, setting of
1279 * bits can be lost, but they will be set again on the next
1280 * force-quiescent-state pass. So lost bit sets do not result
1281 * in incorrect behavior, merely in a grace period lasting
1282 * a few jiffies longer than it might otherwise. Because
1283 * there are at most four threads involved, and because the
1284 * updates are only once every few jiffies, the probability of
1285 * lossage (and thus of slight grace-period extension) is
1286 * quite low.
1287 *
1288 * Note that if the jiffies_till_sched_qs boot/sysfs parameter
1289 * is set too high, we override with half of the RCU CPU stall
1290 * warning delay.
6193c76a 1291 */
4a81e832
PM
1292 rcrmp = &per_cpu(rcu_sched_qs_mask, rdp->cpu);
1293 if (ULONG_CMP_GE(jiffies,
1294 rdp->rsp->gp_start + jiffies_till_sched_qs) ||
cb1e78cf 1295 ULONG_CMP_GE(jiffies, rdp->rsp->jiffies_resched)) {
7d0ae808
PM
1296 if (!(READ_ONCE(*rcrmp) & rdp->rsp->flavor_mask)) {
1297 WRITE_ONCE(rdp->cond_resched_completed,
1298 READ_ONCE(rdp->mynode->completed));
4a81e832 1299 smp_mb(); /* ->cond_resched_completed before *rcrmp. */
7d0ae808
PM
1300 WRITE_ONCE(*rcrmp,
1301 READ_ONCE(*rcrmp) + rdp->rsp->flavor_mask);
4a81e832 1302 }
4914950a 1303 rdp->rsp->jiffies_resched += 5; /* Re-enable beating. */
6193c76a
PM
1304 }
1305
4914950a
PM
1306 /* And if it has been a really long time, kick the CPU as well. */
1307 if (ULONG_CMP_GE(jiffies,
1308 rdp->rsp->gp_start + 2 * jiffies_till_sched_qs) ||
1309 ULONG_CMP_GE(jiffies, rdp->rsp->gp_start + jiffies_till_sched_qs))
1310 resched_cpu(rdp->cpu); /* Force CPU into scheduler. */
1311
a82dcc76 1312 return 0;
64db4cff
PM
1313}
1314
64db4cff
PM
1315static void record_gp_stall_check_time(struct rcu_state *rsp)
1316{
cb1e78cf 1317 unsigned long j = jiffies;
6193c76a 1318 unsigned long j1;
26cdfedf
PM
1319
1320 rsp->gp_start = j;
1321 smp_wmb(); /* Record start time before stall time. */
6193c76a 1322 j1 = rcu_jiffies_till_stall_check();
7d0ae808 1323 WRITE_ONCE(rsp->jiffies_stall, j + j1);
6193c76a 1324 rsp->jiffies_resched = j + j1 / 2;
7d0ae808 1325 rsp->n_force_qs_gpstart = READ_ONCE(rsp->n_force_qs);
64db4cff
PM
1326}
1327
6b50e119
PM
1328/*
1329 * Convert a ->gp_state value to a character string.
1330 */
1331static const char *gp_state_getname(short gs)
1332{
1333 if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
1334 return "???";
1335 return gp_state_names[gs];
1336}
1337
fb81a44b
PM
1338/*
1339 * Complain about starvation of grace-period kthread.
1340 */
1341static void rcu_check_gp_kthread_starvation(struct rcu_state *rsp)
1342{
1343 unsigned long gpa;
1344 unsigned long j;
1345
1346 j = jiffies;
7d0ae808 1347 gpa = READ_ONCE(rsp->gp_activity);
b1adb3e2 1348 if (j - gpa > 2 * HZ) {
6b50e119 1349 pr_err("%s kthread starved for %ld jiffies! g%lu c%lu f%#x %s(%d) ->state=%#lx\n",
81e701e4 1350 rsp->name, j - gpa,
319362c9 1351 rsp->gpnum, rsp->completed,
6b50e119
PM
1352 rsp->gp_flags,
1353 gp_state_getname(rsp->gp_state), rsp->gp_state,
a0e3a3aa 1354 rsp->gp_kthread ? rsp->gp_kthread->state : ~0);
86057b80 1355 if (rsp->gp_kthread) {
b1adb3e2 1356 sched_show_task(rsp->gp_kthread);
86057b80
PM
1357 wake_up_process(rsp->gp_kthread);
1358 }
b1adb3e2 1359 }
64db4cff
PM
1360}
1361
b637a328 1362/*
bc1dce51 1363 * Dump stacks of all tasks running on stalled CPUs.
b637a328
PM
1364 */
1365static void rcu_dump_cpu_stacks(struct rcu_state *rsp)
1366{
1367 int cpu;
1368 unsigned long flags;
1369 struct rcu_node *rnp;
1370
1371 rcu_for_each_leaf_node(rsp, rnp) {
6cf10081 1372 raw_spin_lock_irqsave_rcu_node(rnp, flags);
b637a328 1373 if (rnp->qsmask != 0) {
bc75e999
MR
1374 for_each_leaf_node_possible_cpu(rnp, cpu)
1375 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu))
1376 dump_cpu_task(cpu);
b637a328 1377 }
67c583a7 1378 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
b637a328
PM
1379 }
1380}
1381
8c7c4829
PM
1382/*
1383 * If too much time has passed in the current grace period, and if
1384 * so configured, go kick the relevant kthreads.
1385 */
1386static void rcu_stall_kick_kthreads(struct rcu_state *rsp)
1387{
1388 unsigned long j;
1389
1390 if (!rcu_kick_kthreads)
1391 return;
1392 j = READ_ONCE(rsp->jiffies_kick_kthreads);
aa3e0bf1
PM
1393 if (time_after(jiffies, j) && rsp->gp_kthread &&
1394 (rcu_gp_in_progress(rsp) || READ_ONCE(rsp->gp_flags))) {
8c7c4829 1395 WARN_ONCE(1, "Kicking %s grace-period kthread\n", rsp->name);
5dffed1e 1396 rcu_ftrace_dump(DUMP_ALL);
8c7c4829
PM
1397 wake_up_process(rsp->gp_kthread);
1398 WRITE_ONCE(rsp->jiffies_kick_kthreads, j + HZ);
1399 }
1400}
1401
088e9d25
DBO
1402static inline void panic_on_rcu_stall(void)
1403{
1404 if (sysctl_panic_on_rcu_stall)
1405 panic("RCU Stall\n");
1406}
1407
6ccd2ecd 1408static void print_other_cpu_stall(struct rcu_state *rsp, unsigned long gpnum)
64db4cff
PM
1409{
1410 int cpu;
1411 long delta;
1412 unsigned long flags;
6ccd2ecd
PM
1413 unsigned long gpa;
1414 unsigned long j;
285fe294 1415 int ndetected = 0;
64db4cff 1416 struct rcu_node *rnp = rcu_get_root(rsp);
53bb857c 1417 long totqlen = 0;
64db4cff 1418
8c7c4829
PM
1419 /* Kick and suppress, if so configured. */
1420 rcu_stall_kick_kthreads(rsp);
1421 if (rcu_cpu_stall_suppress)
1422 return;
1423
64db4cff
PM
1424 /* Only let one CPU complain about others per time interval. */
1425
6cf10081 1426 raw_spin_lock_irqsave_rcu_node(rnp, flags);
7d0ae808 1427 delta = jiffies - READ_ONCE(rsp->jiffies_stall);
fc2219d4 1428 if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
67c583a7 1429 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff
PM
1430 return;
1431 }
7d0ae808
PM
1432 WRITE_ONCE(rsp->jiffies_stall,
1433 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
67c583a7 1434 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff 1435
8cdd32a9
PM
1436 /*
1437 * OK, time to rat on our buddy...
1438 * See Documentation/RCU/stallwarn.txt for info on how to debug
1439 * RCU CPU stall warnings.
1440 */
d7f3e207 1441 pr_err("INFO: %s detected stalls on CPUs/tasks:",
4300aa64 1442 rsp->name);
a858af28 1443 print_cpu_stall_info_begin();
a0b6c9a7 1444 rcu_for_each_leaf_node(rsp, rnp) {
6cf10081 1445 raw_spin_lock_irqsave_rcu_node(rnp, flags);
9bc8b558 1446 ndetected += rcu_print_task_stall(rnp);
c8020a67 1447 if (rnp->qsmask != 0) {
bc75e999
MR
1448 for_each_leaf_node_possible_cpu(rnp, cpu)
1449 if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
1450 print_cpu_stall_info(rsp, cpu);
c8020a67
PM
1451 ndetected++;
1452 }
1453 }
67c583a7 1454 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff 1455 }
a858af28 1456
a858af28 1457 print_cpu_stall_info_end();
53bb857c
PM
1458 for_each_possible_cpu(cpu)
1459 totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
83ebe63e 1460 pr_cont("(detected by %d, t=%ld jiffies, g=%ld, c=%ld, q=%lu)\n",
eee05882 1461 smp_processor_id(), (long)(jiffies - rsp->gp_start),
83ebe63e 1462 (long)rsp->gpnum, (long)rsp->completed, totqlen);
6ccd2ecd 1463 if (ndetected) {
b637a328 1464 rcu_dump_cpu_stacks(rsp);
6ccd2ecd 1465 } else {
7d0ae808
PM
1466 if (READ_ONCE(rsp->gpnum) != gpnum ||
1467 READ_ONCE(rsp->completed) == gpnum) {
6ccd2ecd
PM
1468 pr_err("INFO: Stall ended before state dump start\n");
1469 } else {
1470 j = jiffies;
7d0ae808 1471 gpa = READ_ONCE(rsp->gp_activity);
237a0f21 1472 pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
6ccd2ecd 1473 rsp->name, j - gpa, j, gpa,
237a0f21
PM
1474 jiffies_till_next_fqs,
1475 rcu_get_root(rsp)->qsmask);
6ccd2ecd
PM
1476 /* In this case, the current CPU might be at fault. */
1477 sched_show_task(current);
1478 }
1479 }
c1dc0b9c 1480
4cdfc175 1481 /* Complain about tasks blocking the grace period. */
1ed509a2
PM
1482 rcu_print_detail_task_stall(rsp);
1483
fb81a44b
PM
1484 rcu_check_gp_kthread_starvation(rsp);
1485
088e9d25
DBO
1486 panic_on_rcu_stall();
1487
4cdfc175 1488 force_quiescent_state(rsp); /* Kick them all. */
64db4cff
PM
1489}
1490
1491static void print_cpu_stall(struct rcu_state *rsp)
1492{
53bb857c 1493 int cpu;
64db4cff
PM
1494 unsigned long flags;
1495 struct rcu_node *rnp = rcu_get_root(rsp);
53bb857c 1496 long totqlen = 0;
64db4cff 1497
8c7c4829
PM
1498 /* Kick and suppress, if so configured. */
1499 rcu_stall_kick_kthreads(rsp);
1500 if (rcu_cpu_stall_suppress)
1501 return;
1502
8cdd32a9
PM
1503 /*
1504 * OK, time to rat on ourselves...
1505 * See Documentation/RCU/stallwarn.txt for info on how to debug
1506 * RCU CPU stall warnings.
1507 */
d7f3e207 1508 pr_err("INFO: %s self-detected stall on CPU", rsp->name);
a858af28
PM
1509 print_cpu_stall_info_begin();
1510 print_cpu_stall_info(rsp, smp_processor_id());
1511 print_cpu_stall_info_end();
53bb857c
PM
1512 for_each_possible_cpu(cpu)
1513 totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
83ebe63e
PM
1514 pr_cont(" (t=%lu jiffies g=%ld c=%ld q=%lu)\n",
1515 jiffies - rsp->gp_start,
1516 (long)rsp->gpnum, (long)rsp->completed, totqlen);
fb81a44b
PM
1517
1518 rcu_check_gp_kthread_starvation(rsp);
1519
bc1dce51 1520 rcu_dump_cpu_stacks(rsp);
c1dc0b9c 1521
6cf10081 1522 raw_spin_lock_irqsave_rcu_node(rnp, flags);
7d0ae808
PM
1523 if (ULONG_CMP_GE(jiffies, READ_ONCE(rsp->jiffies_stall)))
1524 WRITE_ONCE(rsp->jiffies_stall,
1525 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
67c583a7 1526 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
c1dc0b9c 1527
088e9d25
DBO
1528 panic_on_rcu_stall();
1529
b021fe3e
PZ
1530 /*
1531 * Attempt to revive the RCU machinery by forcing a context switch.
1532 *
1533 * A context switch would normally allow the RCU state machine to make
1534 * progress and it could be we're stuck in kernel space without context
1535 * switches for an entirely unreasonable amount of time.
1536 */
1537 resched_cpu(smp_processor_id());
64db4cff
PM
1538}
1539
1540static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
1541{
26cdfedf
PM
1542 unsigned long completed;
1543 unsigned long gpnum;
1544 unsigned long gps;
bad6e139
PM
1545 unsigned long j;
1546 unsigned long js;
64db4cff
PM
1547 struct rcu_node *rnp;
1548
8c7c4829
PM
1549 if ((rcu_cpu_stall_suppress && !rcu_kick_kthreads) ||
1550 !rcu_gp_in_progress(rsp))
c68de209 1551 return;
8c7c4829 1552 rcu_stall_kick_kthreads(rsp);
cb1e78cf 1553 j = jiffies;
26cdfedf
PM
1554
1555 /*
1556 * Lots of memory barriers to reject false positives.
1557 *
1558 * The idea is to pick up rsp->gpnum, then rsp->jiffies_stall,
1559 * then rsp->gp_start, and finally rsp->completed. These values
1560 * are updated in the opposite order with memory barriers (or
1561 * equivalent) during grace-period initialization and cleanup.
1562 * Now, a false positive can occur if we get an new value of
1563 * rsp->gp_start and a old value of rsp->jiffies_stall. But given
1564 * the memory barriers, the only way that this can happen is if one
1565 * grace period ends and another starts between these two fetches.
1566 * Detect this by comparing rsp->completed with the previous fetch
1567 * from rsp->gpnum.
1568 *
1569 * Given this check, comparisons of jiffies, rsp->jiffies_stall,
1570 * and rsp->gp_start suffice to forestall false positives.
1571 */
7d0ae808 1572 gpnum = READ_ONCE(rsp->gpnum);
26cdfedf 1573 smp_rmb(); /* Pick up ->gpnum first... */
7d0ae808 1574 js = READ_ONCE(rsp->jiffies_stall);
26cdfedf 1575 smp_rmb(); /* ...then ->jiffies_stall before the rest... */
7d0ae808 1576 gps = READ_ONCE(rsp->gp_start);
26cdfedf 1577 smp_rmb(); /* ...and finally ->gp_start before ->completed. */
7d0ae808 1578 completed = READ_ONCE(rsp->completed);
26cdfedf
PM
1579 if (ULONG_CMP_GE(completed, gpnum) ||
1580 ULONG_CMP_LT(j, js) ||
1581 ULONG_CMP_GE(gps, js))
1582 return; /* No stall or GP completed since entering function. */
64db4cff 1583 rnp = rdp->mynode;
c96ea7cf 1584 if (rcu_gp_in_progress(rsp) &&
7d0ae808 1585 (READ_ONCE(rnp->qsmask) & rdp->grpmask)) {
64db4cff
PM
1586
1587 /* We haven't checked in, so go dump stack. */
1588 print_cpu_stall(rsp);
1589
bad6e139
PM
1590 } else if (rcu_gp_in_progress(rsp) &&
1591 ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
64db4cff 1592
bad6e139 1593 /* They had a few time units to dump stack, so complain. */
6ccd2ecd 1594 print_other_cpu_stall(rsp, gpnum);
64db4cff
PM
1595 }
1596}
1597
53d84e00
PM
1598/**
1599 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
1600 *
1601 * Set the stall-warning timeout way off into the future, thus preventing
1602 * any RCU CPU stall-warning messages from appearing in the current set of
1603 * RCU grace periods.
1604 *
1605 * The caller must disable hard irqs.
1606 */
1607void rcu_cpu_stall_reset(void)
1608{
6ce75a23
PM
1609 struct rcu_state *rsp;
1610
1611 for_each_rcu_flavor(rsp)
7d0ae808 1612 WRITE_ONCE(rsp->jiffies_stall, jiffies + ULONG_MAX / 2);
53d84e00
PM
1613}
1614
3f5d3ea6 1615/*
d3f3f3f2
PM
1616 * Initialize the specified rcu_data structure's default callback list
1617 * to empty. The default callback list is the one that is not used by
1618 * no-callbacks CPUs.
3f5d3ea6 1619 */
d3f3f3f2 1620static void init_default_callback_list(struct rcu_data *rdp)
3f5d3ea6
PM
1621{
1622 int i;
1623
1624 rdp->nxtlist = NULL;
1625 for (i = 0; i < RCU_NEXT_SIZE; i++)
1626 rdp->nxttail[i] = &rdp->nxtlist;
1627}
1628
d3f3f3f2
PM
1629/*
1630 * Initialize the specified rcu_data structure's callback list to empty.
1631 */
1632static void init_callback_list(struct rcu_data *rdp)
1633{
1634 if (init_nocb_callback_list(rdp))
1635 return;
1636 init_default_callback_list(rdp);
1637}
1638
dc35c893
PM
1639/*
1640 * Determine the value that ->completed will have at the end of the
1641 * next subsequent grace period. This is used to tag callbacks so that
1642 * a CPU can invoke callbacks in a timely fashion even if that CPU has
1643 * been dyntick-idle for an extended period with callbacks under the
1644 * influence of RCU_FAST_NO_HZ.
1645 *
1646 * The caller must hold rnp->lock with interrupts disabled.
1647 */
1648static unsigned long rcu_cbs_completed(struct rcu_state *rsp,
1649 struct rcu_node *rnp)
1650{
1651 /*
1652 * If RCU is idle, we just wait for the next grace period.
1653 * But we can only be sure that RCU is idle if we are looking
1654 * at the root rcu_node structure -- otherwise, a new grace
1655 * period might have started, but just not yet gotten around
1656 * to initializing the current non-root rcu_node structure.
1657 */
1658 if (rcu_get_root(rsp) == rnp && rnp->gpnum == rnp->completed)
1659 return rnp->completed + 1;
1660
1661 /*
1662 * Otherwise, wait for a possible partial grace period and
1663 * then the subsequent full grace period.
1664 */
1665 return rnp->completed + 2;
1666}
1667
0446be48
PM
1668/*
1669 * Trace-event helper function for rcu_start_future_gp() and
1670 * rcu_nocb_wait_gp().
1671 */
1672static void trace_rcu_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
e66c33d5 1673 unsigned long c, const char *s)
0446be48
PM
1674{
1675 trace_rcu_future_grace_period(rdp->rsp->name, rnp->gpnum,
1676 rnp->completed, c, rnp->level,
1677 rnp->grplo, rnp->grphi, s);
1678}
1679
1680/*
1681 * Start some future grace period, as needed to handle newly arrived
1682 * callbacks. The required future grace periods are recorded in each
48a7639c
PM
1683 * rcu_node structure's ->need_future_gp field. Returns true if there
1684 * is reason to awaken the grace-period kthread.
0446be48
PM
1685 *
1686 * The caller must hold the specified rcu_node structure's ->lock.
1687 */
48a7639c
PM
1688static bool __maybe_unused
1689rcu_start_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
1690 unsigned long *c_out)
0446be48
PM
1691{
1692 unsigned long c;
1693 int i;
48a7639c 1694 bool ret = false;
0446be48
PM
1695 struct rcu_node *rnp_root = rcu_get_root(rdp->rsp);
1696
1697 /*
1698 * Pick up grace-period number for new callbacks. If this
1699 * grace period is already marked as needed, return to the caller.
1700 */
1701 c = rcu_cbs_completed(rdp->rsp, rnp);
f7f7bac9 1702 trace_rcu_future_gp(rnp, rdp, c, TPS("Startleaf"));
0446be48 1703 if (rnp->need_future_gp[c & 0x1]) {
f7f7bac9 1704 trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartleaf"));
48a7639c 1705 goto out;
0446be48
PM
1706 }
1707
1708 /*
1709 * If either this rcu_node structure or the root rcu_node structure
1710 * believe that a grace period is in progress, then we must wait
1711 * for the one following, which is in "c". Because our request
1712 * will be noticed at the end of the current grace period, we don't
48bd8e9b
PK
1713 * need to explicitly start one. We only do the lockless check
1714 * of rnp_root's fields if the current rcu_node structure thinks
1715 * there is no grace period in flight, and because we hold rnp->lock,
1716 * the only possible change is when rnp_root's two fields are
1717 * equal, in which case rnp_root->gpnum might be concurrently
1718 * incremented. But that is OK, as it will just result in our
1719 * doing some extra useless work.
0446be48
PM
1720 */
1721 if (rnp->gpnum != rnp->completed ||
7d0ae808 1722 READ_ONCE(rnp_root->gpnum) != READ_ONCE(rnp_root->completed)) {
0446be48 1723 rnp->need_future_gp[c & 0x1]++;
f7f7bac9 1724 trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleaf"));
48a7639c 1725 goto out;
0446be48
PM
1726 }
1727
1728 /*
1729 * There might be no grace period in progress. If we don't already
1730 * hold it, acquire the root rcu_node structure's lock in order to
1731 * start one (if needed).
1732 */
2a67e741
PZ
1733 if (rnp != rnp_root)
1734 raw_spin_lock_rcu_node(rnp_root);
0446be48
PM
1735
1736 /*
1737 * Get a new grace-period number. If there really is no grace
1738 * period in progress, it will be smaller than the one we obtained
1739 * earlier. Adjust callbacks as needed. Note that even no-CBs
1740 * CPUs have a ->nxtcompleted[] array, so no no-CBs checks needed.
1741 */
1742 c = rcu_cbs_completed(rdp->rsp, rnp_root);
1743 for (i = RCU_DONE_TAIL; i < RCU_NEXT_TAIL; i++)
1744 if (ULONG_CMP_LT(c, rdp->nxtcompleted[i]))
1745 rdp->nxtcompleted[i] = c;
1746
1747 /*
1748 * If the needed for the required grace period is already
1749 * recorded, trace and leave.
1750 */
1751 if (rnp_root->need_future_gp[c & 0x1]) {
f7f7bac9 1752 trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartedroot"));
0446be48
PM
1753 goto unlock_out;
1754 }
1755
1756 /* Record the need for the future grace period. */
1757 rnp_root->need_future_gp[c & 0x1]++;
1758
1759 /* If a grace period is not already in progress, start one. */
1760 if (rnp_root->gpnum != rnp_root->completed) {
f7f7bac9 1761 trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleafroot"));
0446be48 1762 } else {
f7f7bac9 1763 trace_rcu_future_gp(rnp, rdp, c, TPS("Startedroot"));
48a7639c 1764 ret = rcu_start_gp_advanced(rdp->rsp, rnp_root, rdp);
0446be48
PM
1765 }
1766unlock_out:
1767 if (rnp != rnp_root)
67c583a7 1768 raw_spin_unlock_rcu_node(rnp_root);
48a7639c
PM
1769out:
1770 if (c_out != NULL)
1771 *c_out = c;
1772 return ret;
0446be48
PM
1773}
1774
1775/*
1776 * Clean up any old requests for the just-ended grace period. Also return
1777 * whether any additional grace periods have been requested. Also invoke
1778 * rcu_nocb_gp_cleanup() in order to wake up any no-callbacks kthreads
1779 * waiting for this grace period to complete.
1780 */
1781static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
1782{
1783 int c = rnp->completed;
1784 int needmore;
1785 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
1786
0446be48
PM
1787 rnp->need_future_gp[c & 0x1] = 0;
1788 needmore = rnp->need_future_gp[(c + 1) & 0x1];
f7f7bac9
SRRH
1789 trace_rcu_future_gp(rnp, rdp, c,
1790 needmore ? TPS("CleanupMore") : TPS("Cleanup"));
0446be48
PM
1791 return needmore;
1792}
1793
48a7639c
PM
1794/*
1795 * Awaken the grace-period kthread for the specified flavor of RCU.
1796 * Don't do a self-awaken, and don't bother awakening when there is
1797 * nothing for the grace-period kthread to do (as in several CPUs
1798 * raced to awaken, and we lost), and finally don't try to awaken
1799 * a kthread that has not yet been created.
1800 */
1801static void rcu_gp_kthread_wake(struct rcu_state *rsp)
1802{
1803 if (current == rsp->gp_kthread ||
7d0ae808 1804 !READ_ONCE(rsp->gp_flags) ||
48a7639c
PM
1805 !rsp->gp_kthread)
1806 return;
abedf8e2 1807 swake_up(&rsp->gp_wq);
48a7639c
PM
1808}
1809
dc35c893
PM
1810/*
1811 * If there is room, assign a ->completed number to any callbacks on
1812 * this CPU that have not already been assigned. Also accelerate any
1813 * callbacks that were previously assigned a ->completed number that has
1814 * since proven to be too conservative, which can happen if callbacks get
1815 * assigned a ->completed number while RCU is idle, but with reference to
1816 * a non-root rcu_node structure. This function is idempotent, so it does
48a7639c
PM
1817 * not hurt to call it repeatedly. Returns an flag saying that we should
1818 * awaken the RCU grace-period kthread.
dc35c893
PM
1819 *
1820 * The caller must hold rnp->lock with interrupts disabled.
1821 */
48a7639c 1822static bool rcu_accelerate_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
dc35c893
PM
1823 struct rcu_data *rdp)
1824{
1825 unsigned long c;
1826 int i;
48a7639c 1827 bool ret;
dc35c893
PM
1828
1829 /* If the CPU has no callbacks, nothing to do. */
1830 if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
48a7639c 1831 return false;
dc35c893
PM
1832
1833 /*
1834 * Starting from the sublist containing the callbacks most
1835 * recently assigned a ->completed number and working down, find the
1836 * first sublist that is not assignable to an upcoming grace period.
1837 * Such a sublist has something in it (first two tests) and has
1838 * a ->completed number assigned that will complete sooner than
1839 * the ->completed number for newly arrived callbacks (last test).
1840 *
1841 * The key point is that any later sublist can be assigned the
1842 * same ->completed number as the newly arrived callbacks, which
1843 * means that the callbacks in any of these later sublist can be
1844 * grouped into a single sublist, whether or not they have already
1845 * been assigned a ->completed number.
1846 */
1847 c = rcu_cbs_completed(rsp, rnp);
1848 for (i = RCU_NEXT_TAIL - 1; i > RCU_DONE_TAIL; i--)
1849 if (rdp->nxttail[i] != rdp->nxttail[i - 1] &&
1850 !ULONG_CMP_GE(rdp->nxtcompleted[i], c))
1851 break;
1852
1853 /*
1854 * If there are no sublist for unassigned callbacks, leave.
1855 * At the same time, advance "i" one sublist, so that "i" will
1856 * index into the sublist where all the remaining callbacks should
1857 * be grouped into.
1858 */
1859 if (++i >= RCU_NEXT_TAIL)
48a7639c 1860 return false;
dc35c893
PM
1861
1862 /*
1863 * Assign all subsequent callbacks' ->completed number to the next
1864 * full grace period and group them all in the sublist initially
1865 * indexed by "i".
1866 */
1867 for (; i <= RCU_NEXT_TAIL; i++) {
1868 rdp->nxttail[i] = rdp->nxttail[RCU_NEXT_TAIL];
1869 rdp->nxtcompleted[i] = c;
1870 }
910ee45d 1871 /* Record any needed additional grace periods. */
48a7639c 1872 ret = rcu_start_future_gp(rnp, rdp, NULL);
6d4b418c
PM
1873
1874 /* Trace depending on how much we were able to accelerate. */
1875 if (!*rdp->nxttail[RCU_WAIT_TAIL])
f7f7bac9 1876 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccWaitCB"));
6d4b418c 1877 else
f7f7bac9 1878 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccReadyCB"));
48a7639c 1879 return ret;
dc35c893
PM
1880}
1881
1882/*
1883 * Move any callbacks whose grace period has completed to the
1884 * RCU_DONE_TAIL sublist, then compact the remaining sublists and
1885 * assign ->completed numbers to any callbacks in the RCU_NEXT_TAIL
1886 * sublist. This function is idempotent, so it does not hurt to
1887 * invoke it repeatedly. As long as it is not invoked -too- often...
48a7639c 1888 * Returns true if the RCU grace-period kthread needs to be awakened.
dc35c893
PM
1889 *
1890 * The caller must hold rnp->lock with interrupts disabled.
1891 */
48a7639c 1892static bool rcu_advance_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
dc35c893
PM
1893 struct rcu_data *rdp)
1894{
1895 int i, j;
1896
1897 /* If the CPU has no callbacks, nothing to do. */
1898 if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
48a7639c 1899 return false;
dc35c893
PM
1900
1901 /*
1902 * Find all callbacks whose ->completed numbers indicate that they
1903 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
1904 */
1905 for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
1906 if (ULONG_CMP_LT(rnp->completed, rdp->nxtcompleted[i]))
1907 break;
1908 rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[i];
1909 }
1910 /* Clean up any sublist tail pointers that were misordered above. */
1911 for (j = RCU_WAIT_TAIL; j < i; j++)
1912 rdp->nxttail[j] = rdp->nxttail[RCU_DONE_TAIL];
1913
1914 /* Copy down callbacks to fill in empty sublists. */
1915 for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
1916 if (rdp->nxttail[j] == rdp->nxttail[RCU_NEXT_TAIL])
1917 break;
1918 rdp->nxttail[j] = rdp->nxttail[i];
1919 rdp->nxtcompleted[j] = rdp->nxtcompleted[i];
1920 }
1921
1922 /* Classify any remaining callbacks. */
48a7639c 1923 return rcu_accelerate_cbs(rsp, rnp, rdp);
dc35c893
PM
1924}
1925
d09b62df 1926/*
ba9fbe95
PM
1927 * Update CPU-local rcu_data state to record the beginnings and ends of
1928 * grace periods. The caller must hold the ->lock of the leaf rcu_node
1929 * structure corresponding to the current CPU, and must have irqs disabled.
48a7639c 1930 * Returns true if the grace-period kthread needs to be awakened.
d09b62df 1931 */
48a7639c
PM
1932static bool __note_gp_changes(struct rcu_state *rsp, struct rcu_node *rnp,
1933 struct rcu_data *rdp)
d09b62df 1934{
48a7639c 1935 bool ret;
3563a438 1936 bool need_gp;
48a7639c 1937
ba9fbe95 1938 /* Handle the ends of any preceding grace periods first. */
e3663b10 1939 if (rdp->completed == rnp->completed &&
7d0ae808 1940 !unlikely(READ_ONCE(rdp->gpwrap))) {
d09b62df 1941
ba9fbe95 1942 /* No grace period end, so just accelerate recent callbacks. */
48a7639c 1943 ret = rcu_accelerate_cbs(rsp, rnp, rdp);
d09b62df 1944
dc35c893
PM
1945 } else {
1946
1947 /* Advance callbacks. */
48a7639c 1948 ret = rcu_advance_cbs(rsp, rnp, rdp);
d09b62df
PM
1949
1950 /* Remember that we saw this grace-period completion. */
1951 rdp->completed = rnp->completed;
f7f7bac9 1952 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuend"));
d09b62df 1953 }
398ebe60 1954
7d0ae808 1955 if (rdp->gpnum != rnp->gpnum || unlikely(READ_ONCE(rdp->gpwrap))) {
6eaef633
PM
1956 /*
1957 * If the current grace period is waiting for this CPU,
1958 * set up to detect a quiescent state, otherwise don't
1959 * go looking for one.
1960 */
1961 rdp->gpnum = rnp->gpnum;
f7f7bac9 1962 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpustart"));
3563a438
PM
1963 need_gp = !!(rnp->qsmask & rdp->grpmask);
1964 rdp->cpu_no_qs.b.norm = need_gp;
5cd37193 1965 rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_qs_ctr);
3563a438 1966 rdp->core_needs_qs = need_gp;
6eaef633 1967 zero_cpu_stall_ticks(rdp);
7d0ae808 1968 WRITE_ONCE(rdp->gpwrap, false);
6eaef633 1969 }
48a7639c 1970 return ret;
6eaef633
PM
1971}
1972
d34ea322 1973static void note_gp_changes(struct rcu_state *rsp, struct rcu_data *rdp)
6eaef633
PM
1974{
1975 unsigned long flags;
48a7639c 1976 bool needwake;
6eaef633
PM
1977 struct rcu_node *rnp;
1978
1979 local_irq_save(flags);
1980 rnp = rdp->mynode;
7d0ae808
PM
1981 if ((rdp->gpnum == READ_ONCE(rnp->gpnum) &&
1982 rdp->completed == READ_ONCE(rnp->completed) &&
1983 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
2a67e741 1984 !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
6eaef633
PM
1985 local_irq_restore(flags);
1986 return;
1987 }
48a7639c 1988 needwake = __note_gp_changes(rsp, rnp, rdp);
67c583a7 1989 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
48a7639c
PM
1990 if (needwake)
1991 rcu_gp_kthread_wake(rsp);
6eaef633
PM
1992}
1993
0f41c0dd
PM
1994static void rcu_gp_slow(struct rcu_state *rsp, int delay)
1995{
1996 if (delay > 0 &&
1997 !(rsp->gpnum % (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
1998 schedule_timeout_uninterruptible(delay);
1999}
2000
b3dbec76 2001/*
45fed3e7 2002 * Initialize a new grace period. Return false if no grace period required.
b3dbec76 2003 */
45fed3e7 2004static bool rcu_gp_init(struct rcu_state *rsp)
b3dbec76 2005{
0aa04b05 2006 unsigned long oldmask;
b3dbec76 2007 struct rcu_data *rdp;
7fdefc10 2008 struct rcu_node *rnp = rcu_get_root(rsp);
b3dbec76 2009
7d0ae808 2010 WRITE_ONCE(rsp->gp_activity, jiffies);
2a67e741 2011 raw_spin_lock_irq_rcu_node(rnp);
7d0ae808 2012 if (!READ_ONCE(rsp->gp_flags)) {
f7be8209 2013 /* Spurious wakeup, tell caller to go back to sleep. */
67c583a7 2014 raw_spin_unlock_irq_rcu_node(rnp);
45fed3e7 2015 return false;
f7be8209 2016 }
7d0ae808 2017 WRITE_ONCE(rsp->gp_flags, 0); /* Clear all flags: New grace period. */
b3dbec76 2018
f7be8209
PM
2019 if (WARN_ON_ONCE(rcu_gp_in_progress(rsp))) {
2020 /*
2021 * Grace period already in progress, don't start another.
2022 * Not supposed to be able to happen.
2023 */
67c583a7 2024 raw_spin_unlock_irq_rcu_node(rnp);
45fed3e7 2025 return false;
7fdefc10
PM
2026 }
2027
7fdefc10 2028 /* Advance to a new grace period and initialize state. */
26cdfedf 2029 record_gp_stall_check_time(rsp);
765a3f4f
PM
2030 /* Record GP times before starting GP, hence smp_store_release(). */
2031 smp_store_release(&rsp->gpnum, rsp->gpnum + 1);
f7f7bac9 2032 trace_rcu_grace_period(rsp->name, rsp->gpnum, TPS("start"));
67c583a7 2033 raw_spin_unlock_irq_rcu_node(rnp);
7fdefc10 2034
0aa04b05
PM
2035 /*
2036 * Apply per-leaf buffered online and offline operations to the
2037 * rcu_node tree. Note that this new grace period need not wait
2038 * for subsequent online CPUs, and that quiescent-state forcing
2039 * will handle subsequent offline CPUs.
2040 */
2041 rcu_for_each_leaf_node(rsp, rnp) {
0f41c0dd 2042 rcu_gp_slow(rsp, gp_preinit_delay);
2a67e741 2043 raw_spin_lock_irq_rcu_node(rnp);
0aa04b05
PM
2044 if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
2045 !rnp->wait_blkd_tasks) {
2046 /* Nothing to do on this leaf rcu_node structure. */
67c583a7 2047 raw_spin_unlock_irq_rcu_node(rnp);
0aa04b05
PM
2048 continue;
2049 }
2050
2051 /* Record old state, apply changes to ->qsmaskinit field. */
2052 oldmask = rnp->qsmaskinit;
2053 rnp->qsmaskinit = rnp->qsmaskinitnext;
2054
2055 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
2056 if (!oldmask != !rnp->qsmaskinit) {
2057 if (!oldmask) /* First online CPU for this rcu_node. */
2058 rcu_init_new_rnp(rnp);
2059 else if (rcu_preempt_has_tasks(rnp)) /* blocked tasks */
2060 rnp->wait_blkd_tasks = true;
2061 else /* Last offline CPU and can propagate. */
2062 rcu_cleanup_dead_rnp(rnp);
2063 }
2064
2065 /*
2066 * If all waited-on tasks from prior grace period are
2067 * done, and if all this rcu_node structure's CPUs are
2068 * still offline, propagate up the rcu_node tree and
2069 * clear ->wait_blkd_tasks. Otherwise, if one of this
2070 * rcu_node structure's CPUs has since come back online,
2071 * simply clear ->wait_blkd_tasks (but rcu_cleanup_dead_rnp()
2072 * checks for this, so just call it unconditionally).
2073 */
2074 if (rnp->wait_blkd_tasks &&
2075 (!rcu_preempt_has_tasks(rnp) ||
2076 rnp->qsmaskinit)) {
2077 rnp->wait_blkd_tasks = false;
2078 rcu_cleanup_dead_rnp(rnp);
2079 }
2080
67c583a7 2081 raw_spin_unlock_irq_rcu_node(rnp);
0aa04b05 2082 }
7fdefc10
PM
2083
2084 /*
2085 * Set the quiescent-state-needed bits in all the rcu_node
2086 * structures for all currently online CPUs in breadth-first order,
2087 * starting from the root rcu_node structure, relying on the layout
2088 * of the tree within the rsp->node[] array. Note that other CPUs
2089 * will access only the leaves of the hierarchy, thus seeing that no
2090 * grace period is in progress, at least until the corresponding
590d1757 2091 * leaf node has been initialized.
7fdefc10
PM
2092 *
2093 * The grace period cannot complete until the initialization
2094 * process finishes, because this kthread handles both.
2095 */
2096 rcu_for_each_node_breadth_first(rsp, rnp) {
0f41c0dd 2097 rcu_gp_slow(rsp, gp_init_delay);
2a67e741 2098 raw_spin_lock_irq_rcu_node(rnp);
b3dbec76 2099 rdp = this_cpu_ptr(rsp->rda);
7fdefc10
PM
2100 rcu_preempt_check_blocked_tasks(rnp);
2101 rnp->qsmask = rnp->qsmaskinit;
7d0ae808 2102 WRITE_ONCE(rnp->gpnum, rsp->gpnum);
3f47da0f 2103 if (WARN_ON_ONCE(rnp->completed != rsp->completed))
7d0ae808 2104 WRITE_ONCE(rnp->completed, rsp->completed);
7fdefc10 2105 if (rnp == rdp->mynode)
48a7639c 2106 (void)__note_gp_changes(rsp, rnp, rdp);
7fdefc10
PM
2107 rcu_preempt_boost_start_gp(rnp);
2108 trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
2109 rnp->level, rnp->grplo,
2110 rnp->grphi, rnp->qsmask);
67c583a7 2111 raw_spin_unlock_irq_rcu_node(rnp);
bde6c3aa 2112 cond_resched_rcu_qs();
7d0ae808 2113 WRITE_ONCE(rsp->gp_activity, jiffies);
7fdefc10 2114 }
b3dbec76 2115
45fed3e7 2116 return true;
7fdefc10 2117}
b3dbec76 2118
b9a425cf
PM
2119/*
2120 * Helper function for wait_event_interruptible_timeout() wakeup
2121 * at force-quiescent-state time.
2122 */
2123static bool rcu_gp_fqs_check_wake(struct rcu_state *rsp, int *gfp)
2124{
2125 struct rcu_node *rnp = rcu_get_root(rsp);
2126
2127 /* Someone like call_rcu() requested a force-quiescent-state scan. */
2128 *gfp = READ_ONCE(rsp->gp_flags);
2129 if (*gfp & RCU_GP_FLAG_FQS)
2130 return true;
2131
2132 /* The current grace period has completed. */
2133 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
2134 return true;
2135
2136 return false;
2137}
2138
4cdfc175
PM
2139/*
2140 * Do one round of quiescent-state forcing.
2141 */
77f81fe0 2142static void rcu_gp_fqs(struct rcu_state *rsp, bool first_time)
4cdfc175 2143{
217af2a2
PM
2144 bool isidle = false;
2145 unsigned long maxj;
4cdfc175
PM
2146 struct rcu_node *rnp = rcu_get_root(rsp);
2147
7d0ae808 2148 WRITE_ONCE(rsp->gp_activity, jiffies);
4cdfc175 2149 rsp->n_force_qs++;
77f81fe0 2150 if (first_time) {
4cdfc175 2151 /* Collect dyntick-idle snapshots. */
0edd1b17 2152 if (is_sysidle_rcu_state(rsp)) {
e02b2edf 2153 isidle = true;
0edd1b17
PM
2154 maxj = jiffies - ULONG_MAX / 4;
2155 }
217af2a2
PM
2156 force_qs_rnp(rsp, dyntick_save_progress_counter,
2157 &isidle, &maxj);
0edd1b17 2158 rcu_sysidle_report_gp(rsp, isidle, maxj);
4cdfc175
PM
2159 } else {
2160 /* Handle dyntick-idle and offline CPUs. */
675da67f 2161 isidle = true;
217af2a2 2162 force_qs_rnp(rsp, rcu_implicit_dynticks_qs, &isidle, &maxj);
4cdfc175
PM
2163 }
2164 /* Clear flag to prevent immediate re-entry. */
7d0ae808 2165 if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
2a67e741 2166 raw_spin_lock_irq_rcu_node(rnp);
7d0ae808
PM
2167 WRITE_ONCE(rsp->gp_flags,
2168 READ_ONCE(rsp->gp_flags) & ~RCU_GP_FLAG_FQS);
67c583a7 2169 raw_spin_unlock_irq_rcu_node(rnp);
4cdfc175 2170 }
4cdfc175
PM
2171}
2172
7fdefc10
PM
2173/*
2174 * Clean up after the old grace period.
2175 */
4cdfc175 2176static void rcu_gp_cleanup(struct rcu_state *rsp)
7fdefc10
PM
2177{
2178 unsigned long gp_duration;
48a7639c 2179 bool needgp = false;
dae6e64d 2180 int nocb = 0;
7fdefc10
PM
2181 struct rcu_data *rdp;
2182 struct rcu_node *rnp = rcu_get_root(rsp);
abedf8e2 2183 struct swait_queue_head *sq;
b3dbec76 2184
7d0ae808 2185 WRITE_ONCE(rsp->gp_activity, jiffies);
2a67e741 2186 raw_spin_lock_irq_rcu_node(rnp);
7fdefc10
PM
2187 gp_duration = jiffies - rsp->gp_start;
2188 if (gp_duration > rsp->gp_max)
2189 rsp->gp_max = gp_duration;
b3dbec76 2190
7fdefc10
PM
2191 /*
2192 * We know the grace period is complete, but to everyone else
2193 * it appears to still be ongoing. But it is also the case
2194 * that to everyone else it looks like there is nothing that
2195 * they can do to advance the grace period. It is therefore
2196 * safe for us to drop the lock in order to mark the grace
2197 * period as completed in all of the rcu_node structures.
7fdefc10 2198 */
67c583a7 2199 raw_spin_unlock_irq_rcu_node(rnp);
b3dbec76 2200
5d4b8659
PM
2201 /*
2202 * Propagate new ->completed value to rcu_node structures so
2203 * that other CPUs don't have to wait until the start of the next
2204 * grace period to process their callbacks. This also avoids
2205 * some nasty RCU grace-period initialization races by forcing
2206 * the end of the current grace period to be completely recorded in
2207 * all of the rcu_node structures before the beginning of the next
2208 * grace period is recorded in any of the rcu_node structures.
2209 */
2210 rcu_for_each_node_breadth_first(rsp, rnp) {
2a67e741 2211 raw_spin_lock_irq_rcu_node(rnp);
5c60d25f
PM
2212 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
2213 WARN_ON_ONCE(rnp->qsmask);
7d0ae808 2214 WRITE_ONCE(rnp->completed, rsp->gpnum);
b11cc576
PM
2215 rdp = this_cpu_ptr(rsp->rda);
2216 if (rnp == rdp->mynode)
48a7639c 2217 needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
78e4bc34 2218 /* smp_mb() provided by prior unlock-lock pair. */
0446be48 2219 nocb += rcu_future_gp_cleanup(rsp, rnp);
065bb78c 2220 sq = rcu_nocb_gp_get(rnp);
67c583a7 2221 raw_spin_unlock_irq_rcu_node(rnp);
065bb78c 2222 rcu_nocb_gp_cleanup(sq);
bde6c3aa 2223 cond_resched_rcu_qs();
7d0ae808 2224 WRITE_ONCE(rsp->gp_activity, jiffies);
0f41c0dd 2225 rcu_gp_slow(rsp, gp_cleanup_delay);
7fdefc10 2226 }
5d4b8659 2227 rnp = rcu_get_root(rsp);
2a67e741 2228 raw_spin_lock_irq_rcu_node(rnp); /* Order GP before ->completed update. */
dae6e64d 2229 rcu_nocb_gp_set(rnp, nocb);
7fdefc10 2230
765a3f4f 2231 /* Declare grace period done. */
7d0ae808 2232 WRITE_ONCE(rsp->completed, rsp->gpnum);
f7f7bac9 2233 trace_rcu_grace_period(rsp->name, rsp->completed, TPS("end"));
77f81fe0 2234 rsp->gp_state = RCU_GP_IDLE;
5d4b8659 2235 rdp = this_cpu_ptr(rsp->rda);
48a7639c
PM
2236 /* Advance CBs to reduce false positives below. */
2237 needgp = rcu_advance_cbs(rsp, rnp, rdp) || needgp;
2238 if (needgp || cpu_needs_another_gp(rsp, rdp)) {
7d0ae808 2239 WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
bb311ecc 2240 trace_rcu_grace_period(rsp->name,
7d0ae808 2241 READ_ONCE(rsp->gpnum),
bb311ecc
PM
2242 TPS("newreq"));
2243 }
67c583a7 2244 raw_spin_unlock_irq_rcu_node(rnp);
7fdefc10
PM
2245}
2246
2247/*
2248 * Body of kthread that handles grace periods.
2249 */
2250static int __noreturn rcu_gp_kthread(void *arg)
2251{
77f81fe0 2252 bool first_gp_fqs;
88d6df61 2253 int gf;
d40011f6 2254 unsigned long j;
4cdfc175 2255 int ret;
7fdefc10
PM
2256 struct rcu_state *rsp = arg;
2257 struct rcu_node *rnp = rcu_get_root(rsp);
2258
5871968d 2259 rcu_bind_gp_kthread();
7fdefc10
PM
2260 for (;;) {
2261
2262 /* Handle grace-period start. */
2263 for (;;) {
63c4db78 2264 trace_rcu_grace_period(rsp->name,
7d0ae808 2265 READ_ONCE(rsp->gpnum),
63c4db78 2266 TPS("reqwait"));
afea227f 2267 rsp->gp_state = RCU_GP_WAIT_GPS;
abedf8e2 2268 swait_event_interruptible(rsp->gp_wq,
7d0ae808 2269 READ_ONCE(rsp->gp_flags) &
4cdfc175 2270 RCU_GP_FLAG_INIT);
319362c9 2271 rsp->gp_state = RCU_GP_DONE_GPS;
78e4bc34 2272 /* Locking provides needed memory barrier. */
f7be8209 2273 if (rcu_gp_init(rsp))
7fdefc10 2274 break;
bde6c3aa 2275 cond_resched_rcu_qs();
7d0ae808 2276 WRITE_ONCE(rsp->gp_activity, jiffies);
73a860cd 2277 WARN_ON(signal_pending(current));
63c4db78 2278 trace_rcu_grace_period(rsp->name,
7d0ae808 2279 READ_ONCE(rsp->gpnum),
63c4db78 2280 TPS("reqwaitsig"));
7fdefc10 2281 }
cabc49c1 2282
4cdfc175 2283 /* Handle quiescent-state forcing. */
77f81fe0 2284 first_gp_fqs = true;
d40011f6
PM
2285 j = jiffies_till_first_fqs;
2286 if (j > HZ) {
2287 j = HZ;
2288 jiffies_till_first_fqs = HZ;
2289 }
88d6df61 2290 ret = 0;
cabc49c1 2291 for (;;) {
8c7c4829 2292 if (!ret) {
88d6df61 2293 rsp->jiffies_force_qs = jiffies + j;
8c7c4829
PM
2294 WRITE_ONCE(rsp->jiffies_kick_kthreads,
2295 jiffies + 3 * j);
2296 }
63c4db78 2297 trace_rcu_grace_period(rsp->name,
7d0ae808 2298 READ_ONCE(rsp->gpnum),
63c4db78 2299 TPS("fqswait"));
afea227f 2300 rsp->gp_state = RCU_GP_WAIT_FQS;
abedf8e2 2301 ret = swait_event_interruptible_timeout(rsp->gp_wq,
b9a425cf 2302 rcu_gp_fqs_check_wake(rsp, &gf), j);
32bb1c79 2303 rsp->gp_state = RCU_GP_DOING_FQS;
78e4bc34 2304 /* Locking provides needed memory barriers. */
4cdfc175 2305 /* If grace period done, leave loop. */
7d0ae808 2306 if (!READ_ONCE(rnp->qsmask) &&
4cdfc175 2307 !rcu_preempt_blocked_readers_cgp(rnp))
cabc49c1 2308 break;
4cdfc175 2309 /* If time for quiescent-state forcing, do it. */
88d6df61
PM
2310 if (ULONG_CMP_GE(jiffies, rsp->jiffies_force_qs) ||
2311 (gf & RCU_GP_FLAG_FQS)) {
63c4db78 2312 trace_rcu_grace_period(rsp->name,
7d0ae808 2313 READ_ONCE(rsp->gpnum),
63c4db78 2314 TPS("fqsstart"));
77f81fe0
PM
2315 rcu_gp_fqs(rsp, first_gp_fqs);
2316 first_gp_fqs = false;
63c4db78 2317 trace_rcu_grace_period(rsp->name,
7d0ae808 2318 READ_ONCE(rsp->gpnum),
63c4db78 2319 TPS("fqsend"));
bde6c3aa 2320 cond_resched_rcu_qs();
7d0ae808 2321 WRITE_ONCE(rsp->gp_activity, jiffies);
fcfd0a23
PM
2322 ret = 0; /* Force full wait till next FQS. */
2323 j = jiffies_till_next_fqs;
2324 if (j > HZ) {
2325 j = HZ;
2326 jiffies_till_next_fqs = HZ;
2327 } else if (j < 1) {
2328 j = 1;
2329 jiffies_till_next_fqs = 1;
2330 }
4cdfc175
PM
2331 } else {
2332 /* Deal with stray signal. */
bde6c3aa 2333 cond_resched_rcu_qs();
7d0ae808 2334 WRITE_ONCE(rsp->gp_activity, jiffies);
73a860cd 2335 WARN_ON(signal_pending(current));
63c4db78 2336 trace_rcu_grace_period(rsp->name,
7d0ae808 2337 READ_ONCE(rsp->gpnum),
63c4db78 2338 TPS("fqswaitsig"));
fcfd0a23
PM
2339 ret = 1; /* Keep old FQS timing. */
2340 j = jiffies;
2341 if (time_after(jiffies, rsp->jiffies_force_qs))
2342 j = 1;
2343 else
2344 j = rsp->jiffies_force_qs - j;
d40011f6 2345 }
cabc49c1 2346 }
4cdfc175
PM
2347
2348 /* Handle grace-period end. */
319362c9 2349 rsp->gp_state = RCU_GP_CLEANUP;
4cdfc175 2350 rcu_gp_cleanup(rsp);
319362c9 2351 rsp->gp_state = RCU_GP_CLEANED;
b3dbec76 2352 }
b3dbec76
PM
2353}
2354
64db4cff
PM
2355/*
2356 * Start a new RCU grace period if warranted, re-initializing the hierarchy
2357 * in preparation for detecting the next grace period. The caller must hold
b8462084 2358 * the root node's ->lock and hard irqs must be disabled.
e5601400
PM
2359 *
2360 * Note that it is legal for a dying CPU (which is marked as offline) to
2361 * invoke this function. This can happen when the dying CPU reports its
2362 * quiescent state.
48a7639c
PM
2363 *
2364 * Returns true if the grace-period kthread must be awakened.
64db4cff 2365 */
48a7639c 2366static bool
910ee45d
PM
2367rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
2368 struct rcu_data *rdp)
64db4cff 2369{
b8462084 2370 if (!rsp->gp_kthread || !cpu_needs_another_gp(rsp, rdp)) {
afe24b12 2371 /*
b3dbec76 2372 * Either we have not yet spawned the grace-period
62da1921
PM
2373 * task, this CPU does not need another grace period,
2374 * or a grace period is already in progress.
b3dbec76 2375 * Either way, don't start a new grace period.
afe24b12 2376 */
48a7639c 2377 return false;
afe24b12 2378 }
7d0ae808
PM
2379 WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
2380 trace_rcu_grace_period(rsp->name, READ_ONCE(rsp->gpnum),
bb311ecc 2381 TPS("newreq"));
62da1921 2382
016a8d5b
SR
2383 /*
2384 * We can't do wakeups while holding the rnp->lock, as that
1eafd31c 2385 * could cause possible deadlocks with the rq->lock. Defer
48a7639c 2386 * the wakeup to our caller.
016a8d5b 2387 */
48a7639c 2388 return true;
64db4cff
PM
2389}
2390
910ee45d
PM
2391/*
2392 * Similar to rcu_start_gp_advanced(), but also advance the calling CPU's
2393 * callbacks. Note that rcu_start_gp_advanced() cannot do this because it
2394 * is invoked indirectly from rcu_advance_cbs(), which would result in
2395 * endless recursion -- or would do so if it wasn't for the self-deadlock
2396 * that is encountered beforehand.
48a7639c
PM
2397 *
2398 * Returns true if the grace-period kthread needs to be awakened.
910ee45d 2399 */
48a7639c 2400static bool rcu_start_gp(struct rcu_state *rsp)
910ee45d
PM
2401{
2402 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
2403 struct rcu_node *rnp = rcu_get_root(rsp);
48a7639c 2404 bool ret = false;
910ee45d
PM
2405
2406 /*
2407 * If there is no grace period in progress right now, any
2408 * callbacks we have up to this point will be satisfied by the
2409 * next grace period. Also, advancing the callbacks reduces the
2410 * probability of false positives from cpu_needs_another_gp()
2411 * resulting in pointless grace periods. So, advance callbacks
2412 * then start the grace period!
2413 */
48a7639c
PM
2414 ret = rcu_advance_cbs(rsp, rnp, rdp) || ret;
2415 ret = rcu_start_gp_advanced(rsp, rnp, rdp) || ret;
2416 return ret;
910ee45d
PM
2417}
2418
f41d911f 2419/*
8994515c
PM
2420 * Report a full set of quiescent states to the specified rcu_state data
2421 * structure. Invoke rcu_gp_kthread_wake() to awaken the grace-period
2422 * kthread if another grace period is required. Whether we wake
2423 * the grace-period kthread or it awakens itself for the next round
2424 * of quiescent-state forcing, that kthread will clean up after the
2425 * just-completed grace period. Note that the caller must hold rnp->lock,
2426 * which is released before return.
f41d911f 2427 */
d3f6bad3 2428static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
fc2219d4 2429 __releases(rcu_get_root(rsp)->lock)
f41d911f 2430{
fc2219d4 2431 WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
cd73ca21 2432 WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
67c583a7 2433 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
94d44776 2434 rcu_gp_kthread_wake(rsp);
f41d911f
PM
2435}
2436
64db4cff 2437/*
d3f6bad3
PM
2438 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
2439 * Allows quiescent states for a group of CPUs to be reported at one go
2440 * to the specified rcu_node structure, though all the CPUs in the group
654e9533
PM
2441 * must be represented by the same rcu_node structure (which need not be a
2442 * leaf rcu_node structure, though it often will be). The gps parameter
2443 * is the grace-period snapshot, which means that the quiescent states
2444 * are valid only if rnp->gpnum is equal to gps. That structure's lock
2445 * must be held upon entry, and it is released before return.
64db4cff
PM
2446 */
2447static void
d3f6bad3 2448rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
654e9533 2449 struct rcu_node *rnp, unsigned long gps, unsigned long flags)
64db4cff
PM
2450 __releases(rnp->lock)
2451{
654e9533 2452 unsigned long oldmask = 0;
28ecd580
PM
2453 struct rcu_node *rnp_c;
2454
64db4cff
PM
2455 /* Walk up the rcu_node hierarchy. */
2456 for (;;) {
654e9533 2457 if (!(rnp->qsmask & mask) || rnp->gpnum != gps) {
64db4cff 2458
654e9533
PM
2459 /*
2460 * Our bit has already been cleared, or the
2461 * relevant grace period is already over, so done.
2462 */
67c583a7 2463 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff
PM
2464 return;
2465 }
654e9533 2466 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
64db4cff 2467 rnp->qsmask &= ~mask;
d4c08f2a
PM
2468 trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
2469 mask, rnp->qsmask, rnp->level,
2470 rnp->grplo, rnp->grphi,
2471 !!rnp->gp_tasks);
27f4d280 2472 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
64db4cff
PM
2473
2474 /* Other bits still set at this level, so done. */
67c583a7 2475 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff
PM
2476 return;
2477 }
2478 mask = rnp->grpmask;
2479 if (rnp->parent == NULL) {
2480
2481 /* No more levels. Exit loop holding root lock. */
2482
2483 break;
2484 }
67c583a7 2485 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
28ecd580 2486 rnp_c = rnp;
64db4cff 2487 rnp = rnp->parent;
2a67e741 2488 raw_spin_lock_irqsave_rcu_node(rnp, flags);
654e9533 2489 oldmask = rnp_c->qsmask;
64db4cff
PM
2490 }
2491
2492 /*
2493 * Get here if we are the last CPU to pass through a quiescent
d3f6bad3 2494 * state for this grace period. Invoke rcu_report_qs_rsp()
f41d911f 2495 * to clean up and start the next grace period if one is needed.
64db4cff 2496 */
d3f6bad3 2497 rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
64db4cff
PM
2498}
2499
cc99a310
PM
2500/*
2501 * Record a quiescent state for all tasks that were previously queued
2502 * on the specified rcu_node structure and that were blocking the current
2503 * RCU grace period. The caller must hold the specified rnp->lock with
2504 * irqs disabled, and this lock is released upon return, but irqs remain
2505 * disabled.
2506 */
0aa04b05 2507static void rcu_report_unblock_qs_rnp(struct rcu_state *rsp,
cc99a310
PM
2508 struct rcu_node *rnp, unsigned long flags)
2509 __releases(rnp->lock)
2510{
654e9533 2511 unsigned long gps;
cc99a310
PM
2512 unsigned long mask;
2513 struct rcu_node *rnp_p;
2514
a77da14c
PM
2515 if (rcu_state_p == &rcu_sched_state || rsp != rcu_state_p ||
2516 rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
67c583a7 2517 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
cc99a310
PM
2518 return; /* Still need more quiescent states! */
2519 }
2520
2521 rnp_p = rnp->parent;
2522 if (rnp_p == NULL) {
2523 /*
a77da14c
PM
2524 * Only one rcu_node structure in the tree, so don't
2525 * try to report up to its nonexistent parent!
cc99a310
PM
2526 */
2527 rcu_report_qs_rsp(rsp, flags);
2528 return;
2529 }
2530
654e9533
PM
2531 /* Report up the rest of the hierarchy, tracking current ->gpnum. */
2532 gps = rnp->gpnum;
cc99a310 2533 mask = rnp->grpmask;
67c583a7 2534 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
2a67e741 2535 raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
654e9533 2536 rcu_report_qs_rnp(mask, rsp, rnp_p, gps, flags);
cc99a310
PM
2537}
2538
64db4cff 2539/*
d3f6bad3 2540 * Record a quiescent state for the specified CPU to that CPU's rcu_data
4b455dc3 2541 * structure. This must be called from the specified CPU.
64db4cff
PM
2542 */
2543static void
d7d6a11e 2544rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp)
64db4cff
PM
2545{
2546 unsigned long flags;
2547 unsigned long mask;
48a7639c 2548 bool needwake;
64db4cff
PM
2549 struct rcu_node *rnp;
2550
2551 rnp = rdp->mynode;
2a67e741 2552 raw_spin_lock_irqsave_rcu_node(rnp, flags);
5b74c458 2553 if ((rdp->cpu_no_qs.b.norm &&
5cd37193
PM
2554 rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr)) ||
2555 rdp->gpnum != rnp->gpnum || rnp->completed == rnp->gpnum ||
2556 rdp->gpwrap) {
64db4cff
PM
2557
2558 /*
e4cc1f22
PM
2559 * The grace period in which this quiescent state was
2560 * recorded has ended, so don't report it upwards.
2561 * We will instead need a new quiescent state that lies
2562 * within the current grace period.
64db4cff 2563 */
5b74c458 2564 rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
5cd37193 2565 rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_qs_ctr);
67c583a7 2566 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff
PM
2567 return;
2568 }
2569 mask = rdp->grpmask;
2570 if ((rnp->qsmask & mask) == 0) {
67c583a7 2571 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff 2572 } else {
bb53e416 2573 rdp->core_needs_qs = false;
64db4cff
PM
2574
2575 /*
2576 * This GP can't end until cpu checks in, so all of our
2577 * callbacks can be processed during the next GP.
2578 */
48a7639c 2579 needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
64db4cff 2580
654e9533
PM
2581 rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
2582 /* ^^^ Released rnp->lock */
48a7639c
PM
2583 if (needwake)
2584 rcu_gp_kthread_wake(rsp);
64db4cff
PM
2585 }
2586}
2587
2588/*
2589 * Check to see if there is a new grace period of which this CPU
2590 * is not yet aware, and if so, set up local rcu_data state for it.
2591 * Otherwise, see if this CPU has just passed through its first
2592 * quiescent state for this grace period, and record that fact if so.
2593 */
2594static void
2595rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
2596{
05eb552b
PM
2597 /* Check for grace-period ends and beginnings. */
2598 note_gp_changes(rsp, rdp);
64db4cff
PM
2599
2600 /*
2601 * Does this CPU still need to do its part for current grace period?
2602 * If no, return and let the other CPUs do their part as well.
2603 */
97c668b8 2604 if (!rdp->core_needs_qs)
64db4cff
PM
2605 return;
2606
2607 /*
2608 * Was there a quiescent state since the beginning of the grace
2609 * period? If no, then exit and wait for the next call.
2610 */
5b74c458 2611 if (rdp->cpu_no_qs.b.norm &&
5cd37193 2612 rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr))
64db4cff
PM
2613 return;
2614
d3f6bad3
PM
2615 /*
2616 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
2617 * judge of that).
2618 */
d7d6a11e 2619 rcu_report_qs_rdp(rdp->cpu, rsp, rdp);
64db4cff
PM
2620}
2621
e74f4c45 2622/*
b1420f1c
PM
2623 * Send the specified CPU's RCU callbacks to the orphanage. The
2624 * specified CPU must be offline, and the caller must hold the
7b2e6011 2625 * ->orphan_lock.
e74f4c45 2626 */
b1420f1c
PM
2627static void
2628rcu_send_cbs_to_orphanage(int cpu, struct rcu_state *rsp,
2629 struct rcu_node *rnp, struct rcu_data *rdp)
e74f4c45 2630{
3fbfbf7a 2631 /* No-CBs CPUs do not have orphanable callbacks. */
ea46351c 2632 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) || rcu_is_nocb_cpu(rdp->cpu))
3fbfbf7a
PM
2633 return;
2634
b1420f1c
PM
2635 /*
2636 * Orphan the callbacks. First adjust the counts. This is safe
abfd6e58
PM
2637 * because _rcu_barrier() excludes CPU-hotplug operations, so it
2638 * cannot be running now. Thus no memory barrier is required.
b1420f1c 2639 */
a50c3af9 2640 if (rdp->nxtlist != NULL) {
b1420f1c
PM
2641 rsp->qlen_lazy += rdp->qlen_lazy;
2642 rsp->qlen += rdp->qlen;
2643 rdp->n_cbs_orphaned += rdp->qlen;
a50c3af9 2644 rdp->qlen_lazy = 0;
7d0ae808 2645 WRITE_ONCE(rdp->qlen, 0);
a50c3af9
PM
2646 }
2647
2648 /*
b1420f1c
PM
2649 * Next, move those callbacks still needing a grace period to
2650 * the orphanage, where some other CPU will pick them up.
2651 * Some of the callbacks might have gone partway through a grace
2652 * period, but that is too bad. They get to start over because we
2653 * cannot assume that grace periods are synchronized across CPUs.
2654 * We don't bother updating the ->nxttail[] array yet, instead
2655 * we just reset the whole thing later on.
a50c3af9 2656 */
b1420f1c
PM
2657 if (*rdp->nxttail[RCU_DONE_TAIL] != NULL) {
2658 *rsp->orphan_nxttail = *rdp->nxttail[RCU_DONE_TAIL];
2659 rsp->orphan_nxttail = rdp->nxttail[RCU_NEXT_TAIL];
2660 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
a50c3af9
PM
2661 }
2662
2663 /*
b1420f1c
PM
2664 * Then move the ready-to-invoke callbacks to the orphanage,
2665 * where some other CPU will pick them up. These will not be
2666 * required to pass though another grace period: They are done.
a50c3af9 2667 */
e5601400 2668 if (rdp->nxtlist != NULL) {
b1420f1c
PM
2669 *rsp->orphan_donetail = rdp->nxtlist;
2670 rsp->orphan_donetail = rdp->nxttail[RCU_DONE_TAIL];
e5601400 2671 }
e74f4c45 2672
b33078b6
PM
2673 /*
2674 * Finally, initialize the rcu_data structure's list to empty and
2675 * disallow further callbacks on this CPU.
2676 */
3f5d3ea6 2677 init_callback_list(rdp);
b33078b6 2678 rdp->nxttail[RCU_NEXT_TAIL] = NULL;
b1420f1c
PM
2679}
2680
2681/*
2682 * Adopt the RCU callbacks from the specified rcu_state structure's
7b2e6011 2683 * orphanage. The caller must hold the ->orphan_lock.
b1420f1c 2684 */
96d3fd0d 2685static void rcu_adopt_orphan_cbs(struct rcu_state *rsp, unsigned long flags)
b1420f1c
PM
2686{
2687 int i;
fa07a58f 2688 struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
b1420f1c 2689
3fbfbf7a 2690 /* No-CBs CPUs are handled specially. */
ea46351c
PM
2691 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
2692 rcu_nocb_adopt_orphan_cbs(rsp, rdp, flags))
3fbfbf7a
PM
2693 return;
2694
b1420f1c
PM
2695 /* Do the accounting first. */
2696 rdp->qlen_lazy += rsp->qlen_lazy;
2697 rdp->qlen += rsp->qlen;
2698 rdp->n_cbs_adopted += rsp->qlen;
8f5af6f1
PM
2699 if (rsp->qlen_lazy != rsp->qlen)
2700 rcu_idle_count_callbacks_posted();
b1420f1c
PM
2701 rsp->qlen_lazy = 0;
2702 rsp->qlen = 0;
2703
2704 /*
2705 * We do not need a memory barrier here because the only way we
2706 * can get here if there is an rcu_barrier() in flight is if
2707 * we are the task doing the rcu_barrier().
2708 */
2709
2710 /* First adopt the ready-to-invoke callbacks. */
2711 if (rsp->orphan_donelist != NULL) {
2712 *rsp->orphan_donetail = *rdp->nxttail[RCU_DONE_TAIL];
2713 *rdp->nxttail[RCU_DONE_TAIL] = rsp->orphan_donelist;
2714 for (i = RCU_NEXT_SIZE - 1; i >= RCU_DONE_TAIL; i--)
2715 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
2716 rdp->nxttail[i] = rsp->orphan_donetail;
2717 rsp->orphan_donelist = NULL;
2718 rsp->orphan_donetail = &rsp->orphan_donelist;
2719 }
2720
2721 /* And then adopt the callbacks that still need a grace period. */
2722 if (rsp->orphan_nxtlist != NULL) {
2723 *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxtlist;
2724 rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxttail;
2725 rsp->orphan_nxtlist = NULL;
2726 rsp->orphan_nxttail = &rsp->orphan_nxtlist;
2727 }
2728}
2729
2730/*
2731 * Trace the fact that this CPU is going offline.
2732 */
2733static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
2734{
2735 RCU_TRACE(unsigned long mask);
2736 RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda));
2737 RCU_TRACE(struct rcu_node *rnp = rdp->mynode);
2738
ea46351c
PM
2739 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2740 return;
2741
b1420f1c 2742 RCU_TRACE(mask = rdp->grpmask);
e5601400
PM
2743 trace_rcu_grace_period(rsp->name,
2744 rnp->gpnum + 1 - !!(rnp->qsmask & mask),
f7f7bac9 2745 TPS("cpuofl"));
64db4cff
PM
2746}
2747
8af3a5e7
PM
2748/*
2749 * All CPUs for the specified rcu_node structure have gone offline,
2750 * and all tasks that were preempted within an RCU read-side critical
2751 * section while running on one of those CPUs have since exited their RCU
2752 * read-side critical section. Some other CPU is reporting this fact with
2753 * the specified rcu_node structure's ->lock held and interrupts disabled.
2754 * This function therefore goes up the tree of rcu_node structures,
2755 * clearing the corresponding bits in the ->qsmaskinit fields. Note that
2756 * the leaf rcu_node structure's ->qsmaskinit field has already been
2757 * updated
2758 *
2759 * This function does check that the specified rcu_node structure has
2760 * all CPUs offline and no blocked tasks, so it is OK to invoke it
2761 * prematurely. That said, invoking it after the fact will cost you
2762 * a needless lock acquisition. So once it has done its work, don't
2763 * invoke it again.
2764 */
2765static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
2766{
2767 long mask;
2768 struct rcu_node *rnp = rnp_leaf;
2769
ea46351c
PM
2770 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
2771 rnp->qsmaskinit || rcu_preempt_has_tasks(rnp))
8af3a5e7
PM
2772 return;
2773 for (;;) {
2774 mask = rnp->grpmask;
2775 rnp = rnp->parent;
2776 if (!rnp)
2777 break;
2a67e741 2778 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
8af3a5e7 2779 rnp->qsmaskinit &= ~mask;
0aa04b05 2780 rnp->qsmask &= ~mask;
8af3a5e7 2781 if (rnp->qsmaskinit) {
67c583a7
BF
2782 raw_spin_unlock_rcu_node(rnp);
2783 /* irqs remain disabled. */
8af3a5e7
PM
2784 return;
2785 }
67c583a7 2786 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
8af3a5e7
PM
2787 }
2788}
2789
64db4cff 2790/*
e5601400 2791 * The CPU has been completely removed, and some other CPU is reporting
b1420f1c
PM
2792 * this fact from process context. Do the remainder of the cleanup,
2793 * including orphaning the outgoing CPU's RCU callbacks, and also
1331e7a1
PM
2794 * adopting them. There can only be one CPU hotplug operation at a time,
2795 * so no other CPU can be attempting to update rcu_cpu_kthread_task.
64db4cff 2796 */
e5601400 2797static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
64db4cff 2798{
2036d94a 2799 unsigned long flags;
e5601400 2800 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
b1420f1c 2801 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
e5601400 2802
ea46351c
PM
2803 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2804 return;
2805
2036d94a 2806 /* Adjust any no-longer-needed kthreads. */
5d01bbd1 2807 rcu_boost_kthread_setaffinity(rnp, -1);
2036d94a 2808
b1420f1c 2809 /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */
78043c46 2810 raw_spin_lock_irqsave(&rsp->orphan_lock, flags);
b1420f1c 2811 rcu_send_cbs_to_orphanage(cpu, rsp, rnp, rdp);
96d3fd0d 2812 rcu_adopt_orphan_cbs(rsp, flags);
a8f4cbad 2813 raw_spin_unlock_irqrestore(&rsp->orphan_lock, flags);
b1420f1c 2814
cf01537e
PM
2815 WARN_ONCE(rdp->qlen != 0 || rdp->nxtlist != NULL,
2816 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n",
2817 cpu, rdp->qlen, rdp->nxtlist);
64db4cff
PM
2818}
2819
64db4cff
PM
2820/*
2821 * Invoke any RCU callbacks that have made it to the end of their grace
2822 * period. Thottle as specified by rdp->blimit.
2823 */
37c72e56 2824static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
64db4cff
PM
2825{
2826 unsigned long flags;
2827 struct rcu_head *next, *list, **tail;
878d7439
ED
2828 long bl, count, count_lazy;
2829 int i;
64db4cff 2830
dc35c893 2831 /* If no callbacks are ready, just return. */
29c00b4a 2832 if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
486e2593 2833 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0);
7d0ae808 2834 trace_rcu_batch_end(rsp->name, 0, !!READ_ONCE(rdp->nxtlist),
4968c300
PM
2835 need_resched(), is_idle_task(current),
2836 rcu_is_callbacks_kthread());
64db4cff 2837 return;
29c00b4a 2838 }
64db4cff
PM
2839
2840 /*
2841 * Extract the list of ready callbacks, disabling to prevent
2842 * races with call_rcu() from interrupt handlers.
2843 */
2844 local_irq_save(flags);
8146c4e2 2845 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
29c00b4a 2846 bl = rdp->blimit;
486e2593 2847 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl);
64db4cff
PM
2848 list = rdp->nxtlist;
2849 rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
2850 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
2851 tail = rdp->nxttail[RCU_DONE_TAIL];
b41772ab
PM
2852 for (i = RCU_NEXT_SIZE - 1; i >= 0; i--)
2853 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
2854 rdp->nxttail[i] = &rdp->nxtlist;
64db4cff
PM
2855 local_irq_restore(flags);
2856
2857 /* Invoke callbacks. */
486e2593 2858 count = count_lazy = 0;
64db4cff
PM
2859 while (list) {
2860 next = list->next;
2861 prefetch(next);
551d55a9 2862 debug_rcu_head_unqueue(list);
486e2593
PM
2863 if (__rcu_reclaim(rsp->name, list))
2864 count_lazy++;
64db4cff 2865 list = next;
dff1672d
PM
2866 /* Stop only if limit reached and CPU has something to do. */
2867 if (++count >= bl &&
2868 (need_resched() ||
2869 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
64db4cff
PM
2870 break;
2871 }
2872
2873 local_irq_save(flags);
4968c300
PM
2874 trace_rcu_batch_end(rsp->name, count, !!list, need_resched(),
2875 is_idle_task(current),
2876 rcu_is_callbacks_kthread());
64db4cff
PM
2877
2878 /* Update count, and requeue any remaining callbacks. */
64db4cff
PM
2879 if (list != NULL) {
2880 *tail = rdp->nxtlist;
2881 rdp->nxtlist = list;
b41772ab
PM
2882 for (i = 0; i < RCU_NEXT_SIZE; i++)
2883 if (&rdp->nxtlist == rdp->nxttail[i])
2884 rdp->nxttail[i] = tail;
64db4cff
PM
2885 else
2886 break;
2887 }
b1420f1c
PM
2888 smp_mb(); /* List handling before counting for rcu_barrier(). */
2889 rdp->qlen_lazy -= count_lazy;
7d0ae808 2890 WRITE_ONCE(rdp->qlen, rdp->qlen - count);
b1420f1c 2891 rdp->n_cbs_invoked += count;
64db4cff
PM
2892
2893 /* Reinstate batch limit if we have worked down the excess. */
2894 if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
2895 rdp->blimit = blimit;
2896
37c72e56
PM
2897 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
2898 if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
2899 rdp->qlen_last_fqs_check = 0;
2900 rdp->n_force_qs_snap = rsp->n_force_qs;
2901 } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
2902 rdp->qlen_last_fqs_check = rdp->qlen;
cfca9279 2903 WARN_ON_ONCE((rdp->nxtlist == NULL) != (rdp->qlen == 0));
37c72e56 2904
64db4cff
PM
2905 local_irq_restore(flags);
2906
e0f23060 2907 /* Re-invoke RCU core processing if there are callbacks remaining. */
64db4cff 2908 if (cpu_has_callbacks_ready_to_invoke(rdp))
a46e0899 2909 invoke_rcu_core();
64db4cff
PM
2910}
2911
2912/*
2913 * Check to see if this CPU is in a non-context-switch quiescent state
2914 * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
e0f23060 2915 * Also schedule RCU core processing.
64db4cff 2916 *
9b2e4f18 2917 * This function must be called from hardirq context. It is normally
5403d367 2918 * invoked from the scheduling-clock interrupt.
64db4cff 2919 */
c3377c2d 2920void rcu_check_callbacks(int user)
64db4cff 2921{
f7f7bac9 2922 trace_rcu_utilization(TPS("Start scheduler-tick"));
a858af28 2923 increment_cpu_stall_ticks();
9b2e4f18 2924 if (user || rcu_is_cpu_rrupt_from_idle()) {
64db4cff
PM
2925
2926 /*
2927 * Get here if this CPU took its interrupt from user
2928 * mode or from the idle loop, and if this is not a
2929 * nested interrupt. In this case, the CPU is in
d6714c22 2930 * a quiescent state, so note it.
64db4cff
PM
2931 *
2932 * No memory barrier is required here because both
d6714c22
PM
2933 * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
2934 * variables that other CPUs neither access nor modify,
2935 * at least not while the corresponding CPU is online.
64db4cff
PM
2936 */
2937
284a8c93
PM
2938 rcu_sched_qs();
2939 rcu_bh_qs();
64db4cff
PM
2940
2941 } else if (!in_softirq()) {
2942
2943 /*
2944 * Get here if this CPU did not take its interrupt from
2945 * softirq, in other words, if it is not interrupting
2946 * a rcu_bh read-side critical section. This is an _bh
d6714c22 2947 * critical section, so note it.
64db4cff
PM
2948 */
2949
284a8c93 2950 rcu_bh_qs();
64db4cff 2951 }
86aea0e6 2952 rcu_preempt_check_callbacks();
e3950ecd 2953 if (rcu_pending())
a46e0899 2954 invoke_rcu_core();
8315f422
PM
2955 if (user)
2956 rcu_note_voluntary_context_switch(current);
f7f7bac9 2957 trace_rcu_utilization(TPS("End scheduler-tick"));
64db4cff
PM
2958}
2959
64db4cff
PM
2960/*
2961 * Scan the leaf rcu_node structures, processing dyntick state for any that
2962 * have not yet encountered a quiescent state, using the function specified.
27f4d280
PM
2963 * Also initiate boosting for any threads blocked on the root rcu_node.
2964 *
ee47eb9f 2965 * The caller must have suppressed start of new grace periods.
64db4cff 2966 */
217af2a2
PM
2967static void force_qs_rnp(struct rcu_state *rsp,
2968 int (*f)(struct rcu_data *rsp, bool *isidle,
2969 unsigned long *maxj),
2970 bool *isidle, unsigned long *maxj)
64db4cff 2971{
64db4cff
PM
2972 int cpu;
2973 unsigned long flags;
2974 unsigned long mask;
a0b6c9a7 2975 struct rcu_node *rnp;
64db4cff 2976
a0b6c9a7 2977 rcu_for_each_leaf_node(rsp, rnp) {
bde6c3aa 2978 cond_resched_rcu_qs();
64db4cff 2979 mask = 0;
2a67e741 2980 raw_spin_lock_irqsave_rcu_node(rnp, flags);
a0b6c9a7 2981 if (rnp->qsmask == 0) {
a77da14c
PM
2982 if (rcu_state_p == &rcu_sched_state ||
2983 rsp != rcu_state_p ||
2984 rcu_preempt_blocked_readers_cgp(rnp)) {
2985 /*
2986 * No point in scanning bits because they
2987 * are all zero. But we might need to
2988 * priority-boost blocked readers.
2989 */
2990 rcu_initiate_boost(rnp, flags);
2991 /* rcu_initiate_boost() releases rnp->lock */
2992 continue;
2993 }
2994 if (rnp->parent &&
2995 (rnp->parent->qsmask & rnp->grpmask)) {
2996 /*
2997 * Race between grace-period
2998 * initialization and task exiting RCU
2999 * read-side critical section: Report.
3000 */
3001 rcu_report_unblock_qs_rnp(rsp, rnp, flags);
3002 /* rcu_report_unblock_qs_rnp() rlses ->lock */
3003 continue;
3004 }
64db4cff 3005 }
bc75e999
MR
3006 for_each_leaf_node_possible_cpu(rnp, cpu) {
3007 unsigned long bit = leaf_node_cpu_bit(rnp, cpu);
0edd1b17 3008 if ((rnp->qsmask & bit) != 0) {
0edd1b17
PM
3009 if (f(per_cpu_ptr(rsp->rda, cpu), isidle, maxj))
3010 mask |= bit;
3011 }
64db4cff 3012 }
45f014c5 3013 if (mask != 0) {
654e9533
PM
3014 /* Idle/offline CPUs, report (releases rnp->lock. */
3015 rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
0aa04b05
PM
3016 } else {
3017 /* Nothing to do here, so just drop the lock. */
67c583a7 3018 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff 3019 }
64db4cff 3020 }
64db4cff
PM
3021}
3022
3023/*
3024 * Force quiescent states on reluctant CPUs, and also detect which
3025 * CPUs are in dyntick-idle mode.
3026 */
4cdfc175 3027static void force_quiescent_state(struct rcu_state *rsp)
64db4cff
PM
3028{
3029 unsigned long flags;
394f2769
PM
3030 bool ret;
3031 struct rcu_node *rnp;
3032 struct rcu_node *rnp_old = NULL;
3033
3034 /* Funnel through hierarchy to reduce memory contention. */
d860d403 3035 rnp = __this_cpu_read(rsp->rda->mynode);
394f2769 3036 for (; rnp != NULL; rnp = rnp->parent) {
7d0ae808 3037 ret = (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
394f2769
PM
3038 !raw_spin_trylock(&rnp->fqslock);
3039 if (rnp_old != NULL)
3040 raw_spin_unlock(&rnp_old->fqslock);
3041 if (ret) {
a792563b 3042 rsp->n_force_qs_lh++;
394f2769
PM
3043 return;
3044 }
3045 rnp_old = rnp;
3046 }
3047 /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
64db4cff 3048
394f2769 3049 /* Reached the root of the rcu_node tree, acquire lock. */
2a67e741 3050 raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
394f2769 3051 raw_spin_unlock(&rnp_old->fqslock);
7d0ae808 3052 if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
a792563b 3053 rsp->n_force_qs_lh++;
67c583a7 3054 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
4cdfc175 3055 return; /* Someone beat us to it. */
46a1e34e 3056 }
7d0ae808 3057 WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
67c583a7 3058 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
94d44776 3059 rcu_gp_kthread_wake(rsp);
64db4cff
PM
3060}
3061
64db4cff 3062/*
e0f23060
PM
3063 * This does the RCU core processing work for the specified rcu_state
3064 * and rcu_data structures. This may be called only from the CPU to
3065 * whom the rdp belongs.
64db4cff
PM
3066 */
3067static void
1bca8cf1 3068__rcu_process_callbacks(struct rcu_state *rsp)
64db4cff
PM
3069{
3070 unsigned long flags;
48a7639c 3071 bool needwake;
fa07a58f 3072 struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
64db4cff 3073
2e597558
PM
3074 WARN_ON_ONCE(rdp->beenonline == 0);
3075
64db4cff
PM
3076 /* Update RCU state based on any recent quiescent states. */
3077 rcu_check_quiescent_state(rsp, rdp);
3078
3079 /* Does this CPU require a not-yet-started grace period? */
dc35c893 3080 local_irq_save(flags);
64db4cff 3081 if (cpu_needs_another_gp(rsp, rdp)) {
6cf10081 3082 raw_spin_lock_rcu_node(rcu_get_root(rsp)); /* irqs disabled. */
48a7639c 3083 needwake = rcu_start_gp(rsp);
67c583a7 3084 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(rsp), flags);
48a7639c
PM
3085 if (needwake)
3086 rcu_gp_kthread_wake(rsp);
dc35c893
PM
3087 } else {
3088 local_irq_restore(flags);
64db4cff
PM
3089 }
3090
3091 /* If there are callbacks ready, invoke them. */
09223371 3092 if (cpu_has_callbacks_ready_to_invoke(rdp))
a46e0899 3093 invoke_rcu_callbacks(rsp, rdp);
96d3fd0d
PM
3094
3095 /* Do any needed deferred wakeups of rcuo kthreads. */
3096 do_nocb_deferred_wakeup(rdp);
09223371
SL
3097}
3098
64db4cff 3099/*
e0f23060 3100 * Do RCU core processing for the current CPU.
64db4cff 3101 */
0766f788 3102static __latent_entropy void rcu_process_callbacks(struct softirq_action *unused)
64db4cff 3103{
6ce75a23
PM
3104 struct rcu_state *rsp;
3105
bfa00b4c
PM
3106 if (cpu_is_offline(smp_processor_id()))
3107 return;
f7f7bac9 3108 trace_rcu_utilization(TPS("Start RCU core"));
6ce75a23
PM
3109 for_each_rcu_flavor(rsp)
3110 __rcu_process_callbacks(rsp);
f7f7bac9 3111 trace_rcu_utilization(TPS("End RCU core"));
64db4cff
PM
3112}
3113
a26ac245 3114/*
e0f23060
PM
3115 * Schedule RCU callback invocation. If the specified type of RCU
3116 * does not support RCU priority boosting, just do a direct call,
3117 * otherwise wake up the per-CPU kernel kthread. Note that because we
924df8a0 3118 * are running on the current CPU with softirqs disabled, the
e0f23060 3119 * rcu_cpu_kthread_task cannot disappear out from under us.
a26ac245 3120 */
a46e0899 3121static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
a26ac245 3122{
7d0ae808 3123 if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
b0d30417 3124 return;
a46e0899
PM
3125 if (likely(!rsp->boost)) {
3126 rcu_do_batch(rsp, rdp);
a26ac245
PM
3127 return;
3128 }
a46e0899 3129 invoke_rcu_callbacks_kthread();
a26ac245
PM
3130}
3131
a46e0899 3132static void invoke_rcu_core(void)
09223371 3133{
b0f74036
PM
3134 if (cpu_online(smp_processor_id()))
3135 raise_softirq(RCU_SOFTIRQ);
09223371
SL
3136}
3137
29154c57
PM
3138/*
3139 * Handle any core-RCU processing required by a call_rcu() invocation.
3140 */
3141static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
3142 struct rcu_head *head, unsigned long flags)
64db4cff 3143{
48a7639c
PM
3144 bool needwake;
3145
62fde6ed
PM
3146 /*
3147 * If called from an extended quiescent state, invoke the RCU
3148 * core in order to force a re-evaluation of RCU's idleness.
3149 */
9910affa 3150 if (!rcu_is_watching())
62fde6ed
PM
3151 invoke_rcu_core();
3152
a16b7a69 3153 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
29154c57 3154 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
2655d57e 3155 return;
64db4cff 3156
37c72e56
PM
3157 /*
3158 * Force the grace period if too many callbacks or too long waiting.
3159 * Enforce hysteresis, and don't invoke force_quiescent_state()
3160 * if some other CPU has recently done so. Also, don't bother
3161 * invoking force_quiescent_state() if the newly enqueued callback
3162 * is the only one waiting for a grace period to complete.
3163 */
2655d57e 3164 if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
b52573d2
PM
3165
3166 /* Are we ignoring a completed grace period? */
470716fc 3167 note_gp_changes(rsp, rdp);
b52573d2
PM
3168
3169 /* Start a new grace period if one not already started. */
3170 if (!rcu_gp_in_progress(rsp)) {
b52573d2
PM
3171 struct rcu_node *rnp_root = rcu_get_root(rsp);
3172
2a67e741 3173 raw_spin_lock_rcu_node(rnp_root);
48a7639c 3174 needwake = rcu_start_gp(rsp);
67c583a7 3175 raw_spin_unlock_rcu_node(rnp_root);
48a7639c
PM
3176 if (needwake)
3177 rcu_gp_kthread_wake(rsp);
b52573d2
PM
3178 } else {
3179 /* Give the grace period a kick. */
3180 rdp->blimit = LONG_MAX;
3181 if (rsp->n_force_qs == rdp->n_force_qs_snap &&
3182 *rdp->nxttail[RCU_DONE_TAIL] != head)
4cdfc175 3183 force_quiescent_state(rsp);
b52573d2
PM
3184 rdp->n_force_qs_snap = rsp->n_force_qs;
3185 rdp->qlen_last_fqs_check = rdp->qlen;
3186 }
4cdfc175 3187 }
29154c57
PM
3188}
3189
ae150184
PM
3190/*
3191 * RCU callback function to leak a callback.
3192 */
3193static void rcu_leak_callback(struct rcu_head *rhp)
3194{
3195}
3196
3fbfbf7a
PM
3197/*
3198 * Helper function for call_rcu() and friends. The cpu argument will
3199 * normally be -1, indicating "currently running CPU". It may specify
3200 * a CPU only if that CPU is a no-CBs CPU. Currently, only _rcu_barrier()
3201 * is expected to specify a CPU.
3202 */
64db4cff 3203static void
b6a4ae76 3204__call_rcu(struct rcu_head *head, rcu_callback_t func,
3fbfbf7a 3205 struct rcu_state *rsp, int cpu, bool lazy)
64db4cff
PM
3206{
3207 unsigned long flags;
3208 struct rcu_data *rdp;
3209
b8f2ed53
PM
3210 /* Misaligned rcu_head! */
3211 WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
3212
ae150184
PM
3213 if (debug_rcu_head_queue(head)) {
3214 /* Probable double call_rcu(), so leak the callback. */
7d0ae808 3215 WRITE_ONCE(head->func, rcu_leak_callback);
ae150184
PM
3216 WARN_ONCE(1, "__call_rcu(): Leaked duplicate callback\n");
3217 return;
3218 }
64db4cff
PM
3219 head->func = func;
3220 head->next = NULL;
64db4cff 3221 local_irq_save(flags);
394f99a9 3222 rdp = this_cpu_ptr(rsp->rda);
64db4cff
PM
3223
3224 /* Add the callback to our list. */
3fbfbf7a
PM
3225 if (unlikely(rdp->nxttail[RCU_NEXT_TAIL] == NULL) || cpu != -1) {
3226 int offline;
3227
3228 if (cpu != -1)
3229 rdp = per_cpu_ptr(rsp->rda, cpu);
143da9c2
PM
3230 if (likely(rdp->mynode)) {
3231 /* Post-boot, so this should be for a no-CBs CPU. */
3232 offline = !__call_rcu_nocb(rdp, head, lazy, flags);
3233 WARN_ON_ONCE(offline);
3234 /* Offline CPU, _call_rcu() illegal, leak callback. */
3235 local_irq_restore(flags);
3236 return;
3237 }
3238 /*
3239 * Very early boot, before rcu_init(). Initialize if needed
3240 * and then drop through to queue the callback.
3241 */
3242 BUG_ON(cpu != -1);
34404ca8 3243 WARN_ON_ONCE(!rcu_is_watching());
143da9c2
PM
3244 if (!likely(rdp->nxtlist))
3245 init_default_callback_list(rdp);
0d8ee37e 3246 }
7d0ae808 3247 WRITE_ONCE(rdp->qlen, rdp->qlen + 1);
486e2593
PM
3248 if (lazy)
3249 rdp->qlen_lazy++;
c57afe80
PM
3250 else
3251 rcu_idle_count_callbacks_posted();
b1420f1c
PM
3252 smp_mb(); /* Count before adding callback for rcu_barrier(). */
3253 *rdp->nxttail[RCU_NEXT_TAIL] = head;
3254 rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
2655d57e 3255
d4c08f2a
PM
3256 if (__is_kfree_rcu_offset((unsigned long)func))
3257 trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
486e2593 3258 rdp->qlen_lazy, rdp->qlen);
d4c08f2a 3259 else
486e2593 3260 trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen);
d4c08f2a 3261
29154c57
PM
3262 /* Go handle any RCU core processing required. */
3263 __call_rcu_core(rsp, rdp, head, flags);
64db4cff
PM
3264 local_irq_restore(flags);
3265}
3266
3267/*
d6714c22 3268 * Queue an RCU-sched callback for invocation after a grace period.
64db4cff 3269 */
b6a4ae76 3270void call_rcu_sched(struct rcu_head *head, rcu_callback_t func)
64db4cff 3271{
3fbfbf7a 3272 __call_rcu(head, func, &rcu_sched_state, -1, 0);
64db4cff 3273}
d6714c22 3274EXPORT_SYMBOL_GPL(call_rcu_sched);
64db4cff
PM
3275
3276/*
486e2593 3277 * Queue an RCU callback for invocation after a quicker grace period.
64db4cff 3278 */
b6a4ae76 3279void call_rcu_bh(struct rcu_head *head, rcu_callback_t func)
64db4cff 3280{
3fbfbf7a 3281 __call_rcu(head, func, &rcu_bh_state, -1, 0);
64db4cff
PM
3282}
3283EXPORT_SYMBOL_GPL(call_rcu_bh);
3284
495aa969
ACB
3285/*
3286 * Queue an RCU callback for lazy invocation after a grace period.
3287 * This will likely be later named something like "call_rcu_lazy()",
3288 * but this change will require some way of tagging the lazy RCU
3289 * callbacks in the list of pending callbacks. Until then, this
3290 * function may only be called from __kfree_rcu().
3291 */
3292void kfree_call_rcu(struct rcu_head *head,
b6a4ae76 3293 rcu_callback_t func)
495aa969 3294{
e534165b 3295 __call_rcu(head, func, rcu_state_p, -1, 1);
495aa969
ACB
3296}
3297EXPORT_SYMBOL_GPL(kfree_call_rcu);
3298
6d813391
PM
3299/*
3300 * Because a context switch is a grace period for RCU-sched and RCU-bh,
3301 * any blocking grace-period wait automatically implies a grace period
3302 * if there is only one CPU online at any point time during execution
3303 * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
3304 * occasionally incorrectly indicate that there are multiple CPUs online
3305 * when there was in fact only one the whole time, as this just adds
3306 * some overhead: RCU still operates correctly.
6d813391
PM
3307 */
3308static inline int rcu_blocking_is_gp(void)
3309{
95f0c1de
PM
3310 int ret;
3311
6d813391 3312 might_sleep(); /* Check for RCU read-side critical section. */
95f0c1de
PM
3313 preempt_disable();
3314 ret = num_online_cpus() <= 1;
3315 preempt_enable();
3316 return ret;
6d813391
PM
3317}
3318
6ebb237b
PM
3319/**
3320 * synchronize_sched - wait until an rcu-sched grace period has elapsed.
3321 *
3322 * Control will return to the caller some time after a full rcu-sched
3323 * grace period has elapsed, in other words after all currently executing
3324 * rcu-sched read-side critical sections have completed. These read-side
3325 * critical sections are delimited by rcu_read_lock_sched() and
3326 * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
3327 * local_irq_disable(), and so on may be used in place of
3328 * rcu_read_lock_sched().
3329 *
3330 * This means that all preempt_disable code sequences, including NMI and
f0a0e6f2
PM
3331 * non-threaded hardware-interrupt handlers, in progress on entry will
3332 * have completed before this primitive returns. However, this does not
3333 * guarantee that softirq handlers will have completed, since in some
3334 * kernels, these handlers can run in process context, and can block.
3335 *
3336 * Note that this guarantee implies further memory-ordering guarantees.
3337 * On systems with more than one CPU, when synchronize_sched() returns,
3338 * each CPU is guaranteed to have executed a full memory barrier since the
3339 * end of its last RCU-sched read-side critical section whose beginning
3340 * preceded the call to synchronize_sched(). In addition, each CPU having
3341 * an RCU read-side critical section that extends beyond the return from
3342 * synchronize_sched() is guaranteed to have executed a full memory barrier
3343 * after the beginning of synchronize_sched() and before the beginning of
3344 * that RCU read-side critical section. Note that these guarantees include
3345 * CPUs that are offline, idle, or executing in user mode, as well as CPUs
3346 * that are executing in the kernel.
3347 *
3348 * Furthermore, if CPU A invoked synchronize_sched(), which returned
3349 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
3350 * to have executed a full memory barrier during the execution of
3351 * synchronize_sched() -- even if CPU A and CPU B are the same CPU (but
3352 * again only if the system has more than one CPU).
6ebb237b
PM
3353 *
3354 * This primitive provides the guarantees made by the (now removed)
3355 * synchronize_kernel() API. In contrast, synchronize_rcu() only
3356 * guarantees that rcu_read_lock() sections will have completed.
3357 * In "classic RCU", these two guarantees happen to be one and
3358 * the same, but can differ in realtime RCU implementations.
3359 */
3360void synchronize_sched(void)
3361{
f78f5b90
PM
3362 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
3363 lock_is_held(&rcu_lock_map) ||
3364 lock_is_held(&rcu_sched_lock_map),
3365 "Illegal synchronize_sched() in RCU-sched read-side critical section");
6ebb237b
PM
3366 if (rcu_blocking_is_gp())
3367 return;
5afff48b 3368 if (rcu_gp_is_expedited())
3705b88d
AM
3369 synchronize_sched_expedited();
3370 else
3371 wait_rcu_gp(call_rcu_sched);
6ebb237b
PM
3372}
3373EXPORT_SYMBOL_GPL(synchronize_sched);
3374
3375/**
3376 * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
3377 *
3378 * Control will return to the caller some time after a full rcu_bh grace
3379 * period has elapsed, in other words after all currently executing rcu_bh
3380 * read-side critical sections have completed. RCU read-side critical
3381 * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
3382 * and may be nested.
f0a0e6f2
PM
3383 *
3384 * See the description of synchronize_sched() for more detailed information
3385 * on memory ordering guarantees.
6ebb237b
PM
3386 */
3387void synchronize_rcu_bh(void)
3388{
f78f5b90
PM
3389 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
3390 lock_is_held(&rcu_lock_map) ||
3391 lock_is_held(&rcu_sched_lock_map),
3392 "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
6ebb237b
PM
3393 if (rcu_blocking_is_gp())
3394 return;
5afff48b 3395 if (rcu_gp_is_expedited())
3705b88d
AM
3396 synchronize_rcu_bh_expedited();
3397 else
3398 wait_rcu_gp(call_rcu_bh);
6ebb237b
PM
3399}
3400EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
3401
765a3f4f
PM
3402/**
3403 * get_state_synchronize_rcu - Snapshot current RCU state
3404 *
3405 * Returns a cookie that is used by a later call to cond_synchronize_rcu()
3406 * to determine whether or not a full grace period has elapsed in the
3407 * meantime.
3408 */
3409unsigned long get_state_synchronize_rcu(void)
3410{
3411 /*
3412 * Any prior manipulation of RCU-protected data must happen
3413 * before the load from ->gpnum.
3414 */
3415 smp_mb(); /* ^^^ */
3416
3417 /*
3418 * Make sure this load happens before the purportedly
3419 * time-consuming work between get_state_synchronize_rcu()
3420 * and cond_synchronize_rcu().
3421 */
e534165b 3422 return smp_load_acquire(&rcu_state_p->gpnum);
765a3f4f
PM
3423}
3424EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
3425
3426/**
3427 * cond_synchronize_rcu - Conditionally wait for an RCU grace period
3428 *
3429 * @oldstate: return value from earlier call to get_state_synchronize_rcu()
3430 *
3431 * If a full RCU grace period has elapsed since the earlier call to
3432 * get_state_synchronize_rcu(), just return. Otherwise, invoke
3433 * synchronize_rcu() to wait for a full grace period.
3434 *
3435 * Yes, this function does not take counter wrap into account. But
3436 * counter wrap is harmless. If the counter wraps, we have waited for
3437 * more than 2 billion grace periods (and way more on a 64-bit system!),
3438 * so waiting for one additional grace period should be just fine.
3439 */
3440void cond_synchronize_rcu(unsigned long oldstate)
3441{
3442 unsigned long newstate;
3443
3444 /*
3445 * Ensure that this load happens before any RCU-destructive
3446 * actions the caller might carry out after we return.
3447 */
e534165b 3448 newstate = smp_load_acquire(&rcu_state_p->completed);
765a3f4f
PM
3449 if (ULONG_CMP_GE(oldstate, newstate))
3450 synchronize_rcu();
3451}
3452EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
3453
24560056
PM
3454/**
3455 * get_state_synchronize_sched - Snapshot current RCU-sched state
3456 *
3457 * Returns a cookie that is used by a later call to cond_synchronize_sched()
3458 * to determine whether or not a full grace period has elapsed in the
3459 * meantime.
3460 */
3461unsigned long get_state_synchronize_sched(void)
3462{
3463 /*
3464 * Any prior manipulation of RCU-protected data must happen
3465 * before the load from ->gpnum.
3466 */
3467 smp_mb(); /* ^^^ */
3468
3469 /*
3470 * Make sure this load happens before the purportedly
3471 * time-consuming work between get_state_synchronize_sched()
3472 * and cond_synchronize_sched().
3473 */
3474 return smp_load_acquire(&rcu_sched_state.gpnum);
3475}
3476EXPORT_SYMBOL_GPL(get_state_synchronize_sched);
3477
3478/**
3479 * cond_synchronize_sched - Conditionally wait for an RCU-sched grace period
3480 *
3481 * @oldstate: return value from earlier call to get_state_synchronize_sched()
3482 *
3483 * If a full RCU-sched grace period has elapsed since the earlier call to
3484 * get_state_synchronize_sched(), just return. Otherwise, invoke
3485 * synchronize_sched() to wait for a full grace period.
3486 *
3487 * Yes, this function does not take counter wrap into account. But
3488 * counter wrap is harmless. If the counter wraps, we have waited for
3489 * more than 2 billion grace periods (and way more on a 64-bit system!),
3490 * so waiting for one additional grace period should be just fine.
3491 */
3492void cond_synchronize_sched(unsigned long oldstate)
3493{
3494 unsigned long newstate;
3495
3496 /*
3497 * Ensure that this load happens before any RCU-destructive
3498 * actions the caller might carry out after we return.
3499 */
3500 newstate = smp_load_acquire(&rcu_sched_state.completed);
3501 if (ULONG_CMP_GE(oldstate, newstate))
3502 synchronize_sched();
3503}
3504EXPORT_SYMBOL_GPL(cond_synchronize_sched);
3505
28f00767
PM
3506/* Adjust sequence number for start of update-side operation. */
3507static void rcu_seq_start(unsigned long *sp)
3508{
3509 WRITE_ONCE(*sp, *sp + 1);
3510 smp_mb(); /* Ensure update-side operation after counter increment. */
3511 WARN_ON_ONCE(!(*sp & 0x1));
3512}
3513
3514/* Adjust sequence number for end of update-side operation. */
3515static void rcu_seq_end(unsigned long *sp)
3516{
3517 smp_mb(); /* Ensure update-side operation before counter increment. */
3518 WRITE_ONCE(*sp, *sp + 1);
3519 WARN_ON_ONCE(*sp & 0x1);
3520}
3521
3522/* Take a snapshot of the update side's sequence number. */
3523static unsigned long rcu_seq_snap(unsigned long *sp)
3524{
3525 unsigned long s;
3526
28f00767
PM
3527 s = (READ_ONCE(*sp) + 3) & ~0x1;
3528 smp_mb(); /* Above access must not bleed into critical section. */
3529 return s;
3530}
3531
3532/*
3533 * Given a snapshot from rcu_seq_snap(), determine whether or not a
3534 * full update-side operation has occurred.
3535 */
3536static bool rcu_seq_done(unsigned long *sp, unsigned long s)
3537{
3538 return ULONG_CMP_GE(READ_ONCE(*sp), s);
3539}
3540
64db4cff
PM
3541/*
3542 * Check to see if there is any immediate RCU-related work to be done
3543 * by the current CPU, for the specified type of RCU, returning 1 if so.
3544 * The checks are in order of increasing expense: checks that can be
3545 * carried out against CPU-local state are performed first. However,
3546 * we must check for CPU stalls first, else we might not get a chance.
3547 */
3548static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
3549{
2f51f988
PM
3550 struct rcu_node *rnp = rdp->mynode;
3551
64db4cff
PM
3552 rdp->n_rcu_pending++;
3553
3554 /* Check for CPU stalls, if enabled. */
3555 check_cpu_stall(rsp, rdp);
3556
a096932f
PM
3557 /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
3558 if (rcu_nohz_full_cpu(rsp))
3559 return 0;
3560
64db4cff 3561 /* Is the RCU core waiting for a quiescent state from this CPU? */
5c51dd73 3562 if (rcu_scheduler_fully_active &&
5b74c458 3563 rdp->core_needs_qs && rdp->cpu_no_qs.b.norm &&
5cd37193 3564 rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr)) {
97c668b8
PM
3565 rdp->n_rp_core_needs_qs++;
3566 } else if (rdp->core_needs_qs &&
5b74c458 3567 (!rdp->cpu_no_qs.b.norm ||
5cd37193 3568 rdp->rcu_qs_ctr_snap != __this_cpu_read(rcu_qs_ctr))) {
d21670ac 3569 rdp->n_rp_report_qs++;
64db4cff 3570 return 1;
7ba5c840 3571 }
64db4cff
PM
3572
3573 /* Does this CPU have callbacks ready to invoke? */
7ba5c840
PM
3574 if (cpu_has_callbacks_ready_to_invoke(rdp)) {
3575 rdp->n_rp_cb_ready++;
64db4cff 3576 return 1;
7ba5c840 3577 }
64db4cff
PM
3578
3579 /* Has RCU gone idle with this CPU needing another grace period? */
7ba5c840
PM
3580 if (cpu_needs_another_gp(rsp, rdp)) {
3581 rdp->n_rp_cpu_needs_gp++;
64db4cff 3582 return 1;
7ba5c840 3583 }
64db4cff
PM
3584
3585 /* Has another RCU grace period completed? */
7d0ae808 3586 if (READ_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
7ba5c840 3587 rdp->n_rp_gp_completed++;
64db4cff 3588 return 1;
7ba5c840 3589 }
64db4cff
PM
3590
3591 /* Has a new RCU grace period started? */
7d0ae808
PM
3592 if (READ_ONCE(rnp->gpnum) != rdp->gpnum ||
3593 unlikely(READ_ONCE(rdp->gpwrap))) { /* outside lock */
7ba5c840 3594 rdp->n_rp_gp_started++;
64db4cff 3595 return 1;
7ba5c840 3596 }
64db4cff 3597
96d3fd0d
PM
3598 /* Does this CPU need a deferred NOCB wakeup? */
3599 if (rcu_nocb_need_deferred_wakeup(rdp)) {
3600 rdp->n_rp_nocb_defer_wakeup++;
3601 return 1;
3602 }
3603
64db4cff 3604 /* nothing to do */
7ba5c840 3605 rdp->n_rp_need_nothing++;
64db4cff
PM
3606 return 0;
3607}
3608
3609/*
3610 * Check to see if there is any immediate RCU-related work to be done
3611 * by the current CPU, returning 1 if so. This function is part of the
3612 * RCU implementation; it is -not- an exported member of the RCU API.
3613 */
e3950ecd 3614static int rcu_pending(void)
64db4cff 3615{
6ce75a23
PM
3616 struct rcu_state *rsp;
3617
3618 for_each_rcu_flavor(rsp)
e3950ecd 3619 if (__rcu_pending(rsp, this_cpu_ptr(rsp->rda)))
6ce75a23
PM
3620 return 1;
3621 return 0;
64db4cff
PM
3622}
3623
3624/*
c0f4dfd4
PM
3625 * Return true if the specified CPU has any callback. If all_lazy is
3626 * non-NULL, store an indication of whether all callbacks are lazy.
3627 * (If there are no callbacks, all of them are deemed to be lazy.)
64db4cff 3628 */
82072c4f 3629static bool __maybe_unused rcu_cpu_has_callbacks(bool *all_lazy)
64db4cff 3630{
c0f4dfd4
PM
3631 bool al = true;
3632 bool hc = false;
3633 struct rcu_data *rdp;
6ce75a23
PM
3634 struct rcu_state *rsp;
3635
c0f4dfd4 3636 for_each_rcu_flavor(rsp) {
aa6da514 3637 rdp = this_cpu_ptr(rsp->rda);
69c8d28c
PM
3638 if (!rdp->nxtlist)
3639 continue;
3640 hc = true;
3641 if (rdp->qlen != rdp->qlen_lazy || !all_lazy) {
c0f4dfd4 3642 al = false;
69c8d28c
PM
3643 break;
3644 }
c0f4dfd4
PM
3645 }
3646 if (all_lazy)
3647 *all_lazy = al;
3648 return hc;
64db4cff
PM
3649}
3650
a83eff0a
PM
3651/*
3652 * Helper function for _rcu_barrier() tracing. If tracing is disabled,
3653 * the compiler is expected to optimize this away.
3654 */
e66c33d5 3655static void _rcu_barrier_trace(struct rcu_state *rsp, const char *s,
a83eff0a
PM
3656 int cpu, unsigned long done)
3657{
3658 trace_rcu_barrier(rsp->name, s, cpu,
3659 atomic_read(&rsp->barrier_cpu_count), done);
3660}
3661
b1420f1c
PM
3662/*
3663 * RCU callback function for _rcu_barrier(). If we are last, wake
3664 * up the task executing _rcu_barrier().
3665 */
24ebbca8 3666static void rcu_barrier_callback(struct rcu_head *rhp)
d0ec774c 3667{
24ebbca8
PM
3668 struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
3669 struct rcu_state *rsp = rdp->rsp;
3670
a83eff0a 3671 if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
4f525a52 3672 _rcu_barrier_trace(rsp, "LastCB", -1, rsp->barrier_sequence);
7db74df8 3673 complete(&rsp->barrier_completion);
a83eff0a 3674 } else {
4f525a52 3675 _rcu_barrier_trace(rsp, "CB", -1, rsp->barrier_sequence);
a83eff0a 3676 }
d0ec774c
PM
3677}
3678
3679/*
3680 * Called with preemption disabled, and from cross-cpu IRQ context.
3681 */
3682static void rcu_barrier_func(void *type)
3683{
037b64ed 3684 struct rcu_state *rsp = type;
fa07a58f 3685 struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
d0ec774c 3686
4f525a52 3687 _rcu_barrier_trace(rsp, "IRQ", -1, rsp->barrier_sequence);
24ebbca8 3688 atomic_inc(&rsp->barrier_cpu_count);
06668efa 3689 rsp->call(&rdp->barrier_head, rcu_barrier_callback);
d0ec774c
PM
3690}
3691
d0ec774c
PM
3692/*
3693 * Orchestrate the specified type of RCU barrier, waiting for all
3694 * RCU callbacks of the specified type to complete.
3695 */
037b64ed 3696static void _rcu_barrier(struct rcu_state *rsp)
d0ec774c 3697{
b1420f1c 3698 int cpu;
b1420f1c 3699 struct rcu_data *rdp;
4f525a52 3700 unsigned long s = rcu_seq_snap(&rsp->barrier_sequence);
b1420f1c 3701
4f525a52 3702 _rcu_barrier_trace(rsp, "Begin", -1, s);
b1420f1c 3703
e74f4c45 3704 /* Take mutex to serialize concurrent rcu_barrier() requests. */
7be7f0be 3705 mutex_lock(&rsp->barrier_mutex);
b1420f1c 3706
4f525a52
PM
3707 /* Did someone else do our work for us? */
3708 if (rcu_seq_done(&rsp->barrier_sequence, s)) {
3709 _rcu_barrier_trace(rsp, "EarlyExit", -1, rsp->barrier_sequence);
cf3a9c48
PM
3710 smp_mb(); /* caller's subsequent code after above check. */
3711 mutex_unlock(&rsp->barrier_mutex);
3712 return;
3713 }
3714
4f525a52
PM
3715 /* Mark the start of the barrier operation. */
3716 rcu_seq_start(&rsp->barrier_sequence);
3717 _rcu_barrier_trace(rsp, "Inc1", -1, rsp->barrier_sequence);
b1420f1c 3718
d0ec774c 3719 /*
b1420f1c
PM
3720 * Initialize the count to one rather than to zero in order to
3721 * avoid a too-soon return to zero in case of a short grace period
1331e7a1
PM
3722 * (or preemption of this task). Exclude CPU-hotplug operations
3723 * to ensure that no offline CPU has callbacks queued.
d0ec774c 3724 */
7db74df8 3725 init_completion(&rsp->barrier_completion);
24ebbca8 3726 atomic_set(&rsp->barrier_cpu_count, 1);
1331e7a1 3727 get_online_cpus();
b1420f1c
PM
3728
3729 /*
1331e7a1
PM
3730 * Force each CPU with callbacks to register a new callback.
3731 * When that callback is invoked, we will know that all of the
3732 * corresponding CPU's preceding callbacks have been invoked.
b1420f1c 3733 */
3fbfbf7a 3734 for_each_possible_cpu(cpu) {
d1e43fa5 3735 if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
3fbfbf7a 3736 continue;
b1420f1c 3737 rdp = per_cpu_ptr(rsp->rda, cpu);
d1e43fa5 3738 if (rcu_is_nocb_cpu(cpu)) {
d7e29933
PM
3739 if (!rcu_nocb_cpu_needs_barrier(rsp, cpu)) {
3740 _rcu_barrier_trace(rsp, "OfflineNoCB", cpu,
4f525a52 3741 rsp->barrier_sequence);
d7e29933
PM
3742 } else {
3743 _rcu_barrier_trace(rsp, "OnlineNoCB", cpu,
4f525a52 3744 rsp->barrier_sequence);
41050a00 3745 smp_mb__before_atomic();
d7e29933
PM
3746 atomic_inc(&rsp->barrier_cpu_count);
3747 __call_rcu(&rdp->barrier_head,
3748 rcu_barrier_callback, rsp, cpu, 0);
3749 }
7d0ae808 3750 } else if (READ_ONCE(rdp->qlen)) {
a83eff0a 3751 _rcu_barrier_trace(rsp, "OnlineQ", cpu,
4f525a52 3752 rsp->barrier_sequence);
037b64ed 3753 smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
b1420f1c 3754 } else {
a83eff0a 3755 _rcu_barrier_trace(rsp, "OnlineNQ", cpu,
4f525a52 3756 rsp->barrier_sequence);
b1420f1c
PM
3757 }
3758 }
1331e7a1 3759 put_online_cpus();
b1420f1c
PM
3760
3761 /*
3762 * Now that we have an rcu_barrier_callback() callback on each
3763 * CPU, and thus each counted, remove the initial count.
3764 */
24ebbca8 3765 if (atomic_dec_and_test(&rsp->barrier_cpu_count))
7db74df8 3766 complete(&rsp->barrier_completion);
b1420f1c
PM
3767
3768 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
7db74df8 3769 wait_for_completion(&rsp->barrier_completion);
b1420f1c 3770
4f525a52
PM
3771 /* Mark the end of the barrier operation. */
3772 _rcu_barrier_trace(rsp, "Inc2", -1, rsp->barrier_sequence);
3773 rcu_seq_end(&rsp->barrier_sequence);
3774
b1420f1c 3775 /* Other rcu_barrier() invocations can now safely proceed. */
7be7f0be 3776 mutex_unlock(&rsp->barrier_mutex);
d0ec774c 3777}
d0ec774c
PM
3778
3779/**
3780 * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
3781 */
3782void rcu_barrier_bh(void)
3783{
037b64ed 3784 _rcu_barrier(&rcu_bh_state);
d0ec774c
PM
3785}
3786EXPORT_SYMBOL_GPL(rcu_barrier_bh);
3787
3788/**
3789 * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
3790 */
3791void rcu_barrier_sched(void)
3792{
037b64ed 3793 _rcu_barrier(&rcu_sched_state);
d0ec774c
PM
3794}
3795EXPORT_SYMBOL_GPL(rcu_barrier_sched);
3796
0aa04b05
PM
3797/*
3798 * Propagate ->qsinitmask bits up the rcu_node tree to account for the
3799 * first CPU in a given leaf rcu_node structure coming online. The caller
3800 * must hold the corresponding leaf rcu_node ->lock with interrrupts
3801 * disabled.
3802 */
3803static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
3804{
3805 long mask;
3806 struct rcu_node *rnp = rnp_leaf;
3807
3808 for (;;) {
3809 mask = rnp->grpmask;
3810 rnp = rnp->parent;
3811 if (rnp == NULL)
3812 return;
6cf10081 3813 raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
0aa04b05 3814 rnp->qsmaskinit |= mask;
67c583a7 3815 raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
0aa04b05
PM
3816 }
3817}
3818
64db4cff 3819/*
27569620 3820 * Do boot-time initialization of a CPU's per-CPU RCU data.
64db4cff 3821 */
27569620
PM
3822static void __init
3823rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
64db4cff
PM
3824{
3825 unsigned long flags;
394f99a9 3826 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
27569620
PM
3827 struct rcu_node *rnp = rcu_get_root(rsp);
3828
3829 /* Set up local state, ensuring consistent view of global state. */
6cf10081 3830 raw_spin_lock_irqsave_rcu_node(rnp, flags);
bc75e999 3831 rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
27569620 3832 rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
29e37d81 3833 WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_EXIT_IDLE);
02a5c550 3834 WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp->dynticks)));
27569620 3835 rdp->cpu = cpu;
d4c08f2a 3836 rdp->rsp = rsp;
3fbfbf7a 3837 rcu_boot_init_nocb_percpu_data(rdp);
67c583a7 3838 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
27569620
PM
3839}
3840
3841/*
3842 * Initialize a CPU's per-CPU RCU data. Note that only one online or
3843 * offline event can be happening at a given time. Note also that we
3844 * can accept some slop in the rsp->completed access due to the fact
3845 * that this CPU cannot possibly have any RCU callbacks in flight yet.
64db4cff 3846 */
49fb4c62 3847static void
9b67122a 3848rcu_init_percpu_data(int cpu, struct rcu_state *rsp)
64db4cff
PM
3849{
3850 unsigned long flags;
64db4cff 3851 unsigned long mask;
394f99a9 3852 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
64db4cff
PM
3853 struct rcu_node *rnp = rcu_get_root(rsp);
3854
3855 /* Set up local state, ensuring consistent view of global state. */
6cf10081 3856 raw_spin_lock_irqsave_rcu_node(rnp, flags);
37c72e56
PM
3857 rdp->qlen_last_fqs_check = 0;
3858 rdp->n_force_qs_snap = rsp->n_force_qs;
64db4cff 3859 rdp->blimit = blimit;
39c8d313
PM
3860 if (!rdp->nxtlist)
3861 init_callback_list(rdp); /* Re-enable callbacks on this CPU. */
29e37d81 3862 rdp->dynticks->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
2333210b 3863 rcu_sysidle_init_percpu_data(rdp->dynticks);
2625d469 3864 rcu_dynticks_eqs_online();
67c583a7 3865 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
64db4cff 3866
0aa04b05
PM
3867 /*
3868 * Add CPU to leaf rcu_node pending-online bitmask. Any needed
3869 * propagation up the rcu_node tree will happen at the beginning
3870 * of the next grace period.
3871 */
64db4cff
PM
3872 rnp = rdp->mynode;
3873 mask = rdp->grpmask;
2a67e741 3874 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
b9585e94
PM
3875 if (!rdp->beenonline)
3876 WRITE_ONCE(rsp->ncpus, READ_ONCE(rsp->ncpus) + 1);
3877 rdp->beenonline = true; /* We have now been online. */
0aa04b05
PM
3878 rdp->gpnum = rnp->completed; /* Make CPU later note any new GP. */
3879 rdp->completed = rnp->completed;
5b74c458 3880 rdp->cpu_no_qs.b.norm = true;
a738eec6 3881 rdp->rcu_qs_ctr_snap = per_cpu(rcu_qs_ctr, cpu);
97c668b8 3882 rdp->core_needs_qs = false;
0aa04b05 3883 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuonl"));
67c583a7 3884 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff
PM
3885}
3886
4df83742 3887int rcutree_prepare_cpu(unsigned int cpu)
64db4cff 3888{
6ce75a23
PM
3889 struct rcu_state *rsp;
3890
3891 for_each_rcu_flavor(rsp)
9b67122a 3892 rcu_init_percpu_data(cpu, rsp);
4df83742
TG
3893
3894 rcu_prepare_kthreads(cpu);
3895 rcu_spawn_all_nocb_kthreads(cpu);
3896
3897 return 0;
3898}
3899
3900static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
3901{
3902 struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
3903
3904 rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
3905}
3906
3907int rcutree_online_cpu(unsigned int cpu)
3908{
3909 sync_sched_exp_online_cleanup(cpu);
3910 rcutree_affinity_setting(cpu, -1);
3911 return 0;
3912}
3913
3914int rcutree_offline_cpu(unsigned int cpu)
3915{
3916 rcutree_affinity_setting(cpu, cpu);
3917 return 0;
3918}
3919
3920
3921int rcutree_dying_cpu(unsigned int cpu)
3922{
3923 struct rcu_state *rsp;
3924
3925 for_each_rcu_flavor(rsp)
3926 rcu_cleanup_dying_cpu(rsp);
3927 return 0;
3928}
3929
3930int rcutree_dead_cpu(unsigned int cpu)
3931{
3932 struct rcu_state *rsp;
3933
3934 for_each_rcu_flavor(rsp) {
3935 rcu_cleanup_dead_cpu(cpu, rsp);
3936 do_nocb_deferred_wakeup(per_cpu_ptr(rsp->rda, cpu));
3937 }
3938 return 0;
64db4cff
PM
3939}
3940
7ec99de3
PM
3941/*
3942 * Mark the specified CPU as being online so that subsequent grace periods
3943 * (both expedited and normal) will wait on it. Note that this means that
3944 * incoming CPUs are not allowed to use RCU read-side critical sections
3945 * until this function is called. Failing to observe this restriction
3946 * will result in lockdep splats.
3947 */
3948void rcu_cpu_starting(unsigned int cpu)
3949{
3950 unsigned long flags;
3951 unsigned long mask;
3952 struct rcu_data *rdp;
3953 struct rcu_node *rnp;
3954 struct rcu_state *rsp;
3955
3956 for_each_rcu_flavor(rsp) {
3957 rdp = this_cpu_ptr(rsp->rda);
3958 rnp = rdp->mynode;
3959 mask = rdp->grpmask;
3960 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3961 rnp->qsmaskinitnext |= mask;
3962 rnp->expmaskinitnext |= mask;
3963 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3964 }
3965}
3966
27d50c7e
TG
3967#ifdef CONFIG_HOTPLUG_CPU
3968/*
710d60cb
LT
3969 * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
3970 * function. We now remove it from the rcu_node tree's ->qsmaskinit
3971 * bit masks.
27d50c7e
TG
3972 * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
3973 * function. We now remove it from the rcu_node tree's ->qsmaskinit
3974 * bit masks.
3975 */
3976static void rcu_cleanup_dying_idle_cpu(int cpu, struct rcu_state *rsp)
3977{
3978 unsigned long flags;
3979 unsigned long mask;
3980 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
3981 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
3982
27d50c7e
TG
3983 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
3984 mask = rdp->grpmask;
3985 raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
3986 rnp->qsmaskinitnext &= ~mask;
710d60cb 3987 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
27d50c7e
TG
3988}
3989
3990void rcu_report_dead(unsigned int cpu)
3991{
3992 struct rcu_state *rsp;
3993
3994 /* QS for any half-done expedited RCU-sched GP. */
3995 preempt_disable();
3996 rcu_report_exp_rdp(&rcu_sched_state,
3997 this_cpu_ptr(rcu_sched_state.rda), true);
3998 preempt_enable();
3999 for_each_rcu_flavor(rsp)
4000 rcu_cleanup_dying_idle_cpu(cpu, rsp);
4001}
4002#endif
4003
d1d74d14
BP
4004static int rcu_pm_notify(struct notifier_block *self,
4005 unsigned long action, void *hcpu)
4006{
4007 switch (action) {
4008 case PM_HIBERNATION_PREPARE:
4009 case PM_SUSPEND_PREPARE:
4010 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
5afff48b 4011 rcu_expedite_gp();
d1d74d14
BP
4012 break;
4013 case PM_POST_HIBERNATION:
4014 case PM_POST_SUSPEND:
5afff48b
PM
4015 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
4016 rcu_unexpedite_gp();
d1d74d14
BP
4017 break;
4018 default:
4019 break;
4020 }
4021 return NOTIFY_OK;
4022}
4023
b3dbec76 4024/*
9386c0b7 4025 * Spawn the kthreads that handle each RCU flavor's grace periods.
b3dbec76
PM
4026 */
4027static int __init rcu_spawn_gp_kthread(void)
4028{
4029 unsigned long flags;
a94844b2 4030 int kthread_prio_in = kthread_prio;
b3dbec76
PM
4031 struct rcu_node *rnp;
4032 struct rcu_state *rsp;
a94844b2 4033 struct sched_param sp;
b3dbec76
PM
4034 struct task_struct *t;
4035
a94844b2
PM
4036 /* Force priority into range. */
4037 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
4038 kthread_prio = 1;
4039 else if (kthread_prio < 0)
4040 kthread_prio = 0;
4041 else if (kthread_prio > 99)
4042 kthread_prio = 99;
4043 if (kthread_prio != kthread_prio_in)
4044 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
4045 kthread_prio, kthread_prio_in);
4046
9386c0b7 4047 rcu_scheduler_fully_active = 1;
b3dbec76 4048 for_each_rcu_flavor(rsp) {
a94844b2 4049 t = kthread_create(rcu_gp_kthread, rsp, "%s", rsp->name);
b3dbec76
PM
4050 BUG_ON(IS_ERR(t));
4051 rnp = rcu_get_root(rsp);
6cf10081 4052 raw_spin_lock_irqsave_rcu_node(rnp, flags);
b3dbec76 4053 rsp->gp_kthread = t;
a94844b2
PM
4054 if (kthread_prio) {
4055 sp.sched_priority = kthread_prio;
4056 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
4057 }
67c583a7 4058 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
e11f1335 4059 wake_up_process(t);
b3dbec76 4060 }
35ce7f29 4061 rcu_spawn_nocb_kthreads();
9386c0b7 4062 rcu_spawn_boost_kthreads();
b3dbec76
PM
4063 return 0;
4064}
4065early_initcall(rcu_spawn_gp_kthread);
4066
bbad9379 4067/*
52d7e48b
PM
4068 * This function is invoked towards the end of the scheduler's
4069 * initialization process. Before this is called, the idle task might
4070 * contain synchronous grace-period primitives (during which time, this idle
4071 * task is booting the system, and such primitives are no-ops). After this
4072 * function is called, any synchronous grace-period primitives are run as
4073 * expedited, with the requesting task driving the grace period forward.
4074 * A later core_initcall() rcu_exp_runtime_mode() will switch to full
4075 * runtime RCU functionality.
bbad9379
PM
4076 */
4077void rcu_scheduler_starting(void)
4078{
4079 WARN_ON(num_online_cpus() != 1);
4080 WARN_ON(nr_context_switches() > 0);
52d7e48b
PM
4081 rcu_test_sync_prims();
4082 rcu_scheduler_active = RCU_SCHEDULER_INIT;
4083 rcu_test_sync_prims();
bbad9379
PM
4084}
4085
64db4cff
PM
4086/*
4087 * Compute the per-level fanout, either using the exact fanout specified
7fa27001 4088 * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
64db4cff 4089 */
199977bf 4090static void __init rcu_init_levelspread(int *levelspread, const int *levelcnt)
64db4cff 4091{
64db4cff
PM
4092 int i;
4093
7fa27001 4094 if (rcu_fanout_exact) {
199977bf 4095 levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
66292405 4096 for (i = rcu_num_lvls - 2; i >= 0; i--)
199977bf 4097 levelspread[i] = RCU_FANOUT;
66292405
PM
4098 } else {
4099 int ccur;
4100 int cprv;
4101
4102 cprv = nr_cpu_ids;
4103 for (i = rcu_num_lvls - 1; i >= 0; i--) {
199977bf
AG
4104 ccur = levelcnt[i];
4105 levelspread[i] = (cprv + ccur - 1) / ccur;
66292405
PM
4106 cprv = ccur;
4107 }
64db4cff
PM
4108 }
4109}
64db4cff
PM
4110
4111/*
4112 * Helper function for rcu_init() that initializes one rcu_state structure.
4113 */
a87f203e 4114static void __init rcu_init_one(struct rcu_state *rsp)
64db4cff 4115{
cb007102
AG
4116 static const char * const buf[] = RCU_NODE_NAME_INIT;
4117 static const char * const fqs[] = RCU_FQS_NAME_INIT;
3dc5dbe9
PM
4118 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
4119 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
4a81e832 4120 static u8 fl_mask = 0x1;
199977bf
AG
4121
4122 int levelcnt[RCU_NUM_LVLS]; /* # nodes in each level. */
4123 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
64db4cff
PM
4124 int cpustride = 1;
4125 int i;
4126 int j;
4127 struct rcu_node *rnp;
4128
05b84aec 4129 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
b6407e86 4130
3eaaaf6c
PM
4131 /* Silence gcc 4.8 false positive about array index out of range. */
4132 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
4133 panic("rcu_init_one: rcu_num_lvls out of range");
4930521a 4134
64db4cff
PM
4135 /* Initialize the level-tracking arrays. */
4136
f885b7f2 4137 for (i = 0; i < rcu_num_lvls; i++)
199977bf 4138 levelcnt[i] = num_rcu_lvl[i];
f885b7f2 4139 for (i = 1; i < rcu_num_lvls; i++)
199977bf
AG
4140 rsp->level[i] = rsp->level[i - 1] + levelcnt[i - 1];
4141 rcu_init_levelspread(levelspread, levelcnt);
4a81e832
PM
4142 rsp->flavor_mask = fl_mask;
4143 fl_mask <<= 1;
64db4cff
PM
4144
4145 /* Initialize the elements themselves, starting from the leaves. */
4146
f885b7f2 4147 for (i = rcu_num_lvls - 1; i >= 0; i--) {
199977bf 4148 cpustride *= levelspread[i];
64db4cff 4149 rnp = rsp->level[i];
199977bf 4150 for (j = 0; j < levelcnt[i]; j++, rnp++) {
67c583a7
BF
4151 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
4152 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
b6407e86 4153 &rcu_node_class[i], buf[i]);
394f2769
PM
4154 raw_spin_lock_init(&rnp->fqslock);
4155 lockdep_set_class_and_name(&rnp->fqslock,
4156 &rcu_fqs_class[i], fqs[i]);
25d30cf4
PM
4157 rnp->gpnum = rsp->gpnum;
4158 rnp->completed = rsp->completed;
64db4cff
PM
4159 rnp->qsmask = 0;
4160 rnp->qsmaskinit = 0;
4161 rnp->grplo = j * cpustride;
4162 rnp->grphi = (j + 1) * cpustride - 1;
595f3900
HS
4163 if (rnp->grphi >= nr_cpu_ids)
4164 rnp->grphi = nr_cpu_ids - 1;
64db4cff
PM
4165 if (i == 0) {
4166 rnp->grpnum = 0;
4167 rnp->grpmask = 0;
4168 rnp->parent = NULL;
4169 } else {
199977bf 4170 rnp->grpnum = j % levelspread[i - 1];
64db4cff
PM
4171 rnp->grpmask = 1UL << rnp->grpnum;
4172 rnp->parent = rsp->level[i - 1] +
199977bf 4173 j / levelspread[i - 1];
64db4cff
PM
4174 }
4175 rnp->level = i;
12f5f524 4176 INIT_LIST_HEAD(&rnp->blkd_tasks);
dae6e64d 4177 rcu_init_one_nocb(rnp);
f6a12f34
PM
4178 init_waitqueue_head(&rnp->exp_wq[0]);
4179 init_waitqueue_head(&rnp->exp_wq[1]);
3b5f668e
PM
4180 init_waitqueue_head(&rnp->exp_wq[2]);
4181 init_waitqueue_head(&rnp->exp_wq[3]);
f6a12f34 4182 spin_lock_init(&rnp->exp_lock);
64db4cff
PM
4183 }
4184 }
0c34029a 4185
abedf8e2
PG
4186 init_swait_queue_head(&rsp->gp_wq);
4187 init_swait_queue_head(&rsp->expedited_wq);
f885b7f2 4188 rnp = rsp->level[rcu_num_lvls - 1];
0c34029a 4189 for_each_possible_cpu(i) {
4a90a068 4190 while (i > rnp->grphi)
0c34029a 4191 rnp++;
394f99a9 4192 per_cpu_ptr(rsp->rda, i)->mynode = rnp;
0c34029a
LJ
4193 rcu_boot_init_percpu_data(i, rsp);
4194 }
6ce75a23 4195 list_add(&rsp->flavors, &rcu_struct_flavors);
64db4cff
PM
4196}
4197
f885b7f2
PM
4198/*
4199 * Compute the rcu_node tree geometry from kernel parameters. This cannot
4102adab 4200 * replace the definitions in tree.h because those are needed to size
f885b7f2
PM
4201 * the ->node array in the rcu_state structure.
4202 */
4203static void __init rcu_init_geometry(void)
4204{
026ad283 4205 ulong d;
f885b7f2 4206 int i;
05b84aec 4207 int rcu_capacity[RCU_NUM_LVLS];
f885b7f2 4208
026ad283
PM
4209 /*
4210 * Initialize any unspecified boot parameters.
4211 * The default values of jiffies_till_first_fqs and
4212 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
4213 * value, which is a function of HZ, then adding one for each
4214 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
4215 */
4216 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
4217 if (jiffies_till_first_fqs == ULONG_MAX)
4218 jiffies_till_first_fqs = d;
4219 if (jiffies_till_next_fqs == ULONG_MAX)
4220 jiffies_till_next_fqs = d;
4221
f885b7f2 4222 /* If the compile-time values are accurate, just leave. */
47d631af 4223 if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
b17c7035 4224 nr_cpu_ids == NR_CPUS)
f885b7f2 4225 return;
39479098
PM
4226 pr_info("RCU: Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%d\n",
4227 rcu_fanout_leaf, nr_cpu_ids);
f885b7f2 4228
f885b7f2 4229 /*
ee968ac6
PM
4230 * The boot-time rcu_fanout_leaf parameter must be at least two
4231 * and cannot exceed the number of bits in the rcu_node masks.
4232 * Complain and fall back to the compile-time values if this
4233 * limit is exceeded.
f885b7f2 4234 */
ee968ac6 4235 if (rcu_fanout_leaf < 2 ||
75cf15a4 4236 rcu_fanout_leaf > sizeof(unsigned long) * 8) {
13bd6494 4237 rcu_fanout_leaf = RCU_FANOUT_LEAF;
f885b7f2
PM
4238 WARN_ON(1);
4239 return;
4240 }
4241
f885b7f2
PM
4242 /*
4243 * Compute number of nodes that can be handled an rcu_node tree
9618138b 4244 * with the given number of levels.
f885b7f2 4245 */
9618138b 4246 rcu_capacity[0] = rcu_fanout_leaf;
05b84aec 4247 for (i = 1; i < RCU_NUM_LVLS; i++)
05c5df31 4248 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
f885b7f2
PM
4249
4250 /*
75cf15a4 4251 * The tree must be able to accommodate the configured number of CPUs.
ee968ac6 4252 * If this limit is exceeded, fall back to the compile-time values.
f885b7f2 4253 */
ee968ac6
PM
4254 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
4255 rcu_fanout_leaf = RCU_FANOUT_LEAF;
4256 WARN_ON(1);
4257 return;
4258 }
f885b7f2 4259
679f9858 4260 /* Calculate the number of levels in the tree. */
9618138b 4261 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
679f9858 4262 }
9618138b 4263 rcu_num_lvls = i + 1;
679f9858 4264
f885b7f2 4265 /* Calculate the number of rcu_nodes at each level of the tree. */
679f9858 4266 for (i = 0; i < rcu_num_lvls; i++) {
9618138b 4267 int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
679f9858
AG
4268 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
4269 }
f885b7f2
PM
4270
4271 /* Calculate the total number of rcu_node structures. */
4272 rcu_num_nodes = 0;
679f9858 4273 for (i = 0; i < rcu_num_lvls; i++)
f885b7f2 4274 rcu_num_nodes += num_rcu_lvl[i];
f885b7f2
PM
4275}
4276
a3dc2948
PM
4277/*
4278 * Dump out the structure of the rcu_node combining tree associated
4279 * with the rcu_state structure referenced by rsp.
4280 */
4281static void __init rcu_dump_rcu_node_tree(struct rcu_state *rsp)
4282{
4283 int level = 0;
4284 struct rcu_node *rnp;
4285
4286 pr_info("rcu_node tree layout dump\n");
4287 pr_info(" ");
4288 rcu_for_each_node_breadth_first(rsp, rnp) {
4289 if (rnp->level != level) {
4290 pr_cont("\n");
4291 pr_info(" ");
4292 level = rnp->level;
4293 }
4294 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
4295 }
4296 pr_cont("\n");
4297}
4298
9f680ab4 4299void __init rcu_init(void)
64db4cff 4300{
017c4261 4301 int cpu;
9f680ab4 4302
47627678
PM
4303 rcu_early_boot_tests();
4304
f41d911f 4305 rcu_bootup_announce();
f885b7f2 4306 rcu_init_geometry();
a87f203e
PM
4307 rcu_init_one(&rcu_bh_state);
4308 rcu_init_one(&rcu_sched_state);
a3dc2948
PM
4309 if (dump_tree)
4310 rcu_dump_rcu_node_tree(&rcu_sched_state);
f41d911f 4311 __rcu_init_preempt();
b5b39360 4312 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
9f680ab4
PM
4313
4314 /*
4315 * We don't need protection against CPU-hotplug here because
4316 * this is called early in boot, before either interrupts
4317 * or the scheduler are operational.
4318 */
d1d74d14 4319 pm_notifier(rcu_pm_notify, 0);
7ec99de3 4320 for_each_online_cpu(cpu) {
4df83742 4321 rcutree_prepare_cpu(cpu);
7ec99de3
PM
4322 rcu_cpu_starting(cpu);
4323 }
64db4cff
PM
4324}
4325
3549c2bc 4326#include "tree_exp.h"
4102adab 4327#include "tree_plugin.h"