Commit | Line | Data |
---|---|---|
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 | |
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 | ||
931ea9d1 LJ |
39 | /* |
40 | * Initialize an rcu_batch structure to empty. | |
41 | */ | |
42 | static inline void rcu_batch_init(struct rcu_batch *b) | |
43 | { | |
44 | b->head = NULL; | |
45 | b->tail = &b->head; | |
46 | } | |
47 | ||
48 | /* | |
49 | * Enqueue a callback onto the tail of the specified rcu_batch structure. | |
50 | */ | |
51 | static inline void rcu_batch_queue(struct rcu_batch *b, struct rcu_head *head) | |
52 | { | |
53 | *b->tail = head; | |
54 | b->tail = &head->next; | |
55 | } | |
56 | ||
57 | /* | |
58 | * Is the specified rcu_batch structure empty? | |
59 | */ | |
60 | static inline bool rcu_batch_empty(struct rcu_batch *b) | |
61 | { | |
62 | return b->tail == &b->head; | |
63 | } | |
64 | ||
65 | /* | |
66 | * Remove the callback at the head of the specified rcu_batch structure | |
67 | * and return a pointer to it, or return NULL if the structure is empty. | |
68 | */ | |
69 | static inline struct rcu_head *rcu_batch_dequeue(struct rcu_batch *b) | |
70 | { | |
71 | struct rcu_head *head; | |
72 | ||
73 | if (rcu_batch_empty(b)) | |
74 | return NULL; | |
75 | ||
76 | head = b->head; | |
77 | b->head = head->next; | |
78 | if (b->tail == &head->next) | |
79 | rcu_batch_init(b); | |
80 | ||
81 | return head; | |
82 | } | |
83 | ||
84 | /* | |
85 | * Move all callbacks from the rcu_batch structure specified by "from" to | |
86 | * the structure specified by "to". | |
87 | */ | |
88 | static inline void rcu_batch_move(struct rcu_batch *to, struct rcu_batch *from) | |
89 | { | |
90 | if (!rcu_batch_empty(from)) { | |
91 | *to->tail = from->head; | |
92 | to->tail = from->tail; | |
93 | rcu_batch_init(from); | |
94 | } | |
95 | } | |
96 | ||
97 | /* single-thread state-machine */ | |
98 | static void process_srcu(struct work_struct *work); | |
99 | ||
632ee200 PM |
100 | static int init_srcu_struct_fields(struct srcu_struct *sp) |
101 | { | |
102 | sp->completed = 0; | |
931ea9d1 LJ |
103 | spin_lock_init(&sp->queue_lock); |
104 | sp->running = false; | |
105 | rcu_batch_init(&sp->batch_queue); | |
106 | rcu_batch_init(&sp->batch_check0); | |
107 | rcu_batch_init(&sp->batch_check1); | |
108 | rcu_batch_init(&sp->batch_done); | |
109 | INIT_DELAYED_WORK(&sp->work, process_srcu); | |
632ee200 PM |
110 | sp->per_cpu_ref = alloc_percpu(struct srcu_struct_array); |
111 | return sp->per_cpu_ref ? 0 : -ENOMEM; | |
112 | } | |
113 | ||
114 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
115 | ||
116 | int __init_srcu_struct(struct srcu_struct *sp, const char *name, | |
117 | struct lock_class_key *key) | |
118 | { | |
632ee200 PM |
119 | /* Don't re-initialize a lock while it is held. */ |
120 | debug_check_no_locks_freed((void *)sp, sizeof(*sp)); | |
121 | lockdep_init_map(&sp->dep_map, name, key, 0); | |
632ee200 PM |
122 | return init_srcu_struct_fields(sp); |
123 | } | |
124 | EXPORT_SYMBOL_GPL(__init_srcu_struct); | |
125 | ||
126 | #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ | |
127 | ||
621934ee PM |
128 | /** |
129 | * init_srcu_struct - initialize a sleep-RCU structure | |
130 | * @sp: structure to initialize. | |
131 | * | |
132 | * Must invoke this on a given srcu_struct before passing that srcu_struct | |
133 | * to any other function. Each srcu_struct represents a separate domain | |
134 | * of SRCU protection. | |
135 | */ | |
e6a92013 | 136 | int init_srcu_struct(struct srcu_struct *sp) |
621934ee | 137 | { |
632ee200 | 138 | return init_srcu_struct_fields(sp); |
621934ee | 139 | } |
0cd397d3 | 140 | EXPORT_SYMBOL_GPL(init_srcu_struct); |
621934ee | 141 | |
632ee200 PM |
142 | #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
143 | ||
b52ce066 LJ |
144 | /* |
145 | * Returns approximate total of the readers' ->seq[] values for the | |
146 | * rank of per-CPU counters specified by idx. | |
147 | */ | |
148 | static unsigned long srcu_readers_seq_idx(struct srcu_struct *sp, int idx) | |
149 | { | |
150 | int cpu; | |
151 | unsigned long sum = 0; | |
152 | unsigned long t; | |
153 | ||
154 | for_each_possible_cpu(cpu) { | |
155 | t = ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->seq[idx]); | |
156 | sum += t; | |
157 | } | |
158 | return sum; | |
159 | } | |
160 | ||
621934ee | 161 | /* |
cef50120 | 162 | * Returns approximate number of readers active on the specified rank |
b52ce066 | 163 | * of the per-CPU ->c[] counters. |
621934ee | 164 | */ |
cef50120 PM |
165 | static unsigned long srcu_readers_active_idx(struct srcu_struct *sp, int idx) |
166 | { | |
167 | int cpu; | |
168 | unsigned long sum = 0; | |
169 | unsigned long t; | |
621934ee | 170 | |
cef50120 PM |
171 | for_each_possible_cpu(cpu) { |
172 | t = ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx]); | |
173 | sum += t; | |
cef50120 | 174 | } |
b52ce066 | 175 | return sum; |
cef50120 PM |
176 | } |
177 | ||
178 | /* | |
b52ce066 LJ |
179 | * Return true if the number of pre-existing readers is determined to |
180 | * be stably zero. An example unstable zero can occur if the call | |
181 | * to srcu_readers_active_idx() misses an __srcu_read_lock() increment, | |
182 | * but due to task migration, sees the corresponding __srcu_read_unlock() | |
183 | * decrement. This can happen because srcu_readers_active_idx() takes | |
184 | * time to sum the array, and might in fact be interrupted or preempted | |
185 | * partway through the summation. | |
cef50120 PM |
186 | */ |
187 | static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx) | |
621934ee | 188 | { |
b52ce066 LJ |
189 | unsigned long seq; |
190 | ||
191 | seq = srcu_readers_seq_idx(sp, idx); | |
192 | ||
193 | /* | |
194 | * The following smp_mb() A pairs with the smp_mb() B located in | |
195 | * __srcu_read_lock(). This pairing ensures that if an | |
196 | * __srcu_read_lock() increments its counter after the summation | |
197 | * in srcu_readers_active_idx(), then the corresponding SRCU read-side | |
198 | * critical section will see any changes made prior to the start | |
199 | * of the current SRCU grace period. | |
200 | * | |
201 | * Also, if the above call to srcu_readers_seq_idx() saw the | |
202 | * increment of ->seq[], then the call to srcu_readers_active_idx() | |
203 | * must see the increment of ->c[]. | |
204 | */ | |
205 | smp_mb(); /* A */ | |
621934ee | 206 | |
cef50120 PM |
207 | /* |
208 | * Note that srcu_readers_active_idx() can incorrectly return | |
209 | * zero even though there is a pre-existing reader throughout. | |
210 | * To see this, suppose that task A is in a very long SRCU | |
211 | * read-side critical section that started on CPU 0, and that | |
b52ce066 | 212 | * no other reader exists, so that the sum of the counters |
cef50120 PM |
213 | * is equal to one. Then suppose that task B starts executing |
214 | * srcu_readers_active_idx(), summing up to CPU 1, and then that | |
215 | * task C starts reading on CPU 0, so that its increment is not | |
216 | * summed, but finishes reading on CPU 2, so that its decrement | |
217 | * -is- summed. Then when task B completes its sum, it will | |
218 | * incorrectly get zero, despite the fact that task A has been | |
219 | * in its SRCU read-side critical section the whole time. | |
220 | * | |
221 | * We therefore do a validation step should srcu_readers_active_idx() | |
222 | * return zero. | |
223 | */ | |
224 | if (srcu_readers_active_idx(sp, idx) != 0) | |
225 | return false; | |
226 | ||
227 | /* | |
b52ce066 LJ |
228 | * The remainder of this function is the validation step. |
229 | * The following smp_mb() D pairs with the smp_mb() C in | |
230 | * __srcu_read_unlock(). If the __srcu_read_unlock() was seen | |
231 | * by srcu_readers_active_idx() above, then any destructive | |
232 | * operation performed after the grace period will happen after | |
233 | * the corresponding SRCU read-side critical section. | |
cef50120 | 234 | * |
b52ce066 LJ |
235 | * Note that there can be at most NR_CPUS worth of readers using |
236 | * the old index, which is not enough to overflow even a 32-bit | |
237 | * integer. (Yes, this does mean that systems having more than | |
238 | * a billion or so CPUs need to be 64-bit systems.) Therefore, | |
239 | * the sum of the ->seq[] counters cannot possibly overflow. | |
240 | * Therefore, the only way that the return values of the two | |
241 | * calls to srcu_readers_seq_idx() can be equal is if there were | |
242 | * no increments of the corresponding rank of ->seq[] counts | |
243 | * in the interim. But the missed-increment scenario laid out | |
244 | * above includes an increment of the ->seq[] counter by | |
245 | * the corresponding __srcu_read_lock(). Therefore, if this | |
246 | * scenario occurs, the return values from the two calls to | |
247 | * srcu_readers_seq_idx() will differ, and thus the validation | |
248 | * step below suffices. | |
cef50120 | 249 | */ |
b52ce066 LJ |
250 | smp_mb(); /* D */ |
251 | ||
252 | return srcu_readers_seq_idx(sp, idx) == seq; | |
621934ee PM |
253 | } |
254 | ||
255 | /** | |
256 | * srcu_readers_active - returns approximate number of readers. | |
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 | */ | |
bb695170 | 263 | static int 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) { | |
269 | sum += ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[0]); | |
270 | sum += ACCESS_ONCE(per_cpu_ptr(sp->per_cpu_ref, cpu)->c[1]); | |
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 | */ | |
282 | void cleanup_srcu_struct(struct srcu_struct *sp) | |
283 | { | |
284 | int sum; | |
285 | ||
286 | sum = srcu_readers_active(sp); | |
287 | WARN_ON(sum); /* Leakage unless caller handles error. */ | |
288 | if (sum != 0) | |
289 | return; | |
290 | free_percpu(sp->per_cpu_ref); | |
291 | sp->per_cpu_ref = NULL; | |
292 | } | |
0cd397d3 | 293 | EXPORT_SYMBOL_GPL(cleanup_srcu_struct); |
621934ee | 294 | |
632ee200 | 295 | /* |
621934ee PM |
296 | * Counts the new reader in the appropriate per-CPU element of the |
297 | * srcu_struct. Must be called from process context. | |
298 | * Returns an index that must be passed to the matching srcu_read_unlock(). | |
299 | */ | |
632ee200 | 300 | int __srcu_read_lock(struct srcu_struct *sp) |
621934ee PM |
301 | { |
302 | int idx; | |
303 | ||
304 | preempt_disable(); | |
cef50120 PM |
305 | idx = rcu_dereference_index_check(sp->completed, |
306 | rcu_read_lock_sched_held()) & 0x1; | |
b52ce066 | 307 | ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->c[idx]) += 1; |
cef50120 | 308 | smp_mb(); /* B */ /* Avoid leaking the critical section. */ |
b52ce066 | 309 | ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->seq[idx]) += 1; |
621934ee PM |
310 | preempt_enable(); |
311 | return idx; | |
312 | } | |
632ee200 | 313 | EXPORT_SYMBOL_GPL(__srcu_read_lock); |
621934ee | 314 | |
632ee200 | 315 | /* |
621934ee PM |
316 | * Removes the count for the old reader from the appropriate per-CPU |
317 | * element of the srcu_struct. Note that this may well be a different | |
318 | * CPU than that which was incremented by the corresponding srcu_read_lock(). | |
319 | * Must be called from process context. | |
320 | */ | |
632ee200 | 321 | void __srcu_read_unlock(struct srcu_struct *sp, int idx) |
621934ee PM |
322 | { |
323 | preempt_disable(); | |
cef50120 | 324 | smp_mb(); /* C */ /* Avoid leaking the critical section. */ |
440253c1 | 325 | ACCESS_ONCE(this_cpu_ptr(sp->per_cpu_ref)->c[idx]) -= 1; |
621934ee PM |
326 | preempt_enable(); |
327 | } | |
632ee200 | 328 | EXPORT_SYMBOL_GPL(__srcu_read_unlock); |
621934ee | 329 | |
c072a388 PM |
330 | /* |
331 | * We use an adaptive strategy for synchronize_srcu() and especially for | |
332 | * synchronize_srcu_expedited(). We spin for a fixed time period | |
333 | * (defined below) to allow SRCU readers to exit their read-side critical | |
334 | * sections. If there are still some readers after 10 microseconds, | |
335 | * we repeatedly block for 1-millisecond time periods. This approach | |
336 | * has done well in testing, so there is no need for a config parameter. | |
337 | */ | |
931ea9d1 | 338 | #define SRCU_RETRY_CHECK_DELAY 5 |
d9792edd LJ |
339 | #define SYNCHRONIZE_SRCU_TRYCOUNT 2 |
340 | #define SYNCHRONIZE_SRCU_EXP_TRYCOUNT 12 | |
cef50120 | 341 | |
18108ebf | 342 | /* |
931ea9d1 | 343 | * @@@ Wait until all pre-existing readers complete. Such readers |
18108ebf | 344 | * will have used the index specified by "idx". |
931ea9d1 LJ |
345 | * the caller should ensures the ->completed is not changed while checking |
346 | * and idx = (->completed & 1) ^ 1 | |
18108ebf | 347 | */ |
931ea9d1 | 348 | static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount) |
cef50120 | 349 | { |
931ea9d1 LJ |
350 | for (;;) { |
351 | if (srcu_readers_active_idx_check(sp, idx)) | |
352 | return true; | |
353 | if (--trycount <= 0) | |
354 | return false; | |
355 | udelay(SRCU_RETRY_CHECK_DELAY); | |
cef50120 | 356 | } |
cef50120 | 357 | } |
c072a388 | 358 | |
931ea9d1 LJ |
359 | /* |
360 | * Increment the ->completed counter so that future SRCU readers will | |
361 | * use the other rank of the ->c[] and ->seq[] arrays. This allows | |
362 | * us to wait for pre-existing readers in a starvation-free manner. | |
363 | */ | |
18108ebf | 364 | static void srcu_flip(struct srcu_struct *sp) |
944ce9af | 365 | { |
18108ebf | 366 | sp->completed++; |
944ce9af LJ |
367 | } |
368 | ||
931ea9d1 LJ |
369 | /* |
370 | * Enqueue an SRCU callback on the specified srcu_struct structure, | |
371 | * initiating grace-period processing if it is not already running. | |
372 | */ | |
373 | void call_srcu(struct srcu_struct *sp, struct rcu_head *head, | |
374 | void (*func)(struct rcu_head *head)) | |
375 | { | |
376 | unsigned long flags; | |
377 | ||
378 | head->next = NULL; | |
379 | head->func = func; | |
380 | spin_lock_irqsave(&sp->queue_lock, flags); | |
381 | rcu_batch_queue(&sp->batch_queue, head); | |
382 | if (!sp->running) { | |
383 | sp->running = true; | |
3b07e9ca | 384 | schedule_delayed_work(&sp->work, 0); |
931ea9d1 LJ |
385 | } |
386 | spin_unlock_irqrestore(&sp->queue_lock, flags); | |
387 | } | |
388 | EXPORT_SYMBOL_GPL(call_srcu); | |
389 | ||
390 | struct rcu_synchronize { | |
391 | struct rcu_head head; | |
392 | struct completion completion; | |
393 | }; | |
394 | ||
395 | /* | |
396 | * Awaken the corresponding synchronize_srcu() instance now that a | |
397 | * grace period has elapsed. | |
398 | */ | |
399 | static void wakeme_after_rcu(struct rcu_head *head) | |
400 | { | |
401 | struct rcu_synchronize *rcu; | |
402 | ||
403 | rcu = container_of(head, struct rcu_synchronize, head); | |
404 | complete(&rcu->completion); | |
405 | } | |
406 | ||
407 | static void srcu_advance_batches(struct srcu_struct *sp, int trycount); | |
408 | static void srcu_reschedule(struct srcu_struct *sp); | |
409 | ||
0cd397d3 PM |
410 | /* |
411 | * Helper function for synchronize_srcu() and synchronize_srcu_expedited(). | |
621934ee | 412 | */ |
d9792edd | 413 | static void __synchronize_srcu(struct srcu_struct *sp, int trycount) |
621934ee | 414 | { |
931ea9d1 LJ |
415 | struct rcu_synchronize rcu; |
416 | struct rcu_head *head = &rcu.head; | |
417 | bool done = false; | |
18108ebf | 418 | |
fe15d706 PM |
419 | rcu_lockdep_assert(!lock_is_held(&sp->dep_map) && |
420 | !lock_is_held(&rcu_bh_lock_map) && | |
421 | !lock_is_held(&rcu_lock_map) && | |
422 | !lock_is_held(&rcu_sched_lock_map), | |
423 | "Illegal synchronize_srcu() in same-type SRCU (or RCU) read-side critical section"); | |
424 | ||
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 | * | |
457 | * Flip the completed counter, and wait for the old count to drain to zero. | |
458 | * As with classic RCU, the updater must use some separate means of | |
459 | * synchronizing concurrent updates. Can block; must be called from | |
460 | * process context. | |
461 | * | |
462 | * Note that it is illegal to call synchronize_srcu() from the corresponding | |
463 | * SRCU read-side critical section; doing so will result in deadlock. | |
464 | * However, it is perfectly legal to call synchronize_srcu() on one | |
465 | * srcu_struct from some other srcu_struct's read-side critical section. | |
466 | */ | |
467 | void synchronize_srcu(struct srcu_struct *sp) | |
468 | { | |
d9792edd | 469 | __synchronize_srcu(sp, SYNCHRONIZE_SRCU_TRYCOUNT); |
0cd397d3 PM |
470 | } |
471 | EXPORT_SYMBOL_GPL(synchronize_srcu); | |
472 | ||
473 | /** | |
236fefaf | 474 | * synchronize_srcu_expedited - Brute-force SRCU grace period |
0cd397d3 PM |
475 | * @sp: srcu_struct with which to synchronize. |
476 | * | |
cef50120 PM |
477 | * Wait for an SRCU grace period to elapse, but be more aggressive about |
478 | * spinning rather than blocking when waiting. | |
0cd397d3 | 479 | * |
236fefaf | 480 | * Note that it is illegal to call this function while holding any lock |
cef50120 | 481 | * that is acquired by a CPU-hotplug notifier. It is also illegal to call |
236fefaf PM |
482 | * synchronize_srcu_expedited() from the corresponding SRCU read-side |
483 | * critical section; doing so will result in deadlock. However, it is | |
484 | * perfectly legal to call synchronize_srcu_expedited() on one srcu_struct | |
485 | * from some other srcu_struct's read-side critical section, as long as | |
486 | * the resulting graph of srcu_structs is acyclic. | |
0cd397d3 PM |
487 | */ |
488 | void synchronize_srcu_expedited(struct srcu_struct *sp) | |
489 | { | |
d9792edd | 490 | __synchronize_srcu(sp, SYNCHRONIZE_SRCU_EXP_TRYCOUNT); |
0cd397d3 PM |
491 | } |
492 | EXPORT_SYMBOL_GPL(synchronize_srcu_expedited); | |
493 | ||
931ea9d1 LJ |
494 | /** |
495 | * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete. | |
496 | */ | |
497 | void srcu_barrier(struct srcu_struct *sp) | |
498 | { | |
499 | synchronize_srcu(sp); | |
500 | } | |
501 | EXPORT_SYMBOL_GPL(srcu_barrier); | |
502 | ||
621934ee PM |
503 | /** |
504 | * srcu_batches_completed - return batches completed. | |
505 | * @sp: srcu_struct on which to report batch completion. | |
506 | * | |
507 | * Report the number of batches, correlated with, but not necessarily | |
508 | * precisely the same as, the number of grace periods that have elapsed. | |
509 | */ | |
621934ee PM |
510 | long srcu_batches_completed(struct srcu_struct *sp) |
511 | { | |
512 | return sp->completed; | |
513 | } | |
621934ee | 514 | EXPORT_SYMBOL_GPL(srcu_batches_completed); |
931ea9d1 LJ |
515 | |
516 | #define SRCU_CALLBACK_BATCH 10 | |
517 | #define SRCU_INTERVAL 1 | |
518 | ||
519 | /* | |
520 | * Move any new SRCU callbacks to the first stage of the SRCU grace | |
521 | * period pipeline. | |
522 | */ | |
523 | static void srcu_collect_new(struct srcu_struct *sp) | |
524 | { | |
525 | if (!rcu_batch_empty(&sp->batch_queue)) { | |
526 | spin_lock_irq(&sp->queue_lock); | |
527 | rcu_batch_move(&sp->batch_check0, &sp->batch_queue); | |
528 | spin_unlock_irq(&sp->queue_lock); | |
529 | } | |
530 | } | |
531 | ||
532 | /* | |
533 | * Core SRCU state machine. Advance callbacks from ->batch_check0 to | |
534 | * ->batch_check1 and then to ->batch_done as readers drain. | |
535 | */ | |
536 | static void srcu_advance_batches(struct srcu_struct *sp, int trycount) | |
537 | { | |
538 | int idx = 1 ^ (sp->completed & 1); | |
539 | ||
540 | /* | |
541 | * Because readers might be delayed for an extended period after | |
542 | * fetching ->completed for their index, at any point in time there | |
543 | * might well be readers using both idx=0 and idx=1. We therefore | |
544 | * need to wait for readers to clear from both index values before | |
545 | * invoking a callback. | |
546 | */ | |
547 | ||
548 | if (rcu_batch_empty(&sp->batch_check0) && | |
549 | rcu_batch_empty(&sp->batch_check1)) | |
550 | return; /* no callbacks need to be advanced */ | |
551 | ||
552 | if (!try_check_zero(sp, idx, trycount)) | |
553 | return; /* failed to advance, will try after SRCU_INTERVAL */ | |
554 | ||
555 | /* | |
556 | * The callbacks in ->batch_check1 have already done with their | |
557 | * first zero check and flip back when they were enqueued on | |
558 | * ->batch_check0 in a previous invocation of srcu_advance_batches(). | |
559 | * (Presumably try_check_zero() returned false during that | |
560 | * invocation, leaving the callbacks stranded on ->batch_check1.) | |
561 | * They are therefore ready to invoke, so move them to ->batch_done. | |
562 | */ | |
563 | rcu_batch_move(&sp->batch_done, &sp->batch_check1); | |
564 | ||
565 | if (rcu_batch_empty(&sp->batch_check0)) | |
566 | return; /* no callbacks need to be advanced */ | |
567 | srcu_flip(sp); | |
568 | ||
569 | /* | |
570 | * The callbacks in ->batch_check0 just finished their | |
571 | * first check zero and flip, so move them to ->batch_check1 | |
572 | * for future checking on the other idx. | |
573 | */ | |
574 | rcu_batch_move(&sp->batch_check1, &sp->batch_check0); | |
575 | ||
576 | /* | |
577 | * SRCU read-side critical sections are normally short, so check | |
578 | * at least twice in quick succession after a flip. | |
579 | */ | |
580 | trycount = trycount < 2 ? 2 : trycount; | |
581 | if (!try_check_zero(sp, idx^1, trycount)) | |
582 | return; /* failed to advance, will try after SRCU_INTERVAL */ | |
583 | ||
584 | /* | |
585 | * The callbacks in ->batch_check1 have now waited for all | |
586 | * pre-existing readers using both idx values. They are therefore | |
587 | * ready to invoke, so move them to ->batch_done. | |
588 | */ | |
589 | rcu_batch_move(&sp->batch_done, &sp->batch_check1); | |
590 | } | |
591 | ||
592 | /* | |
593 | * Invoke a limited number of SRCU callbacks that have passed through | |
594 | * their grace period. If there are more to do, SRCU will reschedule | |
595 | * the workqueue. | |
596 | */ | |
597 | static void srcu_invoke_callbacks(struct srcu_struct *sp) | |
598 | { | |
599 | int i; | |
600 | struct rcu_head *head; | |
601 | ||
602 | for (i = 0; i < SRCU_CALLBACK_BATCH; i++) { | |
603 | head = rcu_batch_dequeue(&sp->batch_done); | |
604 | if (!head) | |
605 | break; | |
606 | local_bh_disable(); | |
607 | head->func(head); | |
608 | local_bh_enable(); | |
609 | } | |
610 | } | |
611 | ||
612 | /* | |
613 | * Finished one round of SRCU grace period. Start another if there are | |
614 | * more SRCU callbacks queued, otherwise put SRCU into not-running state. | |
615 | */ | |
616 | static void srcu_reschedule(struct srcu_struct *sp) | |
617 | { | |
618 | bool pending = true; | |
619 | ||
620 | if (rcu_batch_empty(&sp->batch_done) && | |
621 | rcu_batch_empty(&sp->batch_check1) && | |
622 | rcu_batch_empty(&sp->batch_check0) && | |
623 | rcu_batch_empty(&sp->batch_queue)) { | |
624 | spin_lock_irq(&sp->queue_lock); | |
625 | if (rcu_batch_empty(&sp->batch_done) && | |
626 | rcu_batch_empty(&sp->batch_check1) && | |
627 | rcu_batch_empty(&sp->batch_check0) && | |
628 | rcu_batch_empty(&sp->batch_queue)) { | |
629 | sp->running = false; | |
630 | pending = false; | |
631 | } | |
632 | spin_unlock_irq(&sp->queue_lock); | |
633 | } | |
634 | ||
635 | if (pending) | |
3b07e9ca | 636 | schedule_delayed_work(&sp->work, SRCU_INTERVAL); |
931ea9d1 LJ |
637 | } |
638 | ||
639 | /* | |
640 | * This is the work-queue function that handles SRCU grace periods. | |
641 | */ | |
642 | static void process_srcu(struct work_struct *work) | |
643 | { | |
644 | struct srcu_struct *sp; | |
645 | ||
646 | sp = container_of(work, struct srcu_struct, work.work); | |
647 | ||
648 | srcu_collect_new(sp); | |
649 | srcu_advance_batches(sp, 1); | |
650 | srcu_invoke_callbacks(sp); | |
651 | srcu_reschedule(sp); | |
652 | } |