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