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