rcu: Fix typo in tree_exp.h comment
[linux-2.6-block.git] / kernel / rcu / tree_plugin.h
CommitLineData
22e40925 1/* SPDX-License-Identifier: GPL-2.0+ */
f41d911f
PM
2/*
3 * Read-Copy Update mechanism for mutual exclusion (tree-based version)
4 * Internal non-public definitions that provide either classic
6cc68793 5 * or preemptible semantics.
f41d911f 6 *
f41d911f
PM
7 * Copyright Red Hat, 2009
8 * Copyright IBM Corporation, 2009
9 *
10 * Author: Ingo Molnar <mingo@elte.hu>
22e40925 11 * Paul E. McKenney <paulmck@linux.ibm.com>
f41d911f
PM
12 */
13
d9a3da06 14#include <linux/delay.h>
3fbfbf7a 15#include <linux/gfp.h>
b626c1b6 16#include <linux/oom.h>
b17b0153 17#include <linux/sched/debug.h>
62ab7072 18#include <linux/smpboot.h>
78634061 19#include <linux/sched/isolation.h>
ae7e81c0 20#include <uapi/linux/sched/types.h>
4102adab 21#include "../time/tick-internal.h"
f41d911f 22
5b61b0ba 23#ifdef CONFIG_RCU_BOOST
abaa93d9 24#include "../locking/rtmutex_common.h"
727b705b
PM
25#else /* #ifdef CONFIG_RCU_BOOST */
26
27/*
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.
32 */
33#define rt_mutex_owner(a) ({ WARN_ON_ONCE(1); NULL; })
b8869781 34#define rt_mutex_futex_unlock(x) WARN_ON_ONCE(1)
727b705b
PM
35
36#endif /* #else #ifdef CONFIG_RCU_BOOST */
5b61b0ba 37
3fbfbf7a
PM
38#ifdef CONFIG_RCU_NOCB_CPU
39static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
1b0048a4 40static bool __read_mostly rcu_nocb_poll; /* Offload kthread are to poll. */
3fbfbf7a
PM
41#endif /* #ifdef CONFIG_RCU_NOCB_CPU */
42
26845c28
PM
43/*
44 * Check the RCU kernel configuration parameters and print informative
699d4035 45 * messages about anything out of the ordinary.
26845c28
PM
46 */
47static void __init rcu_bootup_announce_oddness(void)
48{
ab6f5bd6 49 if (IS_ENABLED(CONFIG_RCU_TRACE))
ae91aa0a 50 pr_info("\tRCU event tracing is enabled.\n");
05c5df31
PM
51 if ((IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 64) ||
52 (!IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 32))
a7538352
JP
53 pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d.\n",
54 RCU_FANOUT);
7fa27001 55 if (rcu_fanout_exact)
ab6f5bd6
PM
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");
c4a09ff7 59 if (IS_ENABLED(CONFIG_PROVE_RCU))
ab6f5bd6 60 pr_info("\tRCU lockdep checking is enabled.\n");
42621697
AG
61 if (RCU_NUM_LVLS >= 4)
62 pr_info("\tFour(or more)-level hierarchy is enabled.\n");
47d631af 63 if (RCU_FANOUT_LEAF != 16)
a3bd2c09 64 pr_info("\tBuild-time adjustment of leaf fanout to %d.\n",
47d631af
PM
65 RCU_FANOUT_LEAF);
66 if (rcu_fanout_leaf != RCU_FANOUT_LEAF)
a7538352
JP
67 pr_info("\tBoot-time adjustment of leaf fanout to %d.\n",
68 rcu_fanout_leaf);
cca6f393 69 if (nr_cpu_ids != NR_CPUS)
9b130ad5 70 pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%u.\n", NR_CPUS, nr_cpu_ids);
17c7798b 71#ifdef CONFIG_RCU_BOOST
a7538352
JP
72 pr_info("\tRCU priority boosting: priority %d delay %d ms.\n",
73 kthread_prio, CONFIG_RCU_BOOST_DELAY);
17c7798b
PM
74#endif
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);
c06aed0e
PM
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);
17c7798b
PM
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");
90040c9e 91 if (gp_preinit_delay)
17c7798b 92 pr_info("\tRCU debug GP pre-init slowdown %d jiffies.\n", gp_preinit_delay);
90040c9e 93 if (gp_init_delay)
17c7798b 94 pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_init_delay);
90040c9e 95 if (gp_cleanup_delay)
17c7798b
PM
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");
59d80fd8 99 rcupdate_announce_bootup_oddness();
26845c28
PM
100}
101
28f6569a 102#ifdef CONFIG_PREEMPT_RCU
f41d911f 103
63d4c8c9 104static void rcu_report_exp_rnp(struct rcu_node *rnp, bool wake);
3949fa9b 105static void rcu_read_unlock_special(struct task_struct *t);
d9a3da06 106
f41d911f
PM
107/*
108 * Tell them what RCU they are running.
109 */
0e0fc1c2 110static void __init rcu_bootup_announce(void)
f41d911f 111{
efc151c3 112 pr_info("Preemptible hierarchical RCU implementation.\n");
26845c28 113 rcu_bootup_announce_oddness();
f41d911f
PM
114}
115
8203d6d0
PM
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
121
122/*
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.
135 *
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.
140 *
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.
149 */
46a5d164
PM
150static void rcu_preempt_ctxt_queue(struct rcu_node *rnp, struct rcu_data *rdp)
151 __releases(rnp->lock) /* But leaves rrupts disabled. */
8203d6d0
PM
152{
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;
158
a32e01ee 159 raw_lockdep_assert_held_rcu_node(rnp);
2dee9404 160 WARN_ON_ONCE(rdp->mynode != rnp);
5b4c11d5 161 WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
1f3e5f51
PM
162 /* RCU better not be waiting on newly onlined CPUs! */
163 WARN_ON_ONCE(rnp->qsmaskinitnext & ~rnp->qsmaskinit & rnp->qsmask &
164 rdp->grpmask);
ea9b0c8a 165
8203d6d0
PM
166 /*
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.
170 */
171 switch (blkd_state) {
172 case 0:
173 case RCU_EXP_TASKS:
174 case RCU_EXP_TASKS + RCU_GP_BLKD:
175 case RCU_GP_TASKS:
176 case RCU_GP_TASKS + RCU_EXP_TASKS:
177
178 /*
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.
183 */
184 list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
185 break;
186
187 case RCU_EXP_BLKD:
188 case RCU_GP_BLKD:
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:
193
194 /*
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.
201 */
202 list_add_tail(&t->rcu_node_entry, &rnp->blkd_tasks);
203 break;
204
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:
208
209 /*
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.
214 */
215 list_add(&t->rcu_node_entry, rnp->exp_tasks);
216 break;
217
218 case RCU_GP_TASKS + RCU_GP_BLKD:
219 case RCU_GP_TASKS + RCU_EXP_TASKS + RCU_GP_BLKD:
220
221 /*
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.
225 */
226 list_add(&t->rcu_node_entry, rnp->gp_tasks);
227 break;
228
229 default:
230
231 /* Yet another exercise in excessive paranoia. */
232 WARN_ON_ONCE(1);
233 break;
234 }
235
236 /*
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
240 * blocked tasks.
241 */
4bc8d555 242 if (!rnp->gp_tasks && (blkd_state & RCU_GP_BLKD)) {
8203d6d0 243 rnp->gp_tasks = &t->rcu_node_entry;
d43a5d32 244 WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq);
4bc8d555 245 }
8203d6d0
PM
246 if (!rnp->exp_tasks && (blkd_state & RCU_EXP_BLKD))
247 rnp->exp_tasks = &t->rcu_node_entry;
2dee9404
PM
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));
67c583a7 252 raw_spin_unlock_rcu_node(rnp); /* interrupts remain disabled. */
8203d6d0
PM
253
254 /*
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.)
259 */
fcc878e4 260 if (blkd_state & RCU_EXP_BLKD && rdp->deferred_qs)
63d4c8c9 261 rcu_report_exp_rdp(rdp);
fcc878e4
PM
262 else
263 WARN_ON_ONCE(rdp->deferred_qs);
8203d6d0
PM
264}
265
f41d911f 266/*
c7037ff5
PM
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.
25502a6c 276 *
c7037ff5 277 * Callers to this function must disable preemption.
f41d911f 278 */
45975c7d 279static void rcu_qs(void)
f41d911f 280{
45975c7d 281 RCU_LOCKDEP_WARN(preemptible(), "rcu_qs() invoked with preemption enabled!!!\n");
2280ee5a 282 if (__this_cpu_read(rcu_data.cpu_no_qs.s)) {
284a8c93 283 trace_rcu_grace_period(TPS("rcu_preempt"),
2280ee5a 284 __this_cpu_read(rcu_data.gp_seq),
284a8c93 285 TPS("cpuqs"));
2280ee5a 286 __this_cpu_write(rcu_data.cpu_no_qs.b.norm, false);
c98cac60 287 barrier(); /* Coordinate with rcu_flavor_sched_clock_irq(). */
284a8c93
PM
288 current->rcu_read_unlock_special.b.need_qs = false;
289 }
f41d911f
PM
290}
291
292/*
c3422bea
PM
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
12f5f524
PM
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.
c3422bea 302 *
46a5d164 303 * Caller must disable interrupts.
f41d911f 304 */
45975c7d 305void rcu_note_context_switch(bool preempt)
f41d911f
PM
306{
307 struct task_struct *t = current;
da1df50d 308 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
f41d911f
PM
309 struct rcu_node *rnp;
310
45975c7d
PM
311 barrier(); /* Avoid RCU read-side critical sections leaking down. */
312 trace_rcu_utilization(TPS("Start context switch"));
b04db8e1 313 lockdep_assert_irqs_disabled();
5b72f964 314 WARN_ON_ONCE(!preempt && t->rcu_read_lock_nesting > 0);
10f39bb1 315 if (t->rcu_read_lock_nesting > 0 &&
1d082fd0 316 !t->rcu_read_unlock_special.b.blocked) {
f41d911f
PM
317
318 /* Possibly blocking in an RCU read-side critical section. */
f41d911f 319 rnp = rdp->mynode;
46a5d164 320 raw_spin_lock_rcu_node(rnp);
1d082fd0 321 t->rcu_read_unlock_special.b.blocked = true;
86848966 322 t->rcu_blocked_node = rnp;
f41d911f
PM
323
324 /*
8203d6d0
PM
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.
f41d911f 328 */
0aa04b05 329 WARN_ON_ONCE((rdp->grpmask & rcu_rnp_online_cpus(rnp)) == 0);
e7d8842e 330 WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
88d1bead 331 trace_rcu_preempt_task(rcu_state.name,
d4c08f2a
PM
332 t->pid,
333 (rnp->qsmask & rdp->grpmask)
598ce094
PM
334 ? rnp->gp_seq
335 : rcu_seq_snap(&rnp->gp_seq));
46a5d164 336 rcu_preempt_ctxt_queue(rnp, rdp);
10f39bb1 337 } else if (t->rcu_read_lock_nesting < 0 &&
1d082fd0 338 t->rcu_read_unlock_special.s) {
10f39bb1
PM
339
340 /*
341 * Complete exit from RCU read-side critical section on
342 * behalf of preempted instance of __rcu_read_unlock().
343 */
344 rcu_read_unlock_special(t);
3e310098
PM
345 rcu_preempt_deferred_qs(t);
346 } else {
347 rcu_preempt_deferred_qs(t);
f41d911f
PM
348 }
349
350 /*
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.
358 */
45975c7d 359 rcu_qs();
ba1c64c2 360 if (rdp->deferred_qs)
63d4c8c9 361 rcu_report_exp_rdp(rdp);
45975c7d
PM
362 trace_rcu_utilization(TPS("End context switch"));
363 barrier(); /* Avoid RCU read-side critical sections leaking up. */
f41d911f 364}
45975c7d 365EXPORT_SYMBOL_GPL(rcu_note_context_switch);
f41d911f 366
fc2219d4
PM
367/*
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.
371 */
27f4d280 372static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
fc2219d4 373{
12f5f524 374 return rnp->gp_tasks != NULL;
fc2219d4
PM
375}
376
5f1a6ef3
PM
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)
381
0e5da22e
PM
382/*
383 * Preemptible RCU implementation for rcu_read_lock().
384 * Just increment ->rcu_read_lock_nesting, shared state will be updated
385 * if we block.
386 */
387void __rcu_read_lock(void)
388{
389 current->rcu_read_lock_nesting++;
5f1a6ef3
PM
390 if (IS_ENABLED(CONFIG_PROVE_LOCKING))
391 WARN_ON_ONCE(current->rcu_read_lock_nesting > RCU_NEST_PMAX);
0e5da22e
PM
392 barrier(); /* critical section after entry code. */
393}
394EXPORT_SYMBOL_GPL(__rcu_read_lock);
395
396/*
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.
402 */
403void __rcu_read_unlock(void)
404{
405 struct task_struct *t = current;
406
407 if (t->rcu_read_lock_nesting != 1) {
408 --t->rcu_read_lock_nesting;
409 } else {
410 barrier(); /* critical section before exit code. */
5f1a6ef3 411 t->rcu_read_lock_nesting = -RCU_NEST_BIAS;
0e5da22e
PM
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;
417 }
5f1a6ef3
PM
418 if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
419 int rrln = t->rcu_read_lock_nesting;
0e5da22e 420
5f1a6ef3 421 WARN_ON_ONCE(rrln < 0 && rrln > RCU_NEST_NMAX);
0e5da22e 422 }
0e5da22e
PM
423}
424EXPORT_SYMBOL_GPL(__rcu_read_unlock);
425
12f5f524
PM
426/*
427 * Advance a ->blkd_tasks-list pointer to the next entry, instead
428 * returning NULL if at the end of the list.
429 */
430static struct list_head *rcu_next_node_entry(struct task_struct *t,
431 struct rcu_node *rnp)
432{
433 struct list_head *np;
434
435 np = t->rcu_node_entry.next;
436 if (np == &rnp->blkd_tasks)
437 np = NULL;
438 return np;
439}
440
8af3a5e7
PM
441/*
442 * Return true if the specified rcu_node structure has tasks that were
443 * preempted within an RCU read-side critical section.
444 */
445static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
446{
447 return !list_empty(&rnp->blkd_tasks);
448}
449
b668c9cf 450/*
3e310098
PM
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().
b668c9cf 454 */
3e310098
PM
455static void
456rcu_preempt_deferred_qs_irqrestore(struct task_struct *t, unsigned long flags)
f41d911f 457{
b6a932d1
PM
458 bool empty_exp;
459 bool empty_norm;
460 bool empty_exp_now;
12f5f524 461 struct list_head *np;
abaa93d9 462 bool drop_boost_mutex = false;
8203d6d0 463 struct rcu_data *rdp;
f41d911f 464 struct rcu_node *rnp;
1d082fd0 465 union rcu_special special;
f41d911f 466
f41d911f 467 /*
8203d6d0
PM
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,
1d082fd0 470 * t->rcu_read_unlock_special cannot change.
f41d911f
PM
471 */
472 special = t->rcu_read_unlock_special;
da1df50d 473 rdp = this_cpu_ptr(&rcu_data);
3e310098
PM
474 if (!special.s && !rdp->deferred_qs) {
475 local_irq_restore(flags);
476 return;
477 }
1d082fd0 478 if (special.b.need_qs) {
45975c7d 479 rcu_qs();
c0135d07 480 t->rcu_read_unlock_special.b.need_qs = false;
3e310098 481 if (!t->rcu_read_unlock_special.s && !rdp->deferred_qs) {
79a62f95
LJ
482 local_irq_restore(flags);
483 return;
484 }
f41d911f
PM
485 }
486
8203d6d0 487 /*
3e310098
PM
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.
8203d6d0 492 */
fcc878e4 493 if (rdp->deferred_qs) {
63d4c8c9 494 rcu_report_exp_rdp(rdp);
8203d6d0
PM
495 if (!t->rcu_read_unlock_special.s) {
496 local_irq_restore(flags);
497 return;
498 }
499 }
500
f41d911f 501 /* Clean up if blocked during RCU read-side critical section. */
1d082fd0
PM
502 if (special.b.blocked) {
503 t->rcu_read_unlock_special.b.blocked = false;
f41d911f 504
dd5d19ba 505 /*
0a0ba1c9 506 * Remove this task from the list it blocked on. The task
8ba9153b
PM
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.
dd5d19ba 510 */
8ba9153b
PM
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);
5b4c11d5 514 WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
74e871ac 515 empty_norm = !rcu_preempt_blocked_readers_cgp(rnp);
d43a5d32 516 WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq &&
4bc8d555 517 (!empty_norm || rnp->qsmask));
8203d6d0 518 empty_exp = sync_rcu_preempt_exp_done(rnp);
d9a3da06 519 smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
12f5f524 520 np = rcu_next_node_entry(t, rnp);
f41d911f 521 list_del_init(&t->rcu_node_entry);
82e78d80 522 t->rcu_blocked_node = NULL;
f7f7bac9 523 trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
865aa1e0 524 rnp->gp_seq, t->pid);
12f5f524
PM
525 if (&t->rcu_node_entry == rnp->gp_tasks)
526 rnp->gp_tasks = np;
527 if (&t->rcu_node_entry == rnp->exp_tasks)
528 rnp->exp_tasks = np;
727b705b 529 if (IS_ENABLED(CONFIG_RCU_BOOST)) {
727b705b
PM
530 /* Snapshot ->boost_mtx ownership w/rnp->lock held. */
531 drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx) == t;
2dee9404
PM
532 if (&t->rcu_node_entry == rnp->boost_tasks)
533 rnp->boost_tasks = np;
727b705b 534 }
f41d911f
PM
535
536 /*
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.
389abd48
PM
539 * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
540 * so we must take a snapshot of the expedited state.
f41d911f 541 */
8203d6d0 542 empty_exp_now = sync_rcu_preempt_exp_done(rnp);
74e871ac 543 if (!empty_norm && !rcu_preempt_blocked_readers_cgp(rnp)) {
f7f7bac9 544 trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
db023296 545 rnp->gp_seq,
d4c08f2a
PM
546 0, rnp->qsmask,
547 rnp->level,
548 rnp->grplo,
549 rnp->grphi,
550 !!rnp->gp_tasks);
139ad4da 551 rcu_report_unblock_qs_rnp(rnp, flags);
c701d5d9 552 } else {
67c583a7 553 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
c701d5d9 554 }
d9a3da06 555
27f4d280 556 /* Unboost if we were boosted. */
727b705b 557 if (IS_ENABLED(CONFIG_RCU_BOOST) && drop_boost_mutex)
02a7c234 558 rt_mutex_futex_unlock(&rnp->boost_mtx);
27f4d280 559
d9a3da06
PM
560 /*
561 * If this was the last task on the expedited lists,
562 * then we need to report up the rcu_node hierarchy.
563 */
389abd48 564 if (!empty_exp && empty_exp_now)
63d4c8c9 565 rcu_report_exp_rnp(rnp, true);
b668c9cf
PM
566 } else {
567 local_irq_restore(flags);
f41d911f 568 }
f41d911f
PM
569}
570
3e310098
PM
571/*
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.
579 */
580static bool rcu_preempt_need_deferred_qs(struct task_struct *t)
581{
117f683c 582 return (__this_cpu_read(rcu_data.deferred_qs) ||
3e310098 583 READ_ONCE(t->rcu_read_unlock_special.s)) &&
27c744e3 584 t->rcu_read_lock_nesting <= 0;
3e310098
PM
585}
586
587/*
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
592 * disabling.
593 */
594static void rcu_preempt_deferred_qs(struct task_struct *t)
595{
596 unsigned long flags;
597 bool couldrecurse = t->rcu_read_lock_nesting >= 0;
598
599 if (!rcu_preempt_need_deferred_qs(t))
600 return;
601 if (couldrecurse)
5f1a6ef3 602 t->rcu_read_lock_nesting -= RCU_NEST_BIAS;
3e310098
PM
603 local_irq_save(flags);
604 rcu_preempt_deferred_qs_irqrestore(t, flags);
605 if (couldrecurse)
5f1a6ef3 606 t->rcu_read_lock_nesting += RCU_NEST_BIAS;
3e310098
PM
607}
608
609/*
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.
613 */
614static void rcu_read_unlock_special(struct task_struct *t)
615{
616 unsigned long flags;
617 bool preempt_bh_were_disabled =
618 !!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK));
619 bool irqs_were_disabled;
620
621 /* NMI handlers cannot block and cannot safely manipulate state. */
622 if (in_nmi())
623 return;
624
625 local_irq_save(flags);
626 irqs_were_disabled = irqs_disabled_flags(flags);
05f41571
PM
627 if (preempt_bh_were_disabled || irqs_were_disabled) {
628 WRITE_ONCE(t->rcu_read_unlock_special.b.exp_hint, false);
3e310098 629 /* Need to defer quiescent state until everything is enabled. */
05f41571
PM
630 if (irqs_were_disabled) {
631 /* Enabling irqs does not reschedule, so... */
632 raise_softirq_irqoff(RCU_SOFTIRQ);
633 } else {
634 /* Enabling BH or preempt does reschedule, so... */
635 set_tsk_need_resched(current);
636 set_preempt_need_resched();
637 }
3e310098
PM
638 local_irq_restore(flags);
639 return;
640 }
05f41571 641 WRITE_ONCE(t->rcu_read_unlock_special.b.exp_hint, false);
3e310098
PM
642 rcu_preempt_deferred_qs_irqrestore(t, flags);
643}
644
1ed509a2
PM
645/*
646 * Dump detailed information for all tasks blocking the current RCU
647 * grace period on the specified rcu_node structure.
648 */
649static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
650{
651 unsigned long flags;
1ed509a2
PM
652 struct task_struct *t;
653
6cf10081 654 raw_spin_lock_irqsave_rcu_node(rnp, flags);
5fd4dc06 655 if (!rcu_preempt_blocked_readers_cgp(rnp)) {
67c583a7 656 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
5fd4dc06
PM
657 return;
658 }
82efed06 659 t = list_entry(rnp->gp_tasks->prev,
12f5f524 660 struct task_struct, rcu_node_entry);
3caa973b
TH
661 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
662 /*
663 * We could be printing a lot while holding a spinlock.
664 * Avoid triggering hard lockup.
665 */
666 touch_nmi_watchdog();
12f5f524 667 sched_show_task(t);
3caa973b 668 }
67c583a7 669 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1ed509a2
PM
670}
671
672/*
673 * Dump detailed information for all tasks blocking the current RCU
674 * grace period.
675 */
a2887cd8 676static void rcu_print_detail_task_stall(void)
1ed509a2 677{
336a4f6c 678 struct rcu_node *rnp = rcu_get_root();
1ed509a2
PM
679
680 rcu_print_detail_task_stall_rnp(rnp);
aedf4ba9 681 rcu_for_each_leaf_node(rnp)
1ed509a2
PM
682 rcu_print_detail_task_stall_rnp(rnp);
683}
684
a858af28
PM
685static void rcu_print_task_stall_begin(struct rcu_node *rnp)
686{
efc151c3 687 pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
a858af28
PM
688 rnp->level, rnp->grplo, rnp->grphi);
689}
690
691static void rcu_print_task_stall_end(void)
692{
efc151c3 693 pr_cont("\n");
a858af28
PM
694}
695
f41d911f
PM
696/*
697 * Scan the current list of tasks blocked within RCU read-side critical
698 * sections, printing out the tid of each.
699 */
9bc8b558 700static int rcu_print_task_stall(struct rcu_node *rnp)
f41d911f 701{
f41d911f 702 struct task_struct *t;
9bc8b558 703 int ndetected = 0;
f41d911f 704
27f4d280 705 if (!rcu_preempt_blocked_readers_cgp(rnp))
9bc8b558 706 return 0;
a858af28 707 rcu_print_task_stall_begin(rnp);
82efed06 708 t = list_entry(rnp->gp_tasks->prev,
12f5f524 709 struct task_struct, rcu_node_entry);
9bc8b558 710 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
efc151c3 711 pr_cont(" P%d", t->pid);
9bc8b558
PM
712 ndetected++;
713 }
a858af28 714 rcu_print_task_stall_end();
9bc8b558 715 return ndetected;
f41d911f
PM
716}
717
74611ecb
PM
718/*
719 * Scan the current list of tasks blocked within RCU read-side critical
720 * sections, printing out the tid of each that is blocking the current
721 * expedited grace period.
722 */
723static int rcu_print_task_exp_stall(struct rcu_node *rnp)
724{
725 struct task_struct *t;
726 int ndetected = 0;
727
728 if (!rnp->exp_tasks)
729 return 0;
730 t = list_entry(rnp->exp_tasks->prev,
731 struct task_struct, rcu_node_entry);
732 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
733 pr_cont(" P%d", t->pid);
734 ndetected++;
735 }
736 return ndetected;
737}
738
b0e165c0
PM
739/*
740 * Check that the list of blocked tasks for the newly completed grace
741 * period is in fact empty. It is a serious bug to complete a grace
742 * period that still has RCU readers blocked! This function must be
ff3bb6f4 743 * invoked -before- updating this rnp's ->gp_seq, and the rnp's ->lock
b0e165c0 744 * must be held by the caller.
12f5f524
PM
745 *
746 * Also, if there are blocked tasks on the list, they automatically
747 * block the newly created grace period, so set up ->gp_tasks accordingly.
b0e165c0 748 */
81ab59a3 749static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
b0e165c0 750{
c5ebe66c
PM
751 struct task_struct *t;
752
ea9b0c8a 753 RCU_LOCKDEP_WARN(preemptible(), "rcu_preempt_check_blocked_tasks() invoked with preemption enabled!!!\n");
4bc8d555 754 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
81ab59a3 755 dump_blkd_tasks(rnp, 10);
0b107d24
PM
756 if (rcu_preempt_has_tasks(rnp) &&
757 (rnp->qsmaskinit || rnp->wait_blkd_tasks)) {
12f5f524 758 rnp->gp_tasks = rnp->blkd_tasks.next;
c5ebe66c
PM
759 t = container_of(rnp->gp_tasks, struct task_struct,
760 rcu_node_entry);
761 trace_rcu_unlock_preempted_task(TPS("rcu_preempt-GPS"),
865aa1e0 762 rnp->gp_seq, t->pid);
c5ebe66c 763 }
28ecd580 764 WARN_ON_ONCE(rnp->qsmask);
b0e165c0
PM
765}
766
f41d911f 767/*
c98cac60
PM
768 * Check for a quiescent state from the current CPU, including voluntary
769 * context switches for Tasks RCU. When a task blocks, the task is
770 * recorded in the corresponding CPU's rcu_node structure, which is checked
771 * elsewhere, hence this function need only check for quiescent states
772 * related to the current CPU, not to those related to tasks.
f41d911f 773 */
c98cac60 774static void rcu_flavor_sched_clock_irq(int user)
f41d911f
PM
775{
776 struct task_struct *t = current;
777
45975c7d
PM
778 if (user || rcu_is_cpu_rrupt_from_idle()) {
779 rcu_note_voluntary_context_switch(current);
780 }
3e310098
PM
781 if (t->rcu_read_lock_nesting > 0 ||
782 (preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK))) {
783 /* No QS, force context switch if deferred. */
fced9c8c
PM
784 if (rcu_preempt_need_deferred_qs(t)) {
785 set_tsk_need_resched(t);
786 set_preempt_need_resched();
787 }
3e310098
PM
788 } else if (rcu_preempt_need_deferred_qs(t)) {
789 rcu_preempt_deferred_qs(t); /* Report deferred QS. */
790 return;
791 } else if (!t->rcu_read_lock_nesting) {
45975c7d 792 rcu_qs(); /* Report immediate QS. */
f41d911f
PM
793 return;
794 }
3e310098
PM
795
796 /* If GP is oldish, ask for help from rcu_read_unlock_special(). */
10f39bb1 797 if (t->rcu_read_lock_nesting > 0 &&
2280ee5a
PM
798 __this_cpu_read(rcu_data.core_needs_qs) &&
799 __this_cpu_read(rcu_data.cpu_no_qs.b.norm) &&
15651201 800 !t->rcu_read_unlock_special.b.need_qs &&
564a9ae6 801 time_after(jiffies, rcu_state.gp_start + HZ))
1d082fd0 802 t->rcu_read_unlock_special.b.need_qs = true;
f41d911f
PM
803}
804
2439b696
PM
805/*
806 * Check for a task exiting while in a preemptible-RCU read-side
884157ce
PM
807 * critical section, clean up if so. No need to issue warnings, as
808 * debug_check_no_locks_held() already does this if lockdep is enabled.
809 * Besides, if this function does anything other than just immediately
810 * return, there was a bug of some sort. Spewing warnings from this
811 * function is like as not to simply obscure important prior warnings.
2439b696
PM
812 */
813void exit_rcu(void)
814{
815 struct task_struct *t = current;
816
884157ce
PM
817 if (unlikely(!list_empty(&current->rcu_node_entry))) {
818 t->rcu_read_lock_nesting = 1;
819 barrier();
820 t->rcu_read_unlock_special.b.blocked = true;
821 } else if (unlikely(t->rcu_read_lock_nesting)) {
822 t->rcu_read_lock_nesting = 1;
823 } else {
2439b696 824 return;
884157ce 825 }
2439b696 826 __rcu_read_unlock();
3e310098 827 rcu_preempt_deferred_qs(current);
2439b696
PM
828}
829
4bc8d555
PM
830/*
831 * Dump the blocked-tasks state, but limit the list dump to the
832 * specified number of elements.
833 */
57738942 834static void
81ab59a3 835dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
4bc8d555 836{
57738942 837 int cpu;
4bc8d555
PM
838 int i;
839 struct list_head *lhp;
57738942
PM
840 bool onl;
841 struct rcu_data *rdp;
ff3cee39 842 struct rcu_node *rnp1;
4bc8d555 843
ce11fae8 844 raw_lockdep_assert_held_rcu_node(rnp);
ff3cee39 845 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
77cfc7bf 846 __func__, rnp->grplo, rnp->grphi, rnp->level,
ff3cee39
PM
847 (long)rnp->gp_seq, (long)rnp->completedqs);
848 for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
849 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx\n",
850 __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext);
77cfc7bf
PM
851 pr_info("%s: ->gp_tasks %p ->boost_tasks %p ->exp_tasks %p\n",
852 __func__, rnp->gp_tasks, rnp->boost_tasks, rnp->exp_tasks);
853 pr_info("%s: ->blkd_tasks", __func__);
4bc8d555
PM
854 i = 0;
855 list_for_each(lhp, &rnp->blkd_tasks) {
856 pr_cont(" %p", lhp);
857 if (++i >= 10)
858 break;
859 }
860 pr_cont("\n");
57738942 861 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++) {
da1df50d 862 rdp = per_cpu_ptr(&rcu_data, cpu);
57738942
PM
863 onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
864 pr_info("\t%d: %c online: %ld(%d) offline: %ld(%d)\n",
865 cpu, ".o"[onl],
866 (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
867 (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
868 }
4bc8d555
PM
869}
870
28f6569a 871#else /* #ifdef CONFIG_PREEMPT_RCU */
f41d911f
PM
872
873/*
874 * Tell them what RCU they are running.
875 */
0e0fc1c2 876static void __init rcu_bootup_announce(void)
f41d911f 877{
efc151c3 878 pr_info("Hierarchical RCU implementation.\n");
26845c28 879 rcu_bootup_announce_oddness();
f41d911f
PM
880}
881
45975c7d
PM
882/*
883 * Note a quiescent state for PREEMPT=n. Because we do not need to know
884 * how many quiescent states passed, just if there was at least one since
885 * the start of the grace period, this just sets a flag. The caller must
886 * have disabled preemption.
887 */
888static void rcu_qs(void)
d28139c4 889{
45975c7d
PM
890 RCU_LOCKDEP_WARN(preemptible(), "rcu_qs() invoked with preemption enabled!!!");
891 if (!__this_cpu_read(rcu_data.cpu_no_qs.s))
892 return;
893 trace_rcu_grace_period(TPS("rcu_sched"),
894 __this_cpu_read(rcu_data.gp_seq), TPS("cpuqs"));
895 __this_cpu_write(rcu_data.cpu_no_qs.b.norm, false);
896 if (!__this_cpu_read(rcu_data.cpu_no_qs.b.exp))
897 return;
898 __this_cpu_write(rcu_data.cpu_no_qs.b.exp, false);
63d4c8c9 899 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
d28139c4
PM
900}
901
395a2f09
PM
902/*
903 * Register an urgently needed quiescent state. If there is an
904 * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
905 * dyntick-idle quiescent state visible to other CPUs, which will in
906 * some cases serve for expedited as well as normal grace periods.
907 * Either way, register a lightweight quiescent state.
908 *
909 * The barrier() calls are redundant in the common case when this is
910 * called externally, but just in case this is called from within this
911 * file.
912 *
913 */
914void rcu_all_qs(void)
915{
916 unsigned long flags;
917
2dba13f0 918 if (!raw_cpu_read(rcu_data.rcu_urgent_qs))
395a2f09
PM
919 return;
920 preempt_disable();
921 /* Load rcu_urgent_qs before other flags. */
2dba13f0 922 if (!smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
395a2f09
PM
923 preempt_enable();
924 return;
925 }
2dba13f0 926 this_cpu_write(rcu_data.rcu_urgent_qs, false);
395a2f09 927 barrier(); /* Avoid RCU read-side critical sections leaking down. */
2dba13f0 928 if (unlikely(raw_cpu_read(rcu_data.rcu_need_heavy_qs))) {
395a2f09
PM
929 local_irq_save(flags);
930 rcu_momentary_dyntick_idle();
931 local_irq_restore(flags);
932 }
7e28c5af 933 rcu_qs();
395a2f09
PM
934 barrier(); /* Avoid RCU read-side critical sections leaking up. */
935 preempt_enable();
936}
937EXPORT_SYMBOL_GPL(rcu_all_qs);
938
cba6d0d6 939/*
45975c7d 940 * Note a PREEMPT=n context switch. The caller must have disabled interrupts.
cba6d0d6 941 */
45975c7d 942void rcu_note_context_switch(bool preempt)
cba6d0d6 943{
45975c7d
PM
944 barrier(); /* Avoid RCU read-side critical sections leaking down. */
945 trace_rcu_utilization(TPS("Start context switch"));
946 rcu_qs();
947 /* Load rcu_urgent_qs before other flags. */
2dba13f0 948 if (!smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs)))
45975c7d 949 goto out;
2dba13f0
PM
950 this_cpu_write(rcu_data.rcu_urgent_qs, false);
951 if (unlikely(raw_cpu_read(rcu_data.rcu_need_heavy_qs)))
45975c7d 952 rcu_momentary_dyntick_idle();
45975c7d
PM
953 if (!preempt)
954 rcu_tasks_qs(current);
955out:
956 trace_rcu_utilization(TPS("End context switch"));
957 barrier(); /* Avoid RCU read-side critical sections leaking up. */
cba6d0d6 958}
45975c7d 959EXPORT_SYMBOL_GPL(rcu_note_context_switch);
cba6d0d6 960
fc2219d4 961/*
6cc68793 962 * Because preemptible RCU does not exist, there are never any preempted
fc2219d4
PM
963 * RCU readers.
964 */
27f4d280 965static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
fc2219d4
PM
966{
967 return 0;
968}
969
8af3a5e7
PM
970/*
971 * Because there is no preemptible RCU, there can be no readers blocked.
972 */
973static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
b668c9cf 974{
8af3a5e7 975 return false;
b668c9cf
PM
976}
977
3e310098
PM
978/*
979 * Because there is no preemptible RCU, there can be no deferred quiescent
980 * states.
981 */
982static bool rcu_preempt_need_deferred_qs(struct task_struct *t)
983{
984 return false;
985}
986static void rcu_preempt_deferred_qs(struct task_struct *t) { }
987
1ed509a2 988/*
6cc68793 989 * Because preemptible RCU does not exist, we never have to check for
1ed509a2
PM
990 * tasks blocked within RCU read-side critical sections.
991 */
a2887cd8 992static void rcu_print_detail_task_stall(void)
1ed509a2
PM
993{
994}
995
f41d911f 996/*
6cc68793 997 * Because preemptible RCU does not exist, we never have to check for
f41d911f
PM
998 * tasks blocked within RCU read-side critical sections.
999 */
9bc8b558 1000static int rcu_print_task_stall(struct rcu_node *rnp)
f41d911f 1001{
9bc8b558 1002 return 0;
f41d911f
PM
1003}
1004
74611ecb
PM
1005/*
1006 * Because preemptible RCU does not exist, we never have to check for
1007 * tasks blocked within RCU read-side critical sections that are
1008 * blocking the current expedited grace period.
1009 */
1010static int rcu_print_task_exp_stall(struct rcu_node *rnp)
1011{
1012 return 0;
1013}
1014
b0e165c0 1015/*
6cc68793 1016 * Because there is no preemptible RCU, there can be no readers blocked,
49e29126
PM
1017 * so there is no need to check for blocked tasks. So check only for
1018 * bogus qsmask values.
b0e165c0 1019 */
81ab59a3 1020static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
b0e165c0 1021{
49e29126 1022 WARN_ON_ONCE(rnp->qsmask);
b0e165c0
PM
1023}
1024
f41d911f 1025/*
c98cac60
PM
1026 * Check to see if this CPU is in a non-context-switch quiescent state,
1027 * namely user mode and idle loop.
f41d911f 1028 */
c98cac60 1029static void rcu_flavor_sched_clock_irq(int user)
f41d911f 1030{
45975c7d 1031 if (user || rcu_is_cpu_rrupt_from_idle()) {
f41d911f 1032
45975c7d
PM
1033 /*
1034 * Get here if this CPU took its interrupt from user
1035 * mode or from the idle loop, and if this is not a
1036 * nested interrupt. In this case, the CPU is in
1037 * a quiescent state, so note it.
1038 *
1039 * No memory barrier is required here because rcu_qs()
1040 * references only CPU-local variables that other CPUs
1041 * neither access nor modify, at least not while the
1042 * corresponding CPU is online.
1043 */
1044
1045 rcu_qs();
1046 }
e74f4c45 1047}
e74f4c45 1048
2439b696
PM
1049/*
1050 * Because preemptible RCU does not exist, tasks cannot possibly exit
1051 * while in preemptible RCU read-side critical sections.
1052 */
1053void exit_rcu(void)
1054{
1055}
1056
4bc8d555
PM
1057/*
1058 * Dump the guaranteed-empty blocked-tasks state. Trust but verify.
1059 */
57738942 1060static void
81ab59a3 1061dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
4bc8d555
PM
1062{
1063 WARN_ON_ONCE(!list_empty(&rnp->blkd_tasks));
1064}
1065
28f6569a 1066#endif /* #else #ifdef CONFIG_PREEMPT_RCU */
8bd93a2c 1067
27f4d280
PM
1068#ifdef CONFIG_RCU_BOOST
1069
5d01bbd1
TG
1070static void rcu_wake_cond(struct task_struct *t, int status)
1071{
1072 /*
1073 * If the thread is yielding, only wake it when this
1074 * is invoked from idle
1075 */
1076 if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
1077 wake_up_process(t);
1078}
1079
27f4d280
PM
1080/*
1081 * Carry out RCU priority boosting on the task indicated by ->exp_tasks
1082 * or ->boost_tasks, advancing the pointer to the next task in the
1083 * ->blkd_tasks list.
1084 *
1085 * Note that irqs must be enabled: boosting the task can block.
1086 * Returns 1 if there are more tasks needing to be boosted.
1087 */
1088static int rcu_boost(struct rcu_node *rnp)
1089{
1090 unsigned long flags;
27f4d280
PM
1091 struct task_struct *t;
1092 struct list_head *tb;
1093
7d0ae808
PM
1094 if (READ_ONCE(rnp->exp_tasks) == NULL &&
1095 READ_ONCE(rnp->boost_tasks) == NULL)
27f4d280
PM
1096 return 0; /* Nothing left to boost. */
1097
2a67e741 1098 raw_spin_lock_irqsave_rcu_node(rnp, flags);
27f4d280
PM
1099
1100 /*
1101 * Recheck under the lock: all tasks in need of boosting
1102 * might exit their RCU read-side critical sections on their own.
1103 */
1104 if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
67c583a7 1105 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
27f4d280
PM
1106 return 0;
1107 }
1108
1109 /*
1110 * Preferentially boost tasks blocking expedited grace periods.
1111 * This cannot starve the normal grace periods because a second
1112 * expedited grace period must boost all blocked tasks, including
1113 * those blocking the pre-existing normal grace period.
1114 */
bec06785 1115 if (rnp->exp_tasks != NULL)
27f4d280 1116 tb = rnp->exp_tasks;
bec06785 1117 else
27f4d280
PM
1118 tb = rnp->boost_tasks;
1119
1120 /*
1121 * We boost task t by manufacturing an rt_mutex that appears to
1122 * be held by task t. We leave a pointer to that rt_mutex where
1123 * task t can find it, and task t will release the mutex when it
1124 * exits its outermost RCU read-side critical section. Then
1125 * simply acquiring this artificial rt_mutex will boost task
1126 * t's priority. (Thanks to tglx for suggesting this approach!)
1127 *
1128 * Note that task t must acquire rnp->lock to remove itself from
1129 * the ->blkd_tasks list, which it will do from exit() if from
1130 * nowhere else. We therefore are guaranteed that task t will
1131 * stay around at least until we drop rnp->lock. Note that
1132 * rnp->lock also resolves races between our priority boosting
1133 * and task t's exiting its outermost RCU read-side critical
1134 * section.
1135 */
1136 t = container_of(tb, struct task_struct, rcu_node_entry);
abaa93d9 1137 rt_mutex_init_proxy_locked(&rnp->boost_mtx, t);
67c583a7 1138 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
abaa93d9
PM
1139 /* Lock only for side effect: boosts task t's priority. */
1140 rt_mutex_lock(&rnp->boost_mtx);
1141 rt_mutex_unlock(&rnp->boost_mtx); /* Then keep lockdep happy. */
27f4d280 1142
7d0ae808
PM
1143 return READ_ONCE(rnp->exp_tasks) != NULL ||
1144 READ_ONCE(rnp->boost_tasks) != NULL;
27f4d280
PM
1145}
1146
27f4d280 1147/*
bc17ea10 1148 * Priority-boosting kthread, one per leaf rcu_node.
27f4d280
PM
1149 */
1150static int rcu_boost_kthread(void *arg)
1151{
1152 struct rcu_node *rnp = (struct rcu_node *)arg;
1153 int spincnt = 0;
1154 int more2boost;
1155
f7f7bac9 1156 trace_rcu_utilization(TPS("Start boost kthread@init"));
27f4d280 1157 for (;;) {
d71df90e 1158 rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
f7f7bac9 1159 trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
08bca60a 1160 rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
f7f7bac9 1161 trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
d71df90e 1162 rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
27f4d280
PM
1163 more2boost = rcu_boost(rnp);
1164 if (more2boost)
1165 spincnt++;
1166 else
1167 spincnt = 0;
1168 if (spincnt > 10) {
5d01bbd1 1169 rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
f7f7bac9 1170 trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
5d01bbd1 1171 schedule_timeout_interruptible(2);
f7f7bac9 1172 trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
27f4d280
PM
1173 spincnt = 0;
1174 }
1175 }
1217ed1b 1176 /* NOTREACHED */
f7f7bac9 1177 trace_rcu_utilization(TPS("End boost kthread@notreached"));
27f4d280
PM
1178 return 0;
1179}
1180
1181/*
1182 * Check to see if it is time to start boosting RCU readers that are
1183 * blocking the current grace period, and, if so, tell the per-rcu_node
1184 * kthread to start boosting them. If there is an expedited grace
1185 * period in progress, it is always time to boost.
1186 *
b065a853
PM
1187 * The caller must hold rnp->lock, which this function releases.
1188 * The ->boost_kthread_task is immortal, so we don't need to worry
1189 * about it going away.
27f4d280 1190 */
1217ed1b 1191static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
615e41c6 1192 __releases(rnp->lock)
27f4d280 1193{
a32e01ee 1194 raw_lockdep_assert_held_rcu_node(rnp);
0ea1f2eb 1195 if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
67c583a7 1196 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
27f4d280 1197 return;
0ea1f2eb 1198 }
27f4d280
PM
1199 if (rnp->exp_tasks != NULL ||
1200 (rnp->gp_tasks != NULL &&
1201 rnp->boost_tasks == NULL &&
1202 rnp->qsmask == 0 &&
1203 ULONG_CMP_GE(jiffies, rnp->boost_time))) {
1204 if (rnp->exp_tasks == NULL)
1205 rnp->boost_tasks = rnp->gp_tasks;
67c583a7 1206 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
a2badefa
PM
1207 rcu_wake_cond(rnp->boost_kthread_task,
1208 rnp->boost_kthread_status);
1217ed1b 1209 } else {
67c583a7 1210 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1217ed1b 1211 }
27f4d280
PM
1212}
1213
a46e0899
PM
1214/*
1215 * Wake up the per-CPU kthread to invoke RCU callbacks.
1216 */
1217static void invoke_rcu_callbacks_kthread(void)
1218{
1219 unsigned long flags;
1220
1221 local_irq_save(flags);
f7e972ee 1222 __this_cpu_write(rcu_data.rcu_cpu_has_work, 1);
37f62d7c
PM
1223 if (__this_cpu_read(rcu_data.rcu_cpu_kthread_task) != NULL &&
1224 current != __this_cpu_read(rcu_data.rcu_cpu_kthread_task)) {
1225 rcu_wake_cond(__this_cpu_read(rcu_data.rcu_cpu_kthread_task),
6ffdde28 1226 __this_cpu_read(rcu_data.rcu_cpu_kthread_status));
5d01bbd1 1227 }
a46e0899
PM
1228 local_irq_restore(flags);
1229}
1230
dff1672d
PM
1231/*
1232 * Is the current CPU running the RCU-callbacks kthread?
1233 * Caller must have preemption disabled.
1234 */
1235static bool rcu_is_callbacks_kthread(void)
1236{
37f62d7c 1237 return __this_cpu_read(rcu_data.rcu_cpu_kthread_task) == current;
dff1672d
PM
1238}
1239
27f4d280
PM
1240#define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
1241
1242/*
1243 * Do priority-boost accounting for the start of a new grace period.
1244 */
1245static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1246{
1247 rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
1248}
1249
27f4d280
PM
1250/*
1251 * Create an RCU-boost kthread for the specified node if one does not
1252 * already exist. We only create this kthread for preemptible RCU.
1253 * Returns zero if all is well, a negated errno otherwise.
1254 */
6dbfdc14 1255static int rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
27f4d280 1256{
6dbfdc14 1257 int rnp_index = rnp - rcu_get_root();
27f4d280
PM
1258 unsigned long flags;
1259 struct sched_param sp;
1260 struct task_struct *t;
1261
6dbfdc14 1262 if (!IS_ENABLED(CONFIG_PREEMPT_RCU))
27f4d280 1263 return 0;
5d01bbd1 1264
0aa04b05 1265 if (!rcu_scheduler_fully_active || rcu_rnp_online_cpus(rnp) == 0)
5d01bbd1
TG
1266 return 0;
1267
6dbfdc14 1268 rcu_state.boost = 1;
27f4d280
PM
1269 if (rnp->boost_kthread_task != NULL)
1270 return 0;
1271 t = kthread_create(rcu_boost_kthread, (void *)rnp,
5b61b0ba 1272 "rcub/%d", rnp_index);
27f4d280
PM
1273 if (IS_ERR(t))
1274 return PTR_ERR(t);
2a67e741 1275 raw_spin_lock_irqsave_rcu_node(rnp, flags);
27f4d280 1276 rnp->boost_kthread_task = t;
67c583a7 1277 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
21871d7e 1278 sp.sched_priority = kthread_prio;
27f4d280 1279 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
9a432736 1280 wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
27f4d280
PM
1281 return 0;
1282}
1283
62ab7072 1284static void rcu_cpu_kthread_setup(unsigned int cpu)
f8b7fc6b 1285{
f8b7fc6b 1286 struct sched_param sp;
f8b7fc6b 1287
21871d7e 1288 sp.sched_priority = kthread_prio;
62ab7072 1289 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
f8b7fc6b
PM
1290}
1291
62ab7072 1292static void rcu_cpu_kthread_park(unsigned int cpu)
f8b7fc6b 1293{
6ffdde28 1294 per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
f8b7fc6b
PM
1295}
1296
62ab7072 1297static int rcu_cpu_kthread_should_run(unsigned int cpu)
f8b7fc6b 1298{
f7e972ee 1299 return __this_cpu_read(rcu_data.rcu_cpu_has_work);
f8b7fc6b
PM
1300}
1301
1302/*
0ae86a27
PM
1303 * Per-CPU kernel thread that invokes RCU callbacks. This replaces
1304 * the RCU softirq used in configurations of RCU that do not support RCU
1305 * priority boosting.
f8b7fc6b 1306 */
62ab7072 1307static void rcu_cpu_kthread(unsigned int cpu)
f8b7fc6b 1308{
6ffdde28 1309 unsigned int *statusp = this_cpu_ptr(&rcu_data.rcu_cpu_kthread_status);
f7e972ee 1310 char work, *workp = this_cpu_ptr(&rcu_data.rcu_cpu_has_work);
62ab7072 1311 int spincnt;
f8b7fc6b 1312
62ab7072 1313 for (spincnt = 0; spincnt < 10; spincnt++) {
f7f7bac9 1314 trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
f8b7fc6b 1315 local_bh_disable();
f8b7fc6b 1316 *statusp = RCU_KTHREAD_RUNNING;
62ab7072 1317 local_irq_disable();
f8b7fc6b
PM
1318 work = *workp;
1319 *workp = 0;
62ab7072 1320 local_irq_enable();
f8b7fc6b 1321 if (work)
c46f497a 1322 rcu_do_batch(this_cpu_ptr(&rcu_data));
f8b7fc6b 1323 local_bh_enable();
62ab7072 1324 if (*workp == 0) {
f7f7bac9 1325 trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
62ab7072
PM
1326 *statusp = RCU_KTHREAD_WAITING;
1327 return;
f8b7fc6b
PM
1328 }
1329 }
62ab7072 1330 *statusp = RCU_KTHREAD_YIELDING;
f7f7bac9 1331 trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
62ab7072 1332 schedule_timeout_interruptible(2);
f7f7bac9 1333 trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
62ab7072 1334 *statusp = RCU_KTHREAD_WAITING;
f8b7fc6b
PM
1335}
1336
1337/*
1338 * Set the per-rcu_node kthread's affinity to cover all CPUs that are
1339 * served by the rcu_node in question. The CPU hotplug lock is still
1340 * held, so the value of rnp->qsmaskinit will be stable.
1341 *
1342 * We don't include outgoingcpu in the affinity set, use -1 if there is
1343 * no outgoing CPU. If there are no CPUs left in the affinity set,
1344 * this function allows the kthread to execute on any CPU.
1345 */
5d01bbd1 1346static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
f8b7fc6b 1347{
5d01bbd1 1348 struct task_struct *t = rnp->boost_kthread_task;
0aa04b05 1349 unsigned long mask = rcu_rnp_online_cpus(rnp);
f8b7fc6b
PM
1350 cpumask_var_t cm;
1351 int cpu;
f8b7fc6b 1352
5d01bbd1 1353 if (!t)
f8b7fc6b 1354 return;
5d01bbd1 1355 if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
f8b7fc6b 1356 return;
bc75e999
MR
1357 for_each_leaf_node_possible_cpu(rnp, cpu)
1358 if ((mask & leaf_node_cpu_bit(rnp, cpu)) &&
1359 cpu != outgoingcpu)
f8b7fc6b 1360 cpumask_set_cpu(cpu, cm);
5d0b0249 1361 if (cpumask_weight(cm) == 0)
f8b7fc6b 1362 cpumask_setall(cm);
5d01bbd1 1363 set_cpus_allowed_ptr(t, cm);
f8b7fc6b
PM
1364 free_cpumask_var(cm);
1365}
1366
62ab7072 1367static struct smp_hotplug_thread rcu_cpu_thread_spec = {
37f62d7c 1368 .store = &rcu_data.rcu_cpu_kthread_task,
62ab7072
PM
1369 .thread_should_run = rcu_cpu_kthread_should_run,
1370 .thread_fn = rcu_cpu_kthread,
1371 .thread_comm = "rcuc/%u",
1372 .setup = rcu_cpu_kthread_setup,
1373 .park = rcu_cpu_kthread_park,
1374};
f8b7fc6b
PM
1375
1376/*
9386c0b7 1377 * Spawn boost kthreads -- called as soon as the scheduler is running.
f8b7fc6b 1378 */
9386c0b7 1379static void __init rcu_spawn_boost_kthreads(void)
f8b7fc6b 1380{
f8b7fc6b 1381 struct rcu_node *rnp;
5d01bbd1 1382 int cpu;
f8b7fc6b 1383
62ab7072 1384 for_each_possible_cpu(cpu)
f7e972ee 1385 per_cpu(rcu_data.rcu_cpu_has_work, cpu) = 0;
9213784b
PM
1386 if (WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec), "%s: Could not start rcub kthread, OOM is now expected behavior\n", __func__))
1387 return;
aedf4ba9 1388 rcu_for_each_leaf_node(rnp)
6dbfdc14 1389 (void)rcu_spawn_one_boost_kthread(rnp);
f8b7fc6b 1390}
f8b7fc6b 1391
49fb4c62 1392static void rcu_prepare_kthreads(int cpu)
f8b7fc6b 1393{
da1df50d 1394 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
f8b7fc6b
PM
1395 struct rcu_node *rnp = rdp->mynode;
1396
1397 /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
62ab7072 1398 if (rcu_scheduler_fully_active)
6dbfdc14 1399 (void)rcu_spawn_one_boost_kthread(rnp);
f8b7fc6b
PM
1400}
1401
27f4d280
PM
1402#else /* #ifdef CONFIG_RCU_BOOST */
1403
1217ed1b 1404static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
615e41c6 1405 __releases(rnp->lock)
27f4d280 1406{
67c583a7 1407 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
27f4d280
PM
1408}
1409
a46e0899 1410static void invoke_rcu_callbacks_kthread(void)
27f4d280 1411{
a46e0899 1412 WARN_ON_ONCE(1);
27f4d280
PM
1413}
1414
dff1672d
PM
1415static bool rcu_is_callbacks_kthread(void)
1416{
1417 return false;
1418}
1419
27f4d280
PM
1420static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1421{
1422}
1423
5d01bbd1 1424static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
f8b7fc6b
PM
1425{
1426}
1427
9386c0b7 1428static void __init rcu_spawn_boost_kthreads(void)
b0d30417 1429{
b0d30417 1430}
b0d30417 1431
49fb4c62 1432static void rcu_prepare_kthreads(int cpu)
f8b7fc6b
PM
1433{
1434}
1435
27f4d280
PM
1436#endif /* #else #ifdef CONFIG_RCU_BOOST */
1437
8bd93a2c
PM
1438#if !defined(CONFIG_RCU_FAST_NO_HZ)
1439
1440/*
1441 * Check to see if any future RCU-related work will need to be done
1442 * by the current CPU, even if none need be done immediately, returning
1443 * 1 if so. This function is part of the RCU implementation; it is -not-
1444 * an exported member of the RCU API.
1445 *
0ae86a27
PM
1446 * Because we not have RCU_FAST_NO_HZ, just check whether or not this
1447 * CPU has RCU callbacks queued.
8bd93a2c 1448 */
c1ad348b 1449int rcu_needs_cpu(u64 basemono, u64 *nextevt)
8bd93a2c 1450{
c1ad348b 1451 *nextevt = KTIME_MAX;
260e1e4f 1452 return !rcu_segcblist_empty(&this_cpu_ptr(&rcu_data)->cblist);
7cb92499
PM
1453}
1454
1455/*
1456 * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
1457 * after it.
1458 */
8fa7845d 1459static void rcu_cleanup_after_idle(void)
7cb92499
PM
1460{
1461}
1462
aea1b35e 1463/*
a858af28 1464 * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
aea1b35e
PM
1465 * is nothing.
1466 */
198bbf81 1467static void rcu_prepare_for_idle(void)
aea1b35e
PM
1468{
1469}
1470
8bd93a2c
PM
1471#else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1472
f23f7fa1
PM
1473/*
1474 * This code is invoked when a CPU goes idle, at which point we want
1475 * to have the CPU do everything required for RCU so that it can enter
1476 * the energy-efficient dyntick-idle mode. This is handled by a
1477 * state machine implemented by rcu_prepare_for_idle() below.
1478 *
1479 * The following three proprocessor symbols control this state machine:
1480 *
f23f7fa1
PM
1481 * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
1482 * to sleep in dyntick-idle mode with RCU callbacks pending. This
1483 * is sized to be roughly one RCU grace period. Those energy-efficiency
1484 * benchmarkers who might otherwise be tempted to set this to a large
1485 * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
1486 * system. And if you are -that- concerned about energy efficiency,
1487 * just power the system down and be done with it!
778d250a
PM
1488 * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
1489 * permitted to sleep in dyntick-idle mode with only lazy RCU
1490 * callbacks pending. Setting this too high can OOM your system.
f23f7fa1
PM
1491 *
1492 * The values below work well in practice. If future workloads require
1493 * adjustment, they can be converted into kernel config parameters, though
1494 * making the state machine smarter might be a better option.
1495 */
e84c48ae 1496#define RCU_IDLE_GP_DELAY 4 /* Roughly one grace period. */
778d250a 1497#define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
f23f7fa1 1498
5e44ce35
PM
1499static int rcu_idle_gp_delay = RCU_IDLE_GP_DELAY;
1500module_param(rcu_idle_gp_delay, int, 0644);
1501static int rcu_idle_lazy_gp_delay = RCU_IDLE_LAZY_GP_DELAY;
1502module_param(rcu_idle_lazy_gp_delay, int, 0644);
486e2593 1503
486e2593 1504/*
0ae86a27
PM
1505 * Try to advance callbacks on the current CPU, but only if it has been
1506 * awhile since the last time we did so. Afterwards, if there are any
1507 * callbacks ready for immediate invocation, return true.
486e2593 1508 */
f1f399d1 1509static bool __maybe_unused rcu_try_advance_all_cbs(void)
486e2593 1510{
c0f4dfd4 1511 bool cbs_ready = false;
5998a75a 1512 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
c0f4dfd4 1513 struct rcu_node *rnp;
486e2593 1514
c229828c 1515 /* Exit early if we advanced recently. */
5998a75a 1516 if (jiffies == rdp->last_advance_all)
d0bc90fd 1517 return false;
5998a75a 1518 rdp->last_advance_all = jiffies;
c229828c 1519
b97d23c5 1520 rnp = rdp->mynode;
486e2593 1521
b97d23c5
PM
1522 /*
1523 * Don't bother checking unless a grace period has
1524 * completed since we last checked and there are
1525 * callbacks not yet ready to invoke.
1526 */
1527 if ((rcu_seq_completed_gp(rdp->gp_seq,
1528 rcu_seq_current(&rnp->gp_seq)) ||
1529 unlikely(READ_ONCE(rdp->gpwrap))) &&
1530 rcu_segcblist_pend_cbs(&rdp->cblist))
1531 note_gp_changes(rdp);
1532
1533 if (rcu_segcblist_ready_cbs(&rdp->cblist))
1534 cbs_ready = true;
c0f4dfd4 1535 return cbs_ready;
486e2593
PM
1536}
1537
aa9b1630 1538/*
c0f4dfd4
PM
1539 * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
1540 * to invoke. If the CPU has callbacks, try to advance them. Tell the
1541 * caller to set the timeout based on whether or not there are non-lazy
1542 * callbacks.
aa9b1630 1543 *
c0f4dfd4 1544 * The caller must have disabled interrupts.
aa9b1630 1545 */
c1ad348b 1546int rcu_needs_cpu(u64 basemono, u64 *nextevt)
aa9b1630 1547{
5998a75a 1548 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
c1ad348b 1549 unsigned long dj;
aa9b1630 1550
b04db8e1 1551 lockdep_assert_irqs_disabled();
3382adbc 1552
aa9b1630 1553 /* If no callbacks, RCU doesn't need the CPU. */
260e1e4f 1554 if (rcu_segcblist_empty(&rdp->cblist)) {
c1ad348b 1555 *nextevt = KTIME_MAX;
aa9b1630
PM
1556 return 0;
1557 }
c0f4dfd4
PM
1558
1559 /* Attempt to advance callbacks. */
1560 if (rcu_try_advance_all_cbs()) {
1561 /* Some ready to invoke, so initiate later invocation. */
1562 invoke_rcu_core();
aa9b1630
PM
1563 return 1;
1564 }
5998a75a 1565 rdp->last_accelerate = jiffies;
c0f4dfd4
PM
1566
1567 /* Request timer delay depending on laziness, and round. */
260e1e4f
PM
1568 rdp->all_lazy = !rcu_segcblist_n_nonlazy_cbs(&rdp->cblist);
1569 if (rdp->all_lazy) {
1570 dj = round_jiffies(rcu_idle_lazy_gp_delay + jiffies) - jiffies;
1571 } else {
c1ad348b 1572 dj = round_up(rcu_idle_gp_delay + jiffies,
c0f4dfd4 1573 rcu_idle_gp_delay) - jiffies;
e84c48ae 1574 }
c1ad348b 1575 *nextevt = basemono + dj * TICK_NSEC;
aa9b1630
PM
1576 return 0;
1577}
1578
21e52e15 1579/*
c0f4dfd4
PM
1580 * Prepare a CPU for idle from an RCU perspective. The first major task
1581 * is to sense whether nohz mode has been enabled or disabled via sysfs.
1582 * The second major task is to check to see if a non-lazy callback has
1583 * arrived at a CPU that previously had only lazy callbacks. The third
1584 * major task is to accelerate (that is, assign grace-period numbers to)
1585 * any recently arrived callbacks.
aea1b35e
PM
1586 *
1587 * The caller must have disabled interrupts.
8bd93a2c 1588 */
198bbf81 1589static void rcu_prepare_for_idle(void)
8bd93a2c 1590{
48a7639c 1591 bool needwake;
0fd79e75 1592 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
c0f4dfd4 1593 struct rcu_node *rnp;
9d2ad243
PM
1594 int tne;
1595
b04db8e1 1596 lockdep_assert_irqs_disabled();
44c65ff2 1597 if (rcu_is_nocb_cpu(smp_processor_id()))
3382adbc
PM
1598 return;
1599
9d2ad243 1600 /* Handle nohz enablement switches conservatively. */
7d0ae808 1601 tne = READ_ONCE(tick_nohz_active);
0fd79e75 1602 if (tne != rdp->tick_nohz_enabled_snap) {
260e1e4f 1603 if (!rcu_segcblist_empty(&rdp->cblist))
9d2ad243 1604 invoke_rcu_core(); /* force nohz to see update. */
0fd79e75 1605 rdp->tick_nohz_enabled_snap = tne;
9d2ad243
PM
1606 return;
1607 }
1608 if (!tne)
1609 return;
f511fc62 1610
c57afe80 1611 /*
c0f4dfd4
PM
1612 * If a non-lazy callback arrived at a CPU having only lazy
1613 * callbacks, invoke RCU core for the side-effect of recalculating
1614 * idle duration on re-entry to idle.
c57afe80 1615 */
260e1e4f 1616 if (rdp->all_lazy && rcu_segcblist_n_nonlazy_cbs(&rdp->cblist)) {
c458a89e 1617 rdp->all_lazy = false;
c0f4dfd4 1618 invoke_rcu_core();
c57afe80
PM
1619 return;
1620 }
c57afe80 1621
3084f2f8 1622 /*
c0f4dfd4
PM
1623 * If we have not yet accelerated this jiffy, accelerate all
1624 * callbacks on this CPU.
3084f2f8 1625 */
5998a75a 1626 if (rdp->last_accelerate == jiffies)
aea1b35e 1627 return;
5998a75a 1628 rdp->last_accelerate = jiffies;
b97d23c5 1629 if (rcu_segcblist_pend_cbs(&rdp->cblist)) {
c0f4dfd4 1630 rnp = rdp->mynode;
2a67e741 1631 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
02f50142 1632 needwake = rcu_accelerate_cbs(rnp, rdp);
67c583a7 1633 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
48a7639c 1634 if (needwake)
532c00c9 1635 rcu_gp_kthread_wake();
77e38ed3 1636 }
c0f4dfd4 1637}
3084f2f8 1638
c0f4dfd4
PM
1639/*
1640 * Clean up for exit from idle. Attempt to advance callbacks based on
1641 * any grace periods that elapsed while the CPU was idle, and if any
1642 * callbacks are now ready to invoke, initiate invocation.
1643 */
8fa7845d 1644static void rcu_cleanup_after_idle(void)
c0f4dfd4 1645{
b04db8e1 1646 lockdep_assert_irqs_disabled();
44c65ff2 1647 if (rcu_is_nocb_cpu(smp_processor_id()))
aea1b35e 1648 return;
7a497c96
PM
1649 if (rcu_try_advance_all_cbs())
1650 invoke_rcu_core();
8bd93a2c
PM
1651}
1652
1653#endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
a858af28 1654
a858af28
PM
1655#ifdef CONFIG_RCU_FAST_NO_HZ
1656
1657static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
1658{
0fd79e75 1659 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
a858af28 1660
260e1e4f 1661 sprintf(cp, "last_accelerate: %04lx/%04lx, Nonlazy posted: %c%c%c",
5998a75a 1662 rdp->last_accelerate & 0xffff, jiffies & 0xffff,
260e1e4f
PM
1663 ".l"[rdp->all_lazy],
1664 ".L"[!rcu_segcblist_n_nonlazy_cbs(&rdp->cblist)],
1665 ".D"[!rdp->tick_nohz_enabled_snap]);
a858af28
PM
1666}
1667
1668#else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
1669
1670static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
1671{
1c17e4d4 1672 *cp = '\0';
a858af28
PM
1673}
1674
1675#endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
1676
1677/* Initiate the stall-info list. */
1678static void print_cpu_stall_info_begin(void)
1679{
efc151c3 1680 pr_cont("\n");
a858af28
PM
1681}
1682
1683/*
1684 * Print out diagnostic information for the specified stalled CPU.
1685 *
564a9ae6
PM
1686 * If the specified CPU is aware of the current RCU grace period, then
1687 * print the number of scheduling clock interrupts the CPU has taken
1688 * during the time that it has been aware. Otherwise, print the number
1689 * of RCU grace periods that this CPU is ignorant of, for example, "1"
1690 * if the CPU was aware of the previous grace period.
a858af28
PM
1691 *
1692 * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
1693 */
b21ebed9 1694static void print_cpu_stall_info(int cpu)
a858af28 1695{
9b9500da 1696 unsigned long delta;
a858af28 1697 char fast_no_hz[72];
da1df50d 1698 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
a858af28
PM
1699 char *ticks_title;
1700 unsigned long ticks_value;
1701
3caa973b
TH
1702 /*
1703 * We could be printing a lot while holding a spinlock. Avoid
1704 * triggering hard lockup.
1705 */
1706 touch_nmi_watchdog();
1707
b21ebed9 1708 ticks_value = rcu_seq_ctr(rcu_state.gp_seq - rdp->gp_seq);
471f87c3
PM
1709 if (ticks_value) {
1710 ticks_title = "GPs behind";
1711 } else {
a858af28
PM
1712 ticks_title = "ticks this GP";
1713 ticks_value = rdp->ticks_this_gp;
a858af28
PM
1714 }
1715 print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
8aa670cd 1716 delta = rcu_seq_ctr(rdp->mynode->gp_seq - rdp->rcu_iw_gp_seq);
89b4cd4b 1717 pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%03x/%ld/%#lx softirq=%u/%u fqs=%ld %s\n",
7f21aeef
PM
1718 cpu,
1719 "O."[!!cpu_online(cpu)],
1720 "o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
1721 "N."[!!(rdp->grpmask & rdp->mynode->qsmaskinitnext)],
9b9500da
PM
1722 !IS_ENABLED(CONFIG_IRQ_WORK) ? '?' :
1723 rdp->rcu_iw_pending ? (int)min(delta, 9UL) + '0' :
1724 "!."[!delta],
7f21aeef 1725 ticks_value, ticks_title,
dc5a4f29 1726 rcu_dynticks_snap(rdp) & 0xfff,
4c5273bf 1727 rdp->dynticks_nesting, rdp->dynticks_nmi_nesting,
6231069b 1728 rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
b21ebed9 1729 READ_ONCE(rcu_state.n_force_qs) - rcu_state.n_force_qs_gpstart,
a858af28
PM
1730 fast_no_hz);
1731}
1732
1733/* Terminate the stall-info list. */
1734static void print_cpu_stall_info_end(void)
1735{
efc151c3 1736 pr_err("\t");
a858af28
PM
1737}
1738
0ae86a27 1739/* Zero ->ticks_this_gp and snapshot the number of RCU softirq handlers. */
a858af28
PM
1740static void zero_cpu_stall_ticks(struct rcu_data *rdp)
1741{
1742 rdp->ticks_this_gp = 0;
6231069b 1743 rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
d3052109 1744 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
a858af28
PM
1745}
1746
3fbfbf7a
PM
1747#ifdef CONFIG_RCU_NOCB_CPU
1748
1749/*
1750 * Offload callback processing from the boot-time-specified set of CPUs
a9fefdb2
PM
1751 * specified by rcu_nocb_mask. For the CPUs in the set, there are kthreads
1752 * created that pull the callbacks from the corresponding CPU, wait for
1753 * a grace period to elapse, and invoke the callbacks. These kthreads
1754 * are organized into leaders, which manage incoming callbacks, wait for
1755 * grace periods, and awaken followers, and the followers, which only
1756 * invoke callbacks. Each leader is its own follower. The no-CBs CPUs
1757 * do a wake_up() on their kthread when they insert a callback into any
1758 * empty list, unless the rcu_nocb_poll boot parameter has been specified,
1759 * in which case each kthread actively polls its CPU. (Which isn't so great
1760 * for energy efficiency, but which does reduce RCU's overhead on that CPU.)
3fbfbf7a
PM
1761 *
1762 * This is intended to be used in conjunction with Frederic Weisbecker's
1763 * adaptive-idle work, which would seriously reduce OS jitter on CPUs
1764 * running CPU-bound user-mode computations.
1765 *
a9fefdb2
PM
1766 * Offloading of callbacks can also be used as an energy-efficiency
1767 * measure because CPUs with no RCU callbacks queued are more aggressive
1768 * about entering dyntick-idle mode.
3fbfbf7a
PM
1769 */
1770
1771
497e4260
PM
1772/*
1773 * Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters.
1774 * The string after the "rcu_nocbs=" is either "all" for all CPUs, or a
1775 * comma-separated list of CPUs and/or CPU ranges. If an invalid list is
1776 * given, a warning is emitted and all CPUs are offloaded.
1777 */
3fbfbf7a
PM
1778static int __init rcu_nocb_setup(char *str)
1779{
1780 alloc_bootmem_cpumask_var(&rcu_nocb_mask);
da8739f2
PM
1781 if (!strcasecmp(str, "all"))
1782 cpumask_setall(rcu_nocb_mask);
1783 else
497e4260
PM
1784 if (cpulist_parse(str, rcu_nocb_mask)) {
1785 pr_warn("rcu_nocbs= bad CPU range, all CPUs set\n");
1786 cpumask_setall(rcu_nocb_mask);
1787 }
3fbfbf7a
PM
1788 return 1;
1789}
1790__setup("rcu_nocbs=", rcu_nocb_setup);
1791
1b0048a4
PG
1792static int __init parse_rcu_nocb_poll(char *arg)
1793{
5455a7f6 1794 rcu_nocb_poll = true;
1b0048a4
PG
1795 return 0;
1796}
1797early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
1798
dae6e64d 1799/*
0446be48
PM
1800 * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
1801 * grace period.
dae6e64d 1802 */
abedf8e2 1803static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
dae6e64d 1804{
abedf8e2 1805 swake_up_all(sq);
dae6e64d
PM
1806}
1807
abedf8e2 1808static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
065bb78c 1809{
e0da2374 1810 return &rnp->nocb_gp_wq[rcu_seq_ctr(rnp->gp_seq) & 0x1];
065bb78c
DW
1811}
1812
dae6e64d 1813static void rcu_init_one_nocb(struct rcu_node *rnp)
34ed6246 1814{
abedf8e2
PG
1815 init_swait_queue_head(&rnp->nocb_gp_wq[0]);
1816 init_swait_queue_head(&rnp->nocb_gp_wq[1]);
34ed6246
PM
1817}
1818
24342c96 1819/* Is the specified CPU a no-CBs CPU? */
d1e43fa5 1820bool rcu_is_nocb_cpu(int cpu)
3fbfbf7a 1821{
84b12b75 1822 if (cpumask_available(rcu_nocb_mask))
3fbfbf7a
PM
1823 return cpumask_test_cpu(cpu, rcu_nocb_mask);
1824 return false;
1825}
1826
fbce7497 1827/*
8be6e1b1
PM
1828 * Kick the leader kthread for this NOCB group. Caller holds ->nocb_lock
1829 * and this function releases it.
fbce7497 1830 */
8be6e1b1
PM
1831static void __wake_nocb_leader(struct rcu_data *rdp, bool force,
1832 unsigned long flags)
1833 __releases(rdp->nocb_lock)
fbce7497
PM
1834{
1835 struct rcu_data *rdp_leader = rdp->nocb_leader;
1836
8be6e1b1
PM
1837 lockdep_assert_held(&rdp->nocb_lock);
1838 if (!READ_ONCE(rdp_leader->nocb_kthread)) {
1839 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
fbce7497 1840 return;
8be6e1b1
PM
1841 }
1842 if (rdp_leader->nocb_leader_sleep || force) {
39953dfd 1843 /* Prior smp_mb__after_atomic() orders against prior enqueue. */
7d0ae808 1844 WRITE_ONCE(rdp_leader->nocb_leader_sleep, false);
8be6e1b1
PM
1845 del_timer(&rdp->nocb_timer);
1846 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
b3dae109
PZ
1847 smp_mb(); /* ->nocb_leader_sleep before swake_up_one(). */
1848 swake_up_one(&rdp_leader->nocb_wq);
8be6e1b1
PM
1849 } else {
1850 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
fbce7497
PM
1851 }
1852}
1853
8be6e1b1
PM
1854/*
1855 * Kick the leader kthread for this NOCB group, but caller has not
1856 * acquired locks.
1857 */
1858static void wake_nocb_leader(struct rcu_data *rdp, bool force)
1859{
1860 unsigned long flags;
1861
1862 raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
1863 __wake_nocb_leader(rdp, force, flags);
1864}
1865
1866/*
1867 * Arrange to wake the leader kthread for this NOCB group at some
1868 * future time when it is safe to do so.
1869 */
1870static void wake_nocb_leader_defer(struct rcu_data *rdp, int waketype,
1871 const char *reason)
1872{
1873 unsigned long flags;
1874
1875 raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
1876 if (rdp->nocb_defer_wakeup == RCU_NOCB_WAKE_NOT)
1877 mod_timer(&rdp->nocb_timer, jiffies + 1);
1878 WRITE_ONCE(rdp->nocb_defer_wakeup, waketype);
88d1bead 1879 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, reason);
8be6e1b1
PM
1880 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
1881}
1882
a9fefdb2 1883/* Does rcu_barrier need to queue an RCU callback on the specified CPU? */
4580b054 1884static bool rcu_nocb_cpu_needs_barrier(int cpu)
d7e29933 1885{
da1df50d 1886 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
41050a00
PM
1887 unsigned long ret;
1888#ifdef CONFIG_PROVE_RCU
d7e29933 1889 struct rcu_head *rhp;
41050a00 1890#endif /* #ifdef CONFIG_PROVE_RCU */
d7e29933 1891
41050a00
PM
1892 /*
1893 * Check count of all no-CBs callbacks awaiting invocation.
1894 * There needs to be a barrier before this function is called,
1895 * but associated with a prior determination that no more
1896 * callbacks would be posted. In the worst case, the first
dd46a788 1897 * barrier in rcu_barrier() suffices (but the caller cannot
41050a00 1898 * necessarily rely on this, not a substitute for the caller
a9fefdb2
PM
1899 * getting the concurrency design right!). There must also be a
1900 * barrier between the following load and posting of a callback
41050a00
PM
1901 * (if a callback is in fact needed). This is associated with an
1902 * atomic_inc() in the caller.
1903 */
903ee83d 1904 ret = rcu_get_n_cbs_nocb_cpu(rdp);
d7e29933 1905
41050a00 1906#ifdef CONFIG_PROVE_RCU
7d0ae808 1907 rhp = READ_ONCE(rdp->nocb_head);
d7e29933 1908 if (!rhp)
7d0ae808 1909 rhp = READ_ONCE(rdp->nocb_gp_head);
d7e29933 1910 if (!rhp)
7d0ae808 1911 rhp = READ_ONCE(rdp->nocb_follower_head);
d7e29933
PM
1912
1913 /* Having no rcuo kthread but CBs after scheduler starts is bad! */
7d0ae808 1914 if (!READ_ONCE(rdp->nocb_kthread) && rhp &&
59f792d1 1915 rcu_scheduler_fully_active) {
d7e29933
PM
1916 /* RCU callback enqueued before CPU first came online??? */
1917 pr_err("RCU: Never-onlined no-CBs CPU %d has CB %p\n",
1918 cpu, rhp->func);
1919 WARN_ON_ONCE(1);
1920 }
41050a00 1921#endif /* #ifdef CONFIG_PROVE_RCU */
d7e29933 1922
41050a00 1923 return !!ret;
d7e29933
PM
1924}
1925
3fbfbf7a
PM
1926/*
1927 * Enqueue the specified string of rcu_head structures onto the specified
1928 * CPU's no-CBs lists. The CPU is specified by rdp, the head of the
1929 * string by rhp, and the tail of the string by rhtp. The non-lazy/lazy
1930 * counts are supplied by rhcount and rhcount_lazy.
1931 *
1932 * If warranted, also wake up the kthread servicing this CPUs queues.
1933 */
1934static void __call_rcu_nocb_enqueue(struct rcu_data *rdp,
1935 struct rcu_head *rhp,
1936 struct rcu_head **rhtp,
96d3fd0d
PM
1937 int rhcount, int rhcount_lazy,
1938 unsigned long flags)
3fbfbf7a
PM
1939{
1940 int len;
1941 struct rcu_head **old_rhpp;
1942 struct task_struct *t;
1943
1944 /* Enqueue the callback on the nocb list and update counts. */
41050a00
PM
1945 atomic_long_add(rhcount, &rdp->nocb_q_count);
1946 /* rcu_barrier() relies on ->nocb_q_count add before xchg. */
3fbfbf7a 1947 old_rhpp = xchg(&rdp->nocb_tail, rhtp);
7d0ae808 1948 WRITE_ONCE(*old_rhpp, rhp);
3fbfbf7a 1949 atomic_long_add(rhcount_lazy, &rdp->nocb_q_count_lazy);
39953dfd 1950 smp_mb__after_atomic(); /* Store *old_rhpp before _wake test. */
3fbfbf7a
PM
1951
1952 /* If we are not being polled and there is a kthread, awaken it ... */
7d0ae808 1953 t = READ_ONCE(rdp->nocb_kthread);
25e03a74 1954 if (rcu_nocb_poll || !t) {
88d1bead 1955 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
9261dd0d 1956 TPS("WakeNotPoll"));
3fbfbf7a 1957 return;
9261dd0d 1958 }
903ee83d 1959 len = rcu_get_n_cbs_nocb_cpu(rdp);
3fbfbf7a 1960 if (old_rhpp == &rdp->nocb_head) {
96d3fd0d 1961 if (!irqs_disabled_flags(flags)) {
fbce7497
PM
1962 /* ... if queue was empty ... */
1963 wake_nocb_leader(rdp, false);
88d1bead 1964 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
96d3fd0d
PM
1965 TPS("WakeEmpty"));
1966 } else {
8be6e1b1
PM
1967 wake_nocb_leader_defer(rdp, RCU_NOCB_WAKE,
1968 TPS("WakeEmptyIsDeferred"));
96d3fd0d 1969 }
3fbfbf7a
PM
1970 rdp->qlen_last_fqs_check = 0;
1971 } else if (len > rdp->qlen_last_fqs_check + qhimark) {
fbce7497 1972 /* ... or if many callbacks queued. */
9fdd3bc9
PM
1973 if (!irqs_disabled_flags(flags)) {
1974 wake_nocb_leader(rdp, true);
88d1bead 1975 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
9fdd3bc9
PM
1976 TPS("WakeOvf"));
1977 } else {
efcd2d54 1978 wake_nocb_leader_defer(rdp, RCU_NOCB_WAKE_FORCE,
8be6e1b1 1979 TPS("WakeOvfIsDeferred"));
9fdd3bc9 1980 }
3fbfbf7a 1981 rdp->qlen_last_fqs_check = LONG_MAX / 2;
9261dd0d 1982 } else {
88d1bead 1983 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
3fbfbf7a
PM
1984 }
1985 return;
1986}
1987
1988/*
1989 * This is a helper for __call_rcu(), which invokes this when the normal
1990 * callback queue is inoperable. If this is not a no-CBs CPU, this
1991 * function returns failure back to __call_rcu(), which can complain
1992 * appropriately.
1993 *
1994 * Otherwise, this function queues the callback where the corresponding
1995 * "rcuo" kthread can find it.
1996 */
1997static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
96d3fd0d 1998 bool lazy, unsigned long flags)
3fbfbf7a
PM
1999{
2000
d1e43fa5 2001 if (!rcu_is_nocb_cpu(rdp->cpu))
c271d3a9 2002 return false;
96d3fd0d 2003 __call_rcu_nocb_enqueue(rdp, rhp, &rhp->next, 1, lazy, flags);
21e7a608 2004 if (__is_kfree_rcu_offset((unsigned long)rhp->func))
88d1bead 2005 trace_rcu_kfree_callback(rcu_state.name, rhp,
21e7a608 2006 (unsigned long)rhp->func,
756cbf6b 2007 -atomic_long_read(&rdp->nocb_q_count_lazy),
903ee83d 2008 -rcu_get_n_cbs_nocb_cpu(rdp));
21e7a608 2009 else
88d1bead 2010 trace_rcu_callback(rcu_state.name, rhp,
756cbf6b 2011 -atomic_long_read(&rdp->nocb_q_count_lazy),
903ee83d 2012 -rcu_get_n_cbs_nocb_cpu(rdp));
1772947b
PM
2013
2014 /*
2015 * If called from an extended quiescent state with interrupts
2016 * disabled, invoke the RCU core in order to allow the idle-entry
2017 * deferred-wakeup check to function.
2018 */
2019 if (irqs_disabled_flags(flags) &&
2020 !rcu_is_watching() &&
2021 cpu_online(smp_processor_id()))
2022 invoke_rcu_core();
2023
c271d3a9 2024 return true;
3fbfbf7a
PM
2025}
2026
2027/*
2028 * Adopt orphaned callbacks on a no-CBs CPU, or return 0 if this is
2029 * not a no-CBs CPU.
2030 */
b1a2d79f 2031static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp,
96d3fd0d
PM
2032 struct rcu_data *rdp,
2033 unsigned long flags)
3fbfbf7a 2034{
b04db8e1 2035 lockdep_assert_irqs_disabled();
d1e43fa5 2036 if (!rcu_is_nocb_cpu(smp_processor_id()))
b1a2d79f
PM
2037 return false; /* Not NOCBs CPU, caller must migrate CBs. */
2038 __call_rcu_nocb_enqueue(my_rdp, rcu_segcblist_head(&rdp->cblist),
2039 rcu_segcblist_tail(&rdp->cblist),
2040 rcu_segcblist_n_cbs(&rdp->cblist),
2041 rcu_segcblist_n_lazy_cbs(&rdp->cblist), flags);
2042 rcu_segcblist_init(&rdp->cblist);
2043 rcu_segcblist_disable(&rdp->cblist);
0a9e1e11 2044 return true;
3fbfbf7a
PM
2045}
2046
2047/*
34ed6246
PM
2048 * If necessary, kick off a new grace period, and either way wait
2049 * for a subsequent grace period to complete.
3fbfbf7a 2050 */
34ed6246 2051static void rcu_nocb_wait_gp(struct rcu_data *rdp)
3fbfbf7a 2052{
34ed6246 2053 unsigned long c;
dae6e64d 2054 bool d;
34ed6246 2055 unsigned long flags;
48a7639c 2056 bool needwake;
34ed6246
PM
2057 struct rcu_node *rnp = rdp->mynode;
2058
ab5e869c 2059 local_irq_save(flags);
88d1bead 2060 c = rcu_seq_snap(&rcu_state.gp_seq);
ab5e869c
PM
2061 if (!rdp->gpwrap && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
2062 local_irq_restore(flags);
2063 } else {
2064 raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
2065 needwake = rcu_start_this_gp(rnp, rdp, c);
2066 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2067 if (needwake)
532c00c9 2068 rcu_gp_kthread_wake();
ab5e869c 2069 }
3fbfbf7a
PM
2070
2071 /*
34ed6246
PM
2072 * Wait for the grace period. Do so interruptibly to avoid messing
2073 * up the load average.
3fbfbf7a 2074 */
41e80595 2075 trace_rcu_this_gp(rnp, rdp, c, TPS("StartWait"));
34ed6246 2076 for (;;) {
b3dae109 2077 swait_event_interruptible_exclusive(
29365e56
PM
2078 rnp->nocb_gp_wq[rcu_seq_ctr(c) & 0x1],
2079 (d = rcu_seq_done(&rnp->gp_seq, c)));
dae6e64d 2080 if (likely(d))
34ed6246 2081 break;
73a860cd 2082 WARN_ON(signal_pending(current));
41e80595 2083 trace_rcu_this_gp(rnp, rdp, c, TPS("ResumeWait"));
34ed6246 2084 }
41e80595 2085 trace_rcu_this_gp(rnp, rdp, c, TPS("EndWait"));
34ed6246 2086 smp_mb(); /* Ensure that CB invocation happens after GP end. */
3fbfbf7a
PM
2087}
2088
fbce7497
PM
2089/*
2090 * Leaders come here to wait for additional callbacks to show up.
2091 * This function does not return until callbacks appear.
2092 */
2093static void nocb_leader_wait(struct rcu_data *my_rdp)
2094{
2095 bool firsttime = true;
8be6e1b1 2096 unsigned long flags;
fbce7497
PM
2097 bool gotcbs;
2098 struct rcu_data *rdp;
2099 struct rcu_head **tail;
2100
2101wait_again:
2102
2103 /* Wait for callbacks to appear. */
2104 if (!rcu_nocb_poll) {
88d1bead 2105 trace_rcu_nocb_wake(rcu_state.name, my_rdp->cpu, TPS("Sleep"));
b3dae109 2106 swait_event_interruptible_exclusive(my_rdp->nocb_wq,
7d0ae808 2107 !READ_ONCE(my_rdp->nocb_leader_sleep));
8be6e1b1
PM
2108 raw_spin_lock_irqsave(&my_rdp->nocb_lock, flags);
2109 my_rdp->nocb_leader_sleep = true;
2110 WRITE_ONCE(my_rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
2111 del_timer(&my_rdp->nocb_timer);
2112 raw_spin_unlock_irqrestore(&my_rdp->nocb_lock, flags);
fbce7497
PM
2113 } else if (firsttime) {
2114 firsttime = false; /* Don't drown trace log with "Poll"! */
88d1bead 2115 trace_rcu_nocb_wake(rcu_state.name, my_rdp->cpu, TPS("Poll"));
fbce7497
PM
2116 }
2117
2118 /*
2119 * Each pass through the following loop checks a follower for CBs.
2120 * We are our own first follower. Any CBs found are moved to
2121 * nocb_gp_head, where they await a grace period.
2122 */
2123 gotcbs = false;
8be6e1b1 2124 smp_mb(); /* wakeup and _sleep before ->nocb_head reads. */
fbce7497 2125 for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
7d0ae808 2126 rdp->nocb_gp_head = READ_ONCE(rdp->nocb_head);
fbce7497
PM
2127 if (!rdp->nocb_gp_head)
2128 continue; /* No CBs here, try next follower. */
2129
2130 /* Move callbacks to wait-for-GP list, which is empty. */
7d0ae808 2131 WRITE_ONCE(rdp->nocb_head, NULL);
fbce7497 2132 rdp->nocb_gp_tail = xchg(&rdp->nocb_tail, &rdp->nocb_head);
fbce7497
PM
2133 gotcbs = true;
2134 }
2135
8be6e1b1 2136 /* No callbacks? Sleep a bit if polling, and go retry. */
fbce7497 2137 if (unlikely(!gotcbs)) {
73a860cd 2138 WARN_ON(signal_pending(current));
8be6e1b1
PM
2139 if (rcu_nocb_poll) {
2140 schedule_timeout_interruptible(1);
2141 } else {
88d1bead 2142 trace_rcu_nocb_wake(rcu_state.name, my_rdp->cpu,
bedbb648 2143 TPS("WokeEmpty"));
8be6e1b1 2144 }
fbce7497
PM
2145 goto wait_again;
2146 }
2147
2148 /* Wait for one grace period. */
2149 rcu_nocb_wait_gp(my_rdp);
2150
fbce7497
PM
2151 /* Each pass through the following loop wakes a follower, if needed. */
2152 for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
8be6e1b1
PM
2153 if (!rcu_nocb_poll &&
2154 READ_ONCE(rdp->nocb_head) &&
2155 READ_ONCE(my_rdp->nocb_leader_sleep)) {
2156 raw_spin_lock_irqsave(&my_rdp->nocb_lock, flags);
11ed7f93 2157 my_rdp->nocb_leader_sleep = false;/* No need to sleep.*/
8be6e1b1
PM
2158 raw_spin_unlock_irqrestore(&my_rdp->nocb_lock, flags);
2159 }
fbce7497
PM
2160 if (!rdp->nocb_gp_head)
2161 continue; /* No CBs, so no need to wake follower. */
2162
2163 /* Append callbacks to follower's "done" list. */
8be6e1b1
PM
2164 raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
2165 tail = rdp->nocb_follower_tail;
2166 rdp->nocb_follower_tail = rdp->nocb_gp_tail;
fbce7497 2167 *tail = rdp->nocb_gp_head;
8be6e1b1 2168 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
fbce7497 2169 if (rdp != my_rdp && tail == &rdp->nocb_follower_head) {
8be6e1b1 2170 /* List was empty, so wake up the follower. */
b3dae109 2171 swake_up_one(&rdp->nocb_wq);
fbce7497
PM
2172 }
2173 }
2174
2175 /* If we (the leader) don't have CBs, go wait some more. */
2176 if (!my_rdp->nocb_follower_head)
2177 goto wait_again;
2178}
2179
2180/*
2181 * Followers come here to wait for additional callbacks to show up.
2182 * This function does not return until callbacks appear.
2183 */
2184static void nocb_follower_wait(struct rcu_data *rdp)
2185{
fbce7497 2186 for (;;) {
88d1bead 2187 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("FollowerSleep"));
b3dae109 2188 swait_event_interruptible_exclusive(rdp->nocb_wq,
8be6e1b1 2189 READ_ONCE(rdp->nocb_follower_head));
fbce7497
PM
2190 if (smp_load_acquire(&rdp->nocb_follower_head)) {
2191 /* ^^^ Ensure CB invocation follows _head test. */
2192 return;
2193 }
73a860cd 2194 WARN_ON(signal_pending(current));
88d1bead 2195 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeEmpty"));
fbce7497
PM
2196 }
2197}
2198
3fbfbf7a
PM
2199/*
2200 * Per-rcu_data kthread, but only for no-CBs CPUs. Each kthread invokes
fbce7497
PM
2201 * callbacks queued by the corresponding no-CBs CPU, however, there is
2202 * an optional leader-follower relationship so that the grace-period
2203 * kthreads don't have to do quite so many wakeups.
3fbfbf7a
PM
2204 */
2205static int rcu_nocb_kthread(void *arg)
2206{
2207 int c, cl;
8be6e1b1 2208 unsigned long flags;
3fbfbf7a
PM
2209 struct rcu_head *list;
2210 struct rcu_head *next;
2211 struct rcu_head **tail;
2212 struct rcu_data *rdp = arg;
2213
2214 /* Each pass through this loop invokes one batch of callbacks */
2215 for (;;) {
fbce7497
PM
2216 /* Wait for callbacks. */
2217 if (rdp->nocb_leader == rdp)
2218 nocb_leader_wait(rdp);
2219 else
2220 nocb_follower_wait(rdp);
2221
2222 /* Pull the ready-to-invoke callbacks onto local list. */
8be6e1b1
PM
2223 raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
2224 list = rdp->nocb_follower_head;
2225 rdp->nocb_follower_head = NULL;
2226 tail = rdp->nocb_follower_tail;
2227 rdp->nocb_follower_tail = &rdp->nocb_follower_head;
2228 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
9213784b
PM
2229 if (WARN_ON_ONCE(!list))
2230 continue;
88d1bead 2231 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeNonEmpty"));
3fbfbf7a
PM
2232
2233 /* Each pass through the following loop invokes a callback. */
88d1bead 2234 trace_rcu_batch_start(rcu_state.name,
41050a00 2235 atomic_long_read(&rdp->nocb_q_count_lazy),
903ee83d 2236 rcu_get_n_cbs_nocb_cpu(rdp), -1);
3fbfbf7a
PM
2237 c = cl = 0;
2238 while (list) {
2239 next = list->next;
2240 /* Wait for enqueuing to complete, if needed. */
2241 while (next == NULL && &list->next != tail) {
88d1bead 2242 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
69a79bb1 2243 TPS("WaitQueue"));
3fbfbf7a 2244 schedule_timeout_interruptible(1);
88d1bead 2245 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
69a79bb1 2246 TPS("WokeQueue"));
3fbfbf7a
PM
2247 next = list->next;
2248 }
2249 debug_rcu_head_unqueue(list);
2250 local_bh_disable();
88d1bead 2251 if (__rcu_reclaim(rcu_state.name, list))
3fbfbf7a
PM
2252 cl++;
2253 c++;
2254 local_bh_enable();
cee43939 2255 cond_resched_tasks_rcu_qs();
3fbfbf7a
PM
2256 list = next;
2257 }
88d1bead 2258 trace_rcu_batch_end(rcu_state.name, c, !!list, 0, 0, 1);
41050a00
PM
2259 smp_mb__before_atomic(); /* _add after CB invocation. */
2260 atomic_long_add(-c, &rdp->nocb_q_count);
2261 atomic_long_add(-cl, &rdp->nocb_q_count_lazy);
3fbfbf7a
PM
2262 }
2263 return 0;
2264}
2265
96d3fd0d 2266/* Is a deferred wakeup of rcu_nocb_kthread() required? */
9fdd3bc9 2267static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
96d3fd0d 2268{
7d0ae808 2269 return READ_ONCE(rdp->nocb_defer_wakeup);
96d3fd0d
PM
2270}
2271
2272/* Do a deferred wakeup of rcu_nocb_kthread(). */
8be6e1b1 2273static void do_nocb_deferred_wakeup_common(struct rcu_data *rdp)
96d3fd0d 2274{
8be6e1b1 2275 unsigned long flags;
9fdd3bc9
PM
2276 int ndw;
2277
8be6e1b1
PM
2278 raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
2279 if (!rcu_nocb_need_deferred_wakeup(rdp)) {
2280 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
96d3fd0d 2281 return;
8be6e1b1 2282 }
7d0ae808 2283 ndw = READ_ONCE(rdp->nocb_defer_wakeup);
511324e4 2284 WRITE_ONCE(rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
8be6e1b1 2285 __wake_nocb_leader(rdp, ndw == RCU_NOCB_WAKE_FORCE, flags);
88d1bead 2286 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DeferredWake"));
96d3fd0d
PM
2287}
2288
8be6e1b1 2289/* Do a deferred wakeup of rcu_nocb_kthread() from a timer handler. */
fd30b717 2290static void do_nocb_deferred_wakeup_timer(struct timer_list *t)
8be6e1b1 2291{
fd30b717
KC
2292 struct rcu_data *rdp = from_timer(rdp, t, nocb_timer);
2293
2294 do_nocb_deferred_wakeup_common(rdp);
8be6e1b1
PM
2295}
2296
2297/*
2298 * Do a deferred wakeup of rcu_nocb_kthread() from fastpath.
2299 * This means we do an inexact common-case check. Note that if
2300 * we miss, ->nocb_timer will eventually clean things up.
2301 */
2302static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
2303{
2304 if (rcu_nocb_need_deferred_wakeup(rdp))
2305 do_nocb_deferred_wakeup_common(rdp);
2306}
2307
f4579fc5
PM
2308void __init rcu_init_nohz(void)
2309{
2310 int cpu;
ef126206 2311 bool need_rcu_nocb_mask = false;
f4579fc5 2312
f4579fc5
PM
2313#if defined(CONFIG_NO_HZ_FULL)
2314 if (tick_nohz_full_running && cpumask_weight(tick_nohz_full_mask))
2315 need_rcu_nocb_mask = true;
2316#endif /* #if defined(CONFIG_NO_HZ_FULL) */
2317
84b12b75 2318 if (!cpumask_available(rcu_nocb_mask) && need_rcu_nocb_mask) {
949cccdb
PK
2319 if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
2320 pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
2321 return;
2322 }
f4579fc5 2323 }
84b12b75 2324 if (!cpumask_available(rcu_nocb_mask))
f4579fc5
PM
2325 return;
2326
f4579fc5
PM
2327#if defined(CONFIG_NO_HZ_FULL)
2328 if (tick_nohz_full_running)
2329 cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
2330#endif /* #if defined(CONFIG_NO_HZ_FULL) */
2331
2332 if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
ef126206 2333 pr_info("\tNote: kernel parameter 'rcu_nocbs=', 'nohz_full', or 'isolcpus=' contains nonexistent CPUs.\n");
f4579fc5
PM
2334 cpumask_and(rcu_nocb_mask, cpu_possible_mask,
2335 rcu_nocb_mask);
2336 }
3016611e
PM
2337 if (cpumask_empty(rcu_nocb_mask))
2338 pr_info("\tOffload RCU callbacks from CPUs: (none).\n");
2339 else
2340 pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
2341 cpumask_pr_args(rcu_nocb_mask));
f4579fc5
PM
2342 if (rcu_nocb_poll)
2343 pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
2344
b97d23c5
PM
2345 for_each_cpu(cpu, rcu_nocb_mask)
2346 init_nocb_callback_list(per_cpu_ptr(&rcu_data, cpu));
2347 rcu_organize_nocb_kthreads();
96d3fd0d
PM
2348}
2349
3fbfbf7a
PM
2350/* Initialize per-rcu_data variables for no-CBs CPUs. */
2351static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
2352{
2353 rdp->nocb_tail = &rdp->nocb_head;
abedf8e2 2354 init_swait_queue_head(&rdp->nocb_wq);
fbce7497 2355 rdp->nocb_follower_tail = &rdp->nocb_follower_head;
8be6e1b1 2356 raw_spin_lock_init(&rdp->nocb_lock);
fd30b717 2357 timer_setup(&rdp->nocb_timer, do_nocb_deferred_wakeup_timer, 0);
3fbfbf7a
PM
2358}
2359
35ce7f29
PM
2360/*
2361 * If the specified CPU is a no-CBs CPU that does not already have its
0ae86a27
PM
2362 * rcuo kthread, spawn it. If the CPUs are brought online out of order,
2363 * this can require re-organizing the leader-follower relationships.
35ce7f29 2364 */
4580b054 2365static void rcu_spawn_one_nocb_kthread(int cpu)
35ce7f29
PM
2366{
2367 struct rcu_data *rdp;
2368 struct rcu_data *rdp_last;
2369 struct rcu_data *rdp_old_leader;
da1df50d 2370 struct rcu_data *rdp_spawn = per_cpu_ptr(&rcu_data, cpu);
35ce7f29
PM
2371 struct task_struct *t;
2372
2373 /*
2374 * If this isn't a no-CBs CPU or if it already has an rcuo kthread,
2375 * then nothing to do.
2376 */
2377 if (!rcu_is_nocb_cpu(cpu) || rdp_spawn->nocb_kthread)
2378 return;
2379
2380 /* If we didn't spawn the leader first, reorganize! */
2381 rdp_old_leader = rdp_spawn->nocb_leader;
2382 if (rdp_old_leader != rdp_spawn && !rdp_old_leader->nocb_kthread) {
2383 rdp_last = NULL;
2384 rdp = rdp_old_leader;
2385 do {
2386 rdp->nocb_leader = rdp_spawn;
2387 if (rdp_last && rdp != rdp_spawn)
2388 rdp_last->nocb_next_follower = rdp;
bbe5d7a9
PM
2389 if (rdp == rdp_spawn) {
2390 rdp = rdp->nocb_next_follower;
2391 } else {
2392 rdp_last = rdp;
2393 rdp = rdp->nocb_next_follower;
2394 rdp_last->nocb_next_follower = NULL;
2395 }
35ce7f29
PM
2396 } while (rdp);
2397 rdp_spawn->nocb_next_follower = rdp_old_leader;
2398 }
2399
0ae86a27 2400 /* Spawn the kthread for this CPU. */
35ce7f29 2401 t = kthread_run(rcu_nocb_kthread, rdp_spawn,
4580b054 2402 "rcuo%c/%d", rcu_state.abbr, cpu);
9213784b
PM
2403 if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo kthread, OOM is now expected behavior\n", __func__))
2404 return;
7d0ae808 2405 WRITE_ONCE(rdp_spawn->nocb_kthread, t);
35ce7f29
PM
2406}
2407
2408/*
2409 * If the specified CPU is a no-CBs CPU that does not already have its
ad368d15 2410 * rcuo kthread, spawn it.
35ce7f29 2411 */
ad368d15 2412static void rcu_spawn_cpu_nocb_kthread(int cpu)
35ce7f29 2413{
35ce7f29 2414 if (rcu_scheduler_fully_active)
b97d23c5 2415 rcu_spawn_one_nocb_kthread(cpu);
35ce7f29
PM
2416}
2417
2418/*
2419 * Once the scheduler is running, spawn rcuo kthreads for all online
2420 * no-CBs CPUs. This assumes that the early_initcall()s happen before
2421 * non-boot CPUs come online -- if this changes, we will need to add
2422 * some mutual exclusion.
2423 */
2424static void __init rcu_spawn_nocb_kthreads(void)
2425{
2426 int cpu;
2427
2428 for_each_online_cpu(cpu)
ad368d15 2429 rcu_spawn_cpu_nocb_kthread(cpu);
35ce7f29
PM
2430}
2431
fbce7497
PM
2432/* How many follower CPU IDs per leader? Default of -1 for sqrt(nr_cpu_ids). */
2433static int rcu_nocb_leader_stride = -1;
2434module_param(rcu_nocb_leader_stride, int, 0444);
2435
2436/*
35ce7f29 2437 * Initialize leader-follower relationships for all no-CBs CPU.
fbce7497 2438 */
4580b054 2439static void __init rcu_organize_nocb_kthreads(void)
3fbfbf7a
PM
2440{
2441 int cpu;
fbce7497
PM
2442 int ls = rcu_nocb_leader_stride;
2443 int nl = 0; /* Next leader. */
3fbfbf7a 2444 struct rcu_data *rdp;
fbce7497
PM
2445 struct rcu_data *rdp_leader = NULL; /* Suppress misguided gcc warn. */
2446 struct rcu_data *rdp_prev = NULL;
3fbfbf7a 2447
84b12b75 2448 if (!cpumask_available(rcu_nocb_mask))
3fbfbf7a 2449 return;
fbce7497
PM
2450 if (ls == -1) {
2451 ls = int_sqrt(nr_cpu_ids);
2452 rcu_nocb_leader_stride = ls;
2453 }
2454
2455 /*
9831ce3b
PM
2456 * Each pass through this loop sets up one rcu_data structure.
2457 * Should the corresponding CPU come online in the future, then
2458 * we will spawn the needed set of rcu_nocb_kthread() kthreads.
fbce7497 2459 */
3fbfbf7a 2460 for_each_cpu(cpu, rcu_nocb_mask) {
da1df50d 2461 rdp = per_cpu_ptr(&rcu_data, cpu);
fbce7497
PM
2462 if (rdp->cpu >= nl) {
2463 /* New leader, set up for followers & next leader. */
2464 nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
2465 rdp->nocb_leader = rdp;
2466 rdp_leader = rdp;
2467 } else {
2468 /* Another follower, link to previous leader. */
2469 rdp->nocb_leader = rdp_leader;
2470 rdp_prev->nocb_next_follower = rdp;
2471 }
2472 rdp_prev = rdp;
3fbfbf7a
PM
2473 }
2474}
2475
2476/* Prevent __call_rcu() from enqueuing callbacks on no-CBs CPUs */
34ed6246 2477static bool init_nocb_callback_list(struct rcu_data *rdp)
3fbfbf7a 2478{
22c2f669 2479 if (!rcu_is_nocb_cpu(rdp->cpu))
34ed6246 2480 return false;
22c2f669 2481
34404ca8 2482 /* If there are early-boot callbacks, move them to nocb lists. */
15fecf89
PM
2483 if (!rcu_segcblist_empty(&rdp->cblist)) {
2484 rdp->nocb_head = rcu_segcblist_head(&rdp->cblist);
2485 rdp->nocb_tail = rcu_segcblist_tail(&rdp->cblist);
2486 atomic_long_set(&rdp->nocb_q_count,
2487 rcu_segcblist_n_cbs(&rdp->cblist));
2488 atomic_long_set(&rdp->nocb_q_count_lazy,
2489 rcu_segcblist_n_lazy_cbs(&rdp->cblist));
2490 rcu_segcblist_init(&rdp->cblist);
34404ca8 2491 }
15fecf89 2492 rcu_segcblist_disable(&rdp->cblist);
34ed6246 2493 return true;
3fbfbf7a
PM
2494}
2495
5ab7ab83
PM
2496/*
2497 * Bind the current task to the offloaded CPUs. If there are no offloaded
2498 * CPUs, leave the task unbound. Splat if the bind attempt fails.
2499 */
2500void rcu_bind_current_to_nocb(void)
2501{
2502 if (cpumask_available(rcu_nocb_mask) && cpumask_weight(rcu_nocb_mask))
2503 WARN_ON(sched_setaffinity(current->pid, rcu_nocb_mask));
2504}
2505EXPORT_SYMBOL_GPL(rcu_bind_current_to_nocb);
2506
903ee83d
PM
2507/*
2508 * Return the number of RCU callbacks still queued from the specified
2509 * CPU, which must be a nocbs CPU.
2510 */
2511static unsigned long rcu_get_n_cbs_nocb_cpu(struct rcu_data *rdp)
2512{
2513 return atomic_long_read(&rdp->nocb_q_count);
2514}
2515
34ed6246
PM
2516#else /* #ifdef CONFIG_RCU_NOCB_CPU */
2517
4580b054 2518static bool rcu_nocb_cpu_needs_barrier(int cpu)
d7e29933
PM
2519{
2520 WARN_ON_ONCE(1); /* Should be dead code. */
2521 return false;
2522}
2523
abedf8e2 2524static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
3fbfbf7a 2525{
3fbfbf7a
PM
2526}
2527
abedf8e2 2528static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
065bb78c
DW
2529{
2530 return NULL;
2531}
2532
dae6e64d
PM
2533static void rcu_init_one_nocb(struct rcu_node *rnp)
2534{
2535}
3fbfbf7a 2536
3fbfbf7a 2537static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
96d3fd0d 2538 bool lazy, unsigned long flags)
3fbfbf7a 2539{
4afc7e26 2540 return false;
3fbfbf7a
PM
2541}
2542
b1a2d79f 2543static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp,
96d3fd0d
PM
2544 struct rcu_data *rdp,
2545 unsigned long flags)
3fbfbf7a 2546{
f4aa84ba 2547 return false;
3fbfbf7a
PM
2548}
2549
3fbfbf7a
PM
2550static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
2551{
2552}
2553
9fdd3bc9 2554static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
96d3fd0d
PM
2555{
2556 return false;
2557}
2558
2559static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
2560{
2561}
2562
ad368d15 2563static void rcu_spawn_cpu_nocb_kthread(int cpu)
35ce7f29
PM
2564{
2565}
2566
2567static void __init rcu_spawn_nocb_kthreads(void)
3fbfbf7a
PM
2568{
2569}
2570
34ed6246 2571static bool init_nocb_callback_list(struct rcu_data *rdp)
3fbfbf7a 2572{
34ed6246 2573 return false;
3fbfbf7a
PM
2574}
2575
903ee83d
PM
2576static unsigned long rcu_get_n_cbs_nocb_cpu(struct rcu_data *rdp)
2577{
2578 return 0;
2579}
2580
3fbfbf7a 2581#endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
65d798f0 2582
a096932f
PM
2583/*
2584 * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
2585 * grace-period kthread will do force_quiescent_state() processing?
2586 * The idea is to avoid waking up RCU core processing on such a
2587 * CPU unless the grace period has extended for too long.
2588 *
2589 * This code relies on the fact that all NO_HZ_FULL CPUs are also
52e2bb95 2590 * CONFIG_RCU_NOCB_CPU CPUs.
a096932f 2591 */
4580b054 2592static bool rcu_nohz_full_cpu(void)
a096932f
PM
2593{
2594#ifdef CONFIG_NO_HZ_FULL
2595 if (tick_nohz_full_cpu(smp_processor_id()) &&
de8e8730 2596 (!rcu_gp_in_progress() ||
4580b054 2597 ULONG_CMP_LT(jiffies, READ_ONCE(rcu_state.gp_start) + HZ)))
5ce035fb 2598 return true;
a096932f 2599#endif /* #ifdef CONFIG_NO_HZ_FULL */
5ce035fb 2600 return false;
a096932f 2601}
5057f55e
PM
2602
2603/*
265f5f28 2604 * Bind the RCU grace-period kthreads to the housekeeping CPU.
5057f55e
PM
2605 */
2606static void rcu_bind_gp_kthread(void)
2607{
c0f489d2 2608 if (!tick_nohz_full_enabled())
5057f55e 2609 return;
de201559 2610 housekeeping_affine(current, HK_FLAG_RCU);
5057f55e 2611}
176f8f7a
PM
2612
2613/* Record the current task on dyntick-idle entry. */
2614static void rcu_dynticks_task_enter(void)
2615{
2616#if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
7d0ae808 2617 WRITE_ONCE(current->rcu_tasks_idle_cpu, smp_processor_id());
176f8f7a
PM
2618#endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
2619}
2620
2621/* Record no current task on dyntick-idle exit. */
2622static void rcu_dynticks_task_exit(void)
2623{
2624#if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
7d0ae808 2625 WRITE_ONCE(current->rcu_tasks_idle_cpu, -1);
176f8f7a
PM
2626#endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
2627}