[SCSI] ips: Fix up for correct scatter/gather processing
[linux-2.6.22.y-op.git] / kernel / rcupdate.c
blob2559d4b8f23f6ef005db9825952afa0936d22951
1 /*
2 * Read-Copy Update mechanism for mutual exclusion
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.
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.
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.
18 * Copyright (C) IBM Corporation, 2001
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
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)
29 * For detailed explanation of Read-Copy Update mechanism see -
30 * http://lse.sourceforge.net/locking/rcupdate.html
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/interrupt.h>
39 #include <linux/sched.h>
40 #include <asm/atomic.h>
41 #include <linux/bitops.h>
42 #include <linux/module.h>
43 #include <linux/completion.h>
44 #include <linux/moduleparam.h>
45 #include <linux/percpu.h>
46 #include <linux/notifier.h>
47 #include <linux/rcupdate.h>
48 #include <linux/rcuref.h>
49 #include <linux/cpu.h>
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 };
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. */
64 static struct rcu_state rcu_state ____cacheline_maxaligned_in_smp =
65 {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
66 static struct rcu_state rcu_bh_state ____cacheline_maxaligned_in_smp =
67 {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
69 DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
70 DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
72 /* Fake initialization required by compiler */
73 static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL};
74 static int maxbatch = 10000;
76 #ifndef __HAVE_ARCH_CMPXCHG
78 * We use an array of spinlocks for the rcurefs -- similar to ones in sparc
79 * 32 bit atomic_t implementations, and a hash function similar to that
80 * for our refcounting needs.
81 * Can't help multiprocessors which donot have cmpxchg :(
84 spinlock_t __rcuref_hash[RCUREF_HASH_SIZE] = {
85 [0 ... (RCUREF_HASH_SIZE-1)] = SPIN_LOCK_UNLOCKED
87 #endif
89 /**
90 * call_rcu - Queue an RCU callback for invocation after a grace period.
91 * @head: structure to be used for queueing the RCU updates.
92 * @func: actual update function to be invoked after the grace period
94 * The update function will be invoked some time after a full grace
95 * period elapses, in other words after all currently executing RCU
96 * read-side critical sections have completed. RCU read-side critical
97 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
98 * and may be nested.
100 void fastcall call_rcu(struct rcu_head *head,
101 void (*func)(struct rcu_head *rcu))
103 unsigned long flags;
104 struct rcu_data *rdp;
106 head->func = func;
107 head->next = NULL;
108 local_irq_save(flags);
109 rdp = &__get_cpu_var(rcu_data);
110 *rdp->nxttail = head;
111 rdp->nxttail = &head->next;
113 if (unlikely(++rdp->count > 10000))
114 set_need_resched();
116 local_irq_restore(flags);
120 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
121 * @head: structure to be used for queueing the RCU updates.
122 * @func: actual update function to be invoked after the grace period
124 * The update function will be invoked some time after a full grace
125 * period elapses, in other words after all currently executing RCU
126 * read-side critical sections have completed. call_rcu_bh() assumes
127 * that the read-side critical sections end on completion of a softirq
128 * handler. This means that read-side critical sections in process
129 * context must not be interrupted by softirqs. This interface is to be
130 * used when most of the read-side critical sections are in softirq context.
131 * RCU read-side critical sections are delimited by rcu_read_lock() and
132 * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
133 * and rcu_read_unlock_bh(), if in process context. These may be nested.
135 void fastcall call_rcu_bh(struct rcu_head *head,
136 void (*func)(struct rcu_head *rcu))
138 unsigned long flags;
139 struct rcu_data *rdp;
141 head->func = func;
142 head->next = NULL;
143 local_irq_save(flags);
144 rdp = &__get_cpu_var(rcu_bh_data);
145 *rdp->nxttail = head;
146 rdp->nxttail = &head->next;
147 rdp->count++;
149 * Should we directly call rcu_do_batch() here ?
150 * if (unlikely(rdp->count > 10000))
151 * rcu_do_batch(rdp);
153 local_irq_restore(flags);
157 * Invoke the completed RCU callbacks. They are expected to be in
158 * a per-cpu list.
160 static void rcu_do_batch(struct rcu_data *rdp)
162 struct rcu_head *next, *list;
163 int count = 0;
165 list = rdp->donelist;
166 while (list) {
167 next = rdp->donelist = list->next;
168 list->func(list);
169 list = next;
170 rdp->count--;
171 if (++count >= maxbatch)
172 break;
174 if (!rdp->donelist)
175 rdp->donetail = &rdp->donelist;
176 else
177 tasklet_schedule(&per_cpu(rcu_tasklet, rdp->cpu));
181 * Grace period handling:
182 * The grace period handling consists out of two steps:
183 * - A new grace period is started.
184 * This is done by rcu_start_batch. The start is not broadcasted to
185 * all cpus, they must pick this up by comparing rcp->cur with
186 * rdp->quiescbatch. All cpus are recorded in the
187 * rcu_state.cpumask bitmap.
188 * - All cpus must go through a quiescent state.
189 * Since the start of the grace period is not broadcasted, at least two
190 * calls to rcu_check_quiescent_state are required:
191 * The first call just notices that a new grace period is running. The
192 * following calls check if there was a quiescent state since the beginning
193 * of the grace period. If so, it updates rcu_state.cpumask. If
194 * the bitmap is empty, then the grace period is completed.
195 * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
196 * period (if necessary).
199 * Register a new batch of callbacks, and start it up if there is currently no
200 * active batch and the batch to be registered has not already occurred.
201 * Caller must hold rcu_state.lock.
203 static void rcu_start_batch(struct rcu_ctrlblk *rcp, struct rcu_state *rsp,
204 int next_pending)
206 if (next_pending)
207 rcp->next_pending = 1;
209 if (rcp->next_pending &&
210 rcp->completed == rcp->cur) {
211 /* Can't change, since spin lock held. */
212 cpus_andnot(rsp->cpumask, cpu_online_map, nohz_cpu_mask);
214 rcp->next_pending = 0;
215 /* next_pending == 0 must be visible in __rcu_process_callbacks()
216 * before it can see new value of cur.
218 smp_wmb();
219 rcp->cur++;
224 * cpu went through a quiescent state since the beginning of the grace period.
225 * Clear it from the cpu mask and complete the grace period if it was the last
226 * cpu. Start another grace period if someone has further entries pending
228 static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp, struct rcu_state *rsp)
230 cpu_clear(cpu, rsp->cpumask);
231 if (cpus_empty(rsp->cpumask)) {
232 /* batch completed ! */
233 rcp->completed = rcp->cur;
234 rcu_start_batch(rcp, rsp, 0);
239 * Check if the cpu has gone through a quiescent state (say context
240 * switch). If so and if it already hasn't done so in this RCU
241 * quiescent cycle, then indicate that it has done so.
243 static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
244 struct rcu_state *rsp, struct rcu_data *rdp)
246 if (rdp->quiescbatch != rcp->cur) {
247 /* start new grace period: */
248 rdp->qs_pending = 1;
249 rdp->passed_quiesc = 0;
250 rdp->quiescbatch = rcp->cur;
251 return;
254 /* Grace period already completed for this cpu?
255 * qs_pending is checked instead of the actual bitmap to avoid
256 * cacheline trashing.
258 if (!rdp->qs_pending)
259 return;
262 * Was there a quiescent state since the beginning of the grace
263 * period? If no, then exit and wait for the next call.
265 if (!rdp->passed_quiesc)
266 return;
267 rdp->qs_pending = 0;
269 spin_lock(&rsp->lock);
271 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
272 * during cpu startup. Ignore the quiescent state.
274 if (likely(rdp->quiescbatch == rcp->cur))
275 cpu_quiet(rdp->cpu, rcp, rsp);
277 spin_unlock(&rsp->lock);
281 #ifdef CONFIG_HOTPLUG_CPU
283 /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
284 * locking requirements, the list it's pulling from has to belong to a cpu
285 * which is dead and hence not processing interrupts.
287 static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
288 struct rcu_head **tail)
290 local_irq_disable();
291 *this_rdp->nxttail = list;
292 if (list)
293 this_rdp->nxttail = tail;
294 local_irq_enable();
297 static void __rcu_offline_cpu(struct rcu_data *this_rdp,
298 struct rcu_ctrlblk *rcp, struct rcu_state *rsp, struct rcu_data *rdp)
300 /* if the cpu going offline owns the grace period
301 * we can block indefinitely waiting for it, so flush
302 * it here
304 spin_lock_bh(&rsp->lock);
305 if (rcp->cur != rcp->completed)
306 cpu_quiet(rdp->cpu, rcp, rsp);
307 spin_unlock_bh(&rsp->lock);
308 rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
309 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
312 static void rcu_offline_cpu(int cpu)
314 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
315 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
317 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk, &rcu_state,
318 &per_cpu(rcu_data, cpu));
319 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk, &rcu_bh_state,
320 &per_cpu(rcu_bh_data, cpu));
321 put_cpu_var(rcu_data);
322 put_cpu_var(rcu_bh_data);
323 tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu);
326 #else
328 static void rcu_offline_cpu(int cpu)
332 #endif
335 * This does the RCU processing work from tasklet context.
337 static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
338 struct rcu_state *rsp, struct rcu_data *rdp)
340 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
341 *rdp->donetail = rdp->curlist;
342 rdp->donetail = rdp->curtail;
343 rdp->curlist = NULL;
344 rdp->curtail = &rdp->curlist;
347 local_irq_disable();
348 if (rdp->nxtlist && !rdp->curlist) {
349 rdp->curlist = rdp->nxtlist;
350 rdp->curtail = rdp->nxttail;
351 rdp->nxtlist = NULL;
352 rdp->nxttail = &rdp->nxtlist;
353 local_irq_enable();
356 * start the next batch of callbacks
359 /* determine batch number */
360 rdp->batch = rcp->cur + 1;
361 /* see the comment and corresponding wmb() in
362 * the rcu_start_batch()
364 smp_rmb();
366 if (!rcp->next_pending) {
367 /* and start it/schedule start if it's a new batch */
368 spin_lock(&rsp->lock);
369 rcu_start_batch(rcp, rsp, 1);
370 spin_unlock(&rsp->lock);
372 } else {
373 local_irq_enable();
375 rcu_check_quiescent_state(rcp, rsp, rdp);
376 if (rdp->donelist)
377 rcu_do_batch(rdp);
380 static void rcu_process_callbacks(unsigned long unused)
382 __rcu_process_callbacks(&rcu_ctrlblk, &rcu_state,
383 &__get_cpu_var(rcu_data));
384 __rcu_process_callbacks(&rcu_bh_ctrlblk, &rcu_bh_state,
385 &__get_cpu_var(rcu_bh_data));
388 void rcu_check_callbacks(int cpu, int user)
390 if (user ||
391 (idle_cpu(cpu) && !in_softirq() &&
392 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
393 rcu_qsctr_inc(cpu);
394 rcu_bh_qsctr_inc(cpu);
395 } else if (!in_softirq())
396 rcu_bh_qsctr_inc(cpu);
397 tasklet_schedule(&per_cpu(rcu_tasklet, cpu));
400 static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
401 struct rcu_data *rdp)
403 memset(rdp, 0, sizeof(*rdp));
404 rdp->curtail = &rdp->curlist;
405 rdp->nxttail = &rdp->nxtlist;
406 rdp->donetail = &rdp->donelist;
407 rdp->quiescbatch = rcp->completed;
408 rdp->qs_pending = 0;
409 rdp->cpu = cpu;
412 static void __devinit rcu_online_cpu(int cpu)
414 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
415 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
417 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
418 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
419 tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL);
422 static int __devinit rcu_cpu_notify(struct notifier_block *self,
423 unsigned long action, void *hcpu)
425 long cpu = (long)hcpu;
426 switch (action) {
427 case CPU_UP_PREPARE:
428 rcu_online_cpu(cpu);
429 break;
430 case CPU_DEAD:
431 rcu_offline_cpu(cpu);
432 break;
433 default:
434 break;
436 return NOTIFY_OK;
439 static struct notifier_block __devinitdata rcu_nb = {
440 .notifier_call = rcu_cpu_notify,
444 * Initializes rcu mechanism. Assumed to be called early.
445 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
446 * Note that rcu_qsctr and friends are implicitly
447 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
449 void __init rcu_init(void)
451 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
452 (void *)(long)smp_processor_id());
453 /* Register notifier for non-boot CPUs */
454 register_cpu_notifier(&rcu_nb);
457 struct rcu_synchronize {
458 struct rcu_head head;
459 struct completion completion;
462 /* Because of FASTCALL declaration of complete, we use this wrapper */
463 static void wakeme_after_rcu(struct rcu_head *head)
465 struct rcu_synchronize *rcu;
467 rcu = container_of(head, struct rcu_synchronize, head);
468 complete(&rcu->completion);
472 * synchronize_rcu - wait until a grace period has elapsed.
474 * Control will return to the caller some time after a full grace
475 * period has elapsed, in other words after all currently executing RCU
476 * read-side critical sections have completed. RCU read-side critical
477 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
478 * and may be nested.
480 * If your read-side code is not protected by rcu_read_lock(), do -not-
481 * use synchronize_rcu().
483 void synchronize_rcu(void)
485 struct rcu_synchronize rcu;
487 init_completion(&rcu.completion);
488 /* Will wake me after RCU finished */
489 call_rcu(&rcu.head, wakeme_after_rcu);
491 /* Wait for it */
492 wait_for_completion(&rcu.completion);
496 * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
498 void synchronize_kernel(void)
500 synchronize_rcu();
503 module_param(maxbatch, int, 0);
504 EXPORT_SYMBOL(call_rcu); /* WARNING: GPL-only in April 2006. */
505 EXPORT_SYMBOL(call_rcu_bh); /* WARNING: GPL-only in April 2006. */
506 EXPORT_SYMBOL_GPL(synchronize_rcu);
507 EXPORT_SYMBOL(synchronize_kernel); /* WARNING: GPL-only in April 2006. */