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/export.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/smp.h>
34 #include <linux/delay.h>
35 #include <linux/srcu.h>
37 static int init_srcu_struct_fields(struct srcu_struct *sp)
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 /* 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 return init_srcu_struct_fields(sp);
55 EXPORT_SYMBOL_GPL(__init_srcu_struct);
57 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
60 * init_srcu_struct - initialize a sleep-RCU structure
61 * @sp: structure to initialize.
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
67 int init_srcu_struct(struct srcu_struct *sp)
69 return init_srcu_struct_fields(sp);
71 EXPORT_SYMBOL_GPL(init_srcu_struct);
73 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
76 * Returns approximate total of the readers' ->seq[] values for the
77 * rank of per-CPU counters specified by idx.
79 static unsigned long srcu_readers_seq_idx(struct srcu_struct *sp, int idx)
82 unsigned long sum = 0;
85 for_each_possible_cpu(cpu) {
86 t = ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->seq[idx]);
93 * Returns approximate number of readers active on the specified rank
94 * of the per-CPU ->c[] counters.
96 static unsigned long srcu_readers_active_idx(struct srcu_struct *sp, int idx)
99 unsigned long sum = 0;
102 for_each_possible_cpu(cpu) {
103 t = ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx]);
110 * Return true if the number of pre-existing readers is determined to
111 * be stably zero. An example unstable zero can occur if the call
112 * to srcu_readers_active_idx() misses an __srcu_read_lock() increment,
113 * but due to task migration, sees the corresponding __srcu_read_unlock()
114 * decrement. This can happen because srcu_readers_active_idx() takes
115 * time to sum the array, and might in fact be interrupted or preempted
116 * partway through the summation.
118 static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
122 seq = srcu_readers_seq_idx(sp, idx);
125 * The following smp_mb() A pairs with the smp_mb() B located in
126 * __srcu_read_lock(). This pairing ensures that if an
127 * __srcu_read_lock() increments its counter after the summation
128 * in srcu_readers_active_idx(), then the corresponding SRCU read-side
129 * critical section will see any changes made prior to the start
130 * of the current SRCU grace period.
132 * Also, if the above call to srcu_readers_seq_idx() saw the
133 * increment of ->seq[], then the call to srcu_readers_active_idx()
134 * must see the increment of ->c[].
139 * Note that srcu_readers_active_idx() can incorrectly return
140 * zero even though there is a pre-existing reader throughout.
141 * To see this, suppose that task A is in a very long SRCU
142 * read-side critical section that started on CPU 0, and that
143 * no other reader exists, so that the sum of the counters
144 * is equal to one. Then suppose that task B starts executing
145 * srcu_readers_active_idx(), summing up to CPU 1, and then that
146 * task C starts reading on CPU 0, so that its increment is not
147 * summed, but finishes reading on CPU 2, so that its decrement
148 * -is- summed. Then when task B completes its sum, it will
149 * incorrectly get zero, despite the fact that task A has been
150 * in its SRCU read-side critical section the whole time.
152 * We therefore do a validation step should srcu_readers_active_idx()
155 if (srcu_readers_active_idx(sp, idx) != 0)
159 * The remainder of this function is the validation step.
160 * The following smp_mb() D pairs with the smp_mb() C in
161 * __srcu_read_unlock(). If the __srcu_read_unlock() was seen
162 * by srcu_readers_active_idx() above, then any destructive
163 * operation performed after the grace period will happen after
164 * the corresponding SRCU read-side critical section.
166 * Note that there can be at most NR_CPUS worth of readers using
167 * the old index, which is not enough to overflow even a 32-bit
168 * integer. (Yes, this does mean that systems having more than
169 * a billion or so CPUs need to be 64-bit systems.) Therefore,
170 * the sum of the ->seq[] counters cannot possibly overflow.
171 * Therefore, the only way that the return values of the two
172 * calls to srcu_readers_seq_idx() can be equal is if there were
173 * no increments of the corresponding rank of ->seq[] counts
174 * in the interim. But the missed-increment scenario laid out
175 * above includes an increment of the ->seq[] counter by
176 * the corresponding __srcu_read_lock(). Therefore, if this
177 * scenario occurs, the return values from the two calls to
178 * srcu_readers_seq_idx() will differ, and thus the validation
179 * step below suffices.
183 return srcu_readers_seq_idx(sp, idx) == seq;
187 * srcu_readers_active - returns approximate number of readers.
188 * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
190 * Note that this is not an atomic primitive, and can therefore suffer
191 * severe errors when invoked on an active srcu_struct. That said, it
192 * can be useful as an error check at cleanup time.
194 static int srcu_readers_active(struct srcu_struct *sp)
196 return srcu_readers_active_idx(sp, 0) + srcu_readers_active_idx(sp, 1);
200 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
201 * @sp: structure to clean up.
203 * Must invoke this after you are finished using a given srcu_struct that
204 * was initialized via init_srcu_struct(), else you leak memory.
206 void cleanup_srcu_struct(struct srcu_struct *sp)
210 sum = srcu_readers_active(sp);
211 WARN_ON(sum); /* Leakage unless caller handles error. */
214 free_percpu(sp->per_cpu_ref);
215 sp->per_cpu_ref = NULL;
217 EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
220 * Counts the new reader in the appropriate per-CPU element of the
221 * srcu_struct. Must be called from process context.
222 * Returns an index that must be passed to the matching srcu_read_unlock().
224 int __srcu_read_lock(struct srcu_struct *sp)
229 idx = rcu_dereference_index_check(sp->completed,
230 rcu_read_lock_sched_held()) & 0x1;
231 ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->c[idx]) += 1;
232 smp_mb(); /* B */ /* Avoid leaking the critical section. */
233 ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->seq[idx]) += 1;
237 EXPORT_SYMBOL_GPL(__srcu_read_lock);
240 * Removes the count for the old reader from the appropriate per-CPU
241 * element of the srcu_struct. Note that this may well be a different
242 * CPU than that which was incremented by the corresponding srcu_read_lock().
243 * Must be called from process context.
245 void __srcu_read_unlock(struct srcu_struct *sp, int idx)
248 smp_mb(); /* C */ /* Avoid leaking the critical section. */
249 ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->c[idx]) -= 1;
252 EXPORT_SYMBOL_GPL(__srcu_read_unlock);
255 * We use an adaptive strategy for synchronize_srcu() and especially for
256 * synchronize_srcu_expedited(). We spin for a fixed time period
257 * (defined below) to allow SRCU readers to exit their read-side critical
258 * sections. If there are still some readers after 10 microseconds,
259 * we repeatedly block for 1-millisecond time periods. This approach
260 * has done well in testing, so there is no need for a config parameter.
262 #define SYNCHRONIZE_SRCU_READER_DELAY 5
265 * Wait until all pre-existing readers complete. Such readers
266 * will have used the index specified by "idx".
268 static void wait_idx(struct srcu_struct *sp, int idx, bool expedited)
273 * SRCU read-side critical sections are normally short, so wait
274 * a small amount of time before possibly blocking.
276 if (!srcu_readers_active_idx_check(sp, idx)) {
277 udelay(SYNCHRONIZE_SRCU_READER_DELAY);
278 while (!srcu_readers_active_idx_check(sp, idx)) {
279 if (expedited && ++ trycount < 10)
280 udelay(SYNCHRONIZE_SRCU_READER_DELAY);
282 schedule_timeout_interruptible(1);
287 static void srcu_flip(struct srcu_struct *sp)
293 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
295 static void __synchronize_srcu(struct srcu_struct *sp, bool expedited)
299 rcu_lockdep_assert(!lock_is_held(&sp->dep_map) &&
300 !lock_is_held(&rcu_bh_lock_map) &&
301 !lock_is_held(&rcu_lock_map) &&
302 !lock_is_held(&rcu_sched_lock_map),
303 "Illegal synchronize_srcu() in same-type SRCU (or RCU) read-side critical section");
305 mutex_lock(&sp->mutex);
306 busy_idx = sp->completed & 0X1UL;
309 * If we recently flipped the index, there will be some readers
310 * using idx=0 and others using idx=1. Therefore, two calls to
311 * wait_idx()s suffice to ensure that all pre-existing readers
314 * __synchronize_srcu() {
315 * wait_idx(sp, 0, expedited);
316 * wait_idx(sp, 1, expedited);
319 * Starvation is prevented by the fact that we flip the index.
320 * While we wait on one index to clear out, almost all new readers
321 * will be using the other index. The number of new readers using the
322 * index we are waiting on is sharply bounded by roughly the number
325 * How can new readers possibly using the old pre-flip value of
326 * the index? Consider the following sequence of events:
328 * 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.
336 * However, this sequence of events is quite improbable, so
337 * this call to wait_idx(), which waits on really old readers
338 * describe in this comment above, will almost never need to wait.
340 wait_idx(sp, 1 - busy_idx, expedited);
342 /* Flip the index to avoid reader-induced starvation. */
345 /* Wait for recent pre-existing readers. */
346 wait_idx(sp, busy_idx, expedited);
348 mutex_unlock(&sp->mutex);
352 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
353 * @sp: srcu_struct with which to synchronize.
355 * Flip the completed counter, and wait for the old count to drain to zero.
356 * As with classic RCU, the updater must use some separate means of
357 * synchronizing concurrent updates. Can block; must be called from
360 * Note that it is illegal to call synchronize_srcu() from the corresponding
361 * SRCU read-side critical section; doing so will result in deadlock.
362 * However, it is perfectly legal to call synchronize_srcu() on one
363 * srcu_struct from some other srcu_struct's read-side critical section.
365 void synchronize_srcu(struct srcu_struct *sp)
367 __synchronize_srcu(sp, 0);
369 EXPORT_SYMBOL_GPL(synchronize_srcu);
372 * synchronize_srcu_expedited - Brute-force SRCU grace period
373 * @sp: srcu_struct with which to synchronize.
375 * Wait for an SRCU grace period to elapse, but be more aggressive about
376 * spinning rather than blocking when waiting.
378 * Note that it is illegal to call this function while holding any lock
379 * that is acquired by a CPU-hotplug notifier. It is also illegal to call
380 * synchronize_srcu_expedited() from the corresponding SRCU read-side
381 * critical section; doing so will result in deadlock. However, it is
382 * perfectly legal to call synchronize_srcu_expedited() on one srcu_struct
383 * from some other srcu_struct's read-side critical section, as long as
384 * the resulting graph of srcu_structs is acyclic.
386 void synchronize_srcu_expedited(struct srcu_struct *sp)
388 __synchronize_srcu(sp, 1);
390 EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
393 * srcu_batches_completed - return batches completed.
394 * @sp: srcu_struct on which to report batch completion.
396 * Report the number of batches, correlated with, but not necessarily
397 * precisely the same as, the number of grace periods that have elapsed.
400 long srcu_batches_completed(struct srcu_struct *sp)
402 return sp->completed;
404 EXPORT_SYMBOL_GPL(srcu_batches_completed);