2 * Read-Copy Update mechanism for mutual exclusion
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
18 * Copyright IBM Corporation, 2008
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
24 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
25 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
27 * For detailed explanation of Read-Copy Update mechanism see -
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>
38 #include <linux/nmi.h>
39 #include <linux/atomic.h>
40 #include <linux/bitops.h>
41 #include <linux/export.h>
42 #include <linux/completion.h>
43 #include <linux/moduleparam.h>
44 #include <linux/module.h>
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>
50 #include <linux/kernel_stat.h>
51 #include <linux/wait.h>
52 #include <linux/kthread.h>
53 #include <linux/prefetch.h>
54 #include <linux/delay.h>
55 #include <linux/stop_machine.h>
56 #include <linux/random.h>
57 #include <linux/trace_events.h>
58 #include <linux/suspend.h>
63 MODULE_ALIAS("rcutree");
64 #ifdef MODULE_PARAM_PREFIX
65 #undef MODULE_PARAM_PREFIX
67 #define MODULE_PARAM_PREFIX "rcutree."
69 /* Data structures. */
71 static struct lock_class_key rcu_node_class
[RCU_NUM_LVLS
];
72 static struct lock_class_key rcu_fqs_class
[RCU_NUM_LVLS
];
73 static struct lock_class_key rcu_exp_class
[RCU_NUM_LVLS
];
74 static struct lock_class_key rcu_exp_sched_class
[RCU_NUM_LVLS
];
77 * In order to export the rcu_state name to the tracing tools, it
78 * needs to be added in the __tracepoint_string section.
79 * This requires defining a separate variable tp_<sname>_varname
80 * that points to the string being used, and this will allow
81 * the tracing userspace tools to be able to decipher the string
82 * address to the matching string.
85 # define DEFINE_RCU_TPS(sname) \
86 static char sname##_varname[] = #sname; \
87 static const char *tp_##sname##_varname __used __tracepoint_string = sname##_varname;
88 # define RCU_STATE_NAME(sname) sname##_varname
90 # define DEFINE_RCU_TPS(sname)
91 # define RCU_STATE_NAME(sname) __stringify(sname)
94 #define RCU_STATE_INITIALIZER(sname, sabbr, cr) \
95 DEFINE_RCU_TPS(sname) \
96 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, sname##_data); \
97 struct rcu_state sname##_state = { \
98 .level = { &sname##_state.node[0] }, \
99 .rda = &sname##_data, \
101 .fqs_state = RCU_GP_IDLE, \
102 .gpnum = 0UL - 300UL, \
103 .completed = 0UL - 300UL, \
104 .orphan_lock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.orphan_lock), \
105 .orphan_nxttail = &sname##_state.orphan_nxtlist, \
106 .orphan_donetail = &sname##_state.orphan_donelist, \
107 .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
108 .name = RCU_STATE_NAME(sname), \
112 RCU_STATE_INITIALIZER(rcu_sched
, 's', call_rcu_sched
);
113 RCU_STATE_INITIALIZER(rcu_bh
, 'b', call_rcu_bh
);
115 static struct rcu_state
*const rcu_state_p
;
116 static struct rcu_data __percpu
*const rcu_data_p
;
117 LIST_HEAD(rcu_struct_flavors
);
119 /* Dump rcu_node combining tree at boot to verify correct setup. */
120 static bool dump_tree
;
121 module_param(dump_tree
, bool, 0444);
122 /* Control rcu_node-tree auto-balancing at boot time. */
123 static bool rcu_fanout_exact
;
124 module_param(rcu_fanout_exact
, bool, 0444);
125 /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
126 static int rcu_fanout_leaf
= RCU_FANOUT_LEAF
;
127 module_param(rcu_fanout_leaf
, int, 0444);
128 int rcu_num_lvls __read_mostly
= RCU_NUM_LVLS
;
129 /* Number of rcu_nodes at specified level. */
130 static int num_rcu_lvl
[] = NUM_RCU_LVL_INIT
;
131 int rcu_num_nodes __read_mostly
= NUM_RCU_NODES
; /* Total # rcu_nodes in use. */
134 * The rcu_scheduler_active variable transitions from zero to one just
135 * before the first task is spawned. So when this variable is zero, RCU
136 * can assume that there is but one task, allowing RCU to (for example)
137 * optimize synchronize_sched() to a simple barrier(). When this variable
138 * is one, RCU must actually do all the hard work required to detect real
139 * grace periods. This variable is also used to suppress boot-time false
140 * positives from lockdep-RCU error checking.
142 int rcu_scheduler_active __read_mostly
;
143 EXPORT_SYMBOL_GPL(rcu_scheduler_active
);
146 * The rcu_scheduler_fully_active variable transitions from zero to one
147 * during the early_initcall() processing, which is after the scheduler
148 * is capable of creating new tasks. So RCU processing (for example,
149 * creating tasks for RCU priority boosting) must be delayed until after
150 * rcu_scheduler_fully_active transitions from zero to one. We also
151 * currently delay invocation of any RCU callbacks until after this point.
153 * It might later prove better for people registering RCU callbacks during
154 * early boot to take responsibility for these callbacks, but one step at
157 static int rcu_scheduler_fully_active __read_mostly
;
159 static void rcu_init_new_rnp(struct rcu_node
*rnp_leaf
);
160 static void rcu_cleanup_dead_rnp(struct rcu_node
*rnp_leaf
);
161 static void rcu_boost_kthread_setaffinity(struct rcu_node
*rnp
, int outgoingcpu
);
162 static void invoke_rcu_core(void);
163 static void invoke_rcu_callbacks(struct rcu_state
*rsp
, struct rcu_data
*rdp
);
165 /* rcuc/rcub kthread realtime priority */
166 #ifdef CONFIG_RCU_KTHREAD_PRIO
167 static int kthread_prio
= CONFIG_RCU_KTHREAD_PRIO
;
168 #else /* #ifdef CONFIG_RCU_KTHREAD_PRIO */
169 static int kthread_prio
= IS_ENABLED(CONFIG_RCU_BOOST
) ? 1 : 0;
170 #endif /* #else #ifdef CONFIG_RCU_KTHREAD_PRIO */
171 module_param(kthread_prio
, int, 0644);
173 /* Delay in jiffies for grace-period initialization delays, debug only. */
175 #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT
176 static int gp_preinit_delay
= CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT_DELAY
;
177 module_param(gp_preinit_delay
, int, 0644);
178 #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
179 static const int gp_preinit_delay
;
180 #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
182 #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT
183 static int gp_init_delay
= CONFIG_RCU_TORTURE_TEST_SLOW_INIT_DELAY
;
184 module_param(gp_init_delay
, int, 0644);
185 #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
186 static const int gp_init_delay
;
187 #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
189 #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP
190 static int gp_cleanup_delay
= CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP_DELAY
;
191 module_param(gp_cleanup_delay
, int, 0644);
192 #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
193 static const int gp_cleanup_delay
;
194 #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
197 * Number of grace periods between delays, normalized by the duration of
198 * the delay. The longer the the delay, the more the grace periods between
199 * each delay. The reason for this normalization is that it means that,
200 * for non-zero delays, the overall slowdown of grace periods is constant
201 * regardless of the duration of the delay. This arrangement balances
202 * the need for long delays to increase some race probabilities with the
203 * need for fast grace periods to increase other race probabilities.
205 #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
208 * Track the rcutorture test sequence number and the update version
209 * number within a given test. The rcutorture_testseq is incremented
210 * on every rcutorture module load and unload, so has an odd value
211 * when a test is running. The rcutorture_vernum is set to zero
212 * when rcutorture starts and is incremented on each rcutorture update.
213 * These variables enable correlating rcutorture output with the
214 * RCU tracing information.
216 unsigned long rcutorture_testseq
;
217 unsigned long rcutorture_vernum
;
220 * Compute the mask of online CPUs for the specified rcu_node structure.
221 * This will not be stable unless the rcu_node structure's ->lock is
222 * held, but the bit corresponding to the current CPU will be stable
225 unsigned long rcu_rnp_online_cpus(struct rcu_node
*rnp
)
227 return READ_ONCE(rnp
->qsmaskinitnext
);
231 * Return true if an RCU grace period is in progress. The READ_ONCE()s
232 * permit this function to be invoked without holding the root rcu_node
233 * structure's ->lock, but of course results can be subject to change.
235 static int rcu_gp_in_progress(struct rcu_state
*rsp
)
237 return READ_ONCE(rsp
->completed
) != READ_ONCE(rsp
->gpnum
);
241 * Note a quiescent state. Because we do not need to know
242 * how many quiescent states passed, just if there was at least
243 * one since the start of the grace period, this just sets a flag.
244 * The caller must have disabled preemption.
246 void rcu_sched_qs(void)
248 if (!__this_cpu_read(rcu_sched_data
.passed_quiesce
)) {
249 trace_rcu_grace_period(TPS("rcu_sched"),
250 __this_cpu_read(rcu_sched_data
.gpnum
),
252 __this_cpu_write(rcu_sched_data
.passed_quiesce
, 1);
258 if (!__this_cpu_read(rcu_bh_data
.passed_quiesce
)) {
259 trace_rcu_grace_period(TPS("rcu_bh"),
260 __this_cpu_read(rcu_bh_data
.gpnum
),
262 __this_cpu_write(rcu_bh_data
.passed_quiesce
, 1);
266 static DEFINE_PER_CPU(int, rcu_sched_qs_mask
);
268 static DEFINE_PER_CPU(struct rcu_dynticks
, rcu_dynticks
) = {
269 .dynticks_nesting
= DYNTICK_TASK_EXIT_IDLE
,
270 .dynticks
= ATOMIC_INIT(1),
271 #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
272 .dynticks_idle_nesting
= DYNTICK_TASK_NEST_VALUE
,
273 .dynticks_idle
= ATOMIC_INIT(1),
274 #endif /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
277 DEFINE_PER_CPU_SHARED_ALIGNED(unsigned long, rcu_qs_ctr
);
278 EXPORT_PER_CPU_SYMBOL_GPL(rcu_qs_ctr
);
281 * Let the RCU core know that this CPU has gone through the scheduler,
282 * which is a quiescent state. This is called when the need for a
283 * quiescent state is urgent, so we burn an atomic operation and full
284 * memory barriers to let the RCU core know about it, regardless of what
285 * this CPU might (or might not) do in the near future.
287 * We inform the RCU core by emulating a zero-duration dyntick-idle
288 * period, which we in turn do by incrementing the ->dynticks counter
291 static void rcu_momentary_dyntick_idle(void)
294 struct rcu_data
*rdp
;
295 struct rcu_dynticks
*rdtp
;
297 struct rcu_state
*rsp
;
299 local_irq_save(flags
);
302 * Yes, we can lose flag-setting operations. This is OK, because
303 * the flag will be set again after some delay.
305 resched_mask
= raw_cpu_read(rcu_sched_qs_mask
);
306 raw_cpu_write(rcu_sched_qs_mask
, 0);
308 /* Find the flavor that needs a quiescent state. */
309 for_each_rcu_flavor(rsp
) {
310 rdp
= raw_cpu_ptr(rsp
->rda
);
311 if (!(resched_mask
& rsp
->flavor_mask
))
313 smp_mb(); /* rcu_sched_qs_mask before cond_resched_completed. */
314 if (READ_ONCE(rdp
->mynode
->completed
) !=
315 READ_ONCE(rdp
->cond_resched_completed
))
319 * Pretend to be momentarily idle for the quiescent state.
320 * This allows the grace-period kthread to record the
321 * quiescent state, with no need for this CPU to do anything
324 rdtp
= this_cpu_ptr(&rcu_dynticks
);
325 smp_mb__before_atomic(); /* Earlier stuff before QS. */
326 atomic_add(2, &rdtp
->dynticks
); /* QS. */
327 smp_mb__after_atomic(); /* Later stuff after QS. */
330 local_irq_restore(flags
);
334 * Note a context switch. This is a quiescent state for RCU-sched,
335 * and requires special handling for preemptible RCU.
336 * The caller must have disabled preemption.
338 void rcu_note_context_switch(void)
340 trace_rcu_utilization(TPS("Start context switch"));
342 rcu_preempt_note_context_switch();
343 if (unlikely(raw_cpu_read(rcu_sched_qs_mask
)))
344 rcu_momentary_dyntick_idle();
345 trace_rcu_utilization(TPS("End context switch"));
347 EXPORT_SYMBOL_GPL(rcu_note_context_switch
);
350 * Register a quiescent state for all RCU flavors. If there is an
351 * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
352 * dyntick-idle quiescent state visible to other CPUs (but only for those
353 * RCU flavors in desperate need of a quiescent state, which will normally
354 * be none of them). Either way, do a lightweight quiescent state for
357 void rcu_all_qs(void)
359 if (unlikely(raw_cpu_read(rcu_sched_qs_mask
)))
360 rcu_momentary_dyntick_idle();
361 this_cpu_inc(rcu_qs_ctr
);
363 EXPORT_SYMBOL_GPL(rcu_all_qs
);
365 static long blimit
= 10; /* Maximum callbacks per rcu_do_batch. */
366 static long qhimark
= 10000; /* If this many pending, ignore blimit. */
367 static long qlowmark
= 100; /* Once only this many pending, use blimit. */
369 module_param(blimit
, long, 0444);
370 module_param(qhimark
, long, 0444);
371 module_param(qlowmark
, long, 0444);
373 static ulong jiffies_till_first_fqs
= ULONG_MAX
;
374 static ulong jiffies_till_next_fqs
= ULONG_MAX
;
376 module_param(jiffies_till_first_fqs
, ulong
, 0644);
377 module_param(jiffies_till_next_fqs
, ulong
, 0644);
380 * How long the grace period must be before we start recruiting
381 * quiescent-state help from rcu_note_context_switch().
383 static ulong jiffies_till_sched_qs
= HZ
/ 20;
384 module_param(jiffies_till_sched_qs
, ulong
, 0644);
386 static bool rcu_start_gp_advanced(struct rcu_state
*rsp
, struct rcu_node
*rnp
,
387 struct rcu_data
*rdp
);
388 static void force_qs_rnp(struct rcu_state
*rsp
,
389 int (*f
)(struct rcu_data
*rsp
, bool *isidle
,
390 unsigned long *maxj
),
391 bool *isidle
, unsigned long *maxj
);
392 static void force_quiescent_state(struct rcu_state
*rsp
);
393 static int rcu_pending(void);
396 * Return the number of RCU batches started thus far for debug & stats.
398 unsigned long rcu_batches_started(void)
400 return rcu_state_p
->gpnum
;
402 EXPORT_SYMBOL_GPL(rcu_batches_started
);
405 * Return the number of RCU-sched batches started thus far for debug & stats.
407 unsigned long rcu_batches_started_sched(void)
409 return rcu_sched_state
.gpnum
;
411 EXPORT_SYMBOL_GPL(rcu_batches_started_sched
);
414 * Return the number of RCU BH batches started thus far for debug & stats.
416 unsigned long rcu_batches_started_bh(void)
418 return rcu_bh_state
.gpnum
;
420 EXPORT_SYMBOL_GPL(rcu_batches_started_bh
);
423 * Return the number of RCU batches completed thus far for debug & stats.
425 unsigned long rcu_batches_completed(void)
427 return rcu_state_p
->completed
;
429 EXPORT_SYMBOL_GPL(rcu_batches_completed
);
432 * Return the number of RCU-sched batches completed thus far for debug & stats.
434 unsigned long rcu_batches_completed_sched(void)
436 return rcu_sched_state
.completed
;
438 EXPORT_SYMBOL_GPL(rcu_batches_completed_sched
);
441 * Return the number of RCU BH batches completed thus far for debug & stats.
443 unsigned long rcu_batches_completed_bh(void)
445 return rcu_bh_state
.completed
;
447 EXPORT_SYMBOL_GPL(rcu_batches_completed_bh
);
450 * Force a quiescent state.
452 void rcu_force_quiescent_state(void)
454 force_quiescent_state(rcu_state_p
);
456 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state
);
459 * Force a quiescent state for RCU BH.
461 void rcu_bh_force_quiescent_state(void)
463 force_quiescent_state(&rcu_bh_state
);
465 EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state
);
468 * Force a quiescent state for RCU-sched.
470 void rcu_sched_force_quiescent_state(void)
472 force_quiescent_state(&rcu_sched_state
);
474 EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state
);
477 * Show the state of the grace-period kthreads.
479 void show_rcu_gp_kthreads(void)
481 struct rcu_state
*rsp
;
483 for_each_rcu_flavor(rsp
) {
484 pr_info("%s: wait state: %d ->state: %#lx\n",
485 rsp
->name
, rsp
->gp_state
, rsp
->gp_kthread
->state
);
486 /* sched_show_task(rsp->gp_kthread); */
489 EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads
);
492 * Record the number of times rcutorture tests have been initiated and
493 * terminated. This information allows the debugfs tracing stats to be
494 * correlated to the rcutorture messages, even when the rcutorture module
495 * is being repeatedly loaded and unloaded. In other words, we cannot
496 * store this state in rcutorture itself.
498 void rcutorture_record_test_transition(void)
500 rcutorture_testseq
++;
501 rcutorture_vernum
= 0;
503 EXPORT_SYMBOL_GPL(rcutorture_record_test_transition
);
506 * Send along grace-period-related data for rcutorture diagnostics.
508 void rcutorture_get_gp_data(enum rcutorture_type test_type
, int *flags
,
509 unsigned long *gpnum
, unsigned long *completed
)
511 struct rcu_state
*rsp
= NULL
;
520 case RCU_SCHED_FLAVOR
:
521 rsp
= &rcu_sched_state
;
527 *flags
= READ_ONCE(rsp
->gp_flags
);
528 *gpnum
= READ_ONCE(rsp
->gpnum
);
529 *completed
= READ_ONCE(rsp
->completed
);
536 EXPORT_SYMBOL_GPL(rcutorture_get_gp_data
);
539 * Record the number of writer passes through the current rcutorture test.
540 * This is also used to correlate debugfs tracing stats with the rcutorture
543 void rcutorture_record_progress(unsigned long vernum
)
547 EXPORT_SYMBOL_GPL(rcutorture_record_progress
);
550 * Does the CPU have callbacks ready to be invoked?
553 cpu_has_callbacks_ready_to_invoke(struct rcu_data
*rdp
)
555 return &rdp
->nxtlist
!= rdp
->nxttail
[RCU_DONE_TAIL
] &&
556 rdp
->nxttail
[RCU_DONE_TAIL
] != NULL
;
560 * Return the root node of the specified rcu_state structure.
562 static struct rcu_node
*rcu_get_root(struct rcu_state
*rsp
)
564 return &rsp
->node
[0];
568 * Is there any need for future grace periods?
569 * Interrupts must be disabled. If the caller does not hold the root
570 * rnp_node structure's ->lock, the results are advisory only.
572 static int rcu_future_needs_gp(struct rcu_state
*rsp
)
574 struct rcu_node
*rnp
= rcu_get_root(rsp
);
575 int idx
= (READ_ONCE(rnp
->completed
) + 1) & 0x1;
576 int *fp
= &rnp
->need_future_gp
[idx
];
578 return READ_ONCE(*fp
);
582 * Does the current CPU require a not-yet-started grace period?
583 * The caller must have disabled interrupts to prevent races with
584 * normal callback registry.
587 cpu_needs_another_gp(struct rcu_state
*rsp
, struct rcu_data
*rdp
)
591 if (rcu_gp_in_progress(rsp
))
592 return 0; /* No, a grace period is already in progress. */
593 if (rcu_future_needs_gp(rsp
))
594 return 1; /* Yes, a no-CBs CPU needs one. */
595 if (!rdp
->nxttail
[RCU_NEXT_TAIL
])
596 return 0; /* No, this is a no-CBs (or offline) CPU. */
597 if (*rdp
->nxttail
[RCU_NEXT_READY_TAIL
])
598 return 1; /* Yes, this CPU has newly registered callbacks. */
599 for (i
= RCU_WAIT_TAIL
; i
< RCU_NEXT_TAIL
; i
++)
600 if (rdp
->nxttail
[i
- 1] != rdp
->nxttail
[i
] &&
601 ULONG_CMP_LT(READ_ONCE(rsp
->completed
),
602 rdp
->nxtcompleted
[i
]))
603 return 1; /* Yes, CBs for future grace period. */
604 return 0; /* No grace period needed. */
608 * rcu_eqs_enter_common - current CPU is moving towards extended quiescent state
610 * If the new value of the ->dynticks_nesting counter now is zero,
611 * we really have entered idle, and must do the appropriate accounting.
612 * The caller must have disabled interrupts.
614 static void rcu_eqs_enter_common(long long oldval
, bool user
)
616 struct rcu_state
*rsp
;
617 struct rcu_data
*rdp
;
618 struct rcu_dynticks
*rdtp
= this_cpu_ptr(&rcu_dynticks
);
620 trace_rcu_dyntick(TPS("Start"), oldval
, rdtp
->dynticks_nesting
);
621 if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG
) &&
622 !user
&& !is_idle_task(current
)) {
623 struct task_struct
*idle __maybe_unused
=
624 idle_task(smp_processor_id());
626 trace_rcu_dyntick(TPS("Error on entry: not idle task"), oldval
, 0);
627 ftrace_dump(DUMP_ORIG
);
628 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
629 current
->pid
, current
->comm
,
630 idle
->pid
, idle
->comm
); /* must be idle task! */
632 for_each_rcu_flavor(rsp
) {
633 rdp
= this_cpu_ptr(rsp
->rda
);
634 do_nocb_deferred_wakeup(rdp
);
636 rcu_prepare_for_idle();
637 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
638 smp_mb__before_atomic(); /* See above. */
639 atomic_inc(&rdtp
->dynticks
);
640 smp_mb__after_atomic(); /* Force ordering with next sojourn. */
641 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG
) &&
642 atomic_read(&rdtp
->dynticks
) & 0x1);
643 rcu_dynticks_task_enter();
646 * It is illegal to enter an extended quiescent state while
647 * in an RCU read-side critical section.
649 RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map
),
650 "Illegal idle entry in RCU read-side critical section.");
651 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map
),
652 "Illegal idle entry in RCU-bh read-side critical section.");
653 RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map
),
654 "Illegal idle entry in RCU-sched read-side critical section.");
658 * Enter an RCU extended quiescent state, which can be either the
659 * idle loop or adaptive-tickless usermode execution.
661 static void rcu_eqs_enter(bool user
)
664 struct rcu_dynticks
*rdtp
;
666 rdtp
= this_cpu_ptr(&rcu_dynticks
);
667 oldval
= rdtp
->dynticks_nesting
;
668 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG
) &&
669 (oldval
& DYNTICK_TASK_NEST_MASK
) == 0);
670 if ((oldval
& DYNTICK_TASK_NEST_MASK
) == DYNTICK_TASK_NEST_VALUE
) {
671 rdtp
->dynticks_nesting
= 0;
672 rcu_eqs_enter_common(oldval
, user
);
674 rdtp
->dynticks_nesting
-= DYNTICK_TASK_NEST_VALUE
;
679 * rcu_idle_enter - inform RCU that current CPU is entering idle
681 * Enter idle mode, in other words, -leave- the mode in which RCU
682 * read-side critical sections can occur. (Though RCU read-side
683 * critical sections can occur in irq handlers in idle, a possibility
684 * handled by irq_enter() and irq_exit().)
686 * We crowbar the ->dynticks_nesting field to zero to allow for
687 * the possibility of usermode upcalls having messed up our count
688 * of interrupt nesting level during the prior busy period.
690 void rcu_idle_enter(void)
694 local_irq_save(flags
);
695 rcu_eqs_enter(false);
696 rcu_sysidle_enter(0);
697 local_irq_restore(flags
);
699 EXPORT_SYMBOL_GPL(rcu_idle_enter
);
701 #ifdef CONFIG_NO_HZ_FULL
703 * rcu_user_enter - inform RCU that we are resuming userspace.
705 * Enter RCU idle mode right before resuming userspace. No use of RCU
706 * is permitted between this call and rcu_user_exit(). This way the
707 * CPU doesn't need to maintain the tick for RCU maintenance purposes
708 * when the CPU runs in userspace.
710 void rcu_user_enter(void)
714 #endif /* CONFIG_NO_HZ_FULL */
717 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
719 * Exit from an interrupt handler, which might possibly result in entering
720 * idle mode, in other words, leaving the mode in which read-side critical
721 * sections can occur.
723 * This code assumes that the idle loop never does anything that might
724 * result in unbalanced calls to irq_enter() and irq_exit(). If your
725 * architecture violates this assumption, RCU will give you what you
726 * deserve, good and hard. But very infrequently and irreproducibly.
728 * Use things like work queues to work around this limitation.
730 * You have been warned.
732 void rcu_irq_exit(void)
736 struct rcu_dynticks
*rdtp
;
738 local_irq_save(flags
);
739 rdtp
= this_cpu_ptr(&rcu_dynticks
);
740 oldval
= rdtp
->dynticks_nesting
;
741 rdtp
->dynticks_nesting
--;
742 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG
) &&
743 rdtp
->dynticks_nesting
< 0);
744 if (rdtp
->dynticks_nesting
)
745 trace_rcu_dyntick(TPS("--="), oldval
, rdtp
->dynticks_nesting
);
747 rcu_eqs_enter_common(oldval
, true);
748 rcu_sysidle_enter(1);
749 local_irq_restore(flags
);
753 * rcu_eqs_exit_common - current CPU moving away from extended quiescent state
755 * If the new value of the ->dynticks_nesting counter was previously zero,
756 * we really have exited idle, and must do the appropriate accounting.
757 * The caller must have disabled interrupts.
759 static void rcu_eqs_exit_common(long long oldval
, int user
)
761 struct rcu_dynticks
*rdtp
= this_cpu_ptr(&rcu_dynticks
);
763 rcu_dynticks_task_exit();
764 smp_mb__before_atomic(); /* Force ordering w/previous sojourn. */
765 atomic_inc(&rdtp
->dynticks
);
766 /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
767 smp_mb__after_atomic(); /* See above. */
768 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG
) &&
769 !(atomic_read(&rdtp
->dynticks
) & 0x1));
770 rcu_cleanup_after_idle();
771 trace_rcu_dyntick(TPS("End"), oldval
, rdtp
->dynticks_nesting
);
772 if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG
) &&
773 !user
&& !is_idle_task(current
)) {
774 struct task_struct
*idle __maybe_unused
=
775 idle_task(smp_processor_id());
777 trace_rcu_dyntick(TPS("Error on exit: not idle task"),
778 oldval
, rdtp
->dynticks_nesting
);
779 ftrace_dump(DUMP_ORIG
);
780 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
781 current
->pid
, current
->comm
,
782 idle
->pid
, idle
->comm
); /* must be idle task! */
787 * Exit an RCU extended quiescent state, which can be either the
788 * idle loop or adaptive-tickless usermode execution.
790 static void rcu_eqs_exit(bool user
)
792 struct rcu_dynticks
*rdtp
;
795 rdtp
= this_cpu_ptr(&rcu_dynticks
);
796 oldval
= rdtp
->dynticks_nesting
;
797 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG
) && oldval
< 0);
798 if (oldval
& DYNTICK_TASK_NEST_MASK
) {
799 rdtp
->dynticks_nesting
+= DYNTICK_TASK_NEST_VALUE
;
801 rdtp
->dynticks_nesting
= DYNTICK_TASK_EXIT_IDLE
;
802 rcu_eqs_exit_common(oldval
, user
);
807 * rcu_idle_exit - inform RCU that current CPU is leaving idle
809 * Exit idle mode, in other words, -enter- the mode in which RCU
810 * read-side critical sections can occur.
812 * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to
813 * allow for the possibility of usermode upcalls messing up our count
814 * of interrupt nesting level during the busy period that is just
817 void rcu_idle_exit(void)
821 local_irq_save(flags
);
824 local_irq_restore(flags
);
826 EXPORT_SYMBOL_GPL(rcu_idle_exit
);
828 #ifdef CONFIG_NO_HZ_FULL
830 * rcu_user_exit - inform RCU that we are exiting userspace.
832 * Exit RCU idle mode while entering the kernel because it can
833 * run a RCU read side critical section anytime.
835 void rcu_user_exit(void)
839 #endif /* CONFIG_NO_HZ_FULL */
842 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
844 * Enter an interrupt handler, which might possibly result in exiting
845 * idle mode, in other words, entering the mode in which read-side critical
846 * sections can occur.
848 * Note that the Linux kernel is fully capable of entering an interrupt
849 * handler that it never exits, for example when doing upcalls to
850 * user mode! This code assumes that the idle loop never does upcalls to
851 * user mode. If your architecture does do upcalls from the idle loop (or
852 * does anything else that results in unbalanced calls to the irq_enter()
853 * and irq_exit() functions), RCU will give you what you deserve, good
854 * and hard. But very infrequently and irreproducibly.
856 * Use things like work queues to work around this limitation.
858 * You have been warned.
860 void rcu_irq_enter(void)
863 struct rcu_dynticks
*rdtp
;
866 local_irq_save(flags
);
867 rdtp
= this_cpu_ptr(&rcu_dynticks
);
868 oldval
= rdtp
->dynticks_nesting
;
869 rdtp
->dynticks_nesting
++;
870 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG
) &&
871 rdtp
->dynticks_nesting
== 0);
873 trace_rcu_dyntick(TPS("++="), oldval
, rdtp
->dynticks_nesting
);
875 rcu_eqs_exit_common(oldval
, true);
877 local_irq_restore(flags
);
881 * rcu_nmi_enter - inform RCU of entry to NMI context
883 * If the CPU was idle from RCU's viewpoint, update rdtp->dynticks and
884 * rdtp->dynticks_nmi_nesting to let the RCU grace-period handling know
885 * that the CPU is active. This implementation permits nested NMIs, as
886 * long as the nesting level does not overflow an int. (You will probably
887 * run out of stack space first.)
889 void rcu_nmi_enter(void)
891 struct rcu_dynticks
*rdtp
= this_cpu_ptr(&rcu_dynticks
);
894 /* Complain about underflow. */
895 WARN_ON_ONCE(rdtp
->dynticks_nmi_nesting
< 0);
898 * If idle from RCU viewpoint, atomically increment ->dynticks
899 * to mark non-idle and increment ->dynticks_nmi_nesting by one.
900 * Otherwise, increment ->dynticks_nmi_nesting by two. This means
901 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
902 * to be in the outermost NMI handler that interrupted an RCU-idle
903 * period (observation due to Andy Lutomirski).
905 if (!(atomic_read(&rdtp
->dynticks
) & 0x1)) {
906 smp_mb__before_atomic(); /* Force delay from prior write. */
907 atomic_inc(&rdtp
->dynticks
);
908 /* atomic_inc() before later RCU read-side crit sects */
909 smp_mb__after_atomic(); /* See above. */
910 WARN_ON_ONCE(!(atomic_read(&rdtp
->dynticks
) & 0x1));
913 rdtp
->dynticks_nmi_nesting
+= incby
;
918 * rcu_nmi_exit - inform RCU of exit from NMI context
920 * If we are returning from the outermost NMI handler that interrupted an
921 * RCU-idle period, update rdtp->dynticks and rdtp->dynticks_nmi_nesting
922 * to let the RCU grace-period handling know that the CPU is back to
925 void rcu_nmi_exit(void)
927 struct rcu_dynticks
*rdtp
= this_cpu_ptr(&rcu_dynticks
);
930 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
931 * (We are exiting an NMI handler, so RCU better be paying attention
934 WARN_ON_ONCE(rdtp
->dynticks_nmi_nesting
<= 0);
935 WARN_ON_ONCE(!(atomic_read(&rdtp
->dynticks
) & 0x1));
938 * If the nesting level is not 1, the CPU wasn't RCU-idle, so
939 * leave it in non-RCU-idle state.
941 if (rdtp
->dynticks_nmi_nesting
!= 1) {
942 rdtp
->dynticks_nmi_nesting
-= 2;
946 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
947 rdtp
->dynticks_nmi_nesting
= 0;
948 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
949 smp_mb__before_atomic(); /* See above. */
950 atomic_inc(&rdtp
->dynticks
);
951 smp_mb__after_atomic(); /* Force delay to next write. */
952 WARN_ON_ONCE(atomic_read(&rdtp
->dynticks
) & 0x1);
956 * __rcu_is_watching - are RCU read-side critical sections safe?
958 * Return true if RCU is watching the running CPU, which means that
959 * this CPU can safely enter RCU read-side critical sections. Unlike
960 * rcu_is_watching(), the caller of __rcu_is_watching() must have at
961 * least disabled preemption.
963 bool notrace
__rcu_is_watching(void)
965 return atomic_read(this_cpu_ptr(&rcu_dynticks
.dynticks
)) & 0x1;
969 * rcu_is_watching - see if RCU thinks that the current CPU is idle
971 * If the current CPU is in its idle loop and is neither in an interrupt
972 * or NMI handler, return true.
974 bool notrace
rcu_is_watching(void)
978 preempt_disable_notrace();
979 ret
= __rcu_is_watching();
980 preempt_enable_notrace();
983 EXPORT_SYMBOL_GPL(rcu_is_watching
);
985 #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
988 * Is the current CPU online? Disable preemption to avoid false positives
989 * that could otherwise happen due to the current CPU number being sampled,
990 * this task being preempted, its old CPU being taken offline, resuming
991 * on some other CPU, then determining that its old CPU is now offline.
992 * It is OK to use RCU on an offline processor during initial boot, hence
993 * the check for rcu_scheduler_fully_active. Note also that it is OK
994 * for a CPU coming online to use RCU for one jiffy prior to marking itself
995 * online in the cpu_online_mask. Similarly, it is OK for a CPU going
996 * offline to continue to use RCU for one jiffy after marking itself
997 * offline in the cpu_online_mask. This leniency is necessary given the
998 * non-atomic nature of the online and offline processing, for example,
999 * the fact that a CPU enters the scheduler after completing the CPU_DYING
1002 * This is also why RCU internally marks CPUs online during the
1003 * CPU_UP_PREPARE phase and offline during the CPU_DEAD phase.
1005 * Disable checking if in an NMI handler because we cannot safely report
1006 * errors from NMI handlers anyway.
1008 bool rcu_lockdep_current_cpu_online(void)
1010 struct rcu_data
*rdp
;
1011 struct rcu_node
*rnp
;
1017 rdp
= this_cpu_ptr(&rcu_sched_data
);
1019 ret
= (rdp
->grpmask
& rcu_rnp_online_cpus(rnp
)) ||
1020 !rcu_scheduler_fully_active
;
1024 EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online
);
1026 #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
1029 * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
1031 * If the current CPU is idle or running at a first-level (not nested)
1032 * interrupt from idle, return true. The caller must have at least
1033 * disabled preemption.
1035 static int rcu_is_cpu_rrupt_from_idle(void)
1037 return __this_cpu_read(rcu_dynticks
.dynticks_nesting
) <= 1;
1041 * Snapshot the specified CPU's dynticks counter so that we can later
1042 * credit them with an implicit quiescent state. Return 1 if this CPU
1043 * is in dynticks idle mode, which is an extended quiescent state.
1045 static int dyntick_save_progress_counter(struct rcu_data
*rdp
,
1046 bool *isidle
, unsigned long *maxj
)
1048 rdp
->dynticks_snap
= atomic_add_return(0, &rdp
->dynticks
->dynticks
);
1049 rcu_sysidle_check_cpu(rdp
, isidle
, maxj
);
1050 if ((rdp
->dynticks_snap
& 0x1) == 0) {
1051 trace_rcu_fqs(rdp
->rsp
->name
, rdp
->gpnum
, rdp
->cpu
, TPS("dti"));
1054 if (ULONG_CMP_LT(READ_ONCE(rdp
->gpnum
) + ULONG_MAX
/ 4,
1055 rdp
->mynode
->gpnum
))
1056 WRITE_ONCE(rdp
->gpwrap
, true);
1062 * Return true if the specified CPU has passed through a quiescent
1063 * state by virtue of being in or having passed through an dynticks
1064 * idle state since the last call to dyntick_save_progress_counter()
1065 * for this same CPU, or by virtue of having been offline.
1067 static int rcu_implicit_dynticks_qs(struct rcu_data
*rdp
,
1068 bool *isidle
, unsigned long *maxj
)
1074 curr
= (unsigned int)atomic_add_return(0, &rdp
->dynticks
->dynticks
);
1075 snap
= (unsigned int)rdp
->dynticks_snap
;
1078 * If the CPU passed through or entered a dynticks idle phase with
1079 * no active irq/NMI handlers, then we can safely pretend that the CPU
1080 * already acknowledged the request to pass through a quiescent
1081 * state. Either way, that CPU cannot possibly be in an RCU
1082 * read-side critical section that started before the beginning
1083 * of the current RCU grace period.
1085 if ((curr
& 0x1) == 0 || UINT_CMP_GE(curr
, snap
+ 2)) {
1086 trace_rcu_fqs(rdp
->rsp
->name
, rdp
->gpnum
, rdp
->cpu
, TPS("dti"));
1087 rdp
->dynticks_fqs
++;
1092 * Check for the CPU being offline, but only if the grace period
1093 * is old enough. We don't need to worry about the CPU changing
1094 * state: If we see it offline even once, it has been through a
1097 * The reason for insisting that the grace period be at least
1098 * one jiffy old is that CPUs that are not quite online and that
1099 * have just gone offline can still execute RCU read-side critical
1102 if (ULONG_CMP_GE(rdp
->rsp
->gp_start
+ 2, jiffies
))
1103 return 0; /* Grace period is not old enough. */
1105 if (cpu_is_offline(rdp
->cpu
)) {
1106 trace_rcu_fqs(rdp
->rsp
->name
, rdp
->gpnum
, rdp
->cpu
, TPS("ofl"));
1112 * A CPU running for an extended time within the kernel can
1113 * delay RCU grace periods. When the CPU is in NO_HZ_FULL mode,
1114 * even context-switching back and forth between a pair of
1115 * in-kernel CPU-bound tasks cannot advance grace periods.
1116 * So if the grace period is old enough, make the CPU pay attention.
1117 * Note that the unsynchronized assignments to the per-CPU
1118 * rcu_sched_qs_mask variable are safe. Yes, setting of
1119 * bits can be lost, but they will be set again on the next
1120 * force-quiescent-state pass. So lost bit sets do not result
1121 * in incorrect behavior, merely in a grace period lasting
1122 * a few jiffies longer than it might otherwise. Because
1123 * there are at most four threads involved, and because the
1124 * updates are only once every few jiffies, the probability of
1125 * lossage (and thus of slight grace-period extension) is
1128 * Note that if the jiffies_till_sched_qs boot/sysfs parameter
1129 * is set too high, we override with half of the RCU CPU stall
1132 rcrmp
= &per_cpu(rcu_sched_qs_mask
, rdp
->cpu
);
1133 if (ULONG_CMP_GE(jiffies
,
1134 rdp
->rsp
->gp_start
+ jiffies_till_sched_qs
) ||
1135 ULONG_CMP_GE(jiffies
, rdp
->rsp
->jiffies_resched
)) {
1136 if (!(READ_ONCE(*rcrmp
) & rdp
->rsp
->flavor_mask
)) {
1137 WRITE_ONCE(rdp
->cond_resched_completed
,
1138 READ_ONCE(rdp
->mynode
->completed
));
1139 smp_mb(); /* ->cond_resched_completed before *rcrmp. */
1141 READ_ONCE(*rcrmp
) + rdp
->rsp
->flavor_mask
);
1142 resched_cpu(rdp
->cpu
); /* Force CPU into scheduler. */
1143 rdp
->rsp
->jiffies_resched
+= 5; /* Enable beating. */
1144 } else if (ULONG_CMP_GE(jiffies
, rdp
->rsp
->jiffies_resched
)) {
1145 /* Time to beat on that CPU again! */
1146 resched_cpu(rdp
->cpu
); /* Force CPU into scheduler. */
1147 rdp
->rsp
->jiffies_resched
+= 5; /* Re-enable beating. */
1154 static void record_gp_stall_check_time(struct rcu_state
*rsp
)
1156 unsigned long j
= jiffies
;
1160 smp_wmb(); /* Record start time before stall time. */
1161 j1
= rcu_jiffies_till_stall_check();
1162 WRITE_ONCE(rsp
->jiffies_stall
, j
+ j1
);
1163 rsp
->jiffies_resched
= j
+ j1
/ 2;
1164 rsp
->n_force_qs_gpstart
= READ_ONCE(rsp
->n_force_qs
);
1168 * Complain about starvation of grace-period kthread.
1170 static void rcu_check_gp_kthread_starvation(struct rcu_state
*rsp
)
1176 gpa
= READ_ONCE(rsp
->gp_activity
);
1177 if (j
- gpa
> 2 * HZ
)
1178 pr_err("%s kthread starved for %ld jiffies! g%lu c%lu f%#x s%d ->state=%#lx\n",
1180 rsp
->gpnum
, rsp
->completed
,
1181 rsp
->gp_flags
, rsp
->gp_state
,
1182 rsp
->gp_kthread
? rsp
->gp_kthread
->state
: 0);
1186 * Dump stacks of all tasks running on stalled CPUs.
1188 static void rcu_dump_cpu_stacks(struct rcu_state
*rsp
)
1191 unsigned long flags
;
1192 struct rcu_node
*rnp
;
1194 rcu_for_each_leaf_node(rsp
, rnp
) {
1195 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
1196 if (rnp
->qsmask
!= 0) {
1197 for (cpu
= 0; cpu
<= rnp
->grphi
- rnp
->grplo
; cpu
++)
1198 if (rnp
->qsmask
& (1UL << cpu
))
1199 dump_cpu_task(rnp
->grplo
+ cpu
);
1201 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
1205 static void print_other_cpu_stall(struct rcu_state
*rsp
, unsigned long gpnum
)
1209 unsigned long flags
;
1213 struct rcu_node
*rnp
= rcu_get_root(rsp
);
1216 /* Only let one CPU complain about others per time interval. */
1218 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
1219 delta
= jiffies
- READ_ONCE(rsp
->jiffies_stall
);
1220 if (delta
< RCU_STALL_RAT_DELAY
|| !rcu_gp_in_progress(rsp
)) {
1221 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
1224 WRITE_ONCE(rsp
->jiffies_stall
,
1225 jiffies
+ 3 * rcu_jiffies_till_stall_check() + 3);
1226 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
1229 * OK, time to rat on our buddy...
1230 * See Documentation/RCU/stallwarn.txt for info on how to debug
1231 * RCU CPU stall warnings.
1233 pr_err("INFO: %s detected stalls on CPUs/tasks:",
1235 print_cpu_stall_info_begin();
1236 rcu_for_each_leaf_node(rsp
, rnp
) {
1237 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
1238 ndetected
+= rcu_print_task_stall(rnp
);
1239 if (rnp
->qsmask
!= 0) {
1240 for (cpu
= 0; cpu
<= rnp
->grphi
- rnp
->grplo
; cpu
++)
1241 if (rnp
->qsmask
& (1UL << cpu
)) {
1242 print_cpu_stall_info(rsp
,
1247 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
1250 print_cpu_stall_info_end();
1251 for_each_possible_cpu(cpu
)
1252 totqlen
+= per_cpu_ptr(rsp
->rda
, cpu
)->qlen
;
1253 pr_cont("(detected by %d, t=%ld jiffies, g=%ld, c=%ld, q=%lu)\n",
1254 smp_processor_id(), (long)(jiffies
- rsp
->gp_start
),
1255 (long)rsp
->gpnum
, (long)rsp
->completed
, totqlen
);
1257 rcu_dump_cpu_stacks(rsp
);
1259 if (READ_ONCE(rsp
->gpnum
) != gpnum
||
1260 READ_ONCE(rsp
->completed
) == gpnum
) {
1261 pr_err("INFO: Stall ended before state dump start\n");
1264 gpa
= READ_ONCE(rsp
->gp_activity
);
1265 pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
1266 rsp
->name
, j
- gpa
, j
, gpa
,
1267 jiffies_till_next_fqs
,
1268 rcu_get_root(rsp
)->qsmask
);
1269 /* In this case, the current CPU might be at fault. */
1270 sched_show_task(current
);
1274 /* Complain about tasks blocking the grace period. */
1275 rcu_print_detail_task_stall(rsp
);
1277 rcu_check_gp_kthread_starvation(rsp
);
1279 force_quiescent_state(rsp
); /* Kick them all. */
1282 static void print_cpu_stall(struct rcu_state
*rsp
)
1285 unsigned long flags
;
1286 struct rcu_node
*rnp
= rcu_get_root(rsp
);
1290 * OK, time to rat on ourselves...
1291 * See Documentation/RCU/stallwarn.txt for info on how to debug
1292 * RCU CPU stall warnings.
1294 pr_err("INFO: %s self-detected stall on CPU", rsp
->name
);
1295 print_cpu_stall_info_begin();
1296 print_cpu_stall_info(rsp
, smp_processor_id());
1297 print_cpu_stall_info_end();
1298 for_each_possible_cpu(cpu
)
1299 totqlen
+= per_cpu_ptr(rsp
->rda
, cpu
)->qlen
;
1300 pr_cont(" (t=%lu jiffies g=%ld c=%ld q=%lu)\n",
1301 jiffies
- rsp
->gp_start
,
1302 (long)rsp
->gpnum
, (long)rsp
->completed
, totqlen
);
1304 rcu_check_gp_kthread_starvation(rsp
);
1306 rcu_dump_cpu_stacks(rsp
);
1308 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
1309 if (ULONG_CMP_GE(jiffies
, READ_ONCE(rsp
->jiffies_stall
)))
1310 WRITE_ONCE(rsp
->jiffies_stall
,
1311 jiffies
+ 3 * rcu_jiffies_till_stall_check() + 3);
1312 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
1315 * Attempt to revive the RCU machinery by forcing a context switch.
1317 * A context switch would normally allow the RCU state machine to make
1318 * progress and it could be we're stuck in kernel space without context
1319 * switches for an entirely unreasonable amount of time.
1321 resched_cpu(smp_processor_id());
1324 static void check_cpu_stall(struct rcu_state
*rsp
, struct rcu_data
*rdp
)
1326 unsigned long completed
;
1327 unsigned long gpnum
;
1331 struct rcu_node
*rnp
;
1333 if (rcu_cpu_stall_suppress
|| !rcu_gp_in_progress(rsp
))
1338 * Lots of memory barriers to reject false positives.
1340 * The idea is to pick up rsp->gpnum, then rsp->jiffies_stall,
1341 * then rsp->gp_start, and finally rsp->completed. These values
1342 * are updated in the opposite order with memory barriers (or
1343 * equivalent) during grace-period initialization and cleanup.
1344 * Now, a false positive can occur if we get an new value of
1345 * rsp->gp_start and a old value of rsp->jiffies_stall. But given
1346 * the memory barriers, the only way that this can happen is if one
1347 * grace period ends and another starts between these two fetches.
1348 * Detect this by comparing rsp->completed with the previous fetch
1351 * Given this check, comparisons of jiffies, rsp->jiffies_stall,
1352 * and rsp->gp_start suffice to forestall false positives.
1354 gpnum
= READ_ONCE(rsp
->gpnum
);
1355 smp_rmb(); /* Pick up ->gpnum first... */
1356 js
= READ_ONCE(rsp
->jiffies_stall
);
1357 smp_rmb(); /* ...then ->jiffies_stall before the rest... */
1358 gps
= READ_ONCE(rsp
->gp_start
);
1359 smp_rmb(); /* ...and finally ->gp_start before ->completed. */
1360 completed
= READ_ONCE(rsp
->completed
);
1361 if (ULONG_CMP_GE(completed
, gpnum
) ||
1362 ULONG_CMP_LT(j
, js
) ||
1363 ULONG_CMP_GE(gps
, js
))
1364 return; /* No stall or GP completed since entering function. */
1366 if (rcu_gp_in_progress(rsp
) &&
1367 (READ_ONCE(rnp
->qsmask
) & rdp
->grpmask
)) {
1369 /* We haven't checked in, so go dump stack. */
1370 print_cpu_stall(rsp
);
1372 } else if (rcu_gp_in_progress(rsp
) &&
1373 ULONG_CMP_GE(j
, js
+ RCU_STALL_RAT_DELAY
)) {
1375 /* They had a few time units to dump stack, so complain. */
1376 print_other_cpu_stall(rsp
, gpnum
);
1381 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
1383 * Set the stall-warning timeout way off into the future, thus preventing
1384 * any RCU CPU stall-warning messages from appearing in the current set of
1385 * RCU grace periods.
1387 * The caller must disable hard irqs.
1389 void rcu_cpu_stall_reset(void)
1391 struct rcu_state
*rsp
;
1393 for_each_rcu_flavor(rsp
)
1394 WRITE_ONCE(rsp
->jiffies_stall
, jiffies
+ ULONG_MAX
/ 2);
1398 * Initialize the specified rcu_data structure's default callback list
1399 * to empty. The default callback list is the one that is not used by
1400 * no-callbacks CPUs.
1402 static void init_default_callback_list(struct rcu_data
*rdp
)
1406 rdp
->nxtlist
= NULL
;
1407 for (i
= 0; i
< RCU_NEXT_SIZE
; i
++)
1408 rdp
->nxttail
[i
] = &rdp
->nxtlist
;
1412 * Initialize the specified rcu_data structure's callback list to empty.
1414 static void init_callback_list(struct rcu_data
*rdp
)
1416 if (init_nocb_callback_list(rdp
))
1418 init_default_callback_list(rdp
);
1422 * Determine the value that ->completed will have at the end of the
1423 * next subsequent grace period. This is used to tag callbacks so that
1424 * a CPU can invoke callbacks in a timely fashion even if that CPU has
1425 * been dyntick-idle for an extended period with callbacks under the
1426 * influence of RCU_FAST_NO_HZ.
1428 * The caller must hold rnp->lock with interrupts disabled.
1430 static unsigned long rcu_cbs_completed(struct rcu_state
*rsp
,
1431 struct rcu_node
*rnp
)
1434 * If RCU is idle, we just wait for the next grace period.
1435 * But we can only be sure that RCU is idle if we are looking
1436 * at the root rcu_node structure -- otherwise, a new grace
1437 * period might have started, but just not yet gotten around
1438 * to initializing the current non-root rcu_node structure.
1440 if (rcu_get_root(rsp
) == rnp
&& rnp
->gpnum
== rnp
->completed
)
1441 return rnp
->completed
+ 1;
1444 * Otherwise, wait for a possible partial grace period and
1445 * then the subsequent full grace period.
1447 return rnp
->completed
+ 2;
1451 * Trace-event helper function for rcu_start_future_gp() and
1452 * rcu_nocb_wait_gp().
1454 static void trace_rcu_future_gp(struct rcu_node
*rnp
, struct rcu_data
*rdp
,
1455 unsigned long c
, const char *s
)
1457 trace_rcu_future_grace_period(rdp
->rsp
->name
, rnp
->gpnum
,
1458 rnp
->completed
, c
, rnp
->level
,
1459 rnp
->grplo
, rnp
->grphi
, s
);
1463 * Start some future grace period, as needed to handle newly arrived
1464 * callbacks. The required future grace periods are recorded in each
1465 * rcu_node structure's ->need_future_gp field. Returns true if there
1466 * is reason to awaken the grace-period kthread.
1468 * The caller must hold the specified rcu_node structure's ->lock.
1470 static bool __maybe_unused
1471 rcu_start_future_gp(struct rcu_node
*rnp
, struct rcu_data
*rdp
,
1472 unsigned long *c_out
)
1477 struct rcu_node
*rnp_root
= rcu_get_root(rdp
->rsp
);
1480 * Pick up grace-period number for new callbacks. If this
1481 * grace period is already marked as needed, return to the caller.
1483 c
= rcu_cbs_completed(rdp
->rsp
, rnp
);
1484 trace_rcu_future_gp(rnp
, rdp
, c
, TPS("Startleaf"));
1485 if (rnp
->need_future_gp
[c
& 0x1]) {
1486 trace_rcu_future_gp(rnp
, rdp
, c
, TPS("Prestartleaf"));
1491 * If either this rcu_node structure or the root rcu_node structure
1492 * believe that a grace period is in progress, then we must wait
1493 * for the one following, which is in "c". Because our request
1494 * will be noticed at the end of the current grace period, we don't
1495 * need to explicitly start one. We only do the lockless check
1496 * of rnp_root's fields if the current rcu_node structure thinks
1497 * there is no grace period in flight, and because we hold rnp->lock,
1498 * the only possible change is when rnp_root's two fields are
1499 * equal, in which case rnp_root->gpnum might be concurrently
1500 * incremented. But that is OK, as it will just result in our
1501 * doing some extra useless work.
1503 if (rnp
->gpnum
!= rnp
->completed
||
1504 READ_ONCE(rnp_root
->gpnum
) != READ_ONCE(rnp_root
->completed
)) {
1505 rnp
->need_future_gp
[c
& 0x1]++;
1506 trace_rcu_future_gp(rnp
, rdp
, c
, TPS("Startedleaf"));
1511 * There might be no grace period in progress. If we don't already
1512 * hold it, acquire the root rcu_node structure's lock in order to
1513 * start one (if needed).
1515 if (rnp
!= rnp_root
) {
1516 raw_spin_lock(&rnp_root
->lock
);
1517 smp_mb__after_unlock_lock();
1521 * Get a new grace-period number. If there really is no grace
1522 * period in progress, it will be smaller than the one we obtained
1523 * earlier. Adjust callbacks as needed. Note that even no-CBs
1524 * CPUs have a ->nxtcompleted[] array, so no no-CBs checks needed.
1526 c
= rcu_cbs_completed(rdp
->rsp
, rnp_root
);
1527 for (i
= RCU_DONE_TAIL
; i
< RCU_NEXT_TAIL
; i
++)
1528 if (ULONG_CMP_LT(c
, rdp
->nxtcompleted
[i
]))
1529 rdp
->nxtcompleted
[i
] = c
;
1532 * If the needed for the required grace period is already
1533 * recorded, trace and leave.
1535 if (rnp_root
->need_future_gp
[c
& 0x1]) {
1536 trace_rcu_future_gp(rnp
, rdp
, c
, TPS("Prestartedroot"));
1540 /* Record the need for the future grace period. */
1541 rnp_root
->need_future_gp
[c
& 0x1]++;
1543 /* If a grace period is not already in progress, start one. */
1544 if (rnp_root
->gpnum
!= rnp_root
->completed
) {
1545 trace_rcu_future_gp(rnp
, rdp
, c
, TPS("Startedleafroot"));
1547 trace_rcu_future_gp(rnp
, rdp
, c
, TPS("Startedroot"));
1548 ret
= rcu_start_gp_advanced(rdp
->rsp
, rnp_root
, rdp
);
1551 if (rnp
!= rnp_root
)
1552 raw_spin_unlock(&rnp_root
->lock
);
1560 * Clean up any old requests for the just-ended grace period. Also return
1561 * whether any additional grace periods have been requested. Also invoke
1562 * rcu_nocb_gp_cleanup() in order to wake up any no-callbacks kthreads
1563 * waiting for this grace period to complete.
1565 static int rcu_future_gp_cleanup(struct rcu_state
*rsp
, struct rcu_node
*rnp
)
1567 int c
= rnp
->completed
;
1569 struct rcu_data
*rdp
= this_cpu_ptr(rsp
->rda
);
1571 rcu_nocb_gp_cleanup(rsp
, rnp
);
1572 rnp
->need_future_gp
[c
& 0x1] = 0;
1573 needmore
= rnp
->need_future_gp
[(c
+ 1) & 0x1];
1574 trace_rcu_future_gp(rnp
, rdp
, c
,
1575 needmore
? TPS("CleanupMore") : TPS("Cleanup"));
1580 * Awaken the grace-period kthread for the specified flavor of RCU.
1581 * Don't do a self-awaken, and don't bother awakening when there is
1582 * nothing for the grace-period kthread to do (as in several CPUs
1583 * raced to awaken, and we lost), and finally don't try to awaken
1584 * a kthread that has not yet been created.
1586 static void rcu_gp_kthread_wake(struct rcu_state
*rsp
)
1588 if (current
== rsp
->gp_kthread
||
1589 !READ_ONCE(rsp
->gp_flags
) ||
1592 wake_up(&rsp
->gp_wq
);
1596 * If there is room, assign a ->completed number to any callbacks on
1597 * this CPU that have not already been assigned. Also accelerate any
1598 * callbacks that were previously assigned a ->completed number that has
1599 * since proven to be too conservative, which can happen if callbacks get
1600 * assigned a ->completed number while RCU is idle, but with reference to
1601 * a non-root rcu_node structure. This function is idempotent, so it does
1602 * not hurt to call it repeatedly. Returns an flag saying that we should
1603 * awaken the RCU grace-period kthread.
1605 * The caller must hold rnp->lock with interrupts disabled.
1607 static bool rcu_accelerate_cbs(struct rcu_state
*rsp
, struct rcu_node
*rnp
,
1608 struct rcu_data
*rdp
)
1614 /* If the CPU has no callbacks, nothing to do. */
1615 if (!rdp
->nxttail
[RCU_NEXT_TAIL
] || !*rdp
->nxttail
[RCU_DONE_TAIL
])
1619 * Starting from the sublist containing the callbacks most
1620 * recently assigned a ->completed number and working down, find the
1621 * first sublist that is not assignable to an upcoming grace period.
1622 * Such a sublist has something in it (first two tests) and has
1623 * a ->completed number assigned that will complete sooner than
1624 * the ->completed number for newly arrived callbacks (last test).
1626 * The key point is that any later sublist can be assigned the
1627 * same ->completed number as the newly arrived callbacks, which
1628 * means that the callbacks in any of these later sublist can be
1629 * grouped into a single sublist, whether or not they have already
1630 * been assigned a ->completed number.
1632 c
= rcu_cbs_completed(rsp
, rnp
);
1633 for (i
= RCU_NEXT_TAIL
- 1; i
> RCU_DONE_TAIL
; i
--)
1634 if (rdp
->nxttail
[i
] != rdp
->nxttail
[i
- 1] &&
1635 !ULONG_CMP_GE(rdp
->nxtcompleted
[i
], c
))
1639 * If there are no sublist for unassigned callbacks, leave.
1640 * At the same time, advance "i" one sublist, so that "i" will
1641 * index into the sublist where all the remaining callbacks should
1644 if (++i
>= RCU_NEXT_TAIL
)
1648 * Assign all subsequent callbacks' ->completed number to the next
1649 * full grace period and group them all in the sublist initially
1652 for (; i
<= RCU_NEXT_TAIL
; i
++) {
1653 rdp
->nxttail
[i
] = rdp
->nxttail
[RCU_NEXT_TAIL
];
1654 rdp
->nxtcompleted
[i
] = c
;
1656 /* Record any needed additional grace periods. */
1657 ret
= rcu_start_future_gp(rnp
, rdp
, NULL
);
1659 /* Trace depending on how much we were able to accelerate. */
1660 if (!*rdp
->nxttail
[RCU_WAIT_TAIL
])
1661 trace_rcu_grace_period(rsp
->name
, rdp
->gpnum
, TPS("AccWaitCB"));
1663 trace_rcu_grace_period(rsp
->name
, rdp
->gpnum
, TPS("AccReadyCB"));
1668 * Move any callbacks whose grace period has completed to the
1669 * RCU_DONE_TAIL sublist, then compact the remaining sublists and
1670 * assign ->completed numbers to any callbacks in the RCU_NEXT_TAIL
1671 * sublist. This function is idempotent, so it does not hurt to
1672 * invoke it repeatedly. As long as it is not invoked -too- often...
1673 * Returns true if the RCU grace-period kthread needs to be awakened.
1675 * The caller must hold rnp->lock with interrupts disabled.
1677 static bool rcu_advance_cbs(struct rcu_state
*rsp
, struct rcu_node
*rnp
,
1678 struct rcu_data
*rdp
)
1682 /* If the CPU has no callbacks, nothing to do. */
1683 if (!rdp
->nxttail
[RCU_NEXT_TAIL
] || !*rdp
->nxttail
[RCU_DONE_TAIL
])
1687 * Find all callbacks whose ->completed numbers indicate that they
1688 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
1690 for (i
= RCU_WAIT_TAIL
; i
< RCU_NEXT_TAIL
; i
++) {
1691 if (ULONG_CMP_LT(rnp
->completed
, rdp
->nxtcompleted
[i
]))
1693 rdp
->nxttail
[RCU_DONE_TAIL
] = rdp
->nxttail
[i
];
1695 /* Clean up any sublist tail pointers that were misordered above. */
1696 for (j
= RCU_WAIT_TAIL
; j
< i
; j
++)
1697 rdp
->nxttail
[j
] = rdp
->nxttail
[RCU_DONE_TAIL
];
1699 /* Copy down callbacks to fill in empty sublists. */
1700 for (j
= RCU_WAIT_TAIL
; i
< RCU_NEXT_TAIL
; i
++, j
++) {
1701 if (rdp
->nxttail
[j
] == rdp
->nxttail
[RCU_NEXT_TAIL
])
1703 rdp
->nxttail
[j
] = rdp
->nxttail
[i
];
1704 rdp
->nxtcompleted
[j
] = rdp
->nxtcompleted
[i
];
1707 /* Classify any remaining callbacks. */
1708 return rcu_accelerate_cbs(rsp
, rnp
, rdp
);
1712 * Update CPU-local rcu_data state to record the beginnings and ends of
1713 * grace periods. The caller must hold the ->lock of the leaf rcu_node
1714 * structure corresponding to the current CPU, and must have irqs disabled.
1715 * Returns true if the grace-period kthread needs to be awakened.
1717 static bool __note_gp_changes(struct rcu_state
*rsp
, struct rcu_node
*rnp
,
1718 struct rcu_data
*rdp
)
1722 /* Handle the ends of any preceding grace periods first. */
1723 if (rdp
->completed
== rnp
->completed
&&
1724 !unlikely(READ_ONCE(rdp
->gpwrap
))) {
1726 /* No grace period end, so just accelerate recent callbacks. */
1727 ret
= rcu_accelerate_cbs(rsp
, rnp
, rdp
);
1731 /* Advance callbacks. */
1732 ret
= rcu_advance_cbs(rsp
, rnp
, rdp
);
1734 /* Remember that we saw this grace-period completion. */
1735 rdp
->completed
= rnp
->completed
;
1736 trace_rcu_grace_period(rsp
->name
, rdp
->gpnum
, TPS("cpuend"));
1739 if (rdp
->gpnum
!= rnp
->gpnum
|| unlikely(READ_ONCE(rdp
->gpwrap
))) {
1741 * If the current grace period is waiting for this CPU,
1742 * set up to detect a quiescent state, otherwise don't
1743 * go looking for one.
1745 rdp
->gpnum
= rnp
->gpnum
;
1746 trace_rcu_grace_period(rsp
->name
, rdp
->gpnum
, TPS("cpustart"));
1747 rdp
->passed_quiesce
= 0;
1748 rdp
->rcu_qs_ctr_snap
= __this_cpu_read(rcu_qs_ctr
);
1749 rdp
->qs_pending
= !!(rnp
->qsmask
& rdp
->grpmask
);
1750 zero_cpu_stall_ticks(rdp
);
1751 WRITE_ONCE(rdp
->gpwrap
, false);
1756 static void note_gp_changes(struct rcu_state
*rsp
, struct rcu_data
*rdp
)
1758 unsigned long flags
;
1760 struct rcu_node
*rnp
;
1762 local_irq_save(flags
);
1764 if ((rdp
->gpnum
== READ_ONCE(rnp
->gpnum
) &&
1765 rdp
->completed
== READ_ONCE(rnp
->completed
) &&
1766 !unlikely(READ_ONCE(rdp
->gpwrap
))) || /* w/out lock. */
1767 !raw_spin_trylock(&rnp
->lock
)) { /* irqs already off, so later. */
1768 local_irq_restore(flags
);
1771 smp_mb__after_unlock_lock();
1772 needwake
= __note_gp_changes(rsp
, rnp
, rdp
);
1773 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
1775 rcu_gp_kthread_wake(rsp
);
1778 static void rcu_gp_slow(struct rcu_state
*rsp
, int delay
)
1781 !(rsp
->gpnum
% (rcu_num_nodes
* PER_RCU_NODE_PERIOD
* delay
)))
1782 schedule_timeout_uninterruptible(delay
);
1786 * Initialize a new grace period. Return 0 if no grace period required.
1788 static int rcu_gp_init(struct rcu_state
*rsp
)
1790 unsigned long oldmask
;
1791 struct rcu_data
*rdp
;
1792 struct rcu_node
*rnp
= rcu_get_root(rsp
);
1794 WRITE_ONCE(rsp
->gp_activity
, jiffies
);
1795 raw_spin_lock_irq(&rnp
->lock
);
1796 smp_mb__after_unlock_lock();
1797 if (!READ_ONCE(rsp
->gp_flags
)) {
1798 /* Spurious wakeup, tell caller to go back to sleep. */
1799 raw_spin_unlock_irq(&rnp
->lock
);
1802 WRITE_ONCE(rsp
->gp_flags
, 0); /* Clear all flags: New grace period. */
1804 if (WARN_ON_ONCE(rcu_gp_in_progress(rsp
))) {
1806 * Grace period already in progress, don't start another.
1807 * Not supposed to be able to happen.
1809 raw_spin_unlock_irq(&rnp
->lock
);
1813 /* Advance to a new grace period and initialize state. */
1814 record_gp_stall_check_time(rsp
);
1815 /* Record GP times before starting GP, hence smp_store_release(). */
1816 smp_store_release(&rsp
->gpnum
, rsp
->gpnum
+ 1);
1817 trace_rcu_grace_period(rsp
->name
, rsp
->gpnum
, TPS("start"));
1818 raw_spin_unlock_irq(&rnp
->lock
);
1821 * Apply per-leaf buffered online and offline operations to the
1822 * rcu_node tree. Note that this new grace period need not wait
1823 * for subsequent online CPUs, and that quiescent-state forcing
1824 * will handle subsequent offline CPUs.
1826 rcu_for_each_leaf_node(rsp
, rnp
) {
1827 rcu_gp_slow(rsp
, gp_preinit_delay
);
1828 raw_spin_lock_irq(&rnp
->lock
);
1829 smp_mb__after_unlock_lock();
1830 if (rnp
->qsmaskinit
== rnp
->qsmaskinitnext
&&
1831 !rnp
->wait_blkd_tasks
) {
1832 /* Nothing to do on this leaf rcu_node structure. */
1833 raw_spin_unlock_irq(&rnp
->lock
);
1837 /* Record old state, apply changes to ->qsmaskinit field. */
1838 oldmask
= rnp
->qsmaskinit
;
1839 rnp
->qsmaskinit
= rnp
->qsmaskinitnext
;
1841 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
1842 if (!oldmask
!= !rnp
->qsmaskinit
) {
1843 if (!oldmask
) /* First online CPU for this rcu_node. */
1844 rcu_init_new_rnp(rnp
);
1845 else if (rcu_preempt_has_tasks(rnp
)) /* blocked tasks */
1846 rnp
->wait_blkd_tasks
= true;
1847 else /* Last offline CPU and can propagate. */
1848 rcu_cleanup_dead_rnp(rnp
);
1852 * If all waited-on tasks from prior grace period are
1853 * done, and if all this rcu_node structure's CPUs are
1854 * still offline, propagate up the rcu_node tree and
1855 * clear ->wait_blkd_tasks. Otherwise, if one of this
1856 * rcu_node structure's CPUs has since come back online,
1857 * simply clear ->wait_blkd_tasks (but rcu_cleanup_dead_rnp()
1858 * checks for this, so just call it unconditionally).
1860 if (rnp
->wait_blkd_tasks
&&
1861 (!rcu_preempt_has_tasks(rnp
) ||
1863 rnp
->wait_blkd_tasks
= false;
1864 rcu_cleanup_dead_rnp(rnp
);
1867 raw_spin_unlock_irq(&rnp
->lock
);
1871 * Set the quiescent-state-needed bits in all the rcu_node
1872 * structures for all currently online CPUs in breadth-first order,
1873 * starting from the root rcu_node structure, relying on the layout
1874 * of the tree within the rsp->node[] array. Note that other CPUs
1875 * will access only the leaves of the hierarchy, thus seeing that no
1876 * grace period is in progress, at least until the corresponding
1877 * leaf node has been initialized. In addition, we have excluded
1878 * CPU-hotplug operations.
1880 * The grace period cannot complete until the initialization
1881 * process finishes, because this kthread handles both.
1883 rcu_for_each_node_breadth_first(rsp
, rnp
) {
1884 rcu_gp_slow(rsp
, gp_init_delay
);
1885 raw_spin_lock_irq(&rnp
->lock
);
1886 smp_mb__after_unlock_lock();
1887 rdp
= this_cpu_ptr(rsp
->rda
);
1888 rcu_preempt_check_blocked_tasks(rnp
);
1889 rnp
->qsmask
= rnp
->qsmaskinit
;
1890 WRITE_ONCE(rnp
->gpnum
, rsp
->gpnum
);
1891 if (WARN_ON_ONCE(rnp
->completed
!= rsp
->completed
))
1892 WRITE_ONCE(rnp
->completed
, rsp
->completed
);
1893 if (rnp
== rdp
->mynode
)
1894 (void)__note_gp_changes(rsp
, rnp
, rdp
);
1895 rcu_preempt_boost_start_gp(rnp
);
1896 trace_rcu_grace_period_init(rsp
->name
, rnp
->gpnum
,
1897 rnp
->level
, rnp
->grplo
,
1898 rnp
->grphi
, rnp
->qsmask
);
1899 raw_spin_unlock_irq(&rnp
->lock
);
1900 cond_resched_rcu_qs();
1901 WRITE_ONCE(rsp
->gp_activity
, jiffies
);
1908 * Helper function for wait_event_interruptible_timeout() wakeup
1909 * at force-quiescent-state time.
1911 static bool rcu_gp_fqs_check_wake(struct rcu_state
*rsp
, int *gfp
)
1913 struct rcu_node
*rnp
= rcu_get_root(rsp
);
1915 /* Someone like call_rcu() requested a force-quiescent-state scan. */
1916 *gfp
= READ_ONCE(rsp
->gp_flags
);
1917 if (*gfp
& RCU_GP_FLAG_FQS
)
1920 /* The current grace period has completed. */
1921 if (!READ_ONCE(rnp
->qsmask
) && !rcu_preempt_blocked_readers_cgp(rnp
))
1928 * Do one round of quiescent-state forcing.
1930 static int rcu_gp_fqs(struct rcu_state
*rsp
, int fqs_state_in
)
1932 int fqs_state
= fqs_state_in
;
1933 bool isidle
= false;
1935 struct rcu_node
*rnp
= rcu_get_root(rsp
);
1937 WRITE_ONCE(rsp
->gp_activity
, jiffies
);
1939 if (fqs_state
== RCU_SAVE_DYNTICK
) {
1940 /* Collect dyntick-idle snapshots. */
1941 if (is_sysidle_rcu_state(rsp
)) {
1943 maxj
= jiffies
- ULONG_MAX
/ 4;
1945 force_qs_rnp(rsp
, dyntick_save_progress_counter
,
1947 rcu_sysidle_report_gp(rsp
, isidle
, maxj
);
1948 fqs_state
= RCU_FORCE_QS
;
1950 /* Handle dyntick-idle and offline CPUs. */
1952 force_qs_rnp(rsp
, rcu_implicit_dynticks_qs
, &isidle
, &maxj
);
1954 /* Clear flag to prevent immediate re-entry. */
1955 if (READ_ONCE(rsp
->gp_flags
) & RCU_GP_FLAG_FQS
) {
1956 raw_spin_lock_irq(&rnp
->lock
);
1957 smp_mb__after_unlock_lock();
1958 WRITE_ONCE(rsp
->gp_flags
,
1959 READ_ONCE(rsp
->gp_flags
) & ~RCU_GP_FLAG_FQS
);
1960 raw_spin_unlock_irq(&rnp
->lock
);
1966 * Clean up after the old grace period.
1968 static void rcu_gp_cleanup(struct rcu_state
*rsp
)
1970 unsigned long gp_duration
;
1971 bool needgp
= false;
1973 struct rcu_data
*rdp
;
1974 struct rcu_node
*rnp
= rcu_get_root(rsp
);
1976 WRITE_ONCE(rsp
->gp_activity
, jiffies
);
1977 raw_spin_lock_irq(&rnp
->lock
);
1978 smp_mb__after_unlock_lock();
1979 gp_duration
= jiffies
- rsp
->gp_start
;
1980 if (gp_duration
> rsp
->gp_max
)
1981 rsp
->gp_max
= gp_duration
;
1984 * We know the grace period is complete, but to everyone else
1985 * it appears to still be ongoing. But it is also the case
1986 * that to everyone else it looks like there is nothing that
1987 * they can do to advance the grace period. It is therefore
1988 * safe for us to drop the lock in order to mark the grace
1989 * period as completed in all of the rcu_node structures.
1991 raw_spin_unlock_irq(&rnp
->lock
);
1994 * Propagate new ->completed value to rcu_node structures so
1995 * that other CPUs don't have to wait until the start of the next
1996 * grace period to process their callbacks. This also avoids
1997 * some nasty RCU grace-period initialization races by forcing
1998 * the end of the current grace period to be completely recorded in
1999 * all of the rcu_node structures before the beginning of the next
2000 * grace period is recorded in any of the rcu_node structures.
2002 rcu_for_each_node_breadth_first(rsp
, rnp
) {
2003 raw_spin_lock_irq(&rnp
->lock
);
2004 smp_mb__after_unlock_lock();
2005 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp
));
2006 WARN_ON_ONCE(rnp
->qsmask
);
2007 WRITE_ONCE(rnp
->completed
, rsp
->gpnum
);
2008 rdp
= this_cpu_ptr(rsp
->rda
);
2009 if (rnp
== rdp
->mynode
)
2010 needgp
= __note_gp_changes(rsp
, rnp
, rdp
) || needgp
;
2011 /* smp_mb() provided by prior unlock-lock pair. */
2012 nocb
+= rcu_future_gp_cleanup(rsp
, rnp
);
2013 raw_spin_unlock_irq(&rnp
->lock
);
2014 cond_resched_rcu_qs();
2015 WRITE_ONCE(rsp
->gp_activity
, jiffies
);
2016 rcu_gp_slow(rsp
, gp_cleanup_delay
);
2018 rnp
= rcu_get_root(rsp
);
2019 raw_spin_lock_irq(&rnp
->lock
);
2020 smp_mb__after_unlock_lock(); /* Order GP before ->completed update. */
2021 rcu_nocb_gp_set(rnp
, nocb
);
2023 /* Declare grace period done. */
2024 WRITE_ONCE(rsp
->completed
, rsp
->gpnum
);
2025 trace_rcu_grace_period(rsp
->name
, rsp
->completed
, TPS("end"));
2026 rsp
->fqs_state
= RCU_GP_IDLE
;
2027 rdp
= this_cpu_ptr(rsp
->rda
);
2028 /* Advance CBs to reduce false positives below. */
2029 needgp
= rcu_advance_cbs(rsp
, rnp
, rdp
) || needgp
;
2030 if (needgp
|| cpu_needs_another_gp(rsp
, rdp
)) {
2031 WRITE_ONCE(rsp
->gp_flags
, RCU_GP_FLAG_INIT
);
2032 trace_rcu_grace_period(rsp
->name
,
2033 READ_ONCE(rsp
->gpnum
),
2036 raw_spin_unlock_irq(&rnp
->lock
);
2040 * Body of kthread that handles grace periods.
2042 static int __noreturn
rcu_gp_kthread(void *arg
)
2048 struct rcu_state
*rsp
= arg
;
2049 struct rcu_node
*rnp
= rcu_get_root(rsp
);
2051 rcu_bind_gp_kthread();
2054 /* Handle grace-period start. */
2056 trace_rcu_grace_period(rsp
->name
,
2057 READ_ONCE(rsp
->gpnum
),
2059 rsp
->gp_state
= RCU_GP_WAIT_GPS
;
2060 wait_event_interruptible(rsp
->gp_wq
,
2061 READ_ONCE(rsp
->gp_flags
) &
2063 rsp
->gp_state
= RCU_GP_DONE_GPS
;
2064 /* Locking provides needed memory barrier. */
2065 if (rcu_gp_init(rsp
))
2067 cond_resched_rcu_qs();
2068 WRITE_ONCE(rsp
->gp_activity
, jiffies
);
2069 WARN_ON(signal_pending(current
));
2070 trace_rcu_grace_period(rsp
->name
,
2071 READ_ONCE(rsp
->gpnum
),
2075 /* Handle quiescent-state forcing. */
2076 fqs_state
= RCU_SAVE_DYNTICK
;
2077 j
= jiffies_till_first_fqs
;
2080 jiffies_till_first_fqs
= HZ
;
2085 rsp
->jiffies_force_qs
= jiffies
+ j
;
2086 trace_rcu_grace_period(rsp
->name
,
2087 READ_ONCE(rsp
->gpnum
),
2089 rsp
->gp_state
= RCU_GP_WAIT_FQS
;
2090 ret
= wait_event_interruptible_timeout(rsp
->gp_wq
,
2091 rcu_gp_fqs_check_wake(rsp
, &gf
), j
);
2092 rsp
->gp_state
= RCU_GP_DOING_FQS
;
2093 /* Locking provides needed memory barriers. */
2094 /* If grace period done, leave loop. */
2095 if (!READ_ONCE(rnp
->qsmask
) &&
2096 !rcu_preempt_blocked_readers_cgp(rnp
))
2098 /* If time for quiescent-state forcing, do it. */
2099 if (ULONG_CMP_GE(jiffies
, rsp
->jiffies_force_qs
) ||
2100 (gf
& RCU_GP_FLAG_FQS
)) {
2101 trace_rcu_grace_period(rsp
->name
,
2102 READ_ONCE(rsp
->gpnum
),
2104 fqs_state
= rcu_gp_fqs(rsp
, fqs_state
);
2105 trace_rcu_grace_period(rsp
->name
,
2106 READ_ONCE(rsp
->gpnum
),
2108 cond_resched_rcu_qs();
2109 WRITE_ONCE(rsp
->gp_activity
, jiffies
);
2111 /* Deal with stray signal. */
2112 cond_resched_rcu_qs();
2113 WRITE_ONCE(rsp
->gp_activity
, jiffies
);
2114 WARN_ON(signal_pending(current
));
2115 trace_rcu_grace_period(rsp
->name
,
2116 READ_ONCE(rsp
->gpnum
),
2119 j
= jiffies_till_next_fqs
;
2122 jiffies_till_next_fqs
= HZ
;
2125 jiffies_till_next_fqs
= 1;
2129 /* Handle grace-period end. */
2130 rsp
->gp_state
= RCU_GP_CLEANUP
;
2131 rcu_gp_cleanup(rsp
);
2132 rsp
->gp_state
= RCU_GP_CLEANED
;
2137 * Start a new RCU grace period if warranted, re-initializing the hierarchy
2138 * in preparation for detecting the next grace period. The caller must hold
2139 * the root node's ->lock and hard irqs must be disabled.
2141 * Note that it is legal for a dying CPU (which is marked as offline) to
2142 * invoke this function. This can happen when the dying CPU reports its
2145 * Returns true if the grace-period kthread must be awakened.
2148 rcu_start_gp_advanced(struct rcu_state
*rsp
, struct rcu_node
*rnp
,
2149 struct rcu_data
*rdp
)
2151 if (!rsp
->gp_kthread
|| !cpu_needs_another_gp(rsp
, rdp
)) {
2153 * Either we have not yet spawned the grace-period
2154 * task, this CPU does not need another grace period,
2155 * or a grace period is already in progress.
2156 * Either way, don't start a new grace period.
2160 WRITE_ONCE(rsp
->gp_flags
, RCU_GP_FLAG_INIT
);
2161 trace_rcu_grace_period(rsp
->name
, READ_ONCE(rsp
->gpnum
),
2165 * We can't do wakeups while holding the rnp->lock, as that
2166 * could cause possible deadlocks with the rq->lock. Defer
2167 * the wakeup to our caller.
2173 * Similar to rcu_start_gp_advanced(), but also advance the calling CPU's
2174 * callbacks. Note that rcu_start_gp_advanced() cannot do this because it
2175 * is invoked indirectly from rcu_advance_cbs(), which would result in
2176 * endless recursion -- or would do so if it wasn't for the self-deadlock
2177 * that is encountered beforehand.
2179 * Returns true if the grace-period kthread needs to be awakened.
2181 static bool rcu_start_gp(struct rcu_state
*rsp
)
2183 struct rcu_data
*rdp
= this_cpu_ptr(rsp
->rda
);
2184 struct rcu_node
*rnp
= rcu_get_root(rsp
);
2188 * If there is no grace period in progress right now, any
2189 * callbacks we have up to this point will be satisfied by the
2190 * next grace period. Also, advancing the callbacks reduces the
2191 * probability of false positives from cpu_needs_another_gp()
2192 * resulting in pointless grace periods. So, advance callbacks
2193 * then start the grace period!
2195 ret
= rcu_advance_cbs(rsp
, rnp
, rdp
) || ret
;
2196 ret
= rcu_start_gp_advanced(rsp
, rnp
, rdp
) || ret
;
2201 * Report a full set of quiescent states to the specified rcu_state
2202 * data structure. This involves cleaning up after the prior grace
2203 * period and letting rcu_start_gp() start up the next grace period
2204 * if one is needed. Note that the caller must hold rnp->lock, which
2205 * is released before return.
2207 static void rcu_report_qs_rsp(struct rcu_state
*rsp
, unsigned long flags
)
2208 __releases(rcu_get_root(rsp
)->lock
)
2210 WARN_ON_ONCE(!rcu_gp_in_progress(rsp
));
2211 WRITE_ONCE(rsp
->gp_flags
, READ_ONCE(rsp
->gp_flags
) | RCU_GP_FLAG_FQS
);
2212 raw_spin_unlock_irqrestore(&rcu_get_root(rsp
)->lock
, flags
);
2213 rcu_gp_kthread_wake(rsp
);
2217 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
2218 * Allows quiescent states for a group of CPUs to be reported at one go
2219 * to the specified rcu_node structure, though all the CPUs in the group
2220 * must be represented by the same rcu_node structure (which need not be a
2221 * leaf rcu_node structure, though it often will be). The gps parameter
2222 * is the grace-period snapshot, which means that the quiescent states
2223 * are valid only if rnp->gpnum is equal to gps. That structure's lock
2224 * must be held upon entry, and it is released before return.
2227 rcu_report_qs_rnp(unsigned long mask
, struct rcu_state
*rsp
,
2228 struct rcu_node
*rnp
, unsigned long gps
, unsigned long flags
)
2229 __releases(rnp
->lock
)
2231 unsigned long oldmask
= 0;
2232 struct rcu_node
*rnp_c
;
2234 /* Walk up the rcu_node hierarchy. */
2236 if (!(rnp
->qsmask
& mask
) || rnp
->gpnum
!= gps
) {
2239 * Our bit has already been cleared, or the
2240 * relevant grace period is already over, so done.
2242 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
2245 WARN_ON_ONCE(oldmask
); /* Any child must be all zeroed! */
2246 rnp
->qsmask
&= ~mask
;
2247 trace_rcu_quiescent_state_report(rsp
->name
, rnp
->gpnum
,
2248 mask
, rnp
->qsmask
, rnp
->level
,
2249 rnp
->grplo
, rnp
->grphi
,
2251 if (rnp
->qsmask
!= 0 || rcu_preempt_blocked_readers_cgp(rnp
)) {
2253 /* Other bits still set at this level, so done. */
2254 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
2257 mask
= rnp
->grpmask
;
2258 if (rnp
->parent
== NULL
) {
2260 /* No more levels. Exit loop holding root lock. */
2264 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
2267 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
2268 smp_mb__after_unlock_lock();
2269 oldmask
= rnp_c
->qsmask
;
2273 * Get here if we are the last CPU to pass through a quiescent
2274 * state for this grace period. Invoke rcu_report_qs_rsp()
2275 * to clean up and start the next grace period if one is needed.
2277 rcu_report_qs_rsp(rsp
, flags
); /* releases rnp->lock. */
2281 * Record a quiescent state for all tasks that were previously queued
2282 * on the specified rcu_node structure and that were blocking the current
2283 * RCU grace period. The caller must hold the specified rnp->lock with
2284 * irqs disabled, and this lock is released upon return, but irqs remain
2287 static void rcu_report_unblock_qs_rnp(struct rcu_state
*rsp
,
2288 struct rcu_node
*rnp
, unsigned long flags
)
2289 __releases(rnp
->lock
)
2293 struct rcu_node
*rnp_p
;
2295 if (rcu_state_p
== &rcu_sched_state
|| rsp
!= rcu_state_p
||
2296 rnp
->qsmask
!= 0 || rcu_preempt_blocked_readers_cgp(rnp
)) {
2297 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
2298 return; /* Still need more quiescent states! */
2301 rnp_p
= rnp
->parent
;
2302 if (rnp_p
== NULL
) {
2304 * Only one rcu_node structure in the tree, so don't
2305 * try to report up to its nonexistent parent!
2307 rcu_report_qs_rsp(rsp
, flags
);
2311 /* Report up the rest of the hierarchy, tracking current ->gpnum. */
2313 mask
= rnp
->grpmask
;
2314 raw_spin_unlock(&rnp
->lock
); /* irqs remain disabled. */
2315 raw_spin_lock(&rnp_p
->lock
); /* irqs already disabled. */
2316 smp_mb__after_unlock_lock();
2317 rcu_report_qs_rnp(mask
, rsp
, rnp_p
, gps
, flags
);
2321 * Record a quiescent state for the specified CPU to that CPU's rcu_data
2322 * structure. This must be either called from the specified CPU, or
2323 * called when the specified CPU is known to be offline (and when it is
2324 * also known that no other CPU is concurrently trying to help the offline
2325 * CPU). The lastcomp argument is used to make sure we are still in the
2326 * grace period of interest. We don't want to end the current grace period
2327 * based on quiescent states detected in an earlier grace period!
2330 rcu_report_qs_rdp(int cpu
, struct rcu_state
*rsp
, struct rcu_data
*rdp
)
2332 unsigned long flags
;
2335 struct rcu_node
*rnp
;
2338 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
2339 smp_mb__after_unlock_lock();
2340 if ((rdp
->passed_quiesce
== 0 &&
2341 rdp
->rcu_qs_ctr_snap
== __this_cpu_read(rcu_qs_ctr
)) ||
2342 rdp
->gpnum
!= rnp
->gpnum
|| rnp
->completed
== rnp
->gpnum
||
2346 * The grace period in which this quiescent state was
2347 * recorded has ended, so don't report it upwards.
2348 * We will instead need a new quiescent state that lies
2349 * within the current grace period.
2351 rdp
->passed_quiesce
= 0; /* need qs for new gp. */
2352 rdp
->rcu_qs_ctr_snap
= __this_cpu_read(rcu_qs_ctr
);
2353 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
2356 mask
= rdp
->grpmask
;
2357 if ((rnp
->qsmask
& mask
) == 0) {
2358 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
2360 rdp
->qs_pending
= 0;
2363 * This GP can't end until cpu checks in, so all of our
2364 * callbacks can be processed during the next GP.
2366 needwake
= rcu_accelerate_cbs(rsp
, rnp
, rdp
);
2368 rcu_report_qs_rnp(mask
, rsp
, rnp
, rnp
->gpnum
, flags
);
2369 /* ^^^ Released rnp->lock */
2371 rcu_gp_kthread_wake(rsp
);
2376 * Check to see if there is a new grace period of which this CPU
2377 * is not yet aware, and if so, set up local rcu_data state for it.
2378 * Otherwise, see if this CPU has just passed through its first
2379 * quiescent state for this grace period, and record that fact if so.
2382 rcu_check_quiescent_state(struct rcu_state
*rsp
, struct rcu_data
*rdp
)
2384 /* Check for grace-period ends and beginnings. */
2385 note_gp_changes(rsp
, rdp
);
2388 * Does this CPU still need to do its part for current grace period?
2389 * If no, return and let the other CPUs do their part as well.
2391 if (!rdp
->qs_pending
)
2395 * Was there a quiescent state since the beginning of the grace
2396 * period? If no, then exit and wait for the next call.
2398 if (!rdp
->passed_quiesce
&&
2399 rdp
->rcu_qs_ctr_snap
== __this_cpu_read(rcu_qs_ctr
))
2403 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
2406 rcu_report_qs_rdp(rdp
->cpu
, rsp
, rdp
);
2410 * Send the specified CPU's RCU callbacks to the orphanage. The
2411 * specified CPU must be offline, and the caller must hold the
2415 rcu_send_cbs_to_orphanage(int cpu
, struct rcu_state
*rsp
,
2416 struct rcu_node
*rnp
, struct rcu_data
*rdp
)
2418 /* No-CBs CPUs do not have orphanable callbacks. */
2419 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU
) || rcu_is_nocb_cpu(rdp
->cpu
))
2423 * Orphan the callbacks. First adjust the counts. This is safe
2424 * because _rcu_barrier() excludes CPU-hotplug operations, so it
2425 * cannot be running now. Thus no memory barrier is required.
2427 if (rdp
->nxtlist
!= NULL
) {
2428 rsp
->qlen_lazy
+= rdp
->qlen_lazy
;
2429 rsp
->qlen
+= rdp
->qlen
;
2430 rdp
->n_cbs_orphaned
+= rdp
->qlen
;
2432 WRITE_ONCE(rdp
->qlen
, 0);
2436 * Next, move those callbacks still needing a grace period to
2437 * the orphanage, where some other CPU will pick them up.
2438 * Some of the callbacks might have gone partway through a grace
2439 * period, but that is too bad. They get to start over because we
2440 * cannot assume that grace periods are synchronized across CPUs.
2441 * We don't bother updating the ->nxttail[] array yet, instead
2442 * we just reset the whole thing later on.
2444 if (*rdp
->nxttail
[RCU_DONE_TAIL
] != NULL
) {
2445 *rsp
->orphan_nxttail
= *rdp
->nxttail
[RCU_DONE_TAIL
];
2446 rsp
->orphan_nxttail
= rdp
->nxttail
[RCU_NEXT_TAIL
];
2447 *rdp
->nxttail
[RCU_DONE_TAIL
] = NULL
;
2451 * Then move the ready-to-invoke callbacks to the orphanage,
2452 * where some other CPU will pick them up. These will not be
2453 * required to pass though another grace period: They are done.
2455 if (rdp
->nxtlist
!= NULL
) {
2456 *rsp
->orphan_donetail
= rdp
->nxtlist
;
2457 rsp
->orphan_donetail
= rdp
->nxttail
[RCU_DONE_TAIL
];
2461 * Finally, initialize the rcu_data structure's list to empty and
2462 * disallow further callbacks on this CPU.
2464 init_callback_list(rdp
);
2465 rdp
->nxttail
[RCU_NEXT_TAIL
] = NULL
;
2469 * Adopt the RCU callbacks from the specified rcu_state structure's
2470 * orphanage. The caller must hold the ->orphan_lock.
2472 static void rcu_adopt_orphan_cbs(struct rcu_state
*rsp
, unsigned long flags
)
2475 struct rcu_data
*rdp
= raw_cpu_ptr(rsp
->rda
);
2477 /* No-CBs CPUs are handled specially. */
2478 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU
) ||
2479 rcu_nocb_adopt_orphan_cbs(rsp
, rdp
, flags
))
2482 /* Do the accounting first. */
2483 rdp
->qlen_lazy
+= rsp
->qlen_lazy
;
2484 rdp
->qlen
+= rsp
->qlen
;
2485 rdp
->n_cbs_adopted
+= rsp
->qlen
;
2486 if (rsp
->qlen_lazy
!= rsp
->qlen
)
2487 rcu_idle_count_callbacks_posted();
2492 * We do not need a memory barrier here because the only way we
2493 * can get here if there is an rcu_barrier() in flight is if
2494 * we are the task doing the rcu_barrier().
2497 /* First adopt the ready-to-invoke callbacks. */
2498 if (rsp
->orphan_donelist
!= NULL
) {
2499 *rsp
->orphan_donetail
= *rdp
->nxttail
[RCU_DONE_TAIL
];
2500 *rdp
->nxttail
[RCU_DONE_TAIL
] = rsp
->orphan_donelist
;
2501 for (i
= RCU_NEXT_SIZE
- 1; i
>= RCU_DONE_TAIL
; i
--)
2502 if (rdp
->nxttail
[i
] == rdp
->nxttail
[RCU_DONE_TAIL
])
2503 rdp
->nxttail
[i
] = rsp
->orphan_donetail
;
2504 rsp
->orphan_donelist
= NULL
;
2505 rsp
->orphan_donetail
= &rsp
->orphan_donelist
;
2508 /* And then adopt the callbacks that still need a grace period. */
2509 if (rsp
->orphan_nxtlist
!= NULL
) {
2510 *rdp
->nxttail
[RCU_NEXT_TAIL
] = rsp
->orphan_nxtlist
;
2511 rdp
->nxttail
[RCU_NEXT_TAIL
] = rsp
->orphan_nxttail
;
2512 rsp
->orphan_nxtlist
= NULL
;
2513 rsp
->orphan_nxttail
= &rsp
->orphan_nxtlist
;
2518 * Trace the fact that this CPU is going offline.
2520 static void rcu_cleanup_dying_cpu(struct rcu_state
*rsp
)
2522 RCU_TRACE(unsigned long mask
);
2523 RCU_TRACE(struct rcu_data
*rdp
= this_cpu_ptr(rsp
->rda
));
2524 RCU_TRACE(struct rcu_node
*rnp
= rdp
->mynode
);
2526 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU
))
2529 RCU_TRACE(mask
= rdp
->grpmask
);
2530 trace_rcu_grace_period(rsp
->name
,
2531 rnp
->gpnum
+ 1 - !!(rnp
->qsmask
& mask
),
2536 * All CPUs for the specified rcu_node structure have gone offline,
2537 * and all tasks that were preempted within an RCU read-side critical
2538 * section while running on one of those CPUs have since exited their RCU
2539 * read-side critical section. Some other CPU is reporting this fact with
2540 * the specified rcu_node structure's ->lock held and interrupts disabled.
2541 * This function therefore goes up the tree of rcu_node structures,
2542 * clearing the corresponding bits in the ->qsmaskinit fields. Note that
2543 * the leaf rcu_node structure's ->qsmaskinit field has already been
2546 * This function does check that the specified rcu_node structure has
2547 * all CPUs offline and no blocked tasks, so it is OK to invoke it
2548 * prematurely. That said, invoking it after the fact will cost you
2549 * a needless lock acquisition. So once it has done its work, don't
2552 static void rcu_cleanup_dead_rnp(struct rcu_node
*rnp_leaf
)
2555 struct rcu_node
*rnp
= rnp_leaf
;
2557 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU
) ||
2558 rnp
->qsmaskinit
|| rcu_preempt_has_tasks(rnp
))
2561 mask
= rnp
->grpmask
;
2565 raw_spin_lock(&rnp
->lock
); /* irqs already disabled. */
2566 smp_mb__after_unlock_lock(); /* GP memory ordering. */
2567 rnp
->qsmaskinit
&= ~mask
;
2568 rnp
->qsmask
&= ~mask
;
2569 if (rnp
->qsmaskinit
) {
2570 raw_spin_unlock(&rnp
->lock
); /* irqs remain disabled. */
2573 raw_spin_unlock(&rnp
->lock
); /* irqs remain disabled. */
2578 * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
2579 * function. We now remove it from the rcu_node tree's ->qsmaskinit
2582 static void rcu_cleanup_dying_idle_cpu(int cpu
, struct rcu_state
*rsp
)
2584 unsigned long flags
;
2586 struct rcu_data
*rdp
= per_cpu_ptr(rsp
->rda
, cpu
);
2587 struct rcu_node
*rnp
= rdp
->mynode
; /* Outgoing CPU's rdp & rnp. */
2589 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU
))
2592 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
2593 mask
= rdp
->grpmask
;
2594 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
2595 smp_mb__after_unlock_lock(); /* Enforce GP memory-order guarantee. */
2596 rnp
->qsmaskinitnext
&= ~mask
;
2597 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
2601 * The CPU has been completely removed, and some other CPU is reporting
2602 * this fact from process context. Do the remainder of the cleanup,
2603 * including orphaning the outgoing CPU's RCU callbacks, and also
2604 * adopting them. There can only be one CPU hotplug operation at a time,
2605 * so no other CPU can be attempting to update rcu_cpu_kthread_task.
2607 static void rcu_cleanup_dead_cpu(int cpu
, struct rcu_state
*rsp
)
2609 unsigned long flags
;
2610 struct rcu_data
*rdp
= per_cpu_ptr(rsp
->rda
, cpu
);
2611 struct rcu_node
*rnp
= rdp
->mynode
; /* Outgoing CPU's rdp & rnp. */
2613 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU
))
2616 /* Adjust any no-longer-needed kthreads. */
2617 rcu_boost_kthread_setaffinity(rnp
, -1);
2619 /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */
2620 raw_spin_lock_irqsave(&rsp
->orphan_lock
, flags
);
2621 rcu_send_cbs_to_orphanage(cpu
, rsp
, rnp
, rdp
);
2622 rcu_adopt_orphan_cbs(rsp
, flags
);
2623 raw_spin_unlock_irqrestore(&rsp
->orphan_lock
, flags
);
2625 WARN_ONCE(rdp
->qlen
!= 0 || rdp
->nxtlist
!= NULL
,
2626 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n",
2627 cpu
, rdp
->qlen
, rdp
->nxtlist
);
2631 * Invoke any RCU callbacks that have made it to the end of their grace
2632 * period. Thottle as specified by rdp->blimit.
2634 static void rcu_do_batch(struct rcu_state
*rsp
, struct rcu_data
*rdp
)
2636 unsigned long flags
;
2637 struct rcu_head
*next
, *list
, **tail
;
2638 long bl
, count
, count_lazy
;
2641 /* If no callbacks are ready, just return. */
2642 if (!cpu_has_callbacks_ready_to_invoke(rdp
)) {
2643 trace_rcu_batch_start(rsp
->name
, rdp
->qlen_lazy
, rdp
->qlen
, 0);
2644 trace_rcu_batch_end(rsp
->name
, 0, !!READ_ONCE(rdp
->nxtlist
),
2645 need_resched(), is_idle_task(current
),
2646 rcu_is_callbacks_kthread());
2651 * Extract the list of ready callbacks, disabling to prevent
2652 * races with call_rcu() from interrupt handlers.
2654 local_irq_save(flags
);
2655 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
2657 trace_rcu_batch_start(rsp
->name
, rdp
->qlen_lazy
, rdp
->qlen
, bl
);
2658 list
= rdp
->nxtlist
;
2659 rdp
->nxtlist
= *rdp
->nxttail
[RCU_DONE_TAIL
];
2660 *rdp
->nxttail
[RCU_DONE_TAIL
] = NULL
;
2661 tail
= rdp
->nxttail
[RCU_DONE_TAIL
];
2662 for (i
= RCU_NEXT_SIZE
- 1; i
>= 0; i
--)
2663 if (rdp
->nxttail
[i
] == rdp
->nxttail
[RCU_DONE_TAIL
])
2664 rdp
->nxttail
[i
] = &rdp
->nxtlist
;
2665 local_irq_restore(flags
);
2667 /* Invoke callbacks. */
2668 count
= count_lazy
= 0;
2672 debug_rcu_head_unqueue(list
);
2673 if (__rcu_reclaim(rsp
->name
, list
))
2676 /* Stop only if limit reached and CPU has something to do. */
2677 if (++count
>= bl
&&
2679 (!is_idle_task(current
) && !rcu_is_callbacks_kthread())))
2683 local_irq_save(flags
);
2684 trace_rcu_batch_end(rsp
->name
, count
, !!list
, need_resched(),
2685 is_idle_task(current
),
2686 rcu_is_callbacks_kthread());
2688 /* Update count, and requeue any remaining callbacks. */
2690 *tail
= rdp
->nxtlist
;
2691 rdp
->nxtlist
= list
;
2692 for (i
= 0; i
< RCU_NEXT_SIZE
; i
++)
2693 if (&rdp
->nxtlist
== rdp
->nxttail
[i
])
2694 rdp
->nxttail
[i
] = tail
;
2698 smp_mb(); /* List handling before counting for rcu_barrier(). */
2699 rdp
->qlen_lazy
-= count_lazy
;
2700 WRITE_ONCE(rdp
->qlen
, rdp
->qlen
- count
);
2701 rdp
->n_cbs_invoked
+= count
;
2703 /* Reinstate batch limit if we have worked down the excess. */
2704 if (rdp
->blimit
== LONG_MAX
&& rdp
->qlen
<= qlowmark
)
2705 rdp
->blimit
= blimit
;
2707 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
2708 if (rdp
->qlen
== 0 && rdp
->qlen_last_fqs_check
!= 0) {
2709 rdp
->qlen_last_fqs_check
= 0;
2710 rdp
->n_force_qs_snap
= rsp
->n_force_qs
;
2711 } else if (rdp
->qlen
< rdp
->qlen_last_fqs_check
- qhimark
)
2712 rdp
->qlen_last_fqs_check
= rdp
->qlen
;
2713 WARN_ON_ONCE((rdp
->nxtlist
== NULL
) != (rdp
->qlen
== 0));
2715 local_irq_restore(flags
);
2717 /* Re-invoke RCU core processing if there are callbacks remaining. */
2718 if (cpu_has_callbacks_ready_to_invoke(rdp
))
2723 * Check to see if this CPU is in a non-context-switch quiescent state
2724 * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
2725 * Also schedule RCU core processing.
2727 * This function must be called from hardirq context. It is normally
2728 * invoked from the scheduling-clock interrupt. If rcu_pending returns
2729 * false, there is no point in invoking rcu_check_callbacks().
2731 void rcu_check_callbacks(int user
)
2733 trace_rcu_utilization(TPS("Start scheduler-tick"));
2734 increment_cpu_stall_ticks();
2735 if (user
|| rcu_is_cpu_rrupt_from_idle()) {
2738 * Get here if this CPU took its interrupt from user
2739 * mode or from the idle loop, and if this is not a
2740 * nested interrupt. In this case, the CPU is in
2741 * a quiescent state, so note it.
2743 * No memory barrier is required here because both
2744 * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
2745 * variables that other CPUs neither access nor modify,
2746 * at least not while the corresponding CPU is online.
2752 } else if (!in_softirq()) {
2755 * Get here if this CPU did not take its interrupt from
2756 * softirq, in other words, if it is not interrupting
2757 * a rcu_bh read-side critical section. This is an _bh
2758 * critical section, so note it.
2763 rcu_preempt_check_callbacks();
2767 rcu_note_voluntary_context_switch(current
);
2768 trace_rcu_utilization(TPS("End scheduler-tick"));
2772 * Scan the leaf rcu_node structures, processing dyntick state for any that
2773 * have not yet encountered a quiescent state, using the function specified.
2774 * Also initiate boosting for any threads blocked on the root rcu_node.
2776 * The caller must have suppressed start of new grace periods.
2778 static void force_qs_rnp(struct rcu_state
*rsp
,
2779 int (*f
)(struct rcu_data
*rsp
, bool *isidle
,
2780 unsigned long *maxj
),
2781 bool *isidle
, unsigned long *maxj
)
2785 unsigned long flags
;
2787 struct rcu_node
*rnp
;
2789 rcu_for_each_leaf_node(rsp
, rnp
) {
2790 cond_resched_rcu_qs();
2792 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
2793 smp_mb__after_unlock_lock();
2794 if (rnp
->qsmask
== 0) {
2795 if (rcu_state_p
== &rcu_sched_state
||
2796 rsp
!= rcu_state_p
||
2797 rcu_preempt_blocked_readers_cgp(rnp
)) {
2799 * No point in scanning bits because they
2800 * are all zero. But we might need to
2801 * priority-boost blocked readers.
2803 rcu_initiate_boost(rnp
, flags
);
2804 /* rcu_initiate_boost() releases rnp->lock */
2808 (rnp
->parent
->qsmask
& rnp
->grpmask
)) {
2810 * Race between grace-period
2811 * initialization and task exiting RCU
2812 * read-side critical section: Report.
2814 rcu_report_unblock_qs_rnp(rsp
, rnp
, flags
);
2815 /* rcu_report_unblock_qs_rnp() rlses ->lock */
2821 for (; cpu
<= rnp
->grphi
; cpu
++, bit
<<= 1) {
2822 if ((rnp
->qsmask
& bit
) != 0) {
2823 if (f(per_cpu_ptr(rsp
->rda
, cpu
), isidle
, maxj
))
2828 /* Idle/offline CPUs, report (releases rnp->lock. */
2829 rcu_report_qs_rnp(mask
, rsp
, rnp
, rnp
->gpnum
, flags
);
2831 /* Nothing to do here, so just drop the lock. */
2832 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
2838 * Force quiescent states on reluctant CPUs, and also detect which
2839 * CPUs are in dyntick-idle mode.
2841 static void force_quiescent_state(struct rcu_state
*rsp
)
2843 unsigned long flags
;
2845 struct rcu_node
*rnp
;
2846 struct rcu_node
*rnp_old
= NULL
;
2848 /* Funnel through hierarchy to reduce memory contention. */
2849 rnp
= __this_cpu_read(rsp
->rda
->mynode
);
2850 for (; rnp
!= NULL
; rnp
= rnp
->parent
) {
2851 ret
= (READ_ONCE(rsp
->gp_flags
) & RCU_GP_FLAG_FQS
) ||
2852 !raw_spin_trylock(&rnp
->fqslock
);
2853 if (rnp_old
!= NULL
)
2854 raw_spin_unlock(&rnp_old
->fqslock
);
2856 rsp
->n_force_qs_lh
++;
2861 /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
2863 /* Reached the root of the rcu_node tree, acquire lock. */
2864 raw_spin_lock_irqsave(&rnp_old
->lock
, flags
);
2865 smp_mb__after_unlock_lock();
2866 raw_spin_unlock(&rnp_old
->fqslock
);
2867 if (READ_ONCE(rsp
->gp_flags
) & RCU_GP_FLAG_FQS
) {
2868 rsp
->n_force_qs_lh
++;
2869 raw_spin_unlock_irqrestore(&rnp_old
->lock
, flags
);
2870 return; /* Someone beat us to it. */
2872 WRITE_ONCE(rsp
->gp_flags
, READ_ONCE(rsp
->gp_flags
) | RCU_GP_FLAG_FQS
);
2873 raw_spin_unlock_irqrestore(&rnp_old
->lock
, flags
);
2874 rcu_gp_kthread_wake(rsp
);
2878 * This does the RCU core processing work for the specified rcu_state
2879 * and rcu_data structures. This may be called only from the CPU to
2880 * whom the rdp belongs.
2883 __rcu_process_callbacks(struct rcu_state
*rsp
)
2885 unsigned long flags
;
2887 struct rcu_data
*rdp
= raw_cpu_ptr(rsp
->rda
);
2889 WARN_ON_ONCE(rdp
->beenonline
== 0);
2891 /* Update RCU state based on any recent quiescent states. */
2892 rcu_check_quiescent_state(rsp
, rdp
);
2894 /* Does this CPU require a not-yet-started grace period? */
2895 local_irq_save(flags
);
2896 if (cpu_needs_another_gp(rsp
, rdp
)) {
2897 raw_spin_lock(&rcu_get_root(rsp
)->lock
); /* irqs disabled. */
2898 needwake
= rcu_start_gp(rsp
);
2899 raw_spin_unlock_irqrestore(&rcu_get_root(rsp
)->lock
, flags
);
2901 rcu_gp_kthread_wake(rsp
);
2903 local_irq_restore(flags
);
2906 /* If there are callbacks ready, invoke them. */
2907 if (cpu_has_callbacks_ready_to_invoke(rdp
))
2908 invoke_rcu_callbacks(rsp
, rdp
);
2910 /* Do any needed deferred wakeups of rcuo kthreads. */
2911 do_nocb_deferred_wakeup(rdp
);
2915 * Do RCU core processing for the current CPU.
2917 static void rcu_process_callbacks(struct softirq_action
*unused
)
2919 struct rcu_state
*rsp
;
2921 if (cpu_is_offline(smp_processor_id()))
2923 trace_rcu_utilization(TPS("Start RCU core"));
2924 for_each_rcu_flavor(rsp
)
2925 __rcu_process_callbacks(rsp
);
2926 trace_rcu_utilization(TPS("End RCU core"));
2930 * Schedule RCU callback invocation. If the specified type of RCU
2931 * does not support RCU priority boosting, just do a direct call,
2932 * otherwise wake up the per-CPU kernel kthread. Note that because we
2933 * are running on the current CPU with softirqs disabled, the
2934 * rcu_cpu_kthread_task cannot disappear out from under us.
2936 static void invoke_rcu_callbacks(struct rcu_state
*rsp
, struct rcu_data
*rdp
)
2938 if (unlikely(!READ_ONCE(rcu_scheduler_fully_active
)))
2940 if (likely(!rsp
->boost
)) {
2941 rcu_do_batch(rsp
, rdp
);
2944 invoke_rcu_callbacks_kthread();
2947 static void invoke_rcu_core(void)
2949 if (cpu_online(smp_processor_id()))
2950 raise_softirq(RCU_SOFTIRQ
);
2954 * Handle any core-RCU processing required by a call_rcu() invocation.
2956 static void __call_rcu_core(struct rcu_state
*rsp
, struct rcu_data
*rdp
,
2957 struct rcu_head
*head
, unsigned long flags
)
2962 * If called from an extended quiescent state, invoke the RCU
2963 * core in order to force a re-evaluation of RCU's idleness.
2965 if (!rcu_is_watching())
2968 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
2969 if (irqs_disabled_flags(flags
) || cpu_is_offline(smp_processor_id()))
2973 * Force the grace period if too many callbacks or too long waiting.
2974 * Enforce hysteresis, and don't invoke force_quiescent_state()
2975 * if some other CPU has recently done so. Also, don't bother
2976 * invoking force_quiescent_state() if the newly enqueued callback
2977 * is the only one waiting for a grace period to complete.
2979 if (unlikely(rdp
->qlen
> rdp
->qlen_last_fqs_check
+ qhimark
)) {
2981 /* Are we ignoring a completed grace period? */
2982 note_gp_changes(rsp
, rdp
);
2984 /* Start a new grace period if one not already started. */
2985 if (!rcu_gp_in_progress(rsp
)) {
2986 struct rcu_node
*rnp_root
= rcu_get_root(rsp
);
2988 raw_spin_lock(&rnp_root
->lock
);
2989 smp_mb__after_unlock_lock();
2990 needwake
= rcu_start_gp(rsp
);
2991 raw_spin_unlock(&rnp_root
->lock
);
2993 rcu_gp_kthread_wake(rsp
);
2995 /* Give the grace period a kick. */
2996 rdp
->blimit
= LONG_MAX
;
2997 if (rsp
->n_force_qs
== rdp
->n_force_qs_snap
&&
2998 *rdp
->nxttail
[RCU_DONE_TAIL
] != head
)
2999 force_quiescent_state(rsp
);
3000 rdp
->n_force_qs_snap
= rsp
->n_force_qs
;
3001 rdp
->qlen_last_fqs_check
= rdp
->qlen
;
3007 * RCU callback function to leak a callback.
3009 static void rcu_leak_callback(struct rcu_head
*rhp
)
3014 * Helper function for call_rcu() and friends. The cpu argument will
3015 * normally be -1, indicating "currently running CPU". It may specify
3016 * a CPU only if that CPU is a no-CBs CPU. Currently, only _rcu_barrier()
3017 * is expected to specify a CPU.
3020 __call_rcu(struct rcu_head
*head
, void (*func
)(struct rcu_head
*rcu
),
3021 struct rcu_state
*rsp
, int cpu
, bool lazy
)
3023 unsigned long flags
;
3024 struct rcu_data
*rdp
;
3026 WARN_ON_ONCE((unsigned long)head
& 0x1); /* Misaligned rcu_head! */
3027 if (debug_rcu_head_queue(head
)) {
3028 /* Probable double call_rcu(), so leak the callback. */
3029 WRITE_ONCE(head
->func
, rcu_leak_callback
);
3030 WARN_ONCE(1, "__call_rcu(): Leaked duplicate callback\n");
3037 * Opportunistically note grace-period endings and beginnings.
3038 * Note that we might see a beginning right after we see an
3039 * end, but never vice versa, since this CPU has to pass through
3040 * a quiescent state betweentimes.
3042 local_irq_save(flags
);
3043 rdp
= this_cpu_ptr(rsp
->rda
);
3045 /* Add the callback to our list. */
3046 if (unlikely(rdp
->nxttail
[RCU_NEXT_TAIL
] == NULL
) || cpu
!= -1) {
3050 rdp
= per_cpu_ptr(rsp
->rda
, cpu
);
3051 if (likely(rdp
->mynode
)) {
3052 /* Post-boot, so this should be for a no-CBs CPU. */
3053 offline
= !__call_rcu_nocb(rdp
, head
, lazy
, flags
);
3054 WARN_ON_ONCE(offline
);
3055 /* Offline CPU, _call_rcu() illegal, leak callback. */
3056 local_irq_restore(flags
);
3060 * Very early boot, before rcu_init(). Initialize if needed
3061 * and then drop through to queue the callback.
3064 WARN_ON_ONCE(!rcu_is_watching());
3065 if (!likely(rdp
->nxtlist
))
3066 init_default_callback_list(rdp
);
3068 WRITE_ONCE(rdp
->qlen
, rdp
->qlen
+ 1);
3072 rcu_idle_count_callbacks_posted();
3073 smp_mb(); /* Count before adding callback for rcu_barrier(). */
3074 *rdp
->nxttail
[RCU_NEXT_TAIL
] = head
;
3075 rdp
->nxttail
[RCU_NEXT_TAIL
] = &head
->next
;
3077 if (__is_kfree_rcu_offset((unsigned long)func
))
3078 trace_rcu_kfree_callback(rsp
->name
, head
, (unsigned long)func
,
3079 rdp
->qlen_lazy
, rdp
->qlen
);
3081 trace_rcu_callback(rsp
->name
, head
, rdp
->qlen_lazy
, rdp
->qlen
);
3083 /* Go handle any RCU core processing required. */
3084 __call_rcu_core(rsp
, rdp
, head
, flags
);
3085 local_irq_restore(flags
);
3089 * Queue an RCU-sched callback for invocation after a grace period.
3091 void call_rcu_sched(struct rcu_head
*head
, void (*func
)(struct rcu_head
*rcu
))
3093 __call_rcu(head
, func
, &rcu_sched_state
, -1, 0);
3095 EXPORT_SYMBOL_GPL(call_rcu_sched
);
3098 * Queue an RCU callback for invocation after a quicker grace period.
3100 void call_rcu_bh(struct rcu_head
*head
, void (*func
)(struct rcu_head
*rcu
))
3102 __call_rcu(head
, func
, &rcu_bh_state
, -1, 0);
3104 EXPORT_SYMBOL_GPL(call_rcu_bh
);
3107 * Queue an RCU callback for lazy invocation after a grace period.
3108 * This will likely be later named something like "call_rcu_lazy()",
3109 * but this change will require some way of tagging the lazy RCU
3110 * callbacks in the list of pending callbacks. Until then, this
3111 * function may only be called from __kfree_rcu().
3113 void kfree_call_rcu(struct rcu_head
*head
,
3114 void (*func
)(struct rcu_head
*rcu
))
3116 __call_rcu(head
, func
, rcu_state_p
, -1, 1);
3118 EXPORT_SYMBOL_GPL(kfree_call_rcu
);
3121 * Because a context switch is a grace period for RCU-sched and RCU-bh,
3122 * any blocking grace-period wait automatically implies a grace period
3123 * if there is only one CPU online at any point time during execution
3124 * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
3125 * occasionally incorrectly indicate that there are multiple CPUs online
3126 * when there was in fact only one the whole time, as this just adds
3127 * some overhead: RCU still operates correctly.
3129 static inline int rcu_blocking_is_gp(void)
3133 might_sleep(); /* Check for RCU read-side critical section. */
3135 ret
= num_online_cpus() <= 1;
3141 * synchronize_sched - wait until an rcu-sched grace period has elapsed.
3143 * Control will return to the caller some time after a full rcu-sched
3144 * grace period has elapsed, in other words after all currently executing
3145 * rcu-sched read-side critical sections have completed. These read-side
3146 * critical sections are delimited by rcu_read_lock_sched() and
3147 * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
3148 * local_irq_disable(), and so on may be used in place of
3149 * rcu_read_lock_sched().
3151 * This means that all preempt_disable code sequences, including NMI and
3152 * non-threaded hardware-interrupt handlers, in progress on entry will
3153 * have completed before this primitive returns. However, this does not
3154 * guarantee that softirq handlers will have completed, since in some
3155 * kernels, these handlers can run in process context, and can block.
3157 * Note that this guarantee implies further memory-ordering guarantees.
3158 * On systems with more than one CPU, when synchronize_sched() returns,
3159 * each CPU is guaranteed to have executed a full memory barrier since the
3160 * end of its last RCU-sched read-side critical section whose beginning
3161 * preceded the call to synchronize_sched(). In addition, each CPU having
3162 * an RCU read-side critical section that extends beyond the return from
3163 * synchronize_sched() is guaranteed to have executed a full memory barrier
3164 * after the beginning of synchronize_sched() and before the beginning of
3165 * that RCU read-side critical section. Note that these guarantees include
3166 * CPUs that are offline, idle, or executing in user mode, as well as CPUs
3167 * that are executing in the kernel.
3169 * Furthermore, if CPU A invoked synchronize_sched(), which returned
3170 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
3171 * to have executed a full memory barrier during the execution of
3172 * synchronize_sched() -- even if CPU A and CPU B are the same CPU (but
3173 * again only if the system has more than one CPU).
3175 * This primitive provides the guarantees made by the (now removed)
3176 * synchronize_kernel() API. In contrast, synchronize_rcu() only
3177 * guarantees that rcu_read_lock() sections will have completed.
3178 * In "classic RCU", these two guarantees happen to be one and
3179 * the same, but can differ in realtime RCU implementations.
3181 void synchronize_sched(void)
3183 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map
) ||
3184 lock_is_held(&rcu_lock_map
) ||
3185 lock_is_held(&rcu_sched_lock_map
),
3186 "Illegal synchronize_sched() in RCU-sched read-side critical section");
3187 if (rcu_blocking_is_gp())
3189 if (rcu_gp_is_expedited())
3190 synchronize_sched_expedited();
3192 wait_rcu_gp(call_rcu_sched
);
3194 EXPORT_SYMBOL_GPL(synchronize_sched
);
3197 * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
3199 * Control will return to the caller some time after a full rcu_bh grace
3200 * period has elapsed, in other words after all currently executing rcu_bh
3201 * read-side critical sections have completed. RCU read-side critical
3202 * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
3203 * and may be nested.
3205 * See the description of synchronize_sched() for more detailed information
3206 * on memory ordering guarantees.
3208 void synchronize_rcu_bh(void)
3210 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map
) ||
3211 lock_is_held(&rcu_lock_map
) ||
3212 lock_is_held(&rcu_sched_lock_map
),
3213 "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
3214 if (rcu_blocking_is_gp())
3216 if (rcu_gp_is_expedited())
3217 synchronize_rcu_bh_expedited();
3219 wait_rcu_gp(call_rcu_bh
);
3221 EXPORT_SYMBOL_GPL(synchronize_rcu_bh
);
3224 * get_state_synchronize_rcu - Snapshot current RCU state
3226 * Returns a cookie that is used by a later call to cond_synchronize_rcu()
3227 * to determine whether or not a full grace period has elapsed in the
3230 unsigned long get_state_synchronize_rcu(void)
3233 * Any prior manipulation of RCU-protected data must happen
3234 * before the load from ->gpnum.
3239 * Make sure this load happens before the purportedly
3240 * time-consuming work between get_state_synchronize_rcu()
3241 * and cond_synchronize_rcu().
3243 return smp_load_acquire(&rcu_state_p
->gpnum
);
3245 EXPORT_SYMBOL_GPL(get_state_synchronize_rcu
);
3248 * cond_synchronize_rcu - Conditionally wait for an RCU grace period
3250 * @oldstate: return value from earlier call to get_state_synchronize_rcu()
3252 * If a full RCU grace period has elapsed since the earlier call to
3253 * get_state_synchronize_rcu(), just return. Otherwise, invoke
3254 * synchronize_rcu() to wait for a full grace period.
3256 * Yes, this function does not take counter wrap into account. But
3257 * counter wrap is harmless. If the counter wraps, we have waited for
3258 * more than 2 billion grace periods (and way more on a 64-bit system!),
3259 * so waiting for one additional grace period should be just fine.
3261 void cond_synchronize_rcu(unsigned long oldstate
)
3263 unsigned long newstate
;
3266 * Ensure that this load happens before any RCU-destructive
3267 * actions the caller might carry out after we return.
3269 newstate
= smp_load_acquire(&rcu_state_p
->completed
);
3270 if (ULONG_CMP_GE(oldstate
, newstate
))
3273 EXPORT_SYMBOL_GPL(cond_synchronize_rcu
);
3276 * get_state_synchronize_sched - Snapshot current RCU-sched state
3278 * Returns a cookie that is used by a later call to cond_synchronize_sched()
3279 * to determine whether or not a full grace period has elapsed in the
3282 unsigned long get_state_synchronize_sched(void)
3285 * Any prior manipulation of RCU-protected data must happen
3286 * before the load from ->gpnum.
3291 * Make sure this load happens before the purportedly
3292 * time-consuming work between get_state_synchronize_sched()
3293 * and cond_synchronize_sched().
3295 return smp_load_acquire(&rcu_sched_state
.gpnum
);
3297 EXPORT_SYMBOL_GPL(get_state_synchronize_sched
);
3300 * cond_synchronize_sched - Conditionally wait for an RCU-sched grace period
3302 * @oldstate: return value from earlier call to get_state_synchronize_sched()
3304 * If a full RCU-sched grace period has elapsed since the earlier call to
3305 * get_state_synchronize_sched(), just return. Otherwise, invoke
3306 * synchronize_sched() to wait for a full grace period.
3308 * Yes, this function does not take counter wrap into account. But
3309 * counter wrap is harmless. If the counter wraps, we have waited for
3310 * more than 2 billion grace periods (and way more on a 64-bit system!),
3311 * so waiting for one additional grace period should be just fine.
3313 void cond_synchronize_sched(unsigned long oldstate
)
3315 unsigned long newstate
;
3318 * Ensure that this load happens before any RCU-destructive
3319 * actions the caller might carry out after we return.
3321 newstate
= smp_load_acquire(&rcu_sched_state
.completed
);
3322 if (ULONG_CMP_GE(oldstate
, newstate
))
3323 synchronize_sched();
3325 EXPORT_SYMBOL_GPL(cond_synchronize_sched
);
3327 /* Adjust sequence number for start of update-side operation. */
3328 static void rcu_seq_start(unsigned long *sp
)
3330 WRITE_ONCE(*sp
, *sp
+ 1);
3331 smp_mb(); /* Ensure update-side operation after counter increment. */
3332 WARN_ON_ONCE(!(*sp
& 0x1));
3335 /* Adjust sequence number for end of update-side operation. */
3336 static void rcu_seq_end(unsigned long *sp
)
3338 smp_mb(); /* Ensure update-side operation before counter increment. */
3339 WRITE_ONCE(*sp
, *sp
+ 1);
3340 WARN_ON_ONCE(*sp
& 0x1);
3343 /* Take a snapshot of the update side's sequence number. */
3344 static unsigned long rcu_seq_snap(unsigned long *sp
)
3348 smp_mb(); /* Caller's modifications seen first by other CPUs. */
3349 s
= (READ_ONCE(*sp
) + 3) & ~0x1;
3350 smp_mb(); /* Above access must not bleed into critical section. */
3355 * Given a snapshot from rcu_seq_snap(), determine whether or not a
3356 * full update-side operation has occurred.
3358 static bool rcu_seq_done(unsigned long *sp
, unsigned long s
)
3360 return ULONG_CMP_GE(READ_ONCE(*sp
), s
);
3363 /* Wrapper functions for expedited grace periods. */
3364 static void rcu_exp_gp_seq_start(struct rcu_state
*rsp
)
3366 rcu_seq_start(&rsp
->expedited_sequence
);
3368 static void rcu_exp_gp_seq_end(struct rcu_state
*rsp
)
3370 rcu_seq_end(&rsp
->expedited_sequence
);
3371 smp_mb(); /* Ensure that consecutive grace periods serialize. */
3373 static unsigned long rcu_exp_gp_seq_snap(struct rcu_state
*rsp
)
3375 return rcu_seq_snap(&rsp
->expedited_sequence
);
3377 static bool rcu_exp_gp_seq_done(struct rcu_state
*rsp
, unsigned long s
)
3379 return rcu_seq_done(&rsp
->expedited_sequence
, s
);
3382 /* Common code for synchronize_{rcu,sched}_expedited() work-done checking. */
3383 static bool sync_exp_work_done(struct rcu_state
*rsp
, struct rcu_node
*rnp
,
3384 struct rcu_data
*rdp
,
3385 atomic_long_t
*stat
, unsigned long s
)
3387 if (rcu_exp_gp_seq_done(rsp
, s
)) {
3389 mutex_unlock(&rnp
->exp_funnel_mutex
);
3391 mutex_unlock(&rdp
->exp_funnel_mutex
);
3392 /* Ensure test happens before caller kfree(). */
3393 smp_mb__before_atomic(); /* ^^^ */
3394 atomic_long_inc(stat
);
3401 * Funnel-lock acquisition for expedited grace periods. Returns a
3402 * pointer to the root rcu_node structure, or NULL if some other
3403 * task did the expedited grace period for us.
3405 static struct rcu_node
*exp_funnel_lock(struct rcu_state
*rsp
, unsigned long s
)
3407 struct rcu_data
*rdp
;
3408 struct rcu_node
*rnp0
;
3409 struct rcu_node
*rnp1
= NULL
;
3412 * First try directly acquiring the root lock in order to reduce
3413 * latency in the common case where expedited grace periods are
3414 * rare. We check mutex_is_locked() to avoid pathological levels of
3415 * memory contention on ->exp_funnel_mutex in the heavy-load case.
3417 rnp0
= rcu_get_root(rsp
);
3418 if (!mutex_is_locked(&rnp0
->exp_funnel_mutex
)) {
3419 if (mutex_trylock(&rnp0
->exp_funnel_mutex
)) {
3420 if (sync_exp_work_done(rsp
, rnp0
, NULL
,
3421 &rsp
->expedited_workdone0
, s
))
3428 * Each pass through the following loop works its way
3429 * up the rcu_node tree, returning if others have done the
3430 * work or otherwise falls through holding the root rnp's
3431 * ->exp_funnel_mutex. The mapping from CPU to rcu_node structure
3432 * can be inexact, as it is just promoting locality and is not
3433 * strictly needed for correctness.
3435 rdp
= per_cpu_ptr(rsp
->rda
, raw_smp_processor_id());
3436 if (sync_exp_work_done(rsp
, NULL
, NULL
, &rsp
->expedited_workdone1
, s
))
3438 mutex_lock(&rdp
->exp_funnel_mutex
);
3440 for (; rnp0
!= NULL
; rnp0
= rnp0
->parent
) {
3441 if (sync_exp_work_done(rsp
, rnp1
, rdp
,
3442 &rsp
->expedited_workdone2
, s
))
3444 mutex_lock(&rnp0
->exp_funnel_mutex
);
3446 mutex_unlock(&rnp1
->exp_funnel_mutex
);
3448 mutex_unlock(&rdp
->exp_funnel_mutex
);
3451 if (sync_exp_work_done(rsp
, rnp1
, rdp
,
3452 &rsp
->expedited_workdone3
, s
))
3457 /* Invoked on each online non-idle CPU for expedited quiescent state. */
3458 static int synchronize_sched_expedited_cpu_stop(void *data
)
3460 struct rcu_data
*rdp
= data
;
3461 struct rcu_state
*rsp
= rdp
->rsp
;
3463 /* We are here: If we are last, do the wakeup. */
3464 rdp
->exp_done
= true;
3465 if (atomic_dec_and_test(&rsp
->expedited_need_qs
))
3466 wake_up(&rsp
->expedited_wq
);
3470 static void synchronize_sched_expedited_wait(struct rcu_state
*rsp
)
3473 unsigned long jiffies_stall
;
3474 unsigned long jiffies_start
;
3475 struct rcu_data
*rdp
;
3478 jiffies_stall
= rcu_jiffies_till_stall_check();
3479 jiffies_start
= jiffies
;
3482 ret
= wait_event_interruptible_timeout(
3484 !atomic_read(&rsp
->expedited_need_qs
),
3489 /* Hit a signal, disable CPU stall warnings. */
3490 wait_event(rsp
->expedited_wq
,
3491 !atomic_read(&rsp
->expedited_need_qs
));
3494 pr_err("INFO: %s detected expedited stalls on CPUs: {",
3496 for_each_online_cpu(cpu
) {
3497 rdp
= per_cpu_ptr(rsp
->rda
, cpu
);
3501 pr_cont(" %d", cpu
);
3503 pr_cont(" } %lu jiffies s: %lu\n",
3504 jiffies
- jiffies_start
, rsp
->expedited_sequence
);
3505 for_each_online_cpu(cpu
) {
3506 rdp
= per_cpu_ptr(rsp
->rda
, cpu
);
3512 jiffies_stall
= 3 * rcu_jiffies_till_stall_check() + 3;
3517 * synchronize_sched_expedited - Brute-force RCU-sched grace period
3519 * Wait for an RCU-sched grace period to elapse, but use a "big hammer"
3520 * approach to force the grace period to end quickly. This consumes
3521 * significant time on all CPUs and is unfriendly to real-time workloads,
3522 * so is thus not recommended for any sort of common-case code. In fact,
3523 * if you are using synchronize_sched_expedited() in a loop, please
3524 * restructure your code to batch your updates, and then use a single
3525 * synchronize_sched() instead.
3527 * This implementation can be thought of as an application of sequence
3528 * locking to expedited grace periods, but using the sequence counter to
3529 * determine when someone else has already done the work instead of for
3532 void synchronize_sched_expedited(void)
3536 struct rcu_node
*rnp
;
3537 struct rcu_state
*rsp
= &rcu_sched_state
;
3539 /* Take a snapshot of the sequence number. */
3540 s
= rcu_exp_gp_seq_snap(rsp
);
3542 if (!try_get_online_cpus()) {
3543 /* CPU hotplug operation in flight, fall back to normal GP. */
3544 wait_rcu_gp(call_rcu_sched
);
3545 atomic_long_inc(&rsp
->expedited_normal
);
3548 WARN_ON_ONCE(cpu_is_offline(raw_smp_processor_id()));
3550 rnp
= exp_funnel_lock(rsp
, s
);
3553 return; /* Someone else did our work for us. */
3556 rcu_exp_gp_seq_start(rsp
);
3558 /* Stop each CPU that is online, non-idle, and not us. */
3559 init_waitqueue_head(&rsp
->expedited_wq
);
3560 atomic_set(&rsp
->expedited_need_qs
, 1); /* Extra count avoids race. */
3561 for_each_online_cpu(cpu
) {
3562 struct rcu_data
*rdp
= per_cpu_ptr(rsp
->rda
, cpu
);
3563 struct rcu_dynticks
*rdtp
= &per_cpu(rcu_dynticks
, cpu
);
3565 rdp
->exp_done
= false;
3567 /* Skip our CPU and any idle CPUs. */
3568 if (raw_smp_processor_id() == cpu
||
3569 !(atomic_add_return(0, &rdtp
->dynticks
) & 0x1))
3571 atomic_inc(&rsp
->expedited_need_qs
);
3572 stop_one_cpu_nowait(cpu
, synchronize_sched_expedited_cpu_stop
,
3573 rdp
, &rdp
->exp_stop_work
);
3576 /* Remove extra count and, if necessary, wait for CPUs to stop. */
3577 if (!atomic_dec_and_test(&rsp
->expedited_need_qs
))
3578 synchronize_sched_expedited_wait(rsp
);
3580 rcu_exp_gp_seq_end(rsp
);
3581 mutex_unlock(&rnp
->exp_funnel_mutex
);
3585 EXPORT_SYMBOL_GPL(synchronize_sched_expedited
);
3588 * Check to see if there is any immediate RCU-related work to be done
3589 * by the current CPU, for the specified type of RCU, returning 1 if so.
3590 * The checks are in order of increasing expense: checks that can be
3591 * carried out against CPU-local state are performed first. However,
3592 * we must check for CPU stalls first, else we might not get a chance.
3594 static int __rcu_pending(struct rcu_state
*rsp
, struct rcu_data
*rdp
)
3596 struct rcu_node
*rnp
= rdp
->mynode
;
3598 rdp
->n_rcu_pending
++;
3600 /* Check for CPU stalls, if enabled. */
3601 check_cpu_stall(rsp
, rdp
);
3603 /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
3604 if (rcu_nohz_full_cpu(rsp
))
3607 /* Is the RCU core waiting for a quiescent state from this CPU? */
3608 if (rcu_scheduler_fully_active
&&
3609 rdp
->qs_pending
&& !rdp
->passed_quiesce
&&
3610 rdp
->rcu_qs_ctr_snap
== __this_cpu_read(rcu_qs_ctr
)) {
3611 rdp
->n_rp_qs_pending
++;
3612 } else if (rdp
->qs_pending
&&
3613 (rdp
->passed_quiesce
||
3614 rdp
->rcu_qs_ctr_snap
!= __this_cpu_read(rcu_qs_ctr
))) {
3615 rdp
->n_rp_report_qs
++;
3619 /* Does this CPU have callbacks ready to invoke? */
3620 if (cpu_has_callbacks_ready_to_invoke(rdp
)) {
3621 rdp
->n_rp_cb_ready
++;
3625 /* Has RCU gone idle with this CPU needing another grace period? */
3626 if (cpu_needs_another_gp(rsp
, rdp
)) {
3627 rdp
->n_rp_cpu_needs_gp
++;
3631 /* Has another RCU grace period completed? */
3632 if (READ_ONCE(rnp
->completed
) != rdp
->completed
) { /* outside lock */
3633 rdp
->n_rp_gp_completed
++;
3637 /* Has a new RCU grace period started? */
3638 if (READ_ONCE(rnp
->gpnum
) != rdp
->gpnum
||
3639 unlikely(READ_ONCE(rdp
->gpwrap
))) { /* outside lock */
3640 rdp
->n_rp_gp_started
++;
3644 /* Does this CPU need a deferred NOCB wakeup? */
3645 if (rcu_nocb_need_deferred_wakeup(rdp
)) {
3646 rdp
->n_rp_nocb_defer_wakeup
++;
3651 rdp
->n_rp_need_nothing
++;
3656 * Check to see if there is any immediate RCU-related work to be done
3657 * by the current CPU, returning 1 if so. This function is part of the
3658 * RCU implementation; it is -not- an exported member of the RCU API.
3660 static int rcu_pending(void)
3662 struct rcu_state
*rsp
;
3664 for_each_rcu_flavor(rsp
)
3665 if (__rcu_pending(rsp
, this_cpu_ptr(rsp
->rda
)))
3671 * Return true if the specified CPU has any callback. If all_lazy is
3672 * non-NULL, store an indication of whether all callbacks are lazy.
3673 * (If there are no callbacks, all of them are deemed to be lazy.)
3675 static bool __maybe_unused
rcu_cpu_has_callbacks(bool *all_lazy
)
3679 struct rcu_data
*rdp
;
3680 struct rcu_state
*rsp
;
3682 for_each_rcu_flavor(rsp
) {
3683 rdp
= this_cpu_ptr(rsp
->rda
);
3687 if (rdp
->qlen
!= rdp
->qlen_lazy
|| !all_lazy
) {
3698 * Helper function for _rcu_barrier() tracing. If tracing is disabled,
3699 * the compiler is expected to optimize this away.
3701 static void _rcu_barrier_trace(struct rcu_state
*rsp
, const char *s
,
3702 int cpu
, unsigned long done
)
3704 trace_rcu_barrier(rsp
->name
, s
, cpu
,
3705 atomic_read(&rsp
->barrier_cpu_count
), done
);
3709 * RCU callback function for _rcu_barrier(). If we are last, wake
3710 * up the task executing _rcu_barrier().
3712 static void rcu_barrier_callback(struct rcu_head
*rhp
)
3714 struct rcu_data
*rdp
= container_of(rhp
, struct rcu_data
, barrier_head
);
3715 struct rcu_state
*rsp
= rdp
->rsp
;
3717 if (atomic_dec_and_test(&rsp
->barrier_cpu_count
)) {
3718 _rcu_barrier_trace(rsp
, "LastCB", -1, rsp
->barrier_sequence
);
3719 complete(&rsp
->barrier_completion
);
3721 _rcu_barrier_trace(rsp
, "CB", -1, rsp
->barrier_sequence
);
3726 * Called with preemption disabled, and from cross-cpu IRQ context.
3728 static void rcu_barrier_func(void *type
)
3730 struct rcu_state
*rsp
= type
;
3731 struct rcu_data
*rdp
= raw_cpu_ptr(rsp
->rda
);
3733 _rcu_barrier_trace(rsp
, "IRQ", -1, rsp
->barrier_sequence
);
3734 atomic_inc(&rsp
->barrier_cpu_count
);
3735 rsp
->call(&rdp
->barrier_head
, rcu_barrier_callback
);
3739 * Orchestrate the specified type of RCU barrier, waiting for all
3740 * RCU callbacks of the specified type to complete.
3742 static void _rcu_barrier(struct rcu_state
*rsp
)
3745 struct rcu_data
*rdp
;
3746 unsigned long s
= rcu_seq_snap(&rsp
->barrier_sequence
);
3748 _rcu_barrier_trace(rsp
, "Begin", -1, s
);
3750 /* Take mutex to serialize concurrent rcu_barrier() requests. */
3751 mutex_lock(&rsp
->barrier_mutex
);
3753 /* Did someone else do our work for us? */
3754 if (rcu_seq_done(&rsp
->barrier_sequence
, s
)) {
3755 _rcu_barrier_trace(rsp
, "EarlyExit", -1, rsp
->barrier_sequence
);
3756 smp_mb(); /* caller's subsequent code after above check. */
3757 mutex_unlock(&rsp
->barrier_mutex
);
3761 /* Mark the start of the barrier operation. */
3762 rcu_seq_start(&rsp
->barrier_sequence
);
3763 _rcu_barrier_trace(rsp
, "Inc1", -1, rsp
->barrier_sequence
);
3766 * Initialize the count to one rather than to zero in order to
3767 * avoid a too-soon return to zero in case of a short grace period
3768 * (or preemption of this task). Exclude CPU-hotplug operations
3769 * to ensure that no offline CPU has callbacks queued.
3771 init_completion(&rsp
->barrier_completion
);
3772 atomic_set(&rsp
->barrier_cpu_count
, 1);
3776 * Force each CPU with callbacks to register a new callback.
3777 * When that callback is invoked, we will know that all of the
3778 * corresponding CPU's preceding callbacks have been invoked.
3780 for_each_possible_cpu(cpu
) {
3781 if (!cpu_online(cpu
) && !rcu_is_nocb_cpu(cpu
))
3783 rdp
= per_cpu_ptr(rsp
->rda
, cpu
);
3784 if (rcu_is_nocb_cpu(cpu
)) {
3785 if (!rcu_nocb_cpu_needs_barrier(rsp
, cpu
)) {
3786 _rcu_barrier_trace(rsp
, "OfflineNoCB", cpu
,
3787 rsp
->barrier_sequence
);
3789 _rcu_barrier_trace(rsp
, "OnlineNoCB", cpu
,
3790 rsp
->barrier_sequence
);
3791 smp_mb__before_atomic();
3792 atomic_inc(&rsp
->barrier_cpu_count
);
3793 __call_rcu(&rdp
->barrier_head
,
3794 rcu_barrier_callback
, rsp
, cpu
, 0);
3796 } else if (READ_ONCE(rdp
->qlen
)) {
3797 _rcu_barrier_trace(rsp
, "OnlineQ", cpu
,
3798 rsp
->barrier_sequence
);
3799 smp_call_function_single(cpu
, rcu_barrier_func
, rsp
, 1);
3801 _rcu_barrier_trace(rsp
, "OnlineNQ", cpu
,
3802 rsp
->barrier_sequence
);
3808 * Now that we have an rcu_barrier_callback() callback on each
3809 * CPU, and thus each counted, remove the initial count.
3811 if (atomic_dec_and_test(&rsp
->barrier_cpu_count
))
3812 complete(&rsp
->barrier_completion
);
3814 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
3815 wait_for_completion(&rsp
->barrier_completion
);
3817 /* Mark the end of the barrier operation. */
3818 _rcu_barrier_trace(rsp
, "Inc2", -1, rsp
->barrier_sequence
);
3819 rcu_seq_end(&rsp
->barrier_sequence
);
3821 /* Other rcu_barrier() invocations can now safely proceed. */
3822 mutex_unlock(&rsp
->barrier_mutex
);
3826 * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
3828 void rcu_barrier_bh(void)
3830 _rcu_barrier(&rcu_bh_state
);
3832 EXPORT_SYMBOL_GPL(rcu_barrier_bh
);
3835 * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
3837 void rcu_barrier_sched(void)
3839 _rcu_barrier(&rcu_sched_state
);
3841 EXPORT_SYMBOL_GPL(rcu_barrier_sched
);
3844 * Propagate ->qsinitmask bits up the rcu_node tree to account for the
3845 * first CPU in a given leaf rcu_node structure coming online. The caller
3846 * must hold the corresponding leaf rcu_node ->lock with interrrupts
3849 static void rcu_init_new_rnp(struct rcu_node
*rnp_leaf
)
3852 struct rcu_node
*rnp
= rnp_leaf
;
3855 mask
= rnp
->grpmask
;
3859 raw_spin_lock(&rnp
->lock
); /* Interrupts already disabled. */
3860 rnp
->qsmaskinit
|= mask
;
3861 raw_spin_unlock(&rnp
->lock
); /* Interrupts remain disabled. */
3866 * Do boot-time initialization of a CPU's per-CPU RCU data.
3869 rcu_boot_init_percpu_data(int cpu
, struct rcu_state
*rsp
)
3871 unsigned long flags
;
3872 struct rcu_data
*rdp
= per_cpu_ptr(rsp
->rda
, cpu
);
3873 struct rcu_node
*rnp
= rcu_get_root(rsp
);
3875 /* Set up local state, ensuring consistent view of global state. */
3876 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
3877 rdp
->grpmask
= 1UL << (cpu
- rdp
->mynode
->grplo
);
3878 rdp
->dynticks
= &per_cpu(rcu_dynticks
, cpu
);
3879 WARN_ON_ONCE(rdp
->dynticks
->dynticks_nesting
!= DYNTICK_TASK_EXIT_IDLE
);
3880 WARN_ON_ONCE(atomic_read(&rdp
->dynticks
->dynticks
) != 1);
3883 mutex_init(&rdp
->exp_funnel_mutex
);
3884 rcu_boot_init_nocb_percpu_data(rdp
);
3885 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
3889 * Initialize a CPU's per-CPU RCU data. Note that only one online or
3890 * offline event can be happening at a given time. Note also that we
3891 * can accept some slop in the rsp->completed access due to the fact
3892 * that this CPU cannot possibly have any RCU callbacks in flight yet.
3895 rcu_init_percpu_data(int cpu
, struct rcu_state
*rsp
)
3897 unsigned long flags
;
3899 struct rcu_data
*rdp
= per_cpu_ptr(rsp
->rda
, cpu
);
3900 struct rcu_node
*rnp
= rcu_get_root(rsp
);
3902 /* Set up local state, ensuring consistent view of global state. */
3903 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
3904 rdp
->beenonline
= 1; /* We have now been online. */
3905 rdp
->qlen_last_fqs_check
= 0;
3906 rdp
->n_force_qs_snap
= rsp
->n_force_qs
;
3907 rdp
->blimit
= blimit
;
3909 init_callback_list(rdp
); /* Re-enable callbacks on this CPU. */
3910 rdp
->dynticks
->dynticks_nesting
= DYNTICK_TASK_EXIT_IDLE
;
3911 rcu_sysidle_init_percpu_data(rdp
->dynticks
);
3912 atomic_set(&rdp
->dynticks
->dynticks
,
3913 (atomic_read(&rdp
->dynticks
->dynticks
) & ~0x1) + 1);
3914 raw_spin_unlock(&rnp
->lock
); /* irqs remain disabled. */
3917 * Add CPU to leaf rcu_node pending-online bitmask. Any needed
3918 * propagation up the rcu_node tree will happen at the beginning
3919 * of the next grace period.
3922 mask
= rdp
->grpmask
;
3923 raw_spin_lock(&rnp
->lock
); /* irqs already disabled. */
3924 smp_mb__after_unlock_lock();
3925 rnp
->qsmaskinitnext
|= mask
;
3926 rdp
->gpnum
= rnp
->completed
; /* Make CPU later note any new GP. */
3927 rdp
->completed
= rnp
->completed
;
3928 rdp
->passed_quiesce
= false;
3929 rdp
->rcu_qs_ctr_snap
= per_cpu(rcu_qs_ctr
, cpu
);
3930 rdp
->qs_pending
= false;
3931 trace_rcu_grace_period(rsp
->name
, rdp
->gpnum
, TPS("cpuonl"));
3932 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
3935 static void rcu_prepare_cpu(int cpu
)
3937 struct rcu_state
*rsp
;
3939 for_each_rcu_flavor(rsp
)
3940 rcu_init_percpu_data(cpu
, rsp
);
3944 * Handle CPU online/offline notification events.
3946 int rcu_cpu_notify(struct notifier_block
*self
,
3947 unsigned long action
, void *hcpu
)
3949 long cpu
= (long)hcpu
;
3950 struct rcu_data
*rdp
= per_cpu_ptr(rcu_state_p
->rda
, cpu
);
3951 struct rcu_node
*rnp
= rdp
->mynode
;
3952 struct rcu_state
*rsp
;
3955 case CPU_UP_PREPARE
:
3956 case CPU_UP_PREPARE_FROZEN
:
3957 rcu_prepare_cpu(cpu
);
3958 rcu_prepare_kthreads(cpu
);
3959 rcu_spawn_all_nocb_kthreads(cpu
);
3962 case CPU_DOWN_FAILED
:
3963 rcu_boost_kthread_setaffinity(rnp
, -1);
3965 case CPU_DOWN_PREPARE
:
3966 rcu_boost_kthread_setaffinity(rnp
, cpu
);
3969 case CPU_DYING_FROZEN
:
3970 for_each_rcu_flavor(rsp
)
3971 rcu_cleanup_dying_cpu(rsp
);
3973 case CPU_DYING_IDLE
:
3974 for_each_rcu_flavor(rsp
) {
3975 rcu_cleanup_dying_idle_cpu(cpu
, rsp
);
3979 case CPU_DEAD_FROZEN
:
3980 case CPU_UP_CANCELED
:
3981 case CPU_UP_CANCELED_FROZEN
:
3982 for_each_rcu_flavor(rsp
) {
3983 rcu_cleanup_dead_cpu(cpu
, rsp
);
3984 do_nocb_deferred_wakeup(per_cpu_ptr(rsp
->rda
, cpu
));
3993 static int rcu_pm_notify(struct notifier_block
*self
,
3994 unsigned long action
, void *hcpu
)
3997 case PM_HIBERNATION_PREPARE
:
3998 case PM_SUSPEND_PREPARE
:
3999 if (nr_cpu_ids
<= 256) /* Expediting bad for large systems. */
4002 case PM_POST_HIBERNATION
:
4003 case PM_POST_SUSPEND
:
4004 if (nr_cpu_ids
<= 256) /* Expediting bad for large systems. */
4005 rcu_unexpedite_gp();
4014 * Spawn the kthreads that handle each RCU flavor's grace periods.
4016 static int __init
rcu_spawn_gp_kthread(void)
4018 unsigned long flags
;
4019 int kthread_prio_in
= kthread_prio
;
4020 struct rcu_node
*rnp
;
4021 struct rcu_state
*rsp
;
4022 struct sched_param sp
;
4023 struct task_struct
*t
;
4025 /* Force priority into range. */
4026 if (IS_ENABLED(CONFIG_RCU_BOOST
) && kthread_prio
< 1)
4028 else if (kthread_prio
< 0)
4030 else if (kthread_prio
> 99)
4032 if (kthread_prio
!= kthread_prio_in
)
4033 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
4034 kthread_prio
, kthread_prio_in
);
4036 rcu_scheduler_fully_active
= 1;
4037 for_each_rcu_flavor(rsp
) {
4038 t
= kthread_create(rcu_gp_kthread
, rsp
, "%s", rsp
->name
);
4040 rnp
= rcu_get_root(rsp
);
4041 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
4042 rsp
->gp_kthread
= t
;
4044 sp
.sched_priority
= kthread_prio
;
4045 sched_setscheduler_nocheck(t
, SCHED_FIFO
, &sp
);
4048 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
4050 rcu_spawn_nocb_kthreads();
4051 rcu_spawn_boost_kthreads();
4054 early_initcall(rcu_spawn_gp_kthread
);
4057 * This function is invoked towards the end of the scheduler's initialization
4058 * process. Before this is called, the idle task might contain
4059 * RCU read-side critical sections (during which time, this idle
4060 * task is booting the system). After this function is called, the
4061 * idle tasks are prohibited from containing RCU read-side critical
4062 * sections. This function also enables RCU lockdep checking.
4064 void rcu_scheduler_starting(void)
4066 WARN_ON(num_online_cpus() != 1);
4067 WARN_ON(nr_context_switches() > 0);
4068 rcu_scheduler_active
= 1;
4072 * Compute the per-level fanout, either using the exact fanout specified
4073 * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
4075 static void __init
rcu_init_levelspread(int *levelspread
, const int *levelcnt
)
4079 if (rcu_fanout_exact
) {
4080 levelspread
[rcu_num_lvls
- 1] = rcu_fanout_leaf
;
4081 for (i
= rcu_num_lvls
- 2; i
>= 0; i
--)
4082 levelspread
[i
] = RCU_FANOUT
;
4088 for (i
= rcu_num_lvls
- 1; i
>= 0; i
--) {
4090 levelspread
[i
] = (cprv
+ ccur
- 1) / ccur
;
4097 * Helper function for rcu_init() that initializes one rcu_state structure.
4099 static void __init
rcu_init_one(struct rcu_state
*rsp
,
4100 struct rcu_data __percpu
*rda
)
4102 static const char * const buf
[] = RCU_NODE_NAME_INIT
;
4103 static const char * const fqs
[] = RCU_FQS_NAME_INIT
;
4104 static const char * const exp
[] = RCU_EXP_NAME_INIT
;
4105 static const char * const exp_sched
[] = RCU_EXP_SCHED_NAME_INIT
;
4106 static u8 fl_mask
= 0x1;
4108 int levelcnt
[RCU_NUM_LVLS
]; /* # nodes in each level. */
4109 int levelspread
[RCU_NUM_LVLS
]; /* kids/node in each level. */
4113 struct rcu_node
*rnp
;
4115 BUILD_BUG_ON(RCU_NUM_LVLS
> ARRAY_SIZE(buf
)); /* Fix buf[] init! */
4117 /* Silence gcc 4.8 false positive about array index out of range. */
4118 if (rcu_num_lvls
<= 0 || rcu_num_lvls
> RCU_NUM_LVLS
)
4119 panic("rcu_init_one: rcu_num_lvls out of range");
4121 /* Initialize the level-tracking arrays. */
4123 for (i
= 0; i
< rcu_num_lvls
; i
++)
4124 levelcnt
[i
] = num_rcu_lvl
[i
];
4125 for (i
= 1; i
< rcu_num_lvls
; i
++)
4126 rsp
->level
[i
] = rsp
->level
[i
- 1] + levelcnt
[i
- 1];
4127 rcu_init_levelspread(levelspread
, levelcnt
);
4128 rsp
->flavor_mask
= fl_mask
;
4131 /* Initialize the elements themselves, starting from the leaves. */
4133 for (i
= rcu_num_lvls
- 1; i
>= 0; i
--) {
4134 cpustride
*= levelspread
[i
];
4135 rnp
= rsp
->level
[i
];
4136 for (j
= 0; j
< levelcnt
[i
]; j
++, rnp
++) {
4137 raw_spin_lock_init(&rnp
->lock
);
4138 lockdep_set_class_and_name(&rnp
->lock
,
4139 &rcu_node_class
[i
], buf
[i
]);
4140 raw_spin_lock_init(&rnp
->fqslock
);
4141 lockdep_set_class_and_name(&rnp
->fqslock
,
4142 &rcu_fqs_class
[i
], fqs
[i
]);
4143 rnp
->gpnum
= rsp
->gpnum
;
4144 rnp
->completed
= rsp
->completed
;
4146 rnp
->qsmaskinit
= 0;
4147 rnp
->grplo
= j
* cpustride
;
4148 rnp
->grphi
= (j
+ 1) * cpustride
- 1;
4149 if (rnp
->grphi
>= nr_cpu_ids
)
4150 rnp
->grphi
= nr_cpu_ids
- 1;
4156 rnp
->grpnum
= j
% levelspread
[i
- 1];
4157 rnp
->grpmask
= 1UL << rnp
->grpnum
;
4158 rnp
->parent
= rsp
->level
[i
- 1] +
4159 j
/ levelspread
[i
- 1];
4162 INIT_LIST_HEAD(&rnp
->blkd_tasks
);
4163 rcu_init_one_nocb(rnp
);
4164 mutex_init(&rnp
->exp_funnel_mutex
);
4165 if (rsp
== &rcu_sched_state
)
4166 lockdep_set_class_and_name(
4167 &rnp
->exp_funnel_mutex
,
4168 &rcu_exp_sched_class
[i
], exp_sched
[i
]);
4170 lockdep_set_class_and_name(
4171 &rnp
->exp_funnel_mutex
,
4172 &rcu_exp_class
[i
], exp
[i
]);
4176 init_waitqueue_head(&rsp
->gp_wq
);
4177 rnp
= rsp
->level
[rcu_num_lvls
- 1];
4178 for_each_possible_cpu(i
) {
4179 while (i
> rnp
->grphi
)
4181 per_cpu_ptr(rsp
->rda
, i
)->mynode
= rnp
;
4182 rcu_boot_init_percpu_data(i
, rsp
);
4184 list_add(&rsp
->flavors
, &rcu_struct_flavors
);
4188 * Compute the rcu_node tree geometry from kernel parameters. This cannot
4189 * replace the definitions in tree.h because those are needed to size
4190 * the ->node array in the rcu_state structure.
4192 static void __init
rcu_init_geometry(void)
4196 int rcu_capacity
[RCU_NUM_LVLS
];
4199 * Initialize any unspecified boot parameters.
4200 * The default values of jiffies_till_first_fqs and
4201 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
4202 * value, which is a function of HZ, then adding one for each
4203 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
4205 d
= RCU_JIFFIES_TILL_FORCE_QS
+ nr_cpu_ids
/ RCU_JIFFIES_FQS_DIV
;
4206 if (jiffies_till_first_fqs
== ULONG_MAX
)
4207 jiffies_till_first_fqs
= d
;
4208 if (jiffies_till_next_fqs
== ULONG_MAX
)
4209 jiffies_till_next_fqs
= d
;
4211 /* If the compile-time values are accurate, just leave. */
4212 if (rcu_fanout_leaf
== RCU_FANOUT_LEAF
&&
4213 nr_cpu_ids
== NR_CPUS
)
4215 pr_info("RCU: Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%d\n",
4216 rcu_fanout_leaf
, nr_cpu_ids
);
4219 * The boot-time rcu_fanout_leaf parameter is only permitted
4220 * to increase the leaf-level fanout, not decrease it. Of course,
4221 * the leaf-level fanout cannot exceed the number of bits in
4222 * the rcu_node masks. Complain and fall back to the compile-
4223 * time values if these limits are exceeded.
4225 if (rcu_fanout_leaf
< RCU_FANOUT_LEAF
||
4226 rcu_fanout_leaf
> sizeof(unsigned long) * 8) {
4227 rcu_fanout_leaf
= RCU_FANOUT_LEAF
;
4233 * Compute number of nodes that can be handled an rcu_node tree
4234 * with the given number of levels.
4236 rcu_capacity
[0] = rcu_fanout_leaf
;
4237 for (i
= 1; i
< RCU_NUM_LVLS
; i
++)
4238 rcu_capacity
[i
] = rcu_capacity
[i
- 1] * RCU_FANOUT
;
4241 * The tree must be able to accommodate the configured number of CPUs.
4242 * If this limit is exceeded than we have a serious problem elsewhere.
4244 if (nr_cpu_ids
> rcu_capacity
[RCU_NUM_LVLS
- 1])
4245 panic("rcu_init_geometry: rcu_capacity[] is too small");
4247 /* Calculate the number of levels in the tree. */
4248 for (i
= 0; nr_cpu_ids
> rcu_capacity
[i
]; i
++) {
4250 rcu_num_lvls
= i
+ 1;
4252 /* Calculate the number of rcu_nodes at each level of the tree. */
4253 for (i
= 0; i
< rcu_num_lvls
; i
++) {
4254 int cap
= rcu_capacity
[(rcu_num_lvls
- 1) - i
];
4255 num_rcu_lvl
[i
] = DIV_ROUND_UP(nr_cpu_ids
, cap
);
4258 /* Calculate the total number of rcu_node structures. */
4260 for (i
= 0; i
< rcu_num_lvls
; i
++)
4261 rcu_num_nodes
+= num_rcu_lvl
[i
];
4265 * Dump out the structure of the rcu_node combining tree associated
4266 * with the rcu_state structure referenced by rsp.
4268 static void __init
rcu_dump_rcu_node_tree(struct rcu_state
*rsp
)
4271 struct rcu_node
*rnp
;
4273 pr_info("rcu_node tree layout dump\n");
4275 rcu_for_each_node_breadth_first(rsp
, rnp
) {
4276 if (rnp
->level
!= level
) {
4281 pr_cont("%d:%d ^%d ", rnp
->grplo
, rnp
->grphi
, rnp
->grpnum
);
4286 void __init
rcu_init(void)
4290 rcu_early_boot_tests();
4292 rcu_bootup_announce();
4293 rcu_init_geometry();
4294 rcu_init_one(&rcu_bh_state
, &rcu_bh_data
);
4295 rcu_init_one(&rcu_sched_state
, &rcu_sched_data
);
4297 rcu_dump_rcu_node_tree(&rcu_sched_state
);
4298 __rcu_init_preempt();
4299 open_softirq(RCU_SOFTIRQ
, rcu_process_callbacks
);
4302 * We don't need protection against CPU-hotplug here because
4303 * this is called early in boot, before either interrupts
4304 * or the scheduler are operational.
4306 cpu_notifier(rcu_cpu_notify
, 0);
4307 pm_notifier(rcu_pm_notify
, 0);
4308 for_each_online_cpu(cpu
)
4309 rcu_cpu_notify(NULL
, CPU_UP_PREPARE
, (void *)(long)cpu
);
4312 #include "tree_plugin.h"