blob: 21115ffb6c446d21a2e18b7f31be4d3b2e3cc957 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0+
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Sleepable Read-Copy Update mechanism for mutual exclusion.
4 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 * Copyright (C) IBM Corporation, 2006
6 * Copyright (C) Fujitsu, 2012
7 *
David Brazdil0f672f62019-12-10 10:32:29 +00008 * Author: Paul McKenney <paulmck@linux.ibm.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009 * Lai Jiangshan <laijs@cn.fujitsu.com>
10 *
11 * For detailed explanation of Read-Copy Update mechanism see -
12 * Documentation/RCU/ *.txt
13 *
14 */
15
16#define pr_fmt(fmt) "rcu: " fmt
17
18#include <linux/export.h>
19#include <linux/mutex.h>
20#include <linux/percpu.h>
21#include <linux/preempt.h>
22#include <linux/rcupdate_wait.h>
23#include <linux/sched.h>
24#include <linux/smp.h>
25#include <linux/delay.h>
26#include <linux/module.h>
27#include <linux/srcu.h>
28
29#include "rcu.h"
30#include "rcu_segcblist.h"
31
32/* Holdoff in nanoseconds for auto-expediting. */
33#define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000)
34static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF;
35module_param(exp_holdoff, ulong, 0444);
36
37/* Overflow-check frequency. N bits roughly says every 2**N grace periods. */
38static ulong counter_wrap_check = (ULONG_MAX >> 2);
39module_param(counter_wrap_check, ulong, 0444);
40
David Brazdil0f672f62019-12-10 10:32:29 +000041/* Early-boot callback-management, so early that no lock is required! */
42static LIST_HEAD(srcu_boot_list);
43static bool __read_mostly srcu_init_done;
44
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000045static void srcu_invoke_callbacks(struct work_struct *work);
David Brazdil0f672f62019-12-10 10:32:29 +000046static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047static void process_srcu(struct work_struct *work);
David Brazdil0f672f62019-12-10 10:32:29 +000048static void srcu_delay_timer(struct timer_list *t);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049
50/* Wrappers for lock acquisition and release, see raw_spin_lock_rcu_node(). */
51#define spin_lock_rcu_node(p) \
52do { \
53 spin_lock(&ACCESS_PRIVATE(p, lock)); \
54 smp_mb__after_unlock_lock(); \
55} while (0)
56
57#define spin_unlock_rcu_node(p) spin_unlock(&ACCESS_PRIVATE(p, lock))
58
59#define spin_lock_irq_rcu_node(p) \
60do { \
61 spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \
62 smp_mb__after_unlock_lock(); \
63} while (0)
64
65#define spin_unlock_irq_rcu_node(p) \
66 spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
67
68#define spin_lock_irqsave_rcu_node(p, flags) \
69do { \
70 spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
71 smp_mb__after_unlock_lock(); \
72} while (0)
73
74#define spin_unlock_irqrestore_rcu_node(p, flags) \
75 spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags) \
76
77/*
78 * Initialize SRCU combining tree. Note that statically allocated
79 * srcu_struct structures might already have srcu_read_lock() and
80 * srcu_read_unlock() running against them. So if the is_static parameter
81 * is set, don't initialize ->srcu_lock_count[] and ->srcu_unlock_count[].
82 */
David Brazdil0f672f62019-12-10 10:32:29 +000083static void init_srcu_struct_nodes(struct srcu_struct *ssp, bool is_static)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084{
85 int cpu;
86 int i;
87 int level = 0;
88 int levelspread[RCU_NUM_LVLS];
89 struct srcu_data *sdp;
90 struct srcu_node *snp;
91 struct srcu_node *snp_first;
92
Olivier Deprez0e641232021-09-23 10:07:05 +020093 /* Initialize geometry if it has not already been initialized. */
94 rcu_init_geometry();
95
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096 /* Work out the overall tree geometry. */
David Brazdil0f672f62019-12-10 10:32:29 +000097 ssp->level[0] = &ssp->node[0];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000098 for (i = 1; i < rcu_num_lvls; i++)
David Brazdil0f672f62019-12-10 10:32:29 +000099 ssp->level[i] = ssp->level[i - 1] + num_rcu_lvl[i - 1];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100 rcu_init_levelspread(levelspread, num_rcu_lvl);
101
102 /* Each pass through this loop initializes one srcu_node structure. */
David Brazdil0f672f62019-12-10 10:32:29 +0000103 srcu_for_each_node_breadth_first(ssp, snp) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104 spin_lock_init(&ACCESS_PRIVATE(snp, lock));
105 WARN_ON_ONCE(ARRAY_SIZE(snp->srcu_have_cbs) !=
106 ARRAY_SIZE(snp->srcu_data_have_cbs));
107 for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {
108 snp->srcu_have_cbs[i] = 0;
109 snp->srcu_data_have_cbs[i] = 0;
110 }
111 snp->srcu_gp_seq_needed_exp = 0;
112 snp->grplo = -1;
113 snp->grphi = -1;
David Brazdil0f672f62019-12-10 10:32:29 +0000114 if (snp == &ssp->node[0]) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115 /* Root node, special case. */
116 snp->srcu_parent = NULL;
117 continue;
118 }
119
120 /* Non-root node. */
David Brazdil0f672f62019-12-10 10:32:29 +0000121 if (snp == ssp->level[level + 1])
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000122 level++;
David Brazdil0f672f62019-12-10 10:32:29 +0000123 snp->srcu_parent = ssp->level[level - 1] +
124 (snp - ssp->level[level]) /
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125 levelspread[level - 1];
126 }
127
128 /*
129 * Initialize the per-CPU srcu_data array, which feeds into the
130 * leaves of the srcu_node tree.
131 */
132 WARN_ON_ONCE(ARRAY_SIZE(sdp->srcu_lock_count) !=
133 ARRAY_SIZE(sdp->srcu_unlock_count));
134 level = rcu_num_lvls - 1;
David Brazdil0f672f62019-12-10 10:32:29 +0000135 snp_first = ssp->level[level];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136 for_each_possible_cpu(cpu) {
David Brazdil0f672f62019-12-10 10:32:29 +0000137 sdp = per_cpu_ptr(ssp->sda, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138 spin_lock_init(&ACCESS_PRIVATE(sdp, lock));
139 rcu_segcblist_init(&sdp->srcu_cblist);
140 sdp->srcu_cblist_invoking = false;
David Brazdil0f672f62019-12-10 10:32:29 +0000141 sdp->srcu_gp_seq_needed = ssp->srcu_gp_seq;
142 sdp->srcu_gp_seq_needed_exp = ssp->srcu_gp_seq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143 sdp->mynode = &snp_first[cpu / levelspread[level]];
144 for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {
145 if (snp->grplo < 0)
146 snp->grplo = cpu;
147 snp->grphi = cpu;
148 }
149 sdp->cpu = cpu;
David Brazdil0f672f62019-12-10 10:32:29 +0000150 INIT_WORK(&sdp->work, srcu_invoke_callbacks);
151 timer_setup(&sdp->delay_work, srcu_delay_timer, 0);
152 sdp->ssp = ssp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153 sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
154 if (is_static)
155 continue;
156
157 /* Dynamically allocated, better be no srcu_read_locks()! */
158 for (i = 0; i < ARRAY_SIZE(sdp->srcu_lock_count); i++) {
159 sdp->srcu_lock_count[i] = 0;
160 sdp->srcu_unlock_count[i] = 0;
161 }
162 }
163}
164
165/*
166 * Initialize non-compile-time initialized fields, including the
167 * associated srcu_node and srcu_data structures. The is_static
168 * parameter is passed through to init_srcu_struct_nodes(), and
169 * also tells us that ->sda has already been wired up to srcu_data.
170 */
David Brazdil0f672f62019-12-10 10:32:29 +0000171static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172{
David Brazdil0f672f62019-12-10 10:32:29 +0000173 mutex_init(&ssp->srcu_cb_mutex);
174 mutex_init(&ssp->srcu_gp_mutex);
175 ssp->srcu_idx = 0;
176 ssp->srcu_gp_seq = 0;
177 ssp->srcu_barrier_seq = 0;
178 mutex_init(&ssp->srcu_barrier_mutex);
179 atomic_set(&ssp->srcu_barrier_cpu_cnt, 0);
180 INIT_DELAYED_WORK(&ssp->work, process_srcu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181 if (!is_static)
David Brazdil0f672f62019-12-10 10:32:29 +0000182 ssp->sda = alloc_percpu(struct srcu_data);
183 init_srcu_struct_nodes(ssp, is_static);
184 ssp->srcu_gp_seq_needed_exp = 0;
185 ssp->srcu_last_gp_end = ktime_get_mono_fast_ns();
186 smp_store_release(&ssp->srcu_gp_seq_needed, 0); /* Init done. */
187 return ssp->sda ? 0 : -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000188}
189
190#ifdef CONFIG_DEBUG_LOCK_ALLOC
191
David Brazdil0f672f62019-12-10 10:32:29 +0000192int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193 struct lock_class_key *key)
194{
195 /* Don't re-initialize a lock while it is held. */
David Brazdil0f672f62019-12-10 10:32:29 +0000196 debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));
197 lockdep_init_map(&ssp->dep_map, name, key, 0);
198 spin_lock_init(&ACCESS_PRIVATE(ssp, lock));
199 return init_srcu_struct_fields(ssp, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200}
201EXPORT_SYMBOL_GPL(__init_srcu_struct);
202
203#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
204
205/**
206 * init_srcu_struct - initialize a sleep-RCU structure
David Brazdil0f672f62019-12-10 10:32:29 +0000207 * @ssp: structure to initialize.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000208 *
209 * Must invoke this on a given srcu_struct before passing that srcu_struct
210 * to any other function. Each srcu_struct represents a separate domain
211 * of SRCU protection.
212 */
David Brazdil0f672f62019-12-10 10:32:29 +0000213int init_srcu_struct(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214{
David Brazdil0f672f62019-12-10 10:32:29 +0000215 spin_lock_init(&ACCESS_PRIVATE(ssp, lock));
216 return init_srcu_struct_fields(ssp, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000217}
218EXPORT_SYMBOL_GPL(init_srcu_struct);
219
220#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
221
222/*
223 * First-use initialization of statically allocated srcu_struct
224 * structure. Wiring up the combining tree is more than can be
225 * done with compile-time initialization, so this check is added
David Brazdil0f672f62019-12-10 10:32:29 +0000226 * to each update-side SRCU primitive. Use ssp->lock, which -is-
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000227 * compile-time initialized, to resolve races involving multiple
228 * CPUs trying to garner first-use privileges.
229 */
David Brazdil0f672f62019-12-10 10:32:29 +0000230static void check_init_srcu_struct(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231{
232 unsigned long flags;
233
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234 /* The smp_load_acquire() pairs with the smp_store_release(). */
David Brazdil0f672f62019-12-10 10:32:29 +0000235 if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_gp_seq_needed))) /*^^^*/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000236 return; /* Already initialized. */
David Brazdil0f672f62019-12-10 10:32:29 +0000237 spin_lock_irqsave_rcu_node(ssp, flags);
238 if (!rcu_seq_state(ssp->srcu_gp_seq_needed)) {
239 spin_unlock_irqrestore_rcu_node(ssp, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000240 return;
241 }
David Brazdil0f672f62019-12-10 10:32:29 +0000242 init_srcu_struct_fields(ssp, true);
243 spin_unlock_irqrestore_rcu_node(ssp, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244}
245
246/*
247 * Returns approximate total of the readers' ->srcu_lock_count[] values
248 * for the rank of per-CPU counters specified by idx.
249 */
David Brazdil0f672f62019-12-10 10:32:29 +0000250static unsigned long srcu_readers_lock_idx(struct srcu_struct *ssp, int idx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251{
252 int cpu;
253 unsigned long sum = 0;
254
255 for_each_possible_cpu(cpu) {
David Brazdil0f672f62019-12-10 10:32:29 +0000256 struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000257
258 sum += READ_ONCE(cpuc->srcu_lock_count[idx]);
259 }
260 return sum;
261}
262
263/*
264 * Returns approximate total of the readers' ->srcu_unlock_count[] values
265 * for the rank of per-CPU counters specified by idx.
266 */
David Brazdil0f672f62019-12-10 10:32:29 +0000267static unsigned long srcu_readers_unlock_idx(struct srcu_struct *ssp, int idx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000268{
269 int cpu;
270 unsigned long sum = 0;
271
272 for_each_possible_cpu(cpu) {
David Brazdil0f672f62019-12-10 10:32:29 +0000273 struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000274
275 sum += READ_ONCE(cpuc->srcu_unlock_count[idx]);
276 }
277 return sum;
278}
279
280/*
281 * Return true if the number of pre-existing readers is determined to
282 * be zero.
283 */
David Brazdil0f672f62019-12-10 10:32:29 +0000284static bool srcu_readers_active_idx_check(struct srcu_struct *ssp, int idx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000285{
286 unsigned long unlocks;
287
David Brazdil0f672f62019-12-10 10:32:29 +0000288 unlocks = srcu_readers_unlock_idx(ssp, idx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289
290 /*
291 * Make sure that a lock is always counted if the corresponding
292 * unlock is counted. Needs to be a smp_mb() as the read side may
293 * contain a read from a variable that is written to before the
294 * synchronize_srcu() in the write side. In this case smp_mb()s
295 * A and B act like the store buffering pattern.
296 *
297 * This smp_mb() also pairs with smp_mb() C to prevent accesses
298 * after the synchronize_srcu() from being executed before the
299 * grace period ends.
300 */
301 smp_mb(); /* A */
302
303 /*
304 * If the locks are the same as the unlocks, then there must have
305 * been no readers on this index at some time in between. This does
306 * not mean that there are no more readers, as one could have read
307 * the current index but not have incremented the lock counter yet.
308 *
309 * So suppose that the updater is preempted here for so long
310 * that more than ULONG_MAX non-nested readers come and go in
311 * the meantime. It turns out that this cannot result in overflow
312 * because if a reader modifies its unlock count after we read it
313 * above, then that reader's next load of ->srcu_idx is guaranteed
314 * to get the new value, which will cause it to operate on the
315 * other bank of counters, where it cannot contribute to the
316 * overflow of these counters. This means that there is a maximum
317 * of 2*NR_CPUS increments, which cannot overflow given current
318 * systems, especially not on 64-bit systems.
319 *
320 * OK, how about nesting? This does impose a limit on nesting
321 * of floor(ULONG_MAX/NR_CPUS/2), which should be sufficient,
322 * especially on 64-bit systems.
323 */
David Brazdil0f672f62019-12-10 10:32:29 +0000324 return srcu_readers_lock_idx(ssp, idx) == unlocks;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000325}
326
327/**
328 * srcu_readers_active - returns true if there are readers. and false
329 * otherwise
David Brazdil0f672f62019-12-10 10:32:29 +0000330 * @ssp: which srcu_struct to count active readers (holding srcu_read_lock).
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000331 *
332 * Note that this is not an atomic primitive, and can therefore suffer
333 * severe errors when invoked on an active srcu_struct. That said, it
334 * can be useful as an error check at cleanup time.
335 */
David Brazdil0f672f62019-12-10 10:32:29 +0000336static bool srcu_readers_active(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000337{
338 int cpu;
339 unsigned long sum = 0;
340
341 for_each_possible_cpu(cpu) {
David Brazdil0f672f62019-12-10 10:32:29 +0000342 struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000343
344 sum += READ_ONCE(cpuc->srcu_lock_count[0]);
345 sum += READ_ONCE(cpuc->srcu_lock_count[1]);
346 sum -= READ_ONCE(cpuc->srcu_unlock_count[0]);
347 sum -= READ_ONCE(cpuc->srcu_unlock_count[1]);
348 }
349 return sum;
350}
351
352#define SRCU_INTERVAL 1
353
354/*
355 * Return grace-period delay, zero if there are expedited grace
356 * periods pending, SRCU_INTERVAL otherwise.
357 */
David Brazdil0f672f62019-12-10 10:32:29 +0000358static unsigned long srcu_get_delay(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000359{
David Brazdil0f672f62019-12-10 10:32:29 +0000360 if (ULONG_CMP_LT(READ_ONCE(ssp->srcu_gp_seq),
361 READ_ONCE(ssp->srcu_gp_seq_needed_exp)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000362 return 0;
363 return SRCU_INTERVAL;
364}
365
David Brazdil0f672f62019-12-10 10:32:29 +0000366/**
367 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
368 * @ssp: structure to clean up.
369 *
370 * Must invoke this after you are finished using a given srcu_struct that
371 * was initialized via init_srcu_struct(), else you leak memory.
372 */
373void cleanup_srcu_struct(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000374{
375 int cpu;
376
David Brazdil0f672f62019-12-10 10:32:29 +0000377 if (WARN_ON(!srcu_get_delay(ssp)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000378 return; /* Just leak it! */
David Brazdil0f672f62019-12-10 10:32:29 +0000379 if (WARN_ON(srcu_readers_active(ssp)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000380 return; /* Just leak it! */
David Brazdil0f672f62019-12-10 10:32:29 +0000381 flush_delayed_work(&ssp->work);
382 for_each_possible_cpu(cpu) {
383 struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
384
385 del_timer_sync(&sdp->delay_work);
386 flush_work(&sdp->work);
387 if (WARN_ON(rcu_segcblist_n_cbs(&sdp->srcu_cblist)))
388 return; /* Forgot srcu_barrier(), so just leak it! */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000389 }
David Brazdil0f672f62019-12-10 10:32:29 +0000390 if (WARN_ON(rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
391 WARN_ON(srcu_readers_active(ssp))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000392 pr_info("%s: Active srcu_struct %p state: %d\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000393 __func__, ssp, rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000394 return; /* Caller forgot to stop doing call_srcu()? */
395 }
David Brazdil0f672f62019-12-10 10:32:29 +0000396 free_percpu(ssp->sda);
397 ssp->sda = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000398}
David Brazdil0f672f62019-12-10 10:32:29 +0000399EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000400
401/*
402 * Counts the new reader in the appropriate per-CPU element of the
403 * srcu_struct.
404 * Returns an index that must be passed to the matching srcu_read_unlock().
405 */
David Brazdil0f672f62019-12-10 10:32:29 +0000406int __srcu_read_lock(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000407{
408 int idx;
409
David Brazdil0f672f62019-12-10 10:32:29 +0000410 idx = READ_ONCE(ssp->srcu_idx) & 0x1;
411 this_cpu_inc(ssp->sda->srcu_lock_count[idx]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000412 smp_mb(); /* B */ /* Avoid leaking the critical section. */
413 return idx;
414}
415EXPORT_SYMBOL_GPL(__srcu_read_lock);
416
417/*
418 * Removes the count for the old reader from the appropriate per-CPU
419 * element of the srcu_struct. Note that this may well be a different
420 * CPU than that which was incremented by the corresponding srcu_read_lock().
421 */
David Brazdil0f672f62019-12-10 10:32:29 +0000422void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000423{
424 smp_mb(); /* C */ /* Avoid leaking the critical section. */
David Brazdil0f672f62019-12-10 10:32:29 +0000425 this_cpu_inc(ssp->sda->srcu_unlock_count[idx]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426}
427EXPORT_SYMBOL_GPL(__srcu_read_unlock);
428
429/*
430 * We use an adaptive strategy for synchronize_srcu() and especially for
431 * synchronize_srcu_expedited(). We spin for a fixed time period
432 * (defined below) to allow SRCU readers to exit their read-side critical
433 * sections. If there are still some readers after a few microseconds,
434 * we repeatedly block for 1-millisecond time periods.
435 */
436#define SRCU_RETRY_CHECK_DELAY 5
437
438/*
439 * Start an SRCU grace period.
440 */
David Brazdil0f672f62019-12-10 10:32:29 +0000441static void srcu_gp_start(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442{
David Brazdil0f672f62019-12-10 10:32:29 +0000443 struct srcu_data *sdp = this_cpu_ptr(ssp->sda);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000444 int state;
445
David Brazdil0f672f62019-12-10 10:32:29 +0000446 lockdep_assert_held(&ACCESS_PRIVATE(ssp, lock));
447 WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed));
448 spin_lock_rcu_node(sdp); /* Interrupts already disabled. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449 rcu_segcblist_advance(&sdp->srcu_cblist,
David Brazdil0f672f62019-12-10 10:32:29 +0000450 rcu_seq_current(&ssp->srcu_gp_seq));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000451 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
David Brazdil0f672f62019-12-10 10:32:29 +0000452 rcu_seq_snap(&ssp->srcu_gp_seq));
453 spin_unlock_rcu_node(sdp); /* Interrupts remain disabled. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000454 smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
David Brazdil0f672f62019-12-10 10:32:29 +0000455 rcu_seq_start(&ssp->srcu_gp_seq);
456 state = rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000457 WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
458}
459
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000460
David Brazdil0f672f62019-12-10 10:32:29 +0000461static void srcu_delay_timer(struct timer_list *t)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000462{
David Brazdil0f672f62019-12-10 10:32:29 +0000463 struct srcu_data *sdp = container_of(t, struct srcu_data, delay_work);
464
465 queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000466}
467
David Brazdil0f672f62019-12-10 10:32:29 +0000468static void srcu_queue_delayed_work_on(struct srcu_data *sdp,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000469 unsigned long delay)
470{
David Brazdil0f672f62019-12-10 10:32:29 +0000471 if (!delay) {
472 queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work);
473 return;
474 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000475
David Brazdil0f672f62019-12-10 10:32:29 +0000476 timer_reduce(&sdp->delay_work, jiffies + delay);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477}
478
479/*
480 * Schedule callback invocation for the specified srcu_data structure,
481 * if possible, on the corresponding CPU.
482 */
483static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
484{
David Brazdil0f672f62019-12-10 10:32:29 +0000485 srcu_queue_delayed_work_on(sdp, delay);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000486}
487
488/*
489 * Schedule callback invocation for all srcu_data structures associated
490 * with the specified srcu_node structure that have callbacks for the
491 * just-completed grace period, the one corresponding to idx. If possible,
492 * schedule this invocation on the corresponding CPUs.
493 */
David Brazdil0f672f62019-12-10 10:32:29 +0000494static void srcu_schedule_cbs_snp(struct srcu_struct *ssp, struct srcu_node *snp,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000495 unsigned long mask, unsigned long delay)
496{
497 int cpu;
498
499 for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
500 if (!(mask & (1 << (cpu - snp->grplo))))
501 continue;
David Brazdil0f672f62019-12-10 10:32:29 +0000502 srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, cpu), delay);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503 }
504}
505
506/*
507 * Note the end of an SRCU grace period. Initiates callback invocation
508 * and starts a new grace period if needed.
509 *
510 * The ->srcu_cb_mutex acquisition does not protect any data, but
511 * instead prevents more than one grace period from starting while we
512 * are initiating callback invocation. This allows the ->srcu_have_cbs[]
513 * array to have a finite number of elements.
514 */
David Brazdil0f672f62019-12-10 10:32:29 +0000515static void srcu_gp_end(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000516{
517 unsigned long cbdelay;
518 bool cbs;
519 bool last_lvl;
520 int cpu;
521 unsigned long flags;
522 unsigned long gpseq;
523 int idx;
524 unsigned long mask;
525 struct srcu_data *sdp;
526 struct srcu_node *snp;
527
528 /* Prevent more than one additional grace period. */
David Brazdil0f672f62019-12-10 10:32:29 +0000529 mutex_lock(&ssp->srcu_cb_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000530
531 /* End the current grace period. */
David Brazdil0f672f62019-12-10 10:32:29 +0000532 spin_lock_irq_rcu_node(ssp);
533 idx = rcu_seq_state(ssp->srcu_gp_seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534 WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
David Brazdil0f672f62019-12-10 10:32:29 +0000535 cbdelay = srcu_get_delay(ssp);
Olivier Deprez0e641232021-09-23 10:07:05 +0200536 WRITE_ONCE(ssp->srcu_last_gp_end, ktime_get_mono_fast_ns());
David Brazdil0f672f62019-12-10 10:32:29 +0000537 rcu_seq_end(&ssp->srcu_gp_seq);
538 gpseq = rcu_seq_current(&ssp->srcu_gp_seq);
539 if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, gpseq))
540 ssp->srcu_gp_seq_needed_exp = gpseq;
541 spin_unlock_irq_rcu_node(ssp);
542 mutex_unlock(&ssp->srcu_gp_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000543 /* A new grace period can start at this point. But only one. */
544
545 /* Initiate callback invocation as needed. */
546 idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
David Brazdil0f672f62019-12-10 10:32:29 +0000547 srcu_for_each_node_breadth_first(ssp, snp) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000548 spin_lock_irq_rcu_node(snp);
549 cbs = false;
David Brazdil0f672f62019-12-10 10:32:29 +0000550 last_lvl = snp >= ssp->level[rcu_num_lvls - 1];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000551 if (last_lvl)
552 cbs = snp->srcu_have_cbs[idx] == gpseq;
553 snp->srcu_have_cbs[idx] = gpseq;
554 rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
555 if (ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, gpseq))
556 snp->srcu_gp_seq_needed_exp = gpseq;
557 mask = snp->srcu_data_have_cbs[idx];
558 snp->srcu_data_have_cbs[idx] = 0;
559 spin_unlock_irq_rcu_node(snp);
560 if (cbs)
David Brazdil0f672f62019-12-10 10:32:29 +0000561 srcu_schedule_cbs_snp(ssp, snp, mask, cbdelay);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000562
563 /* Occasionally prevent srcu_data counter wrap. */
564 if (!(gpseq & counter_wrap_check) && last_lvl)
565 for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
David Brazdil0f672f62019-12-10 10:32:29 +0000566 sdp = per_cpu_ptr(ssp->sda, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000567 spin_lock_irqsave_rcu_node(sdp, flags);
568 if (ULONG_CMP_GE(gpseq,
569 sdp->srcu_gp_seq_needed + 100))
570 sdp->srcu_gp_seq_needed = gpseq;
571 if (ULONG_CMP_GE(gpseq,
572 sdp->srcu_gp_seq_needed_exp + 100))
573 sdp->srcu_gp_seq_needed_exp = gpseq;
574 spin_unlock_irqrestore_rcu_node(sdp, flags);
575 }
576 }
577
578 /* Callback initiation done, allow grace periods after next. */
David Brazdil0f672f62019-12-10 10:32:29 +0000579 mutex_unlock(&ssp->srcu_cb_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000580
581 /* Start a new grace period if needed. */
David Brazdil0f672f62019-12-10 10:32:29 +0000582 spin_lock_irq_rcu_node(ssp);
583 gpseq = rcu_seq_current(&ssp->srcu_gp_seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000584 if (!rcu_seq_state(gpseq) &&
David Brazdil0f672f62019-12-10 10:32:29 +0000585 ULONG_CMP_LT(gpseq, ssp->srcu_gp_seq_needed)) {
586 srcu_gp_start(ssp);
587 spin_unlock_irq_rcu_node(ssp);
588 srcu_reschedule(ssp, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000589 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000590 spin_unlock_irq_rcu_node(ssp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000591 }
592}
593
594/*
595 * Funnel-locking scheme to scalably mediate many concurrent expedited
596 * grace-period requests. This function is invoked for the first known
597 * expedited request for a grace period that has already been requested,
598 * but without expediting. To start a completely new grace period,
599 * whether expedited or not, use srcu_funnel_gp_start() instead.
600 */
David Brazdil0f672f62019-12-10 10:32:29 +0000601static void srcu_funnel_exp_start(struct srcu_struct *ssp, struct srcu_node *snp,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000602 unsigned long s)
603{
604 unsigned long flags;
605
606 for (; snp != NULL; snp = snp->srcu_parent) {
David Brazdil0f672f62019-12-10 10:32:29 +0000607 if (rcu_seq_done(&ssp->srcu_gp_seq, s) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000608 ULONG_CMP_GE(READ_ONCE(snp->srcu_gp_seq_needed_exp), s))
609 return;
610 spin_lock_irqsave_rcu_node(snp, flags);
611 if (ULONG_CMP_GE(snp->srcu_gp_seq_needed_exp, s)) {
612 spin_unlock_irqrestore_rcu_node(snp, flags);
613 return;
614 }
615 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
616 spin_unlock_irqrestore_rcu_node(snp, flags);
617 }
David Brazdil0f672f62019-12-10 10:32:29 +0000618 spin_lock_irqsave_rcu_node(ssp, flags);
619 if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, s))
620 ssp->srcu_gp_seq_needed_exp = s;
621 spin_unlock_irqrestore_rcu_node(ssp, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000622}
623
624/*
625 * Funnel-locking scheme to scalably mediate many concurrent grace-period
626 * requests. The winner has to do the work of actually starting grace
627 * period s. Losers must either ensure that their desired grace-period
628 * number is recorded on at least their leaf srcu_node structure, or they
629 * must take steps to invoke their own callbacks.
630 *
631 * Note that this function also does the work of srcu_funnel_exp_start(),
632 * in some cases by directly invoking it.
633 */
David Brazdil0f672f62019-12-10 10:32:29 +0000634static void srcu_funnel_gp_start(struct srcu_struct *ssp, struct srcu_data *sdp,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635 unsigned long s, bool do_norm)
636{
637 unsigned long flags;
638 int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
639 struct srcu_node *snp = sdp->mynode;
640 unsigned long snp_seq;
641
642 /* Each pass through the loop does one level of the srcu_node tree. */
643 for (; snp != NULL; snp = snp->srcu_parent) {
David Brazdil0f672f62019-12-10 10:32:29 +0000644 if (rcu_seq_done(&ssp->srcu_gp_seq, s) && snp != sdp->mynode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000645 return; /* GP already done and CBs recorded. */
646 spin_lock_irqsave_rcu_node(snp, flags);
647 if (ULONG_CMP_GE(snp->srcu_have_cbs[idx], s)) {
648 snp_seq = snp->srcu_have_cbs[idx];
649 if (snp == sdp->mynode && snp_seq == s)
650 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
651 spin_unlock_irqrestore_rcu_node(snp, flags);
652 if (snp == sdp->mynode && snp_seq != s) {
653 srcu_schedule_cbs_sdp(sdp, do_norm
654 ? SRCU_INTERVAL
655 : 0);
656 return;
657 }
658 if (!do_norm)
David Brazdil0f672f62019-12-10 10:32:29 +0000659 srcu_funnel_exp_start(ssp, snp, s);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000660 return;
661 }
662 snp->srcu_have_cbs[idx] = s;
663 if (snp == sdp->mynode)
664 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
665 if (!do_norm && ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, s))
666 snp->srcu_gp_seq_needed_exp = s;
667 spin_unlock_irqrestore_rcu_node(snp, flags);
668 }
669
670 /* Top of tree, must ensure the grace period will be started. */
David Brazdil0f672f62019-12-10 10:32:29 +0000671 spin_lock_irqsave_rcu_node(ssp, flags);
672 if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed, s)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000673 /*
674 * Record need for grace period s. Pair with load
675 * acquire setting up for initialization.
676 */
David Brazdil0f672f62019-12-10 10:32:29 +0000677 smp_store_release(&ssp->srcu_gp_seq_needed, s); /*^^^*/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000678 }
David Brazdil0f672f62019-12-10 10:32:29 +0000679 if (!do_norm && ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, s))
680 ssp->srcu_gp_seq_needed_exp = s;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000681
682 /* If grace period not already done and none in progress, start it. */
David Brazdil0f672f62019-12-10 10:32:29 +0000683 if (!rcu_seq_done(&ssp->srcu_gp_seq, s) &&
684 rcu_seq_state(ssp->srcu_gp_seq) == SRCU_STATE_IDLE) {
685 WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed));
686 srcu_gp_start(ssp);
687 if (likely(srcu_init_done))
688 queue_delayed_work(rcu_gp_wq, &ssp->work,
689 srcu_get_delay(ssp));
690 else if (list_empty(&ssp->work.work.entry))
691 list_add(&ssp->work.work.entry, &srcu_boot_list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000692 }
David Brazdil0f672f62019-12-10 10:32:29 +0000693 spin_unlock_irqrestore_rcu_node(ssp, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000694}
695
696/*
697 * Wait until all readers counted by array index idx complete, but
698 * loop an additional time if there is an expedited grace period pending.
699 * The caller must ensure that ->srcu_idx is not changed while checking.
700 */
David Brazdil0f672f62019-12-10 10:32:29 +0000701static bool try_check_zero(struct srcu_struct *ssp, int idx, int trycount)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000702{
703 for (;;) {
David Brazdil0f672f62019-12-10 10:32:29 +0000704 if (srcu_readers_active_idx_check(ssp, idx))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000705 return true;
David Brazdil0f672f62019-12-10 10:32:29 +0000706 if (--trycount + !srcu_get_delay(ssp) <= 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000707 return false;
708 udelay(SRCU_RETRY_CHECK_DELAY);
709 }
710}
711
712/*
713 * Increment the ->srcu_idx counter so that future SRCU readers will
714 * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows
715 * us to wait for pre-existing readers in a starvation-free manner.
716 */
David Brazdil0f672f62019-12-10 10:32:29 +0000717static void srcu_flip(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000718{
719 /*
720 * Ensure that if this updater saw a given reader's increment
721 * from __srcu_read_lock(), that reader was using an old value
722 * of ->srcu_idx. Also ensure that if a given reader sees the
723 * new value of ->srcu_idx, this updater's earlier scans cannot
724 * have seen that reader's increments (which is OK, because this
725 * grace period need not wait on that reader).
726 */
727 smp_mb(); /* E */ /* Pairs with B and C. */
728
David Brazdil0f672f62019-12-10 10:32:29 +0000729 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000730
731 /*
732 * Ensure that if the updater misses an __srcu_read_unlock()
733 * increment, that task's next __srcu_read_lock() will see the
734 * above counter update. Note that both this memory barrier
735 * and the one in srcu_readers_active_idx_check() provide the
736 * guarantee for __srcu_read_lock().
737 */
738 smp_mb(); /* D */ /* Pairs with C. */
739}
740
741/*
742 * If SRCU is likely idle, return true, otherwise return false.
743 *
744 * Note that it is OK for several current from-idle requests for a new
745 * grace period from idle to specify expediting because they will all end
746 * up requesting the same grace period anyhow. So no loss.
747 *
748 * Note also that if any CPU (including the current one) is still invoking
749 * callbacks, this function will nevertheless say "idle". This is not
750 * ideal, but the overhead of checking all CPUs' callback lists is even
751 * less ideal, especially on large systems. Furthermore, the wakeup
752 * can happen before the callback is fully removed, so we have no choice
753 * but to accept this type of error.
754 *
755 * This function is also subject to counter-wrap errors, but let's face
756 * it, if this function was preempted for enough time for the counters
757 * to wrap, it really doesn't matter whether or not we expedite the grace
758 * period. The extra overhead of a needlessly expedited grace period is
759 * negligible when amoritized over that time period, and the extra latency
760 * of a needlessly non-expedited grace period is similarly negligible.
761 */
David Brazdil0f672f62019-12-10 10:32:29 +0000762static bool srcu_might_be_idle(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000763{
764 unsigned long curseq;
765 unsigned long flags;
766 struct srcu_data *sdp;
767 unsigned long t;
Olivier Deprez0e641232021-09-23 10:07:05 +0200768 unsigned long tlast;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000769
770 /* If the local srcu_data structure has callbacks, not idle. */
771 local_irq_save(flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000772 sdp = this_cpu_ptr(ssp->sda);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000773 if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
774 local_irq_restore(flags);
775 return false; /* Callbacks already present, so not idle. */
776 }
777 local_irq_restore(flags);
778
779 /*
780 * No local callbacks, so probabalistically probe global state.
781 * Exact information would require acquiring locks, which would
782 * kill scalability, hence the probabalistic nature of the probe.
783 */
784
785 /* First, see if enough time has passed since the last GP. */
786 t = ktime_get_mono_fast_ns();
Olivier Deprez0e641232021-09-23 10:07:05 +0200787 tlast = READ_ONCE(ssp->srcu_last_gp_end);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000788 if (exp_holdoff == 0 ||
Olivier Deprez0e641232021-09-23 10:07:05 +0200789 time_in_range_open(t, tlast, tlast + exp_holdoff))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000790 return false; /* Too soon after last GP. */
791
792 /* Next, check for probable idleness. */
David Brazdil0f672f62019-12-10 10:32:29 +0000793 curseq = rcu_seq_current(&ssp->srcu_gp_seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000794 smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
David Brazdil0f672f62019-12-10 10:32:29 +0000795 if (ULONG_CMP_LT(curseq, READ_ONCE(ssp->srcu_gp_seq_needed)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000796 return false; /* Grace period in progress, so not idle. */
797 smp_mb(); /* Order ->srcu_gp_seq with prior access. */
David Brazdil0f672f62019-12-10 10:32:29 +0000798 if (curseq != rcu_seq_current(&ssp->srcu_gp_seq))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000799 return false; /* GP # changed, so not idle. */
800 return true; /* With reasonable probability, idle! */
801}
802
803/*
804 * SRCU callback function to leak a callback.
805 */
806static void srcu_leak_callback(struct rcu_head *rhp)
807{
808}
809
810/*
811 * Enqueue an SRCU callback on the srcu_data structure associated with
812 * the current CPU and the specified srcu_struct structure, initiating
813 * grace-period processing if it is not already running.
814 *
815 * Note that all CPUs must agree that the grace period extended beyond
816 * all pre-existing SRCU read-side critical section. On systems with
817 * more than one CPU, this means that when "func()" is invoked, each CPU
818 * is guaranteed to have executed a full memory barrier since the end of
819 * its last corresponding SRCU read-side critical section whose beginning
820 * preceded the call to call_srcu(). It also means that each CPU executing
821 * an SRCU read-side critical section that continues beyond the start of
822 * "func()" must have executed a memory barrier after the call_srcu()
823 * but before the beginning of that SRCU read-side critical section.
824 * Note that these guarantees include CPUs that are offline, idle, or
825 * executing in user mode, as well as CPUs that are executing in the kernel.
826 *
827 * Furthermore, if CPU A invoked call_srcu() and CPU B invoked the
828 * resulting SRCU callback function "func()", then both CPU A and CPU
829 * B are guaranteed to execute a full memory barrier during the time
830 * interval between the call to call_srcu() and the invocation of "func()".
831 * This guarantee applies even if CPU A and CPU B are the same CPU (but
832 * again only if the system has more than one CPU).
833 *
834 * Of course, these guarantees apply only for invocations of call_srcu(),
835 * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
836 * srcu_struct structure.
837 */
David Brazdil0f672f62019-12-10 10:32:29 +0000838static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
839 rcu_callback_t func, bool do_norm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000840{
841 unsigned long flags;
David Brazdil0f672f62019-12-10 10:32:29 +0000842 int idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000843 bool needexp = false;
844 bool needgp = false;
845 unsigned long s;
846 struct srcu_data *sdp;
847
David Brazdil0f672f62019-12-10 10:32:29 +0000848 check_init_srcu_struct(ssp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000849 if (debug_rcu_head_queue(rhp)) {
850 /* Probable double call_srcu(), so leak the callback. */
851 WRITE_ONCE(rhp->func, srcu_leak_callback);
852 WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n");
853 return;
854 }
855 rhp->func = func;
David Brazdil0f672f62019-12-10 10:32:29 +0000856 idx = srcu_read_lock(ssp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000857 local_irq_save(flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000858 sdp = this_cpu_ptr(ssp->sda);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000859 spin_lock_rcu_node(sdp);
860 rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp, false);
861 rcu_segcblist_advance(&sdp->srcu_cblist,
David Brazdil0f672f62019-12-10 10:32:29 +0000862 rcu_seq_current(&ssp->srcu_gp_seq));
863 s = rcu_seq_snap(&ssp->srcu_gp_seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000864 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s);
865 if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
866 sdp->srcu_gp_seq_needed = s;
867 needgp = true;
868 }
869 if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
870 sdp->srcu_gp_seq_needed_exp = s;
871 needexp = true;
872 }
873 spin_unlock_irqrestore_rcu_node(sdp, flags);
874 if (needgp)
David Brazdil0f672f62019-12-10 10:32:29 +0000875 srcu_funnel_gp_start(ssp, sdp, s, do_norm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000876 else if (needexp)
David Brazdil0f672f62019-12-10 10:32:29 +0000877 srcu_funnel_exp_start(ssp, sdp->mynode, s);
878 srcu_read_unlock(ssp, idx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000879}
880
881/**
882 * call_srcu() - Queue a callback for invocation after an SRCU grace period
David Brazdil0f672f62019-12-10 10:32:29 +0000883 * @ssp: srcu_struct in queue the callback
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000884 * @rhp: structure to be used for queueing the SRCU callback.
885 * @func: function to be invoked after the SRCU grace period
886 *
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.
894 *
895 * The callback will be invoked from process context, but must nevertheless
896 * be fast and must not block.
897 */
David Brazdil0f672f62019-12-10 10:32:29 +0000898void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000899 rcu_callback_t func)
900{
David Brazdil0f672f62019-12-10 10:32:29 +0000901 __call_srcu(ssp, rhp, func, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000902}
903EXPORT_SYMBOL_GPL(call_srcu);
904
905/*
906 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
907 */
David Brazdil0f672f62019-12-10 10:32:29 +0000908static void __synchronize_srcu(struct srcu_struct *ssp, bool do_norm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000909{
910 struct rcu_synchronize rcu;
911
David Brazdil0f672f62019-12-10 10:32:29 +0000912 RCU_LOCKDEP_WARN(lock_is_held(&ssp->dep_map) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000913 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");
917
918 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
919 return;
920 might_sleep();
David Brazdil0f672f62019-12-10 10:32:29 +0000921 check_init_srcu_struct(ssp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000922 init_completion(&rcu.completion);
923 init_rcu_head_on_stack(&rcu.head);
David Brazdil0f672f62019-12-10 10:32:29 +0000924 __call_srcu(ssp, &rcu.head, wakeme_after_rcu, do_norm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000925 wait_for_completion(&rcu.completion);
926 destroy_rcu_head_on_stack(&rcu.head);
927
928 /*
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.
934 */
935 smp_mb();
936}
937
938/**
939 * synchronize_srcu_expedited - Brute-force SRCU grace period
David Brazdil0f672f62019-12-10 10:32:29 +0000940 * @ssp: srcu_struct with which to synchronize.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000941 *
942 * Wait for an SRCU grace period to elapse, but be more aggressive about
943 * spinning rather than blocking when waiting.
944 *
945 * Note that synchronize_srcu_expedited() has the same deadlock and
946 * memory-ordering properties as does synchronize_srcu().
947 */
David Brazdil0f672f62019-12-10 10:32:29 +0000948void synchronize_srcu_expedited(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000949{
David Brazdil0f672f62019-12-10 10:32:29 +0000950 __synchronize_srcu(ssp, rcu_gp_is_normal());
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000951}
952EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
953
954/**
955 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
David Brazdil0f672f62019-12-10 10:32:29 +0000956 * @ssp: srcu_struct with which to synchronize.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000957 *
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.
962 *
963 * Can block; must be called from process context.
964 *
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.
970 *
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
David Brazdil0f672f62019-12-10 10:32:29 +0000974 * the end of its last corresponding SRCU read-side critical section
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000975 * 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.
982 *
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.
988 *
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.
992 *
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.
997 */
David Brazdil0f672f62019-12-10 10:32:29 +0000998void synchronize_srcu(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000999{
David Brazdil0f672f62019-12-10 10:32:29 +00001000 if (srcu_might_be_idle(ssp) || rcu_gp_is_expedited())
1001 synchronize_srcu_expedited(ssp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001002 else
David Brazdil0f672f62019-12-10 10:32:29 +00001003 __synchronize_srcu(ssp, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001004}
1005EXPORT_SYMBOL_GPL(synchronize_srcu);
1006
1007/*
1008 * Callback function for srcu_barrier() use.
1009 */
1010static void srcu_barrier_cb(struct rcu_head *rhp)
1011{
1012 struct srcu_data *sdp;
David Brazdil0f672f62019-12-10 10:32:29 +00001013 struct srcu_struct *ssp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001014
1015 sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
David Brazdil0f672f62019-12-10 10:32:29 +00001016 ssp = sdp->ssp;
1017 if (atomic_dec_and_test(&ssp->srcu_barrier_cpu_cnt))
1018 complete(&ssp->srcu_barrier_completion);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019}
1020
1021/**
1022 * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
David Brazdil0f672f62019-12-10 10:32:29 +00001023 * @ssp: srcu_struct on which to wait for in-flight callbacks.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001024 */
David Brazdil0f672f62019-12-10 10:32:29 +00001025void srcu_barrier(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001026{
1027 int cpu;
1028 struct srcu_data *sdp;
David Brazdil0f672f62019-12-10 10:32:29 +00001029 unsigned long s = rcu_seq_snap(&ssp->srcu_barrier_seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030
David Brazdil0f672f62019-12-10 10:32:29 +00001031 check_init_srcu_struct(ssp);
1032 mutex_lock(&ssp->srcu_barrier_mutex);
1033 if (rcu_seq_done(&ssp->srcu_barrier_seq, s)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001034 smp_mb(); /* Force ordering following return. */
David Brazdil0f672f62019-12-10 10:32:29 +00001035 mutex_unlock(&ssp->srcu_barrier_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001036 return; /* Someone else did our work for us. */
1037 }
David Brazdil0f672f62019-12-10 10:32:29 +00001038 rcu_seq_start(&ssp->srcu_barrier_seq);
1039 init_completion(&ssp->srcu_barrier_completion);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001040
1041 /* Initial count prevents reaching zero until all CBs are posted. */
David Brazdil0f672f62019-12-10 10:32:29 +00001042 atomic_set(&ssp->srcu_barrier_cpu_cnt, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001043
1044 /*
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.
1051 */
1052 for_each_possible_cpu(cpu) {
David Brazdil0f672f62019-12-10 10:32:29 +00001053 sdp = per_cpu_ptr(ssp->sda, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001054 spin_lock_irq_rcu_node(sdp);
David Brazdil0f672f62019-12-10 10:32:29 +00001055 atomic_inc(&ssp->srcu_barrier_cpu_cnt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001056 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);
David Brazdil0f672f62019-12-10 10:32:29 +00001061 atomic_dec(&ssp->srcu_barrier_cpu_cnt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001062 }
1063 spin_unlock_irq_rcu_node(sdp);
1064 }
1065
1066 /* Remove the initial count, at which point reaching zero can happen. */
David Brazdil0f672f62019-12-10 10:32:29 +00001067 if (atomic_dec_and_test(&ssp->srcu_barrier_cpu_cnt))
1068 complete(&ssp->srcu_barrier_completion);
1069 wait_for_completion(&ssp->srcu_barrier_completion);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001070
David Brazdil0f672f62019-12-10 10:32:29 +00001071 rcu_seq_end(&ssp->srcu_barrier_seq);
1072 mutex_unlock(&ssp->srcu_barrier_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001073}
1074EXPORT_SYMBOL_GPL(srcu_barrier);
1075
1076/**
1077 * srcu_batches_completed - return batches completed.
David Brazdil0f672f62019-12-10 10:32:29 +00001078 * @ssp: srcu_struct on which to report batch completion.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001079 *
1080 * Report the number of batches, correlated with, but not necessarily
1081 * precisely the same as, the number of grace periods that have elapsed.
1082 */
David Brazdil0f672f62019-12-10 10:32:29 +00001083unsigned long srcu_batches_completed(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001084{
David Brazdil0f672f62019-12-10 10:32:29 +00001085 return ssp->srcu_idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001086}
1087EXPORT_SYMBOL_GPL(srcu_batches_completed);
1088
1089/*
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.
1093 */
David Brazdil0f672f62019-12-10 10:32:29 +00001094static void srcu_advance_state(struct srcu_struct *ssp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001095{
1096 int idx;
1097
David Brazdil0f672f62019-12-10 10:32:29 +00001098 mutex_lock(&ssp->srcu_gp_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001099
1100 /*
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.
1106 *
1107 * The load-acquire ensures that we see the accesses performed
1108 * by the prior grace period.
1109 */
David Brazdil0f672f62019-12-10 10:32:29 +00001110 idx = rcu_seq_state(smp_load_acquire(&ssp->srcu_gp_seq)); /* ^^^ */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001111 if (idx == SRCU_STATE_IDLE) {
David Brazdil0f672f62019-12-10 10:32:29 +00001112 spin_lock_irq_rcu_node(ssp);
1113 if (ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)) {
1114 WARN_ON_ONCE(rcu_seq_state(ssp->srcu_gp_seq));
1115 spin_unlock_irq_rcu_node(ssp);
1116 mutex_unlock(&ssp->srcu_gp_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001117 return;
1118 }
David Brazdil0f672f62019-12-10 10:32:29 +00001119 idx = rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001120 if (idx == SRCU_STATE_IDLE)
David Brazdil0f672f62019-12-10 10:32:29 +00001121 srcu_gp_start(ssp);
1122 spin_unlock_irq_rcu_node(ssp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001123 if (idx != SRCU_STATE_IDLE) {
David Brazdil0f672f62019-12-10 10:32:29 +00001124 mutex_unlock(&ssp->srcu_gp_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001125 return; /* Someone else started the grace period. */
1126 }
1127 }
1128
David Brazdil0f672f62019-12-10 10:32:29 +00001129 if (rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
1130 idx = 1 ^ (ssp->srcu_idx & 1);
1131 if (!try_check_zero(ssp, idx, 1)) {
1132 mutex_unlock(&ssp->srcu_gp_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001133 return; /* readers present, retry later. */
1134 }
David Brazdil0f672f62019-12-10 10:32:29 +00001135 srcu_flip(ssp);
1136 rcu_seq_set_state(&ssp->srcu_gp_seq, SRCU_STATE_SCAN2);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001137 }
1138
David Brazdil0f672f62019-12-10 10:32:29 +00001139 if (rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001140
1141 /*
1142 * SRCU read-side critical sections are normally short,
1143 * so check at least twice in quick succession after a flip.
1144 */
David Brazdil0f672f62019-12-10 10:32:29 +00001145 idx = 1 ^ (ssp->srcu_idx & 1);
1146 if (!try_check_zero(ssp, idx, 2)) {
1147 mutex_unlock(&ssp->srcu_gp_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001148 return; /* readers present, retry later. */
1149 }
David Brazdil0f672f62019-12-10 10:32:29 +00001150 srcu_gp_end(ssp); /* Releases ->srcu_gp_mutex. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001151 }
1152}
1153
1154/*
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().
1159 */
1160static void srcu_invoke_callbacks(struct work_struct *work)
1161{
1162 bool more;
1163 struct rcu_cblist ready_cbs;
1164 struct rcu_head *rhp;
1165 struct srcu_data *sdp;
David Brazdil0f672f62019-12-10 10:32:29 +00001166 struct srcu_struct *ssp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001167
David Brazdil0f672f62019-12-10 10:32:29 +00001168 sdp = container_of(work, struct srcu_data, work);
1169
1170 ssp = sdp->ssp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001171 rcu_cblist_init(&ready_cbs);
1172 spin_lock_irq_rcu_node(sdp);
1173 rcu_segcblist_advance(&sdp->srcu_cblist,
David Brazdil0f672f62019-12-10 10:32:29 +00001174 rcu_seq_current(&ssp->srcu_gp_seq));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001175 if (sdp->srcu_cblist_invoking ||
1176 !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
1177 spin_unlock_irq_rcu_node(sdp);
1178 return; /* Someone else on the job or nothing to do. */
1179 }
1180
1181 /* We are on the job! Extract and invoke ready callbacks. */
1182 sdp->srcu_cblist_invoking = true;
1183 rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
1184 spin_unlock_irq_rcu_node(sdp);
1185 rhp = rcu_cblist_dequeue(&ready_cbs);
1186 for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
1187 debug_rcu_head_unqueue(rhp);
1188 local_bh_disable();
1189 rhp->func(rhp);
1190 local_bh_enable();
1191 }
1192
1193 /*
1194 * Update counts, accelerate new callbacks, and if needed,
1195 * schedule another round of callback invocation.
1196 */
1197 spin_lock_irq_rcu_node(sdp);
1198 rcu_segcblist_insert_count(&sdp->srcu_cblist, &ready_cbs);
1199 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
David Brazdil0f672f62019-12-10 10:32:29 +00001200 rcu_seq_snap(&ssp->srcu_gp_seq));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001201 sdp->srcu_cblist_invoking = false;
1202 more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
1203 spin_unlock_irq_rcu_node(sdp);
1204 if (more)
1205 srcu_schedule_cbs_sdp(sdp, 0);
1206}
1207
1208/*
1209 * Finished one round of SRCU grace period. Start another if there are
1210 * more SRCU callbacks queued, otherwise put SRCU into not-running state.
1211 */
David Brazdil0f672f62019-12-10 10:32:29 +00001212static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001213{
1214 bool pushgp = true;
1215
David Brazdil0f672f62019-12-10 10:32:29 +00001216 spin_lock_irq_rcu_node(ssp);
1217 if (ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)) {
1218 if (!WARN_ON_ONCE(rcu_seq_state(ssp->srcu_gp_seq))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001219 /* All requests fulfilled, time to go idle. */
1220 pushgp = false;
1221 }
David Brazdil0f672f62019-12-10 10:32:29 +00001222 } else if (!rcu_seq_state(ssp->srcu_gp_seq)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001223 /* Outstanding request and no GP. Start one. */
David Brazdil0f672f62019-12-10 10:32:29 +00001224 srcu_gp_start(ssp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001225 }
David Brazdil0f672f62019-12-10 10:32:29 +00001226 spin_unlock_irq_rcu_node(ssp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001227
1228 if (pushgp)
David Brazdil0f672f62019-12-10 10:32:29 +00001229 queue_delayed_work(rcu_gp_wq, &ssp->work, delay);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001230}
1231
1232/*
1233 * This is the work-queue function that handles SRCU grace periods.
1234 */
1235static void process_srcu(struct work_struct *work)
1236{
David Brazdil0f672f62019-12-10 10:32:29 +00001237 struct srcu_struct *ssp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001238
David Brazdil0f672f62019-12-10 10:32:29 +00001239 ssp = container_of(work, struct srcu_struct, work.work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001240
David Brazdil0f672f62019-12-10 10:32:29 +00001241 srcu_advance_state(ssp);
1242 srcu_reschedule(ssp, srcu_get_delay(ssp));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001243}
1244
1245void srcutorture_get_gp_data(enum rcutorture_type test_type,
David Brazdil0f672f62019-12-10 10:32:29 +00001246 struct srcu_struct *ssp, int *flags,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001247 unsigned long *gp_seq)
1248{
1249 if (test_type != SRCU_FLAVOR)
1250 return;
1251 *flags = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001252 *gp_seq = rcu_seq_current(&ssp->srcu_gp_seq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001253}
1254EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
1255
David Brazdil0f672f62019-12-10 10:32:29 +00001256void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001257{
1258 int cpu;
1259 int idx;
1260 unsigned long s0 = 0, s1 = 0;
1261
David Brazdil0f672f62019-12-10 10:32:29 +00001262 idx = ssp->srcu_idx & 0x1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001263 pr_alert("%s%s Tree SRCU g%ld per-CPU(idx=%d):",
David Brazdil0f672f62019-12-10 10:32:29 +00001264 tt, tf, rcu_seq_current(&ssp->srcu_gp_seq), idx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001265 for_each_possible_cpu(cpu) {
1266 unsigned long l0, l1;
1267 unsigned long u0, u1;
1268 long c0, c1;
1269 struct srcu_data *sdp;
1270
David Brazdil0f672f62019-12-10 10:32:29 +00001271 sdp = per_cpu_ptr(ssp->sda, cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001272 u0 = sdp->srcu_unlock_count[!idx];
1273 u1 = sdp->srcu_unlock_count[idx];
1274
1275 /*
1276 * Make sure that a lock is always counted if the corresponding
1277 * unlock is counted.
1278 */
1279 smp_rmb();
1280
1281 l0 = sdp->srcu_lock_count[!idx];
1282 l1 = sdp->srcu_lock_count[idx];
1283
1284 c0 = l0 - u0;
1285 c1 = l1 - u1;
David Brazdil0f672f62019-12-10 10:32:29 +00001286 pr_cont(" %d(%ld,%ld %c)",
1287 cpu, c0, c1,
1288 "C."[rcu_segcblist_empty(&sdp->srcu_cblist)]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001289 s0 += c0;
1290 s1 += c1;
1291 }
1292 pr_cont(" T(%ld,%ld)\n", s0, s1);
1293}
1294EXPORT_SYMBOL_GPL(srcu_torture_stats_print);
1295
1296static int __init srcu_bootup_announce(void)
1297{
1298 pr_info("Hierarchical SRCU implementation.\n");
1299 if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF)
1300 pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff);
1301 return 0;
1302}
1303early_initcall(srcu_bootup_announce);
David Brazdil0f672f62019-12-10 10:32:29 +00001304
1305void __init srcu_init(void)
1306{
1307 struct srcu_struct *ssp;
1308
1309 srcu_init_done = true;
1310 while (!list_empty(&srcu_boot_list)) {
1311 ssp = list_first_entry(&srcu_boot_list, struct srcu_struct,
1312 work.work.entry);
1313 check_init_srcu_struct(ssp);
1314 list_del_init(&ssp->work.work.entry);
1315 queue_work(rcu_gp_wq, &ssp->work.work);
1316 }
1317}
1318
1319#ifdef CONFIG_MODULES
1320
1321/* Initialize any global-scope srcu_struct structures used by this module. */
1322static int srcu_module_coming(struct module *mod)
1323{
1324 int i;
1325 struct srcu_struct **sspp = mod->srcu_struct_ptrs;
1326 int ret;
1327
1328 for (i = 0; i < mod->num_srcu_structs; i++) {
1329 ret = init_srcu_struct(*(sspp++));
1330 if (WARN_ON_ONCE(ret))
1331 return ret;
1332 }
1333 return 0;
1334}
1335
1336/* Clean up any global-scope srcu_struct structures used by this module. */
1337static void srcu_module_going(struct module *mod)
1338{
1339 int i;
1340 struct srcu_struct **sspp = mod->srcu_struct_ptrs;
1341
1342 for (i = 0; i < mod->num_srcu_structs; i++)
1343 cleanup_srcu_struct(*(sspp++));
1344}
1345
1346/* Handle one module, either coming or going. */
1347static int srcu_module_notify(struct notifier_block *self,
1348 unsigned long val, void *data)
1349{
1350 struct module *mod = data;
1351 int ret = 0;
1352
1353 switch (val) {
1354 case MODULE_STATE_COMING:
1355 ret = srcu_module_coming(mod);
1356 break;
1357 case MODULE_STATE_GOING:
1358 srcu_module_going(mod);
1359 break;
1360 default:
1361 break;
1362 }
1363 return ret;
1364}
1365
1366static struct notifier_block srcu_module_nb = {
1367 .notifier_call = srcu_module_notify,
1368 .priority = 0,
1369};
1370
1371static __init int init_srcu_module_notifier(void)
1372{
1373 int ret;
1374
1375 ret = register_module_notifier(&srcu_module_nb);
1376 if (ret)
1377 pr_warn("Failed to register srcu module notifier\n");
1378 return ret;
1379}
1380late_initcall(init_srcu_module_notifier);
1381
1382#endif /* #ifdef CONFIG_MODULES */