1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Sleepable Read-Copy Update mechanism for mutual exclusion.
4 *
5 * Copyright (C) IBM Corporation, 2006
6 * Copyright (C) Fujitsu, 2012
7 *
8 * Authors: Paul McKenney <paulmck@linux.ibm.com>
9 * 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/slab.h>
28 #include <linux/srcu.h>
29
30 #include "rcu.h"
31 #include "rcu_segcblist.h"
32
33 /* Holdoff in nanoseconds for auto-expediting. */
34 #define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000)
35 static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF;
36 module_param(exp_holdoff, ulong, 0444);
37
38 /* Overflow-check frequency. N bits roughly says every 2**N grace periods. */
39 static ulong counter_wrap_check = (ULONG_MAX >> 2);
40 module_param(counter_wrap_check, ulong, 0444);
41
42 /*
43 * Control conversion to SRCU_SIZE_BIG:
44 * 0: Don't convert at all.
45 * 1: Convert at init_srcu_struct() time.
46 * 2: Convert when rcutorture invokes srcu_torture_stats_print().
47 * 3: Decide at boot time based on system shape (default).
48 * 0x1x: Convert when excessive contention encountered.
49 */
50 #define SRCU_SIZING_NONE 0
51 #define SRCU_SIZING_INIT 1
52 #define SRCU_SIZING_TORTURE 2
53 #define SRCU_SIZING_AUTO 3
54 #define SRCU_SIZING_CONTEND 0x10
55 #define SRCU_SIZING_IS(x) ((convert_to_big & ~SRCU_SIZING_CONTEND) == x)
56 #define SRCU_SIZING_IS_NONE() (SRCU_SIZING_IS(SRCU_SIZING_NONE))
57 #define SRCU_SIZING_IS_INIT() (SRCU_SIZING_IS(SRCU_SIZING_INIT))
58 #define SRCU_SIZING_IS_TORTURE() (SRCU_SIZING_IS(SRCU_SIZING_TORTURE))
59 #define SRCU_SIZING_IS_CONTEND() (convert_to_big & SRCU_SIZING_CONTEND)
60 static int convert_to_big = SRCU_SIZING_AUTO;
61 module_param(convert_to_big, int, 0444);
62
63 /* Number of CPUs to trigger init_srcu_struct()-time transition to big. */
64 static int big_cpu_lim __read_mostly = 128;
65 module_param(big_cpu_lim, int, 0444);
66
67 /* Contention events per jiffy to initiate transition to big. */
68 static int small_contention_lim __read_mostly = 100;
69 module_param(small_contention_lim, int, 0444);
70
71 /* Early-boot callback-management, so early that no lock is required! */
72 static LIST_HEAD(srcu_boot_list);
73 static bool __read_mostly srcu_init_done;
74
75 static void srcu_invoke_callbacks(struct work_struct *work);
76 static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay);
77 static void process_srcu(struct work_struct *work);
78 static void srcu_delay_timer(struct timer_list *t);
79
80 /* Wrappers for lock acquisition and release, see raw_spin_lock_rcu_node(). */
81 #define spin_lock_rcu_node(p) \
82 do { \
83 spin_lock(&ACCESS_PRIVATE(p, lock)); \
84 smp_mb__after_unlock_lock(); \
85 } while (0)
86
87 #define spin_unlock_rcu_node(p) spin_unlock(&ACCESS_PRIVATE(p, lock))
88
89 #define spin_lock_irq_rcu_node(p) \
90 do { \
91 spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \
92 smp_mb__after_unlock_lock(); \
93 } while (0)
94
95 #define spin_unlock_irq_rcu_node(p) \
96 spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
97
98 #define spin_lock_irqsave_rcu_node(p, flags) \
99 do { \
100 spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
101 smp_mb__after_unlock_lock(); \
102 } while (0)
103
104 #define spin_trylock_irqsave_rcu_node(p, flags) \
105 ({ \
106 bool ___locked = spin_trylock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
107 \
108 if (___locked) \
109 smp_mb__after_unlock_lock(); \
110 ___locked; \
111 })
112
113 #define spin_unlock_irqrestore_rcu_node(p, flags) \
114 spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags) \
115
116 /*
117 * Initialize SRCU per-CPU data. Note that statically allocated
118 * srcu_struct structures might already have srcu_read_lock() and
119 * srcu_read_unlock() running against them. So if the is_static
120 * parameter is set, don't initialize ->srcu_ctrs[].srcu_locks and
121 * ->srcu_ctrs[].srcu_unlocks.
122 */
init_srcu_struct_data(struct srcu_struct * ssp)123 static void init_srcu_struct_data(struct srcu_struct *ssp)
124 {
125 int cpu;
126 struct srcu_data *sdp;
127
128 /*
129 * Initialize the per-CPU srcu_data array, which feeds into the
130 * leaves of the srcu_node tree.
131 */
132 for_each_possible_cpu(cpu) {
133 sdp = per_cpu_ptr(ssp->sda, cpu);
134 spin_lock_init(&ACCESS_PRIVATE(sdp, lock));
135 rcu_segcblist_init(&sdp->srcu_cblist);
136 sdp->srcu_cblist_invoking = false;
137 sdp->srcu_gp_seq_needed = ssp->srcu_sup->srcu_gp_seq;
138 sdp->srcu_gp_seq_needed_exp = ssp->srcu_sup->srcu_gp_seq;
139 sdp->srcu_barrier_head.next = &sdp->srcu_barrier_head;
140 sdp->mynode = NULL;
141 sdp->cpu = cpu;
142 INIT_WORK(&sdp->work, srcu_invoke_callbacks);
143 timer_setup(&sdp->delay_work, srcu_delay_timer, 0);
144 sdp->ssp = ssp;
145 }
146 }
147
148 /* Invalid seq state, used during snp node initialization */
149 #define SRCU_SNP_INIT_SEQ 0x2
150
151 /*
152 * Check whether sequence number corresponding to snp node,
153 * is invalid.
154 */
srcu_invl_snp_seq(unsigned long s)155 static inline bool srcu_invl_snp_seq(unsigned long s)
156 {
157 return s == SRCU_SNP_INIT_SEQ;
158 }
159
160 /*
161 * Allocated and initialize SRCU combining tree. Returns @true if
162 * allocation succeeded and @false otherwise.
163 */
init_srcu_struct_nodes(struct srcu_struct * ssp,gfp_t gfp_flags)164 static bool init_srcu_struct_nodes(struct srcu_struct *ssp, gfp_t gfp_flags)
165 {
166 int cpu;
167 int i;
168 int level = 0;
169 int levelspread[RCU_NUM_LVLS];
170 struct srcu_data *sdp;
171 struct srcu_node *snp;
172 struct srcu_node *snp_first;
173
174 /* Initialize geometry if it has not already been initialized. */
175 rcu_init_geometry();
176 ssp->srcu_sup->node = kcalloc(rcu_num_nodes, sizeof(*ssp->srcu_sup->node), gfp_flags);
177 if (!ssp->srcu_sup->node)
178 return false;
179
180 /* Work out the overall tree geometry. */
181 ssp->srcu_sup->level[0] = &ssp->srcu_sup->node[0];
182 for (i = 1; i < rcu_num_lvls; i++)
183 ssp->srcu_sup->level[i] = ssp->srcu_sup->level[i - 1] + num_rcu_lvl[i - 1];
184 rcu_init_levelspread(levelspread, num_rcu_lvl);
185
186 /* Each pass through this loop initializes one srcu_node structure. */
187 srcu_for_each_node_breadth_first(ssp, snp) {
188 spin_lock_init(&ACCESS_PRIVATE(snp, lock));
189 BUILD_BUG_ON(ARRAY_SIZE(snp->srcu_have_cbs) !=
190 ARRAY_SIZE(snp->srcu_data_have_cbs));
191 for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {
192 snp->srcu_have_cbs[i] = SRCU_SNP_INIT_SEQ;
193 snp->srcu_data_have_cbs[i] = 0;
194 }
195 snp->srcu_gp_seq_needed_exp = SRCU_SNP_INIT_SEQ;
196 snp->grplo = -1;
197 snp->grphi = -1;
198 if (snp == &ssp->srcu_sup->node[0]) {
199 /* Root node, special case. */
200 snp->srcu_parent = NULL;
201 continue;
202 }
203
204 /* Non-root node. */
205 if (snp == ssp->srcu_sup->level[level + 1])
206 level++;
207 snp->srcu_parent = ssp->srcu_sup->level[level - 1] +
208 (snp - ssp->srcu_sup->level[level]) /
209 levelspread[level - 1];
210 }
211
212 /*
213 * Initialize the per-CPU srcu_data array, which feeds into the
214 * leaves of the srcu_node tree.
215 */
216 level = rcu_num_lvls - 1;
217 snp_first = ssp->srcu_sup->level[level];
218 for_each_possible_cpu(cpu) {
219 sdp = per_cpu_ptr(ssp->sda, cpu);
220 sdp->mynode = &snp_first[cpu / levelspread[level]];
221 for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {
222 if (snp->grplo < 0)
223 snp->grplo = cpu;
224 snp->grphi = cpu;
225 }
226 sdp->grpmask = 1UL << (cpu - sdp->mynode->grplo);
227 }
228 smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_WAIT_BARRIER);
229 return true;
230 }
231
232 /*
233 * Initialize non-compile-time initialized fields, including the
234 * associated srcu_node and srcu_data structures. The is_static parameter
235 * tells us that ->sda has already been wired up to srcu_data.
236 */
init_srcu_struct_fields(struct srcu_struct * ssp,bool is_static)237 static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static)
238 {
239 if (!is_static)
240 ssp->srcu_sup = kzalloc(sizeof(*ssp->srcu_sup), GFP_KERNEL);
241 if (!ssp->srcu_sup)
242 return -ENOMEM;
243 if (!is_static)
244 spin_lock_init(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
245 ssp->srcu_sup->srcu_size_state = SRCU_SIZE_SMALL;
246 ssp->srcu_sup->node = NULL;
247 mutex_init(&ssp->srcu_sup->srcu_cb_mutex);
248 mutex_init(&ssp->srcu_sup->srcu_gp_mutex);
249 ssp->srcu_sup->srcu_gp_seq = SRCU_GP_SEQ_INITIAL_VAL;
250 ssp->srcu_sup->srcu_barrier_seq = 0;
251 mutex_init(&ssp->srcu_sup->srcu_barrier_mutex);
252 atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 0);
253 INIT_DELAYED_WORK(&ssp->srcu_sup->work, process_srcu);
254 ssp->srcu_sup->sda_is_static = is_static;
255 if (!is_static) {
256 ssp->sda = alloc_percpu(struct srcu_data);
257 ssp->srcu_ctrp = &ssp->sda->srcu_ctrs[0];
258 }
259 if (!ssp->sda)
260 goto err_free_sup;
261 init_srcu_struct_data(ssp);
262 ssp->srcu_sup->srcu_gp_seq_needed_exp = SRCU_GP_SEQ_INITIAL_VAL;
263 ssp->srcu_sup->srcu_last_gp_end = ktime_get_mono_fast_ns();
264 if (READ_ONCE(ssp->srcu_sup->srcu_size_state) == SRCU_SIZE_SMALL && SRCU_SIZING_IS_INIT()) {
265 if (!init_srcu_struct_nodes(ssp, GFP_ATOMIC))
266 goto err_free_sda;
267 WRITE_ONCE(ssp->srcu_sup->srcu_size_state, SRCU_SIZE_BIG);
268 }
269 ssp->srcu_sup->srcu_ssp = ssp;
270 smp_store_release(&ssp->srcu_sup->srcu_gp_seq_needed,
271 SRCU_GP_SEQ_INITIAL_VAL); /* Init done. */
272 return 0;
273
274 err_free_sda:
275 if (!is_static) {
276 free_percpu(ssp->sda);
277 ssp->sda = NULL;
278 }
279 err_free_sup:
280 if (!is_static) {
281 kfree(ssp->srcu_sup);
282 ssp->srcu_sup = NULL;
283 }
284 return -ENOMEM;
285 }
286
287 #ifdef CONFIG_DEBUG_LOCK_ALLOC
288
__init_srcu_struct(struct srcu_struct * ssp,const char * name,struct lock_class_key * key)289 int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
290 struct lock_class_key *key)
291 {
292 /* Don't re-initialize a lock while it is held. */
293 debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));
294 lockdep_init_map(&ssp->dep_map, name, key, 0);
295 return init_srcu_struct_fields(ssp, false);
296 }
297 EXPORT_SYMBOL_GPL(__init_srcu_struct);
298
299 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
300
301 /**
302 * init_srcu_struct - initialize a sleep-RCU structure
303 * @ssp: structure to initialize.
304 *
305 * Must invoke this on a given srcu_struct before passing that srcu_struct
306 * to any other function. Each srcu_struct represents a separate domain
307 * of SRCU protection.
308 */
init_srcu_struct(struct srcu_struct * ssp)309 int init_srcu_struct(struct srcu_struct *ssp)
310 {
311 return init_srcu_struct_fields(ssp, false);
312 }
313 EXPORT_SYMBOL_GPL(init_srcu_struct);
314
315 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
316
317 /*
318 * Initiate a transition to SRCU_SIZE_BIG with lock held.
319 */
__srcu_transition_to_big(struct srcu_struct * ssp)320 static void __srcu_transition_to_big(struct srcu_struct *ssp)
321 {
322 lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
323 smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_ALLOC);
324 }
325
326 /*
327 * Initiate an idempotent transition to SRCU_SIZE_BIG.
328 */
srcu_transition_to_big(struct srcu_struct * ssp)329 static void srcu_transition_to_big(struct srcu_struct *ssp)
330 {
331 unsigned long flags;
332
333 /* Double-checked locking on ->srcu_size-state. */
334 if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) != SRCU_SIZE_SMALL)
335 return;
336 spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags);
337 if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) != SRCU_SIZE_SMALL) {
338 spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
339 return;
340 }
341 __srcu_transition_to_big(ssp);
342 spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
343 }
344
345 /*
346 * Check to see if the just-encountered contention event justifies
347 * a transition to SRCU_SIZE_BIG.
348 */
spin_lock_irqsave_check_contention(struct srcu_struct * ssp)349 static void spin_lock_irqsave_check_contention(struct srcu_struct *ssp)
350 {
351 unsigned long j;
352
353 if (!SRCU_SIZING_IS_CONTEND() || ssp->srcu_sup->srcu_size_state)
354 return;
355 j = jiffies;
356 if (ssp->srcu_sup->srcu_size_jiffies != j) {
357 ssp->srcu_sup->srcu_size_jiffies = j;
358 ssp->srcu_sup->srcu_n_lock_retries = 0;
359 }
360 if (++ssp->srcu_sup->srcu_n_lock_retries <= small_contention_lim)
361 return;
362 __srcu_transition_to_big(ssp);
363 }
364
365 /*
366 * Acquire the specified srcu_data structure's ->lock, but check for
367 * excessive contention, which results in initiation of a transition
368 * to SRCU_SIZE_BIG. But only if the srcutree.convert_to_big module
369 * parameter permits this.
370 */
spin_lock_irqsave_sdp_contention(struct srcu_data * sdp,unsigned long * flags)371 static void spin_lock_irqsave_sdp_contention(struct srcu_data *sdp, unsigned long *flags)
372 {
373 struct srcu_struct *ssp = sdp->ssp;
374
375 if (spin_trylock_irqsave_rcu_node(sdp, *flags))
376 return;
377 spin_lock_irqsave_rcu_node(ssp->srcu_sup, *flags);
378 spin_lock_irqsave_check_contention(ssp);
379 spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, *flags);
380 spin_lock_irqsave_rcu_node(sdp, *flags);
381 }
382
383 /*
384 * Acquire the specified srcu_struct structure's ->lock, but check for
385 * excessive contention, which results in initiation of a transition
386 * to SRCU_SIZE_BIG. But only if the srcutree.convert_to_big module
387 * parameter permits this.
388 */
spin_lock_irqsave_ssp_contention(struct srcu_struct * ssp,unsigned long * flags)389 static void spin_lock_irqsave_ssp_contention(struct srcu_struct *ssp, unsigned long *flags)
390 {
391 if (spin_trylock_irqsave_rcu_node(ssp->srcu_sup, *flags))
392 return;
393 spin_lock_irqsave_rcu_node(ssp->srcu_sup, *flags);
394 spin_lock_irqsave_check_contention(ssp);
395 }
396
397 /*
398 * First-use initialization of statically allocated srcu_struct
399 * structure. Wiring up the combining tree is more than can be
400 * done with compile-time initialization, so this check is added
401 * to each update-side SRCU primitive. Use ssp->lock, which -is-
402 * compile-time initialized, to resolve races involving multiple
403 * CPUs trying to garner first-use privileges.
404 */
check_init_srcu_struct(struct srcu_struct * ssp)405 static void check_init_srcu_struct(struct srcu_struct *ssp)
406 {
407 unsigned long flags;
408
409 /* The smp_load_acquire() pairs with the smp_store_release(). */
410 if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed))) /*^^^*/
411 return; /* Already initialized. */
412 spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags);
413 if (!rcu_seq_state(ssp->srcu_sup->srcu_gp_seq_needed)) {
414 spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
415 return;
416 }
417 init_srcu_struct_fields(ssp, true);
418 spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
419 }
420
421 /*
422 * Is the current or any upcoming grace period to be expedited?
423 */
srcu_gp_is_expedited(struct srcu_struct * ssp)424 static bool srcu_gp_is_expedited(struct srcu_struct *ssp)
425 {
426 struct srcu_usage *sup = ssp->srcu_sup;
427
428 return ULONG_CMP_LT(READ_ONCE(sup->srcu_gp_seq), READ_ONCE(sup->srcu_gp_seq_needed_exp));
429 }
430
431 /*
432 * Computes approximate total of the readers' ->srcu_ctrs[].srcu_locks
433 * values for the rank of per-CPU counters specified by idx, and returns
434 * true if the caller did the proper barrier (gp), and if the count of
435 * the locks matches that of the unlocks passed in.
436 */
srcu_readers_lock_idx(struct srcu_struct * ssp,int idx,bool gp,unsigned long unlocks)437 static bool srcu_readers_lock_idx(struct srcu_struct *ssp, int idx, bool gp, unsigned long unlocks)
438 {
439 int cpu;
440 unsigned long mask = 0;
441 unsigned long sum = 0;
442
443 for_each_possible_cpu(cpu) {
444 struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
445
446 sum += atomic_long_read(&sdp->srcu_ctrs[idx].srcu_locks);
447 if (IS_ENABLED(CONFIG_PROVE_RCU))
448 mask = mask | READ_ONCE(sdp->srcu_reader_flavor);
449 }
450 WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && (mask & (mask - 1)),
451 "Mixed reader flavors for srcu_struct at %ps.\n", ssp);
452 if (mask & SRCU_READ_FLAVOR_SLOWGP && !gp)
453 return false;
454 return sum == unlocks;
455 }
456
457 /*
458 * Returns approximate total of the readers' ->srcu_ctrs[].srcu_unlocks
459 * values for the rank of per-CPU counters specified by idx.
460 */
srcu_readers_unlock_idx(struct srcu_struct * ssp,int idx,unsigned long * rdm)461 static unsigned long srcu_readers_unlock_idx(struct srcu_struct *ssp, int idx, unsigned long *rdm)
462 {
463 int cpu;
464 unsigned long mask = 0;
465 unsigned long sum = 0;
466
467 for_each_possible_cpu(cpu) {
468 struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
469
470 sum += atomic_long_read(&sdp->srcu_ctrs[idx].srcu_unlocks);
471 mask = mask | READ_ONCE(sdp->srcu_reader_flavor);
472 }
473 WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && (mask & (mask - 1)),
474 "Mixed reader flavors for srcu_struct at %ps.\n", ssp);
475 *rdm = mask;
476 return sum;
477 }
478
479 /*
480 * Return true if the number of pre-existing readers is determined to
481 * be zero.
482 */
srcu_readers_active_idx_check(struct srcu_struct * ssp,int idx)483 static bool srcu_readers_active_idx_check(struct srcu_struct *ssp, int idx)
484 {
485 bool did_gp;
486 unsigned long rdm;
487 unsigned long unlocks;
488
489 unlocks = srcu_readers_unlock_idx(ssp, idx, &rdm);
490 did_gp = !!(rdm & SRCU_READ_FLAVOR_SLOWGP);
491
492 /*
493 * Make sure that a lock is always counted if the corresponding
494 * unlock is counted. Needs to be a smp_mb() as the read side may
495 * contain a read from a variable that is written to before the
496 * synchronize_srcu() in the write side. In this case smp_mb()s
497 * A and B (or X and Y) act like the store buffering pattern.
498 *
499 * This smp_mb() also pairs with smp_mb() C (or, in the case of X,
500 * Z) to prevent accesses after the synchronize_srcu() from being
501 * executed before the grace period ends.
502 */
503 if (!did_gp)
504 smp_mb(); /* A */
505 else if (srcu_gp_is_expedited(ssp))
506 synchronize_rcu_expedited(); /* X */
507 else
508 synchronize_rcu(); /* X */
509
510 /*
511 * If the locks are the same as the unlocks, then there must have
512 * been no readers on this index at some point in this function.
513 * But there might be more readers, as a task might have read
514 * the current ->srcu_ctrp but not yet have incremented its CPU's
515 * ->srcu_ctrs[idx].srcu_locks counter. In fact, it is possible
516 * that most of the tasks have been preempted between fetching
517 * ->srcu_ctrp and incrementing ->srcu_ctrs[idx].srcu_locks. And
518 * there could be almost (ULONG_MAX / sizeof(struct task_struct))
519 * tasks in a system whose address space was fully populated
520 * with memory. Call this quantity Nt.
521 *
522 * So suppose that the updater is preempted at this
523 * point in the code for a long time. That now-preempted
524 * updater has already flipped ->srcu_ctrp (possibly during
525 * the preceding grace period), done an smp_mb() (again,
526 * possibly during the preceding grace period), and summed up
527 * the ->srcu_ctrs[idx].srcu_unlocks counters. How many times
528 * can a given one of the aforementioned Nt tasks increment the
529 * old ->srcu_ctrp value's ->srcu_ctrs[idx].srcu_locks counter,
530 * in the absence of nesting?
531 *
532 * It can clearly do so once, given that it has already fetched
533 * the old value of ->srcu_ctrp and is just about to use that
534 * value to index its increment of ->srcu_ctrs[idx].srcu_locks.
535 * But as soon as it leaves that SRCU read-side critical section,
536 * it will increment ->srcu_ctrs[idx].srcu_unlocks, which must
537 * follow the updater's above read from that same value. Thus,
538 as soon the reading task does an smp_mb() and a later fetch from
539 * ->srcu_ctrp, that task will be guaranteed to get the new index.
540 * Except that the increment of ->srcu_ctrs[idx].srcu_unlocks
541 * in __srcu_read_unlock() is after the smp_mb(), and the fetch
542 * from ->srcu_ctrp in __srcu_read_lock() is before the smp_mb().
543 * Thus, that task might not see the new value of ->srcu_ctrp until
544 * the -second- __srcu_read_lock(), which in turn means that this
545 * task might well increment ->srcu_ctrs[idx].srcu_locks for the
546 * old value of ->srcu_ctrp twice, not just once.
547 *
548 * However, it is important to note that a given smp_mb() takes
549 * effect not just for the task executing it, but also for any
550 * later task running on that same CPU.
551 *
552 * That is, there can be almost Nt + Nc further increments
553 * of ->srcu_ctrs[idx].srcu_locks for the old index, where Nc
554 * is the number of CPUs. But this is OK because the size of
555 * the task_struct structure limits the value of Nt and current
556 * systems limit Nc to a few thousand.
557 *
558 * OK, but what about nesting? This does impose a limit on
559 * nesting of half of the size of the task_struct structure
560 * (measured in bytes), which should be sufficient. A late 2022
561 * TREE01 rcutorture run reported this size to be no less than
562 * 9408 bytes, allowing up to 4704 levels of nesting, which is
563 * comfortably beyond excessive. Especially on 64-bit systems,
564 * which are unlikely to be configured with an address space fully
565 * populated with memory, at least not anytime soon.
566 */
567 return srcu_readers_lock_idx(ssp, idx, did_gp, unlocks);
568 }
569
570 /**
571 * srcu_readers_active - returns true if there are readers. and false
572 * otherwise
573 * @ssp: which srcu_struct to count active readers (holding srcu_read_lock).
574 *
575 * Note that this is not an atomic primitive, and can therefore suffer
576 * severe errors when invoked on an active srcu_struct. That said, it
577 * can be useful as an error check at cleanup time.
578 */
srcu_readers_active(struct srcu_struct * ssp)579 static bool srcu_readers_active(struct srcu_struct *ssp)
580 {
581 int cpu;
582 unsigned long sum = 0;
583
584 for_each_possible_cpu(cpu) {
585 struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
586
587 sum += atomic_long_read(&sdp->srcu_ctrs[0].srcu_locks);
588 sum += atomic_long_read(&sdp->srcu_ctrs[1].srcu_locks);
589 sum -= atomic_long_read(&sdp->srcu_ctrs[0].srcu_unlocks);
590 sum -= atomic_long_read(&sdp->srcu_ctrs[1].srcu_unlocks);
591 }
592 return sum;
593 }
594
595 /*
596 * We use an adaptive strategy for synchronize_srcu() and especially for
597 * synchronize_srcu_expedited(). We spin for a fixed time period
598 * (defined below, boot time configurable) to allow SRCU readers to exit
599 * their read-side critical sections. If there are still some readers
600 * after one jiffy, we repeatedly block for one jiffy time periods.
601 * The blocking time is increased as the grace-period age increases,
602 * with max blocking time capped at 10 jiffies.
603 */
604 #define SRCU_DEFAULT_RETRY_CHECK_DELAY 5
605
606 static ulong srcu_retry_check_delay = SRCU_DEFAULT_RETRY_CHECK_DELAY;
607 module_param(srcu_retry_check_delay, ulong, 0444);
608
609 #define SRCU_INTERVAL 1 // Base delay if no expedited GPs pending.
610 #define SRCU_MAX_INTERVAL 10 // Maximum incremental delay from slow readers.
611
612 #define SRCU_DEFAULT_MAX_NODELAY_PHASE_LO 3UL // Lowmark on default per-GP-phase
613 // no-delay instances.
614 #define SRCU_DEFAULT_MAX_NODELAY_PHASE_HI 1000UL // Highmark on default per-GP-phase
615 // no-delay instances.
616
617 #define SRCU_UL_CLAMP_LO(val, low) ((val) > (low) ? (val) : (low))
618 #define SRCU_UL_CLAMP_HI(val, high) ((val) < (high) ? (val) : (high))
619 #define SRCU_UL_CLAMP(val, low, high) SRCU_UL_CLAMP_HI(SRCU_UL_CLAMP_LO((val), (low)), (high))
620 // per-GP-phase no-delay instances adjusted to allow non-sleeping poll upto
621 // one jiffies time duration. Mult by 2 is done to factor in the srcu_get_delay()
622 // called from process_srcu().
623 #define SRCU_DEFAULT_MAX_NODELAY_PHASE_ADJUSTED \
624 (2UL * USEC_PER_SEC / HZ / SRCU_DEFAULT_RETRY_CHECK_DELAY)
625
626 // Maximum per-GP-phase consecutive no-delay instances.
627 #define SRCU_DEFAULT_MAX_NODELAY_PHASE \
628 SRCU_UL_CLAMP(SRCU_DEFAULT_MAX_NODELAY_PHASE_ADJUSTED, \
629 SRCU_DEFAULT_MAX_NODELAY_PHASE_LO, \
630 SRCU_DEFAULT_MAX_NODELAY_PHASE_HI)
631
632 static ulong srcu_max_nodelay_phase = SRCU_DEFAULT_MAX_NODELAY_PHASE;
633 module_param(srcu_max_nodelay_phase, ulong, 0444);
634
635 // Maximum consecutive no-delay instances.
636 #define SRCU_DEFAULT_MAX_NODELAY (SRCU_DEFAULT_MAX_NODELAY_PHASE > 100 ? \
637 SRCU_DEFAULT_MAX_NODELAY_PHASE : 100)
638
639 static ulong srcu_max_nodelay = SRCU_DEFAULT_MAX_NODELAY;
640 module_param(srcu_max_nodelay, ulong, 0444);
641
642 /*
643 * Return grace-period delay, zero if there are expedited grace
644 * periods pending, SRCU_INTERVAL otherwise.
645 */
srcu_get_delay(struct srcu_struct * ssp)646 static unsigned long srcu_get_delay(struct srcu_struct *ssp)
647 {
648 unsigned long gpstart;
649 unsigned long j;
650 unsigned long jbase = SRCU_INTERVAL;
651 struct srcu_usage *sup = ssp->srcu_sup;
652
653 lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
654 if (srcu_gp_is_expedited(ssp))
655 jbase = 0;
656 if (rcu_seq_state(READ_ONCE(sup->srcu_gp_seq))) {
657 j = jiffies - 1;
658 gpstart = READ_ONCE(sup->srcu_gp_start);
659 if (time_after(j, gpstart))
660 jbase += j - gpstart;
661 if (!jbase) {
662 ASSERT_EXCLUSIVE_WRITER(sup->srcu_n_exp_nodelay);
663 WRITE_ONCE(sup->srcu_n_exp_nodelay, READ_ONCE(sup->srcu_n_exp_nodelay) + 1);
664 if (READ_ONCE(sup->srcu_n_exp_nodelay) > srcu_max_nodelay_phase)
665 jbase = 1;
666 }
667 }
668 return jbase > SRCU_MAX_INTERVAL ? SRCU_MAX_INTERVAL : jbase;
669 }
670
671 /**
672 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
673 * @ssp: structure to clean up.
674 *
675 * Must invoke this after you are finished using a given srcu_struct that
676 * was initialized via init_srcu_struct(), else you leak memory.
677 */
cleanup_srcu_struct(struct srcu_struct * ssp)678 void cleanup_srcu_struct(struct srcu_struct *ssp)
679 {
680 int cpu;
681 unsigned long delay;
682 struct srcu_usage *sup = ssp->srcu_sup;
683
684 spin_lock_irq_rcu_node(ssp->srcu_sup);
685 delay = srcu_get_delay(ssp);
686 spin_unlock_irq_rcu_node(ssp->srcu_sup);
687 if (WARN_ON(!delay))
688 return; /* Just leak it! */
689 if (WARN_ON(srcu_readers_active(ssp)))
690 return; /* Just leak it! */
691 flush_delayed_work(&sup->work);
692 for_each_possible_cpu(cpu) {
693 struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
694
695 timer_delete_sync(&sdp->delay_work);
696 flush_work(&sdp->work);
697 if (WARN_ON(rcu_segcblist_n_cbs(&sdp->srcu_cblist)))
698 return; /* Forgot srcu_barrier(), so just leak it! */
699 }
700 if (WARN_ON(rcu_seq_state(READ_ONCE(sup->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
701 WARN_ON(rcu_seq_current(&sup->srcu_gp_seq) != sup->srcu_gp_seq_needed) ||
702 WARN_ON(srcu_readers_active(ssp))) {
703 pr_info("%s: Active srcu_struct %p read state: %d gp state: %lu/%lu\n",
704 __func__, ssp, rcu_seq_state(READ_ONCE(sup->srcu_gp_seq)),
705 rcu_seq_current(&sup->srcu_gp_seq), sup->srcu_gp_seq_needed);
706 return; // Caller forgot to stop doing call_srcu()?
707 // Or caller invoked start_poll_synchronize_srcu()
708 // and then cleanup_srcu_struct() before that grace
709 // period ended?
710 }
711 kfree(sup->node);
712 sup->node = NULL;
713 sup->srcu_size_state = SRCU_SIZE_SMALL;
714 if (!sup->sda_is_static) {
715 free_percpu(ssp->sda);
716 ssp->sda = NULL;
717 kfree(sup);
718 ssp->srcu_sup = NULL;
719 }
720 }
721 EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
722
723 /*
724 * Check for consistent reader flavor.
725 */
__srcu_check_read_flavor(struct srcu_struct * ssp,int read_flavor)726 void __srcu_check_read_flavor(struct srcu_struct *ssp, int read_flavor)
727 {
728 int old_read_flavor;
729 struct srcu_data *sdp;
730
731 /* NMI-unsafe use in NMI is a bad sign, as is multi-bit read_flavor values. */
732 WARN_ON_ONCE((read_flavor != SRCU_READ_FLAVOR_NMI) && in_nmi());
733 WARN_ON_ONCE(read_flavor & (read_flavor - 1));
734
735 sdp = raw_cpu_ptr(ssp->sda);
736 old_read_flavor = READ_ONCE(sdp->srcu_reader_flavor);
737 if (!old_read_flavor) {
738 old_read_flavor = cmpxchg(&sdp->srcu_reader_flavor, 0, read_flavor);
739 if (!old_read_flavor)
740 return;
741 }
742 WARN_ONCE(old_read_flavor != read_flavor, "CPU %d old state %d new state %d\n", sdp->cpu, old_read_flavor, read_flavor);
743 }
744 EXPORT_SYMBOL_GPL(__srcu_check_read_flavor);
745
746 /*
747 * Counts the new reader in the appropriate per-CPU element of the
748 * srcu_struct.
749 * Returns a guaranteed non-negative index that must be passed to the
750 * matching __srcu_read_unlock().
751 */
__srcu_read_lock(struct srcu_struct * ssp)752 int __srcu_read_lock(struct srcu_struct *ssp)
753 {
754 struct srcu_ctr __percpu *scp = READ_ONCE(ssp->srcu_ctrp);
755
756 this_cpu_inc(scp->srcu_locks.counter);
757 smp_mb(); /* B */ /* Avoid leaking the critical section. */
758 return __srcu_ptr_to_ctr(ssp, scp);
759 }
760 EXPORT_SYMBOL_GPL(__srcu_read_lock);
761
762 /*
763 * Removes the count for the old reader from the appropriate per-CPU
764 * element of the srcu_struct. Note that this may well be a different
765 * CPU than that which was incremented by the corresponding srcu_read_lock().
766 */
__srcu_read_unlock(struct srcu_struct * ssp,int idx)767 void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
768 {
769 smp_mb(); /* C */ /* Avoid leaking the critical section. */
770 this_cpu_inc(__srcu_ctr_to_ptr(ssp, idx)->srcu_unlocks.counter);
771 }
772 EXPORT_SYMBOL_GPL(__srcu_read_unlock);
773
774 #ifdef CONFIG_NEED_SRCU_NMI_SAFE
775
776 /*
777 * Counts the new reader in the appropriate per-CPU element of the
778 * srcu_struct, but in an NMI-safe manner using RMW atomics.
779 * Returns an index that must be passed to the matching srcu_read_unlock().
780 */
__srcu_read_lock_nmisafe(struct srcu_struct * ssp)781 int __srcu_read_lock_nmisafe(struct srcu_struct *ssp)
782 {
783 struct srcu_ctr __percpu *scpp = READ_ONCE(ssp->srcu_ctrp);
784 struct srcu_ctr *scp = raw_cpu_ptr(scpp);
785
786 atomic_long_inc(&scp->srcu_locks);
787 smp_mb__after_atomic(); /* B */ /* Avoid leaking the critical section. */
788 return __srcu_ptr_to_ctr(ssp, scpp);
789 }
790 EXPORT_SYMBOL_GPL(__srcu_read_lock_nmisafe);
791
792 /*
793 * Removes the count for the old reader from the appropriate per-CPU
794 * element of the srcu_struct. Note that this may well be a different
795 * CPU than that which was incremented by the corresponding srcu_read_lock().
796 */
__srcu_read_unlock_nmisafe(struct srcu_struct * ssp,int idx)797 void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
798 {
799 smp_mb__before_atomic(); /* C */ /* Avoid leaking the critical section. */
800 atomic_long_inc(&raw_cpu_ptr(__srcu_ctr_to_ptr(ssp, idx))->srcu_unlocks);
801 }
802 EXPORT_SYMBOL_GPL(__srcu_read_unlock_nmisafe);
803
804 #endif // CONFIG_NEED_SRCU_NMI_SAFE
805
806 /*
807 * Start an SRCU grace period.
808 */
srcu_gp_start(struct srcu_struct * ssp)809 static void srcu_gp_start(struct srcu_struct *ssp)
810 {
811 int state;
812
813 lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
814 WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed));
815 WRITE_ONCE(ssp->srcu_sup->srcu_gp_start, jiffies);
816 WRITE_ONCE(ssp->srcu_sup->srcu_n_exp_nodelay, 0);
817 smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
818 rcu_seq_start(&ssp->srcu_sup->srcu_gp_seq);
819 state = rcu_seq_state(ssp->srcu_sup->srcu_gp_seq);
820 WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
821 }
822
823
srcu_delay_timer(struct timer_list * t)824 static void srcu_delay_timer(struct timer_list *t)
825 {
826 struct srcu_data *sdp = container_of(t, struct srcu_data, delay_work);
827
828 queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work);
829 }
830
srcu_queue_delayed_work_on(struct srcu_data * sdp,unsigned long delay)831 static void srcu_queue_delayed_work_on(struct srcu_data *sdp,
832 unsigned long delay)
833 {
834 if (!delay) {
835 queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work);
836 return;
837 }
838
839 timer_reduce(&sdp->delay_work, jiffies + delay);
840 }
841
842 /*
843 * Schedule callback invocation for the specified srcu_data structure,
844 * if possible, on the corresponding CPU.
845 */
srcu_schedule_cbs_sdp(struct srcu_data * sdp,unsigned long delay)846 static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
847 {
848 srcu_queue_delayed_work_on(sdp, delay);
849 }
850
851 /*
852 * Schedule callback invocation for all srcu_data structures associated
853 * with the specified srcu_node structure that have callbacks for the
854 * just-completed grace period, the one corresponding to idx. If possible,
855 * schedule this invocation on the corresponding CPUs.
856 */
srcu_schedule_cbs_snp(struct srcu_struct * ssp,struct srcu_node * snp,unsigned long mask,unsigned long delay)857 static void srcu_schedule_cbs_snp(struct srcu_struct *ssp, struct srcu_node *snp,
858 unsigned long mask, unsigned long delay)
859 {
860 int cpu;
861
862 for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
863 if (!(mask & (1UL << (cpu - snp->grplo))))
864 continue;
865 srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, cpu), delay);
866 }
867 }
868
869 /*
870 * Note the end of an SRCU grace period. Initiates callback invocation
871 * and starts a new grace period if needed.
872 *
873 * The ->srcu_cb_mutex acquisition does not protect any data, but
874 * instead prevents more than one grace period from starting while we
875 * are initiating callback invocation. This allows the ->srcu_have_cbs[]
876 * array to have a finite number of elements.
877 */
srcu_gp_end(struct srcu_struct * ssp)878 static void srcu_gp_end(struct srcu_struct *ssp)
879 {
880 unsigned long cbdelay = 1;
881 bool cbs;
882 bool last_lvl;
883 int cpu;
884 unsigned long gpseq;
885 int idx;
886 unsigned long mask;
887 struct srcu_data *sdp;
888 unsigned long sgsne;
889 struct srcu_node *snp;
890 int ss_state;
891 struct srcu_usage *sup = ssp->srcu_sup;
892
893 /* Prevent more than one additional grace period. */
894 mutex_lock(&sup->srcu_cb_mutex);
895
896 /* End the current grace period. */
897 spin_lock_irq_rcu_node(sup);
898 idx = rcu_seq_state(sup->srcu_gp_seq);
899 WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
900 if (srcu_gp_is_expedited(ssp))
901 cbdelay = 0;
902
903 WRITE_ONCE(sup->srcu_last_gp_end, ktime_get_mono_fast_ns());
904 rcu_seq_end(&sup->srcu_gp_seq);
905 gpseq = rcu_seq_current(&sup->srcu_gp_seq);
906 if (ULONG_CMP_LT(sup->srcu_gp_seq_needed_exp, gpseq))
907 WRITE_ONCE(sup->srcu_gp_seq_needed_exp, gpseq);
908 spin_unlock_irq_rcu_node(sup);
909 mutex_unlock(&sup->srcu_gp_mutex);
910 /* A new grace period can start at this point. But only one. */
911
912 /* Initiate callback invocation as needed. */
913 ss_state = smp_load_acquire(&sup->srcu_size_state);
914 if (ss_state < SRCU_SIZE_WAIT_BARRIER) {
915 srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, get_boot_cpu_id()),
916 cbdelay);
917 } else {
918 idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
919 srcu_for_each_node_breadth_first(ssp, snp) {
920 spin_lock_irq_rcu_node(snp);
921 cbs = false;
922 last_lvl = snp >= sup->level[rcu_num_lvls - 1];
923 if (last_lvl)
924 cbs = ss_state < SRCU_SIZE_BIG || snp->srcu_have_cbs[idx] == gpseq;
925 snp->srcu_have_cbs[idx] = gpseq;
926 rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
927 sgsne = snp->srcu_gp_seq_needed_exp;
928 if (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, gpseq))
929 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, gpseq);
930 if (ss_state < SRCU_SIZE_BIG)
931 mask = ~0;
932 else
933 mask = snp->srcu_data_have_cbs[idx];
934 snp->srcu_data_have_cbs[idx] = 0;
935 spin_unlock_irq_rcu_node(snp);
936 if (cbs)
937 srcu_schedule_cbs_snp(ssp, snp, mask, cbdelay);
938 }
939 }
940
941 /* Occasionally prevent srcu_data counter wrap. */
942 if (!(gpseq & counter_wrap_check))
943 for_each_possible_cpu(cpu) {
944 sdp = per_cpu_ptr(ssp->sda, cpu);
945 spin_lock_irq_rcu_node(sdp);
946 if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed + 100))
947 sdp->srcu_gp_seq_needed = gpseq;
948 if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed_exp + 100))
949 sdp->srcu_gp_seq_needed_exp = gpseq;
950 spin_unlock_irq_rcu_node(sdp);
951 }
952
953 /* Callback initiation done, allow grace periods after next. */
954 mutex_unlock(&sup->srcu_cb_mutex);
955
956 /* Start a new grace period if needed. */
957 spin_lock_irq_rcu_node(sup);
958 gpseq = rcu_seq_current(&sup->srcu_gp_seq);
959 if (!rcu_seq_state(gpseq) &&
960 ULONG_CMP_LT(gpseq, sup->srcu_gp_seq_needed)) {
961 srcu_gp_start(ssp);
962 spin_unlock_irq_rcu_node(sup);
963 srcu_reschedule(ssp, 0);
964 } else {
965 spin_unlock_irq_rcu_node(sup);
966 }
967
968 /* Transition to big if needed. */
969 if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
970 if (ss_state == SRCU_SIZE_ALLOC)
971 init_srcu_struct_nodes(ssp, GFP_KERNEL);
972 else
973 smp_store_release(&sup->srcu_size_state, ss_state + 1);
974 }
975 }
976
977 /*
978 * Funnel-locking scheme to scalably mediate many concurrent expedited
979 * grace-period requests. This function is invoked for the first known
980 * expedited request for a grace period that has already been requested,
981 * but without expediting. To start a completely new grace period,
982 * whether expedited or not, use srcu_funnel_gp_start() instead.
983 */
srcu_funnel_exp_start(struct srcu_struct * ssp,struct srcu_node * snp,unsigned long s)984 static void srcu_funnel_exp_start(struct srcu_struct *ssp, struct srcu_node *snp,
985 unsigned long s)
986 {
987 unsigned long flags;
988 unsigned long sgsne;
989
990 if (snp)
991 for (; snp != NULL; snp = snp->srcu_parent) {
992 sgsne = READ_ONCE(snp->srcu_gp_seq_needed_exp);
993 if (WARN_ON_ONCE(rcu_seq_done(&ssp->srcu_sup->srcu_gp_seq, s)) ||
994 (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)))
995 return;
996 spin_lock_irqsave_rcu_node(snp, flags);
997 sgsne = snp->srcu_gp_seq_needed_exp;
998 if (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)) {
999 spin_unlock_irqrestore_rcu_node(snp, flags);
1000 return;
1001 }
1002 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
1003 spin_unlock_irqrestore_rcu_node(snp, flags);
1004 }
1005 spin_lock_irqsave_ssp_contention(ssp, &flags);
1006 if (ULONG_CMP_LT(ssp->srcu_sup->srcu_gp_seq_needed_exp, s))
1007 WRITE_ONCE(ssp->srcu_sup->srcu_gp_seq_needed_exp, s);
1008 spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
1009 }
1010
1011 /*
1012 * Funnel-locking scheme to scalably mediate many concurrent grace-period
1013 * requests. The winner has to do the work of actually starting grace
1014 * period s. Losers must either ensure that their desired grace-period
1015 * number is recorded on at least their leaf srcu_node structure, or they
1016 * must take steps to invoke their own callbacks.
1017 *
1018 * Note that this function also does the work of srcu_funnel_exp_start(),
1019 * in some cases by directly invoking it.
1020 *
1021 * The srcu read lock should be hold around this function. And s is a seq snap
1022 * after holding that lock.
1023 */
srcu_funnel_gp_start(struct srcu_struct * ssp,struct srcu_data * sdp,unsigned long s,bool do_norm)1024 static void srcu_funnel_gp_start(struct srcu_struct *ssp, struct srcu_data *sdp,
1025 unsigned long s, bool do_norm)
1026 {
1027 unsigned long flags;
1028 int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
1029 unsigned long sgsne;
1030 struct srcu_node *snp;
1031 struct srcu_node *snp_leaf;
1032 unsigned long snp_seq;
1033 struct srcu_usage *sup = ssp->srcu_sup;
1034
1035 /* Ensure that snp node tree is fully initialized before traversing it */
1036 if (smp_load_acquire(&sup->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
1037 snp_leaf = NULL;
1038 else
1039 snp_leaf = sdp->mynode;
1040
1041 if (snp_leaf)
1042 /* Each pass through the loop does one level of the srcu_node tree. */
1043 for (snp = snp_leaf; snp != NULL; snp = snp->srcu_parent) {
1044 if (WARN_ON_ONCE(rcu_seq_done(&sup->srcu_gp_seq, s)) && snp != snp_leaf)
1045 return; /* GP already done and CBs recorded. */
1046 spin_lock_irqsave_rcu_node(snp, flags);
1047 snp_seq = snp->srcu_have_cbs[idx];
1048 if (!srcu_invl_snp_seq(snp_seq) && ULONG_CMP_GE(snp_seq, s)) {
1049 if (snp == snp_leaf && snp_seq == s)
1050 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
1051 spin_unlock_irqrestore_rcu_node(snp, flags);
1052 if (snp == snp_leaf && snp_seq != s) {
1053 srcu_schedule_cbs_sdp(sdp, do_norm ? SRCU_INTERVAL : 0);
1054 return;
1055 }
1056 if (!do_norm)
1057 srcu_funnel_exp_start(ssp, snp, s);
1058 return;
1059 }
1060 snp->srcu_have_cbs[idx] = s;
1061 if (snp == snp_leaf)
1062 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
1063 sgsne = snp->srcu_gp_seq_needed_exp;
1064 if (!do_norm && (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, s)))
1065 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
1066 spin_unlock_irqrestore_rcu_node(snp, flags);
1067 }
1068
1069 /* Top of tree, must ensure the grace period will be started. */
1070 spin_lock_irqsave_ssp_contention(ssp, &flags);
1071 if (ULONG_CMP_LT(sup->srcu_gp_seq_needed, s)) {
1072 /*
1073 * Record need for grace period s. Pair with load
1074 * acquire setting up for initialization.
1075 */
1076 smp_store_release(&sup->srcu_gp_seq_needed, s); /*^^^*/
1077 }
1078 if (!do_norm && ULONG_CMP_LT(sup->srcu_gp_seq_needed_exp, s))
1079 WRITE_ONCE(sup->srcu_gp_seq_needed_exp, s);
1080
1081 /* If grace period not already in progress, start it. */
1082 if (!WARN_ON_ONCE(rcu_seq_done(&sup->srcu_gp_seq, s)) &&
1083 rcu_seq_state(sup->srcu_gp_seq) == SRCU_STATE_IDLE) {
1084 srcu_gp_start(ssp);
1085
1086 // And how can that list_add() in the "else" clause
1087 // possibly be safe for concurrent execution? Well,
1088 // it isn't. And it does not have to be. After all, it
1089 // can only be executed during early boot when there is only
1090 // the one boot CPU running with interrupts still disabled.
1091 if (likely(srcu_init_done))
1092 queue_delayed_work(rcu_gp_wq, &sup->work,
1093 !!srcu_get_delay(ssp));
1094 else if (list_empty(&sup->work.work.entry))
1095 list_add(&sup->work.work.entry, &srcu_boot_list);
1096 }
1097 spin_unlock_irqrestore_rcu_node(sup, flags);
1098 }
1099
1100 /*
1101 * Wait until all readers counted by array index idx complete, but
1102 * loop an additional time if there is an expedited grace period pending.
1103 * The caller must ensure that ->srcu_ctrp is not changed while checking.
1104 */
try_check_zero(struct srcu_struct * ssp,int idx,int trycount)1105 static bool try_check_zero(struct srcu_struct *ssp, int idx, int trycount)
1106 {
1107 unsigned long curdelay;
1108
1109 spin_lock_irq_rcu_node(ssp->srcu_sup);
1110 curdelay = !srcu_get_delay(ssp);
1111 spin_unlock_irq_rcu_node(ssp->srcu_sup);
1112
1113 for (;;) {
1114 if (srcu_readers_active_idx_check(ssp, idx))
1115 return true;
1116 if ((--trycount + curdelay) <= 0)
1117 return false;
1118 udelay(srcu_retry_check_delay);
1119 }
1120 }
1121
1122 /*
1123 * Increment the ->srcu_ctrp counter so that future SRCU readers will
1124 * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows
1125 * us to wait for pre-existing readers in a starvation-free manner.
1126 */
srcu_flip(struct srcu_struct * ssp)1127 static void srcu_flip(struct srcu_struct *ssp)
1128 {
1129 /*
1130 * Because the flip of ->srcu_ctrp is executed only if the
1131 * preceding call to srcu_readers_active_idx_check() found that
1132 * the ->srcu_ctrs[].srcu_unlocks and ->srcu_ctrs[].srcu_locks sums
1133 * matched and because that summing uses atomic_long_read(),
1134 * there is ordering due to a control dependency between that
1135 * summing and the WRITE_ONCE() in this call to srcu_flip().
1136 * This ordering ensures that if this updater saw a given reader's
1137 * increment from __srcu_read_lock(), that reader was using a value
1138 * of ->srcu_ctrp from before the previous call to srcu_flip(),
1139 * which should be quite rare. This ordering thus helps forward
1140 * progress because the grace period could otherwise be delayed
1141 * by additional calls to __srcu_read_lock() using that old (soon
1142 * to be new) value of ->srcu_ctrp.
1143 *
1144 * This sum-equality check and ordering also ensures that if
1145 * a given call to __srcu_read_lock() uses the new value of
1146 * ->srcu_ctrp, this updater's earlier scans cannot have seen
1147 * that reader's increments, which is all to the good, because
1148 * this grace period need not wait on that reader. After all,
1149 * if those earlier scans had seen that reader, there would have
1150 * been a sum mismatch and this code would not be reached.
1151 *
1152 * This means that the following smp_mb() is redundant, but
1153 * it stays until either (1) Compilers learn about this sort of
1154 * control dependency or (2) Some production workload running on
1155 * a production system is unduly delayed by this slowpath smp_mb().
1156 * Except for _lite() readers, where it is inoperative, which
1157 * means that it is a good thing that it is redundant.
1158 */
1159 smp_mb(); /* E */ /* Pairs with B and C. */
1160
1161 WRITE_ONCE(ssp->srcu_ctrp,
1162 &ssp->sda->srcu_ctrs[!(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0])]);
1163
1164 /*
1165 * Ensure that if the updater misses an __srcu_read_unlock()
1166 * increment, that task's __srcu_read_lock() following its next
1167 * __srcu_read_lock() or __srcu_read_unlock() will see the above
1168 * counter update. Note that both this memory barrier and the
1169 * one in srcu_readers_active_idx_check() provide the guarantee
1170 * for __srcu_read_lock().
1171 */
1172 smp_mb(); /* D */ /* Pairs with C. */
1173 }
1174
1175 /*
1176 * If SRCU is likely idle, in other words, the next SRCU grace period
1177 * should be expedited, return true, otherwise return false. Except that
1178 * in the presence of _lite() readers, always return false.
1179 *
1180 * Note that it is OK for several current from-idle requests for a new
1181 * grace period from idle to specify expediting because they will all end
1182 * up requesting the same grace period anyhow. So no loss.
1183 *
1184 * Note also that if any CPU (including the current one) is still invoking
1185 * callbacks, this function will nevertheless say "idle". This is not
1186 * ideal, but the overhead of checking all CPUs' callback lists is even
1187 * less ideal, especially on large systems. Furthermore, the wakeup
1188 * can happen before the callback is fully removed, so we have no choice
1189 * but to accept this type of error.
1190 *
1191 * This function is also subject to counter-wrap errors, but let's face
1192 * it, if this function was preempted for enough time for the counters
1193 * to wrap, it really doesn't matter whether or not we expedite the grace
1194 * period. The extra overhead of a needlessly expedited grace period is
1195 * negligible when amortized over that time period, and the extra latency
1196 * of a needlessly non-expedited grace period is similarly negligible.
1197 */
srcu_should_expedite(struct srcu_struct * ssp)1198 static bool srcu_should_expedite(struct srcu_struct *ssp)
1199 {
1200 unsigned long curseq;
1201 unsigned long flags;
1202 struct srcu_data *sdp;
1203 unsigned long t;
1204 unsigned long tlast;
1205
1206 check_init_srcu_struct(ssp);
1207 /* If _lite() readers, don't do unsolicited expediting. */
1208 if (this_cpu_read(ssp->sda->srcu_reader_flavor) & SRCU_READ_FLAVOR_SLOWGP)
1209 return false;
1210 /* If the local srcu_data structure has callbacks, not idle. */
1211 sdp = raw_cpu_ptr(ssp->sda);
1212 spin_lock_irqsave_rcu_node(sdp, flags);
1213 if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
1214 spin_unlock_irqrestore_rcu_node(sdp, flags);
1215 return false; /* Callbacks already present, so not idle. */
1216 }
1217 spin_unlock_irqrestore_rcu_node(sdp, flags);
1218
1219 /*
1220 * No local callbacks, so probabilistically probe global state.
1221 * Exact information would require acquiring locks, which would
1222 * kill scalability, hence the probabilistic nature of the probe.
1223 */
1224
1225 /* First, see if enough time has passed since the last GP. */
1226 t = ktime_get_mono_fast_ns();
1227 tlast = READ_ONCE(ssp->srcu_sup->srcu_last_gp_end);
1228 if (exp_holdoff == 0 ||
1229 time_in_range_open(t, tlast, tlast + exp_holdoff))
1230 return false; /* Too soon after last GP. */
1231
1232 /* Next, check for probable idleness. */
1233 curseq = rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq);
1234 smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
1235 if (ULONG_CMP_LT(curseq, READ_ONCE(ssp->srcu_sup->srcu_gp_seq_needed)))
1236 return false; /* Grace period in progress, so not idle. */
1237 smp_mb(); /* Order ->srcu_gp_seq with prior access. */
1238 if (curseq != rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq))
1239 return false; /* GP # changed, so not idle. */
1240 return true; /* With reasonable probability, idle! */
1241 }
1242
1243 /*
1244 * SRCU callback function to leak a callback.
1245 */
srcu_leak_callback(struct rcu_head * rhp)1246 static void srcu_leak_callback(struct rcu_head *rhp)
1247 {
1248 }
1249
1250 /*
1251 * Start an SRCU grace period, and also queue the callback if non-NULL.
1252 */
srcu_gp_start_if_needed(struct srcu_struct * ssp,struct rcu_head * rhp,bool do_norm)1253 static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp,
1254 struct rcu_head *rhp, bool do_norm)
1255 {
1256 unsigned long flags;
1257 int idx;
1258 bool needexp = false;
1259 bool needgp = false;
1260 unsigned long s;
1261 struct srcu_data *sdp;
1262 struct srcu_node *sdp_mynode;
1263 int ss_state;
1264
1265 check_init_srcu_struct(ssp);
1266 /*
1267 * While starting a new grace period, make sure we are in an
1268 * SRCU read-side critical section so that the grace-period
1269 * sequence number cannot wrap around in the meantime.
1270 */
1271 idx = __srcu_read_lock_nmisafe(ssp);
1272 ss_state = smp_load_acquire(&ssp->srcu_sup->srcu_size_state);
1273 if (ss_state < SRCU_SIZE_WAIT_CALL)
1274 sdp = per_cpu_ptr(ssp->sda, get_boot_cpu_id());
1275 else
1276 sdp = raw_cpu_ptr(ssp->sda);
1277 spin_lock_irqsave_sdp_contention(sdp, &flags);
1278 if (rhp)
1279 rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp);
1280 /*
1281 * It's crucial to capture the snapshot 's' for acceleration before
1282 * reading the current gp_seq that is used for advancing. This is
1283 * essential because if the acceleration snapshot is taken after a
1284 * failed advancement attempt, there's a risk that a grace period may
1285 * conclude and a new one may start in the interim. If the snapshot is
1286 * captured after this sequence of events, the acceleration snapshot 's'
1287 * could be excessively advanced, leading to acceleration failure.
1288 * In such a scenario, an 'acceleration leak' can occur, where new
1289 * callbacks become indefinitely stuck in the RCU_NEXT_TAIL segment.
1290 * Also note that encountering advancing failures is a normal
1291 * occurrence when the grace period for RCU_WAIT_TAIL is in progress.
1292 *
1293 * To see this, consider the following events which occur if
1294 * rcu_seq_snap() were to be called after advance:
1295 *
1296 * 1) The RCU_WAIT_TAIL segment has callbacks (gp_num = X + 4) and the
1297 * RCU_NEXT_READY_TAIL also has callbacks (gp_num = X + 8).
1298 *
1299 * 2) The grace period for RCU_WAIT_TAIL is seen as started but not
1300 * completed so rcu_seq_current() returns X + SRCU_STATE_SCAN1.
1301 *
1302 * 3) This value is passed to rcu_segcblist_advance() which can't move
1303 * any segment forward and fails.
1304 *
1305 * 4) srcu_gp_start_if_needed() still proceeds with callback acceleration.
1306 * But then the call to rcu_seq_snap() observes the grace period for the
1307 * RCU_WAIT_TAIL segment as completed and the subsequent one for the
1308 * RCU_NEXT_READY_TAIL segment as started (ie: X + 4 + SRCU_STATE_SCAN1)
1309 * so it returns a snapshot of the next grace period, which is X + 12.
1310 *
1311 * 5) The value of X + 12 is passed to rcu_segcblist_accelerate() but the
1312 * freshly enqueued callback in RCU_NEXT_TAIL can't move to
1313 * RCU_NEXT_READY_TAIL which already has callbacks for a previous grace
1314 * period (gp_num = X + 8). So acceleration fails.
1315 */
1316 s = rcu_seq_snap(&ssp->srcu_sup->srcu_gp_seq);
1317 if (rhp) {
1318 rcu_segcblist_advance(&sdp->srcu_cblist,
1319 rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
1320 /*
1321 * Acceleration can never fail because the base current gp_seq
1322 * used for acceleration is <= the value of gp_seq used for
1323 * advancing. This means that RCU_NEXT_TAIL segment will
1324 * always be able to be emptied by the acceleration into the
1325 * RCU_NEXT_READY_TAIL or RCU_WAIT_TAIL segments.
1326 */
1327 WARN_ON_ONCE(!rcu_segcblist_accelerate(&sdp->srcu_cblist, s));
1328 }
1329 if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
1330 sdp->srcu_gp_seq_needed = s;
1331 needgp = true;
1332 }
1333 if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
1334 sdp->srcu_gp_seq_needed_exp = s;
1335 needexp = true;
1336 }
1337 spin_unlock_irqrestore_rcu_node(sdp, flags);
1338
1339 /* Ensure that snp node tree is fully initialized before traversing it */
1340 if (ss_state < SRCU_SIZE_WAIT_BARRIER)
1341 sdp_mynode = NULL;
1342 else
1343 sdp_mynode = sdp->mynode;
1344
1345 if (needgp)
1346 srcu_funnel_gp_start(ssp, sdp, s, do_norm);
1347 else if (needexp)
1348 srcu_funnel_exp_start(ssp, sdp_mynode, s);
1349 __srcu_read_unlock_nmisafe(ssp, idx);
1350 return s;
1351 }
1352
1353 /*
1354 * Enqueue an SRCU callback on the srcu_data structure associated with
1355 * the current CPU and the specified srcu_struct structure, initiating
1356 * grace-period processing if it is not already running.
1357 *
1358 * Note that all CPUs must agree that the grace period extended beyond
1359 * all pre-existing SRCU read-side critical section. On systems with
1360 * more than one CPU, this means that when "func()" is invoked, each CPU
1361 * is guaranteed to have executed a full memory barrier since the end of
1362 * its last corresponding SRCU read-side critical section whose beginning
1363 * preceded the call to call_srcu(). It also means that each CPU executing
1364 * an SRCU read-side critical section that continues beyond the start of
1365 * "func()" must have executed a memory barrier after the call_srcu()
1366 * but before the beginning of that SRCU read-side critical section.
1367 * Note that these guarantees include CPUs that are offline, idle, or
1368 * executing in user mode, as well as CPUs that are executing in the kernel.
1369 *
1370 * Furthermore, if CPU A invoked call_srcu() and CPU B invoked the
1371 * resulting SRCU callback function "func()", then both CPU A and CPU
1372 * B are guaranteed to execute a full memory barrier during the time
1373 * interval between the call to call_srcu() and the invocation of "func()".
1374 * This guarantee applies even if CPU A and CPU B are the same CPU (but
1375 * again only if the system has more than one CPU).
1376 *
1377 * Of course, these guarantees apply only for invocations of call_srcu(),
1378 * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
1379 * srcu_struct structure.
1380 */
__call_srcu(struct srcu_struct * ssp,struct rcu_head * rhp,rcu_callback_t func,bool do_norm)1381 static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
1382 rcu_callback_t func, bool do_norm)
1383 {
1384 if (debug_rcu_head_queue(rhp)) {
1385 /* Probable double call_srcu(), so leak the callback. */
1386 WRITE_ONCE(rhp->func, srcu_leak_callback);
1387 WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n");
1388 return;
1389 }
1390 rhp->func = func;
1391 (void)srcu_gp_start_if_needed(ssp, rhp, do_norm);
1392 }
1393
1394 /**
1395 * call_srcu() - Queue a callback for invocation after an SRCU grace period
1396 * @ssp: srcu_struct in queue the callback
1397 * @rhp: structure to be used for queueing the SRCU callback.
1398 * @func: function to be invoked after the SRCU grace period
1399 *
1400 * The callback function will be invoked some time after a full SRCU
1401 * grace period elapses, in other words after all pre-existing SRCU
1402 * read-side critical sections have completed. However, the callback
1403 * function might well execute concurrently with other SRCU read-side
1404 * critical sections that started after call_srcu() was invoked. SRCU
1405 * read-side critical sections are delimited by srcu_read_lock() and
1406 * srcu_read_unlock(), and may be nested.
1407 *
1408 * The callback will be invoked from process context, but with bh
1409 * disabled. The callback function must therefore be fast and must
1410 * not block.
1411 *
1412 * See the description of call_rcu() for more detailed information on
1413 * memory ordering guarantees.
1414 */
call_srcu(struct srcu_struct * ssp,struct rcu_head * rhp,rcu_callback_t func)1415 void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
1416 rcu_callback_t func)
1417 {
1418 __call_srcu(ssp, rhp, func, true);
1419 }
1420 EXPORT_SYMBOL_GPL(call_srcu);
1421
1422 /*
1423 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
1424 */
__synchronize_srcu(struct srcu_struct * ssp,bool do_norm)1425 static void __synchronize_srcu(struct srcu_struct *ssp, bool do_norm)
1426 {
1427 struct rcu_synchronize rcu;
1428
1429 srcu_lock_sync(&ssp->dep_map);
1430
1431 RCU_LOCKDEP_WARN(lockdep_is_held(ssp) ||
1432 lock_is_held(&rcu_bh_lock_map) ||
1433 lock_is_held(&rcu_lock_map) ||
1434 lock_is_held(&rcu_sched_lock_map),
1435 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
1436
1437 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
1438 return;
1439 might_sleep();
1440 check_init_srcu_struct(ssp);
1441 init_completion(&rcu.completion);
1442 init_rcu_head_on_stack(&rcu.head);
1443 __call_srcu(ssp, &rcu.head, wakeme_after_rcu, do_norm);
1444 wait_for_completion(&rcu.completion);
1445 destroy_rcu_head_on_stack(&rcu.head);
1446
1447 /*
1448 * Make sure that later code is ordered after the SRCU grace
1449 * period. This pairs with the spin_lock_irq_rcu_node()
1450 * in srcu_invoke_callbacks(). Unlike Tree RCU, this is needed
1451 * because the current CPU might have been totally uninvolved with
1452 * (and thus unordered against) that grace period.
1453 */
1454 smp_mb();
1455 }
1456
1457 /**
1458 * synchronize_srcu_expedited - Brute-force SRCU grace period
1459 * @ssp: srcu_struct with which to synchronize.
1460 *
1461 * Wait for an SRCU grace period to elapse, but be more aggressive about
1462 * spinning rather than blocking when waiting.
1463 *
1464 * Note that synchronize_srcu_expedited() has the same deadlock and
1465 * memory-ordering properties as does synchronize_srcu().
1466 */
synchronize_srcu_expedited(struct srcu_struct * ssp)1467 void synchronize_srcu_expedited(struct srcu_struct *ssp)
1468 {
1469 __synchronize_srcu(ssp, rcu_gp_is_normal());
1470 }
1471 EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
1472
1473 /**
1474 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
1475 * @ssp: srcu_struct with which to synchronize.
1476 *
1477 * Wait for the count to drain to zero of both indexes. To avoid the
1478 * possible starvation of synchronize_srcu(), it waits for the count of
1479 * the index=!(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0]) to drain to zero
1480 * at first, and then flip the ->srcu_ctrp and wait for the count of the
1481 * other index.
1482 *
1483 * Can block; must be called from process context.
1484 *
1485 * Note that it is illegal to call synchronize_srcu() from the corresponding
1486 * SRCU read-side critical section; doing so will result in deadlock.
1487 * However, it is perfectly legal to call synchronize_srcu() on one
1488 * srcu_struct from some other srcu_struct's read-side critical section,
1489 * as long as the resulting graph of srcu_structs is acyclic.
1490 *
1491 * There are memory-ordering constraints implied by synchronize_srcu().
1492 * On systems with more than one CPU, when synchronize_srcu() returns,
1493 * each CPU is guaranteed to have executed a full memory barrier since
1494 * the end of its last corresponding SRCU read-side critical section
1495 * whose beginning preceded the call to synchronize_srcu(). In addition,
1496 * each CPU having an SRCU read-side critical section that extends beyond
1497 * the return from synchronize_srcu() is guaranteed to have executed a
1498 * full memory barrier after the beginning of synchronize_srcu() and before
1499 * the beginning of that SRCU read-side critical section. Note that these
1500 * guarantees include CPUs that are offline, idle, or executing in user mode,
1501 * as well as CPUs that are executing in the kernel.
1502 *
1503 * Furthermore, if CPU A invoked synchronize_srcu(), which returned
1504 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
1505 * to have executed a full memory barrier during the execution of
1506 * synchronize_srcu(). This guarantee applies even if CPU A and CPU B
1507 * are the same CPU, but again only if the system has more than one CPU.
1508 *
1509 * Of course, these memory-ordering guarantees apply only when
1510 * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
1511 * passed the same srcu_struct structure.
1512 *
1513 * Implementation of these memory-ordering guarantees is similar to
1514 * that of synchronize_rcu().
1515 *
1516 * If SRCU is likely idle as determined by srcu_should_expedite(),
1517 * expedite the first request. This semantic was provided by Classic SRCU,
1518 * and is relied upon by its users, so TREE SRCU must also provide it.
1519 * Note that detecting idleness is heuristic and subject to both false
1520 * positives and negatives.
1521 */
synchronize_srcu(struct srcu_struct * ssp)1522 void synchronize_srcu(struct srcu_struct *ssp)
1523 {
1524 if (srcu_should_expedite(ssp) || rcu_gp_is_expedited())
1525 synchronize_srcu_expedited(ssp);
1526 else
1527 __synchronize_srcu(ssp, true);
1528 }
1529 EXPORT_SYMBOL_GPL(synchronize_srcu);
1530
1531 /**
1532 * get_state_synchronize_srcu - Provide an end-of-grace-period cookie
1533 * @ssp: srcu_struct to provide cookie for.
1534 *
1535 * This function returns a cookie that can be passed to
1536 * poll_state_synchronize_srcu(), which will return true if a full grace
1537 * period has elapsed in the meantime. It is the caller's responsibility
1538 * to make sure that grace period happens, for example, by invoking
1539 * call_srcu() after return from get_state_synchronize_srcu().
1540 */
get_state_synchronize_srcu(struct srcu_struct * ssp)1541 unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp)
1542 {
1543 // Any prior manipulation of SRCU-protected data must happen
1544 // before the load from ->srcu_gp_seq.
1545 smp_mb();
1546 return rcu_seq_snap(&ssp->srcu_sup->srcu_gp_seq);
1547 }
1548 EXPORT_SYMBOL_GPL(get_state_synchronize_srcu);
1549
1550 /**
1551 * start_poll_synchronize_srcu - Provide cookie and start grace period
1552 * @ssp: srcu_struct to provide cookie for.
1553 *
1554 * This function returns a cookie that can be passed to
1555 * poll_state_synchronize_srcu(), which will return true if a full grace
1556 * period has elapsed in the meantime. Unlike get_state_synchronize_srcu(),
1557 * this function also ensures that any needed SRCU grace period will be
1558 * started. This convenience does come at a cost in terms of CPU overhead.
1559 */
start_poll_synchronize_srcu(struct srcu_struct * ssp)1560 unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp)
1561 {
1562 return srcu_gp_start_if_needed(ssp, NULL, true);
1563 }
1564 EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu);
1565
1566 /**
1567 * poll_state_synchronize_srcu - Has cookie's grace period ended?
1568 * @ssp: srcu_struct to provide cookie for.
1569 * @cookie: Return value from get_state_synchronize_srcu() or start_poll_synchronize_srcu().
1570 *
1571 * This function takes the cookie that was returned from either
1572 * get_state_synchronize_srcu() or start_poll_synchronize_srcu(), and
1573 * returns @true if an SRCU grace period elapsed since the time that the
1574 * cookie was created.
1575 *
1576 * Because cookies are finite in size, wrapping/overflow is possible.
1577 * This is more pronounced on 32-bit systems where cookies are 32 bits,
1578 * where in theory wrapping could happen in about 14 hours assuming
1579 * 25-microsecond expedited SRCU grace periods. However, a more likely
1580 * overflow lower bound is on the order of 24 days in the case of
1581 * one-millisecond SRCU grace periods. Of course, wrapping in a 64-bit
1582 * system requires geologic timespans, as in more than seven million years
1583 * even for expedited SRCU grace periods.
1584 *
1585 * Wrapping/overflow is much more of an issue for CONFIG_SMP=n systems
1586 * that also have CONFIG_PREEMPTION=n, which selects Tiny SRCU. This uses
1587 * a 16-bit cookie, which rcutorture routinely wraps in a matter of a
1588 * few minutes. If this proves to be a problem, this counter will be
1589 * expanded to the same size as for Tree SRCU.
1590 */
poll_state_synchronize_srcu(struct srcu_struct * ssp,unsigned long cookie)1591 bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie)
1592 {
1593 if (cookie != SRCU_GET_STATE_COMPLETED &&
1594 !rcu_seq_done_exact(&ssp->srcu_sup->srcu_gp_seq, cookie))
1595 return false;
1596 // Ensure that the end of the SRCU grace period happens before
1597 // any subsequent code that the caller might execute.
1598 smp_mb(); // ^^^
1599 return true;
1600 }
1601 EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu);
1602
1603 /*
1604 * Callback function for srcu_barrier() use.
1605 */
srcu_barrier_cb(struct rcu_head * rhp)1606 static void srcu_barrier_cb(struct rcu_head *rhp)
1607 {
1608 struct srcu_data *sdp;
1609 struct srcu_struct *ssp;
1610
1611 rhp->next = rhp; // Mark the callback as having been invoked.
1612 sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
1613 ssp = sdp->ssp;
1614 if (atomic_dec_and_test(&ssp->srcu_sup->srcu_barrier_cpu_cnt))
1615 complete(&ssp->srcu_sup->srcu_barrier_completion);
1616 }
1617
1618 /*
1619 * Enqueue an srcu_barrier() callback on the specified srcu_data
1620 * structure's ->cblist. but only if that ->cblist already has at least one
1621 * callback enqueued. Note that if a CPU already has callbacks enqueue,
1622 * it must have already registered the need for a future grace period,
1623 * so all we need do is enqueue a callback that will use the same grace
1624 * period as the last callback already in the queue.
1625 */
srcu_barrier_one_cpu(struct srcu_struct * ssp,struct srcu_data * sdp)1626 static void srcu_barrier_one_cpu(struct srcu_struct *ssp, struct srcu_data *sdp)
1627 {
1628 spin_lock_irq_rcu_node(sdp);
1629 atomic_inc(&ssp->srcu_sup->srcu_barrier_cpu_cnt);
1630 sdp->srcu_barrier_head.func = srcu_barrier_cb;
1631 debug_rcu_head_queue(&sdp->srcu_barrier_head);
1632 if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
1633 &sdp->srcu_barrier_head)) {
1634 debug_rcu_head_unqueue(&sdp->srcu_barrier_head);
1635 atomic_dec(&ssp->srcu_sup->srcu_barrier_cpu_cnt);
1636 }
1637 spin_unlock_irq_rcu_node(sdp);
1638 }
1639
1640 /**
1641 * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
1642 * @ssp: srcu_struct on which to wait for in-flight callbacks.
1643 */
srcu_barrier(struct srcu_struct * ssp)1644 void srcu_barrier(struct srcu_struct *ssp)
1645 {
1646 int cpu;
1647 int idx;
1648 unsigned long s = rcu_seq_snap(&ssp->srcu_sup->srcu_barrier_seq);
1649
1650 check_init_srcu_struct(ssp);
1651 mutex_lock(&ssp->srcu_sup->srcu_barrier_mutex);
1652 if (rcu_seq_done(&ssp->srcu_sup->srcu_barrier_seq, s)) {
1653 smp_mb(); /* Force ordering following return. */
1654 mutex_unlock(&ssp->srcu_sup->srcu_barrier_mutex);
1655 return; /* Someone else did our work for us. */
1656 }
1657 rcu_seq_start(&ssp->srcu_sup->srcu_barrier_seq);
1658 init_completion(&ssp->srcu_sup->srcu_barrier_completion);
1659
1660 /* Initial count prevents reaching zero until all CBs are posted. */
1661 atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 1);
1662
1663 idx = __srcu_read_lock_nmisafe(ssp);
1664 if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
1665 srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, get_boot_cpu_id()));
1666 else
1667 for_each_possible_cpu(cpu)
1668 srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, cpu));
1669 __srcu_read_unlock_nmisafe(ssp, idx);
1670
1671 /* Remove the initial count, at which point reaching zero can happen. */
1672 if (atomic_dec_and_test(&ssp->srcu_sup->srcu_barrier_cpu_cnt))
1673 complete(&ssp->srcu_sup->srcu_barrier_completion);
1674 wait_for_completion(&ssp->srcu_sup->srcu_barrier_completion);
1675
1676 rcu_seq_end(&ssp->srcu_sup->srcu_barrier_seq);
1677 mutex_unlock(&ssp->srcu_sup->srcu_barrier_mutex);
1678 }
1679 EXPORT_SYMBOL_GPL(srcu_barrier);
1680
1681 /**
1682 * srcu_batches_completed - return batches completed.
1683 * @ssp: srcu_struct on which to report batch completion.
1684 *
1685 * Report the number of batches, correlated with, but not necessarily
1686 * precisely the same as, the number of grace periods that have elapsed.
1687 */
srcu_batches_completed(struct srcu_struct * ssp)1688 unsigned long srcu_batches_completed(struct srcu_struct *ssp)
1689 {
1690 return READ_ONCE(ssp->srcu_sup->srcu_gp_seq);
1691 }
1692 EXPORT_SYMBOL_GPL(srcu_batches_completed);
1693
1694 /*
1695 * Core SRCU state machine. Push state bits of ->srcu_gp_seq
1696 * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
1697 * completed in that state.
1698 */
srcu_advance_state(struct srcu_struct * ssp)1699 static void srcu_advance_state(struct srcu_struct *ssp)
1700 {
1701 int idx;
1702
1703 mutex_lock(&ssp->srcu_sup->srcu_gp_mutex);
1704
1705 /*
1706 * Because readers might be delayed for an extended period after
1707 * fetching ->srcu_ctrp for their index, at any point in time there
1708 * might well be readers using both idx=0 and idx=1. We therefore
1709 * need to wait for readers to clear from both index values before
1710 * invoking a callback.
1711 *
1712 * The load-acquire ensures that we see the accesses performed
1713 * by the prior grace period.
1714 */
1715 idx = rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq)); /* ^^^ */
1716 if (idx == SRCU_STATE_IDLE) {
1717 spin_lock_irq_rcu_node(ssp->srcu_sup);
1718 if (ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed)) {
1719 WARN_ON_ONCE(rcu_seq_state(ssp->srcu_sup->srcu_gp_seq));
1720 spin_unlock_irq_rcu_node(ssp->srcu_sup);
1721 mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
1722 return;
1723 }
1724 idx = rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq));
1725 if (idx == SRCU_STATE_IDLE)
1726 srcu_gp_start(ssp);
1727 spin_unlock_irq_rcu_node(ssp->srcu_sup);
1728 if (idx != SRCU_STATE_IDLE) {
1729 mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
1730 return; /* Someone else started the grace period. */
1731 }
1732 }
1733
1734 if (rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
1735 idx = !(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0]);
1736 if (!try_check_zero(ssp, idx, 1)) {
1737 mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
1738 return; /* readers present, retry later. */
1739 }
1740 srcu_flip(ssp);
1741 spin_lock_irq_rcu_node(ssp->srcu_sup);
1742 rcu_seq_set_state(&ssp->srcu_sup->srcu_gp_seq, SRCU_STATE_SCAN2);
1743 ssp->srcu_sup->srcu_n_exp_nodelay = 0;
1744 spin_unlock_irq_rcu_node(ssp->srcu_sup);
1745 }
1746
1747 if (rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
1748
1749 /*
1750 * SRCU read-side critical sections are normally short,
1751 * so check at least twice in quick succession after a flip.
1752 */
1753 idx = !(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0]);
1754 if (!try_check_zero(ssp, idx, 2)) {
1755 mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
1756 return; /* readers present, retry later. */
1757 }
1758 ssp->srcu_sup->srcu_n_exp_nodelay = 0;
1759 srcu_gp_end(ssp); /* Releases ->srcu_gp_mutex. */
1760 }
1761 }
1762
1763 /*
1764 * Invoke a limited number of SRCU callbacks that have passed through
1765 * their grace period. If there are more to do, SRCU will reschedule
1766 * the workqueue. Note that needed memory barriers have been executed
1767 * in this task's context by srcu_readers_active_idx_check().
1768 */
srcu_invoke_callbacks(struct work_struct * work)1769 static void srcu_invoke_callbacks(struct work_struct *work)
1770 {
1771 long len;
1772 bool more;
1773 struct rcu_cblist ready_cbs;
1774 struct rcu_head *rhp;
1775 struct srcu_data *sdp;
1776 struct srcu_struct *ssp;
1777
1778 sdp = container_of(work, struct srcu_data, work);
1779
1780 ssp = sdp->ssp;
1781 rcu_cblist_init(&ready_cbs);
1782 spin_lock_irq_rcu_node(sdp);
1783 WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL));
1784 rcu_segcblist_advance(&sdp->srcu_cblist,
1785 rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
1786 /*
1787 * Although this function is theoretically re-entrant, concurrent
1788 * callbacks invocation is disallowed to avoid executing an SRCU barrier
1789 * too early.
1790 */
1791 if (sdp->srcu_cblist_invoking ||
1792 !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
1793 spin_unlock_irq_rcu_node(sdp);
1794 return; /* Someone else on the job or nothing to do. */
1795 }
1796
1797 /* We are on the job! Extract and invoke ready callbacks. */
1798 sdp->srcu_cblist_invoking = true;
1799 rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
1800 len = ready_cbs.len;
1801 spin_unlock_irq_rcu_node(sdp);
1802 rhp = rcu_cblist_dequeue(&ready_cbs);
1803 for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
1804 debug_rcu_head_unqueue(rhp);
1805 debug_rcu_head_callback(rhp);
1806 local_bh_disable();
1807 rhp->func(rhp);
1808 local_bh_enable();
1809 }
1810 WARN_ON_ONCE(ready_cbs.len);
1811
1812 /*
1813 * Update counts, accelerate new callbacks, and if needed,
1814 * schedule another round of callback invocation.
1815 */
1816 spin_lock_irq_rcu_node(sdp);
1817 rcu_segcblist_add_len(&sdp->srcu_cblist, -len);
1818 sdp->srcu_cblist_invoking = false;
1819 more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
1820 spin_unlock_irq_rcu_node(sdp);
1821 /* An SRCU barrier or callbacks from previous nesting work pending */
1822 if (more)
1823 srcu_schedule_cbs_sdp(sdp, 0);
1824 }
1825
1826 /*
1827 * Finished one round of SRCU grace period. Start another if there are
1828 * more SRCU callbacks queued, otherwise put SRCU into not-running state.
1829 */
srcu_reschedule(struct srcu_struct * ssp,unsigned long delay)1830 static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay)
1831 {
1832 bool pushgp = true;
1833
1834 spin_lock_irq_rcu_node(ssp->srcu_sup);
1835 if (ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed)) {
1836 if (!WARN_ON_ONCE(rcu_seq_state(ssp->srcu_sup->srcu_gp_seq))) {
1837 /* All requests fulfilled, time to go idle. */
1838 pushgp = false;
1839 }
1840 } else if (!rcu_seq_state(ssp->srcu_sup->srcu_gp_seq)) {
1841 /* Outstanding request and no GP. Start one. */
1842 srcu_gp_start(ssp);
1843 }
1844 spin_unlock_irq_rcu_node(ssp->srcu_sup);
1845
1846 if (pushgp)
1847 queue_delayed_work(rcu_gp_wq, &ssp->srcu_sup->work, delay);
1848 }
1849
1850 /*
1851 * This is the work-queue function that handles SRCU grace periods.
1852 */
process_srcu(struct work_struct * work)1853 static void process_srcu(struct work_struct *work)
1854 {
1855 unsigned long curdelay;
1856 unsigned long j;
1857 struct srcu_struct *ssp;
1858 struct srcu_usage *sup;
1859
1860 sup = container_of(work, struct srcu_usage, work.work);
1861 ssp = sup->srcu_ssp;
1862
1863 srcu_advance_state(ssp);
1864 spin_lock_irq_rcu_node(ssp->srcu_sup);
1865 curdelay = srcu_get_delay(ssp);
1866 spin_unlock_irq_rcu_node(ssp->srcu_sup);
1867 if (curdelay) {
1868 WRITE_ONCE(sup->reschedule_count, 0);
1869 } else {
1870 j = jiffies;
1871 if (READ_ONCE(sup->reschedule_jiffies) == j) {
1872 ASSERT_EXCLUSIVE_WRITER(sup->reschedule_count);
1873 WRITE_ONCE(sup->reschedule_count, READ_ONCE(sup->reschedule_count) + 1);
1874 if (READ_ONCE(sup->reschedule_count) > srcu_max_nodelay)
1875 curdelay = 1;
1876 } else {
1877 WRITE_ONCE(sup->reschedule_count, 1);
1878 WRITE_ONCE(sup->reschedule_jiffies, j);
1879 }
1880 }
1881 srcu_reschedule(ssp, curdelay);
1882 }
1883
srcutorture_get_gp_data(struct srcu_struct * ssp,int * flags,unsigned long * gp_seq)1884 void srcutorture_get_gp_data(struct srcu_struct *ssp, int *flags,
1885 unsigned long *gp_seq)
1886 {
1887 *flags = 0;
1888 *gp_seq = rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq);
1889 }
1890 EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
1891
1892 static const char * const srcu_size_state_name[] = {
1893 "SRCU_SIZE_SMALL",
1894 "SRCU_SIZE_ALLOC",
1895 "SRCU_SIZE_WAIT_BARRIER",
1896 "SRCU_SIZE_WAIT_CALL",
1897 "SRCU_SIZE_WAIT_CBS1",
1898 "SRCU_SIZE_WAIT_CBS2",
1899 "SRCU_SIZE_WAIT_CBS3",
1900 "SRCU_SIZE_WAIT_CBS4",
1901 "SRCU_SIZE_BIG",
1902 "SRCU_SIZE_???",
1903 };
1904
srcu_torture_stats_print(struct srcu_struct * ssp,char * tt,char * tf)1905 void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf)
1906 {
1907 int cpu;
1908 int idx;
1909 unsigned long s0 = 0, s1 = 0;
1910 int ss_state = READ_ONCE(ssp->srcu_sup->srcu_size_state);
1911 int ss_state_idx = ss_state;
1912
1913 idx = ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0];
1914 if (ss_state < 0 || ss_state >= ARRAY_SIZE(srcu_size_state_name))
1915 ss_state_idx = ARRAY_SIZE(srcu_size_state_name) - 1;
1916 pr_alert("%s%s Tree SRCU g%ld state %d (%s)",
1917 tt, tf, rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq), ss_state,
1918 srcu_size_state_name[ss_state_idx]);
1919 if (!ssp->sda) {
1920 // Called after cleanup_srcu_struct(), perhaps.
1921 pr_cont(" No per-CPU srcu_data structures (->sda == NULL).\n");
1922 } else {
1923 pr_cont(" per-CPU(idx=%d):", idx);
1924 for_each_possible_cpu(cpu) {
1925 unsigned long l0, l1;
1926 unsigned long u0, u1;
1927 long c0, c1;
1928 struct srcu_data *sdp;
1929
1930 sdp = per_cpu_ptr(ssp->sda, cpu);
1931 u0 = data_race(atomic_long_read(&sdp->srcu_ctrs[!idx].srcu_unlocks));
1932 u1 = data_race(atomic_long_read(&sdp->srcu_ctrs[idx].srcu_unlocks));
1933
1934 /*
1935 * Make sure that a lock is always counted if the corresponding
1936 * unlock is counted.
1937 */
1938 smp_rmb();
1939
1940 l0 = data_race(atomic_long_read(&sdp->srcu_ctrs[!idx].srcu_locks));
1941 l1 = data_race(atomic_long_read(&sdp->srcu_ctrs[idx].srcu_locks));
1942
1943 c0 = l0 - u0;
1944 c1 = l1 - u1;
1945 pr_cont(" %d(%ld,%ld %c)",
1946 cpu, c0, c1,
1947 "C."[rcu_segcblist_empty(&sdp->srcu_cblist)]);
1948 s0 += c0;
1949 s1 += c1;
1950 }
1951 pr_cont(" T(%ld,%ld)\n", s0, s1);
1952 }
1953 if (SRCU_SIZING_IS_TORTURE())
1954 srcu_transition_to_big(ssp);
1955 }
1956 EXPORT_SYMBOL_GPL(srcu_torture_stats_print);
1957
srcu_bootup_announce(void)1958 static int __init srcu_bootup_announce(void)
1959 {
1960 pr_info("Hierarchical SRCU implementation.\n");
1961 if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF)
1962 pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff);
1963 if (srcu_retry_check_delay != SRCU_DEFAULT_RETRY_CHECK_DELAY)
1964 pr_info("\tNon-default retry check delay of %lu us.\n", srcu_retry_check_delay);
1965 if (srcu_max_nodelay != SRCU_DEFAULT_MAX_NODELAY)
1966 pr_info("\tNon-default max no-delay of %lu.\n", srcu_max_nodelay);
1967 pr_info("\tMax phase no-delay instances is %lu.\n", srcu_max_nodelay_phase);
1968 return 0;
1969 }
1970 early_initcall(srcu_bootup_announce);
1971
srcu_init(void)1972 void __init srcu_init(void)
1973 {
1974 struct srcu_usage *sup;
1975
1976 /* Decide on srcu_struct-size strategy. */
1977 if (SRCU_SIZING_IS(SRCU_SIZING_AUTO)) {
1978 if (nr_cpu_ids >= big_cpu_lim) {
1979 convert_to_big = SRCU_SIZING_INIT; // Don't bother waiting for contention.
1980 pr_info("%s: Setting srcu_struct sizes to big.\n", __func__);
1981 } else {
1982 convert_to_big = SRCU_SIZING_NONE | SRCU_SIZING_CONTEND;
1983 pr_info("%s: Setting srcu_struct sizes based on contention.\n", __func__);
1984 }
1985 }
1986
1987 /*
1988 * Once that is set, call_srcu() can follow the normal path and
1989 * queue delayed work. This must follow RCU workqueues creation
1990 * and timers initialization.
1991 */
1992 srcu_init_done = true;
1993 while (!list_empty(&srcu_boot_list)) {
1994 sup = list_first_entry(&srcu_boot_list, struct srcu_usage,
1995 work.work.entry);
1996 list_del_init(&sup->work.work.entry);
1997 if (SRCU_SIZING_IS(SRCU_SIZING_INIT) &&
1998 sup->srcu_size_state == SRCU_SIZE_SMALL)
1999 sup->srcu_size_state = SRCU_SIZE_ALLOC;
2000 queue_work(rcu_gp_wq, &sup->work.work);
2001 }
2002 }
2003
2004 #ifdef CONFIG_MODULES
2005
2006 /* Initialize any global-scope srcu_struct structures used by this module. */
srcu_module_coming(struct module * mod)2007 static int srcu_module_coming(struct module *mod)
2008 {
2009 int i;
2010 struct srcu_struct *ssp;
2011 struct srcu_struct **sspp = mod->srcu_struct_ptrs;
2012
2013 for (i = 0; i < mod->num_srcu_structs; i++) {
2014 ssp = *(sspp++);
2015 ssp->sda = alloc_percpu(struct srcu_data);
2016 if (WARN_ON_ONCE(!ssp->sda))
2017 return -ENOMEM;
2018 ssp->srcu_ctrp = &ssp->sda->srcu_ctrs[0];
2019 }
2020 return 0;
2021 }
2022
2023 /* Clean up any global-scope srcu_struct structures used by this module. */
srcu_module_going(struct module * mod)2024 static void srcu_module_going(struct module *mod)
2025 {
2026 int i;
2027 struct srcu_struct *ssp;
2028 struct srcu_struct **sspp = mod->srcu_struct_ptrs;
2029
2030 for (i = 0; i < mod->num_srcu_structs; i++) {
2031 ssp = *(sspp++);
2032 if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed)) &&
2033 !WARN_ON_ONCE(!ssp->srcu_sup->sda_is_static))
2034 cleanup_srcu_struct(ssp);
2035 if (!WARN_ON(srcu_readers_active(ssp)))
2036 free_percpu(ssp->sda);
2037 }
2038 }
2039
2040 /* Handle one module, either coming or going. */
srcu_module_notify(struct notifier_block * self,unsigned long val,void * data)2041 static int srcu_module_notify(struct notifier_block *self,
2042 unsigned long val, void *data)
2043 {
2044 struct module *mod = data;
2045 int ret = 0;
2046
2047 switch (val) {
2048 case MODULE_STATE_COMING:
2049 ret = srcu_module_coming(mod);
2050 break;
2051 case MODULE_STATE_GOING:
2052 srcu_module_going(mod);
2053 break;
2054 default:
2055 break;
2056 }
2057 return ret;
2058 }
2059
2060 static struct notifier_block srcu_module_nb = {
2061 .notifier_call = srcu_module_notify,
2062 .priority = 0,
2063 };
2064
init_srcu_module_notifier(void)2065 static __init int init_srcu_module_notifier(void)
2066 {
2067 int ret;
2068
2069 ret = register_module_notifier(&srcu_module_nb);
2070 if (ret)
2071 pr_warn("Failed to register srcu module notifier\n");
2072 return ret;
2073 }
2074 late_initcall(init_srcu_module_notifier);
2075
2076 #endif /* #ifdef CONFIG_MODULES */
2077