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