1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Read-Copy Update mechanism for mutual exclusion (tree-based version)
4 * Internal non-public definitions that provide either classic
5 * or preemptible semantics.
7 * Copyright Red Hat, 2009
8 * Copyright IBM Corporation, 2009
10 * Author: Ingo Molnar <mingo@elte.hu>
11 * Paul E. McKenney <paulmck@linux.ibm.com>
14 #include <linux/delay.h>
15 #include <linux/gfp.h>
16 #include <linux/oom.h>
17 #include <linux/sched/debug.h>
18 #include <linux/smpboot.h>
19 #include <linux/sched/isolation.h>
20 #include <uapi/linux/sched/types.h>
21 #include "../time/tick-internal.h"
23 #ifdef CONFIG_RCU_BOOST
24 #include "../locking/rtmutex_common.h"
25 #else /* #ifdef CONFIG_RCU_BOOST */
28 * Some architectures do not define rt_mutexes, but if !CONFIG_RCU_BOOST,
29 * all uses are in dead code. Provide a definition to keep the compiler
30 * happy, but add WARN_ON_ONCE() to complain if used in the wrong place.
31 * This probably needs to be excluded from -rt builds.
33 #define rt_mutex_owner(a) ({ WARN_ON_ONCE(1); NULL; })
34 #define rt_mutex_futex_unlock(x) WARN_ON_ONCE(1)
36 #endif /* #else #ifdef CONFIG_RCU_BOOST */
38 #ifdef CONFIG_RCU_NOCB_CPU
39 static cpumask_var_t rcu_nocb_mask
; /* CPUs to have callbacks offloaded. */
40 static bool __read_mostly rcu_nocb_poll
; /* Offload kthread are to poll. */
41 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
44 * Check the RCU kernel configuration parameters and print informative
45 * messages about anything out of the ordinary.
47 static void __init
rcu_bootup_announce_oddness(void)
49 if (IS_ENABLED(CONFIG_RCU_TRACE
))
50 pr_info("\tRCU event tracing is enabled.\n");
51 if ((IS_ENABLED(CONFIG_64BIT
) && RCU_FANOUT
!= 64) ||
52 (!IS_ENABLED(CONFIG_64BIT
) && RCU_FANOUT
!= 32))
53 pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d.\n",
56 pr_info("\tHierarchical RCU autobalancing is disabled.\n");
57 if (IS_ENABLED(CONFIG_RCU_FAST_NO_HZ
))
58 pr_info("\tRCU dyntick-idle grace-period acceleration is enabled.\n");
59 if (IS_ENABLED(CONFIG_PROVE_RCU
))
60 pr_info("\tRCU lockdep checking is enabled.\n");
61 if (RCU_NUM_LVLS
>= 4)
62 pr_info("\tFour(or more)-level hierarchy is enabled.\n");
63 if (RCU_FANOUT_LEAF
!= 16)
64 pr_info("\tBuild-time adjustment of leaf fanout to %d.\n",
66 if (rcu_fanout_leaf
!= RCU_FANOUT_LEAF
)
67 pr_info("\tBoot-time adjustment of leaf fanout to %d.\n",
69 if (nr_cpu_ids
!= NR_CPUS
)
70 pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%u.\n", NR_CPUS
, nr_cpu_ids
);
71 #ifdef CONFIG_RCU_BOOST
72 pr_info("\tRCU priority boosting: priority %d delay %d ms.\n",
73 kthread_prio
, CONFIG_RCU_BOOST_DELAY
);
75 if (blimit
!= DEFAULT_RCU_BLIMIT
)
76 pr_info("\tBoot-time adjustment of callback invocation limit to %ld.\n", blimit
);
77 if (qhimark
!= DEFAULT_RCU_QHIMARK
)
78 pr_info("\tBoot-time adjustment of callback high-water mark to %ld.\n", qhimark
);
79 if (qlowmark
!= DEFAULT_RCU_QLOMARK
)
80 pr_info("\tBoot-time adjustment of callback low-water mark to %ld.\n", qlowmark
);
81 if (jiffies_till_first_fqs
!= ULONG_MAX
)
82 pr_info("\tBoot-time adjustment of first FQS scan delay to %ld jiffies.\n", jiffies_till_first_fqs
);
83 if (jiffies_till_next_fqs
!= ULONG_MAX
)
84 pr_info("\tBoot-time adjustment of subsequent FQS scan delay to %ld jiffies.\n", jiffies_till_next_fqs
);
85 if (jiffies_till_sched_qs
!= ULONG_MAX
)
86 pr_info("\tBoot-time adjustment of scheduler-enlistment delay to %ld jiffies.\n", jiffies_till_sched_qs
);
87 if (rcu_kick_kthreads
)
88 pr_info("\tKick kthreads if too-long grace period.\n");
89 if (IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD
))
90 pr_info("\tRCU callback double-/use-after-free debug enabled.\n");
92 pr_info("\tRCU debug GP pre-init slowdown %d jiffies.\n", gp_preinit_delay
);
94 pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_init_delay
);
96 pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_cleanup_delay
);
97 if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG
))
98 pr_info("\tRCU debug extended QS entry/exit.\n");
99 rcupdate_announce_bootup_oddness();
102 #ifdef CONFIG_PREEMPT_RCU
104 static void rcu_report_exp_rnp(struct rcu_node
*rnp
, bool wake
);
105 static void rcu_read_unlock_special(struct task_struct
*t
);
108 * Tell them what RCU they are running.
110 static void __init
rcu_bootup_announce(void)
112 pr_info("Preemptible hierarchical RCU implementation.\n");
113 rcu_bootup_announce_oddness();
116 /* Flags for rcu_preempt_ctxt_queue() decision table. */
117 #define RCU_GP_TASKS 0x8
118 #define RCU_EXP_TASKS 0x4
119 #define RCU_GP_BLKD 0x2
120 #define RCU_EXP_BLKD 0x1
123 * Queues a task preempted within an RCU-preempt read-side critical
124 * section into the appropriate location within the ->blkd_tasks list,
125 * depending on the states of any ongoing normal and expedited grace
126 * periods. The ->gp_tasks pointer indicates which element the normal
127 * grace period is waiting on (NULL if none), and the ->exp_tasks pointer
128 * indicates which element the expedited grace period is waiting on (again,
129 * NULL if none). If a grace period is waiting on a given element in the
130 * ->blkd_tasks list, it also waits on all subsequent elements. Thus,
131 * adding a task to the tail of the list blocks any grace period that is
132 * already waiting on one of the elements. In contrast, adding a task
133 * to the head of the list won't block any grace period that is already
134 * waiting on one of the elements.
136 * This queuing is imprecise, and can sometimes make an ongoing grace
137 * period wait for a task that is not strictly speaking blocking it.
138 * Given the choice, we needlessly block a normal grace period rather than
139 * blocking an expedited grace period.
141 * Note that an endless sequence of expedited grace periods still cannot
142 * indefinitely postpone a normal grace period. Eventually, all of the
143 * fixed number of preempted tasks blocking the normal grace period that are
144 * not also blocking the expedited grace period will resume and complete
145 * their RCU read-side critical sections. At that point, the ->gp_tasks
146 * pointer will equal the ->exp_tasks pointer, at which point the end of
147 * the corresponding expedited grace period will also be the end of the
148 * normal grace period.
150 static void rcu_preempt_ctxt_queue(struct rcu_node
*rnp
, struct rcu_data
*rdp
)
151 __releases(rnp
->lock
) /* But leaves rrupts disabled. */
153 int blkd_state
= (rnp
->gp_tasks
? RCU_GP_TASKS
: 0) +
154 (rnp
->exp_tasks
? RCU_EXP_TASKS
: 0) +
155 (rnp
->qsmask
& rdp
->grpmask
? RCU_GP_BLKD
: 0) +
156 (rnp
->expmask
& rdp
->grpmask
? RCU_EXP_BLKD
: 0);
157 struct task_struct
*t
= current
;
159 raw_lockdep_assert_held_rcu_node(rnp
);
160 WARN_ON_ONCE(rdp
->mynode
!= rnp
);
161 WARN_ON_ONCE(!rcu_is_leaf_node(rnp
));
162 /* RCU better not be waiting on newly onlined CPUs! */
163 WARN_ON_ONCE(rnp
->qsmaskinitnext
& ~rnp
->qsmaskinit
& rnp
->qsmask
&
167 * Decide where to queue the newly blocked task. In theory,
168 * this could be an if-statement. In practice, when I tried
169 * that, it was quite messy.
171 switch (blkd_state
) {
174 case RCU_EXP_TASKS
+ RCU_GP_BLKD
:
176 case RCU_GP_TASKS
+ RCU_EXP_TASKS
:
179 * Blocking neither GP, or first task blocking the normal
180 * GP but not blocking the already-waiting expedited GP.
181 * Queue at the head of the list to avoid unnecessarily
182 * blocking the already-waiting GPs.
184 list_add(&t
->rcu_node_entry
, &rnp
->blkd_tasks
);
189 case RCU_GP_BLKD
+ RCU_EXP_BLKD
:
190 case RCU_GP_TASKS
+ RCU_EXP_BLKD
:
191 case RCU_GP_TASKS
+ RCU_GP_BLKD
+ RCU_EXP_BLKD
:
192 case RCU_GP_TASKS
+ RCU_EXP_TASKS
+ RCU_GP_BLKD
+ RCU_EXP_BLKD
:
195 * First task arriving that blocks either GP, or first task
196 * arriving that blocks the expedited GP (with the normal
197 * GP already waiting), or a task arriving that blocks
198 * both GPs with both GPs already waiting. Queue at the
199 * tail of the list to avoid any GP waiting on any of the
200 * already queued tasks that are not blocking it.
202 list_add_tail(&t
->rcu_node_entry
, &rnp
->blkd_tasks
);
205 case RCU_EXP_TASKS
+ RCU_EXP_BLKD
:
206 case RCU_EXP_TASKS
+ RCU_GP_BLKD
+ RCU_EXP_BLKD
:
207 case RCU_GP_TASKS
+ RCU_EXP_TASKS
+ RCU_EXP_BLKD
:
210 * Second or subsequent task blocking the expedited GP.
211 * The task either does not block the normal GP, or is the
212 * first task blocking the normal GP. Queue just after
213 * the first task blocking the expedited GP.
215 list_add(&t
->rcu_node_entry
, rnp
->exp_tasks
);
218 case RCU_GP_TASKS
+ RCU_GP_BLKD
:
219 case RCU_GP_TASKS
+ RCU_EXP_TASKS
+ RCU_GP_BLKD
:
222 * Second or subsequent task blocking the normal GP.
223 * The task does not block the expedited GP. Queue just
224 * after the first task blocking the normal GP.
226 list_add(&t
->rcu_node_entry
, rnp
->gp_tasks
);
231 /* Yet another exercise in excessive paranoia. */
237 * We have now queued the task. If it was the first one to
238 * block either grace period, update the ->gp_tasks and/or
239 * ->exp_tasks pointers, respectively, to reference the newly
242 if (!rnp
->gp_tasks
&& (blkd_state
& RCU_GP_BLKD
)) {
243 rnp
->gp_tasks
= &t
->rcu_node_entry
;
244 WARN_ON_ONCE(rnp
->completedqs
== rnp
->gp_seq
);
246 if (!rnp
->exp_tasks
&& (blkd_state
& RCU_EXP_BLKD
))
247 rnp
->exp_tasks
= &t
->rcu_node_entry
;
248 WARN_ON_ONCE(!(blkd_state
& RCU_GP_BLKD
) !=
249 !(rnp
->qsmask
& rdp
->grpmask
));
250 WARN_ON_ONCE(!(blkd_state
& RCU_EXP_BLKD
) !=
251 !(rnp
->expmask
& rdp
->grpmask
));
252 raw_spin_unlock_rcu_node(rnp
); /* interrupts remain disabled. */
255 * Report the quiescent state for the expedited GP. This expedited
256 * GP should not be able to end until we report, so there should be
257 * no need to check for a subsequent expedited GP. (Though we are
258 * still in a quiescent state in any case.)
260 if (blkd_state
& RCU_EXP_BLKD
&& rdp
->deferred_qs
)
261 rcu_report_exp_rdp(rdp
);
263 WARN_ON_ONCE(rdp
->deferred_qs
);
267 * Record a preemptible-RCU quiescent state for the specified CPU.
268 * Note that this does not necessarily mean that the task currently running
269 * on the CPU is in a quiescent state: Instead, it means that the current
270 * grace period need not wait on any RCU read-side critical section that
271 * starts later on this CPU. It also means that if the current task is
272 * in an RCU read-side critical section, it has already added itself to
273 * some leaf rcu_node structure's ->blkd_tasks list. In addition to the
274 * current task, there might be any number of other tasks blocked while
275 * in an RCU read-side critical section.
277 * Callers to this function must disable preemption.
279 static void rcu_qs(void)
281 RCU_LOCKDEP_WARN(preemptible(), "rcu_qs() invoked with preemption enabled!!!\n");
282 if (__this_cpu_read(rcu_data
.cpu_no_qs
.s
)) {
283 trace_rcu_grace_period(TPS("rcu_preempt"),
284 __this_cpu_read(rcu_data
.gp_seq
),
286 __this_cpu_write(rcu_data
.cpu_no_qs
.b
.norm
, false);
287 barrier(); /* Coordinate with rcu_flavor_sched_clock_irq(). */
288 WRITE_ONCE(current
->rcu_read_unlock_special
.b
.need_qs
, false);
293 * We have entered the scheduler, and the current task might soon be
294 * context-switched away from. If this task is in an RCU read-side
295 * critical section, we will no longer be able to rely on the CPU to
296 * record that fact, so we enqueue the task on the blkd_tasks list.
297 * The task will dequeue itself when it exits the outermost enclosing
298 * RCU read-side critical section. Therefore, the current grace period
299 * cannot be permitted to complete until the blkd_tasks list entries
300 * predating the current grace period drain, in other words, until
301 * rnp->gp_tasks becomes NULL.
303 * Caller must disable interrupts.
305 void rcu_note_context_switch(bool preempt
)
307 struct task_struct
*t
= current
;
308 struct rcu_data
*rdp
= this_cpu_ptr(&rcu_data
);
309 struct rcu_node
*rnp
;
311 barrier(); /* Avoid RCU read-side critical sections leaking down. */
312 trace_rcu_utilization(TPS("Start context switch"));
313 lockdep_assert_irqs_disabled();
314 WARN_ON_ONCE(!preempt
&& t
->rcu_read_lock_nesting
> 0);
315 if (t
->rcu_read_lock_nesting
> 0 &&
316 !t
->rcu_read_unlock_special
.b
.blocked
) {
318 /* Possibly blocking in an RCU read-side critical section. */
320 raw_spin_lock_rcu_node(rnp
);
321 t
->rcu_read_unlock_special
.b
.blocked
= true;
322 t
->rcu_blocked_node
= rnp
;
325 * Verify the CPU's sanity, trace the preemption, and
326 * then queue the task as required based on the states
327 * of any ongoing and expedited grace periods.
329 WARN_ON_ONCE((rdp
->grpmask
& rcu_rnp_online_cpus(rnp
)) == 0);
330 WARN_ON_ONCE(!list_empty(&t
->rcu_node_entry
));
331 trace_rcu_preempt_task(rcu_state
.name
,
333 (rnp
->qsmask
& rdp
->grpmask
)
335 : rcu_seq_snap(&rnp
->gp_seq
));
336 rcu_preempt_ctxt_queue(rnp
, rdp
);
337 } else if (t
->rcu_read_lock_nesting
< 0 &&
338 t
->rcu_read_unlock_special
.s
) {
341 * Complete exit from RCU read-side critical section on
342 * behalf of preempted instance of __rcu_read_unlock().
344 rcu_read_unlock_special(t
);
345 rcu_preempt_deferred_qs(t
);
347 rcu_preempt_deferred_qs(t
);
351 * Either we were not in an RCU read-side critical section to
352 * begin with, or we have now recorded that critical section
353 * globally. Either way, we can now note a quiescent state
354 * for this CPU. Again, if we were in an RCU read-side critical
355 * section, and if that critical section was blocking the current
356 * grace period, then the fact that the task has been enqueued
357 * means that we continue to block the current grace period.
360 if (rdp
->deferred_qs
)
361 rcu_report_exp_rdp(rdp
);
362 trace_rcu_utilization(TPS("End context switch"));
363 barrier(); /* Avoid RCU read-side critical sections leaking up. */
365 EXPORT_SYMBOL_GPL(rcu_note_context_switch
);
368 * Check for preempted RCU readers blocking the current grace period
369 * for the specified rcu_node structure. If the caller needs a reliable
370 * answer, it must hold the rcu_node's ->lock.
372 static int rcu_preempt_blocked_readers_cgp(struct rcu_node
*rnp
)
374 return rnp
->gp_tasks
!= NULL
;
377 /* Bias and limit values for ->rcu_read_lock_nesting. */
378 #define RCU_NEST_BIAS INT_MAX
379 #define RCU_NEST_NMAX (-INT_MAX / 2)
380 #define RCU_NEST_PMAX (INT_MAX / 2)
383 * Preemptible RCU implementation for rcu_read_lock().
384 * Just increment ->rcu_read_lock_nesting, shared state will be updated
387 void __rcu_read_lock(void)
389 current
->rcu_read_lock_nesting
++;
390 if (IS_ENABLED(CONFIG_PROVE_LOCKING
))
391 WARN_ON_ONCE(current
->rcu_read_lock_nesting
> RCU_NEST_PMAX
);
392 barrier(); /* critical section after entry code. */
394 EXPORT_SYMBOL_GPL(__rcu_read_lock
);
397 * Preemptible RCU implementation for rcu_read_unlock().
398 * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
399 * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
400 * invoke rcu_read_unlock_special() to clean up after a context switch
401 * in an RCU read-side critical section and other special cases.
403 void __rcu_read_unlock(void)
405 struct task_struct
*t
= current
;
407 if (t
->rcu_read_lock_nesting
!= 1) {
408 --t
->rcu_read_lock_nesting
;
410 barrier(); /* critical section before exit code. */
411 t
->rcu_read_lock_nesting
= -RCU_NEST_BIAS
;
412 barrier(); /* assign before ->rcu_read_unlock_special load */
413 if (unlikely(READ_ONCE(t
->rcu_read_unlock_special
.s
)))
414 rcu_read_unlock_special(t
);
415 barrier(); /* ->rcu_read_unlock_special load before assign */
416 t
->rcu_read_lock_nesting
= 0;
418 if (IS_ENABLED(CONFIG_PROVE_LOCKING
)) {
419 int rrln
= t
->rcu_read_lock_nesting
;
421 WARN_ON_ONCE(rrln
< 0 && rrln
> RCU_NEST_NMAX
);
424 EXPORT_SYMBOL_GPL(__rcu_read_unlock
);
427 * Advance a ->blkd_tasks-list pointer to the next entry, instead
428 * returning NULL if at the end of the list.
430 static struct list_head
*rcu_next_node_entry(struct task_struct
*t
,
431 struct rcu_node
*rnp
)
433 struct list_head
*np
;
435 np
= t
->rcu_node_entry
.next
;
436 if (np
== &rnp
->blkd_tasks
)
442 * Return true if the specified rcu_node structure has tasks that were
443 * preempted within an RCU read-side critical section.
445 static bool rcu_preempt_has_tasks(struct rcu_node
*rnp
)
447 return !list_empty(&rnp
->blkd_tasks
);
451 * Report deferred quiescent states. The deferral time can
452 * be quite short, for example, in the case of the call from
453 * rcu_read_unlock_special().
456 rcu_preempt_deferred_qs_irqrestore(struct task_struct
*t
, unsigned long flags
)
461 struct list_head
*np
;
462 bool drop_boost_mutex
= false;
463 struct rcu_data
*rdp
;
464 struct rcu_node
*rnp
;
465 union rcu_special special
;
468 * If RCU core is waiting for this CPU to exit its critical section,
469 * report the fact that it has exited. Because irqs are disabled,
470 * t->rcu_read_unlock_special cannot change.
472 special
= t
->rcu_read_unlock_special
;
473 rdp
= this_cpu_ptr(&rcu_data
);
474 if (!special
.s
&& !rdp
->deferred_qs
) {
475 local_irq_restore(flags
);
478 if (special
.b
.need_qs
) {
480 t
->rcu_read_unlock_special
.b
.need_qs
= false;
481 if (!t
->rcu_read_unlock_special
.s
&& !rdp
->deferred_qs
) {
482 local_irq_restore(flags
);
488 * Respond to a request by an expedited grace period for a
489 * quiescent state from this CPU. Note that requests from
490 * tasks are handled when removing the task from the
491 * blocked-tasks list below.
493 if (rdp
->deferred_qs
) {
494 rcu_report_exp_rdp(rdp
);
495 if (!t
->rcu_read_unlock_special
.s
) {
496 local_irq_restore(flags
);
501 /* Clean up if blocked during RCU read-side critical section. */
502 if (special
.b
.blocked
) {
503 t
->rcu_read_unlock_special
.b
.blocked
= false;
506 * Remove this task from the list it blocked on. The task
507 * now remains queued on the rcu_node corresponding to the
508 * CPU it first blocked on, so there is no longer any need
509 * to loop. Retain a WARN_ON_ONCE() out of sheer paranoia.
511 rnp
= t
->rcu_blocked_node
;
512 raw_spin_lock_rcu_node(rnp
); /* irqs already disabled. */
513 WARN_ON_ONCE(rnp
!= t
->rcu_blocked_node
);
514 WARN_ON_ONCE(!rcu_is_leaf_node(rnp
));
515 empty_norm
= !rcu_preempt_blocked_readers_cgp(rnp
);
516 WARN_ON_ONCE(rnp
->completedqs
== rnp
->gp_seq
&&
517 (!empty_norm
|| rnp
->qsmask
));
518 empty_exp
= sync_rcu_preempt_exp_done(rnp
);
519 smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
520 np
= rcu_next_node_entry(t
, rnp
);
521 list_del_init(&t
->rcu_node_entry
);
522 t
->rcu_blocked_node
= NULL
;
523 trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
524 rnp
->gp_seq
, t
->pid
);
525 if (&t
->rcu_node_entry
== rnp
->gp_tasks
)
527 if (&t
->rcu_node_entry
== rnp
->exp_tasks
)
529 if (IS_ENABLED(CONFIG_RCU_BOOST
)) {
530 /* Snapshot ->boost_mtx ownership w/rnp->lock held. */
531 drop_boost_mutex
= rt_mutex_owner(&rnp
->boost_mtx
) == t
;
532 if (&t
->rcu_node_entry
== rnp
->boost_tasks
)
533 rnp
->boost_tasks
= np
;
537 * If this was the last task on the current list, and if
538 * we aren't waiting on any CPUs, report the quiescent state.
539 * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
540 * so we must take a snapshot of the expedited state.
542 empty_exp_now
= sync_rcu_preempt_exp_done(rnp
);
543 if (!empty_norm
&& !rcu_preempt_blocked_readers_cgp(rnp
)) {
544 trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
551 rcu_report_unblock_qs_rnp(rnp
, flags
);
553 raw_spin_unlock_irqrestore_rcu_node(rnp
, flags
);
556 /* Unboost if we were boosted. */
557 if (IS_ENABLED(CONFIG_RCU_BOOST
) && drop_boost_mutex
)
558 rt_mutex_futex_unlock(&rnp
->boost_mtx
);
561 * If this was the last task on the expedited lists,
562 * then we need to report up the rcu_node hierarchy.
564 if (!empty_exp
&& empty_exp_now
)
565 rcu_report_exp_rnp(rnp
, true);
567 local_irq_restore(flags
);
572 * Is a deferred quiescent-state pending, and are we also not in
573 * an RCU read-side critical section? It is the caller's responsibility
574 * to ensure it is otherwise safe to report any deferred quiescent
575 * states. The reason for this is that it is safe to report a
576 * quiescent state during context switch even though preemption
577 * is disabled. This function cannot be expected to understand these
578 * nuances, so the caller must handle them.
580 static bool rcu_preempt_need_deferred_qs(struct task_struct
*t
)
582 return (__this_cpu_read(rcu_data
.deferred_qs
) ||
583 READ_ONCE(t
->rcu_read_unlock_special
.s
)) &&
584 t
->rcu_read_lock_nesting
<= 0;
588 * Report a deferred quiescent state if needed and safe to do so.
589 * As with rcu_preempt_need_deferred_qs(), "safe" involves only
590 * not being in an RCU read-side critical section. The caller must
591 * evaluate safety in terms of interrupt, softirq, and preemption
594 static void rcu_preempt_deferred_qs(struct task_struct
*t
)
597 bool couldrecurse
= t
->rcu_read_lock_nesting
>= 0;
599 if (!rcu_preempt_need_deferred_qs(t
))
602 t
->rcu_read_lock_nesting
-= RCU_NEST_BIAS
;
603 local_irq_save(flags
);
604 rcu_preempt_deferred_qs_irqrestore(t
, flags
);
606 t
->rcu_read_lock_nesting
+= RCU_NEST_BIAS
;
610 * Handle special cases during rcu_read_unlock(), such as needing to
611 * notify RCU core processing or task having blocked during the RCU
612 * read-side critical section.
614 static void rcu_read_unlock_special(struct task_struct
*t
)
617 bool preempt_bh_were_disabled
=
618 !!(preempt_count() & (PREEMPT_MASK
| SOFTIRQ_MASK
));
619 bool irqs_were_disabled
;
621 /* NMI handlers cannot block and cannot safely manipulate state. */
625 local_irq_save(flags
);
626 irqs_were_disabled
= irqs_disabled_flags(flags
);
627 if (preempt_bh_were_disabled
|| irqs_were_disabled
) {
628 WRITE_ONCE(t
->rcu_read_unlock_special
.b
.exp_hint
, false);
629 /* Need to defer quiescent state until everything is enabled. */
630 if (irqs_were_disabled
) {
631 /* Enabling irqs does not reschedule, so... */
632 raise_softirq_irqoff(RCU_SOFTIRQ
);
634 /* Enabling BH or preempt does reschedule, so... */
635 set_tsk_need_resched(current
);
636 set_preempt_need_resched();
638 local_irq_restore(flags
);
641 WRITE_ONCE(t
->rcu_read_unlock_special
.b
.exp_hint
, false);
642 rcu_preempt_deferred_qs_irqrestore(t
, flags
);
646 * Check that the list of blocked tasks for the newly completed grace
647 * period is in fact empty. It is a serious bug to complete a grace
648 * period that still has RCU readers blocked! This function must be
649 * invoked -before- updating this rnp's ->gp_seq, and the rnp's ->lock
650 * must be held by the caller.
652 * Also, if there are blocked tasks on the list, they automatically
653 * block the newly created grace period, so set up ->gp_tasks accordingly.
655 static void rcu_preempt_check_blocked_tasks(struct rcu_node
*rnp
)
657 struct task_struct
*t
;
659 RCU_LOCKDEP_WARN(preemptible(), "rcu_preempt_check_blocked_tasks() invoked with preemption enabled!!!\n");
660 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp
)))
661 dump_blkd_tasks(rnp
, 10);
662 if (rcu_preempt_has_tasks(rnp
) &&
663 (rnp
->qsmaskinit
|| rnp
->wait_blkd_tasks
)) {
664 rnp
->gp_tasks
= rnp
->blkd_tasks
.next
;
665 t
= container_of(rnp
->gp_tasks
, struct task_struct
,
667 trace_rcu_unlock_preempted_task(TPS("rcu_preempt-GPS"),
668 rnp
->gp_seq
, t
->pid
);
670 WARN_ON_ONCE(rnp
->qsmask
);
674 * Check for a quiescent state from the current CPU, including voluntary
675 * context switches for Tasks RCU. When a task blocks, the task is
676 * recorded in the corresponding CPU's rcu_node structure, which is checked
677 * elsewhere, hence this function need only check for quiescent states
678 * related to the current CPU, not to those related to tasks.
680 static void rcu_flavor_sched_clock_irq(int user
)
682 struct task_struct
*t
= current
;
684 if (user
|| rcu_is_cpu_rrupt_from_idle()) {
685 rcu_note_voluntary_context_switch(current
);
687 if (t
->rcu_read_lock_nesting
> 0 ||
688 (preempt_count() & (PREEMPT_MASK
| SOFTIRQ_MASK
))) {
689 /* No QS, force context switch if deferred. */
690 if (rcu_preempt_need_deferred_qs(t
)) {
691 set_tsk_need_resched(t
);
692 set_preempt_need_resched();
694 } else if (rcu_preempt_need_deferred_qs(t
)) {
695 rcu_preempt_deferred_qs(t
); /* Report deferred QS. */
697 } else if (!t
->rcu_read_lock_nesting
) {
698 rcu_qs(); /* Report immediate QS. */
702 /* If GP is oldish, ask for help from rcu_read_unlock_special(). */
703 if (t
->rcu_read_lock_nesting
> 0 &&
704 __this_cpu_read(rcu_data
.core_needs_qs
) &&
705 __this_cpu_read(rcu_data
.cpu_no_qs
.b
.norm
) &&
706 !t
->rcu_read_unlock_special
.b
.need_qs
&&
707 time_after(jiffies
, rcu_state
.gp_start
+ HZ
))
708 t
->rcu_read_unlock_special
.b
.need_qs
= true;
712 * Check for a task exiting while in a preemptible-RCU read-side
713 * critical section, clean up if so. No need to issue warnings, as
714 * debug_check_no_locks_held() already does this if lockdep is enabled.
715 * Besides, if this function does anything other than just immediately
716 * return, there was a bug of some sort. Spewing warnings from this
717 * function is like as not to simply obscure important prior warnings.
721 struct task_struct
*t
= current
;
723 if (unlikely(!list_empty(¤t
->rcu_node_entry
))) {
724 t
->rcu_read_lock_nesting
= 1;
726 WRITE_ONCE(t
->rcu_read_unlock_special
.b
.blocked
, true);
727 } else if (unlikely(t
->rcu_read_lock_nesting
)) {
728 t
->rcu_read_lock_nesting
= 1;
733 rcu_preempt_deferred_qs(current
);
737 * Dump the blocked-tasks state, but limit the list dump to the
738 * specified number of elements.
741 dump_blkd_tasks(struct rcu_node
*rnp
, int ncheck
)
745 struct list_head
*lhp
;
747 struct rcu_data
*rdp
;
748 struct rcu_node
*rnp1
;
750 raw_lockdep_assert_held_rcu_node(rnp
);
751 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
752 __func__
, rnp
->grplo
, rnp
->grphi
, rnp
->level
,
753 (long)rnp
->gp_seq
, (long)rnp
->completedqs
);
754 for (rnp1
= rnp
; rnp1
; rnp1
= rnp1
->parent
)
755 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx\n",
756 __func__
, rnp1
->grplo
, rnp1
->grphi
, rnp1
->qsmask
, rnp1
->qsmaskinit
, rnp1
->qsmaskinitnext
);
757 pr_info("%s: ->gp_tasks %p ->boost_tasks %p ->exp_tasks %p\n",
758 __func__
, rnp
->gp_tasks
, rnp
->boost_tasks
, rnp
->exp_tasks
);
759 pr_info("%s: ->blkd_tasks", __func__
);
761 list_for_each(lhp
, &rnp
->blkd_tasks
) {
767 for (cpu
= rnp
->grplo
; cpu
<= rnp
->grphi
; cpu
++) {
768 rdp
= per_cpu_ptr(&rcu_data
, cpu
);
769 onl
= !!(rdp
->grpmask
& rcu_rnp_online_cpus(rnp
));
770 pr_info("\t%d: %c online: %ld(%d) offline: %ld(%d)\n",
772 (long)rdp
->rcu_onl_gp_seq
, rdp
->rcu_onl_gp_flags
,
773 (long)rdp
->rcu_ofl_gp_seq
, rdp
->rcu_ofl_gp_flags
);
777 #else /* #ifdef CONFIG_PREEMPT_RCU */
780 * Tell them what RCU they are running.
782 static void __init
rcu_bootup_announce(void)
784 pr_info("Hierarchical RCU implementation.\n");
785 rcu_bootup_announce_oddness();
789 * Note a quiescent state for PREEMPT=n. Because we do not need to know
790 * how many quiescent states passed, just if there was at least one since
791 * the start of the grace period, this just sets a flag. The caller must
792 * have disabled preemption.
794 static void rcu_qs(void)
796 RCU_LOCKDEP_WARN(preemptible(), "rcu_qs() invoked with preemption enabled!!!");
797 if (!__this_cpu_read(rcu_data
.cpu_no_qs
.s
))
799 trace_rcu_grace_period(TPS("rcu_sched"),
800 __this_cpu_read(rcu_data
.gp_seq
), TPS("cpuqs"));
801 __this_cpu_write(rcu_data
.cpu_no_qs
.b
.norm
, false);
802 if (!__this_cpu_read(rcu_data
.cpu_no_qs
.b
.exp
))
804 __this_cpu_write(rcu_data
.cpu_no_qs
.b
.exp
, false);
805 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data
));
809 * Register an urgently needed quiescent state. If there is an
810 * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
811 * dyntick-idle quiescent state visible to other CPUs, which will in
812 * some cases serve for expedited as well as normal grace periods.
813 * Either way, register a lightweight quiescent state.
815 * The barrier() calls are redundant in the common case when this is
816 * called externally, but just in case this is called from within this
820 void rcu_all_qs(void)
824 if (!raw_cpu_read(rcu_data
.rcu_urgent_qs
))
827 /* Load rcu_urgent_qs before other flags. */
828 if (!smp_load_acquire(this_cpu_ptr(&rcu_data
.rcu_urgent_qs
))) {
832 this_cpu_write(rcu_data
.rcu_urgent_qs
, false);
833 barrier(); /* Avoid RCU read-side critical sections leaking down. */
834 if (unlikely(raw_cpu_read(rcu_data
.rcu_need_heavy_qs
))) {
835 local_irq_save(flags
);
836 rcu_momentary_dyntick_idle();
837 local_irq_restore(flags
);
840 barrier(); /* Avoid RCU read-side critical sections leaking up. */
843 EXPORT_SYMBOL_GPL(rcu_all_qs
);
846 * Note a PREEMPT=n context switch. The caller must have disabled interrupts.
848 void rcu_note_context_switch(bool preempt
)
850 barrier(); /* Avoid RCU read-side critical sections leaking down. */
851 trace_rcu_utilization(TPS("Start context switch"));
853 /* Load rcu_urgent_qs before other flags. */
854 if (!smp_load_acquire(this_cpu_ptr(&rcu_data
.rcu_urgent_qs
)))
856 this_cpu_write(rcu_data
.rcu_urgent_qs
, false);
857 if (unlikely(raw_cpu_read(rcu_data
.rcu_need_heavy_qs
)))
858 rcu_momentary_dyntick_idle();
860 rcu_tasks_qs(current
);
862 trace_rcu_utilization(TPS("End context switch"));
863 barrier(); /* Avoid RCU read-side critical sections leaking up. */
865 EXPORT_SYMBOL_GPL(rcu_note_context_switch
);
868 * Because preemptible RCU does not exist, there are never any preempted
871 static int rcu_preempt_blocked_readers_cgp(struct rcu_node
*rnp
)
877 * Because there is no preemptible RCU, there can be no readers blocked.
879 static bool rcu_preempt_has_tasks(struct rcu_node
*rnp
)
885 * Because there is no preemptible RCU, there can be no deferred quiescent
888 static bool rcu_preempt_need_deferred_qs(struct task_struct
*t
)
892 static void rcu_preempt_deferred_qs(struct task_struct
*t
) { }
895 * Because there is no preemptible RCU, there can be no readers blocked,
896 * so there is no need to check for blocked tasks. So check only for
897 * bogus qsmask values.
899 static void rcu_preempt_check_blocked_tasks(struct rcu_node
*rnp
)
901 WARN_ON_ONCE(rnp
->qsmask
);
905 * Check to see if this CPU is in a non-context-switch quiescent state,
906 * namely user mode and idle loop.
908 static void rcu_flavor_sched_clock_irq(int user
)
910 if (user
|| rcu_is_cpu_rrupt_from_idle()) {
913 * Get here if this CPU took its interrupt from user
914 * mode or from the idle loop, and if this is not a
915 * nested interrupt. In this case, the CPU is in
916 * a quiescent state, so note it.
918 * No memory barrier is required here because rcu_qs()
919 * references only CPU-local variables that other CPUs
920 * neither access nor modify, at least not while the
921 * corresponding CPU is online.
929 * Because preemptible RCU does not exist, tasks cannot possibly exit
930 * while in preemptible RCU read-side critical sections.
937 * Dump the guaranteed-empty blocked-tasks state. Trust but verify.
940 dump_blkd_tasks(struct rcu_node
*rnp
, int ncheck
)
942 WARN_ON_ONCE(!list_empty(&rnp
->blkd_tasks
));
945 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
947 #ifdef CONFIG_RCU_BOOST
949 static void rcu_wake_cond(struct task_struct
*t
, int status
)
952 * If the thread is yielding, only wake it when this
953 * is invoked from idle
955 if (status
!= RCU_KTHREAD_YIELDING
|| is_idle_task(current
))
960 * Carry out RCU priority boosting on the task indicated by ->exp_tasks
961 * or ->boost_tasks, advancing the pointer to the next task in the
964 * Note that irqs must be enabled: boosting the task can block.
965 * Returns 1 if there are more tasks needing to be boosted.
967 static int rcu_boost(struct rcu_node
*rnp
)
970 struct task_struct
*t
;
971 struct list_head
*tb
;
973 if (READ_ONCE(rnp
->exp_tasks
) == NULL
&&
974 READ_ONCE(rnp
->boost_tasks
) == NULL
)
975 return 0; /* Nothing left to boost. */
977 raw_spin_lock_irqsave_rcu_node(rnp
, flags
);
980 * Recheck under the lock: all tasks in need of boosting
981 * might exit their RCU read-side critical sections on their own.
983 if (rnp
->exp_tasks
== NULL
&& rnp
->boost_tasks
== NULL
) {
984 raw_spin_unlock_irqrestore_rcu_node(rnp
, flags
);
989 * Preferentially boost tasks blocking expedited grace periods.
990 * This cannot starve the normal grace periods because a second
991 * expedited grace period must boost all blocked tasks, including
992 * those blocking the pre-existing normal grace period.
994 if (rnp
->exp_tasks
!= NULL
)
997 tb
= rnp
->boost_tasks
;
1000 * We boost task t by manufacturing an rt_mutex that appears to
1001 * be held by task t. We leave a pointer to that rt_mutex where
1002 * task t can find it, and task t will release the mutex when it
1003 * exits its outermost RCU read-side critical section. Then
1004 * simply acquiring this artificial rt_mutex will boost task
1005 * t's priority. (Thanks to tglx for suggesting this approach!)
1007 * Note that task t must acquire rnp->lock to remove itself from
1008 * the ->blkd_tasks list, which it will do from exit() if from
1009 * nowhere else. We therefore are guaranteed that task t will
1010 * stay around at least until we drop rnp->lock. Note that
1011 * rnp->lock also resolves races between our priority boosting
1012 * and task t's exiting its outermost RCU read-side critical
1015 t
= container_of(tb
, struct task_struct
, rcu_node_entry
);
1016 rt_mutex_init_proxy_locked(&rnp
->boost_mtx
, t
);
1017 raw_spin_unlock_irqrestore_rcu_node(rnp
, flags
);
1018 /* Lock only for side effect: boosts task t's priority. */
1019 rt_mutex_lock(&rnp
->boost_mtx
);
1020 rt_mutex_unlock(&rnp
->boost_mtx
); /* Then keep lockdep happy. */
1022 return READ_ONCE(rnp
->exp_tasks
) != NULL
||
1023 READ_ONCE(rnp
->boost_tasks
) != NULL
;
1027 * Priority-boosting kthread, one per leaf rcu_node.
1029 static int rcu_boost_kthread(void *arg
)
1031 struct rcu_node
*rnp
= (struct rcu_node
*)arg
;
1035 trace_rcu_utilization(TPS("Start boost kthread@init"));
1037 rnp
->boost_kthread_status
= RCU_KTHREAD_WAITING
;
1038 trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
1039 rcu_wait(rnp
->boost_tasks
|| rnp
->exp_tasks
);
1040 trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
1041 rnp
->boost_kthread_status
= RCU_KTHREAD_RUNNING
;
1042 more2boost
= rcu_boost(rnp
);
1048 rnp
->boost_kthread_status
= RCU_KTHREAD_YIELDING
;
1049 trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
1050 schedule_timeout_interruptible(2);
1051 trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
1056 trace_rcu_utilization(TPS("End boost kthread@notreached"));
1061 * Check to see if it is time to start boosting RCU readers that are
1062 * blocking the current grace period, and, if so, tell the per-rcu_node
1063 * kthread to start boosting them. If there is an expedited grace
1064 * period in progress, it is always time to boost.
1066 * The caller must hold rnp->lock, which this function releases.
1067 * The ->boost_kthread_task is immortal, so we don't need to worry
1068 * about it going away.
1070 static void rcu_initiate_boost(struct rcu_node
*rnp
, unsigned long flags
)
1071 __releases(rnp
->lock
)
1073 raw_lockdep_assert_held_rcu_node(rnp
);
1074 if (!rcu_preempt_blocked_readers_cgp(rnp
) && rnp
->exp_tasks
== NULL
) {
1075 raw_spin_unlock_irqrestore_rcu_node(rnp
, flags
);
1078 if (rnp
->exp_tasks
!= NULL
||
1079 (rnp
->gp_tasks
!= NULL
&&
1080 rnp
->boost_tasks
== NULL
&&
1082 ULONG_CMP_GE(jiffies
, rnp
->boost_time
))) {
1083 if (rnp
->exp_tasks
== NULL
)
1084 rnp
->boost_tasks
= rnp
->gp_tasks
;
1085 raw_spin_unlock_irqrestore_rcu_node(rnp
, flags
);
1086 rcu_wake_cond(rnp
->boost_kthread_task
,
1087 rnp
->boost_kthread_status
);
1089 raw_spin_unlock_irqrestore_rcu_node(rnp
, flags
);
1094 * Wake up the per-CPU kthread to invoke RCU callbacks.
1096 static void invoke_rcu_callbacks_kthread(void)
1098 unsigned long flags
;
1100 local_irq_save(flags
);
1101 __this_cpu_write(rcu_data
.rcu_cpu_has_work
, 1);
1102 if (__this_cpu_read(rcu_data
.rcu_cpu_kthread_task
) != NULL
&&
1103 current
!= __this_cpu_read(rcu_data
.rcu_cpu_kthread_task
)) {
1104 rcu_wake_cond(__this_cpu_read(rcu_data
.rcu_cpu_kthread_task
),
1105 __this_cpu_read(rcu_data
.rcu_cpu_kthread_status
));
1107 local_irq_restore(flags
);
1111 * Is the current CPU running the RCU-callbacks kthread?
1112 * Caller must have preemption disabled.
1114 static bool rcu_is_callbacks_kthread(void)
1116 return __this_cpu_read(rcu_data
.rcu_cpu_kthread_task
) == current
;
1119 #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
1122 * Do priority-boost accounting for the start of a new grace period.
1124 static void rcu_preempt_boost_start_gp(struct rcu_node
*rnp
)
1126 rnp
->boost_time
= jiffies
+ RCU_BOOST_DELAY_JIFFIES
;
1130 * Create an RCU-boost kthread for the specified node if one does not
1131 * already exist. We only create this kthread for preemptible RCU.
1132 * Returns zero if all is well, a negated errno otherwise.
1134 static int rcu_spawn_one_boost_kthread(struct rcu_node
*rnp
)
1136 int rnp_index
= rnp
- rcu_get_root();
1137 unsigned long flags
;
1138 struct sched_param sp
;
1139 struct task_struct
*t
;
1141 if (!IS_ENABLED(CONFIG_PREEMPT_RCU
))
1144 if (!rcu_scheduler_fully_active
|| rcu_rnp_online_cpus(rnp
) == 0)
1147 rcu_state
.boost
= 1;
1148 if (rnp
->boost_kthread_task
!= NULL
)
1150 t
= kthread_create(rcu_boost_kthread
, (void *)rnp
,
1151 "rcub/%d", rnp_index
);
1154 raw_spin_lock_irqsave_rcu_node(rnp
, flags
);
1155 rnp
->boost_kthread_task
= t
;
1156 raw_spin_unlock_irqrestore_rcu_node(rnp
, flags
);
1157 sp
.sched_priority
= kthread_prio
;
1158 sched_setscheduler_nocheck(t
, SCHED_FIFO
, &sp
);
1159 wake_up_process(t
); /* get to TASK_INTERRUPTIBLE quickly. */
1163 static void rcu_cpu_kthread_setup(unsigned int cpu
)
1165 struct sched_param sp
;
1167 sp
.sched_priority
= kthread_prio
;
1168 sched_setscheduler_nocheck(current
, SCHED_FIFO
, &sp
);
1171 static void rcu_cpu_kthread_park(unsigned int cpu
)
1173 per_cpu(rcu_data
.rcu_cpu_kthread_status
, cpu
) = RCU_KTHREAD_OFFCPU
;
1176 static int rcu_cpu_kthread_should_run(unsigned int cpu
)
1178 return __this_cpu_read(rcu_data
.rcu_cpu_has_work
);
1182 * Per-CPU kernel thread that invokes RCU callbacks. This replaces
1183 * the RCU softirq used in configurations of RCU that do not support RCU
1184 * priority boosting.
1186 static void rcu_cpu_kthread(unsigned int cpu
)
1188 unsigned int *statusp
= this_cpu_ptr(&rcu_data
.rcu_cpu_kthread_status
);
1189 char work
, *workp
= this_cpu_ptr(&rcu_data
.rcu_cpu_has_work
);
1192 for (spincnt
= 0; spincnt
< 10; spincnt
++) {
1193 trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
1195 *statusp
= RCU_KTHREAD_RUNNING
;
1196 local_irq_disable();
1201 rcu_do_batch(this_cpu_ptr(&rcu_data
));
1204 trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
1205 *statusp
= RCU_KTHREAD_WAITING
;
1209 *statusp
= RCU_KTHREAD_YIELDING
;
1210 trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
1211 schedule_timeout_interruptible(2);
1212 trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
1213 *statusp
= RCU_KTHREAD_WAITING
;
1217 * Set the per-rcu_node kthread's affinity to cover all CPUs that are
1218 * served by the rcu_node in question. The CPU hotplug lock is still
1219 * held, so the value of rnp->qsmaskinit will be stable.
1221 * We don't include outgoingcpu in the affinity set, use -1 if there is
1222 * no outgoing CPU. If there are no CPUs left in the affinity set,
1223 * this function allows the kthread to execute on any CPU.
1225 static void rcu_boost_kthread_setaffinity(struct rcu_node
*rnp
, int outgoingcpu
)
1227 struct task_struct
*t
= rnp
->boost_kthread_task
;
1228 unsigned long mask
= rcu_rnp_online_cpus(rnp
);
1234 if (!zalloc_cpumask_var(&cm
, GFP_KERNEL
))
1236 for_each_leaf_node_possible_cpu(rnp
, cpu
)
1237 if ((mask
& leaf_node_cpu_bit(rnp
, cpu
)) &&
1239 cpumask_set_cpu(cpu
, cm
);
1240 if (cpumask_weight(cm
) == 0)
1242 set_cpus_allowed_ptr(t
, cm
);
1243 free_cpumask_var(cm
);
1246 static struct smp_hotplug_thread rcu_cpu_thread_spec
= {
1247 .store
= &rcu_data
.rcu_cpu_kthread_task
,
1248 .thread_should_run
= rcu_cpu_kthread_should_run
,
1249 .thread_fn
= rcu_cpu_kthread
,
1250 .thread_comm
= "rcuc/%u",
1251 .setup
= rcu_cpu_kthread_setup
,
1252 .park
= rcu_cpu_kthread_park
,
1256 * Spawn boost kthreads -- called as soon as the scheduler is running.
1258 static void __init
rcu_spawn_boost_kthreads(void)
1260 struct rcu_node
*rnp
;
1263 for_each_possible_cpu(cpu
)
1264 per_cpu(rcu_data
.rcu_cpu_has_work
, cpu
) = 0;
1265 if (WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec
), "%s: Could not start rcub kthread, OOM is now expected behavior\n", __func__
))
1267 rcu_for_each_leaf_node(rnp
)
1268 (void)rcu_spawn_one_boost_kthread(rnp
);
1271 static void rcu_prepare_kthreads(int cpu
)
1273 struct rcu_data
*rdp
= per_cpu_ptr(&rcu_data
, cpu
);
1274 struct rcu_node
*rnp
= rdp
->mynode
;
1276 /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
1277 if (rcu_scheduler_fully_active
)
1278 (void)rcu_spawn_one_boost_kthread(rnp
);
1281 #else /* #ifdef CONFIG_RCU_BOOST */
1283 static void rcu_initiate_boost(struct rcu_node
*rnp
, unsigned long flags
)
1284 __releases(rnp
->lock
)
1286 raw_spin_unlock_irqrestore_rcu_node(rnp
, flags
);
1289 static void invoke_rcu_callbacks_kthread(void)
1294 static bool rcu_is_callbacks_kthread(void)
1299 static void rcu_preempt_boost_start_gp(struct rcu_node
*rnp
)
1303 static void rcu_boost_kthread_setaffinity(struct rcu_node
*rnp
, int outgoingcpu
)
1307 static void __init
rcu_spawn_boost_kthreads(void)
1311 static void rcu_prepare_kthreads(int cpu
)
1315 #endif /* #else #ifdef CONFIG_RCU_BOOST */
1317 #if !defined(CONFIG_RCU_FAST_NO_HZ)
1320 * Check to see if any future RCU-related work will need to be done
1321 * by the current CPU, even if none need be done immediately, returning
1322 * 1 if so. This function is part of the RCU implementation; it is -not-
1323 * an exported member of the RCU API.
1325 * Because we not have RCU_FAST_NO_HZ, just check whether or not this
1326 * CPU has RCU callbacks queued.
1328 int rcu_needs_cpu(u64 basemono
, u64
*nextevt
)
1330 *nextevt
= KTIME_MAX
;
1331 return !rcu_segcblist_empty(&this_cpu_ptr(&rcu_data
)->cblist
);
1335 * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
1338 static void rcu_cleanup_after_idle(void)
1343 * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
1346 static void rcu_prepare_for_idle(void)
1350 #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1353 * This code is invoked when a CPU goes idle, at which point we want
1354 * to have the CPU do everything required for RCU so that it can enter
1355 * the energy-efficient dyntick-idle mode. This is handled by a
1356 * state machine implemented by rcu_prepare_for_idle() below.
1358 * The following three proprocessor symbols control this state machine:
1360 * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
1361 * to sleep in dyntick-idle mode with RCU callbacks pending. This
1362 * is sized to be roughly one RCU grace period. Those energy-efficiency
1363 * benchmarkers who might otherwise be tempted to set this to a large
1364 * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
1365 * system. And if you are -that- concerned about energy efficiency,
1366 * just power the system down and be done with it!
1367 * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
1368 * permitted to sleep in dyntick-idle mode with only lazy RCU
1369 * callbacks pending. Setting this too high can OOM your system.
1371 * The values below work well in practice. If future workloads require
1372 * adjustment, they can be converted into kernel config parameters, though
1373 * making the state machine smarter might be a better option.
1375 #define RCU_IDLE_GP_DELAY 4 /* Roughly one grace period. */
1376 #define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
1378 static int rcu_idle_gp_delay
= RCU_IDLE_GP_DELAY
;
1379 module_param(rcu_idle_gp_delay
, int, 0644);
1380 static int rcu_idle_lazy_gp_delay
= RCU_IDLE_LAZY_GP_DELAY
;
1381 module_param(rcu_idle_lazy_gp_delay
, int, 0644);
1384 * Try to advance callbacks on the current CPU, but only if it has been
1385 * awhile since the last time we did so. Afterwards, if there are any
1386 * callbacks ready for immediate invocation, return true.
1388 static bool __maybe_unused
rcu_try_advance_all_cbs(void)
1390 bool cbs_ready
= false;
1391 struct rcu_data
*rdp
= this_cpu_ptr(&rcu_data
);
1392 struct rcu_node
*rnp
;
1394 /* Exit early if we advanced recently. */
1395 if (jiffies
== rdp
->last_advance_all
)
1397 rdp
->last_advance_all
= jiffies
;
1402 * Don't bother checking unless a grace period has
1403 * completed since we last checked and there are
1404 * callbacks not yet ready to invoke.
1406 if ((rcu_seq_completed_gp(rdp
->gp_seq
,
1407 rcu_seq_current(&rnp
->gp_seq
)) ||
1408 unlikely(READ_ONCE(rdp
->gpwrap
))) &&
1409 rcu_segcblist_pend_cbs(&rdp
->cblist
))
1410 note_gp_changes(rdp
);
1412 if (rcu_segcblist_ready_cbs(&rdp
->cblist
))
1418 * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
1419 * to invoke. If the CPU has callbacks, try to advance them. Tell the
1420 * caller to set the timeout based on whether or not there are non-lazy
1423 * The caller must have disabled interrupts.
1425 int rcu_needs_cpu(u64 basemono
, u64
*nextevt
)
1427 struct rcu_data
*rdp
= this_cpu_ptr(&rcu_data
);
1430 lockdep_assert_irqs_disabled();
1432 /* If no callbacks, RCU doesn't need the CPU. */
1433 if (rcu_segcblist_empty(&rdp
->cblist
)) {
1434 *nextevt
= KTIME_MAX
;
1438 /* Attempt to advance callbacks. */
1439 if (rcu_try_advance_all_cbs()) {
1440 /* Some ready to invoke, so initiate later invocation. */
1444 rdp
->last_accelerate
= jiffies
;
1446 /* Request timer delay depending on laziness, and round. */
1447 rdp
->all_lazy
= !rcu_segcblist_n_nonlazy_cbs(&rdp
->cblist
);
1448 if (rdp
->all_lazy
) {
1449 dj
= round_jiffies(rcu_idle_lazy_gp_delay
+ jiffies
) - jiffies
;
1451 dj
= round_up(rcu_idle_gp_delay
+ jiffies
,
1452 rcu_idle_gp_delay
) - jiffies
;
1454 *nextevt
= basemono
+ dj
* TICK_NSEC
;
1459 * Prepare a CPU for idle from an RCU perspective. The first major task
1460 * is to sense whether nohz mode has been enabled or disabled via sysfs.
1461 * The second major task is to check to see if a non-lazy callback has
1462 * arrived at a CPU that previously had only lazy callbacks. The third
1463 * major task is to accelerate (that is, assign grace-period numbers to)
1464 * any recently arrived callbacks.
1466 * The caller must have disabled interrupts.
1468 static void rcu_prepare_for_idle(void)
1471 struct rcu_data
*rdp
= this_cpu_ptr(&rcu_data
);
1472 struct rcu_node
*rnp
;
1475 lockdep_assert_irqs_disabled();
1476 if (rcu_is_nocb_cpu(smp_processor_id()))
1479 /* Handle nohz enablement switches conservatively. */
1480 tne
= READ_ONCE(tick_nohz_active
);
1481 if (tne
!= rdp
->tick_nohz_enabled_snap
) {
1482 if (!rcu_segcblist_empty(&rdp
->cblist
))
1483 invoke_rcu_core(); /* force nohz to see update. */
1484 rdp
->tick_nohz_enabled_snap
= tne
;
1491 * If a non-lazy callback arrived at a CPU having only lazy
1492 * callbacks, invoke RCU core for the side-effect of recalculating
1493 * idle duration on re-entry to idle.
1495 if (rdp
->all_lazy
&& rcu_segcblist_n_nonlazy_cbs(&rdp
->cblist
)) {
1496 rdp
->all_lazy
= false;
1502 * If we have not yet accelerated this jiffy, accelerate all
1503 * callbacks on this CPU.
1505 if (rdp
->last_accelerate
== jiffies
)
1507 rdp
->last_accelerate
= jiffies
;
1508 if (rcu_segcblist_pend_cbs(&rdp
->cblist
)) {
1510 raw_spin_lock_rcu_node(rnp
); /* irqs already disabled. */
1511 needwake
= rcu_accelerate_cbs(rnp
, rdp
);
1512 raw_spin_unlock_rcu_node(rnp
); /* irqs remain disabled. */
1514 rcu_gp_kthread_wake();
1519 * Clean up for exit from idle. Attempt to advance callbacks based on
1520 * any grace periods that elapsed while the CPU was idle, and if any
1521 * callbacks are now ready to invoke, initiate invocation.
1523 static void rcu_cleanup_after_idle(void)
1525 lockdep_assert_irqs_disabled();
1526 if (rcu_is_nocb_cpu(smp_processor_id()))
1528 if (rcu_try_advance_all_cbs())
1532 #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1534 #ifdef CONFIG_RCU_NOCB_CPU
1537 * Offload callback processing from the boot-time-specified set of CPUs
1538 * specified by rcu_nocb_mask. For the CPUs in the set, there are kthreads
1539 * created that pull the callbacks from the corresponding CPU, wait for
1540 * a grace period to elapse, and invoke the callbacks. These kthreads
1541 * are organized into leaders, which manage incoming callbacks, wait for
1542 * grace periods, and awaken followers, and the followers, which only
1543 * invoke callbacks. Each leader is its own follower. The no-CBs CPUs
1544 * do a wake_up() on their kthread when they insert a callback into any
1545 * empty list, unless the rcu_nocb_poll boot parameter has been specified,
1546 * in which case each kthread actively polls its CPU. (Which isn't so great
1547 * for energy efficiency, but which does reduce RCU's overhead on that CPU.)
1549 * This is intended to be used in conjunction with Frederic Weisbecker's
1550 * adaptive-idle work, which would seriously reduce OS jitter on CPUs
1551 * running CPU-bound user-mode computations.
1553 * Offloading of callbacks can also be used as an energy-efficiency
1554 * measure because CPUs with no RCU callbacks queued are more aggressive
1555 * about entering dyntick-idle mode.
1560 * Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters.
1561 * The string after the "rcu_nocbs=" is either "all" for all CPUs, or a
1562 * comma-separated list of CPUs and/or CPU ranges. If an invalid list is
1563 * given, a warning is emitted and all CPUs are offloaded.
1565 static int __init
rcu_nocb_setup(char *str
)
1567 alloc_bootmem_cpumask_var(&rcu_nocb_mask
);
1568 if (!strcasecmp(str
, "all"))
1569 cpumask_setall(rcu_nocb_mask
);
1571 if (cpulist_parse(str
, rcu_nocb_mask
)) {
1572 pr_warn("rcu_nocbs= bad CPU range, all CPUs set\n");
1573 cpumask_setall(rcu_nocb_mask
);
1577 __setup("rcu_nocbs=", rcu_nocb_setup
);
1579 static int __init
parse_rcu_nocb_poll(char *arg
)
1581 rcu_nocb_poll
= true;
1584 early_param("rcu_nocb_poll", parse_rcu_nocb_poll
);
1587 * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
1590 static void rcu_nocb_gp_cleanup(struct swait_queue_head
*sq
)
1595 static struct swait_queue_head
*rcu_nocb_gp_get(struct rcu_node
*rnp
)
1597 return &rnp
->nocb_gp_wq
[rcu_seq_ctr(rnp
->gp_seq
) & 0x1];
1600 static void rcu_init_one_nocb(struct rcu_node
*rnp
)
1602 init_swait_queue_head(&rnp
->nocb_gp_wq
[0]);
1603 init_swait_queue_head(&rnp
->nocb_gp_wq
[1]);
1606 /* Is the specified CPU a no-CBs CPU? */
1607 bool rcu_is_nocb_cpu(int cpu
)
1609 if (cpumask_available(rcu_nocb_mask
))
1610 return cpumask_test_cpu(cpu
, rcu_nocb_mask
);
1615 * Kick the leader kthread for this NOCB group. Caller holds ->nocb_lock
1616 * and this function releases it.
1618 static void __wake_nocb_leader(struct rcu_data
*rdp
, bool force
,
1619 unsigned long flags
)
1620 __releases(rdp
->nocb_lock
)
1622 struct rcu_data
*rdp_leader
= rdp
->nocb_leader
;
1624 lockdep_assert_held(&rdp
->nocb_lock
);
1625 if (!READ_ONCE(rdp_leader
->nocb_kthread
)) {
1626 raw_spin_unlock_irqrestore(&rdp
->nocb_lock
, flags
);
1629 if (rdp_leader
->nocb_leader_sleep
|| force
) {
1630 /* Prior smp_mb__after_atomic() orders against prior enqueue. */
1631 WRITE_ONCE(rdp_leader
->nocb_leader_sleep
, false);
1632 del_timer(&rdp
->nocb_timer
);
1633 raw_spin_unlock_irqrestore(&rdp
->nocb_lock
, flags
);
1634 smp_mb(); /* ->nocb_leader_sleep before swake_up_one(). */
1635 swake_up_one(&rdp_leader
->nocb_wq
);
1637 raw_spin_unlock_irqrestore(&rdp
->nocb_lock
, flags
);
1642 * Kick the leader kthread for this NOCB group, but caller has not
1645 static void wake_nocb_leader(struct rcu_data
*rdp
, bool force
)
1647 unsigned long flags
;
1649 raw_spin_lock_irqsave(&rdp
->nocb_lock
, flags
);
1650 __wake_nocb_leader(rdp
, force
, flags
);
1654 * Arrange to wake the leader kthread for this NOCB group at some
1655 * future time when it is safe to do so.
1657 static void wake_nocb_leader_defer(struct rcu_data
*rdp
, int waketype
,
1660 unsigned long flags
;
1662 raw_spin_lock_irqsave(&rdp
->nocb_lock
, flags
);
1663 if (rdp
->nocb_defer_wakeup
== RCU_NOCB_WAKE_NOT
)
1664 mod_timer(&rdp
->nocb_timer
, jiffies
+ 1);
1665 WRITE_ONCE(rdp
->nocb_defer_wakeup
, waketype
);
1666 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
, reason
);
1667 raw_spin_unlock_irqrestore(&rdp
->nocb_lock
, flags
);
1670 /* Does rcu_barrier need to queue an RCU callback on the specified CPU? */
1671 static bool rcu_nocb_cpu_needs_barrier(int cpu
)
1673 struct rcu_data
*rdp
= per_cpu_ptr(&rcu_data
, cpu
);
1675 #ifdef CONFIG_PROVE_RCU
1676 struct rcu_head
*rhp
;
1677 #endif /* #ifdef CONFIG_PROVE_RCU */
1680 * Check count of all no-CBs callbacks awaiting invocation.
1681 * There needs to be a barrier before this function is called,
1682 * but associated with a prior determination that no more
1683 * callbacks would be posted. In the worst case, the first
1684 * barrier in rcu_barrier() suffices (but the caller cannot
1685 * necessarily rely on this, not a substitute for the caller
1686 * getting the concurrency design right!). There must also be a
1687 * barrier between the following load and posting of a callback
1688 * (if a callback is in fact needed). This is associated with an
1689 * atomic_inc() in the caller.
1691 ret
= rcu_get_n_cbs_nocb_cpu(rdp
);
1693 #ifdef CONFIG_PROVE_RCU
1694 rhp
= READ_ONCE(rdp
->nocb_head
);
1696 rhp
= READ_ONCE(rdp
->nocb_gp_head
);
1698 rhp
= READ_ONCE(rdp
->nocb_follower_head
);
1700 /* Having no rcuo kthread but CBs after scheduler starts is bad! */
1701 if (!READ_ONCE(rdp
->nocb_kthread
) && rhp
&&
1702 rcu_scheduler_fully_active
) {
1703 /* RCU callback enqueued before CPU first came online??? */
1704 pr_err("RCU: Never-onlined no-CBs CPU %d has CB %p\n",
1708 #endif /* #ifdef CONFIG_PROVE_RCU */
1714 * Enqueue the specified string of rcu_head structures onto the specified
1715 * CPU's no-CBs lists. The CPU is specified by rdp, the head of the
1716 * string by rhp, and the tail of the string by rhtp. The non-lazy/lazy
1717 * counts are supplied by rhcount and rhcount_lazy.
1719 * If warranted, also wake up the kthread servicing this CPUs queues.
1721 static void __call_rcu_nocb_enqueue(struct rcu_data
*rdp
,
1722 struct rcu_head
*rhp
,
1723 struct rcu_head
**rhtp
,
1724 int rhcount
, int rhcount_lazy
,
1725 unsigned long flags
)
1728 struct rcu_head
**old_rhpp
;
1729 struct task_struct
*t
;
1731 /* Enqueue the callback on the nocb list and update counts. */
1732 atomic_long_add(rhcount
, &rdp
->nocb_q_count
);
1733 /* rcu_barrier() relies on ->nocb_q_count add before xchg. */
1734 old_rhpp
= xchg(&rdp
->nocb_tail
, rhtp
);
1735 WRITE_ONCE(*old_rhpp
, rhp
);
1736 atomic_long_add(rhcount_lazy
, &rdp
->nocb_q_count_lazy
);
1737 smp_mb__after_atomic(); /* Store *old_rhpp before _wake test. */
1739 /* If we are not being polled and there is a kthread, awaken it ... */
1740 t
= READ_ONCE(rdp
->nocb_kthread
);
1741 if (rcu_nocb_poll
|| !t
) {
1742 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
,
1743 TPS("WakeNotPoll"));
1746 len
= rcu_get_n_cbs_nocb_cpu(rdp
);
1747 if (old_rhpp
== &rdp
->nocb_head
) {
1748 if (!irqs_disabled_flags(flags
)) {
1749 /* ... if queue was empty ... */
1750 wake_nocb_leader(rdp
, false);
1751 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
,
1754 wake_nocb_leader_defer(rdp
, RCU_NOCB_WAKE
,
1755 TPS("WakeEmptyIsDeferred"));
1757 rdp
->qlen_last_fqs_check
= 0;
1758 } else if (len
> rdp
->qlen_last_fqs_check
+ qhimark
) {
1759 /* ... or if many callbacks queued. */
1760 if (!irqs_disabled_flags(flags
)) {
1761 wake_nocb_leader(rdp
, true);
1762 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
,
1765 wake_nocb_leader_defer(rdp
, RCU_NOCB_WAKE_FORCE
,
1766 TPS("WakeOvfIsDeferred"));
1768 rdp
->qlen_last_fqs_check
= LONG_MAX
/ 2;
1770 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
, TPS("WakeNot"));
1776 * This is a helper for __call_rcu(), which invokes this when the normal
1777 * callback queue is inoperable. If this is not a no-CBs CPU, this
1778 * function returns failure back to __call_rcu(), which can complain
1781 * Otherwise, this function queues the callback where the corresponding
1782 * "rcuo" kthread can find it.
1784 static bool __call_rcu_nocb(struct rcu_data
*rdp
, struct rcu_head
*rhp
,
1785 bool lazy
, unsigned long flags
)
1788 if (!rcu_is_nocb_cpu(rdp
->cpu
))
1790 __call_rcu_nocb_enqueue(rdp
, rhp
, &rhp
->next
, 1, lazy
, flags
);
1791 if (__is_kfree_rcu_offset((unsigned long)rhp
->func
))
1792 trace_rcu_kfree_callback(rcu_state
.name
, rhp
,
1793 (unsigned long)rhp
->func
,
1794 -atomic_long_read(&rdp
->nocb_q_count_lazy
),
1795 -rcu_get_n_cbs_nocb_cpu(rdp
));
1797 trace_rcu_callback(rcu_state
.name
, rhp
,
1798 -atomic_long_read(&rdp
->nocb_q_count_lazy
),
1799 -rcu_get_n_cbs_nocb_cpu(rdp
));
1802 * If called from an extended quiescent state with interrupts
1803 * disabled, invoke the RCU core in order to allow the idle-entry
1804 * deferred-wakeup check to function.
1806 if (irqs_disabled_flags(flags
) &&
1807 !rcu_is_watching() &&
1808 cpu_online(smp_processor_id()))
1815 * Adopt orphaned callbacks on a no-CBs CPU, or return 0 if this is
1818 static bool __maybe_unused
rcu_nocb_adopt_orphan_cbs(struct rcu_data
*my_rdp
,
1819 struct rcu_data
*rdp
,
1820 unsigned long flags
)
1822 lockdep_assert_irqs_disabled();
1823 if (!rcu_is_nocb_cpu(smp_processor_id()))
1824 return false; /* Not NOCBs CPU, caller must migrate CBs. */
1825 __call_rcu_nocb_enqueue(my_rdp
, rcu_segcblist_head(&rdp
->cblist
),
1826 rcu_segcblist_tail(&rdp
->cblist
),
1827 rcu_segcblist_n_cbs(&rdp
->cblist
),
1828 rcu_segcblist_n_lazy_cbs(&rdp
->cblist
), flags
);
1829 rcu_segcblist_init(&rdp
->cblist
);
1830 rcu_segcblist_disable(&rdp
->cblist
);
1835 * If necessary, kick off a new grace period, and either way wait
1836 * for a subsequent grace period to complete.
1838 static void rcu_nocb_wait_gp(struct rcu_data
*rdp
)
1842 unsigned long flags
;
1844 struct rcu_node
*rnp
= rdp
->mynode
;
1846 local_irq_save(flags
);
1847 c
= rcu_seq_snap(&rcu_state
.gp_seq
);
1848 if (!rdp
->gpwrap
&& ULONG_CMP_GE(rdp
->gp_seq_needed
, c
)) {
1849 local_irq_restore(flags
);
1851 raw_spin_lock_rcu_node(rnp
); /* irqs already disabled. */
1852 needwake
= rcu_start_this_gp(rnp
, rdp
, c
);
1853 raw_spin_unlock_irqrestore_rcu_node(rnp
, flags
);
1855 rcu_gp_kthread_wake();
1859 * Wait for the grace period. Do so interruptibly to avoid messing
1860 * up the load average.
1862 trace_rcu_this_gp(rnp
, rdp
, c
, TPS("StartWait"));
1864 swait_event_interruptible_exclusive(
1865 rnp
->nocb_gp_wq
[rcu_seq_ctr(c
) & 0x1],
1866 (d
= rcu_seq_done(&rnp
->gp_seq
, c
)));
1869 WARN_ON(signal_pending(current
));
1870 trace_rcu_this_gp(rnp
, rdp
, c
, TPS("ResumeWait"));
1872 trace_rcu_this_gp(rnp
, rdp
, c
, TPS("EndWait"));
1873 smp_mb(); /* Ensure that CB invocation happens after GP end. */
1877 * Leaders come here to wait for additional callbacks to show up.
1878 * This function does not return until callbacks appear.
1880 static void nocb_leader_wait(struct rcu_data
*my_rdp
)
1882 bool firsttime
= true;
1883 unsigned long flags
;
1885 struct rcu_data
*rdp
;
1886 struct rcu_head
**tail
;
1890 /* Wait for callbacks to appear. */
1891 if (!rcu_nocb_poll
) {
1892 trace_rcu_nocb_wake(rcu_state
.name
, my_rdp
->cpu
, TPS("Sleep"));
1893 swait_event_interruptible_exclusive(my_rdp
->nocb_wq
,
1894 !READ_ONCE(my_rdp
->nocb_leader_sleep
));
1895 raw_spin_lock_irqsave(&my_rdp
->nocb_lock
, flags
);
1896 my_rdp
->nocb_leader_sleep
= true;
1897 WRITE_ONCE(my_rdp
->nocb_defer_wakeup
, RCU_NOCB_WAKE_NOT
);
1898 del_timer(&my_rdp
->nocb_timer
);
1899 raw_spin_unlock_irqrestore(&my_rdp
->nocb_lock
, flags
);
1900 } else if (firsttime
) {
1901 firsttime
= false; /* Don't drown trace log with "Poll"! */
1902 trace_rcu_nocb_wake(rcu_state
.name
, my_rdp
->cpu
, TPS("Poll"));
1906 * Each pass through the following loop checks a follower for CBs.
1907 * We are our own first follower. Any CBs found are moved to
1908 * nocb_gp_head, where they await a grace period.
1911 smp_mb(); /* wakeup and _sleep before ->nocb_head reads. */
1912 for (rdp
= my_rdp
; rdp
; rdp
= rdp
->nocb_next_follower
) {
1913 rdp
->nocb_gp_head
= READ_ONCE(rdp
->nocb_head
);
1914 if (!rdp
->nocb_gp_head
)
1915 continue; /* No CBs here, try next follower. */
1917 /* Move callbacks to wait-for-GP list, which is empty. */
1918 WRITE_ONCE(rdp
->nocb_head
, NULL
);
1919 rdp
->nocb_gp_tail
= xchg(&rdp
->nocb_tail
, &rdp
->nocb_head
);
1923 /* No callbacks? Sleep a bit if polling, and go retry. */
1924 if (unlikely(!gotcbs
)) {
1925 WARN_ON(signal_pending(current
));
1926 if (rcu_nocb_poll
) {
1927 schedule_timeout_interruptible(1);
1929 trace_rcu_nocb_wake(rcu_state
.name
, my_rdp
->cpu
,
1935 /* Wait for one grace period. */
1936 rcu_nocb_wait_gp(my_rdp
);
1938 /* Each pass through the following loop wakes a follower, if needed. */
1939 for (rdp
= my_rdp
; rdp
; rdp
= rdp
->nocb_next_follower
) {
1940 if (!rcu_nocb_poll
&&
1941 READ_ONCE(rdp
->nocb_head
) &&
1942 READ_ONCE(my_rdp
->nocb_leader_sleep
)) {
1943 raw_spin_lock_irqsave(&my_rdp
->nocb_lock
, flags
);
1944 my_rdp
->nocb_leader_sleep
= false;/* No need to sleep.*/
1945 raw_spin_unlock_irqrestore(&my_rdp
->nocb_lock
, flags
);
1947 if (!rdp
->nocb_gp_head
)
1948 continue; /* No CBs, so no need to wake follower. */
1950 /* Append callbacks to follower's "done" list. */
1951 raw_spin_lock_irqsave(&rdp
->nocb_lock
, flags
);
1952 tail
= rdp
->nocb_follower_tail
;
1953 rdp
->nocb_follower_tail
= rdp
->nocb_gp_tail
;
1954 *tail
= rdp
->nocb_gp_head
;
1955 raw_spin_unlock_irqrestore(&rdp
->nocb_lock
, flags
);
1956 if (rdp
!= my_rdp
&& tail
== &rdp
->nocb_follower_head
) {
1957 /* List was empty, so wake up the follower. */
1958 swake_up_one(&rdp
->nocb_wq
);
1962 /* If we (the leader) don't have CBs, go wait some more. */
1963 if (!my_rdp
->nocb_follower_head
)
1968 * Followers come here to wait for additional callbacks to show up.
1969 * This function does not return until callbacks appear.
1971 static void nocb_follower_wait(struct rcu_data
*rdp
)
1974 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
, TPS("FollowerSleep"));
1975 swait_event_interruptible_exclusive(rdp
->nocb_wq
,
1976 READ_ONCE(rdp
->nocb_follower_head
));
1977 if (smp_load_acquire(&rdp
->nocb_follower_head
)) {
1978 /* ^^^ Ensure CB invocation follows _head test. */
1981 WARN_ON(signal_pending(current
));
1982 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
, TPS("WokeEmpty"));
1987 * Per-rcu_data kthread, but only for no-CBs CPUs. Each kthread invokes
1988 * callbacks queued by the corresponding no-CBs CPU, however, there is
1989 * an optional leader-follower relationship so that the grace-period
1990 * kthreads don't have to do quite so many wakeups.
1992 static int rcu_nocb_kthread(void *arg
)
1995 unsigned long flags
;
1996 struct rcu_head
*list
;
1997 struct rcu_head
*next
;
1998 struct rcu_head
**tail
;
1999 struct rcu_data
*rdp
= arg
;
2001 /* Each pass through this loop invokes one batch of callbacks */
2003 /* Wait for callbacks. */
2004 if (rdp
->nocb_leader
== rdp
)
2005 nocb_leader_wait(rdp
);
2007 nocb_follower_wait(rdp
);
2009 /* Pull the ready-to-invoke callbacks onto local list. */
2010 raw_spin_lock_irqsave(&rdp
->nocb_lock
, flags
);
2011 list
= rdp
->nocb_follower_head
;
2012 rdp
->nocb_follower_head
= NULL
;
2013 tail
= rdp
->nocb_follower_tail
;
2014 rdp
->nocb_follower_tail
= &rdp
->nocb_follower_head
;
2015 raw_spin_unlock_irqrestore(&rdp
->nocb_lock
, flags
);
2016 if (WARN_ON_ONCE(!list
))
2018 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
, TPS("WokeNonEmpty"));
2020 /* Each pass through the following loop invokes a callback. */
2021 trace_rcu_batch_start(rcu_state
.name
,
2022 atomic_long_read(&rdp
->nocb_q_count_lazy
),
2023 rcu_get_n_cbs_nocb_cpu(rdp
), -1);
2027 /* Wait for enqueuing to complete, if needed. */
2028 while (next
== NULL
&& &list
->next
!= tail
) {
2029 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
,
2031 schedule_timeout_interruptible(1);
2032 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
,
2036 debug_rcu_head_unqueue(list
);
2038 if (__rcu_reclaim(rcu_state
.name
, list
))
2042 cond_resched_tasks_rcu_qs();
2045 trace_rcu_batch_end(rcu_state
.name
, c
, !!list
, 0, 0, 1);
2046 smp_mb__before_atomic(); /* _add after CB invocation. */
2047 atomic_long_add(-c
, &rdp
->nocb_q_count
);
2048 atomic_long_add(-cl
, &rdp
->nocb_q_count_lazy
);
2053 /* Is a deferred wakeup of rcu_nocb_kthread() required? */
2054 static int rcu_nocb_need_deferred_wakeup(struct rcu_data
*rdp
)
2056 return READ_ONCE(rdp
->nocb_defer_wakeup
);
2059 /* Do a deferred wakeup of rcu_nocb_kthread(). */
2060 static void do_nocb_deferred_wakeup_common(struct rcu_data
*rdp
)
2062 unsigned long flags
;
2065 raw_spin_lock_irqsave(&rdp
->nocb_lock
, flags
);
2066 if (!rcu_nocb_need_deferred_wakeup(rdp
)) {
2067 raw_spin_unlock_irqrestore(&rdp
->nocb_lock
, flags
);
2070 ndw
= READ_ONCE(rdp
->nocb_defer_wakeup
);
2071 WRITE_ONCE(rdp
->nocb_defer_wakeup
, RCU_NOCB_WAKE_NOT
);
2072 __wake_nocb_leader(rdp
, ndw
== RCU_NOCB_WAKE_FORCE
, flags
);
2073 trace_rcu_nocb_wake(rcu_state
.name
, rdp
->cpu
, TPS("DeferredWake"));
2076 /* Do a deferred wakeup of rcu_nocb_kthread() from a timer handler. */
2077 static void do_nocb_deferred_wakeup_timer(struct timer_list
*t
)
2079 struct rcu_data
*rdp
= from_timer(rdp
, t
, nocb_timer
);
2081 do_nocb_deferred_wakeup_common(rdp
);
2085 * Do a deferred wakeup of rcu_nocb_kthread() from fastpath.
2086 * This means we do an inexact common-case check. Note that if
2087 * we miss, ->nocb_timer will eventually clean things up.
2089 static void do_nocb_deferred_wakeup(struct rcu_data
*rdp
)
2091 if (rcu_nocb_need_deferred_wakeup(rdp
))
2092 do_nocb_deferred_wakeup_common(rdp
);
2095 void __init
rcu_init_nohz(void)
2098 bool need_rcu_nocb_mask
= false;
2100 #if defined(CONFIG_NO_HZ_FULL)
2101 if (tick_nohz_full_running
&& cpumask_weight(tick_nohz_full_mask
))
2102 need_rcu_nocb_mask
= true;
2103 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
2105 if (!cpumask_available(rcu_nocb_mask
) && need_rcu_nocb_mask
) {
2106 if (!zalloc_cpumask_var(&rcu_nocb_mask
, GFP_KERNEL
)) {
2107 pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
2111 if (!cpumask_available(rcu_nocb_mask
))
2114 #if defined(CONFIG_NO_HZ_FULL)
2115 if (tick_nohz_full_running
)
2116 cpumask_or(rcu_nocb_mask
, rcu_nocb_mask
, tick_nohz_full_mask
);
2117 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
2119 if (!cpumask_subset(rcu_nocb_mask
, cpu_possible_mask
)) {
2120 pr_info("\tNote: kernel parameter 'rcu_nocbs=', 'nohz_full', or 'isolcpus=' contains nonexistent CPUs.\n");
2121 cpumask_and(rcu_nocb_mask
, cpu_possible_mask
,
2124 if (cpumask_empty(rcu_nocb_mask
))
2125 pr_info("\tOffload RCU callbacks from CPUs: (none).\n");
2127 pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
2128 cpumask_pr_args(rcu_nocb_mask
));
2130 pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
2132 for_each_cpu(cpu
, rcu_nocb_mask
)
2133 init_nocb_callback_list(per_cpu_ptr(&rcu_data
, cpu
));
2134 rcu_organize_nocb_kthreads();
2137 /* Initialize per-rcu_data variables for no-CBs CPUs. */
2138 static void __init
rcu_boot_init_nocb_percpu_data(struct rcu_data
*rdp
)
2140 rdp
->nocb_tail
= &rdp
->nocb_head
;
2141 init_swait_queue_head(&rdp
->nocb_wq
);
2142 rdp
->nocb_follower_tail
= &rdp
->nocb_follower_head
;
2143 raw_spin_lock_init(&rdp
->nocb_lock
);
2144 timer_setup(&rdp
->nocb_timer
, do_nocb_deferred_wakeup_timer
, 0);
2148 * If the specified CPU is a no-CBs CPU that does not already have its
2149 * rcuo kthread, spawn it. If the CPUs are brought online out of order,
2150 * this can require re-organizing the leader-follower relationships.
2152 static void rcu_spawn_one_nocb_kthread(int cpu
)
2154 struct rcu_data
*rdp
;
2155 struct rcu_data
*rdp_last
;
2156 struct rcu_data
*rdp_old_leader
;
2157 struct rcu_data
*rdp_spawn
= per_cpu_ptr(&rcu_data
, cpu
);
2158 struct task_struct
*t
;
2161 * If this isn't a no-CBs CPU or if it already has an rcuo kthread,
2162 * then nothing to do.
2164 if (!rcu_is_nocb_cpu(cpu
) || rdp_spawn
->nocb_kthread
)
2167 /* If we didn't spawn the leader first, reorganize! */
2168 rdp_old_leader
= rdp_spawn
->nocb_leader
;
2169 if (rdp_old_leader
!= rdp_spawn
&& !rdp_old_leader
->nocb_kthread
) {
2171 rdp
= rdp_old_leader
;
2173 rdp
->nocb_leader
= rdp_spawn
;
2174 if (rdp_last
&& rdp
!= rdp_spawn
)
2175 rdp_last
->nocb_next_follower
= rdp
;
2176 if (rdp
== rdp_spawn
) {
2177 rdp
= rdp
->nocb_next_follower
;
2180 rdp
= rdp
->nocb_next_follower
;
2181 rdp_last
->nocb_next_follower
= NULL
;
2184 rdp_spawn
->nocb_next_follower
= rdp_old_leader
;
2187 /* Spawn the kthread for this CPU. */
2188 t
= kthread_run(rcu_nocb_kthread
, rdp_spawn
,
2189 "rcuo%c/%d", rcu_state
.abbr
, cpu
);
2190 if (WARN_ONCE(IS_ERR(t
), "%s: Could not start rcuo kthread, OOM is now expected behavior\n", __func__
))
2192 WRITE_ONCE(rdp_spawn
->nocb_kthread
, t
);
2196 * If the specified CPU is a no-CBs CPU that does not already have its
2197 * rcuo kthread, spawn it.
2199 static void rcu_spawn_cpu_nocb_kthread(int cpu
)
2201 if (rcu_scheduler_fully_active
)
2202 rcu_spawn_one_nocb_kthread(cpu
);
2206 * Once the scheduler is running, spawn rcuo kthreads for all online
2207 * no-CBs CPUs. This assumes that the early_initcall()s happen before
2208 * non-boot CPUs come online -- if this changes, we will need to add
2209 * some mutual exclusion.
2211 static void __init
rcu_spawn_nocb_kthreads(void)
2215 for_each_online_cpu(cpu
)
2216 rcu_spawn_cpu_nocb_kthread(cpu
);
2219 /* How many follower CPU IDs per leader? Default of -1 for sqrt(nr_cpu_ids). */
2220 static int rcu_nocb_leader_stride
= -1;
2221 module_param(rcu_nocb_leader_stride
, int, 0444);
2224 * Initialize leader-follower relationships for all no-CBs CPU.
2226 static void __init
rcu_organize_nocb_kthreads(void)
2229 int ls
= rcu_nocb_leader_stride
;
2230 int nl
= 0; /* Next leader. */
2231 struct rcu_data
*rdp
;
2232 struct rcu_data
*rdp_leader
= NULL
; /* Suppress misguided gcc warn. */
2233 struct rcu_data
*rdp_prev
= NULL
;
2235 if (!cpumask_available(rcu_nocb_mask
))
2238 ls
= int_sqrt(nr_cpu_ids
);
2239 rcu_nocb_leader_stride
= ls
;
2243 * Each pass through this loop sets up one rcu_data structure.
2244 * Should the corresponding CPU come online in the future, then
2245 * we will spawn the needed set of rcu_nocb_kthread() kthreads.
2247 for_each_cpu(cpu
, rcu_nocb_mask
) {
2248 rdp
= per_cpu_ptr(&rcu_data
, cpu
);
2249 if (rdp
->cpu
>= nl
) {
2250 /* New leader, set up for followers & next leader. */
2251 nl
= DIV_ROUND_UP(rdp
->cpu
+ 1, ls
) * ls
;
2252 rdp
->nocb_leader
= rdp
;
2255 /* Another follower, link to previous leader. */
2256 rdp
->nocb_leader
= rdp_leader
;
2257 rdp_prev
->nocb_next_follower
= rdp
;
2263 /* Prevent __call_rcu() from enqueuing callbacks on no-CBs CPUs */
2264 static bool init_nocb_callback_list(struct rcu_data
*rdp
)
2266 if (!rcu_is_nocb_cpu(rdp
->cpu
))
2269 /* If there are early-boot callbacks, move them to nocb lists. */
2270 if (!rcu_segcblist_empty(&rdp
->cblist
)) {
2271 rdp
->nocb_head
= rcu_segcblist_head(&rdp
->cblist
);
2272 rdp
->nocb_tail
= rcu_segcblist_tail(&rdp
->cblist
);
2273 atomic_long_set(&rdp
->nocb_q_count
,
2274 rcu_segcblist_n_cbs(&rdp
->cblist
));
2275 atomic_long_set(&rdp
->nocb_q_count_lazy
,
2276 rcu_segcblist_n_lazy_cbs(&rdp
->cblist
));
2277 rcu_segcblist_init(&rdp
->cblist
);
2279 rcu_segcblist_disable(&rdp
->cblist
);
2284 * Bind the current task to the offloaded CPUs. If there are no offloaded
2285 * CPUs, leave the task unbound. Splat if the bind attempt fails.
2287 void rcu_bind_current_to_nocb(void)
2289 if (cpumask_available(rcu_nocb_mask
) && cpumask_weight(rcu_nocb_mask
))
2290 WARN_ON(sched_setaffinity(current
->pid
, rcu_nocb_mask
));
2292 EXPORT_SYMBOL_GPL(rcu_bind_current_to_nocb
);
2295 * Return the number of RCU callbacks still queued from the specified
2296 * CPU, which must be a nocbs CPU.
2298 static unsigned long rcu_get_n_cbs_nocb_cpu(struct rcu_data
*rdp
)
2300 return atomic_long_read(&rdp
->nocb_q_count
);
2303 #else /* #ifdef CONFIG_RCU_NOCB_CPU */
2305 static bool rcu_nocb_cpu_needs_barrier(int cpu
)
2307 WARN_ON_ONCE(1); /* Should be dead code. */
2311 static void rcu_nocb_gp_cleanup(struct swait_queue_head
*sq
)
2315 static struct swait_queue_head
*rcu_nocb_gp_get(struct rcu_node
*rnp
)
2320 static void rcu_init_one_nocb(struct rcu_node
*rnp
)
2324 static bool __call_rcu_nocb(struct rcu_data
*rdp
, struct rcu_head
*rhp
,
2325 bool lazy
, unsigned long flags
)
2330 static bool __maybe_unused
rcu_nocb_adopt_orphan_cbs(struct rcu_data
*my_rdp
,
2331 struct rcu_data
*rdp
,
2332 unsigned long flags
)
2337 static void __init
rcu_boot_init_nocb_percpu_data(struct rcu_data
*rdp
)
2341 static int rcu_nocb_need_deferred_wakeup(struct rcu_data
*rdp
)
2346 static void do_nocb_deferred_wakeup(struct rcu_data
*rdp
)
2350 static void rcu_spawn_cpu_nocb_kthread(int cpu
)
2354 static void __init
rcu_spawn_nocb_kthreads(void)
2358 static bool init_nocb_callback_list(struct rcu_data
*rdp
)
2363 static unsigned long rcu_get_n_cbs_nocb_cpu(struct rcu_data
*rdp
)
2368 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
2371 * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
2372 * grace-period kthread will do force_quiescent_state() processing?
2373 * The idea is to avoid waking up RCU core processing on such a
2374 * CPU unless the grace period has extended for too long.
2376 * This code relies on the fact that all NO_HZ_FULL CPUs are also
2377 * CONFIG_RCU_NOCB_CPU CPUs.
2379 static bool rcu_nohz_full_cpu(void)
2381 #ifdef CONFIG_NO_HZ_FULL
2382 if (tick_nohz_full_cpu(smp_processor_id()) &&
2383 (!rcu_gp_in_progress() ||
2384 ULONG_CMP_LT(jiffies
, READ_ONCE(rcu_state
.gp_start
) + HZ
)))
2386 #endif /* #ifdef CONFIG_NO_HZ_FULL */
2391 * Bind the RCU grace-period kthreads to the housekeeping CPU.
2393 static void rcu_bind_gp_kthread(void)
2395 if (!tick_nohz_full_enabled())
2397 housekeeping_affine(current
, HK_FLAG_RCU
);
2400 /* Record the current task on dyntick-idle entry. */
2401 static void rcu_dynticks_task_enter(void)
2403 #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
2404 WRITE_ONCE(current
->rcu_tasks_idle_cpu
, smp_processor_id());
2405 #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
2408 /* Record no current task on dyntick-idle exit. */
2409 static void rcu_dynticks_task_exit(void)
2411 #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
2412 WRITE_ONCE(current
->rcu_tasks_idle_cpu
, -1);
2413 #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */