blob: b4123d7a2cec4f9010178d9b0049c2e43bbf833b [file] [log] [blame]
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001/*
2 * Sleepable Read-Copy Update mechanism for mutual exclusion.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * Copyright (C) IBM Corporation, 2006
19 * Copyright (C) Fujitsu, 2012
20 *
21 * Author: Paul McKenney <paulmck@us.ibm.com>
22 * Lai Jiangshan <laijs@cn.fujitsu.com>
23 *
24 * For detailed explanation of Read-Copy Update mechanism see -
25 * Documentation/RCU/ *.txt
26 *
27 */
28
29#include <linux/export.h>
30#include <linux/mutex.h>
31#include <linux/percpu.h>
32#include <linux/preempt.h>
33#include <linux/rcupdate_wait.h>
34#include <linux/sched.h>
35#include <linux/smp.h>
36#include <linux/delay.h>
Paul E. McKenney22607d62017-04-25 14:03:11 -070037#include <linux/module.h>
Paul E. McKenneydad81a22017-03-25 17:23:44 -070038#include <linux/srcu.h>
39
Paul E. McKenneydad81a22017-03-25 17:23:44 -070040#include "rcu.h"
Ingo Molnar45753c52017-05-02 10:31:18 +020041#include "rcu_segcblist.h"
Paul E. McKenneydad81a22017-03-25 17:23:44 -070042
Paul E. McKenney0c8e0e32017-04-28 11:24:22 -070043/* Holdoff in nanoseconds for auto-expediting. */
44#define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000)
45static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF;
Paul E. McKenney22607d62017-04-25 14:03:11 -070046module_param(exp_holdoff, ulong, 0444);
47
Paul E. McKenneyc350c002017-05-03 15:35:32 -070048/* Overflow-check frequency. N bits roughly says every 2**N grace periods. */
49static ulong counter_wrap_check = (ULONG_MAX >> 2);
50module_param(counter_wrap_check, ulong, 0444);
51
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070052static void srcu_invoke_callbacks(struct work_struct *work);
53static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay);
Paul E. McKenney0d8a1e82017-06-15 17:06:38 -070054static void process_srcu(struct work_struct *work);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070055
Paul E. McKenneyd6331982017-10-10 13:52:30 -070056/* Wrappers for lock acquisition and release, see raw_spin_lock_rcu_node(). */
57#define spin_lock_rcu_node(p) \
58do { \
59 spin_lock(&ACCESS_PRIVATE(p, lock)); \
60 smp_mb__after_unlock_lock(); \
61} while (0)
62
63#define spin_unlock_rcu_node(p) spin_unlock(&ACCESS_PRIVATE(p, lock))
64
65#define spin_lock_irq_rcu_node(p) \
66do { \
67 spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \
68 smp_mb__after_unlock_lock(); \
69} while (0)
70
71#define spin_unlock_irq_rcu_node(p) \
72 spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
73
74#define spin_lock_irqsave_rcu_node(p, flags) \
75do { \
76 spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
77 smp_mb__after_unlock_lock(); \
78} while (0)
79
80#define spin_unlock_irqrestore_rcu_node(p, flags) \
81 spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags) \
82
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070083/*
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[].
88 */
89static void init_srcu_struct_nodes(struct srcu_struct *sp, bool is_static)
Paul E. McKenneydad81a22017-03-25 17:23:44 -070090{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070091 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;
98
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);
104
105 /* Each pass through this loop initializes one srcu_node structure. */
106 rcu_for_each_node_breadth_first(sp, snp) {
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700107 spin_lock_init(&ACCESS_PRIVATE(snp, lock));
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700108 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++) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700111 snp->srcu_have_cbs[i] = 0;
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700112 snp->srcu_data_have_cbs[i] = 0;
113 }
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700114 snp->srcu_gp_seq_needed_exp = 0;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700115 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;
121 }
122
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];
129 }
130
131 /*
132 * Initialize the per-CPU srcu_data array, which feeds into the
133 * leaves of the srcu_node tree.
134 */
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);
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700141 spin_lock_init(&ACCESS_PRIVATE(sdp, lock));
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700142 rcu_segcblist_init(&sdp->srcu_cblist);
143 sdp->srcu_cblist_invoking = false;
144 sdp->srcu_gp_seq_needed = sp->srcu_gp_seq;
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700145 sdp->srcu_gp_seq_needed_exp = sp->srcu_gp_seq;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700146 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;
151 }
152 sdp->cpu = cpu;
153 INIT_DELAYED_WORK(&sdp->work, srcu_invoke_callbacks);
154 sdp->sp = sp;
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700155 sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700156 if (is_static)
157 continue;
158
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;
163 }
164 }
165}
166
167/*
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.
172 */
173static int init_srcu_struct_fields(struct srcu_struct *sp, bool is_static)
174{
175 mutex_init(&sp->srcu_cb_mutex);
176 mutex_init(&sp->srcu_gp_mutex);
177 sp->srcu_idx = 0;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700178 sp->srcu_gp_seq = 0;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700179 sp->srcu_barrier_seq = 0;
180 mutex_init(&sp->srcu_barrier_mutex);
181 atomic_set(&sp->srcu_barrier_cpu_cnt, 0);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700182 INIT_DELAYED_WORK(&sp->work, process_srcu);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700183 if (!is_static)
184 sp->sda = alloc_percpu(struct srcu_data);
185 init_srcu_struct_nodes(sp, is_static);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700186 sp->srcu_gp_seq_needed_exp = 0;
Paul E. McKenney22607d62017-04-25 14:03:11 -0700187 sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700188 smp_store_release(&sp->srcu_gp_seq_needed, 0); /* Init done. */
189 return sp->sda ? 0 : -ENOMEM;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700190}
191
192#ifdef CONFIG_DEBUG_LOCK_ALLOC
193
194int __init_srcu_struct(struct srcu_struct *sp, const char *name,
195 struct lock_class_key *key)
196{
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);
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700200 spin_lock_init(&ACCESS_PRIVATE(sp, lock));
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700201 return init_srcu_struct_fields(sp, false);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700202}
203EXPORT_SYMBOL_GPL(__init_srcu_struct);
204
205#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
206
207/**
208 * init_srcu_struct - initialize a sleep-RCU structure
209 * @sp: structure to initialize.
210 *
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.
214 */
215int init_srcu_struct(struct srcu_struct *sp)
216{
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700217 spin_lock_init(&ACCESS_PRIVATE(sp, lock));
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700218 return init_srcu_struct_fields(sp, false);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700219}
220EXPORT_SYMBOL_GPL(init_srcu_struct);
221
222#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
223
224/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700225 * 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
Paul E. McKenneya3883df2017-05-09 15:00:14 -0700228 * to each update-side SRCU primitive. Use sp->lock, which -is-
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700229 * compile-time initialized, to resolve races involving multiple
230 * CPUs trying to garner first-use privileges.
231 */
232static void check_init_srcu_struct(struct srcu_struct *sp)
233{
234 unsigned long flags;
235
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. */
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700240 spin_lock_irqsave_rcu_node(sp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700241 if (!rcu_seq_state(sp->srcu_gp_seq_needed)) {
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700242 spin_unlock_irqrestore_rcu_node(sp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700243 return;
244 }
245 init_srcu_struct_fields(sp, true);
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700246 spin_unlock_irqrestore_rcu_node(sp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700247}
248
249/*
250 * Returns approximate total of the readers' ->srcu_lock_count[] values
251 * for the rank of per-CPU counters specified by idx.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700252 */
253static unsigned long srcu_readers_lock_idx(struct srcu_struct *sp, int idx)
254{
255 int cpu;
256 unsigned long sum = 0;
257
258 for_each_possible_cpu(cpu) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700259 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700260
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700261 sum += READ_ONCE(cpuc->srcu_lock_count[idx]);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700262 }
263 return sum;
264}
265
266/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700267 * Returns approximate total of the readers' ->srcu_unlock_count[] values
268 * for the rank of per-CPU counters specified by idx.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700269 */
270static unsigned long srcu_readers_unlock_idx(struct srcu_struct *sp, int idx)
271{
272 int cpu;
273 unsigned long sum = 0;
274
275 for_each_possible_cpu(cpu) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700276 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700277
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700278 sum += READ_ONCE(cpuc->srcu_unlock_count[idx]);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700279 }
280 return sum;
281}
282
283/*
284 * Return true if the number of pre-existing readers is determined to
285 * be zero.
286 */
287static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
288{
289 unsigned long unlocks;
290
291 unlocks = srcu_readers_unlock_idx(sp, idx);
292
293 /*
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.
299 *
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.
303 */
304 smp_mb(); /* A */
305
306 /*
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.
311 *
Paul E. McKenney881ec9d2017-04-12 15:16:50 -0700312 * 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.
322 *
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.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700326 */
327 return srcu_readers_lock_idx(sp, idx) == unlocks;
328}
329
330/**
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).
334 *
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.
338 */
339static bool srcu_readers_active(struct srcu_struct *sp)
340{
341 int cpu;
342 unsigned long sum = 0;
343
344 for_each_possible_cpu(cpu) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700345 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700346
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700347 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]);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700351 }
352 return sum;
353}
354
355#define SRCU_INTERVAL 1
356
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700357/*
358 * Return grace-period delay, zero if there are expedited grace
359 * periods pending, SRCU_INTERVAL otherwise.
360 */
361static unsigned long srcu_get_delay(struct srcu_struct *sp)
362{
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;
367}
368
Paul E. McKenneyf7194ac2018-04-05 17:19:17 -0700369/* Helper for cleanup_srcu_struct() and cleanup_srcu_struct_quiesced(). */
370void _cleanup_srcu_struct(struct srcu_struct *sp, bool quiesced)
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700371{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700372 int cpu;
373
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700374 if (WARN_ON(!srcu_get_delay(sp)))
Paul E. McKenneyf7194ac2018-04-05 17:19:17 -0700375 return; /* Just leak it! */
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700376 if (WARN_ON(srcu_readers_active(sp)))
Paul E. McKenneyf7194ac2018-04-05 17:19:17 -0700377 return; /* Just leak it! */
378 if (quiesced) {
379 if (WARN_ON(delayed_work_pending(&sp->work)))
380 return; /* Just leak it! */
381 } else {
382 flush_delayed_work(&sp->work);
383 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700384 for_each_possible_cpu(cpu)
Paul E. McKenneyf7194ac2018-04-05 17:19:17 -0700385 if (quiesced) {
386 if (WARN_ON(delayed_work_pending(&per_cpu_ptr(sp->sda, cpu)->work)))
387 return; /* Just leak it! */
388 } else {
389 flush_delayed_work(&per_cpu_ptr(sp->sda, cpu)->work);
390 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700391 if (WARN_ON(rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
392 WARN_ON(srcu_readers_active(sp))) {
Paul E. McKenneycb4081c2017-12-01 15:28:11 -0800393 pr_info("%s: Active srcu_struct %p state: %d\n", __func__, sp, rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)));
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700394 return; /* Caller forgot to stop doing call_srcu()? */
395 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700396 free_percpu(sp->sda);
397 sp->sda = NULL;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700398}
Paul E. McKenneyf7194ac2018-04-05 17:19:17 -0700399EXPORT_SYMBOL_GPL(_cleanup_srcu_struct);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700400
401/*
402 * Counts the new reader in the appropriate per-CPU element of the
Paolo Bonzinicdf7abc2017-05-31 14:03:10 +0200403 * srcu_struct.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700404 * Returns an index that must be passed to the matching srcu_read_unlock().
405 */
406int __srcu_read_lock(struct srcu_struct *sp)
407{
408 int idx;
409
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700410 idx = READ_ONCE(sp->srcu_idx) & 0x1;
Paolo Bonzinicdf7abc2017-05-31 14:03:10 +0200411 this_cpu_inc(sp->sda->srcu_lock_count[idx]);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700412 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().
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700421 */
422void __srcu_read_unlock(struct srcu_struct *sp, int idx)
423{
424 smp_mb(); /* C */ /* Avoid leaking the critical section. */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700425 this_cpu_inc(sp->sda->srcu_unlock_count[idx]);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700426}
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 */
441static void srcu_gp_start(struct srcu_struct *sp)
442{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700443 struct srcu_data *sdp = this_cpu_ptr(sp->sda);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700444 int state;
445
Matthew Wilcoxa32e01e2018-01-17 06:24:30 -0800446 lockdep_assert_held(&ACCESS_PRIVATE(sp, lock));
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700447 WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
448 rcu_segcblist_advance(&sdp->srcu_cblist,
449 rcu_seq_current(&sp->srcu_gp_seq));
450 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
451 rcu_seq_snap(&sp->srcu_gp_seq));
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700452 smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700453 rcu_seq_start(&sp->srcu_gp_seq);
454 state = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
455 WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
456}
457
458/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700459 * Track online CPUs to guide callback workqueue placement.
460 */
461DEFINE_PER_CPU(bool, srcu_online);
462
463void srcu_online_cpu(unsigned int cpu)
464{
465 WRITE_ONCE(per_cpu(srcu_online, cpu), true);
466}
467
468void srcu_offline_cpu(unsigned int cpu)
469{
470 WRITE_ONCE(per_cpu(srcu_online, cpu), false);
471}
472
473/*
474 * Place the workqueue handler on the specified CPU if online, otherwise
475 * just run it whereever. This is useful for placing workqueue handlers
476 * that are to invoke the specified CPU's callbacks.
477 */
478static bool srcu_queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
479 struct delayed_work *dwork,
480 unsigned long delay)
481{
482 bool ret;
483
484 preempt_disable();
485 if (READ_ONCE(per_cpu(srcu_online, cpu)))
486 ret = queue_delayed_work_on(cpu, wq, dwork, delay);
487 else
488 ret = queue_delayed_work(wq, dwork, delay);
489 preempt_enable();
490 return ret;
491}
492
493/*
494 * Schedule callback invocation for the specified srcu_data structure,
495 * if possible, on the corresponding CPU.
496 */
497static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
498{
Paul E. McKenneyad7c9462018-01-08 14:35:52 -0800499 srcu_queue_delayed_work_on(sdp->cpu, rcu_gp_wq, &sdp->work, delay);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700500}
501
502/*
503 * Schedule callback invocation for all srcu_data structures associated
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700504 * with the specified srcu_node structure that have callbacks for the
505 * just-completed grace period, the one corresponding to idx. If possible,
506 * schedule this invocation on the corresponding CPUs.
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700507 */
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700508static void srcu_schedule_cbs_snp(struct srcu_struct *sp, struct srcu_node *snp,
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700509 unsigned long mask, unsigned long delay)
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700510{
511 int cpu;
512
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700513 for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
514 if (!(mask & (1 << (cpu - snp->grplo))))
515 continue;
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700516 srcu_schedule_cbs_sdp(per_cpu_ptr(sp->sda, cpu), delay);
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700517 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700518}
519
520/*
521 * Note the end of an SRCU grace period. Initiates callback invocation
522 * and starts a new grace period if needed.
523 *
524 * The ->srcu_cb_mutex acquisition does not protect any data, but
525 * instead prevents more than one grace period from starting while we
526 * are initiating callback invocation. This allows the ->srcu_have_cbs[]
527 * array to have a finite number of elements.
528 */
529static void srcu_gp_end(struct srcu_struct *sp)
530{
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700531 unsigned long cbdelay;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700532 bool cbs;
Ildar Ismagilov8ddbd882018-01-31 22:42:21 +0300533 bool last_lvl;
Paul E. McKenneyc350c002017-05-03 15:35:32 -0700534 int cpu;
535 unsigned long flags;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700536 unsigned long gpseq;
537 int idx;
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700538 unsigned long mask;
Paul E. McKenneyc350c002017-05-03 15:35:32 -0700539 struct srcu_data *sdp;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700540 struct srcu_node *snp;
541
542 /* Prevent more than one additional grace period. */
543 mutex_lock(&sp->srcu_cb_mutex);
544
545 /* End the current grace period. */
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700546 spin_lock_irq_rcu_node(sp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700547 idx = rcu_seq_state(sp->srcu_gp_seq);
548 WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700549 cbdelay = srcu_get_delay(sp);
Paul E. McKenney22607d62017-04-25 14:03:11 -0700550 sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700551 rcu_seq_end(&sp->srcu_gp_seq);
552 gpseq = rcu_seq_current(&sp->srcu_gp_seq);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700553 if (ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, gpseq))
554 sp->srcu_gp_seq_needed_exp = gpseq;
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700555 spin_unlock_irq_rcu_node(sp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700556 mutex_unlock(&sp->srcu_gp_mutex);
557 /* A new grace period can start at this point. But only one. */
558
559 /* Initiate callback invocation as needed. */
560 idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700561 rcu_for_each_node_breadth_first(sp, snp) {
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700562 spin_lock_irq_rcu_node(snp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700563 cbs = false;
Ildar Ismagilov8ddbd882018-01-31 22:42:21 +0300564 last_lvl = snp >= sp->level[rcu_num_lvls - 1];
565 if (last_lvl)
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700566 cbs = snp->srcu_have_cbs[idx] == gpseq;
567 snp->srcu_have_cbs[idx] = gpseq;
568 rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700569 if (ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, gpseq))
570 snp->srcu_gp_seq_needed_exp = gpseq;
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700571 mask = snp->srcu_data_have_cbs[idx];
572 snp->srcu_data_have_cbs[idx] = 0;
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700573 spin_unlock_irq_rcu_node(snp);
Paul E. McKenneya3883df2017-05-09 15:00:14 -0700574 if (cbs)
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700575 srcu_schedule_cbs_snp(sp, snp, mask, cbdelay);
Paul E. McKenneyc350c002017-05-03 15:35:32 -0700576
577 /* Occasionally prevent srcu_data counter wrap. */
Ildar Ismagilov8ddbd882018-01-31 22:42:21 +0300578 if (!(gpseq & counter_wrap_check) && last_lvl)
Paul E. McKenneyc350c002017-05-03 15:35:32 -0700579 for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
580 sdp = per_cpu_ptr(sp->sda, cpu);
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700581 spin_lock_irqsave_rcu_node(sdp, flags);
Paul E. McKenneyc350c002017-05-03 15:35:32 -0700582 if (ULONG_CMP_GE(gpseq,
583 sdp->srcu_gp_seq_needed + 100))
584 sdp->srcu_gp_seq_needed = gpseq;
Ildar Ismagilova35d13e2018-01-31 22:39:53 +0300585 if (ULONG_CMP_GE(gpseq,
586 sdp->srcu_gp_seq_needed_exp + 100))
587 sdp->srcu_gp_seq_needed_exp = gpseq;
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700588 spin_unlock_irqrestore_rcu_node(sdp, flags);
Paul E. McKenneyc350c002017-05-03 15:35:32 -0700589 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700590 }
591
592 /* Callback initiation done, allow grace periods after next. */
593 mutex_unlock(&sp->srcu_cb_mutex);
594
595 /* Start a new grace period if needed. */
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700596 spin_lock_irq_rcu_node(sp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700597 gpseq = rcu_seq_current(&sp->srcu_gp_seq);
598 if (!rcu_seq_state(gpseq) &&
599 ULONG_CMP_LT(gpseq, sp->srcu_gp_seq_needed)) {
600 srcu_gp_start(sp);
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700601 spin_unlock_irq_rcu_node(sp);
Paul E. McKenney6308f342018-02-14 14:20:45 -0800602 srcu_reschedule(sp, 0);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700603 } else {
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700604 spin_unlock_irq_rcu_node(sp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700605 }
606}
607
608/*
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700609 * Funnel-locking scheme to scalably mediate many concurrent expedited
610 * grace-period requests. This function is invoked for the first known
611 * expedited request for a grace period that has already been requested,
612 * but without expediting. To start a completely new grace period,
613 * whether expedited or not, use srcu_funnel_gp_start() instead.
614 */
615static void srcu_funnel_exp_start(struct srcu_struct *sp, struct srcu_node *snp,
616 unsigned long s)
617{
618 unsigned long flags;
619
620 for (; snp != NULL; snp = snp->srcu_parent) {
621 if (rcu_seq_done(&sp->srcu_gp_seq, s) ||
622 ULONG_CMP_GE(READ_ONCE(snp->srcu_gp_seq_needed_exp), s))
623 return;
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700624 spin_lock_irqsave_rcu_node(snp, flags);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700625 if (ULONG_CMP_GE(snp->srcu_gp_seq_needed_exp, s)) {
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700626 spin_unlock_irqrestore_rcu_node(snp, flags);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700627 return;
628 }
629 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700630 spin_unlock_irqrestore_rcu_node(snp, flags);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700631 }
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700632 spin_lock_irqsave_rcu_node(sp, flags);
Ildar Ismagilov274afd62018-01-30 15:40:16 -0800633 if (ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700634 sp->srcu_gp_seq_needed_exp = s;
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700635 spin_unlock_irqrestore_rcu_node(sp, flags);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700636}
637
638/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700639 * Funnel-locking scheme to scalably mediate many concurrent grace-period
640 * requests. The winner has to do the work of actually starting grace
641 * period s. Losers must either ensure that their desired grace-period
642 * number is recorded on at least their leaf srcu_node structure, or they
643 * must take steps to invoke their own callbacks.
644 */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700645static void srcu_funnel_gp_start(struct srcu_struct *sp, struct srcu_data *sdp,
646 unsigned long s, bool do_norm)
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700647{
648 unsigned long flags;
649 int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
650 struct srcu_node *snp = sdp->mynode;
651 unsigned long snp_seq;
652
653 /* Each pass through the loop does one level of the srcu_node tree. */
654 for (; snp != NULL; snp = snp->srcu_parent) {
655 if (rcu_seq_done(&sp->srcu_gp_seq, s) && snp != sdp->mynode)
656 return; /* GP already done and CBs recorded. */
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700657 spin_lock_irqsave_rcu_node(snp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700658 if (ULONG_CMP_GE(snp->srcu_have_cbs[idx], s)) {
659 snp_seq = snp->srcu_have_cbs[idx];
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700660 if (snp == sdp->mynode && snp_seq == s)
661 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700662 spin_unlock_irqrestore_rcu_node(snp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700663 if (snp == sdp->mynode && snp_seq != s) {
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700664 srcu_schedule_cbs_sdp(sdp, do_norm
665 ? SRCU_INTERVAL
666 : 0);
667 return;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700668 }
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700669 if (!do_norm)
670 srcu_funnel_exp_start(sp, snp, s);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700671 return;
672 }
673 snp->srcu_have_cbs[idx] = s;
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700674 if (snp == sdp->mynode)
675 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700676 if (!do_norm && ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, s))
677 snp->srcu_gp_seq_needed_exp = s;
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700678 spin_unlock_irqrestore_rcu_node(snp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700679 }
680
681 /* Top of tree, must ensure the grace period will be started. */
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700682 spin_lock_irqsave_rcu_node(sp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700683 if (ULONG_CMP_LT(sp->srcu_gp_seq_needed, s)) {
684 /*
685 * Record need for grace period s. Pair with load
686 * acquire setting up for initialization.
687 */
688 smp_store_release(&sp->srcu_gp_seq_needed, s); /*^^^*/
689 }
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700690 if (!do_norm && ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
691 sp->srcu_gp_seq_needed_exp = s;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700692
693 /* If grace period not already done and none in progress, start it. */
694 if (!rcu_seq_done(&sp->srcu_gp_seq, s) &&
695 rcu_seq_state(sp->srcu_gp_seq) == SRCU_STATE_IDLE) {
696 WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
697 srcu_gp_start(sp);
Paul E. McKenneyad7c9462018-01-08 14:35:52 -0800698 queue_delayed_work(rcu_gp_wq, &sp->work, srcu_get_delay(sp));
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700699 }
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700700 spin_unlock_irqrestore_rcu_node(sp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700701}
702
703/*
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700704 * Wait until all readers counted by array index idx complete, but
705 * loop an additional time if there is an expedited grace period pending.
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700706 * The caller must ensure that ->srcu_idx is not changed while checking.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700707 */
708static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
709{
710 for (;;) {
711 if (srcu_readers_active_idx_check(sp, idx))
712 return true;
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700713 if (--trycount + !srcu_get_delay(sp) <= 0)
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700714 return false;
715 udelay(SRCU_RETRY_CHECK_DELAY);
716 }
717}
718
719/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700720 * Increment the ->srcu_idx counter so that future SRCU readers will
721 * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700722 * us to wait for pre-existing readers in a starvation-free manner.
723 */
724static void srcu_flip(struct srcu_struct *sp)
725{
Paul E. McKenney881ec9d2017-04-12 15:16:50 -0700726 /*
727 * Ensure that if this updater saw a given reader's increment
728 * from __srcu_read_lock(), that reader was using an old value
729 * of ->srcu_idx. Also ensure that if a given reader sees the
730 * new value of ->srcu_idx, this updater's earlier scans cannot
731 * have seen that reader's increments (which is OK, because this
732 * grace period need not wait on that reader).
733 */
734 smp_mb(); /* E */ /* Pairs with B and C. */
735
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700736 WRITE_ONCE(sp->srcu_idx, sp->srcu_idx + 1);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700737
738 /*
739 * Ensure that if the updater misses an __srcu_read_unlock()
740 * increment, that task's next __srcu_read_lock() will see the
741 * above counter update. Note that both this memory barrier
742 * and the one in srcu_readers_active_idx_check() provide the
743 * guarantee for __srcu_read_lock().
744 */
745 smp_mb(); /* D */ /* Pairs with C. */
746}
747
748/*
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700749 * If SRCU is likely idle, return true, otherwise return false.
750 *
751 * Note that it is OK for several current from-idle requests for a new
752 * grace period from idle to specify expediting because they will all end
753 * up requesting the same grace period anyhow. So no loss.
754 *
755 * Note also that if any CPU (including the current one) is still invoking
756 * callbacks, this function will nevertheless say "idle". This is not
757 * ideal, but the overhead of checking all CPUs' callback lists is even
758 * less ideal, especially on large systems. Furthermore, the wakeup
759 * can happen before the callback is fully removed, so we have no choice
760 * but to accept this type of error.
761 *
762 * This function is also subject to counter-wrap errors, but let's face
763 * it, if this function was preempted for enough time for the counters
764 * to wrap, it really doesn't matter whether or not we expedite the grace
765 * period. The extra overhead of a needlessly expedited grace period is
766 * negligible when amoritized over that time period, and the extra latency
767 * of a needlessly non-expedited grace period is similarly negligible.
768 */
769static bool srcu_might_be_idle(struct srcu_struct *sp)
770{
Paul E. McKenney22607d62017-04-25 14:03:11 -0700771 unsigned long curseq;
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700772 unsigned long flags;
773 struct srcu_data *sdp;
Paul E. McKenney22607d62017-04-25 14:03:11 -0700774 unsigned long t;
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700775
776 /* If the local srcu_data structure has callbacks, not idle. */
777 local_irq_save(flags);
778 sdp = this_cpu_ptr(sp->sda);
779 if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
780 local_irq_restore(flags);
781 return false; /* Callbacks already present, so not idle. */
782 }
783 local_irq_restore(flags);
784
785 /*
786 * No local callbacks, so probabalistically probe global state.
787 * Exact information would require acquiring locks, which would
788 * kill scalability, hence the probabalistic nature of the probe.
789 */
Paul E. McKenney22607d62017-04-25 14:03:11 -0700790
791 /* First, see if enough time has passed since the last GP. */
792 t = ktime_get_mono_fast_ns();
793 if (exp_holdoff == 0 ||
794 time_in_range_open(t, sp->srcu_last_gp_end,
795 sp->srcu_last_gp_end + exp_holdoff))
796 return false; /* Too soon after last GP. */
797
798 /* Next, check for probable idleness. */
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700799 curseq = rcu_seq_current(&sp->srcu_gp_seq);
800 smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
801 if (ULONG_CMP_LT(curseq, READ_ONCE(sp->srcu_gp_seq_needed)))
802 return false; /* Grace period in progress, so not idle. */
803 smp_mb(); /* Order ->srcu_gp_seq with prior access. */
804 if (curseq != rcu_seq_current(&sp->srcu_gp_seq))
805 return false; /* GP # changed, so not idle. */
806 return true; /* With reasonable probability, idle! */
807}
808
809/*
Paul E. McKenneya6025382017-04-28 15:39:34 -0700810 * SRCU callback function to leak a callback.
811 */
812static void srcu_leak_callback(struct rcu_head *rhp)
813{
814}
815
816/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700817 * Enqueue an SRCU callback on the srcu_data structure associated with
818 * the current CPU and the specified srcu_struct structure, initiating
819 * grace-period processing if it is not already running.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700820 *
821 * Note that all CPUs must agree that the grace period extended beyond
822 * all pre-existing SRCU read-side critical section. On systems with
823 * more than one CPU, this means that when "func()" is invoked, each CPU
824 * is guaranteed to have executed a full memory barrier since the end of
825 * its last corresponding SRCU read-side critical section whose beginning
826 * preceded the call to call_rcu(). It also means that each CPU executing
827 * an SRCU read-side critical section that continues beyond the start of
828 * "func()" must have executed a memory barrier after the call_rcu()
829 * but before the beginning of that SRCU read-side critical section.
830 * Note that these guarantees include CPUs that are offline, idle, or
831 * executing in user mode, as well as CPUs that are executing in the kernel.
832 *
833 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
834 * resulting SRCU callback function "func()", then both CPU A and CPU
835 * B are guaranteed to execute a full memory barrier during the time
836 * interval between the call to call_rcu() and the invocation of "func()".
837 * This guarantee applies even if CPU A and CPU B are the same CPU (but
838 * again only if the system has more than one CPU).
839 *
840 * Of course, these guarantees apply only for invocations of call_srcu(),
841 * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
842 * srcu_struct structure.
843 */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700844void __call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
845 rcu_callback_t func, bool do_norm)
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700846{
847 unsigned long flags;
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700848 bool needexp = false;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700849 bool needgp = false;
850 unsigned long s;
851 struct srcu_data *sdp;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700852
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700853 check_init_srcu_struct(sp);
Paul E. McKenneya6025382017-04-28 15:39:34 -0700854 if (debug_rcu_head_queue(rhp)) {
855 /* Probable double call_srcu(), so leak the callback. */
856 WRITE_ONCE(rhp->func, srcu_leak_callback);
857 WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n");
858 return;
859 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700860 rhp->func = func;
861 local_irq_save(flags);
862 sdp = this_cpu_ptr(sp->sda);
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700863 spin_lock_rcu_node(sdp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700864 rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp, false);
865 rcu_segcblist_advance(&sdp->srcu_cblist,
866 rcu_seq_current(&sp->srcu_gp_seq));
867 s = rcu_seq_snap(&sp->srcu_gp_seq);
868 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s);
869 if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
870 sdp->srcu_gp_seq_needed = s;
871 needgp = true;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700872 }
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700873 if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
874 sdp->srcu_gp_seq_needed_exp = s;
875 needexp = true;
876 }
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700877 spin_unlock_irqrestore_rcu_node(sdp, flags);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700878 if (needgp)
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700879 srcu_funnel_gp_start(sp, sdp, s, do_norm);
880 else if (needexp)
881 srcu_funnel_exp_start(sp, sdp->mynode, s);
882}
883
Paul E. McKenney5a0465e2017-05-04 11:31:04 -0700884/**
885 * call_srcu() - Queue a callback for invocation after an SRCU grace period
886 * @sp: srcu_struct in queue the callback
Paul E. McKenney27fdb352017-10-19 14:26:21 -0700887 * @rhp: structure to be used for queueing the SRCU callback.
Paul E. McKenney5a0465e2017-05-04 11:31:04 -0700888 * @func: function to be invoked after the SRCU grace period
889 *
890 * The callback function will be invoked some time after a full SRCU
891 * grace period elapses, in other words after all pre-existing SRCU
892 * read-side critical sections have completed. However, the callback
893 * function might well execute concurrently with other SRCU read-side
894 * critical sections that started after call_srcu() was invoked. SRCU
895 * read-side critical sections are delimited by srcu_read_lock() and
896 * srcu_read_unlock(), and may be nested.
897 *
898 * The callback will be invoked from process context, but must nevertheless
899 * be fast and must not block.
900 */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700901void call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
902 rcu_callback_t func)
903{
904 __call_srcu(sp, rhp, func, true);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700905}
906EXPORT_SYMBOL_GPL(call_srcu);
907
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700908/*
909 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
910 */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700911static void __synchronize_srcu(struct srcu_struct *sp, bool do_norm)
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700912{
913 struct rcu_synchronize rcu;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700914
915 RCU_LOCKDEP_WARN(lock_is_held(&sp->dep_map) ||
916 lock_is_held(&rcu_bh_lock_map) ||
917 lock_is_held(&rcu_lock_map) ||
918 lock_is_held(&rcu_sched_lock_map),
919 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
920
921 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
922 return;
923 might_sleep();
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700924 check_init_srcu_struct(sp);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700925 init_completion(&rcu.completion);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700926 init_rcu_head_on_stack(&rcu.head);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700927 __call_srcu(sp, &rcu.head, wakeme_after_rcu, do_norm);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700928 wait_for_completion(&rcu.completion);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700929 destroy_rcu_head_on_stack(&rcu.head);
Paul E. McKenney35732cf2017-07-05 13:30:21 -0700930
931 /*
932 * Make sure that later code is ordered after the SRCU grace
Paul E. McKenneyd6331982017-10-10 13:52:30 -0700933 * period. This pairs with the spin_lock_irq_rcu_node()
Paul E. McKenney35732cf2017-07-05 13:30:21 -0700934 * in srcu_invoke_callbacks(). Unlike Tree RCU, this is needed
935 * because the current CPU might have been totally uninvolved with
936 * (and thus unordered against) that grace period.
937 */
938 smp_mb();
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700939}
940
941/**
942 * synchronize_srcu_expedited - Brute-force SRCU grace period
943 * @sp: srcu_struct with which to synchronize.
944 *
945 * Wait for an SRCU grace period to elapse, but be more aggressive about
946 * spinning rather than blocking when waiting.
947 *
948 * Note that synchronize_srcu_expedited() has the same deadlock and
949 * memory-ordering properties as does synchronize_srcu().
950 */
951void synchronize_srcu_expedited(struct srcu_struct *sp)
952{
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700953 __synchronize_srcu(sp, rcu_gp_is_normal());
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700954}
955EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
956
957/**
958 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
959 * @sp: srcu_struct with which to synchronize.
960 *
961 * Wait for the count to drain to zero of both indexes. To avoid the
962 * possible starvation of synchronize_srcu(), it waits for the count of
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700963 * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
964 * and then flip the srcu_idx and wait for the count of the other index.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700965 *
966 * Can block; must be called from process context.
967 *
968 * Note that it is illegal to call synchronize_srcu() from the corresponding
969 * SRCU read-side critical section; doing so will result in deadlock.
970 * However, it is perfectly legal to call synchronize_srcu() on one
971 * srcu_struct from some other srcu_struct's read-side critical section,
972 * as long as the resulting graph of srcu_structs is acyclic.
973 *
974 * There are memory-ordering constraints implied by synchronize_srcu().
975 * On systems with more than one CPU, when synchronize_srcu() returns,
976 * each CPU is guaranteed to have executed a full memory barrier since
977 * the end of its last corresponding SRCU-sched read-side critical section
978 * whose beginning preceded the call to synchronize_srcu(). In addition,
979 * each CPU having an SRCU read-side critical section that extends beyond
980 * the return from synchronize_srcu() is guaranteed to have executed a
981 * full memory barrier after the beginning of synchronize_srcu() and before
982 * the beginning of that SRCU read-side critical section. Note that these
983 * guarantees include CPUs that are offline, idle, or executing in user mode,
984 * as well as CPUs that are executing in the kernel.
985 *
986 * Furthermore, if CPU A invoked synchronize_srcu(), which returned
987 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
988 * to have executed a full memory barrier during the execution of
989 * synchronize_srcu(). This guarantee applies even if CPU A and CPU B
990 * are the same CPU, but again only if the system has more than one CPU.
991 *
992 * Of course, these memory-ordering guarantees apply only when
993 * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
994 * passed the same srcu_struct structure.
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700995 *
996 * If SRCU is likely idle, expedite the first request. This semantic
997 * was provided by Classic SRCU, and is relied upon by its users, so TREE
998 * SRCU must also provide it. Note that detecting idleness is heuristic
999 * and subject to both false positives and negatives.
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001000 */
1001void synchronize_srcu(struct srcu_struct *sp)
1002{
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -07001003 if (srcu_might_be_idle(sp) || rcu_gp_is_expedited())
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001004 synchronize_srcu_expedited(sp);
1005 else
Paul E. McKenney1e9a0382017-04-24 16:02:09 -07001006 __synchronize_srcu(sp, true);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001007}
1008EXPORT_SYMBOL_GPL(synchronize_srcu);
1009
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001010/*
1011 * Callback function for srcu_barrier() use.
1012 */
1013static void srcu_barrier_cb(struct rcu_head *rhp)
1014{
1015 struct srcu_data *sdp;
1016 struct srcu_struct *sp;
1017
1018 sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
1019 sp = sdp->sp;
1020 if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
1021 complete(&sp->srcu_barrier_completion);
1022}
1023
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001024/**
1025 * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
1026 * @sp: srcu_struct on which to wait for in-flight callbacks.
1027 */
1028void srcu_barrier(struct srcu_struct *sp)
1029{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001030 int cpu;
1031 struct srcu_data *sdp;
1032 unsigned long s = rcu_seq_snap(&sp->srcu_barrier_seq);
1033
1034 check_init_srcu_struct(sp);
1035 mutex_lock(&sp->srcu_barrier_mutex);
1036 if (rcu_seq_done(&sp->srcu_barrier_seq, s)) {
1037 smp_mb(); /* Force ordering following return. */
1038 mutex_unlock(&sp->srcu_barrier_mutex);
1039 return; /* Someone else did our work for us. */
1040 }
1041 rcu_seq_start(&sp->srcu_barrier_seq);
1042 init_completion(&sp->srcu_barrier_completion);
1043
1044 /* Initial count prevents reaching zero until all CBs are posted. */
1045 atomic_set(&sp->srcu_barrier_cpu_cnt, 1);
1046
1047 /*
1048 * Each pass through this loop enqueues a callback, but only
1049 * on CPUs already having callbacks enqueued. Note that if
1050 * a CPU already has callbacks enqueue, it must have already
1051 * registered the need for a future grace period, so all we
1052 * need do is enqueue a callback that will use the same
1053 * grace period as the last callback already in the queue.
1054 */
1055 for_each_possible_cpu(cpu) {
1056 sdp = per_cpu_ptr(sp->sda, cpu);
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001057 spin_lock_irq_rcu_node(sdp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001058 atomic_inc(&sp->srcu_barrier_cpu_cnt);
1059 sdp->srcu_barrier_head.func = srcu_barrier_cb;
Paul E. McKenneya6025382017-04-28 15:39:34 -07001060 debug_rcu_head_queue(&sdp->srcu_barrier_head);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001061 if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
Paul E. McKenneya6025382017-04-28 15:39:34 -07001062 &sdp->srcu_barrier_head, 0)) {
1063 debug_rcu_head_unqueue(&sdp->srcu_barrier_head);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001064 atomic_dec(&sp->srcu_barrier_cpu_cnt);
Paul E. McKenneya6025382017-04-28 15:39:34 -07001065 }
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001066 spin_unlock_irq_rcu_node(sdp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001067 }
1068
1069 /* Remove the initial count, at which point reaching zero can happen. */
1070 if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
1071 complete(&sp->srcu_barrier_completion);
1072 wait_for_completion(&sp->srcu_barrier_completion);
1073
1074 rcu_seq_end(&sp->srcu_barrier_seq);
1075 mutex_unlock(&sp->srcu_barrier_mutex);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001076}
1077EXPORT_SYMBOL_GPL(srcu_barrier);
1078
1079/**
1080 * srcu_batches_completed - return batches completed.
1081 * @sp: srcu_struct on which to report batch completion.
1082 *
1083 * Report the number of batches, correlated with, but not necessarily
1084 * precisely the same as, the number of grace periods that have elapsed.
1085 */
1086unsigned long srcu_batches_completed(struct srcu_struct *sp)
1087{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001088 return sp->srcu_idx;
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001089}
1090EXPORT_SYMBOL_GPL(srcu_batches_completed);
1091
1092/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001093 * Core SRCU state machine. Push state bits of ->srcu_gp_seq
1094 * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
1095 * completed in that state.
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001096 */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001097static void srcu_advance_state(struct srcu_struct *sp)
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001098{
1099 int idx;
1100
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001101 mutex_lock(&sp->srcu_gp_mutex);
1102
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001103 /*
1104 * Because readers might be delayed for an extended period after
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001105 * fetching ->srcu_idx for their index, at any point in time there
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001106 * might well be readers using both idx=0 and idx=1. We therefore
1107 * need to wait for readers to clear from both index values before
1108 * invoking a callback.
1109 *
1110 * The load-acquire ensures that we see the accesses performed
1111 * by the prior grace period.
1112 */
1113 idx = rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq)); /* ^^^ */
1114 if (idx == SRCU_STATE_IDLE) {
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001115 spin_lock_irq_rcu_node(sp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001116 if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
1117 WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq));
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001118 spin_unlock_irq_rcu_node(sp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001119 mutex_unlock(&sp->srcu_gp_mutex);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001120 return;
1121 }
1122 idx = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
1123 if (idx == SRCU_STATE_IDLE)
1124 srcu_gp_start(sp);
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001125 spin_unlock_irq_rcu_node(sp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001126 if (idx != SRCU_STATE_IDLE) {
1127 mutex_unlock(&sp->srcu_gp_mutex);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001128 return; /* Someone else started the grace period. */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001129 }
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001130 }
1131
1132 if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001133 idx = 1 ^ (sp->srcu_idx & 1);
1134 if (!try_check_zero(sp, idx, 1)) {
1135 mutex_unlock(&sp->srcu_gp_mutex);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001136 return; /* readers present, retry later. */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001137 }
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001138 srcu_flip(sp);
1139 rcu_seq_set_state(&sp->srcu_gp_seq, SRCU_STATE_SCAN2);
1140 }
1141
1142 if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
1143
1144 /*
1145 * SRCU read-side critical sections are normally short,
1146 * so check at least twice in quick succession after a flip.
1147 */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001148 idx = 1 ^ (sp->srcu_idx & 1);
1149 if (!try_check_zero(sp, idx, 2)) {
1150 mutex_unlock(&sp->srcu_gp_mutex);
1151 return; /* readers present, retry later. */
1152 }
1153 srcu_gp_end(sp); /* Releases ->srcu_gp_mutex. */
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001154 }
1155}
1156
1157/*
1158 * Invoke a limited number of SRCU callbacks that have passed through
1159 * their grace period. If there are more to do, SRCU will reschedule
1160 * the workqueue. Note that needed memory barriers have been executed
1161 * in this task's context by srcu_readers_active_idx_check().
1162 */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001163static void srcu_invoke_callbacks(struct work_struct *work)
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001164{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001165 bool more;
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001166 struct rcu_cblist ready_cbs;
1167 struct rcu_head *rhp;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001168 struct srcu_data *sdp;
1169 struct srcu_struct *sp;
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001170
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001171 sdp = container_of(work, struct srcu_data, work.work);
1172 sp = sdp->sp;
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001173 rcu_cblist_init(&ready_cbs);
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001174 spin_lock_irq_rcu_node(sdp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001175 rcu_segcblist_advance(&sdp->srcu_cblist,
1176 rcu_seq_current(&sp->srcu_gp_seq));
1177 if (sdp->srcu_cblist_invoking ||
1178 !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001179 spin_unlock_irq_rcu_node(sdp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001180 return; /* Someone else on the job or nothing to do. */
1181 }
1182
1183 /* We are on the job! Extract and invoke ready callbacks. */
1184 sdp->srcu_cblist_invoking = true;
1185 rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001186 spin_unlock_irq_rcu_node(sdp);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001187 rhp = rcu_cblist_dequeue(&ready_cbs);
1188 for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
Paul E. McKenneya6025382017-04-28 15:39:34 -07001189 debug_rcu_head_unqueue(rhp);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001190 local_bh_disable();
1191 rhp->func(rhp);
1192 local_bh_enable();
1193 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001194
1195 /*
1196 * Update counts, accelerate new callbacks, and if needed,
1197 * schedule another round of callback invocation.
1198 */
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001199 spin_lock_irq_rcu_node(sdp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001200 rcu_segcblist_insert_count(&sdp->srcu_cblist, &ready_cbs);
1201 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
1202 rcu_seq_snap(&sp->srcu_gp_seq));
1203 sdp->srcu_cblist_invoking = false;
1204 more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001205 spin_unlock_irq_rcu_node(sdp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001206 if (more)
1207 srcu_schedule_cbs_sdp(sdp, 0);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001208}
1209
1210/*
1211 * Finished one round of SRCU grace period. Start another if there are
1212 * more SRCU callbacks queued, otherwise put SRCU into not-running state.
1213 */
1214static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay)
1215{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001216 bool pushgp = true;
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001217
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001218 spin_lock_irq_rcu_node(sp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001219 if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
1220 if (!WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq))) {
1221 /* All requests fulfilled, time to go idle. */
1222 pushgp = false;
1223 }
1224 } else if (!rcu_seq_state(sp->srcu_gp_seq)) {
1225 /* Outstanding request and no GP. Start one. */
1226 srcu_gp_start(sp);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001227 }
Paul E. McKenneyd6331982017-10-10 13:52:30 -07001228 spin_unlock_irq_rcu_node(sp);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001229
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001230 if (pushgp)
Paul E. McKenneyad7c9462018-01-08 14:35:52 -08001231 queue_delayed_work(rcu_gp_wq, &sp->work, delay);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001232}
1233
1234/*
1235 * This is the work-queue function that handles SRCU grace periods.
1236 */
Paul E. McKenney0d8a1e82017-06-15 17:06:38 -07001237static void process_srcu(struct work_struct *work)
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001238{
1239 struct srcu_struct *sp;
1240
1241 sp = container_of(work, struct srcu_struct, work.work);
1242
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001243 srcu_advance_state(sp);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -07001244 srcu_reschedule(sp, srcu_get_delay(sp));
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001245}
Paul E. McKenney7f6733c2017-04-18 17:17:35 -07001246
1247void srcutorture_get_gp_data(enum rcutorture_type test_type,
Paul E. McKenney1e9a0382017-04-24 16:02:09 -07001248 struct srcu_struct *sp, int *flags,
1249 unsigned long *gpnum, unsigned long *completed)
Paul E. McKenney7f6733c2017-04-18 17:17:35 -07001250{
1251 if (test_type != SRCU_FLAVOR)
1252 return;
1253 *flags = 0;
1254 *completed = rcu_seq_ctr(sp->srcu_gp_seq);
1255 *gpnum = rcu_seq_ctr(sp->srcu_gp_seq_needed);
1256}
1257EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
Paul E. McKenney1f4f6da2017-04-21 11:16:32 -07001258
Paul E. McKenney115a1a52017-05-22 13:31:03 -07001259void srcu_torture_stats_print(struct srcu_struct *sp, char *tt, char *tf)
1260{
1261 int cpu;
1262 int idx;
Paul E. McKenneyac3748c2017-05-22 13:59:52 -07001263 unsigned long s0 = 0, s1 = 0;
Paul E. McKenney115a1a52017-05-22 13:31:03 -07001264
1265 idx = sp->srcu_idx & 0x1;
1266 pr_alert("%s%s Tree SRCU per-CPU(idx=%d):", tt, tf, idx);
1267 for_each_possible_cpu(cpu) {
1268 unsigned long l0, l1;
1269 unsigned long u0, u1;
1270 long c0, c1;
1271 struct srcu_data *counts;
1272
1273 counts = per_cpu_ptr(sp->sda, cpu);
1274 u0 = counts->srcu_unlock_count[!idx];
1275 u1 = counts->srcu_unlock_count[idx];
1276
1277 /*
1278 * Make sure that a lock is always counted if the corresponding
1279 * unlock is counted.
1280 */
1281 smp_rmb();
1282
1283 l0 = counts->srcu_lock_count[!idx];
1284 l1 = counts->srcu_lock_count[idx];
1285
1286 c0 = l0 - u0;
1287 c1 = l1 - u1;
1288 pr_cont(" %d(%ld,%ld)", cpu, c0, c1);
Paul E. McKenneyac3748c2017-05-22 13:59:52 -07001289 s0 += c0;
1290 s1 += c1;
Paul E. McKenney115a1a52017-05-22 13:31:03 -07001291 }
Paul E. McKenneyac3748c2017-05-22 13:59:52 -07001292 pr_cont(" T(%ld,%ld)\n", s0, s1);
Paul E. McKenney115a1a52017-05-22 13:31:03 -07001293}
1294EXPORT_SYMBOL_GPL(srcu_torture_stats_print);
1295
Paul E. McKenney1f4f6da2017-04-21 11:16:32 -07001296static int __init srcu_bootup_announce(void)
1297{
1298 pr_info("Hierarchical SRCU implementation.\n");
Paul E. McKenney0c8e0e32017-04-28 11:24:22 -07001299 if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF)
1300 pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff);
Paul E. McKenney1f4f6da2017-04-21 11:16:32 -07001301 return 0;
1302}
1303early_initcall(srcu_bootup_announce);