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 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 -
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/cpu.h>
49 #include <linux/mutex.h>
50 #include <linux/time.h>
52 #ifdef CONFIG_DEBUG_LOCK_ALLOC
53 static struct lock_class_key rcu_lock_key;
54 struct lockdep_map rcu_lock_map =
55 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
56 EXPORT_SYMBOL_GPL(rcu_lock_map);
60 /* Definition for rcupdate control block. */
61 static struct rcu_ctrlblk rcu_ctrlblk = {
65 .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock),
66 .cpumask = CPU_MASK_NONE,
68 static struct rcu_ctrlblk rcu_bh_ctrlblk = {
72 .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock),
73 .cpumask = CPU_MASK_NONE,
76 DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
77 DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
79 static int blimit = 10;
80 static int qhimark = 10000;
81 static int qlowmark = 100;
84 static void force_quiescent_state(struct rcu_data *rdp,
85 struct rcu_ctrlblk *rcp)
92 spin_lock_irqsave(&rcp->lock, flags);
93 if (unlikely(!rcp->signaled)) {
96 * Don't send IPI to itself. With irqs disabled,
97 * rdp->cpu is the current cpu.
99 * cpu_online_map is updated by the _cpu_down()
100 * using __stop_machine(). Since we're in irqs disabled
101 * section, __stop_machine() is not exectuting, hence
102 * the cpu_online_map is stable.
104 * However, a cpu might have been offlined _just_ before
105 * we disabled irqs while entering here.
106 * And rcu subsystem might not yet have handled the CPU_DEAD
107 * notification, leading to the offlined cpu's bit
108 * being set in the rcp->cpumask.
110 * Hence cpumask = (rcp->cpumask & cpu_online_map) to prevent
111 * sending smp_reschedule() to an offlined CPU.
113 cpus_and(cpumask, rcp->cpumask, cpu_online_map);
114 cpu_clear(rdp->cpu, cpumask);
115 for_each_cpu_mask_nr(cpu, cpumask)
116 smp_send_reschedule(cpu);
118 spin_unlock_irqrestore(&rcp->lock, flags);
121 static inline void force_quiescent_state(struct rcu_data *rdp,
122 struct rcu_ctrlblk *rcp)
128 static void __call_rcu(struct rcu_head *head, struct rcu_ctrlblk *rcp,
129 struct rcu_data *rdp)
134 smp_mb(); /* Read of rcu->cur must happen after any change by caller. */
137 * Determine the batch number of this callback.
139 * Using ACCESS_ONCE to avoid the following error when gcc eliminates
140 * local variable "batch" and emits codes like this:
141 * 1) rdp->batch = rcp->cur + 1 # gets old value
143 * 2)rcu_batch_after(rcp->cur + 1, rdp->batch) # gets new value
144 * then [*nxttail[0], *nxttail[1]) may contain callbacks
145 * that batch# = rdp->batch, see the comment of struct rcu_data.
147 batch = ACCESS_ONCE(rcp->cur) + 1;
149 if (rdp->nxtlist && rcu_batch_after(batch, rdp->batch)) {
150 /* process callbacks */
151 rdp->nxttail[0] = rdp->nxttail[1];
152 rdp->nxttail[1] = rdp->nxttail[2];
153 if (rcu_batch_after(batch - 1, rdp->batch))
154 rdp->nxttail[0] = rdp->nxttail[2];
158 *rdp->nxttail[2] = head;
159 rdp->nxttail[2] = &head->next;
161 if (unlikely(++rdp->qlen > qhimark)) {
162 rdp->blimit = INT_MAX;
163 force_quiescent_state(rdp, &rcu_ctrlblk);
168 * call_rcu - Queue an RCU callback for invocation after a grace period.
169 * @head: structure to be used for queueing the RCU updates.
170 * @func: actual update function to be invoked after the grace period
172 * The update function will be invoked some time after a full grace
173 * period elapses, in other words after all currently executing RCU
174 * read-side critical sections have completed. RCU read-side critical
175 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
178 void call_rcu(struct rcu_head *head,
179 void (*func)(struct rcu_head *rcu))
184 local_irq_save(flags);
185 __call_rcu(head, &rcu_ctrlblk, &__get_cpu_var(rcu_data));
186 local_irq_restore(flags);
188 EXPORT_SYMBOL_GPL(call_rcu);
191 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
192 * @head: structure to be used for queueing the RCU updates.
193 * @func: actual update function to be invoked after the grace period
195 * The update function will be invoked some time after a full grace
196 * period elapses, in other words after all currently executing RCU
197 * read-side critical sections have completed. call_rcu_bh() assumes
198 * that the read-side critical sections end on completion of a softirq
199 * handler. This means that read-side critical sections in process
200 * context must not be interrupted by softirqs. This interface is to be
201 * used when most of the read-side critical sections are in softirq context.
202 * RCU read-side critical sections are delimited by rcu_read_lock() and
203 * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
204 * and rcu_read_unlock_bh(), if in process context. These may be nested.
206 void call_rcu_bh(struct rcu_head *head,
207 void (*func)(struct rcu_head *rcu))
212 local_irq_save(flags);
213 __call_rcu(head, &rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
214 local_irq_restore(flags);
216 EXPORT_SYMBOL_GPL(call_rcu_bh);
219 * Return the number of RCU batches processed thus far. Useful
220 * for debug and statistics.
222 long rcu_batches_completed(void)
224 return rcu_ctrlblk.completed;
226 EXPORT_SYMBOL_GPL(rcu_batches_completed);
229 * Return the number of RCU batches processed thus far. Useful
230 * for debug and statistics.
232 long rcu_batches_completed_bh(void)
234 return rcu_bh_ctrlblk.completed;
236 EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
238 /* Raises the softirq for processing rcu_callbacks. */
239 static inline void raise_rcu_softirq(void)
241 raise_softirq(RCU_SOFTIRQ);
245 * Invoke the completed RCU callbacks. They are expected to be in
248 static void rcu_do_batch(struct rcu_data *rdp)
251 struct rcu_head *next, *list;
254 list = rdp->donelist;
260 if (++count >= rdp->blimit)
263 rdp->donelist = list;
265 local_irq_save(flags);
267 local_irq_restore(flags);
268 if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark)
269 rdp->blimit = blimit;
272 rdp->donetail = &rdp->donelist;
278 * Grace period handling:
279 * The grace period handling consists out of two steps:
280 * - A new grace period is started.
281 * This is done by rcu_start_batch. The start is not broadcasted to
282 * all cpus, they must pick this up by comparing rcp->cur with
283 * rdp->quiescbatch. All cpus are recorded in the
284 * rcu_ctrlblk.cpumask bitmap.
285 * - All cpus must go through a quiescent state.
286 * Since the start of the grace period is not broadcasted, at least two
287 * calls to rcu_check_quiescent_state are required:
288 * The first call just notices that a new grace period is running. The
289 * following calls check if there was a quiescent state since the beginning
290 * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If
291 * the bitmap is empty, then the grace period is completed.
292 * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
293 * period (if necessary).
296 #ifdef CONFIG_DEBUG_RCU_STALL
298 static inline void record_gp_check_time(struct rcu_ctrlblk *rcp)
300 rcp->gp_check = get_seconds() + 3;
303 static void print_other_cpu_stall(struct rcu_ctrlblk *rcp)
309 /* Only let one CPU complain about others per time interval. */
311 spin_lock_irqsave(&rcp->lock, flags);
312 delta = get_seconds() - rcp->gp_check;
313 if (delta < 2L || cpus_empty(rcp->cpumask)) {
314 spin_unlock(&rcp->lock);
317 rcp->gp_check = get_seconds() + 30;
318 spin_unlock_irqrestore(&rcp->lock, flags);
320 /* OK, time to rat on our buddy... */
322 printk(KERN_ERR "RCU detected CPU stalls:");
323 for_each_cpu_mask(cpu, rcp->cpumask)
325 printk(" (detected by %d, t=%lu/%lu)\n",
326 smp_processor_id(), get_seconds(), rcp->gp_check);
329 static void print_cpu_stall(struct rcu_ctrlblk *rcp)
333 printk(KERN_ERR "RCU detected CPU %d stall (t=%lu/%lu)\n",
334 smp_processor_id(), get_seconds(), rcp->gp_check);
336 spin_lock_irqsave(&rcp->lock, flags);
337 if ((long)(get_seconds() - rcp->gp_check) >= 0L)
338 rcp->gp_check = get_seconds() + 30;
339 spin_unlock_irqrestore(&rcp->lock, flags);
342 static void check_cpu_stall(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
346 delta = get_seconds() - rcp->gp_check;
347 if (cpu_isset(smp_processor_id(), rcp->cpumask) && delta >= 0L) {
349 /* We haven't checked in, so go dump stack. */
351 print_cpu_stall(rcp);
354 if (!cpus_empty(rcp->cpumask) && delta >= 2L) {
355 /* They had two seconds to dump stack, so complain. */
356 print_other_cpu_stall(rcp);
361 #else /* #ifdef CONFIG_DEBUG_RCU_STALL */
363 static inline void record_gp_check_time(struct rcu_ctrlblk *rcp)
368 check_cpu_stall(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
372 #endif /* #else #ifdef CONFIG_DEBUG_RCU_STALL */
375 * Register a new batch of callbacks, and start it up if there is currently no
376 * active batch and the batch to be registered has not already occurred.
377 * Caller must hold rcu_ctrlblk.lock.
379 static void rcu_start_batch(struct rcu_ctrlblk *rcp)
381 if (rcp->cur != rcp->pending &&
382 rcp->completed == rcp->cur) {
384 record_gp_check_time(rcp);
387 * Accessing nohz_cpu_mask before incrementing rcp->cur needs a
388 * Barrier Otherwise it can cause tickless idle CPUs to be
389 * included in rcp->cpumask, which will extend graceperiods
393 cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask);
400 * cpu went through a quiescent state since the beginning of the grace period.
401 * Clear it from the cpu mask and complete the grace period if it was the last
402 * cpu. Start another grace period if someone has further entries pending
404 static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
406 cpu_clear(cpu, rcp->cpumask);
407 if (cpus_empty(rcp->cpumask)) {
408 /* batch completed ! */
409 rcp->completed = rcp->cur;
410 rcu_start_batch(rcp);
415 * Check if the cpu has gone through a quiescent state (say context
416 * switch). If so and if it already hasn't done so in this RCU
417 * quiescent cycle, then indicate that it has done so.
419 static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
420 struct rcu_data *rdp)
424 if (rdp->quiescbatch != rcp->cur) {
425 /* start new grace period: */
427 rdp->passed_quiesc = 0;
428 rdp->quiescbatch = rcp->cur;
432 /* Grace period already completed for this cpu?
433 * qs_pending is checked instead of the actual bitmap to avoid
434 * cacheline trashing.
436 if (!rdp->qs_pending)
440 * Was there a quiescent state since the beginning of the grace
441 * period? If no, then exit and wait for the next call.
443 if (!rdp->passed_quiesc)
447 spin_lock_irqsave(&rcp->lock, flags);
449 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
450 * during cpu startup. Ignore the quiescent state.
452 if (likely(rdp->quiescbatch == rcp->cur))
453 cpu_quiet(rdp->cpu, rcp);
455 spin_unlock_irqrestore(&rcp->lock, flags);
459 #ifdef CONFIG_HOTPLUG_CPU
461 /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
462 * locking requirements, the list it's pulling from has to belong to a cpu
463 * which is dead and hence not processing interrupts.
465 static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
466 struct rcu_head **tail, long batch)
471 local_irq_save(flags);
472 this_rdp->batch = batch;
473 *this_rdp->nxttail[2] = list;
474 this_rdp->nxttail[2] = tail;
475 local_irq_restore(flags);
479 static void __rcu_offline_cpu(struct rcu_data *this_rdp,
480 struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
485 * if the cpu going offline owns the grace period
486 * we can block indefinitely waiting for it, so flush
489 spin_lock_irqsave(&rcp->lock, flags);
490 if (rcp->cur != rcp->completed)
491 cpu_quiet(rdp->cpu, rcp);
492 rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail, rcp->cur + 1);
493 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail[2], rcp->cur + 1);
494 spin_unlock(&rcp->lock);
496 this_rdp->qlen += rdp->qlen;
497 local_irq_restore(flags);
500 static void rcu_offline_cpu(int cpu)
502 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
503 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
505 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk,
506 &per_cpu(rcu_data, cpu));
507 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk,
508 &per_cpu(rcu_bh_data, cpu));
509 put_cpu_var(rcu_data);
510 put_cpu_var(rcu_bh_data);
515 static void rcu_offline_cpu(int cpu)
522 * This does the RCU processing work from softirq context.
524 static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
525 struct rcu_data *rdp)
531 local_irq_save(flags);
532 completed_snap = ACCESS_ONCE(rcp->completed);
535 * move the other grace-period-completed entries to
536 * [rdp->nxtlist, *rdp->nxttail[0]) temporarily
538 if (!rcu_batch_before(completed_snap, rdp->batch))
539 rdp->nxttail[0] = rdp->nxttail[1] = rdp->nxttail[2];
540 else if (!rcu_batch_before(completed_snap, rdp->batch - 1))
541 rdp->nxttail[0] = rdp->nxttail[1];
544 * the grace period for entries in
545 * [rdp->nxtlist, *rdp->nxttail[0]) has completed and
546 * move these entries to donelist
548 if (rdp->nxttail[0] != &rdp->nxtlist) {
549 *rdp->donetail = rdp->nxtlist;
550 rdp->donetail = rdp->nxttail[0];
551 rdp->nxtlist = *rdp->nxttail[0];
552 *rdp->donetail = NULL;
554 if (rdp->nxttail[1] == rdp->nxttail[0])
555 rdp->nxttail[1] = &rdp->nxtlist;
556 if (rdp->nxttail[2] == rdp->nxttail[0])
557 rdp->nxttail[2] = &rdp->nxtlist;
558 rdp->nxttail[0] = &rdp->nxtlist;
561 local_irq_restore(flags);
563 if (rcu_batch_after(rdp->batch, rcp->pending)) {
566 /* and start it/schedule start if it's a new batch */
567 spin_lock_irqsave(&rcp->lock, flags);
568 if (rcu_batch_after(rdp->batch, rcp->pending)) {
569 rcp->pending = rdp->batch;
570 rcu_start_batch(rcp);
572 spin_unlock_irqrestore(&rcp->lock, flags);
576 rcu_check_quiescent_state(rcp, rdp);
581 static void rcu_process_callbacks(struct softirq_action *unused)
584 * Memory references from any prior RCU read-side critical sections
585 * executed by the interrupted code must be see before any RCU
586 * grace-period manupulations below.
589 smp_mb(); /* See above block comment. */
591 __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
592 __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
595 * Memory references from any later RCU read-side critical sections
596 * executed by the interrupted code must be see after any RCU
597 * grace-period manupulations above.
600 smp_mb(); /* See above block comment. */
603 static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
605 /* Check for CPU stalls, if enabled. */
606 check_cpu_stall(rcp, rdp);
609 long completed_snap = ACCESS_ONCE(rcp->completed);
612 * This cpu has pending rcu entries and the grace period
613 * for them has completed.
615 if (!rcu_batch_before(completed_snap, rdp->batch))
617 if (!rcu_batch_before(completed_snap, rdp->batch - 1) &&
618 rdp->nxttail[0] != rdp->nxttail[1])
620 if (rdp->nxttail[0] != &rdp->nxtlist)
624 * This cpu has pending rcu entries and the new batch
625 * for then hasn't been started nor scheduled start
627 if (rcu_batch_after(rdp->batch, rcp->pending))
631 /* This cpu has finished callbacks to invoke */
635 /* The rcu core waits for a quiescent state from the cpu */
636 if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
644 * Check to see if there is any immediate RCU-related work to be done
645 * by the current CPU, returning 1 if so. This function is part of the
646 * RCU implementation; it is -not- an exported member of the RCU API.
648 int rcu_pending(int cpu)
650 return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) ||
651 __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu));
655 * Check to see if any future RCU-related work will need to be done
656 * by the current CPU, even if none need be done immediately, returning
657 * 1 if so. This function is part of the RCU implementation; it is -not-
658 * an exported member of the RCU API.
660 int rcu_needs_cpu(int cpu)
662 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
663 struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu);
665 return !!rdp->nxtlist || !!rdp_bh->nxtlist || rcu_pending(cpu);
669 * Top-level function driving RCU grace-period detection, normally
670 * invoked from the scheduler-clock interrupt. This function simply
671 * increments counters that are read only from softirq by this same
672 * CPU, so there are no memory barriers required.
674 void rcu_check_callbacks(int cpu, int user)
677 (idle_cpu(cpu) && !in_softirq() &&
678 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
681 * Get here if this CPU took its interrupt from user
682 * mode or from the idle loop, and if this is not a
683 * nested interrupt. In this case, the CPU is in
684 * a quiescent state, so count it.
686 * Also do a memory barrier. This is needed to handle
687 * the case where writes from a preempt-disable section
688 * of code get reordered into schedule() by this CPU's
689 * write buffer. The memory barrier makes sure that
690 * the rcu_qsctr_inc() and rcu_bh_qsctr_inc() are see
691 * by other CPUs to happen after any such write.
694 smp_mb(); /* See above block comment. */
696 rcu_bh_qsctr_inc(cpu);
698 } else if (!in_softirq()) {
701 * Get here if this CPU did not take its interrupt from
702 * softirq, in other words, if it is not interrupting
703 * a rcu_bh read-side critical section. This is an _bh
704 * critical section, so count it. The memory barrier
705 * is needed for the same reason as is the above one.
708 smp_mb(); /* See above block comment. */
709 rcu_bh_qsctr_inc(cpu);
714 static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
715 struct rcu_data *rdp)
719 spin_lock_irqsave(&rcp->lock, flags);
720 memset(rdp, 0, sizeof(*rdp));
721 rdp->nxttail[0] = rdp->nxttail[1] = rdp->nxttail[2] = &rdp->nxtlist;
722 rdp->donetail = &rdp->donelist;
723 rdp->quiescbatch = rcp->completed;
726 rdp->blimit = blimit;
727 spin_unlock_irqrestore(&rcp->lock, flags);
730 static void __cpuinit rcu_online_cpu(int cpu)
732 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
733 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
735 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
736 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
737 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
740 static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
741 unsigned long action, void *hcpu)
743 long cpu = (long)hcpu;
747 case CPU_UP_PREPARE_FROZEN:
751 case CPU_DEAD_FROZEN:
752 rcu_offline_cpu(cpu);
760 static struct notifier_block __cpuinitdata rcu_nb = {
761 .notifier_call = rcu_cpu_notify,
765 * Initializes rcu mechanism. Assumed to be called early.
766 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
767 * Note that rcu_qsctr and friends are implicitly
768 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
770 void __init __rcu_init(void)
772 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
773 (void *)(long)smp_processor_id());
774 /* Register notifier for non-boot CPUs */
775 register_cpu_notifier(&rcu_nb);
778 module_param(blimit, int, 0);
779 module_param(qhimark, int, 0);
780 module_param(qlowmark, int, 0);