]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - kernel/rcu/tree.c
Merge branch 'fixes' of git://ftp.arm.linux.org.uk/~rmk/linux-arm
[mirror_ubuntu-artful-kernel.git] / kernel / rcu / tree.c
1 /*
2 * Read-Copy Update mechanism for mutual exclusion
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * Copyright IBM Corporation, 2008
19 *
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
22 * Paul E. McKenney <paulmck@linux.vnet.ibm.com> Hierarchical version
23 *
24 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
25 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
26 *
27 * For detailed explanation of Read-Copy Update mechanism see -
28 * Documentation/RCU
29 */
30 #include <linux/types.h>
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/spinlock.h>
34 #include <linux/smp.h>
35 #include <linux/rcupdate.h>
36 #include <linux/interrupt.h>
37 #include <linux/sched.h>
38 #include <linux/nmi.h>
39 #include <linux/atomic.h>
40 #include <linux/bitops.h>
41 #include <linux/export.h>
42 #include <linux/completion.h>
43 #include <linux/moduleparam.h>
44 #include <linux/module.h>
45 #include <linux/percpu.h>
46 #include <linux/notifier.h>
47 #include <linux/cpu.h>
48 #include <linux/mutex.h>
49 #include <linux/time.h>
50 #include <linux/kernel_stat.h>
51 #include <linux/wait.h>
52 #include <linux/kthread.h>
53 #include <linux/prefetch.h>
54 #include <linux/delay.h>
55 #include <linux/stop_machine.h>
56 #include <linux/random.h>
57 #include <linux/trace_events.h>
58 #include <linux/suspend.h>
59
60 #include "tree.h"
61 #include "rcu.h"
62
63 MODULE_ALIAS("rcutree");
64 #ifdef MODULE_PARAM_PREFIX
65 #undef MODULE_PARAM_PREFIX
66 #endif
67 #define MODULE_PARAM_PREFIX "rcutree."
68
69 /* Data structures. */
70
71 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
72 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
73 static struct lock_class_key rcu_exp_class[RCU_NUM_LVLS];
74 static struct lock_class_key rcu_exp_sched_class[RCU_NUM_LVLS];
75
76 /*
77 * In order to export the rcu_state name to the tracing tools, it
78 * needs to be added in the __tracepoint_string section.
79 * This requires defining a separate variable tp_<sname>_varname
80 * that points to the string being used, and this will allow
81 * the tracing userspace tools to be able to decipher the string
82 * address to the matching string.
83 */
84 #ifdef CONFIG_TRACING
85 # define DEFINE_RCU_TPS(sname) \
86 static char sname##_varname[] = #sname; \
87 static const char *tp_##sname##_varname __used __tracepoint_string = sname##_varname;
88 # define RCU_STATE_NAME(sname) sname##_varname
89 #else
90 # define DEFINE_RCU_TPS(sname)
91 # define RCU_STATE_NAME(sname) __stringify(sname)
92 #endif
93
94 #define RCU_STATE_INITIALIZER(sname, sabbr, cr) \
95 DEFINE_RCU_TPS(sname) \
96 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, sname##_data); \
97 struct rcu_state sname##_state = { \
98 .level = { &sname##_state.node[0] }, \
99 .rda = &sname##_data, \
100 .call = cr, \
101 .fqs_state = RCU_GP_IDLE, \
102 .gpnum = 0UL - 300UL, \
103 .completed = 0UL - 300UL, \
104 .orphan_lock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.orphan_lock), \
105 .orphan_nxttail = &sname##_state.orphan_nxtlist, \
106 .orphan_donetail = &sname##_state.orphan_donelist, \
107 .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
108 .name = RCU_STATE_NAME(sname), \
109 .abbr = sabbr, \
110 }
111
112 RCU_STATE_INITIALIZER(rcu_sched, 's', call_rcu_sched);
113 RCU_STATE_INITIALIZER(rcu_bh, 'b', call_rcu_bh);
114
115 static struct rcu_state *const rcu_state_p;
116 static struct rcu_data __percpu *const rcu_data_p;
117 LIST_HEAD(rcu_struct_flavors);
118
119 /* Dump rcu_node combining tree at boot to verify correct setup. */
120 static bool dump_tree;
121 module_param(dump_tree, bool, 0444);
122 /* Control rcu_node-tree auto-balancing at boot time. */
123 static bool rcu_fanout_exact;
124 module_param(rcu_fanout_exact, bool, 0444);
125 /* Increase (but not decrease) the RCU_FANOUT_LEAF at boot time. */
126 static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
127 module_param(rcu_fanout_leaf, int, 0444);
128 int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
129 /* Number of rcu_nodes at specified level. */
130 static int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
131 int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
132
133 /*
134 * The rcu_scheduler_active variable transitions from zero to one just
135 * before the first task is spawned. So when this variable is zero, RCU
136 * can assume that there is but one task, allowing RCU to (for example)
137 * optimize synchronize_sched() to a simple barrier(). When this variable
138 * is one, RCU must actually do all the hard work required to detect real
139 * grace periods. This variable is also used to suppress boot-time false
140 * positives from lockdep-RCU error checking.
141 */
142 int rcu_scheduler_active __read_mostly;
143 EXPORT_SYMBOL_GPL(rcu_scheduler_active);
144
145 /*
146 * The rcu_scheduler_fully_active variable transitions from zero to one
147 * during the early_initcall() processing, which is after the scheduler
148 * is capable of creating new tasks. So RCU processing (for example,
149 * creating tasks for RCU priority boosting) must be delayed until after
150 * rcu_scheduler_fully_active transitions from zero to one. We also
151 * currently delay invocation of any RCU callbacks until after this point.
152 *
153 * It might later prove better for people registering RCU callbacks during
154 * early boot to take responsibility for these callbacks, but one step at
155 * a time.
156 */
157 static int rcu_scheduler_fully_active __read_mostly;
158
159 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
160 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
161 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
162 static void invoke_rcu_core(void);
163 static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
164
165 /* rcuc/rcub kthread realtime priority */
166 #ifdef CONFIG_RCU_KTHREAD_PRIO
167 static int kthread_prio = CONFIG_RCU_KTHREAD_PRIO;
168 #else /* #ifdef CONFIG_RCU_KTHREAD_PRIO */
169 static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
170 #endif /* #else #ifdef CONFIG_RCU_KTHREAD_PRIO */
171 module_param(kthread_prio, int, 0644);
172
173 /* Delay in jiffies for grace-period initialization delays, debug only. */
174
175 #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT
176 static int gp_preinit_delay = CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT_DELAY;
177 module_param(gp_preinit_delay, int, 0644);
178 #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
179 static const int gp_preinit_delay;
180 #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_PREINIT */
181
182 #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT
183 static int gp_init_delay = CONFIG_RCU_TORTURE_TEST_SLOW_INIT_DELAY;
184 module_param(gp_init_delay, int, 0644);
185 #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
186 static const int gp_init_delay;
187 #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_INIT */
188
189 #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP
190 static int gp_cleanup_delay = CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP_DELAY;
191 module_param(gp_cleanup_delay, int, 0644);
192 #else /* #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
193 static const int gp_cleanup_delay;
194 #endif /* #else #ifdef CONFIG_RCU_TORTURE_TEST_SLOW_CLEANUP */
195
196 /*
197 * Number of grace periods between delays, normalized by the duration of
198 * the delay. The longer the the delay, the more the grace periods between
199 * each delay. The reason for this normalization is that it means that,
200 * for non-zero delays, the overall slowdown of grace periods is constant
201 * regardless of the duration of the delay. This arrangement balances
202 * the need for long delays to increase some race probabilities with the
203 * need for fast grace periods to increase other race probabilities.
204 */
205 #define PER_RCU_NODE_PERIOD 3 /* Number of grace periods between delays. */
206
207 /*
208 * Track the rcutorture test sequence number and the update version
209 * number within a given test. The rcutorture_testseq is incremented
210 * on every rcutorture module load and unload, so has an odd value
211 * when a test is running. The rcutorture_vernum is set to zero
212 * when rcutorture starts and is incremented on each rcutorture update.
213 * These variables enable correlating rcutorture output with the
214 * RCU tracing information.
215 */
216 unsigned long rcutorture_testseq;
217 unsigned long rcutorture_vernum;
218
219 /*
220 * Compute the mask of online CPUs for the specified rcu_node structure.
221 * This will not be stable unless the rcu_node structure's ->lock is
222 * held, but the bit corresponding to the current CPU will be stable
223 * in most contexts.
224 */
225 unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
226 {
227 return READ_ONCE(rnp->qsmaskinitnext);
228 }
229
230 /*
231 * Return true if an RCU grace period is in progress. The READ_ONCE()s
232 * permit this function to be invoked without holding the root rcu_node
233 * structure's ->lock, but of course results can be subject to change.
234 */
235 static int rcu_gp_in_progress(struct rcu_state *rsp)
236 {
237 return READ_ONCE(rsp->completed) != READ_ONCE(rsp->gpnum);
238 }
239
240 /*
241 * Note a quiescent state. Because we do not need to know
242 * how many quiescent states passed, just if there was at least
243 * one since the start of the grace period, this just sets a flag.
244 * The caller must have disabled preemption.
245 */
246 void rcu_sched_qs(void)
247 {
248 if (!__this_cpu_read(rcu_sched_data.passed_quiesce)) {
249 trace_rcu_grace_period(TPS("rcu_sched"),
250 __this_cpu_read(rcu_sched_data.gpnum),
251 TPS("cpuqs"));
252 __this_cpu_write(rcu_sched_data.passed_quiesce, 1);
253 }
254 }
255
256 void rcu_bh_qs(void)
257 {
258 if (!__this_cpu_read(rcu_bh_data.passed_quiesce)) {
259 trace_rcu_grace_period(TPS("rcu_bh"),
260 __this_cpu_read(rcu_bh_data.gpnum),
261 TPS("cpuqs"));
262 __this_cpu_write(rcu_bh_data.passed_quiesce, 1);
263 }
264 }
265
266 static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
267
268 static DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
269 .dynticks_nesting = DYNTICK_TASK_EXIT_IDLE,
270 .dynticks = ATOMIC_INIT(1),
271 #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
272 .dynticks_idle_nesting = DYNTICK_TASK_NEST_VALUE,
273 .dynticks_idle = ATOMIC_INIT(1),
274 #endif /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
275 };
276
277 DEFINE_PER_CPU_SHARED_ALIGNED(unsigned long, rcu_qs_ctr);
278 EXPORT_PER_CPU_SYMBOL_GPL(rcu_qs_ctr);
279
280 /*
281 * Let the RCU core know that this CPU has gone through the scheduler,
282 * which is a quiescent state. This is called when the need for a
283 * quiescent state is urgent, so we burn an atomic operation and full
284 * memory barriers to let the RCU core know about it, regardless of what
285 * this CPU might (or might not) do in the near future.
286 *
287 * We inform the RCU core by emulating a zero-duration dyntick-idle
288 * period, which we in turn do by incrementing the ->dynticks counter
289 * by two.
290 */
291 static void rcu_momentary_dyntick_idle(void)
292 {
293 unsigned long flags;
294 struct rcu_data *rdp;
295 struct rcu_dynticks *rdtp;
296 int resched_mask;
297 struct rcu_state *rsp;
298
299 local_irq_save(flags);
300
301 /*
302 * Yes, we can lose flag-setting operations. This is OK, because
303 * the flag will be set again after some delay.
304 */
305 resched_mask = raw_cpu_read(rcu_sched_qs_mask);
306 raw_cpu_write(rcu_sched_qs_mask, 0);
307
308 /* Find the flavor that needs a quiescent state. */
309 for_each_rcu_flavor(rsp) {
310 rdp = raw_cpu_ptr(rsp->rda);
311 if (!(resched_mask & rsp->flavor_mask))
312 continue;
313 smp_mb(); /* rcu_sched_qs_mask before cond_resched_completed. */
314 if (READ_ONCE(rdp->mynode->completed) !=
315 READ_ONCE(rdp->cond_resched_completed))
316 continue;
317
318 /*
319 * Pretend to be momentarily idle for the quiescent state.
320 * This allows the grace-period kthread to record the
321 * quiescent state, with no need for this CPU to do anything
322 * further.
323 */
324 rdtp = this_cpu_ptr(&rcu_dynticks);
325 smp_mb__before_atomic(); /* Earlier stuff before QS. */
326 atomic_add(2, &rdtp->dynticks); /* QS. */
327 smp_mb__after_atomic(); /* Later stuff after QS. */
328 break;
329 }
330 local_irq_restore(flags);
331 }
332
333 /*
334 * Note a context switch. This is a quiescent state for RCU-sched,
335 * and requires special handling for preemptible RCU.
336 * The caller must have disabled preemption.
337 */
338 void rcu_note_context_switch(void)
339 {
340 trace_rcu_utilization(TPS("Start context switch"));
341 rcu_sched_qs();
342 rcu_preempt_note_context_switch();
343 if (unlikely(raw_cpu_read(rcu_sched_qs_mask)))
344 rcu_momentary_dyntick_idle();
345 trace_rcu_utilization(TPS("End context switch"));
346 }
347 EXPORT_SYMBOL_GPL(rcu_note_context_switch);
348
349 /*
350 * Register a quiescent state for all RCU flavors. If there is an
351 * emergency, invoke rcu_momentary_dyntick_idle() to do a heavy-weight
352 * dyntick-idle quiescent state visible to other CPUs (but only for those
353 * RCU flavors in desperate need of a quiescent state, which will normally
354 * be none of them). Either way, do a lightweight quiescent state for
355 * all RCU flavors.
356 */
357 void rcu_all_qs(void)
358 {
359 if (unlikely(raw_cpu_read(rcu_sched_qs_mask)))
360 rcu_momentary_dyntick_idle();
361 this_cpu_inc(rcu_qs_ctr);
362 }
363 EXPORT_SYMBOL_GPL(rcu_all_qs);
364
365 static long blimit = 10; /* Maximum callbacks per rcu_do_batch. */
366 static long qhimark = 10000; /* If this many pending, ignore blimit. */
367 static long qlowmark = 100; /* Once only this many pending, use blimit. */
368
369 module_param(blimit, long, 0444);
370 module_param(qhimark, long, 0444);
371 module_param(qlowmark, long, 0444);
372
373 static ulong jiffies_till_first_fqs = ULONG_MAX;
374 static ulong jiffies_till_next_fqs = ULONG_MAX;
375
376 module_param(jiffies_till_first_fqs, ulong, 0644);
377 module_param(jiffies_till_next_fqs, ulong, 0644);
378
379 /*
380 * How long the grace period must be before we start recruiting
381 * quiescent-state help from rcu_note_context_switch().
382 */
383 static ulong jiffies_till_sched_qs = HZ / 20;
384 module_param(jiffies_till_sched_qs, ulong, 0644);
385
386 static bool rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
387 struct rcu_data *rdp);
388 static void force_qs_rnp(struct rcu_state *rsp,
389 int (*f)(struct rcu_data *rsp, bool *isidle,
390 unsigned long *maxj),
391 bool *isidle, unsigned long *maxj);
392 static void force_quiescent_state(struct rcu_state *rsp);
393 static int rcu_pending(void);
394
395 /*
396 * Return the number of RCU batches started thus far for debug & stats.
397 */
398 unsigned long rcu_batches_started(void)
399 {
400 return rcu_state_p->gpnum;
401 }
402 EXPORT_SYMBOL_GPL(rcu_batches_started);
403
404 /*
405 * Return the number of RCU-sched batches started thus far for debug & stats.
406 */
407 unsigned long rcu_batches_started_sched(void)
408 {
409 return rcu_sched_state.gpnum;
410 }
411 EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
412
413 /*
414 * Return the number of RCU BH batches started thus far for debug & stats.
415 */
416 unsigned long rcu_batches_started_bh(void)
417 {
418 return rcu_bh_state.gpnum;
419 }
420 EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
421
422 /*
423 * Return the number of RCU batches completed thus far for debug & stats.
424 */
425 unsigned long rcu_batches_completed(void)
426 {
427 return rcu_state_p->completed;
428 }
429 EXPORT_SYMBOL_GPL(rcu_batches_completed);
430
431 /*
432 * Return the number of RCU-sched batches completed thus far for debug & stats.
433 */
434 unsigned long rcu_batches_completed_sched(void)
435 {
436 return rcu_sched_state.completed;
437 }
438 EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
439
440 /*
441 * Return the number of RCU BH batches completed thus far for debug & stats.
442 */
443 unsigned long rcu_batches_completed_bh(void)
444 {
445 return rcu_bh_state.completed;
446 }
447 EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
448
449 /*
450 * Force a quiescent state.
451 */
452 void rcu_force_quiescent_state(void)
453 {
454 force_quiescent_state(rcu_state_p);
455 }
456 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
457
458 /*
459 * Force a quiescent state for RCU BH.
460 */
461 void rcu_bh_force_quiescent_state(void)
462 {
463 force_quiescent_state(&rcu_bh_state);
464 }
465 EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
466
467 /*
468 * Force a quiescent state for RCU-sched.
469 */
470 void rcu_sched_force_quiescent_state(void)
471 {
472 force_quiescent_state(&rcu_sched_state);
473 }
474 EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
475
476 /*
477 * Show the state of the grace-period kthreads.
478 */
479 void show_rcu_gp_kthreads(void)
480 {
481 struct rcu_state *rsp;
482
483 for_each_rcu_flavor(rsp) {
484 pr_info("%s: wait state: %d ->state: %#lx\n",
485 rsp->name, rsp->gp_state, rsp->gp_kthread->state);
486 /* sched_show_task(rsp->gp_kthread); */
487 }
488 }
489 EXPORT_SYMBOL_GPL(show_rcu_gp_kthreads);
490
491 /*
492 * Record the number of times rcutorture tests have been initiated and
493 * terminated. This information allows the debugfs tracing stats to be
494 * correlated to the rcutorture messages, even when the rcutorture module
495 * is being repeatedly loaded and unloaded. In other words, we cannot
496 * store this state in rcutorture itself.
497 */
498 void rcutorture_record_test_transition(void)
499 {
500 rcutorture_testseq++;
501 rcutorture_vernum = 0;
502 }
503 EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
504
505 /*
506 * Send along grace-period-related data for rcutorture diagnostics.
507 */
508 void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
509 unsigned long *gpnum, unsigned long *completed)
510 {
511 struct rcu_state *rsp = NULL;
512
513 switch (test_type) {
514 case RCU_FLAVOR:
515 rsp = rcu_state_p;
516 break;
517 case RCU_BH_FLAVOR:
518 rsp = &rcu_bh_state;
519 break;
520 case RCU_SCHED_FLAVOR:
521 rsp = &rcu_sched_state;
522 break;
523 default:
524 break;
525 }
526 if (rsp != NULL) {
527 *flags = READ_ONCE(rsp->gp_flags);
528 *gpnum = READ_ONCE(rsp->gpnum);
529 *completed = READ_ONCE(rsp->completed);
530 return;
531 }
532 *flags = 0;
533 *gpnum = 0;
534 *completed = 0;
535 }
536 EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
537
538 /*
539 * Record the number of writer passes through the current rcutorture test.
540 * This is also used to correlate debugfs tracing stats with the rcutorture
541 * messages.
542 */
543 void rcutorture_record_progress(unsigned long vernum)
544 {
545 rcutorture_vernum++;
546 }
547 EXPORT_SYMBOL_GPL(rcutorture_record_progress);
548
549 /*
550 * Does the CPU have callbacks ready to be invoked?
551 */
552 static int
553 cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
554 {
555 return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL] &&
556 rdp->nxttail[RCU_DONE_TAIL] != NULL;
557 }
558
559 /*
560 * Return the root node of the specified rcu_state structure.
561 */
562 static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
563 {
564 return &rsp->node[0];
565 }
566
567 /*
568 * Is there any need for future grace periods?
569 * Interrupts must be disabled. If the caller does not hold the root
570 * rnp_node structure's ->lock, the results are advisory only.
571 */
572 static int rcu_future_needs_gp(struct rcu_state *rsp)
573 {
574 struct rcu_node *rnp = rcu_get_root(rsp);
575 int idx = (READ_ONCE(rnp->completed) + 1) & 0x1;
576 int *fp = &rnp->need_future_gp[idx];
577
578 return READ_ONCE(*fp);
579 }
580
581 /*
582 * Does the current CPU require a not-yet-started grace period?
583 * The caller must have disabled interrupts to prevent races with
584 * normal callback registry.
585 */
586 static int
587 cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
588 {
589 int i;
590
591 if (rcu_gp_in_progress(rsp))
592 return 0; /* No, a grace period is already in progress. */
593 if (rcu_future_needs_gp(rsp))
594 return 1; /* Yes, a no-CBs CPU needs one. */
595 if (!rdp->nxttail[RCU_NEXT_TAIL])
596 return 0; /* No, this is a no-CBs (or offline) CPU. */
597 if (*rdp->nxttail[RCU_NEXT_READY_TAIL])
598 return 1; /* Yes, this CPU has newly registered callbacks. */
599 for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++)
600 if (rdp->nxttail[i - 1] != rdp->nxttail[i] &&
601 ULONG_CMP_LT(READ_ONCE(rsp->completed),
602 rdp->nxtcompleted[i]))
603 return 1; /* Yes, CBs for future grace period. */
604 return 0; /* No grace period needed. */
605 }
606
607 /*
608 * rcu_eqs_enter_common - current CPU is moving towards extended quiescent state
609 *
610 * If the new value of the ->dynticks_nesting counter now is zero,
611 * we really have entered idle, and must do the appropriate accounting.
612 * The caller must have disabled interrupts.
613 */
614 static void rcu_eqs_enter_common(long long oldval, bool user)
615 {
616 struct rcu_state *rsp;
617 struct rcu_data *rdp;
618 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
619
620 trace_rcu_dyntick(TPS("Start"), oldval, rdtp->dynticks_nesting);
621 if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
622 !user && !is_idle_task(current)) {
623 struct task_struct *idle __maybe_unused =
624 idle_task(smp_processor_id());
625
626 trace_rcu_dyntick(TPS("Error on entry: not idle task"), oldval, 0);
627 ftrace_dump(DUMP_ORIG);
628 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
629 current->pid, current->comm,
630 idle->pid, idle->comm); /* must be idle task! */
631 }
632 for_each_rcu_flavor(rsp) {
633 rdp = this_cpu_ptr(rsp->rda);
634 do_nocb_deferred_wakeup(rdp);
635 }
636 rcu_prepare_for_idle();
637 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
638 smp_mb__before_atomic(); /* See above. */
639 atomic_inc(&rdtp->dynticks);
640 smp_mb__after_atomic(); /* Force ordering with next sojourn. */
641 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
642 atomic_read(&rdtp->dynticks) & 0x1);
643 rcu_dynticks_task_enter();
644
645 /*
646 * It is illegal to enter an extended quiescent state while
647 * in an RCU read-side critical section.
648 */
649 RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
650 "Illegal idle entry in RCU read-side critical section.");
651 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map),
652 "Illegal idle entry in RCU-bh read-side critical section.");
653 RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map),
654 "Illegal idle entry in RCU-sched read-side critical section.");
655 }
656
657 /*
658 * Enter an RCU extended quiescent state, which can be either the
659 * idle loop or adaptive-tickless usermode execution.
660 */
661 static void rcu_eqs_enter(bool user)
662 {
663 long long oldval;
664 struct rcu_dynticks *rdtp;
665
666 rdtp = this_cpu_ptr(&rcu_dynticks);
667 oldval = rdtp->dynticks_nesting;
668 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
669 (oldval & DYNTICK_TASK_NEST_MASK) == 0);
670 if ((oldval & DYNTICK_TASK_NEST_MASK) == DYNTICK_TASK_NEST_VALUE) {
671 rdtp->dynticks_nesting = 0;
672 rcu_eqs_enter_common(oldval, user);
673 } else {
674 rdtp->dynticks_nesting -= DYNTICK_TASK_NEST_VALUE;
675 }
676 }
677
678 /**
679 * rcu_idle_enter - inform RCU that current CPU is entering idle
680 *
681 * Enter idle mode, in other words, -leave- the mode in which RCU
682 * read-side critical sections can occur. (Though RCU read-side
683 * critical sections can occur in irq handlers in idle, a possibility
684 * handled by irq_enter() and irq_exit().)
685 *
686 * We crowbar the ->dynticks_nesting field to zero to allow for
687 * the possibility of usermode upcalls having messed up our count
688 * of interrupt nesting level during the prior busy period.
689 */
690 void rcu_idle_enter(void)
691 {
692 unsigned long flags;
693
694 local_irq_save(flags);
695 rcu_eqs_enter(false);
696 rcu_sysidle_enter(0);
697 local_irq_restore(flags);
698 }
699 EXPORT_SYMBOL_GPL(rcu_idle_enter);
700
701 #ifdef CONFIG_NO_HZ_FULL
702 /**
703 * rcu_user_enter - inform RCU that we are resuming userspace.
704 *
705 * Enter RCU idle mode right before resuming userspace. No use of RCU
706 * is permitted between this call and rcu_user_exit(). This way the
707 * CPU doesn't need to maintain the tick for RCU maintenance purposes
708 * when the CPU runs in userspace.
709 */
710 void rcu_user_enter(void)
711 {
712 rcu_eqs_enter(1);
713 }
714 #endif /* CONFIG_NO_HZ_FULL */
715
716 /**
717 * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
718 *
719 * Exit from an interrupt handler, which might possibly result in entering
720 * idle mode, in other words, leaving the mode in which read-side critical
721 * sections can occur.
722 *
723 * This code assumes that the idle loop never does anything that might
724 * result in unbalanced calls to irq_enter() and irq_exit(). If your
725 * architecture violates this assumption, RCU will give you what you
726 * deserve, good and hard. But very infrequently and irreproducibly.
727 *
728 * Use things like work queues to work around this limitation.
729 *
730 * You have been warned.
731 */
732 void rcu_irq_exit(void)
733 {
734 unsigned long flags;
735 long long oldval;
736 struct rcu_dynticks *rdtp;
737
738 local_irq_save(flags);
739 rdtp = this_cpu_ptr(&rcu_dynticks);
740 oldval = rdtp->dynticks_nesting;
741 rdtp->dynticks_nesting--;
742 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
743 rdtp->dynticks_nesting < 0);
744 if (rdtp->dynticks_nesting)
745 trace_rcu_dyntick(TPS("--="), oldval, rdtp->dynticks_nesting);
746 else
747 rcu_eqs_enter_common(oldval, true);
748 rcu_sysidle_enter(1);
749 local_irq_restore(flags);
750 }
751
752 /*
753 * rcu_eqs_exit_common - current CPU moving away from extended quiescent state
754 *
755 * If the new value of the ->dynticks_nesting counter was previously zero,
756 * we really have exited idle, and must do the appropriate accounting.
757 * The caller must have disabled interrupts.
758 */
759 static void rcu_eqs_exit_common(long long oldval, int user)
760 {
761 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
762
763 rcu_dynticks_task_exit();
764 smp_mb__before_atomic(); /* Force ordering w/previous sojourn. */
765 atomic_inc(&rdtp->dynticks);
766 /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
767 smp_mb__after_atomic(); /* See above. */
768 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
769 !(atomic_read(&rdtp->dynticks) & 0x1));
770 rcu_cleanup_after_idle();
771 trace_rcu_dyntick(TPS("End"), oldval, rdtp->dynticks_nesting);
772 if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
773 !user && !is_idle_task(current)) {
774 struct task_struct *idle __maybe_unused =
775 idle_task(smp_processor_id());
776
777 trace_rcu_dyntick(TPS("Error on exit: not idle task"),
778 oldval, rdtp->dynticks_nesting);
779 ftrace_dump(DUMP_ORIG);
780 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
781 current->pid, current->comm,
782 idle->pid, idle->comm); /* must be idle task! */
783 }
784 }
785
786 /*
787 * Exit an RCU extended quiescent state, which can be either the
788 * idle loop or adaptive-tickless usermode execution.
789 */
790 static void rcu_eqs_exit(bool user)
791 {
792 struct rcu_dynticks *rdtp;
793 long long oldval;
794
795 rdtp = this_cpu_ptr(&rcu_dynticks);
796 oldval = rdtp->dynticks_nesting;
797 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
798 if (oldval & DYNTICK_TASK_NEST_MASK) {
799 rdtp->dynticks_nesting += DYNTICK_TASK_NEST_VALUE;
800 } else {
801 rdtp->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
802 rcu_eqs_exit_common(oldval, user);
803 }
804 }
805
806 /**
807 * rcu_idle_exit - inform RCU that current CPU is leaving idle
808 *
809 * Exit idle mode, in other words, -enter- the mode in which RCU
810 * read-side critical sections can occur.
811 *
812 * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to
813 * allow for the possibility of usermode upcalls messing up our count
814 * of interrupt nesting level during the busy period that is just
815 * now starting.
816 */
817 void rcu_idle_exit(void)
818 {
819 unsigned long flags;
820
821 local_irq_save(flags);
822 rcu_eqs_exit(false);
823 rcu_sysidle_exit(0);
824 local_irq_restore(flags);
825 }
826 EXPORT_SYMBOL_GPL(rcu_idle_exit);
827
828 #ifdef CONFIG_NO_HZ_FULL
829 /**
830 * rcu_user_exit - inform RCU that we are exiting userspace.
831 *
832 * Exit RCU idle mode while entering the kernel because it can
833 * run a RCU read side critical section anytime.
834 */
835 void rcu_user_exit(void)
836 {
837 rcu_eqs_exit(1);
838 }
839 #endif /* CONFIG_NO_HZ_FULL */
840
841 /**
842 * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
843 *
844 * Enter an interrupt handler, which might possibly result in exiting
845 * idle mode, in other words, entering the mode in which read-side critical
846 * sections can occur.
847 *
848 * Note that the Linux kernel is fully capable of entering an interrupt
849 * handler that it never exits, for example when doing upcalls to
850 * user mode! This code assumes that the idle loop never does upcalls to
851 * user mode. If your architecture does do upcalls from the idle loop (or
852 * does anything else that results in unbalanced calls to the irq_enter()
853 * and irq_exit() functions), RCU will give you what you deserve, good
854 * and hard. But very infrequently and irreproducibly.
855 *
856 * Use things like work queues to work around this limitation.
857 *
858 * You have been warned.
859 */
860 void rcu_irq_enter(void)
861 {
862 unsigned long flags;
863 struct rcu_dynticks *rdtp;
864 long long oldval;
865
866 local_irq_save(flags);
867 rdtp = this_cpu_ptr(&rcu_dynticks);
868 oldval = rdtp->dynticks_nesting;
869 rdtp->dynticks_nesting++;
870 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
871 rdtp->dynticks_nesting == 0);
872 if (oldval)
873 trace_rcu_dyntick(TPS("++="), oldval, rdtp->dynticks_nesting);
874 else
875 rcu_eqs_exit_common(oldval, true);
876 rcu_sysidle_exit(1);
877 local_irq_restore(flags);
878 }
879
880 /**
881 * rcu_nmi_enter - inform RCU of entry to NMI context
882 *
883 * If the CPU was idle from RCU's viewpoint, update rdtp->dynticks and
884 * rdtp->dynticks_nmi_nesting to let the RCU grace-period handling know
885 * that the CPU is active. This implementation permits nested NMIs, as
886 * long as the nesting level does not overflow an int. (You will probably
887 * run out of stack space first.)
888 */
889 void rcu_nmi_enter(void)
890 {
891 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
892 int incby = 2;
893
894 /* Complain about underflow. */
895 WARN_ON_ONCE(rdtp->dynticks_nmi_nesting < 0);
896
897 /*
898 * If idle from RCU viewpoint, atomically increment ->dynticks
899 * to mark non-idle and increment ->dynticks_nmi_nesting by one.
900 * Otherwise, increment ->dynticks_nmi_nesting by two. This means
901 * if ->dynticks_nmi_nesting is equal to one, we are guaranteed
902 * to be in the outermost NMI handler that interrupted an RCU-idle
903 * period (observation due to Andy Lutomirski).
904 */
905 if (!(atomic_read(&rdtp->dynticks) & 0x1)) {
906 smp_mb__before_atomic(); /* Force delay from prior write. */
907 atomic_inc(&rdtp->dynticks);
908 /* atomic_inc() before later RCU read-side crit sects */
909 smp_mb__after_atomic(); /* See above. */
910 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
911 incby = 1;
912 }
913 rdtp->dynticks_nmi_nesting += incby;
914 barrier();
915 }
916
917 /**
918 * rcu_nmi_exit - inform RCU of exit from NMI context
919 *
920 * If we are returning from the outermost NMI handler that interrupted an
921 * RCU-idle period, update rdtp->dynticks and rdtp->dynticks_nmi_nesting
922 * to let the RCU grace-period handling know that the CPU is back to
923 * being RCU-idle.
924 */
925 void rcu_nmi_exit(void)
926 {
927 struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
928
929 /*
930 * Check for ->dynticks_nmi_nesting underflow and bad ->dynticks.
931 * (We are exiting an NMI handler, so RCU better be paying attention
932 * to us!)
933 */
934 WARN_ON_ONCE(rdtp->dynticks_nmi_nesting <= 0);
935 WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
936
937 /*
938 * If the nesting level is not 1, the CPU wasn't RCU-idle, so
939 * leave it in non-RCU-idle state.
940 */
941 if (rdtp->dynticks_nmi_nesting != 1) {
942 rdtp->dynticks_nmi_nesting -= 2;
943 return;
944 }
945
946 /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */
947 rdtp->dynticks_nmi_nesting = 0;
948 /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
949 smp_mb__before_atomic(); /* See above. */
950 atomic_inc(&rdtp->dynticks);
951 smp_mb__after_atomic(); /* Force delay to next write. */
952 WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
953 }
954
955 /**
956 * __rcu_is_watching - are RCU read-side critical sections safe?
957 *
958 * Return true if RCU is watching the running CPU, which means that
959 * this CPU can safely enter RCU read-side critical sections. Unlike
960 * rcu_is_watching(), the caller of __rcu_is_watching() must have at
961 * least disabled preemption.
962 */
963 bool notrace __rcu_is_watching(void)
964 {
965 return atomic_read(this_cpu_ptr(&rcu_dynticks.dynticks)) & 0x1;
966 }
967
968 /**
969 * rcu_is_watching - see if RCU thinks that the current CPU is idle
970 *
971 * If the current CPU is in its idle loop and is neither in an interrupt
972 * or NMI handler, return true.
973 */
974 bool notrace rcu_is_watching(void)
975 {
976 bool ret;
977
978 preempt_disable_notrace();
979 ret = __rcu_is_watching();
980 preempt_enable_notrace();
981 return ret;
982 }
983 EXPORT_SYMBOL_GPL(rcu_is_watching);
984
985 #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
986
987 /*
988 * Is the current CPU online? Disable preemption to avoid false positives
989 * that could otherwise happen due to the current CPU number being sampled,
990 * this task being preempted, its old CPU being taken offline, resuming
991 * on some other CPU, then determining that its old CPU is now offline.
992 * It is OK to use RCU on an offline processor during initial boot, hence
993 * the check for rcu_scheduler_fully_active. Note also that it is OK
994 * for a CPU coming online to use RCU for one jiffy prior to marking itself
995 * online in the cpu_online_mask. Similarly, it is OK for a CPU going
996 * offline to continue to use RCU for one jiffy after marking itself
997 * offline in the cpu_online_mask. This leniency is necessary given the
998 * non-atomic nature of the online and offline processing, for example,
999 * the fact that a CPU enters the scheduler after completing the CPU_DYING
1000 * notifiers.
1001 *
1002 * This is also why RCU internally marks CPUs online during the
1003 * CPU_UP_PREPARE phase and offline during the CPU_DEAD phase.
1004 *
1005 * Disable checking if in an NMI handler because we cannot safely report
1006 * errors from NMI handlers anyway.
1007 */
1008 bool rcu_lockdep_current_cpu_online(void)
1009 {
1010 struct rcu_data *rdp;
1011 struct rcu_node *rnp;
1012 bool ret;
1013
1014 if (in_nmi())
1015 return true;
1016 preempt_disable();
1017 rdp = this_cpu_ptr(&rcu_sched_data);
1018 rnp = rdp->mynode;
1019 ret = (rdp->grpmask & rcu_rnp_online_cpus(rnp)) ||
1020 !rcu_scheduler_fully_active;
1021 preempt_enable();
1022 return ret;
1023 }
1024 EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
1025
1026 #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
1027
1028 /**
1029 * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
1030 *
1031 * If the current CPU is idle or running at a first-level (not nested)
1032 * interrupt from idle, return true. The caller must have at least
1033 * disabled preemption.
1034 */
1035 static int rcu_is_cpu_rrupt_from_idle(void)
1036 {
1037 return __this_cpu_read(rcu_dynticks.dynticks_nesting) <= 1;
1038 }
1039
1040 /*
1041 * Snapshot the specified CPU's dynticks counter so that we can later
1042 * credit them with an implicit quiescent state. Return 1 if this CPU
1043 * is in dynticks idle mode, which is an extended quiescent state.
1044 */
1045 static int dyntick_save_progress_counter(struct rcu_data *rdp,
1046 bool *isidle, unsigned long *maxj)
1047 {
1048 rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
1049 rcu_sysidle_check_cpu(rdp, isidle, maxj);
1050 if ((rdp->dynticks_snap & 0x1) == 0) {
1051 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
1052 return 1;
1053 } else {
1054 if (ULONG_CMP_LT(READ_ONCE(rdp->gpnum) + ULONG_MAX / 4,
1055 rdp->mynode->gpnum))
1056 WRITE_ONCE(rdp->gpwrap, true);
1057 return 0;
1058 }
1059 }
1060
1061 /*
1062 * Return true if the specified CPU has passed through a quiescent
1063 * state by virtue of being in or having passed through an dynticks
1064 * idle state since the last call to dyntick_save_progress_counter()
1065 * for this same CPU, or by virtue of having been offline.
1066 */
1067 static int rcu_implicit_dynticks_qs(struct rcu_data *rdp,
1068 bool *isidle, unsigned long *maxj)
1069 {
1070 unsigned int curr;
1071 int *rcrmp;
1072 unsigned int snap;
1073
1074 curr = (unsigned int)atomic_add_return(0, &rdp->dynticks->dynticks);
1075 snap = (unsigned int)rdp->dynticks_snap;
1076
1077 /*
1078 * If the CPU passed through or entered a dynticks idle phase with
1079 * no active irq/NMI handlers, then we can safely pretend that the CPU
1080 * already acknowledged the request to pass through a quiescent
1081 * state. Either way, that CPU cannot possibly be in an RCU
1082 * read-side critical section that started before the beginning
1083 * of the current RCU grace period.
1084 */
1085 if ((curr & 0x1) == 0 || UINT_CMP_GE(curr, snap + 2)) {
1086 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("dti"));
1087 rdp->dynticks_fqs++;
1088 return 1;
1089 }
1090
1091 /*
1092 * Check for the CPU being offline, but only if the grace period
1093 * is old enough. We don't need to worry about the CPU changing
1094 * state: If we see it offline even once, it has been through a
1095 * quiescent state.
1096 *
1097 * The reason for insisting that the grace period be at least
1098 * one jiffy old is that CPUs that are not quite online and that
1099 * have just gone offline can still execute RCU read-side critical
1100 * sections.
1101 */
1102 if (ULONG_CMP_GE(rdp->rsp->gp_start + 2, jiffies))
1103 return 0; /* Grace period is not old enough. */
1104 barrier();
1105 if (cpu_is_offline(rdp->cpu)) {
1106 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, TPS("ofl"));
1107 rdp->offline_fqs++;
1108 return 1;
1109 }
1110
1111 /*
1112 * A CPU running for an extended time within the kernel can
1113 * delay RCU grace periods. When the CPU is in NO_HZ_FULL mode,
1114 * even context-switching back and forth between a pair of
1115 * in-kernel CPU-bound tasks cannot advance grace periods.
1116 * So if the grace period is old enough, make the CPU pay attention.
1117 * Note that the unsynchronized assignments to the per-CPU
1118 * rcu_sched_qs_mask variable are safe. Yes, setting of
1119 * bits can be lost, but they will be set again on the next
1120 * force-quiescent-state pass. So lost bit sets do not result
1121 * in incorrect behavior, merely in a grace period lasting
1122 * a few jiffies longer than it might otherwise. Because
1123 * there are at most four threads involved, and because the
1124 * updates are only once every few jiffies, the probability of
1125 * lossage (and thus of slight grace-period extension) is
1126 * quite low.
1127 *
1128 * Note that if the jiffies_till_sched_qs boot/sysfs parameter
1129 * is set too high, we override with half of the RCU CPU stall
1130 * warning delay.
1131 */
1132 rcrmp = &per_cpu(rcu_sched_qs_mask, rdp->cpu);
1133 if (ULONG_CMP_GE(jiffies,
1134 rdp->rsp->gp_start + jiffies_till_sched_qs) ||
1135 ULONG_CMP_GE(jiffies, rdp->rsp->jiffies_resched)) {
1136 if (!(READ_ONCE(*rcrmp) & rdp->rsp->flavor_mask)) {
1137 WRITE_ONCE(rdp->cond_resched_completed,
1138 READ_ONCE(rdp->mynode->completed));
1139 smp_mb(); /* ->cond_resched_completed before *rcrmp. */
1140 WRITE_ONCE(*rcrmp,
1141 READ_ONCE(*rcrmp) + rdp->rsp->flavor_mask);
1142 resched_cpu(rdp->cpu); /* Force CPU into scheduler. */
1143 rdp->rsp->jiffies_resched += 5; /* Enable beating. */
1144 } else if (ULONG_CMP_GE(jiffies, rdp->rsp->jiffies_resched)) {
1145 /* Time to beat on that CPU again! */
1146 resched_cpu(rdp->cpu); /* Force CPU into scheduler. */
1147 rdp->rsp->jiffies_resched += 5; /* Re-enable beating. */
1148 }
1149 }
1150
1151 return 0;
1152 }
1153
1154 static void record_gp_stall_check_time(struct rcu_state *rsp)
1155 {
1156 unsigned long j = jiffies;
1157 unsigned long j1;
1158
1159 rsp->gp_start = j;
1160 smp_wmb(); /* Record start time before stall time. */
1161 j1 = rcu_jiffies_till_stall_check();
1162 WRITE_ONCE(rsp->jiffies_stall, j + j1);
1163 rsp->jiffies_resched = j + j1 / 2;
1164 rsp->n_force_qs_gpstart = READ_ONCE(rsp->n_force_qs);
1165 }
1166
1167 /*
1168 * Complain about starvation of grace-period kthread.
1169 */
1170 static void rcu_check_gp_kthread_starvation(struct rcu_state *rsp)
1171 {
1172 unsigned long gpa;
1173 unsigned long j;
1174
1175 j = jiffies;
1176 gpa = READ_ONCE(rsp->gp_activity);
1177 if (j - gpa > 2 * HZ)
1178 pr_err("%s kthread starved for %ld jiffies! g%lu c%lu f%#x s%d ->state=%#lx\n",
1179 rsp->name, j - gpa,
1180 rsp->gpnum, rsp->completed,
1181 rsp->gp_flags, rsp->gp_state,
1182 rsp->gp_kthread ? rsp->gp_kthread->state : 0);
1183 }
1184
1185 /*
1186 * Dump stacks of all tasks running on stalled CPUs.
1187 */
1188 static void rcu_dump_cpu_stacks(struct rcu_state *rsp)
1189 {
1190 int cpu;
1191 unsigned long flags;
1192 struct rcu_node *rnp;
1193
1194 rcu_for_each_leaf_node(rsp, rnp) {
1195 raw_spin_lock_irqsave(&rnp->lock, flags);
1196 if (rnp->qsmask != 0) {
1197 for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
1198 if (rnp->qsmask & (1UL << cpu))
1199 dump_cpu_task(rnp->grplo + cpu);
1200 }
1201 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1202 }
1203 }
1204
1205 static void print_other_cpu_stall(struct rcu_state *rsp, unsigned long gpnum)
1206 {
1207 int cpu;
1208 long delta;
1209 unsigned long flags;
1210 unsigned long gpa;
1211 unsigned long j;
1212 int ndetected = 0;
1213 struct rcu_node *rnp = rcu_get_root(rsp);
1214 long totqlen = 0;
1215
1216 /* Only let one CPU complain about others per time interval. */
1217
1218 raw_spin_lock_irqsave(&rnp->lock, flags);
1219 delta = jiffies - READ_ONCE(rsp->jiffies_stall);
1220 if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
1221 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1222 return;
1223 }
1224 WRITE_ONCE(rsp->jiffies_stall,
1225 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
1226 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1227
1228 /*
1229 * OK, time to rat on our buddy...
1230 * See Documentation/RCU/stallwarn.txt for info on how to debug
1231 * RCU CPU stall warnings.
1232 */
1233 pr_err("INFO: %s detected stalls on CPUs/tasks:",
1234 rsp->name);
1235 print_cpu_stall_info_begin();
1236 rcu_for_each_leaf_node(rsp, rnp) {
1237 raw_spin_lock_irqsave(&rnp->lock, flags);
1238 ndetected += rcu_print_task_stall(rnp);
1239 if (rnp->qsmask != 0) {
1240 for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
1241 if (rnp->qsmask & (1UL << cpu)) {
1242 print_cpu_stall_info(rsp,
1243 rnp->grplo + cpu);
1244 ndetected++;
1245 }
1246 }
1247 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1248 }
1249
1250 print_cpu_stall_info_end();
1251 for_each_possible_cpu(cpu)
1252 totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
1253 pr_cont("(detected by %d, t=%ld jiffies, g=%ld, c=%ld, q=%lu)\n",
1254 smp_processor_id(), (long)(jiffies - rsp->gp_start),
1255 (long)rsp->gpnum, (long)rsp->completed, totqlen);
1256 if (ndetected) {
1257 rcu_dump_cpu_stacks(rsp);
1258 } else {
1259 if (READ_ONCE(rsp->gpnum) != gpnum ||
1260 READ_ONCE(rsp->completed) == gpnum) {
1261 pr_err("INFO: Stall ended before state dump start\n");
1262 } else {
1263 j = jiffies;
1264 gpa = READ_ONCE(rsp->gp_activity);
1265 pr_err("All QSes seen, last %s kthread activity %ld (%ld-%ld), jiffies_till_next_fqs=%ld, root ->qsmask %#lx\n",
1266 rsp->name, j - gpa, j, gpa,
1267 jiffies_till_next_fqs,
1268 rcu_get_root(rsp)->qsmask);
1269 /* In this case, the current CPU might be at fault. */
1270 sched_show_task(current);
1271 }
1272 }
1273
1274 /* Complain about tasks blocking the grace period. */
1275 rcu_print_detail_task_stall(rsp);
1276
1277 rcu_check_gp_kthread_starvation(rsp);
1278
1279 force_quiescent_state(rsp); /* Kick them all. */
1280 }
1281
1282 static void print_cpu_stall(struct rcu_state *rsp)
1283 {
1284 int cpu;
1285 unsigned long flags;
1286 struct rcu_node *rnp = rcu_get_root(rsp);
1287 long totqlen = 0;
1288
1289 /*
1290 * OK, time to rat on ourselves...
1291 * See Documentation/RCU/stallwarn.txt for info on how to debug
1292 * RCU CPU stall warnings.
1293 */
1294 pr_err("INFO: %s self-detected stall on CPU", rsp->name);
1295 print_cpu_stall_info_begin();
1296 print_cpu_stall_info(rsp, smp_processor_id());
1297 print_cpu_stall_info_end();
1298 for_each_possible_cpu(cpu)
1299 totqlen += per_cpu_ptr(rsp->rda, cpu)->qlen;
1300 pr_cont(" (t=%lu jiffies g=%ld c=%ld q=%lu)\n",
1301 jiffies - rsp->gp_start,
1302 (long)rsp->gpnum, (long)rsp->completed, totqlen);
1303
1304 rcu_check_gp_kthread_starvation(rsp);
1305
1306 rcu_dump_cpu_stacks(rsp);
1307
1308 raw_spin_lock_irqsave(&rnp->lock, flags);
1309 if (ULONG_CMP_GE(jiffies, READ_ONCE(rsp->jiffies_stall)))
1310 WRITE_ONCE(rsp->jiffies_stall,
1311 jiffies + 3 * rcu_jiffies_till_stall_check() + 3);
1312 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1313
1314 /*
1315 * Attempt to revive the RCU machinery by forcing a context switch.
1316 *
1317 * A context switch would normally allow the RCU state machine to make
1318 * progress and it could be we're stuck in kernel space without context
1319 * switches for an entirely unreasonable amount of time.
1320 */
1321 resched_cpu(smp_processor_id());
1322 }
1323
1324 static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
1325 {
1326 unsigned long completed;
1327 unsigned long gpnum;
1328 unsigned long gps;
1329 unsigned long j;
1330 unsigned long js;
1331 struct rcu_node *rnp;
1332
1333 if (rcu_cpu_stall_suppress || !rcu_gp_in_progress(rsp))
1334 return;
1335 j = jiffies;
1336
1337 /*
1338 * Lots of memory barriers to reject false positives.
1339 *
1340 * The idea is to pick up rsp->gpnum, then rsp->jiffies_stall,
1341 * then rsp->gp_start, and finally rsp->completed. These values
1342 * are updated in the opposite order with memory barriers (or
1343 * equivalent) during grace-period initialization and cleanup.
1344 * Now, a false positive can occur if we get an new value of
1345 * rsp->gp_start and a old value of rsp->jiffies_stall. But given
1346 * the memory barriers, the only way that this can happen is if one
1347 * grace period ends and another starts between these two fetches.
1348 * Detect this by comparing rsp->completed with the previous fetch
1349 * from rsp->gpnum.
1350 *
1351 * Given this check, comparisons of jiffies, rsp->jiffies_stall,
1352 * and rsp->gp_start suffice to forestall false positives.
1353 */
1354 gpnum = READ_ONCE(rsp->gpnum);
1355 smp_rmb(); /* Pick up ->gpnum first... */
1356 js = READ_ONCE(rsp->jiffies_stall);
1357 smp_rmb(); /* ...then ->jiffies_stall before the rest... */
1358 gps = READ_ONCE(rsp->gp_start);
1359 smp_rmb(); /* ...and finally ->gp_start before ->completed. */
1360 completed = READ_ONCE(rsp->completed);
1361 if (ULONG_CMP_GE(completed, gpnum) ||
1362 ULONG_CMP_LT(j, js) ||
1363 ULONG_CMP_GE(gps, js))
1364 return; /* No stall or GP completed since entering function. */
1365 rnp = rdp->mynode;
1366 if (rcu_gp_in_progress(rsp) &&
1367 (READ_ONCE(rnp->qsmask) & rdp->grpmask)) {
1368
1369 /* We haven't checked in, so go dump stack. */
1370 print_cpu_stall(rsp);
1371
1372 } else if (rcu_gp_in_progress(rsp) &&
1373 ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
1374
1375 /* They had a few time units to dump stack, so complain. */
1376 print_other_cpu_stall(rsp, gpnum);
1377 }
1378 }
1379
1380 /**
1381 * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
1382 *
1383 * Set the stall-warning timeout way off into the future, thus preventing
1384 * any RCU CPU stall-warning messages from appearing in the current set of
1385 * RCU grace periods.
1386 *
1387 * The caller must disable hard irqs.
1388 */
1389 void rcu_cpu_stall_reset(void)
1390 {
1391 struct rcu_state *rsp;
1392
1393 for_each_rcu_flavor(rsp)
1394 WRITE_ONCE(rsp->jiffies_stall, jiffies + ULONG_MAX / 2);
1395 }
1396
1397 /*
1398 * Initialize the specified rcu_data structure's default callback list
1399 * to empty. The default callback list is the one that is not used by
1400 * no-callbacks CPUs.
1401 */
1402 static void init_default_callback_list(struct rcu_data *rdp)
1403 {
1404 int i;
1405
1406 rdp->nxtlist = NULL;
1407 for (i = 0; i < RCU_NEXT_SIZE; i++)
1408 rdp->nxttail[i] = &rdp->nxtlist;
1409 }
1410
1411 /*
1412 * Initialize the specified rcu_data structure's callback list to empty.
1413 */
1414 static void init_callback_list(struct rcu_data *rdp)
1415 {
1416 if (init_nocb_callback_list(rdp))
1417 return;
1418 init_default_callback_list(rdp);
1419 }
1420
1421 /*
1422 * Determine the value that ->completed will have at the end of the
1423 * next subsequent grace period. This is used to tag callbacks so that
1424 * a CPU can invoke callbacks in a timely fashion even if that CPU has
1425 * been dyntick-idle for an extended period with callbacks under the
1426 * influence of RCU_FAST_NO_HZ.
1427 *
1428 * The caller must hold rnp->lock with interrupts disabled.
1429 */
1430 static unsigned long rcu_cbs_completed(struct rcu_state *rsp,
1431 struct rcu_node *rnp)
1432 {
1433 /*
1434 * If RCU is idle, we just wait for the next grace period.
1435 * But we can only be sure that RCU is idle if we are looking
1436 * at the root rcu_node structure -- otherwise, a new grace
1437 * period might have started, but just not yet gotten around
1438 * to initializing the current non-root rcu_node structure.
1439 */
1440 if (rcu_get_root(rsp) == rnp && rnp->gpnum == rnp->completed)
1441 return rnp->completed + 1;
1442
1443 /*
1444 * Otherwise, wait for a possible partial grace period and
1445 * then the subsequent full grace period.
1446 */
1447 return rnp->completed + 2;
1448 }
1449
1450 /*
1451 * Trace-event helper function for rcu_start_future_gp() and
1452 * rcu_nocb_wait_gp().
1453 */
1454 static void trace_rcu_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
1455 unsigned long c, const char *s)
1456 {
1457 trace_rcu_future_grace_period(rdp->rsp->name, rnp->gpnum,
1458 rnp->completed, c, rnp->level,
1459 rnp->grplo, rnp->grphi, s);
1460 }
1461
1462 /*
1463 * Start some future grace period, as needed to handle newly arrived
1464 * callbacks. The required future grace periods are recorded in each
1465 * rcu_node structure's ->need_future_gp field. Returns true if there
1466 * is reason to awaken the grace-period kthread.
1467 *
1468 * The caller must hold the specified rcu_node structure's ->lock.
1469 */
1470 static bool __maybe_unused
1471 rcu_start_future_gp(struct rcu_node *rnp, struct rcu_data *rdp,
1472 unsigned long *c_out)
1473 {
1474 unsigned long c;
1475 int i;
1476 bool ret = false;
1477 struct rcu_node *rnp_root = rcu_get_root(rdp->rsp);
1478
1479 /*
1480 * Pick up grace-period number for new callbacks. If this
1481 * grace period is already marked as needed, return to the caller.
1482 */
1483 c = rcu_cbs_completed(rdp->rsp, rnp);
1484 trace_rcu_future_gp(rnp, rdp, c, TPS("Startleaf"));
1485 if (rnp->need_future_gp[c & 0x1]) {
1486 trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartleaf"));
1487 goto out;
1488 }
1489
1490 /*
1491 * If either this rcu_node structure or the root rcu_node structure
1492 * believe that a grace period is in progress, then we must wait
1493 * for the one following, which is in "c". Because our request
1494 * will be noticed at the end of the current grace period, we don't
1495 * need to explicitly start one. We only do the lockless check
1496 * of rnp_root's fields if the current rcu_node structure thinks
1497 * there is no grace period in flight, and because we hold rnp->lock,
1498 * the only possible change is when rnp_root's two fields are
1499 * equal, in which case rnp_root->gpnum might be concurrently
1500 * incremented. But that is OK, as it will just result in our
1501 * doing some extra useless work.
1502 */
1503 if (rnp->gpnum != rnp->completed ||
1504 READ_ONCE(rnp_root->gpnum) != READ_ONCE(rnp_root->completed)) {
1505 rnp->need_future_gp[c & 0x1]++;
1506 trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleaf"));
1507 goto out;
1508 }
1509
1510 /*
1511 * There might be no grace period in progress. If we don't already
1512 * hold it, acquire the root rcu_node structure's lock in order to
1513 * start one (if needed).
1514 */
1515 if (rnp != rnp_root) {
1516 raw_spin_lock(&rnp_root->lock);
1517 smp_mb__after_unlock_lock();
1518 }
1519
1520 /*
1521 * Get a new grace-period number. If there really is no grace
1522 * period in progress, it will be smaller than the one we obtained
1523 * earlier. Adjust callbacks as needed. Note that even no-CBs
1524 * CPUs have a ->nxtcompleted[] array, so no no-CBs checks needed.
1525 */
1526 c = rcu_cbs_completed(rdp->rsp, rnp_root);
1527 for (i = RCU_DONE_TAIL; i < RCU_NEXT_TAIL; i++)
1528 if (ULONG_CMP_LT(c, rdp->nxtcompleted[i]))
1529 rdp->nxtcompleted[i] = c;
1530
1531 /*
1532 * If the needed for the required grace period is already
1533 * recorded, trace and leave.
1534 */
1535 if (rnp_root->need_future_gp[c & 0x1]) {
1536 trace_rcu_future_gp(rnp, rdp, c, TPS("Prestartedroot"));
1537 goto unlock_out;
1538 }
1539
1540 /* Record the need for the future grace period. */
1541 rnp_root->need_future_gp[c & 0x1]++;
1542
1543 /* If a grace period is not already in progress, start one. */
1544 if (rnp_root->gpnum != rnp_root->completed) {
1545 trace_rcu_future_gp(rnp, rdp, c, TPS("Startedleafroot"));
1546 } else {
1547 trace_rcu_future_gp(rnp, rdp, c, TPS("Startedroot"));
1548 ret = rcu_start_gp_advanced(rdp->rsp, rnp_root, rdp);
1549 }
1550 unlock_out:
1551 if (rnp != rnp_root)
1552 raw_spin_unlock(&rnp_root->lock);
1553 out:
1554 if (c_out != NULL)
1555 *c_out = c;
1556 return ret;
1557 }
1558
1559 /*
1560 * Clean up any old requests for the just-ended grace period. Also return
1561 * whether any additional grace periods have been requested. Also invoke
1562 * rcu_nocb_gp_cleanup() in order to wake up any no-callbacks kthreads
1563 * waiting for this grace period to complete.
1564 */
1565 static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
1566 {
1567 int c = rnp->completed;
1568 int needmore;
1569 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
1570
1571 rcu_nocb_gp_cleanup(rsp, rnp);
1572 rnp->need_future_gp[c & 0x1] = 0;
1573 needmore = rnp->need_future_gp[(c + 1) & 0x1];
1574 trace_rcu_future_gp(rnp, rdp, c,
1575 needmore ? TPS("CleanupMore") : TPS("Cleanup"));
1576 return needmore;
1577 }
1578
1579 /*
1580 * Awaken the grace-period kthread for the specified flavor of RCU.
1581 * Don't do a self-awaken, and don't bother awakening when there is
1582 * nothing for the grace-period kthread to do (as in several CPUs
1583 * raced to awaken, and we lost), and finally don't try to awaken
1584 * a kthread that has not yet been created.
1585 */
1586 static void rcu_gp_kthread_wake(struct rcu_state *rsp)
1587 {
1588 if (current == rsp->gp_kthread ||
1589 !READ_ONCE(rsp->gp_flags) ||
1590 !rsp->gp_kthread)
1591 return;
1592 wake_up(&rsp->gp_wq);
1593 }
1594
1595 /*
1596 * If there is room, assign a ->completed number to any callbacks on
1597 * this CPU that have not already been assigned. Also accelerate any
1598 * callbacks that were previously assigned a ->completed number that has
1599 * since proven to be too conservative, which can happen if callbacks get
1600 * assigned a ->completed number while RCU is idle, but with reference to
1601 * a non-root rcu_node structure. This function is idempotent, so it does
1602 * not hurt to call it repeatedly. Returns an flag saying that we should
1603 * awaken the RCU grace-period kthread.
1604 *
1605 * The caller must hold rnp->lock with interrupts disabled.
1606 */
1607 static bool rcu_accelerate_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
1608 struct rcu_data *rdp)
1609 {
1610 unsigned long c;
1611 int i;
1612 bool ret;
1613
1614 /* If the CPU has no callbacks, nothing to do. */
1615 if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
1616 return false;
1617
1618 /*
1619 * Starting from the sublist containing the callbacks most
1620 * recently assigned a ->completed number and working down, find the
1621 * first sublist that is not assignable to an upcoming grace period.
1622 * Such a sublist has something in it (first two tests) and has
1623 * a ->completed number assigned that will complete sooner than
1624 * the ->completed number for newly arrived callbacks (last test).
1625 *
1626 * The key point is that any later sublist can be assigned the
1627 * same ->completed number as the newly arrived callbacks, which
1628 * means that the callbacks in any of these later sublist can be
1629 * grouped into a single sublist, whether or not they have already
1630 * been assigned a ->completed number.
1631 */
1632 c = rcu_cbs_completed(rsp, rnp);
1633 for (i = RCU_NEXT_TAIL - 1; i > RCU_DONE_TAIL; i--)
1634 if (rdp->nxttail[i] != rdp->nxttail[i - 1] &&
1635 !ULONG_CMP_GE(rdp->nxtcompleted[i], c))
1636 break;
1637
1638 /*
1639 * If there are no sublist for unassigned callbacks, leave.
1640 * At the same time, advance "i" one sublist, so that "i" will
1641 * index into the sublist where all the remaining callbacks should
1642 * be grouped into.
1643 */
1644 if (++i >= RCU_NEXT_TAIL)
1645 return false;
1646
1647 /*
1648 * Assign all subsequent callbacks' ->completed number to the next
1649 * full grace period and group them all in the sublist initially
1650 * indexed by "i".
1651 */
1652 for (; i <= RCU_NEXT_TAIL; i++) {
1653 rdp->nxttail[i] = rdp->nxttail[RCU_NEXT_TAIL];
1654 rdp->nxtcompleted[i] = c;
1655 }
1656 /* Record any needed additional grace periods. */
1657 ret = rcu_start_future_gp(rnp, rdp, NULL);
1658
1659 /* Trace depending on how much we were able to accelerate. */
1660 if (!*rdp->nxttail[RCU_WAIT_TAIL])
1661 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccWaitCB"));
1662 else
1663 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("AccReadyCB"));
1664 return ret;
1665 }
1666
1667 /*
1668 * Move any callbacks whose grace period has completed to the
1669 * RCU_DONE_TAIL sublist, then compact the remaining sublists and
1670 * assign ->completed numbers to any callbacks in the RCU_NEXT_TAIL
1671 * sublist. This function is idempotent, so it does not hurt to
1672 * invoke it repeatedly. As long as it is not invoked -too- often...
1673 * Returns true if the RCU grace-period kthread needs to be awakened.
1674 *
1675 * The caller must hold rnp->lock with interrupts disabled.
1676 */
1677 static bool rcu_advance_cbs(struct rcu_state *rsp, struct rcu_node *rnp,
1678 struct rcu_data *rdp)
1679 {
1680 int i, j;
1681
1682 /* If the CPU has no callbacks, nothing to do. */
1683 if (!rdp->nxttail[RCU_NEXT_TAIL] || !*rdp->nxttail[RCU_DONE_TAIL])
1684 return false;
1685
1686 /*
1687 * Find all callbacks whose ->completed numbers indicate that they
1688 * are ready to invoke, and put them into the RCU_DONE_TAIL sublist.
1689 */
1690 for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
1691 if (ULONG_CMP_LT(rnp->completed, rdp->nxtcompleted[i]))
1692 break;
1693 rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[i];
1694 }
1695 /* Clean up any sublist tail pointers that were misordered above. */
1696 for (j = RCU_WAIT_TAIL; j < i; j++)
1697 rdp->nxttail[j] = rdp->nxttail[RCU_DONE_TAIL];
1698
1699 /* Copy down callbacks to fill in empty sublists. */
1700 for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
1701 if (rdp->nxttail[j] == rdp->nxttail[RCU_NEXT_TAIL])
1702 break;
1703 rdp->nxttail[j] = rdp->nxttail[i];
1704 rdp->nxtcompleted[j] = rdp->nxtcompleted[i];
1705 }
1706
1707 /* Classify any remaining callbacks. */
1708 return rcu_accelerate_cbs(rsp, rnp, rdp);
1709 }
1710
1711 /*
1712 * Update CPU-local rcu_data state to record the beginnings and ends of
1713 * grace periods. The caller must hold the ->lock of the leaf rcu_node
1714 * structure corresponding to the current CPU, and must have irqs disabled.
1715 * Returns true if the grace-period kthread needs to be awakened.
1716 */
1717 static bool __note_gp_changes(struct rcu_state *rsp, struct rcu_node *rnp,
1718 struct rcu_data *rdp)
1719 {
1720 bool ret;
1721
1722 /* Handle the ends of any preceding grace periods first. */
1723 if (rdp->completed == rnp->completed &&
1724 !unlikely(READ_ONCE(rdp->gpwrap))) {
1725
1726 /* No grace period end, so just accelerate recent callbacks. */
1727 ret = rcu_accelerate_cbs(rsp, rnp, rdp);
1728
1729 } else {
1730
1731 /* Advance callbacks. */
1732 ret = rcu_advance_cbs(rsp, rnp, rdp);
1733
1734 /* Remember that we saw this grace-period completion. */
1735 rdp->completed = rnp->completed;
1736 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuend"));
1737 }
1738
1739 if (rdp->gpnum != rnp->gpnum || unlikely(READ_ONCE(rdp->gpwrap))) {
1740 /*
1741 * If the current grace period is waiting for this CPU,
1742 * set up to detect a quiescent state, otherwise don't
1743 * go looking for one.
1744 */
1745 rdp->gpnum = rnp->gpnum;
1746 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpustart"));
1747 rdp->passed_quiesce = 0;
1748 rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_qs_ctr);
1749 rdp->qs_pending = !!(rnp->qsmask & rdp->grpmask);
1750 zero_cpu_stall_ticks(rdp);
1751 WRITE_ONCE(rdp->gpwrap, false);
1752 }
1753 return ret;
1754 }
1755
1756 static void note_gp_changes(struct rcu_state *rsp, struct rcu_data *rdp)
1757 {
1758 unsigned long flags;
1759 bool needwake;
1760 struct rcu_node *rnp;
1761
1762 local_irq_save(flags);
1763 rnp = rdp->mynode;
1764 if ((rdp->gpnum == READ_ONCE(rnp->gpnum) &&
1765 rdp->completed == READ_ONCE(rnp->completed) &&
1766 !unlikely(READ_ONCE(rdp->gpwrap))) || /* w/out lock. */
1767 !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
1768 local_irq_restore(flags);
1769 return;
1770 }
1771 smp_mb__after_unlock_lock();
1772 needwake = __note_gp_changes(rsp, rnp, rdp);
1773 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1774 if (needwake)
1775 rcu_gp_kthread_wake(rsp);
1776 }
1777
1778 static void rcu_gp_slow(struct rcu_state *rsp, int delay)
1779 {
1780 if (delay > 0 &&
1781 !(rsp->gpnum % (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
1782 schedule_timeout_uninterruptible(delay);
1783 }
1784
1785 /*
1786 * Initialize a new grace period. Return 0 if no grace period required.
1787 */
1788 static int rcu_gp_init(struct rcu_state *rsp)
1789 {
1790 unsigned long oldmask;
1791 struct rcu_data *rdp;
1792 struct rcu_node *rnp = rcu_get_root(rsp);
1793
1794 WRITE_ONCE(rsp->gp_activity, jiffies);
1795 raw_spin_lock_irq(&rnp->lock);
1796 smp_mb__after_unlock_lock();
1797 if (!READ_ONCE(rsp->gp_flags)) {
1798 /* Spurious wakeup, tell caller to go back to sleep. */
1799 raw_spin_unlock_irq(&rnp->lock);
1800 return 0;
1801 }
1802 WRITE_ONCE(rsp->gp_flags, 0); /* Clear all flags: New grace period. */
1803
1804 if (WARN_ON_ONCE(rcu_gp_in_progress(rsp))) {
1805 /*
1806 * Grace period already in progress, don't start another.
1807 * Not supposed to be able to happen.
1808 */
1809 raw_spin_unlock_irq(&rnp->lock);
1810 return 0;
1811 }
1812
1813 /* Advance to a new grace period and initialize state. */
1814 record_gp_stall_check_time(rsp);
1815 /* Record GP times before starting GP, hence smp_store_release(). */
1816 smp_store_release(&rsp->gpnum, rsp->gpnum + 1);
1817 trace_rcu_grace_period(rsp->name, rsp->gpnum, TPS("start"));
1818 raw_spin_unlock_irq(&rnp->lock);
1819
1820 /*
1821 * Apply per-leaf buffered online and offline operations to the
1822 * rcu_node tree. Note that this new grace period need not wait
1823 * for subsequent online CPUs, and that quiescent-state forcing
1824 * will handle subsequent offline CPUs.
1825 */
1826 rcu_for_each_leaf_node(rsp, rnp) {
1827 rcu_gp_slow(rsp, gp_preinit_delay);
1828 raw_spin_lock_irq(&rnp->lock);
1829 smp_mb__after_unlock_lock();
1830 if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
1831 !rnp->wait_blkd_tasks) {
1832 /* Nothing to do on this leaf rcu_node structure. */
1833 raw_spin_unlock_irq(&rnp->lock);
1834 continue;
1835 }
1836
1837 /* Record old state, apply changes to ->qsmaskinit field. */
1838 oldmask = rnp->qsmaskinit;
1839 rnp->qsmaskinit = rnp->qsmaskinitnext;
1840
1841 /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
1842 if (!oldmask != !rnp->qsmaskinit) {
1843 if (!oldmask) /* First online CPU for this rcu_node. */
1844 rcu_init_new_rnp(rnp);
1845 else if (rcu_preempt_has_tasks(rnp)) /* blocked tasks */
1846 rnp->wait_blkd_tasks = true;
1847 else /* Last offline CPU and can propagate. */
1848 rcu_cleanup_dead_rnp(rnp);
1849 }
1850
1851 /*
1852 * If all waited-on tasks from prior grace period are
1853 * done, and if all this rcu_node structure's CPUs are
1854 * still offline, propagate up the rcu_node tree and
1855 * clear ->wait_blkd_tasks. Otherwise, if one of this
1856 * rcu_node structure's CPUs has since come back online,
1857 * simply clear ->wait_blkd_tasks (but rcu_cleanup_dead_rnp()
1858 * checks for this, so just call it unconditionally).
1859 */
1860 if (rnp->wait_blkd_tasks &&
1861 (!rcu_preempt_has_tasks(rnp) ||
1862 rnp->qsmaskinit)) {
1863 rnp->wait_blkd_tasks = false;
1864 rcu_cleanup_dead_rnp(rnp);
1865 }
1866
1867 raw_spin_unlock_irq(&rnp->lock);
1868 }
1869
1870 /*
1871 * Set the quiescent-state-needed bits in all the rcu_node
1872 * structures for all currently online CPUs in breadth-first order,
1873 * starting from the root rcu_node structure, relying on the layout
1874 * of the tree within the rsp->node[] array. Note that other CPUs
1875 * will access only the leaves of the hierarchy, thus seeing that no
1876 * grace period is in progress, at least until the corresponding
1877 * leaf node has been initialized. In addition, we have excluded
1878 * CPU-hotplug operations.
1879 *
1880 * The grace period cannot complete until the initialization
1881 * process finishes, because this kthread handles both.
1882 */
1883 rcu_for_each_node_breadth_first(rsp, rnp) {
1884 rcu_gp_slow(rsp, gp_init_delay);
1885 raw_spin_lock_irq(&rnp->lock);
1886 smp_mb__after_unlock_lock();
1887 rdp = this_cpu_ptr(rsp->rda);
1888 rcu_preempt_check_blocked_tasks(rnp);
1889 rnp->qsmask = rnp->qsmaskinit;
1890 WRITE_ONCE(rnp->gpnum, rsp->gpnum);
1891 if (WARN_ON_ONCE(rnp->completed != rsp->completed))
1892 WRITE_ONCE(rnp->completed, rsp->completed);
1893 if (rnp == rdp->mynode)
1894 (void)__note_gp_changes(rsp, rnp, rdp);
1895 rcu_preempt_boost_start_gp(rnp);
1896 trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
1897 rnp->level, rnp->grplo,
1898 rnp->grphi, rnp->qsmask);
1899 raw_spin_unlock_irq(&rnp->lock);
1900 cond_resched_rcu_qs();
1901 WRITE_ONCE(rsp->gp_activity, jiffies);
1902 }
1903
1904 return 1;
1905 }
1906
1907 /*
1908 * Helper function for wait_event_interruptible_timeout() wakeup
1909 * at force-quiescent-state time.
1910 */
1911 static bool rcu_gp_fqs_check_wake(struct rcu_state *rsp, int *gfp)
1912 {
1913 struct rcu_node *rnp = rcu_get_root(rsp);
1914
1915 /* Someone like call_rcu() requested a force-quiescent-state scan. */
1916 *gfp = READ_ONCE(rsp->gp_flags);
1917 if (*gfp & RCU_GP_FLAG_FQS)
1918 return true;
1919
1920 /* The current grace period has completed. */
1921 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
1922 return true;
1923
1924 return false;
1925 }
1926
1927 /*
1928 * Do one round of quiescent-state forcing.
1929 */
1930 static int rcu_gp_fqs(struct rcu_state *rsp, int fqs_state_in)
1931 {
1932 int fqs_state = fqs_state_in;
1933 bool isidle = false;
1934 unsigned long maxj;
1935 struct rcu_node *rnp = rcu_get_root(rsp);
1936
1937 WRITE_ONCE(rsp->gp_activity, jiffies);
1938 rsp->n_force_qs++;
1939 if (fqs_state == RCU_SAVE_DYNTICK) {
1940 /* Collect dyntick-idle snapshots. */
1941 if (is_sysidle_rcu_state(rsp)) {
1942 isidle = true;
1943 maxj = jiffies - ULONG_MAX / 4;
1944 }
1945 force_qs_rnp(rsp, dyntick_save_progress_counter,
1946 &isidle, &maxj);
1947 rcu_sysidle_report_gp(rsp, isidle, maxj);
1948 fqs_state = RCU_FORCE_QS;
1949 } else {
1950 /* Handle dyntick-idle and offline CPUs. */
1951 isidle = true;
1952 force_qs_rnp(rsp, rcu_implicit_dynticks_qs, &isidle, &maxj);
1953 }
1954 /* Clear flag to prevent immediate re-entry. */
1955 if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
1956 raw_spin_lock_irq(&rnp->lock);
1957 smp_mb__after_unlock_lock();
1958 WRITE_ONCE(rsp->gp_flags,
1959 READ_ONCE(rsp->gp_flags) & ~RCU_GP_FLAG_FQS);
1960 raw_spin_unlock_irq(&rnp->lock);
1961 }
1962 return fqs_state;
1963 }
1964
1965 /*
1966 * Clean up after the old grace period.
1967 */
1968 static void rcu_gp_cleanup(struct rcu_state *rsp)
1969 {
1970 unsigned long gp_duration;
1971 bool needgp = false;
1972 int nocb = 0;
1973 struct rcu_data *rdp;
1974 struct rcu_node *rnp = rcu_get_root(rsp);
1975
1976 WRITE_ONCE(rsp->gp_activity, jiffies);
1977 raw_spin_lock_irq(&rnp->lock);
1978 smp_mb__after_unlock_lock();
1979 gp_duration = jiffies - rsp->gp_start;
1980 if (gp_duration > rsp->gp_max)
1981 rsp->gp_max = gp_duration;
1982
1983 /*
1984 * We know the grace period is complete, but to everyone else
1985 * it appears to still be ongoing. But it is also the case
1986 * that to everyone else it looks like there is nothing that
1987 * they can do to advance the grace period. It is therefore
1988 * safe for us to drop the lock in order to mark the grace
1989 * period as completed in all of the rcu_node structures.
1990 */
1991 raw_spin_unlock_irq(&rnp->lock);
1992
1993 /*
1994 * Propagate new ->completed value to rcu_node structures so
1995 * that other CPUs don't have to wait until the start of the next
1996 * grace period to process their callbacks. This also avoids
1997 * some nasty RCU grace-period initialization races by forcing
1998 * the end of the current grace period to be completely recorded in
1999 * all of the rcu_node structures before the beginning of the next
2000 * grace period is recorded in any of the rcu_node structures.
2001 */
2002 rcu_for_each_node_breadth_first(rsp, rnp) {
2003 raw_spin_lock_irq(&rnp->lock);
2004 smp_mb__after_unlock_lock();
2005 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
2006 WARN_ON_ONCE(rnp->qsmask);
2007 WRITE_ONCE(rnp->completed, rsp->gpnum);
2008 rdp = this_cpu_ptr(rsp->rda);
2009 if (rnp == rdp->mynode)
2010 needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
2011 /* smp_mb() provided by prior unlock-lock pair. */
2012 nocb += rcu_future_gp_cleanup(rsp, rnp);
2013 raw_spin_unlock_irq(&rnp->lock);
2014 cond_resched_rcu_qs();
2015 WRITE_ONCE(rsp->gp_activity, jiffies);
2016 rcu_gp_slow(rsp, gp_cleanup_delay);
2017 }
2018 rnp = rcu_get_root(rsp);
2019 raw_spin_lock_irq(&rnp->lock);
2020 smp_mb__after_unlock_lock(); /* Order GP before ->completed update. */
2021 rcu_nocb_gp_set(rnp, nocb);
2022
2023 /* Declare grace period done. */
2024 WRITE_ONCE(rsp->completed, rsp->gpnum);
2025 trace_rcu_grace_period(rsp->name, rsp->completed, TPS("end"));
2026 rsp->fqs_state = RCU_GP_IDLE;
2027 rdp = this_cpu_ptr(rsp->rda);
2028 /* Advance CBs to reduce false positives below. */
2029 needgp = rcu_advance_cbs(rsp, rnp, rdp) || needgp;
2030 if (needgp || cpu_needs_another_gp(rsp, rdp)) {
2031 WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
2032 trace_rcu_grace_period(rsp->name,
2033 READ_ONCE(rsp->gpnum),
2034 TPS("newreq"));
2035 }
2036 raw_spin_unlock_irq(&rnp->lock);
2037 }
2038
2039 /*
2040 * Body of kthread that handles grace periods.
2041 */
2042 static int __noreturn rcu_gp_kthread(void *arg)
2043 {
2044 int fqs_state;
2045 int gf;
2046 unsigned long j;
2047 int ret;
2048 struct rcu_state *rsp = arg;
2049 struct rcu_node *rnp = rcu_get_root(rsp);
2050
2051 rcu_bind_gp_kthread();
2052 for (;;) {
2053
2054 /* Handle grace-period start. */
2055 for (;;) {
2056 trace_rcu_grace_period(rsp->name,
2057 READ_ONCE(rsp->gpnum),
2058 TPS("reqwait"));
2059 rsp->gp_state = RCU_GP_WAIT_GPS;
2060 wait_event_interruptible(rsp->gp_wq,
2061 READ_ONCE(rsp->gp_flags) &
2062 RCU_GP_FLAG_INIT);
2063 rsp->gp_state = RCU_GP_DONE_GPS;
2064 /* Locking provides needed memory barrier. */
2065 if (rcu_gp_init(rsp))
2066 break;
2067 cond_resched_rcu_qs();
2068 WRITE_ONCE(rsp->gp_activity, jiffies);
2069 WARN_ON(signal_pending(current));
2070 trace_rcu_grace_period(rsp->name,
2071 READ_ONCE(rsp->gpnum),
2072 TPS("reqwaitsig"));
2073 }
2074
2075 /* Handle quiescent-state forcing. */
2076 fqs_state = RCU_SAVE_DYNTICK;
2077 j = jiffies_till_first_fqs;
2078 if (j > HZ) {
2079 j = HZ;
2080 jiffies_till_first_fqs = HZ;
2081 }
2082 ret = 0;
2083 for (;;) {
2084 if (!ret)
2085 rsp->jiffies_force_qs = jiffies + j;
2086 trace_rcu_grace_period(rsp->name,
2087 READ_ONCE(rsp->gpnum),
2088 TPS("fqswait"));
2089 rsp->gp_state = RCU_GP_WAIT_FQS;
2090 ret = wait_event_interruptible_timeout(rsp->gp_wq,
2091 rcu_gp_fqs_check_wake(rsp, &gf), j);
2092 rsp->gp_state = RCU_GP_DOING_FQS;
2093 /* Locking provides needed memory barriers. */
2094 /* If grace period done, leave loop. */
2095 if (!READ_ONCE(rnp->qsmask) &&
2096 !rcu_preempt_blocked_readers_cgp(rnp))
2097 break;
2098 /* If time for quiescent-state forcing, do it. */
2099 if (ULONG_CMP_GE(jiffies, rsp->jiffies_force_qs) ||
2100 (gf & RCU_GP_FLAG_FQS)) {
2101 trace_rcu_grace_period(rsp->name,
2102 READ_ONCE(rsp->gpnum),
2103 TPS("fqsstart"));
2104 fqs_state = rcu_gp_fqs(rsp, fqs_state);
2105 trace_rcu_grace_period(rsp->name,
2106 READ_ONCE(rsp->gpnum),
2107 TPS("fqsend"));
2108 cond_resched_rcu_qs();
2109 WRITE_ONCE(rsp->gp_activity, jiffies);
2110 } else {
2111 /* Deal with stray signal. */
2112 cond_resched_rcu_qs();
2113 WRITE_ONCE(rsp->gp_activity, jiffies);
2114 WARN_ON(signal_pending(current));
2115 trace_rcu_grace_period(rsp->name,
2116 READ_ONCE(rsp->gpnum),
2117 TPS("fqswaitsig"));
2118 }
2119 j = jiffies_till_next_fqs;
2120 if (j > HZ) {
2121 j = HZ;
2122 jiffies_till_next_fqs = HZ;
2123 } else if (j < 1) {
2124 j = 1;
2125 jiffies_till_next_fqs = 1;
2126 }
2127 }
2128
2129 /* Handle grace-period end. */
2130 rsp->gp_state = RCU_GP_CLEANUP;
2131 rcu_gp_cleanup(rsp);
2132 rsp->gp_state = RCU_GP_CLEANED;
2133 }
2134 }
2135
2136 /*
2137 * Start a new RCU grace period if warranted, re-initializing the hierarchy
2138 * in preparation for detecting the next grace period. The caller must hold
2139 * the root node's ->lock and hard irqs must be disabled.
2140 *
2141 * Note that it is legal for a dying CPU (which is marked as offline) to
2142 * invoke this function. This can happen when the dying CPU reports its
2143 * quiescent state.
2144 *
2145 * Returns true if the grace-period kthread must be awakened.
2146 */
2147 static bool
2148 rcu_start_gp_advanced(struct rcu_state *rsp, struct rcu_node *rnp,
2149 struct rcu_data *rdp)
2150 {
2151 if (!rsp->gp_kthread || !cpu_needs_another_gp(rsp, rdp)) {
2152 /*
2153 * Either we have not yet spawned the grace-period
2154 * task, this CPU does not need another grace period,
2155 * or a grace period is already in progress.
2156 * Either way, don't start a new grace period.
2157 */
2158 return false;
2159 }
2160 WRITE_ONCE(rsp->gp_flags, RCU_GP_FLAG_INIT);
2161 trace_rcu_grace_period(rsp->name, READ_ONCE(rsp->gpnum),
2162 TPS("newreq"));
2163
2164 /*
2165 * We can't do wakeups while holding the rnp->lock, as that
2166 * could cause possible deadlocks with the rq->lock. Defer
2167 * the wakeup to our caller.
2168 */
2169 return true;
2170 }
2171
2172 /*
2173 * Similar to rcu_start_gp_advanced(), but also advance the calling CPU's
2174 * callbacks. Note that rcu_start_gp_advanced() cannot do this because it
2175 * is invoked indirectly from rcu_advance_cbs(), which would result in
2176 * endless recursion -- or would do so if it wasn't for the self-deadlock
2177 * that is encountered beforehand.
2178 *
2179 * Returns true if the grace-period kthread needs to be awakened.
2180 */
2181 static bool rcu_start_gp(struct rcu_state *rsp)
2182 {
2183 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
2184 struct rcu_node *rnp = rcu_get_root(rsp);
2185 bool ret = false;
2186
2187 /*
2188 * If there is no grace period in progress right now, any
2189 * callbacks we have up to this point will be satisfied by the
2190 * next grace period. Also, advancing the callbacks reduces the
2191 * probability of false positives from cpu_needs_another_gp()
2192 * resulting in pointless grace periods. So, advance callbacks
2193 * then start the grace period!
2194 */
2195 ret = rcu_advance_cbs(rsp, rnp, rdp) || ret;
2196 ret = rcu_start_gp_advanced(rsp, rnp, rdp) || ret;
2197 return ret;
2198 }
2199
2200 /*
2201 * Report a full set of quiescent states to the specified rcu_state
2202 * data structure. This involves cleaning up after the prior grace
2203 * period and letting rcu_start_gp() start up the next grace period
2204 * if one is needed. Note that the caller must hold rnp->lock, which
2205 * is released before return.
2206 */
2207 static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
2208 __releases(rcu_get_root(rsp)->lock)
2209 {
2210 WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
2211 WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
2212 raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
2213 rcu_gp_kthread_wake(rsp);
2214 }
2215
2216 /*
2217 * Similar to rcu_report_qs_rdp(), for which it is a helper function.
2218 * Allows quiescent states for a group of CPUs to be reported at one go
2219 * to the specified rcu_node structure, though all the CPUs in the group
2220 * must be represented by the same rcu_node structure (which need not be a
2221 * leaf rcu_node structure, though it often will be). The gps parameter
2222 * is the grace-period snapshot, which means that the quiescent states
2223 * are valid only if rnp->gpnum is equal to gps. That structure's lock
2224 * must be held upon entry, and it is released before return.
2225 */
2226 static void
2227 rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
2228 struct rcu_node *rnp, unsigned long gps, unsigned long flags)
2229 __releases(rnp->lock)
2230 {
2231 unsigned long oldmask = 0;
2232 struct rcu_node *rnp_c;
2233
2234 /* Walk up the rcu_node hierarchy. */
2235 for (;;) {
2236 if (!(rnp->qsmask & mask) || rnp->gpnum != gps) {
2237
2238 /*
2239 * Our bit has already been cleared, or the
2240 * relevant grace period is already over, so done.
2241 */
2242 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2243 return;
2244 }
2245 WARN_ON_ONCE(oldmask); /* Any child must be all zeroed! */
2246 rnp->qsmask &= ~mask;
2247 trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
2248 mask, rnp->qsmask, rnp->level,
2249 rnp->grplo, rnp->grphi,
2250 !!rnp->gp_tasks);
2251 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
2252
2253 /* Other bits still set at this level, so done. */
2254 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2255 return;
2256 }
2257 mask = rnp->grpmask;
2258 if (rnp->parent == NULL) {
2259
2260 /* No more levels. Exit loop holding root lock. */
2261
2262 break;
2263 }
2264 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2265 rnp_c = rnp;
2266 rnp = rnp->parent;
2267 raw_spin_lock_irqsave(&rnp->lock, flags);
2268 smp_mb__after_unlock_lock();
2269 oldmask = rnp_c->qsmask;
2270 }
2271
2272 /*
2273 * Get here if we are the last CPU to pass through a quiescent
2274 * state for this grace period. Invoke rcu_report_qs_rsp()
2275 * to clean up and start the next grace period if one is needed.
2276 */
2277 rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
2278 }
2279
2280 /*
2281 * Record a quiescent state for all tasks that were previously queued
2282 * on the specified rcu_node structure and that were blocking the current
2283 * RCU grace period. The caller must hold the specified rnp->lock with
2284 * irqs disabled, and this lock is released upon return, but irqs remain
2285 * disabled.
2286 */
2287 static void rcu_report_unblock_qs_rnp(struct rcu_state *rsp,
2288 struct rcu_node *rnp, unsigned long flags)
2289 __releases(rnp->lock)
2290 {
2291 unsigned long gps;
2292 unsigned long mask;
2293 struct rcu_node *rnp_p;
2294
2295 if (rcu_state_p == &rcu_sched_state || rsp != rcu_state_p ||
2296 rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
2297 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2298 return; /* Still need more quiescent states! */
2299 }
2300
2301 rnp_p = rnp->parent;
2302 if (rnp_p == NULL) {
2303 /*
2304 * Only one rcu_node structure in the tree, so don't
2305 * try to report up to its nonexistent parent!
2306 */
2307 rcu_report_qs_rsp(rsp, flags);
2308 return;
2309 }
2310
2311 /* Report up the rest of the hierarchy, tracking current ->gpnum. */
2312 gps = rnp->gpnum;
2313 mask = rnp->grpmask;
2314 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
2315 raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
2316 smp_mb__after_unlock_lock();
2317 rcu_report_qs_rnp(mask, rsp, rnp_p, gps, flags);
2318 }
2319
2320 /*
2321 * Record a quiescent state for the specified CPU to that CPU's rcu_data
2322 * structure. This must be either called from the specified CPU, or
2323 * called when the specified CPU is known to be offline (and when it is
2324 * also known that no other CPU is concurrently trying to help the offline
2325 * CPU). The lastcomp argument is used to make sure we are still in the
2326 * grace period of interest. We don't want to end the current grace period
2327 * based on quiescent states detected in an earlier grace period!
2328 */
2329 static void
2330 rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp)
2331 {
2332 unsigned long flags;
2333 unsigned long mask;
2334 bool needwake;
2335 struct rcu_node *rnp;
2336
2337 rnp = rdp->mynode;
2338 raw_spin_lock_irqsave(&rnp->lock, flags);
2339 smp_mb__after_unlock_lock();
2340 if ((rdp->passed_quiesce == 0 &&
2341 rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr)) ||
2342 rdp->gpnum != rnp->gpnum || rnp->completed == rnp->gpnum ||
2343 rdp->gpwrap) {
2344
2345 /*
2346 * The grace period in which this quiescent state was
2347 * recorded has ended, so don't report it upwards.
2348 * We will instead need a new quiescent state that lies
2349 * within the current grace period.
2350 */
2351 rdp->passed_quiesce = 0; /* need qs for new gp. */
2352 rdp->rcu_qs_ctr_snap = __this_cpu_read(rcu_qs_ctr);
2353 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2354 return;
2355 }
2356 mask = rdp->grpmask;
2357 if ((rnp->qsmask & mask) == 0) {
2358 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2359 } else {
2360 rdp->qs_pending = 0;
2361
2362 /*
2363 * This GP can't end until cpu checks in, so all of our
2364 * callbacks can be processed during the next GP.
2365 */
2366 needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
2367
2368 rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
2369 /* ^^^ Released rnp->lock */
2370 if (needwake)
2371 rcu_gp_kthread_wake(rsp);
2372 }
2373 }
2374
2375 /*
2376 * Check to see if there is a new grace period of which this CPU
2377 * is not yet aware, and if so, set up local rcu_data state for it.
2378 * Otherwise, see if this CPU has just passed through its first
2379 * quiescent state for this grace period, and record that fact if so.
2380 */
2381 static void
2382 rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
2383 {
2384 /* Check for grace-period ends and beginnings. */
2385 note_gp_changes(rsp, rdp);
2386
2387 /*
2388 * Does this CPU still need to do its part for current grace period?
2389 * If no, return and let the other CPUs do their part as well.
2390 */
2391 if (!rdp->qs_pending)
2392 return;
2393
2394 /*
2395 * Was there a quiescent state since the beginning of the grace
2396 * period? If no, then exit and wait for the next call.
2397 */
2398 if (!rdp->passed_quiesce &&
2399 rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr))
2400 return;
2401
2402 /*
2403 * Tell RCU we are done (but rcu_report_qs_rdp() will be the
2404 * judge of that).
2405 */
2406 rcu_report_qs_rdp(rdp->cpu, rsp, rdp);
2407 }
2408
2409 /*
2410 * Send the specified CPU's RCU callbacks to the orphanage. The
2411 * specified CPU must be offline, and the caller must hold the
2412 * ->orphan_lock.
2413 */
2414 static void
2415 rcu_send_cbs_to_orphanage(int cpu, struct rcu_state *rsp,
2416 struct rcu_node *rnp, struct rcu_data *rdp)
2417 {
2418 /* No-CBs CPUs do not have orphanable callbacks. */
2419 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) || rcu_is_nocb_cpu(rdp->cpu))
2420 return;
2421
2422 /*
2423 * Orphan the callbacks. First adjust the counts. This is safe
2424 * because _rcu_barrier() excludes CPU-hotplug operations, so it
2425 * cannot be running now. Thus no memory barrier is required.
2426 */
2427 if (rdp->nxtlist != NULL) {
2428 rsp->qlen_lazy += rdp->qlen_lazy;
2429 rsp->qlen += rdp->qlen;
2430 rdp->n_cbs_orphaned += rdp->qlen;
2431 rdp->qlen_lazy = 0;
2432 WRITE_ONCE(rdp->qlen, 0);
2433 }
2434
2435 /*
2436 * Next, move those callbacks still needing a grace period to
2437 * the orphanage, where some other CPU will pick them up.
2438 * Some of the callbacks might have gone partway through a grace
2439 * period, but that is too bad. They get to start over because we
2440 * cannot assume that grace periods are synchronized across CPUs.
2441 * We don't bother updating the ->nxttail[] array yet, instead
2442 * we just reset the whole thing later on.
2443 */
2444 if (*rdp->nxttail[RCU_DONE_TAIL] != NULL) {
2445 *rsp->orphan_nxttail = *rdp->nxttail[RCU_DONE_TAIL];
2446 rsp->orphan_nxttail = rdp->nxttail[RCU_NEXT_TAIL];
2447 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
2448 }
2449
2450 /*
2451 * Then move the ready-to-invoke callbacks to the orphanage,
2452 * where some other CPU will pick them up. These will not be
2453 * required to pass though another grace period: They are done.
2454 */
2455 if (rdp->nxtlist != NULL) {
2456 *rsp->orphan_donetail = rdp->nxtlist;
2457 rsp->orphan_donetail = rdp->nxttail[RCU_DONE_TAIL];
2458 }
2459
2460 /*
2461 * Finally, initialize the rcu_data structure's list to empty and
2462 * disallow further callbacks on this CPU.
2463 */
2464 init_callback_list(rdp);
2465 rdp->nxttail[RCU_NEXT_TAIL] = NULL;
2466 }
2467
2468 /*
2469 * Adopt the RCU callbacks from the specified rcu_state structure's
2470 * orphanage. The caller must hold the ->orphan_lock.
2471 */
2472 static void rcu_adopt_orphan_cbs(struct rcu_state *rsp, unsigned long flags)
2473 {
2474 int i;
2475 struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
2476
2477 /* No-CBs CPUs are handled specially. */
2478 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
2479 rcu_nocb_adopt_orphan_cbs(rsp, rdp, flags))
2480 return;
2481
2482 /* Do the accounting first. */
2483 rdp->qlen_lazy += rsp->qlen_lazy;
2484 rdp->qlen += rsp->qlen;
2485 rdp->n_cbs_adopted += rsp->qlen;
2486 if (rsp->qlen_lazy != rsp->qlen)
2487 rcu_idle_count_callbacks_posted();
2488 rsp->qlen_lazy = 0;
2489 rsp->qlen = 0;
2490
2491 /*
2492 * We do not need a memory barrier here because the only way we
2493 * can get here if there is an rcu_barrier() in flight is if
2494 * we are the task doing the rcu_barrier().
2495 */
2496
2497 /* First adopt the ready-to-invoke callbacks. */
2498 if (rsp->orphan_donelist != NULL) {
2499 *rsp->orphan_donetail = *rdp->nxttail[RCU_DONE_TAIL];
2500 *rdp->nxttail[RCU_DONE_TAIL] = rsp->orphan_donelist;
2501 for (i = RCU_NEXT_SIZE - 1; i >= RCU_DONE_TAIL; i--)
2502 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
2503 rdp->nxttail[i] = rsp->orphan_donetail;
2504 rsp->orphan_donelist = NULL;
2505 rsp->orphan_donetail = &rsp->orphan_donelist;
2506 }
2507
2508 /* And then adopt the callbacks that still need a grace period. */
2509 if (rsp->orphan_nxtlist != NULL) {
2510 *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxtlist;
2511 rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxttail;
2512 rsp->orphan_nxtlist = NULL;
2513 rsp->orphan_nxttail = &rsp->orphan_nxtlist;
2514 }
2515 }
2516
2517 /*
2518 * Trace the fact that this CPU is going offline.
2519 */
2520 static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
2521 {
2522 RCU_TRACE(unsigned long mask);
2523 RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda));
2524 RCU_TRACE(struct rcu_node *rnp = rdp->mynode);
2525
2526 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2527 return;
2528
2529 RCU_TRACE(mask = rdp->grpmask);
2530 trace_rcu_grace_period(rsp->name,
2531 rnp->gpnum + 1 - !!(rnp->qsmask & mask),
2532 TPS("cpuofl"));
2533 }
2534
2535 /*
2536 * All CPUs for the specified rcu_node structure have gone offline,
2537 * and all tasks that were preempted within an RCU read-side critical
2538 * section while running on one of those CPUs have since exited their RCU
2539 * read-side critical section. Some other CPU is reporting this fact with
2540 * the specified rcu_node structure's ->lock held and interrupts disabled.
2541 * This function therefore goes up the tree of rcu_node structures,
2542 * clearing the corresponding bits in the ->qsmaskinit fields. Note that
2543 * the leaf rcu_node structure's ->qsmaskinit field has already been
2544 * updated
2545 *
2546 * This function does check that the specified rcu_node structure has
2547 * all CPUs offline and no blocked tasks, so it is OK to invoke it
2548 * prematurely. That said, invoking it after the fact will cost you
2549 * a needless lock acquisition. So once it has done its work, don't
2550 * invoke it again.
2551 */
2552 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
2553 {
2554 long mask;
2555 struct rcu_node *rnp = rnp_leaf;
2556
2557 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
2558 rnp->qsmaskinit || rcu_preempt_has_tasks(rnp))
2559 return;
2560 for (;;) {
2561 mask = rnp->grpmask;
2562 rnp = rnp->parent;
2563 if (!rnp)
2564 break;
2565 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
2566 smp_mb__after_unlock_lock(); /* GP memory ordering. */
2567 rnp->qsmaskinit &= ~mask;
2568 rnp->qsmask &= ~mask;
2569 if (rnp->qsmaskinit) {
2570 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
2571 return;
2572 }
2573 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
2574 }
2575 }
2576
2577 /*
2578 * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
2579 * function. We now remove it from the rcu_node tree's ->qsmaskinit
2580 * bit masks.
2581 */
2582 static void rcu_cleanup_dying_idle_cpu(int cpu, struct rcu_state *rsp)
2583 {
2584 unsigned long flags;
2585 unsigned long mask;
2586 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
2587 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
2588
2589 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2590 return;
2591
2592 /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
2593 mask = rdp->grpmask;
2594 raw_spin_lock_irqsave(&rnp->lock, flags);
2595 smp_mb__after_unlock_lock(); /* Enforce GP memory-order guarantee. */
2596 rnp->qsmaskinitnext &= ~mask;
2597 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2598 }
2599
2600 /*
2601 * The CPU has been completely removed, and some other CPU is reporting
2602 * this fact from process context. Do the remainder of the cleanup,
2603 * including orphaning the outgoing CPU's RCU callbacks, and also
2604 * adopting them. There can only be one CPU hotplug operation at a time,
2605 * so no other CPU can be attempting to update rcu_cpu_kthread_task.
2606 */
2607 static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
2608 {
2609 unsigned long flags;
2610 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
2611 struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */
2612
2613 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2614 return;
2615
2616 /* Adjust any no-longer-needed kthreads. */
2617 rcu_boost_kthread_setaffinity(rnp, -1);
2618
2619 /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */
2620 raw_spin_lock_irqsave(&rsp->orphan_lock, flags);
2621 rcu_send_cbs_to_orphanage(cpu, rsp, rnp, rdp);
2622 rcu_adopt_orphan_cbs(rsp, flags);
2623 raw_spin_unlock_irqrestore(&rsp->orphan_lock, flags);
2624
2625 WARN_ONCE(rdp->qlen != 0 || rdp->nxtlist != NULL,
2626 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n",
2627 cpu, rdp->qlen, rdp->nxtlist);
2628 }
2629
2630 /*
2631 * Invoke any RCU callbacks that have made it to the end of their grace
2632 * period. Thottle as specified by rdp->blimit.
2633 */
2634 static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
2635 {
2636 unsigned long flags;
2637 struct rcu_head *next, *list, **tail;
2638 long bl, count, count_lazy;
2639 int i;
2640
2641 /* If no callbacks are ready, just return. */
2642 if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
2643 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0);
2644 trace_rcu_batch_end(rsp->name, 0, !!READ_ONCE(rdp->nxtlist),
2645 need_resched(), is_idle_task(current),
2646 rcu_is_callbacks_kthread());
2647 return;
2648 }
2649
2650 /*
2651 * Extract the list of ready callbacks, disabling to prevent
2652 * races with call_rcu() from interrupt handlers.
2653 */
2654 local_irq_save(flags);
2655 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
2656 bl = rdp->blimit;
2657 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl);
2658 list = rdp->nxtlist;
2659 rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
2660 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
2661 tail = rdp->nxttail[RCU_DONE_TAIL];
2662 for (i = RCU_NEXT_SIZE - 1; i >= 0; i--)
2663 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
2664 rdp->nxttail[i] = &rdp->nxtlist;
2665 local_irq_restore(flags);
2666
2667 /* Invoke callbacks. */
2668 count = count_lazy = 0;
2669 while (list) {
2670 next = list->next;
2671 prefetch(next);
2672 debug_rcu_head_unqueue(list);
2673 if (__rcu_reclaim(rsp->name, list))
2674 count_lazy++;
2675 list = next;
2676 /* Stop only if limit reached and CPU has something to do. */
2677 if (++count >= bl &&
2678 (need_resched() ||
2679 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
2680 break;
2681 }
2682
2683 local_irq_save(flags);
2684 trace_rcu_batch_end(rsp->name, count, !!list, need_resched(),
2685 is_idle_task(current),
2686 rcu_is_callbacks_kthread());
2687
2688 /* Update count, and requeue any remaining callbacks. */
2689 if (list != NULL) {
2690 *tail = rdp->nxtlist;
2691 rdp->nxtlist = list;
2692 for (i = 0; i < RCU_NEXT_SIZE; i++)
2693 if (&rdp->nxtlist == rdp->nxttail[i])
2694 rdp->nxttail[i] = tail;
2695 else
2696 break;
2697 }
2698 smp_mb(); /* List handling before counting for rcu_barrier(). */
2699 rdp->qlen_lazy -= count_lazy;
2700 WRITE_ONCE(rdp->qlen, rdp->qlen - count);
2701 rdp->n_cbs_invoked += count;
2702
2703 /* Reinstate batch limit if we have worked down the excess. */
2704 if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
2705 rdp->blimit = blimit;
2706
2707 /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
2708 if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
2709 rdp->qlen_last_fqs_check = 0;
2710 rdp->n_force_qs_snap = rsp->n_force_qs;
2711 } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
2712 rdp->qlen_last_fqs_check = rdp->qlen;
2713 WARN_ON_ONCE((rdp->nxtlist == NULL) != (rdp->qlen == 0));
2714
2715 local_irq_restore(flags);
2716
2717 /* Re-invoke RCU core processing if there are callbacks remaining. */
2718 if (cpu_has_callbacks_ready_to_invoke(rdp))
2719 invoke_rcu_core();
2720 }
2721
2722 /*
2723 * Check to see if this CPU is in a non-context-switch quiescent state
2724 * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
2725 * Also schedule RCU core processing.
2726 *
2727 * This function must be called from hardirq context. It is normally
2728 * invoked from the scheduling-clock interrupt. If rcu_pending returns
2729 * false, there is no point in invoking rcu_check_callbacks().
2730 */
2731 void rcu_check_callbacks(int user)
2732 {
2733 trace_rcu_utilization(TPS("Start scheduler-tick"));
2734 increment_cpu_stall_ticks();
2735 if (user || rcu_is_cpu_rrupt_from_idle()) {
2736
2737 /*
2738 * Get here if this CPU took its interrupt from user
2739 * mode or from the idle loop, and if this is not a
2740 * nested interrupt. In this case, the CPU is in
2741 * a quiescent state, so note it.
2742 *
2743 * No memory barrier is required here because both
2744 * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
2745 * variables that other CPUs neither access nor modify,
2746 * at least not while the corresponding CPU is online.
2747 */
2748
2749 rcu_sched_qs();
2750 rcu_bh_qs();
2751
2752 } else if (!in_softirq()) {
2753
2754 /*
2755 * Get here if this CPU did not take its interrupt from
2756 * softirq, in other words, if it is not interrupting
2757 * a rcu_bh read-side critical section. This is an _bh
2758 * critical section, so note it.
2759 */
2760
2761 rcu_bh_qs();
2762 }
2763 rcu_preempt_check_callbacks();
2764 if (rcu_pending())
2765 invoke_rcu_core();
2766 if (user)
2767 rcu_note_voluntary_context_switch(current);
2768 trace_rcu_utilization(TPS("End scheduler-tick"));
2769 }
2770
2771 /*
2772 * Scan the leaf rcu_node structures, processing dyntick state for any that
2773 * have not yet encountered a quiescent state, using the function specified.
2774 * Also initiate boosting for any threads blocked on the root rcu_node.
2775 *
2776 * The caller must have suppressed start of new grace periods.
2777 */
2778 static void force_qs_rnp(struct rcu_state *rsp,
2779 int (*f)(struct rcu_data *rsp, bool *isidle,
2780 unsigned long *maxj),
2781 bool *isidle, unsigned long *maxj)
2782 {
2783 unsigned long bit;
2784 int cpu;
2785 unsigned long flags;
2786 unsigned long mask;
2787 struct rcu_node *rnp;
2788
2789 rcu_for_each_leaf_node(rsp, rnp) {
2790 cond_resched_rcu_qs();
2791 mask = 0;
2792 raw_spin_lock_irqsave(&rnp->lock, flags);
2793 smp_mb__after_unlock_lock();
2794 if (rnp->qsmask == 0) {
2795 if (rcu_state_p == &rcu_sched_state ||
2796 rsp != rcu_state_p ||
2797 rcu_preempt_blocked_readers_cgp(rnp)) {
2798 /*
2799 * No point in scanning bits because they
2800 * are all zero. But we might need to
2801 * priority-boost blocked readers.
2802 */
2803 rcu_initiate_boost(rnp, flags);
2804 /* rcu_initiate_boost() releases rnp->lock */
2805 continue;
2806 }
2807 if (rnp->parent &&
2808 (rnp->parent->qsmask & rnp->grpmask)) {
2809 /*
2810 * Race between grace-period
2811 * initialization and task exiting RCU
2812 * read-side critical section: Report.
2813 */
2814 rcu_report_unblock_qs_rnp(rsp, rnp, flags);
2815 /* rcu_report_unblock_qs_rnp() rlses ->lock */
2816 continue;
2817 }
2818 }
2819 cpu = rnp->grplo;
2820 bit = 1;
2821 for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
2822 if ((rnp->qsmask & bit) != 0) {
2823 if (f(per_cpu_ptr(rsp->rda, cpu), isidle, maxj))
2824 mask |= bit;
2825 }
2826 }
2827 if (mask != 0) {
2828 /* Idle/offline CPUs, report (releases rnp->lock. */
2829 rcu_report_qs_rnp(mask, rsp, rnp, rnp->gpnum, flags);
2830 } else {
2831 /* Nothing to do here, so just drop the lock. */
2832 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2833 }
2834 }
2835 }
2836
2837 /*
2838 * Force quiescent states on reluctant CPUs, and also detect which
2839 * CPUs are in dyntick-idle mode.
2840 */
2841 static void force_quiescent_state(struct rcu_state *rsp)
2842 {
2843 unsigned long flags;
2844 bool ret;
2845 struct rcu_node *rnp;
2846 struct rcu_node *rnp_old = NULL;
2847
2848 /* Funnel through hierarchy to reduce memory contention. */
2849 rnp = __this_cpu_read(rsp->rda->mynode);
2850 for (; rnp != NULL; rnp = rnp->parent) {
2851 ret = (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
2852 !raw_spin_trylock(&rnp->fqslock);
2853 if (rnp_old != NULL)
2854 raw_spin_unlock(&rnp_old->fqslock);
2855 if (ret) {
2856 rsp->n_force_qs_lh++;
2857 return;
2858 }
2859 rnp_old = rnp;
2860 }
2861 /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
2862
2863 /* Reached the root of the rcu_node tree, acquire lock. */
2864 raw_spin_lock_irqsave(&rnp_old->lock, flags);
2865 smp_mb__after_unlock_lock();
2866 raw_spin_unlock(&rnp_old->fqslock);
2867 if (READ_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
2868 rsp->n_force_qs_lh++;
2869 raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
2870 return; /* Someone beat us to it. */
2871 }
2872 WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
2873 raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
2874 rcu_gp_kthread_wake(rsp);
2875 }
2876
2877 /*
2878 * This does the RCU core processing work for the specified rcu_state
2879 * and rcu_data structures. This may be called only from the CPU to
2880 * whom the rdp belongs.
2881 */
2882 static void
2883 __rcu_process_callbacks(struct rcu_state *rsp)
2884 {
2885 unsigned long flags;
2886 bool needwake;
2887 struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
2888
2889 WARN_ON_ONCE(rdp->beenonline == 0);
2890
2891 /* Update RCU state based on any recent quiescent states. */
2892 rcu_check_quiescent_state(rsp, rdp);
2893
2894 /* Does this CPU require a not-yet-started grace period? */
2895 local_irq_save(flags);
2896 if (cpu_needs_another_gp(rsp, rdp)) {
2897 raw_spin_lock(&rcu_get_root(rsp)->lock); /* irqs disabled. */
2898 needwake = rcu_start_gp(rsp);
2899 raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
2900 if (needwake)
2901 rcu_gp_kthread_wake(rsp);
2902 } else {
2903 local_irq_restore(flags);
2904 }
2905
2906 /* If there are callbacks ready, invoke them. */
2907 if (cpu_has_callbacks_ready_to_invoke(rdp))
2908 invoke_rcu_callbacks(rsp, rdp);
2909
2910 /* Do any needed deferred wakeups of rcuo kthreads. */
2911 do_nocb_deferred_wakeup(rdp);
2912 }
2913
2914 /*
2915 * Do RCU core processing for the current CPU.
2916 */
2917 static void rcu_process_callbacks(struct softirq_action *unused)
2918 {
2919 struct rcu_state *rsp;
2920
2921 if (cpu_is_offline(smp_processor_id()))
2922 return;
2923 trace_rcu_utilization(TPS("Start RCU core"));
2924 for_each_rcu_flavor(rsp)
2925 __rcu_process_callbacks(rsp);
2926 trace_rcu_utilization(TPS("End RCU core"));
2927 }
2928
2929 /*
2930 * Schedule RCU callback invocation. If the specified type of RCU
2931 * does not support RCU priority boosting, just do a direct call,
2932 * otherwise wake up the per-CPU kernel kthread. Note that because we
2933 * are running on the current CPU with softirqs disabled, the
2934 * rcu_cpu_kthread_task cannot disappear out from under us.
2935 */
2936 static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
2937 {
2938 if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
2939 return;
2940 if (likely(!rsp->boost)) {
2941 rcu_do_batch(rsp, rdp);
2942 return;
2943 }
2944 invoke_rcu_callbacks_kthread();
2945 }
2946
2947 static void invoke_rcu_core(void)
2948 {
2949 if (cpu_online(smp_processor_id()))
2950 raise_softirq(RCU_SOFTIRQ);
2951 }
2952
2953 /*
2954 * Handle any core-RCU processing required by a call_rcu() invocation.
2955 */
2956 static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
2957 struct rcu_head *head, unsigned long flags)
2958 {
2959 bool needwake;
2960
2961 /*
2962 * If called from an extended quiescent state, invoke the RCU
2963 * core in order to force a re-evaluation of RCU's idleness.
2964 */
2965 if (!rcu_is_watching())
2966 invoke_rcu_core();
2967
2968 /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
2969 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
2970 return;
2971
2972 /*
2973 * Force the grace period if too many callbacks or too long waiting.
2974 * Enforce hysteresis, and don't invoke force_quiescent_state()
2975 * if some other CPU has recently done so. Also, don't bother
2976 * invoking force_quiescent_state() if the newly enqueued callback
2977 * is the only one waiting for a grace period to complete.
2978 */
2979 if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
2980
2981 /* Are we ignoring a completed grace period? */
2982 note_gp_changes(rsp, rdp);
2983
2984 /* Start a new grace period if one not already started. */
2985 if (!rcu_gp_in_progress(rsp)) {
2986 struct rcu_node *rnp_root = rcu_get_root(rsp);
2987
2988 raw_spin_lock(&rnp_root->lock);
2989 smp_mb__after_unlock_lock();
2990 needwake = rcu_start_gp(rsp);
2991 raw_spin_unlock(&rnp_root->lock);
2992 if (needwake)
2993 rcu_gp_kthread_wake(rsp);
2994 } else {
2995 /* Give the grace period a kick. */
2996 rdp->blimit = LONG_MAX;
2997 if (rsp->n_force_qs == rdp->n_force_qs_snap &&
2998 *rdp->nxttail[RCU_DONE_TAIL] != head)
2999 force_quiescent_state(rsp);
3000 rdp->n_force_qs_snap = rsp->n_force_qs;
3001 rdp->qlen_last_fqs_check = rdp->qlen;
3002 }
3003 }
3004 }
3005
3006 /*
3007 * RCU callback function to leak a callback.
3008 */
3009 static void rcu_leak_callback(struct rcu_head *rhp)
3010 {
3011 }
3012
3013 /*
3014 * Helper function for call_rcu() and friends. The cpu argument will
3015 * normally be -1, indicating "currently running CPU". It may specify
3016 * a CPU only if that CPU is a no-CBs CPU. Currently, only _rcu_barrier()
3017 * is expected to specify a CPU.
3018 */
3019 static void
3020 __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
3021 struct rcu_state *rsp, int cpu, bool lazy)
3022 {
3023 unsigned long flags;
3024 struct rcu_data *rdp;
3025
3026 WARN_ON_ONCE((unsigned long)head & 0x1); /* Misaligned rcu_head! */
3027 if (debug_rcu_head_queue(head)) {
3028 /* Probable double call_rcu(), so leak the callback. */
3029 WRITE_ONCE(head->func, rcu_leak_callback);
3030 WARN_ONCE(1, "__call_rcu(): Leaked duplicate callback\n");
3031 return;
3032 }
3033 head->func = func;
3034 head->next = NULL;
3035
3036 /*
3037 * Opportunistically note grace-period endings and beginnings.
3038 * Note that we might see a beginning right after we see an
3039 * end, but never vice versa, since this CPU has to pass through
3040 * a quiescent state betweentimes.
3041 */
3042 local_irq_save(flags);
3043 rdp = this_cpu_ptr(rsp->rda);
3044
3045 /* Add the callback to our list. */
3046 if (unlikely(rdp->nxttail[RCU_NEXT_TAIL] == NULL) || cpu != -1) {
3047 int offline;
3048
3049 if (cpu != -1)
3050 rdp = per_cpu_ptr(rsp->rda, cpu);
3051 if (likely(rdp->mynode)) {
3052 /* Post-boot, so this should be for a no-CBs CPU. */
3053 offline = !__call_rcu_nocb(rdp, head, lazy, flags);
3054 WARN_ON_ONCE(offline);
3055 /* Offline CPU, _call_rcu() illegal, leak callback. */
3056 local_irq_restore(flags);
3057 return;
3058 }
3059 /*
3060 * Very early boot, before rcu_init(). Initialize if needed
3061 * and then drop through to queue the callback.
3062 */
3063 BUG_ON(cpu != -1);
3064 WARN_ON_ONCE(!rcu_is_watching());
3065 if (!likely(rdp->nxtlist))
3066 init_default_callback_list(rdp);
3067 }
3068 WRITE_ONCE(rdp->qlen, rdp->qlen + 1);
3069 if (lazy)
3070 rdp->qlen_lazy++;
3071 else
3072 rcu_idle_count_callbacks_posted();
3073 smp_mb(); /* Count before adding callback for rcu_barrier(). */
3074 *rdp->nxttail[RCU_NEXT_TAIL] = head;
3075 rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
3076
3077 if (__is_kfree_rcu_offset((unsigned long)func))
3078 trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
3079 rdp->qlen_lazy, rdp->qlen);
3080 else
3081 trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen);
3082
3083 /* Go handle any RCU core processing required. */
3084 __call_rcu_core(rsp, rdp, head, flags);
3085 local_irq_restore(flags);
3086 }
3087
3088 /*
3089 * Queue an RCU-sched callback for invocation after a grace period.
3090 */
3091 void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
3092 {
3093 __call_rcu(head, func, &rcu_sched_state, -1, 0);
3094 }
3095 EXPORT_SYMBOL_GPL(call_rcu_sched);
3096
3097 /*
3098 * Queue an RCU callback for invocation after a quicker grace period.
3099 */
3100 void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
3101 {
3102 __call_rcu(head, func, &rcu_bh_state, -1, 0);
3103 }
3104 EXPORT_SYMBOL_GPL(call_rcu_bh);
3105
3106 /*
3107 * Queue an RCU callback for lazy invocation after a grace period.
3108 * This will likely be later named something like "call_rcu_lazy()",
3109 * but this change will require some way of tagging the lazy RCU
3110 * callbacks in the list of pending callbacks. Until then, this
3111 * function may only be called from __kfree_rcu().
3112 */
3113 void kfree_call_rcu(struct rcu_head *head,
3114 void (*func)(struct rcu_head *rcu))
3115 {
3116 __call_rcu(head, func, rcu_state_p, -1, 1);
3117 }
3118 EXPORT_SYMBOL_GPL(kfree_call_rcu);
3119
3120 /*
3121 * Because a context switch is a grace period for RCU-sched and RCU-bh,
3122 * any blocking grace-period wait automatically implies a grace period
3123 * if there is only one CPU online at any point time during execution
3124 * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to
3125 * occasionally incorrectly indicate that there are multiple CPUs online
3126 * when there was in fact only one the whole time, as this just adds
3127 * some overhead: RCU still operates correctly.
3128 */
3129 static inline int rcu_blocking_is_gp(void)
3130 {
3131 int ret;
3132
3133 might_sleep(); /* Check for RCU read-side critical section. */
3134 preempt_disable();
3135 ret = num_online_cpus() <= 1;
3136 preempt_enable();
3137 return ret;
3138 }
3139
3140 /**
3141 * synchronize_sched - wait until an rcu-sched grace period has elapsed.
3142 *
3143 * Control will return to the caller some time after a full rcu-sched
3144 * grace period has elapsed, in other words after all currently executing
3145 * rcu-sched read-side critical sections have completed. These read-side
3146 * critical sections are delimited by rcu_read_lock_sched() and
3147 * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
3148 * local_irq_disable(), and so on may be used in place of
3149 * rcu_read_lock_sched().
3150 *
3151 * This means that all preempt_disable code sequences, including NMI and
3152 * non-threaded hardware-interrupt handlers, in progress on entry will
3153 * have completed before this primitive returns. However, this does not
3154 * guarantee that softirq handlers will have completed, since in some
3155 * kernels, these handlers can run in process context, and can block.
3156 *
3157 * Note that this guarantee implies further memory-ordering guarantees.
3158 * On systems with more than one CPU, when synchronize_sched() returns,
3159 * each CPU is guaranteed to have executed a full memory barrier since the
3160 * end of its last RCU-sched read-side critical section whose beginning
3161 * preceded the call to synchronize_sched(). In addition, each CPU having
3162 * an RCU read-side critical section that extends beyond the return from
3163 * synchronize_sched() is guaranteed to have executed a full memory barrier
3164 * after the beginning of synchronize_sched() and before the beginning of
3165 * that RCU read-side critical section. Note that these guarantees include
3166 * CPUs that are offline, idle, or executing in user mode, as well as CPUs
3167 * that are executing in the kernel.
3168 *
3169 * Furthermore, if CPU A invoked synchronize_sched(), which returned
3170 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
3171 * to have executed a full memory barrier during the execution of
3172 * synchronize_sched() -- even if CPU A and CPU B are the same CPU (but
3173 * again only if the system has more than one CPU).
3174 *
3175 * This primitive provides the guarantees made by the (now removed)
3176 * synchronize_kernel() API. In contrast, synchronize_rcu() only
3177 * guarantees that rcu_read_lock() sections will have completed.
3178 * In "classic RCU", these two guarantees happen to be one and
3179 * the same, but can differ in realtime RCU implementations.
3180 */
3181 void synchronize_sched(void)
3182 {
3183 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
3184 lock_is_held(&rcu_lock_map) ||
3185 lock_is_held(&rcu_sched_lock_map),
3186 "Illegal synchronize_sched() in RCU-sched read-side critical section");
3187 if (rcu_blocking_is_gp())
3188 return;
3189 if (rcu_gp_is_expedited())
3190 synchronize_sched_expedited();
3191 else
3192 wait_rcu_gp(call_rcu_sched);
3193 }
3194 EXPORT_SYMBOL_GPL(synchronize_sched);
3195
3196 /**
3197 * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
3198 *
3199 * Control will return to the caller some time after a full rcu_bh grace
3200 * period has elapsed, in other words after all currently executing rcu_bh
3201 * read-side critical sections have completed. RCU read-side critical
3202 * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
3203 * and may be nested.
3204 *
3205 * See the description of synchronize_sched() for more detailed information
3206 * on memory ordering guarantees.
3207 */
3208 void synchronize_rcu_bh(void)
3209 {
3210 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
3211 lock_is_held(&rcu_lock_map) ||
3212 lock_is_held(&rcu_sched_lock_map),
3213 "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
3214 if (rcu_blocking_is_gp())
3215 return;
3216 if (rcu_gp_is_expedited())
3217 synchronize_rcu_bh_expedited();
3218 else
3219 wait_rcu_gp(call_rcu_bh);
3220 }
3221 EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
3222
3223 /**
3224 * get_state_synchronize_rcu - Snapshot current RCU state
3225 *
3226 * Returns a cookie that is used by a later call to cond_synchronize_rcu()
3227 * to determine whether or not a full grace period has elapsed in the
3228 * meantime.
3229 */
3230 unsigned long get_state_synchronize_rcu(void)
3231 {
3232 /*
3233 * Any prior manipulation of RCU-protected data must happen
3234 * before the load from ->gpnum.
3235 */
3236 smp_mb(); /* ^^^ */
3237
3238 /*
3239 * Make sure this load happens before the purportedly
3240 * time-consuming work between get_state_synchronize_rcu()
3241 * and cond_synchronize_rcu().
3242 */
3243 return smp_load_acquire(&rcu_state_p->gpnum);
3244 }
3245 EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
3246
3247 /**
3248 * cond_synchronize_rcu - Conditionally wait for an RCU grace period
3249 *
3250 * @oldstate: return value from earlier call to get_state_synchronize_rcu()
3251 *
3252 * If a full RCU grace period has elapsed since the earlier call to
3253 * get_state_synchronize_rcu(), just return. Otherwise, invoke
3254 * synchronize_rcu() to wait for a full grace period.
3255 *
3256 * Yes, this function does not take counter wrap into account. But
3257 * counter wrap is harmless. If the counter wraps, we have waited for
3258 * more than 2 billion grace periods (and way more on a 64-bit system!),
3259 * so waiting for one additional grace period should be just fine.
3260 */
3261 void cond_synchronize_rcu(unsigned long oldstate)
3262 {
3263 unsigned long newstate;
3264
3265 /*
3266 * Ensure that this load happens before any RCU-destructive
3267 * actions the caller might carry out after we return.
3268 */
3269 newstate = smp_load_acquire(&rcu_state_p->completed);
3270 if (ULONG_CMP_GE(oldstate, newstate))
3271 synchronize_rcu();
3272 }
3273 EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
3274
3275 /**
3276 * get_state_synchronize_sched - Snapshot current RCU-sched state
3277 *
3278 * Returns a cookie that is used by a later call to cond_synchronize_sched()
3279 * to determine whether or not a full grace period has elapsed in the
3280 * meantime.
3281 */
3282 unsigned long get_state_synchronize_sched(void)
3283 {
3284 /*
3285 * Any prior manipulation of RCU-protected data must happen
3286 * before the load from ->gpnum.
3287 */
3288 smp_mb(); /* ^^^ */
3289
3290 /*
3291 * Make sure this load happens before the purportedly
3292 * time-consuming work between get_state_synchronize_sched()
3293 * and cond_synchronize_sched().
3294 */
3295 return smp_load_acquire(&rcu_sched_state.gpnum);
3296 }
3297 EXPORT_SYMBOL_GPL(get_state_synchronize_sched);
3298
3299 /**
3300 * cond_synchronize_sched - Conditionally wait for an RCU-sched grace period
3301 *
3302 * @oldstate: return value from earlier call to get_state_synchronize_sched()
3303 *
3304 * If a full RCU-sched grace period has elapsed since the earlier call to
3305 * get_state_synchronize_sched(), just return. Otherwise, invoke
3306 * synchronize_sched() to wait for a full grace period.
3307 *
3308 * Yes, this function does not take counter wrap into account. But
3309 * counter wrap is harmless. If the counter wraps, we have waited for
3310 * more than 2 billion grace periods (and way more on a 64-bit system!),
3311 * so waiting for one additional grace period should be just fine.
3312 */
3313 void cond_synchronize_sched(unsigned long oldstate)
3314 {
3315 unsigned long newstate;
3316
3317 /*
3318 * Ensure that this load happens before any RCU-destructive
3319 * actions the caller might carry out after we return.
3320 */
3321 newstate = smp_load_acquire(&rcu_sched_state.completed);
3322 if (ULONG_CMP_GE(oldstate, newstate))
3323 synchronize_sched();
3324 }
3325 EXPORT_SYMBOL_GPL(cond_synchronize_sched);
3326
3327 /* Adjust sequence number for start of update-side operation. */
3328 static void rcu_seq_start(unsigned long *sp)
3329 {
3330 WRITE_ONCE(*sp, *sp + 1);
3331 smp_mb(); /* Ensure update-side operation after counter increment. */
3332 WARN_ON_ONCE(!(*sp & 0x1));
3333 }
3334
3335 /* Adjust sequence number for end of update-side operation. */
3336 static void rcu_seq_end(unsigned long *sp)
3337 {
3338 smp_mb(); /* Ensure update-side operation before counter increment. */
3339 WRITE_ONCE(*sp, *sp + 1);
3340 WARN_ON_ONCE(*sp & 0x1);
3341 }
3342
3343 /* Take a snapshot of the update side's sequence number. */
3344 static unsigned long rcu_seq_snap(unsigned long *sp)
3345 {
3346 unsigned long s;
3347
3348 smp_mb(); /* Caller's modifications seen first by other CPUs. */
3349 s = (READ_ONCE(*sp) + 3) & ~0x1;
3350 smp_mb(); /* Above access must not bleed into critical section. */
3351 return s;
3352 }
3353
3354 /*
3355 * Given a snapshot from rcu_seq_snap(), determine whether or not a
3356 * full update-side operation has occurred.
3357 */
3358 static bool rcu_seq_done(unsigned long *sp, unsigned long s)
3359 {
3360 return ULONG_CMP_GE(READ_ONCE(*sp), s);
3361 }
3362
3363 /* Wrapper functions for expedited grace periods. */
3364 static void rcu_exp_gp_seq_start(struct rcu_state *rsp)
3365 {
3366 rcu_seq_start(&rsp->expedited_sequence);
3367 }
3368 static void rcu_exp_gp_seq_end(struct rcu_state *rsp)
3369 {
3370 rcu_seq_end(&rsp->expedited_sequence);
3371 smp_mb(); /* Ensure that consecutive grace periods serialize. */
3372 }
3373 static unsigned long rcu_exp_gp_seq_snap(struct rcu_state *rsp)
3374 {
3375 return rcu_seq_snap(&rsp->expedited_sequence);
3376 }
3377 static bool rcu_exp_gp_seq_done(struct rcu_state *rsp, unsigned long s)
3378 {
3379 return rcu_seq_done(&rsp->expedited_sequence, s);
3380 }
3381
3382 /* Common code for synchronize_{rcu,sched}_expedited() work-done checking. */
3383 static bool sync_exp_work_done(struct rcu_state *rsp, struct rcu_node *rnp,
3384 struct rcu_data *rdp,
3385 atomic_long_t *stat, unsigned long s)
3386 {
3387 if (rcu_exp_gp_seq_done(rsp, s)) {
3388 if (rnp)
3389 mutex_unlock(&rnp->exp_funnel_mutex);
3390 else if (rdp)
3391 mutex_unlock(&rdp->exp_funnel_mutex);
3392 /* Ensure test happens before caller kfree(). */
3393 smp_mb__before_atomic(); /* ^^^ */
3394 atomic_long_inc(stat);
3395 return true;
3396 }
3397 return false;
3398 }
3399
3400 /*
3401 * Funnel-lock acquisition for expedited grace periods. Returns a
3402 * pointer to the root rcu_node structure, or NULL if some other
3403 * task did the expedited grace period for us.
3404 */
3405 static struct rcu_node *exp_funnel_lock(struct rcu_state *rsp, unsigned long s)
3406 {
3407 struct rcu_data *rdp;
3408 struct rcu_node *rnp0;
3409 struct rcu_node *rnp1 = NULL;
3410
3411 /*
3412 * First try directly acquiring the root lock in order to reduce
3413 * latency in the common case where expedited grace periods are
3414 * rare. We check mutex_is_locked() to avoid pathological levels of
3415 * memory contention on ->exp_funnel_mutex in the heavy-load case.
3416 */
3417 rnp0 = rcu_get_root(rsp);
3418 if (!mutex_is_locked(&rnp0->exp_funnel_mutex)) {
3419 if (mutex_trylock(&rnp0->exp_funnel_mutex)) {
3420 if (sync_exp_work_done(rsp, rnp0, NULL,
3421 &rsp->expedited_workdone0, s))
3422 return NULL;
3423 return rnp0;
3424 }
3425 }
3426
3427 /*
3428 * Each pass through the following loop works its way
3429 * up the rcu_node tree, returning if others have done the
3430 * work or otherwise falls through holding the root rnp's
3431 * ->exp_funnel_mutex. The mapping from CPU to rcu_node structure
3432 * can be inexact, as it is just promoting locality and is not
3433 * strictly needed for correctness.
3434 */
3435 rdp = per_cpu_ptr(rsp->rda, raw_smp_processor_id());
3436 if (sync_exp_work_done(rsp, NULL, NULL, &rsp->expedited_workdone1, s))
3437 return NULL;
3438 mutex_lock(&rdp->exp_funnel_mutex);
3439 rnp0 = rdp->mynode;
3440 for (; rnp0 != NULL; rnp0 = rnp0->parent) {
3441 if (sync_exp_work_done(rsp, rnp1, rdp,
3442 &rsp->expedited_workdone2, s))
3443 return NULL;
3444 mutex_lock(&rnp0->exp_funnel_mutex);
3445 if (rnp1)
3446 mutex_unlock(&rnp1->exp_funnel_mutex);
3447 else
3448 mutex_unlock(&rdp->exp_funnel_mutex);
3449 rnp1 = rnp0;
3450 }
3451 if (sync_exp_work_done(rsp, rnp1, rdp,
3452 &rsp->expedited_workdone3, s))
3453 return NULL;
3454 return rnp1;
3455 }
3456
3457 /* Invoked on each online non-idle CPU for expedited quiescent state. */
3458 static int synchronize_sched_expedited_cpu_stop(void *data)
3459 {
3460 struct rcu_data *rdp = data;
3461 struct rcu_state *rsp = rdp->rsp;
3462
3463 /* We are here: If we are last, do the wakeup. */
3464 rdp->exp_done = true;
3465 if (atomic_dec_and_test(&rsp->expedited_need_qs))
3466 wake_up(&rsp->expedited_wq);
3467 return 0;
3468 }
3469
3470 static void synchronize_sched_expedited_wait(struct rcu_state *rsp)
3471 {
3472 int cpu;
3473 unsigned long jiffies_stall;
3474 unsigned long jiffies_start;
3475 struct rcu_data *rdp;
3476 int ret;
3477
3478 jiffies_stall = rcu_jiffies_till_stall_check();
3479 jiffies_start = jiffies;
3480
3481 for (;;) {
3482 ret = wait_event_interruptible_timeout(
3483 rsp->expedited_wq,
3484 !atomic_read(&rsp->expedited_need_qs),
3485 jiffies_stall);
3486 if (ret > 0)
3487 return;
3488 if (ret < 0) {
3489 /* Hit a signal, disable CPU stall warnings. */
3490 wait_event(rsp->expedited_wq,
3491 !atomic_read(&rsp->expedited_need_qs));
3492 return;
3493 }
3494 pr_err("INFO: %s detected expedited stalls on CPUs: {",
3495 rsp->name);
3496 for_each_online_cpu(cpu) {
3497 rdp = per_cpu_ptr(rsp->rda, cpu);
3498
3499 if (rdp->exp_done)
3500 continue;
3501 pr_cont(" %d", cpu);
3502 }
3503 pr_cont(" } %lu jiffies s: %lu\n",
3504 jiffies - jiffies_start, rsp->expedited_sequence);
3505 for_each_online_cpu(cpu) {
3506 rdp = per_cpu_ptr(rsp->rda, cpu);
3507
3508 if (rdp->exp_done)
3509 continue;
3510 dump_cpu_task(cpu);
3511 }
3512 jiffies_stall = 3 * rcu_jiffies_till_stall_check() + 3;
3513 }
3514 }
3515
3516 /**
3517 * synchronize_sched_expedited - Brute-force RCU-sched grace period
3518 *
3519 * Wait for an RCU-sched grace period to elapse, but use a "big hammer"
3520 * approach to force the grace period to end quickly. This consumes
3521 * significant time on all CPUs and is unfriendly to real-time workloads,
3522 * so is thus not recommended for any sort of common-case code. In fact,
3523 * if you are using synchronize_sched_expedited() in a loop, please
3524 * restructure your code to batch your updates, and then use a single
3525 * synchronize_sched() instead.
3526 *
3527 * This implementation can be thought of as an application of sequence
3528 * locking to expedited grace periods, but using the sequence counter to
3529 * determine when someone else has already done the work instead of for
3530 * retrying readers.
3531 */
3532 void synchronize_sched_expedited(void)
3533 {
3534 int cpu;
3535 unsigned long s;
3536 struct rcu_node *rnp;
3537 struct rcu_state *rsp = &rcu_sched_state;
3538
3539 /* Take a snapshot of the sequence number. */
3540 s = rcu_exp_gp_seq_snap(rsp);
3541
3542 if (!try_get_online_cpus()) {
3543 /* CPU hotplug operation in flight, fall back to normal GP. */
3544 wait_rcu_gp(call_rcu_sched);
3545 atomic_long_inc(&rsp->expedited_normal);
3546 return;
3547 }
3548 WARN_ON_ONCE(cpu_is_offline(raw_smp_processor_id()));
3549
3550 rnp = exp_funnel_lock(rsp, s);
3551 if (rnp == NULL) {
3552 put_online_cpus();
3553 return; /* Someone else did our work for us. */
3554 }
3555
3556 rcu_exp_gp_seq_start(rsp);
3557
3558 /* Stop each CPU that is online, non-idle, and not us. */
3559 init_waitqueue_head(&rsp->expedited_wq);
3560 atomic_set(&rsp->expedited_need_qs, 1); /* Extra count avoids race. */
3561 for_each_online_cpu(cpu) {
3562 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
3563 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
3564
3565 rdp->exp_done = false;
3566
3567 /* Skip our CPU and any idle CPUs. */
3568 if (raw_smp_processor_id() == cpu ||
3569 !(atomic_add_return(0, &rdtp->dynticks) & 0x1))
3570 continue;
3571 atomic_inc(&rsp->expedited_need_qs);
3572 stop_one_cpu_nowait(cpu, synchronize_sched_expedited_cpu_stop,
3573 rdp, &rdp->exp_stop_work);
3574 }
3575
3576 /* Remove extra count and, if necessary, wait for CPUs to stop. */
3577 if (!atomic_dec_and_test(&rsp->expedited_need_qs))
3578 synchronize_sched_expedited_wait(rsp);
3579
3580 rcu_exp_gp_seq_end(rsp);
3581 mutex_unlock(&rnp->exp_funnel_mutex);
3582
3583 put_online_cpus();
3584 }
3585 EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
3586
3587 /*
3588 * Check to see if there is any immediate RCU-related work to be done
3589 * by the current CPU, for the specified type of RCU, returning 1 if so.
3590 * The checks are in order of increasing expense: checks that can be
3591 * carried out against CPU-local state are performed first. However,
3592 * we must check for CPU stalls first, else we might not get a chance.
3593 */
3594 static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
3595 {
3596 struct rcu_node *rnp = rdp->mynode;
3597
3598 rdp->n_rcu_pending++;
3599
3600 /* Check for CPU stalls, if enabled. */
3601 check_cpu_stall(rsp, rdp);
3602
3603 /* Is this CPU a NO_HZ_FULL CPU that should ignore RCU? */
3604 if (rcu_nohz_full_cpu(rsp))
3605 return 0;
3606
3607 /* Is the RCU core waiting for a quiescent state from this CPU? */
3608 if (rcu_scheduler_fully_active &&
3609 rdp->qs_pending && !rdp->passed_quiesce &&
3610 rdp->rcu_qs_ctr_snap == __this_cpu_read(rcu_qs_ctr)) {
3611 rdp->n_rp_qs_pending++;
3612 } else if (rdp->qs_pending &&
3613 (rdp->passed_quiesce ||
3614 rdp->rcu_qs_ctr_snap != __this_cpu_read(rcu_qs_ctr))) {
3615 rdp->n_rp_report_qs++;
3616 return 1;
3617 }
3618
3619 /* Does this CPU have callbacks ready to invoke? */
3620 if (cpu_has_callbacks_ready_to_invoke(rdp)) {
3621 rdp->n_rp_cb_ready++;
3622 return 1;
3623 }
3624
3625 /* Has RCU gone idle with this CPU needing another grace period? */
3626 if (cpu_needs_another_gp(rsp, rdp)) {
3627 rdp->n_rp_cpu_needs_gp++;
3628 return 1;
3629 }
3630
3631 /* Has another RCU grace period completed? */
3632 if (READ_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
3633 rdp->n_rp_gp_completed++;
3634 return 1;
3635 }
3636
3637 /* Has a new RCU grace period started? */
3638 if (READ_ONCE(rnp->gpnum) != rdp->gpnum ||
3639 unlikely(READ_ONCE(rdp->gpwrap))) { /* outside lock */
3640 rdp->n_rp_gp_started++;
3641 return 1;
3642 }
3643
3644 /* Does this CPU need a deferred NOCB wakeup? */
3645 if (rcu_nocb_need_deferred_wakeup(rdp)) {
3646 rdp->n_rp_nocb_defer_wakeup++;
3647 return 1;
3648 }
3649
3650 /* nothing to do */
3651 rdp->n_rp_need_nothing++;
3652 return 0;
3653 }
3654
3655 /*
3656 * Check to see if there is any immediate RCU-related work to be done
3657 * by the current CPU, returning 1 if so. This function is part of the
3658 * RCU implementation; it is -not- an exported member of the RCU API.
3659 */
3660 static int rcu_pending(void)
3661 {
3662 struct rcu_state *rsp;
3663
3664 for_each_rcu_flavor(rsp)
3665 if (__rcu_pending(rsp, this_cpu_ptr(rsp->rda)))
3666 return 1;
3667 return 0;
3668 }
3669
3670 /*
3671 * Return true if the specified CPU has any callback. If all_lazy is
3672 * non-NULL, store an indication of whether all callbacks are lazy.
3673 * (If there are no callbacks, all of them are deemed to be lazy.)
3674 */
3675 static bool __maybe_unused rcu_cpu_has_callbacks(bool *all_lazy)
3676 {
3677 bool al = true;
3678 bool hc = false;
3679 struct rcu_data *rdp;
3680 struct rcu_state *rsp;
3681
3682 for_each_rcu_flavor(rsp) {
3683 rdp = this_cpu_ptr(rsp->rda);
3684 if (!rdp->nxtlist)
3685 continue;
3686 hc = true;
3687 if (rdp->qlen != rdp->qlen_lazy || !all_lazy) {
3688 al = false;
3689 break;
3690 }
3691 }
3692 if (all_lazy)
3693 *all_lazy = al;
3694 return hc;
3695 }
3696
3697 /*
3698 * Helper function for _rcu_barrier() tracing. If tracing is disabled,
3699 * the compiler is expected to optimize this away.
3700 */
3701 static void _rcu_barrier_trace(struct rcu_state *rsp, const char *s,
3702 int cpu, unsigned long done)
3703 {
3704 trace_rcu_barrier(rsp->name, s, cpu,
3705 atomic_read(&rsp->barrier_cpu_count), done);
3706 }
3707
3708 /*
3709 * RCU callback function for _rcu_barrier(). If we are last, wake
3710 * up the task executing _rcu_barrier().
3711 */
3712 static void rcu_barrier_callback(struct rcu_head *rhp)
3713 {
3714 struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
3715 struct rcu_state *rsp = rdp->rsp;
3716
3717 if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
3718 _rcu_barrier_trace(rsp, "LastCB", -1, rsp->barrier_sequence);
3719 complete(&rsp->barrier_completion);
3720 } else {
3721 _rcu_barrier_trace(rsp, "CB", -1, rsp->barrier_sequence);
3722 }
3723 }
3724
3725 /*
3726 * Called with preemption disabled, and from cross-cpu IRQ context.
3727 */
3728 static void rcu_barrier_func(void *type)
3729 {
3730 struct rcu_state *rsp = type;
3731 struct rcu_data *rdp = raw_cpu_ptr(rsp->rda);
3732
3733 _rcu_barrier_trace(rsp, "IRQ", -1, rsp->barrier_sequence);
3734 atomic_inc(&rsp->barrier_cpu_count);
3735 rsp->call(&rdp->barrier_head, rcu_barrier_callback);
3736 }
3737
3738 /*
3739 * Orchestrate the specified type of RCU barrier, waiting for all
3740 * RCU callbacks of the specified type to complete.
3741 */
3742 static void _rcu_barrier(struct rcu_state *rsp)
3743 {
3744 int cpu;
3745 struct rcu_data *rdp;
3746 unsigned long s = rcu_seq_snap(&rsp->barrier_sequence);
3747
3748 _rcu_barrier_trace(rsp, "Begin", -1, s);
3749
3750 /* Take mutex to serialize concurrent rcu_barrier() requests. */
3751 mutex_lock(&rsp->barrier_mutex);
3752
3753 /* Did someone else do our work for us? */
3754 if (rcu_seq_done(&rsp->barrier_sequence, s)) {
3755 _rcu_barrier_trace(rsp, "EarlyExit", -1, rsp->barrier_sequence);
3756 smp_mb(); /* caller's subsequent code after above check. */
3757 mutex_unlock(&rsp->barrier_mutex);
3758 return;
3759 }
3760
3761 /* Mark the start of the barrier operation. */
3762 rcu_seq_start(&rsp->barrier_sequence);
3763 _rcu_barrier_trace(rsp, "Inc1", -1, rsp->barrier_sequence);
3764
3765 /*
3766 * Initialize the count to one rather than to zero in order to
3767 * avoid a too-soon return to zero in case of a short grace period
3768 * (or preemption of this task). Exclude CPU-hotplug operations
3769 * to ensure that no offline CPU has callbacks queued.
3770 */
3771 init_completion(&rsp->barrier_completion);
3772 atomic_set(&rsp->barrier_cpu_count, 1);
3773 get_online_cpus();
3774
3775 /*
3776 * Force each CPU with callbacks to register a new callback.
3777 * When that callback is invoked, we will know that all of the
3778 * corresponding CPU's preceding callbacks have been invoked.
3779 */
3780 for_each_possible_cpu(cpu) {
3781 if (!cpu_online(cpu) && !rcu_is_nocb_cpu(cpu))
3782 continue;
3783 rdp = per_cpu_ptr(rsp->rda, cpu);
3784 if (rcu_is_nocb_cpu(cpu)) {
3785 if (!rcu_nocb_cpu_needs_barrier(rsp, cpu)) {
3786 _rcu_barrier_trace(rsp, "OfflineNoCB", cpu,
3787 rsp->barrier_sequence);
3788 } else {
3789 _rcu_barrier_trace(rsp, "OnlineNoCB", cpu,
3790 rsp->barrier_sequence);
3791 smp_mb__before_atomic();
3792 atomic_inc(&rsp->barrier_cpu_count);
3793 __call_rcu(&rdp->barrier_head,
3794 rcu_barrier_callback, rsp, cpu, 0);
3795 }
3796 } else if (READ_ONCE(rdp->qlen)) {
3797 _rcu_barrier_trace(rsp, "OnlineQ", cpu,
3798 rsp->barrier_sequence);
3799 smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
3800 } else {
3801 _rcu_barrier_trace(rsp, "OnlineNQ", cpu,
3802 rsp->barrier_sequence);
3803 }
3804 }
3805 put_online_cpus();
3806
3807 /*
3808 * Now that we have an rcu_barrier_callback() callback on each
3809 * CPU, and thus each counted, remove the initial count.
3810 */
3811 if (atomic_dec_and_test(&rsp->barrier_cpu_count))
3812 complete(&rsp->barrier_completion);
3813
3814 /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
3815 wait_for_completion(&rsp->barrier_completion);
3816
3817 /* Mark the end of the barrier operation. */
3818 _rcu_barrier_trace(rsp, "Inc2", -1, rsp->barrier_sequence);
3819 rcu_seq_end(&rsp->barrier_sequence);
3820
3821 /* Other rcu_barrier() invocations can now safely proceed. */
3822 mutex_unlock(&rsp->barrier_mutex);
3823 }
3824
3825 /**
3826 * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
3827 */
3828 void rcu_barrier_bh(void)
3829 {
3830 _rcu_barrier(&rcu_bh_state);
3831 }
3832 EXPORT_SYMBOL_GPL(rcu_barrier_bh);
3833
3834 /**
3835 * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
3836 */
3837 void rcu_barrier_sched(void)
3838 {
3839 _rcu_barrier(&rcu_sched_state);
3840 }
3841 EXPORT_SYMBOL_GPL(rcu_barrier_sched);
3842
3843 /*
3844 * Propagate ->qsinitmask bits up the rcu_node tree to account for the
3845 * first CPU in a given leaf rcu_node structure coming online. The caller
3846 * must hold the corresponding leaf rcu_node ->lock with interrrupts
3847 * disabled.
3848 */
3849 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
3850 {
3851 long mask;
3852 struct rcu_node *rnp = rnp_leaf;
3853
3854 for (;;) {
3855 mask = rnp->grpmask;
3856 rnp = rnp->parent;
3857 if (rnp == NULL)
3858 return;
3859 raw_spin_lock(&rnp->lock); /* Interrupts already disabled. */
3860 rnp->qsmaskinit |= mask;
3861 raw_spin_unlock(&rnp->lock); /* Interrupts remain disabled. */
3862 }
3863 }
3864
3865 /*
3866 * Do boot-time initialization of a CPU's per-CPU RCU data.
3867 */
3868 static void __init
3869 rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
3870 {
3871 unsigned long flags;
3872 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
3873 struct rcu_node *rnp = rcu_get_root(rsp);
3874
3875 /* Set up local state, ensuring consistent view of global state. */
3876 raw_spin_lock_irqsave(&rnp->lock, flags);
3877 rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
3878 rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
3879 WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_EXIT_IDLE);
3880 WARN_ON_ONCE(atomic_read(&rdp->dynticks->dynticks) != 1);
3881 rdp->cpu = cpu;
3882 rdp->rsp = rsp;
3883 mutex_init(&rdp->exp_funnel_mutex);
3884 rcu_boot_init_nocb_percpu_data(rdp);
3885 raw_spin_unlock_irqrestore(&rnp->lock, flags);
3886 }
3887
3888 /*
3889 * Initialize a CPU's per-CPU RCU data. Note that only one online or
3890 * offline event can be happening at a given time. Note also that we
3891 * can accept some slop in the rsp->completed access due to the fact
3892 * that this CPU cannot possibly have any RCU callbacks in flight yet.
3893 */
3894 static void
3895 rcu_init_percpu_data(int cpu, struct rcu_state *rsp)
3896 {
3897 unsigned long flags;
3898 unsigned long mask;
3899 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
3900 struct rcu_node *rnp = rcu_get_root(rsp);
3901
3902 /* Set up local state, ensuring consistent view of global state. */
3903 raw_spin_lock_irqsave(&rnp->lock, flags);
3904 rdp->beenonline = 1; /* We have now been online. */
3905 rdp->qlen_last_fqs_check = 0;
3906 rdp->n_force_qs_snap = rsp->n_force_qs;
3907 rdp->blimit = blimit;
3908 if (!rdp->nxtlist)
3909 init_callback_list(rdp); /* Re-enable callbacks on this CPU. */
3910 rdp->dynticks->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
3911 rcu_sysidle_init_percpu_data(rdp->dynticks);
3912 atomic_set(&rdp->dynticks->dynticks,
3913 (atomic_read(&rdp->dynticks->dynticks) & ~0x1) + 1);
3914 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
3915
3916 /*
3917 * Add CPU to leaf rcu_node pending-online bitmask. Any needed
3918 * propagation up the rcu_node tree will happen at the beginning
3919 * of the next grace period.
3920 */
3921 rnp = rdp->mynode;
3922 mask = rdp->grpmask;
3923 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
3924 smp_mb__after_unlock_lock();
3925 rnp->qsmaskinitnext |= mask;
3926 rdp->gpnum = rnp->completed; /* Make CPU later note any new GP. */
3927 rdp->completed = rnp->completed;
3928 rdp->passed_quiesce = false;
3929 rdp->rcu_qs_ctr_snap = per_cpu(rcu_qs_ctr, cpu);
3930 rdp->qs_pending = false;
3931 trace_rcu_grace_period(rsp->name, rdp->gpnum, TPS("cpuonl"));
3932 raw_spin_unlock_irqrestore(&rnp->lock, flags);
3933 }
3934
3935 static void rcu_prepare_cpu(int cpu)
3936 {
3937 struct rcu_state *rsp;
3938
3939 for_each_rcu_flavor(rsp)
3940 rcu_init_percpu_data(cpu, rsp);
3941 }
3942
3943 /*
3944 * Handle CPU online/offline notification events.
3945 */
3946 int rcu_cpu_notify(struct notifier_block *self,
3947 unsigned long action, void *hcpu)
3948 {
3949 long cpu = (long)hcpu;
3950 struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
3951 struct rcu_node *rnp = rdp->mynode;
3952 struct rcu_state *rsp;
3953
3954 switch (action) {
3955 case CPU_UP_PREPARE:
3956 case CPU_UP_PREPARE_FROZEN:
3957 rcu_prepare_cpu(cpu);
3958 rcu_prepare_kthreads(cpu);
3959 rcu_spawn_all_nocb_kthreads(cpu);
3960 break;
3961 case CPU_ONLINE:
3962 case CPU_DOWN_FAILED:
3963 rcu_boost_kthread_setaffinity(rnp, -1);
3964 break;
3965 case CPU_DOWN_PREPARE:
3966 rcu_boost_kthread_setaffinity(rnp, cpu);
3967 break;
3968 case CPU_DYING:
3969 case CPU_DYING_FROZEN:
3970 for_each_rcu_flavor(rsp)
3971 rcu_cleanup_dying_cpu(rsp);
3972 break;
3973 case CPU_DYING_IDLE:
3974 for_each_rcu_flavor(rsp) {
3975 rcu_cleanup_dying_idle_cpu(cpu, rsp);
3976 }
3977 break;
3978 case CPU_DEAD:
3979 case CPU_DEAD_FROZEN:
3980 case CPU_UP_CANCELED:
3981 case CPU_UP_CANCELED_FROZEN:
3982 for_each_rcu_flavor(rsp) {
3983 rcu_cleanup_dead_cpu(cpu, rsp);
3984 do_nocb_deferred_wakeup(per_cpu_ptr(rsp->rda, cpu));
3985 }
3986 break;
3987 default:
3988 break;
3989 }
3990 return NOTIFY_OK;
3991 }
3992
3993 static int rcu_pm_notify(struct notifier_block *self,
3994 unsigned long action, void *hcpu)
3995 {
3996 switch (action) {
3997 case PM_HIBERNATION_PREPARE:
3998 case PM_SUSPEND_PREPARE:
3999 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
4000 rcu_expedite_gp();
4001 break;
4002 case PM_POST_HIBERNATION:
4003 case PM_POST_SUSPEND:
4004 if (nr_cpu_ids <= 256) /* Expediting bad for large systems. */
4005 rcu_unexpedite_gp();
4006 break;
4007 default:
4008 break;
4009 }
4010 return NOTIFY_OK;
4011 }
4012
4013 /*
4014 * Spawn the kthreads that handle each RCU flavor's grace periods.
4015 */
4016 static int __init rcu_spawn_gp_kthread(void)
4017 {
4018 unsigned long flags;
4019 int kthread_prio_in = kthread_prio;
4020 struct rcu_node *rnp;
4021 struct rcu_state *rsp;
4022 struct sched_param sp;
4023 struct task_struct *t;
4024
4025 /* Force priority into range. */
4026 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
4027 kthread_prio = 1;
4028 else if (kthread_prio < 0)
4029 kthread_prio = 0;
4030 else if (kthread_prio > 99)
4031 kthread_prio = 99;
4032 if (kthread_prio != kthread_prio_in)
4033 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
4034 kthread_prio, kthread_prio_in);
4035
4036 rcu_scheduler_fully_active = 1;
4037 for_each_rcu_flavor(rsp) {
4038 t = kthread_create(rcu_gp_kthread, rsp, "%s", rsp->name);
4039 BUG_ON(IS_ERR(t));
4040 rnp = rcu_get_root(rsp);
4041 raw_spin_lock_irqsave(&rnp->lock, flags);
4042 rsp->gp_kthread = t;
4043 if (kthread_prio) {
4044 sp.sched_priority = kthread_prio;
4045 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
4046 }
4047 wake_up_process(t);
4048 raw_spin_unlock_irqrestore(&rnp->lock, flags);
4049 }
4050 rcu_spawn_nocb_kthreads();
4051 rcu_spawn_boost_kthreads();
4052 return 0;
4053 }
4054 early_initcall(rcu_spawn_gp_kthread);
4055
4056 /*
4057 * This function is invoked towards the end of the scheduler's initialization
4058 * process. Before this is called, the idle task might contain
4059 * RCU read-side critical sections (during which time, this idle
4060 * task is booting the system). After this function is called, the
4061 * idle tasks are prohibited from containing RCU read-side critical
4062 * sections. This function also enables RCU lockdep checking.
4063 */
4064 void rcu_scheduler_starting(void)
4065 {
4066 WARN_ON(num_online_cpus() != 1);
4067 WARN_ON(nr_context_switches() > 0);
4068 rcu_scheduler_active = 1;
4069 }
4070
4071 /*
4072 * Compute the per-level fanout, either using the exact fanout specified
4073 * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
4074 */
4075 static void __init rcu_init_levelspread(int *levelspread, const int *levelcnt)
4076 {
4077 int i;
4078
4079 if (rcu_fanout_exact) {
4080 levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
4081 for (i = rcu_num_lvls - 2; i >= 0; i--)
4082 levelspread[i] = RCU_FANOUT;
4083 } else {
4084 int ccur;
4085 int cprv;
4086
4087 cprv = nr_cpu_ids;
4088 for (i = rcu_num_lvls - 1; i >= 0; i--) {
4089 ccur = levelcnt[i];
4090 levelspread[i] = (cprv + ccur - 1) / ccur;
4091 cprv = ccur;
4092 }
4093 }
4094 }
4095
4096 /*
4097 * Helper function for rcu_init() that initializes one rcu_state structure.
4098 */
4099 static void __init rcu_init_one(struct rcu_state *rsp,
4100 struct rcu_data __percpu *rda)
4101 {
4102 static const char * const buf[] = RCU_NODE_NAME_INIT;
4103 static const char * const fqs[] = RCU_FQS_NAME_INIT;
4104 static const char * const exp[] = RCU_EXP_NAME_INIT;
4105 static const char * const exp_sched[] = RCU_EXP_SCHED_NAME_INIT;
4106 static u8 fl_mask = 0x1;
4107
4108 int levelcnt[RCU_NUM_LVLS]; /* # nodes in each level. */
4109 int levelspread[RCU_NUM_LVLS]; /* kids/node in each level. */
4110 int cpustride = 1;
4111 int i;
4112 int j;
4113 struct rcu_node *rnp;
4114
4115 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */
4116
4117 /* Silence gcc 4.8 false positive about array index out of range. */
4118 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
4119 panic("rcu_init_one: rcu_num_lvls out of range");
4120
4121 /* Initialize the level-tracking arrays. */
4122
4123 for (i = 0; i < rcu_num_lvls; i++)
4124 levelcnt[i] = num_rcu_lvl[i];
4125 for (i = 1; i < rcu_num_lvls; i++)
4126 rsp->level[i] = rsp->level[i - 1] + levelcnt[i - 1];
4127 rcu_init_levelspread(levelspread, levelcnt);
4128 rsp->flavor_mask = fl_mask;
4129 fl_mask <<= 1;
4130
4131 /* Initialize the elements themselves, starting from the leaves. */
4132
4133 for (i = rcu_num_lvls - 1; i >= 0; i--) {
4134 cpustride *= levelspread[i];
4135 rnp = rsp->level[i];
4136 for (j = 0; j < levelcnt[i]; j++, rnp++) {
4137 raw_spin_lock_init(&rnp->lock);
4138 lockdep_set_class_and_name(&rnp->lock,
4139 &rcu_node_class[i], buf[i]);
4140 raw_spin_lock_init(&rnp->fqslock);
4141 lockdep_set_class_and_name(&rnp->fqslock,
4142 &rcu_fqs_class[i], fqs[i]);
4143 rnp->gpnum = rsp->gpnum;
4144 rnp->completed = rsp->completed;
4145 rnp->qsmask = 0;
4146 rnp->qsmaskinit = 0;
4147 rnp->grplo = j * cpustride;
4148 rnp->grphi = (j + 1) * cpustride - 1;
4149 if (rnp->grphi >= nr_cpu_ids)
4150 rnp->grphi = nr_cpu_ids - 1;
4151 if (i == 0) {
4152 rnp->grpnum = 0;
4153 rnp->grpmask = 0;
4154 rnp->parent = NULL;
4155 } else {
4156 rnp->grpnum = j % levelspread[i - 1];
4157 rnp->grpmask = 1UL << rnp->grpnum;
4158 rnp->parent = rsp->level[i - 1] +
4159 j / levelspread[i - 1];
4160 }
4161 rnp->level = i;
4162 INIT_LIST_HEAD(&rnp->blkd_tasks);
4163 rcu_init_one_nocb(rnp);
4164 mutex_init(&rnp->exp_funnel_mutex);
4165 if (rsp == &rcu_sched_state)
4166 lockdep_set_class_and_name(
4167 &rnp->exp_funnel_mutex,
4168 &rcu_exp_sched_class[i], exp_sched[i]);
4169 else
4170 lockdep_set_class_and_name(
4171 &rnp->exp_funnel_mutex,
4172 &rcu_exp_class[i], exp[i]);
4173 }
4174 }
4175
4176 init_waitqueue_head(&rsp->gp_wq);
4177 rnp = rsp->level[rcu_num_lvls - 1];
4178 for_each_possible_cpu(i) {
4179 while (i > rnp->grphi)
4180 rnp++;
4181 per_cpu_ptr(rsp->rda, i)->mynode = rnp;
4182 rcu_boot_init_percpu_data(i, rsp);
4183 }
4184 list_add(&rsp->flavors, &rcu_struct_flavors);
4185 }
4186
4187 /*
4188 * Compute the rcu_node tree geometry from kernel parameters. This cannot
4189 * replace the definitions in tree.h because those are needed to size
4190 * the ->node array in the rcu_state structure.
4191 */
4192 static void __init rcu_init_geometry(void)
4193 {
4194 ulong d;
4195 int i;
4196 int rcu_capacity[RCU_NUM_LVLS];
4197
4198 /*
4199 * Initialize any unspecified boot parameters.
4200 * The default values of jiffies_till_first_fqs and
4201 * jiffies_till_next_fqs are set to the RCU_JIFFIES_TILL_FORCE_QS
4202 * value, which is a function of HZ, then adding one for each
4203 * RCU_JIFFIES_FQS_DIV CPUs that might be on the system.
4204 */
4205 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
4206 if (jiffies_till_first_fqs == ULONG_MAX)
4207 jiffies_till_first_fqs = d;
4208 if (jiffies_till_next_fqs == ULONG_MAX)
4209 jiffies_till_next_fqs = d;
4210
4211 /* If the compile-time values are accurate, just leave. */
4212 if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
4213 nr_cpu_ids == NR_CPUS)
4214 return;
4215 pr_info("RCU: Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%d\n",
4216 rcu_fanout_leaf, nr_cpu_ids);
4217
4218 /*
4219 * The boot-time rcu_fanout_leaf parameter is only permitted
4220 * to increase the leaf-level fanout, not decrease it. Of course,
4221 * the leaf-level fanout cannot exceed the number of bits in
4222 * the rcu_node masks. Complain and fall back to the compile-
4223 * time values if these limits are exceeded.
4224 */
4225 if (rcu_fanout_leaf < RCU_FANOUT_LEAF ||
4226 rcu_fanout_leaf > sizeof(unsigned long) * 8) {
4227 rcu_fanout_leaf = RCU_FANOUT_LEAF;
4228 WARN_ON(1);
4229 return;
4230 }
4231
4232 /*
4233 * Compute number of nodes that can be handled an rcu_node tree
4234 * with the given number of levels.
4235 */
4236 rcu_capacity[0] = rcu_fanout_leaf;
4237 for (i = 1; i < RCU_NUM_LVLS; i++)
4238 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
4239
4240 /*
4241 * The tree must be able to accommodate the configured number of CPUs.
4242 * If this limit is exceeded than we have a serious problem elsewhere.
4243 */
4244 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1])
4245 panic("rcu_init_geometry: rcu_capacity[] is too small");
4246
4247 /* Calculate the number of levels in the tree. */
4248 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
4249 }
4250 rcu_num_lvls = i + 1;
4251
4252 /* Calculate the number of rcu_nodes at each level of the tree. */
4253 for (i = 0; i < rcu_num_lvls; i++) {
4254 int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
4255 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
4256 }
4257
4258 /* Calculate the total number of rcu_node structures. */
4259 rcu_num_nodes = 0;
4260 for (i = 0; i < rcu_num_lvls; i++)
4261 rcu_num_nodes += num_rcu_lvl[i];
4262 }
4263
4264 /*
4265 * Dump out the structure of the rcu_node combining tree associated
4266 * with the rcu_state structure referenced by rsp.
4267 */
4268 static void __init rcu_dump_rcu_node_tree(struct rcu_state *rsp)
4269 {
4270 int level = 0;
4271 struct rcu_node *rnp;
4272
4273 pr_info("rcu_node tree layout dump\n");
4274 pr_info(" ");
4275 rcu_for_each_node_breadth_first(rsp, rnp) {
4276 if (rnp->level != level) {
4277 pr_cont("\n");
4278 pr_info(" ");
4279 level = rnp->level;
4280 }
4281 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
4282 }
4283 pr_cont("\n");
4284 }
4285
4286 void __init rcu_init(void)
4287 {
4288 int cpu;
4289
4290 rcu_early_boot_tests();
4291
4292 rcu_bootup_announce();
4293 rcu_init_geometry();
4294 rcu_init_one(&rcu_bh_state, &rcu_bh_data);
4295 rcu_init_one(&rcu_sched_state, &rcu_sched_data);
4296 if (dump_tree)
4297 rcu_dump_rcu_node_tree(&rcu_sched_state);
4298 __rcu_init_preempt();
4299 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
4300
4301 /*
4302 * We don't need protection against CPU-hotplug here because
4303 * this is called early in boot, before either interrupts
4304 * or the scheduler are operational.
4305 */
4306 cpu_notifier(rcu_cpu_notify, 0);
4307 pm_notifier(rcu_pm_notify, 0);
4308 for_each_online_cpu(cpu)
4309 rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
4310 }
4311
4312 #include "tree_plugin.h"