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