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