rcu: classic RCU locking and memory-barrier cleanups
[safe/jmp/linux-2.6] / kernel / rcuclassic.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, 2001
19  *
20  * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21  *          Manfred Spraul <manfred@colorfullife.com>
22  *
23  * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
24  * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
25  * Papers:
26  * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
27  * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
28  *
29  * For detailed explanation of Read-Copy Update mechanism see -
30  *              Documentation/RCU
31  *
32  */
33 #include <linux/types.h>
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/spinlock.h>
37 #include <linux/smp.h>
38 #include <linux/rcupdate.h>
39 #include <linux/interrupt.h>
40 #include <linux/sched.h>
41 #include <asm/atomic.h>
42 #include <linux/bitops.h>
43 #include <linux/module.h>
44 #include <linux/completion.h>
45 #include <linux/moduleparam.h>
46 #include <linux/percpu.h>
47 #include <linux/notifier.h>
48 #include <linux/cpu.h>
49 #include <linux/mutex.h>
50 #include <linux/time.h>
51
52 #ifdef CONFIG_DEBUG_LOCK_ALLOC
53 static struct lock_class_key rcu_lock_key;
54 struct lockdep_map rcu_lock_map =
55         STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
56 EXPORT_SYMBOL_GPL(rcu_lock_map);
57 #endif
58
59
60 /* Definition for rcupdate control block. */
61 static struct rcu_ctrlblk rcu_ctrlblk = {
62         .cur = -300,
63         .completed = -300,
64         .pending = -300,
65         .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock),
66         .cpumask = CPU_MASK_NONE,
67 };
68 static struct rcu_ctrlblk rcu_bh_ctrlblk = {
69         .cur = -300,
70         .completed = -300,
71         .pending = -300,
72         .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock),
73         .cpumask = CPU_MASK_NONE,
74 };
75
76 DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
77 DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
78
79 static int blimit = 10;
80 static int qhimark = 10000;
81 static int qlowmark = 100;
82
83 #ifdef CONFIG_SMP
84 static void force_quiescent_state(struct rcu_data *rdp,
85                         struct rcu_ctrlblk *rcp)
86 {
87         int cpu;
88         cpumask_t cpumask;
89         set_need_resched();
90         spin_lock(&rcp->lock);
91         if (unlikely(!rcp->signaled)) {
92                 rcp->signaled = 1;
93                 /*
94                  * Don't send IPI to itself. With irqs disabled,
95                  * rdp->cpu is the current cpu.
96                  *
97                  * cpu_online_map is updated by the _cpu_down()
98                  * using __stop_machine(). Since we're in irqs disabled
99                  * section, __stop_machine() is not exectuting, hence
100                  * the cpu_online_map is stable.
101                  *
102                  * However,  a cpu might have been offlined _just_ before
103                  * we disabled irqs while entering here.
104                  * And rcu subsystem might not yet have handled the CPU_DEAD
105                  * notification, leading to the offlined cpu's bit
106                  * being set in the rcp->cpumask.
107                  *
108                  * Hence cpumask = (rcp->cpumask & cpu_online_map) to prevent
109                  * sending smp_reschedule() to an offlined CPU.
110                  */
111                 cpus_and(cpumask, rcp->cpumask, cpu_online_map);
112                 cpu_clear(rdp->cpu, cpumask);
113                 for_each_cpu_mask_nr(cpu, cpumask)
114                         smp_send_reschedule(cpu);
115         }
116         spin_unlock(&rcp->lock);
117 }
118 #else
119 static inline void force_quiescent_state(struct rcu_data *rdp,
120                         struct rcu_ctrlblk *rcp)
121 {
122         set_need_resched();
123 }
124 #endif
125
126 static void __call_rcu(struct rcu_head *head, struct rcu_ctrlblk *rcp,
127                 struct rcu_data *rdp)
128 {
129         long batch;
130
131         head->next = NULL;
132         smp_mb(); /* Read of rcu->cur must happen after any change by caller. */
133
134         /*
135          * Determine the batch number of this callback.
136          *
137          * Using ACCESS_ONCE to avoid the following error when gcc eliminates
138          * local variable "batch" and emits codes like this:
139          *      1) rdp->batch = rcp->cur + 1 # gets old value
140          *      ......
141          *      2)rcu_batch_after(rcp->cur + 1, rdp->batch) # gets new value
142          * then [*nxttail[0], *nxttail[1]) may contain callbacks
143          * that batch# = rdp->batch, see the comment of struct rcu_data.
144          */
145         batch = ACCESS_ONCE(rcp->cur) + 1;
146
147         if (rdp->nxtlist && rcu_batch_after(batch, rdp->batch)) {
148                 /* process callbacks */
149                 rdp->nxttail[0] = rdp->nxttail[1];
150                 rdp->nxttail[1] = rdp->nxttail[2];
151                 if (rcu_batch_after(batch - 1, rdp->batch))
152                         rdp->nxttail[0] = rdp->nxttail[2];
153         }
154
155         rdp->batch = batch;
156         *rdp->nxttail[2] = head;
157         rdp->nxttail[2] = &head->next;
158
159         if (unlikely(++rdp->qlen > qhimark)) {
160                 rdp->blimit = INT_MAX;
161                 force_quiescent_state(rdp, &rcu_ctrlblk);
162         }
163 }
164
165 /**
166  * call_rcu - Queue an RCU callback for invocation after a grace period.
167  * @head: structure to be used for queueing the RCU updates.
168  * @func: actual update function to be invoked after the grace period
169  *
170  * The update function will be invoked some time after a full grace
171  * period elapses, in other words after all currently executing RCU
172  * read-side critical sections have completed.  RCU read-side critical
173  * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
174  * and may be nested.
175  */
176 void call_rcu(struct rcu_head *head,
177                                 void (*func)(struct rcu_head *rcu))
178 {
179         unsigned long flags;
180
181         head->func = func;
182         local_irq_save(flags);
183         __call_rcu(head, &rcu_ctrlblk, &__get_cpu_var(rcu_data));
184         local_irq_restore(flags);
185 }
186 EXPORT_SYMBOL_GPL(call_rcu);
187
188 /**
189  * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
190  * @head: structure to be used for queueing the RCU updates.
191  * @func: actual update function to be invoked after the grace period
192  *
193  * The update function will be invoked some time after a full grace
194  * period elapses, in other words after all currently executing RCU
195  * read-side critical sections have completed. call_rcu_bh() assumes
196  * that the read-side critical sections end on completion of a softirq
197  * handler. This means that read-side critical sections in process
198  * context must not be interrupted by softirqs. This interface is to be
199  * used when most of the read-side critical sections are in softirq context.
200  * RCU read-side critical sections are delimited by rcu_read_lock() and
201  * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
202  * and rcu_read_unlock_bh(), if in process context. These may be nested.
203  */
204 void call_rcu_bh(struct rcu_head *head,
205                                 void (*func)(struct rcu_head *rcu))
206 {
207         unsigned long flags;
208
209         head->func = func;
210         local_irq_save(flags);
211         __call_rcu(head, &rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
212         local_irq_restore(flags);
213 }
214 EXPORT_SYMBOL_GPL(call_rcu_bh);
215
216 /*
217  * Return the number of RCU batches processed thus far.  Useful
218  * for debug and statistics.
219  */
220 long rcu_batches_completed(void)
221 {
222         return rcu_ctrlblk.completed;
223 }
224 EXPORT_SYMBOL_GPL(rcu_batches_completed);
225
226 /*
227  * Return the number of RCU batches processed thus far.  Useful
228  * for debug and statistics.
229  */
230 long rcu_batches_completed_bh(void)
231 {
232         return rcu_bh_ctrlblk.completed;
233 }
234 EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
235
236 /* Raises the softirq for processing rcu_callbacks. */
237 static inline void raise_rcu_softirq(void)
238 {
239         raise_softirq(RCU_SOFTIRQ);
240 }
241
242 /*
243  * Invoke the completed RCU callbacks. They are expected to be in
244  * a per-cpu list.
245  */
246 static void rcu_do_batch(struct rcu_data *rdp)
247 {
248         struct rcu_head *next, *list;
249         int count = 0;
250
251         list = rdp->donelist;
252         while (list) {
253                 next = list->next;
254                 prefetch(next);
255                 list->func(list);
256                 list = next;
257                 if (++count >= rdp->blimit)
258                         break;
259         }
260         rdp->donelist = list;
261
262         local_irq_disable();
263         rdp->qlen -= count;
264         local_irq_enable();
265         if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark)
266                 rdp->blimit = blimit;
267
268         if (!rdp->donelist)
269                 rdp->donetail = &rdp->donelist;
270         else
271                 raise_rcu_softirq();
272 }
273
274 /*
275  * Grace period handling:
276  * The grace period handling consists out of two steps:
277  * - A new grace period is started.
278  *   This is done by rcu_start_batch. The start is not broadcasted to
279  *   all cpus, they must pick this up by comparing rcp->cur with
280  *   rdp->quiescbatch. All cpus are recorded  in the
281  *   rcu_ctrlblk.cpumask bitmap.
282  * - All cpus must go through a quiescent state.
283  *   Since the start of the grace period is not broadcasted, at least two
284  *   calls to rcu_check_quiescent_state are required:
285  *   The first call just notices that a new grace period is running. The
286  *   following calls check if there was a quiescent state since the beginning
287  *   of the grace period. If so, it updates rcu_ctrlblk.cpumask. If
288  *   the bitmap is empty, then the grace period is completed.
289  *   rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
290  *   period (if necessary).
291  */
292
293 #ifdef CONFIG_DEBUG_RCU_STALL
294
295 static inline void record_gp_check_time(struct rcu_ctrlblk *rcp)
296 {
297         rcp->gp_check = get_seconds() + 3;
298 }
299
300 static void print_other_cpu_stall(struct rcu_ctrlblk *rcp)
301 {
302         int cpu;
303         long delta;
304
305         /* Only let one CPU complain about others per time interval. */
306
307         spin_lock(&rcp->lock);
308         delta = get_seconds() - rcp->gp_check;
309         if (delta < 2L || cpus_empty(rcp->cpumask)) {
310                 spin_unlock(&rcp->lock);
311                 return;
312         }
313         rcp->gp_check = get_seconds() + 30;
314         spin_unlock(&rcp->lock);
315
316         /* OK, time to rat on our buddy... */
317
318         printk(KERN_ERR "RCU detected CPU stalls:");
319         for_each_cpu_mask(cpu, rcp->cpumask)
320                 printk(" %d", cpu);
321         printk(" (detected by %d, t=%lu/%lu)\n",
322                smp_processor_id(), get_seconds(), rcp->gp_check);
323 }
324
325 static void print_cpu_stall(struct rcu_ctrlblk *rcp)
326 {
327         printk(KERN_ERR "RCU detected CPU %d stall (t=%lu/%lu)\n",
328                         smp_processor_id(), get_seconds(), rcp->gp_check);
329         dump_stack();
330         spin_lock(&rcp->lock);
331         if ((long)(get_seconds() - rcp->gp_check) >= 0L)
332                 rcp->gp_check = get_seconds() + 30;
333         spin_unlock(&rcp->lock);
334 }
335
336 static void check_cpu_stall(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
337 {
338         long delta;
339
340         delta = get_seconds() - rcp->gp_check;
341         if (cpu_isset(smp_processor_id(), rcp->cpumask) && delta >= 0L) {
342
343                 /* We haven't checked in, so go dump stack. */
344
345                 print_cpu_stall(rcp);
346
347         } else {
348                 if (!cpus_empty(rcp->cpumask) && delta >= 2L) {
349                         /* They had two seconds to dump stack, so complain. */
350                         print_other_cpu_stall(rcp);
351                 }
352         }
353 }
354
355 #else /* #ifdef CONFIG_DEBUG_RCU_STALL */
356
357 static inline void record_gp_check_time(struct rcu_ctrlblk *rcp)
358 {
359 }
360
361 static inline void
362 check_cpu_stall(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
363 {
364 }
365
366 #endif /* #else #ifdef CONFIG_DEBUG_RCU_STALL */
367
368 /*
369  * Register a new batch of callbacks, and start it up if there is currently no
370  * active batch and the batch to be registered has not already occurred.
371  * Caller must hold rcu_ctrlblk.lock.
372  */
373 static void rcu_start_batch(struct rcu_ctrlblk *rcp)
374 {
375         if (rcp->cur != rcp->pending &&
376                         rcp->completed == rcp->cur) {
377                 rcp->cur++;
378                 record_gp_check_time(rcp);
379
380                 /*
381                  * Accessing nohz_cpu_mask before incrementing rcp->cur needs a
382                  * Barrier  Otherwise it can cause tickless idle CPUs to be
383                  * included in rcp->cpumask, which will extend graceperiods
384                  * unnecessarily.
385                  */
386                 smp_mb();
387                 cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask);
388
389                 rcp->signaled = 0;
390         }
391 }
392
393 /*
394  * cpu went through a quiescent state since the beginning of the grace period.
395  * Clear it from the cpu mask and complete the grace period if it was the last
396  * cpu. Start another grace period if someone has further entries pending
397  */
398 static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
399 {
400         cpu_clear(cpu, rcp->cpumask);
401         if (cpus_empty(rcp->cpumask)) {
402                 /* batch completed ! */
403                 rcp->completed = rcp->cur;
404                 rcu_start_batch(rcp);
405         }
406 }
407
408 /*
409  * Check if the cpu has gone through a quiescent state (say context
410  * switch). If so and if it already hasn't done so in this RCU
411  * quiescent cycle, then indicate that it has done so.
412  */
413 static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
414                                         struct rcu_data *rdp)
415 {
416         if (rdp->quiescbatch != rcp->cur) {
417                 /* start new grace period: */
418                 rdp->qs_pending = 1;
419                 rdp->passed_quiesc = 0;
420                 rdp->quiescbatch = rcp->cur;
421                 return;
422         }
423
424         /* Grace period already completed for this cpu?
425          * qs_pending is checked instead of the actual bitmap to avoid
426          * cacheline trashing.
427          */
428         if (!rdp->qs_pending)
429                 return;
430
431         /*
432          * Was there a quiescent state since the beginning of the grace
433          * period? If no, then exit and wait for the next call.
434          */
435         if (!rdp->passed_quiesc)
436                 return;
437         rdp->qs_pending = 0;
438
439         spin_lock(&rcp->lock);
440         /*
441          * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
442          * during cpu startup. Ignore the quiescent state.
443          */
444         if (likely(rdp->quiescbatch == rcp->cur))
445                 cpu_quiet(rdp->cpu, rcp);
446
447         spin_unlock(&rcp->lock);
448 }
449
450
451 #ifdef CONFIG_HOTPLUG_CPU
452
453 /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
454  * locking requirements, the list it's pulling from has to belong to a cpu
455  * which is dead and hence not processing interrupts.
456  */
457 static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
458                                 struct rcu_head **tail, long batch)
459 {
460         if (list) {
461                 local_irq_disable();
462                 this_rdp->batch = batch;
463                 *this_rdp->nxttail[2] = list;
464                 this_rdp->nxttail[2] = tail;
465                 local_irq_enable();
466         }
467 }
468
469 static void __rcu_offline_cpu(struct rcu_data *this_rdp,
470                                 struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
471 {
472         /*
473          * if the cpu going offline owns the grace period
474          * we can block indefinitely waiting for it, so flush
475          * it here
476          */
477         spin_lock_bh(&rcp->lock);
478         if (rcp->cur != rcp->completed)
479                 cpu_quiet(rdp->cpu, rcp);
480         rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail, rcp->cur + 1);
481         rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail[2], rcp->cur + 1);
482         spin_unlock_bh(&rcp->lock);
483
484         local_irq_disable();
485         this_rdp->qlen += rdp->qlen;
486         local_irq_enable();
487 }
488
489 static void rcu_offline_cpu(int cpu)
490 {
491         struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
492         struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
493
494         __rcu_offline_cpu(this_rdp, &rcu_ctrlblk,
495                                         &per_cpu(rcu_data, cpu));
496         __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk,
497                                         &per_cpu(rcu_bh_data, cpu));
498         put_cpu_var(rcu_data);
499         put_cpu_var(rcu_bh_data);
500 }
501
502 #else
503
504 static void rcu_offline_cpu(int cpu)
505 {
506 }
507
508 #endif
509
510 /*
511  * This does the RCU processing work from softirq context.
512  */
513 static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
514                                         struct rcu_data *rdp)
515 {
516         long completed_snap;
517
518         if (rdp->nxtlist) {
519                 local_irq_disable();
520                 completed_snap = ACCESS_ONCE(rcp->completed);
521
522                 /*
523                  * move the other grace-period-completed entries to
524                  * [rdp->nxtlist, *rdp->nxttail[0]) temporarily
525                  */
526                 if (!rcu_batch_before(completed_snap, rdp->batch))
527                         rdp->nxttail[0] = rdp->nxttail[1] = rdp->nxttail[2];
528                 else if (!rcu_batch_before(completed_snap, rdp->batch - 1))
529                         rdp->nxttail[0] = rdp->nxttail[1];
530
531                 /*
532                  * the grace period for entries in
533                  * [rdp->nxtlist, *rdp->nxttail[0]) has completed and
534                  * move these entries to donelist
535                  */
536                 if (rdp->nxttail[0] != &rdp->nxtlist) {
537                         *rdp->donetail = rdp->nxtlist;
538                         rdp->donetail = rdp->nxttail[0];
539                         rdp->nxtlist = *rdp->nxttail[0];
540                         *rdp->donetail = NULL;
541
542                         if (rdp->nxttail[1] == rdp->nxttail[0])
543                                 rdp->nxttail[1] = &rdp->nxtlist;
544                         if (rdp->nxttail[2] == rdp->nxttail[0])
545                                 rdp->nxttail[2] = &rdp->nxtlist;
546                         rdp->nxttail[0] = &rdp->nxtlist;
547                 }
548
549                 local_irq_enable();
550
551                 if (rcu_batch_after(rdp->batch, rcp->pending)) {
552                         /* and start it/schedule start if it's a new batch */
553                         spin_lock(&rcp->lock);
554                         if (rcu_batch_after(rdp->batch, rcp->pending)) {
555                                 rcp->pending = rdp->batch;
556                                 rcu_start_batch(rcp);
557                         }
558                         spin_unlock(&rcp->lock);
559                 }
560         }
561
562         rcu_check_quiescent_state(rcp, rdp);
563         if (rdp->donelist)
564                 rcu_do_batch(rdp);
565 }
566
567 static void rcu_process_callbacks(struct softirq_action *unused)
568 {
569         /*
570          * Memory references from any prior RCU read-side critical sections
571          * executed by the interrupted code must be see before any RCU
572          * grace-period manupulations below.
573          */
574
575         smp_mb(); /* See above block comment. */
576
577         __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
578         __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
579
580         /*
581          * Memory references from any later RCU read-side critical sections
582          * executed by the interrupted code must be see after any RCU
583          * grace-period manupulations above.
584          */
585
586         smp_mb(); /* See above block comment. */
587 }
588
589 static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
590 {
591         /* Check for CPU stalls, if enabled. */
592         check_cpu_stall(rcp, rdp);
593
594         if (rdp->nxtlist) {
595                 long completed_snap = ACCESS_ONCE(rcp->completed);
596
597                 /*
598                  * This cpu has pending rcu entries and the grace period
599                  * for them has completed.
600                  */
601                 if (!rcu_batch_before(completed_snap, rdp->batch))
602                         return 1;
603                 if (!rcu_batch_before(completed_snap, rdp->batch - 1) &&
604                                 rdp->nxttail[0] != rdp->nxttail[1])
605                         return 1;
606                 if (rdp->nxttail[0] != &rdp->nxtlist)
607                         return 1;
608
609                 /*
610                  * This cpu has pending rcu entries and the new batch
611                  * for then hasn't been started nor scheduled start
612                  */
613                 if (rcu_batch_after(rdp->batch, rcp->pending))
614                         return 1;
615         }
616
617         /* This cpu has finished callbacks to invoke */
618         if (rdp->donelist)
619                 return 1;
620
621         /* The rcu core waits for a quiescent state from the cpu */
622         if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
623                 return 1;
624
625         /* nothing to do */
626         return 0;
627 }
628
629 /*
630  * Check to see if there is any immediate RCU-related work to be done
631  * by the current CPU, returning 1 if so.  This function is part of the
632  * RCU implementation; it is -not- an exported member of the RCU API.
633  */
634 int rcu_pending(int cpu)
635 {
636         return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) ||
637                 __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu));
638 }
639
640 /*
641  * Check to see if any future RCU-related work will need to be done
642  * by the current CPU, even if none need be done immediately, returning
643  * 1 if so.  This function is part of the RCU implementation; it is -not-
644  * an exported member of the RCU API.
645  */
646 int rcu_needs_cpu(int cpu)
647 {
648         struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
649         struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu);
650
651         return !!rdp->nxtlist || !!rdp_bh->nxtlist || rcu_pending(cpu);
652 }
653
654 /*
655  * Top-level function driving RCU grace-period detection, normally
656  * invoked from the scheduler-clock interrupt.  This function simply
657  * increments counters that are read only from softirq by this same
658  * CPU, so there are no memory barriers required.
659  */
660 void rcu_check_callbacks(int cpu, int user)
661 {
662         if (user ||
663             (idle_cpu(cpu) && !in_softirq() &&
664                                 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
665
666                 /*
667                  * Get here if this CPU took its interrupt from user
668                  * mode or from the idle loop, and if this is not a
669                  * nested interrupt.  In this case, the CPU is in
670                  * a quiescent state, so count it.
671                  *
672                  * Also do a memory barrier.  This is needed to handle
673                  * the case where writes from a preempt-disable section
674                  * of code get reordered into schedule() by this CPU's
675                  * write buffer.  The memory barrier makes sure that
676                  * the rcu_qsctr_inc() and rcu_bh_qsctr_inc() are see
677                  * by other CPUs to happen after any such write.
678                  */
679
680                 smp_mb();  /* See above block comment. */
681                 rcu_qsctr_inc(cpu);
682                 rcu_bh_qsctr_inc(cpu);
683
684         } else if (!in_softirq()) {
685
686                 /*
687                  * Get here if this CPU did not take its interrupt from
688                  * softirq, in other words, if it is not interrupting
689                  * a rcu_bh read-side critical section.  This is an _bh
690                  * critical section, so count it.  The memory barrier
691                  * is needed for the same reason as is the above one.
692                  */
693
694                 smp_mb();  /* See above block comment. */
695                 rcu_bh_qsctr_inc(cpu);
696         }
697         raise_rcu_softirq();
698 }
699
700 static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
701                                                 struct rcu_data *rdp)
702 {
703         spin_lock(&rcp->lock);
704         memset(rdp, 0, sizeof(*rdp));
705         rdp->nxttail[0] = rdp->nxttail[1] = rdp->nxttail[2] = &rdp->nxtlist;
706         rdp->donetail = &rdp->donelist;
707         rdp->quiescbatch = rcp->completed;
708         rdp->qs_pending = 0;
709         rdp->cpu = cpu;
710         rdp->blimit = blimit;
711         spin_unlock(&rcp->lock);
712 }
713
714 static void __cpuinit rcu_online_cpu(int cpu)
715 {
716         struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
717         struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
718
719         rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
720         rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
721         open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
722 }
723
724 static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
725                                 unsigned long action, void *hcpu)
726 {
727         long cpu = (long)hcpu;
728
729         switch (action) {
730         case CPU_UP_PREPARE:
731         case CPU_UP_PREPARE_FROZEN:
732                 rcu_online_cpu(cpu);
733                 break;
734         case CPU_DEAD:
735         case CPU_DEAD_FROZEN:
736                 rcu_offline_cpu(cpu);
737                 break;
738         default:
739                 break;
740         }
741         return NOTIFY_OK;
742 }
743
744 static struct notifier_block __cpuinitdata rcu_nb = {
745         .notifier_call  = rcu_cpu_notify,
746 };
747
748 /*
749  * Initializes rcu mechanism.  Assumed to be called early.
750  * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
751  * Note that rcu_qsctr and friends are implicitly
752  * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
753  */
754 void __init __rcu_init(void)
755 {
756         rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
757                         (void *)(long)smp_processor_id());
758         /* Register notifier for non-boot CPUs */
759         register_cpu_notifier(&rcu_nb);
760 }
761
762 module_param(blimit, int, 0);
763 module_param(qhimark, int, 0);
764 module_param(qlowmark, int, 0);