[IA64-SGI] Shub2 BTE support - BTE recovery code
[linux/fpc-iii.git] / kernel / rcupdate.c
blobd00eded75d71f8b2df9170be42fbd30059641fd8
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/cpu.h>
50 /* Definition for rcupdate control block. */
51 struct rcu_ctrlblk rcu_ctrlblk =
52 { .cur = -300, .completed = -300 };
53 struct rcu_ctrlblk rcu_bh_ctrlblk =
54 { .cur = -300, .completed = -300 };
56 /* Bookkeeping of the progress of the grace period */
57 struct rcu_state {
58 spinlock_t lock; /* Guard this struct and writes to rcu_ctrlblk */
59 cpumask_t cpumask; /* CPUs that need to switch in order */
60 /* for current batch to proceed. */
63 static struct rcu_state rcu_state ____cacheline_maxaligned_in_smp =
64 {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
65 static struct rcu_state rcu_bh_state ____cacheline_maxaligned_in_smp =
66 {.lock = SPIN_LOCK_UNLOCKED, .cpumask = CPU_MASK_NONE };
68 DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
69 DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
71 /* Fake initialization required by compiler */
72 static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL};
73 static int maxbatch = 10;
75 /**
76 * call_rcu - Queue an RCU callback for invocation after a grace period.
77 * @head: structure to be used for queueing the RCU updates.
78 * @func: actual update function to be invoked after the grace period
80 * The update function will be invoked some time after a full grace
81 * period elapses, in other words after all currently executing RCU
82 * read-side critical sections have completed. RCU read-side critical
83 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
84 * and may be nested.
86 void fastcall call_rcu(struct rcu_head *head,
87 void (*func)(struct rcu_head *rcu))
89 unsigned long flags;
90 struct rcu_data *rdp;
92 head->func = func;
93 head->next = NULL;
94 local_irq_save(flags);
95 rdp = &__get_cpu_var(rcu_data);
96 *rdp->nxttail = head;
97 rdp->nxttail = &head->next;
98 local_irq_restore(flags);
102 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
103 * @head: structure to be used for queueing the RCU updates.
104 * @func: actual update function to be invoked after the grace period
106 * The update function will be invoked some time after a full grace
107 * period elapses, in other words after all currently executing RCU
108 * read-side critical sections have completed. call_rcu_bh() assumes
109 * that the read-side critical sections end on completion of a softirq
110 * handler. This means that read-side critical sections in process
111 * context must not be interrupted by softirqs. This interface is to be
112 * used when most of the read-side critical sections are in softirq context.
113 * RCU read-side critical sections are delimited by rcu_read_lock() and
114 * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
115 * and rcu_read_unlock_bh(), if in process context. These may be nested.
117 void fastcall call_rcu_bh(struct rcu_head *head,
118 void (*func)(struct rcu_head *rcu))
120 unsigned long flags;
121 struct rcu_data *rdp;
123 head->func = func;
124 head->next = NULL;
125 local_irq_save(flags);
126 rdp = &__get_cpu_var(rcu_bh_data);
127 *rdp->nxttail = head;
128 rdp->nxttail = &head->next;
129 local_irq_restore(flags);
133 * Invoke the completed RCU callbacks. They are expected to be in
134 * a per-cpu list.
136 static void rcu_do_batch(struct rcu_data *rdp)
138 struct rcu_head *next, *list;
139 int count = 0;
141 list = rdp->donelist;
142 while (list) {
143 next = rdp->donelist = list->next;
144 list->func(list);
145 list = next;
146 if (++count >= maxbatch)
147 break;
149 if (!rdp->donelist)
150 rdp->donetail = &rdp->donelist;
151 else
152 tasklet_schedule(&per_cpu(rcu_tasklet, rdp->cpu));
156 * Grace period handling:
157 * The grace period handling consists out of two steps:
158 * - A new grace period is started.
159 * This is done by rcu_start_batch. The start is not broadcasted to
160 * all cpus, they must pick this up by comparing rcp->cur with
161 * rdp->quiescbatch. All cpus are recorded in the
162 * rcu_state.cpumask bitmap.
163 * - All cpus must go through a quiescent state.
164 * Since the start of the grace period is not broadcasted, at least two
165 * calls to rcu_check_quiescent_state are required:
166 * The first call just notices that a new grace period is running. The
167 * following calls check if there was a quiescent state since the beginning
168 * of the grace period. If so, it updates rcu_state.cpumask. If
169 * the bitmap is empty, then the grace period is completed.
170 * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
171 * period (if necessary).
174 * Register a new batch of callbacks, and start it up if there is currently no
175 * active batch and the batch to be registered has not already occurred.
176 * Caller must hold rcu_state.lock.
178 static void rcu_start_batch(struct rcu_ctrlblk *rcp, struct rcu_state *rsp,
179 int next_pending)
181 if (next_pending)
182 rcp->next_pending = 1;
184 if (rcp->next_pending &&
185 rcp->completed == rcp->cur) {
186 /* Can't change, since spin lock held. */
187 cpus_andnot(rsp->cpumask, cpu_online_map, nohz_cpu_mask);
189 rcp->next_pending = 0;
190 /* next_pending == 0 must be visible in __rcu_process_callbacks()
191 * before it can see new value of cur.
193 smp_wmb();
194 rcp->cur++;
199 * cpu went through a quiescent state since the beginning of the grace period.
200 * Clear it from the cpu mask and complete the grace period if it was the last
201 * cpu. Start another grace period if someone has further entries pending
203 static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp, struct rcu_state *rsp)
205 cpu_clear(cpu, rsp->cpumask);
206 if (cpus_empty(rsp->cpumask)) {
207 /* batch completed ! */
208 rcp->completed = rcp->cur;
209 rcu_start_batch(rcp, rsp, 0);
214 * Check if the cpu has gone through a quiescent state (say context
215 * switch). If so and if it already hasn't done so in this RCU
216 * quiescent cycle, then indicate that it has done so.
218 static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
219 struct rcu_state *rsp, struct rcu_data *rdp)
221 if (rdp->quiescbatch != rcp->cur) {
222 /* start new grace period: */
223 rdp->qs_pending = 1;
224 rdp->passed_quiesc = 0;
225 rdp->quiescbatch = rcp->cur;
226 return;
229 /* Grace period already completed for this cpu?
230 * qs_pending is checked instead of the actual bitmap to avoid
231 * cacheline trashing.
233 if (!rdp->qs_pending)
234 return;
237 * Was there a quiescent state since the beginning of the grace
238 * period? If no, then exit and wait for the next call.
240 if (!rdp->passed_quiesc)
241 return;
242 rdp->qs_pending = 0;
244 spin_lock(&rsp->lock);
246 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
247 * during cpu startup. Ignore the quiescent state.
249 if (likely(rdp->quiescbatch == rcp->cur))
250 cpu_quiet(rdp->cpu, rcp, rsp);
252 spin_unlock(&rsp->lock);
256 #ifdef CONFIG_HOTPLUG_CPU
258 /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
259 * locking requirements, the list it's pulling from has to belong to a cpu
260 * which is dead and hence not processing interrupts.
262 static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
263 struct rcu_head **tail)
265 local_irq_disable();
266 *this_rdp->nxttail = list;
267 if (list)
268 this_rdp->nxttail = tail;
269 local_irq_enable();
272 static void __rcu_offline_cpu(struct rcu_data *this_rdp,
273 struct rcu_ctrlblk *rcp, struct rcu_state *rsp, struct rcu_data *rdp)
275 /* if the cpu going offline owns the grace period
276 * we can block indefinitely waiting for it, so flush
277 * it here
279 spin_lock_bh(&rsp->lock);
280 if (rcp->cur != rcp->completed)
281 cpu_quiet(rdp->cpu, rcp, rsp);
282 spin_unlock_bh(&rsp->lock);
283 rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
284 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
287 static void rcu_offline_cpu(int cpu)
289 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
290 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
292 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk, &rcu_state,
293 &per_cpu(rcu_data, cpu));
294 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk, &rcu_bh_state,
295 &per_cpu(rcu_bh_data, cpu));
296 put_cpu_var(rcu_data);
297 put_cpu_var(rcu_bh_data);
298 tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu);
301 #else
303 static void rcu_offline_cpu(int cpu)
307 #endif
310 * This does the RCU processing work from tasklet context.
312 static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
313 struct rcu_state *rsp, struct rcu_data *rdp)
315 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
316 *rdp->donetail = rdp->curlist;
317 rdp->donetail = rdp->curtail;
318 rdp->curlist = NULL;
319 rdp->curtail = &rdp->curlist;
322 local_irq_disable();
323 if (rdp->nxtlist && !rdp->curlist) {
324 rdp->curlist = rdp->nxtlist;
325 rdp->curtail = rdp->nxttail;
326 rdp->nxtlist = NULL;
327 rdp->nxttail = &rdp->nxtlist;
328 local_irq_enable();
331 * start the next batch of callbacks
334 /* determine batch number */
335 rdp->batch = rcp->cur + 1;
336 /* see the comment and corresponding wmb() in
337 * the rcu_start_batch()
339 smp_rmb();
341 if (!rcp->next_pending) {
342 /* and start it/schedule start if it's a new batch */
343 spin_lock(&rsp->lock);
344 rcu_start_batch(rcp, rsp, 1);
345 spin_unlock(&rsp->lock);
347 } else {
348 local_irq_enable();
350 rcu_check_quiescent_state(rcp, rsp, rdp);
351 if (rdp->donelist)
352 rcu_do_batch(rdp);
355 static void rcu_process_callbacks(unsigned long unused)
357 __rcu_process_callbacks(&rcu_ctrlblk, &rcu_state,
358 &__get_cpu_var(rcu_data));
359 __rcu_process_callbacks(&rcu_bh_ctrlblk, &rcu_bh_state,
360 &__get_cpu_var(rcu_bh_data));
363 void rcu_check_callbacks(int cpu, int user)
365 if (user ||
366 (idle_cpu(cpu) && !in_softirq() &&
367 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
368 rcu_qsctr_inc(cpu);
369 rcu_bh_qsctr_inc(cpu);
370 } else if (!in_softirq())
371 rcu_bh_qsctr_inc(cpu);
372 tasklet_schedule(&per_cpu(rcu_tasklet, cpu));
375 static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
376 struct rcu_data *rdp)
378 memset(rdp, 0, sizeof(*rdp));
379 rdp->curtail = &rdp->curlist;
380 rdp->nxttail = &rdp->nxtlist;
381 rdp->donetail = &rdp->donelist;
382 rdp->quiescbatch = rcp->completed;
383 rdp->qs_pending = 0;
384 rdp->cpu = cpu;
387 static void __devinit rcu_online_cpu(int cpu)
389 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
390 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
392 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
393 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
394 tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL);
397 static int __devinit rcu_cpu_notify(struct notifier_block *self,
398 unsigned long action, void *hcpu)
400 long cpu = (long)hcpu;
401 switch (action) {
402 case CPU_UP_PREPARE:
403 rcu_online_cpu(cpu);
404 break;
405 case CPU_DEAD:
406 rcu_offline_cpu(cpu);
407 break;
408 default:
409 break;
411 return NOTIFY_OK;
414 static struct notifier_block __devinitdata rcu_nb = {
415 .notifier_call = rcu_cpu_notify,
419 * Initializes rcu mechanism. Assumed to be called early.
420 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
421 * Note that rcu_qsctr and friends are implicitly
422 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
424 void __init rcu_init(void)
426 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
427 (void *)(long)smp_processor_id());
428 /* Register notifier for non-boot CPUs */
429 register_cpu_notifier(&rcu_nb);
432 struct rcu_synchronize {
433 struct rcu_head head;
434 struct completion completion;
437 /* Because of FASTCALL declaration of complete, we use this wrapper */
438 static void wakeme_after_rcu(struct rcu_head *head)
440 struct rcu_synchronize *rcu;
442 rcu = container_of(head, struct rcu_synchronize, head);
443 complete(&rcu->completion);
447 * synchronize_kernel - wait until a grace period has elapsed.
449 * Control will return to the caller some time after a full grace
450 * period has elapsed, in other words after all currently executing RCU
451 * read-side critical sections have completed. RCU read-side critical
452 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
453 * and may be nested.
455 void synchronize_kernel(void)
457 struct rcu_synchronize rcu;
459 init_completion(&rcu.completion);
460 /* Will wake me after RCU finished */
461 call_rcu(&rcu.head, wakeme_after_rcu);
463 /* Wait for it */
464 wait_for_completion(&rcu.completion);
467 module_param(maxbatch, int, 0);
468 EXPORT_SYMBOL_GPL(call_rcu);
469 EXPORT_SYMBOL_GPL(call_rcu_bh);
470 EXPORT_SYMBOL_GPL(synchronize_kernel);