blob: de9074047c92beffc237be9ecf93ca52b814a31d [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
Antti P Miettinen3705b882012-10-05 09:59:15 +030037#include <trace/events/rcu.h>
38
39#include "rcu.h"
40
Lai Jiangshan931ea9d2012-03-19 16:12:13 +080041/*
42 * Initialize an rcu_batch structure to empty.
43 */
44static inline void rcu_batch_init(struct rcu_batch *b)
45{
46 b->head = NULL;
47 b->tail = &b->head;
48}
49
50/*
51 * Enqueue a callback onto the tail of the specified rcu_batch structure.
52 */
53static inline void rcu_batch_queue(struct rcu_batch *b, struct rcu_head *head)
54{
55 *b->tail = head;
56 b->tail = &head->next;
57}
58
59/*
60 * Is the specified rcu_batch structure empty?
61 */
62static inline bool rcu_batch_empty(struct rcu_batch *b)
63{
64 return b->tail == &b->head;
65}
66
67/*
68 * Remove the callback at the head of the specified rcu_batch structure
69 * and return a pointer to it, or return NULL if the structure is empty.
70 */
71static inline struct rcu_head *rcu_batch_dequeue(struct rcu_batch *b)
72{
73 struct rcu_head *head;
74
75 if (rcu_batch_empty(b))
76 return NULL;
77
78 head = b->head;
79 b->head = head->next;
80 if (b->tail == &head->next)
81 rcu_batch_init(b);
82
83 return head;
84}
85
86/*
87 * Move all callbacks from the rcu_batch structure specified by "from" to
88 * the structure specified by "to".
89 */
90static inline void rcu_batch_move(struct rcu_batch *to, struct rcu_batch *from)
91{
92 if (!rcu_batch_empty(from)) {
93 *to->tail = from->head;
94 to->tail = from->tail;
95 rcu_batch_init(from);
96 }
97}
98
99/* single-thread state-machine */
100static void process_srcu(struct work_struct *work);
101
Paul E. McKenney632ee202010-02-22 17:04:45 -0800102static int init_srcu_struct_fields(struct srcu_struct *sp)
103{
104 sp->completed = 0;
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800105 spin_lock_init(&sp->queue_lock);
106 sp->running = false;
107 rcu_batch_init(&sp->batch_queue);
108 rcu_batch_init(&sp->batch_check0);
109 rcu_batch_init(&sp->batch_check1);
110 rcu_batch_init(&sp->batch_done);
111 INIT_DELAYED_WORK(&sp->work, process_srcu);
Paul E. McKenney632ee202010-02-22 17:04:45 -0800112 sp->per_cpu_ref = alloc_percpu(struct srcu_struct_array);
113 return sp->per_cpu_ref ? 0 : -ENOMEM;
114}
115
116#ifdef CONFIG_DEBUG_LOCK_ALLOC
117
118int __init_srcu_struct(struct srcu_struct *sp, const char *name,
119 struct lock_class_key *key)
120{
Paul E. McKenney632ee202010-02-22 17:04:45 -0800121 /* Don't re-initialize a lock while it is held. */
122 debug_check_no_locks_freed((void *)sp, sizeof(*sp));
123 lockdep_init_map(&sp->dep_map, name, key, 0);
Paul E. McKenney632ee202010-02-22 17:04:45 -0800124 return init_srcu_struct_fields(sp);
125}
126EXPORT_SYMBOL_GPL(__init_srcu_struct);
127
128#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
129
Paul E. McKenney621934e2006-10-04 02:17:02 -0700130/**
131 * init_srcu_struct - initialize a sleep-RCU structure
132 * @sp: structure to initialize.
133 *
134 * Must invoke this on a given srcu_struct before passing that srcu_struct
135 * to any other function. Each srcu_struct represents a separate domain
136 * of SRCU protection.
137 */
Alan Sterne6a92012006-10-04 02:17:05 -0700138int init_srcu_struct(struct srcu_struct *sp)
Paul E. McKenney621934e2006-10-04 02:17:02 -0700139{
Paul E. McKenney632ee202010-02-22 17:04:45 -0800140 return init_srcu_struct_fields(sp);
Paul E. McKenney621934e2006-10-04 02:17:02 -0700141}
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700142EXPORT_SYMBOL_GPL(init_srcu_struct);
Paul E. McKenney621934e2006-10-04 02:17:02 -0700143
Paul E. McKenney632ee202010-02-22 17:04:45 -0800144#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
145
Paul E. McKenney621934e2006-10-04 02:17:02 -0700146/*
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800147 * Returns approximate total of the readers' ->seq[] values for the
148 * rank of per-CPU counters specified by idx.
149 */
150static unsigned long srcu_readers_seq_idx(struct srcu_struct *sp, int idx)
151{
152 int cpu;
153 unsigned long sum = 0;
154 unsigned long t;
155
156 for_each_possible_cpu(cpu) {
157 t = ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->seq[idx]);
158 sum += t;
159 }
160 return sum;
161}
162
163/*
Paul E. McKenneycef50122012-02-05 07:42:44 -0800164 * Returns approximate number of readers active on the specified rank
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800165 * of the per-CPU ->c[] counters.
Paul E. McKenney621934e2006-10-04 02:17:02 -0700166 */
Paul E. McKenneycef50122012-02-05 07:42:44 -0800167static unsigned long srcu_readers_active_idx(struct srcu_struct *sp, int idx)
Paul E. McKenney621934e2006-10-04 02:17:02 -0700168{
169 int cpu;
Paul E. McKenneycef50122012-02-05 07:42:44 -0800170 unsigned long sum = 0;
171 unsigned long t;
Paul E. McKenney621934e2006-10-04 02:17:02 -0700172
Paul E. McKenneycef50122012-02-05 07:42:44 -0800173 for_each_possible_cpu(cpu) {
174 t = ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx]);
175 sum += t;
Paul E. McKenneycef50122012-02-05 07:42:44 -0800176 }
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800177 return sum;
Paul E. McKenneycef50122012-02-05 07:42:44 -0800178}
179
180/*
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800181 * Return true if the number of pre-existing readers is determined to
182 * be stably zero. An example unstable zero can occur if the call
183 * to srcu_readers_active_idx() misses an __srcu_read_lock() increment,
184 * but due to task migration, sees the corresponding __srcu_read_unlock()
185 * decrement. This can happen because srcu_readers_active_idx() takes
186 * time to sum the array, and might in fact be interrupted or preempted
187 * partway through the summation.
Paul E. McKenneycef50122012-02-05 07:42:44 -0800188 */
189static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
190{
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800191 unsigned long seq;
192
193 seq = srcu_readers_seq_idx(sp, idx);
194
195 /*
196 * The following smp_mb() A pairs with the smp_mb() B located in
197 * __srcu_read_lock(). This pairing ensures that if an
198 * __srcu_read_lock() increments its counter after the summation
199 * in srcu_readers_active_idx(), then the corresponding SRCU read-side
200 * critical section will see any changes made prior to the start
201 * of the current SRCU grace period.
202 *
203 * Also, if the above call to srcu_readers_seq_idx() saw the
204 * increment of ->seq[], then the call to srcu_readers_active_idx()
205 * must see the increment of ->c[].
206 */
207 smp_mb(); /* A */
Paul E. McKenneycef50122012-02-05 07:42:44 -0800208
209 /*
210 * Note that srcu_readers_active_idx() can incorrectly return
211 * zero even though there is a pre-existing reader throughout.
212 * To see this, suppose that task A is in a very long SRCU
213 * read-side critical section that started on CPU 0, and that
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800214 * no other reader exists, so that the sum of the counters
Paul E. McKenneycef50122012-02-05 07:42:44 -0800215 * is equal to one. Then suppose that task B starts executing
216 * srcu_readers_active_idx(), summing up to CPU 1, and then that
217 * task C starts reading on CPU 0, so that its increment is not
218 * summed, but finishes reading on CPU 2, so that its decrement
219 * -is- summed. Then when task B completes its sum, it will
220 * incorrectly get zero, despite the fact that task A has been
221 * in its SRCU read-side critical section the whole time.
222 *
223 * We therefore do a validation step should srcu_readers_active_idx()
224 * return zero.
225 */
226 if (srcu_readers_active_idx(sp, idx) != 0)
227 return false;
228
229 /*
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800230 * The remainder of this function is the validation step.
231 * The following smp_mb() D pairs with the smp_mb() C in
232 * __srcu_read_unlock(). If the __srcu_read_unlock() was seen
233 * by srcu_readers_active_idx() above, then any destructive
234 * operation performed after the grace period will happen after
235 * the corresponding SRCU read-side critical section.
236 *
237 * Note that there can be at most NR_CPUS worth of readers using
238 * the old index, which is not enough to overflow even a 32-bit
239 * integer. (Yes, this does mean that systems having more than
240 * a billion or so CPUs need to be 64-bit systems.) Therefore,
241 * the sum of the ->seq[] counters cannot possibly overflow.
242 * Therefore, the only way that the return values of the two
243 * calls to srcu_readers_seq_idx() can be equal is if there were
244 * no increments of the corresponding rank of ->seq[] counts
245 * in the interim. But the missed-increment scenario laid out
246 * above includes an increment of the ->seq[] counter by
247 * the corresponding __srcu_read_lock(). Therefore, if this
248 * scenario occurs, the return values from the two calls to
249 * srcu_readers_seq_idx() will differ, and thus the validation
250 * step below suffices.
Paul E. McKenneycef50122012-02-05 07:42:44 -0800251 */
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800252 smp_mb(); /* D */
Paul E. McKenneycef50122012-02-05 07:42:44 -0800253
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800254 return srcu_readers_seq_idx(sp, idx) == seq;
Paul E. McKenney621934e2006-10-04 02:17:02 -0700255}
256
257/**
258 * srcu_readers_active - returns approximate number of readers.
259 * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
260 *
261 * Note that this is not an atomic primitive, and can therefore suffer
262 * severe errors when invoked on an active srcu_struct. That said, it
263 * can be useful as an error check at cleanup time.
264 */
Adrian Bunkbb695172008-02-06 01:36:45 -0800265static int srcu_readers_active(struct srcu_struct *sp)
Paul E. McKenney621934e2006-10-04 02:17:02 -0700266{
Lai Jiangshandc879172012-03-06 17:57:34 +0800267 int cpu;
268 unsigned long sum = 0;
269
270 for_each_possible_cpu(cpu) {
271 sum += ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[0]);
272 sum += ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[1]);
273 }
274 return sum;
Paul E. McKenney621934e2006-10-04 02:17:02 -0700275}
276
277/**
278 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
279 * @sp: structure to clean up.
280 *
281 * Must invoke this after you are finished using a given srcu_struct that
282 * was initialized via init_srcu_struct(), else you leak memory.
283 */
284void cleanup_srcu_struct(struct srcu_struct *sp)
285{
286 int sum;
287
288 sum = srcu_readers_active(sp);
289 WARN_ON(sum); /* Leakage unless caller handles error. */
290 if (sum != 0)
291 return;
292 free_percpu(sp->per_cpu_ref);
293 sp->per_cpu_ref = NULL;
294}
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700295EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
Paul E. McKenney621934e2006-10-04 02:17:02 -0700296
Paul E. McKenney632ee202010-02-22 17:04:45 -0800297/*
Paul E. McKenney621934e2006-10-04 02:17:02 -0700298 * Counts the new reader in the appropriate per-CPU element of the
299 * srcu_struct. Must be called from process context.
300 * Returns an index that must be passed to the matching srcu_read_unlock().
301 */
Paul E. McKenney632ee202010-02-22 17:04:45 -0800302int __srcu_read_lock(struct srcu_struct *sp)
Paul E. McKenney621934e2006-10-04 02:17:02 -0700303{
304 int idx;
305
306 preempt_disable();
Paul E. McKenneycef50122012-02-05 07:42:44 -0800307 idx = rcu_dereference_index_check(sp->completed,
308 rcu_read_lock_sched_held()) & 0x1;
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800309 ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->c[idx]) += 1;
Paul E. McKenneycef50122012-02-05 07:42:44 -0800310 smp_mb(); /* B */ /* Avoid leaking the critical section. */
Lai Jiangshanb52ce062012-02-27 09:29:09 -0800311 ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->seq[idx]) += 1;
Paul E. McKenney621934e2006-10-04 02:17:02 -0700312 preempt_enable();
313 return idx;
314}
Paul E. McKenney632ee202010-02-22 17:04:45 -0800315EXPORT_SYMBOL_GPL(__srcu_read_lock);
Paul E. McKenney621934e2006-10-04 02:17:02 -0700316
Paul E. McKenney632ee202010-02-22 17:04:45 -0800317/*
Paul E. McKenney621934e2006-10-04 02:17:02 -0700318 * Removes the count for the old reader from the appropriate per-CPU
319 * element of the srcu_struct. Note that this may well be a different
320 * CPU than that which was incremented by the corresponding srcu_read_lock().
321 * Must be called from process context.
322 */
Paul E. McKenney632ee202010-02-22 17:04:45 -0800323void __srcu_read_unlock(struct srcu_struct *sp, int idx)
Paul E. McKenney621934e2006-10-04 02:17:02 -0700324{
325 preempt_disable();
Paul E. McKenneycef50122012-02-05 07:42:44 -0800326 smp_mb(); /* C */ /* Avoid leaking the critical section. */
Lai Jiangshan440253c2012-02-22 13:29:06 -0800327 ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->c[idx]) -= 1;
Paul E. McKenney621934e2006-10-04 02:17:02 -0700328 preempt_enable();
329}
Paul E. McKenney632ee202010-02-22 17:04:45 -0800330EXPORT_SYMBOL_GPL(__srcu_read_unlock);
Paul E. McKenney621934e2006-10-04 02:17:02 -0700331
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700332/*
Paul E. McKenneyc072a382011-01-07 02:33:47 -0800333 * We use an adaptive strategy for synchronize_srcu() and especially for
334 * synchronize_srcu_expedited(). We spin for a fixed time period
335 * (defined below) to allow SRCU readers to exit their read-side critical
336 * sections. If there are still some readers after 10 microseconds,
337 * we repeatedly block for 1-millisecond time periods. This approach
338 * has done well in testing, so there is no need for a config parameter.
339 */
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800340#define SRCU_RETRY_CHECK_DELAY 5
Lai Jiangshand9792ed2012-03-19 16:12:12 +0800341#define SYNCHRONIZE_SRCU_TRYCOUNT 2
342#define SYNCHRONIZE_SRCU_EXP_TRYCOUNT 12
Paul E. McKenneycef50122012-02-05 07:42:44 -0800343
Lai Jiangshan18108eb2012-02-27 09:28:10 -0800344/*
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800345 * @@@ Wait until all pre-existing readers complete. Such readers
Lai Jiangshan18108eb2012-02-27 09:28:10 -0800346 * will have used the index specified by "idx".
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800347 * the caller should ensures the ->completed is not changed while checking
348 * and idx = (->completed & 1) ^ 1
Lai Jiangshan18108eb2012-02-27 09:28:10 -0800349 */
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800350static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
Paul E. McKenneycef50122012-02-05 07:42:44 -0800351{
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800352 for (;;) {
353 if (srcu_readers_active_idx_check(sp, idx))
354 return true;
355 if (--trycount <= 0)
356 return false;
357 udelay(SRCU_RETRY_CHECK_DELAY);
Paul E. McKenneycef50122012-02-05 07:42:44 -0800358 }
Paul E. McKenneycef50122012-02-05 07:42:44 -0800359}
Paul E. McKenneyc072a382011-01-07 02:33:47 -0800360
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800361/*
362 * Increment the ->completed counter so that future SRCU readers will
363 * use the other rank of the ->c[] and ->seq[] arrays. This allows
364 * us to wait for pre-existing readers in a starvation-free manner.
365 */
Lai Jiangshan18108eb2012-02-27 09:28:10 -0800366static void srcu_flip(struct srcu_struct *sp)
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800367{
Lai Jiangshan18108eb2012-02-27 09:28:10 -0800368 sp->completed++;
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800369}
370
371/*
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800372 * Enqueue an SRCU callback on the specified srcu_struct structure,
373 * initiating grace-period processing if it is not already running.
374 */
375void call_srcu(struct srcu_struct *sp, struct rcu_head *head,
376 void (*func)(struct rcu_head *head))
377{
378 unsigned long flags;
379
380 head->next = NULL;
381 head->func = func;
382 spin_lock_irqsave(&sp->queue_lock, flags);
383 rcu_batch_queue(&sp->batch_queue, head);
384 if (!sp->running) {
385 sp->running = true;
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700386 schedule_delayed_work(&sp->work, 0);
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800387 }
388 spin_unlock_irqrestore(&sp->queue_lock, flags);
389}
390EXPORT_SYMBOL_GPL(call_srcu);
391
392struct rcu_synchronize {
393 struct rcu_head head;
394 struct completion completion;
395};
396
397/*
398 * Awaken the corresponding synchronize_srcu() instance now that a
399 * grace period has elapsed.
400 */
401static void wakeme_after_rcu(struct rcu_head *head)
402{
403 struct rcu_synchronize *rcu;
404
405 rcu = container_of(head, struct rcu_synchronize, head);
406 complete(&rcu->completion);
407}
408
409static void srcu_advance_batches(struct srcu_struct *sp, int trycount);
410static void srcu_reschedule(struct srcu_struct *sp);
411
412/*
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700413 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
Paul E. McKenney621934e2006-10-04 02:17:02 -0700414 */
Lai Jiangshand9792ed2012-03-19 16:12:12 +0800415static void __synchronize_srcu(struct srcu_struct *sp, int trycount)
Paul E. McKenney621934e2006-10-04 02:17:02 -0700416{
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800417 struct rcu_synchronize rcu;
418 struct rcu_head *head = &rcu.head;
419 bool done = false;
Lai Jiangshan18108eb2012-02-27 09:28:10 -0800420
Paul E. McKenneyfe15d702012-01-04 13:30:33 -0800421 rcu_lockdep_assert(!lock_is_held(&sp->dep_map) &&
422 !lock_is_held(&rcu_bh_lock_map) &&
423 !lock_is_held(&rcu_lock_map) &&
424 !lock_is_held(&rcu_sched_lock_map),
425 "Illegal synchronize_srcu() in same-type SRCU (or RCU) read-side critical section");
426
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800427 init_completion(&rcu.completion);
Paul E. McKenney621934e2006-10-04 02:17:02 -0700428
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800429 head->next = NULL;
430 head->func = wakeme_after_rcu;
431 spin_lock_irq(&sp->queue_lock);
432 if (!sp->running) {
433 /* steal the processing owner */
434 sp->running = true;
435 rcu_batch_queue(&sp->batch_check0, head);
436 spin_unlock_irq(&sp->queue_lock);
Lai Jiangshan944ce9a2012-02-22 16:43:55 -0800437
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800438 srcu_advance_batches(sp, trycount);
439 if (!rcu_batch_empty(&sp->batch_done)) {
440 BUG_ON(sp->batch_done.head != head);
441 rcu_batch_dequeue(&sp->batch_done);
442 done = true;
443 }
444 /* give the processing owner to work_struct */
445 srcu_reschedule(sp);
446 } else {
447 rcu_batch_queue(&sp->batch_queue, head);
448 spin_unlock_irq(&sp->queue_lock);
449 }
Lai Jiangshan18108eb2012-02-27 09:28:10 -0800450
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800451 if (!done)
452 wait_for_completion(&rcu.completion);
Paul E. McKenney621934e2006-10-04 02:17:02 -0700453}
454
455/**
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700456 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
457 * @sp: srcu_struct with which to synchronize.
458 *
459 * Flip the completed counter, and wait for the old count to drain to zero.
460 * As with classic RCU, the updater must use some separate means of
461 * synchronizing concurrent updates. Can block; must be called from
462 * process context.
463 *
464 * Note that it is illegal to call synchronize_srcu() from the corresponding
465 * SRCU read-side critical section; doing so will result in deadlock.
466 * However, it is perfectly legal to call synchronize_srcu() on one
467 * srcu_struct from some other srcu_struct's read-side critical section.
468 */
469void synchronize_srcu(struct srcu_struct *sp)
470{
Antti P Miettinen3705b882012-10-05 09:59:15 +0300471 __synchronize_srcu(sp, rcu_expedited
472 ? SYNCHRONIZE_SRCU_EXP_TRYCOUNT
473 : SYNCHRONIZE_SRCU_TRYCOUNT);
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700474}
475EXPORT_SYMBOL_GPL(synchronize_srcu);
476
477/**
Paul E. McKenney236fefa2012-01-31 14:00:41 -0800478 * synchronize_srcu_expedited - Brute-force SRCU grace period
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700479 * @sp: srcu_struct with which to synchronize.
480 *
Paul E. McKenneycef50122012-02-05 07:42:44 -0800481 * Wait for an SRCU grace period to elapse, but be more aggressive about
482 * spinning rather than blocking when waiting.
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700483 *
Paul E. McKenney236fefa2012-01-31 14:00:41 -0800484 * Note that it is illegal to call this function while holding any lock
Paul E. McKenneycef50122012-02-05 07:42:44 -0800485 * that is acquired by a CPU-hotplug notifier. It is also illegal to call
Paul E. McKenney236fefa2012-01-31 14:00:41 -0800486 * synchronize_srcu_expedited() from the corresponding SRCU read-side
487 * critical section; doing so will result in deadlock. However, it is
488 * perfectly legal to call synchronize_srcu_expedited() on one srcu_struct
489 * from some other srcu_struct's read-side critical section, as long as
490 * the resulting graph of srcu_structs is acyclic.
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700491 */
492void synchronize_srcu_expedited(struct srcu_struct *sp)
493{
Lai Jiangshand9792ed2012-03-19 16:12:12 +0800494 __synchronize_srcu(sp, SYNCHRONIZE_SRCU_EXP_TRYCOUNT);
Paul E. McKenney0cd397d2009-10-25 19:03:51 -0700495}
496EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
497
498/**
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800499 * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
500 */
501void srcu_barrier(struct srcu_struct *sp)
502{
503 synchronize_srcu(sp);
504}
505EXPORT_SYMBOL_GPL(srcu_barrier);
506
507/**
Paul E. McKenney621934e2006-10-04 02:17:02 -0700508 * srcu_batches_completed - return batches completed.
509 * @sp: srcu_struct on which to report batch completion.
510 *
511 * Report the number of batches, correlated with, but not necessarily
512 * precisely the same as, the number of grace periods that have elapsed.
513 */
Paul E. McKenney621934e2006-10-04 02:17:02 -0700514long srcu_batches_completed(struct srcu_struct *sp)
515{
516 return sp->completed;
517}
Paul E. McKenney621934e2006-10-04 02:17:02 -0700518EXPORT_SYMBOL_GPL(srcu_batches_completed);
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800519
520#define SRCU_CALLBACK_BATCH 10
521#define SRCU_INTERVAL 1
522
523/*
524 * Move any new SRCU callbacks to the first stage of the SRCU grace
525 * period pipeline.
526 */
527static void srcu_collect_new(struct srcu_struct *sp)
528{
529 if (!rcu_batch_empty(&sp->batch_queue)) {
530 spin_lock_irq(&sp->queue_lock);
531 rcu_batch_move(&sp->batch_check0, &sp->batch_queue);
532 spin_unlock_irq(&sp->queue_lock);
533 }
534}
535
536/*
537 * Core SRCU state machine. Advance callbacks from ->batch_check0 to
538 * ->batch_check1 and then to ->batch_done as readers drain.
539 */
540static void srcu_advance_batches(struct srcu_struct *sp, int trycount)
541{
542 int idx = 1 ^ (sp->completed & 1);
543
544 /*
545 * Because readers might be delayed for an extended period after
546 * fetching ->completed for their index, at any point in time there
547 * might well be readers using both idx=0 and idx=1. We therefore
548 * need to wait for readers to clear from both index values before
549 * invoking a callback.
550 */
551
552 if (rcu_batch_empty(&sp->batch_check0) &&
553 rcu_batch_empty(&sp->batch_check1))
554 return; /* no callbacks need to be advanced */
555
556 if (!try_check_zero(sp, idx, trycount))
557 return; /* failed to advance, will try after SRCU_INTERVAL */
558
559 /*
560 * The callbacks in ->batch_check1 have already done with their
561 * first zero check and flip back when they were enqueued on
562 * ->batch_check0 in a previous invocation of srcu_advance_batches().
563 * (Presumably try_check_zero() returned false during that
564 * invocation, leaving the callbacks stranded on ->batch_check1.)
565 * They are therefore ready to invoke, so move them to ->batch_done.
566 */
567 rcu_batch_move(&sp->batch_done, &sp->batch_check1);
568
569 if (rcu_batch_empty(&sp->batch_check0))
570 return; /* no callbacks need to be advanced */
571 srcu_flip(sp);
572
573 /*
574 * The callbacks in ->batch_check0 just finished their
575 * first check zero and flip, so move them to ->batch_check1
576 * for future checking on the other idx.
577 */
578 rcu_batch_move(&sp->batch_check1, &sp->batch_check0);
579
580 /*
581 * SRCU read-side critical sections are normally short, so check
582 * at least twice in quick succession after a flip.
583 */
584 trycount = trycount < 2 ? 2 : trycount;
585 if (!try_check_zero(sp, idx^1, trycount))
586 return; /* failed to advance, will try after SRCU_INTERVAL */
587
588 /*
589 * The callbacks in ->batch_check1 have now waited for all
590 * pre-existing readers using both idx values. They are therefore
591 * ready to invoke, so move them to ->batch_done.
592 */
593 rcu_batch_move(&sp->batch_done, &sp->batch_check1);
594}
595
596/*
597 * Invoke a limited number of SRCU callbacks that have passed through
598 * their grace period. If there are more to do, SRCU will reschedule
599 * the workqueue.
600 */
601static void srcu_invoke_callbacks(struct srcu_struct *sp)
602{
603 int i;
604 struct rcu_head *head;
605
606 for (i = 0; i < SRCU_CALLBACK_BATCH; i++) {
607 head = rcu_batch_dequeue(&sp->batch_done);
608 if (!head)
609 break;
610 local_bh_disable();
611 head->func(head);
612 local_bh_enable();
613 }
614}
615
616/*
617 * Finished one round of SRCU grace period. Start another if there are
618 * more SRCU callbacks queued, otherwise put SRCU into not-running state.
619 */
620static void srcu_reschedule(struct srcu_struct *sp)
621{
622 bool pending = true;
623
624 if (rcu_batch_empty(&sp->batch_done) &&
625 rcu_batch_empty(&sp->batch_check1) &&
626 rcu_batch_empty(&sp->batch_check0) &&
627 rcu_batch_empty(&sp->batch_queue)) {
628 spin_lock_irq(&sp->queue_lock);
629 if (rcu_batch_empty(&sp->batch_done) &&
630 rcu_batch_empty(&sp->batch_check1) &&
631 rcu_batch_empty(&sp->batch_check0) &&
632 rcu_batch_empty(&sp->batch_queue)) {
633 sp->running = false;
634 pending = false;
635 }
636 spin_unlock_irq(&sp->queue_lock);
637 }
638
639 if (pending)
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700640 schedule_delayed_work(&sp->work, SRCU_INTERVAL);
Lai Jiangshan931ea9d2012-03-19 16:12:13 +0800641}
642
643/*
644 * This is the work-queue function that handles SRCU grace periods.
645 */
646static void process_srcu(struct work_struct *work)
647{
648 struct srcu_struct *sp;
649
650 sp = container_of(work, struct srcu_struct, work.work);
651
652 srcu_collect_new(sp);
653 srcu_advance_batches(sp, 1);
654 srcu_invoke_callbacks(sp);
655 srcu_reschedule(sp);
656}