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