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