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