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