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