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