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