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