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