| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 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 |  * | 
 | 18 |  * Copyright (C) IBM Corporation, 2001 | 
 | 19 |  * | 
 | 20 |  * Authors: Dipankar Sarma <dipankar@in.ibm.com> | 
 | 21 |  *	    Manfred Spraul <manfred@colorfullife.com> | 
 | 22 |  *  | 
 | 23 |  * 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 - | 
 | 30 |  * 		http://lse.sourceforge.net/locking/rcupdate.html | 
 | 31 |  * | 
 | 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> | 
| Ingo Molnar | e56d090 | 2006-01-08 01:01:37 -0800 | [diff] [blame] | 38 | #include <linux/rcupdate.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/rcupdate.h> | 
 | 49 | #include <linux/cpu.h> | 
| Ingo Molnar | 9331b31 | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 50 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
 | 52 | /* Definition for rcupdate control block. */ | 
| Adrian Bunk | 2178426 | 2006-03-23 03:01:00 -0800 | [diff] [blame] | 53 | static struct rcu_ctrlblk rcu_ctrlblk = { | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 54 | 	.cur = -300, | 
 | 55 | 	.completed = -300, | 
| Ingo Molnar | e4d9191 | 2006-07-03 00:24:34 -0700 | [diff] [blame] | 56 | 	.lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock), | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 57 | 	.cpumask = CPU_MASK_NONE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | }; | 
| Adrian Bunk | 2178426 | 2006-03-23 03:01:00 -0800 | [diff] [blame] | 59 | static struct rcu_ctrlblk rcu_bh_ctrlblk = { | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 60 | 	.cur = -300, | 
 | 61 | 	.completed = -300, | 
| Ingo Molnar | e4d9191 | 2006-07-03 00:24:34 -0700 | [diff] [blame] | 62 | 	.lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock), | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 63 | 	.cpumask = CPU_MASK_NONE, | 
 | 64 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 |  | 
 | 66 | DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L }; | 
 | 67 | DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L }; | 
 | 68 |  | 
 | 69 | /* Fake initialization required by compiler */ | 
 | 70 | static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL}; | 
| Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 71 | static int blimit = 10; | 
 | 72 | static int qhimark = 10000; | 
 | 73 | static int qlowmark = 100; | 
| Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 74 |  | 
 | 75 | static atomic_t rcu_barrier_cpu_count; | 
| Ingo Molnar | 9331b31 | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 76 | static DEFINE_MUTEX(rcu_barrier_mutex); | 
| Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 77 | static struct completion rcu_barrier_completion; | 
 | 78 |  | 
 | 79 | #ifdef CONFIG_SMP | 
 | 80 | static void force_quiescent_state(struct rcu_data *rdp, | 
 | 81 | 			struct rcu_ctrlblk *rcp) | 
 | 82 | { | 
 | 83 | 	int cpu; | 
 | 84 | 	cpumask_t cpumask; | 
 | 85 | 	set_need_resched(); | 
| Oleg Nesterov | 20e9751 | 2006-10-04 02:17:17 -0700 | [diff] [blame] | 86 | 	if (unlikely(!rcp->signaled)) { | 
 | 87 | 		rcp->signaled = 1; | 
| Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 88 | 		/* | 
 | 89 | 		 * Don't send IPI to itself. With irqs disabled, | 
 | 90 | 		 * rdp->cpu is the current cpu. | 
 | 91 | 		 */ | 
 | 92 | 		cpumask = rcp->cpumask; | 
 | 93 | 		cpu_clear(rdp->cpu, cpumask); | 
 | 94 | 		for_each_cpu_mask(cpu, cpumask) | 
 | 95 | 			smp_send_reschedule(cpu); | 
 | 96 | 	} | 
 | 97 | } | 
 | 98 | #else | 
 | 99 | static inline void force_quiescent_state(struct rcu_data *rdp, | 
 | 100 | 			struct rcu_ctrlblk *rcp) | 
 | 101 | { | 
 | 102 | 	set_need_resched(); | 
 | 103 | } | 
 | 104 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 |  | 
 | 106 | /** | 
 | 107 |  * call_rcu - Queue an RCU callback for invocation after a grace period. | 
 | 108 |  * @head: structure to be used for queueing the RCU updates. | 
 | 109 |  * @func: actual update function to be invoked after the grace period | 
 | 110 |  * | 
 | 111 |  * The update function will be invoked some time after a full grace | 
 | 112 |  * period elapses, in other words after all currently executing RCU | 
 | 113 |  * read-side critical sections have completed.  RCU read-side critical | 
 | 114 |  * sections are delimited by rcu_read_lock() and rcu_read_unlock(), | 
 | 115 |  * and may be nested. | 
 | 116 |  */ | 
 | 117 | void fastcall call_rcu(struct rcu_head *head, | 
 | 118 | 				void (*func)(struct rcu_head *rcu)) | 
 | 119 | { | 
 | 120 | 	unsigned long flags; | 
 | 121 | 	struct rcu_data *rdp; | 
 | 122 |  | 
 | 123 | 	head->func = func; | 
 | 124 | 	head->next = NULL; | 
 | 125 | 	local_irq_save(flags); | 
 | 126 | 	rdp = &__get_cpu_var(rcu_data); | 
 | 127 | 	*rdp->nxttail = head; | 
 | 128 | 	rdp->nxttail = &head->next; | 
| Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 129 | 	if (unlikely(++rdp->qlen > qhimark)) { | 
 | 130 | 		rdp->blimit = INT_MAX; | 
 | 131 | 		force_quiescent_state(rdp, &rcu_ctrlblk); | 
 | 132 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | 	local_irq_restore(flags); | 
 | 134 | } | 
 | 135 |  | 
 | 136 | /** | 
 | 137 |  * call_rcu_bh - Queue an RCU for invocation after a quicker grace period. | 
 | 138 |  * @head: structure to be used for queueing the RCU updates. | 
 | 139 |  * @func: actual update function to be invoked after the grace period | 
 | 140 |  * | 
 | 141 |  * The update function will be invoked some time after a full grace | 
 | 142 |  * period elapses, in other words after all currently executing RCU | 
 | 143 |  * read-side critical sections have completed. call_rcu_bh() assumes | 
 | 144 |  * that the read-side critical sections end on completion of a softirq | 
 | 145 |  * handler. This means that read-side critical sections in process | 
 | 146 |  * context must not be interrupted by softirqs. This interface is to be | 
 | 147 |  * used when most of the read-side critical sections are in softirq context. | 
 | 148 |  * RCU read-side critical sections are delimited by rcu_read_lock() and | 
 | 149 |  * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh() | 
 | 150 |  * and rcu_read_unlock_bh(), if in process context. These may be nested. | 
 | 151 |  */ | 
 | 152 | void fastcall call_rcu_bh(struct rcu_head *head, | 
 | 153 | 				void (*func)(struct rcu_head *rcu)) | 
 | 154 | { | 
 | 155 | 	unsigned long flags; | 
 | 156 | 	struct rcu_data *rdp; | 
 | 157 |  | 
 | 158 | 	head->func = func; | 
 | 159 | 	head->next = NULL; | 
 | 160 | 	local_irq_save(flags); | 
 | 161 | 	rdp = &__get_cpu_var(rcu_bh_data); | 
 | 162 | 	*rdp->nxttail = head; | 
 | 163 | 	rdp->nxttail = &head->next; | 
| Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 164 |  | 
 | 165 | 	if (unlikely(++rdp->qlen > qhimark)) { | 
 | 166 | 		rdp->blimit = INT_MAX; | 
 | 167 | 		force_quiescent_state(rdp, &rcu_bh_ctrlblk); | 
 | 168 | 	} | 
 | 169 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | 	local_irq_restore(flags); | 
 | 171 | } | 
 | 172 |  | 
 | 173 | /* | 
| Paul E. McKenney | a241ec6 | 2005-10-30 15:03:12 -0800 | [diff] [blame] | 174 |  * Return the number of RCU batches processed thus far.  Useful | 
 | 175 |  * for debug and statistics. | 
 | 176 |  */ | 
 | 177 | long rcu_batches_completed(void) | 
 | 178 | { | 
 | 179 | 	return rcu_ctrlblk.completed; | 
 | 180 | } | 
 | 181 |  | 
| Paul E. McKenney | c32e066 | 2006-06-27 02:54:04 -0700 | [diff] [blame] | 182 | /* | 
 | 183 |  * Return the number of RCU batches processed thus far.  Useful | 
 | 184 |  * for debug and statistics. | 
 | 185 |  */ | 
 | 186 | long rcu_batches_completed_bh(void) | 
 | 187 | { | 
 | 188 | 	return rcu_bh_ctrlblk.completed; | 
 | 189 | } | 
 | 190 |  | 
| Dipankar Sarma | ab4720e | 2005-12-12 00:37:05 -0800 | [diff] [blame] | 191 | static void rcu_barrier_callback(struct rcu_head *notused) | 
 | 192 | { | 
 | 193 | 	if (atomic_dec_and_test(&rcu_barrier_cpu_count)) | 
 | 194 | 		complete(&rcu_barrier_completion); | 
 | 195 | } | 
 | 196 |  | 
 | 197 | /* | 
 | 198 |  * Called with preemption disabled, and from cross-cpu IRQ context. | 
 | 199 |  */ | 
 | 200 | static void rcu_barrier_func(void *notused) | 
 | 201 | { | 
 | 202 | 	int cpu = smp_processor_id(); | 
 | 203 | 	struct rcu_data *rdp = &per_cpu(rcu_data, cpu); | 
 | 204 | 	struct rcu_head *head; | 
 | 205 |  | 
 | 206 | 	head = &rdp->barrier; | 
 | 207 | 	atomic_inc(&rcu_barrier_cpu_count); | 
 | 208 | 	call_rcu(head, rcu_barrier_callback); | 
 | 209 | } | 
 | 210 |  | 
 | 211 | /** | 
 | 212 |  * rcu_barrier - Wait until all the in-flight RCUs are complete. | 
 | 213 |  */ | 
 | 214 | void rcu_barrier(void) | 
 | 215 | { | 
 | 216 | 	BUG_ON(in_interrupt()); | 
| Ingo Molnar | 9331b31 | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 217 | 	/* Take cpucontrol mutex to protect against CPU hotplug */ | 
 | 218 | 	mutex_lock(&rcu_barrier_mutex); | 
| Dipankar Sarma | ab4720e | 2005-12-12 00:37:05 -0800 | [diff] [blame] | 219 | 	init_completion(&rcu_barrier_completion); | 
 | 220 | 	atomic_set(&rcu_barrier_cpu_count, 0); | 
 | 221 | 	on_each_cpu(rcu_barrier_func, NULL, 0, 1); | 
 | 222 | 	wait_for_completion(&rcu_barrier_completion); | 
| Ingo Molnar | 9331b31 | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 223 | 	mutex_unlock(&rcu_barrier_mutex); | 
| Dipankar Sarma | ab4720e | 2005-12-12 00:37:05 -0800 | [diff] [blame] | 224 | } | 
 | 225 | EXPORT_SYMBOL_GPL(rcu_barrier); | 
 | 226 |  | 
| Paul E. McKenney | a241ec6 | 2005-10-30 15:03:12 -0800 | [diff] [blame] | 227 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 |  * Invoke the completed RCU callbacks. They are expected to be in | 
 | 229 |  * a per-cpu list. | 
 | 230 |  */ | 
 | 231 | static void rcu_do_batch(struct rcu_data *rdp) | 
 | 232 | { | 
 | 233 | 	struct rcu_head *next, *list; | 
 | 234 | 	int count = 0; | 
 | 235 |  | 
 | 236 | 	list = rdp->donelist; | 
 | 237 | 	while (list) { | 
| Eric Dumazet | 1c69d92 | 2006-12-06 20:38:44 -0800 | [diff] [blame] | 238 | 		next = list->next; | 
 | 239 | 		prefetch(next); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | 		list->func(list); | 
 | 241 | 		list = next; | 
| Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 242 | 		if (++count >= rdp->blimit) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | 			break; | 
 | 244 | 	} | 
| Eric Dumazet | 1c69d92 | 2006-12-06 20:38:44 -0800 | [diff] [blame] | 245 | 	rdp->donelist = list; | 
| Oleg Nesterov | dd9daa2 | 2006-09-12 20:35:55 -0700 | [diff] [blame] | 246 |  | 
 | 247 | 	local_irq_disable(); | 
 | 248 | 	rdp->qlen -= count; | 
 | 249 | 	local_irq_enable(); | 
| Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 250 | 	if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark) | 
 | 251 | 		rdp->blimit = blimit; | 
| Oleg Nesterov | dd9daa2 | 2006-09-12 20:35:55 -0700 | [diff] [blame] | 252 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | 	if (!rdp->donelist) | 
 | 254 | 		rdp->donetail = &rdp->donelist; | 
 | 255 | 	else | 
 | 256 | 		tasklet_schedule(&per_cpu(rcu_tasklet, rdp->cpu)); | 
 | 257 | } | 
 | 258 |  | 
 | 259 | /* | 
 | 260 |  * Grace period handling: | 
 | 261 |  * The grace period handling consists out of two steps: | 
 | 262 |  * - A new grace period is started. | 
 | 263 |  *   This is done by rcu_start_batch. The start is not broadcasted to | 
 | 264 |  *   all cpus, they must pick this up by comparing rcp->cur with | 
 | 265 |  *   rdp->quiescbatch. All cpus are recorded  in the | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 266 |  *   rcu_ctrlblk.cpumask bitmap. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 |  * - All cpus must go through a quiescent state. | 
 | 268 |  *   Since the start of the grace period is not broadcasted, at least two | 
 | 269 |  *   calls to rcu_check_quiescent_state are required: | 
 | 270 |  *   The first call just notices that a new grace period is running. The | 
 | 271 |  *   following calls check if there was a quiescent state since the beginning | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 272 |  *   of the grace period. If so, it updates rcu_ctrlblk.cpumask. If | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 |  *   the bitmap is empty, then the grace period is completed. | 
 | 274 |  *   rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace | 
 | 275 |  *   period (if necessary). | 
 | 276 |  */ | 
 | 277 | /* | 
 | 278 |  * Register a new batch of callbacks, and start it up if there is currently no | 
 | 279 |  * active batch and the batch to be registered has not already occurred. | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 280 |  * Caller must hold rcu_ctrlblk.lock. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 |  */ | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 282 | static void rcu_start_batch(struct rcu_ctrlblk *rcp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | 	if (rcp->next_pending && | 
 | 285 | 			rcp->completed == rcp->cur) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | 		rcp->next_pending = 0; | 
| Srivatsa Vaddagiri | c3f5902 | 2005-12-12 00:37:07 -0800 | [diff] [blame] | 287 | 		/* | 
 | 288 | 		 * next_pending == 0 must be visible in | 
 | 289 | 		 * __rcu_process_callbacks() before it can see new value of cur. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 		 */ | 
 | 291 | 		smp_wmb(); | 
 | 292 | 		rcp->cur++; | 
| Srivatsa Vaddagiri | c3f5902 | 2005-12-12 00:37:07 -0800 | [diff] [blame] | 293 |  | 
 | 294 | 		/* | 
 | 295 | 		 * Accessing nohz_cpu_mask before incrementing rcp->cur needs a | 
 | 296 | 		 * Barrier  Otherwise it can cause tickless idle CPUs to be | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 297 | 		 * included in rcp->cpumask, which will extend graceperiods | 
| Srivatsa Vaddagiri | c3f5902 | 2005-12-12 00:37:07 -0800 | [diff] [blame] | 298 | 		 * unnecessarily. | 
 | 299 | 		 */ | 
 | 300 | 		smp_mb(); | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 301 | 		cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask); | 
| Srivatsa Vaddagiri | c3f5902 | 2005-12-12 00:37:07 -0800 | [diff] [blame] | 302 |  | 
| Oleg Nesterov | 20e9751 | 2006-10-04 02:17:17 -0700 | [diff] [blame] | 303 | 		rcp->signaled = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | 	} | 
 | 305 | } | 
 | 306 |  | 
 | 307 | /* | 
 | 308 |  * cpu went through a quiescent state since the beginning of the grace period. | 
 | 309 |  * Clear it from the cpu mask and complete the grace period if it was the last | 
 | 310 |  * cpu. Start another grace period if someone has further entries pending | 
 | 311 |  */ | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 312 | static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 314 | 	cpu_clear(cpu, rcp->cpumask); | 
 | 315 | 	if (cpus_empty(rcp->cpumask)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | 		/* batch completed ! */ | 
 | 317 | 		rcp->completed = rcp->cur; | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 318 | 		rcu_start_batch(rcp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | 	} | 
 | 320 | } | 
 | 321 |  | 
 | 322 | /* | 
 | 323 |  * Check if the cpu has gone through a quiescent state (say context | 
 | 324 |  * switch). If so and if it already hasn't done so in this RCU | 
 | 325 |  * quiescent cycle, then indicate that it has done so. | 
 | 326 |  */ | 
 | 327 | static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp, | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 328 | 					struct rcu_data *rdp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { | 
 | 330 | 	if (rdp->quiescbatch != rcp->cur) { | 
 | 331 | 		/* start new grace period: */ | 
 | 332 | 		rdp->qs_pending = 1; | 
 | 333 | 		rdp->passed_quiesc = 0; | 
 | 334 | 		rdp->quiescbatch = rcp->cur; | 
 | 335 | 		return; | 
 | 336 | 	} | 
 | 337 |  | 
 | 338 | 	/* Grace period already completed for this cpu? | 
 | 339 | 	 * qs_pending is checked instead of the actual bitmap to avoid | 
 | 340 | 	 * cacheline trashing. | 
 | 341 | 	 */ | 
 | 342 | 	if (!rdp->qs_pending) | 
 | 343 | 		return; | 
 | 344 |  | 
 | 345 | 	/*  | 
 | 346 | 	 * Was there a quiescent state since the beginning of the grace | 
 | 347 | 	 * period? If no, then exit and wait for the next call. | 
 | 348 | 	 */ | 
 | 349 | 	if (!rdp->passed_quiesc) | 
 | 350 | 		return; | 
 | 351 | 	rdp->qs_pending = 0; | 
 | 352 |  | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 353 | 	spin_lock(&rcp->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | 	/* | 
 | 355 | 	 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync | 
 | 356 | 	 * during cpu startup. Ignore the quiescent state. | 
 | 357 | 	 */ | 
 | 358 | 	if (likely(rdp->quiescbatch == rcp->cur)) | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 359 | 		cpu_quiet(rdp->cpu, rcp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 |  | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 361 | 	spin_unlock(&rcp->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } | 
 | 363 |  | 
 | 364 |  | 
 | 365 | #ifdef CONFIG_HOTPLUG_CPU | 
 | 366 |  | 
 | 367 | /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing | 
 | 368 |  * locking requirements, the list it's pulling from has to belong to a cpu | 
 | 369 |  * which is dead and hence not processing interrupts. | 
 | 370 |  */ | 
 | 371 | static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list, | 
 | 372 | 				struct rcu_head **tail) | 
 | 373 | { | 
 | 374 | 	local_irq_disable(); | 
 | 375 | 	*this_rdp->nxttail = list; | 
 | 376 | 	if (list) | 
 | 377 | 		this_rdp->nxttail = tail; | 
 | 378 | 	local_irq_enable(); | 
 | 379 | } | 
 | 380 |  | 
 | 381 | static void __rcu_offline_cpu(struct rcu_data *this_rdp, | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 382 | 				struct rcu_ctrlblk *rcp, struct rcu_data *rdp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { | 
 | 384 | 	/* if the cpu going offline owns the grace period | 
 | 385 | 	 * we can block indefinitely waiting for it, so flush | 
 | 386 | 	 * it here | 
 | 387 | 	 */ | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 388 | 	spin_lock_bh(&rcp->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | 	if (rcp->cur != rcp->completed) | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 390 | 		cpu_quiet(rdp->cpu, rcp); | 
 | 391 | 	spin_unlock_bh(&rcp->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | 	rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail); | 
 | 393 | 	rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail); | 
| Oleg Nesterov | a9c8281 | 2006-01-10 17:24:53 +0300 | [diff] [blame] | 394 | 	rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } | 
| Oleg Nesterov | a9c8281 | 2006-01-10 17:24:53 +0300 | [diff] [blame] | 396 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | static void rcu_offline_cpu(int cpu) | 
 | 398 | { | 
 | 399 | 	struct rcu_data *this_rdp = &get_cpu_var(rcu_data); | 
 | 400 | 	struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data); | 
 | 401 |  | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 402 | 	__rcu_offline_cpu(this_rdp, &rcu_ctrlblk, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | 					&per_cpu(rcu_data, cpu)); | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 404 | 	__rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | 					&per_cpu(rcu_bh_data, cpu)); | 
 | 406 | 	put_cpu_var(rcu_data); | 
 | 407 | 	put_cpu_var(rcu_bh_data); | 
 | 408 | 	tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu); | 
 | 409 | } | 
 | 410 |  | 
 | 411 | #else | 
 | 412 |  | 
 | 413 | static void rcu_offline_cpu(int cpu) | 
 | 414 | { | 
 | 415 | } | 
 | 416 |  | 
 | 417 | #endif | 
 | 418 |  | 
 | 419 | /* | 
 | 420 |  * This does the RCU processing work from tasklet context.  | 
 | 421 |  */ | 
 | 422 | static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp, | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 423 | 					struct rcu_data *rdp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { | 
 | 425 | 	if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) { | 
 | 426 | 		*rdp->donetail = rdp->curlist; | 
 | 427 | 		rdp->donetail = rdp->curtail; | 
 | 428 | 		rdp->curlist = NULL; | 
 | 429 | 		rdp->curtail = &rdp->curlist; | 
 | 430 | 	} | 
 | 431 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | 	if (rdp->nxtlist && !rdp->curlist) { | 
| Oleg Nesterov | caa9ee7 | 2006-03-24 03:15:50 -0800 | [diff] [blame] | 433 | 		local_irq_disable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | 		rdp->curlist = rdp->nxtlist; | 
 | 435 | 		rdp->curtail = rdp->nxttail; | 
 | 436 | 		rdp->nxtlist = NULL; | 
 | 437 | 		rdp->nxttail = &rdp->nxtlist; | 
 | 438 | 		local_irq_enable(); | 
 | 439 |  | 
 | 440 | 		/* | 
 | 441 | 		 * start the next batch of callbacks | 
 | 442 | 		 */ | 
 | 443 |  | 
 | 444 | 		/* determine batch number */ | 
 | 445 | 		rdp->batch = rcp->cur + 1; | 
 | 446 | 		/* see the comment and corresponding wmb() in | 
 | 447 | 		 * the rcu_start_batch() | 
 | 448 | 		 */ | 
 | 449 | 		smp_rmb(); | 
 | 450 |  | 
 | 451 | 		if (!rcp->next_pending) { | 
 | 452 | 			/* and start it/schedule start if it's a new batch */ | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 453 | 			spin_lock(&rcp->lock); | 
| Oleg Nesterov | dbc1651 | 2006-01-08 22:19:33 +0300 | [diff] [blame] | 454 | 			rcp->next_pending = 1; | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 455 | 			rcu_start_batch(rcp); | 
 | 456 | 			spin_unlock(&rcp->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | 	} | 
| Oleg Nesterov | caa9ee7 | 2006-03-24 03:15:50 -0800 | [diff] [blame] | 459 |  | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 460 | 	rcu_check_quiescent_state(rcp, rdp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | 	if (rdp->donelist) | 
 | 462 | 		rcu_do_batch(rdp); | 
 | 463 | } | 
 | 464 |  | 
 | 465 | static void rcu_process_callbacks(unsigned long unused) | 
 | 466 | { | 
| Oleg Nesterov | 69a0b31 | 2006-01-10 16:48:02 +0300 | [diff] [blame] | 467 | 	__rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data)); | 
 | 468 | 	__rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } | 
 | 470 |  | 
| Oleg Nesterov | 6775177 | 2006-01-08 22:19:16 +0300 | [diff] [blame] | 471 | static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp) | 
 | 472 | { | 
 | 473 | 	/* This cpu has pending rcu entries and the grace period | 
 | 474 | 	 * for them has completed. | 
 | 475 | 	 */ | 
 | 476 | 	if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) | 
 | 477 | 		return 1; | 
 | 478 |  | 
 | 479 | 	/* This cpu has no pending entries, but there are new entries */ | 
 | 480 | 	if (!rdp->curlist && rdp->nxtlist) | 
 | 481 | 		return 1; | 
 | 482 |  | 
 | 483 | 	/* This cpu has finished callbacks to invoke */ | 
 | 484 | 	if (rdp->donelist) | 
 | 485 | 		return 1; | 
 | 486 |  | 
 | 487 | 	/* The rcu core waits for a quiescent state from the cpu */ | 
 | 488 | 	if (rdp->quiescbatch != rcp->cur || rdp->qs_pending) | 
 | 489 | 		return 1; | 
 | 490 |  | 
 | 491 | 	/* nothing to do */ | 
 | 492 | 	return 0; | 
 | 493 | } | 
 | 494 |  | 
| Heiko Carstens | 986733e | 2006-05-15 09:43:58 -0700 | [diff] [blame] | 495 | /* | 
 | 496 |  * Check to see if there is any immediate RCU-related work to be done | 
 | 497 |  * by the current CPU, returning 1 if so.  This function is part of the | 
 | 498 |  * RCU implementation; it is -not- an exported member of the RCU API. | 
 | 499 |  */ | 
| Oleg Nesterov | 6775177 | 2006-01-08 22:19:16 +0300 | [diff] [blame] | 500 | int rcu_pending(int cpu) | 
 | 501 | { | 
 | 502 | 	return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) || | 
 | 503 | 		__rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu)); | 
 | 504 | } | 
 | 505 |  | 
| Heiko Carstens | 986733e | 2006-05-15 09:43:58 -0700 | [diff] [blame] | 506 | /* | 
 | 507 |  * Check to see if any future RCU-related work will need to be done | 
 | 508 |  * by the current CPU, even if none need be done immediately, returning | 
 | 509 |  * 1 if so.  This function is part of the RCU implementation; it is -not- | 
 | 510 |  * an exported member of the RCU API. | 
 | 511 |  */ | 
 | 512 | int rcu_needs_cpu(int cpu) | 
 | 513 | { | 
 | 514 | 	struct rcu_data *rdp = &per_cpu(rcu_data, cpu); | 
 | 515 | 	struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu); | 
 | 516 |  | 
 | 517 | 	return (!!rdp->curlist || !!rdp_bh->curlist || rcu_pending(cpu)); | 
 | 518 | } | 
 | 519 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | void rcu_check_callbacks(int cpu, int user) | 
 | 521 | { | 
 | 522 | 	if (user ||  | 
 | 523 | 	    (idle_cpu(cpu) && !in_softirq() &&  | 
 | 524 | 				hardirq_count() <= (1 << HARDIRQ_SHIFT))) { | 
 | 525 | 		rcu_qsctr_inc(cpu); | 
 | 526 | 		rcu_bh_qsctr_inc(cpu); | 
 | 527 | 	} else if (!in_softirq()) | 
 | 528 | 		rcu_bh_qsctr_inc(cpu); | 
 | 529 | 	tasklet_schedule(&per_cpu(rcu_tasklet, cpu)); | 
 | 530 | } | 
 | 531 |  | 
 | 532 | static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp, | 
 | 533 | 						struct rcu_data *rdp) | 
 | 534 | { | 
 | 535 | 	memset(rdp, 0, sizeof(*rdp)); | 
 | 536 | 	rdp->curtail = &rdp->curlist; | 
 | 537 | 	rdp->nxttail = &rdp->nxtlist; | 
 | 538 | 	rdp->donetail = &rdp->donelist; | 
 | 539 | 	rdp->quiescbatch = rcp->completed; | 
 | 540 | 	rdp->qs_pending = 0; | 
 | 541 | 	rdp->cpu = cpu; | 
| Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 542 | 	rdp->blimit = blimit; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } | 
 | 544 |  | 
 | 545 | static void __devinit rcu_online_cpu(int cpu) | 
 | 546 | { | 
 | 547 | 	struct rcu_data *rdp = &per_cpu(rcu_data, cpu); | 
 | 548 | 	struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu); | 
 | 549 |  | 
 | 550 | 	rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp); | 
 | 551 | 	rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp); | 
 | 552 | 	tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL); | 
 | 553 | } | 
 | 554 |  | 
| Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 555 | static int __cpuinit rcu_cpu_notify(struct notifier_block *self, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | 				unsigned long action, void *hcpu) | 
 | 557 | { | 
 | 558 | 	long cpu = (long)hcpu; | 
 | 559 | 	switch (action) { | 
 | 560 | 	case CPU_UP_PREPARE: | 
 | 561 | 		rcu_online_cpu(cpu); | 
 | 562 | 		break; | 
 | 563 | 	case CPU_DEAD: | 
 | 564 | 		rcu_offline_cpu(cpu); | 
 | 565 | 		break; | 
 | 566 | 	default: | 
 | 567 | 		break; | 
 | 568 | 	} | 
 | 569 | 	return NOTIFY_OK; | 
 | 570 | } | 
 | 571 |  | 
| Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 572 | static struct notifier_block __cpuinitdata rcu_nb = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | 	.notifier_call	= rcu_cpu_notify, | 
 | 574 | }; | 
 | 575 |  | 
 | 576 | /* | 
 | 577 |  * Initializes rcu mechanism.  Assumed to be called early. | 
 | 578 |  * That is before local timer(SMP) or jiffie timer (uniproc) is setup. | 
 | 579 |  * Note that rcu_qsctr and friends are implicitly | 
 | 580 |  * initialized due to the choice of ``0'' for RCU_CTR_INVALID. | 
 | 581 |  */ | 
 | 582 | void __init rcu_init(void) | 
 | 583 | { | 
 | 584 | 	rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, | 
 | 585 | 			(void *)(long)smp_processor_id()); | 
 | 586 | 	/* Register notifier for non-boot CPUs */ | 
 | 587 | 	register_cpu_notifier(&rcu_nb); | 
 | 588 | } | 
 | 589 |  | 
 | 590 | struct rcu_synchronize { | 
 | 591 | 	struct rcu_head head; | 
 | 592 | 	struct completion completion; | 
 | 593 | }; | 
 | 594 |  | 
 | 595 | /* Because of FASTCALL declaration of complete, we use this wrapper */ | 
 | 596 | static void wakeme_after_rcu(struct rcu_head  *head) | 
 | 597 | { | 
 | 598 | 	struct rcu_synchronize *rcu; | 
 | 599 |  | 
 | 600 | 	rcu = container_of(head, struct rcu_synchronize, head); | 
 | 601 | 	complete(&rcu->completion); | 
 | 602 | } | 
 | 603 |  | 
 | 604 | /** | 
| Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 605 |  * synchronize_rcu - wait until a grace period has elapsed. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 |  * | 
 | 607 |  * Control will return to the caller some time after a full grace | 
 | 608 |  * period has elapsed, in other words after all currently executing RCU | 
 | 609 |  * read-side critical sections have completed.  RCU read-side critical | 
 | 610 |  * sections are delimited by rcu_read_lock() and rcu_read_unlock(), | 
 | 611 |  * and may be nested. | 
| Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 612 |  * | 
 | 613 |  * If your read-side code is not protected by rcu_read_lock(), do -not- | 
 | 614 |  * use synchronize_rcu(). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 |  */ | 
| Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 616 | void synchronize_rcu(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { | 
 | 618 | 	struct rcu_synchronize rcu; | 
 | 619 |  | 
 | 620 | 	init_completion(&rcu.completion); | 
 | 621 | 	/* Will wake me after RCU finished */ | 
 | 622 | 	call_rcu(&rcu.head, wakeme_after_rcu); | 
 | 623 |  | 
 | 624 | 	/* Wait for it */ | 
 | 625 | 	wait_for_completion(&rcu.completion); | 
 | 626 | } | 
 | 627 |  | 
| Dipankar Sarma | 21a1ea9 | 2006-03-07 21:55:33 -0800 | [diff] [blame] | 628 | module_param(blimit, int, 0); | 
 | 629 | module_param(qhimark, int, 0); | 
 | 630 | module_param(qlowmark, int, 0); | 
| Paul E. McKenney | a241ec6 | 2005-10-30 15:03:12 -0800 | [diff] [blame] | 631 | EXPORT_SYMBOL_GPL(rcu_batches_completed); | 
| Paul E. McKenney | c32e066 | 2006-06-27 02:54:04 -0700 | [diff] [blame] | 632 | EXPORT_SYMBOL_GPL(rcu_batches_completed_bh); | 
| Paul E. McKenney | d83015b | 2006-06-23 02:05:51 -0700 | [diff] [blame] | 633 | EXPORT_SYMBOL_GPL(call_rcu); | 
 | 634 | EXPORT_SYMBOL_GPL(call_rcu_bh); | 
| Paul E. McKenney | 9b06e81 | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 635 | EXPORT_SYMBOL_GPL(synchronize_rcu); |