[PATCH] uml: avoid malloc to sleep in atomic sections
[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);
a9c82815 346 rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail);
1da177e4 347}
a9c82815 348
1da177e4
LT
349static void rcu_offline_cpu(int cpu)
350{
351 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
352 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
353
69a0b315 354 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk,
1da177e4 355 &per_cpu(rcu_data, cpu));
69a0b315 356 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk,
1da177e4
LT
357 &per_cpu(rcu_bh_data, cpu));
358 put_cpu_var(rcu_data);
359 put_cpu_var(rcu_bh_data);
360 tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu);
361}
362
363#else
364
365static void rcu_offline_cpu(int cpu)
366{
367}
368
369#endif
370
371/*
372 * This does the RCU processing work from tasklet context.
373 */
374static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
69a0b315 375 struct rcu_data *rdp)
1da177e4
LT
376{
377 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
378 *rdp->donetail = rdp->curlist;
379 rdp->donetail = rdp->curtail;
380 rdp->curlist = NULL;
381 rdp->curtail = &rdp->curlist;
382 }
383
384 local_irq_disable();
385 if (rdp->nxtlist && !rdp->curlist) {
386 rdp->curlist = rdp->nxtlist;
387 rdp->curtail = rdp->nxttail;
388 rdp->nxtlist = NULL;
389 rdp->nxttail = &rdp->nxtlist;
390 local_irq_enable();
391
392 /*
393 * start the next batch of callbacks
394 */
395
396 /* determine batch number */
397 rdp->batch = rcp->cur + 1;
398 /* see the comment and corresponding wmb() in
399 * the rcu_start_batch()
400 */
401 smp_rmb();
402
403 if (!rcp->next_pending) {
404 /* and start it/schedule start if it's a new batch */
69a0b315 405 spin_lock(&rcp->lock);
dbc1651f 406 rcp->next_pending = 1;
69a0b315
ON
407 rcu_start_batch(rcp);
408 spin_unlock(&rcp->lock);
1da177e4
LT
409 }
410 } else {
411 local_irq_enable();
412 }
69a0b315 413 rcu_check_quiescent_state(rcp, rdp);
1da177e4
LT
414 if (rdp->donelist)
415 rcu_do_batch(rdp);
416}
417
418static void rcu_process_callbacks(unsigned long unused)
419{
69a0b315
ON
420 __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
421 __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
1da177e4
LT
422}
423
67751777
ON
424static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
425{
426 /* This cpu has pending rcu entries and the grace period
427 * for them has completed.
428 */
429 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch))
430 return 1;
431
432 /* This cpu has no pending entries, but there are new entries */
433 if (!rdp->curlist && rdp->nxtlist)
434 return 1;
435
436 /* This cpu has finished callbacks to invoke */
437 if (rdp->donelist)
438 return 1;
439
440 /* The rcu core waits for a quiescent state from the cpu */
441 if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
442 return 1;
443
444 /* nothing to do */
445 return 0;
446}
447
448int rcu_pending(int cpu)
449{
450 return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) ||
451 __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu));
452}
453
1da177e4
LT
454void rcu_check_callbacks(int cpu, int user)
455{
456 if (user ||
457 (idle_cpu(cpu) && !in_softirq() &&
458 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
459 rcu_qsctr_inc(cpu);
460 rcu_bh_qsctr_inc(cpu);
461 } else if (!in_softirq())
462 rcu_bh_qsctr_inc(cpu);
463 tasklet_schedule(&per_cpu(rcu_tasklet, cpu));
464}
465
466static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
467 struct rcu_data *rdp)
468{
469 memset(rdp, 0, sizeof(*rdp));
470 rdp->curtail = &rdp->curlist;
471 rdp->nxttail = &rdp->nxtlist;
472 rdp->donetail = &rdp->donelist;
473 rdp->quiescbatch = rcp->completed;
474 rdp->qs_pending = 0;
475 rdp->cpu = cpu;
476}
477
478static void __devinit rcu_online_cpu(int cpu)
479{
480 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
481 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
482
483 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
484 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
485 tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL);
486}
487
488static int __devinit rcu_cpu_notify(struct notifier_block *self,
489 unsigned long action, void *hcpu)
490{
491 long cpu = (long)hcpu;
492 switch (action) {
493 case CPU_UP_PREPARE:
494 rcu_online_cpu(cpu);
495 break;
496 case CPU_DEAD:
497 rcu_offline_cpu(cpu);
498 break;
499 default:
500 break;
501 }
502 return NOTIFY_OK;
503}
504
505static struct notifier_block __devinitdata rcu_nb = {
506 .notifier_call = rcu_cpu_notify,
507};
508
509/*
510 * Initializes rcu mechanism. Assumed to be called early.
511 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
512 * Note that rcu_qsctr and friends are implicitly
513 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
514 */
515void __init rcu_init(void)
516{
ab4720ec 517 sema_init(&rcu_barrier_sema, 1);
1da177e4
LT
518 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
519 (void *)(long)smp_processor_id());
520 /* Register notifier for non-boot CPUs */
521 register_cpu_notifier(&rcu_nb);
522}
523
524struct rcu_synchronize {
525 struct rcu_head head;
526 struct completion completion;
527};
528
529/* Because of FASTCALL declaration of complete, we use this wrapper */
530static void wakeme_after_rcu(struct rcu_head *head)
531{
532 struct rcu_synchronize *rcu;
533
534 rcu = container_of(head, struct rcu_synchronize, head);
535 complete(&rcu->completion);
536}
537
538/**
9b06e818 539 * synchronize_rcu - wait until a grace period has elapsed.
1da177e4
LT
540 *
541 * Control will return to the caller some time after a full grace
542 * period has elapsed, in other words after all currently executing RCU
543 * read-side critical sections have completed. RCU read-side critical
544 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
545 * and may be nested.
9b06e818
PM
546 *
547 * If your read-side code is not protected by rcu_read_lock(), do -not-
548 * use synchronize_rcu().
1da177e4 549 */
9b06e818 550void synchronize_rcu(void)
1da177e4
LT
551{
552 struct rcu_synchronize rcu;
553
554 init_completion(&rcu.completion);
555 /* Will wake me after RCU finished */
556 call_rcu(&rcu.head, wakeme_after_rcu);
557
558 /* Wait for it */
559 wait_for_completion(&rcu.completion);
560}
561
9b06e818
PM
562/*
563 * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
564 */
565void synchronize_kernel(void)
566{
567 synchronize_rcu();
568}
569
1da177e4 570module_param(maxbatch, int, 0);
a241ec65 571EXPORT_SYMBOL_GPL(rcu_batches_completed);
66cf8f14
PM
572EXPORT_SYMBOL(call_rcu); /* WARNING: GPL-only in April 2006. */
573EXPORT_SYMBOL(call_rcu_bh); /* WARNING: GPL-only in April 2006. */
9b06e818 574EXPORT_SYMBOL_GPL(synchronize_rcu);
66cf8f14 575EXPORT_SYMBOL(synchronize_kernel); /* WARNING: GPL-only in April 2006. */
This page took 0.121389 seconds and 5 git commands to generate.