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