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