| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Sleepable 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, 2006 | 
 | 19 |  * | 
 | 20 |  * Author: Paul McKenney <paulmck@us.ibm.com> | 
 | 21 |  * | 
 | 22 |  * For detailed explanation of Read-Copy Update mechanism see - | 
 | 23 |  * 		Documentation/RCU/ *.txt | 
 | 24 |  * | 
 | 25 |  */ | 
 | 26 |  | 
 | 27 | #include <linux/module.h> | 
 | 28 | #include <linux/mutex.h> | 
 | 29 | #include <linux/percpu.h> | 
 | 30 | #include <linux/preempt.h> | 
 | 31 | #include <linux/rcupdate.h> | 
 | 32 | #include <linux/sched.h> | 
 | 33 | #include <linux/slab.h> | 
 | 34 | #include <linux/smp.h> | 
 | 35 | #include <linux/srcu.h> | 
 | 36 |  | 
 | 37 | /** | 
 | 38 |  * init_srcu_struct - initialize a sleep-RCU structure | 
 | 39 |  * @sp: structure to initialize. | 
 | 40 |  * | 
 | 41 |  * Must invoke this on a given srcu_struct before passing that srcu_struct | 
 | 42 |  * to any other function.  Each srcu_struct represents a separate domain | 
 | 43 |  * of SRCU protection. | 
 | 44 |  */ | 
| Alan Stern | e6a9201 | 2006-10-04 02:17:05 -0700 | [diff] [blame] | 45 | int init_srcu_struct(struct srcu_struct *sp) | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 46 | { | 
 | 47 | 	sp->completed = 0; | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 48 | 	mutex_init(&sp->mutex); | 
| Alan Stern | e6a9201 | 2006-10-04 02:17:05 -0700 | [diff] [blame] | 49 | 	sp->per_cpu_ref = alloc_percpu(struct srcu_struct_array); | 
 | 50 | 	return (sp->per_cpu_ref ? 0 : -ENOMEM); | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 51 | } | 
| Paul E. McKenney | 0cd397d | 2009-10-25 19:03:51 -0700 | [diff] [blame] | 52 | EXPORT_SYMBOL_GPL(init_srcu_struct); | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 53 |  | 
 | 54 | /* | 
 | 55 |  * srcu_readers_active_idx -- returns approximate number of readers | 
 | 56 |  *	active on the specified rank of per-CPU counters. | 
 | 57 |  */ | 
 | 58 |  | 
 | 59 | static int srcu_readers_active_idx(struct srcu_struct *sp, int idx) | 
 | 60 | { | 
 | 61 | 	int cpu; | 
 | 62 | 	int sum; | 
 | 63 |  | 
 | 64 | 	sum = 0; | 
 | 65 | 	for_each_possible_cpu(cpu) | 
 | 66 | 		sum += per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx]; | 
 | 67 | 	return sum; | 
 | 68 | } | 
 | 69 |  | 
 | 70 | /** | 
 | 71 |  * srcu_readers_active - returns approximate number of readers. | 
 | 72 |  * @sp: which srcu_struct to count active readers (holding srcu_read_lock). | 
 | 73 |  * | 
 | 74 |  * Note that this is not an atomic primitive, and can therefore suffer | 
 | 75 |  * severe errors when invoked on an active srcu_struct.  That said, it | 
 | 76 |  * can be useful as an error check at cleanup time. | 
 | 77 |  */ | 
| Adrian Bunk | bb69517 | 2008-02-06 01:36:45 -0800 | [diff] [blame] | 78 | static int srcu_readers_active(struct srcu_struct *sp) | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 79 | { | 
 | 80 | 	return srcu_readers_active_idx(sp, 0) + srcu_readers_active_idx(sp, 1); | 
 | 81 | } | 
 | 82 |  | 
 | 83 | /** | 
 | 84 |  * cleanup_srcu_struct - deconstruct a sleep-RCU structure | 
 | 85 |  * @sp: structure to clean up. | 
 | 86 |  * | 
 | 87 |  * Must invoke this after you are finished using a given srcu_struct that | 
 | 88 |  * was initialized via init_srcu_struct(), else you leak memory. | 
 | 89 |  */ | 
 | 90 | void cleanup_srcu_struct(struct srcu_struct *sp) | 
 | 91 | { | 
 | 92 | 	int sum; | 
 | 93 |  | 
 | 94 | 	sum = srcu_readers_active(sp); | 
 | 95 | 	WARN_ON(sum);  /* Leakage unless caller handles error. */ | 
 | 96 | 	if (sum != 0) | 
 | 97 | 		return; | 
 | 98 | 	free_percpu(sp->per_cpu_ref); | 
 | 99 | 	sp->per_cpu_ref = NULL; | 
 | 100 | } | 
| Paul E. McKenney | 0cd397d | 2009-10-25 19:03:51 -0700 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(cleanup_srcu_struct); | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 102 |  | 
 | 103 | /** | 
 | 104 |  * srcu_read_lock - register a new reader for an SRCU-protected structure. | 
 | 105 |  * @sp: srcu_struct in which to register the new reader. | 
 | 106 |  * | 
 | 107 |  * Counts the new reader in the appropriate per-CPU element of the | 
 | 108 |  * srcu_struct.  Must be called from process context. | 
 | 109 |  * Returns an index that must be passed to the matching srcu_read_unlock(). | 
 | 110 |  */ | 
 | 111 | int srcu_read_lock(struct srcu_struct *sp) | 
 | 112 | { | 
 | 113 | 	int idx; | 
 | 114 |  | 
 | 115 | 	preempt_disable(); | 
 | 116 | 	idx = sp->completed & 0x1; | 
 | 117 | 	barrier();  /* ensure compiler looks -once- at sp->completed. */ | 
 | 118 | 	per_cpu_ptr(sp->per_cpu_ref, smp_processor_id())->c[idx]++; | 
 | 119 | 	srcu_barrier();  /* ensure compiler won't misorder critical section. */ | 
 | 120 | 	preempt_enable(); | 
 | 121 | 	return idx; | 
 | 122 | } | 
| Paul E. McKenney | 0cd397d | 2009-10-25 19:03:51 -0700 | [diff] [blame] | 123 | EXPORT_SYMBOL_GPL(srcu_read_lock); | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 124 |  | 
 | 125 | /** | 
 | 126 |  * srcu_read_unlock - unregister a old reader from an SRCU-protected structure. | 
 | 127 |  * @sp: srcu_struct in which to unregister the old reader. | 
 | 128 |  * @idx: return value from corresponding srcu_read_lock(). | 
 | 129 |  * | 
 | 130 |  * Removes the count for the old reader from the appropriate per-CPU | 
 | 131 |  * element of the srcu_struct.  Note that this may well be a different | 
 | 132 |  * CPU than that which was incremented by the corresponding srcu_read_lock(). | 
 | 133 |  * Must be called from process context. | 
 | 134 |  */ | 
 | 135 | void srcu_read_unlock(struct srcu_struct *sp, int idx) | 
 | 136 | { | 
 | 137 | 	preempt_disable(); | 
 | 138 | 	srcu_barrier();  /* ensure compiler won't misorder critical section. */ | 
 | 139 | 	per_cpu_ptr(sp->per_cpu_ref, smp_processor_id())->c[idx]--; | 
 | 140 | 	preempt_enable(); | 
 | 141 | } | 
| Paul E. McKenney | 0cd397d | 2009-10-25 19:03:51 -0700 | [diff] [blame] | 142 | EXPORT_SYMBOL_GPL(srcu_read_unlock); | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 143 |  | 
| Paul E. McKenney | 0cd397d | 2009-10-25 19:03:51 -0700 | [diff] [blame] | 144 | /* | 
 | 145 |  * Helper function for synchronize_srcu() and synchronize_srcu_expedited(). | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 146 |  */ | 
| Paul E. McKenney | 0cd397d | 2009-10-25 19:03:51 -0700 | [diff] [blame] | 147 | void __synchronize_srcu(struct srcu_struct *sp, void (*sync_func)(void)) | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 148 | { | 
 | 149 | 	int idx; | 
 | 150 |  | 
 | 151 | 	idx = sp->completed; | 
 | 152 | 	mutex_lock(&sp->mutex); | 
 | 153 |  | 
 | 154 | 	/* | 
 | 155 | 	 * Check to see if someone else did the work for us while we were | 
 | 156 | 	 * waiting to acquire the lock.  We need -two- advances of | 
 | 157 | 	 * the counter, not just one.  If there was but one, we might have | 
 | 158 | 	 * shown up -after- our helper's first synchronize_sched(), thus | 
 | 159 | 	 * having failed to prevent CPU-reordering races with concurrent | 
 | 160 | 	 * srcu_read_unlock()s on other CPUs (see comment below).  So we | 
 | 161 | 	 * either (1) wait for two or (2) supply the second ourselves. | 
 | 162 | 	 */ | 
 | 163 |  | 
 | 164 | 	if ((sp->completed - idx) >= 2) { | 
 | 165 | 		mutex_unlock(&sp->mutex); | 
 | 166 | 		return; | 
 | 167 | 	} | 
 | 168 |  | 
| Paul E. McKenney | 0cd397d | 2009-10-25 19:03:51 -0700 | [diff] [blame] | 169 | 	sync_func();  /* Force memory barrier on all CPUs. */ | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 170 |  | 
 | 171 | 	/* | 
 | 172 | 	 * The preceding synchronize_sched() ensures that any CPU that | 
 | 173 | 	 * sees the new value of sp->completed will also see any preceding | 
 | 174 | 	 * changes to data structures made by this CPU.  This prevents | 
 | 175 | 	 * some other CPU from reordering the accesses in its SRCU | 
 | 176 | 	 * read-side critical section to precede the corresponding | 
 | 177 | 	 * srcu_read_lock() -- ensuring that such references will in | 
 | 178 | 	 * fact be protected. | 
 | 179 | 	 * | 
 | 180 | 	 * So it is now safe to do the flip. | 
 | 181 | 	 */ | 
 | 182 |  | 
 | 183 | 	idx = sp->completed & 0x1; | 
 | 184 | 	sp->completed++; | 
 | 185 |  | 
| Paul E. McKenney | 0cd397d | 2009-10-25 19:03:51 -0700 | [diff] [blame] | 186 | 	sync_func();  /* Force memory barrier on all CPUs. */ | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 187 |  | 
 | 188 | 	/* | 
 | 189 | 	 * At this point, because of the preceding synchronize_sched(), | 
 | 190 | 	 * all srcu_read_lock() calls using the old counters have completed. | 
 | 191 | 	 * Their corresponding critical sections might well be still | 
 | 192 | 	 * executing, but the srcu_read_lock() primitives themselves | 
 | 193 | 	 * will have finished executing. | 
 | 194 | 	 */ | 
 | 195 |  | 
 | 196 | 	while (srcu_readers_active_idx(sp, idx)) | 
 | 197 | 		schedule_timeout_interruptible(1); | 
 | 198 |  | 
| Paul E. McKenney | 0cd397d | 2009-10-25 19:03:51 -0700 | [diff] [blame] | 199 | 	sync_func();  /* Force memory barrier on all CPUs. */ | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 200 |  | 
 | 201 | 	/* | 
 | 202 | 	 * The preceding synchronize_sched() forces all srcu_read_unlock() | 
 | 203 | 	 * primitives that were executing concurrently with the preceding | 
 | 204 | 	 * for_each_possible_cpu() loop to have completed by this point. | 
 | 205 | 	 * More importantly, it also forces the corresponding SRCU read-side | 
 | 206 | 	 * critical sections to have also completed, and the corresponding | 
 | 207 | 	 * references to SRCU-protected data items to be dropped. | 
 | 208 | 	 * | 
 | 209 | 	 * Note: | 
 | 210 | 	 * | 
 | 211 | 	 *	Despite what you might think at first glance, the | 
 | 212 | 	 *	preceding synchronize_sched() -must- be within the | 
 | 213 | 	 *	critical section ended by the following mutex_unlock(). | 
 | 214 | 	 *	Otherwise, a task taking the early exit can race | 
 | 215 | 	 *	with a srcu_read_unlock(), which might have executed | 
 | 216 | 	 *	just before the preceding srcu_readers_active() check, | 
 | 217 | 	 *	and whose CPU might have reordered the srcu_read_unlock() | 
 | 218 | 	 *	with the preceding critical section.  In this case, there | 
 | 219 | 	 *	is nothing preventing the synchronize_sched() task that is | 
 | 220 | 	 *	taking the early exit from freeing a data structure that | 
 | 221 | 	 *	is still being referenced (out of order) by the task | 
 | 222 | 	 *	doing the srcu_read_unlock(). | 
 | 223 | 	 * | 
 | 224 | 	 *	Alternatively, the comparison with "2" on the early exit | 
 | 225 | 	 *	could be changed to "3", but this increases synchronize_srcu() | 
 | 226 | 	 *	latency for bulk loads.  So the current code is preferred. | 
 | 227 | 	 */ | 
 | 228 |  | 
 | 229 | 	mutex_unlock(&sp->mutex); | 
 | 230 | } | 
 | 231 |  | 
 | 232 | /** | 
| Paul E. McKenney | 0cd397d | 2009-10-25 19:03:51 -0700 | [diff] [blame] | 233 |  * synchronize_srcu - wait for prior SRCU read-side critical-section completion | 
 | 234 |  * @sp: srcu_struct with which to synchronize. | 
 | 235 |  * | 
 | 236 |  * Flip the completed counter, and wait for the old count to drain to zero. | 
 | 237 |  * As with classic RCU, the updater must use some separate means of | 
 | 238 |  * synchronizing concurrent updates.  Can block; must be called from | 
 | 239 |  * process context. | 
 | 240 |  * | 
 | 241 |  * Note that it is illegal to call synchronize_srcu() from the corresponding | 
 | 242 |  * SRCU read-side critical section; doing so will result in deadlock. | 
 | 243 |  * However, it is perfectly legal to call synchronize_srcu() on one | 
 | 244 |  * srcu_struct from some other srcu_struct's read-side critical section. | 
 | 245 |  */ | 
 | 246 | void synchronize_srcu(struct srcu_struct *sp) | 
 | 247 | { | 
 | 248 | 	__synchronize_srcu(sp, synchronize_sched); | 
 | 249 | } | 
 | 250 | EXPORT_SYMBOL_GPL(synchronize_srcu); | 
 | 251 |  | 
 | 252 | /** | 
 | 253 |  * synchronize_srcu_expedited - like synchronize_srcu, but less patient | 
 | 254 |  * @sp: srcu_struct with which to synchronize. | 
 | 255 |  * | 
 | 256 |  * Flip the completed counter, and wait for the old count to drain to zero. | 
 | 257 |  * As with classic RCU, the updater must use some separate means of | 
 | 258 |  * synchronizing concurrent updates.  Can block; must be called from | 
 | 259 |  * process context. | 
 | 260 |  * | 
 | 261 |  * Note that it is illegal to call synchronize_srcu_expedited() | 
 | 262 |  * from the corresponding SRCU read-side critical section; doing so | 
 | 263 |  * will result in deadlock.  However, it is perfectly legal to call | 
 | 264 |  * synchronize_srcu_expedited() on one srcu_struct from some other | 
 | 265 |  * srcu_struct's read-side critical section. | 
 | 266 |  */ | 
 | 267 | void synchronize_srcu_expedited(struct srcu_struct *sp) | 
 | 268 | { | 
 | 269 | 	__synchronize_srcu(sp, synchronize_sched_expedited); | 
 | 270 | } | 
 | 271 | EXPORT_SYMBOL_GPL(synchronize_srcu_expedited); | 
 | 272 |  | 
 | 273 | /** | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 274 |  * srcu_batches_completed - return batches completed. | 
 | 275 |  * @sp: srcu_struct on which to report batch completion. | 
 | 276 |  * | 
 | 277 |  * Report the number of batches, correlated with, but not necessarily | 
 | 278 |  * precisely the same as, the number of grace periods that have elapsed. | 
 | 279 |  */ | 
 | 280 |  | 
 | 281 | long srcu_batches_completed(struct srcu_struct *sp) | 
 | 282 | { | 
 | 283 | 	return sp->completed; | 
 | 284 | } | 
| Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 285 | EXPORT_SYMBOL_GPL(srcu_batches_completed); |