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