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