rcu: Fix rcu_lock_map build failure on CONFIG_PROVE_LOCKING=y
[safe/jmp/linux-2.6] / 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 <asm/atomic.h>
40 #include <linux/bitops.h>
41 #include <linux/module.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
50 #include "rcutree.h"
51
52 /* Data structures. */
53
54 #define RCU_STATE_INITIALIZER(name) { \
55         .level = { &name.node[0] }, \
56         .levelcnt = { \
57                 NUM_RCU_LVL_0,  /* root of hierarchy. */ \
58                 NUM_RCU_LVL_1, \
59                 NUM_RCU_LVL_2, \
60                 NUM_RCU_LVL_3, /* == MAX_RCU_LVLS */ \
61         }, \
62         .signaled = RCU_SIGNAL_INIT, \
63         .gpnum = -300, \
64         .completed = -300, \
65         .onofflock = __SPIN_LOCK_UNLOCKED(&name.onofflock), \
66         .fqslock = __SPIN_LOCK_UNLOCKED(&name.fqslock), \
67         .n_force_qs = 0, \
68         .n_force_qs_ngp = 0, \
69 }
70
71 struct rcu_state rcu_sched_state = RCU_STATE_INITIALIZER(rcu_sched_state);
72 DEFINE_PER_CPU(struct rcu_data, rcu_sched_data);
73
74 struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh_state);
75 DEFINE_PER_CPU(struct rcu_data, rcu_bh_data);
76
77
78 /*
79  * Return true if an RCU grace period is in progress.  The ACCESS_ONCE()s
80  * permit this function to be invoked without holding the root rcu_node
81  * structure's ->lock, but of course results can be subject to change.
82  */
83 static int rcu_gp_in_progress(struct rcu_state *rsp)
84 {
85         return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum);
86 }
87
88 /*
89  * Note a quiescent state.  Because we do not need to know
90  * how many quiescent states passed, just if there was at least
91  * one since the start of the grace period, this just sets a flag.
92  */
93 void rcu_sched_qs(int cpu)
94 {
95         struct rcu_data *rdp;
96
97         rdp = &per_cpu(rcu_sched_data, cpu);
98         rdp->passed_quiesc_completed = rdp->completed;
99         barrier();
100         rdp->passed_quiesc = 1;
101         rcu_preempt_note_context_switch(cpu);
102 }
103
104 void rcu_bh_qs(int cpu)
105 {
106         struct rcu_data *rdp;
107
108         rdp = &per_cpu(rcu_bh_data, cpu);
109         rdp->passed_quiesc_completed = rdp->completed;
110         barrier();
111         rdp->passed_quiesc = 1;
112 }
113
114 #ifdef CONFIG_NO_HZ
115 DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = {
116         .dynticks_nesting = 1,
117         .dynticks = 1,
118 };
119 #endif /* #ifdef CONFIG_NO_HZ */
120
121 static int blimit = 10;         /* Maximum callbacks per softirq. */
122 static int qhimark = 10000;     /* If this many pending, ignore blimit. */
123 static int qlowmark = 100;      /* Once only this many pending, use blimit. */
124
125 static void force_quiescent_state(struct rcu_state *rsp, int relaxed);
126 static int rcu_pending(int cpu);
127
128 /*
129  * Return the number of RCU-sched batches processed thus far for debug & stats.
130  */
131 long rcu_batches_completed_sched(void)
132 {
133         return rcu_sched_state.completed;
134 }
135 EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
136
137 /*
138  * Return the number of RCU BH batches processed thus far for debug & stats.
139  */
140 long rcu_batches_completed_bh(void)
141 {
142         return rcu_bh_state.completed;
143 }
144 EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
145
146 /*
147  * Does the CPU have callbacks ready to be invoked?
148  */
149 static int
150 cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)
151 {
152         return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];
153 }
154
155 /*
156  * Does the current CPU require a yet-as-unscheduled grace period?
157  */
158 static int
159 cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp)
160 {
161         return *rdp->nxttail[RCU_DONE_TAIL] && !rcu_gp_in_progress(rsp);
162 }
163
164 /*
165  * Return the root node of the specified rcu_state structure.
166  */
167 static struct rcu_node *rcu_get_root(struct rcu_state *rsp)
168 {
169         return &rsp->node[0];
170 }
171
172 #ifdef CONFIG_SMP
173
174 /*
175  * If the specified CPU is offline, tell the caller that it is in
176  * a quiescent state.  Otherwise, whack it with a reschedule IPI.
177  * Grace periods can end up waiting on an offline CPU when that
178  * CPU is in the process of coming online -- it will be added to the
179  * rcu_node bitmasks before it actually makes it online.  The same thing
180  * can happen while a CPU is in the process of coming online.  Because this
181  * race is quite rare, we check for it after detecting that the grace
182  * period has been delayed rather than checking each and every CPU
183  * each and every time we start a new grace period.
184  */
185 static int rcu_implicit_offline_qs(struct rcu_data *rdp)
186 {
187         /*
188          * If the CPU is offline, it is in a quiescent state.  We can
189          * trust its state not to change because interrupts are disabled.
190          */
191         if (cpu_is_offline(rdp->cpu)) {
192                 rdp->offline_fqs++;
193                 return 1;
194         }
195
196         /* If preemptable RCU, no point in sending reschedule IPI. */
197         if (rdp->preemptable)
198                 return 0;
199
200         /* The CPU is online, so send it a reschedule IPI. */
201         if (rdp->cpu != smp_processor_id())
202                 smp_send_reschedule(rdp->cpu);
203         else
204                 set_need_resched();
205         rdp->resched_ipi++;
206         return 0;
207 }
208
209 #endif /* #ifdef CONFIG_SMP */
210
211 #ifdef CONFIG_NO_HZ
212
213 /**
214  * rcu_enter_nohz - inform RCU that current CPU is entering nohz
215  *
216  * Enter nohz mode, in other words, -leave- the mode in which RCU
217  * read-side critical sections can occur.  (Though RCU read-side
218  * critical sections can occur in irq handlers in nohz mode, a possibility
219  * handled by rcu_irq_enter() and rcu_irq_exit()).
220  */
221 void rcu_enter_nohz(void)
222 {
223         unsigned long flags;
224         struct rcu_dynticks *rdtp;
225
226         smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
227         local_irq_save(flags);
228         rdtp = &__get_cpu_var(rcu_dynticks);
229         rdtp->dynticks++;
230         rdtp->dynticks_nesting--;
231         WARN_ON_ONCE(rdtp->dynticks & 0x1);
232         local_irq_restore(flags);
233 }
234
235 /*
236  * rcu_exit_nohz - inform RCU that current CPU is leaving nohz
237  *
238  * Exit nohz mode, in other words, -enter- the mode in which RCU
239  * read-side critical sections normally occur.
240  */
241 void rcu_exit_nohz(void)
242 {
243         unsigned long flags;
244         struct rcu_dynticks *rdtp;
245
246         local_irq_save(flags);
247         rdtp = &__get_cpu_var(rcu_dynticks);
248         rdtp->dynticks++;
249         rdtp->dynticks_nesting++;
250         WARN_ON_ONCE(!(rdtp->dynticks & 0x1));
251         local_irq_restore(flags);
252         smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
253 }
254
255 /**
256  * rcu_nmi_enter - inform RCU of entry to NMI context
257  *
258  * If the CPU was idle with dynamic ticks active, and there is no
259  * irq handler running, this updates rdtp->dynticks_nmi to let the
260  * RCU grace-period handling know that the CPU is active.
261  */
262 void rcu_nmi_enter(void)
263 {
264         struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
265
266         if (rdtp->dynticks & 0x1)
267                 return;
268         rdtp->dynticks_nmi++;
269         WARN_ON_ONCE(!(rdtp->dynticks_nmi & 0x1));
270         smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
271 }
272
273 /**
274  * rcu_nmi_exit - inform RCU of exit from NMI context
275  *
276  * If the CPU was idle with dynamic ticks active, and there is no
277  * irq handler running, this updates rdtp->dynticks_nmi to let the
278  * RCU grace-period handling know that the CPU is no longer active.
279  */
280 void rcu_nmi_exit(void)
281 {
282         struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
283
284         if (rdtp->dynticks & 0x1)
285                 return;
286         smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
287         rdtp->dynticks_nmi++;
288         WARN_ON_ONCE(rdtp->dynticks_nmi & 0x1);
289 }
290
291 /**
292  * rcu_irq_enter - inform RCU of entry to hard irq context
293  *
294  * If the CPU was idle with dynamic ticks active, this updates the
295  * rdtp->dynticks to let the RCU handling know that the CPU is active.
296  */
297 void rcu_irq_enter(void)
298 {
299         struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
300
301         if (rdtp->dynticks_nesting++)
302                 return;
303         rdtp->dynticks++;
304         WARN_ON_ONCE(!(rdtp->dynticks & 0x1));
305         smp_mb(); /* CPUs seeing ++ must see later RCU read-side crit sects */
306 }
307
308 /**
309  * rcu_irq_exit - inform RCU of exit from hard irq context
310  *
311  * If the CPU was idle with dynamic ticks active, update the rdp->dynticks
312  * to put let the RCU handling be aware that the CPU is going back to idle
313  * with no ticks.
314  */
315 void rcu_irq_exit(void)
316 {
317         struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks);
318
319         if (--rdtp->dynticks_nesting)
320                 return;
321         smp_mb(); /* CPUs seeing ++ must see prior RCU read-side crit sects */
322         rdtp->dynticks++;
323         WARN_ON_ONCE(rdtp->dynticks & 0x1);
324
325         /* If the interrupt queued a callback, get out of dyntick mode. */
326         if (__get_cpu_var(rcu_sched_data).nxtlist ||
327             __get_cpu_var(rcu_bh_data).nxtlist)
328                 set_need_resched();
329 }
330
331 /*
332  * Record the specified "completed" value, which is later used to validate
333  * dynticks counter manipulations.  Specify "rsp->completed - 1" to
334  * unconditionally invalidate any future dynticks manipulations (which is
335  * useful at the beginning of a grace period).
336  */
337 static void dyntick_record_completed(struct rcu_state *rsp, long comp)
338 {
339         rsp->dynticks_completed = comp;
340 }
341
342 #ifdef CONFIG_SMP
343
344 /*
345  * Recall the previously recorded value of the completion for dynticks.
346  */
347 static long dyntick_recall_completed(struct rcu_state *rsp)
348 {
349         return rsp->dynticks_completed;
350 }
351
352 /*
353  * Snapshot the specified CPU's dynticks counter so that we can later
354  * credit them with an implicit quiescent state.  Return 1 if this CPU
355  * is in dynticks idle mode, which is an extended quiescent state.
356  */
357 static int dyntick_save_progress_counter(struct rcu_data *rdp)
358 {
359         int ret;
360         int snap;
361         int snap_nmi;
362
363         snap = rdp->dynticks->dynticks;
364         snap_nmi = rdp->dynticks->dynticks_nmi;
365         smp_mb();       /* Order sampling of snap with end of grace period. */
366         rdp->dynticks_snap = snap;
367         rdp->dynticks_nmi_snap = snap_nmi;
368         ret = ((snap & 0x1) == 0) && ((snap_nmi & 0x1) == 0);
369         if (ret)
370                 rdp->dynticks_fqs++;
371         return ret;
372 }
373
374 /*
375  * Return true if the specified CPU has passed through a quiescent
376  * state by virtue of being in or having passed through an dynticks
377  * idle state since the last call to dyntick_save_progress_counter()
378  * for this same CPU.
379  */
380 static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
381 {
382         long curr;
383         long curr_nmi;
384         long snap;
385         long snap_nmi;
386
387         curr = rdp->dynticks->dynticks;
388         snap = rdp->dynticks_snap;
389         curr_nmi = rdp->dynticks->dynticks_nmi;
390         snap_nmi = rdp->dynticks_nmi_snap;
391         smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
392
393         /*
394          * If the CPU passed through or entered a dynticks idle phase with
395          * no active irq/NMI handlers, then we can safely pretend that the CPU
396          * already acknowledged the request to pass through a quiescent
397          * state.  Either way, that CPU cannot possibly be in an RCU
398          * read-side critical section that started before the beginning
399          * of the current RCU grace period.
400          */
401         if ((curr != snap || (curr & 0x1) == 0) &&
402             (curr_nmi != snap_nmi || (curr_nmi & 0x1) == 0)) {
403                 rdp->dynticks_fqs++;
404                 return 1;
405         }
406
407         /* Go check for the CPU being offline. */
408         return rcu_implicit_offline_qs(rdp);
409 }
410
411 #endif /* #ifdef CONFIG_SMP */
412
413 #else /* #ifdef CONFIG_NO_HZ */
414
415 static void dyntick_record_completed(struct rcu_state *rsp, long comp)
416 {
417 }
418
419 #ifdef CONFIG_SMP
420
421 /*
422  * If there are no dynticks, then the only way that a CPU can passively
423  * be in a quiescent state is to be offline.  Unlike dynticks idle, which
424  * is a point in time during the prior (already finished) grace period,
425  * an offline CPU is always in a quiescent state, and thus can be
426  * unconditionally applied.  So just return the current value of completed.
427  */
428 static long dyntick_recall_completed(struct rcu_state *rsp)
429 {
430         return rsp->completed;
431 }
432
433 static int dyntick_save_progress_counter(struct rcu_data *rdp)
434 {
435         return 0;
436 }
437
438 static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
439 {
440         return rcu_implicit_offline_qs(rdp);
441 }
442
443 #endif /* #ifdef CONFIG_SMP */
444
445 #endif /* #else #ifdef CONFIG_NO_HZ */
446
447 #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
448
449 static void record_gp_stall_check_time(struct rcu_state *rsp)
450 {
451         rsp->gp_start = jiffies;
452         rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_CHECK;
453 }
454
455 static void print_other_cpu_stall(struct rcu_state *rsp)
456 {
457         int cpu;
458         long delta;
459         unsigned long flags;
460         struct rcu_node *rnp = rcu_get_root(rsp);
461         struct rcu_node *rnp_cur = rsp->level[NUM_RCU_LVLS - 1];
462         struct rcu_node *rnp_end = &rsp->node[NUM_RCU_NODES];
463
464         /* Only let one CPU complain about others per time interval. */
465
466         spin_lock_irqsave(&rnp->lock, flags);
467         delta = jiffies - rsp->jiffies_stall;
468         if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) {
469                 spin_unlock_irqrestore(&rnp->lock, flags);
470                 return;
471         }
472         rsp->jiffies_stall = jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
473         spin_unlock_irqrestore(&rnp->lock, flags);
474
475         /* OK, time to rat on our buddy... */
476
477         printk(KERN_ERR "INFO: RCU detected CPU stalls:");
478         for (; rnp_cur < rnp_end; rnp_cur++) {
479                 rcu_print_task_stall(rnp);
480                 if (rnp_cur->qsmask == 0)
481                         continue;
482                 for (cpu = 0; cpu <= rnp_cur->grphi - rnp_cur->grplo; cpu++)
483                         if (rnp_cur->qsmask & (1UL << cpu))
484                                 printk(" %d", rnp_cur->grplo + cpu);
485         }
486         printk(" (detected by %d, t=%ld jiffies)\n",
487                smp_processor_id(), (long)(jiffies - rsp->gp_start));
488         trigger_all_cpu_backtrace();
489
490         force_quiescent_state(rsp, 0);  /* Kick them all. */
491 }
492
493 static void print_cpu_stall(struct rcu_state *rsp)
494 {
495         unsigned long flags;
496         struct rcu_node *rnp = rcu_get_root(rsp);
497
498         printk(KERN_ERR "INFO: RCU detected CPU %d stall (t=%lu jiffies)\n",
499                         smp_processor_id(), jiffies - rsp->gp_start);
500         trigger_all_cpu_backtrace();
501
502         spin_lock_irqsave(&rnp->lock, flags);
503         if ((long)(jiffies - rsp->jiffies_stall) >= 0)
504                 rsp->jiffies_stall =
505                         jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
506         spin_unlock_irqrestore(&rnp->lock, flags);
507
508         set_need_resched();  /* kick ourselves to get things going. */
509 }
510
511 static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
512 {
513         long delta;
514         struct rcu_node *rnp;
515
516         delta = jiffies - rsp->jiffies_stall;
517         rnp = rdp->mynode;
518         if ((rnp->qsmask & rdp->grpmask) && delta >= 0) {
519
520                 /* We haven't checked in, so go dump stack. */
521                 print_cpu_stall(rsp);
522
523         } else if (rcu_gp_in_progress(rsp) && delta >= RCU_STALL_RAT_DELAY) {
524
525                 /* They had two time units to dump stack, so complain. */
526                 print_other_cpu_stall(rsp);
527         }
528 }
529
530 #else /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
531
532 static void record_gp_stall_check_time(struct rcu_state *rsp)
533 {
534 }
535
536 static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp)
537 {
538 }
539
540 #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
541
542 /*
543  * Update CPU-local rcu_data state to record the newly noticed grace period.
544  * This is used both when we started the grace period and when we notice
545  * that someone else started the grace period.
546  */
547 static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp)
548 {
549         rdp->qs_pending = 1;
550         rdp->passed_quiesc = 0;
551         rdp->gpnum = rsp->gpnum;
552 }
553
554 /*
555  * Did someone else start a new RCU grace period start since we last
556  * checked?  Update local state appropriately if so.  Must be called
557  * on the CPU corresponding to rdp.
558  */
559 static int
560 check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp)
561 {
562         unsigned long flags;
563         int ret = 0;
564
565         local_irq_save(flags);
566         if (rdp->gpnum != rsp->gpnum) {
567                 note_new_gpnum(rsp, rdp);
568                 ret = 1;
569         }
570         local_irq_restore(flags);
571         return ret;
572 }
573
574 /*
575  * Start a new RCU grace period if warranted, re-initializing the hierarchy
576  * in preparation for detecting the next grace period.  The caller must hold
577  * the root node's ->lock, which is released before return.  Hard irqs must
578  * be disabled.
579  */
580 static void
581 rcu_start_gp(struct rcu_state *rsp, unsigned long flags)
582         __releases(rcu_get_root(rsp)->lock)
583 {
584         struct rcu_data *rdp = rsp->rda[smp_processor_id()];
585         struct rcu_node *rnp = rcu_get_root(rsp);
586
587         if (!cpu_needs_another_gp(rsp, rdp)) {
588                 spin_unlock_irqrestore(&rnp->lock, flags);
589                 return;
590         }
591
592         /* Advance to a new grace period and initialize state. */
593         rsp->gpnum++;
594         WARN_ON_ONCE(rsp->signaled == RCU_GP_INIT);
595         rsp->signaled = RCU_GP_INIT; /* Hold off force_quiescent_state. */
596         rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
597         record_gp_stall_check_time(rsp);
598         dyntick_record_completed(rsp, rsp->completed - 1);
599         note_new_gpnum(rsp, rdp);
600
601         /*
602          * Because this CPU just now started the new grace period, we know
603          * that all of its callbacks will be covered by this upcoming grace
604          * period, even the ones that were registered arbitrarily recently.
605          * Therefore, advance all outstanding callbacks to RCU_WAIT_TAIL.
606          *
607          * Other CPUs cannot be sure exactly when the grace period started.
608          * Therefore, their recently registered callbacks must pass through
609          * an additional RCU_NEXT_READY stage, so that they will be handled
610          * by the next RCU grace period.
611          */
612         rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
613         rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
614
615         /* Special-case the common single-level case. */
616         if (NUM_RCU_NODES == 1) {
617                 rcu_preempt_check_blocked_tasks(rnp);
618                 rnp->qsmask = rnp->qsmaskinit;
619                 rnp->gpnum = rsp->gpnum;
620                 rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state OK. */
621                 spin_unlock_irqrestore(&rnp->lock, flags);
622                 return;
623         }
624
625         spin_unlock(&rnp->lock);  /* leave irqs disabled. */
626
627
628         /* Exclude any concurrent CPU-hotplug operations. */
629         spin_lock(&rsp->onofflock);  /* irqs already disabled. */
630
631         /*
632          * Set the quiescent-state-needed bits in all the rcu_node
633          * structures for all currently online CPUs in breadth-first
634          * order, starting from the root rcu_node structure.  This
635          * operation relies on the layout of the hierarchy within the
636          * rsp->node[] array.  Note that other CPUs will access only
637          * the leaves of the hierarchy, which still indicate that no
638          * grace period is in progress, at least until the corresponding
639          * leaf node has been initialized.  In addition, we have excluded
640          * CPU-hotplug operations.
641          *
642          * Note that the grace period cannot complete until we finish
643          * the initialization process, as there will be at least one
644          * qsmask bit set in the root node until that time, namely the
645          * one corresponding to this CPU, due to the fact that we have
646          * irqs disabled.
647          */
648         for (rnp = &rsp->node[0]; rnp < &rsp->node[NUM_RCU_NODES]; rnp++) {
649                 spin_lock(&rnp->lock);  /* irqs already disabled. */
650                 rcu_preempt_check_blocked_tasks(rnp);
651                 rnp->qsmask = rnp->qsmaskinit;
652                 rnp->gpnum = rsp->gpnum;
653                 spin_unlock(&rnp->lock);        /* irqs already disabled. */
654         }
655
656         rsp->signaled = RCU_SIGNAL_INIT; /* force_quiescent_state now OK. */
657         spin_unlock_irqrestore(&rsp->onofflock, flags);
658 }
659
660 /*
661  * Advance this CPU's callbacks, but only if the current grace period
662  * has ended.  This may be called only from the CPU to whom the rdp
663  * belongs.
664  */
665 static void
666 rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp)
667 {
668         long completed_snap;
669         unsigned long flags;
670
671         local_irq_save(flags);
672         completed_snap = ACCESS_ONCE(rsp->completed);  /* outside of lock. */
673
674         /* Did another grace period end? */
675         if (rdp->completed != completed_snap) {
676
677                 /* Advance callbacks.  No harm if list empty. */
678                 rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL];
679                 rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL];
680                 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
681
682                 /* Remember that we saw this grace-period completion. */
683                 rdp->completed = completed_snap;
684         }
685         local_irq_restore(flags);
686 }
687
688 /*
689  * Clean up after the prior grace period and let rcu_start_gp() start up
690  * the next grace period if one is needed.  Note that the caller must
691  * hold rnp->lock, as required by rcu_start_gp(), which will release it.
692  */
693 static void cpu_quiet_msk_finish(struct rcu_state *rsp, unsigned long flags)
694         __releases(rcu_get_root(rsp)->lock)
695 {
696         WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
697         rsp->completed = rsp->gpnum;
698         rcu_process_gp_end(rsp, rsp->rda[smp_processor_id()]);
699         rcu_start_gp(rsp, flags);  /* releases root node's rnp->lock. */
700 }
701
702 /*
703  * Similar to cpu_quiet(), for which it is a helper function.  Allows
704  * a group of CPUs to be quieted at one go, though all the CPUs in the
705  * group must be represented by the same leaf rcu_node structure.
706  * That structure's lock must be held upon entry, and it is released
707  * before return.
708  */
709 static void
710 cpu_quiet_msk(unsigned long mask, struct rcu_state *rsp, struct rcu_node *rnp,
711               unsigned long flags)
712         __releases(rnp->lock)
713 {
714         struct rcu_node *rnp_c;
715
716         /* Walk up the rcu_node hierarchy. */
717         for (;;) {
718                 if (!(rnp->qsmask & mask)) {
719
720                         /* Our bit has already been cleared, so done. */
721                         spin_unlock_irqrestore(&rnp->lock, flags);
722                         return;
723                 }
724                 rnp->qsmask &= ~mask;
725                 if (rnp->qsmask != 0 || rcu_preempted_readers(rnp)) {
726
727                         /* Other bits still set at this level, so done. */
728                         spin_unlock_irqrestore(&rnp->lock, flags);
729                         return;
730                 }
731                 mask = rnp->grpmask;
732                 if (rnp->parent == NULL) {
733
734                         /* No more levels.  Exit loop holding root lock. */
735
736                         break;
737                 }
738                 spin_unlock_irqrestore(&rnp->lock, flags);
739                 rnp_c = rnp;
740                 rnp = rnp->parent;
741                 spin_lock_irqsave(&rnp->lock, flags);
742                 WARN_ON_ONCE(rnp_c->qsmask);
743         }
744
745         /*
746          * Get here if we are the last CPU to pass through a quiescent
747          * state for this grace period.  Invoke cpu_quiet_msk_finish()
748          * to clean up and start the next grace period if one is needed.
749          */
750         cpu_quiet_msk_finish(rsp, flags); /* releases rnp->lock. */
751 }
752
753 /*
754  * Record a quiescent state for the specified CPU, which must either be
755  * the current CPU.  The lastcomp argument is used to make sure we are
756  * still in the grace period of interest.  We don't want to end the current
757  * grace period based on quiescent states detected in an earlier grace
758  * period!
759  */
760 static void
761 cpu_quiet(int cpu, struct rcu_state *rsp, struct rcu_data *rdp, long lastcomp)
762 {
763         unsigned long flags;
764         unsigned long mask;
765         struct rcu_node *rnp;
766
767         rnp = rdp->mynode;
768         spin_lock_irqsave(&rnp->lock, flags);
769         if (lastcomp != ACCESS_ONCE(rsp->completed)) {
770
771                 /*
772                  * Someone beat us to it for this grace period, so leave.
773                  * The race with GP start is resolved by the fact that we
774                  * hold the leaf rcu_node lock, so that the per-CPU bits
775                  * cannot yet be initialized -- so we would simply find our
776                  * CPU's bit already cleared in cpu_quiet_msk() if this race
777                  * occurred.
778                  */
779                 rdp->passed_quiesc = 0; /* try again later! */
780                 spin_unlock_irqrestore(&rnp->lock, flags);
781                 return;
782         }
783         mask = rdp->grpmask;
784         if ((rnp->qsmask & mask) == 0) {
785                 spin_unlock_irqrestore(&rnp->lock, flags);
786         } else {
787                 rdp->qs_pending = 0;
788
789                 /*
790                  * This GP can't end until cpu checks in, so all of our
791                  * callbacks can be processed during the next GP.
792                  */
793                 rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
794
795                 cpu_quiet_msk(mask, rsp, rnp, flags); /* releases rnp->lock */
796         }
797 }
798
799 /*
800  * Check to see if there is a new grace period of which this CPU
801  * is not yet aware, and if so, set up local rcu_data state for it.
802  * Otherwise, see if this CPU has just passed through its first
803  * quiescent state for this grace period, and record that fact if so.
804  */
805 static void
806 rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp)
807 {
808         /* If there is now a new grace period, record and return. */
809         if (check_for_new_grace_period(rsp, rdp))
810                 return;
811
812         /*
813          * Does this CPU still need to do its part for current grace period?
814          * If no, return and let the other CPUs do their part as well.
815          */
816         if (!rdp->qs_pending)
817                 return;
818
819         /*
820          * Was there a quiescent state since the beginning of the grace
821          * period? If no, then exit and wait for the next call.
822          */
823         if (!rdp->passed_quiesc)
824                 return;
825
826         /* Tell RCU we are done (but cpu_quiet() will be the judge of that). */
827         cpu_quiet(rdp->cpu, rsp, rdp, rdp->passed_quiesc_completed);
828 }
829
830 #ifdef CONFIG_HOTPLUG_CPU
831
832 /*
833  * Remove the outgoing CPU from the bitmasks in the rcu_node hierarchy
834  * and move all callbacks from the outgoing CPU to the current one.
835  */
836 static void __rcu_offline_cpu(int cpu, struct rcu_state *rsp)
837 {
838         int i;
839         unsigned long flags;
840         long lastcomp;
841         unsigned long mask;
842         struct rcu_data *rdp = rsp->rda[cpu];
843         struct rcu_data *rdp_me;
844         struct rcu_node *rnp;
845
846         /* Exclude any attempts to start a new grace period. */
847         spin_lock_irqsave(&rsp->onofflock, flags);
848
849         /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */
850         rnp = rdp->mynode;      /* this is the outgoing CPU's rnp. */
851         mask = rdp->grpmask;    /* rnp->grplo is constant. */
852         do {
853                 spin_lock(&rnp->lock);          /* irqs already disabled. */
854                 rnp->qsmaskinit &= ~mask;
855                 if (rnp->qsmaskinit != 0) {
856                         spin_unlock(&rnp->lock); /* irqs remain disabled. */
857                         break;
858                 }
859                 rcu_preempt_offline_tasks(rsp, rnp, rdp);
860                 mask = rnp->grpmask;
861                 spin_unlock(&rnp->lock);        /* irqs remain disabled. */
862                 rnp = rnp->parent;
863         } while (rnp != NULL);
864         lastcomp = rsp->completed;
865
866         spin_unlock(&rsp->onofflock);           /* irqs remain disabled. */
867
868         /*
869          * Move callbacks from the outgoing CPU to the running CPU.
870          * Note that the outgoing CPU is now quiescent, so it is now
871          * (uncharacteristically) safe to access its rcu_data structure.
872          * Note also that we must carefully retain the order of the
873          * outgoing CPU's callbacks in order for rcu_barrier() to work
874          * correctly.  Finally, note that we start all the callbacks
875          * afresh, even those that have passed through a grace period
876          * and are therefore ready to invoke.  The theory is that hotplug
877          * events are rare, and that if they are frequent enough to
878          * indefinitely delay callbacks, you have far worse things to
879          * be worrying about.
880          */
881         rdp_me = rsp->rda[smp_processor_id()];
882         if (rdp->nxtlist != NULL) {
883                 *rdp_me->nxttail[RCU_NEXT_TAIL] = rdp->nxtlist;
884                 rdp_me->nxttail[RCU_NEXT_TAIL] = rdp->nxttail[RCU_NEXT_TAIL];
885                 rdp->nxtlist = NULL;
886                 for (i = 0; i < RCU_NEXT_SIZE; i++)
887                         rdp->nxttail[i] = &rdp->nxtlist;
888                 rdp_me->qlen += rdp->qlen;
889                 rdp->qlen = 0;
890         }
891         local_irq_restore(flags);
892 }
893
894 /*
895  * Remove the specified CPU from the RCU hierarchy and move any pending
896  * callbacks that it might have to the current CPU.  This code assumes
897  * that at least one CPU in the system will remain running at all times.
898  * Any attempt to offline -all- CPUs is likely to strand RCU callbacks.
899  */
900 static void rcu_offline_cpu(int cpu)
901 {
902         __rcu_offline_cpu(cpu, &rcu_sched_state);
903         __rcu_offline_cpu(cpu, &rcu_bh_state);
904         rcu_preempt_offline_cpu(cpu);
905 }
906
907 #else /* #ifdef CONFIG_HOTPLUG_CPU */
908
909 static void rcu_offline_cpu(int cpu)
910 {
911 }
912
913 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
914
915 /*
916  * Invoke any RCU callbacks that have made it to the end of their grace
917  * period.  Thottle as specified by rdp->blimit.
918  */
919 static void rcu_do_batch(struct rcu_data *rdp)
920 {
921         unsigned long flags;
922         struct rcu_head *next, *list, **tail;
923         int count;
924
925         /* If no callbacks are ready, just return.*/
926         if (!cpu_has_callbacks_ready_to_invoke(rdp))
927                 return;
928
929         /*
930          * Extract the list of ready callbacks, disabling to prevent
931          * races with call_rcu() from interrupt handlers.
932          */
933         local_irq_save(flags);
934         list = rdp->nxtlist;
935         rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL];
936         *rdp->nxttail[RCU_DONE_TAIL] = NULL;
937         tail = rdp->nxttail[RCU_DONE_TAIL];
938         for (count = RCU_NEXT_SIZE - 1; count >= 0; count--)
939                 if (rdp->nxttail[count] == rdp->nxttail[RCU_DONE_TAIL])
940                         rdp->nxttail[count] = &rdp->nxtlist;
941         local_irq_restore(flags);
942
943         /* Invoke callbacks. */
944         count = 0;
945         while (list) {
946                 next = list->next;
947                 prefetch(next);
948                 list->func(list);
949                 list = next;
950                 if (++count >= rdp->blimit)
951                         break;
952         }
953
954         local_irq_save(flags);
955
956         /* Update count, and requeue any remaining callbacks. */
957         rdp->qlen -= count;
958         if (list != NULL) {
959                 *tail = rdp->nxtlist;
960                 rdp->nxtlist = list;
961                 for (count = 0; count < RCU_NEXT_SIZE; count++)
962                         if (&rdp->nxtlist == rdp->nxttail[count])
963                                 rdp->nxttail[count] = tail;
964                         else
965                                 break;
966         }
967
968         /* Reinstate batch limit if we have worked down the excess. */
969         if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark)
970                 rdp->blimit = blimit;
971
972         local_irq_restore(flags);
973
974         /* Re-raise the RCU softirq if there are callbacks remaining. */
975         if (cpu_has_callbacks_ready_to_invoke(rdp))
976                 raise_softirq(RCU_SOFTIRQ);
977 }
978
979 /*
980  * Check to see if this CPU is in a non-context-switch quiescent state
981  * (user mode or idle loop for rcu, non-softirq execution for rcu_bh).
982  * Also schedule the RCU softirq handler.
983  *
984  * This function must be called with hardirqs disabled.  It is normally
985  * invoked from the scheduling-clock interrupt.  If rcu_pending returns
986  * false, there is no point in invoking rcu_check_callbacks().
987  */
988 void rcu_check_callbacks(int cpu, int user)
989 {
990         if (!rcu_pending(cpu))
991                 return; /* if nothing for RCU to do. */
992         if (user ||
993             (idle_cpu(cpu) && rcu_scheduler_active &&
994              !in_softirq() && hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
995
996                 /*
997                  * Get here if this CPU took its interrupt from user
998                  * mode or from the idle loop, and if this is not a
999                  * nested interrupt.  In this case, the CPU is in
1000                  * a quiescent state, so note it.
1001                  *
1002                  * No memory barrier is required here because both
1003                  * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local
1004                  * variables that other CPUs neither access nor modify,
1005                  * at least not while the corresponding CPU is online.
1006                  */
1007
1008                 rcu_sched_qs(cpu);
1009                 rcu_bh_qs(cpu);
1010
1011         } else if (!in_softirq()) {
1012
1013                 /*
1014                  * Get here if this CPU did not take its interrupt from
1015                  * softirq, in other words, if it is not interrupting
1016                  * a rcu_bh read-side critical section.  This is an _bh
1017                  * critical section, so note it.
1018                  */
1019
1020                 rcu_bh_qs(cpu);
1021         }
1022         rcu_preempt_check_callbacks(cpu);
1023         raise_softirq(RCU_SOFTIRQ);
1024 }
1025
1026 #ifdef CONFIG_SMP
1027
1028 /*
1029  * Scan the leaf rcu_node structures, processing dyntick state for any that
1030  * have not yet encountered a quiescent state, using the function specified.
1031  * Returns 1 if the current grace period ends while scanning (possibly
1032  * because we made it end).
1033  */
1034 static int rcu_process_dyntick(struct rcu_state *rsp, long lastcomp,
1035                                int (*f)(struct rcu_data *))
1036 {
1037         unsigned long bit;
1038         int cpu;
1039         unsigned long flags;
1040         unsigned long mask;
1041         struct rcu_node *rnp_cur = rsp->level[NUM_RCU_LVLS - 1];
1042         struct rcu_node *rnp_end = &rsp->node[NUM_RCU_NODES];
1043
1044         for (; rnp_cur < rnp_end; rnp_cur++) {
1045                 mask = 0;
1046                 spin_lock_irqsave(&rnp_cur->lock, flags);
1047                 if (rsp->completed != lastcomp) {
1048                         spin_unlock_irqrestore(&rnp_cur->lock, flags);
1049                         return 1;
1050                 }
1051                 if (rnp_cur->qsmask == 0) {
1052                         spin_unlock_irqrestore(&rnp_cur->lock, flags);
1053                         continue;
1054                 }
1055                 cpu = rnp_cur->grplo;
1056                 bit = 1;
1057                 for (; cpu <= rnp_cur->grphi; cpu++, bit <<= 1) {
1058                         if ((rnp_cur->qsmask & bit) != 0 && f(rsp->rda[cpu]))
1059                                 mask |= bit;
1060                 }
1061                 if (mask != 0 && rsp->completed == lastcomp) {
1062
1063                         /* cpu_quiet_msk() releases rnp_cur->lock. */
1064                         cpu_quiet_msk(mask, rsp, rnp_cur, flags);
1065                         continue;
1066                 }
1067                 spin_unlock_irqrestore(&rnp_cur->lock, flags);
1068         }
1069         return 0;
1070 }
1071
1072 /*
1073  * Force quiescent states on reluctant CPUs, and also detect which
1074  * CPUs are in dyntick-idle mode.
1075  */
1076 static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
1077 {
1078         unsigned long flags;
1079         long lastcomp;
1080         struct rcu_node *rnp = rcu_get_root(rsp);
1081         u8 signaled;
1082
1083         if (!rcu_gp_in_progress(rsp))
1084                 return;  /* No grace period in progress, nothing to force. */
1085         if (!spin_trylock_irqsave(&rsp->fqslock, flags)) {
1086                 rsp->n_force_qs_lh++; /* Inexact, can lose counts.  Tough! */
1087                 return; /* Someone else is already on the job. */
1088         }
1089         if (relaxed &&
1090             (long)(rsp->jiffies_force_qs - jiffies) >= 0)
1091                 goto unlock_ret; /* no emergency and done recently. */
1092         rsp->n_force_qs++;
1093         spin_lock(&rnp->lock);
1094         lastcomp = rsp->completed;
1095         signaled = rsp->signaled;
1096         rsp->jiffies_force_qs = jiffies + RCU_JIFFIES_TILL_FORCE_QS;
1097         if (lastcomp == rsp->gpnum) {
1098                 rsp->n_force_qs_ngp++;
1099                 spin_unlock(&rnp->lock);
1100                 goto unlock_ret;  /* no GP in progress, time updated. */
1101         }
1102         spin_unlock(&rnp->lock);
1103         switch (signaled) {
1104         case RCU_GP_INIT:
1105
1106                 break; /* grace period still initializing, ignore. */
1107
1108         case RCU_SAVE_DYNTICK:
1109
1110                 if (RCU_SIGNAL_INIT != RCU_SAVE_DYNTICK)
1111                         break; /* So gcc recognizes the dead code. */
1112
1113                 /* Record dyntick-idle state. */
1114                 if (rcu_process_dyntick(rsp, lastcomp,
1115                                         dyntick_save_progress_counter))
1116                         goto unlock_ret;
1117
1118                 /* Update state, record completion counter. */
1119                 spin_lock(&rnp->lock);
1120                 if (lastcomp == rsp->completed) {
1121                         rsp->signaled = RCU_FORCE_QS;
1122                         dyntick_record_completed(rsp, lastcomp);
1123                 }
1124                 spin_unlock(&rnp->lock);
1125                 break;
1126
1127         case RCU_FORCE_QS:
1128
1129                 /* Check dyntick-idle state, send IPI to laggarts. */
1130                 if (rcu_process_dyntick(rsp, dyntick_recall_completed(rsp),
1131                                         rcu_implicit_dynticks_qs))
1132                         goto unlock_ret;
1133
1134                 /* Leave state in case more forcing is required. */
1135
1136                 break;
1137         }
1138 unlock_ret:
1139         spin_unlock_irqrestore(&rsp->fqslock, flags);
1140 }
1141
1142 #else /* #ifdef CONFIG_SMP */
1143
1144 static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
1145 {
1146         set_need_resched();
1147 }
1148
1149 #endif /* #else #ifdef CONFIG_SMP */
1150
1151 /*
1152  * This does the RCU processing work from softirq context for the
1153  * specified rcu_state and rcu_data structures.  This may be called
1154  * only from the CPU to whom the rdp belongs.
1155  */
1156 static void
1157 __rcu_process_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
1158 {
1159         unsigned long flags;
1160
1161         WARN_ON_ONCE(rdp->beenonline == 0);
1162
1163         /*
1164          * If an RCU GP has gone long enough, go check for dyntick
1165          * idle CPUs and, if needed, send resched IPIs.
1166          */
1167         if ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0)
1168                 force_quiescent_state(rsp, 1);
1169
1170         /*
1171          * Advance callbacks in response to end of earlier grace
1172          * period that some other CPU ended.
1173          */
1174         rcu_process_gp_end(rsp, rdp);
1175
1176         /* Update RCU state based on any recent quiescent states. */
1177         rcu_check_quiescent_state(rsp, rdp);
1178
1179         /* Does this CPU require a not-yet-started grace period? */
1180         if (cpu_needs_another_gp(rsp, rdp)) {
1181                 spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags);
1182                 rcu_start_gp(rsp, flags);  /* releases above lock */
1183         }
1184
1185         /* If there are callbacks ready, invoke them. */
1186         rcu_do_batch(rdp);
1187 }
1188
1189 /*
1190  * Do softirq processing for the current CPU.
1191  */
1192 static void rcu_process_callbacks(struct softirq_action *unused)
1193 {
1194         /*
1195          * Memory references from any prior RCU read-side critical sections
1196          * executed by the interrupted code must be seen before any RCU
1197          * grace-period manipulations below.
1198          */
1199         smp_mb(); /* See above block comment. */
1200
1201         __rcu_process_callbacks(&rcu_sched_state,
1202                                 &__get_cpu_var(rcu_sched_data));
1203         __rcu_process_callbacks(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
1204         rcu_preempt_process_callbacks();
1205
1206         /*
1207          * Memory references from any later RCU read-side critical sections
1208          * executed by the interrupted code must be seen after any RCU
1209          * grace-period manipulations above.
1210          */
1211         smp_mb(); /* See above block comment. */
1212 }
1213
1214 static void
1215 __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
1216            struct rcu_state *rsp)
1217 {
1218         unsigned long flags;
1219         struct rcu_data *rdp;
1220
1221         head->func = func;
1222         head->next = NULL;
1223
1224         smp_mb(); /* Ensure RCU update seen before callback registry. */
1225
1226         /*
1227          * Opportunistically note grace-period endings and beginnings.
1228          * Note that we might see a beginning right after we see an
1229          * end, but never vice versa, since this CPU has to pass through
1230          * a quiescent state betweentimes.
1231          */
1232         local_irq_save(flags);
1233         rdp = rsp->rda[smp_processor_id()];
1234         rcu_process_gp_end(rsp, rdp);
1235         check_for_new_grace_period(rsp, rdp);
1236
1237         /* Add the callback to our list. */
1238         *rdp->nxttail[RCU_NEXT_TAIL] = head;
1239         rdp->nxttail[RCU_NEXT_TAIL] = &head->next;
1240
1241         /* Start a new grace period if one not already started. */
1242         if (!rcu_gp_in_progress(rsp)) {
1243                 unsigned long nestflag;
1244                 struct rcu_node *rnp_root = rcu_get_root(rsp);
1245
1246                 spin_lock_irqsave(&rnp_root->lock, nestflag);
1247                 rcu_start_gp(rsp, nestflag);  /* releases rnp_root->lock. */
1248         }
1249
1250         /* Force the grace period if too many callbacks or too long waiting. */
1251         if (unlikely(++rdp->qlen > qhimark)) {
1252                 rdp->blimit = LONG_MAX;
1253                 force_quiescent_state(rsp, 0);
1254         } else if ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0)
1255                 force_quiescent_state(rsp, 1);
1256         local_irq_restore(flags);
1257 }
1258
1259 /*
1260  * Queue an RCU-sched callback for invocation after a grace period.
1261  */
1262 void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
1263 {
1264         __call_rcu(head, func, &rcu_sched_state);
1265 }
1266 EXPORT_SYMBOL_GPL(call_rcu_sched);
1267
1268 /*
1269  * Queue an RCU for invocation after a quicker grace period.
1270  */
1271 void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
1272 {
1273         __call_rcu(head, func, &rcu_bh_state);
1274 }
1275 EXPORT_SYMBOL_GPL(call_rcu_bh);
1276
1277 /*
1278  * Check to see if there is any immediate RCU-related work to be done
1279  * by the current CPU, for the specified type of RCU, returning 1 if so.
1280  * The checks are in order of increasing expense: checks that can be
1281  * carried out against CPU-local state are performed first.  However,
1282  * we must check for CPU stalls first, else we might not get a chance.
1283  */
1284 static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
1285 {
1286         rdp->n_rcu_pending++;
1287
1288         /* Check for CPU stalls, if enabled. */
1289         check_cpu_stall(rsp, rdp);
1290
1291         /* Is the RCU core waiting for a quiescent state from this CPU? */
1292         if (rdp->qs_pending) {
1293                 rdp->n_rp_qs_pending++;
1294                 return 1;
1295         }
1296
1297         /* Does this CPU have callbacks ready to invoke? */
1298         if (cpu_has_callbacks_ready_to_invoke(rdp)) {
1299                 rdp->n_rp_cb_ready++;
1300                 return 1;
1301         }
1302
1303         /* Has RCU gone idle with this CPU needing another grace period? */
1304         if (cpu_needs_another_gp(rsp, rdp)) {
1305                 rdp->n_rp_cpu_needs_gp++;
1306                 return 1;
1307         }
1308
1309         /* Has another RCU grace period completed?  */
1310         if (ACCESS_ONCE(rsp->completed) != rdp->completed) { /* outside lock */
1311                 rdp->n_rp_gp_completed++;
1312                 return 1;
1313         }
1314
1315         /* Has a new RCU grace period started? */
1316         if (ACCESS_ONCE(rsp->gpnum) != rdp->gpnum) { /* outside lock */
1317                 rdp->n_rp_gp_started++;
1318                 return 1;
1319         }
1320
1321         /* Has an RCU GP gone long enough to send resched IPIs &c? */
1322         if (rcu_gp_in_progress(rsp) &&
1323             ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0)) {
1324                 rdp->n_rp_need_fqs++;
1325                 return 1;
1326         }
1327
1328         /* nothing to do */
1329         rdp->n_rp_need_nothing++;
1330         return 0;
1331 }
1332
1333 /*
1334  * Check to see if there is any immediate RCU-related work to be done
1335  * by the current CPU, returning 1 if so.  This function is part of the
1336  * RCU implementation; it is -not- an exported member of the RCU API.
1337  */
1338 static int rcu_pending(int cpu)
1339 {
1340         return __rcu_pending(&rcu_sched_state, &per_cpu(rcu_sched_data, cpu)) ||
1341                __rcu_pending(&rcu_bh_state, &per_cpu(rcu_bh_data, cpu)) ||
1342                rcu_preempt_pending(cpu);
1343 }
1344
1345 /*
1346  * Check to see if any future RCU-related work will need to be done
1347  * by the current CPU, even if none need be done immediately, returning
1348  * 1 if so.  This function is part of the RCU implementation; it is -not-
1349  * an exported member of the RCU API.
1350  */
1351 int rcu_needs_cpu(int cpu)
1352 {
1353         /* RCU callbacks either ready or pending? */
1354         return per_cpu(rcu_sched_data, cpu).nxtlist ||
1355                per_cpu(rcu_bh_data, cpu).nxtlist ||
1356                rcu_preempt_needs_cpu(cpu);
1357 }
1358
1359 /*
1360  * Do boot-time initialization of a CPU's per-CPU RCU data.
1361  */
1362 static void __init
1363 rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp)
1364 {
1365         unsigned long flags;
1366         int i;
1367         struct rcu_data *rdp = rsp->rda[cpu];
1368         struct rcu_node *rnp = rcu_get_root(rsp);
1369
1370         /* Set up local state, ensuring consistent view of global state. */
1371         spin_lock_irqsave(&rnp->lock, flags);
1372         rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo);
1373         rdp->nxtlist = NULL;
1374         for (i = 0; i < RCU_NEXT_SIZE; i++)
1375                 rdp->nxttail[i] = &rdp->nxtlist;
1376         rdp->qlen = 0;
1377 #ifdef CONFIG_NO_HZ
1378         rdp->dynticks = &per_cpu(rcu_dynticks, cpu);
1379 #endif /* #ifdef CONFIG_NO_HZ */
1380         rdp->cpu = cpu;
1381         spin_unlock_irqrestore(&rnp->lock, flags);
1382 }
1383
1384 /*
1385  * Initialize a CPU's per-CPU RCU data.  Note that only one online or
1386  * offline event can be happening at a given time.  Note also that we
1387  * can accept some slop in the rsp->completed access due to the fact
1388  * that this CPU cannot possibly have any RCU callbacks in flight yet.
1389  */
1390 static void __cpuinit
1391 rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptable)
1392 {
1393         unsigned long flags;
1394         long lastcomp;
1395         unsigned long mask;
1396         struct rcu_data *rdp = rsp->rda[cpu];
1397         struct rcu_node *rnp = rcu_get_root(rsp);
1398
1399         /* Set up local state, ensuring consistent view of global state. */
1400         spin_lock_irqsave(&rnp->lock, flags);
1401         lastcomp = rsp->completed;
1402         rdp->completed = lastcomp;
1403         rdp->gpnum = lastcomp;
1404         rdp->passed_quiesc = 0;  /* We could be racing with new GP, */
1405         rdp->qs_pending = 1;     /*  so set up to respond to current GP. */
1406         rdp->beenonline = 1;     /* We have now been online. */
1407         rdp->preemptable = preemptable;
1408         rdp->passed_quiesc_completed = lastcomp - 1;
1409         rdp->blimit = blimit;
1410         spin_unlock(&rnp->lock);                /* irqs remain disabled. */
1411
1412         /*
1413          * A new grace period might start here.  If so, we won't be part
1414          * of it, but that is OK, as we are currently in a quiescent state.
1415          */
1416
1417         /* Exclude any attempts to start a new GP on large systems. */
1418         spin_lock(&rsp->onofflock);             /* irqs already disabled. */
1419
1420         /* Add CPU to rcu_node bitmasks. */
1421         rnp = rdp->mynode;
1422         mask = rdp->grpmask;
1423         do {
1424                 /* Exclude any attempts to start a new GP on small systems. */
1425                 spin_lock(&rnp->lock);  /* irqs already disabled. */
1426                 rnp->qsmaskinit |= mask;
1427                 mask = rnp->grpmask;
1428                 spin_unlock(&rnp->lock); /* irqs already disabled. */
1429                 rnp = rnp->parent;
1430         } while (rnp != NULL && !(rnp->qsmaskinit & mask));
1431
1432         spin_unlock_irqrestore(&rsp->onofflock, flags);
1433 }
1434
1435 static void __cpuinit rcu_online_cpu(int cpu)
1436 {
1437         rcu_init_percpu_data(cpu, &rcu_sched_state, 0);
1438         rcu_init_percpu_data(cpu, &rcu_bh_state, 0);
1439         rcu_preempt_init_percpu_data(cpu);
1440 }
1441
1442 /*
1443  * Handle CPU online/offline notification events.
1444  */
1445 int __cpuinit rcu_cpu_notify(struct notifier_block *self,
1446                              unsigned long action, void *hcpu)
1447 {
1448         long cpu = (long)hcpu;
1449
1450         switch (action) {
1451         case CPU_UP_PREPARE:
1452         case CPU_UP_PREPARE_FROZEN:
1453                 rcu_online_cpu(cpu);
1454                 break;
1455         case CPU_DEAD:
1456         case CPU_DEAD_FROZEN:
1457         case CPU_UP_CANCELED:
1458         case CPU_UP_CANCELED_FROZEN:
1459                 rcu_offline_cpu(cpu);
1460                 break;
1461         default:
1462                 break;
1463         }
1464         return NOTIFY_OK;
1465 }
1466
1467 /*
1468  * Compute the per-level fanout, either using the exact fanout specified
1469  * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT.
1470  */
1471 #ifdef CONFIG_RCU_FANOUT_EXACT
1472 static void __init rcu_init_levelspread(struct rcu_state *rsp)
1473 {
1474         int i;
1475
1476         for (i = NUM_RCU_LVLS - 1; i >= 0; i--)
1477                 rsp->levelspread[i] = CONFIG_RCU_FANOUT;
1478 }
1479 #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */
1480 static void __init rcu_init_levelspread(struct rcu_state *rsp)
1481 {
1482         int ccur;
1483         int cprv;
1484         int i;
1485
1486         cprv = NR_CPUS;
1487         for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
1488                 ccur = rsp->levelcnt[i];
1489                 rsp->levelspread[i] = (cprv + ccur - 1) / ccur;
1490                 cprv = ccur;
1491         }
1492 }
1493 #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */
1494
1495 /*
1496  * Helper function for rcu_init() that initializes one rcu_state structure.
1497  */
1498 static void __init rcu_init_one(struct rcu_state *rsp)
1499 {
1500         int cpustride = 1;
1501         int i;
1502         int j;
1503         struct rcu_node *rnp;
1504
1505         /* Initialize the level-tracking arrays. */
1506
1507         for (i = 1; i < NUM_RCU_LVLS; i++)
1508                 rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1];
1509         rcu_init_levelspread(rsp);
1510
1511         /* Initialize the elements themselves, starting from the leaves. */
1512
1513         for (i = NUM_RCU_LVLS - 1; i >= 0; i--) {
1514                 cpustride *= rsp->levelspread[i];
1515                 rnp = rsp->level[i];
1516                 for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) {
1517                         spin_lock_init(&rnp->lock);
1518                         rnp->gpnum = 0;
1519                         rnp->qsmask = 0;
1520                         rnp->qsmaskinit = 0;
1521                         rnp->grplo = j * cpustride;
1522                         rnp->grphi = (j + 1) * cpustride - 1;
1523                         if (rnp->grphi >= NR_CPUS)
1524                                 rnp->grphi = NR_CPUS - 1;
1525                         if (i == 0) {
1526                                 rnp->grpnum = 0;
1527                                 rnp->grpmask = 0;
1528                                 rnp->parent = NULL;
1529                         } else {
1530                                 rnp->grpnum = j % rsp->levelspread[i - 1];
1531                                 rnp->grpmask = 1UL << rnp->grpnum;
1532                                 rnp->parent = rsp->level[i - 1] +
1533                                               j / rsp->levelspread[i - 1];
1534                         }
1535                         rnp->level = i;
1536                         INIT_LIST_HEAD(&rnp->blocked_tasks[0]);
1537                         INIT_LIST_HEAD(&rnp->blocked_tasks[1]);
1538                 }
1539         }
1540 }
1541
1542 /*
1543  * Helper macro for __rcu_init() and __rcu_init_preempt().  To be used
1544  * nowhere else!  Assigns leaf node pointers into each CPU's rcu_data
1545  * structure.
1546  */
1547 #define RCU_INIT_FLAVOR(rsp, rcu_data) \
1548 do { \
1549         rcu_init_one(rsp); \
1550         rnp = (rsp)->level[NUM_RCU_LVLS - 1]; \
1551         j = 0; \
1552         for_each_possible_cpu(i) { \
1553                 if (i > rnp[j].grphi) \
1554                         j++; \
1555                 per_cpu(rcu_data, i).mynode = &rnp[j]; \
1556                 (rsp)->rda[i] = &per_cpu(rcu_data, i); \
1557                 rcu_boot_init_percpu_data(i, rsp); \
1558         } \
1559 } while (0)
1560
1561 void __init __rcu_init(void)
1562 {
1563         int i;                  /* All used by RCU_INIT_FLAVOR(). */
1564         int j;
1565         struct rcu_node *rnp;
1566
1567         rcu_bootup_announce();
1568 #ifdef CONFIG_RCU_CPU_STALL_DETECTOR
1569         printk(KERN_INFO "RCU-based detection of stalled CPUs is enabled.\n");
1570 #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
1571         RCU_INIT_FLAVOR(&rcu_sched_state, rcu_sched_data);
1572         RCU_INIT_FLAVOR(&rcu_bh_state, rcu_bh_data);
1573         __rcu_init_preempt();
1574         open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
1575 }
1576
1577 #include "rcutree_plugin.h"
1578
1579 module_param(blimit, int, 0);
1580 module_param(qhimark, int, 0);
1581 module_param(qlowmark, int, 0);