blob: 4a189ea18b48a19bc61ff60f45d1bb20798e6aa8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Paul E. McKenney01c1c662008-01-25 21:08:24 +010018 * Copyright IBM Corporation, 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
Paul E. McKenneya71fca52009-09-18 10:28:19 -070022 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * 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 -
Paul E. McKenneya71fca52009-09-18 10:28:19 -070030 * http://lse.sourceforge.net/locking/rcupdate.html
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
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>
38#include <linux/interrupt.h>
39#include <linux/sched.h>
40#include <asm/atomic.h>
41#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/percpu.h>
43#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/cpu.h>
Ingo Molnar9331b312006-03-23 03:00:19 -080045#include <linux/mutex.h>
Paul E. McKenney01c1c662008-01-25 21:08:24 +010046#include <linux/module.h>
Paul E. McKenneya6826042009-02-25 18:03:42 -080047#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Paul E. McKenney162cc272009-09-23 16:18:13 -070049#ifdef CONFIG_DEBUG_LOCK_ALLOC
50static struct lock_class_key rcu_lock_key;
51struct lockdep_map rcu_lock_map =
52 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
53EXPORT_SYMBOL_GPL(rcu_lock_map);
54#endif
55
Paul E. McKenney70f12f82008-05-12 21:21:05 +020056enum rcu_barrier {
57 RCU_BARRIER_STD,
58 RCU_BARRIER_BH,
59 RCU_BARRIER_SCHED,
60};
61
Paul E. McKenney01c1c662008-01-25 21:08:24 +010062static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};
Dipankar Sarma21a1ea92006-03-07 21:55:33 -080063static atomic_t rcu_barrier_cpu_count;
Ingo Molnar9331b312006-03-23 03:00:19 -080064static DEFINE_MUTEX(rcu_barrier_mutex);
Dipankar Sarma21a1ea92006-03-07 21:55:33 -080065static struct completion rcu_barrier_completion;
Paul E. McKenneya6826042009-02-25 18:03:42 -080066int rcu_scheduler_active __read_mostly;
Dipankar Sarma21a1ea92006-03-07 21:55:33 -080067
David Howells5b1d07e2009-04-15 19:35:01 +010068static atomic_t rcu_migrate_type_count = ATOMIC_INIT(0);
69static struct rcu_head rcu_migrate_head[3];
70static DECLARE_WAIT_QUEUE_HEAD(rcu_migrate_wq);
71
Paul E. McKenneyfbf6bfc2008-02-13 15:03:15 -080072/*
73 * Awaken the corresponding synchronize_rcu() instance now that a
74 * grace period has elapsed.
75 */
Paul E. McKenney4446a362008-05-12 21:21:05 +020076void wakeme_after_rcu(struct rcu_head *head)
Dipankar Sarma21a1ea92006-03-07 21:55:33 -080077{
Paul E. McKenney01c1c662008-01-25 21:08:24 +010078 struct rcu_synchronize *rcu;
79
80 rcu = container_of(head, struct rcu_synchronize, head);
81 complete(&rcu->completion);
Dipankar Sarma21a1ea92006-03-07 21:55:33 -080082}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Paul E. McKenney16e30812009-09-13 09:15:11 -070084#ifdef CONFIG_TREE_PREEMPT_RCU
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/**
Paul E. McKenney01c1c662008-01-25 21:08:24 +010087 * synchronize_rcu - wait until a grace period has elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
Paul E. McKenney01c1c662008-01-25 21:08:24 +010089 * Control will return to the caller some time after a full grace
90 * period has elapsed, in other words after all currently executing RCU
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * read-side critical sections have completed. RCU read-side critical
92 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
93 * and may be nested.
94 */
Paul E. McKenneyea7d3fe2009-01-04 13:03:02 -080095void synchronize_rcu(void)
96{
97 struct rcu_synchronize rcu;
Paul E. McKenneya6826042009-02-25 18:03:42 -080098
Paul E. McKenney16e30812009-09-13 09:15:11 -070099 if (!rcu_scheduler_active)
Paul E. McKenneya6826042009-02-25 18:03:42 -0800100 return;
101
Paul E. McKenneyea7d3fe2009-01-04 13:03:02 -0800102 init_completion(&rcu.completion);
103 /* Will wake me after RCU finished. */
104 call_rcu(&rcu.head, wakeme_after_rcu);
105 /* Wait for it. */
106 wait_for_completion(&rcu.completion);
107}
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100108EXPORT_SYMBOL_GPL(synchronize_rcu);
Paul E. McKenneyc32e0662006-06-27 02:54:04 -0700109
Paul E. McKenney16e30812009-09-13 09:15:11 -0700110#endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
111
112/**
113 * synchronize_sched - wait until an rcu-sched grace period has elapsed.
114 *
115 * Control will return to the caller some time after a full rcu-sched
116 * grace period has elapsed, in other words after all currently executing
117 * rcu-sched read-side critical sections have completed. These read-side
118 * critical sections are delimited by rcu_read_lock_sched() and
119 * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(),
120 * local_irq_disable(), and so on may be used in place of
121 * rcu_read_lock_sched().
122 *
123 * This means that all preempt_disable code sequences, including NMI and
124 * hardware-interrupt handlers, in progress on entry will have completed
125 * before this primitive returns. However, this does not guarantee that
126 * softirq handlers will have completed, since in some kernels, these
127 * handlers can run in process context, and can block.
128 *
129 * This primitive provides the guarantees made by the (now removed)
130 * synchronize_kernel() API. In contrast, synchronize_rcu() only
131 * guarantees that rcu_read_lock() sections will have completed.
132 * In "classic RCU", these two guarantees happen to be one and
133 * the same, but can differ in realtime RCU implementations.
134 */
135void synchronize_sched(void)
136{
137 struct rcu_synchronize rcu;
138
139 if (rcu_blocking_is_gp())
140 return;
141
142 init_completion(&rcu.completion);
143 /* Will wake me after RCU finished. */
144 call_rcu_sched(&rcu.head, wakeme_after_rcu);
145 /* Wait for it. */
146 wait_for_completion(&rcu.completion);
147}
148EXPORT_SYMBOL_GPL(synchronize_sched);
149
Paul E. McKenney03b042b2009-06-25 09:08:16 -0700150/**
151 * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
152 *
153 * Control will return to the caller some time after a full rcu_bh grace
154 * period has elapsed, in other words after all currently executing rcu_bh
155 * read-side critical sections have completed. RCU read-side critical
156 * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
157 * and may be nested.
158 */
159void synchronize_rcu_bh(void)
160{
161 struct rcu_synchronize rcu;
162
163 if (rcu_blocking_is_gp())
164 return;
165
166 init_completion(&rcu.completion);
167 /* Will wake me after RCU finished. */
168 call_rcu_bh(&rcu.head, wakeme_after_rcu);
169 /* Wait for it. */
170 wait_for_completion(&rcu.completion);
171}
172EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
173
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800174static void rcu_barrier_callback(struct rcu_head *notused)
175{
176 if (atomic_dec_and_test(&rcu_barrier_cpu_count))
177 complete(&rcu_barrier_completion);
178}
179
180/*
181 * Called with preemption disabled, and from cross-cpu IRQ context.
182 */
Paul E. McKenney70f12f82008-05-12 21:21:05 +0200183static void rcu_barrier_func(void *type)
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800184{
185 int cpu = smp_processor_id();
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100186 struct rcu_head *head = &per_cpu(rcu_barrier_head, cpu);
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800187
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800188 atomic_inc(&rcu_barrier_cpu_count);
Paul E. McKenney70f12f82008-05-12 21:21:05 +0200189 switch ((enum rcu_barrier)type) {
190 case RCU_BARRIER_STD:
191 call_rcu(head, rcu_barrier_callback);
192 break;
193 case RCU_BARRIER_BH:
194 call_rcu_bh(head, rcu_barrier_callback);
195 break;
196 case RCU_BARRIER_SCHED:
197 call_rcu_sched(head, rcu_barrier_callback);
198 break;
199 }
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800200}
201
David Howells5b1d07e2009-04-15 19:35:01 +0100202static inline void wait_migrated_callbacks(void)
203{
204 wait_event(rcu_migrate_wq, !atomic_read(&rcu_migrate_type_count));
Paul E. McKenney03b042b2009-06-25 09:08:16 -0700205 smp_mb(); /* In case we didn't sleep. */
David Howells5b1d07e2009-04-15 19:35:01 +0100206}
Lai Jiangshanf69b17d2009-03-20 17:40:06 +0800207
Paul E. McKenney70f12f82008-05-12 21:21:05 +0200208/*
209 * Orchestrate the specified type of RCU barrier, waiting for all
210 * RCU callbacks of the specified type to complete.
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800211 */
Paul E. McKenney70f12f82008-05-12 21:21:05 +0200212static void _rcu_barrier(enum rcu_barrier type)
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800213{
214 BUG_ON(in_interrupt());
Ingo Molnar9331b312006-03-23 03:00:19 -0800215 /* Take cpucontrol mutex to protect against CPU hotplug */
216 mutex_lock(&rcu_barrier_mutex);
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800217 init_completion(&rcu_barrier_completion);
Paul E. McKenneye0ecfa72008-01-25 21:08:24 +0100218 /*
Lai Jiangshan5f865152008-10-17 14:40:30 +0800219 * Initialize rcu_barrier_cpu_count to 1, then invoke
220 * rcu_barrier_func() on each CPU, so that each CPU also has
221 * incremented rcu_barrier_cpu_count. Only then is it safe to
222 * decrement rcu_barrier_cpu_count -- otherwise the first CPU
223 * might complete its grace period before all of the other CPUs
224 * did their increment, causing this function to return too
225 * early.
Paul E. McKenneye0ecfa72008-01-25 21:08:24 +0100226 */
Lai Jiangshan5f865152008-10-17 14:40:30 +0800227 atomic_set(&rcu_barrier_cpu_count, 1);
Linus Torvalds59190f42008-07-15 14:02:33 -0700228 on_each_cpu(rcu_barrier_func, (void *)type, 1);
Lai Jiangshan5f865152008-10-17 14:40:30 +0800229 if (atomic_dec_and_test(&rcu_barrier_cpu_count))
230 complete(&rcu_barrier_completion);
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800231 wait_for_completion(&rcu_barrier_completion);
Ingo Molnar9331b312006-03-23 03:00:19 -0800232 mutex_unlock(&rcu_barrier_mutex);
Lai Jiangshanf69b17d2009-03-20 17:40:06 +0800233 wait_migrated_callbacks();
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800234}
Paul E. McKenney70f12f82008-05-12 21:21:05 +0200235
236/**
237 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
238 */
239void rcu_barrier(void)
240{
241 _rcu_barrier(RCU_BARRIER_STD);
242}
Dipankar Sarmaab4720e2005-12-12 00:37:05 -0800243EXPORT_SYMBOL_GPL(rcu_barrier);
244
Paul E. McKenney70f12f82008-05-12 21:21:05 +0200245/**
246 * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
247 */
248void rcu_barrier_bh(void)
249{
250 _rcu_barrier(RCU_BARRIER_BH);
251}
252EXPORT_SYMBOL_GPL(rcu_barrier_bh);
253
254/**
255 * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
256 */
257void rcu_barrier_sched(void)
258{
259 _rcu_barrier(RCU_BARRIER_SCHED);
260}
261EXPORT_SYMBOL_GPL(rcu_barrier_sched);
262
Lai Jiangshanf69b17d2009-03-20 17:40:06 +0800263static void rcu_migrate_callback(struct rcu_head *notused)
264{
265 if (atomic_dec_and_test(&rcu_migrate_type_count))
266 wake_up(&rcu_migrate_wq);
267}
268
Lai Jiangshanf69b17d2009-03-20 17:40:06 +0800269static int __cpuinit rcu_barrier_cpu_hotplug(struct notifier_block *self,
270 unsigned long action, void *hcpu)
271{
Paul E. McKenney2e597552009-08-15 09:53:48 -0700272 rcu_cpu_notify(self, action, hcpu);
Lai Jiangshanf69b17d2009-03-20 17:40:06 +0800273 if (action == CPU_DYING) {
274 /*
275 * preempt_disable() in on_each_cpu() prevents stop_machine(),
276 * so when "on_each_cpu(rcu_barrier_func, (void *)type, 1);"
277 * returns, all online cpus have queued rcu_barrier_func(),
278 * and the dead cpu(if it exist) queues rcu_migrate_callback()s.
279 *
280 * These callbacks ensure _rcu_barrier() waits for all
281 * RCU callbacks of the specified type to complete.
282 */
283 atomic_set(&rcu_migrate_type_count, 3);
284 call_rcu_bh(rcu_migrate_head, rcu_migrate_callback);
285 call_rcu_sched(rcu_migrate_head + 1, rcu_migrate_callback);
286 call_rcu(rcu_migrate_head + 2, rcu_migrate_callback);
Paul E. McKenney1423cc02009-08-18 23:06:14 -0700287 } else if (action == CPU_DOWN_PREPARE) {
288 /* Don't need to wait until next removal operation. */
Lai Jiangshanf69b17d2009-03-20 17:40:06 +0800289 /* rcu_migrate_head is protected by cpu_add_remove_lock */
290 wait_migrated_callbacks();
291 }
292
293 return NOTIFY_OK;
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296void __init rcu_init(void)
297{
Paul E. McKenney2e597552009-08-15 09:53:48 -0700298 int i;
299
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100300 __rcu_init();
Paul E. McKenney2e597552009-08-15 09:53:48 -0700301 cpu_notifier(rcu_barrier_cpu_hotplug, 0);
302
303 /*
304 * We don't need protection against CPU-hotplug here because
305 * this is called early in boot, before either interrupts
306 * or the scheduler are operational.
307 */
308 for_each_online_cpu(i)
309 rcu_barrier_cpu_hotplug(NULL, CPU_UP_PREPARE, (void *)(long)i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Paul E. McKenneya6826042009-02-25 18:03:42 -0800312void rcu_scheduler_starting(void)
313{
314 WARN_ON(num_online_cpus() != 1);
315 WARN_ON(nr_context_switches() > 0);
316 rcu_scheduler_active = 1;
317}