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/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 */
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
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(),
100 void fastcall
call_rcu(struct rcu_head
*head
,
101 void (*func
)(struct rcu_head
*rcu
))
104 struct rcu_data
*rdp
;
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))
116 local_irq_restore(flags
);
119 static atomic_t rcu_barrier_cpu_count
;
120 static struct semaphore rcu_barrier_sema
;
121 static struct completion rcu_barrier_completion
;
124 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
125 * @head: structure to be used for queueing the RCU updates.
126 * @func: actual update function to be invoked after the grace period
128 * The update function will be invoked some time after a full grace
129 * period elapses, in other words after all currently executing RCU
130 * read-side critical sections have completed. call_rcu_bh() assumes
131 * that the read-side critical sections end on completion of a softirq
132 * handler. This means that read-side critical sections in process
133 * context must not be interrupted by softirqs. This interface is to be
134 * used when most of the read-side critical sections are in softirq context.
135 * RCU read-side critical sections are delimited by rcu_read_lock() and
136 * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
137 * and rcu_read_unlock_bh(), if in process context. These may be nested.
139 void fastcall
call_rcu_bh(struct rcu_head
*head
,
140 void (*func
)(struct rcu_head
*rcu
))
143 struct rcu_data
*rdp
;
147 local_irq_save(flags
);
148 rdp
= &__get_cpu_var(rcu_bh_data
);
149 *rdp
->nxttail
= head
;
150 rdp
->nxttail
= &head
->next
;
153 * Should we directly call rcu_do_batch() here ?
154 * if (unlikely(rdp->count > 10000))
157 local_irq_restore(flags
);
161 * Return the number of RCU batches processed thus far. Useful
162 * for debug and statistics.
164 long rcu_batches_completed(void)
166 return rcu_ctrlblk
.completed
;
169 static void rcu_barrier_callback(struct rcu_head
*notused
)
171 if (atomic_dec_and_test(&rcu_barrier_cpu_count
))
172 complete(&rcu_barrier_completion
);
176 * Called with preemption disabled, and from cross-cpu IRQ context.
178 static void rcu_barrier_func(void *notused
)
180 int cpu
= smp_processor_id();
181 struct rcu_data
*rdp
= &per_cpu(rcu_data
, cpu
);
182 struct rcu_head
*head
;
184 head
= &rdp
->barrier
;
185 atomic_inc(&rcu_barrier_cpu_count
);
186 call_rcu(head
, rcu_barrier_callback
);
190 * rcu_barrier - Wait until all the in-flight RCUs are complete.
192 void rcu_barrier(void)
194 BUG_ON(in_interrupt());
195 /* Take cpucontrol semaphore to protect against CPU hotplug */
196 down(&rcu_barrier_sema
);
197 init_completion(&rcu_barrier_completion
);
198 atomic_set(&rcu_barrier_cpu_count
, 0);
199 on_each_cpu(rcu_barrier_func
, NULL
, 0, 1);
200 wait_for_completion(&rcu_barrier_completion
);
201 up(&rcu_barrier_sema
);
203 EXPORT_SYMBOL_GPL(rcu_barrier
);
206 * Invoke the completed RCU callbacks. They are expected to be in
209 static void rcu_do_batch(struct rcu_data
*rdp
)
211 struct rcu_head
*next
, *list
;
214 list
= rdp
->donelist
;
216 next
= rdp
->donelist
= list
->next
;
220 if (++count
>= maxbatch
)
224 rdp
->donetail
= &rdp
->donelist
;
226 tasklet_schedule(&per_cpu(rcu_tasklet
, rdp
->cpu
));
230 * Grace period handling:
231 * The grace period handling consists out of two steps:
232 * - A new grace period is started.
233 * This is done by rcu_start_batch. The start is not broadcasted to
234 * all cpus, they must pick this up by comparing rcp->cur with
235 * rdp->quiescbatch. All cpus are recorded in the
236 * rcu_state.cpumask bitmap.
237 * - All cpus must go through a quiescent state.
238 * Since the start of the grace period is not broadcasted, at least two
239 * calls to rcu_check_quiescent_state are required:
240 * The first call just notices that a new grace period is running. The
241 * following calls check if there was a quiescent state since the beginning
242 * of the grace period. If so, it updates rcu_state.cpumask. If
243 * the bitmap is empty, then the grace period is completed.
244 * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
245 * period (if necessary).
248 * Register a new batch of callbacks, and start it up if there is currently no
249 * active batch and the batch to be registered has not already occurred.
250 * Caller must hold rcu_state.lock.
252 static void rcu_start_batch(struct rcu_ctrlblk
*rcp
, struct rcu_state
*rsp
,
256 rcp
->next_pending
= 1;
258 if (rcp
->next_pending
&&
259 rcp
->completed
== rcp
->cur
) {
260 /* Can't change, since spin lock held. */
261 cpus_andnot(rsp
->cpumask
, cpu_online_map
, nohz_cpu_mask
);
263 rcp
->next_pending
= 0;
264 /* next_pending == 0 must be visible in __rcu_process_callbacks()
265 * before it can see new value of cur.
273 * cpu went through a quiescent state since the beginning of the grace period.
274 * Clear it from the cpu mask and complete the grace period if it was the last
275 * cpu. Start another grace period if someone has further entries pending
277 static void cpu_quiet(int cpu
, struct rcu_ctrlblk
*rcp
, struct rcu_state
*rsp
)
279 cpu_clear(cpu
, rsp
->cpumask
);
280 if (cpus_empty(rsp
->cpumask
)) {
281 /* batch completed ! */
282 rcp
->completed
= rcp
->cur
;
283 rcu_start_batch(rcp
, rsp
, 0);
288 * Check if the cpu has gone through a quiescent state (say context
289 * switch). If so and if it already hasn't done so in this RCU
290 * quiescent cycle, then indicate that it has done so.
292 static void rcu_check_quiescent_state(struct rcu_ctrlblk
*rcp
,
293 struct rcu_state
*rsp
, struct rcu_data
*rdp
)
295 if (rdp
->quiescbatch
!= rcp
->cur
) {
296 /* start new grace period: */
298 rdp
->passed_quiesc
= 0;
299 rdp
->quiescbatch
= rcp
->cur
;
303 /* Grace period already completed for this cpu?
304 * qs_pending is checked instead of the actual bitmap to avoid
305 * cacheline trashing.
307 if (!rdp
->qs_pending
)
311 * Was there a quiescent state since the beginning of the grace
312 * period? If no, then exit and wait for the next call.
314 if (!rdp
->passed_quiesc
)
318 spin_lock(&rsp
->lock
);
320 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
321 * during cpu startup. Ignore the quiescent state.
323 if (likely(rdp
->quiescbatch
== rcp
->cur
))
324 cpu_quiet(rdp
->cpu
, rcp
, rsp
);
326 spin_unlock(&rsp
->lock
);
330 #ifdef CONFIG_HOTPLUG_CPU
332 /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
333 * locking requirements, the list it's pulling from has to belong to a cpu
334 * which is dead and hence not processing interrupts.
336 static void rcu_move_batch(struct rcu_data
*this_rdp
, struct rcu_head
*list
,
337 struct rcu_head
**tail
)
340 *this_rdp
->nxttail
= list
;
342 this_rdp
->nxttail
= tail
;
346 static void __rcu_offline_cpu(struct rcu_data
*this_rdp
,
347 struct rcu_ctrlblk
*rcp
, struct rcu_state
*rsp
, struct rcu_data
*rdp
)
349 /* if the cpu going offline owns the grace period
350 * we can block indefinitely waiting for it, so flush
353 spin_lock_bh(&rsp
->lock
);
354 if (rcp
->cur
!= rcp
->completed
)
355 cpu_quiet(rdp
->cpu
, rcp
, rsp
);
356 spin_unlock_bh(&rsp
->lock
);
357 rcu_move_batch(this_rdp
, rdp
->curlist
, rdp
->curtail
);
358 rcu_move_batch(this_rdp
, rdp
->nxtlist
, rdp
->nxttail
);
361 static void rcu_offline_cpu(int cpu
)
363 struct rcu_data
*this_rdp
= &get_cpu_var(rcu_data
);
364 struct rcu_data
*this_bh_rdp
= &get_cpu_var(rcu_bh_data
);
366 __rcu_offline_cpu(this_rdp
, &rcu_ctrlblk
, &rcu_state
,
367 &per_cpu(rcu_data
, cpu
));
368 __rcu_offline_cpu(this_bh_rdp
, &rcu_bh_ctrlblk
, &rcu_bh_state
,
369 &per_cpu(rcu_bh_data
, cpu
));
370 put_cpu_var(rcu_data
);
371 put_cpu_var(rcu_bh_data
);
372 tasklet_kill_immediate(&per_cpu(rcu_tasklet
, cpu
), cpu
);
377 static void rcu_offline_cpu(int cpu
)
384 * This does the RCU processing work from tasklet context.
386 static void __rcu_process_callbacks(struct rcu_ctrlblk
*rcp
,
387 struct rcu_state
*rsp
, struct rcu_data
*rdp
)
389 if (rdp
->curlist
&& !rcu_batch_before(rcp
->completed
, rdp
->batch
)) {
390 *rdp
->donetail
= rdp
->curlist
;
391 rdp
->donetail
= rdp
->curtail
;
393 rdp
->curtail
= &rdp
->curlist
;
397 if (rdp
->nxtlist
&& !rdp
->curlist
) {
398 rdp
->curlist
= rdp
->nxtlist
;
399 rdp
->curtail
= rdp
->nxttail
;
401 rdp
->nxttail
= &rdp
->nxtlist
;
405 * start the next batch of callbacks
408 /* determine batch number */
409 rdp
->batch
= rcp
->cur
+ 1;
410 /* see the comment and corresponding wmb() in
411 * the rcu_start_batch()
415 if (!rcp
->next_pending
) {
416 /* and start it/schedule start if it's a new batch */
417 spin_lock(&rsp
->lock
);
418 rcu_start_batch(rcp
, rsp
, 1);
419 spin_unlock(&rsp
->lock
);
424 rcu_check_quiescent_state(rcp
, rsp
, rdp
);
429 static void rcu_process_callbacks(unsigned long unused
)
431 __rcu_process_callbacks(&rcu_ctrlblk
, &rcu_state
,
432 &__get_cpu_var(rcu_data
));
433 __rcu_process_callbacks(&rcu_bh_ctrlblk
, &rcu_bh_state
,
434 &__get_cpu_var(rcu_bh_data
));
437 void rcu_check_callbacks(int cpu
, int user
)
440 (idle_cpu(cpu
) && !in_softirq() &&
441 hardirq_count() <= (1 << HARDIRQ_SHIFT
))) {
443 rcu_bh_qsctr_inc(cpu
);
444 } else if (!in_softirq())
445 rcu_bh_qsctr_inc(cpu
);
446 tasklet_schedule(&per_cpu(rcu_tasklet
, cpu
));
449 static void rcu_init_percpu_data(int cpu
, struct rcu_ctrlblk
*rcp
,
450 struct rcu_data
*rdp
)
452 memset(rdp
, 0, sizeof(*rdp
));
453 rdp
->curtail
= &rdp
->curlist
;
454 rdp
->nxttail
= &rdp
->nxtlist
;
455 rdp
->donetail
= &rdp
->donelist
;
456 rdp
->quiescbatch
= rcp
->completed
;
461 static void __devinit
rcu_online_cpu(int cpu
)
463 struct rcu_data
*rdp
= &per_cpu(rcu_data
, cpu
);
464 struct rcu_data
*bh_rdp
= &per_cpu(rcu_bh_data
, cpu
);
466 rcu_init_percpu_data(cpu
, &rcu_ctrlblk
, rdp
);
467 rcu_init_percpu_data(cpu
, &rcu_bh_ctrlblk
, bh_rdp
);
468 tasklet_init(&per_cpu(rcu_tasklet
, cpu
), rcu_process_callbacks
, 0UL);
471 static int __devinit
rcu_cpu_notify(struct notifier_block
*self
,
472 unsigned long action
, void *hcpu
)
474 long cpu
= (long)hcpu
;
480 rcu_offline_cpu(cpu
);
488 static struct notifier_block __devinitdata rcu_nb
= {
489 .notifier_call
= rcu_cpu_notify
,
493 * Initializes rcu mechanism. Assumed to be called early.
494 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
495 * Note that rcu_qsctr and friends are implicitly
496 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
498 void __init
rcu_init(void)
500 sema_init(&rcu_barrier_sema
, 1);
501 rcu_cpu_notify(&rcu_nb
, CPU_UP_PREPARE
,
502 (void *)(long)smp_processor_id());
503 /* Register notifier for non-boot CPUs */
504 register_cpu_notifier(&rcu_nb
);
507 struct rcu_synchronize
{
508 struct rcu_head head
;
509 struct completion completion
;
512 /* Because of FASTCALL declaration of complete, we use this wrapper */
513 static void wakeme_after_rcu(struct rcu_head
*head
)
515 struct rcu_synchronize
*rcu
;
517 rcu
= container_of(head
, struct rcu_synchronize
, head
);
518 complete(&rcu
->completion
);
522 * synchronize_rcu - wait until a grace period has elapsed.
524 * Control will return to the caller some time after a full grace
525 * period has elapsed, in other words after all currently executing RCU
526 * read-side critical sections have completed. RCU read-side critical
527 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
530 * If your read-side code is not protected by rcu_read_lock(), do -not-
531 * use synchronize_rcu().
533 void synchronize_rcu(void)
535 struct rcu_synchronize rcu
;
537 init_completion(&rcu
.completion
);
538 /* Will wake me after RCU finished */
539 call_rcu(&rcu
.head
, wakeme_after_rcu
);
542 wait_for_completion(&rcu
.completion
);
546 * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
548 void synchronize_kernel(void)
553 module_param(maxbatch
, int, 0);
554 EXPORT_SYMBOL_GPL(rcu_batches_completed
);
555 EXPORT_SYMBOL(call_rcu
); /* WARNING: GPL-only in April 2006. */
556 EXPORT_SYMBOL(call_rcu_bh
); /* WARNING: GPL-only in April 2006. */
557 EXPORT_SYMBOL_GPL(synchronize_rcu
);
558 EXPORT_SYMBOL(synchronize_kernel
); /* WARNING: GPL-only in April 2006. */