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