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