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