rcupdate.c 7.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Read-Copy Update mechanism for mutual exclusion
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
18
 * Copyright IBM Corporation, 2001
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 *
 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
 *	    Manfred Spraul <manfred@colorfullife.com>
 * 
 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
 * Papers:
 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
 *
 * For detailed explanation of Read-Copy Update mechanism see -
 * 		http://lse.sourceforge.net/locking/rcupdate.html
 *
 */
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/smp.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <asm/atomic.h>
#include <linux/bitops.h>
#include <linux/percpu.h>
#include <linux/notifier.h>
#include <linux/cpu.h>
45
#include <linux/mutex.h>
46
#include <linux/module.h>
47
#include <linux/kernel_stat.h>
L
Linus Torvalds 已提交
48

49 50 51 52 53 54
enum rcu_barrier {
	RCU_BARRIER_STD,
	RCU_BARRIER_BH,
	RCU_BARRIER_SCHED,
};

55
static DEFINE_PER_CPU(struct rcu_head, rcu_barrier_head) = {NULL};
D
Dipankar Sarma 已提交
56
static atomic_t rcu_barrier_cpu_count;
57
static DEFINE_MUTEX(rcu_barrier_mutex);
D
Dipankar Sarma 已提交
58
static struct completion rcu_barrier_completion;
59
int rcu_scheduler_active __read_mostly;
D
Dipankar Sarma 已提交
60

61 62 63 64
static atomic_t rcu_migrate_type_count = ATOMIC_INIT(0);
static struct rcu_head rcu_migrate_head[3];
static DECLARE_WAIT_QUEUE_HEAD(rcu_migrate_wq);

P
Paul E. McKenney 已提交
65 66 67 68
/*
 * Awaken the corresponding synchronize_rcu() instance now that a
 * grace period has elapsed.
 */
P
Paul E. McKenney 已提交
69
void wakeme_after_rcu(struct rcu_head  *head)
D
Dipankar Sarma 已提交
70
{
71 72 73 74
	struct rcu_synchronize *rcu;

	rcu = container_of(head, struct rcu_synchronize, head);
	complete(&rcu->completion);
D
Dipankar Sarma 已提交
75
}
L
Linus Torvalds 已提交
76 77

/**
78
 * synchronize_rcu - wait until a grace period has elapsed.
L
Linus Torvalds 已提交
79
 *
80 81
 * Control will return to the caller some time after a full grace
 * period has elapsed, in other words after all currently executing RCU
L
Linus Torvalds 已提交
82 83 84 85
 * read-side critical sections have completed.  RCU read-side critical
 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
 * and may be nested.
 */
86 87 88
void synchronize_rcu(void)
{
	struct rcu_synchronize rcu;
89 90 91 92

	if (rcu_blocking_is_gp())
		return;

93 94 95 96 97 98
	init_completion(&rcu.completion);
	/* Will wake me after RCU finished. */
	call_rcu(&rcu.head, wakeme_after_rcu);
	/* Wait for it. */
	wait_for_completion(&rcu.completion);
}
99
EXPORT_SYMBOL_GPL(synchronize_rcu);
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/**
 * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
 *
 * Control will return to the caller some time after a full rcu_bh grace
 * period has elapsed, in other words after all currently executing rcu_bh
 * read-side critical sections have completed.  RCU read-side critical
 * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(),
 * and may be nested.
 */
void synchronize_rcu_bh(void)
{
	struct rcu_synchronize rcu;

	if (rcu_blocking_is_gp())
		return;

	init_completion(&rcu.completion);
	/* Will wake me after RCU finished. */
	call_rcu_bh(&rcu.head, wakeme_after_rcu);
	/* Wait for it. */
	wait_for_completion(&rcu.completion);
}
EXPORT_SYMBOL_GPL(synchronize_rcu_bh);

125 126 127 128 129 130 131 132 133
static void rcu_barrier_callback(struct rcu_head *notused)
{
	if (atomic_dec_and_test(&rcu_barrier_cpu_count))
		complete(&rcu_barrier_completion);
}

/*
 * Called with preemption disabled, and from cross-cpu IRQ context.
 */
134
static void rcu_barrier_func(void *type)
135 136
{
	int cpu = smp_processor_id();
137
	struct rcu_head *head = &per_cpu(rcu_barrier_head, cpu);
138 139

	atomic_inc(&rcu_barrier_cpu_count);
140 141 142 143 144 145 146 147 148 149 150
	switch ((enum rcu_barrier)type) {
	case RCU_BARRIER_STD:
		call_rcu(head, rcu_barrier_callback);
		break;
	case RCU_BARRIER_BH:
		call_rcu_bh(head, rcu_barrier_callback);
		break;
	case RCU_BARRIER_SCHED:
		call_rcu_sched(head, rcu_barrier_callback);
		break;
	}
151 152
}

153 154 155
static inline void wait_migrated_callbacks(void)
{
	wait_event(rcu_migrate_wq, !atomic_read(&rcu_migrate_type_count));
156
	smp_mb(); /* In case we didn't sleep. */
157
}
158

159 160 161
/*
 * Orchestrate the specified type of RCU barrier, waiting for all
 * RCU callbacks of the specified type to complete.
162
 */
163
static void _rcu_barrier(enum rcu_barrier type)
164 165
{
	BUG_ON(in_interrupt());
166 167
	/* Take cpucontrol mutex to protect against CPU hotplug */
	mutex_lock(&rcu_barrier_mutex);
168
	init_completion(&rcu_barrier_completion);
169
	/*
170 171 172 173 174 175 176
	 * Initialize rcu_barrier_cpu_count to 1, then invoke
	 * rcu_barrier_func() on each CPU, so that each CPU also has
	 * incremented rcu_barrier_cpu_count.  Only then is it safe to
	 * decrement rcu_barrier_cpu_count -- otherwise the first CPU
	 * might complete its grace period before all of the other CPUs
	 * did their increment, causing this function to return too
	 * early.
177
	 */
178
	atomic_set(&rcu_barrier_cpu_count, 1);
179
	on_each_cpu(rcu_barrier_func, (void *)type, 1);
180 181
	if (atomic_dec_and_test(&rcu_barrier_cpu_count))
		complete(&rcu_barrier_completion);
182
	wait_for_completion(&rcu_barrier_completion);
183
	mutex_unlock(&rcu_barrier_mutex);
184
	wait_migrated_callbacks();
185
}
186 187 188 189 190 191 192 193

/**
 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
 */
void rcu_barrier(void)
{
	_rcu_barrier(RCU_BARRIER_STD);
}
194 195
EXPORT_SYMBOL_GPL(rcu_barrier);

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
/**
 * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
 */
void rcu_barrier_bh(void)
{
	_rcu_barrier(RCU_BARRIER_BH);
}
EXPORT_SYMBOL_GPL(rcu_barrier_bh);

/**
 * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
 */
void rcu_barrier_sched(void)
{
	_rcu_barrier(RCU_BARRIER_SCHED);
}
EXPORT_SYMBOL_GPL(rcu_barrier_sched);

214 215 216 217 218 219
static void rcu_migrate_callback(struct rcu_head *notused)
{
	if (atomic_dec_and_test(&rcu_migrate_type_count))
		wake_up(&rcu_migrate_wq);
}

220 221 222
extern int rcu_cpu_notify(struct notifier_block *self,
			  unsigned long action, void *hcpu);

223 224 225
static int __cpuinit rcu_barrier_cpu_hotplug(struct notifier_block *self,
		unsigned long action, void *hcpu)
{
226
	rcu_cpu_notify(self, action, hcpu);
227 228 229 230 231 232 233 234 235 236 237 238 239 240
	if (action == CPU_DYING) {
		/*
		 * preempt_disable() in on_each_cpu() prevents stop_machine(),
		 * so when "on_each_cpu(rcu_barrier_func, (void *)type, 1);"
		 * returns, all online cpus have queued rcu_barrier_func(),
		 * and the dead cpu(if it exist) queues rcu_migrate_callback()s.
		 *
		 * These callbacks ensure _rcu_barrier() waits for all
		 * RCU callbacks of the specified type to complete.
		 */
		atomic_set(&rcu_migrate_type_count, 3);
		call_rcu_bh(rcu_migrate_head, rcu_migrate_callback);
		call_rcu_sched(rcu_migrate_head + 1, rcu_migrate_callback);
		call_rcu(rcu_migrate_head + 2, rcu_migrate_callback);
241 242
	} else if (action == CPU_DOWN_PREPARE) {
		/* Don't need to wait until next removal operation. */
243 244 245 246 247 248 249
		/* rcu_migrate_head is protected by cpu_add_remove_lock */
		wait_migrated_callbacks();
	}

	return NOTIFY_OK;
}

L
Linus Torvalds 已提交
250 251
void __init rcu_init(void)
{
252 253
	int i;

254
	__rcu_init();
255 256 257 258 259 260 261 262 263
	cpu_notifier(rcu_barrier_cpu_hotplug, 0);

	/*
	 * We don't need protection against CPU-hotplug here because
	 * this is called early in boot, before either interrupts
	 * or the scheduler are operational.
	 */
	for_each_online_cpu(i)
		rcu_barrier_cpu_hotplug(NULL, CPU_UP_PREPARE, (void *)(long)i);
L
Linus Torvalds 已提交
264 265
}

266 267 268 269 270 271
void rcu_scheduler_starting(void)
{
	WARN_ON(num_online_cpus() != 1);
	WARN_ON(nr_context_switches() > 0);
	rcu_scheduler_active = 1;
}