[PATCH] rcu: join rcu_ctrlblk and rcu_state
[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};
2cc78eb5 70static int maxbatch = 10000;
1da177e4
LT
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 */
83void 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;
5ee832db
ED
95
96 if (unlikely(++rdp->count > 10000))
97 set_need_resched();
98
1da177e4
LT
99 local_irq_restore(flags);
100}
101
ab4720ec
DS
102static atomic_t rcu_barrier_cpu_count;
103static struct semaphore rcu_barrier_sema;
104static struct completion rcu_barrier_completion;
105
1da177e4
LT
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 */
122void 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;
5ee832db
ED
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 */
1da177e4
LT
140 local_irq_restore(flags);
141}
142
a241ec65
PM
143/*
144 * Return the number of RCU batches processed thus far. Useful
145 * for debug and statistics.
146 */
147long rcu_batches_completed(void)
148{
149 return rcu_ctrlblk.completed;
150}
151
ab4720ec
DS
152static 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 */
161static 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 */
175void 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}
186EXPORT_SYMBOL_GPL(rcu_barrier);
187
1da177e4
LT
188/*
189 * Invoke the completed RCU callbacks. They are expected to be in
190 * a per-cpu list.
191 */
192static 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;
5ee832db 202 rdp->count--;
1da177e4
LT
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
69a0b315 219 * rcu_ctrlblk.cpumask bitmap.
1da177e4
LT
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
69a0b315 225 * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If
1da177e4
LT
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.
69a0b315 233 * Caller must hold rcu_ctrlblk.lock.
1da177e4 234 */
69a0b315 235static void rcu_start_batch(struct rcu_ctrlblk *rcp)
1da177e4 236{
1da177e4
LT
237 if (rcp->next_pending &&
238 rcp->completed == rcp->cur) {
1da177e4 239 rcp->next_pending = 0;
c3f59023
SV
240 /*
241 * next_pending == 0 must be visible in
242 * __rcu_process_callbacks() before it can see new value of cur.
1da177e4
LT
243 */
244 smp_wmb();
245 rcp->cur++;
c3f59023
SV
246
247 /*
248 * Accessing nohz_cpu_mask before incrementing rcp->cur needs a
249 * Barrier Otherwise it can cause tickless idle CPUs to be
69a0b315 250 * included in rcp->cpumask, which will extend graceperiods
c3f59023
SV
251 * unnecessarily.
252 */
253 smp_mb();
69a0b315 254 cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask);
c3f59023 255
1da177e4
LT
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 */
69a0b315 264static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
1da177e4 265{
69a0b315
ON
266 cpu_clear(cpu, rcp->cpumask);
267 if (cpus_empty(rcp->cpumask)) {
1da177e4
LT
268 /* batch completed ! */
269 rcp->completed = rcp->cur;
69a0b315 270 rcu_start_batch(rcp);
1da177e4
LT
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 */
279static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
69a0b315 280 struct rcu_data *rdp)
1da177e4
LT
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
69a0b315 305 spin_lock(&rcp->lock);
1da177e4
LT
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))
69a0b315 311 cpu_quiet(rdp->cpu, rcp);
1da177e4 312
69a0b315 313 spin_unlock(&rcp->lock);
1da177e4
LT
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 */
323static 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
333static void __rcu_offline_cpu(struct rcu_data *this_rdp,
69a0b315 334 struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
1da177e4
LT
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 */
69a0b315 340 spin_lock_bh(&rcp->lock);
1da177e4 341 if (rcp->cur != rcp->completed)
69a0b315
ON
342 cpu_quiet(rdp->cpu, rcp);
343 spin_unlock_bh(&rcp->lock);
1da177e4
LT
344 rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
345 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
346
347}
348static void rcu_offline_cpu(int cpu)
349{
350 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
351 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
352
69a0b315 353 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk,
1da177e4 354 &per_cpu(rcu_data, cpu));
69a0b315 355 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk,
1da177e4
LT
356 &per_cpu(rcu_bh_data, cpu));
357 put_cpu_var(rcu_data);
358 put_cpu_var(rcu_bh_data);
359 tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu);
360}
361
362#else
363
364static void rcu_offline_cpu(int cpu)
365{
366}
367
368#endif
369
370/*
371 * This does the RCU processing work from tasklet context.
372 */
373static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
69a0b315 374 struct rcu_data *rdp)
1da177e4
LT
375{
376 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
377 *rdp->donetail = rdp->curlist;
378 rdp->donetail = rdp->curtail;
379 rdp->curlist = NULL;
380 rdp->curtail = &rdp->curlist;
381 }
382
383 local_irq_disable();
384 if (rdp->nxtlist && !rdp->curlist) {
385 rdp->curlist = rdp->nxtlist;
386 rdp->curtail = rdp->nxttail;
387 rdp->nxtlist = NULL;
388 rdp->nxttail = &rdp->nxtlist;
389 local_irq_enable();
390
391 /*
392 * start the next batch of callbacks
393 */
394
395 /* determine batch number */
396 rdp->batch = rcp->cur + 1;
397 /* see the comment and corresponding wmb() in
398 * the rcu_start_batch()
399 */
400 smp_rmb();
401
402 if (!rcp->next_pending) {
403 /* and start it/schedule start if it's a new batch */
69a0b315 404 spin_lock(&rcp->lock);
dbc1651f 405 rcp->next_pending = 1;
69a0b315
ON
406 rcu_start_batch(rcp);
407 spin_unlock(&rcp->lock);
1da177e4
LT
408 }
409 } else {
410 local_irq_enable();
411 }
69a0b315 412 rcu_check_quiescent_state(rcp, rdp);
1da177e4
LT
413 if (rdp->donelist)
414 rcu_do_batch(rdp);
415}
416
417static void rcu_process_callbacks(unsigned long unused)
418{
69a0b315
ON
419 __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
420 __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
1da177e4
LT
421}
422
67751777
ON
423static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
424{
425 /* This cpu has pending rcu entries and the grace period
426 * for them has completed.
427 */
428 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch))
429 return 1;
430
431 /* This cpu has no pending entries, but there are new entries */
432 if (!rdp->curlist && rdp->nxtlist)
433 return 1;
434
435 /* This cpu has finished callbacks to invoke */
436 if (rdp->donelist)
437 return 1;
438
439 /* The rcu core waits for a quiescent state from the cpu */
440 if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
441 return 1;
442
443 /* nothing to do */
444 return 0;
445}
446
447int rcu_pending(int cpu)
448{
449 return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) ||
450 __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu));
451}
452
1da177e4
LT
453void rcu_check_callbacks(int cpu, int user)
454{
455 if (user ||
456 (idle_cpu(cpu) && !in_softirq() &&
457 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
458 rcu_qsctr_inc(cpu);
459 rcu_bh_qsctr_inc(cpu);
460 } else if (!in_softirq())
461 rcu_bh_qsctr_inc(cpu);
462 tasklet_schedule(&per_cpu(rcu_tasklet, cpu));
463}
464
465static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
466 struct rcu_data *rdp)
467{
468 memset(rdp, 0, sizeof(*rdp));
469 rdp->curtail = &rdp->curlist;
470 rdp->nxttail = &rdp->nxtlist;
471 rdp->donetail = &rdp->donelist;
472 rdp->quiescbatch = rcp->completed;
473 rdp->qs_pending = 0;
474 rdp->cpu = cpu;
475}
476
477static void __devinit rcu_online_cpu(int cpu)
478{
479 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
480 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
481
482 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
483 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
484 tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL);
485}
486
487static int __devinit rcu_cpu_notify(struct notifier_block *self,
488 unsigned long action, void *hcpu)
489{
490 long cpu = (long)hcpu;
491 switch (action) {
492 case CPU_UP_PREPARE:
493 rcu_online_cpu(cpu);
494 break;
495 case CPU_DEAD:
496 rcu_offline_cpu(cpu);
497 break;
498 default:
499 break;
500 }
501 return NOTIFY_OK;
502}
503
504static struct notifier_block __devinitdata rcu_nb = {
505 .notifier_call = rcu_cpu_notify,
506};
507
508/*
509 * Initializes rcu mechanism. Assumed to be called early.
510 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
511 * Note that rcu_qsctr and friends are implicitly
512 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
513 */
514void __init rcu_init(void)
515{
ab4720ec 516 sema_init(&rcu_barrier_sema, 1);
1da177e4
LT
517 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
518 (void *)(long)smp_processor_id());
519 /* Register notifier for non-boot CPUs */
520 register_cpu_notifier(&rcu_nb);
521}
522
523struct rcu_synchronize {
524 struct rcu_head head;
525 struct completion completion;
526};
527
528/* Because of FASTCALL declaration of complete, we use this wrapper */
529static void wakeme_after_rcu(struct rcu_head *head)
530{
531 struct rcu_synchronize *rcu;
532
533 rcu = container_of(head, struct rcu_synchronize, head);
534 complete(&rcu->completion);
535}
536
537/**
9b06e818 538 * synchronize_rcu - wait until a grace period has elapsed.
1da177e4
LT
539 *
540 * Control will return to the caller some time after a full grace
541 * period has elapsed, in other words after all currently executing RCU
542 * read-side critical sections have completed. RCU read-side critical
543 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
544 * and may be nested.
9b06e818
PM
545 *
546 * If your read-side code is not protected by rcu_read_lock(), do -not-
547 * use synchronize_rcu().
1da177e4 548 */
9b06e818 549void synchronize_rcu(void)
1da177e4
LT
550{
551 struct rcu_synchronize rcu;
552
553 init_completion(&rcu.completion);
554 /* Will wake me after RCU finished */
555 call_rcu(&rcu.head, wakeme_after_rcu);
556
557 /* Wait for it */
558 wait_for_completion(&rcu.completion);
559}
560
9b06e818
PM
561/*
562 * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
563 */
564void synchronize_kernel(void)
565{
566 synchronize_rcu();
567}
568
1da177e4 569module_param(maxbatch, int, 0);
a241ec65 570EXPORT_SYMBOL_GPL(rcu_batches_completed);
66cf8f14
PM
571EXPORT_SYMBOL(call_rcu); /* WARNING: GPL-only in April 2006. */
572EXPORT_SYMBOL(call_rcu_bh); /* WARNING: GPL-only in April 2006. */
9b06e818 573EXPORT_SYMBOL_GPL(synchronize_rcu);
66cf8f14 574EXPORT_SYMBOL(synchronize_kernel); /* WARNING: GPL-only in April 2006. */
This page took 0.106727 seconds and 5 git commands to generate.