[PATCH] ide-cd: clear random-write capability it not supported
[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. */
52struct rcu_ctrlblk rcu_ctrlblk =
53 { .cur = -300, .completed = -300 };
54struct rcu_ctrlblk rcu_bh_ctrlblk =
55 { .cur = -300, .completed = -300 };
56
57/* Bookkeeping of the progress of the grace period */
58struct 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
22fc6ecc 64static struct rcu_state rcu_state ____cacheline_internodealigned_in_smp =
1da177e4 65 {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
22fc6ecc 66static struct rcu_state rcu_bh_state ____cacheline_internodealigned_in_smp =
1da177e4
LT
67 {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
68
69DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
70DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
71
72/* Fake initialization required by compiler */
73static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL};
2cc78eb5 74static int maxbatch = 10000;
1da177e4
LT
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 */
87void 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;
5ee832db
ED
99
100 if (unlikely(++rdp->count > 10000))
101 set_need_resched();
102
1da177e4
LT
103 local_irq_restore(flags);
104}
105
ab4720ec
DS
106static atomic_t rcu_barrier_cpu_count;
107static struct semaphore rcu_barrier_sema;
108static struct completion rcu_barrier_completion;
109
1da177e4
LT
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 */
126void 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;
5ee832db
ED
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 */
1da177e4
LT
144 local_irq_restore(flags);
145}
146
a241ec65
PM
147/*
148 * Return the number of RCU batches processed thus far. Useful
149 * for debug and statistics.
150 */
151long rcu_batches_completed(void)
152{
153 return rcu_ctrlblk.completed;
154}
155
ab4720ec
DS
156static 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 */
165static 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 */
179void 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}
190EXPORT_SYMBOL_GPL(rcu_barrier);
191
1da177e4
LT
192/*
193 * Invoke the completed RCU callbacks. They are expected to be in
194 * a per-cpu list.
195 */
196static 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;
5ee832db 206 rdp->count--;
1da177e4
LT
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 */
dbc1651f 239static void rcu_start_batch(struct rcu_ctrlblk *rcp, struct rcu_state *rsp)
1da177e4 240{
1da177e4
LT
241 if (rcp->next_pending &&
242 rcp->completed == rcp->cur) {
1da177e4 243 rcp->next_pending = 0;
c3f59023
SV
244 /*
245 * next_pending == 0 must be visible in
246 * __rcu_process_callbacks() before it can see new value of cur.
1da177e4
LT
247 */
248 smp_wmb();
249 rcp->cur++;
c3f59023
SV
250
251 /*
252 * Accessing nohz_cpu_mask before incrementing rcp->cur needs a
253 * Barrier Otherwise it can cause tickless idle CPUs to be
254 * included in rsp->cpumask, which will extend graceperiods
255 * unnecessarily.
256 */
257 smp_mb();
258 cpus_andnot(rsp->cpumask, cpu_online_map, nohz_cpu_mask);
259
1da177e4
LT
260 }
261}
262
263/*
264 * cpu went through a quiescent state since the beginning of the grace period.
265 * Clear it from the cpu mask and complete the grace period if it was the last
266 * cpu. Start another grace period if someone has further entries pending
267 */
268static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp, struct rcu_state *rsp)
269{
270 cpu_clear(cpu, rsp->cpumask);
271 if (cpus_empty(rsp->cpumask)) {
272 /* batch completed ! */
273 rcp->completed = rcp->cur;
dbc1651f 274 rcu_start_batch(rcp, rsp);
1da177e4
LT
275 }
276}
277
278/*
279 * Check if the cpu has gone through a quiescent state (say context
280 * switch). If so and if it already hasn't done so in this RCU
281 * quiescent cycle, then indicate that it has done so.
282 */
283static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
284 struct rcu_state *rsp, struct rcu_data *rdp)
285{
286 if (rdp->quiescbatch != rcp->cur) {
287 /* start new grace period: */
288 rdp->qs_pending = 1;
289 rdp->passed_quiesc = 0;
290 rdp->quiescbatch = rcp->cur;
291 return;
292 }
293
294 /* Grace period already completed for this cpu?
295 * qs_pending is checked instead of the actual bitmap to avoid
296 * cacheline trashing.
297 */
298 if (!rdp->qs_pending)
299 return;
300
301 /*
302 * Was there a quiescent state since the beginning of the grace
303 * period? If no, then exit and wait for the next call.
304 */
305 if (!rdp->passed_quiesc)
306 return;
307 rdp->qs_pending = 0;
308
309 spin_lock(&rsp->lock);
310 /*
311 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
312 * during cpu startup. Ignore the quiescent state.
313 */
314 if (likely(rdp->quiescbatch == rcp->cur))
315 cpu_quiet(rdp->cpu, rcp, rsp);
316
317 spin_unlock(&rsp->lock);
318}
319
320
321#ifdef CONFIG_HOTPLUG_CPU
322
323/* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
324 * locking requirements, the list it's pulling from has to belong to a cpu
325 * which is dead and hence not processing interrupts.
326 */
327static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
328 struct rcu_head **tail)
329{
330 local_irq_disable();
331 *this_rdp->nxttail = list;
332 if (list)
333 this_rdp->nxttail = tail;
334 local_irq_enable();
335}
336
337static void __rcu_offline_cpu(struct rcu_data *this_rdp,
338 struct rcu_ctrlblk *rcp, struct rcu_state *rsp, struct rcu_data *rdp)
339{
340 /* if the cpu going offline owns the grace period
341 * we can block indefinitely waiting for it, so flush
342 * it here
343 */
344 spin_lock_bh(&rsp->lock);
345 if (rcp->cur != rcp->completed)
346 cpu_quiet(rdp->cpu, rcp, rsp);
347 spin_unlock_bh(&rsp->lock);
348 rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
349 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
350
351}
352static void rcu_offline_cpu(int cpu)
353{
354 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
355 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
356
357 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk, &rcu_state,
358 &per_cpu(rcu_data, cpu));
359 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk, &rcu_bh_state,
360 &per_cpu(rcu_bh_data, cpu));
361 put_cpu_var(rcu_data);
362 put_cpu_var(rcu_bh_data);
363 tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu);
364}
365
366#else
367
368static void rcu_offline_cpu(int cpu)
369{
370}
371
372#endif
373
374/*
375 * This does the RCU processing work from tasklet context.
376 */
377static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
378 struct rcu_state *rsp, struct rcu_data *rdp)
379{
380 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
381 *rdp->donetail = rdp->curlist;
382 rdp->donetail = rdp->curtail;
383 rdp->curlist = NULL;
384 rdp->curtail = &rdp->curlist;
385 }
386
387 local_irq_disable();
388 if (rdp->nxtlist && !rdp->curlist) {
389 rdp->curlist = rdp->nxtlist;
390 rdp->curtail = rdp->nxttail;
391 rdp->nxtlist = NULL;
392 rdp->nxttail = &rdp->nxtlist;
393 local_irq_enable();
394
395 /*
396 * start the next batch of callbacks
397 */
398
399 /* determine batch number */
400 rdp->batch = rcp->cur + 1;
401 /* see the comment and corresponding wmb() in
402 * the rcu_start_batch()
403 */
404 smp_rmb();
405
406 if (!rcp->next_pending) {
407 /* and start it/schedule start if it's a new batch */
408 spin_lock(&rsp->lock);
dbc1651f
ON
409 rcp->next_pending = 1;
410 rcu_start_batch(rcp, rsp);
1da177e4
LT
411 spin_unlock(&rsp->lock);
412 }
413 } else {
414 local_irq_enable();
415 }
416 rcu_check_quiescent_state(rcp, rsp, rdp);
417 if (rdp->donelist)
418 rcu_do_batch(rdp);
419}
420
421static void rcu_process_callbacks(unsigned long unused)
422{
423 __rcu_process_callbacks(&rcu_ctrlblk, &rcu_state,
424 &__get_cpu_var(rcu_data));
425 __rcu_process_callbacks(&rcu_bh_ctrlblk, &rcu_bh_state,
426 &__get_cpu_var(rcu_bh_data));
427}
428
67751777
ON
429static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
430{
431 /* This cpu has pending rcu entries and the grace period
432 * for them has completed.
433 */
434 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch))
435 return 1;
436
437 /* This cpu has no pending entries, but there are new entries */
438 if (!rdp->curlist && rdp->nxtlist)
439 return 1;
440
441 /* This cpu has finished callbacks to invoke */
442 if (rdp->donelist)
443 return 1;
444
445 /* The rcu core waits for a quiescent state from the cpu */
446 if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
447 return 1;
448
449 /* nothing to do */
450 return 0;
451}
452
453int rcu_pending(int cpu)
454{
455 return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) ||
456 __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu));
457}
458
1da177e4
LT
459void rcu_check_callbacks(int cpu, int user)
460{
461 if (user ||
462 (idle_cpu(cpu) && !in_softirq() &&
463 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
464 rcu_qsctr_inc(cpu);
465 rcu_bh_qsctr_inc(cpu);
466 } else if (!in_softirq())
467 rcu_bh_qsctr_inc(cpu);
468 tasklet_schedule(&per_cpu(rcu_tasklet, cpu));
469}
470
471static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
472 struct rcu_data *rdp)
473{
474 memset(rdp, 0, sizeof(*rdp));
475 rdp->curtail = &rdp->curlist;
476 rdp->nxttail = &rdp->nxtlist;
477 rdp->donetail = &rdp->donelist;
478 rdp->quiescbatch = rcp->completed;
479 rdp->qs_pending = 0;
480 rdp->cpu = cpu;
481}
482
483static void __devinit rcu_online_cpu(int cpu)
484{
485 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
486 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
487
488 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
489 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
490 tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL);
491}
492
493static int __devinit rcu_cpu_notify(struct notifier_block *self,
494 unsigned long action, void *hcpu)
495{
496 long cpu = (long)hcpu;
497 switch (action) {
498 case CPU_UP_PREPARE:
499 rcu_online_cpu(cpu);
500 break;
501 case CPU_DEAD:
502 rcu_offline_cpu(cpu);
503 break;
504 default:
505 break;
506 }
507 return NOTIFY_OK;
508}
509
510static struct notifier_block __devinitdata rcu_nb = {
511 .notifier_call = rcu_cpu_notify,
512};
513
514/*
515 * Initializes rcu mechanism. Assumed to be called early.
516 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
517 * Note that rcu_qsctr and friends are implicitly
518 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
519 */
520void __init rcu_init(void)
521{
ab4720ec 522 sema_init(&rcu_barrier_sema, 1);
1da177e4
LT
523 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
524 (void *)(long)smp_processor_id());
525 /* Register notifier for non-boot CPUs */
526 register_cpu_notifier(&rcu_nb);
527}
528
529struct rcu_synchronize {
530 struct rcu_head head;
531 struct completion completion;
532};
533
534/* Because of FASTCALL declaration of complete, we use this wrapper */
535static void wakeme_after_rcu(struct rcu_head *head)
536{
537 struct rcu_synchronize *rcu;
538
539 rcu = container_of(head, struct rcu_synchronize, head);
540 complete(&rcu->completion);
541}
542
543/**
9b06e818 544 * synchronize_rcu - wait until a grace period has elapsed.
1da177e4
LT
545 *
546 * Control will return to the caller some time after a full grace
547 * period has elapsed, in other words after all currently executing RCU
548 * read-side critical sections have completed. RCU read-side critical
549 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
550 * and may be nested.
9b06e818
PM
551 *
552 * If your read-side code is not protected by rcu_read_lock(), do -not-
553 * use synchronize_rcu().
1da177e4 554 */
9b06e818 555void synchronize_rcu(void)
1da177e4
LT
556{
557 struct rcu_synchronize rcu;
558
559 init_completion(&rcu.completion);
560 /* Will wake me after RCU finished */
561 call_rcu(&rcu.head, wakeme_after_rcu);
562
563 /* Wait for it */
564 wait_for_completion(&rcu.completion);
565}
566
9b06e818
PM
567/*
568 * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
569 */
570void synchronize_kernel(void)
571{
572 synchronize_rcu();
573}
574
1da177e4 575module_param(maxbatch, int, 0);
a241ec65 576EXPORT_SYMBOL_GPL(rcu_batches_completed);
66cf8f14
PM
577EXPORT_SYMBOL(call_rcu); /* WARNING: GPL-only in April 2006. */
578EXPORT_SYMBOL(call_rcu_bh); /* WARNING: GPL-only in April 2006. */
9b06e818 579EXPORT_SYMBOL_GPL(synchronize_rcu);
66cf8f14 580EXPORT_SYMBOL(synchronize_kernel); /* WARNING: GPL-only in April 2006. */
This page took 0.104507 seconds and 5 git commands to generate.