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