blob: b6b9ea2eb51c289672c3bec1699552e92c556940 [file] [log] [blame]
Paul E. McKenney621934e2006-10-04 02:17:02 -07001/*
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
Paul Gortmaker9984de12011-05-23 14:51:41 -040027#include <linux/export.h>
Paul E. McKenney621934e2006-10-04 02:17:02 -070028#include <linux/mutex.h>
29#include <linux/percpu.h>
30#include <linux/preempt.h>
31#include <linux/rcupdate.h>
32#include <linux/sched.h>
Paul E. McKenney621934e2006-10-04 02:17:02 -070033#include <linux/smp.h>
Paul E. McKenney46fdb092010-10-26 02:11:40 -070034#include <linux/delay.h>
Paul E. McKenney621934e2006-10-04 02:17:02 -070035#include <linux/srcu.h>
36
Paul E. McKenney632ee202010-02-22 17:04:45 -080037static int init_srcu_struct_fields(struct srcu_struct *sp)
38{
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;
43}
44
45#ifdef CONFIG_DEBUG_LOCK_ALLOC
46
47int __init_srcu_struct(struct srcu_struct *sp, const char *name,
48 struct lock_class_key *key)
49{
Paul E. McKenney632ee202010-02-22 17:04:45 -080050 /* 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);
Paul E. McKenney632ee202010-02-22 17:04:45 -080053 return init_srcu_struct_fields(sp);
54}
55EXPORT_SYMBOL_GPL(__init_srcu_struct);
56
57#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
58
Paul E. McKenney621934e2006-10-04 02:17:02 -070059/**
60 * init_srcu_struct - initialize a sleep-RCU structure
61 * @sp: structure to initialize.
62 *
63 * Must invoke this on a given srcu_struct before passing that srcu_struct
64 * to any other function. Each srcu_struct represents a separate domain
65 * of SRCU protection.
66 */
Alan Sterne6a92012006-10-04 02:17:05 -070067int init_srcu_struct(struct srcu_struct *sp)
Paul E. McKenney621934e2006-10-04 02:17:02 -070068{
Paul E. McKenney632ee202010-02-22 17:04:45 -080069 return init_srcu_struct_fields(sp);
Paul E. McKenney621934e2006-10-04 02:17:02 -070070}
Paul E. McKenney0cd397d2009-10-25 19:03:51 -070071EXPORT_SYMBOL_GPL(init_srcu_struct);
Paul E. McKenney621934e2006-10-04 02:17:02 -070072
Paul E. McKenney632ee202010-02-22 17:04:45 -080073#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
74
Paul E. McKenney621934e2006-10-04 02:17:02 -070075/*
Paul E. McKenneycef50122012-02-05 07:42:44 -080076 * Returns approximate number of readers active on the specified rank
77 * of per-CPU counters. Also snapshots each counter's value in the
78 * corresponding element of sp->snap[] for later use validating
79 * the sum.
Paul E. McKenney621934e2006-10-04 02:17:02 -070080 */
Paul E. McKenneycef50122012-02-05 07:42:44 -080081static unsigned long srcu_readers_active_idx(struct srcu_struct *sp, int idx)
Paul E. McKenney621934e2006-10-04 02:17:02 -070082{
83 int cpu;
Paul E. McKenneycef50122012-02-05 07:42:44 -080084 unsigned long sum = 0;
85 unsigned long t;
Paul E. McKenney621934e2006-10-04 02:17:02 -070086
Paul E. McKenneycef50122012-02-05 07:42:44 -080087 for_each_possible_cpu(cpu) {
88 t = ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx]);
89 sum += t;
90 sp->snap[cpu] = t;
91 }
92 return sum & SRCU_REF_MASK;
93}
94
95/*
96 * To be called from the update side after an index flip. Returns true
97 * if the modulo sum of the counters is stably zero, false if there is
98 * some possibility of non-zero.
99 */
100static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
101{
102 int cpu;
103
104 /*
105 * Note that srcu_readers_active_idx() can incorrectly return
106 * zero even though there is a pre-existing reader throughout.
107 * To see this, suppose that task A is in a very long SRCU
108 * read-side critical section that started on CPU 0, and that
109 * no other reader exists, so that the modulo sum of the counters
110 * is equal to one. Then suppose that task B starts executing
111 * srcu_readers_active_idx(), summing up to CPU 1, and then that
112 * task C starts reading on CPU 0, so that its increment is not
113 * summed, but finishes reading on CPU 2, so that its decrement
114 * -is- summed. Then when task B completes its sum, it will
115 * incorrectly get zero, despite the fact that task A has been
116 * in its SRCU read-side critical section the whole time.
117 *
118 * We therefore do a validation step should srcu_readers_active_idx()
119 * return zero.
120 */
121 if (srcu_readers_active_idx(sp, idx) != 0)
122 return false;
123
124 /*
125 * Since the caller recently flipped ->completed, we can see at
126 * most one increment of each CPU's counter from this point
127 * forward. The reason for this is that the reader CPU must have
128 * fetched the index before srcu_readers_active_idx checked
129 * that CPU's counter, but not yet incremented its counter.
130 * Its eventual counter increment will follow the read in
131 * srcu_readers_active_idx(), and that increment is immediately
132 * followed by smp_mb() B. Because smp_mb() D is between
133 * the ->completed flip and srcu_readers_active_idx()'s read,
134 * that CPU's subsequent load of ->completed must see the new
135 * value, and therefore increment the counter in the other rank.
136 */
137 smp_mb(); /* A */
138
139 /*
140 * Now, we check the ->snap array that srcu_readers_active_idx()
Lai Jiangshan440253c2012-02-22 13:29:06 -0800141 * filled in from the per-CPU counter values. Since
142 * __srcu_read_lock() increments the upper bits of the per-CPU
143 * counter, an increment/decrement pair will change the value
144 * of the counter. Since there is only one possible increment,
145 * the only way to wrap the counter is to have a huge number of
146 * counter decrements, which requires a huge number of tasks and
147 * huge SRCU read-side critical-section nesting levels, even on
148 * 32-bit systems.
Paul E. McKenneycef50122012-02-05 07:42:44 -0800149 *
150 * All of the ways of confusing the readings require that the scan
151 * in srcu_readers_active_idx() see the read-side task's decrement,
152 * but not its increment. However, between that decrement and
153 * increment are smb_mb() B and C. Either or both of these pair
154 * with smp_mb() A above to ensure that the scan below will see
155 * the read-side tasks's increment, thus noting a difference in
156 * the counter values between the two passes.
157 *
158 * Therefore, if srcu_readers_active_idx() returned zero, and
159 * none of the counters changed, we know that the zero was the
160 * correct sum.
161 *
162 * Of course, it is possible that a task might be delayed
163 * for a very long time in __srcu_read_lock() after fetching
164 * the index but before incrementing its counter. This
165 * possibility will be dealt with in __synchronize_srcu().
166 */
Paul E. McKenney621934e2006-10-04 02:17:02 -0700167 for_each_possible_cpu(cpu)
Paul E. McKenneycef50122012-02-05 07:42:44 -0800168 if (sp->snap[cpu] !=
169 ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx]))
170 return false; /* False zero reading! */
171 return true;
Paul E. McKenney621934e2006-10-04 02:17:02 -0700172}
173
174/**
175 * srcu_readers_active - returns approximate number of readers.
176 * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
177 *
178 * Note that this is not an atomic primitive, and can therefore suffer
179 * severe errors when invoked on an active srcu_struct. That said, it
180 * can be useful as an error check at cleanup time.
181 */
Adrian Bunkbb695172008-02-06 01:36:45 -0800182static int srcu_readers_active(struct srcu_struct *sp)
Paul E. McKenney621934e2006-10-04 02:17:02 -0700183{
184 return srcu_readers_active_idx(sp, 0) + srcu_readers_active_idx(sp, 1);
185}
186
187/**
188 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
189 * @sp: structure to clean up.
190 *
191 * Must invoke this after you are finished using a given srcu_struct that
192 * was initialized via init_srcu_struct(), else you leak memory.
193 */
194void cleanup_srcu_struct(struct srcu_struct *sp)
195{
196 int sum;
197
198 sum = srcu_readers_active(sp);
199 WARN_ON(sum); /* Leakage unless caller handles error. */
200 if (sum != 0)
201 return;
202 free_percpu(sp->per_cpu_ref);
203 sp->per_cpu_ref = NULL;
204}
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700205EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
Paul E. McKenney621934e2006-10-04 02:17:02 -0700206
Paul E. McKenney632ee202010-02-22 17:04:45 -0800207/*
Paul E. McKenney621934e2006-10-04 02:17:02 -0700208 * Counts the new reader in the appropriate per-CPU element of the
209 * srcu_struct. Must be called from process context.
210 * Returns an index that must be passed to the matching srcu_read_unlock().
211 */
Paul E. McKenney632ee202010-02-22 17:04:45 -0800212int __srcu_read_lock(struct srcu_struct *sp)
Paul E. McKenney621934e2006-10-04 02:17:02 -0700213{
214 int idx;
215
216 preempt_disable();
Paul E. McKenneycef50122012-02-05 07:42:44 -0800217 idx = rcu_dereference_index_check(sp->completed,
218 rcu_read_lock_sched_held()) & 0x1;
219 ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->c[idx]) +=
220 SRCU_USAGE_COUNT + 1;
221 smp_mb(); /* B */ /* Avoid leaking the critical section. */
Paul E. McKenney621934e2006-10-04 02:17:02 -0700222 preempt_enable();
223 return idx;
224}
Paul E. McKenney632ee202010-02-22 17:04:45 -0800225EXPORT_SYMBOL_GPL(__srcu_read_lock);
Paul E. McKenney621934e2006-10-04 02:17:02 -0700226
Paul E. McKenney632ee202010-02-22 17:04:45 -0800227/*
Paul E. McKenney621934e2006-10-04 02:17:02 -0700228 * Removes the count for the old reader from the appropriate per-CPU
229 * element of the srcu_struct. Note that this may well be a different
230 * CPU than that which was incremented by the corresponding srcu_read_lock().
231 * Must be called from process context.
232 */
Paul E. McKenney632ee202010-02-22 17:04:45 -0800233void __srcu_read_unlock(struct srcu_struct *sp, int idx)
Paul E. McKenney621934e2006-10-04 02:17:02 -0700234{
235 preempt_disable();
Paul E. McKenneycef50122012-02-05 07:42:44 -0800236 smp_mb(); /* C */ /* Avoid leaking the critical section. */
Lai Jiangshan440253c2012-02-22 13:29:06 -0800237 ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->c[idx]) -= 1;
Paul E. McKenney621934e2006-10-04 02:17:02 -0700238 preempt_enable();
239}
Paul E. McKenney632ee202010-02-22 17:04:45 -0800240EXPORT_SYMBOL_GPL(__srcu_read_unlock);
Paul E. McKenney621934e2006-10-04 02:17:02 -0700241
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700242/*
Paul E. McKenneyc072a382011-01-07 02:33:47 -0800243 * We use an adaptive strategy for synchronize_srcu() and especially for
244 * synchronize_srcu_expedited(). We spin for a fixed time period
245 * (defined below) to allow SRCU readers to exit their read-side critical
246 * sections. If there are still some readers after 10 microseconds,
247 * we repeatedly block for 1-millisecond time periods. This approach
248 * has done well in testing, so there is no need for a config parameter.
249 */
Paul E. McKenneycef50122012-02-05 07:42:44 -0800250#define SYNCHRONIZE_SRCU_READER_DELAY 5
251
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800252static void wait_idx(struct srcu_struct *sp, int idx, bool expedited)
Paul E. McKenneycef50122012-02-05 07:42:44 -0800253{
Paul E. McKenneycef50122012-02-05 07:42:44 -0800254 int trycount = 0;
255
Paul E. McKenneycef50122012-02-05 07:42:44 -0800256 /*
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800257 * If a reader fetches the index before the ->completed increment,
Paul E. McKenneycef50122012-02-05 07:42:44 -0800258 * but increments its counter after srcu_readers_active_idx_check()
259 * sums it, then smp_mb() D will pair with __srcu_read_lock()'s
260 * smp_mb() B to ensure that the SRCU read-side critical section
261 * will see any updates that the current task performed before its
262 * call to synchronize_srcu(), or to synchronize_srcu_expedited(),
263 * as the case may be.
264 */
265 smp_mb(); /* D */
266
267 /*
268 * SRCU read-side critical sections are normally short, so wait
269 * a small amount of time before possibly blocking.
270 */
271 if (!srcu_readers_active_idx_check(sp, idx)) {
272 udelay(SYNCHRONIZE_SRCU_READER_DELAY);
273 while (!srcu_readers_active_idx_check(sp, idx)) {
274 if (expedited && ++ trycount < 10)
275 udelay(SYNCHRONIZE_SRCU_READER_DELAY);
276 else
277 schedule_timeout_interruptible(1);
278 }
279 }
280
281 /*
282 * The following smp_mb() E pairs with srcu_read_unlock()'s
283 * smp_mb C to ensure that if srcu_readers_active_idx_check()
284 * sees srcu_read_unlock()'s counter decrement, then any
285 * of the current task's subsequent code will happen after
286 * that SRCU read-side critical section.
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800287 *
288 * It also ensures the order between the above waiting and
289 * the next flipping.
Paul E. McKenneycef50122012-02-05 07:42:44 -0800290 */
291 smp_mb(); /* E */
292}
Paul E. McKenneyc072a382011-01-07 02:33:47 -0800293
294/*
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800295 * Flip the readers' index by incrementing ->completed, then wait
296 * until there are no more readers using the counters referenced by
297 * the old index value. (Recall that the index is the bottom bit
298 * of ->completed.)
299 *
300 * Of course, it is possible that a reader might be delayed for the
301 * full duration of flip_idx_and_wait() between fetching the
302 * index and incrementing its counter. This possibility is handled
303 * by the next __synchronize_srcu() invoking wait_idx() for such readers
304 * before starting a new grace period.
305 */
306static void flip_idx_and_wait(struct srcu_struct *sp, bool expedited)
307{
308 int idx;
309
310 idx = sp->completed++ & 0x1;
311 wait_idx(sp, idx, expedited);
312}
313
314/*
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700315 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
Paul E. McKenney621934e2006-10-04 02:17:02 -0700316 */
Paul E. McKenneycef50122012-02-05 07:42:44 -0800317static void __synchronize_srcu(struct srcu_struct *sp, bool expedited)
Paul E. McKenney621934e2006-10-04 02:17:02 -0700318{
Paul E. McKenneyfe15d702012-01-04 13:30:33 -0800319 rcu_lockdep_assert(!lock_is_held(&sp->dep_map) &&
320 !lock_is_held(&rcu_bh_lock_map) &&
321 !lock_is_held(&rcu_lock_map) &&
322 !lock_is_held(&rcu_sched_lock_map),
323 "Illegal synchronize_srcu() in same-type SRCU (or RCU) read-side critical section");
324
Paul E. McKenney621934e2006-10-04 02:17:02 -0700325 mutex_lock(&sp->mutex);
326
327 /*
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800328 * Suppose that during the previous grace period, a reader
329 * picked up the old value of the index, but did not increment
330 * its counter until after the previous instance of
331 * __synchronize_srcu() did the counter summation and recheck.
332 * That previous grace period was OK because the reader did
333 * not start until after the grace period started, so the grace
334 * period was not obligated to wait for that reader.
Paul E. McKenney621934e2006-10-04 02:17:02 -0700335 *
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800336 * However, the current SRCU grace period does have to wait for
337 * that reader. This is handled by invoking wait_idx() on the
338 * non-active set of counters (hence sp->completed - 1). Once
339 * wait_idx() returns, we know that all readers that picked up
340 * the old value of ->completed and that already incremented their
341 * counter will have completed.
Paul E. McKenneycef50122012-02-05 07:42:44 -0800342 *
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800343 * But what about readers that picked up the old value of
344 * ->completed, but -still- have not managed to increment their
345 * counter? We do not need to wait for those readers, because
346 * they will have started their SRCU read-side critical section
347 * after the current grace period starts.
348 *
349 * Because it is unlikely that readers will be preempted between
350 * fetching ->completed and incrementing their counter, wait_idx()
351 * will normally not need to wait.
Paul E. McKenney621934e2006-10-04 02:17:02 -0700352 */
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800353 wait_idx(sp, (sp->completed - 1) & 0x1, expedited);
354
355 /*
356 * Now that wait_idx() has waited for the really old readers,
357 * invoke flip_idx_and_wait() to flip the counter and wait
358 * for current SRCU readers.
359 */
360 flip_idx_and_wait(sp, expedited);
361
Paul E. McKenney621934e2006-10-04 02:17:02 -0700362 mutex_unlock(&sp->mutex);
363}
364
365/**
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700366 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
367 * @sp: srcu_struct with which to synchronize.
368 *
369 * Flip the completed counter, and wait for the old count to drain to zero.
370 * As with classic RCU, the updater must use some separate means of
371 * synchronizing concurrent updates. Can block; must be called from
372 * process context.
373 *
374 * Note that it is illegal to call synchronize_srcu() from the corresponding
375 * SRCU read-side critical section; doing so will result in deadlock.
376 * However, it is perfectly legal to call synchronize_srcu() on one
377 * srcu_struct from some other srcu_struct's read-side critical section.
378 */
379void synchronize_srcu(struct srcu_struct *sp)
380{
Paul E. McKenneycef50122012-02-05 07:42:44 -0800381 __synchronize_srcu(sp, 0);
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700382}
383EXPORT_SYMBOL_GPL(synchronize_srcu);
384
385/**
Paul E. McKenney236fefa2012-01-31 14:00:41 -0800386 * synchronize_srcu_expedited - Brute-force SRCU grace period
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700387 * @sp: srcu_struct with which to synchronize.
388 *
Paul E. McKenneycef50122012-02-05 07:42:44 -0800389 * Wait for an SRCU grace period to elapse, but be more aggressive about
390 * spinning rather than blocking when waiting.
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700391 *
Paul E. McKenney236fefa2012-01-31 14:00:41 -0800392 * Note that it is illegal to call this function while holding any lock
Paul E. McKenneycef50122012-02-05 07:42:44 -0800393 * that is acquired by a CPU-hotplug notifier. It is also illegal to call
Paul E. McKenney236fefa2012-01-31 14:00:41 -0800394 * synchronize_srcu_expedited() from the corresponding SRCU read-side
395 * critical section; doing so will result in deadlock. However, it is
396 * perfectly legal to call synchronize_srcu_expedited() on one srcu_struct
397 * from some other srcu_struct's read-side critical section, as long as
398 * the resulting graph of srcu_structs is acyclic.
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700399 */
400void synchronize_srcu_expedited(struct srcu_struct *sp)
401{
Paul E. McKenneycef50122012-02-05 07:42:44 -0800402 __synchronize_srcu(sp, 1);
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700403}
404EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
405
406/**
Paul E. McKenney621934e2006-10-04 02:17:02 -0700407 * srcu_batches_completed - return batches completed.
408 * @sp: srcu_struct on which to report batch completion.
409 *
410 * Report the number of batches, correlated with, but not necessarily
411 * precisely the same as, the number of grace periods that have elapsed.
412 */
413
414long srcu_batches_completed(struct srcu_struct *sp)
415{
416 return sp->completed;
417}
Paul E. McKenney621934e2006-10-04 02:17:02 -0700418EXPORT_SYMBOL_GPL(srcu_batches_completed);