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