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