rcu: Use rcu_callback_t in call_rcu*() and friends
[deliverable/linux.git] / kernel / rcu / 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
87de1cfd
PM
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
621934ee
PM
17 *
18 * Copyright (C) IBM Corporation, 2006
4e87b2d7 19 * Copyright (C) Fujitsu, 2012
621934ee
PM
20 *
21 * Author: Paul McKenney <paulmck@us.ibm.com>
4e87b2d7 22 * Lai Jiangshan <laijs@cn.fujitsu.com>
621934ee
PM
23 *
24 * For detailed explanation of Read-Copy Update mechanism see -
25 * Documentation/RCU/ *.txt
26 *
27 */
28
9984de1a 29#include <linux/export.h>
621934ee
PM
30#include <linux/mutex.h>
31#include <linux/percpu.h>
32#include <linux/preempt.h>
33#include <linux/rcupdate.h>
34#include <linux/sched.h>
621934ee 35#include <linux/smp.h>
46fdb093 36#include <linux/delay.h>
621934ee
PM
37#include <linux/srcu.h>
38
3705b88d
AM
39#include "rcu.h"
40
931ea9d1
LJ
41/*
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
632ee200
PM
99static int init_srcu_struct_fields(struct srcu_struct *sp)
100{
101 sp->completed = 0;
931ea9d1
LJ
102 spin_lock_init(&sp->queue_lock);
103 sp->running = false;
104 rcu_batch_init(&sp->batch_queue);
105 rcu_batch_init(&sp->batch_check0);
106 rcu_batch_init(&sp->batch_check1);
107 rcu_batch_init(&sp->batch_done);
108 INIT_DELAYED_WORK(&sp->work, process_srcu);
632ee200
PM
109 sp->per_cpu_ref = alloc_percpu(struct srcu_struct_array);
110 return sp->per_cpu_ref ? 0 : -ENOMEM;
111}
112
113#ifdef CONFIG_DEBUG_LOCK_ALLOC
114
115int __init_srcu_struct(struct srcu_struct *sp, const char *name,
116 struct lock_class_key *key)
117{
632ee200
PM
118 /* Don't re-initialize a lock while it is held. */
119 debug_check_no_locks_freed((void *)sp, sizeof(*sp));
120 lockdep_init_map(&sp->dep_map, name, key, 0);
632ee200
PM
121 return init_srcu_struct_fields(sp);
122}
123EXPORT_SYMBOL_GPL(__init_srcu_struct);
124
125#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
126
621934ee
PM
127/**
128 * init_srcu_struct - initialize a sleep-RCU structure
129 * @sp: structure to initialize.
130 *
131 * Must invoke this on a given srcu_struct before passing that srcu_struct
132 * to any other function. Each srcu_struct represents a separate domain
133 * of SRCU protection.
134 */
e6a92013 135int init_srcu_struct(struct srcu_struct *sp)
621934ee 136{
632ee200 137 return init_srcu_struct_fields(sp);
621934ee 138}
0cd397d3 139EXPORT_SYMBOL_GPL(init_srcu_struct);
621934ee 140
632ee200
PM
141#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
142
b52ce066
LJ
143/*
144 * Returns approximate total of the readers' ->seq[] values for the
145 * rank of per-CPU counters specified by idx.
146 */
147static unsigned long srcu_readers_seq_idx(struct srcu_struct *sp, int idx)
148{
149 int cpu;
150 unsigned long sum = 0;
151 unsigned long t;
152
153 for_each_possible_cpu(cpu) {
7d0ae808 154 t = READ_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->seq[idx]);
b52ce066
LJ
155 sum += t;
156 }
157 return sum;
158}
159
621934ee 160/*
cef50120 161 * Returns approximate number of readers active on the specified rank
b52ce066 162 * of the per-CPU ->c[] counters.
621934ee 163 */
cef50120
PM
164static unsigned long srcu_readers_active_idx(struct srcu_struct *sp, int idx)
165{
166 int cpu;
167 unsigned long sum = 0;
168 unsigned long t;
621934ee 169
cef50120 170 for_each_possible_cpu(cpu) {
7d0ae808 171 t = READ_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx]);
cef50120 172 sum += t;
cef50120 173 }
b52ce066 174 return sum;
cef50120
PM
175}
176
177/*
b52ce066
LJ
178 * Return true if the number of pre-existing readers is determined to
179 * be stably zero. An example unstable zero can occur if the call
180 * to srcu_readers_active_idx() misses an __srcu_read_lock() increment,
181 * but due to task migration, sees the corresponding __srcu_read_unlock()
182 * decrement. This can happen because srcu_readers_active_idx() takes
183 * time to sum the array, and might in fact be interrupted or preempted
184 * partway through the summation.
cef50120
PM
185 */
186static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
621934ee 187{
b52ce066
LJ
188 unsigned long seq;
189
190 seq = srcu_readers_seq_idx(sp, idx);
191
192 /*
193 * The following smp_mb() A pairs with the smp_mb() B located in
194 * __srcu_read_lock(). This pairing ensures that if an
195 * __srcu_read_lock() increments its counter after the summation
196 * in srcu_readers_active_idx(), then the corresponding SRCU read-side
197 * critical section will see any changes made prior to the start
198 * of the current SRCU grace period.
199 *
200 * Also, if the above call to srcu_readers_seq_idx() saw the
201 * increment of ->seq[], then the call to srcu_readers_active_idx()
202 * must see the increment of ->c[].
203 */
204 smp_mb(); /* A */
621934ee 205
cef50120
PM
206 /*
207 * Note that srcu_readers_active_idx() can incorrectly return
208 * zero even though there is a pre-existing reader throughout.
209 * To see this, suppose that task A is in a very long SRCU
210 * read-side critical section that started on CPU 0, and that
b52ce066 211 * no other reader exists, so that the sum of the counters
cef50120
PM
212 * is equal to one. Then suppose that task B starts executing
213 * srcu_readers_active_idx(), summing up to CPU 1, and then that
214 * task C starts reading on CPU 0, so that its increment is not
215 * summed, but finishes reading on CPU 2, so that its decrement
216 * -is- summed. Then when task B completes its sum, it will
217 * incorrectly get zero, despite the fact that task A has been
218 * in its SRCU read-side critical section the whole time.
219 *
220 * We therefore do a validation step should srcu_readers_active_idx()
221 * return zero.
222 */
223 if (srcu_readers_active_idx(sp, idx) != 0)
224 return false;
225
226 /*
b52ce066
LJ
227 * The remainder of this function is the validation step.
228 * The following smp_mb() D pairs with the smp_mb() C in
229 * __srcu_read_unlock(). If the __srcu_read_unlock() was seen
230 * by srcu_readers_active_idx() above, then any destructive
231 * operation performed after the grace period will happen after
232 * the corresponding SRCU read-side critical section.
cef50120 233 *
b52ce066
LJ
234 * Note that there can be at most NR_CPUS worth of readers using
235 * the old index, which is not enough to overflow even a 32-bit
236 * integer. (Yes, this does mean that systems having more than
237 * a billion or so CPUs need to be 64-bit systems.) Therefore,
238 * the sum of the ->seq[] counters cannot possibly overflow.
239 * Therefore, the only way that the return values of the two
240 * calls to srcu_readers_seq_idx() can be equal is if there were
241 * no increments of the corresponding rank of ->seq[] counts
242 * in the interim. But the missed-increment scenario laid out
243 * above includes an increment of the ->seq[] counter by
244 * the corresponding __srcu_read_lock(). Therefore, if this
245 * scenario occurs, the return values from the two calls to
246 * srcu_readers_seq_idx() will differ, and thus the validation
247 * step below suffices.
cef50120 248 */
b52ce066
LJ
249 smp_mb(); /* D */
250
251 return srcu_readers_seq_idx(sp, idx) == seq;
621934ee
PM
252}
253
254/**
f765d113
NMG
255 * srcu_readers_active - returns true if there are readers. and false
256 * otherwise
621934ee
PM
257 * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
258 *
259 * Note that this is not an atomic primitive, and can therefore suffer
260 * severe errors when invoked on an active srcu_struct. That said, it
261 * can be useful as an error check at cleanup time.
262 */
f765d113 263static bool srcu_readers_active(struct srcu_struct *sp)
621934ee 264{
dc879175
LJ
265 int cpu;
266 unsigned long sum = 0;
267
268 for_each_possible_cpu(cpu) {
7d0ae808
PM
269 sum += READ_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[0]);
270 sum += READ_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[1]);
dc879175
LJ
271 }
272 return sum;
621934ee
PM
273}
274
275/**
276 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
277 * @sp: structure to clean up.
278 *
279 * Must invoke this after you are finished using a given srcu_struct that
280 * was initialized via init_srcu_struct(), else you leak memory.
281 */
282void cleanup_srcu_struct(struct srcu_struct *sp)
283{
ab4d2986
LJ
284 if (WARN_ON(srcu_readers_active(sp)))
285 return; /* Leakage unless caller handles error. */
621934ee
PM
286 free_percpu(sp->per_cpu_ref);
287 sp->per_cpu_ref = NULL;
288}
0cd397d3 289EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
621934ee 290
632ee200 291/*
621934ee
PM
292 * Counts the new reader in the appropriate per-CPU element of the
293 * srcu_struct. Must be called from process context.
294 * Returns an index that must be passed to the matching srcu_read_unlock().
295 */
632ee200 296int __srcu_read_lock(struct srcu_struct *sp)
621934ee
PM
297{
298 int idx;
299
7d0ae808 300 idx = READ_ONCE(sp->completed) & 0x1;
621934ee 301 preempt_disable();
a792563b 302 __this_cpu_inc(sp->per_cpu_ref->c[idx]);
cef50120 303 smp_mb(); /* B */ /* Avoid leaking the critical section. */
a792563b 304 __this_cpu_inc(sp->per_cpu_ref->seq[idx]);
621934ee
PM
305 preempt_enable();
306 return idx;
307}
632ee200 308EXPORT_SYMBOL_GPL(__srcu_read_lock);
621934ee 309
632ee200 310/*
621934ee
PM
311 * Removes the count for the old reader from the appropriate per-CPU
312 * element of the srcu_struct. Note that this may well be a different
313 * CPU than that which was incremented by the corresponding srcu_read_lock().
314 * Must be called from process context.
315 */
632ee200 316void __srcu_read_unlock(struct srcu_struct *sp, int idx)
621934ee 317{
cef50120 318 smp_mb(); /* C */ /* Avoid leaking the critical section. */
5a41344a 319 this_cpu_dec(sp->per_cpu_ref->c[idx]);
621934ee 320}
632ee200 321EXPORT_SYMBOL_GPL(__srcu_read_unlock);
621934ee 322
c072a388
PM
323/*
324 * We use an adaptive strategy for synchronize_srcu() and especially for
325 * synchronize_srcu_expedited(). We spin for a fixed time period
326 * (defined below) to allow SRCU readers to exit their read-side critical
327 * sections. If there are still some readers after 10 microseconds,
328 * we repeatedly block for 1-millisecond time periods. This approach
329 * has done well in testing, so there is no need for a config parameter.
330 */
931ea9d1 331#define SRCU_RETRY_CHECK_DELAY 5
d9792edd
LJ
332#define SYNCHRONIZE_SRCU_TRYCOUNT 2
333#define SYNCHRONIZE_SRCU_EXP_TRYCOUNT 12
cef50120 334
18108ebf 335/*
931ea9d1 336 * @@@ Wait until all pre-existing readers complete. Such readers
18108ebf 337 * will have used the index specified by "idx".
931ea9d1
LJ
338 * the caller should ensures the ->completed is not changed while checking
339 * and idx = (->completed & 1) ^ 1
18108ebf 340 */
931ea9d1 341static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
cef50120 342{
931ea9d1
LJ
343 for (;;) {
344 if (srcu_readers_active_idx_check(sp, idx))
345 return true;
346 if (--trycount <= 0)
347 return false;
348 udelay(SRCU_RETRY_CHECK_DELAY);
cef50120 349 }
cef50120 350}
c072a388 351
931ea9d1
LJ
352/*
353 * Increment the ->completed counter so that future SRCU readers will
354 * use the other rank of the ->c[] and ->seq[] arrays. This allows
355 * us to wait for pre-existing readers in a starvation-free manner.
356 */
18108ebf 357static void srcu_flip(struct srcu_struct *sp)
944ce9af 358{
18108ebf 359 sp->completed++;
944ce9af
LJ
360}
361
931ea9d1
LJ
362/*
363 * Enqueue an SRCU callback on the specified srcu_struct structure,
364 * initiating grace-period processing if it is not already running.
bc72d962
PM
365 *
366 * Note that all CPUs must agree that the grace period extended beyond
367 * all pre-existing SRCU read-side critical section. On systems with
368 * more than one CPU, this means that when "func()" is invoked, each CPU
369 * is guaranteed to have executed a full memory barrier since the end of
370 * its last corresponding SRCU read-side critical section whose beginning
371 * preceded the call to call_rcu(). It also means that each CPU executing
372 * an SRCU read-side critical section that continues beyond the start of
373 * "func()" must have executed a memory barrier after the call_rcu()
374 * but before the beginning of that SRCU read-side critical section.
375 * Note that these guarantees include CPUs that are offline, idle, or
376 * executing in user mode, as well as CPUs that are executing in the kernel.
377 *
378 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
379 * resulting SRCU callback function "func()", then both CPU A and CPU
380 * B are guaranteed to execute a full memory barrier during the time
381 * interval between the call to call_rcu() and the invocation of "func()".
382 * This guarantee applies even if CPU A and CPU B are the same CPU (but
383 * again only if the system has more than one CPU).
384 *
385 * Of course, these guarantees apply only for invocations of call_srcu(),
386 * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
387 * srcu_struct structure.
931ea9d1
LJ
388 */
389void call_srcu(struct srcu_struct *sp, struct rcu_head *head,
b6a4ae76 390 rcu_callback_t func)
931ea9d1
LJ
391{
392 unsigned long flags;
393
394 head->next = NULL;
395 head->func = func;
396 spin_lock_irqsave(&sp->queue_lock, flags);
397 rcu_batch_queue(&sp->batch_queue, head);
398 if (!sp->running) {
399 sp->running = true;
ae167033 400 queue_delayed_work(system_power_efficient_wq, &sp->work, 0);
931ea9d1
LJ
401 }
402 spin_unlock_irqrestore(&sp->queue_lock, flags);
403}
404EXPORT_SYMBOL_GPL(call_srcu);
405
931ea9d1
LJ
406static void srcu_advance_batches(struct srcu_struct *sp, int trycount);
407static void srcu_reschedule(struct srcu_struct *sp);
408
0cd397d3
PM
409/*
410 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
621934ee 411 */
d9792edd 412static void __synchronize_srcu(struct srcu_struct *sp, int trycount)
621934ee 413{
931ea9d1
LJ
414 struct rcu_synchronize rcu;
415 struct rcu_head *head = &rcu.head;
416 bool done = false;
18108ebf 417
f78f5b90
PM
418 RCU_LOCKDEP_WARN(lock_is_held(&sp->dep_map) ||
419 lock_is_held(&rcu_bh_lock_map) ||
420 lock_is_held(&rcu_lock_map) ||
421 lock_is_held(&rcu_sched_lock_map),
422 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
fe15d706 423
6e6f1b30 424 might_sleep();
931ea9d1
LJ
425 init_completion(&rcu.completion);
426
427 head->next = NULL;
428 head->func = wakeme_after_rcu;
429 spin_lock_irq(&sp->queue_lock);
430 if (!sp->running) {
431 /* steal the processing owner */
432 sp->running = true;
433 rcu_batch_queue(&sp->batch_check0, head);
434 spin_unlock_irq(&sp->queue_lock);
435
436 srcu_advance_batches(sp, trycount);
437 if (!rcu_batch_empty(&sp->batch_done)) {
438 BUG_ON(sp->batch_done.head != head);
439 rcu_batch_dequeue(&sp->batch_done);
440 done = true;
441 }
442 /* give the processing owner to work_struct */
443 srcu_reschedule(sp);
444 } else {
445 rcu_batch_queue(&sp->batch_queue, head);
446 spin_unlock_irq(&sp->queue_lock);
447 }
944ce9af 448
931ea9d1
LJ
449 if (!done)
450 wait_for_completion(&rcu.completion);
621934ee
PM
451}
452
0cd397d3
PM
453/**
454 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
455 * @sp: srcu_struct with which to synchronize.
456 *
34a64b6b
LJ
457 * Wait for the count to drain to zero of both indexes. To avoid the
458 * possible starvation of synchronize_srcu(), it waits for the count of
459 * the index=((->completed & 1) ^ 1) to drain to zero at first,
460 * and then flip the completed and wait for the count of the other index.
461 *
462 * Can block; must be called from process context.
0cd397d3
PM
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
bc72d962
PM
467 * srcu_struct from some other srcu_struct's read-side critical section,
468 * as long as the resulting graph of srcu_structs is acyclic.
469 *
470 * There are memory-ordering constraints implied by synchronize_srcu().
471 * On systems with more than one CPU, when synchronize_srcu() returns,
472 * each CPU is guaranteed to have executed a full memory barrier since
473 * the end of its last corresponding SRCU-sched read-side critical section
474 * whose beginning preceded the call to synchronize_srcu(). In addition,
475 * each CPU having an SRCU read-side critical section that extends beyond
476 * the return from synchronize_srcu() is guaranteed to have executed a
477 * full memory barrier after the beginning of synchronize_srcu() and before
478 * the beginning of that SRCU read-side critical section. Note that these
479 * guarantees include CPUs that are offline, idle, or executing in user mode,
480 * as well as CPUs that are executing in the kernel.
481 *
482 * Furthermore, if CPU A invoked synchronize_srcu(), which returned
483 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
484 * to have executed a full memory barrier during the execution of
485 * synchronize_srcu(). This guarantee applies even if CPU A and CPU B
486 * are the same CPU, but again only if the system has more than one CPU.
487 *
488 * Of course, these memory-ordering guarantees apply only when
489 * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
490 * passed the same srcu_struct structure.
0cd397d3
PM
491 */
492void synchronize_srcu(struct srcu_struct *sp)
493{
5afff48b 494 __synchronize_srcu(sp, rcu_gp_is_expedited()
3705b88d
AM
495 ? SYNCHRONIZE_SRCU_EXP_TRYCOUNT
496 : SYNCHRONIZE_SRCU_TRYCOUNT);
0cd397d3
PM
497}
498EXPORT_SYMBOL_GPL(synchronize_srcu);
499
500/**
236fefaf 501 * synchronize_srcu_expedited - Brute-force SRCU grace period
0cd397d3
PM
502 * @sp: srcu_struct with which to synchronize.
503 *
cef50120
PM
504 * Wait for an SRCU grace period to elapse, but be more aggressive about
505 * spinning rather than blocking when waiting.
0cd397d3 506 *
bc72d962
PM
507 * Note that synchronize_srcu_expedited() has the same deadlock and
508 * memory-ordering properties as does synchronize_srcu().
0cd397d3
PM
509 */
510void synchronize_srcu_expedited(struct srcu_struct *sp)
511{
d9792edd 512 __synchronize_srcu(sp, SYNCHRONIZE_SRCU_EXP_TRYCOUNT);
0cd397d3
PM
513}
514EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
515
931ea9d1
LJ
516/**
517 * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
4461212a 518 * @sp: srcu_struct on which to wait for in-flight callbacks.
931ea9d1
LJ
519 */
520void srcu_barrier(struct srcu_struct *sp)
521{
522 synchronize_srcu(sp);
523}
524EXPORT_SYMBOL_GPL(srcu_barrier);
525
621934ee
PM
526/**
527 * srcu_batches_completed - return batches completed.
528 * @sp: srcu_struct on which to report batch completion.
529 *
530 * Report the number of batches, correlated with, but not necessarily
531 * precisely the same as, the number of grace periods that have elapsed.
532 */
a5c198f4 533unsigned long srcu_batches_completed(struct srcu_struct *sp)
621934ee
PM
534{
535 return sp->completed;
536}
621934ee 537EXPORT_SYMBOL_GPL(srcu_batches_completed);
931ea9d1
LJ
538
539#define SRCU_CALLBACK_BATCH 10
540#define SRCU_INTERVAL 1
541
542/*
543 * Move any new SRCU callbacks to the first stage of the SRCU grace
544 * period pipeline.
545 */
546static void srcu_collect_new(struct srcu_struct *sp)
547{
548 if (!rcu_batch_empty(&sp->batch_queue)) {
549 spin_lock_irq(&sp->queue_lock);
550 rcu_batch_move(&sp->batch_check0, &sp->batch_queue);
551 spin_unlock_irq(&sp->queue_lock);
552 }
553}
554
555/*
556 * Core SRCU state machine. Advance callbacks from ->batch_check0 to
557 * ->batch_check1 and then to ->batch_done as readers drain.
558 */
559static void srcu_advance_batches(struct srcu_struct *sp, int trycount)
560{
561 int idx = 1 ^ (sp->completed & 1);
562
563 /*
564 * Because readers might be delayed for an extended period after
565 * fetching ->completed for their index, at any point in time there
566 * might well be readers using both idx=0 and idx=1. We therefore
567 * need to wait for readers to clear from both index values before
568 * invoking a callback.
569 */
570
571 if (rcu_batch_empty(&sp->batch_check0) &&
572 rcu_batch_empty(&sp->batch_check1))
573 return; /* no callbacks need to be advanced */
574
575 if (!try_check_zero(sp, idx, trycount))
576 return; /* failed to advance, will try after SRCU_INTERVAL */
577
578 /*
579 * The callbacks in ->batch_check1 have already done with their
580 * first zero check and flip back when they were enqueued on
581 * ->batch_check0 in a previous invocation of srcu_advance_batches().
582 * (Presumably try_check_zero() returned false during that
583 * invocation, leaving the callbacks stranded on ->batch_check1.)
584 * They are therefore ready to invoke, so move them to ->batch_done.
585 */
586 rcu_batch_move(&sp->batch_done, &sp->batch_check1);
587
588 if (rcu_batch_empty(&sp->batch_check0))
589 return; /* no callbacks need to be advanced */
590 srcu_flip(sp);
591
592 /*
593 * The callbacks in ->batch_check0 just finished their
594 * first check zero and flip, so move them to ->batch_check1
595 * for future checking on the other idx.
596 */
597 rcu_batch_move(&sp->batch_check1, &sp->batch_check0);
598
599 /*
600 * SRCU read-side critical sections are normally short, so check
601 * at least twice in quick succession after a flip.
602 */
603 trycount = trycount < 2 ? 2 : trycount;
604 if (!try_check_zero(sp, idx^1, trycount))
605 return; /* failed to advance, will try after SRCU_INTERVAL */
606
607 /*
608 * The callbacks in ->batch_check1 have now waited for all
609 * pre-existing readers using both idx values. They are therefore
610 * ready to invoke, so move them to ->batch_done.
611 */
612 rcu_batch_move(&sp->batch_done, &sp->batch_check1);
613}
614
615/*
616 * Invoke a limited number of SRCU callbacks that have passed through
617 * their grace period. If there are more to do, SRCU will reschedule
618 * the workqueue.
619 */
620static void srcu_invoke_callbacks(struct srcu_struct *sp)
621{
622 int i;
623 struct rcu_head *head;
624
625 for (i = 0; i < SRCU_CALLBACK_BATCH; i++) {
626 head = rcu_batch_dequeue(&sp->batch_done);
627 if (!head)
628 break;
629 local_bh_disable();
630 head->func(head);
631 local_bh_enable();
632 }
633}
634
635/*
636 * Finished one round of SRCU grace period. Start another if there are
637 * more SRCU callbacks queued, otherwise put SRCU into not-running state.
638 */
639static void srcu_reschedule(struct srcu_struct *sp)
640{
641 bool pending = true;
642
643 if (rcu_batch_empty(&sp->batch_done) &&
644 rcu_batch_empty(&sp->batch_check1) &&
645 rcu_batch_empty(&sp->batch_check0) &&
646 rcu_batch_empty(&sp->batch_queue)) {
647 spin_lock_irq(&sp->queue_lock);
648 if (rcu_batch_empty(&sp->batch_done) &&
649 rcu_batch_empty(&sp->batch_check1) &&
650 rcu_batch_empty(&sp->batch_check0) &&
651 rcu_batch_empty(&sp->batch_queue)) {
652 sp->running = false;
653 pending = false;
654 }
655 spin_unlock_irq(&sp->queue_lock);
656 }
657
658 if (pending)
ae167033
SD
659 queue_delayed_work(system_power_efficient_wq,
660 &sp->work, SRCU_INTERVAL);
931ea9d1
LJ
661}
662
663/*
664 * This is the work-queue function that handles SRCU grace periods.
665 */
f2ebfbc9 666void process_srcu(struct work_struct *work)
931ea9d1
LJ
667{
668 struct srcu_struct *sp;
669
670 sp = container_of(work, struct srcu_struct, work.work);
671
672 srcu_collect_new(sp);
673 srcu_advance_batches(sp, 1);
674 srcu_invoke_callbacks(sp);
675 srcu_reschedule(sp);
676}
f2ebfbc9 677EXPORT_SYMBOL_GPL(process_srcu);
This page took 0.527273 seconds and 5 git commands to generate.