wireless: rt2x00: rt2800usb: be in sync with latest windows drivers.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / srcu.c
blobbde4295774c8f921f235fd4d2efebbd726132b38
1 /*
2 * Sleepable 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, 2006
20 * Author: Paul McKenney <paulmck@us.ibm.com>
22 * For detailed explanation of Read-Copy Update mechanism see -
23 * Documentation/RCU/ *.txt
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>
37 static int init_srcu_struct_fields(struct srcu_struct *sp)
39 sp->completed = 0;
40 mutex_init(&sp->mutex);
41 sp->per_cpu_ref = alloc_percpu(struct srcu_struct_array);
42 return sp->per_cpu_ref ? 0 : -ENOMEM;
45 #ifdef CONFIG_DEBUG_LOCK_ALLOC
47 int __init_srcu_struct(struct srcu_struct *sp, const char *name,
48 struct lock_class_key *key)
50 #ifdef CONFIG_DEBUG_LOCK_ALLOC
51 /* Don't re-initialize a lock while it is held. */
52 debug_check_no_locks_freed((void *)sp, sizeof(*sp));
53 lockdep_init_map(&sp->dep_map, name, key, 0);
54 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
55 return init_srcu_struct_fields(sp);
57 EXPORT_SYMBOL_GPL(__init_srcu_struct);
59 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
61 /**
62 * init_srcu_struct - initialize a sleep-RCU structure
63 * @sp: structure to initialize.
65 * Must invoke this on a given srcu_struct before passing that srcu_struct
66 * to any other function. Each srcu_struct represents a separate domain
67 * of SRCU protection.
69 int init_srcu_struct(struct srcu_struct *sp)
71 return init_srcu_struct_fields(sp);
73 EXPORT_SYMBOL_GPL(init_srcu_struct);
75 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
78 * srcu_readers_active_idx -- returns approximate number of readers
79 * active on the specified rank of per-CPU counters.
82 static int srcu_readers_active_idx(struct srcu_struct *sp, int idx)
84 int cpu;
85 int sum;
87 sum = 0;
88 for_each_possible_cpu(cpu)
89 sum += per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx];
90 return sum;
93 /**
94 * srcu_readers_active - returns approximate number of readers.
95 * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
97 * Note that this is not an atomic primitive, and can therefore suffer
98 * severe errors when invoked on an active srcu_struct. That said, it
99 * can be useful as an error check at cleanup time.
101 static int srcu_readers_active(struct srcu_struct *sp)
103 return srcu_readers_active_idx(sp, 0) + srcu_readers_active_idx(sp, 1);
107 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
108 * @sp: structure to clean up.
110 * Must invoke this after you are finished using a given srcu_struct that
111 * was initialized via init_srcu_struct(), else you leak memory.
113 void cleanup_srcu_struct(struct srcu_struct *sp)
115 int sum;
117 sum = srcu_readers_active(sp);
118 WARN_ON(sum); /* Leakage unless caller handles error. */
119 if (sum != 0)
120 return;
121 free_percpu(sp->per_cpu_ref);
122 sp->per_cpu_ref = NULL;
124 EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
127 * Counts the new reader in the appropriate per-CPU element of the
128 * srcu_struct. Must be called from process context.
129 * Returns an index that must be passed to the matching srcu_read_unlock().
131 int __srcu_read_lock(struct srcu_struct *sp)
133 int idx;
135 preempt_disable();
136 idx = sp->completed & 0x1;
137 barrier(); /* ensure compiler looks -once- at sp->completed. */
138 per_cpu_ptr(sp->per_cpu_ref, smp_processor_id())->c[idx]++;
139 srcu_barrier(); /* ensure compiler won't misorder critical section. */
140 preempt_enable();
141 return idx;
143 EXPORT_SYMBOL_GPL(__srcu_read_lock);
146 * Removes the count for the old reader from the appropriate per-CPU
147 * element of the srcu_struct. Note that this may well be a different
148 * CPU than that which was incremented by the corresponding srcu_read_lock().
149 * Must be called from process context.
151 void __srcu_read_unlock(struct srcu_struct *sp, int idx)
153 preempt_disable();
154 srcu_barrier(); /* ensure compiler won't misorder critical section. */
155 per_cpu_ptr(sp->per_cpu_ref, smp_processor_id())->c[idx]--;
156 preempt_enable();
158 EXPORT_SYMBOL_GPL(__srcu_read_unlock);
161 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
163 static void __synchronize_srcu(struct srcu_struct *sp, void (*sync_func)(void))
165 int idx;
167 idx = sp->completed;
168 mutex_lock(&sp->mutex);
171 * Check to see if someone else did the work for us while we were
172 * waiting to acquire the lock. We need -two- advances of
173 * the counter, not just one. If there was but one, we might have
174 * shown up -after- our helper's first synchronize_sched(), thus
175 * having failed to prevent CPU-reordering races with concurrent
176 * srcu_read_unlock()s on other CPUs (see comment below). So we
177 * either (1) wait for two or (2) supply the second ourselves.
180 if ((sp->completed - idx) >= 2) {
181 mutex_unlock(&sp->mutex);
182 return;
185 sync_func(); /* Force memory barrier on all CPUs. */
188 * The preceding synchronize_sched() ensures that any CPU that
189 * sees the new value of sp->completed will also see any preceding
190 * changes to data structures made by this CPU. This prevents
191 * some other CPU from reordering the accesses in its SRCU
192 * read-side critical section to precede the corresponding
193 * srcu_read_lock() -- ensuring that such references will in
194 * fact be protected.
196 * So it is now safe to do the flip.
199 idx = sp->completed & 0x1;
200 sp->completed++;
202 sync_func(); /* Force memory barrier on all CPUs. */
205 * At this point, because of the preceding synchronize_sched(),
206 * all srcu_read_lock() calls using the old counters have completed.
207 * Their corresponding critical sections might well be still
208 * executing, but the srcu_read_lock() primitives themselves
209 * will have finished executing.
212 while (srcu_readers_active_idx(sp, idx))
213 schedule_timeout_interruptible(1);
215 sync_func(); /* Force memory barrier on all CPUs. */
218 * The preceding synchronize_sched() forces all srcu_read_unlock()
219 * primitives that were executing concurrently with the preceding
220 * for_each_possible_cpu() loop to have completed by this point.
221 * More importantly, it also forces the corresponding SRCU read-side
222 * critical sections to have also completed, and the corresponding
223 * references to SRCU-protected data items to be dropped.
225 * Note:
227 * Despite what you might think at first glance, the
228 * preceding synchronize_sched() -must- be within the
229 * critical section ended by the following mutex_unlock().
230 * Otherwise, a task taking the early exit can race
231 * with a srcu_read_unlock(), which might have executed
232 * just before the preceding srcu_readers_active() check,
233 * and whose CPU might have reordered the srcu_read_unlock()
234 * with the preceding critical section. In this case, there
235 * is nothing preventing the synchronize_sched() task that is
236 * taking the early exit from freeing a data structure that
237 * is still being referenced (out of order) by the task
238 * doing the srcu_read_unlock().
240 * Alternatively, the comparison with "2" on the early exit
241 * could be changed to "3", but this increases synchronize_srcu()
242 * latency for bulk loads. So the current code is preferred.
245 mutex_unlock(&sp->mutex);
249 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
250 * @sp: srcu_struct with which to synchronize.
252 * Flip the completed counter, and wait for the old count to drain to zero.
253 * As with classic RCU, the updater must use some separate means of
254 * synchronizing concurrent updates. Can block; must be called from
255 * process context.
257 * Note that it is illegal to call synchronize_srcu() from the corresponding
258 * SRCU read-side critical section; doing so will result in deadlock.
259 * However, it is perfectly legal to call synchronize_srcu() on one
260 * srcu_struct from some other srcu_struct's read-side critical section.
262 void synchronize_srcu(struct srcu_struct *sp)
264 __synchronize_srcu(sp, synchronize_sched);
266 EXPORT_SYMBOL_GPL(synchronize_srcu);
269 * synchronize_srcu_expedited - like synchronize_srcu, but less patient
270 * @sp: srcu_struct with which to synchronize.
272 * Flip the completed counter, and wait for the old count to drain to zero.
273 * As with classic RCU, the updater must use some separate means of
274 * synchronizing concurrent updates. Can block; must be called from
275 * process context.
277 * Note that it is illegal to call synchronize_srcu_expedited()
278 * from the corresponding SRCU read-side critical section; doing so
279 * will result in deadlock. However, it is perfectly legal to call
280 * synchronize_srcu_expedited() on one srcu_struct from some other
281 * srcu_struct's read-side critical section.
283 void synchronize_srcu_expedited(struct srcu_struct *sp)
285 __synchronize_srcu(sp, synchronize_sched_expedited);
287 EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
290 * srcu_batches_completed - return batches completed.
291 * @sp: srcu_struct on which to report batch completion.
293 * Report the number of batches, correlated with, but not necessarily
294 * precisely the same as, the number of grace periods that have elapsed.
297 long srcu_batches_completed(struct srcu_struct *sp)
299 return sp->completed;
301 EXPORT_SYMBOL_GPL(srcu_batches_completed);