rcu: Settle config for userspace extended quiescent state
[firefly-linux-kernel-4.4.55.git] / kernel / rcutree.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, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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/percpu.h>
45 #include <linux/notifier.h>
46 #include <linux/cpu.h>
47 #include <linux/mutex.h>
48 #include <linux/time.h>
49 #include <linux/kernel_stat.h>
50 #include <linux/wait.h>
51 #include <linux/kthread.h>
52 #include <linux/prefetch.h>
53 #include <linux/delay.h>
54 #include <linux/stop_machine.h>
55 #include <linux/random.h>
56
57 #include "rcutree.h"
58 #include <trace/events/rcu.h>
59
60 #include "rcu.h"
61
62 /* Data structures. */
63
64 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
65 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
66
67 #define RCU_STATE_INITIALIZER(sname, cr) { \
68         .level = { &sname##_state.node[0] }, \
69         .call = cr, \
70         .fqs_state = RCU_GP_IDLE, \
71         .gpnum = -300, \
72         .completed = -300, \
73         .onofflock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.onofflock), \
74         .orphan_nxttail = &sname##_state.orphan_nxtlist, \
75         .orphan_donetail = &sname##_state.orphan_donelist, \
76         .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \
77         .name = #sname, \
78 }
79
80 struct rcu_state rcu_sched_state =
81         RCU_STATE_INITIALIZER(rcu_sched, call_rcu_sched);
82 DEFINE_PER_CPU(struct rcu_data, rcu_sched_data);
83
84 struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh, call_rcu_bh);
85 DEFINE_PER_CPU(struct rcu_data, rcu_bh_data);
86
87 static struct rcu_state *rcu_state;
88 LIST_HEAD(rcu_struct_flavors);
89
90 /* Increase (but not decrease) the CONFIG_RCU_FANOUT_LEAF at boot time. */
91 static int rcu_fanout_leaf = CONFIG_RCU_FANOUT_LEAF;
92 module_param(rcu_fanout_leaf, int, 0444);
93 int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
94 static int num_rcu_lvl[] = {  /* Number of rcu_nodes at specified level. */
95         NUM_RCU_LVL_0,
96         NUM_RCU_LVL_1,
97         NUM_RCU_LVL_2,
98         NUM_RCU_LVL_3,
99         NUM_RCU_LVL_4,
100 };
101 int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */
102
103 /*
104  * The rcu_scheduler_active variable transitions from zero to one just
105  * before the first task is spawned.  So when this variable is zero, RCU
106  * can assume that there is but one task, allowing RCU to (for example)
107  * optimized synchronize_sched() to a simple barrier().  When this variable
108  * is one, RCU must actually do all the hard work required to detect real
109  * grace periods.  This variable is also used to suppress boot-time false
110  * positives from lockdep-RCU error checking.
111  */
112 int rcu_scheduler_active __read_mostly;
113 EXPORT_SYMBOL_GPL(rcu_scheduler_active);
114
115 /*
116  * The rcu_scheduler_fully_active variable transitions from zero to one
117  * during the early_initcall() processing, which is after the scheduler
118  * is capable of creating new tasks.  So RCU processing (for example,
119  * creating tasks for RCU priority boosting) must be delayed until after
120  * rcu_scheduler_fully_active transitions from zero to one.  We also
121  * currently delay invocation of any RCU callbacks until after this point.
122  *
123  * It might later prove better for people registering RCU callbacks during
124  * early boot to take responsibility for these callbacks, but one step at
125  * a time.
126  */
127 static int rcu_scheduler_fully_active __read_mostly;
128
129 #ifdef CONFIG_RCU_BOOST
130
131 /*
132  * Control variables for per-CPU and per-rcu_node kthreads.  These
133  * handle all flavors of RCU.
134  */
135 static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
136 DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
137 DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
138 DEFINE_PER_CPU(char, rcu_cpu_has_work);
139
140 #endif /* #ifdef CONFIG_RCU_BOOST */
141
142 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
143 static void invoke_rcu_core(void);
144 static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp);
145
146 /*
147  * Track the rcutorture test sequence number and the update version
148  * number within a given test.  The rcutorture_testseq is incremented
149  * on every rcutorture module load and unload, so has an odd value
150  * when a test is running.  The rcutorture_vernum is set to zero
151  * when rcutorture starts and is incremented on each rcutorture update.
152  * These variables enable correlating rcutorture output with the
153  * RCU tracing information.
154  */
155 unsigned long rcutorture_testseq;
156 unsigned long rcutorture_vernum;
157
158 /*
159  * Return true if an RCU grace period is in progress.  The ACCESS_ONCE()s
160  * permit this function to be invoked without holding the root rcu_node
161  * structure's ->lock, but of course results can be subject to change.
162  */
163 static int rcu_gp_in_progress(struct rcu_state *rsp)
164 {
165         return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
166 }
167
168 /*
169  * Note a quiescent state.  Because we do not need to know
170  * how many quiescent states passed, just if there was at least
171  * one since the start of the grace period, this just sets a flag.
172  * The caller must have disabled preemption.
173  */
174 void rcu_sched_qs(int cpu)
175 {
176         struct rcu_data *rdp = &per_cpu(rcu_sched_data, cpu);
177
178         if (rdp->passed_quiesce == 0)
179                 trace_rcu_grace_period("rcu_sched", rdp->gpnum, "cpuqs");
180         rdp->passed_quiesce = 1;
181 }
182
183 void rcu_bh_qs(int cpu)
184 {
185         struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);
186
187         if (rdp->passed_quiesce == 0)
188                 trace_rcu_grace_period("rcu_bh", rdp->gpnum, "cpuqs");
189         rdp->passed_quiesce = 1;
190 }
191
192 /*
193  * Note a context switch.  This is a quiescent state for RCU-sched,
194  * and requires special handling for preemptible RCU.
195  * The caller must have disabled preemption.
196  */
197 void rcu_note_context_switch(int cpu)
198 {
199         trace_rcu_utilization("Start context switch");
200         rcu_sched_qs(cpu);
201         rcu_preempt_note_context_switch(cpu);
202         trace_rcu_utilization("End context switch");
203 }
204 EXPORT_SYMBOL_GPL(rcu_note_context_switch);
205
206 DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
207         .dynticks_nesting = DYNTICK_TASK_EXIT_IDLE,
208         .dynticks = ATOMIC_INIT(1),
209 };
210
211 static int blimit = 10;         /* Maximum callbacks per rcu_do_batch. */
212 static int qhimark = 10000;     /* If this many pending, ignore blimit. */
213 static int qlowmark = 100;      /* Once only this many pending, use blimit. */
214
215 module_param(blimit, int, 0444);
216 module_param(qhimark, int, 0444);
217 module_param(qlowmark, int, 0444);
218
219 int rcu_cpu_stall_suppress __read_mostly; /* 1 = suppress stall warnings. */
220 int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
221
222 module_param(rcu_cpu_stall_suppress, int, 0644);
223 module_param(rcu_cpu_stall_timeout, int, 0644);
224
225 static ulong jiffies_till_first_fqs = RCU_JIFFIES_TILL_FORCE_QS;
226 static ulong jiffies_till_next_fqs = RCU_JIFFIES_TILL_FORCE_QS;
227
228 module_param(jiffies_till_first_fqs, ulong, 0644);
229 module_param(jiffies_till_next_fqs, ulong, 0644);
230
231 static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *));
232 static void force_quiescent_state(struct rcu_state *rsp);
233 static int rcu_pending(int cpu);
234
235 /*
236  * Return the number of RCU-sched batches processed thus far for debug & stats.
237  */
238 long rcu_batches_completed_sched(void)
239 {
240         return rcu_sched_state.completed;
241 }
242 EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
243
244 /*
245  * Return the number of RCU BH batches processed thus far for debug & stats.
246  */
247 long rcu_batches_completed_bh(void)
248 {
249         return rcu_bh_state.completed;
250 }
251 EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
252
253 /*
254  * Force a quiescent state for RCU BH.
255  */
256 void rcu_bh_force_quiescent_state(void)
257 {
258         force_quiescent_state(&rcu_bh_state);
259 }
260 EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
261
262 /*
263  * Record the number of times rcutorture tests have been initiated and
264  * terminated.  This information allows the debugfs tracing stats to be
265  * correlated to the rcutorture messages, even when the rcutorture module
266  * is being repeatedly loaded and unloaded.  In other words, we cannot
267  * store this state in rcutorture itself.
268  */
269 void rcutorture_record_test_transition(void)
270 {
271         rcutorture_testseq++;
272         rcutorture_vernum = 0;
273 }
274 EXPORT_SYMBOL_GPL(rcutorture_record_test_transition);
275
276 /*
277  * Record the number of writer passes through the current rcutorture test.
278  * This is also used to correlate debugfs tracing stats with the rcutorture
279  * messages.
280  */
281 void rcutorture_record_progress(unsigned long vernum)
282 {
283         rcutorture_vernum++;
284 }
285 EXPORT_SYMBOL_GPL(rcutorture_record_progress);
286
287 /*
288  * Force a quiescent state for RCU-sched.
289  */
290 void rcu_sched_force_quiescent_state(void)
291 {
292         force_quiescent_state(&rcu_sched_state);
293 }
294 EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state);
295
296 /*
297  * Does the CPU have callbacks ready to be invoked?
298  */
299 static int
300 cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
301 {
302         return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
303 }
304
305 /*
306  * Does the current CPU require a yet-as-unscheduled grace period?
307  */
308 static int
309 cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
310 {
311         return *rdp->nxttail[RCU_DONE_TAIL +
312                              ACCESS_ONCE(rsp->completed) != rdp->completed] &&
313                !rcu_gp_in_progress(rsp);
314 }
315
316 /*
317  * Return the root node of the specified rcu_state structure.
318  */
319 static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
320 {
321         return &rsp->node[0];
322 }
323
324 /*
325  * rcu_eqs_enter_common - current CPU is moving towards extended quiescent state
326  *
327  * If the new value of the ->dynticks_nesting counter now is zero,
328  * we really have entered idle, and must do the appropriate accounting.
329  * The caller must have disabled interrupts.
330  */
331 static void rcu_eqs_enter_common(struct rcu_dynticks *rdtp, long long oldval,
332                                 bool user)
333 {
334         trace_rcu_dyntick("Start", oldval, 0);
335         if (!is_idle_task(current) && !user) {
336                 struct task_struct *idle = idle_task(smp_processor_id());
337
338                 trace_rcu_dyntick("Error on entry: not idle task", oldval, 0);
339                 ftrace_dump(DUMP_ORIG);
340                 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
341                           current->pid, current->comm,
342                           idle->pid, idle->comm); /* must be idle task! */
343         }
344         rcu_prepare_for_idle(smp_processor_id());
345         /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
346         smp_mb__before_atomic_inc();  /* See above. */
347         atomic_inc(&rdtp->dynticks);
348         smp_mb__after_atomic_inc();  /* Force ordering with next sojourn. */
349         WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
350
351         /*
352          * It is illegal to enter an extended quiescent state while
353          * in an RCU read-side critical section.
354          */
355         rcu_lockdep_assert(!lock_is_held(&rcu_lock_map),
356                            "Illegal idle entry in RCU read-side critical section.");
357         rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map),
358                            "Illegal idle entry in RCU-bh read-side critical section.");
359         rcu_lockdep_assert(!lock_is_held(&rcu_sched_lock_map),
360                            "Illegal idle entry in RCU-sched read-side critical section.");
361 }
362
363 /*
364  * Enter an RCU extended quiescent state, which can be either the
365  * idle loop or adaptive-tickless usermode execution.
366  */
367 static void rcu_eqs_enter(bool user)
368 {
369         unsigned long flags;
370         long long oldval;
371         struct rcu_dynticks *rdtp;
372
373         local_irq_save(flags);
374         rdtp = &__get_cpu_var(rcu_dynticks);
375         oldval = rdtp->dynticks_nesting;
376         WARN_ON_ONCE((oldval & DYNTICK_TASK_NEST_MASK) == 0);
377         if ((oldval & DYNTICK_TASK_NEST_MASK) == DYNTICK_TASK_NEST_VALUE)
378                 rdtp->dynticks_nesting = 0;
379         else
380                 rdtp->dynticks_nesting -= DYNTICK_TASK_NEST_VALUE;
381         rcu_eqs_enter_common(rdtp, oldval, user);
382         local_irq_restore(flags);
383 }
384
385 /**
386  * rcu_idle_enter - inform RCU that current CPU is entering idle
387  *
388  * Enter idle mode, in other words, -leave- the mode in which RCU
389  * read-side critical sections can occur.  (Though RCU read-side
390  * critical sections can occur in irq handlers in idle, a possibility
391  * handled by irq_enter() and irq_exit().)
392  *
393  * We crowbar the ->dynticks_nesting field to zero to allow for
394  * the possibility of usermode upcalls having messed up our count
395  * of interrupt nesting level during the prior busy period.
396  */
397 void rcu_idle_enter(void)
398 {
399         rcu_eqs_enter(0);
400 }
401 EXPORT_SYMBOL_GPL(rcu_idle_enter);
402
403 #ifdef CONFIG_RCU_USER_QS
404 /**
405  * rcu_user_enter - inform RCU that we are resuming userspace.
406  *
407  * Enter RCU idle mode right before resuming userspace.  No use of RCU
408  * is permitted between this call and rcu_user_exit(). This way the
409  * CPU doesn't need to maintain the tick for RCU maintenance purposes
410  * when the CPU runs in userspace.
411  */
412 void rcu_user_enter(void)
413 {
414         /*
415          * Some contexts may involve an exception occuring in an irq,
416          * leading to that nesting:
417          * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
418          * This would mess up the dyntick_nesting count though. And rcu_irq_*()
419          * helpers are enough to protect RCU uses inside the exception. So
420          * just return immediately if we detect we are in an IRQ.
421          */
422         if (in_interrupt())
423                 return;
424
425         rcu_eqs_enter(1);
426 }
427
428 /**
429  * rcu_user_enter_after_irq - inform RCU that we are going to resume userspace
430  * after the current irq returns.
431  *
432  * This is similar to rcu_user_enter() but in the context of a non-nesting
433  * irq. After this call, RCU enters into idle mode when the interrupt
434  * returns.
435  */
436 void rcu_user_enter_after_irq(void)
437 {
438         unsigned long flags;
439         struct rcu_dynticks *rdtp;
440
441         local_irq_save(flags);
442         rdtp = &__get_cpu_var(rcu_dynticks);
443         /* Ensure this irq is interrupting a non-idle RCU state.  */
444         WARN_ON_ONCE(!(rdtp->dynticks_nesting & DYNTICK_TASK_MASK));
445         rdtp->dynticks_nesting = 1;
446         local_irq_restore(flags);
447 }
448 #endif /* CONFIG_RCU_USER_QS */
449
450 /**
451  * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle
452  *
453  * Exit from an interrupt handler, which might possibly result in entering
454  * idle mode, in other words, leaving the mode in which read-side critical
455  * sections can occur.
456  *
457  * This code assumes that the idle loop never does anything that might
458  * result in unbalanced calls to irq_enter() and irq_exit().  If your
459  * architecture violates this assumption, RCU will give you what you
460  * deserve, good and hard.  But very infrequently and irreproducibly.
461  *
462  * Use things like work queues to work around this limitation.
463  *
464  * You have been warned.
465  */
466 void rcu_irq_exit(void)
467 {
468         unsigned long flags;
469         long long oldval;
470         struct rcu_dynticks *rdtp;
471
472         local_irq_save(flags);
473         rdtp = &__get_cpu_var(rcu_dynticks);
474         oldval = rdtp->dynticks_nesting;
475         rdtp->dynticks_nesting--;
476         WARN_ON_ONCE(rdtp->dynticks_nesting < 0);
477         if (rdtp->dynticks_nesting)
478                 trace_rcu_dyntick("--=", oldval, rdtp->dynticks_nesting);
479         else
480                 rcu_eqs_enter_common(rdtp, oldval, 1);
481         local_irq_restore(flags);
482 }
483
484 /*
485  * rcu_eqs_exit_common - current CPU moving away from extended quiescent state
486  *
487  * If the new value of the ->dynticks_nesting counter was previously zero,
488  * we really have exited idle, and must do the appropriate accounting.
489  * The caller must have disabled interrupts.
490  */
491 static void rcu_eqs_exit_common(struct rcu_dynticks *rdtp, long long oldval,
492                                int user)
493 {
494         smp_mb__before_atomic_inc();  /* Force ordering w/previous sojourn. */
495         atomic_inc(&rdtp->dynticks);
496         /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
497         smp_mb__after_atomic_inc();  /* See above. */
498         WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
499         rcu_cleanup_after_idle(smp_processor_id());
500         trace_rcu_dyntick("End", oldval, rdtp->dynticks_nesting);
501         if (!is_idle_task(current) && !user) {
502                 struct task_struct *idle = idle_task(smp_processor_id());
503
504                 trace_rcu_dyntick("Error on exit: not idle task",
505                                   oldval, rdtp->dynticks_nesting);
506                 ftrace_dump(DUMP_ORIG);
507                 WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s",
508                           current->pid, current->comm,
509                           idle->pid, idle->comm); /* must be idle task! */
510         }
511 }
512
513 /*
514  * Exit an RCU extended quiescent state, which can be either the
515  * idle loop or adaptive-tickless usermode execution.
516  */
517 static void rcu_eqs_exit(bool user)
518 {
519         unsigned long flags;
520         struct rcu_dynticks *rdtp;
521         long long oldval;
522
523         local_irq_save(flags);
524         rdtp = &__get_cpu_var(rcu_dynticks);
525         oldval = rdtp->dynticks_nesting;
526         WARN_ON_ONCE(oldval < 0);
527         if (oldval & DYNTICK_TASK_NEST_MASK)
528                 rdtp->dynticks_nesting += DYNTICK_TASK_NEST_VALUE;
529         else
530                 rdtp->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
531         rcu_eqs_exit_common(rdtp, oldval, user);
532         local_irq_restore(flags);
533 }
534
535 /**
536  * rcu_idle_exit - inform RCU that current CPU is leaving idle
537  *
538  * Exit idle mode, in other words, -enter- the mode in which RCU
539  * read-side critical sections can occur.
540  *
541  * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to
542  * allow for the possibility of usermode upcalls messing up our count
543  * of interrupt nesting level during the busy period that is just
544  * now starting.
545  */
546 void rcu_idle_exit(void)
547 {
548         rcu_eqs_exit(0);
549 }
550 EXPORT_SYMBOL_GPL(rcu_idle_exit);
551
552 #ifdef CONFIG_RCU_USER_QS
553 /**
554  * rcu_user_exit - inform RCU that we are exiting userspace.
555  *
556  * Exit RCU idle mode while entering the kernel because it can
557  * run a RCU read side critical section anytime.
558  */
559 void rcu_user_exit(void)
560 {
561         /*
562          * Some contexts may involve an exception occuring in an irq,
563          * leading to that nesting:
564          * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
565          * This would mess up the dyntick_nesting count though. And rcu_irq_*()
566          * helpers are enough to protect RCU uses inside the exception. So
567          * just return immediately if we detect we are in an IRQ.
568          */
569         if (in_interrupt())
570                 return;
571
572         rcu_eqs_exit(1);
573 }
574
575 /**
576  * rcu_user_exit_after_irq - inform RCU that we won't resume to userspace
577  * idle mode after the current non-nesting irq returns.
578  *
579  * This is similar to rcu_user_exit() but in the context of an irq.
580  * This is called when the irq has interrupted a userspace RCU idle mode
581  * context. When the current non-nesting interrupt returns after this call,
582  * the CPU won't restore the RCU idle mode.
583  */
584 void rcu_user_exit_after_irq(void)
585 {
586         unsigned long flags;
587         struct rcu_dynticks *rdtp;
588
589         local_irq_save(flags);
590         rdtp = &__get_cpu_var(rcu_dynticks);
591         /* Ensure we are interrupting an RCU idle mode. */
592         WARN_ON_ONCE(rdtp->dynticks_nesting & DYNTICK_TASK_NEST_MASK);
593         rdtp->dynticks_nesting += DYNTICK_TASK_EXIT_IDLE;
594         local_irq_restore(flags);
595 }
596 #endif /* CONFIG_RCU_USER_QS */
597
598 /**
599  * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle
600  *
601  * Enter an interrupt handler, which might possibly result in exiting
602  * idle mode, in other words, entering the mode in which read-side critical
603  * sections can occur.
604  *
605  * Note that the Linux kernel is fully capable of entering an interrupt
606  * handler that it never exits, for example when doing upcalls to
607  * user mode!  This code assumes that the idle loop never does upcalls to
608  * user mode.  If your architecture does do upcalls from the idle loop (or
609  * does anything else that results in unbalanced calls to the irq_enter()
610  * and irq_exit() functions), RCU will give you what you deserve, good
611  * and hard.  But very infrequently and irreproducibly.
612  *
613  * Use things like work queues to work around this limitation.
614  *
615  * You have been warned.
616  */
617 void rcu_irq_enter(void)
618 {
619         unsigned long flags;
620         struct rcu_dynticks *rdtp;
621         long long oldval;
622
623         local_irq_save(flags);
624         rdtp = &__get_cpu_var(rcu_dynticks);
625         oldval = rdtp->dynticks_nesting;
626         rdtp->dynticks_nesting++;
627         WARN_ON_ONCE(rdtp->dynticks_nesting == 0);
628         if (oldval)
629                 trace_rcu_dyntick("++=", oldval, rdtp->dynticks_nesting);
630         else
631                 rcu_eqs_exit_common(rdtp, oldval, 1);
632         local_irq_restore(flags);
633 }
634
635 /**
636  * rcu_nmi_enter - inform RCU of entry to NMI context
637  *
638  * If the CPU was idle with dynamic ticks active, and there is no
639  * irq handler running, this updates rdtp->dynticks_nmi to let the
640  * RCU grace-period handling know that the CPU is active.
641  */
642 void rcu_nmi_enter(void)
643 {
644         struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
645
646         if (rdtp->dynticks_nmi_nesting == 0 &&
647             (atomic_read(&rdtp->dynticks) & 0x1))
648                 return;
649         rdtp->dynticks_nmi_nesting++;
650         smp_mb__before_atomic_inc();  /* Force delay from prior write. */
651         atomic_inc(&rdtp->dynticks);
652         /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */
653         smp_mb__after_atomic_inc();  /* See above. */
654         WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1));
655 }
656
657 /**
658  * rcu_nmi_exit - inform RCU of exit from NMI context
659  *
660  * If the CPU was idle with dynamic ticks active, and there is no
661  * irq handler running, this updates rdtp->dynticks_nmi to let the
662  * RCU grace-period handling know that the CPU is no longer active.
663  */
664 void rcu_nmi_exit(void)
665 {
666         struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
667
668         if (rdtp->dynticks_nmi_nesting == 0 ||
669             --rdtp->dynticks_nmi_nesting != 0)
670                 return;
671         /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */
672         smp_mb__before_atomic_inc();  /* See above. */
673         atomic_inc(&rdtp->dynticks);
674         smp_mb__after_atomic_inc();  /* Force delay to next write. */
675         WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1);
676 }
677
678 /**
679  * rcu_is_cpu_idle - see if RCU thinks that the current CPU is idle
680  *
681  * If the current CPU is in its idle loop and is neither in an interrupt
682  * or NMI handler, return true.
683  */
684 int rcu_is_cpu_idle(void)
685 {
686         int ret;
687
688         preempt_disable();
689         ret = (atomic_read(&__get_cpu_var(rcu_dynticks).dynticks) & 0x1) == 0;
690         preempt_enable();
691         return ret;
692 }
693 EXPORT_SYMBOL(rcu_is_cpu_idle);
694
695 #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
696
697 /*
698  * Is the current CPU online?  Disable preemption to avoid false positives
699  * that could otherwise happen due to the current CPU number being sampled,
700  * this task being preempted, its old CPU being taken offline, resuming
701  * on some other CPU, then determining that its old CPU is now offline.
702  * It is OK to use RCU on an offline processor during initial boot, hence
703  * the check for rcu_scheduler_fully_active.  Note also that it is OK
704  * for a CPU coming online to use RCU for one jiffy prior to marking itself
705  * online in the cpu_online_mask.  Similarly, it is OK for a CPU going
706  * offline to continue to use RCU for one jiffy after marking itself
707  * offline in the cpu_online_mask.  This leniency is necessary given the
708  * non-atomic nature of the online and offline processing, for example,
709  * the fact that a CPU enters the scheduler after completing the CPU_DYING
710  * notifiers.
711  *
712  * This is also why RCU internally marks CPUs online during the
713  * CPU_UP_PREPARE phase and offline during the CPU_DEAD phase.
714  *
715  * Disable checking if in an NMI handler because we cannot safely report
716  * errors from NMI handlers anyway.
717  */
718 bool rcu_lockdep_current_cpu_online(void)
719 {
720         struct rcu_data *rdp;
721         struct rcu_node *rnp;
722         bool ret;
723
724         if (in_nmi())
725                 return 1;
726         preempt_disable();
727         rdp = &__get_cpu_var(rcu_sched_data);
728         rnp = rdp->mynode;
729         ret = (rdp->grpmask & rnp->qsmaskinit) ||
730               !rcu_scheduler_fully_active;
731         preempt_enable();
732         return ret;
733 }
734 EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
735
736 #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */
737
738 /**
739  * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle
740  *
741  * If the current CPU is idle or running at a first-level (not nested)
742  * interrupt from idle, return true.  The caller must have at least
743  * disabled preemption.
744  */
745 int rcu_is_cpu_rrupt_from_idle(void)
746 {
747         return __get_cpu_var(rcu_dynticks).dynticks_nesting <= 1;
748 }
749
750 /*
751  * Snapshot the specified CPU's dynticks counter so that we can later
752  * credit them with an implicit quiescent state.  Return 1 if this CPU
753  * is in dynticks idle mode, which is an extended quiescent state.
754  */
755 static int dyntick_save_progress_counter(struct rcu_data *rdp)
756 {
757         rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks);
758         return (rdp->dynticks_snap & 0x1) == 0;
759 }
760
761 /*
762  * Return true if the specified CPU has passed through a quiescent
763  * state by virtue of being in or having passed through an dynticks
764  * idle state since the last call to dyntick_save_progress_counter()
765  * for this same CPU, or by virtue of having been offline.
766  */
767 static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
768 {
769         unsigned int curr;
770         unsigned int snap;
771
772         curr = (unsigned int)atomic_add_return(0, &rdp->dynticks->dynticks);
773         snap = (unsigned int)rdp->dynticks_snap;
774
775         /*
776          * If the CPU passed through or entered a dynticks idle phase with
777          * no active irq/NMI handlers, then we can safely pretend that the CPU
778          * already acknowledged the request to pass through a quiescent
779          * state.  Either way, that CPU cannot possibly be in an RCU
780          * read-side critical section that started before the beginning
781          * of the current RCU grace period.
782          */
783         if ((curr & 0x1) == 0 || UINT_CMP_GE(curr, snap + 2)) {
784                 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, "dti");
785                 rdp->dynticks_fqs++;
786                 return 1;
787         }
788
789         /*
790          * Check for the CPU being offline, but only if the grace period
791          * is old enough.  We don't need to worry about the CPU changing
792          * state: If we see it offline even once, it has been through a
793          * quiescent state.
794          *
795          * The reason for insisting that the grace period be at least
796          * one jiffy old is that CPUs that are not quite online and that
797          * have just gone offline can still execute RCU read-side critical
798          * sections.
799          */
800         if (ULONG_CMP_GE(rdp->rsp->gp_start + 2, jiffies))
801                 return 0;  /* Grace period is not old enough. */
802         barrier();
803         if (cpu_is_offline(rdp->cpu)) {
804                 trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, "ofl");
805                 rdp->offline_fqs++;
806                 return 1;
807         }
808         return 0;
809 }
810
811 static int jiffies_till_stall_check(void)
812 {
813         int till_stall_check = ACCESS_ONCE(rcu_cpu_stall_timeout);
814
815         /*
816          * Limit check must be consistent with the Kconfig limits
817          * for CONFIG_RCU_CPU_STALL_TIMEOUT.
818          */
819         if (till_stall_check < 3) {
820                 ACCESS_ONCE(rcu_cpu_stall_timeout) = 3;
821                 till_stall_check = 3;
822         } else if (till_stall_check > 300) {
823                 ACCESS_ONCE(rcu_cpu_stall_timeout) = 300;
824                 till_stall_check = 300;
825         }
826         return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
827 }
828
829 static void record_gp_stall_check_time(struct rcu_state *rsp)
830 {
831         rsp->gp_start = jiffies;
832         rsp->jiffies_stall = jiffies + jiffies_till_stall_check();
833 }
834
835 static void print_other_cpu_stall(struct rcu_state *rsp)
836 {
837         int cpu;
838         long delta;
839         unsigned long flags;
840         int ndetected = 0;
841         struct rcu_node *rnp = rcu_get_root(rsp);
842
843         /* Only let one CPU complain about others per time interval. */
844
845         raw_spin_lock_irqsave(&rnp->lock, flags);
846         delta = jiffies - rsp->jiffies_stall;
847         if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
848                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
849                 return;
850         }
851         rsp->jiffies_stall = jiffies + 3 * jiffies_till_stall_check() + 3;
852         raw_spin_unlock_irqrestore(&rnp->lock, flags);
853
854         /*
855          * OK, time to rat on our buddy...
856          * See Documentation/RCU/stallwarn.txt for info on how to debug
857          * RCU CPU stall warnings.
858          */
859         printk(KERN_ERR "INFO: %s detected stalls on CPUs/tasks:",
860                rsp->name);
861         print_cpu_stall_info_begin();
862         rcu_for_each_leaf_node(rsp, rnp) {
863                 raw_spin_lock_irqsave(&rnp->lock, flags);
864                 ndetected += rcu_print_task_stall(rnp);
865                 if (rnp->qsmask != 0) {
866                         for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++)
867                                 if (rnp->qsmask & (1UL << cpu)) {
868                                         print_cpu_stall_info(rsp,
869                                                              rnp->grplo + cpu);
870                                         ndetected++;
871                                 }
872                 }
873                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
874         }
875
876         /*
877          * Now rat on any tasks that got kicked up to the root rcu_node
878          * due to CPU offlining.
879          */
880         rnp = rcu_get_root(rsp);
881         raw_spin_lock_irqsave(&rnp->lock, flags);
882         ndetected += rcu_print_task_stall(rnp);
883         raw_spin_unlock_irqrestore(&rnp->lock, flags);
884
885         print_cpu_stall_info_end();
886         printk(KERN_CONT "(detected by %d, t=%ld jiffies)\n",
887                smp_processor_id(), (long)(jiffies - rsp->gp_start));
888         if (ndetected == 0)
889                 printk(KERN_ERR "INFO: Stall ended before state dump start\n");
890         else if (!trigger_all_cpu_backtrace())
891                 dump_stack();
892
893         /* Complain about tasks blocking the grace period. */
894
895         rcu_print_detail_task_stall(rsp);
896
897         force_quiescent_state(rsp);  /* Kick them all. */
898 }
899
900 static void print_cpu_stall(struct rcu_state *rsp)
901 {
902         unsigned long flags;
903         struct rcu_node *rnp = rcu_get_root(rsp);
904
905         /*
906          * OK, time to rat on ourselves...
907          * See Documentation/RCU/stallwarn.txt for info on how to debug
908          * RCU CPU stall warnings.
909          */
910         printk(KERN_ERR "INFO: %s self-detected stall on CPU", rsp->name);
911         print_cpu_stall_info_begin();
912         print_cpu_stall_info(rsp, smp_processor_id());
913         print_cpu_stall_info_end();
914         printk(KERN_CONT " (t=%lu jiffies)\n", jiffies - rsp->gp_start);
915         if (!trigger_all_cpu_backtrace())
916                 dump_stack();
917
918         raw_spin_lock_irqsave(&rnp->lock, flags);
919         if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall))
920                 rsp->jiffies_stall = jiffies +
921                                      3 * jiffies_till_stall_check() + 3;
922         raw_spin_unlock_irqrestore(&rnp->lock, flags);
923
924         set_need_resched();  /* kick ourselves to get things going. */
925 }
926
927 static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
928 {
929         unsigned long j;
930         unsigned long js;
931         struct rcu_node *rnp;
932
933         if (rcu_cpu_stall_suppress)
934                 return;
935         j = ACCESS_ONCE(jiffies);
936         js = ACCESS_ONCE(rsp->jiffies_stall);
937         rnp = rdp->mynode;
938         if (rcu_gp_in_progress(rsp) &&
939             (ACCESS_ONCE(rnp->qsmask) & rdp->grpmask) && ULONG_CMP_GE(j, js)) {
940
941                 /* We haven't checked in, so go dump stack. */
942                 print_cpu_stall(rsp);
943
944         } else if (rcu_gp_in_progress(rsp) &&
945                    ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) {
946
947                 /* They had a few time units to dump stack, so complain. */
948                 print_other_cpu_stall(rsp);
949         }
950 }
951
952 static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
953 {
954         rcu_cpu_stall_suppress = 1;
955         return NOTIFY_DONE;
956 }
957
958 /**
959  * rcu_cpu_stall_reset - prevent further stall warnings in current grace period
960  *
961  * Set the stall-warning timeout way off into the future, thus preventing
962  * any RCU CPU stall-warning messages from appearing in the current set of
963  * RCU grace periods.
964  *
965  * The caller must disable hard irqs.
966  */
967 void rcu_cpu_stall_reset(void)
968 {
969         struct rcu_state *rsp;
970
971         for_each_rcu_flavor(rsp)
972                 rsp->jiffies_stall = jiffies + ULONG_MAX / 2;
973 }
974
975 static struct notifier_block rcu_panic_block = {
976         .notifier_call = rcu_panic,
977 };
978
979 static void __init check_cpu_stall_init(void)
980 {
981         atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
982 }
983
984 /*
985  * Update CPU-local rcu_data state to record the newly noticed grace period.
986  * This is used both when we started the grace period and when we notice
987  * that someone else started the grace period.  The caller must hold the
988  * ->lock of the leaf rcu_node structure corresponding to the current CPU,
989  *  and must have irqs disabled.
990  */
991 static void __note_new_gpnum(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
992 {
993         if (rdp->gpnum != rnp->gpnum) {
994                 /*
995                  * If the current grace period is waiting for this CPU,
996                  * set up to detect a quiescent state, otherwise don't
997                  * go looking for one.
998                  */
999                 rdp->gpnum = rnp->gpnum;
1000                 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpustart");
1001                 rdp->passed_quiesce = 0;
1002                 rdp->qs_pending = !!(rnp->qsmask & rdp->grpmask);
1003                 zero_cpu_stall_ticks(rdp);
1004         }
1005 }
1006
1007 static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
1008 {
1009         unsigned long flags;
1010         struct rcu_node *rnp;
1011
1012         local_irq_save(flags);
1013         rnp = rdp->mynode;
1014         if (rdp->gpnum == ACCESS_ONCE(rnp->gpnum) || /* outside lock. */
1015             !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
1016                 local_irq_restore(flags);
1017                 return;
1018         }
1019         __note_new_gpnum(rsp, rnp, rdp);
1020         raw_spin_unlock_irqrestore(&rnp->lock, flags);
1021 }
1022
1023 /*
1024  * Did someone else start a new RCU grace period start since we last
1025  * checked?  Update local state appropriately if so.  Must be called
1026  * on the CPU corresponding to rdp.
1027  */
1028 static int
1029 check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
1030 {
1031         unsigned long flags;
1032         int ret = 0;
1033
1034         local_irq_save(flags);
1035         if (rdp->gpnum != rsp->gpnum) {
1036                 note_new_gpnum(rsp, rdp);
1037                 ret = 1;
1038         }
1039         local_irq_restore(flags);
1040         return ret;
1041 }
1042
1043 /*
1044  * Initialize the specified rcu_data structure's callback list to empty.
1045  */
1046 static void init_callback_list(struct rcu_data *rdp)
1047 {
1048         int i;
1049
1050         rdp->nxtlist = NULL;
1051         for (i = 0; i < RCU_NEXT_SIZE; i++)
1052                 rdp->nxttail[i] = &rdp->nxtlist;
1053 }
1054
1055 /*
1056  * Advance this CPU's callbacks, but only if the current grace period
1057  * has ended.  This may be called only from the CPU to whom the rdp
1058  * belongs.  In addition, the corresponding leaf rcu_node structure's
1059  * ->lock must be held by the caller, with irqs disabled.
1060  */
1061 static void
1062 __rcu_process_gp_end(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
1063 {
1064         /* Did another grace period end? */
1065         if (rdp->completed != rnp->completed) {
1066
1067                 /* Advance callbacks.  No harm if list empty. */
1068                 rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
1069                 rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
1070                 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
1071
1072                 /* Remember that we saw this grace-period completion. */
1073                 rdp->completed = rnp->completed;
1074                 trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpuend");
1075
1076                 /*
1077                  * If we were in an extended quiescent state, we may have
1078                  * missed some grace periods that others CPUs handled on
1079                  * our behalf. Catch up with this state to avoid noting
1080                  * spurious new grace periods.  If another grace period
1081                  * has started, then rnp->gpnum will have advanced, so
1082                  * we will detect this later on.  Of course, any quiescent
1083                  * states we found for the old GP are now invalid.
1084                  */
1085                 if (ULONG_CMP_LT(rdp->gpnum, rdp->completed)) {
1086                         rdp->gpnum = rdp->completed;
1087                         rdp->passed_quiesce = 0;
1088                 }
1089
1090                 /*
1091                  * If RCU does not need a quiescent state from this CPU,
1092                  * then make sure that this CPU doesn't go looking for one.
1093                  */
1094                 if ((rnp->qsmask & rdp->grpmask) == 0)
1095                         rdp->qs_pending = 0;
1096         }
1097 }
1098
1099 /*
1100  * Advance this CPU's callbacks, but only if the current grace period
1101  * has ended.  This may be called only from the CPU to whom the rdp
1102  * belongs.
1103  */
1104 static void
1105 rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
1106 {
1107         unsigned long flags;
1108         struct rcu_node *rnp;
1109
1110         local_irq_save(flags);
1111         rnp = rdp->mynode;
1112         if (rdp->completed == ACCESS_ONCE(rnp->completed) || /* outside lock. */
1113             !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */
1114                 local_irq_restore(flags);
1115                 return;
1116         }
1117         __rcu_process_gp_end(rsp, rnp, rdp);
1118         raw_spin_unlock_irqrestore(&rnp->lock, flags);
1119 }
1120
1121 /*
1122  * Do per-CPU grace-period initialization for running CPU.  The caller
1123  * must hold the lock of the leaf rcu_node structure corresponding to
1124  * this CPU.
1125  */
1126 static void
1127 rcu_start_gp_per_cpu(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp)
1128 {
1129         /* Prior grace period ended, so advance callbacks for current CPU. */
1130         __rcu_process_gp_end(rsp, rnp, rdp);
1131
1132         /* Set state so that this CPU will detect the next quiescent state. */
1133         __note_new_gpnum(rsp, rnp, rdp);
1134 }
1135
1136 /*
1137  * Initialize a new grace period.
1138  */
1139 static int rcu_gp_init(struct rcu_state *rsp)
1140 {
1141         struct rcu_data *rdp;
1142         struct rcu_node *rnp = rcu_get_root(rsp);
1143
1144         raw_spin_lock_irq(&rnp->lock);
1145         rsp->gp_flags = 0; /* Clear all flags: New grace period. */
1146
1147         if (rcu_gp_in_progress(rsp)) {
1148                 /* Grace period already in progress, don't start another.  */
1149                 raw_spin_unlock_irq(&rnp->lock);
1150                 return 0;
1151         }
1152
1153         /* Advance to a new grace period and initialize state. */
1154         rsp->gpnum++;
1155         trace_rcu_grace_period(rsp->name, rsp->gpnum, "start");
1156         record_gp_stall_check_time(rsp);
1157         raw_spin_unlock_irq(&rnp->lock);
1158
1159         /* Exclude any concurrent CPU-hotplug operations. */
1160         get_online_cpus();
1161
1162         /*
1163          * Set the quiescent-state-needed bits in all the rcu_node
1164          * structures for all currently online CPUs in breadth-first order,
1165          * starting from the root rcu_node structure, relying on the layout
1166          * of the tree within the rsp->node[] array.  Note that other CPUs
1167          * will access only the leaves of the hierarchy, thus seeing that no
1168          * grace period is in progress, at least until the corresponding
1169          * leaf node has been initialized.  In addition, we have excluded
1170          * CPU-hotplug operations.
1171          *
1172          * The grace period cannot complete until the initialization
1173          * process finishes, because this kthread handles both.
1174          */
1175         rcu_for_each_node_breadth_first(rsp, rnp) {
1176                 raw_spin_lock_irq(&rnp->lock);
1177                 rdp = this_cpu_ptr(rsp->rda);
1178                 rcu_preempt_check_blocked_tasks(rnp);
1179                 rnp->qsmask = rnp->qsmaskinit;
1180                 rnp->gpnum = rsp->gpnum;
1181                 WARN_ON_ONCE(rnp->completed != rsp->completed);
1182                 rnp->completed = rsp->completed;
1183                 if (rnp == rdp->mynode)
1184                         rcu_start_gp_per_cpu(rsp, rnp, rdp);
1185                 rcu_preempt_boost_start_gp(rnp);
1186                 trace_rcu_grace_period_init(rsp->name, rnp->gpnum,
1187                                             rnp->level, rnp->grplo,
1188                                             rnp->grphi, rnp->qsmask);
1189                 raw_spin_unlock_irq(&rnp->lock);
1190 #ifdef CONFIG_PROVE_RCU_DELAY
1191                 if ((random32() % (rcu_num_nodes * 8)) == 0)
1192                         schedule_timeout_uninterruptible(2);
1193 #endif /* #ifdef CONFIG_PROVE_RCU_DELAY */
1194                 cond_resched();
1195         }
1196
1197         put_online_cpus();
1198         return 1;
1199 }
1200
1201 /*
1202  * Do one round of quiescent-state forcing.
1203  */
1204 int rcu_gp_fqs(struct rcu_state *rsp, int fqs_state_in)
1205 {
1206         int fqs_state = fqs_state_in;
1207         struct rcu_node *rnp = rcu_get_root(rsp);
1208
1209         rsp->n_force_qs++;
1210         if (fqs_state == RCU_SAVE_DYNTICK) {
1211                 /* Collect dyntick-idle snapshots. */
1212                 force_qs_rnp(rsp, dyntick_save_progress_counter);
1213                 fqs_state = RCU_FORCE_QS;
1214         } else {
1215                 /* Handle dyntick-idle and offline CPUs. */
1216                 force_qs_rnp(rsp, rcu_implicit_dynticks_qs);
1217         }
1218         /* Clear flag to prevent immediate re-entry. */
1219         if (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
1220                 raw_spin_lock_irq(&rnp->lock);
1221                 rsp->gp_flags &= ~RCU_GP_FLAG_FQS;
1222                 raw_spin_unlock_irq(&rnp->lock);
1223         }
1224         return fqs_state;
1225 }
1226
1227 /*
1228  * Clean up after the old grace period.
1229  */
1230 static void rcu_gp_cleanup(struct rcu_state *rsp)
1231 {
1232         unsigned long gp_duration;
1233         struct rcu_data *rdp;
1234         struct rcu_node *rnp = rcu_get_root(rsp);
1235
1236         raw_spin_lock_irq(&rnp->lock);
1237         gp_duration = jiffies - rsp->gp_start;
1238         if (gp_duration > rsp->gp_max)
1239                 rsp->gp_max = gp_duration;
1240
1241         /*
1242          * We know the grace period is complete, but to everyone else
1243          * it appears to still be ongoing.  But it is also the case
1244          * that to everyone else it looks like there is nothing that
1245          * they can do to advance the grace period.  It is therefore
1246          * safe for us to drop the lock in order to mark the grace
1247          * period as completed in all of the rcu_node structures.
1248          */
1249         raw_spin_unlock_irq(&rnp->lock);
1250
1251         /*
1252          * Propagate new ->completed value to rcu_node structures so
1253          * that other CPUs don't have to wait until the start of the next
1254          * grace period to process their callbacks.  This also avoids
1255          * some nasty RCU grace-period initialization races by forcing
1256          * the end of the current grace period to be completely recorded in
1257          * all of the rcu_node structures before the beginning of the next
1258          * grace period is recorded in any of the rcu_node structures.
1259          */
1260         rcu_for_each_node_breadth_first(rsp, rnp) {
1261                 raw_spin_lock_irq(&rnp->lock);
1262                 rnp->completed = rsp->gpnum;
1263                 raw_spin_unlock_irq(&rnp->lock);
1264                 cond_resched();
1265         }
1266         rnp = rcu_get_root(rsp);
1267         raw_spin_lock_irq(&rnp->lock);
1268
1269         rsp->completed = rsp->gpnum; /* Declare grace period done. */
1270         trace_rcu_grace_period(rsp->name, rsp->completed, "end");
1271         rsp->fqs_state = RCU_GP_IDLE;
1272         rdp = this_cpu_ptr(rsp->rda);
1273         if (cpu_needs_another_gp(rsp, rdp))
1274                 rsp->gp_flags = 1;
1275         raw_spin_unlock_irq(&rnp->lock);
1276 }
1277
1278 /*
1279  * Body of kthread that handles grace periods.
1280  */
1281 static int __noreturn rcu_gp_kthread(void *arg)
1282 {
1283         int fqs_state;
1284         unsigned long j;
1285         int ret;
1286         struct rcu_state *rsp = arg;
1287         struct rcu_node *rnp = rcu_get_root(rsp);
1288
1289         for (;;) {
1290
1291                 /* Handle grace-period start. */
1292                 for (;;) {
1293                         wait_event_interruptible(rsp->gp_wq,
1294                                                  rsp->gp_flags &
1295                                                  RCU_GP_FLAG_INIT);
1296                         if ((rsp->gp_flags & RCU_GP_FLAG_INIT) &&
1297                             rcu_gp_init(rsp))
1298                                 break;
1299                         cond_resched();
1300                         flush_signals(current);
1301                 }
1302
1303                 /* Handle quiescent-state forcing. */
1304                 fqs_state = RCU_SAVE_DYNTICK;
1305                 j = jiffies_till_first_fqs;
1306                 if (j > HZ) {
1307                         j = HZ;
1308                         jiffies_till_first_fqs = HZ;
1309                 }
1310                 for (;;) {
1311                         rsp->jiffies_force_qs = jiffies + j;
1312                         ret = wait_event_interruptible_timeout(rsp->gp_wq,
1313                                         (rsp->gp_flags & RCU_GP_FLAG_FQS) ||
1314                                         (!ACCESS_ONCE(rnp->qsmask) &&
1315                                          !rcu_preempt_blocked_readers_cgp(rnp)),
1316                                         j);
1317                         /* If grace period done, leave loop. */
1318                         if (!ACCESS_ONCE(rnp->qsmask) &&
1319                             !rcu_preempt_blocked_readers_cgp(rnp))
1320                                 break;
1321                         /* If time for quiescent-state forcing, do it. */
1322                         if (ret == 0 || (rsp->gp_flags & RCU_GP_FLAG_FQS)) {
1323                                 fqs_state = rcu_gp_fqs(rsp, fqs_state);
1324                                 cond_resched();
1325                         } else {
1326                                 /* Deal with stray signal. */
1327                                 cond_resched();
1328                                 flush_signals(current);
1329                         }
1330                         j = jiffies_till_next_fqs;
1331                         if (j > HZ) {
1332                                 j = HZ;
1333                                 jiffies_till_next_fqs = HZ;
1334                         } else if (j < 1) {
1335                                 j = 1;
1336                                 jiffies_till_next_fqs = 1;
1337                         }
1338                 }
1339
1340                 /* Handle grace-period end. */
1341                 rcu_gp_cleanup(rsp);
1342         }
1343 }
1344
1345 /*
1346  * Start a new RCU grace period if warranted, re-initializing the hierarchy
1347  * in preparation for detecting the next grace period.  The caller must hold
1348  * the root node's ->lock, which is released before return.  Hard irqs must
1349  * be disabled.
1350  *
1351  * Note that it is legal for a dying CPU (which is marked as offline) to
1352  * invoke this function.  This can happen when the dying CPU reports its
1353  * quiescent state.
1354  */
1355 static void
1356 rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
1357         __releases(rcu_get_root(rsp)->lock)
1358 {
1359         struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
1360         struct rcu_node *rnp = rcu_get_root(rsp);
1361
1362         if (!rsp->gp_kthread ||
1363             !cpu_needs_another_gp(rsp, rdp)) {
1364                 /*
1365                  * Either we have not yet spawned the grace-period
1366                  * task or this CPU does not need another grace period.
1367                  * Either way, don't start a new grace period.
1368                  */
1369                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1370                 return;
1371         }
1372
1373         rsp->gp_flags = RCU_GP_FLAG_INIT;
1374         raw_spin_unlock_irqrestore(&rnp->lock, flags);
1375         wake_up(&rsp->gp_wq);
1376 }
1377
1378 /*
1379  * Report a full set of quiescent states to the specified rcu_state
1380  * data structure.  This involves cleaning up after the prior grace
1381  * period and letting rcu_start_gp() start up the next grace period
1382  * if one is needed.  Note that the caller must hold rnp->lock, as
1383  * required by rcu_start_gp(), which will release it.
1384  */
1385 static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
1386         __releases(rcu_get_root(rsp)->lock)
1387 {
1388         WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
1389         raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
1390         wake_up(&rsp->gp_wq);  /* Memory barrier implied by wake_up() path. */
1391 }
1392
1393 /*
1394  * Similar to rcu_report_qs_rdp(), for which it is a helper function.
1395  * Allows quiescent states for a group of CPUs to be reported at one go
1396  * to the specified rcu_node structure, though all the CPUs in the group
1397  * must be represented by the same rcu_node structure (which need not be
1398  * a leaf rcu_node structure, though it often will be).  That structure's
1399  * lock must be held upon entry, and it is released before return.
1400  */
1401 static void
1402 rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp,
1403                   struct rcu_node *rnp, unsigned long flags)
1404         __releases(rnp->lock)
1405 {
1406         struct rcu_node *rnp_c;
1407
1408         /* Walk up the rcu_node hierarchy. */
1409         for (;;) {
1410                 if (!(rnp->qsmask & mask)) {
1411
1412                         /* Our bit has already been cleared, so done. */
1413                         raw_spin_unlock_irqrestore(&rnp->lock, flags);
1414                         return;
1415                 }
1416                 rnp->qsmask &= ~mask;
1417                 trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum,
1418                                                  mask, rnp->qsmask, rnp->level,
1419                                                  rnp->grplo, rnp->grphi,
1420                                                  !!rnp->gp_tasks);
1421                 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
1422
1423                         /* Other bits still set at this level, so done. */
1424                         raw_spin_unlock_irqrestore(&rnp->lock, flags);
1425                         return;
1426                 }
1427                 mask = rnp->grpmask;
1428                 if (rnp->parent == NULL) {
1429
1430                         /* No more levels.  Exit loop holding root lock. */
1431
1432                         break;
1433                 }
1434                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1435                 rnp_c = rnp;
1436                 rnp = rnp->parent;
1437                 raw_spin_lock_irqsave(&rnp->lock, flags);
1438                 WARN_ON_ONCE(rnp_c->qsmask);
1439         }
1440
1441         /*
1442          * Get here if we are the last CPU to pass through a quiescent
1443          * state for this grace period.  Invoke rcu_report_qs_rsp()
1444          * to clean up and start the next grace period if one is needed.
1445          */
1446         rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */
1447 }
1448
1449 /*
1450  * Record a quiescent state for the specified CPU to that CPU's rcu_data
1451  * structure.  This must be either called from the specified CPU, or
1452  * called when the specified CPU is known to be offline (and when it is
1453  * also known that no other CPU is concurrently trying to help the offline
1454  * CPU).  The lastcomp argument is used to make sure we are still in the
1455  * grace period of interest.  We don't want to end the current grace period
1456  * based on quiescent states detected in an earlier grace period!
1457  */
1458 static void
1459 rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp)
1460 {
1461         unsigned long flags;
1462         unsigned long mask;
1463         struct rcu_node *rnp;
1464
1465         rnp = rdp->mynode;
1466         raw_spin_lock_irqsave(&rnp->lock, flags);
1467         if (rdp->passed_quiesce == 0 || rdp->gpnum != rnp->gpnum ||
1468             rnp->completed == rnp->gpnum) {
1469
1470                 /*
1471                  * The grace period in which this quiescent state was
1472                  * recorded has ended, so don't report it upwards.
1473                  * We will instead need a new quiescent state that lies
1474                  * within the current grace period.
1475                  */
1476                 rdp->passed_quiesce = 0;        /* need qs for new gp. */
1477                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1478                 return;
1479         }
1480         mask = rdp->grpmask;
1481         if ((rnp->qsmask & mask) == 0) {
1482                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1483         } else {
1484                 rdp->qs_pending = 0;
1485
1486                 /*
1487                  * This GP can't end until cpu checks in, so all of our
1488                  * callbacks can be processed during the next GP.
1489                  */
1490                 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
1491
1492                 rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */
1493         }
1494 }
1495
1496 /*
1497  * Check to see if there is a new grace period of which this CPU
1498  * is not yet aware, and if so, set up local rcu_data state for it.
1499  * Otherwise, see if this CPU has just passed through its first
1500  * quiescent state for this grace period, and record that fact if so.
1501  */
1502 static void
1503 rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
1504 {
1505         /* If there is now a new grace period, record and return. */
1506         if (check_for_new_grace_period(rsp, rdp))
1507                 return;
1508
1509         /*
1510          * Does this CPU still need to do its part for current grace period?
1511          * If no, return and let the other CPUs do their part as well.
1512          */
1513         if (!rdp->qs_pending)
1514                 return;
1515
1516         /*
1517          * Was there a quiescent state since the beginning of the grace
1518          * period? If no, then exit and wait for the next call.
1519          */
1520         if (!rdp->passed_quiesce)
1521                 return;
1522
1523         /*
1524          * Tell RCU we are done (but rcu_report_qs_rdp() will be the
1525          * judge of that).
1526          */
1527         rcu_report_qs_rdp(rdp->cpu, rsp, rdp);
1528 }
1529
1530 #ifdef CONFIG_HOTPLUG_CPU
1531
1532 /*
1533  * Send the specified CPU's RCU callbacks to the orphanage.  The
1534  * specified CPU must be offline, and the caller must hold the
1535  * ->onofflock.
1536  */
1537 static void
1538 rcu_send_cbs_to_orphanage(int cpu, struct rcu_state *rsp,
1539                           struct rcu_node *rnp, struct rcu_data *rdp)
1540 {
1541         /*
1542          * Orphan the callbacks.  First adjust the counts.  This is safe
1543          * because ->onofflock excludes _rcu_barrier()'s adoption of
1544          * the callbacks, thus no memory barrier is required.
1545          */
1546         if (rdp->nxtlist != NULL) {
1547                 rsp->qlen_lazy += rdp->qlen_lazy;
1548                 rsp->qlen += rdp->qlen;
1549                 rdp->n_cbs_orphaned += rdp->qlen;
1550                 rdp->qlen_lazy = 0;
1551                 ACCESS_ONCE(rdp->qlen) = 0;
1552         }
1553
1554         /*
1555          * Next, move those callbacks still needing a grace period to
1556          * the orphanage, where some other CPU will pick them up.
1557          * Some of the callbacks might have gone partway through a grace
1558          * period, but that is too bad.  They get to start over because we
1559          * cannot assume that grace periods are synchronized across CPUs.
1560          * We don't bother updating the ->nxttail[] array yet, instead
1561          * we just reset the whole thing later on.
1562          */
1563         if (*rdp->nxttail[RCU_DONE_TAIL] != NULL) {
1564                 *rsp->orphan_nxttail = *rdp->nxttail[RCU_DONE_TAIL];
1565                 rsp->orphan_nxttail = rdp->nxttail[RCU_NEXT_TAIL];
1566                 *rdp->nxttail[RCU_DONE_TAIL] = NULL;
1567         }
1568
1569         /*
1570          * Then move the ready-to-invoke callbacks to the orphanage,
1571          * where some other CPU will pick them up.  These will not be
1572          * required to pass though another grace period: They are done.
1573          */
1574         if (rdp->nxtlist != NULL) {
1575                 *rsp->orphan_donetail = rdp->nxtlist;
1576                 rsp->orphan_donetail = rdp->nxttail[RCU_DONE_TAIL];
1577         }
1578
1579         /* Finally, initialize the rcu_data structure's list to empty.  */
1580         init_callback_list(rdp);
1581 }
1582
1583 /*
1584  * Adopt the RCU callbacks from the specified rcu_state structure's
1585  * orphanage.  The caller must hold the ->onofflock.
1586  */
1587 static void rcu_adopt_orphan_cbs(struct rcu_state *rsp)
1588 {
1589         int i;
1590         struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
1591
1592         /* Do the accounting first. */
1593         rdp->qlen_lazy += rsp->qlen_lazy;
1594         rdp->qlen += rsp->qlen;
1595         rdp->n_cbs_adopted += rsp->qlen;
1596         if (rsp->qlen_lazy != rsp->qlen)
1597                 rcu_idle_count_callbacks_posted();
1598         rsp->qlen_lazy = 0;
1599         rsp->qlen = 0;
1600
1601         /*
1602          * We do not need a memory barrier here because the only way we
1603          * can get here if there is an rcu_barrier() in flight is if
1604          * we are the task doing the rcu_barrier().
1605          */
1606
1607         /* First adopt the ready-to-invoke callbacks. */
1608         if (rsp->orphan_donelist != NULL) {
1609                 *rsp->orphan_donetail = *rdp->nxttail[RCU_DONE_TAIL];
1610                 *rdp->nxttail[RCU_DONE_TAIL] = rsp->orphan_donelist;
1611                 for (i = RCU_NEXT_SIZE - 1; i >= RCU_DONE_TAIL; i--)
1612                         if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
1613                                 rdp->nxttail[i] = rsp->orphan_donetail;
1614                 rsp->orphan_donelist = NULL;
1615                 rsp->orphan_donetail = &rsp->orphan_donelist;
1616         }
1617
1618         /* And then adopt the callbacks that still need a grace period. */
1619         if (rsp->orphan_nxtlist != NULL) {
1620                 *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxtlist;
1621                 rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxttail;
1622                 rsp->orphan_nxtlist = NULL;
1623                 rsp->orphan_nxttail = &rsp->orphan_nxtlist;
1624         }
1625 }
1626
1627 /*
1628  * Trace the fact that this CPU is going offline.
1629  */
1630 static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
1631 {
1632         RCU_TRACE(unsigned long mask);
1633         RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda));
1634         RCU_TRACE(struct rcu_node *rnp = rdp->mynode);
1635
1636         RCU_TRACE(mask = rdp->grpmask);
1637         trace_rcu_grace_period(rsp->name,
1638                                rnp->gpnum + 1 - !!(rnp->qsmask & mask),
1639                                "cpuofl");
1640 }
1641
1642 /*
1643  * The CPU has been completely removed, and some other CPU is reporting
1644  * this fact from process context.  Do the remainder of the cleanup,
1645  * including orphaning the outgoing CPU's RCU callbacks, and also
1646  * adopting them.  There can only be one CPU hotplug operation at a time,
1647  * so no other CPU can be attempting to update rcu_cpu_kthread_task.
1648  */
1649 static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
1650 {
1651         unsigned long flags;
1652         unsigned long mask;
1653         int need_report = 0;
1654         struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
1655         struct rcu_node *rnp = rdp->mynode;  /* Outgoing CPU's rdp & rnp. */
1656
1657         /* Adjust any no-longer-needed kthreads. */
1658         rcu_boost_kthread_setaffinity(rnp, -1);
1659
1660         /* Remove the dead CPU from the bitmasks in the rcu_node hierarchy. */
1661
1662         /* Exclude any attempts to start a new grace period. */
1663         raw_spin_lock_irqsave(&rsp->onofflock, flags);
1664
1665         /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */
1666         rcu_send_cbs_to_orphanage(cpu, rsp, rnp, rdp);
1667         rcu_adopt_orphan_cbs(rsp);
1668
1669         /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
1670         mask = rdp->grpmask;    /* rnp->grplo is constant. */
1671         do {
1672                 raw_spin_lock(&rnp->lock);      /* irqs already disabled. */
1673                 rnp->qsmaskinit &= ~mask;
1674                 if (rnp->qsmaskinit != 0) {
1675                         if (rnp != rdp->mynode)
1676                                 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1677                         break;
1678                 }
1679                 if (rnp == rdp->mynode)
1680                         need_report = rcu_preempt_offline_tasks(rsp, rnp, rdp);
1681                 else
1682                         raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1683                 mask = rnp->grpmask;
1684                 rnp = rnp->parent;
1685         } while (rnp != NULL);
1686
1687         /*
1688          * We still hold the leaf rcu_node structure lock here, and
1689          * irqs are still disabled.  The reason for this subterfuge is
1690          * because invoking rcu_report_unblock_qs_rnp() with ->onofflock
1691          * held leads to deadlock.
1692          */
1693         raw_spin_unlock(&rsp->onofflock); /* irqs remain disabled. */
1694         rnp = rdp->mynode;
1695         if (need_report & RCU_OFL_TASKS_NORM_GP)
1696                 rcu_report_unblock_qs_rnp(rnp, flags);
1697         else
1698                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1699         if (need_report & RCU_OFL_TASKS_EXP_GP)
1700                 rcu_report_exp_rnp(rsp, rnp, true);
1701         WARN_ONCE(rdp->qlen != 0 || rdp->nxtlist != NULL,
1702                   "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n",
1703                   cpu, rdp->qlen, rdp->nxtlist);
1704         init_callback_list(rdp);
1705         /* Disallow further callbacks on this CPU. */
1706         rdp->nxttail[RCU_NEXT_TAIL] = NULL;
1707 }
1708
1709 #else /* #ifdef CONFIG_HOTPLUG_CPU */
1710
1711 static void rcu_cleanup_dying_cpu(struct rcu_state *rsp)
1712 {
1713 }
1714
1715 static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp)
1716 {
1717 }
1718
1719 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1720
1721 /*
1722  * Invoke any RCU callbacks that have made it to the end of their grace
1723  * period.  Thottle as specified by rdp->blimit.
1724  */
1725 static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp)
1726 {
1727         unsigned long flags;
1728         struct rcu_head *next, *list, **tail;
1729         int bl, count, count_lazy, i;
1730
1731         /* If no callbacks are ready, just return.*/
1732         if (!cpu_has_callbacks_ready_to_invoke(rdp)) {
1733                 trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0);
1734                 trace_rcu_batch_end(rsp->name, 0, !!ACCESS_ONCE(rdp->nxtlist),
1735                                     need_resched(), is_idle_task(current),
1736                                     rcu_is_callbacks_kthread());
1737                 return;
1738         }
1739
1740         /*
1741          * Extract the list of ready callbacks, disabling to prevent
1742          * races with call_rcu() from interrupt handlers.
1743          */
1744         local_irq_save(flags);
1745         WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
1746         bl = rdp->blimit;
1747         trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl);
1748         list = rdp->nxtlist;
1749         rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
1750         *rdp->nxttail[RCU_DONE_TAIL] = NULL;
1751         tail = rdp->nxttail[RCU_DONE_TAIL];
1752         for (i = RCU_NEXT_SIZE - 1; i >= 0; i--)
1753                 if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL])
1754                         rdp->nxttail[i] = &rdp->nxtlist;
1755         local_irq_restore(flags);
1756
1757         /* Invoke callbacks. */
1758         count = count_lazy = 0;
1759         while (list) {
1760                 next = list->next;
1761                 prefetch(next);
1762                 debug_rcu_head_unqueue(list);
1763                 if (__rcu_reclaim(rsp->name, list))
1764                         count_lazy++;
1765                 list = next;
1766                 /* Stop only if limit reached and CPU has something to do. */
1767                 if (++count >= bl &&
1768                     (need_resched() ||
1769                      (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
1770                         break;
1771         }
1772
1773         local_irq_save(flags);
1774         trace_rcu_batch_end(rsp->name, count, !!list, need_resched(),
1775                             is_idle_task(current),
1776                             rcu_is_callbacks_kthread());
1777
1778         /* Update count, and requeue any remaining callbacks. */
1779         if (list != NULL) {
1780                 *tail = rdp->nxtlist;
1781                 rdp->nxtlist = list;
1782                 for (i = 0; i < RCU_NEXT_SIZE; i++)
1783                         if (&rdp->nxtlist == rdp->nxttail[i])
1784                                 rdp->nxttail[i] = tail;
1785                         else
1786                                 break;
1787         }
1788         smp_mb(); /* List handling before counting for rcu_barrier(). */
1789         rdp->qlen_lazy -= count_lazy;
1790         ACCESS_ONCE(rdp->qlen) -= count;
1791         rdp->n_cbs_invoked += count;
1792
1793         /* Reinstate batch limit if we have worked down the excess. */
1794         if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
1795                 rdp->blimit = blimit;
1796
1797         /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */
1798         if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) {
1799                 rdp->qlen_last_fqs_check = 0;
1800                 rdp->n_force_qs_snap = rsp->n_force_qs;
1801         } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark)
1802                 rdp->qlen_last_fqs_check = rdp->qlen;
1803         WARN_ON_ONCE((rdp->nxtlist == NULL) != (rdp->qlen == 0));
1804
1805         local_irq_restore(flags);
1806
1807         /* Re-invoke RCU core processing if there are callbacks remaining. */
1808         if (cpu_has_callbacks_ready_to_invoke(rdp))
1809                 invoke_rcu_core();
1810 }
1811
1812 /*
1813  * Check to see if this CPU is in a non-context-switch quiescent state
1814  * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
1815  * Also schedule RCU core processing.
1816  *
1817  * This function must be called from hardirq context.  It is normally
1818  * invoked from the scheduling-clock interrupt.  If rcu_pending returns
1819  * false, there is no point in invoking rcu_check_callbacks().
1820  */
1821 void rcu_check_callbacks(int cpu, int user)
1822 {
1823         trace_rcu_utilization("Start scheduler-tick");
1824         increment_cpu_stall_ticks();
1825         if (user || rcu_is_cpu_rrupt_from_idle()) {
1826
1827                 /*
1828                  * Get here if this CPU took its interrupt from user
1829                  * mode or from the idle loop, and if this is not a
1830                  * nested interrupt.  In this case, the CPU is in
1831                  * a quiescent state, so note it.
1832                  *
1833                  * No memory barrier is required here because both
1834                  * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
1835                  * variables that other CPUs neither access nor modify,
1836                  * at least not while the corresponding CPU is online.
1837                  */
1838
1839                 rcu_sched_qs(cpu);
1840                 rcu_bh_qs(cpu);
1841
1842         } else if (!in_softirq()) {
1843
1844                 /*
1845                  * Get here if this CPU did not take its interrupt from
1846                  * softirq, in other words, if it is not interrupting
1847                  * a rcu_bh read-side critical section.  This is an _bh
1848                  * critical section, so note it.
1849                  */
1850
1851                 rcu_bh_qs(cpu);
1852         }
1853         rcu_preempt_check_callbacks(cpu);
1854         if (rcu_pending(cpu))
1855                 invoke_rcu_core();
1856         trace_rcu_utilization("End scheduler-tick");
1857 }
1858
1859 /*
1860  * Scan the leaf rcu_node structures, processing dyntick state for any that
1861  * have not yet encountered a quiescent state, using the function specified.
1862  * Also initiate boosting for any threads blocked on the root rcu_node.
1863  *
1864  * The caller must have suppressed start of new grace periods.
1865  */
1866 static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *))
1867 {
1868         unsigned long bit;
1869         int cpu;
1870         unsigned long flags;
1871         unsigned long mask;
1872         struct rcu_node *rnp;
1873
1874         rcu_for_each_leaf_node(rsp, rnp) {
1875                 cond_resched();
1876                 mask = 0;
1877                 raw_spin_lock_irqsave(&rnp->lock, flags);
1878                 if (!rcu_gp_in_progress(rsp)) {
1879                         raw_spin_unlock_irqrestore(&rnp->lock, flags);
1880                         return;
1881                 }
1882                 if (rnp->qsmask == 0) {
1883                         rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
1884                         continue;
1885                 }
1886                 cpu = rnp->grplo;
1887                 bit = 1;
1888                 for (; cpu <= rnp->grphi; cpu++, bit <<= 1) {
1889                         if ((rnp->qsmask & bit) != 0 &&
1890                             f(per_cpu_ptr(rsp->rda, cpu)))
1891                                 mask |= bit;
1892                 }
1893                 if (mask != 0) {
1894
1895                         /* rcu_report_qs_rnp() releases rnp->lock. */
1896                         rcu_report_qs_rnp(mask, rsp, rnp, flags);
1897                         continue;
1898                 }
1899                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1900         }
1901         rnp = rcu_get_root(rsp);
1902         if (rnp->qsmask == 0) {
1903                 raw_spin_lock_irqsave(&rnp->lock, flags);
1904                 rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
1905         }
1906 }
1907
1908 /*
1909  * Force quiescent states on reluctant CPUs, and also detect which
1910  * CPUs are in dyntick-idle mode.
1911  */
1912 static void force_quiescent_state(struct rcu_state *rsp)
1913 {
1914         unsigned long flags;
1915         bool ret;
1916         struct rcu_node *rnp;
1917         struct rcu_node *rnp_old = NULL;
1918
1919         /* Funnel through hierarchy to reduce memory contention. */
1920         rnp = per_cpu_ptr(rsp->rda, raw_smp_processor_id())->mynode;
1921         for (; rnp != NULL; rnp = rnp->parent) {
1922                 ret = (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) ||
1923                       !raw_spin_trylock(&rnp->fqslock);
1924                 if (rnp_old != NULL)
1925                         raw_spin_unlock(&rnp_old->fqslock);
1926                 if (ret) {
1927                         rsp->n_force_qs_lh++;
1928                         return;
1929                 }
1930                 rnp_old = rnp;
1931         }
1932         /* rnp_old == rcu_get_root(rsp), rnp == NULL. */
1933
1934         /* Reached the root of the rcu_node tree, acquire lock. */
1935         raw_spin_lock_irqsave(&rnp_old->lock, flags);
1936         raw_spin_unlock(&rnp_old->fqslock);
1937         if (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) {
1938                 rsp->n_force_qs_lh++;
1939                 raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
1940                 return;  /* Someone beat us to it. */
1941         }
1942         rsp->gp_flags |= RCU_GP_FLAG_FQS;
1943         raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
1944         wake_up(&rsp->gp_wq);  /* Memory barrier implied by wake_up() path. */
1945 }
1946
1947 /*
1948  * This does the RCU core processing work for the specified rcu_state
1949  * and rcu_data structures.  This may be called only from the CPU to
1950  * whom the rdp belongs.
1951  */
1952 static void
1953 __rcu_process_callbacks(struct rcu_state *rsp)
1954 {
1955         unsigned long flags;
1956         struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
1957
1958         WARN_ON_ONCE(rdp->beenonline == 0);
1959
1960         /*
1961          * Advance callbacks in response to end of earlier grace
1962          * period that some other CPU ended.
1963          */
1964         rcu_process_gp_end(rsp, rdp);
1965
1966         /* Update RCU state based on any recent quiescent states. */
1967         rcu_check_quiescent_state(rsp, rdp);
1968
1969         /* Does this CPU require a not-yet-started grace period? */
1970         if (cpu_needs_another_gp(rsp, rdp)) {
1971                 raw_spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
1972                 rcu_start_gp(rsp, flags);  /* releases above lock */
1973         }
1974
1975         /* If there are callbacks ready, invoke them. */
1976         if (cpu_has_callbacks_ready_to_invoke(rdp))
1977                 invoke_rcu_callbacks(rsp, rdp);
1978 }
1979
1980 /*
1981  * Do RCU core processing for the current CPU.
1982  */
1983 static void rcu_process_callbacks(struct softirq_action *unused)
1984 {
1985         struct rcu_state *rsp;
1986
1987         if (cpu_is_offline(smp_processor_id()))
1988                 return;
1989         trace_rcu_utilization("Start RCU core");
1990         for_each_rcu_flavor(rsp)
1991                 __rcu_process_callbacks(rsp);
1992         trace_rcu_utilization("End RCU core");
1993 }
1994
1995 /*
1996  * Schedule RCU callback invocation.  If the specified type of RCU
1997  * does not support RCU priority boosting, just do a direct call,
1998  * otherwise wake up the per-CPU kernel kthread.  Note that because we
1999  * are running on the current CPU with interrupts disabled, the
2000  * rcu_cpu_kthread_task cannot disappear out from under us.
2001  */
2002 static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
2003 {
2004         if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active)))
2005                 return;
2006         if (likely(!rsp->boost)) {
2007                 rcu_do_batch(rsp, rdp);
2008                 return;
2009         }
2010         invoke_rcu_callbacks_kthread();
2011 }
2012
2013 static void invoke_rcu_core(void)
2014 {
2015         raise_softirq(RCU_SOFTIRQ);
2016 }
2017
2018 /*
2019  * Handle any core-RCU processing required by a call_rcu() invocation.
2020  */
2021 static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp,
2022                             struct rcu_head *head, unsigned long flags)
2023 {
2024         /*
2025          * If called from an extended quiescent state, invoke the RCU
2026          * core in order to force a re-evaluation of RCU's idleness.
2027          */
2028         if (rcu_is_cpu_idle() && cpu_online(smp_processor_id()))
2029                 invoke_rcu_core();
2030
2031         /* If interrupts were disabled or CPU offline, don't invoke RCU core. */
2032         if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
2033                 return;
2034
2035         /*
2036          * Force the grace period if too many callbacks or too long waiting.
2037          * Enforce hysteresis, and don't invoke force_quiescent_state()
2038          * if some other CPU has recently done so.  Also, don't bother
2039          * invoking force_quiescent_state() if the newly enqueued callback
2040          * is the only one waiting for a grace period to complete.
2041          */
2042         if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) {
2043
2044                 /* Are we ignoring a completed grace period? */
2045                 rcu_process_gp_end(rsp, rdp);
2046                 check_for_new_grace_period(rsp, rdp);
2047
2048                 /* Start a new grace period if one not already started. */
2049                 if (!rcu_gp_in_progress(rsp)) {
2050                         unsigned long nestflag;
2051                         struct rcu_node *rnp_root = rcu_get_root(rsp);
2052
2053                         raw_spin_lock_irqsave(&rnp_root->lock, nestflag);
2054                         rcu_start_gp(rsp, nestflag);  /* rlses rnp_root->lock */
2055                 } else {
2056                         /* Give the grace period a kick. */
2057                         rdp->blimit = LONG_MAX;
2058                         if (rsp->n_force_qs == rdp->n_force_qs_snap &&
2059                             *rdp->nxttail[RCU_DONE_TAIL] != head)
2060                                 force_quiescent_state(rsp);
2061                         rdp->n_force_qs_snap = rsp->n_force_qs;
2062                         rdp->qlen_last_fqs_check = rdp->qlen;
2063                 }
2064         }
2065 }
2066
2067 static void
2068 __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
2069            struct rcu_state *rsp, bool lazy)
2070 {
2071         unsigned long flags;
2072         struct rcu_data *rdp;
2073
2074         WARN_ON_ONCE((unsigned long)head & 0x3); /* Misaligned rcu_head! */
2075         debug_rcu_head_queue(head);
2076         head->func = func;
2077         head->next = NULL;
2078
2079         /*
2080          * Opportunistically note grace-period endings and beginnings.
2081          * Note that we might see a beginning right after we see an
2082          * end, but never vice versa, since this CPU has to pass through
2083          * a quiescent state betweentimes.
2084          */
2085         local_irq_save(flags);
2086         rdp = this_cpu_ptr(rsp->rda);
2087
2088         /* Add the callback to our list. */
2089         if (unlikely(rdp->nxttail[RCU_NEXT_TAIL] == NULL)) {
2090                 /* _call_rcu() is illegal on offline CPU; leak the callback. */
2091                 WARN_ON_ONCE(1);
2092                 local_irq_restore(flags);
2093                 return;
2094         }
2095         ACCESS_ONCE(rdp->qlen)++;
2096         if (lazy)
2097                 rdp->qlen_lazy++;
2098         else
2099                 rcu_idle_count_callbacks_posted();
2100         smp_mb();  /* Count before adding callback for rcu_barrier(). */
2101         *rdp->nxttail[RCU_NEXT_TAIL] = head;
2102         rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
2103
2104         if (__is_kfree_rcu_offset((unsigned long)func))
2105                 trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func,
2106                                          rdp->qlen_lazy, rdp->qlen);
2107         else
2108                 trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen);
2109
2110         /* Go handle any RCU core processing required. */
2111         __call_rcu_core(rsp, rdp, head, flags);
2112         local_irq_restore(flags);
2113 }
2114
2115 /*
2116  * Queue an RCU-sched callback for invocation after a grace period.
2117  */
2118 void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
2119 {
2120         __call_rcu(head, func, &rcu_sched_state, 0);
2121 }
2122 EXPORT_SYMBOL_GPL(call_rcu_sched);
2123
2124 /*
2125  * Queue an RCU callback for invocation after a quicker grace period.
2126  */
2127 void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
2128 {
2129         __call_rcu(head, func, &rcu_bh_state, 0);
2130 }
2131 EXPORT_SYMBOL_GPL(call_rcu_bh);
2132
2133 /*
2134  * Because a context switch is a grace period for RCU-sched and RCU-bh,
2135  * any blocking grace-period wait automatically implies a grace period
2136  * if there is only one CPU online at any point time during execution
2137  * of either synchronize_sched() or synchronize_rcu_bh().  It is OK to
2138  * occasionally incorrectly indicate that there are multiple CPUs online
2139  * when there was in fact only one the whole time, as this just adds
2140  * some overhead: RCU still operates correctly.
2141  */
2142 static inline int rcu_blocking_is_gp(void)
2143 {
2144         int ret;
2145
2146         might_sleep();  /* Check for RCU read-side critical section. */
2147         preempt_disable();
2148         ret = num_online_cpus() <= 1;
2149         preempt_enable();
2150         return ret;
2151 }
2152
2153 /**
2154  * synchronize_sched - wait until an rcu-sched grace period has elapsed.
2155  *
2156  * Control will return to the caller some time after a full rcu-sched
2157  * grace period has elapsed, in other words after all currently executing
2158  * rcu-sched read-side critical sections have completed.   These read-side
2159  * critical sections are delimited by rcu_read_lock_sched() and
2160  * rcu_read_unlock_sched(), and may be nested.  Note that preempt_disable(),
2161  * local_irq_disable(), and so on may be used in place of
2162  * rcu_read_lock_sched().
2163  *
2164  * This means that all preempt_disable code sequences, including NMI and
2165  * hardware-interrupt handlers, in progress on entry will have completed
2166  * before this primitive returns.  However, this does not guarantee that
2167  * softirq handlers will have completed, since in some kernels, these
2168  * handlers can run in process context, and can block.
2169  *
2170  * This primitive provides the guarantees made by the (now removed)
2171  * synchronize_kernel() API.  In contrast, synchronize_rcu() only
2172  * guarantees that rcu_read_lock() sections will have completed.
2173  * In "classic RCU", these two guarantees happen to be one and
2174  * the same, but can differ in realtime RCU implementations.
2175  */
2176 void synchronize_sched(void)
2177 {
2178         rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
2179                            !lock_is_held(&rcu_lock_map) &&
2180                            !lock_is_held(&rcu_sched_lock_map),
2181                            "Illegal synchronize_sched() in RCU-sched read-side critical section");
2182         if (rcu_blocking_is_gp())
2183                 return;
2184         wait_rcu_gp(call_rcu_sched);
2185 }
2186 EXPORT_SYMBOL_GPL(synchronize_sched);
2187
2188 /**
2189  * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
2190  *
2191  * Control will return to the caller some time after a full rcu_bh grace
2192  * period has elapsed, in other words after all currently executing rcu_bh
2193  * read-side critical sections have completed.  RCU read-side critical
2194  * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
2195  * and may be nested.
2196  */
2197 void synchronize_rcu_bh(void)
2198 {
2199         rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
2200                            !lock_is_held(&rcu_lock_map) &&
2201                            !lock_is_held(&rcu_sched_lock_map),
2202                            "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section");
2203         if (rcu_blocking_is_gp())
2204                 return;
2205         wait_rcu_gp(call_rcu_bh);
2206 }
2207 EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
2208
2209 static atomic_t sync_sched_expedited_started = ATOMIC_INIT(0);
2210 static atomic_t sync_sched_expedited_done = ATOMIC_INIT(0);
2211
2212 static int synchronize_sched_expedited_cpu_stop(void *data)
2213 {
2214         /*
2215          * There must be a full memory barrier on each affected CPU
2216          * between the time that try_stop_cpus() is called and the
2217          * time that it returns.
2218          *
2219          * In the current initial implementation of cpu_stop, the
2220          * above condition is already met when the control reaches
2221          * this point and the following smp_mb() is not strictly
2222          * necessary.  Do smp_mb() anyway for documentation and
2223          * robustness against future implementation changes.
2224          */
2225         smp_mb(); /* See above comment block. */
2226         return 0;
2227 }
2228
2229 /**
2230  * synchronize_sched_expedited - Brute-force RCU-sched grace period
2231  *
2232  * Wait for an RCU-sched grace period to elapse, but use a "big hammer"
2233  * approach to force the grace period to end quickly.  This consumes
2234  * significant time on all CPUs and is unfriendly to real-time workloads,
2235  * so is thus not recommended for any sort of common-case code.  In fact,
2236  * if you are using synchronize_sched_expedited() in a loop, please
2237  * restructure your code to batch your updates, and then use a single
2238  * synchronize_sched() instead.
2239  *
2240  * Note that it is illegal to call this function while holding any lock
2241  * that is acquired by a CPU-hotplug notifier.  And yes, it is also illegal
2242  * to call this function from a CPU-hotplug notifier.  Failing to observe
2243  * these restriction will result in deadlock.
2244  *
2245  * This implementation can be thought of as an application of ticket
2246  * locking to RCU, with sync_sched_expedited_started and
2247  * sync_sched_expedited_done taking on the roles of the halves
2248  * of the ticket-lock word.  Each task atomically increments
2249  * sync_sched_expedited_started upon entry, snapshotting the old value,
2250  * then attempts to stop all the CPUs.  If this succeeds, then each
2251  * CPU will have executed a context switch, resulting in an RCU-sched
2252  * grace period.  We are then done, so we use atomic_cmpxchg() to
2253  * update sync_sched_expedited_done to match our snapshot -- but
2254  * only if someone else has not already advanced past our snapshot.
2255  *
2256  * On the other hand, if try_stop_cpus() fails, we check the value
2257  * of sync_sched_expedited_done.  If it has advanced past our
2258  * initial snapshot, then someone else must have forced a grace period
2259  * some time after we took our snapshot.  In this case, our work is
2260  * done for us, and we can simply return.  Otherwise, we try again,
2261  * but keep our initial snapshot for purposes of checking for someone
2262  * doing our work for us.
2263  *
2264  * If we fail too many times in a row, we fall back to synchronize_sched().
2265  */
2266 void synchronize_sched_expedited(void)
2267 {
2268         int firstsnap, s, snap, trycount = 0;
2269
2270         /* Note that atomic_inc_return() implies full memory barrier. */
2271         firstsnap = snap = atomic_inc_return(&sync_sched_expedited_started);
2272         get_online_cpus();
2273         WARN_ON_ONCE(cpu_is_offline(raw_smp_processor_id()));
2274
2275         /*
2276          * Each pass through the following loop attempts to force a
2277          * context switch on each CPU.
2278          */
2279         while (try_stop_cpus(cpu_online_mask,
2280                              synchronize_sched_expedited_cpu_stop,
2281                              NULL) == -EAGAIN) {
2282                 put_online_cpus();
2283
2284                 /* No joy, try again later.  Or just synchronize_sched(). */
2285                 if (trycount++ < 10) {
2286                         udelay(trycount * num_online_cpus());
2287                 } else {
2288                         synchronize_sched();
2289                         return;
2290                 }
2291
2292                 /* Check to see if someone else did our work for us. */
2293                 s = atomic_read(&sync_sched_expedited_done);
2294                 if (UINT_CMP_GE((unsigned)s, (unsigned)firstsnap)) {
2295                         smp_mb(); /* ensure test happens before caller kfree */
2296                         return;
2297                 }
2298
2299                 /*
2300                  * Refetching sync_sched_expedited_started allows later
2301                  * callers to piggyback on our grace period.  We subtract
2302                  * 1 to get the same token that the last incrementer got.
2303                  * We retry after they started, so our grace period works
2304                  * for them, and they started after our first try, so their
2305                  * grace period works for us.
2306                  */
2307                 get_online_cpus();
2308                 snap = atomic_read(&sync_sched_expedited_started);
2309                 smp_mb(); /* ensure read is before try_stop_cpus(). */
2310         }
2311
2312         /*
2313          * Everyone up to our most recent fetch is covered by our grace
2314          * period.  Update the counter, but only if our work is still
2315          * relevant -- which it won't be if someone who started later
2316          * than we did beat us to the punch.
2317          */
2318         do {
2319                 s = atomic_read(&sync_sched_expedited_done);
2320                 if (UINT_CMP_GE((unsigned)s, (unsigned)snap)) {
2321                         smp_mb(); /* ensure test happens before caller kfree */
2322                         break;
2323                 }
2324         } while (atomic_cmpxchg(&sync_sched_expedited_done, s, snap) != s);
2325
2326         put_online_cpus();
2327 }
2328 EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
2329
2330 /*
2331  * Check to see if there is any immediate RCU-related work to be done
2332  * by the current CPU, for the specified type of RCU, returning 1 if so.
2333  * The checks are in order of increasing expense: checks that can be
2334  * carried out against CPU-local state are performed first.  However,
2335  * we must check for CPU stalls first, else we might not get a chance.
2336  */
2337 static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
2338 {
2339         struct rcu_node *rnp = rdp->mynode;
2340
2341         rdp->n_rcu_pending++;
2342
2343         /* Check for CPU stalls, if enabled. */
2344         check_cpu_stall(rsp, rdp);
2345
2346         /* Is the RCU core waiting for a quiescent state from this CPU? */
2347         if (rcu_scheduler_fully_active &&
2348             rdp->qs_pending && !rdp->passed_quiesce) {
2349                 rdp->n_rp_qs_pending++;
2350         } else if (rdp->qs_pending && rdp->passed_quiesce) {
2351                 rdp->n_rp_report_qs++;
2352                 return 1;
2353         }
2354
2355         /* Does this CPU have callbacks ready to invoke? */
2356         if (cpu_has_callbacks_ready_to_invoke(rdp)) {
2357                 rdp->n_rp_cb_ready++;
2358                 return 1;
2359         }
2360
2361         /* Has RCU gone idle with this CPU needing another grace period? */
2362         if (cpu_needs_another_gp(rsp, rdp)) {
2363                 rdp->n_rp_cpu_needs_gp++;
2364                 return 1;
2365         }
2366
2367         /* Has another RCU grace period completed?  */
2368         if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */
2369                 rdp->n_rp_gp_completed++;
2370                 return 1;
2371         }
2372
2373         /* Has a new RCU grace period started? */
2374         if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */
2375                 rdp->n_rp_gp_started++;
2376                 return 1;
2377         }
2378
2379         /* nothing to do */
2380         rdp->n_rp_need_nothing++;
2381         return 0;
2382 }
2383
2384 /*
2385  * Check to see if there is any immediate RCU-related work to be done
2386  * by the current CPU, returning 1 if so.  This function is part of the
2387  * RCU implementation; it is -not- an exported member of the RCU API.
2388  */
2389 static int rcu_pending(int cpu)
2390 {
2391         struct rcu_state *rsp;
2392
2393         for_each_rcu_flavor(rsp)
2394                 if (__rcu_pending(rsp, per_cpu_ptr(rsp->rda, cpu)))
2395                         return 1;
2396         return 0;
2397 }
2398
2399 /*
2400  * Check to see if any future RCU-related work will need to be done
2401  * by the current CPU, even if none need be done immediately, returning
2402  * 1 if so.
2403  */
2404 static int rcu_cpu_has_callbacks(int cpu)
2405 {
2406         struct rcu_state *rsp;
2407
2408         /* RCU callbacks either ready or pending? */
2409         for_each_rcu_flavor(rsp)
2410                 if (per_cpu_ptr(rsp->rda, cpu)->nxtlist)
2411                         return 1;
2412         return 0;
2413 }
2414
2415 /*
2416  * Helper function for _rcu_barrier() tracing.  If tracing is disabled,
2417  * the compiler is expected to optimize this away.
2418  */
2419 static void _rcu_barrier_trace(struct rcu_state *rsp, char *s,
2420                                int cpu, unsigned long done)
2421 {
2422         trace_rcu_barrier(rsp->name, s, cpu,
2423                           atomic_read(&rsp->barrier_cpu_count), done);
2424 }
2425
2426 /*
2427  * RCU callback function for _rcu_barrier().  If we are last, wake
2428  * up the task executing _rcu_barrier().
2429  */
2430 static void rcu_barrier_callback(struct rcu_head *rhp)
2431 {
2432         struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head);
2433         struct rcu_state *rsp = rdp->rsp;
2434
2435         if (atomic_dec_and_test(&rsp->barrier_cpu_count)) {
2436                 _rcu_barrier_trace(rsp, "LastCB", -1, rsp->n_barrier_done);
2437                 complete(&rsp->barrier_completion);
2438         } else {
2439                 _rcu_barrier_trace(rsp, "CB", -1, rsp->n_barrier_done);
2440         }
2441 }
2442
2443 /*
2444  * Called with preemption disabled, and from cross-cpu IRQ context.
2445  */
2446 static void rcu_barrier_func(void *type)
2447 {
2448         struct rcu_state *rsp = type;
2449         struct rcu_data *rdp = __this_cpu_ptr(rsp->rda);
2450
2451         _rcu_barrier_trace(rsp, "IRQ", -1, rsp->n_barrier_done);
2452         atomic_inc(&rsp->barrier_cpu_count);
2453         rsp->call(&rdp->barrier_head, rcu_barrier_callback);
2454 }
2455
2456 /*
2457  * Orchestrate the specified type of RCU barrier, waiting for all
2458  * RCU callbacks of the specified type to complete.
2459  */
2460 static void _rcu_barrier(struct rcu_state *rsp)
2461 {
2462         int cpu;
2463         struct rcu_data *rdp;
2464         unsigned long snap = ACCESS_ONCE(rsp->n_barrier_done);
2465         unsigned long snap_done;
2466
2467         _rcu_barrier_trace(rsp, "Begin", -1, snap);
2468
2469         /* Take mutex to serialize concurrent rcu_barrier() requests. */
2470         mutex_lock(&rsp->barrier_mutex);
2471
2472         /*
2473          * Ensure that all prior references, including to ->n_barrier_done,
2474          * are ordered before the _rcu_barrier() machinery.
2475          */
2476         smp_mb();  /* See above block comment. */
2477
2478         /*
2479          * Recheck ->n_barrier_done to see if others did our work for us.
2480          * This means checking ->n_barrier_done for an even-to-odd-to-even
2481          * transition.  The "if" expression below therefore rounds the old
2482          * value up to the next even number and adds two before comparing.
2483          */
2484         snap_done = ACCESS_ONCE(rsp->n_barrier_done);
2485         _rcu_barrier_trace(rsp, "Check", -1, snap_done);
2486         if (ULONG_CMP_GE(snap_done, ((snap + 1) & ~0x1) + 2)) {
2487                 _rcu_barrier_trace(rsp, "EarlyExit", -1, snap_done);
2488                 smp_mb(); /* caller's subsequent code after above check. */
2489                 mutex_unlock(&rsp->barrier_mutex);
2490                 return;
2491         }
2492
2493         /*
2494          * Increment ->n_barrier_done to avoid duplicate work.  Use
2495          * ACCESS_ONCE() to prevent the compiler from speculating
2496          * the increment to precede the early-exit check.
2497          */
2498         ACCESS_ONCE(rsp->n_barrier_done)++;
2499         WARN_ON_ONCE((rsp->n_barrier_done & 0x1) != 1);
2500         _rcu_barrier_trace(rsp, "Inc1", -1, rsp->n_barrier_done);
2501         smp_mb(); /* Order ->n_barrier_done increment with below mechanism. */
2502
2503         /*
2504          * Initialize the count to one rather than to zero in order to
2505          * avoid a too-soon return to zero in case of a short grace period
2506          * (or preemption of this task).  Exclude CPU-hotplug operations
2507          * to ensure that no offline CPU has callbacks queued.
2508          */
2509         init_completion(&rsp->barrier_completion);
2510         atomic_set(&rsp->barrier_cpu_count, 1);
2511         get_online_cpus();
2512
2513         /*
2514          * Force each CPU with callbacks to register a new callback.
2515          * When that callback is invoked, we will know that all of the
2516          * corresponding CPU's preceding callbacks have been invoked.
2517          */
2518         for_each_online_cpu(cpu) {
2519                 rdp = per_cpu_ptr(rsp->rda, cpu);
2520                 if (ACCESS_ONCE(rdp->qlen)) {
2521                         _rcu_barrier_trace(rsp, "OnlineQ", cpu,
2522                                            rsp->n_barrier_done);
2523                         smp_call_function_single(cpu, rcu_barrier_func, rsp, 1);
2524                 } else {
2525                         _rcu_barrier_trace(rsp, "OnlineNQ", cpu,
2526                                            rsp->n_barrier_done);
2527                 }
2528         }
2529         put_online_cpus();
2530
2531         /*
2532          * Now that we have an rcu_barrier_callback() callback on each
2533          * CPU, and thus each counted, remove the initial count.
2534          */
2535         if (atomic_dec_and_test(&rsp->barrier_cpu_count))
2536                 complete(&rsp->barrier_completion);
2537
2538         /* Increment ->n_barrier_done to prevent duplicate work. */
2539         smp_mb(); /* Keep increment after above mechanism. */
2540         ACCESS_ONCE(rsp->n_barrier_done)++;
2541         WARN_ON_ONCE((rsp->n_barrier_done & 0x1) != 0);
2542         _rcu_barrier_trace(rsp, "Inc2", -1, rsp->n_barrier_done);
2543         smp_mb(); /* Keep increment before caller's subsequent code. */
2544
2545         /* Wait for all rcu_barrier_callback() callbacks to be invoked. */
2546         wait_for_completion(&rsp->barrier_completion);
2547
2548         /* Other rcu_barrier() invocations can now safely proceed. */
2549         mutex_unlock(&rsp->barrier_mutex);
2550 }
2551
2552 /**
2553  * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
2554  */
2555 void rcu_barrier_bh(void)
2556 {
2557         _rcu_barrier(&rcu_bh_state);
2558 }
2559 EXPORT_SYMBOL_GPL(rcu_barrier_bh);
2560
2561 /**
2562  * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
2563  */
2564 void rcu_barrier_sched(void)
2565 {
2566         _rcu_barrier(&rcu_sched_state);
2567 }
2568 EXPORT_SYMBOL_GPL(rcu_barrier_sched);
2569
2570 /*
2571  * Do boot-time initialization of a CPU's per-CPU RCU data.
2572  */
2573 static void __init
2574 rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
2575 {
2576         unsigned long flags;
2577         struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
2578         struct rcu_node *rnp = rcu_get_root(rsp);
2579
2580         /* Set up local state, ensuring consistent view of global state. */
2581         raw_spin_lock_irqsave(&rnp->lock, flags);
2582         rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
2583         init_callback_list(rdp);
2584         rdp->qlen_lazy = 0;
2585         ACCESS_ONCE(rdp->qlen) = 0;
2586         rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
2587         WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_EXIT_IDLE);
2588         WARN_ON_ONCE(atomic_read(&rdp->dynticks->dynticks) != 1);
2589         rdp->cpu = cpu;
2590         rdp->rsp = rsp;
2591         raw_spin_unlock_irqrestore(&rnp->lock, flags);
2592 }
2593
2594 /*
2595  * Initialize a CPU's per-CPU RCU data.  Note that only one online or
2596  * offline event can be happening at a given time.  Note also that we
2597  * can accept some slop in the rsp->completed access due to the fact
2598  * that this CPU cannot possibly have any RCU callbacks in flight yet.
2599  */
2600 static void __cpuinit
2601 rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptible)
2602 {
2603         unsigned long flags;
2604         unsigned long mask;
2605         struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
2606         struct rcu_node *rnp = rcu_get_root(rsp);
2607
2608         /* Set up local state, ensuring consistent view of global state. */
2609         raw_spin_lock_irqsave(&rnp->lock, flags);
2610         rdp->beenonline = 1;     /* We have now been online. */
2611         rdp->preemptible = preemptible;
2612         rdp->qlen_last_fqs_check = 0;
2613         rdp->n_force_qs_snap = rsp->n_force_qs;
2614         rdp->blimit = blimit;
2615         init_callback_list(rdp);  /* Re-enable callbacks on this CPU. */
2616         rdp->dynticks->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE;
2617         atomic_set(&rdp->dynticks->dynticks,
2618                    (atomic_read(&rdp->dynticks->dynticks) & ~0x1) + 1);
2619         rcu_prepare_for_idle_init(cpu);
2620         raw_spin_unlock(&rnp->lock);            /* irqs remain disabled. */
2621
2622         /*
2623          * A new grace period might start here.  If so, we won't be part
2624          * of it, but that is OK, as we are currently in a quiescent state.
2625          */
2626
2627         /* Exclude any attempts to start a new GP on large systems. */
2628         raw_spin_lock(&rsp->onofflock);         /* irqs already disabled. */
2629
2630         /* Add CPU to rcu_node bitmasks. */
2631         rnp = rdp->mynode;
2632         mask = rdp->grpmask;
2633         do {
2634                 /* Exclude any attempts to start a new GP on small systems. */
2635                 raw_spin_lock(&rnp->lock);      /* irqs already disabled. */
2636                 rnp->qsmaskinit |= mask;
2637                 mask = rnp->grpmask;
2638                 if (rnp == rdp->mynode) {
2639                         /*
2640                          * If there is a grace period in progress, we will
2641                          * set up to wait for it next time we run the
2642                          * RCU core code.
2643                          */
2644                         rdp->gpnum = rnp->completed;
2645                         rdp->completed = rnp->completed;
2646                         rdp->passed_quiesce = 0;
2647                         rdp->qs_pending = 0;
2648                         trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpuonl");
2649                 }
2650                 raw_spin_unlock(&rnp->lock); /* irqs already disabled. */
2651                 rnp = rnp->parent;
2652         } while (rnp != NULL && !(rnp->qsmaskinit & mask));
2653
2654         raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
2655 }
2656
2657 static void __cpuinit rcu_prepare_cpu(int cpu)
2658 {
2659         struct rcu_state *rsp;
2660
2661         for_each_rcu_flavor(rsp)
2662                 rcu_init_percpu_data(cpu, rsp,
2663                                      strcmp(rsp->name, "rcu_preempt") == 0);
2664 }
2665
2666 /*
2667  * Handle CPU online/offline notification events.
2668  */
2669 static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
2670                                     unsigned long action, void *hcpu)
2671 {
2672         long cpu = (long)hcpu;
2673         struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
2674         struct rcu_node *rnp = rdp->mynode;
2675         struct rcu_state *rsp;
2676
2677         trace_rcu_utilization("Start CPU hotplug");
2678         switch (action) {
2679         case CPU_UP_PREPARE:
2680         case CPU_UP_PREPARE_FROZEN:
2681                 rcu_prepare_cpu(cpu);
2682                 rcu_prepare_kthreads(cpu);
2683                 break;
2684         case CPU_ONLINE:
2685         case CPU_DOWN_FAILED:
2686                 rcu_boost_kthread_setaffinity(rnp, -1);
2687                 break;
2688         case CPU_DOWN_PREPARE:
2689                 rcu_boost_kthread_setaffinity(rnp, cpu);
2690                 break;
2691         case CPU_DYING:
2692         case CPU_DYING_FROZEN:
2693                 /*
2694                  * The whole machine is "stopped" except this CPU, so we can
2695                  * touch any data without introducing corruption. We send the
2696                  * dying CPU's callbacks to an arbitrarily chosen online CPU.
2697                  */
2698                 for_each_rcu_flavor(rsp)
2699                         rcu_cleanup_dying_cpu(rsp);
2700                 rcu_cleanup_after_idle(cpu);
2701                 break;
2702         case CPU_DEAD:
2703         case CPU_DEAD_FROZEN:
2704         case CPU_UP_CANCELED:
2705         case CPU_UP_CANCELED_FROZEN:
2706                 for_each_rcu_flavor(rsp)
2707                         rcu_cleanup_dead_cpu(cpu, rsp);
2708                 break;
2709         default:
2710                 break;
2711         }
2712         trace_rcu_utilization("End CPU hotplug");
2713         return NOTIFY_OK;
2714 }
2715
2716 /*
2717  * Spawn the kthread that handles this RCU flavor's grace periods.
2718  */
2719 static int __init rcu_spawn_gp_kthread(void)
2720 {
2721         unsigned long flags;
2722         struct rcu_node *rnp;
2723         struct rcu_state *rsp;
2724         struct task_struct *t;
2725
2726         for_each_rcu_flavor(rsp) {
2727                 t = kthread_run(rcu_gp_kthread, rsp, rsp->name);
2728                 BUG_ON(IS_ERR(t));
2729                 rnp = rcu_get_root(rsp);
2730                 raw_spin_lock_irqsave(&rnp->lock, flags);
2731                 rsp->gp_kthread = t;
2732                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
2733         }
2734         return 0;
2735 }
2736 early_initcall(rcu_spawn_gp_kthread);
2737
2738 /*
2739  * This function is invoked towards the end of the scheduler's initialization
2740  * process.  Before this is called, the idle task might contain
2741  * RCU read-side critical sections (during which time, this idle
2742  * task is booting the system).  After this function is called, the
2743  * idle tasks are prohibited from containing RCU read-side critical
2744  * sections.  This function also enables RCU lockdep checking.
2745  */
2746 void rcu_scheduler_starting(void)
2747 {
2748         WARN_ON(num_online_cpus() != 1);
2749         WARN_ON(nr_context_switches() > 0);
2750         rcu_scheduler_active = 1;
2751 }
2752
2753 /*
2754  * Compute the per-level fanout, either using the exact fanout specified
2755  * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
2756  */
2757 #ifdef CONFIG_RCU_FANOUT_EXACT
2758 static void __init rcu_init_levelspread(struct rcu_state *rsp)
2759 {
2760         int i;
2761
2762         for (i = rcu_num_lvls - 1; i > 0; i--)
2763                 rsp->levelspread[i] = CONFIG_RCU_FANOUT;
2764         rsp->levelspread[0] = rcu_fanout_leaf;
2765 }
2766 #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
2767 static void __init rcu_init_levelspread(struct rcu_state *rsp)
2768 {
2769         int ccur;
2770         int cprv;
2771         int i;
2772
2773         cprv = nr_cpu_ids;
2774         for (i = rcu_num_lvls - 1; i >= 0; i--) {
2775                 ccur = rsp->levelcnt[i];
2776                 rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
2777                 cprv = ccur;
2778         }
2779 }
2780 #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
2781
2782 /*
2783  * Helper function for rcu_init() that initializes one rcu_state structure.
2784  */
2785 static void __init rcu_init_one(struct rcu_state *rsp,
2786                 struct rcu_data __percpu *rda)
2787 {
2788         static char *buf[] = { "rcu_node_0",
2789                                "rcu_node_1",
2790                                "rcu_node_2",
2791                                "rcu_node_3" };  /* Match MAX_RCU_LVLS */
2792         static char *fqs[] = { "rcu_node_fqs_0",
2793                                "rcu_node_fqs_1",
2794                                "rcu_node_fqs_2",
2795                                "rcu_node_fqs_3" };  /* Match MAX_RCU_LVLS */
2796         int cpustride = 1;
2797         int i;
2798         int j;
2799         struct rcu_node *rnp;
2800
2801         BUILD_BUG_ON(MAX_RCU_LVLS > ARRAY_SIZE(buf));  /* Fix buf[] init! */
2802
2803         /* Initialize the level-tracking arrays. */
2804
2805         for (i = 0; i < rcu_num_lvls; i++)
2806                 rsp->levelcnt[i] = num_rcu_lvl[i];
2807         for (i = 1; i < rcu_num_lvls; i++)
2808                 rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
2809         rcu_init_levelspread(rsp);
2810
2811         /* Initialize the elements themselves, starting from the leaves. */
2812
2813         for (i = rcu_num_lvls - 1; i >= 0; i--) {
2814                 cpustride *= rsp->levelspread[i];
2815                 rnp = rsp->level[i];
2816                 for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
2817                         raw_spin_lock_init(&rnp->lock);
2818                         lockdep_set_class_and_name(&rnp->lock,
2819                                                    &rcu_node_class[i], buf[i]);
2820                         raw_spin_lock_init(&rnp->fqslock);
2821                         lockdep_set_class_and_name(&rnp->fqslock,
2822                                                    &rcu_fqs_class[i], fqs[i]);
2823                         rnp->gpnum = rsp->gpnum;
2824                         rnp->completed = rsp->completed;
2825                         rnp->qsmask = 0;
2826                         rnp->qsmaskinit = 0;
2827                         rnp->grplo = j * cpustride;
2828                         rnp->grphi = (j + 1) * cpustride - 1;
2829                         if (rnp->grphi >= NR_CPUS)
2830                                 rnp->grphi = NR_CPUS - 1;
2831                         if (i == 0) {
2832                                 rnp->grpnum = 0;
2833                                 rnp->grpmask = 0;
2834                                 rnp->parent = NULL;
2835                         } else {
2836                                 rnp->grpnum = j % rsp->levelspread[i - 1];
2837                                 rnp->grpmask = 1UL << rnp->grpnum;
2838                                 rnp->parent = rsp->level[i - 1] +
2839                                               j / rsp->levelspread[i - 1];
2840                         }
2841                         rnp->level = i;
2842                         INIT_LIST_HEAD(&rnp->blkd_tasks);
2843                 }
2844         }
2845
2846         rsp->rda = rda;
2847         init_waitqueue_head(&rsp->gp_wq);
2848         rnp = rsp->level[rcu_num_lvls - 1];
2849         for_each_possible_cpu(i) {
2850                 while (i > rnp->grphi)
2851                         rnp++;
2852                 per_cpu_ptr(rsp->rda, i)->mynode = rnp;
2853                 rcu_boot_init_percpu_data(i, rsp);
2854         }
2855         list_add(&rsp->flavors, &rcu_struct_flavors);
2856 }
2857
2858 /*
2859  * Compute the rcu_node tree geometry from kernel parameters.  This cannot
2860  * replace the definitions in rcutree.h because those are needed to size
2861  * the ->node array in the rcu_state structure.
2862  */
2863 static void __init rcu_init_geometry(void)
2864 {
2865         int i;
2866         int j;
2867         int n = nr_cpu_ids;
2868         int rcu_capacity[MAX_RCU_LVLS + 1];
2869
2870         /* If the compile-time values are accurate, just leave. */
2871         if (rcu_fanout_leaf == CONFIG_RCU_FANOUT_LEAF &&
2872             nr_cpu_ids == NR_CPUS)
2873                 return;
2874
2875         /*
2876          * Compute number of nodes that can be handled an rcu_node tree
2877          * with the given number of levels.  Setting rcu_capacity[0] makes
2878          * some of the arithmetic easier.
2879          */
2880         rcu_capacity[0] = 1;
2881         rcu_capacity[1] = rcu_fanout_leaf;
2882         for (i = 2; i <= MAX_RCU_LVLS; i++)
2883                 rcu_capacity[i] = rcu_capacity[i - 1] * CONFIG_RCU_FANOUT;
2884
2885         /*
2886          * The boot-time rcu_fanout_leaf parameter is only permitted
2887          * to increase the leaf-level fanout, not decrease it.  Of course,
2888          * the leaf-level fanout cannot exceed the number of bits in
2889          * the rcu_node masks.  Finally, the tree must be able to accommodate
2890          * the configured number of CPUs.  Complain and fall back to the
2891          * compile-time values if these limits are exceeded.
2892          */
2893         if (rcu_fanout_leaf < CONFIG_RCU_FANOUT_LEAF ||
2894             rcu_fanout_leaf > sizeof(unsigned long) * 8 ||
2895             n > rcu_capacity[MAX_RCU_LVLS]) {
2896                 WARN_ON(1);
2897                 return;
2898         }
2899
2900         /* Calculate the number of rcu_nodes at each level of the tree. */
2901         for (i = 1; i <= MAX_RCU_LVLS; i++)
2902                 if (n <= rcu_capacity[i]) {
2903                         for (j = 0; j <= i; j++)
2904                                 num_rcu_lvl[j] =
2905                                         DIV_ROUND_UP(n, rcu_capacity[i - j]);
2906                         rcu_num_lvls = i;
2907                         for (j = i + 1; j <= MAX_RCU_LVLS; j++)
2908                                 num_rcu_lvl[j] = 0;
2909                         break;
2910                 }
2911
2912         /* Calculate the total number of rcu_node structures. */
2913         rcu_num_nodes = 0;
2914         for (i = 0; i <= MAX_RCU_LVLS; i++)
2915                 rcu_num_nodes += num_rcu_lvl[i];
2916         rcu_num_nodes -= n;
2917 }
2918
2919 void __init rcu_init(void)
2920 {
2921         int cpu;
2922
2923         rcu_bootup_announce();
2924         rcu_init_geometry();
2925         rcu_init_one(&rcu_sched_state, &rcu_sched_data);
2926         rcu_init_one(&rcu_bh_state, &rcu_bh_data);
2927         __rcu_init_preempt();
2928          open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
2929
2930         /*
2931          * We don't need protection against CPU-hotplug here because
2932          * this is called early in boot, before either interrupts
2933          * or the scheduler are operational.
2934          */
2935         cpu_notifier(rcu_cpu_notify, 0);
2936         for_each_online_cpu(cpu)
2937                 rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
2938         check_cpu_stall_init();
2939 }
2940
2941 #include "rcutree_plugin.h"