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 * Author: Dipankar Sarma <dipankar@in.ibm.com>
22 * Based on the original work by Paul McKenney <paul.mckenney@us.ibm.com>
23 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
25 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
26 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
28 * For detailed explanation of Read-Copy Update mechanism see -
29 * http://lse.sourceforge.net/locking/rcupdate.html
32 #include <linux/types.h>
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/spinlock.h>
36 #include <linux/smp.h>
37 #include <linux/interrupt.h>
38 #include <linux/sched.h>
39 #include <asm/atomic.h>
40 #include <asm/bitops.h>
41 #include <linux/module.h>
42 #include <linux/completion.h>
43 #include <linux/percpu.h>
44 #include <linux/notifier.h>
45 #include <linux/rcupdate.h>
46 #include <linux/cpu.h>
48 /* Definition for rcupdate control block. */
49 struct rcu_ctrlblk rcu_ctrlblk =
50 { .mutex = SPIN_LOCK_UNLOCKED, .curbatch = 1,
51 .maxbatch = 1, .rcu_cpu_mask = CPU_MASK_NONE };
52 DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
54 /* Fake initialization required by compiler */
55 static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL};
56 #define RCU_tasklet(cpu) (per_cpu(rcu_tasklet, cpu))
59 * call_rcu - Queue an RCU update request.
60 * @head: structure to be used for queueing the RCU updates.
61 * @func: actual update function to be invoked after the grace period
62 * @arg: argument to be passed to the update function
64 * The update function will be invoked as soon as all CPUs have performed
65 * a context switch or been seen in the idle loop or in a user process.
66 * The read-side of critical section that use call_rcu() for updation must
67 * be protected by rcu_read_lock()/rcu_read_unlock().
69 void fastcall call_rcu(struct rcu_head *head, void (*func)(void *arg), void *arg)
76 local_irq_save(flags);
77 cpu = smp_processor_id();
78 list_add_tail(&head->list, &RCU_nxtlist(cpu));
79 local_irq_restore(flags);
83 * Invoke the completed RCU callbacks. They are expected to be in
86 static void rcu_do_batch(struct list_head *list)
88 struct list_head *entry;
89 struct rcu_head *head;
91 while (!list_empty(list)) {
94 head = list_entry(entry, struct rcu_head, list);
95 head->func(head->arg);
100 * Register a new batch of callbacks, and start it up if there is currently no
101 * active batch and the batch to be registered has not already occurred.
102 * Caller must hold the rcu_ctrlblk lock.
104 static void rcu_start_batch(long newbatch)
108 if (rcu_batch_before(rcu_ctrlblk.maxbatch, newbatch)) {
109 rcu_ctrlblk.maxbatch = newbatch;
111 if (rcu_batch_before(rcu_ctrlblk.maxbatch, rcu_ctrlblk.curbatch) ||
112 !cpus_empty(rcu_ctrlblk.rcu_cpu_mask)) {
115 /* Can't change, since spin lock held. */
116 active = idle_cpu_mask;
117 cpus_complement(active);
118 cpus_and(rcu_ctrlblk.rcu_cpu_mask, cpu_online_map, active);
122 * Check if the cpu has gone through a quiescent state (say context
123 * switch). If so and if it already hasn't done so in this RCU
124 * quiescent cycle, then indicate that it has done so.
126 static void rcu_check_quiescent_state(void)
128 int cpu = smp_processor_id();
130 if (!cpu_isset(cpu, rcu_ctrlblk.rcu_cpu_mask))
134 * Races with local timer interrupt - in the worst case
135 * we may miss one quiescent state of that CPU. That is
136 * tolerable. So no need to disable interrupts.
138 if (RCU_last_qsctr(cpu) == RCU_QSCTR_INVALID) {
139 RCU_last_qsctr(cpu) = RCU_qsctr(cpu);
142 if (RCU_qsctr(cpu) == RCU_last_qsctr(cpu))
145 spin_lock(&rcu_ctrlblk.mutex);
146 if (!cpu_isset(cpu, rcu_ctrlblk.rcu_cpu_mask))
149 cpu_clear(cpu, rcu_ctrlblk.rcu_cpu_mask);
150 RCU_last_qsctr(cpu) = RCU_QSCTR_INVALID;
151 if (!cpus_empty(rcu_ctrlblk.rcu_cpu_mask))
154 rcu_ctrlblk.curbatch++;
155 rcu_start_batch(rcu_ctrlblk.maxbatch);
158 spin_unlock(&rcu_ctrlblk.mutex);
162 #ifdef CONFIG_HOTPLUG_CPU
164 /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
165 * locking requirements, the list it's pulling from has to belong to a cpu
166 * which is dead and hence not processing interrupts.
168 static void rcu_move_batch(struct list_head *list)
170 struct list_head *entry;
171 int cpu = smp_processor_id();
174 while (!list_empty(list)) {
177 list_add_tail(entry, &RCU_nxtlist(cpu));
182 static void rcu_offline_cpu(int cpu)
184 /* if the cpu going offline owns the grace period
185 * we can block indefinitely waiting for it, so flush
188 spin_lock_irq(&rcu_ctrlblk.mutex);
189 if (cpus_empty(rcu_ctrlblk.rcu_cpu_mask))
192 cpu_clear(cpu, rcu_ctrlblk.rcu_cpu_mask);
193 if (cpus_empty(rcu_ctrlblk.rcu_cpu_mask)) {
194 rcu_ctrlblk.curbatch++;
195 /* We may avoid calling start batch if
196 * we are starting the batch only
197 * because of the DEAD CPU (the current
198 * CPU will start a new batch anyway for
199 * the callbacks we will move to current CPU).
200 * However, we will avoid this optimisation
203 rcu_start_batch(rcu_ctrlblk.maxbatch);
206 spin_unlock_irq(&rcu_ctrlblk.mutex);
208 rcu_move_batch(&RCU_curlist(cpu));
209 rcu_move_batch(&RCU_nxtlist(cpu));
211 tasklet_kill_immediate(&RCU_tasklet(cpu), cpu);
217 * This does the RCU processing work from tasklet context.
219 static void rcu_process_callbacks(unsigned long unused)
221 int cpu = smp_processor_id();
224 if (!list_empty(&RCU_curlist(cpu)) &&
225 rcu_batch_after(rcu_ctrlblk.curbatch, RCU_batch(cpu))) {
226 list_splice(&RCU_curlist(cpu), &list);
227 INIT_LIST_HEAD(&RCU_curlist(cpu));
231 if (!list_empty(&RCU_nxtlist(cpu)) && list_empty(&RCU_curlist(cpu))) {
232 list_splice(&RCU_nxtlist(cpu), &RCU_curlist(cpu));
233 INIT_LIST_HEAD(&RCU_nxtlist(cpu));
237 * start the next batch of callbacks
239 spin_lock(&rcu_ctrlblk.mutex);
240 RCU_batch(cpu) = rcu_ctrlblk.curbatch + 1;
241 rcu_start_batch(RCU_batch(cpu));
242 spin_unlock(&rcu_ctrlblk.mutex);
246 rcu_check_quiescent_state();
247 if (!list_empty(&list))
251 void rcu_check_callbacks(int cpu, int user)
254 (idle_cpu(cpu) && !in_softirq() &&
255 hardirq_count() <= (1 << HARDIRQ_SHIFT)))
257 tasklet_schedule(&RCU_tasklet(cpu));
260 static void __devinit rcu_online_cpu(int cpu)
262 memset(&per_cpu(rcu_data, cpu), 0, sizeof(struct rcu_data));
263 tasklet_init(&RCU_tasklet(cpu), rcu_process_callbacks, 0UL);
264 INIT_LIST_HEAD(&RCU_nxtlist(cpu));
265 INIT_LIST_HEAD(&RCU_curlist(cpu));
268 static int __devinit rcu_cpu_notify(struct notifier_block *self,
269 unsigned long action, void *hcpu)
271 long cpu = (long)hcpu;
276 #ifdef CONFIG_HOTPLUG_CPU
278 rcu_offline_cpu(cpu);
287 static struct notifier_block __devinitdata rcu_nb = {
288 .notifier_call = rcu_cpu_notify,
292 * Initializes rcu mechanism. Assumed to be called early.
293 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
294 * Note that rcu_qsctr and friends are implicitly
295 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
297 void __init rcu_init(void)
299 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
300 (void *)(long)smp_processor_id());
301 /* Register notifier for non-boot CPUs */
302 register_cpu_notifier(&rcu_nb);
306 /* Because of FASTCALL declaration of complete, we use this wrapper */
307 static void wakeme_after_rcu(void *completion)
309 complete(completion);
313 * synchronize-kernel - wait until all the CPUs have gone
314 * through a "quiescent" state. It may sleep.
316 void synchronize_kernel(void)
319 DECLARE_COMPLETION(completion);
321 /* Will wake me after RCU finished */
322 call_rcu(&rcu, wakeme_after_rcu, &completion);
325 wait_for_completion(&completion);
329 EXPORT_SYMBOL(call_rcu);
330 EXPORT_SYMBOL(synchronize_kernel);