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.
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/rcupdate.h>
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>
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 */
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_internodealigned_in_smp
=
65 {.lock
= SPIN_LOCK_UNLOCKED
, .cpumask
= CPU_MASK_NONE
};
66 static struct rcu_state rcu_bh_state ____cacheline_internodealigned_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;
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
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(),
87 void fastcall
call_rcu(struct rcu_head
*head
,
88 void (*func
)(struct rcu_head
*rcu
))
95 local_irq_save(flags
);
96 rdp
= &__get_cpu_var(rcu_data
);
98 rdp
->nxttail
= &head
->next
;
100 if (unlikely(++rdp
->count
> 10000))
103 local_irq_restore(flags
);
106 static atomic_t rcu_barrier_cpu_count
;
107 static struct semaphore rcu_barrier_sema
;
108 static struct completion rcu_barrier_completion
;
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
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.
126 void fastcall
call_rcu_bh(struct rcu_head
*head
,
127 void (*func
)(struct rcu_head
*rcu
))
130 struct rcu_data
*rdp
;
134 local_irq_save(flags
);
135 rdp
= &__get_cpu_var(rcu_bh_data
);
136 *rdp
->nxttail
= head
;
137 rdp
->nxttail
= &head
->next
;
140 * Should we directly call rcu_do_batch() here ?
141 * if (unlikely(rdp->count > 10000))
144 local_irq_restore(flags
);
148 * Return the number of RCU batches processed thus far. Useful
149 * for debug and statistics.
151 long rcu_batches_completed(void)
153 return rcu_ctrlblk
.completed
;
156 static void rcu_barrier_callback(struct rcu_head
*notused
)
158 if (atomic_dec_and_test(&rcu_barrier_cpu_count
))
159 complete(&rcu_barrier_completion
);
163 * Called with preemption disabled, and from cross-cpu IRQ context.
165 static void rcu_barrier_func(void *notused
)
167 int cpu
= smp_processor_id();
168 struct rcu_data
*rdp
= &per_cpu(rcu_data
, cpu
);
169 struct rcu_head
*head
;
171 head
= &rdp
->barrier
;
172 atomic_inc(&rcu_barrier_cpu_count
);
173 call_rcu(head
, rcu_barrier_callback
);
177 * rcu_barrier - Wait until all the in-flight RCUs are complete.
179 void rcu_barrier(void)
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
);
190 EXPORT_SYMBOL_GPL(rcu_barrier
);
193 * Invoke the completed RCU callbacks. They are expected to be in
196 static void rcu_do_batch(struct rcu_data
*rdp
)
198 struct rcu_head
*next
, *list
;
201 list
= rdp
->donelist
;
203 next
= rdp
->donelist
= list
->next
;
207 if (++count
>= maxbatch
)
211 rdp
->donetail
= &rdp
->donelist
;
213 tasklet_schedule(&per_cpu(rcu_tasklet
, rdp
->cpu
));
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).
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.
239 static void rcu_start_batch(struct rcu_ctrlblk
*rcp
, struct rcu_state
*rsp
,
243 rcp
->next_pending
= 1;
245 if (rcp
->next_pending
&&
246 rcp
->completed
== rcp
->cur
) {
247 rcp
->next_pending
= 0;
249 * next_pending == 0 must be visible in
250 * __rcu_process_callbacks() before it can see new value of cur.
256 * Accessing nohz_cpu_mask before incrementing rcp->cur needs a
257 * Barrier Otherwise it can cause tickless idle CPUs to be
258 * included in rsp->cpumask, which will extend graceperiods
262 cpus_andnot(rsp
->cpumask
, cpu_online_map
, nohz_cpu_mask
);
268 * cpu went through a quiescent state since the beginning of the grace period.
269 * Clear it from the cpu mask and complete the grace period if it was the last
270 * cpu. Start another grace period if someone has further entries pending
272 static void cpu_quiet(int cpu
, struct rcu_ctrlblk
*rcp
, struct rcu_state
*rsp
)
274 cpu_clear(cpu
, rsp
->cpumask
);
275 if (cpus_empty(rsp
->cpumask
)) {
276 /* batch completed ! */
277 rcp
->completed
= rcp
->cur
;
278 rcu_start_batch(rcp
, rsp
, 0);
283 * Check if the cpu has gone through a quiescent state (say context
284 * switch). If so and if it already hasn't done so in this RCU
285 * quiescent cycle, then indicate that it has done so.
287 static void rcu_check_quiescent_state(struct rcu_ctrlblk
*rcp
,
288 struct rcu_state
*rsp
, struct rcu_data
*rdp
)
290 if (rdp
->quiescbatch
!= rcp
->cur
) {
291 /* start new grace period: */
293 rdp
->passed_quiesc
= 0;
294 rdp
->quiescbatch
= rcp
->cur
;
298 /* Grace period already completed for this cpu?
299 * qs_pending is checked instead of the actual bitmap to avoid
300 * cacheline trashing.
302 if (!rdp
->qs_pending
)
306 * Was there a quiescent state since the beginning of the grace
307 * period? If no, then exit and wait for the next call.
309 if (!rdp
->passed_quiesc
)
313 spin_lock(&rsp
->lock
);
315 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
316 * during cpu startup. Ignore the quiescent state.
318 if (likely(rdp
->quiescbatch
== rcp
->cur
))
319 cpu_quiet(rdp
->cpu
, rcp
, rsp
);
321 spin_unlock(&rsp
->lock
);
325 #ifdef CONFIG_HOTPLUG_CPU
327 /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
328 * locking requirements, the list it's pulling from has to belong to a cpu
329 * which is dead and hence not processing interrupts.
331 static void rcu_move_batch(struct rcu_data
*this_rdp
, struct rcu_head
*list
,
332 struct rcu_head
**tail
)
335 *this_rdp
->nxttail
= list
;
337 this_rdp
->nxttail
= tail
;
341 static void __rcu_offline_cpu(struct rcu_data
*this_rdp
,
342 struct rcu_ctrlblk
*rcp
, struct rcu_state
*rsp
, struct rcu_data
*rdp
)
344 /* if the cpu going offline owns the grace period
345 * we can block indefinitely waiting for it, so flush
348 spin_lock_bh(&rsp
->lock
);
349 if (rcp
->cur
!= rcp
->completed
)
350 cpu_quiet(rdp
->cpu
, rcp
, rsp
);
351 spin_unlock_bh(&rsp
->lock
);
352 rcu_move_batch(this_rdp
, rdp
->curlist
, rdp
->curtail
);
353 rcu_move_batch(this_rdp
, rdp
->nxtlist
, rdp
->nxttail
);
356 static void rcu_offline_cpu(int cpu
)
358 struct rcu_data
*this_rdp
= &get_cpu_var(rcu_data
);
359 struct rcu_data
*this_bh_rdp
= &get_cpu_var(rcu_bh_data
);
361 __rcu_offline_cpu(this_rdp
, &rcu_ctrlblk
, &rcu_state
,
362 &per_cpu(rcu_data
, cpu
));
363 __rcu_offline_cpu(this_bh_rdp
, &rcu_bh_ctrlblk
, &rcu_bh_state
,
364 &per_cpu(rcu_bh_data
, cpu
));
365 put_cpu_var(rcu_data
);
366 put_cpu_var(rcu_bh_data
);
367 tasklet_kill_immediate(&per_cpu(rcu_tasklet
, cpu
), cpu
);
372 static void rcu_offline_cpu(int cpu
)
379 * This does the RCU processing work from tasklet context.
381 static void __rcu_process_callbacks(struct rcu_ctrlblk
*rcp
,
382 struct rcu_state
*rsp
, struct rcu_data
*rdp
)
384 if (rdp
->curlist
&& !rcu_batch_before(rcp
->completed
, rdp
->batch
)) {
385 *rdp
->donetail
= rdp
->curlist
;
386 rdp
->donetail
= rdp
->curtail
;
388 rdp
->curtail
= &rdp
->curlist
;
392 if (rdp
->nxtlist
&& !rdp
->curlist
) {
393 rdp
->curlist
= rdp
->nxtlist
;
394 rdp
->curtail
= rdp
->nxttail
;
396 rdp
->nxttail
= &rdp
->nxtlist
;
400 * start the next batch of callbacks
403 /* determine batch number */
404 rdp
->batch
= rcp
->cur
+ 1;
405 /* see the comment and corresponding wmb() in
406 * the rcu_start_batch()
410 if (!rcp
->next_pending
) {
411 /* and start it/schedule start if it's a new batch */
412 spin_lock(&rsp
->lock
);
413 rcu_start_batch(rcp
, rsp
, 1);
414 spin_unlock(&rsp
->lock
);
419 rcu_check_quiescent_state(rcp
, rsp
, rdp
);
424 static void rcu_process_callbacks(unsigned long unused
)
426 __rcu_process_callbacks(&rcu_ctrlblk
, &rcu_state
,
427 &__get_cpu_var(rcu_data
));
428 __rcu_process_callbacks(&rcu_bh_ctrlblk
, &rcu_bh_state
,
429 &__get_cpu_var(rcu_bh_data
));
432 static int __rcu_pending(struct rcu_ctrlblk
*rcp
, struct rcu_data
*rdp
)
434 /* This cpu has pending rcu entries and the grace period
435 * for them has completed.
437 if (rdp
->curlist
&& !rcu_batch_before(rcp
->completed
, rdp
->batch
))
440 /* This cpu has no pending entries, but there are new entries */
441 if (!rdp
->curlist
&& rdp
->nxtlist
)
444 /* This cpu has finished callbacks to invoke */
448 /* The rcu core waits for a quiescent state from the cpu */
449 if (rdp
->quiescbatch
!= rcp
->cur
|| rdp
->qs_pending
)
456 int rcu_pending(int cpu
)
458 return __rcu_pending(&rcu_ctrlblk
, &per_cpu(rcu_data
, cpu
)) ||
459 __rcu_pending(&rcu_bh_ctrlblk
, &per_cpu(rcu_bh_data
, cpu
));
462 void rcu_check_callbacks(int cpu
, int user
)
465 (idle_cpu(cpu
) && !in_softirq() &&
466 hardirq_count() <= (1 << HARDIRQ_SHIFT
))) {
468 rcu_bh_qsctr_inc(cpu
);
469 } else if (!in_softirq())
470 rcu_bh_qsctr_inc(cpu
);
471 tasklet_schedule(&per_cpu(rcu_tasklet
, cpu
));
474 static void rcu_init_percpu_data(int cpu
, struct rcu_ctrlblk
*rcp
,
475 struct rcu_data
*rdp
)
477 memset(rdp
, 0, sizeof(*rdp
));
478 rdp
->curtail
= &rdp
->curlist
;
479 rdp
->nxttail
= &rdp
->nxtlist
;
480 rdp
->donetail
= &rdp
->donelist
;
481 rdp
->quiescbatch
= rcp
->completed
;
486 static void __devinit
rcu_online_cpu(int cpu
)
488 struct rcu_data
*rdp
= &per_cpu(rcu_data
, cpu
);
489 struct rcu_data
*bh_rdp
= &per_cpu(rcu_bh_data
, cpu
);
491 rcu_init_percpu_data(cpu
, &rcu_ctrlblk
, rdp
);
492 rcu_init_percpu_data(cpu
, &rcu_bh_ctrlblk
, bh_rdp
);
493 tasklet_init(&per_cpu(rcu_tasklet
, cpu
), rcu_process_callbacks
, 0UL);
496 static int __devinit
rcu_cpu_notify(struct notifier_block
*self
,
497 unsigned long action
, void *hcpu
)
499 long cpu
= (long)hcpu
;
505 rcu_offline_cpu(cpu
);
513 static struct notifier_block __devinitdata rcu_nb
= {
514 .notifier_call
= rcu_cpu_notify
,
518 * Initializes rcu mechanism. Assumed to be called early.
519 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
520 * Note that rcu_qsctr and friends are implicitly
521 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
523 void __init
rcu_init(void)
525 sema_init(&rcu_barrier_sema
, 1);
526 rcu_cpu_notify(&rcu_nb
, CPU_UP_PREPARE
,
527 (void *)(long)smp_processor_id());
528 /* Register notifier for non-boot CPUs */
529 register_cpu_notifier(&rcu_nb
);
532 struct rcu_synchronize
{
533 struct rcu_head head
;
534 struct completion completion
;
537 /* Because of FASTCALL declaration of complete, we use this wrapper */
538 static void wakeme_after_rcu(struct rcu_head
*head
)
540 struct rcu_synchronize
*rcu
;
542 rcu
= container_of(head
, struct rcu_synchronize
, head
);
543 complete(&rcu
->completion
);
547 * synchronize_rcu - wait until a grace period has elapsed.
549 * Control will return to the caller some time after a full grace
550 * period has elapsed, in other words after all currently executing RCU
551 * read-side critical sections have completed. RCU read-side critical
552 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
555 * If your read-side code is not protected by rcu_read_lock(), do -not-
556 * use synchronize_rcu().
558 void synchronize_rcu(void)
560 struct rcu_synchronize rcu
;
562 init_completion(&rcu
.completion
);
563 /* Will wake me after RCU finished */
564 call_rcu(&rcu
.head
, wakeme_after_rcu
);
567 wait_for_completion(&rcu
.completion
);
571 * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
573 void synchronize_kernel(void)
578 module_param(maxbatch
, int, 0);
579 EXPORT_SYMBOL_GPL(rcu_batches_completed
);
580 EXPORT_SYMBOL(call_rcu
); /* WARNING: GPL-only in April 2006. */
581 EXPORT_SYMBOL(call_rcu_bh
); /* WARNING: GPL-only in April 2006. */
582 EXPORT_SYMBOL_GPL(synchronize_rcu
);
583 EXPORT_SYMBOL(synchronize_kernel
); /* WARNING: GPL-only in April 2006. */