[PATCH] timer initialization cleanup: DEFINE_TIMER
[pandora-kernel.git] / kernel / rcupdate.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 (C) 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  *              http://lse.sourceforge.net/locking/rcupdate.html
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/interrupt.h>
39 #include <linux/sched.h>
40 #include <asm/atomic.h>
41 #include <linux/bitops.h>
42 #include <linux/module.h>
43 #include <linux/completion.h>
44 #include <linux/moduleparam.h>
45 #include <linux/percpu.h>
46 #include <linux/notifier.h>
47 #include <linux/rcupdate.h>
48 #include <linux/rcuref.h>
49 #include <linux/cpu.h>
50
51 /* Definition for rcupdate control block. */
52 struct rcu_ctrlblk rcu_ctrlblk = 
53         { .cur = -300, .completed = -300 };
54 struct rcu_ctrlblk rcu_bh_ctrlblk =
55         { .cur = -300, .completed = -300 };
56
57 /* Bookkeeping of the progress of the grace period */
58 struct rcu_state {
59         spinlock_t      lock; /* Guard this struct and writes to rcu_ctrlblk */
60         cpumask_t       cpumask; /* CPUs that need to switch in order    */
61                                       /* for current batch to proceed.        */
62 };
63
64 static struct rcu_state rcu_state ____cacheline_maxaligned_in_smp =
65           {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
66 static struct rcu_state rcu_bh_state ____cacheline_maxaligned_in_smp =
67           {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
68
69 DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
70 DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
71
72 /* Fake initialization required by compiler */
73 static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL};
74 static int maxbatch = 10;
75
76 #ifndef __HAVE_ARCH_CMPXCHG
77 /*
78  * We use an array of spinlocks for the rcurefs -- similar to ones in sparc
79  * 32 bit atomic_t implementations, and a hash function similar to that
80  * for our refcounting needs.
81  * Can't help multiprocessors which donot have cmpxchg :(
82  */
83
84 spinlock_t __rcuref_hash[RCUREF_HASH_SIZE] = {
85         [0 ... (RCUREF_HASH_SIZE-1)] = SPIN_LOCK_UNLOCKED
86 };
87 #endif
88
89 /**
90  * call_rcu - Queue an RCU callback for invocation after a grace period.
91  * @head: structure to be used for queueing the RCU updates.
92  * @func: actual update function to be invoked after the grace period
93  *
94  * The update function will be invoked some time after a full grace
95  * period elapses, in other words after all currently executing RCU
96  * read-side critical sections have completed.  RCU read-side critical
97  * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
98  * and may be nested.
99  */
100 void fastcall call_rcu(struct rcu_head *head,
101                                 void (*func)(struct rcu_head *rcu))
102 {
103         unsigned long flags;
104         struct rcu_data *rdp;
105
106         head->func = func;
107         head->next = NULL;
108         local_irq_save(flags);
109         rdp = &__get_cpu_var(rcu_data);
110         *rdp->nxttail = head;
111         rdp->nxttail = &head->next;
112         local_irq_restore(flags);
113 }
114
115 /**
116  * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
117  * @head: structure to be used for queueing the RCU updates.
118  * @func: actual update function to be invoked after the grace period
119  *
120  * The update function will be invoked some time after a full grace
121  * period elapses, in other words after all currently executing RCU
122  * read-side critical sections have completed. call_rcu_bh() assumes
123  * that the read-side critical sections end on completion of a softirq
124  * handler. This means that read-side critical sections in process
125  * context must not be interrupted by softirqs. This interface is to be
126  * used when most of the read-side critical sections are in softirq context.
127  * RCU read-side critical sections are delimited by rcu_read_lock() and
128  * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
129  * and rcu_read_unlock_bh(), if in process context. These may be nested.
130  */
131 void fastcall call_rcu_bh(struct rcu_head *head,
132                                 void (*func)(struct rcu_head *rcu))
133 {
134         unsigned long flags;
135         struct rcu_data *rdp;
136
137         head->func = func;
138         head->next = NULL;
139         local_irq_save(flags);
140         rdp = &__get_cpu_var(rcu_bh_data);
141         *rdp->nxttail = head;
142         rdp->nxttail = &head->next;
143         local_irq_restore(flags);
144 }
145
146 /*
147  * Invoke the completed RCU callbacks. They are expected to be in
148  * a per-cpu list.
149  */
150 static void rcu_do_batch(struct rcu_data *rdp)
151 {
152         struct rcu_head *next, *list;
153         int count = 0;
154
155         list = rdp->donelist;
156         while (list) {
157                 next = rdp->donelist = list->next;
158                 list->func(list);
159                 list = next;
160                 if (++count >= maxbatch)
161                         break;
162         }
163         if (!rdp->donelist)
164                 rdp->donetail = &rdp->donelist;
165         else
166                 tasklet_schedule(&per_cpu(rcu_tasklet, rdp->cpu));
167 }
168
169 /*
170  * Grace period handling:
171  * The grace period handling consists out of two steps:
172  * - A new grace period is started.
173  *   This is done by rcu_start_batch. The start is not broadcasted to
174  *   all cpus, they must pick this up by comparing rcp->cur with
175  *   rdp->quiescbatch. All cpus are recorded  in the
176  *   rcu_state.cpumask bitmap.
177  * - All cpus must go through a quiescent state.
178  *   Since the start of the grace period is not broadcasted, at least two
179  *   calls to rcu_check_quiescent_state are required:
180  *   The first call just notices that a new grace period is running. The
181  *   following calls check if there was a quiescent state since the beginning
182  *   of the grace period. If so, it updates rcu_state.cpumask. If
183  *   the bitmap is empty, then the grace period is completed.
184  *   rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
185  *   period (if necessary).
186  */
187 /*
188  * Register a new batch of callbacks, and start it up if there is currently no
189  * active batch and the batch to be registered has not already occurred.
190  * Caller must hold rcu_state.lock.
191  */
192 static void rcu_start_batch(struct rcu_ctrlblk *rcp, struct rcu_state *rsp,
193                                 int next_pending)
194 {
195         if (next_pending)
196                 rcp->next_pending = 1;
197
198         if (rcp->next_pending &&
199                         rcp->completed == rcp->cur) {
200                 /* Can't change, since spin lock held. */
201                 cpus_andnot(rsp->cpumask, cpu_online_map, nohz_cpu_mask);
202
203                 rcp->next_pending = 0;
204                 /* next_pending == 0 must be visible in __rcu_process_callbacks()
205                  * before it can see new value of cur.
206                  */
207                 smp_wmb();
208                 rcp->cur++;
209         }
210 }
211
212 /*
213  * cpu went through a quiescent state since the beginning of the grace period.
214  * Clear it from the cpu mask and complete the grace period if it was the last
215  * cpu. Start another grace period if someone has further entries pending
216  */
217 static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp, struct rcu_state *rsp)
218 {
219         cpu_clear(cpu, rsp->cpumask);
220         if (cpus_empty(rsp->cpumask)) {
221                 /* batch completed ! */
222                 rcp->completed = rcp->cur;
223                 rcu_start_batch(rcp, rsp, 0);
224         }
225 }
226
227 /*
228  * Check if the cpu has gone through a quiescent state (say context
229  * switch). If so and if it already hasn't done so in this RCU
230  * quiescent cycle, then indicate that it has done so.
231  */
232 static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
233                         struct rcu_state *rsp, struct rcu_data *rdp)
234 {
235         if (rdp->quiescbatch != rcp->cur) {
236                 /* start new grace period: */
237                 rdp->qs_pending = 1;
238                 rdp->passed_quiesc = 0;
239                 rdp->quiescbatch = rcp->cur;
240                 return;
241         }
242
243         /* Grace period already completed for this cpu?
244          * qs_pending is checked instead of the actual bitmap to avoid
245          * cacheline trashing.
246          */
247         if (!rdp->qs_pending)
248                 return;
249
250         /* 
251          * Was there a quiescent state since the beginning of the grace
252          * period? If no, then exit and wait for the next call.
253          */
254         if (!rdp->passed_quiesc)
255                 return;
256         rdp->qs_pending = 0;
257
258         spin_lock(&rsp->lock);
259         /*
260          * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
261          * during cpu startup. Ignore the quiescent state.
262          */
263         if (likely(rdp->quiescbatch == rcp->cur))
264                 cpu_quiet(rdp->cpu, rcp, rsp);
265
266         spin_unlock(&rsp->lock);
267 }
268
269
270 #ifdef CONFIG_HOTPLUG_CPU
271
272 /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
273  * locking requirements, the list it's pulling from has to belong to a cpu
274  * which is dead and hence not processing interrupts.
275  */
276 static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
277                                 struct rcu_head **tail)
278 {
279         local_irq_disable();
280         *this_rdp->nxttail = list;
281         if (list)
282                 this_rdp->nxttail = tail;
283         local_irq_enable();
284 }
285
286 static void __rcu_offline_cpu(struct rcu_data *this_rdp,
287         struct rcu_ctrlblk *rcp, struct rcu_state *rsp, struct rcu_data *rdp)
288 {
289         /* if the cpu going offline owns the grace period
290          * we can block indefinitely waiting for it, so flush
291          * it here
292          */
293         spin_lock_bh(&rsp->lock);
294         if (rcp->cur != rcp->completed)
295                 cpu_quiet(rdp->cpu, rcp, rsp);
296         spin_unlock_bh(&rsp->lock);
297         rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
298         rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
299
300 }
301 static void rcu_offline_cpu(int cpu)
302 {
303         struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
304         struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
305
306         __rcu_offline_cpu(this_rdp, &rcu_ctrlblk, &rcu_state,
307                                         &per_cpu(rcu_data, cpu));
308         __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk, &rcu_bh_state,
309                                         &per_cpu(rcu_bh_data, cpu));
310         put_cpu_var(rcu_data);
311         put_cpu_var(rcu_bh_data);
312         tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu);
313 }
314
315 #else
316
317 static void rcu_offline_cpu(int cpu)
318 {
319 }
320
321 #endif
322
323 /*
324  * This does the RCU processing work from tasklet context. 
325  */
326 static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
327                         struct rcu_state *rsp, struct rcu_data *rdp)
328 {
329         if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
330                 *rdp->donetail = rdp->curlist;
331                 rdp->donetail = rdp->curtail;
332                 rdp->curlist = NULL;
333                 rdp->curtail = &rdp->curlist;
334         }
335
336         local_irq_disable();
337         if (rdp->nxtlist && !rdp->curlist) {
338                 rdp->curlist = rdp->nxtlist;
339                 rdp->curtail = rdp->nxttail;
340                 rdp->nxtlist = NULL;
341                 rdp->nxttail = &rdp->nxtlist;
342                 local_irq_enable();
343
344                 /*
345                  * start the next batch of callbacks
346                  */
347
348                 /* determine batch number */
349                 rdp->batch = rcp->cur + 1;
350                 /* see the comment and corresponding wmb() in
351                  * the rcu_start_batch()
352                  */
353                 smp_rmb();
354
355                 if (!rcp->next_pending) {
356                         /* and start it/schedule start if it's a new batch */
357                         spin_lock(&rsp->lock);
358                         rcu_start_batch(rcp, rsp, 1);
359                         spin_unlock(&rsp->lock);
360                 }
361         } else {
362                 local_irq_enable();
363         }
364         rcu_check_quiescent_state(rcp, rsp, rdp);
365         if (rdp->donelist)
366                 rcu_do_batch(rdp);
367 }
368
369 static void rcu_process_callbacks(unsigned long unused)
370 {
371         __rcu_process_callbacks(&rcu_ctrlblk, &rcu_state,
372                                 &__get_cpu_var(rcu_data));
373         __rcu_process_callbacks(&rcu_bh_ctrlblk, &rcu_bh_state,
374                                 &__get_cpu_var(rcu_bh_data));
375 }
376
377 void rcu_check_callbacks(int cpu, int user)
378 {
379         if (user || 
380             (idle_cpu(cpu) && !in_softirq() && 
381                                 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
382                 rcu_qsctr_inc(cpu);
383                 rcu_bh_qsctr_inc(cpu);
384         } else if (!in_softirq())
385                 rcu_bh_qsctr_inc(cpu);
386         tasklet_schedule(&per_cpu(rcu_tasklet, cpu));
387 }
388
389 static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
390                                                 struct rcu_data *rdp)
391 {
392         memset(rdp, 0, sizeof(*rdp));
393         rdp->curtail = &rdp->curlist;
394         rdp->nxttail = &rdp->nxtlist;
395         rdp->donetail = &rdp->donelist;
396         rdp->quiescbatch = rcp->completed;
397         rdp->qs_pending = 0;
398         rdp->cpu = cpu;
399 }
400
401 static void __devinit rcu_online_cpu(int cpu)
402 {
403         struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
404         struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
405
406         rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
407         rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
408         tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL);
409 }
410
411 static int __devinit rcu_cpu_notify(struct notifier_block *self, 
412                                 unsigned long action, void *hcpu)
413 {
414         long cpu = (long)hcpu;
415         switch (action) {
416         case CPU_UP_PREPARE:
417                 rcu_online_cpu(cpu);
418                 break;
419         case CPU_DEAD:
420                 rcu_offline_cpu(cpu);
421                 break;
422         default:
423                 break;
424         }
425         return NOTIFY_OK;
426 }
427
428 static struct notifier_block __devinitdata rcu_nb = {
429         .notifier_call  = rcu_cpu_notify,
430 };
431
432 /*
433  * Initializes rcu mechanism.  Assumed to be called early.
434  * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
435  * Note that rcu_qsctr and friends are implicitly
436  * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
437  */
438 void __init rcu_init(void)
439 {
440         rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
441                         (void *)(long)smp_processor_id());
442         /* Register notifier for non-boot CPUs */
443         register_cpu_notifier(&rcu_nb);
444 }
445
446 struct rcu_synchronize {
447         struct rcu_head head;
448         struct completion completion;
449 };
450
451 /* Because of FASTCALL declaration of complete, we use this wrapper */
452 static void wakeme_after_rcu(struct rcu_head  *head)
453 {
454         struct rcu_synchronize *rcu;
455
456         rcu = container_of(head, struct rcu_synchronize, head);
457         complete(&rcu->completion);
458 }
459
460 /**
461  * synchronize_rcu - wait until a grace period has elapsed.
462  *
463  * Control will return to the caller some time after a full grace
464  * period has elapsed, in other words after all currently executing RCU
465  * read-side critical sections have completed.  RCU read-side critical
466  * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
467  * and may be nested.
468  *
469  * If your read-side code is not protected by rcu_read_lock(), do -not-
470  * use synchronize_rcu().
471  */
472 void synchronize_rcu(void)
473 {
474         struct rcu_synchronize rcu;
475
476         init_completion(&rcu.completion);
477         /* Will wake me after RCU finished */
478         call_rcu(&rcu.head, wakeme_after_rcu);
479
480         /* Wait for it */
481         wait_for_completion(&rcu.completion);
482 }
483
484 /*
485  * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
486  */
487 void synchronize_kernel(void)
488 {
489         synchronize_rcu();
490 }
491
492 module_param(maxbatch, int, 0);
493 EXPORT_SYMBOL(call_rcu);  /* WARNING: GPL-only in April 2006. */
494 EXPORT_SYMBOL(call_rcu_bh);  /* WARNING: GPL-only in April 2006. */
495 EXPORT_SYMBOL_GPL(synchronize_rcu);
496 EXPORT_SYMBOL(synchronize_kernel);  /* WARNING: GPL-only in April 2006. */