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