blob: 03d57fe9f0949be93240e481d1775a3227c60a2c [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. McKenneyb5fe2232017-04-27 07:52:35 -070043ulong exp_holdoff = 25 * 1000; /* Holdoff (ns) for auto-expediting. */
Paul E. McKenney22607d62017-04-25 14:03:11 -070044module_param(exp_holdoff, ulong, 0444);
45
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070046static void srcu_invoke_callbacks(struct work_struct *work);
47static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay);
48
49/*
50 * Initialize SRCU combining tree. Note that statically allocated
51 * srcu_struct structures might already have srcu_read_lock() and
52 * srcu_read_unlock() running against them. So if the is_static parameter
53 * is set, don't initialize ->srcu_lock_count[] and ->srcu_unlock_count[].
54 */
55static void init_srcu_struct_nodes(struct srcu_struct *sp, bool is_static)
Paul E. McKenneydad81a22017-03-25 17:23:44 -070056{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070057 int cpu;
58 int i;
59 int level = 0;
60 int levelspread[RCU_NUM_LVLS];
61 struct srcu_data *sdp;
62 struct srcu_node *snp;
63 struct srcu_node *snp_first;
64
65 /* Work out the overall tree geometry. */
66 sp->level[0] = &sp->node[0];
67 for (i = 1; i < rcu_num_lvls; i++)
68 sp->level[i] = sp->level[i - 1] + num_rcu_lvl[i - 1];
69 rcu_init_levelspread(levelspread, num_rcu_lvl);
70
71 /* Each pass through this loop initializes one srcu_node structure. */
72 rcu_for_each_node_breadth_first(sp, snp) {
73 spin_lock_init(&snp->lock);
Paul E. McKenneyc7e88062017-04-18 16:01:46 -070074 WARN_ON_ONCE(ARRAY_SIZE(snp->srcu_have_cbs) !=
75 ARRAY_SIZE(snp->srcu_data_have_cbs));
76 for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070077 snp->srcu_have_cbs[i] = 0;
Paul E. McKenneyc7e88062017-04-18 16:01:46 -070078 snp->srcu_data_have_cbs[i] = 0;
79 }
Paul E. McKenney1e9a0382017-04-24 16:02:09 -070080 snp->srcu_gp_seq_needed_exp = 0;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070081 snp->grplo = -1;
82 snp->grphi = -1;
83 if (snp == &sp->node[0]) {
84 /* Root node, special case. */
85 snp->srcu_parent = NULL;
86 continue;
87 }
88
89 /* Non-root node. */
90 if (snp == sp->level[level + 1])
91 level++;
92 snp->srcu_parent = sp->level[level - 1] +
93 (snp - sp->level[level]) /
94 levelspread[level - 1];
95 }
96
97 /*
98 * Initialize the per-CPU srcu_data array, which feeds into the
99 * leaves of the srcu_node tree.
100 */
101 WARN_ON_ONCE(ARRAY_SIZE(sdp->srcu_lock_count) !=
102 ARRAY_SIZE(sdp->srcu_unlock_count));
103 level = rcu_num_lvls - 1;
104 snp_first = sp->level[level];
105 for_each_possible_cpu(cpu) {
106 sdp = per_cpu_ptr(sp->sda, cpu);
107 spin_lock_init(&sdp->lock);
108 rcu_segcblist_init(&sdp->srcu_cblist);
109 sdp->srcu_cblist_invoking = false;
110 sdp->srcu_gp_seq_needed = sp->srcu_gp_seq;
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700111 sdp->srcu_gp_seq_needed_exp = sp->srcu_gp_seq;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700112 sdp->mynode = &snp_first[cpu / levelspread[level]];
113 for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {
114 if (snp->grplo < 0)
115 snp->grplo = cpu;
116 snp->grphi = cpu;
117 }
118 sdp->cpu = cpu;
119 INIT_DELAYED_WORK(&sdp->work, srcu_invoke_callbacks);
120 sdp->sp = sp;
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700121 sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700122 if (is_static)
123 continue;
124
125 /* Dynamically allocated, better be no srcu_read_locks()! */
126 for (i = 0; i < ARRAY_SIZE(sdp->srcu_lock_count); i++) {
127 sdp->srcu_lock_count[i] = 0;
128 sdp->srcu_unlock_count[i] = 0;
129 }
130 }
131}
132
133/*
134 * Initialize non-compile-time initialized fields, including the
135 * associated srcu_node and srcu_data structures. The is_static
136 * parameter is passed through to init_srcu_struct_nodes(), and
137 * also tells us that ->sda has already been wired up to srcu_data.
138 */
139static int init_srcu_struct_fields(struct srcu_struct *sp, bool is_static)
140{
141 mutex_init(&sp->srcu_cb_mutex);
142 mutex_init(&sp->srcu_gp_mutex);
143 sp->srcu_idx = 0;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700144 sp->srcu_gp_seq = 0;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700145 sp->srcu_barrier_seq = 0;
146 mutex_init(&sp->srcu_barrier_mutex);
147 atomic_set(&sp->srcu_barrier_cpu_cnt, 0);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700148 INIT_DELAYED_WORK(&sp->work, process_srcu);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700149 if (!is_static)
150 sp->sda = alloc_percpu(struct srcu_data);
151 init_srcu_struct_nodes(sp, is_static);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700152 sp->srcu_gp_seq_needed_exp = 0;
Paul E. McKenney22607d62017-04-25 14:03:11 -0700153 sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700154 smp_store_release(&sp->srcu_gp_seq_needed, 0); /* Init done. */
155 return sp->sda ? 0 : -ENOMEM;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700156}
157
158#ifdef CONFIG_DEBUG_LOCK_ALLOC
159
160int __init_srcu_struct(struct srcu_struct *sp, const char *name,
161 struct lock_class_key *key)
162{
163 /* Don't re-initialize a lock while it is held. */
164 debug_check_no_locks_freed((void *)sp, sizeof(*sp));
165 lockdep_init_map(&sp->dep_map, name, key, 0);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700166 spin_lock_init(&sp->gp_lock);
167 return init_srcu_struct_fields(sp, false);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700168}
169EXPORT_SYMBOL_GPL(__init_srcu_struct);
170
171#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
172
173/**
174 * init_srcu_struct - initialize a sleep-RCU structure
175 * @sp: structure to initialize.
176 *
177 * Must invoke this on a given srcu_struct before passing that srcu_struct
178 * to any other function. Each srcu_struct represents a separate domain
179 * of SRCU protection.
180 */
181int init_srcu_struct(struct srcu_struct *sp)
182{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700183 spin_lock_init(&sp->gp_lock);
184 return init_srcu_struct_fields(sp, false);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700185}
186EXPORT_SYMBOL_GPL(init_srcu_struct);
187
188#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
189
190/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700191 * First-use initialization of statically allocated srcu_struct
192 * structure. Wiring up the combining tree is more than can be
193 * done with compile-time initialization, so this check is added
194 * to each update-side SRCU primitive. Use ->gp_lock, which -is-
195 * compile-time initialized, to resolve races involving multiple
196 * CPUs trying to garner first-use privileges.
197 */
198static void check_init_srcu_struct(struct srcu_struct *sp)
199{
200 unsigned long flags;
201
202 WARN_ON_ONCE(rcu_scheduler_active == RCU_SCHEDULER_INIT);
203 /* The smp_load_acquire() pairs with the smp_store_release(). */
204 if (!rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq_needed))) /*^^^*/
205 return; /* Already initialized. */
206 spin_lock_irqsave(&sp->gp_lock, flags);
207 if (!rcu_seq_state(sp->srcu_gp_seq_needed)) {
208 spin_unlock_irqrestore(&sp->gp_lock, flags);
209 return;
210 }
211 init_srcu_struct_fields(sp, true);
212 spin_unlock_irqrestore(&sp->gp_lock, flags);
213}
214
215/*
216 * Returns approximate total of the readers' ->srcu_lock_count[] values
217 * for the rank of per-CPU counters specified by idx.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700218 */
219static unsigned long srcu_readers_lock_idx(struct srcu_struct *sp, int idx)
220{
221 int cpu;
222 unsigned long sum = 0;
223
224 for_each_possible_cpu(cpu) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700225 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700226
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700227 sum += READ_ONCE(cpuc->srcu_lock_count[idx]);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700228 }
229 return sum;
230}
231
232/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700233 * Returns approximate total of the readers' ->srcu_unlock_count[] values
234 * for the rank of per-CPU counters specified by idx.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700235 */
236static unsigned long srcu_readers_unlock_idx(struct srcu_struct *sp, int idx)
237{
238 int cpu;
239 unsigned long sum = 0;
240
241 for_each_possible_cpu(cpu) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700242 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700243
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700244 sum += READ_ONCE(cpuc->srcu_unlock_count[idx]);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700245 }
246 return sum;
247}
248
249/*
250 * Return true if the number of pre-existing readers is determined to
251 * be zero.
252 */
253static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
254{
255 unsigned long unlocks;
256
257 unlocks = srcu_readers_unlock_idx(sp, idx);
258
259 /*
260 * Make sure that a lock is always counted if the corresponding
261 * unlock is counted. Needs to be a smp_mb() as the read side may
262 * contain a read from a variable that is written to before the
263 * synchronize_srcu() in the write side. In this case smp_mb()s
264 * A and B act like the store buffering pattern.
265 *
266 * This smp_mb() also pairs with smp_mb() C to prevent accesses
267 * after the synchronize_srcu() from being executed before the
268 * grace period ends.
269 */
270 smp_mb(); /* A */
271
272 /*
273 * If the locks are the same as the unlocks, then there must have
274 * been no readers on this index at some time in between. This does
275 * not mean that there are no more readers, as one could have read
276 * the current index but not have incremented the lock counter yet.
277 *
Paul E. McKenney881ec9d2017-04-12 15:16:50 -0700278 * So suppose that the updater is preempted here for so long
279 * that more than ULONG_MAX non-nested readers come and go in
280 * the meantime. It turns out that this cannot result in overflow
281 * because if a reader modifies its unlock count after we read it
282 * above, then that reader's next load of ->srcu_idx is guaranteed
283 * to get the new value, which will cause it to operate on the
284 * other bank of counters, where it cannot contribute to the
285 * overflow of these counters. This means that there is a maximum
286 * of 2*NR_CPUS increments, which cannot overflow given current
287 * systems, especially not on 64-bit systems.
288 *
289 * OK, how about nesting? This does impose a limit on nesting
290 * of floor(ULONG_MAX/NR_CPUS/2), which should be sufficient,
291 * especially on 64-bit systems.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700292 */
293 return srcu_readers_lock_idx(sp, idx) == unlocks;
294}
295
296/**
297 * srcu_readers_active - returns true if there are readers. and false
298 * otherwise
299 * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
300 *
301 * Note that this is not an atomic primitive, and can therefore suffer
302 * severe errors when invoked on an active srcu_struct. That said, it
303 * can be useful as an error check at cleanup time.
304 */
305static bool srcu_readers_active(struct srcu_struct *sp)
306{
307 int cpu;
308 unsigned long sum = 0;
309
310 for_each_possible_cpu(cpu) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700311 struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700312
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700313 sum += READ_ONCE(cpuc->srcu_lock_count[0]);
314 sum += READ_ONCE(cpuc->srcu_lock_count[1]);
315 sum -= READ_ONCE(cpuc->srcu_unlock_count[0]);
316 sum -= READ_ONCE(cpuc->srcu_unlock_count[1]);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700317 }
318 return sum;
319}
320
321#define SRCU_INTERVAL 1
322
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700323/*
324 * Return grace-period delay, zero if there are expedited grace
325 * periods pending, SRCU_INTERVAL otherwise.
326 */
327static unsigned long srcu_get_delay(struct srcu_struct *sp)
328{
329 if (ULONG_CMP_LT(READ_ONCE(sp->srcu_gp_seq),
330 READ_ONCE(sp->srcu_gp_seq_needed_exp)))
331 return 0;
332 return SRCU_INTERVAL;
333}
334
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700335/**
336 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
337 * @sp: structure to clean up.
338 *
339 * Must invoke this after you are finished using a given srcu_struct that
340 * was initialized via init_srcu_struct(), else you leak memory.
341 */
342void cleanup_srcu_struct(struct srcu_struct *sp)
343{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700344 int cpu;
345
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700346 if (WARN_ON(!srcu_get_delay(sp)))
347 return; /* Leakage unless caller handles error. */
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700348 if (WARN_ON(srcu_readers_active(sp)))
349 return; /* Leakage unless caller handles error. */
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700350 flush_delayed_work(&sp->work);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700351 for_each_possible_cpu(cpu)
352 flush_delayed_work(&per_cpu_ptr(sp->sda, cpu)->work);
353 if (WARN_ON(rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
354 WARN_ON(srcu_readers_active(sp))) {
355 pr_info("cleanup_srcu_struct: Active srcu_struct %p state: %d\n", sp, rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)));
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700356 return; /* Caller forgot to stop doing call_srcu()? */
357 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700358 free_percpu(sp->sda);
359 sp->sda = NULL;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700360}
361EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
362
363/*
364 * Counts the new reader in the appropriate per-CPU element of the
Paolo Bonzinicdf7abc2017-05-31 14:03:10 +0200365 * srcu_struct.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700366 * Returns an index that must be passed to the matching srcu_read_unlock().
367 */
368int __srcu_read_lock(struct srcu_struct *sp)
369{
370 int idx;
371
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700372 idx = READ_ONCE(sp->srcu_idx) & 0x1;
Paolo Bonzinicdf7abc2017-05-31 14:03:10 +0200373 this_cpu_inc(sp->sda->srcu_lock_count[idx]);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700374 smp_mb(); /* B */ /* Avoid leaking the critical section. */
375 return idx;
376}
377EXPORT_SYMBOL_GPL(__srcu_read_lock);
378
379/*
380 * Removes the count for the old reader from the appropriate per-CPU
381 * element of the srcu_struct. Note that this may well be a different
382 * CPU than that which was incremented by the corresponding srcu_read_lock().
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700383 */
384void __srcu_read_unlock(struct srcu_struct *sp, int idx)
385{
386 smp_mb(); /* C */ /* Avoid leaking the critical section. */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700387 this_cpu_inc(sp->sda->srcu_unlock_count[idx]);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700388}
389EXPORT_SYMBOL_GPL(__srcu_read_unlock);
390
391/*
392 * We use an adaptive strategy for synchronize_srcu() and especially for
393 * synchronize_srcu_expedited(). We spin for a fixed time period
394 * (defined below) to allow SRCU readers to exit their read-side critical
395 * sections. If there are still some readers after a few microseconds,
396 * we repeatedly block for 1-millisecond time periods.
397 */
398#define SRCU_RETRY_CHECK_DELAY 5
399
400/*
401 * Start an SRCU grace period.
402 */
403static void srcu_gp_start(struct srcu_struct *sp)
404{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700405 struct srcu_data *sdp = this_cpu_ptr(sp->sda);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700406 int state;
407
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700408 RCU_LOCKDEP_WARN(!lockdep_is_held(&sp->gp_lock),
409 "Invoked srcu_gp_start() without ->gp_lock!");
410 WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
411 rcu_segcblist_advance(&sdp->srcu_cblist,
412 rcu_seq_current(&sp->srcu_gp_seq));
413 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
414 rcu_seq_snap(&sp->srcu_gp_seq));
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700415 smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700416 rcu_seq_start(&sp->srcu_gp_seq);
417 state = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
418 WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
419}
420
421/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700422 * Track online CPUs to guide callback workqueue placement.
423 */
424DEFINE_PER_CPU(bool, srcu_online);
425
426void srcu_online_cpu(unsigned int cpu)
427{
428 WRITE_ONCE(per_cpu(srcu_online, cpu), true);
429}
430
431void srcu_offline_cpu(unsigned int cpu)
432{
433 WRITE_ONCE(per_cpu(srcu_online, cpu), false);
434}
435
436/*
437 * Place the workqueue handler on the specified CPU if online, otherwise
438 * just run it whereever. This is useful for placing workqueue handlers
439 * that are to invoke the specified CPU's callbacks.
440 */
441static bool srcu_queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
442 struct delayed_work *dwork,
443 unsigned long delay)
444{
445 bool ret;
446
447 preempt_disable();
448 if (READ_ONCE(per_cpu(srcu_online, cpu)))
449 ret = queue_delayed_work_on(cpu, wq, dwork, delay);
450 else
451 ret = queue_delayed_work(wq, dwork, delay);
452 preempt_enable();
453 return ret;
454}
455
456/*
457 * Schedule callback invocation for the specified srcu_data structure,
458 * if possible, on the corresponding CPU.
459 */
460static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
461{
462 srcu_queue_delayed_work_on(sdp->cpu, system_power_efficient_wq,
463 &sdp->work, delay);
464}
465
466/*
467 * Schedule callback invocation for all srcu_data structures associated
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700468 * with the specified srcu_node structure that have callbacks for the
469 * just-completed grace period, the one corresponding to idx. If possible,
470 * schedule this invocation on the corresponding CPUs.
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700471 */
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700472static void srcu_schedule_cbs_snp(struct srcu_struct *sp, struct srcu_node *snp,
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700473 unsigned long mask, unsigned long delay)
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700474{
475 int cpu;
476
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700477 for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
478 if (!(mask & (1 << (cpu - snp->grplo))))
479 continue;
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700480 srcu_schedule_cbs_sdp(per_cpu_ptr(sp->sda, cpu), delay);
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700481 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700482}
483
484/*
485 * Note the end of an SRCU grace period. Initiates callback invocation
486 * and starts a new grace period if needed.
487 *
488 * The ->srcu_cb_mutex acquisition does not protect any data, but
489 * instead prevents more than one grace period from starting while we
490 * are initiating callback invocation. This allows the ->srcu_have_cbs[]
491 * array to have a finite number of elements.
492 */
493static void srcu_gp_end(struct srcu_struct *sp)
494{
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700495 unsigned long cbdelay;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700496 bool cbs;
497 unsigned long gpseq;
498 int idx;
499 int idxnext;
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700500 unsigned long mask;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700501 struct srcu_node *snp;
502
503 /* Prevent more than one additional grace period. */
504 mutex_lock(&sp->srcu_cb_mutex);
505
506 /* End the current grace period. */
507 spin_lock_irq(&sp->gp_lock);
508 idx = rcu_seq_state(sp->srcu_gp_seq);
509 WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700510 cbdelay = srcu_get_delay(sp);
Paul E. McKenney22607d62017-04-25 14:03:11 -0700511 sp->srcu_last_gp_end = ktime_get_mono_fast_ns();
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700512 rcu_seq_end(&sp->srcu_gp_seq);
513 gpseq = rcu_seq_current(&sp->srcu_gp_seq);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700514 if (ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, gpseq))
515 sp->srcu_gp_seq_needed_exp = gpseq;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700516 spin_unlock_irq(&sp->gp_lock);
517 mutex_unlock(&sp->srcu_gp_mutex);
518 /* A new grace period can start at this point. But only one. */
519
520 /* Initiate callback invocation as needed. */
521 idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
522 idxnext = (idx + 1) % ARRAY_SIZE(snp->srcu_have_cbs);
523 rcu_for_each_node_breadth_first(sp, snp) {
524 spin_lock_irq(&snp->lock);
525 cbs = false;
526 if (snp >= sp->level[rcu_num_lvls - 1])
527 cbs = snp->srcu_have_cbs[idx] == gpseq;
528 snp->srcu_have_cbs[idx] = gpseq;
529 rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700530 if (ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, gpseq))
531 snp->srcu_gp_seq_needed_exp = gpseq;
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700532 mask = snp->srcu_data_have_cbs[idx];
533 snp->srcu_data_have_cbs[idx] = 0;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700534 spin_unlock_irq(&snp->lock);
535 if (cbs) {
536 smp_mb(); /* GP end before CB invocation. */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700537 srcu_schedule_cbs_snp(sp, snp, mask, cbdelay);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700538 }
539 }
540
541 /* Callback initiation done, allow grace periods after next. */
542 mutex_unlock(&sp->srcu_cb_mutex);
543
544 /* Start a new grace period if needed. */
545 spin_lock_irq(&sp->gp_lock);
546 gpseq = rcu_seq_current(&sp->srcu_gp_seq);
547 if (!rcu_seq_state(gpseq) &&
548 ULONG_CMP_LT(gpseq, sp->srcu_gp_seq_needed)) {
549 srcu_gp_start(sp);
550 spin_unlock_irq(&sp->gp_lock);
551 /* Throttle expedited grace periods: Should be rare! */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700552 srcu_reschedule(sp, rcu_seq_ctr(gpseq) & 0x3ff
553 ? 0 : SRCU_INTERVAL);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700554 } else {
555 spin_unlock_irq(&sp->gp_lock);
556 }
557}
558
559/*
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700560 * Funnel-locking scheme to scalably mediate many concurrent expedited
561 * grace-period requests. This function is invoked for the first known
562 * expedited request for a grace period that has already been requested,
563 * but without expediting. To start a completely new grace period,
564 * whether expedited or not, use srcu_funnel_gp_start() instead.
565 */
566static void srcu_funnel_exp_start(struct srcu_struct *sp, struct srcu_node *snp,
567 unsigned long s)
568{
569 unsigned long flags;
570
571 for (; snp != NULL; snp = snp->srcu_parent) {
572 if (rcu_seq_done(&sp->srcu_gp_seq, s) ||
573 ULONG_CMP_GE(READ_ONCE(snp->srcu_gp_seq_needed_exp), s))
574 return;
575 spin_lock_irqsave(&snp->lock, flags);
576 if (ULONG_CMP_GE(snp->srcu_gp_seq_needed_exp, s)) {
577 spin_unlock_irqrestore(&snp->lock, flags);
578 return;
579 }
580 WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
581 spin_unlock_irqrestore(&snp->lock, flags);
582 }
583 spin_lock_irqsave(&sp->gp_lock, flags);
584 if (!ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
585 sp->srcu_gp_seq_needed_exp = s;
586 spin_unlock_irqrestore(&sp->gp_lock, flags);
587}
588
589/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700590 * Funnel-locking scheme to scalably mediate many concurrent grace-period
591 * requests. The winner has to do the work of actually starting grace
592 * period s. Losers must either ensure that their desired grace-period
593 * number is recorded on at least their leaf srcu_node structure, or they
594 * must take steps to invoke their own callbacks.
595 */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700596static void srcu_funnel_gp_start(struct srcu_struct *sp, struct srcu_data *sdp,
597 unsigned long s, bool do_norm)
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700598{
599 unsigned long flags;
600 int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
601 struct srcu_node *snp = sdp->mynode;
602 unsigned long snp_seq;
603
604 /* Each pass through the loop does one level of the srcu_node tree. */
605 for (; snp != NULL; snp = snp->srcu_parent) {
606 if (rcu_seq_done(&sp->srcu_gp_seq, s) && snp != sdp->mynode)
607 return; /* GP already done and CBs recorded. */
608 spin_lock_irqsave(&snp->lock, flags);
609 if (ULONG_CMP_GE(snp->srcu_have_cbs[idx], s)) {
610 snp_seq = snp->srcu_have_cbs[idx];
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700611 if (snp == sdp->mynode && snp_seq == s)
612 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700613 spin_unlock_irqrestore(&snp->lock, flags);
614 if (snp == sdp->mynode && snp_seq != s) {
615 smp_mb(); /* CBs after GP! */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700616 srcu_schedule_cbs_sdp(sdp, do_norm
617 ? SRCU_INTERVAL
618 : 0);
619 return;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700620 }
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700621 if (!do_norm)
622 srcu_funnel_exp_start(sp, snp, s);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700623 return;
624 }
625 snp->srcu_have_cbs[idx] = s;
Paul E. McKenneyc7e88062017-04-18 16:01:46 -0700626 if (snp == sdp->mynode)
627 snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700628 if (!do_norm && ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, s))
629 snp->srcu_gp_seq_needed_exp = s;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700630 spin_unlock_irqrestore(&snp->lock, flags);
631 }
632
633 /* Top of tree, must ensure the grace period will be started. */
634 spin_lock_irqsave(&sp->gp_lock, flags);
635 if (ULONG_CMP_LT(sp->srcu_gp_seq_needed, s)) {
636 /*
637 * Record need for grace period s. Pair with load
638 * acquire setting up for initialization.
639 */
640 smp_store_release(&sp->srcu_gp_seq_needed, s); /*^^^*/
641 }
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700642 if (!do_norm && ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
643 sp->srcu_gp_seq_needed_exp = s;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700644
645 /* If grace period not already done and none in progress, start it. */
646 if (!rcu_seq_done(&sp->srcu_gp_seq, s) &&
647 rcu_seq_state(sp->srcu_gp_seq) == SRCU_STATE_IDLE) {
648 WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
649 srcu_gp_start(sp);
650 queue_delayed_work(system_power_efficient_wq, &sp->work,
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700651 srcu_get_delay(sp));
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700652 }
653 spin_unlock_irqrestore(&sp->gp_lock, flags);
654}
655
656/*
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700657 * Wait until all readers counted by array index idx complete, but
658 * loop an additional time if there is an expedited grace period pending.
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700659 * The caller must ensure that ->srcu_idx is not changed while checking.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700660 */
661static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
662{
663 for (;;) {
664 if (srcu_readers_active_idx_check(sp, idx))
665 return true;
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700666 if (--trycount + !srcu_get_delay(sp) <= 0)
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700667 return false;
668 udelay(SRCU_RETRY_CHECK_DELAY);
669 }
670}
671
672/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700673 * Increment the ->srcu_idx counter so that future SRCU readers will
674 * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700675 * us to wait for pre-existing readers in a starvation-free manner.
676 */
677static void srcu_flip(struct srcu_struct *sp)
678{
Paul E. McKenney881ec9d2017-04-12 15:16:50 -0700679 /*
680 * Ensure that if this updater saw a given reader's increment
681 * from __srcu_read_lock(), that reader was using an old value
682 * of ->srcu_idx. Also ensure that if a given reader sees the
683 * new value of ->srcu_idx, this updater's earlier scans cannot
684 * have seen that reader's increments (which is OK, because this
685 * grace period need not wait on that reader).
686 */
687 smp_mb(); /* E */ /* Pairs with B and C. */
688
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700689 WRITE_ONCE(sp->srcu_idx, sp->srcu_idx + 1);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700690
691 /*
692 * Ensure that if the updater misses an __srcu_read_unlock()
693 * increment, that task's next __srcu_read_lock() will see the
694 * above counter update. Note that both this memory barrier
695 * and the one in srcu_readers_active_idx_check() provide the
696 * guarantee for __srcu_read_lock().
697 */
698 smp_mb(); /* D */ /* Pairs with C. */
699}
700
701/*
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700702 * If SRCU is likely idle, return true, otherwise return false.
703 *
704 * Note that it is OK for several current from-idle requests for a new
705 * grace period from idle to specify expediting because they will all end
706 * up requesting the same grace period anyhow. So no loss.
707 *
708 * Note also that if any CPU (including the current one) is still invoking
709 * callbacks, this function will nevertheless say "idle". This is not
710 * ideal, but the overhead of checking all CPUs' callback lists is even
711 * less ideal, especially on large systems. Furthermore, the wakeup
712 * can happen before the callback is fully removed, so we have no choice
713 * but to accept this type of error.
714 *
715 * This function is also subject to counter-wrap errors, but let's face
716 * it, if this function was preempted for enough time for the counters
717 * to wrap, it really doesn't matter whether or not we expedite the grace
718 * period. The extra overhead of a needlessly expedited grace period is
719 * negligible when amoritized over that time period, and the extra latency
720 * of a needlessly non-expedited grace period is similarly negligible.
721 */
722static bool srcu_might_be_idle(struct srcu_struct *sp)
723{
Paul E. McKenney22607d62017-04-25 14:03:11 -0700724 unsigned long curseq;
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700725 unsigned long flags;
726 struct srcu_data *sdp;
Paul E. McKenney22607d62017-04-25 14:03:11 -0700727 unsigned long t;
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700728
729 /* If the local srcu_data structure has callbacks, not idle. */
730 local_irq_save(flags);
731 sdp = this_cpu_ptr(sp->sda);
732 if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
733 local_irq_restore(flags);
734 return false; /* Callbacks already present, so not idle. */
735 }
736 local_irq_restore(flags);
737
738 /*
739 * No local callbacks, so probabalistically probe global state.
740 * Exact information would require acquiring locks, which would
741 * kill scalability, hence the probabalistic nature of the probe.
742 */
Paul E. McKenney22607d62017-04-25 14:03:11 -0700743
744 /* First, see if enough time has passed since the last GP. */
745 t = ktime_get_mono_fast_ns();
746 if (exp_holdoff == 0 ||
747 time_in_range_open(t, sp->srcu_last_gp_end,
748 sp->srcu_last_gp_end + exp_holdoff))
749 return false; /* Too soon after last GP. */
750
751 /* Next, check for probable idleness. */
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700752 curseq = rcu_seq_current(&sp->srcu_gp_seq);
753 smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
754 if (ULONG_CMP_LT(curseq, READ_ONCE(sp->srcu_gp_seq_needed)))
755 return false; /* Grace period in progress, so not idle. */
756 smp_mb(); /* Order ->srcu_gp_seq with prior access. */
757 if (curseq != rcu_seq_current(&sp->srcu_gp_seq))
758 return false; /* GP # changed, so not idle. */
759 return true; /* With reasonable probability, idle! */
760}
761
762/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700763 * Enqueue an SRCU callback on the srcu_data structure associated with
764 * the current CPU and the specified srcu_struct structure, initiating
765 * grace-period processing if it is not already running.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700766 *
767 * Note that all CPUs must agree that the grace period extended beyond
768 * all pre-existing SRCU read-side critical section. On systems with
769 * more than one CPU, this means that when "func()" is invoked, each CPU
770 * is guaranteed to have executed a full memory barrier since the end of
771 * its last corresponding SRCU read-side critical section whose beginning
772 * preceded the call to call_rcu(). It also means that each CPU executing
773 * an SRCU read-side critical section that continues beyond the start of
774 * "func()" must have executed a memory barrier after the call_rcu()
775 * but before the beginning of that SRCU read-side critical section.
776 * Note that these guarantees include CPUs that are offline, idle, or
777 * executing in user mode, as well as CPUs that are executing in the kernel.
778 *
779 * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
780 * resulting SRCU callback function "func()", then both CPU A and CPU
781 * B are guaranteed to execute a full memory barrier during the time
782 * interval between the call to call_rcu() and the invocation of "func()".
783 * This guarantee applies even if CPU A and CPU B are the same CPU (but
784 * again only if the system has more than one CPU).
785 *
786 * Of course, these guarantees apply only for invocations of call_srcu(),
787 * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
788 * srcu_struct structure.
789 */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700790void __call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
791 rcu_callback_t func, bool do_norm)
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700792{
793 unsigned long flags;
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700794 bool needexp = false;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700795 bool needgp = false;
796 unsigned long s;
797 struct srcu_data *sdp;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700798
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700799 check_init_srcu_struct(sp);
800 rhp->func = func;
801 local_irq_save(flags);
802 sdp = this_cpu_ptr(sp->sda);
803 spin_lock(&sdp->lock);
804 rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp, false);
805 rcu_segcblist_advance(&sdp->srcu_cblist,
806 rcu_seq_current(&sp->srcu_gp_seq));
807 s = rcu_seq_snap(&sp->srcu_gp_seq);
808 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s);
809 if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
810 sdp->srcu_gp_seq_needed = s;
811 needgp = true;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700812 }
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700813 if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
814 sdp->srcu_gp_seq_needed_exp = s;
815 needexp = true;
816 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700817 spin_unlock_irqrestore(&sdp->lock, flags);
818 if (needgp)
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700819 srcu_funnel_gp_start(sp, sdp, s, do_norm);
820 else if (needexp)
821 srcu_funnel_exp_start(sp, sdp->mynode, s);
822}
823
824void call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
825 rcu_callback_t func)
826{
827 __call_srcu(sp, rhp, func, true);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700828}
829EXPORT_SYMBOL_GPL(call_srcu);
830
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700831/*
832 * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
833 */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700834static void __synchronize_srcu(struct srcu_struct *sp, bool do_norm)
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700835{
836 struct rcu_synchronize rcu;
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700837
838 RCU_LOCKDEP_WARN(lock_is_held(&sp->dep_map) ||
839 lock_is_held(&rcu_bh_lock_map) ||
840 lock_is_held(&rcu_lock_map) ||
841 lock_is_held(&rcu_sched_lock_map),
842 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
843
844 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
845 return;
846 might_sleep();
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700847 check_init_srcu_struct(sp);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700848 init_completion(&rcu.completion);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700849 init_rcu_head_on_stack(&rcu.head);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700850 __call_srcu(sp, &rcu.head, wakeme_after_rcu, do_norm);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700851 wait_for_completion(&rcu.completion);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700852 destroy_rcu_head_on_stack(&rcu.head);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700853}
854
855/**
856 * synchronize_srcu_expedited - Brute-force SRCU grace period
857 * @sp: srcu_struct with which to synchronize.
858 *
859 * Wait for an SRCU grace period to elapse, but be more aggressive about
860 * spinning rather than blocking when waiting.
861 *
862 * Note that synchronize_srcu_expedited() has the same deadlock and
863 * memory-ordering properties as does synchronize_srcu().
864 */
865void synchronize_srcu_expedited(struct srcu_struct *sp)
866{
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700867 __synchronize_srcu(sp, rcu_gp_is_normal());
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700868}
869EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
870
871/**
872 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
873 * @sp: srcu_struct with which to synchronize.
874 *
875 * Wait for the count to drain to zero of both indexes. To avoid the
876 * possible starvation of synchronize_srcu(), it waits for the count of
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700877 * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
878 * and then flip the srcu_idx and wait for the count of the other index.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700879 *
880 * Can block; must be called from process context.
881 *
882 * Note that it is illegal to call synchronize_srcu() from the corresponding
883 * SRCU read-side critical section; doing so will result in deadlock.
884 * However, it is perfectly legal to call synchronize_srcu() on one
885 * srcu_struct from some other srcu_struct's read-side critical section,
886 * as long as the resulting graph of srcu_structs is acyclic.
887 *
888 * There are memory-ordering constraints implied by synchronize_srcu().
889 * On systems with more than one CPU, when synchronize_srcu() returns,
890 * each CPU is guaranteed to have executed a full memory barrier since
891 * the end of its last corresponding SRCU-sched read-side critical section
892 * whose beginning preceded the call to synchronize_srcu(). In addition,
893 * each CPU having an SRCU read-side critical section that extends beyond
894 * the return from synchronize_srcu() is guaranteed to have executed a
895 * full memory barrier after the beginning of synchronize_srcu() and before
896 * the beginning of that SRCU read-side critical section. Note that these
897 * guarantees include CPUs that are offline, idle, or executing in user mode,
898 * as well as CPUs that are executing in the kernel.
899 *
900 * Furthermore, if CPU A invoked synchronize_srcu(), which returned
901 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
902 * to have executed a full memory barrier during the execution of
903 * synchronize_srcu(). This guarantee applies even if CPU A and CPU B
904 * are the same CPU, but again only if the system has more than one CPU.
905 *
906 * Of course, these memory-ordering guarantees apply only when
907 * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
908 * passed the same srcu_struct structure.
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700909 *
910 * If SRCU is likely idle, expedite the first request. This semantic
911 * was provided by Classic SRCU, and is relied upon by its users, so TREE
912 * SRCU must also provide it. Note that detecting idleness is heuristic
913 * and subject to both false positives and negatives.
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700914 */
915void synchronize_srcu(struct srcu_struct *sp)
916{
Paul E. McKenney2da4b2a2017-04-25 11:34:40 -0700917 if (srcu_might_be_idle(sp) || rcu_gp_is_expedited())
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700918 synchronize_srcu_expedited(sp);
919 else
Paul E. McKenney1e9a0382017-04-24 16:02:09 -0700920 __synchronize_srcu(sp, true);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700921}
922EXPORT_SYMBOL_GPL(synchronize_srcu);
923
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700924/*
925 * Callback function for srcu_barrier() use.
926 */
927static void srcu_barrier_cb(struct rcu_head *rhp)
928{
929 struct srcu_data *sdp;
930 struct srcu_struct *sp;
931
932 sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
933 sp = sdp->sp;
934 if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
935 complete(&sp->srcu_barrier_completion);
936}
937
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700938/**
939 * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
940 * @sp: srcu_struct on which to wait for in-flight callbacks.
941 */
942void srcu_barrier(struct srcu_struct *sp)
943{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700944 int cpu;
945 struct srcu_data *sdp;
946 unsigned long s = rcu_seq_snap(&sp->srcu_barrier_seq);
947
948 check_init_srcu_struct(sp);
949 mutex_lock(&sp->srcu_barrier_mutex);
950 if (rcu_seq_done(&sp->srcu_barrier_seq, s)) {
951 smp_mb(); /* Force ordering following return. */
952 mutex_unlock(&sp->srcu_barrier_mutex);
953 return; /* Someone else did our work for us. */
954 }
955 rcu_seq_start(&sp->srcu_barrier_seq);
956 init_completion(&sp->srcu_barrier_completion);
957
958 /* Initial count prevents reaching zero until all CBs are posted. */
959 atomic_set(&sp->srcu_barrier_cpu_cnt, 1);
960
961 /*
962 * Each pass through this loop enqueues a callback, but only
963 * on CPUs already having callbacks enqueued. Note that if
964 * a CPU already has callbacks enqueue, it must have already
965 * registered the need for a future grace period, so all we
966 * need do is enqueue a callback that will use the same
967 * grace period as the last callback already in the queue.
968 */
969 for_each_possible_cpu(cpu) {
970 sdp = per_cpu_ptr(sp->sda, cpu);
971 spin_lock_irq(&sdp->lock);
972 atomic_inc(&sp->srcu_barrier_cpu_cnt);
973 sdp->srcu_barrier_head.func = srcu_barrier_cb;
974 if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
975 &sdp->srcu_barrier_head, 0))
976 atomic_dec(&sp->srcu_barrier_cpu_cnt);
977 spin_unlock_irq(&sdp->lock);
978 }
979
980 /* Remove the initial count, at which point reaching zero can happen. */
981 if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
982 complete(&sp->srcu_barrier_completion);
983 wait_for_completion(&sp->srcu_barrier_completion);
984
985 rcu_seq_end(&sp->srcu_barrier_seq);
986 mutex_unlock(&sp->srcu_barrier_mutex);
Paul E. McKenneydad81a22017-03-25 17:23:44 -0700987}
988EXPORT_SYMBOL_GPL(srcu_barrier);
989
990/**
991 * srcu_batches_completed - return batches completed.
992 * @sp: srcu_struct on which to report batch completion.
993 *
994 * Report the number of batches, correlated with, but not necessarily
995 * precisely the same as, the number of grace periods that have elapsed.
996 */
997unsigned long srcu_batches_completed(struct srcu_struct *sp)
998{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700999 return sp->srcu_idx;
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001000}
1001EXPORT_SYMBOL_GPL(srcu_batches_completed);
1002
1003/*
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001004 * Core SRCU state machine. Push state bits of ->srcu_gp_seq
1005 * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
1006 * completed in that state.
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001007 */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001008static void srcu_advance_state(struct srcu_struct *sp)
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001009{
1010 int idx;
1011
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001012 mutex_lock(&sp->srcu_gp_mutex);
1013
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001014 /*
1015 * Because readers might be delayed for an extended period after
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001016 * fetching ->srcu_idx for their index, at any point in time there
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001017 * might well be readers using both idx=0 and idx=1. We therefore
1018 * need to wait for readers to clear from both index values before
1019 * invoking a callback.
1020 *
1021 * The load-acquire ensures that we see the accesses performed
1022 * by the prior grace period.
1023 */
1024 idx = rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq)); /* ^^^ */
1025 if (idx == SRCU_STATE_IDLE) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001026 spin_lock_irq(&sp->gp_lock);
1027 if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
1028 WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq));
1029 spin_unlock_irq(&sp->gp_lock);
1030 mutex_unlock(&sp->srcu_gp_mutex);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001031 return;
1032 }
1033 idx = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
1034 if (idx == SRCU_STATE_IDLE)
1035 srcu_gp_start(sp);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001036 spin_unlock_irq(&sp->gp_lock);
1037 if (idx != SRCU_STATE_IDLE) {
1038 mutex_unlock(&sp->srcu_gp_mutex);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001039 return; /* Someone else started the grace period. */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001040 }
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001041 }
1042
1043 if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001044 idx = 1 ^ (sp->srcu_idx & 1);
1045 if (!try_check_zero(sp, idx, 1)) {
1046 mutex_unlock(&sp->srcu_gp_mutex);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001047 return; /* readers present, retry later. */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001048 }
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001049 srcu_flip(sp);
1050 rcu_seq_set_state(&sp->srcu_gp_seq, SRCU_STATE_SCAN2);
1051 }
1052
1053 if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
1054
1055 /*
1056 * SRCU read-side critical sections are normally short,
1057 * so check at least twice in quick succession after a flip.
1058 */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001059 idx = 1 ^ (sp->srcu_idx & 1);
1060 if (!try_check_zero(sp, idx, 2)) {
1061 mutex_unlock(&sp->srcu_gp_mutex);
1062 return; /* readers present, retry later. */
1063 }
1064 srcu_gp_end(sp); /* Releases ->srcu_gp_mutex. */
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001065 }
1066}
1067
1068/*
1069 * Invoke a limited number of SRCU callbacks that have passed through
1070 * their grace period. If there are more to do, SRCU will reschedule
1071 * the workqueue. Note that needed memory barriers have been executed
1072 * in this task's context by srcu_readers_active_idx_check().
1073 */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001074static void srcu_invoke_callbacks(struct work_struct *work)
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001075{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001076 bool more;
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001077 struct rcu_cblist ready_cbs;
1078 struct rcu_head *rhp;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001079 struct srcu_data *sdp;
1080 struct srcu_struct *sp;
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001081
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001082 sdp = container_of(work, struct srcu_data, work.work);
1083 sp = sdp->sp;
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001084 rcu_cblist_init(&ready_cbs);
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001085 spin_lock_irq(&sdp->lock);
1086 smp_mb(); /* Old grace periods before callback invocation! */
1087 rcu_segcblist_advance(&sdp->srcu_cblist,
1088 rcu_seq_current(&sp->srcu_gp_seq));
1089 if (sdp->srcu_cblist_invoking ||
1090 !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
1091 spin_unlock_irq(&sdp->lock);
1092 return; /* Someone else on the job or nothing to do. */
1093 }
1094
1095 /* We are on the job! Extract and invoke ready callbacks. */
1096 sdp->srcu_cblist_invoking = true;
1097 rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
1098 spin_unlock_irq(&sdp->lock);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001099 rhp = rcu_cblist_dequeue(&ready_cbs);
1100 for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
1101 local_bh_disable();
1102 rhp->func(rhp);
1103 local_bh_enable();
1104 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001105
1106 /*
1107 * Update counts, accelerate new callbacks, and if needed,
1108 * schedule another round of callback invocation.
1109 */
1110 spin_lock_irq(&sdp->lock);
1111 rcu_segcblist_insert_count(&sdp->srcu_cblist, &ready_cbs);
1112 (void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
1113 rcu_seq_snap(&sp->srcu_gp_seq));
1114 sdp->srcu_cblist_invoking = false;
1115 more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
1116 spin_unlock_irq(&sdp->lock);
1117 if (more)
1118 srcu_schedule_cbs_sdp(sdp, 0);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001119}
1120
1121/*
1122 * Finished one round of SRCU grace period. Start another if there are
1123 * more SRCU callbacks queued, otherwise put SRCU into not-running state.
1124 */
1125static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay)
1126{
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001127 bool pushgp = true;
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001128
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001129 spin_lock_irq(&sp->gp_lock);
1130 if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
1131 if (!WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq))) {
1132 /* All requests fulfilled, time to go idle. */
1133 pushgp = false;
1134 }
1135 } else if (!rcu_seq_state(sp->srcu_gp_seq)) {
1136 /* Outstanding request and no GP. Start one. */
1137 srcu_gp_start(sp);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001138 }
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001139 spin_unlock_irq(&sp->gp_lock);
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001140
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001141 if (pushgp)
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001142 queue_delayed_work(system_power_efficient_wq, &sp->work, delay);
1143}
1144
1145/*
1146 * This is the work-queue function that handles SRCU grace periods.
1147 */
1148void process_srcu(struct work_struct *work)
1149{
1150 struct srcu_struct *sp;
1151
1152 sp = container_of(work, struct srcu_struct, work.work);
1153
Paul E. McKenneyda915ad2017-04-05 09:01:53 -07001154 srcu_advance_state(sp);
Paul E. McKenney1e9a0382017-04-24 16:02:09 -07001155 srcu_reschedule(sp, srcu_get_delay(sp));
Paul E. McKenneydad81a22017-03-25 17:23:44 -07001156}
1157EXPORT_SYMBOL_GPL(process_srcu);
Paul E. McKenney7f6733c2017-04-18 17:17:35 -07001158
1159void srcutorture_get_gp_data(enum rcutorture_type test_type,
Paul E. McKenney1e9a0382017-04-24 16:02:09 -07001160 struct srcu_struct *sp, int *flags,
1161 unsigned long *gpnum, unsigned long *completed)
Paul E. McKenney7f6733c2017-04-18 17:17:35 -07001162{
1163 if (test_type != SRCU_FLAVOR)
1164 return;
1165 *flags = 0;
1166 *completed = rcu_seq_ctr(sp->srcu_gp_seq);
1167 *gpnum = rcu_seq_ctr(sp->srcu_gp_seq_needed);
1168}
1169EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
Paul E. McKenney1f4f6da2017-04-21 11:16:32 -07001170
1171static int __init srcu_bootup_announce(void)
1172{
1173 pr_info("Hierarchical SRCU implementation.\n");
1174 return 0;
1175}
1176early_initcall(srcu_bootup_announce);