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