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