dt-bindings: phy-rockchip-typec: deprecate some register properties.
[linux/fpc-iii.git] / kernel / rcu / srcutree.c
blobd5cea81378cc6dc895685d9f86311d020173bd0f
1 /*
2 * Sleepable Read-Copy Update mechanism for mutual exclusion.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
18 * Copyright (C) IBM Corporation, 2006
19 * Copyright (C) Fujitsu, 2012
21 * Author: Paul McKenney <paulmck@us.ibm.com>
22 * Lai Jiangshan <laijs@cn.fujitsu.com>
24 * For detailed explanation of Read-Copy Update mechanism see -
25 * Documentation/RCU/ *.txt
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>
37 #include <linux/module.h>
38 #include <linux/srcu.h>
40 #include "rcu.h"
41 #include "rcu_segcblist.h"
43 /* Holdoff in nanoseconds for auto-expediting. */
44 #define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000)
45 static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF;
46 module_param(exp_holdoff, ulong, 0444);
48 /* Overflow-check frequency. N bits roughly says every 2**N grace periods. */
49 static ulong counter_wrap_check = (ULONG_MAX >> 2);
50 module_param(counter_wrap_check, ulong, 0444);
52 static void srcu_invoke_callbacks(struct work_struct *work);
53 static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay);
54 static void process_srcu(struct work_struct *work);
56 /* Wrappers for lock acquisition and release, see raw_spin_lock_rcu_node(). */
57 #define spin_lock_rcu_node(p) \
58 do { \
59 spin_lock(&ACCESS_PRIVATE(p, lock)); \
60 smp_mb__after_unlock_lock(); \
61 } while (0)
63 #define spin_unlock_rcu_node(p) spin_unlock(&ACCESS_PRIVATE(p, lock))
65 #define spin_lock_irq_rcu_node(p) \
66 do { \
67 spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \
68 smp_mb__after_unlock_lock(); \
69 } while (0)
71 #define spin_unlock_irq_rcu_node(p) \
72 spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
74 #define spin_lock_irqsave_rcu_node(p, flags) \
75 do { \
76 spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
77 smp_mb__after_unlock_lock(); \
78 } while (0)
80 #define spin_unlock_irqrestore_rcu_node(p, flags) \
81 spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags) \
84 * Initialize SRCU combining tree. Note that statically allocated
85 * srcu_struct structures might already have srcu_read_lock() and
86 * srcu_read_unlock() running against them. So if the is_static parameter
87 * is set, don't initialize ->srcu_lock_count[] and ->srcu_unlock_count[].
89 static void init_srcu_struct_nodes(struct srcu_struct *sp, bool is_static)
91 int cpu;
92 int i;
93 int level = 0;
94 int levelspread[RCU_NUM_LVLS];
95 struct srcu_data *sdp;
96 struct srcu_node *snp;
97 struct srcu_node *snp_first;
99 /* Work out the overall tree geometry. */
100 sp->level[0] = &sp->node[0];
101 for (i = 1; i < rcu_num_lvls; i++)
102 sp->level[i] = sp->level[i - 1] + num_rcu_lvl[i - 1];
103 rcu_init_levelspread(levelspread, num_rcu_lvl);
105 /* Each pass through this loop initializes one srcu_node structure. */
106 rcu_for_each_node_breadth_first(sp, snp) {
107 spin_lock_init(&ACCESS_PRIVATE(snp, lock));
108 WARN_ON_ONCE(ARRAY_SIZE(snp->srcu_have_cbs) !=
109 ARRAY_SIZE(snp->srcu_data_have_cbs));
110 for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {
111 snp->srcu_have_cbs[i] = 0;
112 snp->srcu_data_have_cbs[i] = 0;
114 snp->srcu_gp_seq_needed_exp = 0;
115 snp->grplo = -1;
116 snp->grphi = -1;
117 if (snp == &sp->node[0]) {
118 /* Root node, special case. */
119 snp->srcu_parent = NULL;
120 continue;
123 /* Non-root node. */
124 if (snp == sp->level[level + 1])
125 level++;
126 snp->srcu_parent = sp->level[level - 1] +
127 (snp - sp->level[level]) /
128 levelspread[level - 1];
132 * Initialize the per-CPU srcu_data array, which feeds into the
133 * leaves of the srcu_node tree.
135 WARN_ON_ONCE(ARRAY_SIZE(sdp->srcu_lock_count) !=
136 ARRAY_SIZE(sdp->srcu_unlock_count));
137 level = rcu_num_lvls - 1;
138 snp_first = sp->level[level];
139 for_each_possible_cpu(cpu) {
140 sdp = per_cpu_ptr(sp->sda, cpu);
141 spin_lock_init(&ACCESS_PRIVATE(sdp, lock));
142 rcu_segcblist_init(&sdp->srcu_cblist);
143 sdp->srcu_cblist_invoking = false;
144 sdp->srcu_gp_seq_needed = sp->srcu_gp_seq;
145 sdp->srcu_gp_seq_needed_exp = sp->srcu_gp_seq;
146 sdp->mynode = &snp_first[cpu / levelspread[level]];
147 for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {
148 if (snp->grplo < 0)
149 snp->grplo = cpu;
150 snp->grphi = cpu;
152 sdp->cpu = cpu;
153 INIT_DELAYED_WORK(&sdp->work, srcu_invoke_callbacks);
154 sdp->sp = sp;
155 sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
156 if (is_static)
157 continue;
159 /* Dynamically allocated, better be no srcu_read_locks()! */
160 for (i = 0; i < ARRAY_SIZE(sdp->srcu_lock_count); i++) {
161 sdp->srcu_lock_count[i] = 0;
162 sdp->srcu_unlock_count[i] = 0;
168 * Initialize non-compile-time initialized fields, including the
169 * associated srcu_node and srcu_data structures. The is_static
170 * parameter is passed through to init_srcu_struct_nodes(), and
171 * also tells us that ->sda has already been wired up to srcu_data.
173 static int init_srcu_struct_fields(struct srcu_struct *sp, bool is_static)
175 mutex_init(&sp->srcu_cb_mutex);
176 mutex_init(&sp->srcu_gp_mutex);
177 sp->srcu_idx = 0;
178 sp->srcu_gp_seq = 0;
179 sp->srcu_barrier_seq = 0;
180 mutex_init(&sp->srcu_barrier_mutex);
181 atomic_set(&sp->srcu_barrier_cpu_cnt, 0);
182 INIT_DELAYED_WORK(&sp->work, process_srcu);
183 if (!is_static)
184 sp->sda = alloc_percpu(struct srcu_data);
185 init_srcu_struct_nodes(sp, is_static);
186 sp->srcu_gp_seq_needed_exp = 0;
187 sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
188 smp_store_release(&sp->srcu_gp_seq_needed, 0); /* Init done. */
189 return sp->sda ? 0 : -ENOMEM;
192 #ifdef CONFIG_DEBUG_LOCK_ALLOC
194 int __init_srcu_struct(struct srcu_struct *sp, const char *name,
195 struct lock_class_key *key)
197 /* Don't re-initialize a lock while it is held. */
198 debug_check_no_locks_freed((void *)sp, sizeof(*sp));
199 lockdep_init_map(&sp->dep_map, name, key, 0);
200 spin_lock_init(&ACCESS_PRIVATE(sp, lock));
201 return init_srcu_struct_fields(sp, false);
203 EXPORT_SYMBOL_GPL(__init_srcu_struct);
205 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
208 * init_srcu_struct - initialize a sleep-RCU structure
209 * @sp: structure to initialize.
211 * Must invoke this on a given srcu_struct before passing that srcu_struct
212 * to any other function. Each srcu_struct represents a separate domain
213 * of SRCU protection.
215 int init_srcu_struct(struct srcu_struct *sp)
217 spin_lock_init(&ACCESS_PRIVATE(sp, lock));
218 return init_srcu_struct_fields(sp, false);
220 EXPORT_SYMBOL_GPL(init_srcu_struct);
222 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
225 * First-use initialization of statically allocated srcu_struct
226 * structure. Wiring up the combining tree is more than can be
227 * done with compile-time initialization, so this check is added
228 * to each update-side SRCU primitive. Use sp->lock, which -is-
229 * compile-time initialized, to resolve races involving multiple
230 * CPUs trying to garner first-use privileges.
232 static void check_init_srcu_struct(struct srcu_struct *sp)
234 unsigned long flags;
236 WARN_ON_ONCE(rcu_scheduler_active == RCU_SCHEDULER_INIT);
237 /* The smp_load_acquire() pairs with the smp_store_release(). */
238 if (!rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq_needed))) /*^^^*/
239 return; /* Already initialized. */
240 spin_lock_irqsave_rcu_node(sp, flags);
241 if (!rcu_seq_state(sp->srcu_gp_seq_needed)) {
242 spin_unlock_irqrestore_rcu_node(sp, flags);
243 return;
245 init_srcu_struct_fields(sp, true);
246 spin_unlock_irqrestore_rcu_node(sp, flags);
250 * Returns approximate total of the readers' ->srcu_lock_count[] values
251 * for the rank of per-CPU counters specified by idx.
253 static unsigned long srcu_readers_lock_idx(struct srcu_struct *sp, int idx)
255 int cpu;
256 unsigned long sum = 0;
258 for_each_possible_cpu(cpu) {
259 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
261 sum += READ_ONCE(cpuc->srcu_lock_count[idx]);
263 return sum;
267 * Returns approximate total of the readers' ->srcu_unlock_count[] values
268 * for the rank of per-CPU counters specified by idx.
270 static unsigned long srcu_readers_unlock_idx(struct srcu_struct *sp, int idx)
272 int cpu;
273 unsigned long sum = 0;
275 for_each_possible_cpu(cpu) {
276 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
278 sum += READ_ONCE(cpuc->srcu_unlock_count[idx]);
280 return sum;
284 * Return true if the number of pre-existing readers is determined to
285 * be zero.
287 static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
289 unsigned long unlocks;
291 unlocks = srcu_readers_unlock_idx(sp, idx);
294 * Make sure that a lock is always counted if the corresponding
295 * unlock is counted. Needs to be a smp_mb() as the read side may
296 * contain a read from a variable that is written to before the
297 * synchronize_srcu() in the write side. In this case smp_mb()s
298 * A and B act like the store buffering pattern.
300 * This smp_mb() also pairs with smp_mb() C to prevent accesses
301 * after the synchronize_srcu() from being executed before the
302 * grace period ends.
304 smp_mb(); /* A */
307 * If the locks are the same as the unlocks, then there must have
308 * been no readers on this index at some time in between. This does
309 * not mean that there are no more readers, as one could have read
310 * the current index but not have incremented the lock counter yet.
312 * So suppose that the updater is preempted here for so long
313 * that more than ULONG_MAX non-nested readers come and go in
314 * the meantime. It turns out that this cannot result in overflow
315 * because if a reader modifies its unlock count after we read it
316 * above, then that reader's next load of ->srcu_idx is guaranteed
317 * to get the new value, which will cause it to operate on the
318 * other bank of counters, where it cannot contribute to the
319 * overflow of these counters. This means that there is a maximum
320 * of 2*NR_CPUS increments, which cannot overflow given current
321 * systems, especially not on 64-bit systems.
323 * OK, how about nesting? This does impose a limit on nesting
324 * of floor(ULONG_MAX/NR_CPUS/2), which should be sufficient,
325 * especially on 64-bit systems.
327 return srcu_readers_lock_idx(sp, idx) == unlocks;
331 * srcu_readers_active - returns true if there are readers. and false
332 * otherwise
333 * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
335 * Note that this is not an atomic primitive, and can therefore suffer
336 * severe errors when invoked on an active srcu_struct. That said, it
337 * can be useful as an error check at cleanup time.
339 static bool srcu_readers_active(struct srcu_struct *sp)
341 int cpu;
342 unsigned long sum = 0;
344 for_each_possible_cpu(cpu) {
345 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
347 sum += READ_ONCE(cpuc->srcu_lock_count[0]);
348 sum += READ_ONCE(cpuc->srcu_lock_count[1]);
349 sum -= READ_ONCE(cpuc->srcu_unlock_count[0]);
350 sum -= READ_ONCE(cpuc->srcu_unlock_count[1]);
352 return sum;
355 #define SRCU_INTERVAL 1
358 * Return grace-period delay, zero if there are expedited grace
359 * periods pending, SRCU_INTERVAL otherwise.
361 static unsigned long srcu_get_delay(struct srcu_struct *sp)
363 if (ULONG_CMP_LT(READ_ONCE(sp->srcu_gp_seq),
364 READ_ONCE(sp->srcu_gp_seq_needed_exp)))
365 return 0;
366 return SRCU_INTERVAL;
370 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
371 * @sp: structure to clean up.
373 * Must invoke this after you are finished using a given srcu_struct that
374 * was initialized via init_srcu_struct(), else you leak memory.
376 void cleanup_srcu_struct(struct srcu_struct *sp)
378 int cpu;
380 if (WARN_ON(!srcu_get_delay(sp)))
381 return; /* Leakage unless caller handles error. */
382 if (WARN_ON(srcu_readers_active(sp)))
383 return; /* Leakage unless caller handles error. */
384 flush_delayed_work(&sp->work);
385 for_each_possible_cpu(cpu)
386 flush_delayed_work(&per_cpu_ptr(sp->sda, cpu)->work);
387 if (WARN_ON(rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
388 WARN_ON(srcu_readers_active(sp))) {
389 pr_info("cleanup_srcu_struct: Active srcu_struct %p state: %d\n", sp, rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)));
390 return; /* Caller forgot to stop doing call_srcu()? */
392 free_percpu(sp->sda);
393 sp->sda = NULL;
395 EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
398 * Counts the new reader in the appropriate per-CPU element of the
399 * srcu_struct.
400 * Returns an index that must be passed to the matching srcu_read_unlock().
402 int __srcu_read_lock(struct srcu_struct *sp)
404 int idx;
406 idx = READ_ONCE(sp->srcu_idx) & 0x1;
407 this_cpu_inc(sp->sda->srcu_lock_count[idx]);
408 smp_mb(); /* B */ /* Avoid leaking the critical section. */
409 return idx;
411 EXPORT_SYMBOL_GPL(__srcu_read_lock);
414 * Removes the count for the old reader from the appropriate per-CPU
415 * element of the srcu_struct. Note that this may well be a different
416 * CPU than that which was incremented by the corresponding srcu_read_lock().
418 void __srcu_read_unlock(struct srcu_struct *sp, int idx)
420 smp_mb(); /* C */ /* Avoid leaking the critical section. */
421 this_cpu_inc(sp->sda->srcu_unlock_count[idx]);
423 EXPORT_SYMBOL_GPL(__srcu_read_unlock);
426 * We use an adaptive strategy for synchronize_srcu() and especially for
427 * synchronize_srcu_expedited(). We spin for a fixed time period
428 * (defined below) to allow SRCU readers to exit their read-side critical
429 * sections. If there are still some readers after a few microseconds,
430 * we repeatedly block for 1-millisecond time periods.
432 #define SRCU_RETRY_CHECK_DELAY 5
435 * Start an SRCU grace period.
437 static void srcu_gp_start(struct srcu_struct *sp)
439 struct srcu_data *sdp = this_cpu_ptr(sp->sda);
440 int state;
442 lockdep_assert_held(&sp->lock);
443 WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
444 rcu_segcblist_advance(&sdp->srcu_cblist,
445 rcu_seq_current(&sp->srcu_gp_seq));
446 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
447 rcu_seq_snap(&sp->srcu_gp_seq));
448 smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
449 rcu_seq_start(&sp->srcu_gp_seq);
450 state = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
451 WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
455 * Track online CPUs to guide callback workqueue placement.
457 DEFINE_PER_CPU(bool, srcu_online);
459 void srcu_online_cpu(unsigned int cpu)
461 WRITE_ONCE(per_cpu(srcu_online, cpu), true);
464 void srcu_offline_cpu(unsigned int cpu)
466 WRITE_ONCE(per_cpu(srcu_online, cpu), false);
470 * Place the workqueue handler on the specified CPU if online, otherwise
471 * just run it whereever. This is useful for placing workqueue handlers
472 * that are to invoke the specified CPU's callbacks.
474 static bool srcu_queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
475 struct delayed_work *dwork,
476 unsigned long delay)
478 bool ret;
480 preempt_disable();
481 if (READ_ONCE(per_cpu(srcu_online, cpu)))
482 ret = queue_delayed_work_on(cpu, wq, dwork, delay);
483 else
484 ret = queue_delayed_work(wq, dwork, delay);
485 preempt_enable();
486 return ret;
490 * Schedule callback invocation for the specified srcu_data structure,
491 * if possible, on the corresponding CPU.
493 static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
495 srcu_queue_delayed_work_on(sdp->cpu, system_power_efficient_wq,
496 &sdp->work, delay);
500 * Schedule callback invocation for all srcu_data structures associated
501 * with the specified srcu_node structure that have callbacks for the
502 * just-completed grace period, the one corresponding to idx. If possible,
503 * schedule this invocation on the corresponding CPUs.
505 static void srcu_schedule_cbs_snp(struct srcu_struct *sp, struct srcu_node *snp,
506 unsigned long mask, unsigned long delay)
508 int cpu;
510 for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
511 if (!(mask & (1 << (cpu - snp->grplo))))
512 continue;
513 srcu_schedule_cbs_sdp(per_cpu_ptr(sp->sda, cpu), delay);
518 * Note the end of an SRCU grace period. Initiates callback invocation
519 * and starts a new grace period if needed.
521 * The ->srcu_cb_mutex acquisition does not protect any data, but
522 * instead prevents more than one grace period from starting while we
523 * are initiating callback invocation. This allows the ->srcu_have_cbs[]
524 * array to have a finite number of elements.
526 static void srcu_gp_end(struct srcu_struct *sp)
528 unsigned long cbdelay;
529 bool cbs;
530 int cpu;
531 unsigned long flags;
532 unsigned long gpseq;
533 int idx;
534 int idxnext;
535 unsigned long mask;
536 struct srcu_data *sdp;
537 struct srcu_node *snp;
539 /* Prevent more than one additional grace period. */
540 mutex_lock(&sp->srcu_cb_mutex);
542 /* End the current grace period. */
543 spin_lock_irq_rcu_node(sp);
544 idx = rcu_seq_state(sp->srcu_gp_seq);
545 WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
546 cbdelay = srcu_get_delay(sp);
547 sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
548 rcu_seq_end(&sp->srcu_gp_seq);
549 gpseq = rcu_seq_current(&sp->srcu_gp_seq);
550 if (ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, gpseq))
551 sp->srcu_gp_seq_needed_exp = gpseq;
552 spin_unlock_irq_rcu_node(sp);
553 mutex_unlock(&sp->srcu_gp_mutex);
554 /* A new grace period can start at this point. But only one. */
556 /* Initiate callback invocation as needed. */
557 idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
558 idxnext = (idx + 1) % ARRAY_SIZE(snp->srcu_have_cbs);
559 rcu_for_each_node_breadth_first(sp, snp) {
560 spin_lock_irq_rcu_node(snp);
561 cbs = false;
562 if (snp >= sp->level[rcu_num_lvls - 1])
563 cbs = snp->srcu_have_cbs[idx] == gpseq;
564 snp->srcu_have_cbs[idx] = gpseq;
565 rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
566 if (ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, gpseq))
567 snp->srcu_gp_seq_needed_exp = gpseq;
568 mask = snp->srcu_data_have_cbs[idx];
569 snp->srcu_data_have_cbs[idx] = 0;
570 spin_unlock_irq_rcu_node(snp);
571 if (cbs)
572 srcu_schedule_cbs_snp(sp, snp, mask, cbdelay);
574 /* Occasionally prevent srcu_data counter wrap. */
575 if (!(gpseq & counter_wrap_check))
576 for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
577 sdp = per_cpu_ptr(sp->sda, cpu);
578 spin_lock_irqsave_rcu_node(sdp, flags);
579 if (ULONG_CMP_GE(gpseq,
580 sdp->srcu_gp_seq_needed + 100))
581 sdp->srcu_gp_seq_needed = gpseq;
582 spin_unlock_irqrestore_rcu_node(sdp, flags);
586 /* Callback initiation done, allow grace periods after next. */
587 mutex_unlock(&sp->srcu_cb_mutex);
589 /* Start a new grace period if needed. */
590 spin_lock_irq_rcu_node(sp);
591 gpseq = rcu_seq_current(&sp->srcu_gp_seq);
592 if (!rcu_seq_state(gpseq) &&
593 ULONG_CMP_LT(gpseq, sp->srcu_gp_seq_needed)) {
594 srcu_gp_start(sp);
595 spin_unlock_irq_rcu_node(sp);
596 /* Throttle expedited grace periods: Should be rare! */
597 srcu_reschedule(sp, rcu_seq_ctr(gpseq) & 0x3ff
598 ? 0 : SRCU_INTERVAL);
599 } else {
600 spin_unlock_irq_rcu_node(sp);
605 * Funnel-locking scheme to scalably mediate many concurrent expedited
606 * grace-period requests. This function is invoked for the first known
607 * expedited request for a grace period that has already been requested,
608 * but without expediting. To start a completely new grace period,
609 * whether expedited or not, use srcu_funnel_gp_start() instead.
611 static void srcu_funnel_exp_start(struct srcu_struct *sp, struct srcu_node *snp,
612 unsigned long s)
614 unsigned long flags;
616 for (; snp != NULL; snp = snp->srcu_parent) {
617 if (rcu_seq_done(&sp->srcu_gp_seq, s) ||
618 ULONG_CMP_GE(READ_ONCE(snp->srcu_gp_seq_needed_exp), s))
619 return;
620 spin_lock_irqsave_rcu_node(snp, flags);
621 if (ULONG_CMP_GE(snp->srcu_gp_seq_needed_exp, s)) {
622 spin_unlock_irqrestore_rcu_node(snp, flags);
623 return;
625 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
626 spin_unlock_irqrestore_rcu_node(snp, flags);
628 spin_lock_irqsave_rcu_node(sp, flags);
629 if (!ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
630 sp->srcu_gp_seq_needed_exp = s;
631 spin_unlock_irqrestore_rcu_node(sp, flags);
635 * Funnel-locking scheme to scalably mediate many concurrent grace-period
636 * requests. The winner has to do the work of actually starting grace
637 * period s. Losers must either ensure that their desired grace-period
638 * number is recorded on at least their leaf srcu_node structure, or they
639 * must take steps to invoke their own callbacks.
641 static void srcu_funnel_gp_start(struct srcu_struct *sp, struct srcu_data *sdp,
642 unsigned long s, bool do_norm)
644 unsigned long flags;
645 int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
646 struct srcu_node *snp = sdp->mynode;
647 unsigned long snp_seq;
649 /* Each pass through the loop does one level of the srcu_node tree. */
650 for (; snp != NULL; snp = snp->srcu_parent) {
651 if (rcu_seq_done(&sp->srcu_gp_seq, s) && snp != sdp->mynode)
652 return; /* GP already done and CBs recorded. */
653 spin_lock_irqsave_rcu_node(snp, flags);
654 if (ULONG_CMP_GE(snp->srcu_have_cbs[idx], s)) {
655 snp_seq = snp->srcu_have_cbs[idx];
656 if (snp == sdp->mynode && snp_seq == s)
657 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
658 spin_unlock_irqrestore_rcu_node(snp, flags);
659 if (snp == sdp->mynode && snp_seq != s) {
660 srcu_schedule_cbs_sdp(sdp, do_norm
661 ? SRCU_INTERVAL
662 : 0);
663 return;
665 if (!do_norm)
666 srcu_funnel_exp_start(sp, snp, s);
667 return;
669 snp->srcu_have_cbs[idx] = s;
670 if (snp == sdp->mynode)
671 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
672 if (!do_norm && ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, s))
673 snp->srcu_gp_seq_needed_exp = s;
674 spin_unlock_irqrestore_rcu_node(snp, flags);
677 /* Top of tree, must ensure the grace period will be started. */
678 spin_lock_irqsave_rcu_node(sp, flags);
679 if (ULONG_CMP_LT(sp->srcu_gp_seq_needed, s)) {
681 * Record need for grace period s. Pair with load
682 * acquire setting up for initialization.
684 smp_store_release(&sp->srcu_gp_seq_needed, s); /*^^^*/
686 if (!do_norm && ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
687 sp->srcu_gp_seq_needed_exp = s;
689 /* If grace period not already done and none in progress, start it. */
690 if (!rcu_seq_done(&sp->srcu_gp_seq, s) &&
691 rcu_seq_state(sp->srcu_gp_seq) == SRCU_STATE_IDLE) {
692 WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
693 srcu_gp_start(sp);
694 queue_delayed_work(system_power_efficient_wq, &sp->work,
695 srcu_get_delay(sp));
697 spin_unlock_irqrestore_rcu_node(sp, flags);
701 * Wait until all readers counted by array index idx complete, but
702 * loop an additional time if there is an expedited grace period pending.
703 * The caller must ensure that ->srcu_idx is not changed while checking.
705 static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
707 for (;;) {
708 if (srcu_readers_active_idx_check(sp, idx))
709 return true;
710 if (--trycount + !srcu_get_delay(sp) <= 0)
711 return false;
712 udelay(SRCU_RETRY_CHECK_DELAY);
717 * Increment the ->srcu_idx counter so that future SRCU readers will
718 * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows
719 * us to wait for pre-existing readers in a starvation-free manner.
721 static void srcu_flip(struct srcu_struct *sp)
724 * Ensure that if this updater saw a given reader's increment
725 * from __srcu_read_lock(), that reader was using an old value
726 * of ->srcu_idx. Also ensure that if a given reader sees the
727 * new value of ->srcu_idx, this updater's earlier scans cannot
728 * have seen that reader's increments (which is OK, because this
729 * grace period need not wait on that reader).
731 smp_mb(); /* E */ /* Pairs with B and C. */
733 WRITE_ONCE(sp->srcu_idx, sp->srcu_idx + 1);
736 * Ensure that if the updater misses an __srcu_read_unlock()
737 * increment, that task's next __srcu_read_lock() will see the
738 * above counter update. Note that both this memory barrier
739 * and the one in srcu_readers_active_idx_check() provide the
740 * guarantee for __srcu_read_lock().
742 smp_mb(); /* D */ /* Pairs with C. */
746 * If SRCU is likely idle, return true, otherwise return false.
748 * Note that it is OK for several current from-idle requests for a new
749 * grace period from idle to specify expediting because they will all end
750 * up requesting the same grace period anyhow. So no loss.
752 * Note also that if any CPU (including the current one) is still invoking
753 * callbacks, this function will nevertheless say "idle". This is not
754 * ideal, but the overhead of checking all CPUs' callback lists is even
755 * less ideal, especially on large systems. Furthermore, the wakeup
756 * can happen before the callback is fully removed, so we have no choice
757 * but to accept this type of error.
759 * This function is also subject to counter-wrap errors, but let's face
760 * it, if this function was preempted for enough time for the counters
761 * to wrap, it really doesn't matter whether or not we expedite the grace
762 * period. The extra overhead of a needlessly expedited grace period is
763 * negligible when amoritized over that time period, and the extra latency
764 * of a needlessly non-expedited grace period is similarly negligible.
766 static bool srcu_might_be_idle(struct srcu_struct *sp)
768 unsigned long curseq;
769 unsigned long flags;
770 struct srcu_data *sdp;
771 unsigned long t;
773 /* If the local srcu_data structure has callbacks, not idle. */
774 local_irq_save(flags);
775 sdp = this_cpu_ptr(sp->sda);
776 if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
777 local_irq_restore(flags);
778 return false; /* Callbacks already present, so not idle. */
780 local_irq_restore(flags);
783 * No local callbacks, so probabalistically probe global state.
784 * Exact information would require acquiring locks, which would
785 * kill scalability, hence the probabalistic nature of the probe.
788 /* First, see if enough time has passed since the last GP. */
789 t = ktime_get_mono_fast_ns();
790 if (exp_holdoff == 0 ||
791 time_in_range_open(t, sp->srcu_last_gp_end,
792 sp->srcu_last_gp_end + exp_holdoff))
793 return false; /* Too soon after last GP. */
795 /* Next, check for probable idleness. */
796 curseq = rcu_seq_current(&sp->srcu_gp_seq);
797 smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
798 if (ULONG_CMP_LT(curseq, READ_ONCE(sp->srcu_gp_seq_needed)))
799 return false; /* Grace period in progress, so not idle. */
800 smp_mb(); /* Order ->srcu_gp_seq with prior access. */
801 if (curseq != rcu_seq_current(&sp->srcu_gp_seq))
802 return false; /* GP # changed, so not idle. */
803 return true; /* With reasonable probability, idle! */
807 * SRCU callback function to leak a callback.
809 static void srcu_leak_callback(struct rcu_head *rhp)
814 * Enqueue an SRCU callback on the srcu_data structure associated with
815 * the current CPU and the specified srcu_struct structure, initiating
816 * grace-period processing if it is not already running.
818 * Note that all CPUs must agree that the grace period extended beyond
819 * all pre-existing SRCU read-side critical section. On systems with
820 * more than one CPU, this means that when "func()" is invoked, each CPU
821 * is guaranteed to have executed a full memory barrier since the end of
822 * its last corresponding SRCU read-side critical section whose beginning
823 * preceded the call to call_rcu(). It also means that each CPU executing
824 * an SRCU read-side critical section that continues beyond the start of
825 * "func()" must have executed a memory barrier after the call_rcu()
826 * but before the beginning of that SRCU read-side critical section.
827 * Note that these guarantees include CPUs that are offline, idle, or
828 * executing in user mode, as well as CPUs that are executing in the kernel.
830 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
831 * resulting SRCU callback function "func()", then both CPU A and CPU
832 * B are guaranteed to execute a full memory barrier during the time
833 * interval between the call to call_rcu() and the invocation of "func()".
834 * This guarantee applies even if CPU A and CPU B are the same CPU (but
835 * again only if the system has more than one CPU).
837 * Of course, these guarantees apply only for invocations of call_srcu(),
838 * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
839 * srcu_struct structure.
841 void __call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
842 rcu_callback_t func, bool do_norm)
844 unsigned long flags;
845 bool needexp = false;
846 bool needgp = false;
847 unsigned long s;
848 struct srcu_data *sdp;
850 check_init_srcu_struct(sp);
851 if (debug_rcu_head_queue(rhp)) {
852 /* Probable double call_srcu(), so leak the callback. */
853 WRITE_ONCE(rhp->func, srcu_leak_callback);
854 WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n");
855 return;
857 rhp->func = func;
858 local_irq_save(flags);
859 sdp = this_cpu_ptr(sp->sda);
860 spin_lock_rcu_node(sdp);
861 rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp, false);
862 rcu_segcblist_advance(&sdp->srcu_cblist,
863 rcu_seq_current(&sp->srcu_gp_seq));
864 s = rcu_seq_snap(&sp->srcu_gp_seq);
865 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s);
866 if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
867 sdp->srcu_gp_seq_needed = s;
868 needgp = true;
870 if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
871 sdp->srcu_gp_seq_needed_exp = s;
872 needexp = true;
874 spin_unlock_irqrestore_rcu_node(sdp, flags);
875 if (needgp)
876 srcu_funnel_gp_start(sp, sdp, s, do_norm);
877 else if (needexp)
878 srcu_funnel_exp_start(sp, sdp->mynode, s);
882 * call_srcu() - Queue a callback for invocation after an SRCU grace period
883 * @sp: srcu_struct in queue the callback
884 * @rhp: structure to be used for queueing the SRCU callback.
885 * @func: function to be invoked after the SRCU grace period
887 * The callback function will be invoked some time after a full SRCU
888 * grace period elapses, in other words after all pre-existing SRCU
889 * read-side critical sections have completed. However, the callback
890 * function might well execute concurrently with other SRCU read-side
891 * critical sections that started after call_srcu() was invoked. SRCU
892 * read-side critical sections are delimited by srcu_read_lock() and
893 * srcu_read_unlock(), and may be nested.
895 * The callback will be invoked from process context, but must nevertheless
896 * be fast and must not block.
898 void call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
899 rcu_callback_t func)
901 __call_srcu(sp, rhp, func, true);
903 EXPORT_SYMBOL_GPL(call_srcu);
906 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
908 static void __synchronize_srcu(struct srcu_struct *sp, bool do_norm)
910 struct rcu_synchronize rcu;
912 RCU_LOCKDEP_WARN(lock_is_held(&sp->dep_map) ||
913 lock_is_held(&rcu_bh_lock_map) ||
914 lock_is_held(&rcu_lock_map) ||
915 lock_is_held(&rcu_sched_lock_map),
916 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
918 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
919 return;
920 might_sleep();
921 check_init_srcu_struct(sp);
922 init_completion(&rcu.completion);
923 init_rcu_head_on_stack(&rcu.head);
924 __call_srcu(sp, &rcu.head, wakeme_after_rcu, do_norm);
925 wait_for_completion(&rcu.completion);
926 destroy_rcu_head_on_stack(&rcu.head);
929 * Make sure that later code is ordered after the SRCU grace
930 * period. This pairs with the spin_lock_irq_rcu_node()
931 * in srcu_invoke_callbacks(). Unlike Tree RCU, this is needed
932 * because the current CPU might have been totally uninvolved with
933 * (and thus unordered against) that grace period.
935 smp_mb();
939 * synchronize_srcu_expedited - Brute-force SRCU grace period
940 * @sp: srcu_struct with which to synchronize.
942 * Wait for an SRCU grace period to elapse, but be more aggressive about
943 * spinning rather than blocking when waiting.
945 * Note that synchronize_srcu_expedited() has the same deadlock and
946 * memory-ordering properties as does synchronize_srcu().
948 void synchronize_srcu_expedited(struct srcu_struct *sp)
950 __synchronize_srcu(sp, rcu_gp_is_normal());
952 EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
955 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
956 * @sp: srcu_struct with which to synchronize.
958 * Wait for the count to drain to zero of both indexes. To avoid the
959 * possible starvation of synchronize_srcu(), it waits for the count of
960 * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
961 * and then flip the srcu_idx and wait for the count of the other index.
963 * Can block; must be called from process context.
965 * Note that it is illegal to call synchronize_srcu() from the corresponding
966 * SRCU read-side critical section; doing so will result in deadlock.
967 * However, it is perfectly legal to call synchronize_srcu() on one
968 * srcu_struct from some other srcu_struct's read-side critical section,
969 * as long as the resulting graph of srcu_structs is acyclic.
971 * There are memory-ordering constraints implied by synchronize_srcu().
972 * On systems with more than one CPU, when synchronize_srcu() returns,
973 * each CPU is guaranteed to have executed a full memory barrier since
974 * the end of its last corresponding SRCU-sched read-side critical section
975 * whose beginning preceded the call to synchronize_srcu(). In addition,
976 * each CPU having an SRCU read-side critical section that extends beyond
977 * the return from synchronize_srcu() is guaranteed to have executed a
978 * full memory barrier after the beginning of synchronize_srcu() and before
979 * the beginning of that SRCU read-side critical section. Note that these
980 * guarantees include CPUs that are offline, idle, or executing in user mode,
981 * as well as CPUs that are executing in the kernel.
983 * Furthermore, if CPU A invoked synchronize_srcu(), which returned
984 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
985 * to have executed a full memory barrier during the execution of
986 * synchronize_srcu(). This guarantee applies even if CPU A and CPU B
987 * are the same CPU, but again only if the system has more than one CPU.
989 * Of course, these memory-ordering guarantees apply only when
990 * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
991 * passed the same srcu_struct structure.
993 * If SRCU is likely idle, expedite the first request. This semantic
994 * was provided by Classic SRCU, and is relied upon by its users, so TREE
995 * SRCU must also provide it. Note that detecting idleness is heuristic
996 * and subject to both false positives and negatives.
998 void synchronize_srcu(struct srcu_struct *sp)
1000 if (srcu_might_be_idle(sp) || rcu_gp_is_expedited())
1001 synchronize_srcu_expedited(sp);
1002 else
1003 __synchronize_srcu(sp, true);
1005 EXPORT_SYMBOL_GPL(synchronize_srcu);
1008 * Callback function for srcu_barrier() use.
1010 static void srcu_barrier_cb(struct rcu_head *rhp)
1012 struct srcu_data *sdp;
1013 struct srcu_struct *sp;
1015 sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
1016 sp = sdp->sp;
1017 if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
1018 complete(&sp->srcu_barrier_completion);
1022 * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
1023 * @sp: srcu_struct on which to wait for in-flight callbacks.
1025 void srcu_barrier(struct srcu_struct *sp)
1027 int cpu;
1028 struct srcu_data *sdp;
1029 unsigned long s = rcu_seq_snap(&sp->srcu_barrier_seq);
1031 check_init_srcu_struct(sp);
1032 mutex_lock(&sp->srcu_barrier_mutex);
1033 if (rcu_seq_done(&sp->srcu_barrier_seq, s)) {
1034 smp_mb(); /* Force ordering following return. */
1035 mutex_unlock(&sp->srcu_barrier_mutex);
1036 return; /* Someone else did our work for us. */
1038 rcu_seq_start(&sp->srcu_barrier_seq);
1039 init_completion(&sp->srcu_barrier_completion);
1041 /* Initial count prevents reaching zero until all CBs are posted. */
1042 atomic_set(&sp->srcu_barrier_cpu_cnt, 1);
1045 * Each pass through this loop enqueues a callback, but only
1046 * on CPUs already having callbacks enqueued. Note that if
1047 * a CPU already has callbacks enqueue, it must have already
1048 * registered the need for a future grace period, so all we
1049 * need do is enqueue a callback that will use the same
1050 * grace period as the last callback already in the queue.
1052 for_each_possible_cpu(cpu) {
1053 sdp = per_cpu_ptr(sp->sda, cpu);
1054 spin_lock_irq_rcu_node(sdp);
1055 atomic_inc(&sp->srcu_barrier_cpu_cnt);
1056 sdp->srcu_barrier_head.func = srcu_barrier_cb;
1057 debug_rcu_head_queue(&sdp->srcu_barrier_head);
1058 if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
1059 &sdp->srcu_barrier_head, 0)) {
1060 debug_rcu_head_unqueue(&sdp->srcu_barrier_head);
1061 atomic_dec(&sp->srcu_barrier_cpu_cnt);
1063 spin_unlock_irq_rcu_node(sdp);
1066 /* Remove the initial count, at which point reaching zero can happen. */
1067 if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
1068 complete(&sp->srcu_barrier_completion);
1069 wait_for_completion(&sp->srcu_barrier_completion);
1071 rcu_seq_end(&sp->srcu_barrier_seq);
1072 mutex_unlock(&sp->srcu_barrier_mutex);
1074 EXPORT_SYMBOL_GPL(srcu_barrier);
1077 * srcu_batches_completed - return batches completed.
1078 * @sp: srcu_struct on which to report batch completion.
1080 * Report the number of batches, correlated with, but not necessarily
1081 * precisely the same as, the number of grace periods that have elapsed.
1083 unsigned long srcu_batches_completed(struct srcu_struct *sp)
1085 return sp->srcu_idx;
1087 EXPORT_SYMBOL_GPL(srcu_batches_completed);
1090 * Core SRCU state machine. Push state bits of ->srcu_gp_seq
1091 * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
1092 * completed in that state.
1094 static void srcu_advance_state(struct srcu_struct *sp)
1096 int idx;
1098 mutex_lock(&sp->srcu_gp_mutex);
1101 * Because readers might be delayed for an extended period after
1102 * fetching ->srcu_idx for their index, at any point in time there
1103 * might well be readers using both idx=0 and idx=1. We therefore
1104 * need to wait for readers to clear from both index values before
1105 * invoking a callback.
1107 * The load-acquire ensures that we see the accesses performed
1108 * by the prior grace period.
1110 idx = rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq)); /* ^^^ */
1111 if (idx == SRCU_STATE_IDLE) {
1112 spin_lock_irq_rcu_node(sp);
1113 if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
1114 WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq));
1115 spin_unlock_irq_rcu_node(sp);
1116 mutex_unlock(&sp->srcu_gp_mutex);
1117 return;
1119 idx = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
1120 if (idx == SRCU_STATE_IDLE)
1121 srcu_gp_start(sp);
1122 spin_unlock_irq_rcu_node(sp);
1123 if (idx != SRCU_STATE_IDLE) {
1124 mutex_unlock(&sp->srcu_gp_mutex);
1125 return; /* Someone else started the grace period. */
1129 if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
1130 idx = 1 ^ (sp->srcu_idx & 1);
1131 if (!try_check_zero(sp, idx, 1)) {
1132 mutex_unlock(&sp->srcu_gp_mutex);
1133 return; /* readers present, retry later. */
1135 srcu_flip(sp);
1136 rcu_seq_set_state(&sp->srcu_gp_seq, SRCU_STATE_SCAN2);
1139 if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
1142 * SRCU read-side critical sections are normally short,
1143 * so check at least twice in quick succession after a flip.
1145 idx = 1 ^ (sp->srcu_idx & 1);
1146 if (!try_check_zero(sp, idx, 2)) {
1147 mutex_unlock(&sp->srcu_gp_mutex);
1148 return; /* readers present, retry later. */
1150 srcu_gp_end(sp); /* Releases ->srcu_gp_mutex. */
1155 * Invoke a limited number of SRCU callbacks that have passed through
1156 * their grace period. If there are more to do, SRCU will reschedule
1157 * the workqueue. Note that needed memory barriers have been executed
1158 * in this task's context by srcu_readers_active_idx_check().
1160 static void srcu_invoke_callbacks(struct work_struct *work)
1162 bool more;
1163 struct rcu_cblist ready_cbs;
1164 struct rcu_head *rhp;
1165 struct srcu_data *sdp;
1166 struct srcu_struct *sp;
1168 sdp = container_of(work, struct srcu_data, work.work);
1169 sp = sdp->sp;
1170 rcu_cblist_init(&ready_cbs);
1171 spin_lock_irq_rcu_node(sdp);
1172 rcu_segcblist_advance(&sdp->srcu_cblist,
1173 rcu_seq_current(&sp->srcu_gp_seq));
1174 if (sdp->srcu_cblist_invoking ||
1175 !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
1176 spin_unlock_irq_rcu_node(sdp);
1177 return; /* Someone else on the job or nothing to do. */
1180 /* We are on the job! Extract and invoke ready callbacks. */
1181 sdp->srcu_cblist_invoking = true;
1182 rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
1183 spin_unlock_irq_rcu_node(sdp);
1184 rhp = rcu_cblist_dequeue(&ready_cbs);
1185 for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
1186 debug_rcu_head_unqueue(rhp);
1187 local_bh_disable();
1188 rhp->func(rhp);
1189 local_bh_enable();
1193 * Update counts, accelerate new callbacks, and if needed,
1194 * schedule another round of callback invocation.
1196 spin_lock_irq_rcu_node(sdp);
1197 rcu_segcblist_insert_count(&sdp->srcu_cblist, &ready_cbs);
1198 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
1199 rcu_seq_snap(&sp->srcu_gp_seq));
1200 sdp->srcu_cblist_invoking = false;
1201 more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
1202 spin_unlock_irq_rcu_node(sdp);
1203 if (more)
1204 srcu_schedule_cbs_sdp(sdp, 0);
1208 * Finished one round of SRCU grace period. Start another if there are
1209 * more SRCU callbacks queued, otherwise put SRCU into not-running state.
1211 static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay)
1213 bool pushgp = true;
1215 spin_lock_irq_rcu_node(sp);
1216 if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
1217 if (!WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq))) {
1218 /* All requests fulfilled, time to go idle. */
1219 pushgp = false;
1221 } else if (!rcu_seq_state(sp->srcu_gp_seq)) {
1222 /* Outstanding request and no GP. Start one. */
1223 srcu_gp_start(sp);
1225 spin_unlock_irq_rcu_node(sp);
1227 if (pushgp)
1228 queue_delayed_work(system_power_efficient_wq, &sp->work, delay);
1232 * This is the work-queue function that handles SRCU grace periods.
1234 static void process_srcu(struct work_struct *work)
1236 struct srcu_struct *sp;
1238 sp = container_of(work, struct srcu_struct, work.work);
1240 srcu_advance_state(sp);
1241 srcu_reschedule(sp, srcu_get_delay(sp));
1244 void srcutorture_get_gp_data(enum rcutorture_type test_type,
1245 struct srcu_struct *sp, int *flags,
1246 unsigned long *gpnum, unsigned long *completed)
1248 if (test_type != SRCU_FLAVOR)
1249 return;
1250 *flags = 0;
1251 *completed = rcu_seq_ctr(sp->srcu_gp_seq);
1252 *gpnum = rcu_seq_ctr(sp->srcu_gp_seq_needed);
1254 EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
1256 void srcu_torture_stats_print(struct srcu_struct *sp, char *tt, char *tf)
1258 int cpu;
1259 int idx;
1260 unsigned long s0 = 0, s1 = 0;
1262 idx = sp->srcu_idx & 0x1;
1263 pr_alert("%s%s Tree SRCU per-CPU(idx=%d):", tt, tf, idx);
1264 for_each_possible_cpu(cpu) {
1265 unsigned long l0, l1;
1266 unsigned long u0, u1;
1267 long c0, c1;
1268 struct srcu_data *counts;
1270 counts = per_cpu_ptr(sp->sda, cpu);
1271 u0 = counts->srcu_unlock_count[!idx];
1272 u1 = counts->srcu_unlock_count[idx];
1275 * Make sure that a lock is always counted if the corresponding
1276 * unlock is counted.
1278 smp_rmb();
1280 l0 = counts->srcu_lock_count[!idx];
1281 l1 = counts->srcu_lock_count[idx];
1283 c0 = l0 - u0;
1284 c1 = l1 - u1;
1285 pr_cont(" %d(%ld,%ld)", cpu, c0, c1);
1286 s0 += c0;
1287 s1 += c1;
1289 pr_cont(" T(%ld,%ld)\n", s0, s1);
1291 EXPORT_SYMBOL_GPL(srcu_torture_stats_print);
1293 static int __init srcu_bootup_announce(void)
1295 pr_info("Hierarchical SRCU implementation.\n");
1296 if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF)
1297 pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff);
1298 return 0;
1300 early_initcall(srcu_bootup_announce);