]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/rcu/srcutree.c
srcu: Print non-default exp_holdoff values at boot time
[mirror_ubuntu-bionic-kernel.git] / kernel / rcu / srcutree.c
CommitLineData
dad81a20
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, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * Copyright (C) IBM Corporation, 2006
19 * Copyright (C) Fujitsu, 2012
20 *
21 * Author: Paul McKenney <paulmck@us.ibm.com>
22 * Lai Jiangshan <laijs@cn.fujitsu.com>
23 *
24 * For detailed explanation of Read-Copy Update mechanism see -
25 * Documentation/RCU/ *.txt
26 *
27 */
28
29#include <linux/export.h>
30#include <linux/mutex.h>
31#include <linux/percpu.h>
32#include <linux/preempt.h>
33#include <linux/rcupdate_wait.h>
34#include <linux/sched.h>
35#include <linux/smp.h>
36#include <linux/delay.h>
22607d66 37#include <linux/module.h>
dad81a20
PM
38#include <linux/srcu.h>
39
dad81a20 40#include "rcu.h"
45753c5f 41#include "rcu_segcblist.h"
dad81a20 42
0c8e0e3c
PM
43/* Holdoff in nanoseconds for auto-expediting. */
44#define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000)
45static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF;
22607d66
PM
46module_param(exp_holdoff, ulong, 0444);
47
da915ad5
PM
48static void srcu_invoke_callbacks(struct work_struct *work);
49static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay);
50
51/*
52 * Initialize SRCU combining tree. Note that statically allocated
53 * srcu_struct structures might already have srcu_read_lock() and
54 * srcu_read_unlock() running against them. So if the is_static parameter
55 * is set, don't initialize ->srcu_lock_count[] and ->srcu_unlock_count[].
56 */
57static void init_srcu_struct_nodes(struct srcu_struct *sp, bool is_static)
dad81a20 58{
da915ad5
PM
59 int cpu;
60 int i;
61 int level = 0;
62 int levelspread[RCU_NUM_LVLS];
63 struct srcu_data *sdp;
64 struct srcu_node *snp;
65 struct srcu_node *snp_first;
66
67 /* Work out the overall tree geometry. */
68 sp->level[0] = &sp->node[0];
69 for (i = 1; i < rcu_num_lvls; i++)
70 sp->level[i] = sp->level[i - 1] + num_rcu_lvl[i - 1];
71 rcu_init_levelspread(levelspread, num_rcu_lvl);
72
73 /* Each pass through this loop initializes one srcu_node structure. */
74 rcu_for_each_node_breadth_first(sp, snp) {
75 spin_lock_init(&snp->lock);
c7e88067
PM
76 WARN_ON_ONCE(ARRAY_SIZE(snp->srcu_have_cbs) !=
77 ARRAY_SIZE(snp->srcu_data_have_cbs));
78 for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {
da915ad5 79 snp->srcu_have_cbs[i] = 0;
c7e88067
PM
80 snp->srcu_data_have_cbs[i] = 0;
81 }
1e9a038b 82 snp->srcu_gp_seq_needed_exp = 0;
da915ad5
PM
83 snp->grplo = -1;
84 snp->grphi = -1;
85 if (snp == &sp->node[0]) {
86 /* Root node, special case. */
87 snp->srcu_parent = NULL;
88 continue;
89 }
90
91 /* Non-root node. */
92 if (snp == sp->level[level + 1])
93 level++;
94 snp->srcu_parent = sp->level[level - 1] +
95 (snp - sp->level[level]) /
96 levelspread[level - 1];
97 }
98
99 /*
100 * Initialize the per-CPU srcu_data array, which feeds into the
101 * leaves of the srcu_node tree.
102 */
103 WARN_ON_ONCE(ARRAY_SIZE(sdp->srcu_lock_count) !=
104 ARRAY_SIZE(sdp->srcu_unlock_count));
105 level = rcu_num_lvls - 1;
106 snp_first = sp->level[level];
107 for_each_possible_cpu(cpu) {
108 sdp = per_cpu_ptr(sp->sda, cpu);
109 spin_lock_init(&sdp->lock);
110 rcu_segcblist_init(&sdp->srcu_cblist);
111 sdp->srcu_cblist_invoking = false;
112 sdp->srcu_gp_seq_needed = sp->srcu_gp_seq;
1e9a038b 113 sdp->srcu_gp_seq_needed_exp = sp->srcu_gp_seq;
da915ad5
PM
114 sdp->mynode = &snp_first[cpu / levelspread[level]];
115 for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {
116 if (snp->grplo < 0)
117 snp->grplo = cpu;
118 snp->grphi = cpu;
119 }
120 sdp->cpu = cpu;
121 INIT_DELAYED_WORK(&sdp->work, srcu_invoke_callbacks);
122 sdp->sp = sp;
c7e88067 123 sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
da915ad5
PM
124 if (is_static)
125 continue;
126
127 /* Dynamically allocated, better be no srcu_read_locks()! */
128 for (i = 0; i < ARRAY_SIZE(sdp->srcu_lock_count); i++) {
129 sdp->srcu_lock_count[i] = 0;
130 sdp->srcu_unlock_count[i] = 0;
131 }
132 }
133}
134
135/*
136 * Initialize non-compile-time initialized fields, including the
137 * associated srcu_node and srcu_data structures. The is_static
138 * parameter is passed through to init_srcu_struct_nodes(), and
139 * also tells us that ->sda has already been wired up to srcu_data.
140 */
141static int init_srcu_struct_fields(struct srcu_struct *sp, bool is_static)
142{
143 mutex_init(&sp->srcu_cb_mutex);
144 mutex_init(&sp->srcu_gp_mutex);
145 sp->srcu_idx = 0;
dad81a20 146 sp->srcu_gp_seq = 0;
da915ad5
PM
147 sp->srcu_barrier_seq = 0;
148 mutex_init(&sp->srcu_barrier_mutex);
149 atomic_set(&sp->srcu_barrier_cpu_cnt, 0);
dad81a20 150 INIT_DELAYED_WORK(&sp->work, process_srcu);
da915ad5
PM
151 if (!is_static)
152 sp->sda = alloc_percpu(struct srcu_data);
153 init_srcu_struct_nodes(sp, is_static);
1e9a038b 154 sp->srcu_gp_seq_needed_exp = 0;
22607d66 155 sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
da915ad5
PM
156 smp_store_release(&sp->srcu_gp_seq_needed, 0); /* Init done. */
157 return sp->sda ? 0 : -ENOMEM;
dad81a20
PM
158}
159
160#ifdef CONFIG_DEBUG_LOCK_ALLOC
161
162int __init_srcu_struct(struct srcu_struct *sp, const char *name,
163 struct lock_class_key *key)
164{
165 /* Don't re-initialize a lock while it is held. */
166 debug_check_no_locks_freed((void *)sp, sizeof(*sp));
167 lockdep_init_map(&sp->dep_map, name, key, 0);
da915ad5
PM
168 spin_lock_init(&sp->gp_lock);
169 return init_srcu_struct_fields(sp, false);
dad81a20
PM
170}
171EXPORT_SYMBOL_GPL(__init_srcu_struct);
172
173#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
174
175/**
176 * init_srcu_struct - initialize a sleep-RCU structure
177 * @sp: structure to initialize.
178 *
179 * Must invoke this on a given srcu_struct before passing that srcu_struct
180 * to any other function. Each srcu_struct represents a separate domain
181 * of SRCU protection.
182 */
183int init_srcu_struct(struct srcu_struct *sp)
184{
da915ad5
PM
185 spin_lock_init(&sp->gp_lock);
186 return init_srcu_struct_fields(sp, false);
dad81a20
PM
187}
188EXPORT_SYMBOL_GPL(init_srcu_struct);
189
190#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
191
192/*
da915ad5
PM
193 * First-use initialization of statically allocated srcu_struct
194 * structure. Wiring up the combining tree is more than can be
195 * done with compile-time initialization, so this check is added
196 * to each update-side SRCU primitive. Use ->gp_lock, which -is-
197 * compile-time initialized, to resolve races involving multiple
198 * CPUs trying to garner first-use privileges.
199 */
200static void check_init_srcu_struct(struct srcu_struct *sp)
201{
202 unsigned long flags;
203
204 WARN_ON_ONCE(rcu_scheduler_active == RCU_SCHEDULER_INIT);
205 /* The smp_load_acquire() pairs with the smp_store_release(). */
206 if (!rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq_needed))) /*^^^*/
207 return; /* Already initialized. */
208 spin_lock_irqsave(&sp->gp_lock, flags);
209 if (!rcu_seq_state(sp->srcu_gp_seq_needed)) {
210 spin_unlock_irqrestore(&sp->gp_lock, flags);
211 return;
212 }
213 init_srcu_struct_fields(sp, true);
214 spin_unlock_irqrestore(&sp->gp_lock, flags);
215}
216
217/*
218 * Returns approximate total of the readers' ->srcu_lock_count[] values
219 * for the rank of per-CPU counters specified by idx.
dad81a20
PM
220 */
221static unsigned long srcu_readers_lock_idx(struct srcu_struct *sp, int idx)
222{
223 int cpu;
224 unsigned long sum = 0;
225
226 for_each_possible_cpu(cpu) {
da915ad5 227 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
dad81a20 228
da915ad5 229 sum += READ_ONCE(cpuc->srcu_lock_count[idx]);
dad81a20
PM
230 }
231 return sum;
232}
233
234/*
da915ad5
PM
235 * Returns approximate total of the readers' ->srcu_unlock_count[] values
236 * for the rank of per-CPU counters specified by idx.
dad81a20
PM
237 */
238static unsigned long srcu_readers_unlock_idx(struct srcu_struct *sp, int idx)
239{
240 int cpu;
241 unsigned long sum = 0;
242
243 for_each_possible_cpu(cpu) {
da915ad5 244 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
dad81a20 245
da915ad5 246 sum += READ_ONCE(cpuc->srcu_unlock_count[idx]);
dad81a20
PM
247 }
248 return sum;
249}
250
251/*
252 * Return true if the number of pre-existing readers is determined to
253 * be zero.
254 */
255static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
256{
257 unsigned long unlocks;
258
259 unlocks = srcu_readers_unlock_idx(sp, idx);
260
261 /*
262 * Make sure that a lock is always counted if the corresponding
263 * unlock is counted. Needs to be a smp_mb() as the read side may
264 * contain a read from a variable that is written to before the
265 * synchronize_srcu() in the write side. In this case smp_mb()s
266 * A and B act like the store buffering pattern.
267 *
268 * This smp_mb() also pairs with smp_mb() C to prevent accesses
269 * after the synchronize_srcu() from being executed before the
270 * grace period ends.
271 */
272 smp_mb(); /* A */
273
274 /*
275 * If the locks are the same as the unlocks, then there must have
276 * been no readers on this index at some time in between. This does
277 * not mean that there are no more readers, as one could have read
278 * the current index but not have incremented the lock counter yet.
279 *
881ec9d2
PM
280 * So suppose that the updater is preempted here for so long
281 * that more than ULONG_MAX non-nested readers come and go in
282 * the meantime. It turns out that this cannot result in overflow
283 * because if a reader modifies its unlock count after we read it
284 * above, then that reader's next load of ->srcu_idx is guaranteed
285 * to get the new value, which will cause it to operate on the
286 * other bank of counters, where it cannot contribute to the
287 * overflow of these counters. This means that there is a maximum
288 * of 2*NR_CPUS increments, which cannot overflow given current
289 * systems, especially not on 64-bit systems.
290 *
291 * OK, how about nesting? This does impose a limit on nesting
292 * of floor(ULONG_MAX/NR_CPUS/2), which should be sufficient,
293 * especially on 64-bit systems.
dad81a20
PM
294 */
295 return srcu_readers_lock_idx(sp, idx) == unlocks;
296}
297
298/**
299 * srcu_readers_active - returns true if there are readers. and false
300 * otherwise
301 * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
302 *
303 * Note that this is not an atomic primitive, and can therefore suffer
304 * severe errors when invoked on an active srcu_struct. That said, it
305 * can be useful as an error check at cleanup time.
306 */
307static bool srcu_readers_active(struct srcu_struct *sp)
308{
309 int cpu;
310 unsigned long sum = 0;
311
312 for_each_possible_cpu(cpu) {
da915ad5 313 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
dad81a20 314
da915ad5
PM
315 sum += READ_ONCE(cpuc->srcu_lock_count[0]);
316 sum += READ_ONCE(cpuc->srcu_lock_count[1]);
317 sum -= READ_ONCE(cpuc->srcu_unlock_count[0]);
318 sum -= READ_ONCE(cpuc->srcu_unlock_count[1]);
dad81a20
PM
319 }
320 return sum;
321}
322
323#define SRCU_INTERVAL 1
324
1e9a038b
PM
325/*
326 * Return grace-period delay, zero if there are expedited grace
327 * periods pending, SRCU_INTERVAL otherwise.
328 */
329static unsigned long srcu_get_delay(struct srcu_struct *sp)
330{
331 if (ULONG_CMP_LT(READ_ONCE(sp->srcu_gp_seq),
332 READ_ONCE(sp->srcu_gp_seq_needed_exp)))
333 return 0;
334 return SRCU_INTERVAL;
335}
336
dad81a20
PM
337/**
338 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
339 * @sp: structure to clean up.
340 *
341 * Must invoke this after you are finished using a given srcu_struct that
342 * was initialized via init_srcu_struct(), else you leak memory.
343 */
344void cleanup_srcu_struct(struct srcu_struct *sp)
345{
da915ad5
PM
346 int cpu;
347
1e9a038b
PM
348 if (WARN_ON(!srcu_get_delay(sp)))
349 return; /* Leakage unless caller handles error. */
dad81a20
PM
350 if (WARN_ON(srcu_readers_active(sp)))
351 return; /* Leakage unless caller handles error. */
dad81a20 352 flush_delayed_work(&sp->work);
da915ad5
PM
353 for_each_possible_cpu(cpu)
354 flush_delayed_work(&per_cpu_ptr(sp->sda, cpu)->work);
355 if (WARN_ON(rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
356 WARN_ON(srcu_readers_active(sp))) {
357 pr_info("cleanup_srcu_struct: Active srcu_struct %p state: %d\n", sp, rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)));
dad81a20
PM
358 return; /* Caller forgot to stop doing call_srcu()? */
359 }
da915ad5
PM
360 free_percpu(sp->sda);
361 sp->sda = NULL;
dad81a20
PM
362}
363EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
364
365/*
366 * Counts the new reader in the appropriate per-CPU element of the
cdf7abc4 367 * srcu_struct.
dad81a20
PM
368 * Returns an index that must be passed to the matching srcu_read_unlock().
369 */
370int __srcu_read_lock(struct srcu_struct *sp)
371{
372 int idx;
373
da915ad5 374 idx = READ_ONCE(sp->srcu_idx) & 0x1;
cdf7abc4 375 this_cpu_inc(sp->sda->srcu_lock_count[idx]);
dad81a20
PM
376 smp_mb(); /* B */ /* Avoid leaking the critical section. */
377 return idx;
378}
379EXPORT_SYMBOL_GPL(__srcu_read_lock);
380
381/*
382 * Removes the count for the old reader from the appropriate per-CPU
383 * element of the srcu_struct. Note that this may well be a different
384 * CPU than that which was incremented by the corresponding srcu_read_lock().
dad81a20
PM
385 */
386void __srcu_read_unlock(struct srcu_struct *sp, int idx)
387{
388 smp_mb(); /* C */ /* Avoid leaking the critical section. */
da915ad5 389 this_cpu_inc(sp->sda->srcu_unlock_count[idx]);
dad81a20
PM
390}
391EXPORT_SYMBOL_GPL(__srcu_read_unlock);
392
393/*
394 * We use an adaptive strategy for synchronize_srcu() and especially for
395 * synchronize_srcu_expedited(). We spin for a fixed time period
396 * (defined below) to allow SRCU readers to exit their read-side critical
397 * sections. If there are still some readers after a few microseconds,
398 * we repeatedly block for 1-millisecond time periods.
399 */
400#define SRCU_RETRY_CHECK_DELAY 5
401
402/*
403 * Start an SRCU grace period.
404 */
405static void srcu_gp_start(struct srcu_struct *sp)
406{
da915ad5 407 struct srcu_data *sdp = this_cpu_ptr(sp->sda);
dad81a20
PM
408 int state;
409
da915ad5
PM
410 RCU_LOCKDEP_WARN(!lockdep_is_held(&sp->gp_lock),
411 "Invoked srcu_gp_start() without ->gp_lock!");
412 WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
413 rcu_segcblist_advance(&sdp->srcu_cblist,
414 rcu_seq_current(&sp->srcu_gp_seq));
415 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
416 rcu_seq_snap(&sp->srcu_gp_seq));
2da4b2a7 417 smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
dad81a20
PM
418 rcu_seq_start(&sp->srcu_gp_seq);
419 state = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
420 WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
421}
422
da915ad5
PM
423/*
424 * Track online CPUs to guide callback workqueue placement.
425 */
426DEFINE_PER_CPU(bool, srcu_online);
427
428void srcu_online_cpu(unsigned int cpu)
429{
430 WRITE_ONCE(per_cpu(srcu_online, cpu), true);
431}
432
433void srcu_offline_cpu(unsigned int cpu)
434{
435 WRITE_ONCE(per_cpu(srcu_online, cpu), false);
436}
437
438/*
439 * Place the workqueue handler on the specified CPU if online, otherwise
440 * just run it whereever. This is useful for placing workqueue handlers
441 * that are to invoke the specified CPU's callbacks.
442 */
443static bool srcu_queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
444 struct delayed_work *dwork,
445 unsigned long delay)
446{
447 bool ret;
448
449 preempt_disable();
450 if (READ_ONCE(per_cpu(srcu_online, cpu)))
451 ret = queue_delayed_work_on(cpu, wq, dwork, delay);
452 else
453 ret = queue_delayed_work(wq, dwork, delay);
454 preempt_enable();
455 return ret;
456}
457
458/*
459 * Schedule callback invocation for the specified srcu_data structure,
460 * if possible, on the corresponding CPU.
461 */
462static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
463{
464 srcu_queue_delayed_work_on(sdp->cpu, system_power_efficient_wq,
465 &sdp->work, delay);
466}
467
468/*
469 * Schedule callback invocation for all srcu_data structures associated
c7e88067
PM
470 * with the specified srcu_node structure that have callbacks for the
471 * just-completed grace period, the one corresponding to idx. If possible,
472 * schedule this invocation on the corresponding CPUs.
da915ad5 473 */
c7e88067 474static void srcu_schedule_cbs_snp(struct srcu_struct *sp, struct srcu_node *snp,
1e9a038b 475 unsigned long mask, unsigned long delay)
da915ad5
PM
476{
477 int cpu;
478
c7e88067
PM
479 for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
480 if (!(mask & (1 << (cpu - snp->grplo))))
481 continue;
1e9a038b 482 srcu_schedule_cbs_sdp(per_cpu_ptr(sp->sda, cpu), delay);
c7e88067 483 }
da915ad5
PM
484}
485
486/*
487 * Note the end of an SRCU grace period. Initiates callback invocation
488 * and starts a new grace period if needed.
489 *
490 * The ->srcu_cb_mutex acquisition does not protect any data, but
491 * instead prevents more than one grace period from starting while we
492 * are initiating callback invocation. This allows the ->srcu_have_cbs[]
493 * array to have a finite number of elements.
494 */
495static void srcu_gp_end(struct srcu_struct *sp)
496{
1e9a038b 497 unsigned long cbdelay;
da915ad5
PM
498 bool cbs;
499 unsigned long gpseq;
500 int idx;
501 int idxnext;
c7e88067 502 unsigned long mask;
da915ad5
PM
503 struct srcu_node *snp;
504
505 /* Prevent more than one additional grace period. */
506 mutex_lock(&sp->srcu_cb_mutex);
507
508 /* End the current grace period. */
509 spin_lock_irq(&sp->gp_lock);
510 idx = rcu_seq_state(sp->srcu_gp_seq);
511 WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
1e9a038b 512 cbdelay = srcu_get_delay(sp);
22607d66 513 sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
da915ad5
PM
514 rcu_seq_end(&sp->srcu_gp_seq);
515 gpseq = rcu_seq_current(&sp->srcu_gp_seq);
1e9a038b
PM
516 if (ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, gpseq))
517 sp->srcu_gp_seq_needed_exp = gpseq;
da915ad5
PM
518 spin_unlock_irq(&sp->gp_lock);
519 mutex_unlock(&sp->srcu_gp_mutex);
520 /* A new grace period can start at this point. But only one. */
521
522 /* Initiate callback invocation as needed. */
523 idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
524 idxnext = (idx + 1) % ARRAY_SIZE(snp->srcu_have_cbs);
525 rcu_for_each_node_breadth_first(sp, snp) {
526 spin_lock_irq(&snp->lock);
527 cbs = false;
528 if (snp >= sp->level[rcu_num_lvls - 1])
529 cbs = snp->srcu_have_cbs[idx] == gpseq;
530 snp->srcu_have_cbs[idx] = gpseq;
531 rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
1e9a038b
PM
532 if (ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, gpseq))
533 snp->srcu_gp_seq_needed_exp = gpseq;
c7e88067
PM
534 mask = snp->srcu_data_have_cbs[idx];
535 snp->srcu_data_have_cbs[idx] = 0;
da915ad5
PM
536 spin_unlock_irq(&snp->lock);
537 if (cbs) {
538 smp_mb(); /* GP end before CB invocation. */
1e9a038b 539 srcu_schedule_cbs_snp(sp, snp, mask, cbdelay);
da915ad5
PM
540 }
541 }
542
543 /* Callback initiation done, allow grace periods after next. */
544 mutex_unlock(&sp->srcu_cb_mutex);
545
546 /* Start a new grace period if needed. */
547 spin_lock_irq(&sp->gp_lock);
548 gpseq = rcu_seq_current(&sp->srcu_gp_seq);
549 if (!rcu_seq_state(gpseq) &&
550 ULONG_CMP_LT(gpseq, sp->srcu_gp_seq_needed)) {
551 srcu_gp_start(sp);
552 spin_unlock_irq(&sp->gp_lock);
553 /* Throttle expedited grace periods: Should be rare! */
1e9a038b
PM
554 srcu_reschedule(sp, rcu_seq_ctr(gpseq) & 0x3ff
555 ? 0 : SRCU_INTERVAL);
da915ad5
PM
556 } else {
557 spin_unlock_irq(&sp->gp_lock);
558 }
559}
560
1e9a038b
PM
561/*
562 * Funnel-locking scheme to scalably mediate many concurrent expedited
563 * grace-period requests. This function is invoked for the first known
564 * expedited request for a grace period that has already been requested,
565 * but without expediting. To start a completely new grace period,
566 * whether expedited or not, use srcu_funnel_gp_start() instead.
567 */
568static void srcu_funnel_exp_start(struct srcu_struct *sp, struct srcu_node *snp,
569 unsigned long s)
570{
571 unsigned long flags;
572
573 for (; snp != NULL; snp = snp->srcu_parent) {
574 if (rcu_seq_done(&sp->srcu_gp_seq, s) ||
575 ULONG_CMP_GE(READ_ONCE(snp->srcu_gp_seq_needed_exp), s))
576 return;
577 spin_lock_irqsave(&snp->lock, flags);
578 if (ULONG_CMP_GE(snp->srcu_gp_seq_needed_exp, s)) {
579 spin_unlock_irqrestore(&snp->lock, flags);
580 return;
581 }
582 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
583 spin_unlock_irqrestore(&snp->lock, flags);
584 }
585 spin_lock_irqsave(&sp->gp_lock, flags);
586 if (!ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
587 sp->srcu_gp_seq_needed_exp = s;
588 spin_unlock_irqrestore(&sp->gp_lock, flags);
589}
590
da915ad5
PM
591/*
592 * Funnel-locking scheme to scalably mediate many concurrent grace-period
593 * requests. The winner has to do the work of actually starting grace
594 * period s. Losers must either ensure that their desired grace-period
595 * number is recorded on at least their leaf srcu_node structure, or they
596 * must take steps to invoke their own callbacks.
597 */
1e9a038b
PM
598static void srcu_funnel_gp_start(struct srcu_struct *sp, struct srcu_data *sdp,
599 unsigned long s, bool do_norm)
da915ad5
PM
600{
601 unsigned long flags;
602 int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
603 struct srcu_node *snp = sdp->mynode;
604 unsigned long snp_seq;
605
606 /* Each pass through the loop does one level of the srcu_node tree. */
607 for (; snp != NULL; snp = snp->srcu_parent) {
608 if (rcu_seq_done(&sp->srcu_gp_seq, s) && snp != sdp->mynode)
609 return; /* GP already done and CBs recorded. */
610 spin_lock_irqsave(&snp->lock, flags);
611 if (ULONG_CMP_GE(snp->srcu_have_cbs[idx], s)) {
612 snp_seq = snp->srcu_have_cbs[idx];
c7e88067
PM
613 if (snp == sdp->mynode && snp_seq == s)
614 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
da915ad5
PM
615 spin_unlock_irqrestore(&snp->lock, flags);
616 if (snp == sdp->mynode && snp_seq != s) {
617 smp_mb(); /* CBs after GP! */
1e9a038b
PM
618 srcu_schedule_cbs_sdp(sdp, do_norm
619 ? SRCU_INTERVAL
620 : 0);
621 return;
da915ad5 622 }
1e9a038b
PM
623 if (!do_norm)
624 srcu_funnel_exp_start(sp, snp, s);
da915ad5
PM
625 return;
626 }
627 snp->srcu_have_cbs[idx] = s;
c7e88067
PM
628 if (snp == sdp->mynode)
629 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
1e9a038b
PM
630 if (!do_norm && ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, s))
631 snp->srcu_gp_seq_needed_exp = s;
da915ad5
PM
632 spin_unlock_irqrestore(&snp->lock, flags);
633 }
634
635 /* Top of tree, must ensure the grace period will be started. */
636 spin_lock_irqsave(&sp->gp_lock, flags);
637 if (ULONG_CMP_LT(sp->srcu_gp_seq_needed, s)) {
638 /*
639 * Record need for grace period s. Pair with load
640 * acquire setting up for initialization.
641 */
642 smp_store_release(&sp->srcu_gp_seq_needed, s); /*^^^*/
643 }
1e9a038b
PM
644 if (!do_norm && ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
645 sp->srcu_gp_seq_needed_exp = s;
da915ad5
PM
646
647 /* If grace period not already done and none in progress, start it. */
648 if (!rcu_seq_done(&sp->srcu_gp_seq, s) &&
649 rcu_seq_state(sp->srcu_gp_seq) == SRCU_STATE_IDLE) {
650 WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
651 srcu_gp_start(sp);
652 queue_delayed_work(system_power_efficient_wq, &sp->work,
1e9a038b 653 srcu_get_delay(sp));
da915ad5
PM
654 }
655 spin_unlock_irqrestore(&sp->gp_lock, flags);
656}
657
dad81a20
PM
658/*
659 * Wait until all readers counted by array index idx complete, but
660 * loop an additional time if there is an expedited grace period pending.
da915ad5 661 * The caller must ensure that ->srcu_idx is not changed while checking.
dad81a20
PM
662 */
663static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
664{
665 for (;;) {
666 if (srcu_readers_active_idx_check(sp, idx))
667 return true;
1e9a038b 668 if (--trycount + !srcu_get_delay(sp) <= 0)
dad81a20
PM
669 return false;
670 udelay(SRCU_RETRY_CHECK_DELAY);
671 }
672}
673
674/*
da915ad5
PM
675 * Increment the ->srcu_idx counter so that future SRCU readers will
676 * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows
dad81a20
PM
677 * us to wait for pre-existing readers in a starvation-free manner.
678 */
679static void srcu_flip(struct srcu_struct *sp)
680{
881ec9d2
PM
681 /*
682 * Ensure that if this updater saw a given reader's increment
683 * from __srcu_read_lock(), that reader was using an old value
684 * of ->srcu_idx. Also ensure that if a given reader sees the
685 * new value of ->srcu_idx, this updater's earlier scans cannot
686 * have seen that reader's increments (which is OK, because this
687 * grace period need not wait on that reader).
688 */
689 smp_mb(); /* E */ /* Pairs with B and C. */
690
da915ad5 691 WRITE_ONCE(sp->srcu_idx, sp->srcu_idx + 1);
dad81a20
PM
692
693 /*
694 * Ensure that if the updater misses an __srcu_read_unlock()
695 * increment, that task's next __srcu_read_lock() will see the
696 * above counter update. Note that both this memory barrier
697 * and the one in srcu_readers_active_idx_check() provide the
698 * guarantee for __srcu_read_lock().
699 */
700 smp_mb(); /* D */ /* Pairs with C. */
701}
702
2da4b2a7
PM
703/*
704 * If SRCU is likely idle, return true, otherwise return false.
705 *
706 * Note that it is OK for several current from-idle requests for a new
707 * grace period from idle to specify expediting because they will all end
708 * up requesting the same grace period anyhow. So no loss.
709 *
710 * Note also that if any CPU (including the current one) is still invoking
711 * callbacks, this function will nevertheless say "idle". This is not
712 * ideal, but the overhead of checking all CPUs' callback lists is even
713 * less ideal, especially on large systems. Furthermore, the wakeup
714 * can happen before the callback is fully removed, so we have no choice
715 * but to accept this type of error.
716 *
717 * This function is also subject to counter-wrap errors, but let's face
718 * it, if this function was preempted for enough time for the counters
719 * to wrap, it really doesn't matter whether or not we expedite the grace
720 * period. The extra overhead of a needlessly expedited grace period is
721 * negligible when amoritized over that time period, and the extra latency
722 * of a needlessly non-expedited grace period is similarly negligible.
723 */
724static bool srcu_might_be_idle(struct srcu_struct *sp)
725{
22607d66 726 unsigned long curseq;
2da4b2a7
PM
727 unsigned long flags;
728 struct srcu_data *sdp;
22607d66 729 unsigned long t;
2da4b2a7
PM
730
731 /* If the local srcu_data structure has callbacks, not idle. */
732 local_irq_save(flags);
733 sdp = this_cpu_ptr(sp->sda);
734 if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
735 local_irq_restore(flags);
736 return false; /* Callbacks already present, so not idle. */
737 }
738 local_irq_restore(flags);
739
740 /*
741 * No local callbacks, so probabalistically probe global state.
742 * Exact information would require acquiring locks, which would
743 * kill scalability, hence the probabalistic nature of the probe.
744 */
22607d66
PM
745
746 /* First, see if enough time has passed since the last GP. */
747 t = ktime_get_mono_fast_ns();
748 if (exp_holdoff == 0 ||
749 time_in_range_open(t, sp->srcu_last_gp_end,
750 sp->srcu_last_gp_end + exp_holdoff))
751 return false; /* Too soon after last GP. */
752
753 /* Next, check for probable idleness. */
2da4b2a7
PM
754 curseq = rcu_seq_current(&sp->srcu_gp_seq);
755 smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
756 if (ULONG_CMP_LT(curseq, READ_ONCE(sp->srcu_gp_seq_needed)))
757 return false; /* Grace period in progress, so not idle. */
758 smp_mb(); /* Order ->srcu_gp_seq with prior access. */
759 if (curseq != rcu_seq_current(&sp->srcu_gp_seq))
760 return false; /* GP # changed, so not idle. */
761 return true; /* With reasonable probability, idle! */
762}
763
dad81a20 764/*
da915ad5
PM
765 * Enqueue an SRCU callback on the srcu_data structure associated with
766 * the current CPU and the specified srcu_struct structure, initiating
767 * grace-period processing if it is not already running.
dad81a20
PM
768 *
769 * Note that all CPUs must agree that the grace period extended beyond
770 * all pre-existing SRCU read-side critical section. On systems with
771 * more than one CPU, this means that when "func()" is invoked, each CPU
772 * is guaranteed to have executed a full memory barrier since the end of
773 * its last corresponding SRCU read-side critical section whose beginning
774 * preceded the call to call_rcu(). It also means that each CPU executing
775 * an SRCU read-side critical section that continues beyond the start of
776 * "func()" must have executed a memory barrier after the call_rcu()
777 * but before the beginning of that SRCU read-side critical section.
778 * Note that these guarantees include CPUs that are offline, idle, or
779 * executing in user mode, as well as CPUs that are executing in the kernel.
780 *
781 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
782 * resulting SRCU callback function "func()", then both CPU A and CPU
783 * B are guaranteed to execute a full memory barrier during the time
784 * interval between the call to call_rcu() and the invocation of "func()".
785 * This guarantee applies even if CPU A and CPU B are the same CPU (but
786 * again only if the system has more than one CPU).
787 *
788 * Of course, these guarantees apply only for invocations of call_srcu(),
789 * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
790 * srcu_struct structure.
791 */
1e9a038b
PM
792void __call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
793 rcu_callback_t func, bool do_norm)
dad81a20
PM
794{
795 unsigned long flags;
1e9a038b 796 bool needexp = false;
da915ad5
PM
797 bool needgp = false;
798 unsigned long s;
799 struct srcu_data *sdp;
800
801 check_init_srcu_struct(sp);
802 rhp->func = func;
803 local_irq_save(flags);
804 sdp = this_cpu_ptr(sp->sda);
805 spin_lock(&sdp->lock);
806 rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp, false);
807 rcu_segcblist_advance(&sdp->srcu_cblist,
808 rcu_seq_current(&sp->srcu_gp_seq));
809 s = rcu_seq_snap(&sp->srcu_gp_seq);
810 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s);
811 if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
812 sdp->srcu_gp_seq_needed = s;
813 needgp = true;
dad81a20 814 }
1e9a038b
PM
815 if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
816 sdp->srcu_gp_seq_needed_exp = s;
817 needexp = true;
818 }
da915ad5
PM
819 spin_unlock_irqrestore(&sdp->lock, flags);
820 if (needgp)
1e9a038b
PM
821 srcu_funnel_gp_start(sp, sdp, s, do_norm);
822 else if (needexp)
823 srcu_funnel_exp_start(sp, sdp->mynode, s);
824}
825
826void call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
827 rcu_callback_t func)
828{
829 __call_srcu(sp, rhp, func, true);
dad81a20
PM
830}
831EXPORT_SYMBOL_GPL(call_srcu);
832
dad81a20
PM
833/*
834 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
835 */
1e9a038b 836static void __synchronize_srcu(struct srcu_struct *sp, bool do_norm)
dad81a20
PM
837{
838 struct rcu_synchronize rcu;
dad81a20
PM
839
840 RCU_LOCKDEP_WARN(lock_is_held(&sp->dep_map) ||
841 lock_is_held(&rcu_bh_lock_map) ||
842 lock_is_held(&rcu_lock_map) ||
843 lock_is_held(&rcu_sched_lock_map),
844 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
845
846 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
847 return;
848 might_sleep();
da915ad5 849 check_init_srcu_struct(sp);
dad81a20 850 init_completion(&rcu.completion);
da915ad5 851 init_rcu_head_on_stack(&rcu.head);
1e9a038b 852 __call_srcu(sp, &rcu.head, wakeme_after_rcu, do_norm);
dad81a20 853 wait_for_completion(&rcu.completion);
da915ad5 854 destroy_rcu_head_on_stack(&rcu.head);
dad81a20
PM
855}
856
857/**
858 * synchronize_srcu_expedited - Brute-force SRCU grace period
859 * @sp: srcu_struct with which to synchronize.
860 *
861 * Wait for an SRCU grace period to elapse, but be more aggressive about
862 * spinning rather than blocking when waiting.
863 *
864 * Note that synchronize_srcu_expedited() has the same deadlock and
865 * memory-ordering properties as does synchronize_srcu().
866 */
867void synchronize_srcu_expedited(struct srcu_struct *sp)
868{
1e9a038b 869 __synchronize_srcu(sp, rcu_gp_is_normal());
dad81a20
PM
870}
871EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
872
873/**
874 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
875 * @sp: srcu_struct with which to synchronize.
876 *
877 * Wait for the count to drain to zero of both indexes. To avoid the
878 * possible starvation of synchronize_srcu(), it waits for the count of
da915ad5
PM
879 * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
880 * and then flip the srcu_idx and wait for the count of the other index.
dad81a20
PM
881 *
882 * Can block; must be called from process context.
883 *
884 * Note that it is illegal to call synchronize_srcu() from the corresponding
885 * SRCU read-side critical section; doing so will result in deadlock.
886 * However, it is perfectly legal to call synchronize_srcu() on one
887 * srcu_struct from some other srcu_struct's read-side critical section,
888 * as long as the resulting graph of srcu_structs is acyclic.
889 *
890 * There are memory-ordering constraints implied by synchronize_srcu().
891 * On systems with more than one CPU, when synchronize_srcu() returns,
892 * each CPU is guaranteed to have executed a full memory barrier since
893 * the end of its last corresponding SRCU-sched read-side critical section
894 * whose beginning preceded the call to synchronize_srcu(). In addition,
895 * each CPU having an SRCU read-side critical section that extends beyond
896 * the return from synchronize_srcu() is guaranteed to have executed a
897 * full memory barrier after the beginning of synchronize_srcu() and before
898 * the beginning of that SRCU read-side critical section. Note that these
899 * guarantees include CPUs that are offline, idle, or executing in user mode,
900 * as well as CPUs that are executing in the kernel.
901 *
902 * Furthermore, if CPU A invoked synchronize_srcu(), which returned
903 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
904 * to have executed a full memory barrier during the execution of
905 * synchronize_srcu(). This guarantee applies even if CPU A and CPU B
906 * are the same CPU, but again only if the system has more than one CPU.
907 *
908 * Of course, these memory-ordering guarantees apply only when
909 * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
910 * passed the same srcu_struct structure.
2da4b2a7
PM
911 *
912 * If SRCU is likely idle, expedite the first request. This semantic
913 * was provided by Classic SRCU, and is relied upon by its users, so TREE
914 * SRCU must also provide it. Note that detecting idleness is heuristic
915 * and subject to both false positives and negatives.
dad81a20
PM
916 */
917void synchronize_srcu(struct srcu_struct *sp)
918{
2da4b2a7 919 if (srcu_might_be_idle(sp) || rcu_gp_is_expedited())
dad81a20
PM
920 synchronize_srcu_expedited(sp);
921 else
1e9a038b 922 __synchronize_srcu(sp, true);
dad81a20
PM
923}
924EXPORT_SYMBOL_GPL(synchronize_srcu);
925
da915ad5
PM
926/*
927 * Callback function for srcu_barrier() use.
928 */
929static void srcu_barrier_cb(struct rcu_head *rhp)
930{
931 struct srcu_data *sdp;
932 struct srcu_struct *sp;
933
934 sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
935 sp = sdp->sp;
936 if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
937 complete(&sp->srcu_barrier_completion);
938}
939
dad81a20
PM
940/**
941 * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
942 * @sp: srcu_struct on which to wait for in-flight callbacks.
943 */
944void srcu_barrier(struct srcu_struct *sp)
945{
da915ad5
PM
946 int cpu;
947 struct srcu_data *sdp;
948 unsigned long s = rcu_seq_snap(&sp->srcu_barrier_seq);
949
950 check_init_srcu_struct(sp);
951 mutex_lock(&sp->srcu_barrier_mutex);
952 if (rcu_seq_done(&sp->srcu_barrier_seq, s)) {
953 smp_mb(); /* Force ordering following return. */
954 mutex_unlock(&sp->srcu_barrier_mutex);
955 return; /* Someone else did our work for us. */
956 }
957 rcu_seq_start(&sp->srcu_barrier_seq);
958 init_completion(&sp->srcu_barrier_completion);
959
960 /* Initial count prevents reaching zero until all CBs are posted. */
961 atomic_set(&sp->srcu_barrier_cpu_cnt, 1);
962
963 /*
964 * Each pass through this loop enqueues a callback, but only
965 * on CPUs already having callbacks enqueued. Note that if
966 * a CPU already has callbacks enqueue, it must have already
967 * registered the need for a future grace period, so all we
968 * need do is enqueue a callback that will use the same
969 * grace period as the last callback already in the queue.
970 */
971 for_each_possible_cpu(cpu) {
972 sdp = per_cpu_ptr(sp->sda, cpu);
973 spin_lock_irq(&sdp->lock);
974 atomic_inc(&sp->srcu_barrier_cpu_cnt);
975 sdp->srcu_barrier_head.func = srcu_barrier_cb;
976 if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
977 &sdp->srcu_barrier_head, 0))
978 atomic_dec(&sp->srcu_barrier_cpu_cnt);
979 spin_unlock_irq(&sdp->lock);
980 }
981
982 /* Remove the initial count, at which point reaching zero can happen. */
983 if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
984 complete(&sp->srcu_barrier_completion);
985 wait_for_completion(&sp->srcu_barrier_completion);
986
987 rcu_seq_end(&sp->srcu_barrier_seq);
988 mutex_unlock(&sp->srcu_barrier_mutex);
dad81a20
PM
989}
990EXPORT_SYMBOL_GPL(srcu_barrier);
991
992/**
993 * srcu_batches_completed - return batches completed.
994 * @sp: srcu_struct on which to report batch completion.
995 *
996 * Report the number of batches, correlated with, but not necessarily
997 * precisely the same as, the number of grace periods that have elapsed.
998 */
999unsigned long srcu_batches_completed(struct srcu_struct *sp)
1000{
da915ad5 1001 return sp->srcu_idx;
dad81a20
PM
1002}
1003EXPORT_SYMBOL_GPL(srcu_batches_completed);
1004
1005/*
da915ad5
PM
1006 * Core SRCU state machine. Push state bits of ->srcu_gp_seq
1007 * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
1008 * completed in that state.
dad81a20 1009 */
da915ad5 1010static void srcu_advance_state(struct srcu_struct *sp)
dad81a20
PM
1011{
1012 int idx;
1013
da915ad5
PM
1014 mutex_lock(&sp->srcu_gp_mutex);
1015
dad81a20
PM
1016 /*
1017 * Because readers might be delayed for an extended period after
da915ad5 1018 * fetching ->srcu_idx for their index, at any point in time there
dad81a20
PM
1019 * might well be readers using both idx=0 and idx=1. We therefore
1020 * need to wait for readers to clear from both index values before
1021 * invoking a callback.
1022 *
1023 * The load-acquire ensures that we see the accesses performed
1024 * by the prior grace period.
1025 */
1026 idx = rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq)); /* ^^^ */
1027 if (idx == SRCU_STATE_IDLE) {
da915ad5
PM
1028 spin_lock_irq(&sp->gp_lock);
1029 if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
1030 WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq));
1031 spin_unlock_irq(&sp->gp_lock);
1032 mutex_unlock(&sp->srcu_gp_mutex);
dad81a20
PM
1033 return;
1034 }
1035 idx = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
1036 if (idx == SRCU_STATE_IDLE)
1037 srcu_gp_start(sp);
da915ad5
PM
1038 spin_unlock_irq(&sp->gp_lock);
1039 if (idx != SRCU_STATE_IDLE) {
1040 mutex_unlock(&sp->srcu_gp_mutex);
dad81a20 1041 return; /* Someone else started the grace period. */
da915ad5 1042 }
dad81a20
PM
1043 }
1044
1045 if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
da915ad5
PM
1046 idx = 1 ^ (sp->srcu_idx & 1);
1047 if (!try_check_zero(sp, idx, 1)) {
1048 mutex_unlock(&sp->srcu_gp_mutex);
dad81a20 1049 return; /* readers present, retry later. */
da915ad5 1050 }
dad81a20
PM
1051 srcu_flip(sp);
1052 rcu_seq_set_state(&sp->srcu_gp_seq, SRCU_STATE_SCAN2);
1053 }
1054
1055 if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
1056
1057 /*
1058 * SRCU read-side critical sections are normally short,
1059 * so check at least twice in quick succession after a flip.
1060 */
da915ad5
PM
1061 idx = 1 ^ (sp->srcu_idx & 1);
1062 if (!try_check_zero(sp, idx, 2)) {
1063 mutex_unlock(&sp->srcu_gp_mutex);
1064 return; /* readers present, retry later. */
1065 }
1066 srcu_gp_end(sp); /* Releases ->srcu_gp_mutex. */
dad81a20
PM
1067 }
1068}
1069
1070/*
1071 * Invoke a limited number of SRCU callbacks that have passed through
1072 * their grace period. If there are more to do, SRCU will reschedule
1073 * the workqueue. Note that needed memory barriers have been executed
1074 * in this task's context by srcu_readers_active_idx_check().
1075 */
da915ad5 1076static void srcu_invoke_callbacks(struct work_struct *work)
dad81a20 1077{
da915ad5 1078 bool more;
dad81a20
PM
1079 struct rcu_cblist ready_cbs;
1080 struct rcu_head *rhp;
da915ad5
PM
1081 struct srcu_data *sdp;
1082 struct srcu_struct *sp;
dad81a20 1083
da915ad5
PM
1084 sdp = container_of(work, struct srcu_data, work.work);
1085 sp = sdp->sp;
dad81a20 1086 rcu_cblist_init(&ready_cbs);
da915ad5
PM
1087 spin_lock_irq(&sdp->lock);
1088 smp_mb(); /* Old grace periods before callback invocation! */
1089 rcu_segcblist_advance(&sdp->srcu_cblist,
1090 rcu_seq_current(&sp->srcu_gp_seq));
1091 if (sdp->srcu_cblist_invoking ||
1092 !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
1093 spin_unlock_irq(&sdp->lock);
1094 return; /* Someone else on the job or nothing to do. */
1095 }
1096
1097 /* We are on the job! Extract and invoke ready callbacks. */
1098 sdp->srcu_cblist_invoking = true;
1099 rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
1100 spin_unlock_irq(&sdp->lock);
dad81a20
PM
1101 rhp = rcu_cblist_dequeue(&ready_cbs);
1102 for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
1103 local_bh_disable();
1104 rhp->func(rhp);
1105 local_bh_enable();
1106 }
da915ad5
PM
1107
1108 /*
1109 * Update counts, accelerate new callbacks, and if needed,
1110 * schedule another round of callback invocation.
1111 */
1112 spin_lock_irq(&sdp->lock);
1113 rcu_segcblist_insert_count(&sdp->srcu_cblist, &ready_cbs);
1114 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
1115 rcu_seq_snap(&sp->srcu_gp_seq));
1116 sdp->srcu_cblist_invoking = false;
1117 more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
1118 spin_unlock_irq(&sdp->lock);
1119 if (more)
1120 srcu_schedule_cbs_sdp(sdp, 0);
dad81a20
PM
1121}
1122
1123/*
1124 * Finished one round of SRCU grace period. Start another if there are
1125 * more SRCU callbacks queued, otherwise put SRCU into not-running state.
1126 */
1127static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay)
1128{
da915ad5 1129 bool pushgp = true;
dad81a20 1130
da915ad5
PM
1131 spin_lock_irq(&sp->gp_lock);
1132 if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
1133 if (!WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq))) {
1134 /* All requests fulfilled, time to go idle. */
1135 pushgp = false;
1136 }
1137 } else if (!rcu_seq_state(sp->srcu_gp_seq)) {
1138 /* Outstanding request and no GP. Start one. */
1139 srcu_gp_start(sp);
dad81a20 1140 }
da915ad5 1141 spin_unlock_irq(&sp->gp_lock);
dad81a20 1142
da915ad5 1143 if (pushgp)
dad81a20
PM
1144 queue_delayed_work(system_power_efficient_wq, &sp->work, delay);
1145}
1146
1147/*
1148 * This is the work-queue function that handles SRCU grace periods.
1149 */
1150void process_srcu(struct work_struct *work)
1151{
1152 struct srcu_struct *sp;
1153
1154 sp = container_of(work, struct srcu_struct, work.work);
1155
da915ad5 1156 srcu_advance_state(sp);
1e9a038b 1157 srcu_reschedule(sp, srcu_get_delay(sp));
dad81a20
PM
1158}
1159EXPORT_SYMBOL_GPL(process_srcu);
7f6733c3
PM
1160
1161void srcutorture_get_gp_data(enum rcutorture_type test_type,
1e9a038b
PM
1162 struct srcu_struct *sp, int *flags,
1163 unsigned long *gpnum, unsigned long *completed)
7f6733c3
PM
1164{
1165 if (test_type != SRCU_FLAVOR)
1166 return;
1167 *flags = 0;
1168 *completed = rcu_seq_ctr(sp->srcu_gp_seq);
1169 *gpnum = rcu_seq_ctr(sp->srcu_gp_seq_needed);
1170}
1171EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
1f4f6da1
PM
1172
1173static int __init srcu_bootup_announce(void)
1174{
1175 pr_info("Hierarchical SRCU implementation.\n");
0c8e0e3c
PM
1176 if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF)
1177 pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff);
1f4f6da1
PM
1178 return 0;
1179}
1180early_initcall(srcu_bootup_announce);