]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/rcu/tree.c
Merge tag 'aspeed-5.5-devicetree-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / kernel / rcu / tree.c
CommitLineData
22e40925 1// SPDX-License-Identifier: GPL-2.0+
64db4cff
PM
2/*
3 * Read-Copy Update mechanism for mutual exclusion
4 *
64db4cff
PM
5 * Copyright IBM Corporation, 2008
6 *
7 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
8 * Manfred Spraul <manfred@colorfullife.com>
22e40925 9 * Paul E. McKenney <paulmck@linux.ibm.com> Hierarchical version
64db4cff 10 *
22e40925 11 * Based on the original work by Paul McKenney <paulmck@linux.ibm.com>
64db4cff
PM
12 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
13 *
14 * For detailed explanation of Read-Copy Update mechanism see -
a71fca58 15 * Documentation/RCU
64db4cff 16 */
a7538352
JP
17
18#define pr_fmt(fmt) "rcu: " fmt
19
64db4cff
PM
20#include <linux/types.h>
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/spinlock.h>
24#include <linux/smp.h>
f9411ebe 25#include <linux/rcupdate_wait.h>
64db4cff
PM
26#include <linux/interrupt.h>
27#include <linux/sched.h>
b17b0153 28#include <linux/sched/debug.h>
c1dc0b9c 29#include <linux/nmi.h>
8826f3b0 30#include <linux/atomic.h>
64db4cff 31#include <linux/bitops.h>
9984de1a 32#include <linux/export.h>
64db4cff
PM
33#include <linux/completion.h>
34#include <linux/moduleparam.h>
35#include <linux/percpu.h>
36#include <linux/notifier.h>
37#include <linux/cpu.h>
38#include <linux/mutex.h>
39#include <linux/time.h>
bbad9379 40#include <linux/kernel_stat.h>
a26ac245
PM
41#include <linux/wait.h>
42#include <linux/kthread.h>
ae7e81c0 43#include <uapi/linux/sched/types.h>
268bb0ce 44#include <linux/prefetch.h>
3d3b7db0
PM
45#include <linux/delay.h>
46#include <linux/stop_machine.h>
661a85dc 47#include <linux/random.h>
af658dca 48#include <linux/trace_events.h>
d1d74d14 49#include <linux/suspend.h>
a278d471 50#include <linux/ftrace.h>
d3052109 51#include <linux/tick.h>
2ccaff10 52#include <linux/sysrq.h>
c13324a5 53#include <linux/kprobes.h>
48d07c04
SAS
54#include <linux/gfp.h>
55#include <linux/oom.h>
56#include <linux/smpboot.h>
57#include <linux/jiffies.h>
58#include <linux/sched/isolation.h>
cfcdef5e 59#include <linux/sched/clock.h>
48d07c04 60#include "../time/tick-internal.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 72/*
dc5a4f29
PM
73 * Steal a bit from the bottom of ->dynticks for idle entry/exit
74 * control. Initially this is for TLB flushing.
f7f7bac9 75 */
dc5a4f29
PM
76#define RCU_DYNTICK_CTRL_MASK 0x1
77#define RCU_DYNTICK_CTRL_CTR (RCU_DYNTICK_CTRL_MASK + 1)
78#ifndef rcu_eqs_special_exit
79#define rcu_eqs_special_exit() do { } while (0)
a8a29b3b
AB
80#endif
81
4c5273bf
PM
82static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
83 .dynticks_nesting = 1,
84 .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,
dc5a4f29 85 .dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),
4c5273bf 86};
358be2d3
PM
87struct rcu_state rcu_state = {
88 .level = { &rcu_state.node[0] },
358be2d3
PM
89 .gp_state = RCU_GP_IDLE,
90 .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT,
91 .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex),
92 .name = RCU_NAME,
93 .abbr = RCU_ABBR,
94 .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
95 .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
894d45bb 96 .ofl_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.ofl_lock),
358be2d3 97};
27f4d280 98
a3dc2948
PM
99/* Dump rcu_node combining tree at boot to verify correct setup. */
100static bool dump_tree;
101module_param(dump_tree, bool, 0444);
48d07c04
SAS
102/* By default, use RCU_SOFTIRQ instead of rcuc kthreads. */
103static bool use_softirq = 1;
104module_param(use_softirq, bool, 0444);
7fa27001
PM
105/* Control rcu_node-tree auto-balancing at boot time. */
106static bool rcu_fanout_exact;
107module_param(rcu_fanout_exact, bool, 0444);
47d631af
PM
108/* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
109static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
7e5c2dfb 110module_param(rcu_fanout_leaf, int, 0444);
f885b7f2 111int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
cb007102 112/* Number of rcu_nodes at specified level. */
e95d68d2 113int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
f885b7f2
PM
114int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
115
b0d30417 116/*
52d7e48b
PM
117 * The rcu_scheduler_active variable is initialized to the value
118 * RCU_SCHEDULER_INACTIVE and transitions RCU_SCHEDULER_INIT just before the
119 * first task is spawned. So when this variable is RCU_SCHEDULER_INACTIVE,
120 * RCU can assume that there is but one task, allowing RCU to (for example)
0d95092c 121 * optimize synchronize_rcu() to a simple barrier(). When this variable
52d7e48b
PM
122 * is RCU_SCHEDULER_INIT, RCU must actually do all the hard work required
123 * to detect real grace periods. This variable is also used to suppress
124 * boot-time false positives from lockdep-RCU error checking. Finally, it
125 * transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU
126 * is fully initialized, including all of its kthreads having been spawned.
b0d30417 127 */
bbad9379
PM
128int rcu_scheduler_active __read_mostly;
129EXPORT_SYMBOL_GPL(rcu_scheduler_active);
130
b0d30417
PM
131/*
132 * The rcu_scheduler_fully_active variable transitions from zero to one
133 * during the early_initcall() processing, which is after the scheduler
134 * is capable of creating new tasks. So RCU processing (for example,
135 * creating tasks for RCU priority boosting) must be delayed until after
136 * rcu_scheduler_fully_active transitions from zero to one. We also
137 * currently delay invocation of any RCU callbacks until after this point.
138 *
139 * It might later prove better for people registering RCU callbacks during
140 * early boot to take responsibility for these callbacks, but one step at
141 * a time.
142 */
143static int rcu_scheduler_fully_active __read_mostly;
144
b50912d0
PM
145static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
146 unsigned long gps, unsigned long flags);
0aa04b05
PM
147static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
148static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
5d01bbd1 149static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
a46e0899 150static void invoke_rcu_core(void);
63d4c8c9 151static void rcu_report_exp_rdp(struct rcu_data *rdp);
3549c2bc 152static void sync_sched_exp_online_cleanup(int cpu);
a26ac245 153
a94844b2 154/* rcuc/rcub kthread realtime priority */
26730f55 155static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
3ffe3d1a 156module_param(kthread_prio, int, 0444);
a94844b2 157
8d7dc928 158/* Delay in jiffies for grace-period initialization delays, debug only. */
0f41c0dd 159
90040c9e
PM
160static int gp_preinit_delay;
161module_param(gp_preinit_delay, int, 0444);
162static int gp_init_delay;
163module_param(gp_init_delay, int, 0444);
164static int gp_cleanup_delay;
165module_param(gp_cleanup_delay, int, 0444);
0f41c0dd 166
4cf439a2 167/* Retrieve RCU kthreads priority for rcutorture */
4babd855
JFG
168int rcu_get_gp_kthreads_prio(void)
169{
170 return kthread_prio;
171}
172EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio);
173
eab128e8
PM
174/*
175 * Number of grace periods between delays, normalized by the duration of
bfd090be 176 * the delay. The longer the delay, the more the grace periods between
eab128e8
PM
177 * each delay. The reason for this normalization is that it means that,
178 * for non-zero delays, the overall slowdown of grace periods is constant
179 * regardless of the duration of the delay. This arrangement balances
180 * the need for long delays to increase some race probabilities with the
181 * need for fast grace periods to increase other race probabilities.
182 */
183#define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
37745d28 184
0aa04b05
PM
185/*
186 * Compute the mask of online CPUs for the specified rcu_node structure.
187 * This will not be stable unless the rcu_node structure's ->lock is
188 * held, but the bit corresponding to the current CPU will be stable
189 * in most contexts.
190 */
191unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
192{
7d0ae808 193 return READ_ONCE(rnp->qsmaskinitnext);
0aa04b05
PM
194}
195
fc2219d4 196/*
7d0ae808 197 * Return true if an RCU grace period is in progress. The READ_ONCE()s
fc2219d4
PM
198 * permit this function to be invoked without holding the root rcu_node
199 * structure's ->lock, but of course results can be subject to change.
200 */
de8e8730 201static int rcu_gp_in_progress(void)
fc2219d4 202{
de8e8730 203 return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq));
b1f77b05
IM
204}
205
903ee83d
PM
206/*
207 * Return the number of callbacks queued on the specified CPU.
208 * Handles both the nocbs and normal cases.
209 */
210static long rcu_get_n_cbs_cpu(int cpu)
211{
212 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
213
c035280f 214 if (rcu_segcblist_is_enabled(&rdp->cblist))
903ee83d 215 return rcu_segcblist_n_cbs(&rdp->cblist);
c035280f 216 return 0;
903ee83d
PM
217}
218
d28139c4 219void rcu_softirq_qs(void)
b1f77b05 220{
45975c7d 221 rcu_qs();
d28139c4 222 rcu_preempt_deferred_qs(current);
b1f77b05 223}
64db4cff 224
2625d469
PM
225/*
226 * Record entry into an extended quiescent state. This is only to be
227 * called when not already in an extended quiescent state.
228 */
229static void rcu_dynticks_eqs_enter(void)
230{
dc5a4f29 231 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
b8c17e66 232 int seq;
2625d469
PM
233
234 /*
b8c17e66 235 * CPUs seeing atomic_add_return() must see prior RCU read-side
2625d469
PM
236 * critical sections, and we also must force ordering with the
237 * next idle sojourn.
238 */
dc5a4f29 239 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
b8c17e66
PM
240 /* Better be in an extended quiescent state! */
241 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
242 (seq & RCU_DYNTICK_CTRL_CTR));
243 /* Better not have special action (TLB flush) pending! */
244 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
245 (seq & RCU_DYNTICK_CTRL_MASK));
2625d469
PM
246}
247
248/*
249 * Record exit from an extended quiescent state. This is only to be
250 * called from an extended quiescent state.
251 */
252static void rcu_dynticks_eqs_exit(void)
253{
dc5a4f29 254 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
b8c17e66 255 int seq;
2625d469
PM
256
257 /*
b8c17e66 258 * CPUs seeing atomic_add_return() must see prior idle sojourns,
2625d469
PM
259 * and we also must force ordering with the next RCU read-side
260 * critical section.
261 */
dc5a4f29 262 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
b8c17e66
PM
263 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
264 !(seq & RCU_DYNTICK_CTRL_CTR));
265 if (seq & RCU_DYNTICK_CTRL_MASK) {
dc5a4f29 266 atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdp->dynticks);
b8c17e66
PM
267 smp_mb__after_atomic(); /* _exit after clearing mask. */
268 /* Prefer duplicate flushes to losing a flush. */
269 rcu_eqs_special_exit();
270 }
2625d469
PM
271}
272
273/*
274 * Reset the current CPU's ->dynticks counter to indicate that the
275 * newly onlined CPU is no longer in an extended quiescent state.
276 * This will either leave the counter unchanged, or increment it
277 * to the next non-quiescent value.
278 *
279 * The non-atomic test/increment sequence works because the upper bits
280 * of the ->dynticks counter are manipulated only by the corresponding CPU,
281 * or when the corresponding CPU is offline.
282 */
283static void rcu_dynticks_eqs_online(void)
284{
dc5a4f29 285 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
2625d469 286
dc5a4f29 287 if (atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR)
2625d469 288 return;
dc5a4f29 289 atomic_add(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
2625d469
PM
290}
291
02a5c550
PM
292/*
293 * Is the current CPU in an extended quiescent state?
294 *
295 * No ordering, as we are sampling CPU-local information.
296 */
297bool rcu_dynticks_curr_cpu_in_eqs(void)
298{
dc5a4f29 299 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
02a5c550 300
dc5a4f29 301 return !(atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR);
02a5c550
PM
302}
303
8b2f63ab
PM
304/*
305 * Snapshot the ->dynticks counter with full ordering so as to allow
306 * stable comparison of this counter with past and future snapshots.
307 */
dc5a4f29 308int rcu_dynticks_snap(struct rcu_data *rdp)
8b2f63ab 309{
dc5a4f29 310 int snap = atomic_add_return(0, &rdp->dynticks);
8b2f63ab 311
b8c17e66 312 return snap & ~RCU_DYNTICK_CTRL_MASK;
8b2f63ab
PM
313}
314
02a5c550
PM
315/*
316 * Return true if the snapshot returned from rcu_dynticks_snap()
317 * indicates that RCU is in an extended quiescent state.
318 */
319static bool rcu_dynticks_in_eqs(int snap)
320{
b8c17e66 321 return !(snap & RCU_DYNTICK_CTRL_CTR);
02a5c550
PM
322}
323
324/*
dc5a4f29 325 * Return true if the CPU corresponding to the specified rcu_data
02a5c550
PM
326 * structure has spent some time in an extended quiescent state since
327 * rcu_dynticks_snap() returned the specified snapshot.
328 */
dc5a4f29 329static bool rcu_dynticks_in_eqs_since(struct rcu_data *rdp, int snap)
02a5c550 330{
dc5a4f29 331 return snap != rcu_dynticks_snap(rdp);
02a5c550
PM
332}
333
b8c17e66
PM
334/*
335 * Set the special (bottom) bit of the specified CPU so that it
336 * will take special action (such as flushing its TLB) on the
337 * next exit from an extended quiescent state. Returns true if
338 * the bit was successfully set, or false if the CPU was not in
339 * an extended quiescent state.
340 */
341bool rcu_eqs_special_set(int cpu)
342{
343 int old;
344 int new;
dc5a4f29 345 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
b8c17e66
PM
346
347 do {
dc5a4f29 348 old = atomic_read(&rdp->dynticks);
b8c17e66
PM
349 if (old & RCU_DYNTICK_CTRL_CTR)
350 return false;
351 new = old | RCU_DYNTICK_CTRL_MASK;
dc5a4f29 352 } while (atomic_cmpxchg(&rdp->dynticks, old, new) != old);
b8c17e66 353 return true;
6563de9d 354}
5cd37193 355
4a81e832
PM
356/*
357 * Let the RCU core know that this CPU has gone through the scheduler,
358 * which is a quiescent state. This is called when the need for a
359 * quiescent state is urgent, so we burn an atomic operation and full
360 * memory barriers to let the RCU core know about it, regardless of what
361 * this CPU might (or might not) do in the near future.
362 *
0f9be8ca 363 * We inform the RCU core by emulating a zero-duration dyntick-idle period.
46a5d164 364 *
3b57a399 365 * The caller must have disabled interrupts and must not be idle.
4a81e832 366 */
366237e7 367void rcu_momentary_dyntick_idle(void)
4a81e832 368{
3b57a399
PM
369 int special;
370
2dba13f0 371 raw_cpu_write(rcu_data.rcu_need_heavy_qs, false);
dc5a4f29
PM
372 special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR,
373 &this_cpu_ptr(&rcu_data)->dynticks);
3b57a399
PM
374 /* It is illegal to call this from idle state. */
375 WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR));
3e310098 376 rcu_preempt_deferred_qs(current);
4a81e832 377}
79ba7ff5 378EXPORT_SYMBOL_GPL(rcu_momentary_dyntick_idle);
4a81e832 379
45975c7d 380/**
eddded80 381 * rcu_is_cpu_rrupt_from_idle - see if interrupted from idle
bb73c52b 382 *
eddded80 383 * If the current CPU is idle and running at a first-level (not nested)
45975c7d
PM
384 * interrupt from idle, return true. The caller must have at least
385 * disabled preemption.
5cd37193 386 */
45975c7d 387static int rcu_is_cpu_rrupt_from_idle(void)
5cd37193 388{
eddded80
JFG
389 /* Called only from within the scheduling-clock interrupt */
390 lockdep_assert_in_irq();
391
392 /* Check for counter underflows */
393 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nesting) < 0,
394 "RCU dynticks_nesting counter underflow!");
395 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nmi_nesting) <= 0,
396 "RCU dynticks_nmi_nesting counter underflow/zero!");
397
398 /* Are we at first interrupt nesting level? */
399 if (__this_cpu_read(rcu_data.dynticks_nmi_nesting) != 1)
400 return false;
401
402 /* Does CPU appear to be idle from an RCU standpoint? */
403 return __this_cpu_read(rcu_data.dynticks_nesting) == 0;
5cd37193 404}
5cd37193 405
d5a9a8c3
PM
406#define DEFAULT_RCU_BLIMIT 10 /* Maximum callbacks per rcu_do_batch ... */
407#define DEFAULT_MAX_RCU_BLIMIT 10000 /* ... even during callback flood. */
17c7798b
PM
408static long blimit = DEFAULT_RCU_BLIMIT;
409#define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */
410static long qhimark = DEFAULT_RCU_QHIMARK;
411#define DEFAULT_RCU_QLOMARK 100 /* Once only this many pending, use blimit. */
412static long qlowmark = DEFAULT_RCU_QLOMARK;
64db4cff 413
878d7439
ED
414module_param(blimit, long, 0444);
415module_param(qhimark, long, 0444);
416module_param(qlowmark, long, 0444);
3d76c082 417
026ad283
PM
418static ulong jiffies_till_first_fqs = ULONG_MAX;
419static ulong jiffies_till_next_fqs = ULONG_MAX;
8c7c4829 420static bool rcu_kick_kthreads;
cfcdef5e
ED
421static int rcu_divisor = 7;
422module_param(rcu_divisor, int, 0644);
423
424/* Force an exit from rcu_do_batch() after 3 milliseconds. */
425static long rcu_resched_ns = 3 * NSEC_PER_MSEC;
426module_param(rcu_resched_ns, long, 0644);
d40011f6 427
c06aed0e
PM
428/*
429 * How long the grace period must be before we start recruiting
430 * quiescent-state help from rcu_note_context_switch().
431 */
432static ulong jiffies_till_sched_qs = ULONG_MAX;
433module_param(jiffies_till_sched_qs, ulong, 0444);
85f2b60c 434static ulong jiffies_to_sched_qs; /* See adjust_jiffies_till_sched_qs(). */
c06aed0e
PM
435module_param(jiffies_to_sched_qs, ulong, 0444); /* Display only! */
436
437/*
438 * Make sure that we give the grace-period kthread time to detect any
439 * idle CPUs before taking active measures to force quiescent states.
440 * However, don't go below 100 milliseconds, adjusted upwards for really
441 * large systems.
442 */
443static void adjust_jiffies_till_sched_qs(void)
444{
445 unsigned long j;
446
447 /* If jiffies_till_sched_qs was specified, respect the request. */
448 if (jiffies_till_sched_qs != ULONG_MAX) {
449 WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs);
450 return;
451 }
85f2b60c 452 /* Otherwise, set to third fqs scan, but bound below on large system. */
c06aed0e
PM
453 j = READ_ONCE(jiffies_till_first_fqs) +
454 2 * READ_ONCE(jiffies_till_next_fqs);
455 if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV)
456 j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
457 pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j);
458 WRITE_ONCE(jiffies_to_sched_qs, j);
459}
460
67abb96c
BP
461static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp)
462{
463 ulong j;
464 int ret = kstrtoul(val, 0, &j);
465
c06aed0e 466 if (!ret) {
67abb96c 467 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j);
c06aed0e
PM
468 adjust_jiffies_till_sched_qs();
469 }
67abb96c
BP
470 return ret;
471}
472
473static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp)
474{
475 ulong j;
476 int ret = kstrtoul(val, 0, &j);
477
c06aed0e 478 if (!ret) {
67abb96c 479 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1));
c06aed0e
PM
480 adjust_jiffies_till_sched_qs();
481 }
67abb96c
BP
482 return ret;
483}
484
485static struct kernel_param_ops first_fqs_jiffies_ops = {
486 .set = param_set_first_fqs_jiffies,
487 .get = param_get_ulong,
488};
489
490static struct kernel_param_ops next_fqs_jiffies_ops = {
491 .set = param_set_next_fqs_jiffies,
492 .get = param_get_ulong,
493};
494
495module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644);
496module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644);
8c7c4829 497module_param(rcu_kick_kthreads, bool, 0644);
d40011f6 498
8ff0b907 499static void force_qs_rnp(int (*f)(struct rcu_data *rdp));
dd7dafd1 500static int rcu_pending(int user);
64db4cff
PM
501
502/*
17ef2fe9 503 * Return the number of RCU GPs completed thus far for debug & stats.
64db4cff 504 */
17ef2fe9 505unsigned long rcu_get_gp_seq(void)
917963d0 506{
16fc9c60 507 return READ_ONCE(rcu_state.gp_seq);
917963d0 508}
17ef2fe9 509EXPORT_SYMBOL_GPL(rcu_get_gp_seq);
917963d0 510
291783b8
PM
511/*
512 * Return the number of RCU expedited batches completed thus far for
513 * debug & stats. Odd numbers mean that a batch is in progress, even
514 * numbers mean idle. The value returned will thus be roughly double
515 * the cumulative batches since boot.
516 */
517unsigned long rcu_exp_batches_completed(void)
518{
16fc9c60 519 return rcu_state.expedited_sequence;
291783b8
PM
520}
521EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
522
fd897573
PM
523/*
524 * Return the root node of the rcu_state structure.
525 */
526static struct rcu_node *rcu_get_root(void)
527{
528 return &rcu_state.node[0];
529}
530
69196019
PM
531/*
532 * Convert a ->gp_state value to a character string.
533 */
534static const char *gp_state_getname(short gs)
535{
536 if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
537 return "???";
538 return gp_state_names[gs];
539}
540
ad0dc7f9
PM
541/*
542 * Send along grace-period-related data for rcutorture diagnostics.
543 */
544void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
aebc8264 545 unsigned long *gp_seq)
ad0dc7f9 546{
ad0dc7f9
PM
547 switch (test_type) {
548 case RCU_FLAVOR:
f7dd7d44
PM
549 *flags = READ_ONCE(rcu_state.gp_flags);
550 *gp_seq = rcu_seq_current(&rcu_state.gp_seq);
ad0dc7f9
PM
551 break;
552 default:
553 break;
554 }
ad0dc7f9
PM
555}
556EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
557
9b2e4f18 558/*
215bba9f
PM
559 * Enter an RCU extended quiescent state, which can be either the
560 * idle loop or adaptive-tickless usermode execution.
9b2e4f18 561 *
215bba9f
PM
562 * We crowbar the ->dynticks_nmi_nesting field to zero to allow for
563 * the possibility of usermode upcalls having messed up our count
564 * of interrupt nesting level during the prior busy period.
9b2e4f18 565 */
215bba9f 566static void rcu_eqs_enter(bool user)
9b2e4f18 567{
4c5273bf 568 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
96d3fd0d 569
4c5273bf
PM
570 WARN_ON_ONCE(rdp->dynticks_nmi_nesting != DYNTICK_IRQ_NONIDLE);
571 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0);
215bba9f 572 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
4c5273bf
PM
573 rdp->dynticks_nesting == 0);
574 if (rdp->dynticks_nesting != 1) {
575 rdp->dynticks_nesting--;
215bba9f 576 return;
9b2e4f18 577 }
96d3fd0d 578
b04db8e1 579 lockdep_assert_irqs_disabled();
dc5a4f29 580 trace_rcu_dyntick(TPS("Start"), rdp->dynticks_nesting, 0, rdp->dynticks);
e68bbb26 581 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
b97d23c5
PM
582 rdp = this_cpu_ptr(&rcu_data);
583 do_nocb_deferred_wakeup(rdp);
198bbf81 584 rcu_prepare_for_idle();
3e310098 585 rcu_preempt_deferred_qs(current);
4c5273bf 586 WRITE_ONCE(rdp->dynticks_nesting, 0); /* Avoid irq-access tearing. */
844ccdd7 587 rcu_dynticks_eqs_enter();
176f8f7a 588 rcu_dynticks_task_enter();
64db4cff 589}
adf5091e
FW
590
591/**
592 * rcu_idle_enter - inform RCU that current CPU is entering idle
593 *
594 * Enter idle mode, in other words, -leave- the mode in which RCU
595 * read-side critical sections can occur. (Though RCU read-side
596 * critical sections can occur in irq handlers in idle, a possibility
597 * handled by irq_enter() and irq_exit().)
598 *
c0da313e
PM
599 * If you add or remove a call to rcu_idle_enter(), be sure to test with
600 * CONFIG_RCU_EQS_DEBUG=y.
adf5091e
FW
601 */
602void rcu_idle_enter(void)
603{
b04db8e1 604 lockdep_assert_irqs_disabled();
cb349ca9 605 rcu_eqs_enter(false);
adf5091e 606}
64db4cff 607
d1ec4c34 608#ifdef CONFIG_NO_HZ_FULL
adf5091e
FW
609/**
610 * rcu_user_enter - inform RCU that we are resuming userspace.
611 *
612 * Enter RCU idle mode right before resuming userspace. No use of RCU
613 * is permitted between this call and rcu_user_exit(). This way the
614 * CPU doesn't need to maintain the tick for RCU maintenance purposes
615 * when the CPU runs in userspace.
c0da313e
PM
616 *
617 * If you add or remove a call to rcu_user_enter(), be sure to test with
618 * CONFIG_RCU_EQS_DEBUG=y.
adf5091e
FW
619 */
620void rcu_user_enter(void)
621{
b04db8e1 622 lockdep_assert_irqs_disabled();
d4db30af 623 rcu_eqs_enter(true);
adf5091e 624}
d1ec4c34 625#endif /* CONFIG_NO_HZ_FULL */
19dd1591 626
cf7614e1 627/*
fd581a91 628 * If we are returning from the outermost NMI handler that interrupted an
dc5a4f29 629 * RCU-idle period, update rdp->dynticks and rdp->dynticks_nmi_nesting
fd581a91
PM
630 * to let the RCU grace-period handling know that the CPU is back to
631 * being RCU-idle.
632 *
cf7614e1 633 * If you add or remove a call to rcu_nmi_exit_common(), be sure to test
fd581a91
PM
634 * with CONFIG_RCU_EQS_DEBUG=y.
635 */
cf7614e1 636static __always_inline void rcu_nmi_exit_common(bool irq)
fd581a91 637{
4c5273bf 638 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
fd581a91
PM
639
640 /*
641 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
642 * (We are exiting an NMI handler, so RCU better be paying attention
643 * to us!)
644 */
4c5273bf 645 WARN_ON_ONCE(rdp->dynticks_nmi_nesting <= 0);
fd581a91
PM
646 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
647
648 /*
649 * If the nesting level is not 1, the CPU wasn't RCU-idle, so
650 * leave it in non-RCU-idle state.
651 */
4c5273bf 652 if (rdp->dynticks_nmi_nesting != 1) {
dc5a4f29 653 trace_rcu_dyntick(TPS("--="), rdp->dynticks_nmi_nesting, rdp->dynticks_nmi_nesting - 2, rdp->dynticks);
4c5273bf
PM
654 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* No store tearing. */
655 rdp->dynticks_nmi_nesting - 2);
fd581a91
PM
656 return;
657 }
658
659 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
dc5a4f29 660 trace_rcu_dyntick(TPS("Startirq"), rdp->dynticks_nmi_nesting, 0, rdp->dynticks);
4c5273bf 661 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); /* Avoid store tearing. */
cf7614e1
BP
662
663 if (irq)
664 rcu_prepare_for_idle();
665
fd581a91 666 rcu_dynticks_eqs_enter();
cf7614e1
BP
667
668 if (irq)
669 rcu_dynticks_task_enter();
670}
671
672/**
673 * rcu_nmi_exit - inform RCU of exit from NMI context
cf7614e1
BP
674 *
675 * If you add or remove a call to rcu_nmi_exit(), be sure to test
676 * with CONFIG_RCU_EQS_DEBUG=y.
677 */
678void rcu_nmi_exit(void)
679{
680 rcu_nmi_exit_common(false);
fd581a91
PM
681}
682
9b2e4f18
PM
683/**
684 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
685 *
686 * Exit from an interrupt handler, which might possibly result in entering
687 * idle mode, in other words, leaving the mode in which read-side critical
7c9906ca 688 * sections can occur. The caller must have disabled interrupts.
64db4cff 689 *
9b2e4f18
PM
690 * This code assumes that the idle loop never does anything that might
691 * result in unbalanced calls to irq_enter() and irq_exit(). If your
58721f5d
PM
692 * architecture's idle loop violates this assumption, RCU will give you what
693 * you deserve, good and hard. But very infrequently and irreproducibly.
9b2e4f18
PM
694 *
695 * Use things like work queues to work around this limitation.
696 *
697 * You have been warned.
c0da313e
PM
698 *
699 * If you add or remove a call to rcu_irq_exit(), be sure to test with
700 * CONFIG_RCU_EQS_DEBUG=y.
64db4cff 701 */
9b2e4f18 702void rcu_irq_exit(void)
64db4cff 703{
b04db8e1 704 lockdep_assert_irqs_disabled();
cf7614e1 705 rcu_nmi_exit_common(true);
7c9906ca
PM
706}
707
708/*
709 * Wrapper for rcu_irq_exit() where interrupts are enabled.
c0da313e
PM
710 *
711 * If you add or remove a call to rcu_irq_exit_irqson(), be sure to test
712 * with CONFIG_RCU_EQS_DEBUG=y.
7c9906ca
PM
713 */
714void rcu_irq_exit_irqson(void)
715{
716 unsigned long flags;
717
718 local_irq_save(flags);
719 rcu_irq_exit();
9b2e4f18
PM
720 local_irq_restore(flags);
721}
722
adf5091e
FW
723/*
724 * Exit an RCU extended quiescent state, which can be either the
725 * idle loop or adaptive-tickless usermode execution.
51a1fd30
PM
726 *
727 * We crowbar the ->dynticks_nmi_nesting field to DYNTICK_IRQ_NONIDLE to
728 * allow for the possibility of usermode upcalls messing up our count of
729 * interrupt nesting level during the busy period that is just now starting.
9b2e4f18 730 */
adf5091e 731static void rcu_eqs_exit(bool user)
9b2e4f18 732{
4c5273bf 733 struct rcu_data *rdp;
84585aa8 734 long oldval;
9b2e4f18 735
b04db8e1 736 lockdep_assert_irqs_disabled();
4c5273bf
PM
737 rdp = this_cpu_ptr(&rcu_data);
738 oldval = rdp->dynticks_nesting;
1ce46ee5 739 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
51a1fd30 740 if (oldval) {
4c5273bf 741 rdp->dynticks_nesting++;
9dd238e2 742 return;
3a592405 743 }
9dd238e2
PM
744 rcu_dynticks_task_exit();
745 rcu_dynticks_eqs_exit();
746 rcu_cleanup_after_idle();
dc5a4f29 747 trace_rcu_dyntick(TPS("End"), rdp->dynticks_nesting, 1, rdp->dynticks);
e68bbb26 748 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
4c5273bf
PM
749 WRITE_ONCE(rdp->dynticks_nesting, 1);
750 WARN_ON_ONCE(rdp->dynticks_nmi_nesting);
751 WRITE_ONCE(rdp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE);
9b2e4f18 752}
adf5091e
FW
753
754/**
755 * rcu_idle_exit - inform RCU that current CPU is leaving idle
756 *
757 * Exit idle mode, in other words, -enter- the mode in which RCU
758 * read-side critical sections can occur.
759 *
c0da313e
PM
760 * If you add or remove a call to rcu_idle_exit(), be sure to test with
761 * CONFIG_RCU_EQS_DEBUG=y.
adf5091e
FW
762 */
763void rcu_idle_exit(void)
764{
c5d900bf
FW
765 unsigned long flags;
766
767 local_irq_save(flags);
cb349ca9 768 rcu_eqs_exit(false);
c5d900bf 769 local_irq_restore(flags);
adf5091e 770}
9b2e4f18 771
d1ec4c34 772#ifdef CONFIG_NO_HZ_FULL
adf5091e
FW
773/**
774 * rcu_user_exit - inform RCU that we are exiting userspace.
775 *
776 * Exit RCU idle mode while entering the kernel because it can
777 * run a RCU read side critical section anytime.
c0da313e
PM
778 *
779 * If you add or remove a call to rcu_user_exit(), be sure to test with
780 * CONFIG_RCU_EQS_DEBUG=y.
adf5091e
FW
781 */
782void rcu_user_exit(void)
783{
91d1aa43 784 rcu_eqs_exit(1);
adf5091e 785}
d1ec4c34 786#endif /* CONFIG_NO_HZ_FULL */
19dd1591 787
64db4cff 788/**
cf7614e1
BP
789 * rcu_nmi_enter_common - inform RCU of entry to NMI context
790 * @irq: Is this call from rcu_irq_enter?
64db4cff 791 *
dc5a4f29 792 * If the CPU was idle from RCU's viewpoint, update rdp->dynticks and
4c5273bf 793 * rdp->dynticks_nmi_nesting to let the RCU grace-period handling know
734d1680
PM
794 * that the CPU is active. This implementation permits nested NMIs, as
795 * long as the nesting level does not overflow an int. (You will probably
796 * run out of stack space first.)
c0da313e 797 *
cf7614e1 798 * If you add or remove a call to rcu_nmi_enter_common(), be sure to test
c0da313e 799 * with CONFIG_RCU_EQS_DEBUG=y.
64db4cff 800 */
cf7614e1 801static __always_inline void rcu_nmi_enter_common(bool irq)
64db4cff 802{
4c5273bf 803 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
84585aa8 804 long incby = 2;
64db4cff 805
734d1680 806 /* Complain about underflow. */
4c5273bf 807 WARN_ON_ONCE(rdp->dynticks_nmi_nesting < 0);
734d1680
PM
808
809 /*
810 * If idle from RCU viewpoint, atomically increment ->dynticks
811 * to mark non-idle and increment ->dynticks_nmi_nesting by one.
812 * Otherwise, increment ->dynticks_nmi_nesting by two. This means
813 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
814 * to be in the outermost NMI handler that interrupted an RCU-idle
815 * period (observation due to Andy Lutomirski).
816 */
02a5c550 817 if (rcu_dynticks_curr_cpu_in_eqs()) {
cf7614e1
BP
818
819 if (irq)
820 rcu_dynticks_task_exit();
821
2625d469 822 rcu_dynticks_eqs_exit();
cf7614e1
BP
823
824 if (irq)
825 rcu_cleanup_after_idle();
826
734d1680 827 incby = 1;
b200a048
PM
828 } else if (tick_nohz_full_cpu(rdp->cpu) &&
829 rdp->dynticks_nmi_nesting == DYNTICK_IRQ_NONIDLE &&
516e5ae0 830 READ_ONCE(rdp->rcu_urgent_qs) && !rdp->rcu_forced_tick) {
b200a048
PM
831 rdp->rcu_forced_tick = true;
832 tick_dep_set_cpu(rdp->cpu, TICK_DEP_BIT_RCU);
734d1680 833 }
bd2b879a 834 trace_rcu_dyntick(incby == 1 ? TPS("Endirq") : TPS("++="),
4c5273bf 835 rdp->dynticks_nmi_nesting,
dc5a4f29 836 rdp->dynticks_nmi_nesting + incby, rdp->dynticks);
4c5273bf
PM
837 WRITE_ONCE(rdp->dynticks_nmi_nesting, /* Prevent store tearing. */
838 rdp->dynticks_nmi_nesting + incby);
734d1680 839 barrier();
64db4cff
PM
840}
841
cf7614e1
BP
842/**
843 * rcu_nmi_enter - inform RCU of entry to NMI context
844 */
845void rcu_nmi_enter(void)
846{
847 rcu_nmi_enter_common(false);
848}
c13324a5 849NOKPROBE_SYMBOL(rcu_nmi_enter);
cf7614e1 850
64db4cff 851/**
9b2e4f18 852 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
64db4cff 853 *
9b2e4f18
PM
854 * Enter an interrupt handler, which might possibly result in exiting
855 * idle mode, in other words, entering the mode in which read-side critical
7c9906ca 856 * sections can occur. The caller must have disabled interrupts.
c0da313e 857 *
9b2e4f18 858 * Note that the Linux kernel is fully capable of entering an interrupt
58721f5d
PM
859 * handler that it never exits, for example when doing upcalls to user mode!
860 * This code assumes that the idle loop never does upcalls to user mode.
861 * If your architecture's idle loop does do upcalls to user mode (or does
862 * anything else that results in unbalanced calls to the irq_enter() and
863 * irq_exit() functions), RCU will give you what you deserve, good and hard.
864 * But very infrequently and irreproducibly.
9b2e4f18
PM
865 *
866 * Use things like work queues to work around this limitation.
867 *
868 * You have been warned.
c0da313e
PM
869 *
870 * If you add or remove a call to rcu_irq_enter(), be sure to test with
871 * CONFIG_RCU_EQS_DEBUG=y.
64db4cff 872 */
9b2e4f18 873void rcu_irq_enter(void)
64db4cff 874{
b04db8e1 875 lockdep_assert_irqs_disabled();
cf7614e1 876 rcu_nmi_enter_common(true);
7c9906ca 877}
734d1680 878
7c9906ca
PM
879/*
880 * Wrapper for rcu_irq_enter() where interrupts are enabled.
c0da313e
PM
881 *
882 * If you add or remove a call to rcu_irq_enter_irqson(), be sure to test
883 * with CONFIG_RCU_EQS_DEBUG=y.
7c9906ca
PM
884 */
885void rcu_irq_enter_irqson(void)
886{
887 unsigned long flags;
734d1680 888
7c9906ca
PM
889 local_irq_save(flags);
890 rcu_irq_enter();
64db4cff 891 local_irq_restore(flags);
64db4cff
PM
892}
893
66e4c33b 894/*
516e5ae0
JFG
895 * If any sort of urgency was applied to the current CPU (for example,
896 * the scheduler-clock interrupt was enabled on a nohz_full CPU) in order
897 * to get to a quiescent state, disable it.
66e4c33b 898 */
516e5ae0 899static void rcu_disable_urgency_upon_qs(struct rcu_data *rdp)
66e4c33b 900{
516e5ae0
JFG
901 WRITE_ONCE(rdp->rcu_urgent_qs, false);
902 WRITE_ONCE(rdp->rcu_need_heavy_qs, false);
66e4c33b
PM
903 if (tick_nohz_full_cpu(rdp->cpu) && rdp->rcu_forced_tick) {
904 tick_dep_clear_cpu(rdp->cpu, TICK_DEP_BIT_RCU);
905 rdp->rcu_forced_tick = false;
906 }
907}
908
5c173eb8 909/**
2320bda2 910 * rcu_is_watching - see if RCU thinks that the current CPU is not idle
64db4cff 911 *
791875d1
PM
912 * Return true if RCU is watching the running CPU, which means that this
913 * CPU can safely enter RCU read-side critical sections. In other words,
2320bda2
ZZ
914 * if the current CPU is not in its idle loop or is in an interrupt or
915 * NMI handler, return true.
64db4cff 916 */
9418fb20 917bool notrace rcu_is_watching(void)
64db4cff 918{
f534ed1f 919 bool ret;
34240697 920
46f00d18 921 preempt_disable_notrace();
791875d1 922 ret = !rcu_dynticks_curr_cpu_in_eqs();
46f00d18 923 preempt_enable_notrace();
34240697 924 return ret;
64db4cff 925}
5c173eb8 926EXPORT_SYMBOL_GPL(rcu_is_watching);
64db4cff 927
bcbfdd01
PM
928/*
929 * If a holdout task is actually running, request an urgent quiescent
930 * state from its CPU. This is unsynchronized, so migrations can cause
931 * the request to go to the wrong CPU. Which is OK, all that will happen
932 * is that the CPU's next context switch will be a bit slower and next
933 * time around this task will generate another request.
934 */
935void rcu_request_urgent_qs_task(struct task_struct *t)
936{
937 int cpu;
938
939 barrier();
940 cpu = task_cpu(t);
941 if (!task_curr(t))
942 return; /* This task is not running on that CPU. */
2dba13f0 943 smp_store_release(per_cpu_ptr(&rcu_data.rcu_urgent_qs, cpu), true);
bcbfdd01
PM
944}
945
62fde6ed 946#if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
c0d6d01b
PM
947
948/*
5554788e 949 * Is the current CPU online as far as RCU is concerned?
2036d94a 950 *
5554788e
PM
951 * Disable preemption to avoid false positives that could otherwise
952 * happen due to the current CPU number being sampled, this task being
953 * preempted, its old CPU being taken offline, resuming on some other CPU,
49918a54 954 * then determining that its old CPU is now offline.
c0d6d01b 955 *
5554788e
PM
956 * Disable checking if in an NMI handler because we cannot safely
957 * report errors from NMI handlers anyway. In addition, it is OK to use
958 * RCU on an offline processor during initial boot, hence the check for
959 * rcu_scheduler_fully_active.
c0d6d01b
PM
960 */
961bool rcu_lockdep_current_cpu_online(void)
962{
2036d94a
PM
963 struct rcu_data *rdp;
964 struct rcu_node *rnp;
b97d23c5 965 bool ret = false;
c0d6d01b 966
5554788e 967 if (in_nmi() || !rcu_scheduler_fully_active)
f6f7ee9a 968 return true;
c0d6d01b 969 preempt_disable();
b97d23c5
PM
970 rdp = this_cpu_ptr(&rcu_data);
971 rnp = rdp->mynode;
972 if (rdp->grpmask & rcu_rnp_online_cpus(rnp))
973 ret = true;
c0d6d01b 974 preempt_enable();
b97d23c5 975 return ret;
c0d6d01b
PM
976}
977EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
978
62fde6ed 979#endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
9b2e4f18 980
9b9500da
PM
981/*
982 * We are reporting a quiescent state on behalf of some other CPU, so
983 * it is our responsibility to check for and handle potential overflow
a66ae8ae 984 * of the rcu_node ->gp_seq counter with respect to the rcu_data counters.
9b9500da
PM
985 * After all, the CPU might be in deep idle state, and thus executing no
986 * code whatsoever.
987 */
988static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
989{
a32e01ee 990 raw_lockdep_assert_held_rcu_node(rnp);
a66ae8ae
PM
991 if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + ULONG_MAX / 4,
992 rnp->gp_seq))
9b9500da 993 WRITE_ONCE(rdp->gpwrap, true);
8aa670cd
PM
994 if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq))
995 rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4;
9b9500da
PM
996}
997
64db4cff
PM
998/*
999 * Snapshot the specified CPU's dynticks counter so that we can later
1000 * credit them with an implicit quiescent state. Return 1 if this CPU
1eba8f84 1001 * is in dynticks idle mode, which is an extended quiescent state.
64db4cff 1002 */
fe5ac724 1003static int dyntick_save_progress_counter(struct rcu_data *rdp)
64db4cff 1004{
dc5a4f29 1005 rdp->dynticks_snap = rcu_dynticks_snap(rdp);
02a5c550 1006 if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
88d1bead 1007 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
9b9500da 1008 rcu_gpnum_ovf(rdp->mynode, rdp);
23a9bacd 1009 return 1;
7941dbde 1010 }
23a9bacd 1011 return 0;
64db4cff
PM
1012}
1013
1014/*
1015 * Return true if the specified CPU has passed through a quiescent
1016 * state by virtue of being in or having passed through an dynticks
1017 * idle state since the last call to dyntick_save_progress_counter()
a82dcc76 1018 * for this same CPU, or by virtue of having been offline.
64db4cff 1019 */
fe5ac724 1020static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
64db4cff 1021{
3a19b46a 1022 unsigned long jtsq;
0f9be8ca 1023 bool *rnhqp;
9226b10d 1024 bool *ruqp;
9b9500da 1025 struct rcu_node *rnp = rdp->mynode;
64db4cff
PM
1026
1027 /*
1028 * If the CPU passed through or entered a dynticks idle phase with
1029 * no active irq/NMI handlers, then we can safely pretend that the CPU
1030 * already acknowledged the request to pass through a quiescent
1031 * state. Either way, that CPU cannot possibly be in an RCU
1032 * read-side critical section that started before the beginning
1033 * of the current RCU grace period.
1034 */
dc5a4f29 1035 if (rcu_dynticks_in_eqs_since(rdp, rdp->dynticks_snap)) {
88d1bead 1036 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
9b9500da 1037 rcu_gpnum_ovf(rnp, rdp);
3a19b46a
PM
1038 return 1;
1039 }
1040
f2e2df59
PM
1041 /* If waiting too long on an offline CPU, complain. */
1042 if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp)) &&
88d1bead 1043 time_after(jiffies, rcu_state.gp_start + HZ)) {
f2e2df59
PM
1044 bool onl;
1045 struct rcu_node *rnp1;
1046
1047 WARN_ON(1); /* Offline CPUs are supposed to report QS! */
1048 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
1049 __func__, rnp->grplo, rnp->grphi, rnp->level,
1050 (long)rnp->gp_seq, (long)rnp->completedqs);
1051 for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
1052 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n",
1053 __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask);
1054 onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
1055 pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n",
1056 __func__, rdp->cpu, ".o"[onl],
1057 (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
1058 (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
1059 return 1; /* Break things loose after complaining. */
1060 }
1061
65d798f0 1062 /*
4a81e832 1063 * A CPU running for an extended time within the kernel can
c06aed0e
PM
1064 * delay RCU grace periods: (1) At age jiffies_to_sched_qs,
1065 * set .rcu_urgent_qs, (2) At age 2*jiffies_to_sched_qs, set
7e28c5af
PM
1066 * both .rcu_need_heavy_qs and .rcu_urgent_qs. Note that the
1067 * unsynchronized assignments to the per-CPU rcu_need_heavy_qs
1068 * variable are safe because the assignments are repeated if this
1069 * CPU failed to pass through a quiescent state. This code
c06aed0e 1070 * also checks .jiffies_resched in case jiffies_to_sched_qs
7e28c5af 1071 * is set way high.
6193c76a 1072 */
c06aed0e 1073 jtsq = READ_ONCE(jiffies_to_sched_qs);
2dba13f0
PM
1074 ruqp = per_cpu_ptr(&rcu_data.rcu_urgent_qs, rdp->cpu);
1075 rnhqp = &per_cpu(rcu_data.rcu_need_heavy_qs, rdp->cpu);
0f9be8ca 1076 if (!READ_ONCE(*rnhqp) &&
7e28c5af 1077 (time_after(jiffies, rcu_state.gp_start + jtsq * 2) ||
88d1bead 1078 time_after(jiffies, rcu_state.jiffies_resched))) {
0f9be8ca 1079 WRITE_ONCE(*rnhqp, true);
9226b10d
PM
1080 /* Store rcu_need_heavy_qs before rcu_urgent_qs. */
1081 smp_store_release(ruqp, true);
7e28c5af
PM
1082 } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) {
1083 WRITE_ONCE(*ruqp, true);
6193c76a
PM
1084 }
1085
28053bc7 1086 /*
c98cac60 1087 * NO_HZ_FULL CPUs can run in-kernel without rcu_sched_clock_irq!
d3052109
PM
1088 * The above code handles this, but only for straight cond_resched().
1089 * And some in-kernel loops check need_resched() before calling
1090 * cond_resched(), which defeats the above code for CPUs that are
1091 * running in-kernel with scheduling-clock interrupts disabled.
1092 * So hit them over the head with the resched_cpu() hammer!
28053bc7 1093 */
d3052109
PM
1094 if (tick_nohz_full_cpu(rdp->cpu) &&
1095 time_after(jiffies,
1096 READ_ONCE(rdp->last_fqs_resched) + jtsq * 3)) {
05ef9e9e 1097 WRITE_ONCE(*ruqp, true);
28053bc7 1098 resched_cpu(rdp->cpu);
d3052109
PM
1099 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1100 }
1101
1102 /*
1103 * If more than halfway to RCU CPU stall-warning time, invoke
1104 * resched_cpu() more frequently to try to loosen things up a bit.
1105 * Also check to see if the CPU is getting hammered with interrupts,
1106 * but only once per grace period, just to keep the IPIs down to
1107 * a dull roar.
1108 */
1109 if (time_after(jiffies, rcu_state.jiffies_resched)) {
1110 if (time_after(jiffies,
1111 READ_ONCE(rdp->last_fqs_resched) + jtsq)) {
1112 resched_cpu(rdp->cpu);
1113 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1114 }
9b9500da 1115 if (IS_ENABLED(CONFIG_IRQ_WORK) &&
8aa670cd 1116 !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq &&
9b9500da
PM
1117 (rnp->ffmask & rdp->grpmask)) {
1118 init_irq_work(&rdp->rcu_iw, rcu_iw_handler);
1119 rdp->rcu_iw_pending = true;
8aa670cd 1120 rdp->rcu_iw_gp_seq = rnp->gp_seq;
9b9500da
PM
1121 irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
1122 }
1123 }
4914950a 1124
a82dcc76 1125 return 0;
64db4cff
PM
1126}
1127
41e80595
PM
1128/* Trace-event wrapper function for trace_rcu_future_grace_period. */
1129static void trace_rcu_this_gp(struct rcu_node *rnp, struct rcu_data *rdp,
b73de91d 1130 unsigned long gp_seq_req, const char *s)
0446be48 1131{
88d1bead 1132 trace_rcu_future_grace_period(rcu_state.name, rnp->gp_seq, gp_seq_req,
abd13fdd 1133 rnp->level, rnp->grplo, rnp->grphi, s);
0446be48
PM
1134}
1135
1136/*
b73de91d 1137 * rcu_start_this_gp - Request the start of a particular grace period
df2bf8f7 1138 * @rnp_start: The leaf node of the CPU from which to start.
b73de91d
JF
1139 * @rdp: The rcu_data corresponding to the CPU from which to start.
1140 * @gp_seq_req: The gp_seq of the grace period to start.
1141 *
41e80595 1142 * Start the specified grace period, as needed to handle newly arrived
0446be48 1143 * callbacks. The required future grace periods are recorded in each
7a1d0f23 1144 * rcu_node structure's ->gp_seq_needed field. Returns true if there
48a7639c 1145 * is reason to awaken the grace-period kthread.
0446be48 1146 *
d5cd9685
PM
1147 * The caller must hold the specified rcu_node structure's ->lock, which
1148 * is why the caller is responsible for waking the grace-period kthread.
b73de91d
JF
1149 *
1150 * Returns true if the GP thread needs to be awakened else false.
0446be48 1151 */
df2bf8f7 1152static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp,
b73de91d 1153 unsigned long gp_seq_req)
0446be48 1154{
48a7639c 1155 bool ret = false;
df2bf8f7 1156 struct rcu_node *rnp;
0446be48
PM
1157
1158 /*
360e0da6
PM
1159 * Use funnel locking to either acquire the root rcu_node
1160 * structure's lock or bail out if the need for this grace period
df2bf8f7
JFG
1161 * has already been recorded -- or if that grace period has in
1162 * fact already started. If there is already a grace period in
1163 * progress in a non-leaf node, no recording is needed because the
1164 * end of the grace period will scan the leaf rcu_node structures.
1165 * Note that rnp_start->lock must not be released.
0446be48 1166 */
df2bf8f7
JFG
1167 raw_lockdep_assert_held_rcu_node(rnp_start);
1168 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, TPS("Startleaf"));
1169 for (rnp = rnp_start; 1; rnp = rnp->parent) {
1170 if (rnp != rnp_start)
1171 raw_spin_lock_rcu_node(rnp);
1172 if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) ||
1173 rcu_seq_started(&rnp->gp_seq, gp_seq_req) ||
1174 (rnp != rnp_start &&
1175 rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) {
1176 trace_rcu_this_gp(rnp, rdp, gp_seq_req,
b73de91d 1177 TPS("Prestarted"));
360e0da6
PM
1178 goto unlock_out;
1179 }
df2bf8f7 1180 rnp->gp_seq_needed = gp_seq_req;
226ca5e7 1181 if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) {
a2165e41 1182 /*
226ca5e7
JFG
1183 * We just marked the leaf or internal node, and a
1184 * grace period is in progress, which means that
1185 * rcu_gp_cleanup() will see the marking. Bail to
1186 * reduce contention.
a2165e41 1187 */
df2bf8f7 1188 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req,
b73de91d 1189 TPS("Startedleaf"));
a2165e41
PM
1190 goto unlock_out;
1191 }
df2bf8f7
JFG
1192 if (rnp != rnp_start && rnp->parent != NULL)
1193 raw_spin_unlock_rcu_node(rnp);
1194 if (!rnp->parent)
360e0da6 1195 break; /* At root, and perhaps also leaf. */
0446be48
PM
1196 }
1197
360e0da6 1198 /* If GP already in progress, just leave, otherwise start one. */
de8e8730 1199 if (rcu_gp_in_progress()) {
df2bf8f7 1200 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedleafroot"));
0446be48
PM
1201 goto unlock_out;
1202 }
df2bf8f7 1203 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedroot"));
9cbc5b97
PM
1204 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT);
1205 rcu_state.gp_req_activity = jiffies;
1206 if (!rcu_state.gp_kthread) {
df2bf8f7 1207 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("NoGPkthread"));
360e0da6 1208 goto unlock_out;
0446be48 1209 }
9cbc5b97 1210 trace_rcu_grace_period(rcu_state.name, READ_ONCE(rcu_state.gp_seq), TPS("newreq"));
360e0da6 1211 ret = true; /* Caller must wake GP kthread. */
0446be48 1212unlock_out:
ab5e869c 1213 /* Push furthest requested GP to leaf node and rcu_data structure. */
df2bf8f7
JFG
1214 if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) {
1215 rnp_start->gp_seq_needed = rnp->gp_seq_needed;
1216 rdp->gp_seq_needed = rnp->gp_seq_needed;
ab5e869c 1217 }
df2bf8f7
JFG
1218 if (rnp != rnp_start)
1219 raw_spin_unlock_rcu_node(rnp);
48a7639c 1220 return ret;
0446be48
PM
1221}
1222
1223/*
1224 * Clean up any old requests for the just-ended grace period. Also return
d1e4f01d 1225 * whether any additional grace periods have been requested.
0446be48 1226 */
3481f2ea 1227static bool rcu_future_gp_cleanup(struct rcu_node *rnp)
0446be48 1228{
fb31340f 1229 bool needmore;
da1df50d 1230 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
0446be48 1231
7a1d0f23
PM
1232 needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed);
1233 if (!needmore)
1234 rnp->gp_seq_needed = rnp->gp_seq; /* Avoid counter wrap. */
b73de91d 1235 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq,
41e80595 1236 needmore ? TPS("CleanupMore") : TPS("Cleanup"));
0446be48
PM
1237 return needmore;
1238}
1239
48a7639c 1240/*
1d1f898d
ZJ
1241 * Awaken the grace-period kthread. Don't do a self-awaken (unless in
1242 * an interrupt or softirq handler), and don't bother awakening when there
1243 * is nothing for the grace-period kthread to do (as in several CPUs raced
1244 * to awaken, and we lost), and finally don't try to awaken a kthread that
1245 * has not yet been created. If all those checks are passed, track some
1246 * debug information and awaken.
1247 *
1248 * So why do the self-wakeup when in an interrupt or softirq handler
1249 * in the grace-period kthread's context? Because the kthread might have
1250 * been interrupted just as it was going to sleep, and just after the final
1251 * pre-sleep check of the awaken condition. In this case, a wakeup really
1252 * is required, and is therefore supplied.
48a7639c 1253 */
532c00c9 1254static void rcu_gp_kthread_wake(void)
48a7639c 1255{
1d1f898d 1256 if ((current == rcu_state.gp_kthread &&
0f58d2ac 1257 !in_irq() && !in_serving_softirq()) ||
532c00c9
PM
1258 !READ_ONCE(rcu_state.gp_flags) ||
1259 !rcu_state.gp_kthread)
48a7639c 1260 return;
fd897573
PM
1261 WRITE_ONCE(rcu_state.gp_wake_time, jiffies);
1262 WRITE_ONCE(rcu_state.gp_wake_seq, READ_ONCE(rcu_state.gp_seq));
532c00c9 1263 swake_up_one(&rcu_state.gp_wq);
48a7639c
PM
1264}
1265
dc35c893 1266/*
29365e56
PM
1267 * If there is room, assign a ->gp_seq number to any callbacks on this
1268 * CPU that have not already been assigned. Also accelerate any callbacks
1269 * that were previously assigned a ->gp_seq number that has since proven
1270 * to be too conservative, which can happen if callbacks get assigned a
1271 * ->gp_seq number while RCU is idle, but with reference to a non-root
1272 * rcu_node structure. This function is idempotent, so it does not hurt
1273 * to call it repeatedly. Returns an flag saying that we should awaken
1274 * the RCU grace-period kthread.
dc35c893
PM
1275 *
1276 * The caller must hold rnp->lock with interrupts disabled.
1277 */
02f50142 1278static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
dc35c893 1279{
b73de91d 1280 unsigned long gp_seq_req;
15fecf89 1281 bool ret = false;
dc35c893 1282
d1b222c6 1283 rcu_lockdep_assert_cblist_protected(rdp);
a32e01ee 1284 raw_lockdep_assert_held_rcu_node(rnp);
c0b334c5 1285
15fecf89
PM
1286 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1287 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
48a7639c 1288 return false;
dc35c893
PM
1289
1290 /*
15fecf89
PM
1291 * Callbacks are often registered with incomplete grace-period
1292 * information. Something about the fact that getting exact
1293 * information requires acquiring a global lock... RCU therefore
1294 * makes a conservative estimate of the grace period number at which
1295 * a given callback will become ready to invoke. The following
1296 * code checks this estimate and improves it when possible, thus
1297 * accelerating callback invocation to an earlier grace-period
1298 * number.
dc35c893 1299 */
9cbc5b97 1300 gp_seq_req = rcu_seq_snap(&rcu_state.gp_seq);
b73de91d
JF
1301 if (rcu_segcblist_accelerate(&rdp->cblist, gp_seq_req))
1302 ret = rcu_start_this_gp(rnp, rdp, gp_seq_req);
6d4b418c
PM
1303
1304 /* Trace depending on how much we were able to accelerate. */
15fecf89 1305 if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
9cbc5b97 1306 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccWaitCB"));
6d4b418c 1307 else
9cbc5b97 1308 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccReadyCB"));
48a7639c 1309 return ret;
dc35c893
PM
1310}
1311
e44e73ca
PM
1312/*
1313 * Similar to rcu_accelerate_cbs(), but does not require that the leaf
1314 * rcu_node structure's ->lock be held. It consults the cached value
1315 * of ->gp_seq_needed in the rcu_data structure, and if that indicates
1316 * that a new grace-period request be made, invokes rcu_accelerate_cbs()
1317 * while holding the leaf rcu_node structure's ->lock.
1318 */
c6e09b97 1319static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp,
e44e73ca
PM
1320 struct rcu_data *rdp)
1321{
1322 unsigned long c;
1323 bool needwake;
1324
d1b222c6 1325 rcu_lockdep_assert_cblist_protected(rdp);
c6e09b97 1326 c = rcu_seq_snap(&rcu_state.gp_seq);
e44e73ca
PM
1327 if (!rdp->gpwrap && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
1328 /* Old request still live, so mark recent callbacks. */
1329 (void)rcu_segcblist_accelerate(&rdp->cblist, c);
1330 return;
1331 }
1332 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
02f50142 1333 needwake = rcu_accelerate_cbs(rnp, rdp);
e44e73ca
PM
1334 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
1335 if (needwake)
532c00c9 1336 rcu_gp_kthread_wake();
e44e73ca
PM
1337}
1338
dc35c893
PM
1339/*
1340 * Move any callbacks whose grace period has completed to the
1341 * RCU_DONE_TAIL sublist, then compact the remaining sublists and
29365e56 1342 * assign ->gp_seq numbers to any callbacks in the RCU_NEXT_TAIL
dc35c893
PM
1343 * sublist. This function is idempotent, so it does not hurt to
1344 * invoke it repeatedly. As long as it is not invoked -too- often...
48a7639c 1345 * Returns true if the RCU grace-period kthread needs to be awakened.
dc35c893
PM
1346 *
1347 * The caller must hold rnp->lock with interrupts disabled.
1348 */
834f56bf 1349static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
dc35c893 1350{
d1b222c6 1351 rcu_lockdep_assert_cblist_protected(rdp);
a32e01ee 1352 raw_lockdep_assert_held_rcu_node(rnp);
c0b334c5 1353
15fecf89
PM
1354 /* If no pending (not yet ready to invoke) callbacks, nothing to do. */
1355 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
48a7639c 1356 return false;
dc35c893
PM
1357
1358 /*
29365e56 1359 * Find all callbacks whose ->gp_seq numbers indicate that they
dc35c893
PM
1360 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
1361 */
29365e56 1362 rcu_segcblist_advance(&rdp->cblist, rnp->gp_seq);
dc35c893
PM
1363
1364 /* Classify any remaining callbacks. */
02f50142 1365 return rcu_accelerate_cbs(rnp, rdp);
dc35c893
PM
1366}
1367
7f36ef82
PM
1368/*
1369 * Move and classify callbacks, but only if doing so won't require
1370 * that the RCU grace-period kthread be awakened.
1371 */
1372static void __maybe_unused rcu_advance_cbs_nowake(struct rcu_node *rnp,
1373 struct rcu_data *rdp)
1374{
d1b222c6 1375 rcu_lockdep_assert_cblist_protected(rdp);
6608c3a0
PM
1376 if (!rcu_seq_state(rcu_seq_current(&rnp->gp_seq)) ||
1377 !raw_spin_trylock_rcu_node(rnp))
7f36ef82
PM
1378 return;
1379 WARN_ON_ONCE(rcu_advance_cbs(rnp, rdp));
6608c3a0 1380 raw_spin_unlock_rcu_node(rnp);
7f36ef82
PM
1381}
1382
d09b62df 1383/*
ba9fbe95
PM
1384 * Update CPU-local rcu_data state to record the beginnings and ends of
1385 * grace periods. The caller must hold the ->lock of the leaf rcu_node
1386 * structure corresponding to the current CPU, and must have irqs disabled.
48a7639c 1387 * Returns true if the grace-period kthread needs to be awakened.
d09b62df 1388 */
c7e48f7b 1389static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp)
d09b62df 1390{
5d6742b3 1391 bool ret = false;
3563a438 1392 bool need_gp;
5d6742b3
PM
1393 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
1394 rcu_segcblist_is_offloaded(&rdp->cblist);
48a7639c 1395
a32e01ee 1396 raw_lockdep_assert_held_rcu_node(rnp);
c0b334c5 1397
67e14c1e
PM
1398 if (rdp->gp_seq == rnp->gp_seq)
1399 return false; /* Nothing to do. */
d09b62df 1400
67e14c1e
PM
1401 /* Handle the ends of any preceding grace periods first. */
1402 if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) ||
1403 unlikely(READ_ONCE(rdp->gpwrap))) {
5d6742b3
PM
1404 if (!offloaded)
1405 ret = rcu_advance_cbs(rnp, rdp); /* Advance CBs. */
9cbc5b97 1406 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend"));
67e14c1e 1407 } else {
5d6742b3
PM
1408 if (!offloaded)
1409 ret = rcu_accelerate_cbs(rnp, rdp); /* Recent CBs. */
d09b62df 1410 }
398ebe60 1411
67e14c1e
PM
1412 /* Now handle the beginnings of any new-to-this-CPU grace periods. */
1413 if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) ||
1414 unlikely(READ_ONCE(rdp->gpwrap))) {
6eaef633
PM
1415 /*
1416 * If the current grace period is waiting for this CPU,
1417 * set up to detect a quiescent state, otherwise don't
1418 * go looking for one.
1419 */
9cbc5b97 1420 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart"));
3563a438
PM
1421 need_gp = !!(rnp->qsmask & rdp->grpmask);
1422 rdp->cpu_no_qs.b.norm = need_gp;
3563a438 1423 rdp->core_needs_qs = need_gp;
6eaef633
PM
1424 zero_cpu_stall_ticks(rdp);
1425 }
67e14c1e 1426 rdp->gp_seq = rnp->gp_seq; /* Remember new grace-period state. */
13dc7d0c 1427 if (ULONG_CMP_LT(rdp->gp_seq_needed, rnp->gp_seq_needed) || rdp->gpwrap)
3d18469a
PM
1428 rdp->gp_seq_needed = rnp->gp_seq_needed;
1429 WRITE_ONCE(rdp->gpwrap, false);
1430 rcu_gpnum_ovf(rnp, rdp);
48a7639c 1431 return ret;
6eaef633
PM
1432}
1433
15cabdff 1434static void note_gp_changes(struct rcu_data *rdp)
6eaef633
PM
1435{
1436 unsigned long flags;
48a7639c 1437 bool needwake;
6eaef633
PM
1438 struct rcu_node *rnp;
1439
1440 local_irq_save(flags);
1441 rnp = rdp->mynode;
67e14c1e 1442 if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) &&
7d0ae808 1443 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
2a67e741 1444 !raw_spin_trylock_rcu_node(rnp)) { /* irqs already off, so later. */
6eaef633
PM
1445 local_irq_restore(flags);
1446 return;
1447 }
c7e48f7b 1448 needwake = __note_gp_changes(rnp, rdp);
67c583a7 1449 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
48a7639c 1450 if (needwake)
532c00c9 1451 rcu_gp_kthread_wake();
6eaef633
PM
1452}
1453
22212332 1454static void rcu_gp_slow(int delay)
0f41c0dd
PM
1455{
1456 if (delay > 0 &&
22212332 1457 !(rcu_seq_ctr(rcu_state.gp_seq) %
dee4f422 1458 (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
0f41c0dd
PM
1459 schedule_timeout_uninterruptible(delay);
1460}
1461
b3dbec76 1462/*
45fed3e7 1463 * Initialize a new grace period. Return false if no grace period required.
b3dbec76 1464 */
0854a05c 1465static bool rcu_gp_init(void)
b3dbec76 1466{
ec2c2976 1467 unsigned long flags;
0aa04b05 1468 unsigned long oldmask;
ec2c2976 1469 unsigned long mask;
b3dbec76 1470 struct rcu_data *rdp;
336a4f6c 1471 struct rcu_node *rnp = rcu_get_root();
b3dbec76 1472
9cbc5b97 1473 WRITE_ONCE(rcu_state.gp_activity, jiffies);
2a67e741 1474 raw_spin_lock_irq_rcu_node(rnp);
9cbc5b97 1475 if (!READ_ONCE(rcu_state.gp_flags)) {
f7be8209 1476 /* Spurious wakeup, tell caller to go back to sleep. */
67c583a7 1477 raw_spin_unlock_irq_rcu_node(rnp);
45fed3e7 1478 return false;
f7be8209 1479 }
9cbc5b97 1480 WRITE_ONCE(rcu_state.gp_flags, 0); /* Clear all flags: New GP. */
b3dbec76 1481
de8e8730 1482 if (WARN_ON_ONCE(rcu_gp_in_progress())) {
f7be8209
PM
1483 /*
1484 * Grace period already in progress, don't start another.
1485 * Not supposed to be able to happen.
1486 */
67c583a7 1487 raw_spin_unlock_irq_rcu_node(rnp);
45fed3e7 1488 return false;
7fdefc10
PM
1489 }
1490
7fdefc10 1491 /* Advance to a new grace period and initialize state. */
ad3832e9 1492 record_gp_stall_check_time();
ff3bb6f4 1493 /* Record GP times before starting GP, hence rcu_seq_start(). */
9cbc5b97
PM
1494 rcu_seq_start(&rcu_state.gp_seq);
1495 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
67c583a7 1496 raw_spin_unlock_irq_rcu_node(rnp);
7fdefc10 1497
0aa04b05
PM
1498 /*
1499 * Apply per-leaf buffered online and offline operations to the
1500 * rcu_node tree. Note that this new grace period need not wait
1501 * for subsequent online CPUs, and that quiescent-state forcing
1502 * will handle subsequent offline CPUs.
1503 */
9cbc5b97 1504 rcu_state.gp_state = RCU_GP_ONOFF;
aedf4ba9 1505 rcu_for_each_leaf_node(rnp) {
894d45bb 1506 raw_spin_lock(&rcu_state.ofl_lock);
2a67e741 1507 raw_spin_lock_irq_rcu_node(rnp);
0aa04b05
PM
1508 if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
1509 !rnp->wait_blkd_tasks) {
1510 /* Nothing to do on this leaf rcu_node structure. */
67c583a7 1511 raw_spin_unlock_irq_rcu_node(rnp);
894d45bb 1512 raw_spin_unlock(&rcu_state.ofl_lock);
0aa04b05
PM
1513 continue;
1514 }
1515
1516 /* Record old state, apply changes to ->qsmaskinit field. */
1517 oldmask = rnp->qsmaskinit;
1518 rnp->qsmaskinit = rnp->qsmaskinitnext;
1519
1520 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
1521 if (!oldmask != !rnp->qsmaskinit) {
962aff03
PM
1522 if (!oldmask) { /* First online CPU for rcu_node. */
1523 if (!rnp->wait_blkd_tasks) /* Ever offline? */
1524 rcu_init_new_rnp(rnp);
1525 } else if (rcu_preempt_has_tasks(rnp)) {
1526 rnp->wait_blkd_tasks = true; /* blocked tasks */
1527 } else { /* Last offline CPU and can propagate. */
0aa04b05 1528 rcu_cleanup_dead_rnp(rnp);
962aff03 1529 }
0aa04b05
PM
1530 }
1531
1532 /*
1533 * If all waited-on tasks from prior grace period are
1534 * done, and if all this rcu_node structure's CPUs are
1535 * still offline, propagate up the rcu_node tree and
1536 * clear ->wait_blkd_tasks. Otherwise, if one of this
1537 * rcu_node structure's CPUs has since come back online,
962aff03 1538 * simply clear ->wait_blkd_tasks.
0aa04b05
PM
1539 */
1540 if (rnp->wait_blkd_tasks &&
962aff03 1541 (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) {
0aa04b05 1542 rnp->wait_blkd_tasks = false;
962aff03
PM
1543 if (!rnp->qsmaskinit)
1544 rcu_cleanup_dead_rnp(rnp);
0aa04b05
PM
1545 }
1546
67c583a7 1547 raw_spin_unlock_irq_rcu_node(rnp);
894d45bb 1548 raw_spin_unlock(&rcu_state.ofl_lock);
0aa04b05 1549 }
22212332 1550 rcu_gp_slow(gp_preinit_delay); /* Races with CPU hotplug. */
7fdefc10
PM
1551
1552 /*
1553 * Set the quiescent-state-needed bits in all the rcu_node
9cbc5b97
PM
1554 * structures for all currently online CPUs in breadth-first
1555 * order, starting from the root rcu_node structure, relying on the
1556 * layout of the tree within the rcu_state.node[] array. Note that
1557 * other CPUs will access only the leaves of the hierarchy, thus
1558 * seeing that no grace period is in progress, at least until the
1559 * corresponding leaf node has been initialized.
7fdefc10
PM
1560 *
1561 * The grace period cannot complete until the initialization
1562 * process finishes, because this kthread handles both.
1563 */
9cbc5b97 1564 rcu_state.gp_state = RCU_GP_INIT;
aedf4ba9 1565 rcu_for_each_node_breadth_first(rnp) {
22212332 1566 rcu_gp_slow(gp_init_delay);
ec2c2976 1567 raw_spin_lock_irqsave_rcu_node(rnp, flags);
da1df50d 1568 rdp = this_cpu_ptr(&rcu_data);
81ab59a3 1569 rcu_preempt_check_blocked_tasks(rnp);
7fdefc10 1570 rnp->qsmask = rnp->qsmaskinit;
9cbc5b97 1571 WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);
7fdefc10 1572 if (rnp == rdp->mynode)
c7e48f7b 1573 (void)__note_gp_changes(rnp, rdp);
7fdefc10 1574 rcu_preempt_boost_start_gp(rnp);
9cbc5b97 1575 trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq,
7fdefc10
PM
1576 rnp->level, rnp->grplo,
1577 rnp->grphi, rnp->qsmask);
ec2c2976
PM
1578 /* Quiescent states for tasks on any now-offline CPUs. */
1579 mask = rnp->qsmask & ~rnp->qsmaskinitnext;
f2e2df59 1580 rnp->rcu_gp_init_mask = mask;
ec2c2976 1581 if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp))
b50912d0 1582 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
ec2c2976
PM
1583 else
1584 raw_spin_unlock_irq_rcu_node(rnp);
cee43939 1585 cond_resched_tasks_rcu_qs();
9cbc5b97 1586 WRITE_ONCE(rcu_state.gp_activity, jiffies);
7fdefc10 1587 }
b3dbec76 1588
45fed3e7 1589 return true;
7fdefc10 1590}
b3dbec76 1591
b9a425cf 1592/*
b3dae109 1593 * Helper function for swait_event_idle_exclusive() wakeup at force-quiescent-state
d5374226 1594 * time.
b9a425cf 1595 */
0854a05c 1596static bool rcu_gp_fqs_check_wake(int *gfp)
b9a425cf 1597{
336a4f6c 1598 struct rcu_node *rnp = rcu_get_root();
b9a425cf
PM
1599
1600 /* Someone like call_rcu() requested a force-quiescent-state scan. */
0854a05c 1601 *gfp = READ_ONCE(rcu_state.gp_flags);
b9a425cf
PM
1602 if (*gfp & RCU_GP_FLAG_FQS)
1603 return true;
1604
1605 /* The current grace period has completed. */
1606 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
1607 return true;
1608
1609 return false;
1610}
1611
4cdfc175
PM
1612/*
1613 * Do one round of quiescent-state forcing.
1614 */
0854a05c 1615static void rcu_gp_fqs(bool first_time)
4cdfc175 1616{
336a4f6c 1617 struct rcu_node *rnp = rcu_get_root();
4cdfc175 1618
9cbc5b97
PM
1619 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1620 rcu_state.n_force_qs++;
77f81fe0 1621 if (first_time) {
4cdfc175 1622 /* Collect dyntick-idle snapshots. */
e9ecb780 1623 force_qs_rnp(dyntick_save_progress_counter);
4cdfc175
PM
1624 } else {
1625 /* Handle dyntick-idle and offline CPUs. */
e9ecb780 1626 force_qs_rnp(rcu_implicit_dynticks_qs);
4cdfc175
PM
1627 }
1628 /* Clear flag to prevent immediate re-entry. */
9cbc5b97 1629 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
2a67e741 1630 raw_spin_lock_irq_rcu_node(rnp);
9cbc5b97
PM
1631 WRITE_ONCE(rcu_state.gp_flags,
1632 READ_ONCE(rcu_state.gp_flags) & ~RCU_GP_FLAG_FQS);
67c583a7 1633 raw_spin_unlock_irq_rcu_node(rnp);
4cdfc175 1634 }
4cdfc175
PM
1635}
1636
c3854a05
PM
1637/*
1638 * Loop doing repeated quiescent-state forcing until the grace period ends.
1639 */
1640static void rcu_gp_fqs_loop(void)
1641{
1642 bool first_gp_fqs;
1643 int gf;
1644 unsigned long j;
1645 int ret;
1646 struct rcu_node *rnp = rcu_get_root();
1647
1648 first_gp_fqs = true;
c06aed0e 1649 j = READ_ONCE(jiffies_till_first_fqs);
c3854a05
PM
1650 ret = 0;
1651 for (;;) {
1652 if (!ret) {
1653 rcu_state.jiffies_force_qs = jiffies + j;
1654 WRITE_ONCE(rcu_state.jiffies_kick_kthreads,
9cf422a8 1655 jiffies + (j ? 3 * j : 2));
c3854a05
PM
1656 }
1657 trace_rcu_grace_period(rcu_state.name,
1658 READ_ONCE(rcu_state.gp_seq),
1659 TPS("fqswait"));
1660 rcu_state.gp_state = RCU_GP_WAIT_FQS;
1661 ret = swait_event_idle_timeout_exclusive(
1662 rcu_state.gp_wq, rcu_gp_fqs_check_wake(&gf), j);
1663 rcu_state.gp_state = RCU_GP_DOING_FQS;
1664 /* Locking provides needed memory barriers. */
1665 /* If grace period done, leave loop. */
1666 if (!READ_ONCE(rnp->qsmask) &&
1667 !rcu_preempt_blocked_readers_cgp(rnp))
1668 break;
1669 /* If time for quiescent-state forcing, do it. */
1670 if (ULONG_CMP_GE(jiffies, rcu_state.jiffies_force_qs) ||
1671 (gf & RCU_GP_FLAG_FQS)) {
1672 trace_rcu_grace_period(rcu_state.name,
1673 READ_ONCE(rcu_state.gp_seq),
1674 TPS("fqsstart"));
1675 rcu_gp_fqs(first_gp_fqs);
1676 first_gp_fqs = false;
1677 trace_rcu_grace_period(rcu_state.name,
1678 READ_ONCE(rcu_state.gp_seq),
1679 TPS("fqsend"));
1680 cond_resched_tasks_rcu_qs();
1681 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1682 ret = 0; /* Force full wait till next FQS. */
c06aed0e 1683 j = READ_ONCE(jiffies_till_next_fqs);
c3854a05
PM
1684 } else {
1685 /* Deal with stray signal. */
1686 cond_resched_tasks_rcu_qs();
1687 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1688 WARN_ON(signal_pending(current));
1689 trace_rcu_grace_period(rcu_state.name,
1690 READ_ONCE(rcu_state.gp_seq),
1691 TPS("fqswaitsig"));
1692 ret = 1; /* Keep old FQS timing. */
1693 j = jiffies;
1694 if (time_after(jiffies, rcu_state.jiffies_force_qs))
1695 j = 1;
1696 else
1697 j = rcu_state.jiffies_force_qs - j;
1698 }
1699 }
1700}
1701
7fdefc10
PM
1702/*
1703 * Clean up after the old grace period.
1704 */
0854a05c 1705static void rcu_gp_cleanup(void)
7fdefc10
PM
1706{
1707 unsigned long gp_duration;
48a7639c 1708 bool needgp = false;
de30ad51 1709 unsigned long new_gp_seq;
5d6742b3 1710 bool offloaded;
7fdefc10 1711 struct rcu_data *rdp;
336a4f6c 1712 struct rcu_node *rnp = rcu_get_root();
abedf8e2 1713 struct swait_queue_head *sq;
b3dbec76 1714
9cbc5b97 1715 WRITE_ONCE(rcu_state.gp_activity, jiffies);
2a67e741 1716 raw_spin_lock_irq_rcu_node(rnp);
c51d7b5e
PM
1717 rcu_state.gp_end = jiffies;
1718 gp_duration = rcu_state.gp_end - rcu_state.gp_start;
9cbc5b97
PM
1719 if (gp_duration > rcu_state.gp_max)
1720 rcu_state.gp_max = gp_duration;
b3dbec76 1721
7fdefc10
PM
1722 /*
1723 * We know the grace period is complete, but to everyone else
1724 * it appears to still be ongoing. But it is also the case
1725 * that to everyone else it looks like there is nothing that
1726 * they can do to advance the grace period. It is therefore
1727 * safe for us to drop the lock in order to mark the grace
1728 * period as completed in all of the rcu_node structures.
7fdefc10 1729 */
67c583a7 1730 raw_spin_unlock_irq_rcu_node(rnp);
b3dbec76 1731
5d4b8659 1732 /*
ff3bb6f4
PM
1733 * Propagate new ->gp_seq value to rcu_node structures so that
1734 * other CPUs don't have to wait until the start of the next grace
1735 * period to process their callbacks. This also avoids some nasty
1736 * RCU grace-period initialization races by forcing the end of
1737 * the current grace period to be completely recorded in all of
1738 * the rcu_node structures before the beginning of the next grace
1739 * period is recorded in any of the rcu_node structures.
5d4b8659 1740 */
9cbc5b97 1741 new_gp_seq = rcu_state.gp_seq;
de30ad51 1742 rcu_seq_end(&new_gp_seq);
aedf4ba9 1743 rcu_for_each_node_breadth_first(rnp) {
2a67e741 1744 raw_spin_lock_irq_rcu_node(rnp);
4bc8d555 1745 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
81ab59a3 1746 dump_blkd_tasks(rnp, 10);
5c60d25f 1747 WARN_ON_ONCE(rnp->qsmask);
de30ad51 1748 WRITE_ONCE(rnp->gp_seq, new_gp_seq);
da1df50d 1749 rdp = this_cpu_ptr(&rcu_data);
b11cc576 1750 if (rnp == rdp->mynode)
c7e48f7b 1751 needgp = __note_gp_changes(rnp, rdp) || needgp;
78e4bc34 1752 /* smp_mb() provided by prior unlock-lock pair. */
3481f2ea 1753 needgp = rcu_future_gp_cleanup(rnp) || needgp;
065bb78c 1754 sq = rcu_nocb_gp_get(rnp);
67c583a7 1755 raw_spin_unlock_irq_rcu_node(rnp);
065bb78c 1756 rcu_nocb_gp_cleanup(sq);
cee43939 1757 cond_resched_tasks_rcu_qs();
9cbc5b97 1758 WRITE_ONCE(rcu_state.gp_activity, jiffies);
22212332 1759 rcu_gp_slow(gp_cleanup_delay);
7fdefc10 1760 }
336a4f6c 1761 rnp = rcu_get_root();
9cbc5b97 1762 raw_spin_lock_irq_rcu_node(rnp); /* GP before ->gp_seq update. */
7fdefc10 1763
0a89e5a4 1764 /* Declare grace period done, trace first to use old GP number. */
9cbc5b97 1765 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end"));
0a89e5a4 1766 rcu_seq_end(&rcu_state.gp_seq);
9cbc5b97 1767 rcu_state.gp_state = RCU_GP_IDLE;
fb31340f 1768 /* Check for GP requests since above loop. */
da1df50d 1769 rdp = this_cpu_ptr(&rcu_data);
5b55072f 1770 if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) {
abd13fdd 1771 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq_needed,
41e80595 1772 TPS("CleanupMore"));
fb31340f
PM
1773 needgp = true;
1774 }
48a7639c 1775 /* Advance CBs to reduce false positives below. */
5d6742b3
PM
1776 offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
1777 rcu_segcblist_is_offloaded(&rdp->cblist);
1778 if ((offloaded || !rcu_accelerate_cbs(rnp, rdp)) && needgp) {
9cbc5b97
PM
1779 WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT);
1780 rcu_state.gp_req_activity = jiffies;
1781 trace_rcu_grace_period(rcu_state.name,
1782 READ_ONCE(rcu_state.gp_seq),
bb311ecc 1783 TPS("newreq"));
18390aea 1784 } else {
9cbc5b97
PM
1785 WRITE_ONCE(rcu_state.gp_flags,
1786 rcu_state.gp_flags & RCU_GP_FLAG_INIT);
bb311ecc 1787 }
67c583a7 1788 raw_spin_unlock_irq_rcu_node(rnp);
7fdefc10
PM
1789}
1790
1791/*
1792 * Body of kthread that handles grace periods.
1793 */
0854a05c 1794static int __noreturn rcu_gp_kthread(void *unused)
7fdefc10 1795{
5871968d 1796 rcu_bind_gp_kthread();
7fdefc10
PM
1797 for (;;) {
1798
1799 /* Handle grace-period start. */
1800 for (;;) {
9cbc5b97
PM
1801 trace_rcu_grace_period(rcu_state.name,
1802 READ_ONCE(rcu_state.gp_seq),
63c4db78 1803 TPS("reqwait"));
9cbc5b97
PM
1804 rcu_state.gp_state = RCU_GP_WAIT_GPS;
1805 swait_event_idle_exclusive(rcu_state.gp_wq,
1806 READ_ONCE(rcu_state.gp_flags) &
1807 RCU_GP_FLAG_INIT);
1808 rcu_state.gp_state = RCU_GP_DONE_GPS;
78e4bc34 1809 /* Locking provides needed memory barrier. */
0854a05c 1810 if (rcu_gp_init())
7fdefc10 1811 break;
cee43939 1812 cond_resched_tasks_rcu_qs();
9cbc5b97 1813 WRITE_ONCE(rcu_state.gp_activity, jiffies);
73a860cd 1814 WARN_ON(signal_pending(current));
9cbc5b97
PM
1815 trace_rcu_grace_period(rcu_state.name,
1816 READ_ONCE(rcu_state.gp_seq),
63c4db78 1817 TPS("reqwaitsig"));
7fdefc10 1818 }
cabc49c1 1819
4cdfc175 1820 /* Handle quiescent-state forcing. */
c3854a05 1821 rcu_gp_fqs_loop();
4cdfc175
PM
1822
1823 /* Handle grace-period end. */
9cbc5b97 1824 rcu_state.gp_state = RCU_GP_CLEANUP;
0854a05c 1825 rcu_gp_cleanup();
9cbc5b97 1826 rcu_state.gp_state = RCU_GP_CLEANED;
b3dbec76 1827 }
b3dbec76
PM
1828}
1829
f41d911f 1830/*
49918a54
PM
1831 * Report a full set of quiescent states to the rcu_state data structure.
1832 * Invoke rcu_gp_kthread_wake() to awaken the grace-period kthread if
1833 * another grace period is required. Whether we wake the grace-period
1834 * kthread or it awakens itself for the next round of quiescent-state
1835 * forcing, that kthread will clean up after the just-completed grace
1836 * period. Note that the caller must hold rnp->lock, which is released
1837 * before return.
f41d911f 1838 */
aff4e9ed 1839static void rcu_report_qs_rsp(unsigned long flags)
336a4f6c 1840 __releases(rcu_get_root()->lock)
f41d911f 1841{
336a4f6c 1842 raw_lockdep_assert_held_rcu_node(rcu_get_root());
de8e8730 1843 WARN_ON_ONCE(!rcu_gp_in_progress());
9cbc5b97
PM
1844 WRITE_ONCE(rcu_state.gp_flags,
1845 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
336a4f6c 1846 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags);
532c00c9 1847 rcu_gp_kthread_wake();
f41d911f
PM
1848}
1849
64db4cff 1850/*
d3f6bad3
PM
1851 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
1852 * Allows quiescent states for a group of CPUs to be reported at one go
1853 * to the specified rcu_node structure, though all the CPUs in the group
654e9533
PM
1854 * must be represented by the same rcu_node structure (which need not be a
1855 * leaf rcu_node structure, though it often will be). The gps parameter
1856 * is the grace-period snapshot, which means that the quiescent states
c9a24e2d 1857 * are valid only if rnp->gp_seq is equal to gps. That structure's lock
654e9533 1858 * must be held upon entry, and it is released before return.
ec2c2976
PM
1859 *
1860 * As a special case, if mask is zero, the bit-already-cleared check is
1861 * disabled. This allows propagating quiescent state due to resumed tasks
1862 * during grace-period initialization.
64db4cff 1863 */
b50912d0
PM
1864static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
1865 unsigned long gps, unsigned long flags)
64db4cff
PM
1866 __releases(rnp->lock)
1867{
654e9533 1868 unsigned long oldmask = 0;
28ecd580
PM
1869 struct rcu_node *rnp_c;
1870
a32e01ee 1871 raw_lockdep_assert_held_rcu_node(rnp);
c0b334c5 1872
64db4cff
PM
1873 /* Walk up the rcu_node hierarchy. */
1874 for (;;) {
ec2c2976 1875 if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) {
64db4cff 1876
654e9533
PM
1877 /*
1878 * Our bit has already been cleared, or the
1879 * relevant grace period is already over, so done.
1880 */
67c583a7 1881 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff
PM
1882 return;
1883 }
654e9533 1884 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
5b4c11d5 1885 WARN_ON_ONCE(!rcu_is_leaf_node(rnp) &&
2dee9404 1886 rcu_preempt_blocked_readers_cgp(rnp));
64db4cff 1887 rnp->qsmask &= ~mask;
67a0edbf 1888 trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq,
d4c08f2a
PM
1889 mask, rnp->qsmask, rnp->level,
1890 rnp->grplo, rnp->grphi,
1891 !!rnp->gp_tasks);
27f4d280 1892 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
64db4cff
PM
1893
1894 /* Other bits still set at this level, so done. */
67c583a7 1895 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff
PM
1896 return;
1897 }
d43a5d32 1898 rnp->completedqs = rnp->gp_seq;
64db4cff
PM
1899 mask = rnp->grpmask;
1900 if (rnp->parent == NULL) {
1901
1902 /* No more levels. Exit loop holding root lock. */
1903
1904 break;
1905 }
67c583a7 1906 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
28ecd580 1907 rnp_c = rnp;
64db4cff 1908 rnp = rnp->parent;
2a67e741 1909 raw_spin_lock_irqsave_rcu_node(rnp, flags);
654e9533 1910 oldmask = rnp_c->qsmask;
64db4cff
PM
1911 }
1912
1913 /*
1914 * Get here if we are the last CPU to pass through a quiescent
d3f6bad3 1915 * state for this grace period. Invoke rcu_report_qs_rsp()
f41d911f 1916 * to clean up and start the next grace period if one is needed.
64db4cff 1917 */
aff4e9ed 1918 rcu_report_qs_rsp(flags); /* releases rnp->lock. */
64db4cff
PM
1919}
1920
cc99a310
PM
1921/*
1922 * Record a quiescent state for all tasks that were previously queued
1923 * on the specified rcu_node structure and that were blocking the current
49918a54 1924 * RCU grace period. The caller must hold the corresponding rnp->lock with
cc99a310
PM
1925 * irqs disabled, and this lock is released upon return, but irqs remain
1926 * disabled.
1927 */
17a8212b 1928static void __maybe_unused
139ad4da 1929rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
cc99a310
PM
1930 __releases(rnp->lock)
1931{
654e9533 1932 unsigned long gps;
cc99a310
PM
1933 unsigned long mask;
1934 struct rcu_node *rnp_p;
1935
a32e01ee 1936 raw_lockdep_assert_held_rcu_node(rnp);
01b1d88b 1937 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPTION)) ||
c74859d1
PM
1938 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) ||
1939 rnp->qsmask != 0) {
67c583a7 1940 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
cc99a310
PM
1941 return; /* Still need more quiescent states! */
1942 }
1943
77cfc7bf 1944 rnp->completedqs = rnp->gp_seq;
cc99a310
PM
1945 rnp_p = rnp->parent;
1946 if (rnp_p == NULL) {
1947 /*
a77da14c
PM
1948 * Only one rcu_node structure in the tree, so don't
1949 * try to report up to its nonexistent parent!
cc99a310 1950 */
aff4e9ed 1951 rcu_report_qs_rsp(flags);
cc99a310
PM
1952 return;
1953 }
1954
c9a24e2d
PM
1955 /* Report up the rest of the hierarchy, tracking current ->gp_seq. */
1956 gps = rnp->gp_seq;
cc99a310 1957 mask = rnp->grpmask;
67c583a7 1958 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
2a67e741 1959 raw_spin_lock_rcu_node(rnp_p); /* irqs already disabled. */
b50912d0 1960 rcu_report_qs_rnp(mask, rnp_p, gps, flags);
cc99a310
PM
1961}
1962
64db4cff 1963/*
d3f6bad3 1964 * Record a quiescent state for the specified CPU to that CPU's rcu_data
4b455dc3 1965 * structure. This must be called from the specified CPU.
64db4cff
PM
1966 */
1967static void
33085c46 1968rcu_report_qs_rdp(int cpu, struct rcu_data *rdp)
64db4cff
PM
1969{
1970 unsigned long flags;
1971 unsigned long mask;
5d6742b3
PM
1972 bool needwake = false;
1973 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
1974 rcu_segcblist_is_offloaded(&rdp->cblist);
64db4cff
PM
1975 struct rcu_node *rnp;
1976
1977 rnp = rdp->mynode;
2a67e741 1978 raw_spin_lock_irqsave_rcu_node(rnp, flags);
c9a24e2d
PM
1979 if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq ||
1980 rdp->gpwrap) {
64db4cff
PM
1981
1982 /*
e4cc1f22
PM
1983 * The grace period in which this quiescent state was
1984 * recorded has ended, so don't report it upwards.
1985 * We will instead need a new quiescent state that lies
1986 * within the current grace period.
64db4cff 1987 */
5b74c458 1988 rdp->cpu_no_qs.b.norm = true; /* need qs for new gp. */
67c583a7 1989 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff
PM
1990 return;
1991 }
1992 mask = rdp->grpmask;
1993 if ((rnp->qsmask & mask) == 0) {
67c583a7 1994 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff 1995 } else {
64db4cff
PM
1996 /*
1997 * This GP can't end until cpu checks in, so all of our
1998 * callbacks can be processed during the next GP.
1999 */
5d6742b3
PM
2000 if (!offloaded)
2001 needwake = rcu_accelerate_cbs(rnp, rdp);
64db4cff 2002
516e5ae0 2003 rcu_disable_urgency_upon_qs(rdp);
b50912d0 2004 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
654e9533 2005 /* ^^^ Released rnp->lock */
48a7639c 2006 if (needwake)
532c00c9 2007 rcu_gp_kthread_wake();
64db4cff
PM
2008 }
2009}
2010
2011/*
2012 * Check to see if there is a new grace period of which this CPU
2013 * is not yet aware, and if so, set up local rcu_data state for it.
2014 * Otherwise, see if this CPU has just passed through its first
2015 * quiescent state for this grace period, and record that fact if so.
2016 */
2017static void
8087d3e3 2018rcu_check_quiescent_state(struct rcu_data *rdp)
64db4cff 2019{
05eb552b 2020 /* Check for grace-period ends and beginnings. */
15cabdff 2021 note_gp_changes(rdp);
64db4cff
PM
2022
2023 /*
2024 * Does this CPU still need to do its part for current grace period?
2025 * If no, return and let the other CPUs do their part as well.
2026 */
97c668b8 2027 if (!rdp->core_needs_qs)
64db4cff
PM
2028 return;
2029
2030 /*
2031 * Was there a quiescent state since the beginning of the grace
2032 * period? If no, then exit and wait for the next call.
2033 */
3a19b46a 2034 if (rdp->cpu_no_qs.b.norm)
64db4cff
PM
2035 return;
2036
d3f6bad3
PM
2037 /*
2038 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
2039 * judge of that).
2040 */
33085c46 2041 rcu_report_qs_rdp(rdp->cpu, rdp);
64db4cff
PM
2042}
2043
b1420f1c 2044/*
780cd590
PM
2045 * Near the end of the offline process. Trace the fact that this CPU
2046 * is going offline.
b1420f1c 2047 */
780cd590 2048int rcutree_dying_cpu(unsigned int cpu)
b1420f1c 2049{
4f5fbd78
YS
2050 bool blkd;
2051 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
2052 struct rcu_node *rnp = rdp->mynode;
b1420f1c 2053
ea46351c 2054 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
780cd590 2055 return 0;
ea46351c 2056
4f5fbd78 2057 blkd = !!(rnp->qsmask & rdp->grpmask);
780cd590 2058 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq,
477351f7 2059 blkd ? TPS("cpuofl") : TPS("cpuofl-bgp"));
780cd590 2060 return 0;
64db4cff
PM
2061}
2062
8af3a5e7
PM
2063/*
2064 * All CPUs for the specified rcu_node structure have gone offline,
2065 * and all tasks that were preempted within an RCU read-side critical
2066 * section while running on one of those CPUs have since exited their RCU
2067 * read-side critical section. Some other CPU is reporting this fact with
2068 * the specified rcu_node structure's ->lock held and interrupts disabled.
2069 * This function therefore goes up the tree of rcu_node structures,
2070 * clearing the corresponding bits in the ->qsmaskinit fields. Note that
2071 * the leaf rcu_node structure's ->qsmaskinit field has already been
c50cbe53 2072 * updated.
8af3a5e7
PM
2073 *
2074 * This function does check that the specified rcu_node structure has
2075 * all CPUs offline and no blocked tasks, so it is OK to invoke it
2076 * prematurely. That said, invoking it after the fact will cost you
2077 * a needless lock acquisition. So once it has done its work, don't
2078 * invoke it again.
2079 */
2080static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
2081{
2082 long mask;
2083 struct rcu_node *rnp = rnp_leaf;
2084
962aff03 2085 raw_lockdep_assert_held_rcu_node(rnp_leaf);
ea46351c 2086 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
962aff03
PM
2087 WARN_ON_ONCE(rnp_leaf->qsmaskinit) ||
2088 WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf)))
8af3a5e7
PM
2089 return;
2090 for (;;) {
2091 mask = rnp->grpmask;
2092 rnp = rnp->parent;
2093 if (!rnp)
2094 break;
2a67e741 2095 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
8af3a5e7 2096 rnp->qsmaskinit &= ~mask;
962aff03
PM
2097 /* Between grace periods, so better already be zero! */
2098 WARN_ON_ONCE(rnp->qsmask);
8af3a5e7 2099 if (rnp->qsmaskinit) {
67c583a7
BF
2100 raw_spin_unlock_rcu_node(rnp);
2101 /* irqs remain disabled. */
8af3a5e7
PM
2102 return;
2103 }
67c583a7 2104 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
8af3a5e7
PM
2105 }
2106}
2107
64db4cff 2108/*
e5601400 2109 * The CPU has been completely removed, and some other CPU is reporting
a58163d8
PM
2110 * this fact from process context. Do the remainder of the cleanup.
2111 * There can only be one CPU hotplug operation at a time, so no need for
2112 * explicit locking.
64db4cff 2113 */
780cd590 2114int rcutree_dead_cpu(unsigned int cpu)
64db4cff 2115{
da1df50d 2116 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
b1420f1c 2117 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
e5601400 2118
ea46351c 2119 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
780cd590 2120 return 0;
ea46351c 2121
2036d94a 2122 /* Adjust any no-longer-needed kthreads. */
5d01bbd1 2123 rcu_boost_kthread_setaffinity(rnp, -1);
780cd590
PM
2124 /* Do any needed no-CB deferred wakeups from this CPU. */
2125 do_nocb_deferred_wakeup(per_cpu_ptr(&rcu_data, cpu));
96926686
PM
2126
2127 // Stop-machine done, so allow nohz_full to disable tick.
2128 tick_dep_clear(TICK_DEP_BIT_RCU);
780cd590 2129 return 0;
64db4cff
PM
2130}
2131
64db4cff
PM
2132/*
2133 * Invoke any RCU callbacks that have made it to the end of their grace
2134 * period. Thottle as specified by rdp->blimit.
2135 */
5bb5d09c 2136static void rcu_do_batch(struct rcu_data *rdp)
64db4cff
PM
2137{
2138 unsigned long flags;
ec5ef87b
PM
2139 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2140 rcu_segcblist_is_offloaded(&rdp->cblist);
15fecf89
PM
2141 struct rcu_head *rhp;
2142 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
2143 long bl, count;
cfcdef5e 2144 long pending, tlimit = 0;
64db4cff 2145
dc35c893 2146 /* If no callbacks are ready, just return. */
15fecf89 2147 if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
3c779dfe 2148 trace_rcu_batch_start(rcu_state.name,
15fecf89
PM
2149 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2150 rcu_segcblist_n_cbs(&rdp->cblist), 0);
3c779dfe 2151 trace_rcu_batch_end(rcu_state.name, 0,
15fecf89 2152 !rcu_segcblist_empty(&rdp->cblist),
4968c300
PM
2153 need_resched(), is_idle_task(current),
2154 rcu_is_callbacks_kthread());
64db4cff 2155 return;
29c00b4a 2156 }
64db4cff
PM
2157
2158 /*
2159 * Extract the list of ready callbacks, disabling to prevent
15fecf89
PM
2160 * races with call_rcu() from interrupt handlers. Leave the
2161 * callback counts, as rcu_barrier() needs to be conservative.
64db4cff
PM
2162 */
2163 local_irq_save(flags);
5d6742b3 2164 rcu_nocb_lock(rdp);
8146c4e2 2165 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
cfcdef5e
ED
2166 pending = rcu_segcblist_n_cbs(&rdp->cblist);
2167 bl = max(rdp->blimit, pending >> rcu_divisor);
2168 if (unlikely(bl > 100))
2169 tlimit = local_clock() + rcu_resched_ns;
3c779dfe
PM
2170 trace_rcu_batch_start(rcu_state.name,
2171 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
15fecf89
PM
2172 rcu_segcblist_n_cbs(&rdp->cblist), bl);
2173 rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
7f36ef82
PM
2174 if (offloaded)
2175 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
5d6742b3 2176 rcu_nocb_unlock_irqrestore(rdp, flags);
64db4cff
PM
2177
2178 /* Invoke callbacks. */
6a949b7a 2179 tick_dep_set_task(current, TICK_DEP_BIT_RCU);
15fecf89
PM
2180 rhp = rcu_cblist_dequeue(&rcl);
2181 for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
2182 debug_rcu_head_unqueue(rhp);
3c779dfe 2183 if (__rcu_reclaim(rcu_state.name, rhp))
15fecf89
PM
2184 rcu_cblist_dequeued_lazy(&rcl);
2185 /*
2186 * Stop only if limit reached and CPU has something to do.
2187 * Note: The rcl structure counts down from zero.
2188 */
ec5ef87b 2189 if (-rcl.len >= bl && !offloaded &&
dff1672d
PM
2190 (need_resched() ||
2191 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
64db4cff 2192 break;
cfcdef5e
ED
2193 if (unlikely(tlimit)) {
2194 /* only call local_clock() every 32 callbacks */
2195 if (likely((-rcl.len & 31) || local_clock() < tlimit))
2196 continue;
2197 /* Exceeded the time limit, so leave. */
2198 break;
2199 }
ec5ef87b 2200 if (offloaded) {
5d6742b3
PM
2201 WARN_ON_ONCE(in_serving_softirq());
2202 local_bh_enable();
2203 lockdep_assert_irqs_enabled();
2204 cond_resched_tasks_rcu_qs();
2205 lockdep_assert_irqs_enabled();
2206 local_bh_disable();
2207 }
64db4cff
PM
2208 }
2209
2210 local_irq_save(flags);
5d6742b3 2211 rcu_nocb_lock(rdp);
4b27f20b 2212 count = -rcl.len;
3c779dfe 2213 trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
8ef0f37e 2214 is_idle_task(current), rcu_is_callbacks_kthread());
64db4cff 2215
15fecf89
PM
2216 /* Update counts and requeue any remaining callbacks. */
2217 rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
b1420f1c 2218 smp_mb(); /* List handling before counting for rcu_barrier(). */
15fecf89 2219 rcu_segcblist_insert_count(&rdp->cblist, &rcl);
64db4cff
PM
2220
2221 /* Reinstate batch limit if we have worked down the excess. */
15fecf89 2222 count = rcu_segcblist_n_cbs(&rdp->cblist);
d5a9a8c3 2223 if (rdp->blimit >= DEFAULT_MAX_RCU_BLIMIT && count <= qlowmark)
64db4cff
PM
2224 rdp->blimit = blimit;
2225
37c72e56 2226 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
15fecf89 2227 if (count == 0 && rdp->qlen_last_fqs_check != 0) {
37c72e56 2228 rdp->qlen_last_fqs_check = 0;
3c779dfe 2229 rdp->n_force_qs_snap = rcu_state.n_force_qs;
15fecf89
PM
2230 } else if (count < rdp->qlen_last_fqs_check - qhimark)
2231 rdp->qlen_last_fqs_check = count;
efd88b02
PM
2232
2233 /*
2234 * The following usually indicates a double call_rcu(). To track
2235 * this down, try building with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.
2236 */
d1b222c6
PM
2237 WARN_ON_ONCE(count == 0 && !rcu_segcblist_empty(&rdp->cblist));
2238 WARN_ON_ONCE(!IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2239 count != 0 && rcu_segcblist_empty(&rdp->cblist));
37c72e56 2240
5d6742b3 2241 rcu_nocb_unlock_irqrestore(rdp, flags);
64db4cff 2242
e0f23060 2243 /* Re-invoke RCU core processing if there are callbacks remaining. */
ec5ef87b 2244 if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist))
a46e0899 2245 invoke_rcu_core();
6a949b7a 2246 tick_dep_clear_task(current, TICK_DEP_BIT_RCU);
64db4cff
PM
2247}
2248
2249/*
c98cac60
PM
2250 * This function is invoked from each scheduling-clock interrupt,
2251 * and checks to see if this CPU is in a non-context-switch quiescent
2252 * state, for example, user mode or idle loop. It also schedules RCU
2253 * core processing. If the current grace period has gone on too long,
2254 * it will ask the scheduler to manufacture a context switch for the sole
2255 * purpose of providing a providing the needed quiescent state.
64db4cff 2256 */
c98cac60 2257void rcu_sched_clock_irq(int user)
64db4cff 2258{
f7f7bac9 2259 trace_rcu_utilization(TPS("Start scheduler-tick"));
4e95020c 2260 raw_cpu_inc(rcu_data.ticks_this_gp);
92aa39e9 2261 /* The load-acquire pairs with the store-release setting to true. */
2dba13f0 2262 if (smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
92aa39e9 2263 /* Idle and userspace execution already are quiescent states. */
a0ef9ec2 2264 if (!rcu_is_cpu_rrupt_from_idle() && !user) {
92aa39e9
PM
2265 set_tsk_need_resched(current);
2266 set_preempt_need_resched();
2267 }
2dba13f0 2268 __this_cpu_write(rcu_data.rcu_urgent_qs, false);
64db4cff 2269 }
c98cac60 2270 rcu_flavor_sched_clock_irq(user);
dd7dafd1 2271 if (rcu_pending(user))
a46e0899 2272 invoke_rcu_core();
07f27570 2273
f7f7bac9 2274 trace_rcu_utilization(TPS("End scheduler-tick"));
64db4cff
PM
2275}
2276
64db4cff 2277/*
5d8a752e
ZZ
2278 * Scan the leaf rcu_node structures. For each structure on which all
2279 * CPUs have reported a quiescent state and on which there are tasks
2280 * blocking the current grace period, initiate RCU priority boosting.
2281 * Otherwise, invoke the specified function to check dyntick state for
2282 * each CPU that has not yet reported a quiescent state.
64db4cff 2283 */
8ff0b907 2284static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
64db4cff 2285{
64db4cff
PM
2286 int cpu;
2287 unsigned long flags;
2288 unsigned long mask;
66e4c33b 2289 struct rcu_data *rdp;
a0b6c9a7 2290 struct rcu_node *rnp;
64db4cff 2291
aedf4ba9 2292 rcu_for_each_leaf_node(rnp) {
cee43939 2293 cond_resched_tasks_rcu_qs();
64db4cff 2294 mask = 0;
2a67e741 2295 raw_spin_lock_irqsave_rcu_node(rnp, flags);
a0b6c9a7 2296 if (rnp->qsmask == 0) {
01b1d88b 2297 if (!IS_ENABLED(CONFIG_PREEMPTION) ||
a77da14c
PM
2298 rcu_preempt_blocked_readers_cgp(rnp)) {
2299 /*
2300 * No point in scanning bits because they
2301 * are all zero. But we might need to
2302 * priority-boost blocked readers.
2303 */
2304 rcu_initiate_boost(rnp, flags);
2305 /* rcu_initiate_boost() releases rnp->lock */
2306 continue;
2307 }
92816435
PM
2308 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2309 continue;
64db4cff 2310 }
bc75e999
MR
2311 for_each_leaf_node_possible_cpu(rnp, cpu) {
2312 unsigned long bit = leaf_node_cpu_bit(rnp, cpu);
0edd1b17 2313 if ((rnp->qsmask & bit) != 0) {
66e4c33b
PM
2314 rdp = per_cpu_ptr(&rcu_data, cpu);
2315 if (f(rdp)) {
0edd1b17 2316 mask |= bit;
516e5ae0 2317 rcu_disable_urgency_upon_qs(rdp);
66e4c33b 2318 }
0edd1b17 2319 }
64db4cff 2320 }
45f014c5 2321 if (mask != 0) {
c9a24e2d 2322 /* Idle/offline CPUs, report (releases rnp->lock). */
b50912d0 2323 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
0aa04b05
PM
2324 } else {
2325 /* Nothing to do here, so just drop the lock. */
67c583a7 2326 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
64db4cff 2327 }
64db4cff 2328 }
64db4cff
PM
2329}
2330
2331/*
2332 * Force quiescent states on reluctant CPUs, and also detect which
2333 * CPUs are in dyntick-idle mode.
2334 */
cd920e5a 2335void rcu_force_quiescent_state(void)
64db4cff
PM
2336{
2337 unsigned long flags;
394f2769
PM
2338 bool ret;
2339 struct rcu_node *rnp;
2340 struct rcu_node *rnp_old = NULL;
2341
2342 /* Funnel through hierarchy to reduce memory contention. */
da1df50d 2343 rnp = __this_cpu_read(rcu_data.mynode);
394f2769 2344 for (; rnp != NULL; rnp = rnp->parent) {
67a0edbf 2345 ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) ||
66e4c33b 2346 !raw_spin_trylock(&rnp->fqslock);
394f2769
PM
2347 if (rnp_old != NULL)
2348 raw_spin_unlock(&rnp_old->fqslock);
d62df573 2349 if (ret)
394f2769 2350 return;
394f2769
PM
2351 rnp_old = rnp;
2352 }
336a4f6c 2353 /* rnp_old == rcu_get_root(), rnp == NULL. */
64db4cff 2354
394f2769 2355 /* Reached the root of the rcu_node tree, acquire lock. */
2a67e741 2356 raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
394f2769 2357 raw_spin_unlock(&rnp_old->fqslock);
67a0edbf 2358 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
67c583a7 2359 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
4cdfc175 2360 return; /* Someone beat us to it. */
46a1e34e 2361 }
67a0edbf
PM
2362 WRITE_ONCE(rcu_state.gp_flags,
2363 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
67c583a7 2364 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
532c00c9 2365 rcu_gp_kthread_wake();
64db4cff 2366}
cd920e5a 2367EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
64db4cff 2368
fb60e533 2369/* Perform RCU core processing work for the current CPU. */
48d07c04 2370static __latent_entropy void rcu_core(void)
64db4cff
PM
2371{
2372 unsigned long flags;
da1df50d 2373 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
26d950a9 2374 struct rcu_node *rnp = rdp->mynode;
c1ab99d6
PM
2375 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2376 rcu_segcblist_is_offloaded(&rdp->cblist);
64db4cff 2377
b049fdf8
PM
2378 if (cpu_is_offline(smp_processor_id()))
2379 return;
2380 trace_rcu_utilization(TPS("Start RCU core"));
50dc7def 2381 WARN_ON_ONCE(!rdp->beenonline);
2e597558 2382
3e310098 2383 /* Report any deferred quiescent states if preemption enabled. */
fced9c8c 2384 if (!(preempt_count() & PREEMPT_MASK)) {
3e310098 2385 rcu_preempt_deferred_qs(current);
fced9c8c
PM
2386 } else if (rcu_preempt_need_deferred_qs(current)) {
2387 set_tsk_need_resched(current);
2388 set_preempt_need_resched();
2389 }
3e310098 2390
64db4cff 2391 /* Update RCU state based on any recent quiescent states. */
8087d3e3 2392 rcu_check_quiescent_state(rdp);
64db4cff 2393
bd7af846 2394 /* No grace period and unregistered callbacks? */
de8e8730 2395 if (!rcu_gp_in_progress() &&
c1ab99d6 2396 rcu_segcblist_is_enabled(&rdp->cblist) && !offloaded) {
bd7af846 2397 local_irq_save(flags);
e44e73ca 2398 if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
c6e09b97 2399 rcu_accelerate_cbs_unlocked(rnp, rdp);
e44e73ca 2400 local_irq_restore(flags);
64db4cff
PM
2401 }
2402
791416c4 2403 rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check());
26d950a9 2404
64db4cff 2405 /* If there are callbacks ready, invoke them. */
c1ab99d6 2406 if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist) &&
43e903ad
PM
2407 likely(READ_ONCE(rcu_scheduler_fully_active)))
2408 rcu_do_batch(rdp);
96d3fd0d
PM
2409
2410 /* Do any needed deferred wakeups of rcuo kthreads. */
2411 do_nocb_deferred_wakeup(rdp);
f7f7bac9 2412 trace_rcu_utilization(TPS("End RCU core"));
64db4cff
PM
2413}
2414
48d07c04
SAS
2415static void rcu_core_si(struct softirq_action *h)
2416{
2417 rcu_core();
2418}
2419
2420static void rcu_wake_cond(struct task_struct *t, int status)
2421{
2422 /*
2423 * If the thread is yielding, only wake it when this
2424 * is invoked from idle
2425 */
2426 if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
2427 wake_up_process(t);
2428}
2429
2430static void invoke_rcu_core_kthread(void)
2431{
2432 struct task_struct *t;
2433 unsigned long flags;
2434
2435 local_irq_save(flags);
2436 __this_cpu_write(rcu_data.rcu_cpu_has_work, 1);
2437 t = __this_cpu_read(rcu_data.rcu_cpu_kthread_task);
2438 if (t != NULL && t != current)
2439 rcu_wake_cond(t, __this_cpu_read(rcu_data.rcu_cpu_kthread_status));
2440 local_irq_restore(flags);
2441}
2442
48d07c04
SAS
2443/*
2444 * Wake up this CPU's rcuc kthread to do RCU core processing.
2445 */
a46e0899 2446static void invoke_rcu_core(void)
09223371 2447{
48d07c04
SAS
2448 if (!cpu_online(smp_processor_id()))
2449 return;
2450 if (use_softirq)
b0f74036 2451 raise_softirq(RCU_SOFTIRQ);
48d07c04
SAS
2452 else
2453 invoke_rcu_core_kthread();
2454}
2455
2456static void rcu_cpu_kthread_park(unsigned int cpu)
2457{
2458 per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
2459}
2460
2461static int rcu_cpu_kthread_should_run(unsigned int cpu)
2462{
2463 return __this_cpu_read(rcu_data.rcu_cpu_has_work);
2464}
2465
2466/*
2467 * Per-CPU kernel thread that invokes RCU callbacks. This replaces
2468 * the RCU softirq used in configurations of RCU that do not support RCU
2469 * priority boosting.
2470 */
2471static void rcu_cpu_kthread(unsigned int cpu)
2472{
2473 unsigned int *statusp = this_cpu_ptr(&rcu_data.rcu_cpu_kthread_status);
2474 char work, *workp = this_cpu_ptr(&rcu_data.rcu_cpu_has_work);
2475 int spincnt;
2476
2477 for (spincnt = 0; spincnt < 10; spincnt++) {
2478 trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
2479 local_bh_disable();
2480 *statusp = RCU_KTHREAD_RUNNING;
2481 local_irq_disable();
2482 work = *workp;
2483 *workp = 0;
2484 local_irq_enable();
2485 if (work)
2486 rcu_core();
2487 local_bh_enable();
2488 if (*workp == 0) {
2489 trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
2490 *statusp = RCU_KTHREAD_WAITING;
2491 return;
2492 }
2493 }
2494 *statusp = RCU_KTHREAD_YIELDING;
2495 trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
2496 schedule_timeout_interruptible(2);
2497 trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
2498 *statusp = RCU_KTHREAD_WAITING;
2499}
2500
2501static struct smp_hotplug_thread rcu_cpu_thread_spec = {
2502 .store = &rcu_data.rcu_cpu_kthread_task,
2503 .thread_should_run = rcu_cpu_kthread_should_run,
2504 .thread_fn = rcu_cpu_kthread,
2505 .thread_comm = "rcuc/%u",
2506 .setup = rcu_cpu_kthread_setup,
2507 .park = rcu_cpu_kthread_park,
2508};
2509
2510/*
2511 * Spawn per-CPU RCU core processing kthreads.
2512 */
2513static int __init rcu_spawn_core_kthreads(void)
2514{
2515 int cpu;
2516
2517 for_each_possible_cpu(cpu)
2518 per_cpu(rcu_data.rcu_cpu_has_work, cpu) = 0;
2519 if (!IS_ENABLED(CONFIG_RCU_BOOST) && use_softirq)
2520 return 0;
2521 WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec),
2522 "%s: Could not start rcuc kthread, OOM is now expected behavior\n", __func__);
2523 return 0;
09223371 2524}
48d07c04 2525early_initcall(rcu_spawn_core_kthreads);
09223371 2526
29154c57
PM
2527/*
2528 * Handle any core-RCU processing required by a call_rcu() invocation.
2529 */
5c7d8967
PM
2530static void __call_rcu_core(struct rcu_data *rdp, struct rcu_head *head,
2531 unsigned long flags)
64db4cff 2532{
62fde6ed
PM
2533 /*
2534 * If called from an extended quiescent state, invoke the RCU
2535 * core in order to force a re-evaluation of RCU's idleness.
2536 */
9910affa 2537 if (!rcu_is_watching())
62fde6ed
PM
2538 invoke_rcu_core();
2539
a16b7a69 2540 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
29154c57 2541 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
2655d57e 2542 return;
64db4cff 2543
37c72e56
PM
2544 /*
2545 * Force the grace period if too many callbacks or too long waiting.
cd920e5a 2546 * Enforce hysteresis, and don't invoke rcu_force_quiescent_state()
37c72e56 2547 * if some other CPU has recently done so. Also, don't bother
cd920e5a 2548 * invoking rcu_force_quiescent_state() if the newly enqueued callback
37c72e56
PM
2549 * is the only one waiting for a grace period to complete.
2550 */
15fecf89
PM
2551 if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
2552 rdp->qlen_last_fqs_check + qhimark)) {
b52573d2
PM
2553
2554 /* Are we ignoring a completed grace period? */
15cabdff 2555 note_gp_changes(rdp);
b52573d2
PM
2556
2557 /* Start a new grace period if one not already started. */
de8e8730 2558 if (!rcu_gp_in_progress()) {
c6e09b97 2559 rcu_accelerate_cbs_unlocked(rdp->mynode, rdp);
b52573d2
PM
2560 } else {
2561 /* Give the grace period a kick. */
d5a9a8c3 2562 rdp->blimit = DEFAULT_MAX_RCU_BLIMIT;
5c7d8967 2563 if (rcu_state.n_force_qs == rdp->n_force_qs_snap &&
15fecf89 2564 rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
cd920e5a 2565 rcu_force_quiescent_state();
5c7d8967 2566 rdp->n_force_qs_snap = rcu_state.n_force_qs;
15fecf89 2567 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
b52573d2 2568 }
4cdfc175 2569 }
29154c57
PM
2570}
2571
ae150184
PM
2572/*
2573 * RCU callback function to leak a callback.
2574 */
2575static void rcu_leak_callback(struct rcu_head *rhp)
2576{
2577}
2578
3fbfbf7a
PM
2579/*
2580 * Helper function for call_rcu() and friends. The cpu argument will
2581 * normally be -1, indicating "currently running CPU". It may specify
dd46a788 2582 * a CPU only if that CPU is a no-CBs CPU. Currently, only rcu_barrier()
3fbfbf7a
PM
2583 * is expected to specify a CPU.
2584 */
64db4cff 2585static void
5d6742b3 2586__call_rcu(struct rcu_head *head, rcu_callback_t func, bool lazy)
64db4cff
PM
2587{
2588 unsigned long flags;
2589 struct rcu_data *rdp;
5d6742b3 2590 bool was_alldone;
64db4cff 2591
b8f2ed53
PM
2592 /* Misaligned rcu_head! */
2593 WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
2594
ae150184 2595 if (debug_rcu_head_queue(head)) {
fa3c6647
PM
2596 /*
2597 * Probable double call_rcu(), so leak the callback.
2598 * Use rcu:rcu_callback trace event to find the previous
2599 * time callback was passed to __call_rcu().
2600 */
d75f773c 2601 WARN_ONCE(1, "__call_rcu(): Double-freed CB %p->%pS()!!!\n",
fa3c6647 2602 head, head->func);
7d0ae808 2603 WRITE_ONCE(head->func, rcu_leak_callback);
ae150184
PM
2604 return;
2605 }
64db4cff
PM
2606 head->func = func;
2607 head->next = NULL;
64db4cff 2608 local_irq_save(flags);
da1df50d 2609 rdp = this_cpu_ptr(&rcu_data);
64db4cff
PM
2610
2611 /* Add the callback to our list. */
5d6742b3
PM
2612 if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist))) {
2613 // This can trigger due to call_rcu() from offline CPU:
2614 WARN_ON_ONCE(rcu_scheduler_active != RCU_SCHEDULER_INACTIVE);
34404ca8 2615 WARN_ON_ONCE(!rcu_is_watching());
5d6742b3
PM
2616 // Very early boot, before rcu_init(). Initialize if needed
2617 // and then drop through to queue the callback.
15fecf89
PM
2618 if (rcu_segcblist_empty(&rdp->cblist))
2619 rcu_segcblist_init(&rdp->cblist);
0d8ee37e 2620 }
d1b222c6
PM
2621 if (rcu_nocb_try_bypass(rdp, head, &was_alldone, flags))
2622 return; // Enqueued onto ->nocb_bypass, so just leave.
2623 /* If we get here, rcu_nocb_try_bypass() acquired ->nocb_lock. */
15fecf89 2624 rcu_segcblist_enqueue(&rdp->cblist, head, lazy);
d4c08f2a 2625 if (__is_kfree_rcu_offset((unsigned long)func))
3c779dfe
PM
2626 trace_rcu_kfree_callback(rcu_state.name, head,
2627 (unsigned long)func,
15fecf89
PM
2628 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2629 rcu_segcblist_n_cbs(&rdp->cblist));
d4c08f2a 2630 else
3c779dfe 2631 trace_rcu_callback(rcu_state.name, head,
15fecf89
PM
2632 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2633 rcu_segcblist_n_cbs(&rdp->cblist));
d4c08f2a 2634
29154c57 2635 /* Go handle any RCU core processing required. */
5d6742b3
PM
2636 if (IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2637 unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
2638 __call_rcu_nocb_wake(rdp, was_alldone, flags); /* unlocks */
2639 } else {
2640 __call_rcu_core(rdp, head, flags);
2641 local_irq_restore(flags);
2642 }
64db4cff
PM
2643}
2644
a68a2bb2 2645/**
45975c7d 2646 * call_rcu() - Queue an RCU callback for invocation after a grace period.
a68a2bb2
PM
2647 * @head: structure to be used for queueing the RCU updates.
2648 * @func: actual callback function to be invoked after the grace period
2649 *
2650 * The callback function will be invoked some time after a full grace
45975c7d
PM
2651 * period elapses, in other words after all pre-existing RCU read-side
2652 * critical sections have completed. However, the callback function
2653 * might well execute concurrently with RCU read-side critical sections
2654 * that started after call_rcu() was invoked. RCU read-side critical
2655 * sections are delimited by rcu_read_lock() and rcu_read_unlock(), and
2656 * may be nested. In addition, regions of code across which interrupts,
2657 * preemption, or softirqs have been disabled also serve as RCU read-side
2658 * critical sections. This includes hardware interrupt handlers, softirq
2659 * handlers, and NMI handlers.
2660 *
2661 * Note that all CPUs must agree that the grace period extended beyond
2662 * all pre-existing RCU read-side critical section. On systems with more
2663 * than one CPU, this means that when "func()" is invoked, each CPU is
2664 * guaranteed to have executed a full memory barrier since the end of its
2665 * last RCU read-side critical section whose beginning preceded the call
2666 * to call_rcu(). It also means that each CPU executing an RCU read-side
2667 * critical section that continues beyond the start of "func()" must have
2668 * executed a memory barrier after the call_rcu() but before the beginning
2669 * of that RCU read-side critical section. Note that these guarantees
2670 * include CPUs that are offline, idle, or executing in user mode, as
2671 * well as CPUs that are executing in the kernel.
2672 *
2673 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
2674 * resulting RCU callback function "func()", then both CPU A and CPU B are
2675 * guaranteed to execute a full memory barrier during the time interval
2676 * between the call to call_rcu() and the invocation of "func()" -- even
2677 * if CPU A and CPU B are the same CPU (but again only if the system has
2678 * more than one CPU).
2679 */
2680void call_rcu(struct rcu_head *head, rcu_callback_t func)
2681{
5d6742b3 2682 __call_rcu(head, func, 0);
45975c7d
PM
2683}
2684EXPORT_SYMBOL_GPL(call_rcu);
64db4cff 2685
495aa969
ACB
2686/*
2687 * Queue an RCU callback for lazy invocation after a grace period.
2688 * This will likely be later named something like "call_rcu_lazy()",
2689 * but this change will require some way of tagging the lazy RCU
2690 * callbacks in the list of pending callbacks. Until then, this
2691 * function may only be called from __kfree_rcu().
2692 */
98ece508 2693void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
495aa969 2694{
5d6742b3 2695 __call_rcu(head, func, 1);
495aa969
ACB
2696}
2697EXPORT_SYMBOL_GPL(kfree_call_rcu);
2698
e5bc3af7
PM
2699/*
2700 * During early boot, any blocking grace-period wait automatically
2701 * implies a grace period. Later on, this is never the case for PREEMPT.
2702 *
2703 * Howevr, because a context switch is a grace period for !PREEMPT, any
2704 * blocking grace-period wait automatically implies a grace period if
2705 * there is only one CPU online at any point time during execution of
2706 * either synchronize_rcu() or synchronize_rcu_expedited(). It is OK to
2707 * occasionally incorrectly indicate that there are multiple CPUs online
2708 * when there was in fact only one the whole time, as this just adds some
2709 * overhead: RCU still operates correctly.
2710 */
2711static int rcu_blocking_is_gp(void)
2712{
2713 int ret;
2714
01b1d88b 2715 if (IS_ENABLED(CONFIG_PREEMPTION))
e5bc3af7
PM
2716 return rcu_scheduler_active == RCU_SCHEDULER_INACTIVE;
2717 might_sleep(); /* Check for RCU read-side critical section. */
2718 preempt_disable();
2719 ret = num_online_cpus() <= 1;
2720 preempt_enable();
2721 return ret;
2722}
2723
2724/**
2725 * synchronize_rcu - wait until a grace period has elapsed.
2726 *
2727 * Control will return to the caller some time after a full grace
2728 * period has elapsed, in other words after all currently executing RCU
2729 * read-side critical sections have completed. Note, however, that
2730 * upon return from synchronize_rcu(), the caller might well be executing
2731 * concurrently with new RCU read-side critical sections that began while
2732 * synchronize_rcu() was waiting. RCU read-side critical sections are
2733 * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
2734 * In addition, regions of code across which interrupts, preemption, or
2735 * softirqs have been disabled also serve as RCU read-side critical
2736 * sections. This includes hardware interrupt handlers, softirq handlers,
2737 * and NMI handlers.
2738 *
2739 * Note that this guarantee implies further memory-ordering guarantees.
2740 * On systems with more than one CPU, when synchronize_rcu() returns,
2741 * each CPU is guaranteed to have executed a full memory barrier since
2742 * the end of its last RCU read-side critical section whose beginning
2743 * preceded the call to synchronize_rcu(). In addition, each CPU having
2744 * an RCU read-side critical section that extends beyond the return from
2745 * synchronize_rcu() is guaranteed to have executed a full memory barrier
2746 * after the beginning of synchronize_rcu() and before the beginning of
2747 * that RCU read-side critical section. Note that these guarantees include
2748 * CPUs that are offline, idle, or executing in user mode, as well as CPUs
2749 * that are executing in the kernel.
2750 *
2751 * Furthermore, if CPU A invoked synchronize_rcu(), which returned
2752 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
2753 * to have executed a full memory barrier during the execution of
2754 * synchronize_rcu() -- even if CPU A and CPU B are the same CPU (but
2755 * again only if the system has more than one CPU).
2756 */
2757void synchronize_rcu(void)
2758{
2759 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
2760 lock_is_held(&rcu_lock_map) ||
2761 lock_is_held(&rcu_sched_lock_map),
2762 "Illegal synchronize_rcu() in RCU read-side critical section");
2763 if (rcu_blocking_is_gp())
2764 return;
2765 if (rcu_gp_is_expedited())
2766 synchronize_rcu_expedited();
2767 else
2768 wait_rcu_gp(call_rcu);
2769}
2770EXPORT_SYMBOL_GPL(synchronize_rcu);
2771
765a3f4f
PM
2772/**
2773 * get_state_synchronize_rcu - Snapshot current RCU state
2774 *
2775 * Returns a cookie that is used by a later call to cond_synchronize_rcu()
2776 * to determine whether or not a full grace period has elapsed in the
2777 * meantime.
2778 */
2779unsigned long get_state_synchronize_rcu(void)
2780{
2781 /*
2782 * Any prior manipulation of RCU-protected data must happen
e4be81a2 2783 * before the load from ->gp_seq.
765a3f4f
PM
2784 */
2785 smp_mb(); /* ^^^ */
16fc9c60 2786 return rcu_seq_snap(&rcu_state.gp_seq);
765a3f4f
PM
2787}
2788EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
2789
2790/**
2791 * cond_synchronize_rcu - Conditionally wait for an RCU grace period
2792 *
2793 * @oldstate: return value from earlier call to get_state_synchronize_rcu()
2794 *
2795 * If a full RCU grace period has elapsed since the earlier call to
2796 * get_state_synchronize_rcu(), just return. Otherwise, invoke
2797 * synchronize_rcu() to wait for a full grace period.
2798 *
2799 * Yes, this function does not take counter wrap into account. But
2800 * counter wrap is harmless. If the counter wraps, we have waited for
2801 * more than 2 billion grace periods (and way more on a 64-bit system!),
2802 * so waiting for one additional grace period should be just fine.
2803 */
2804void cond_synchronize_rcu(unsigned long oldstate)
2805{
16fc9c60 2806 if (!rcu_seq_done(&rcu_state.gp_seq, oldstate))
765a3f4f 2807 synchronize_rcu();
e4be81a2
PM
2808 else
2809 smp_mb(); /* Ensure GP ends before subsequent accesses. */
765a3f4f
PM
2810}
2811EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
2812
64db4cff 2813/*
98ece508 2814 * Check to see if there is any immediate RCU-related work to be done by
49918a54
PM
2815 * the current CPU, returning 1 if so and zero otherwise. The checks are
2816 * in order of increasing expense: checks that can be carried out against
2817 * CPU-local state are performed first. However, we must check for CPU
2818 * stalls first, else we might not get a chance.
64db4cff 2819 */
dd7dafd1 2820static int rcu_pending(int user)
64db4cff 2821{
ed93dfc6 2822 bool gp_in_progress;
98ece508 2823 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
2f51f988
PM
2824 struct rcu_node *rnp = rdp->mynode;
2825
64db4cff 2826 /* Check for CPU stalls, if enabled. */
ea12ff2b 2827 check_cpu_stall(rdp);
64db4cff 2828
85f69b32
PM
2829 /* Does this CPU need a deferred NOCB wakeup? */
2830 if (rcu_nocb_need_deferred_wakeup(rdp))
2831 return 1;
2832
dd7dafd1
PM
2833 /* Is this a nohz_full CPU in userspace or idle? (Ignore RCU if so.) */
2834 if ((user || rcu_is_cpu_rrupt_from_idle()) && rcu_nohz_full_cpu())
a096932f
PM
2835 return 0;
2836
64db4cff 2837 /* Is the RCU core waiting for a quiescent state from this CPU? */
ed93dfc6
PM
2838 gp_in_progress = rcu_gp_in_progress();
2839 if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm && gp_in_progress)
64db4cff
PM
2840 return 1;
2841
2842 /* Does this CPU have callbacks ready to invoke? */
01c495f7 2843 if (rcu_segcblist_ready_cbs(&rdp->cblist))
64db4cff
PM
2844 return 1;
2845
2846 /* Has RCU gone idle with this CPU needing another grace period? */
ed93dfc6 2847 if (!gp_in_progress && rcu_segcblist_is_enabled(&rdp->cblist) &&
921bb5fa
PM
2848 (!IS_ENABLED(CONFIG_RCU_NOCB_CPU) ||
2849 !rcu_segcblist_is_offloaded(&rdp->cblist)) &&
c1935209 2850 !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
64db4cff
PM
2851 return 1;
2852
67e14c1e
PM
2853 /* Have RCU grace period completed or started? */
2854 if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq ||
01c495f7 2855 unlikely(READ_ONCE(rdp->gpwrap))) /* outside lock */
64db4cff
PM
2856 return 1;
2857
64db4cff
PM
2858 /* nothing to do */
2859 return 0;
2860}
2861
a83eff0a 2862/*
dd46a788 2863 * Helper function for rcu_barrier() tracing. If tracing is disabled,
a83eff0a
PM
2864 * the compiler is expected to optimize this away.
2865 */
dd46a788 2866static void rcu_barrier_trace(const char *s, int cpu, unsigned long done)
a83eff0a 2867{
8344b871
PM
2868 trace_rcu_barrier(rcu_state.name, s, cpu,
2869 atomic_read(&rcu_state.barrier_cpu_count), done);
a83eff0a
PM
2870}
2871
b1420f1c 2872/*
dd46a788
PM
2873 * RCU callback function for rcu_barrier(). If we are last, wake
2874 * up the task executing rcu_barrier().
b1420f1c 2875 */
24ebbca8 2876static void rcu_barrier_callback(struct rcu_head *rhp)
d0ec774c 2877{
ec9f5835 2878 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) {
dd46a788 2879 rcu_barrier_trace(TPS("LastCB"), -1,
66e4c33b 2880 rcu_state.barrier_sequence);
ec9f5835 2881 complete(&rcu_state.barrier_completion);
a83eff0a 2882 } else {
dd46a788 2883 rcu_barrier_trace(TPS("CB"), -1, rcu_state.barrier_sequence);
a83eff0a 2884 }
d0ec774c
PM
2885}
2886
2887/*
2888 * Called with preemption disabled, and from cross-cpu IRQ context.
2889 */
ec9f5835 2890static void rcu_barrier_func(void *unused)
d0ec774c 2891{
da1df50d 2892 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
d0ec774c 2893
dd46a788 2894 rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence);
f92c734f
PM
2895 rdp->barrier_head.func = rcu_barrier_callback;
2896 debug_rcu_head_queue(&rdp->barrier_head);
5d6742b3 2897 rcu_nocb_lock(rdp);
d1b222c6 2898 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies));
f92c734f 2899 if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head, 0)) {
ec9f5835 2900 atomic_inc(&rcu_state.barrier_cpu_count);
f92c734f
PM
2901 } else {
2902 debug_rcu_head_unqueue(&rdp->barrier_head);
dd46a788 2903 rcu_barrier_trace(TPS("IRQNQ"), -1,
66e4c33b 2904 rcu_state.barrier_sequence);
f92c734f 2905 }
5d6742b3 2906 rcu_nocb_unlock(rdp);
d0ec774c
PM
2907}
2908
dd46a788
PM
2909/**
2910 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
2911 *
2912 * Note that this primitive does not necessarily wait for an RCU grace period
2913 * to complete. For example, if there are no RCU callbacks queued anywhere
2914 * in the system, then rcu_barrier() is within its rights to return
2915 * immediately, without waiting for anything, much less an RCU grace period.
d0ec774c 2916 */
dd46a788 2917void rcu_barrier(void)
d0ec774c 2918{
b1420f1c 2919 int cpu;
b1420f1c 2920 struct rcu_data *rdp;
ec9f5835 2921 unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence);
b1420f1c 2922
dd46a788 2923 rcu_barrier_trace(TPS("Begin"), -1, s);
b1420f1c 2924
e74f4c45 2925 /* Take mutex to serialize concurrent rcu_barrier() requests. */
ec9f5835 2926 mutex_lock(&rcu_state.barrier_mutex);
b1420f1c 2927
4f525a52 2928 /* Did someone else do our work for us? */
ec9f5835 2929 if (rcu_seq_done(&rcu_state.barrier_sequence, s)) {
dd46a788 2930 rcu_barrier_trace(TPS("EarlyExit"), -1,
66e4c33b 2931 rcu_state.barrier_sequence);
cf3a9c48 2932 smp_mb(); /* caller's subsequent code after above check. */
ec9f5835 2933 mutex_unlock(&rcu_state.barrier_mutex);
cf3a9c48
PM
2934 return;
2935 }
2936
4f525a52 2937 /* Mark the start of the barrier operation. */
ec9f5835 2938 rcu_seq_start(&rcu_state.barrier_sequence);
dd46a788 2939 rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence);
b1420f1c 2940
d0ec774c 2941 /*
b1420f1c
PM
2942 * Initialize the count to one rather than to zero in order to
2943 * avoid a too-soon return to zero in case of a short grace period
1331e7a1
PM
2944 * (or preemption of this task). Exclude CPU-hotplug operations
2945 * to ensure that no offline CPU has callbacks queued.
d0ec774c 2946 */
ec9f5835
PM
2947 init_completion(&rcu_state.barrier_completion);
2948 atomic_set(&rcu_state.barrier_cpu_count, 1);
1331e7a1 2949 get_online_cpus();
b1420f1c
PM
2950
2951 /*
1331e7a1
PM
2952 * Force each CPU with callbacks to register a new callback.
2953 * When that callback is invoked, we will know that all of the
2954 * corresponding CPU's preceding callbacks have been invoked.
b1420f1c 2955 */
3fbfbf7a 2956 for_each_possible_cpu(cpu) {
da1df50d 2957 rdp = per_cpu_ptr(&rcu_data, cpu);
ce5215c1
PM
2958 if (!cpu_online(cpu) &&
2959 !rcu_segcblist_is_offloaded(&rdp->cblist))
2960 continue;
5d6742b3 2961 if (rcu_segcblist_n_cbs(&rdp->cblist)) {
dd46a788 2962 rcu_barrier_trace(TPS("OnlineQ"), cpu,
66e4c33b 2963 rcu_state.barrier_sequence);
ec9f5835 2964 smp_call_function_single(cpu, rcu_barrier_func, NULL, 1);
b1420f1c 2965 } else {
dd46a788 2966 rcu_barrier_trace(TPS("OnlineNQ"), cpu,
66e4c33b 2967 rcu_state.barrier_sequence);
b1420f1c
PM
2968 }
2969 }
1331e7a1 2970 put_online_cpus();
b1420f1c
PM
2971
2972 /*
2973 * Now that we have an rcu_barrier_callback() callback on each
2974 * CPU, and thus each counted, remove the initial count.
2975 */
ec9f5835
PM
2976 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count))
2977 complete(&rcu_state.barrier_completion);
b1420f1c
PM
2978
2979 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
ec9f5835 2980 wait_for_completion(&rcu_state.barrier_completion);
b1420f1c 2981
4f525a52 2982 /* Mark the end of the barrier operation. */
dd46a788 2983 rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence);
ec9f5835 2984 rcu_seq_end(&rcu_state.barrier_sequence);
4f525a52 2985
b1420f1c 2986 /* Other rcu_barrier() invocations can now safely proceed. */
ec9f5835 2987 mutex_unlock(&rcu_state.barrier_mutex);
d0ec774c 2988}
45975c7d 2989EXPORT_SYMBOL_GPL(rcu_barrier);
d0ec774c 2990
0aa04b05
PM
2991/*
2992 * Propagate ->qsinitmask bits up the rcu_node tree to account for the
2993 * first CPU in a given leaf rcu_node structure coming online. The caller
2994 * must hold the corresponding leaf rcu_node ->lock with interrrupts
2995 * disabled.
2996 */
2997static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
2998{
2999 long mask;
8d672fa6 3000 long oldmask;
0aa04b05
PM
3001 struct rcu_node *rnp = rnp_leaf;
3002
8d672fa6 3003 raw_lockdep_assert_held_rcu_node(rnp_leaf);
962aff03 3004 WARN_ON_ONCE(rnp->wait_blkd_tasks);
0aa04b05
PM
3005 for (;;) {
3006 mask = rnp->grpmask;
3007 rnp = rnp->parent;
3008 if (rnp == NULL)
3009 return;
6cf10081 3010 raw_spin_lock_rcu_node(rnp); /* Interrupts already disabled. */
8d672fa6 3011 oldmask = rnp->qsmaskinit;
0aa04b05 3012 rnp->qsmaskinit |= mask;
67c583a7 3013 raw_spin_unlock_rcu_node(rnp); /* Interrupts remain disabled. */
8d672fa6
PM
3014 if (oldmask)
3015 return;
0aa04b05
PM
3016 }
3017}
3018
64db4cff 3019/*
27569620 3020 * Do boot-time initialization of a CPU's per-CPU RCU data.
64db4cff 3021 */
27569620 3022static void __init
53b46303 3023rcu_boot_init_percpu_data(int cpu)
64db4cff 3024{
da1df50d 3025 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
27569620
PM
3026
3027 /* Set up local state, ensuring consistent view of global state. */
bc75e999 3028 rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
4c5273bf 3029 WARN_ON_ONCE(rdp->dynticks_nesting != 1);
dc5a4f29 3030 WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp)));
53b46303 3031 rdp->rcu_ofl_gp_seq = rcu_state.gp_seq;
57738942 3032 rdp->rcu_ofl_gp_flags = RCU_GP_CLEANED;
53b46303 3033 rdp->rcu_onl_gp_seq = rcu_state.gp_seq;
57738942 3034 rdp->rcu_onl_gp_flags = RCU_GP_CLEANED;
27569620 3035 rdp->cpu = cpu;
3fbfbf7a 3036 rcu_boot_init_nocb_percpu_data(rdp);
27569620
PM
3037}
3038
3039/*
53b46303
PM
3040 * Invoked early in the CPU-online process, when pretty much all services
3041 * are available. The incoming CPU is not present.
3042 *
3043 * Initializes a CPU's per-CPU RCU data. Note that only one online or
ff3bb6f4
PM
3044 * offline event can be happening at a given time. Note also that we can
3045 * accept some slop in the rsp->gp_seq access due to the fact that this
e83e73f5
PM
3046 * CPU cannot possibly have any non-offloaded RCU callbacks in flight yet.
3047 * And any offloaded callbacks are being numbered elsewhere.
64db4cff 3048 */
53b46303 3049int rcutree_prepare_cpu(unsigned int cpu)
64db4cff
PM
3050{
3051 unsigned long flags;
da1df50d 3052 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
336a4f6c 3053 struct rcu_node *rnp = rcu_get_root();
64db4cff
PM
3054
3055 /* Set up local state, ensuring consistent view of global state. */
6cf10081 3056 raw_spin_lock_irqsave_rcu_node(rnp, flags);
37c72e56 3057 rdp->qlen_last_fqs_check = 0;
53b46303 3058 rdp->n_force_qs_snap = rcu_state.n_force_qs;
64db4cff 3059 rdp->blimit = blimit;
15fecf89 3060 if (rcu_segcblist_empty(&rdp->cblist) && /* No early-boot CBs? */
e83e73f5 3061 !rcu_segcblist_is_offloaded(&rdp->cblist))
15fecf89 3062 rcu_segcblist_init(&rdp->cblist); /* Re-enable callbacks. */
4c5273bf 3063 rdp->dynticks_nesting = 1; /* CPU not up, no tearing. */
2625d469 3064 rcu_dynticks_eqs_online();
67c583a7 3065 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
64db4cff 3066
0aa04b05
PM
3067 /*
3068 * Add CPU to leaf rcu_node pending-online bitmask. Any needed
3069 * propagation up the rcu_node tree will happen at the beginning
3070 * of the next grace period.
3071 */
64db4cff 3072 rnp = rdp->mynode;
2a67e741 3073 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
b9585e94 3074 rdp->beenonline = true; /* We have now been online. */
de30ad51 3075 rdp->gp_seq = rnp->gp_seq;
7a1d0f23 3076 rdp->gp_seq_needed = rnp->gp_seq;
5b74c458 3077 rdp->cpu_no_qs.b.norm = true;
97c668b8 3078 rdp->core_needs_qs = false;
9b9500da 3079 rdp->rcu_iw_pending = false;
8aa670cd 3080 rdp->rcu_iw_gp_seq = rnp->gp_seq - 1;
53b46303 3081 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl"));
67c583a7 3082 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
4df83742 3083 rcu_prepare_kthreads(cpu);
ad368d15 3084 rcu_spawn_cpu_nocb_kthread(cpu);
4df83742
TG
3085
3086 return 0;
3087}
3088
deb34f36
PM
3089/*
3090 * Update RCU priority boot kthread affinity for CPU-hotplug changes.
3091 */
4df83742
TG
3092static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
3093{
da1df50d 3094 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
4df83742
TG
3095
3096 rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
3097}
3098
deb34f36
PM
3099/*
3100 * Near the end of the CPU-online process. Pretty much all services
3101 * enabled, and the CPU is now very much alive.
3102 */
4df83742
TG
3103int rcutree_online_cpu(unsigned int cpu)
3104{
9b9500da
PM
3105 unsigned long flags;
3106 struct rcu_data *rdp;
3107 struct rcu_node *rnp;
9b9500da 3108
b97d23c5
PM
3109 rdp = per_cpu_ptr(&rcu_data, cpu);
3110 rnp = rdp->mynode;
3111 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3112 rnp->ffmask |= rdp->grpmask;
3113 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
9b9500da
PM
3114 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
3115 return 0; /* Too early in boot for scheduler work. */
3116 sync_sched_exp_online_cleanup(cpu);
3117 rcutree_affinity_setting(cpu, -1);
96926686
PM
3118
3119 // Stop-machine done, so allow nohz_full to disable tick.
3120 tick_dep_clear(TICK_DEP_BIT_RCU);
4df83742
TG
3121 return 0;
3122}
3123
deb34f36
PM
3124/*
3125 * Near the beginning of the process. The CPU is still very much alive
3126 * with pretty much all services enabled.
3127 */
4df83742
TG
3128int rcutree_offline_cpu(unsigned int cpu)
3129{
9b9500da
PM
3130 unsigned long flags;
3131 struct rcu_data *rdp;
3132 struct rcu_node *rnp;
9b9500da 3133
b97d23c5
PM
3134 rdp = per_cpu_ptr(&rcu_data, cpu);
3135 rnp = rdp->mynode;
3136 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3137 rnp->ffmask &= ~rdp->grpmask;
3138 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
9b9500da 3139
4df83742 3140 rcutree_affinity_setting(cpu, cpu);
96926686
PM
3141
3142 // nohz_full CPUs need the tick for stop-machine to work quickly
3143 tick_dep_set(TICK_DEP_BIT_RCU);
4df83742
TG
3144 return 0;
3145}
3146
f64c6013
PZ
3147static DEFINE_PER_CPU(int, rcu_cpu_started);
3148
7ec99de3
PM
3149/*
3150 * Mark the specified CPU as being online so that subsequent grace periods
3151 * (both expedited and normal) will wait on it. Note that this means that
3152 * incoming CPUs are not allowed to use RCU read-side critical sections
3153 * until this function is called. Failing to observe this restriction
3154 * will result in lockdep splats.
deb34f36
PM
3155 *
3156 * Note that this function is special in that it is invoked directly
3157 * from the incoming CPU rather than from the cpuhp_step mechanism.
3158 * This is because this function must be invoked at a precise location.
7ec99de3
PM
3159 */
3160void rcu_cpu_starting(unsigned int cpu)
3161{
3162 unsigned long flags;
3163 unsigned long mask;
313517fc
PM
3164 int nbits;
3165 unsigned long oldmask;
7ec99de3
PM
3166 struct rcu_data *rdp;
3167 struct rcu_node *rnp;
7ec99de3 3168
f64c6013
PZ
3169 if (per_cpu(rcu_cpu_started, cpu))
3170 return;
3171
3172 per_cpu(rcu_cpu_started, cpu) = 1;
3173
b97d23c5
PM
3174 rdp = per_cpu_ptr(&rcu_data, cpu);
3175 rnp = rdp->mynode;
3176 mask = rdp->grpmask;
3177 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3178 rnp->qsmaskinitnext |= mask;
3179 oldmask = rnp->expmaskinitnext;
3180 rnp->expmaskinitnext |= mask;
3181 oldmask ^= rnp->expmaskinitnext;
3182 nbits = bitmap_weight(&oldmask, BITS_PER_LONG);
3183 /* Allow lockless access for expedited grace periods. */
eb7a6653 3184 smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + nbits); /* ^^^ */
b97d23c5 3185 rcu_gpnum_ovf(rnp, rdp); /* Offline-induced counter wrap? */
eb7a6653
PM
3186 rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3187 rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags);
b97d23c5 3188 if (rnp->qsmask & mask) { /* RCU waiting on incoming CPU? */
516e5ae0 3189 rcu_disable_urgency_upon_qs(rdp);
b97d23c5
PM
3190 /* Report QS -after- changing ->qsmaskinitnext! */
3191 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3192 } else {
3193 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
7ec99de3 3194 }
313517fc 3195 smp_mb(); /* Ensure RCU read-side usage follows above initialization. */
7ec99de3
PM
3196}
3197
27d50c7e
TG
3198#ifdef CONFIG_HOTPLUG_CPU
3199/*
53b46303
PM
3200 * The outgoing function has no further need of RCU, so remove it from
3201 * the rcu_node tree's ->qsmaskinitnext bit masks.
3202 *
3203 * Note that this function is special in that it is invoked directly
3204 * from the outgoing CPU rather than from the cpuhp_step mechanism.
3205 * This is because this function must be invoked at a precise location.
27d50c7e 3206 */
53b46303 3207void rcu_report_dead(unsigned int cpu)
27d50c7e
TG
3208{
3209 unsigned long flags;
3210 unsigned long mask;
da1df50d 3211 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
27d50c7e
TG
3212 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
3213
49918a54 3214 /* QS for any half-done expedited grace period. */
53b46303 3215 preempt_disable();
63d4c8c9 3216 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
53b46303
PM
3217 preempt_enable();
3218 rcu_preempt_deferred_qs(current);
3219
27d50c7e
TG
3220 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
3221 mask = rdp->grpmask;
894d45bb 3222 raw_spin_lock(&rcu_state.ofl_lock);
27d50c7e 3223 raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
53b46303
PM
3224 rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3225 rdp->rcu_ofl_gp_flags = READ_ONCE(rcu_state.gp_flags);
fece2776
PM
3226 if (rnp->qsmask & mask) { /* RCU waiting on outgoing CPU? */
3227 /* Report quiescent state -before- changing ->qsmaskinitnext! */
b50912d0 3228 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
fece2776
PM
3229 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3230 }
27d50c7e 3231 rnp->qsmaskinitnext &= ~mask;
710d60cb 3232 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
894d45bb 3233 raw_spin_unlock(&rcu_state.ofl_lock);
f64c6013
PZ
3234
3235 per_cpu(rcu_cpu_started, cpu) = 0;
27d50c7e 3236}
a58163d8 3237
53b46303
PM
3238/*
3239 * The outgoing CPU has just passed through the dying-idle state, and we
3240 * are being invoked from the CPU that was IPIed to continue the offline
3241 * operation. Migrate the outgoing CPU's callbacks to the current CPU.
3242 */
3243void rcutree_migrate_callbacks(int cpu)
a58163d8
PM
3244{
3245 unsigned long flags;
b1a2d79f 3246 struct rcu_data *my_rdp;
c00045be 3247 struct rcu_node *my_rnp;
da1df50d 3248 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
ec4eacce 3249 bool needwake;
a58163d8 3250
ce5215c1
PM
3251 if (rcu_segcblist_is_offloaded(&rdp->cblist) ||
3252 rcu_segcblist_empty(&rdp->cblist))
95335c03
PM
3253 return; /* No callbacks to migrate. */
3254
b1a2d79f 3255 local_irq_save(flags);
da1df50d 3256 my_rdp = this_cpu_ptr(&rcu_data);
c00045be 3257 my_rnp = my_rdp->mynode;
5d6742b3 3258 rcu_nocb_lock(my_rdp); /* irqs already disabled. */
d1b222c6 3259 WARN_ON_ONCE(!rcu_nocb_flush_bypass(my_rdp, NULL, jiffies));
c00045be 3260 raw_spin_lock_rcu_node(my_rnp); /* irqs already disabled. */
ec4eacce 3261 /* Leverage recent GPs and set GP for new callbacks. */
c00045be
PM
3262 needwake = rcu_advance_cbs(my_rnp, rdp) ||
3263 rcu_advance_cbs(my_rnp, my_rdp);
f2dbe4a5 3264 rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
23651d9b 3265 needwake = needwake || rcu_advance_cbs(my_rnp, my_rdp);
c035280f 3266 rcu_segcblist_disable(&rdp->cblist);
09efeeee
PM
3267 WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) !=
3268 !rcu_segcblist_n_cbs(&my_rdp->cblist));
5d6742b3
PM
3269 if (rcu_segcblist_is_offloaded(&my_rdp->cblist)) {
3270 raw_spin_unlock_rcu_node(my_rnp); /* irqs remain disabled. */
3271 __call_rcu_nocb_wake(my_rdp, true, flags);
3272 } else {
3273 rcu_nocb_unlock(my_rdp); /* irqs remain disabled. */
3274 raw_spin_unlock_irqrestore_rcu_node(my_rnp, flags);
3275 }
ec4eacce 3276 if (needwake)
532c00c9 3277 rcu_gp_kthread_wake();
5d6742b3 3278 lockdep_assert_irqs_enabled();
a58163d8
PM
3279 WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
3280 !rcu_segcblist_empty(&rdp->cblist),
3281 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
3282 cpu, rcu_segcblist_n_cbs(&rdp->cblist),
3283 rcu_segcblist_first_cb(&rdp->cblist));
3284}
27d50c7e
TG
3285#endif
3286
deb34f36
PM
3287/*
3288 * On non-huge systems, use expedited RCU grace periods to make suspend
3289 * and hibernation run faster.
3290 */
d1d74d14
BP
3291static int rcu_pm_notify(struct notifier_block *self,
3292 unsigned long action, void *hcpu)
3293{
3294 switch (action) {
3295 case PM_HIBERNATION_PREPARE:
3296 case PM_SUSPEND_PREPARE:
e85e6a21 3297 rcu_expedite_gp();
d1d74d14
BP
3298 break;
3299 case PM_POST_HIBERNATION:
3300 case PM_POST_SUSPEND:
e85e6a21 3301 rcu_unexpedite_gp();
d1d74d14
BP
3302 break;
3303 default:
3304 break;
3305 }
3306 return NOTIFY_OK;
3307}
3308
b3dbec76 3309/*
49918a54 3310 * Spawn the kthreads that handle RCU's grace periods.
b3dbec76
PM
3311 */
3312static int __init rcu_spawn_gp_kthread(void)
3313{
3314 unsigned long flags;
a94844b2 3315 int kthread_prio_in = kthread_prio;
b3dbec76 3316 struct rcu_node *rnp;
a94844b2 3317 struct sched_param sp;
b3dbec76
PM
3318 struct task_struct *t;
3319
a94844b2 3320 /* Force priority into range. */
c7cd161e
JFG
3321 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2
3322 && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST))
3323 kthread_prio = 2;
3324 else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
a94844b2
PM
3325 kthread_prio = 1;
3326 else if (kthread_prio < 0)
3327 kthread_prio = 0;
3328 else if (kthread_prio > 99)
3329 kthread_prio = 99;
c7cd161e 3330
a94844b2
PM
3331 if (kthread_prio != kthread_prio_in)
3332 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
3333 kthread_prio, kthread_prio_in);
3334
9386c0b7 3335 rcu_scheduler_fully_active = 1;
b97d23c5 3336 t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name);
08543bda
PM
3337 if (WARN_ONCE(IS_ERR(t), "%s: Could not start grace-period kthread, OOM is now expected behavior\n", __func__))
3338 return 0;
b97d23c5
PM
3339 if (kthread_prio) {
3340 sp.sched_priority = kthread_prio;
3341 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
b3dbec76 3342 }
b97d23c5
PM
3343 rnp = rcu_get_root();
3344 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3345 rcu_state.gp_kthread = t;
b97d23c5
PM
3346 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3347 wake_up_process(t);
35ce7f29 3348 rcu_spawn_nocb_kthreads();
9386c0b7 3349 rcu_spawn_boost_kthreads();
b3dbec76
PM
3350 return 0;
3351}
3352early_initcall(rcu_spawn_gp_kthread);
3353
bbad9379 3354/*
52d7e48b
PM
3355 * This function is invoked towards the end of the scheduler's
3356 * initialization process. Before this is called, the idle task might
3357 * contain synchronous grace-period primitives (during which time, this idle
3358 * task is booting the system, and such primitives are no-ops). After this
3359 * function is called, any synchronous grace-period primitives are run as
3360 * expedited, with the requesting task driving the grace period forward.
900b1028 3361 * A later core_initcall() rcu_set_runtime_mode() will switch to full
52d7e48b 3362 * runtime RCU functionality.
bbad9379
PM
3363 */
3364void rcu_scheduler_starting(void)
3365{
3366 WARN_ON(num_online_cpus() != 1);
3367 WARN_ON(nr_context_switches() > 0);
52d7e48b
PM
3368 rcu_test_sync_prims();
3369 rcu_scheduler_active = RCU_SCHEDULER_INIT;
3370 rcu_test_sync_prims();
bbad9379
PM
3371}
3372
64db4cff 3373/*
49918a54 3374 * Helper function for rcu_init() that initializes the rcu_state structure.
64db4cff 3375 */
b8bb1f63 3376static void __init rcu_init_one(void)
64db4cff 3377{
cb007102
AG
3378 static const char * const buf[] = RCU_NODE_NAME_INIT;
3379 static const char * const fqs[] = RCU_FQS_NAME_INIT;
3dc5dbe9
PM
3380 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
3381 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
199977bf 3382
199977bf 3383 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
64db4cff
PM
3384 int cpustride = 1;
3385 int i;
3386 int j;
3387 struct rcu_node *rnp;
3388
05b84aec 3389 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
b6407e86 3390
3eaaaf6c
PM
3391 /* Silence gcc 4.8 false positive about array index out of range. */
3392 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
3393 panic("rcu_init_one: rcu_num_lvls out of range");
4930521a 3394
64db4cff
PM
3395 /* Initialize the level-tracking arrays. */
3396
f885b7f2 3397 for (i = 1; i < rcu_num_lvls; i++)
eb7a6653
PM
3398 rcu_state.level[i] =
3399 rcu_state.level[i - 1] + num_rcu_lvl[i - 1];
41f5c631 3400 rcu_init_levelspread(levelspread, num_rcu_lvl);
64db4cff
PM
3401
3402 /* Initialize the elements themselves, starting from the leaves. */
3403
f885b7f2 3404 for (i = rcu_num_lvls - 1; i >= 0; i--) {
199977bf 3405 cpustride *= levelspread[i];
eb7a6653 3406 rnp = rcu_state.level[i];
41f5c631 3407 for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
67c583a7
BF
3408 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
3409 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
b6407e86 3410 &rcu_node_class[i], buf[i]);
394f2769
PM
3411 raw_spin_lock_init(&rnp->fqslock);
3412 lockdep_set_class_and_name(&rnp->fqslock,
3413 &rcu_fqs_class[i], fqs[i]);
eb7a6653
PM
3414 rnp->gp_seq = rcu_state.gp_seq;
3415 rnp->gp_seq_needed = rcu_state.gp_seq;
3416 rnp->completedqs = rcu_state.gp_seq;
64db4cff
PM
3417 rnp->qsmask = 0;
3418 rnp->qsmaskinit = 0;
3419 rnp->grplo = j * cpustride;
3420 rnp->grphi = (j + 1) * cpustride - 1;
595f3900
HS
3421 if (rnp->grphi >= nr_cpu_ids)
3422 rnp->grphi = nr_cpu_ids - 1;
64db4cff
PM
3423 if (i == 0) {
3424 rnp->grpnum = 0;
3425 rnp->grpmask = 0;
3426 rnp->parent = NULL;
3427 } else {
199977bf 3428 rnp->grpnum = j % levelspread[i - 1];
df63fa5b 3429 rnp->grpmask = BIT(rnp->grpnum);
eb7a6653 3430 rnp->parent = rcu_state.level[i - 1] +
199977bf 3431 j / levelspread[i - 1];
64db4cff
PM
3432 }
3433 rnp->level = i;
12f5f524 3434 INIT_LIST_HEAD(&rnp->blkd_tasks);
dae6e64d 3435 rcu_init_one_nocb(rnp);
f6a12f34
PM
3436 init_waitqueue_head(&rnp->exp_wq[0]);
3437 init_waitqueue_head(&rnp->exp_wq[1]);
3b5f668e
PM
3438 init_waitqueue_head(&rnp->exp_wq[2]);
3439 init_waitqueue_head(&rnp->exp_wq[3]);
f6a12f34 3440 spin_lock_init(&rnp->exp_lock);
64db4cff
PM
3441 }
3442 }
0c34029a 3443
eb7a6653
PM
3444 init_swait_queue_head(&rcu_state.gp_wq);
3445 init_swait_queue_head(&rcu_state.expedited_wq);
aedf4ba9 3446 rnp = rcu_first_leaf_node();
0c34029a 3447 for_each_possible_cpu(i) {
4a90a068 3448 while (i > rnp->grphi)
0c34029a 3449 rnp++;
da1df50d 3450 per_cpu_ptr(&rcu_data, i)->mynode = rnp;
53b46303 3451 rcu_boot_init_percpu_data(i);
0c34029a 3452 }
64db4cff
PM
3453}
3454
f885b7f2
PM
3455/*
3456 * Compute the rcu_node tree geometry from kernel parameters. This cannot
4102adab 3457 * replace the definitions in tree.h because those are needed to size
f885b7f2
PM
3458 * the ->node array in the rcu_state structure.
3459 */
3460static void __init rcu_init_geometry(void)
3461{
026ad283 3462 ulong d;
f885b7f2 3463 int i;
05b84aec 3464 int rcu_capacity[RCU_NUM_LVLS];
f885b7f2 3465
026ad283
PM
3466 /*
3467 * Initialize any unspecified boot parameters.
3468 * The default values of jiffies_till_first_fqs and
3469 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
3470 * value, which is a function of HZ, then adding one for each
3471 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
3472 */
3473 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
3474 if (jiffies_till_first_fqs == ULONG_MAX)
3475 jiffies_till_first_fqs = d;
3476 if (jiffies_till_next_fqs == ULONG_MAX)
3477 jiffies_till_next_fqs = d;
6973032a 3478 adjust_jiffies_till_sched_qs();
026ad283 3479
f885b7f2 3480 /* If the compile-time values are accurate, just leave. */
47d631af 3481 if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
b17c7035 3482 nr_cpu_ids == NR_CPUS)
f885b7f2 3483 return;
a7538352 3484 pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
39479098 3485 rcu_fanout_leaf, nr_cpu_ids);
f885b7f2 3486
f885b7f2 3487 /*
ee968ac6
PM
3488 * The boot-time rcu_fanout_leaf parameter must be at least two
3489 * and cannot exceed the number of bits in the rcu_node masks.
3490 * Complain and fall back to the compile-time values if this
3491 * limit is exceeded.
f885b7f2 3492 */
ee968ac6 3493 if (rcu_fanout_leaf < 2 ||
75cf15a4 3494 rcu_fanout_leaf > sizeof(unsigned long) * 8) {
13bd6494 3495 rcu_fanout_leaf = RCU_FANOUT_LEAF;
f885b7f2
PM
3496 WARN_ON(1);
3497 return;
3498 }
3499
f885b7f2
PM
3500 /*
3501 * Compute number of nodes that can be handled an rcu_node tree
9618138b 3502 * with the given number of levels.
f885b7f2 3503 */
9618138b 3504 rcu_capacity[0] = rcu_fanout_leaf;
05b84aec 3505 for (i = 1; i < RCU_NUM_LVLS; i++)
05c5df31 3506 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
f885b7f2
PM
3507
3508 /*
75cf15a4 3509 * The tree must be able to accommodate the configured number of CPUs.
ee968ac6 3510 * If this limit is exceeded, fall back to the compile-time values.
f885b7f2 3511 */
ee968ac6
PM
3512 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
3513 rcu_fanout_leaf = RCU_FANOUT_LEAF;
3514 WARN_ON(1);
3515 return;
3516 }
f885b7f2 3517
679f9858 3518 /* Calculate the number of levels in the tree. */
9618138b 3519 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
679f9858 3520 }
9618138b 3521 rcu_num_lvls = i + 1;
679f9858 3522
f885b7f2 3523 /* Calculate the number of rcu_nodes at each level of the tree. */
679f9858 3524 for (i = 0; i < rcu_num_lvls; i++) {
9618138b 3525 int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
679f9858
AG
3526 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
3527 }
f885b7f2
PM
3528
3529 /* Calculate the total number of rcu_node structures. */
3530 rcu_num_nodes = 0;
679f9858 3531 for (i = 0; i < rcu_num_lvls; i++)
f885b7f2 3532 rcu_num_nodes += num_rcu_lvl[i];
f885b7f2
PM
3533}
3534
a3dc2948
PM
3535/*
3536 * Dump out the structure of the rcu_node combining tree associated
49918a54 3537 * with the rcu_state structure.
a3dc2948 3538 */
b8bb1f63 3539static void __init rcu_dump_rcu_node_tree(void)
a3dc2948
PM
3540{
3541 int level = 0;
3542 struct rcu_node *rnp;
3543
3544 pr_info("rcu_node tree layout dump\n");
3545 pr_info(" ");
aedf4ba9 3546 rcu_for_each_node_breadth_first(rnp) {
a3dc2948
PM
3547 if (rnp->level != level) {
3548 pr_cont("\n");
3549 pr_info(" ");
3550 level = rnp->level;
3551 }
3552 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
3553 }
3554 pr_cont("\n");
3555}
3556
ad7c946b 3557struct workqueue_struct *rcu_gp_wq;
25f3d7ef 3558struct workqueue_struct *rcu_par_gp_wq;
ad7c946b 3559
9f680ab4 3560void __init rcu_init(void)
64db4cff 3561{
017c4261 3562 int cpu;
9f680ab4 3563
47627678
PM
3564 rcu_early_boot_tests();
3565
f41d911f 3566 rcu_bootup_announce();
f885b7f2 3567 rcu_init_geometry();
b8bb1f63 3568 rcu_init_one();
a3dc2948 3569 if (dump_tree)
b8bb1f63 3570 rcu_dump_rcu_node_tree();
48d07c04
SAS
3571 if (use_softirq)
3572 open_softirq(RCU_SOFTIRQ, rcu_core_si);
9f680ab4
PM
3573
3574 /*
3575 * We don't need protection against CPU-hotplug here because
3576 * this is called early in boot, before either interrupts
3577 * or the scheduler are operational.
3578 */
d1d74d14 3579 pm_notifier(rcu_pm_notify, 0);
7ec99de3 3580 for_each_online_cpu(cpu) {
4df83742 3581 rcutree_prepare_cpu(cpu);
7ec99de3 3582 rcu_cpu_starting(cpu);
9b9500da 3583 rcutree_online_cpu(cpu);
7ec99de3 3584 }
ad7c946b
PM
3585
3586 /* Create workqueue for expedited GPs and for Tree SRCU. */
3587 rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
3588 WARN_ON(!rcu_gp_wq);
25f3d7ef
PM
3589 rcu_par_gp_wq = alloc_workqueue("rcu_par_gp", WQ_MEM_RECLAIM, 0);
3590 WARN_ON(!rcu_par_gp_wq);
e0fcba9a 3591 srcu_init();
64db4cff
PM
3592}
3593
10462d6f 3594#include "tree_stall.h"
3549c2bc 3595#include "tree_exp.h"
4102adab 3596#include "tree_plugin.h"