xref: /linux/kernel/rcu/srcutree.c (revision 2da4b2a7fd8de52c44fa83c59f00d38a0d90caae)
1dad81a20SPaul E. McKenney /*
2dad81a20SPaul E. McKenney  * Sleepable Read-Copy Update mechanism for mutual exclusion.
3dad81a20SPaul E. McKenney  *
4dad81a20SPaul E. McKenney  * This program is free software; you can redistribute it and/or modify
5dad81a20SPaul E. McKenney  * it under the terms of the GNU General Public License as published by
6dad81a20SPaul E. McKenney  * the Free Software Foundation; either version 2 of the License, or
7dad81a20SPaul E. McKenney  * (at your option) any later version.
8dad81a20SPaul E. McKenney  *
9dad81a20SPaul E. McKenney  * This program is distributed in the hope that it will be useful,
10dad81a20SPaul E. McKenney  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11dad81a20SPaul E. McKenney  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12dad81a20SPaul E. McKenney  * GNU General Public License for more details.
13dad81a20SPaul E. McKenney  *
14dad81a20SPaul E. McKenney  * You should have received a copy of the GNU General Public License
15dad81a20SPaul E. McKenney  * along with this program; if not, you can access it online at
16dad81a20SPaul E. McKenney  * http://www.gnu.org/licenses/gpl-2.0.html.
17dad81a20SPaul E. McKenney  *
18dad81a20SPaul E. McKenney  * Copyright (C) IBM Corporation, 2006
19dad81a20SPaul E. McKenney  * Copyright (C) Fujitsu, 2012
20dad81a20SPaul E. McKenney  *
21dad81a20SPaul E. McKenney  * Author: Paul McKenney <paulmck@us.ibm.com>
22dad81a20SPaul E. McKenney  *	   Lai Jiangshan <laijs@cn.fujitsu.com>
23dad81a20SPaul E. McKenney  *
24dad81a20SPaul E. McKenney  * For detailed explanation of Read-Copy Update mechanism see -
25dad81a20SPaul E. McKenney  *		Documentation/RCU/ *.txt
26dad81a20SPaul E. McKenney  *
27dad81a20SPaul E. McKenney  */
28dad81a20SPaul E. McKenney 
29dad81a20SPaul E. McKenney #include <linux/export.h>
30dad81a20SPaul E. McKenney #include <linux/mutex.h>
31dad81a20SPaul E. McKenney #include <linux/percpu.h>
32dad81a20SPaul E. McKenney #include <linux/preempt.h>
33dad81a20SPaul E. McKenney #include <linux/rcupdate_wait.h>
34dad81a20SPaul E. McKenney #include <linux/sched.h>
35dad81a20SPaul E. McKenney #include <linux/smp.h>
36dad81a20SPaul E. McKenney #include <linux/delay.h>
37dad81a20SPaul E. McKenney #include <linux/srcu.h>
38dad81a20SPaul E. McKenney 
39dad81a20SPaul E. McKenney #include "rcu.h"
40dad81a20SPaul E. McKenney 
41da915ad5SPaul E. McKenney static void srcu_invoke_callbacks(struct work_struct *work);
42da915ad5SPaul E. McKenney static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay);
43da915ad5SPaul E. McKenney 
44da915ad5SPaul E. McKenney /*
45da915ad5SPaul E. McKenney  * Initialize SRCU combining tree.  Note that statically allocated
46da915ad5SPaul E. McKenney  * srcu_struct structures might already have srcu_read_lock() and
47da915ad5SPaul E. McKenney  * srcu_read_unlock() running against them.  So if the is_static parameter
48da915ad5SPaul E. McKenney  * is set, don't initialize ->srcu_lock_count[] and ->srcu_unlock_count[].
49da915ad5SPaul E. McKenney  */
50da915ad5SPaul E. McKenney static void init_srcu_struct_nodes(struct srcu_struct *sp, bool is_static)
51dad81a20SPaul E. McKenney {
52da915ad5SPaul E. McKenney 	int cpu;
53da915ad5SPaul E. McKenney 	int i;
54da915ad5SPaul E. McKenney 	int level = 0;
55da915ad5SPaul E. McKenney 	int levelspread[RCU_NUM_LVLS];
56da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
57da915ad5SPaul E. McKenney 	struct srcu_node *snp;
58da915ad5SPaul E. McKenney 	struct srcu_node *snp_first;
59da915ad5SPaul E. McKenney 
60da915ad5SPaul E. McKenney 	/* Work out the overall tree geometry. */
61da915ad5SPaul E. McKenney 	sp->level[0] = &sp->node[0];
62da915ad5SPaul E. McKenney 	for (i = 1; i < rcu_num_lvls; i++)
63da915ad5SPaul E. McKenney 		sp->level[i] = sp->level[i - 1] + num_rcu_lvl[i - 1];
64da915ad5SPaul E. McKenney 	rcu_init_levelspread(levelspread, num_rcu_lvl);
65da915ad5SPaul E. McKenney 
66da915ad5SPaul E. McKenney 	/* Each pass through this loop initializes one srcu_node structure. */
67da915ad5SPaul E. McKenney 	rcu_for_each_node_breadth_first(sp, snp) {
68da915ad5SPaul E. McKenney 		spin_lock_init(&snp->lock);
69c7e88067SPaul E. McKenney 		WARN_ON_ONCE(ARRAY_SIZE(snp->srcu_have_cbs) !=
70c7e88067SPaul E. McKenney 			     ARRAY_SIZE(snp->srcu_data_have_cbs));
71c7e88067SPaul E. McKenney 		for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {
72da915ad5SPaul E. McKenney 			snp->srcu_have_cbs[i] = 0;
73c7e88067SPaul E. McKenney 			snp->srcu_data_have_cbs[i] = 0;
74c7e88067SPaul E. McKenney 		}
751e9a038bSPaul E. McKenney 		snp->srcu_gp_seq_needed_exp = 0;
76da915ad5SPaul E. McKenney 		snp->grplo = -1;
77da915ad5SPaul E. McKenney 		snp->grphi = -1;
78da915ad5SPaul E. McKenney 		if (snp == &sp->node[0]) {
79da915ad5SPaul E. McKenney 			/* Root node, special case. */
80da915ad5SPaul E. McKenney 			snp->srcu_parent = NULL;
81da915ad5SPaul E. McKenney 			continue;
82da915ad5SPaul E. McKenney 		}
83da915ad5SPaul E. McKenney 
84da915ad5SPaul E. McKenney 		/* Non-root node. */
85da915ad5SPaul E. McKenney 		if (snp == sp->level[level + 1])
86da915ad5SPaul E. McKenney 			level++;
87da915ad5SPaul E. McKenney 		snp->srcu_parent = sp->level[level - 1] +
88da915ad5SPaul E. McKenney 				   (snp - sp->level[level]) /
89da915ad5SPaul E. McKenney 				   levelspread[level - 1];
90da915ad5SPaul E. McKenney 	}
91da915ad5SPaul E. McKenney 
92da915ad5SPaul E. McKenney 	/*
93da915ad5SPaul E. McKenney 	 * Initialize the per-CPU srcu_data array, which feeds into the
94da915ad5SPaul E. McKenney 	 * leaves of the srcu_node tree.
95da915ad5SPaul E. McKenney 	 */
96da915ad5SPaul E. McKenney 	WARN_ON_ONCE(ARRAY_SIZE(sdp->srcu_lock_count) !=
97da915ad5SPaul E. McKenney 		     ARRAY_SIZE(sdp->srcu_unlock_count));
98da915ad5SPaul E. McKenney 	level = rcu_num_lvls - 1;
99da915ad5SPaul E. McKenney 	snp_first = sp->level[level];
100da915ad5SPaul E. McKenney 	for_each_possible_cpu(cpu) {
101da915ad5SPaul E. McKenney 		sdp = per_cpu_ptr(sp->sda, cpu);
102da915ad5SPaul E. McKenney 		spin_lock_init(&sdp->lock);
103da915ad5SPaul E. McKenney 		rcu_segcblist_init(&sdp->srcu_cblist);
104da915ad5SPaul E. McKenney 		sdp->srcu_cblist_invoking = false;
105da915ad5SPaul E. McKenney 		sdp->srcu_gp_seq_needed = sp->srcu_gp_seq;
1061e9a038bSPaul E. McKenney 		sdp->srcu_gp_seq_needed_exp = sp->srcu_gp_seq;
107da915ad5SPaul E. McKenney 		sdp->mynode = &snp_first[cpu / levelspread[level]];
108da915ad5SPaul E. McKenney 		for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {
109da915ad5SPaul E. McKenney 			if (snp->grplo < 0)
110da915ad5SPaul E. McKenney 				snp->grplo = cpu;
111da915ad5SPaul E. McKenney 			snp->grphi = cpu;
112da915ad5SPaul E. McKenney 		}
113da915ad5SPaul E. McKenney 		sdp->cpu = cpu;
114da915ad5SPaul E. McKenney 		INIT_DELAYED_WORK(&sdp->work, srcu_invoke_callbacks);
115da915ad5SPaul E. McKenney 		sdp->sp = sp;
116c7e88067SPaul E. McKenney 		sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
117da915ad5SPaul E. McKenney 		if (is_static)
118da915ad5SPaul E. McKenney 			continue;
119da915ad5SPaul E. McKenney 
120da915ad5SPaul E. McKenney 		/* Dynamically allocated, better be no srcu_read_locks()! */
121da915ad5SPaul E. McKenney 		for (i = 0; i < ARRAY_SIZE(sdp->srcu_lock_count); i++) {
122da915ad5SPaul E. McKenney 			sdp->srcu_lock_count[i] = 0;
123da915ad5SPaul E. McKenney 			sdp->srcu_unlock_count[i] = 0;
124da915ad5SPaul E. McKenney 		}
125da915ad5SPaul E. McKenney 	}
126da915ad5SPaul E. McKenney }
127da915ad5SPaul E. McKenney 
128da915ad5SPaul E. McKenney /*
129da915ad5SPaul E. McKenney  * Initialize non-compile-time initialized fields, including the
130da915ad5SPaul E. McKenney  * associated srcu_node and srcu_data structures.  The is_static
131da915ad5SPaul E. McKenney  * parameter is passed through to init_srcu_struct_nodes(), and
132da915ad5SPaul E. McKenney  * also tells us that ->sda has already been wired up to srcu_data.
133da915ad5SPaul E. McKenney  */
134da915ad5SPaul E. McKenney static int init_srcu_struct_fields(struct srcu_struct *sp, bool is_static)
135da915ad5SPaul E. McKenney {
136da915ad5SPaul E. McKenney 	mutex_init(&sp->srcu_cb_mutex);
137da915ad5SPaul E. McKenney 	mutex_init(&sp->srcu_gp_mutex);
138da915ad5SPaul E. McKenney 	sp->srcu_idx = 0;
139dad81a20SPaul E. McKenney 	sp->srcu_gp_seq = 0;
140da915ad5SPaul E. McKenney 	sp->srcu_barrier_seq = 0;
141da915ad5SPaul E. McKenney 	mutex_init(&sp->srcu_barrier_mutex);
142da915ad5SPaul E. McKenney 	atomic_set(&sp->srcu_barrier_cpu_cnt, 0);
143dad81a20SPaul E. McKenney 	INIT_DELAYED_WORK(&sp->work, process_srcu);
144da915ad5SPaul E. McKenney 	if (!is_static)
145da915ad5SPaul E. McKenney 		sp->sda = alloc_percpu(struct srcu_data);
146da915ad5SPaul E. McKenney 	init_srcu_struct_nodes(sp, is_static);
1471e9a038bSPaul E. McKenney 	sp->srcu_gp_seq_needed_exp = 0;
148da915ad5SPaul E. McKenney 	smp_store_release(&sp->srcu_gp_seq_needed, 0); /* Init done. */
149da915ad5SPaul E. McKenney 	return sp->sda ? 0 : -ENOMEM;
150dad81a20SPaul E. McKenney }
151dad81a20SPaul E. McKenney 
152dad81a20SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
153dad81a20SPaul E. McKenney 
154dad81a20SPaul E. McKenney int __init_srcu_struct(struct srcu_struct *sp, const char *name,
155dad81a20SPaul E. McKenney 		       struct lock_class_key *key)
156dad81a20SPaul E. McKenney {
157dad81a20SPaul E. McKenney 	/* Don't re-initialize a lock while it is held. */
158dad81a20SPaul E. McKenney 	debug_check_no_locks_freed((void *)sp, sizeof(*sp));
159dad81a20SPaul E. McKenney 	lockdep_init_map(&sp->dep_map, name, key, 0);
160da915ad5SPaul E. McKenney 	spin_lock_init(&sp->gp_lock);
161da915ad5SPaul E. McKenney 	return init_srcu_struct_fields(sp, false);
162dad81a20SPaul E. McKenney }
163dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__init_srcu_struct);
164dad81a20SPaul E. McKenney 
165dad81a20SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
166dad81a20SPaul E. McKenney 
167dad81a20SPaul E. McKenney /**
168dad81a20SPaul E. McKenney  * init_srcu_struct - initialize a sleep-RCU structure
169dad81a20SPaul E. McKenney  * @sp: structure to initialize.
170dad81a20SPaul E. McKenney  *
171dad81a20SPaul E. McKenney  * Must invoke this on a given srcu_struct before passing that srcu_struct
172dad81a20SPaul E. McKenney  * to any other function.  Each srcu_struct represents a separate domain
173dad81a20SPaul E. McKenney  * of SRCU protection.
174dad81a20SPaul E. McKenney  */
175dad81a20SPaul E. McKenney int init_srcu_struct(struct srcu_struct *sp)
176dad81a20SPaul E. McKenney {
177da915ad5SPaul E. McKenney 	spin_lock_init(&sp->gp_lock);
178da915ad5SPaul E. McKenney 	return init_srcu_struct_fields(sp, false);
179dad81a20SPaul E. McKenney }
180dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(init_srcu_struct);
181dad81a20SPaul E. McKenney 
182dad81a20SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
183dad81a20SPaul E. McKenney 
184dad81a20SPaul E. McKenney /*
185da915ad5SPaul E. McKenney  * First-use initialization of statically allocated srcu_struct
186da915ad5SPaul E. McKenney  * structure.  Wiring up the combining tree is more than can be
187da915ad5SPaul E. McKenney  * done with compile-time initialization, so this check is added
188da915ad5SPaul E. McKenney  * to each update-side SRCU primitive.  Use ->gp_lock, which -is-
189da915ad5SPaul E. McKenney  * compile-time initialized, to resolve races involving multiple
190da915ad5SPaul E. McKenney  * CPUs trying to garner first-use privileges.
191da915ad5SPaul E. McKenney  */
192da915ad5SPaul E. McKenney static void check_init_srcu_struct(struct srcu_struct *sp)
193da915ad5SPaul E. McKenney {
194da915ad5SPaul E. McKenney 	unsigned long flags;
195da915ad5SPaul E. McKenney 
196da915ad5SPaul E. McKenney 	WARN_ON_ONCE(rcu_scheduler_active == RCU_SCHEDULER_INIT);
197da915ad5SPaul E. McKenney 	/* The smp_load_acquire() pairs with the smp_store_release(). */
198da915ad5SPaul E. McKenney 	if (!rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq_needed))) /*^^^*/
199da915ad5SPaul E. McKenney 		return; /* Already initialized. */
200da915ad5SPaul E. McKenney 	spin_lock_irqsave(&sp->gp_lock, flags);
201da915ad5SPaul E. McKenney 	if (!rcu_seq_state(sp->srcu_gp_seq_needed)) {
202da915ad5SPaul E. McKenney 		spin_unlock_irqrestore(&sp->gp_lock, flags);
203da915ad5SPaul E. McKenney 		return;
204da915ad5SPaul E. McKenney 	}
205da915ad5SPaul E. McKenney 	init_srcu_struct_fields(sp, true);
206da915ad5SPaul E. McKenney 	spin_unlock_irqrestore(&sp->gp_lock, flags);
207da915ad5SPaul E. McKenney }
208da915ad5SPaul E. McKenney 
209da915ad5SPaul E. McKenney /*
210da915ad5SPaul E. McKenney  * Returns approximate total of the readers' ->srcu_lock_count[] values
211da915ad5SPaul E. McKenney  * for the rank of per-CPU counters specified by idx.
212dad81a20SPaul E. McKenney  */
213dad81a20SPaul E. McKenney static unsigned long srcu_readers_lock_idx(struct srcu_struct *sp, int idx)
214dad81a20SPaul E. McKenney {
215dad81a20SPaul E. McKenney 	int cpu;
216dad81a20SPaul E. McKenney 	unsigned long sum = 0;
217dad81a20SPaul E. McKenney 
218dad81a20SPaul E. McKenney 	for_each_possible_cpu(cpu) {
219da915ad5SPaul E. McKenney 		struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
220dad81a20SPaul E. McKenney 
221da915ad5SPaul E. McKenney 		sum += READ_ONCE(cpuc->srcu_lock_count[idx]);
222dad81a20SPaul E. McKenney 	}
223dad81a20SPaul E. McKenney 	return sum;
224dad81a20SPaul E. McKenney }
225dad81a20SPaul E. McKenney 
226dad81a20SPaul E. McKenney /*
227da915ad5SPaul E. McKenney  * Returns approximate total of the readers' ->srcu_unlock_count[] values
228da915ad5SPaul E. McKenney  * for the rank of per-CPU counters specified by idx.
229dad81a20SPaul E. McKenney  */
230dad81a20SPaul E. McKenney static unsigned long srcu_readers_unlock_idx(struct srcu_struct *sp, int idx)
231dad81a20SPaul E. McKenney {
232dad81a20SPaul E. McKenney 	int cpu;
233dad81a20SPaul E. McKenney 	unsigned long sum = 0;
234dad81a20SPaul E. McKenney 
235dad81a20SPaul E. McKenney 	for_each_possible_cpu(cpu) {
236da915ad5SPaul E. McKenney 		struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
237dad81a20SPaul E. McKenney 
238da915ad5SPaul E. McKenney 		sum += READ_ONCE(cpuc->srcu_unlock_count[idx]);
239dad81a20SPaul E. McKenney 	}
240dad81a20SPaul E. McKenney 	return sum;
241dad81a20SPaul E. McKenney }
242dad81a20SPaul E. McKenney 
243dad81a20SPaul E. McKenney /*
244dad81a20SPaul E. McKenney  * Return true if the number of pre-existing readers is determined to
245dad81a20SPaul E. McKenney  * be zero.
246dad81a20SPaul E. McKenney  */
247dad81a20SPaul E. McKenney static bool srcu_readers_active_idx_check(struct srcu_struct *sp, int idx)
248dad81a20SPaul E. McKenney {
249dad81a20SPaul E. McKenney 	unsigned long unlocks;
250dad81a20SPaul E. McKenney 
251dad81a20SPaul E. McKenney 	unlocks = srcu_readers_unlock_idx(sp, idx);
252dad81a20SPaul E. McKenney 
253dad81a20SPaul E. McKenney 	/*
254dad81a20SPaul E. McKenney 	 * Make sure that a lock is always counted if the corresponding
255dad81a20SPaul E. McKenney 	 * unlock is counted. Needs to be a smp_mb() as the read side may
256dad81a20SPaul E. McKenney 	 * contain a read from a variable that is written to before the
257dad81a20SPaul E. McKenney 	 * synchronize_srcu() in the write side. In this case smp_mb()s
258dad81a20SPaul E. McKenney 	 * A and B act like the store buffering pattern.
259dad81a20SPaul E. McKenney 	 *
260dad81a20SPaul E. McKenney 	 * This smp_mb() also pairs with smp_mb() C to prevent accesses
261dad81a20SPaul E. McKenney 	 * after the synchronize_srcu() from being executed before the
262dad81a20SPaul E. McKenney 	 * grace period ends.
263dad81a20SPaul E. McKenney 	 */
264dad81a20SPaul E. McKenney 	smp_mb(); /* A */
265dad81a20SPaul E. McKenney 
266dad81a20SPaul E. McKenney 	/*
267dad81a20SPaul E. McKenney 	 * If the locks are the same as the unlocks, then there must have
268dad81a20SPaul E. McKenney 	 * been no readers on this index at some time in between. This does
269dad81a20SPaul E. McKenney 	 * not mean that there are no more readers, as one could have read
270dad81a20SPaul E. McKenney 	 * the current index but not have incremented the lock counter yet.
271dad81a20SPaul E. McKenney 	 *
272dad81a20SPaul E. McKenney 	 * Possible bug: There is no guarantee that there haven't been
273da915ad5SPaul E. McKenney 	 * ULONG_MAX increments of ->srcu_lock_count[] since the unlocks were
274dad81a20SPaul E. McKenney 	 * counted, meaning that this could return true even if there are
275dad81a20SPaul E. McKenney 	 * still active readers.  Since there are no memory barriers around
276da915ad5SPaul E. McKenney 	 * srcu_flip(), the CPU is not required to increment ->srcu_idx
277dad81a20SPaul E. McKenney 	 * before running srcu_readers_unlock_idx(), which means that there
278dad81a20SPaul E. McKenney 	 * could be an arbitrarily large number of critical sections that
279dad81a20SPaul E. McKenney 	 * execute after srcu_readers_unlock_idx() but use the old value
280da915ad5SPaul E. McKenney 	 * of ->srcu_idx.
281dad81a20SPaul E. McKenney 	 */
282dad81a20SPaul E. McKenney 	return srcu_readers_lock_idx(sp, idx) == unlocks;
283dad81a20SPaul E. McKenney }
284dad81a20SPaul E. McKenney 
285dad81a20SPaul E. McKenney /**
286dad81a20SPaul E. McKenney  * srcu_readers_active - returns true if there are readers. and false
287dad81a20SPaul E. McKenney  *                       otherwise
288dad81a20SPaul E. McKenney  * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
289dad81a20SPaul E. McKenney  *
290dad81a20SPaul E. McKenney  * Note that this is not an atomic primitive, and can therefore suffer
291dad81a20SPaul E. McKenney  * severe errors when invoked on an active srcu_struct.  That said, it
292dad81a20SPaul E. McKenney  * can be useful as an error check at cleanup time.
293dad81a20SPaul E. McKenney  */
294dad81a20SPaul E. McKenney static bool srcu_readers_active(struct srcu_struct *sp)
295dad81a20SPaul E. McKenney {
296dad81a20SPaul E. McKenney 	int cpu;
297dad81a20SPaul E. McKenney 	unsigned long sum = 0;
298dad81a20SPaul E. McKenney 
299dad81a20SPaul E. McKenney 	for_each_possible_cpu(cpu) {
300da915ad5SPaul E. McKenney 		struct srcu_data *cpuc = per_cpu_ptr(sp->sda, cpu);
301dad81a20SPaul E. McKenney 
302da915ad5SPaul E. McKenney 		sum += READ_ONCE(cpuc->srcu_lock_count[0]);
303da915ad5SPaul E. McKenney 		sum += READ_ONCE(cpuc->srcu_lock_count[1]);
304da915ad5SPaul E. McKenney 		sum -= READ_ONCE(cpuc->srcu_unlock_count[0]);
305da915ad5SPaul E. McKenney 		sum -= READ_ONCE(cpuc->srcu_unlock_count[1]);
306dad81a20SPaul E. McKenney 	}
307dad81a20SPaul E. McKenney 	return sum;
308dad81a20SPaul E. McKenney }
309dad81a20SPaul E. McKenney 
310dad81a20SPaul E. McKenney #define SRCU_INTERVAL		1
311dad81a20SPaul E. McKenney 
3121e9a038bSPaul E. McKenney /*
3131e9a038bSPaul E. McKenney  * Return grace-period delay, zero if there are expedited grace
3141e9a038bSPaul E. McKenney  * periods pending, SRCU_INTERVAL otherwise.
3151e9a038bSPaul E. McKenney  */
3161e9a038bSPaul E. McKenney static unsigned long srcu_get_delay(struct srcu_struct *sp)
3171e9a038bSPaul E. McKenney {
3181e9a038bSPaul E. McKenney 	if (ULONG_CMP_LT(READ_ONCE(sp->srcu_gp_seq),
3191e9a038bSPaul E. McKenney 			 READ_ONCE(sp->srcu_gp_seq_needed_exp)))
3201e9a038bSPaul E. McKenney 		return 0;
3211e9a038bSPaul E. McKenney 	return SRCU_INTERVAL;
3221e9a038bSPaul E. McKenney }
3231e9a038bSPaul E. McKenney 
324dad81a20SPaul E. McKenney /**
325dad81a20SPaul E. McKenney  * cleanup_srcu_struct - deconstruct a sleep-RCU structure
326dad81a20SPaul E. McKenney  * @sp: structure to clean up.
327dad81a20SPaul E. McKenney  *
328dad81a20SPaul E. McKenney  * Must invoke this after you are finished using a given srcu_struct that
329dad81a20SPaul E. McKenney  * was initialized via init_srcu_struct(), else you leak memory.
330dad81a20SPaul E. McKenney  */
331dad81a20SPaul E. McKenney void cleanup_srcu_struct(struct srcu_struct *sp)
332dad81a20SPaul E. McKenney {
333da915ad5SPaul E. McKenney 	int cpu;
334da915ad5SPaul E. McKenney 
3351e9a038bSPaul E. McKenney 	if (WARN_ON(!srcu_get_delay(sp)))
3361e9a038bSPaul E. McKenney 		return; /* Leakage unless caller handles error. */
337dad81a20SPaul E. McKenney 	if (WARN_ON(srcu_readers_active(sp)))
338dad81a20SPaul E. McKenney 		return; /* Leakage unless caller handles error. */
339dad81a20SPaul E. McKenney 	flush_delayed_work(&sp->work);
340da915ad5SPaul E. McKenney 	for_each_possible_cpu(cpu)
341da915ad5SPaul E. McKenney 		flush_delayed_work(&per_cpu_ptr(sp->sda, cpu)->work);
342da915ad5SPaul E. McKenney 	if (WARN_ON(rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
343da915ad5SPaul E. McKenney 	    WARN_ON(srcu_readers_active(sp))) {
344da915ad5SPaul E. McKenney 		pr_info("cleanup_srcu_struct: Active srcu_struct %p state: %d\n", sp, rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)));
345dad81a20SPaul E. McKenney 		return; /* Caller forgot to stop doing call_srcu()? */
346dad81a20SPaul E. McKenney 	}
347da915ad5SPaul E. McKenney 	free_percpu(sp->sda);
348da915ad5SPaul E. McKenney 	sp->sda = NULL;
349dad81a20SPaul E. McKenney }
350dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
351dad81a20SPaul E. McKenney 
352dad81a20SPaul E. McKenney /*
353dad81a20SPaul E. McKenney  * Counts the new reader in the appropriate per-CPU element of the
354dad81a20SPaul E. McKenney  * srcu_struct.  Must be called from process context.
355dad81a20SPaul E. McKenney  * Returns an index that must be passed to the matching srcu_read_unlock().
356dad81a20SPaul E. McKenney  */
357dad81a20SPaul E. McKenney int __srcu_read_lock(struct srcu_struct *sp)
358dad81a20SPaul E. McKenney {
359dad81a20SPaul E. McKenney 	int idx;
360dad81a20SPaul E. McKenney 
361da915ad5SPaul E. McKenney 	idx = READ_ONCE(sp->srcu_idx) & 0x1;
362da915ad5SPaul E. McKenney 	__this_cpu_inc(sp->sda->srcu_lock_count[idx]);
363dad81a20SPaul E. McKenney 	smp_mb(); /* B */  /* Avoid leaking the critical section. */
364dad81a20SPaul E. McKenney 	return idx;
365dad81a20SPaul E. McKenney }
366dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_lock);
367dad81a20SPaul E. McKenney 
368dad81a20SPaul E. McKenney /*
369dad81a20SPaul E. McKenney  * Removes the count for the old reader from the appropriate per-CPU
370dad81a20SPaul E. McKenney  * element of the srcu_struct.  Note that this may well be a different
371dad81a20SPaul E. McKenney  * CPU than that which was incremented by the corresponding srcu_read_lock().
372dad81a20SPaul E. McKenney  * Must be called from process context.
373dad81a20SPaul E. McKenney  */
374dad81a20SPaul E. McKenney void __srcu_read_unlock(struct srcu_struct *sp, int idx)
375dad81a20SPaul E. McKenney {
376dad81a20SPaul E. McKenney 	smp_mb(); /* C */  /* Avoid leaking the critical section. */
377da915ad5SPaul E. McKenney 	this_cpu_inc(sp->sda->srcu_unlock_count[idx]);
378dad81a20SPaul E. McKenney }
379dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_unlock);
380dad81a20SPaul E. McKenney 
381dad81a20SPaul E. McKenney /*
382dad81a20SPaul E. McKenney  * We use an adaptive strategy for synchronize_srcu() and especially for
383dad81a20SPaul E. McKenney  * synchronize_srcu_expedited().  We spin for a fixed time period
384dad81a20SPaul E. McKenney  * (defined below) to allow SRCU readers to exit their read-side critical
385dad81a20SPaul E. McKenney  * sections.  If there are still some readers after a few microseconds,
386dad81a20SPaul E. McKenney  * we repeatedly block for 1-millisecond time periods.
387dad81a20SPaul E. McKenney  */
388dad81a20SPaul E. McKenney #define SRCU_RETRY_CHECK_DELAY		5
389dad81a20SPaul E. McKenney 
390dad81a20SPaul E. McKenney /*
391dad81a20SPaul E. McKenney  * Start an SRCU grace period.
392dad81a20SPaul E. McKenney  */
393dad81a20SPaul E. McKenney static void srcu_gp_start(struct srcu_struct *sp)
394dad81a20SPaul E. McKenney {
395da915ad5SPaul E. McKenney 	struct srcu_data *sdp = this_cpu_ptr(sp->sda);
396dad81a20SPaul E. McKenney 	int state;
397dad81a20SPaul E. McKenney 
398da915ad5SPaul E. McKenney 	RCU_LOCKDEP_WARN(!lockdep_is_held(&sp->gp_lock),
399da915ad5SPaul E. McKenney 			 "Invoked srcu_gp_start() without ->gp_lock!");
400da915ad5SPaul E. McKenney 	WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
401da915ad5SPaul E. McKenney 	rcu_segcblist_advance(&sdp->srcu_cblist,
402da915ad5SPaul E. McKenney 			      rcu_seq_current(&sp->srcu_gp_seq));
403da915ad5SPaul E. McKenney 	(void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
404dad81a20SPaul E. McKenney 				       rcu_seq_snap(&sp->srcu_gp_seq));
405*2da4b2a7SPaul E. McKenney 	smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
406dad81a20SPaul E. McKenney 	rcu_seq_start(&sp->srcu_gp_seq);
407dad81a20SPaul E. McKenney 	state = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
408dad81a20SPaul E. McKenney 	WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
409dad81a20SPaul E. McKenney }
410dad81a20SPaul E. McKenney 
411dad81a20SPaul E. McKenney /*
412da915ad5SPaul E. McKenney  * Track online CPUs to guide callback workqueue placement.
413da915ad5SPaul E. McKenney  */
414da915ad5SPaul E. McKenney DEFINE_PER_CPU(bool, srcu_online);
415da915ad5SPaul E. McKenney 
416da915ad5SPaul E. McKenney void srcu_online_cpu(unsigned int cpu)
417da915ad5SPaul E. McKenney {
418da915ad5SPaul E. McKenney 	WRITE_ONCE(per_cpu(srcu_online, cpu), true);
419da915ad5SPaul E. McKenney }
420da915ad5SPaul E. McKenney 
421da915ad5SPaul E. McKenney void srcu_offline_cpu(unsigned int cpu)
422da915ad5SPaul E. McKenney {
423da915ad5SPaul E. McKenney 	WRITE_ONCE(per_cpu(srcu_online, cpu), false);
424da915ad5SPaul E. McKenney }
425da915ad5SPaul E. McKenney 
426da915ad5SPaul E. McKenney /*
427da915ad5SPaul E. McKenney  * Place the workqueue handler on the specified CPU if online, otherwise
428da915ad5SPaul E. McKenney  * just run it whereever.  This is useful for placing workqueue handlers
429da915ad5SPaul E. McKenney  * that are to invoke the specified CPU's callbacks.
430da915ad5SPaul E. McKenney  */
431da915ad5SPaul E. McKenney static bool srcu_queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
432da915ad5SPaul E. McKenney 				       struct delayed_work *dwork,
433da915ad5SPaul E. McKenney 				       unsigned long delay)
434da915ad5SPaul E. McKenney {
435da915ad5SPaul E. McKenney 	bool ret;
436da915ad5SPaul E. McKenney 
437da915ad5SPaul E. McKenney 	preempt_disable();
438da915ad5SPaul E. McKenney 	if (READ_ONCE(per_cpu(srcu_online, cpu)))
439da915ad5SPaul E. McKenney 		ret = queue_delayed_work_on(cpu, wq, dwork, delay);
440da915ad5SPaul E. McKenney 	else
441da915ad5SPaul E. McKenney 		ret = queue_delayed_work(wq, dwork, delay);
442da915ad5SPaul E. McKenney 	preempt_enable();
443da915ad5SPaul E. McKenney 	return ret;
444da915ad5SPaul E. McKenney }
445da915ad5SPaul E. McKenney 
446da915ad5SPaul E. McKenney /*
447da915ad5SPaul E. McKenney  * Schedule callback invocation for the specified srcu_data structure,
448da915ad5SPaul E. McKenney  * if possible, on the corresponding CPU.
449da915ad5SPaul E. McKenney  */
450da915ad5SPaul E. McKenney static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
451da915ad5SPaul E. McKenney {
452da915ad5SPaul E. McKenney 	srcu_queue_delayed_work_on(sdp->cpu, system_power_efficient_wq,
453da915ad5SPaul E. McKenney 				   &sdp->work, delay);
454da915ad5SPaul E. McKenney }
455da915ad5SPaul E. McKenney 
456da915ad5SPaul E. McKenney /*
457da915ad5SPaul E. McKenney  * Schedule callback invocation for all srcu_data structures associated
458c7e88067SPaul E. McKenney  * with the specified srcu_node structure that have callbacks for the
459c7e88067SPaul E. McKenney  * just-completed grace period, the one corresponding to idx.  If possible,
460c7e88067SPaul E. McKenney  * schedule this invocation on the corresponding CPUs.
461da915ad5SPaul E. McKenney  */
462c7e88067SPaul E. McKenney static void srcu_schedule_cbs_snp(struct srcu_struct *sp, struct srcu_node *snp,
4631e9a038bSPaul E. McKenney 				  unsigned long mask, unsigned long delay)
464da915ad5SPaul E. McKenney {
465da915ad5SPaul E. McKenney 	int cpu;
466da915ad5SPaul E. McKenney 
467c7e88067SPaul E. McKenney 	for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
468c7e88067SPaul E. McKenney 		if (!(mask & (1 << (cpu - snp->grplo))))
469c7e88067SPaul E. McKenney 			continue;
4701e9a038bSPaul E. McKenney 		srcu_schedule_cbs_sdp(per_cpu_ptr(sp->sda, cpu), delay);
471da915ad5SPaul E. McKenney 	}
472c7e88067SPaul E. McKenney }
473da915ad5SPaul E. McKenney 
474da915ad5SPaul E. McKenney /*
475da915ad5SPaul E. McKenney  * Note the end of an SRCU grace period.  Initiates callback invocation
476da915ad5SPaul E. McKenney  * and starts a new grace period if needed.
477da915ad5SPaul E. McKenney  *
478da915ad5SPaul E. McKenney  * The ->srcu_cb_mutex acquisition does not protect any data, but
479da915ad5SPaul E. McKenney  * instead prevents more than one grace period from starting while we
480da915ad5SPaul E. McKenney  * are initiating callback invocation.  This allows the ->srcu_have_cbs[]
481da915ad5SPaul E. McKenney  * array to have a finite number of elements.
482da915ad5SPaul E. McKenney  */
483da915ad5SPaul E. McKenney static void srcu_gp_end(struct srcu_struct *sp)
484da915ad5SPaul E. McKenney {
4851e9a038bSPaul E. McKenney 	unsigned long cbdelay;
486da915ad5SPaul E. McKenney 	bool cbs;
487da915ad5SPaul E. McKenney 	unsigned long gpseq;
488da915ad5SPaul E. McKenney 	int idx;
489da915ad5SPaul E. McKenney 	int idxnext;
490c7e88067SPaul E. McKenney 	unsigned long mask;
491da915ad5SPaul E. McKenney 	struct srcu_node *snp;
492da915ad5SPaul E. McKenney 
493da915ad5SPaul E. McKenney 	/* Prevent more than one additional grace period. */
494da915ad5SPaul E. McKenney 	mutex_lock(&sp->srcu_cb_mutex);
495da915ad5SPaul E. McKenney 
496da915ad5SPaul E. McKenney 	/* End the current grace period. */
497da915ad5SPaul E. McKenney 	spin_lock_irq(&sp->gp_lock);
498da915ad5SPaul E. McKenney 	idx = rcu_seq_state(sp->srcu_gp_seq);
499da915ad5SPaul E. McKenney 	WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
5001e9a038bSPaul E. McKenney 	cbdelay = srcu_get_delay(sp);
501da915ad5SPaul E. McKenney 	rcu_seq_end(&sp->srcu_gp_seq);
502da915ad5SPaul E. McKenney 	gpseq = rcu_seq_current(&sp->srcu_gp_seq);
5031e9a038bSPaul E. McKenney 	if (ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, gpseq))
5041e9a038bSPaul E. McKenney 		sp->srcu_gp_seq_needed_exp = gpseq;
505da915ad5SPaul E. McKenney 	spin_unlock_irq(&sp->gp_lock);
506da915ad5SPaul E. McKenney 	mutex_unlock(&sp->srcu_gp_mutex);
507da915ad5SPaul E. McKenney 	/* A new grace period can start at this point.  But only one. */
508da915ad5SPaul E. McKenney 
509da915ad5SPaul E. McKenney 	/* Initiate callback invocation as needed. */
510da915ad5SPaul E. McKenney 	idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
511da915ad5SPaul E. McKenney 	idxnext = (idx + 1) % ARRAY_SIZE(snp->srcu_have_cbs);
512da915ad5SPaul E. McKenney 	rcu_for_each_node_breadth_first(sp, snp) {
513da915ad5SPaul E. McKenney 		spin_lock_irq(&snp->lock);
514da915ad5SPaul E. McKenney 		cbs = false;
515da915ad5SPaul E. McKenney 		if (snp >= sp->level[rcu_num_lvls - 1])
516da915ad5SPaul E. McKenney 			cbs = snp->srcu_have_cbs[idx] == gpseq;
517da915ad5SPaul E. McKenney 		snp->srcu_have_cbs[idx] = gpseq;
518da915ad5SPaul E. McKenney 		rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
5191e9a038bSPaul E. McKenney 		if (ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, gpseq))
5201e9a038bSPaul E. McKenney 			snp->srcu_gp_seq_needed_exp = gpseq;
521c7e88067SPaul E. McKenney 		mask = snp->srcu_data_have_cbs[idx];
522c7e88067SPaul E. McKenney 		snp->srcu_data_have_cbs[idx] = 0;
523da915ad5SPaul E. McKenney 		spin_unlock_irq(&snp->lock);
524da915ad5SPaul E. McKenney 		if (cbs) {
525da915ad5SPaul E. McKenney 			smp_mb(); /* GP end before CB invocation. */
5261e9a038bSPaul E. McKenney 			srcu_schedule_cbs_snp(sp, snp, mask, cbdelay);
527da915ad5SPaul E. McKenney 		}
528da915ad5SPaul E. McKenney 	}
529da915ad5SPaul E. McKenney 
530da915ad5SPaul E. McKenney 	/* Callback initiation done, allow grace periods after next. */
531da915ad5SPaul E. McKenney 	mutex_unlock(&sp->srcu_cb_mutex);
532da915ad5SPaul E. McKenney 
533da915ad5SPaul E. McKenney 	/* Start a new grace period if needed. */
534da915ad5SPaul E. McKenney 	spin_lock_irq(&sp->gp_lock);
535da915ad5SPaul E. McKenney 	gpseq = rcu_seq_current(&sp->srcu_gp_seq);
536da915ad5SPaul E. McKenney 	if (!rcu_seq_state(gpseq) &&
537da915ad5SPaul E. McKenney 	    ULONG_CMP_LT(gpseq, sp->srcu_gp_seq_needed)) {
538da915ad5SPaul E. McKenney 		srcu_gp_start(sp);
539da915ad5SPaul E. McKenney 		spin_unlock_irq(&sp->gp_lock);
540da915ad5SPaul E. McKenney 		/* Throttle expedited grace periods: Should be rare! */
5411e9a038bSPaul E. McKenney 		srcu_reschedule(sp, rcu_seq_ctr(gpseq) & 0x3ff
5421e9a038bSPaul E. McKenney 				    ? 0 : SRCU_INTERVAL);
543da915ad5SPaul E. McKenney 	} else {
544da915ad5SPaul E. McKenney 		spin_unlock_irq(&sp->gp_lock);
545da915ad5SPaul E. McKenney 	}
546da915ad5SPaul E. McKenney }
547da915ad5SPaul E. McKenney 
548da915ad5SPaul E. McKenney /*
5491e9a038bSPaul E. McKenney  * Funnel-locking scheme to scalably mediate many concurrent expedited
5501e9a038bSPaul E. McKenney  * grace-period requests.  This function is invoked for the first known
5511e9a038bSPaul E. McKenney  * expedited request for a grace period that has already been requested,
5521e9a038bSPaul E. McKenney  * but without expediting.  To start a completely new grace period,
5531e9a038bSPaul E. McKenney  * whether expedited or not, use srcu_funnel_gp_start() instead.
5541e9a038bSPaul E. McKenney  */
5551e9a038bSPaul E. McKenney static void srcu_funnel_exp_start(struct srcu_struct *sp, struct srcu_node *snp,
5561e9a038bSPaul E. McKenney 				  unsigned long s)
5571e9a038bSPaul E. McKenney {
5581e9a038bSPaul E. McKenney 	unsigned long flags;
5591e9a038bSPaul E. McKenney 
5601e9a038bSPaul E. McKenney 	for (; snp != NULL; snp = snp->srcu_parent) {
5611e9a038bSPaul E. McKenney 		if (rcu_seq_done(&sp->srcu_gp_seq, s) ||
5621e9a038bSPaul E. McKenney 		    ULONG_CMP_GE(READ_ONCE(snp->srcu_gp_seq_needed_exp), s))
5631e9a038bSPaul E. McKenney 			return;
5641e9a038bSPaul E. McKenney 		spin_lock_irqsave(&snp->lock, flags);
5651e9a038bSPaul E. McKenney 		if (ULONG_CMP_GE(snp->srcu_gp_seq_needed_exp, s)) {
5661e9a038bSPaul E. McKenney 			spin_unlock_irqrestore(&snp->lock, flags);
5671e9a038bSPaul E. McKenney 			return;
5681e9a038bSPaul E. McKenney 		}
5691e9a038bSPaul E. McKenney 		WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
5701e9a038bSPaul E. McKenney 		spin_unlock_irqrestore(&snp->lock, flags);
5711e9a038bSPaul E. McKenney 	}
5721e9a038bSPaul E. McKenney 	spin_lock_irqsave(&sp->gp_lock, flags);
5731e9a038bSPaul E. McKenney 	if (!ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
5741e9a038bSPaul E. McKenney 		sp->srcu_gp_seq_needed_exp = s;
5751e9a038bSPaul E. McKenney 	spin_unlock_irqrestore(&sp->gp_lock, flags);
5761e9a038bSPaul E. McKenney }
5771e9a038bSPaul E. McKenney 
5781e9a038bSPaul E. McKenney /*
579da915ad5SPaul E. McKenney  * Funnel-locking scheme to scalably mediate many concurrent grace-period
580da915ad5SPaul E. McKenney  * requests.  The winner has to do the work of actually starting grace
581da915ad5SPaul E. McKenney  * period s.  Losers must either ensure that their desired grace-period
582da915ad5SPaul E. McKenney  * number is recorded on at least their leaf srcu_node structure, or they
583da915ad5SPaul E. McKenney  * must take steps to invoke their own callbacks.
584da915ad5SPaul E. McKenney  */
5851e9a038bSPaul E. McKenney static void srcu_funnel_gp_start(struct srcu_struct *sp, struct srcu_data *sdp,
5861e9a038bSPaul E. McKenney 				 unsigned long s, bool do_norm)
587da915ad5SPaul E. McKenney {
588da915ad5SPaul E. McKenney 	unsigned long flags;
589da915ad5SPaul E. McKenney 	int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
590da915ad5SPaul E. McKenney 	struct srcu_node *snp = sdp->mynode;
591da915ad5SPaul E. McKenney 	unsigned long snp_seq;
592da915ad5SPaul E. McKenney 
593da915ad5SPaul E. McKenney 	/* Each pass through the loop does one level of the srcu_node tree. */
594da915ad5SPaul E. McKenney 	for (; snp != NULL; snp = snp->srcu_parent) {
595da915ad5SPaul E. McKenney 		if (rcu_seq_done(&sp->srcu_gp_seq, s) && snp != sdp->mynode)
596da915ad5SPaul E. McKenney 			return; /* GP already done and CBs recorded. */
597da915ad5SPaul E. McKenney 		spin_lock_irqsave(&snp->lock, flags);
598da915ad5SPaul E. McKenney 		if (ULONG_CMP_GE(snp->srcu_have_cbs[idx], s)) {
599da915ad5SPaul E. McKenney 			snp_seq = snp->srcu_have_cbs[idx];
600c7e88067SPaul E. McKenney 			if (snp == sdp->mynode && snp_seq == s)
601c7e88067SPaul E. McKenney 				snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
602da915ad5SPaul E. McKenney 			spin_unlock_irqrestore(&snp->lock, flags);
603da915ad5SPaul E. McKenney 			if (snp == sdp->mynode && snp_seq != s) {
604da915ad5SPaul E. McKenney 				smp_mb(); /* CBs after GP! */
6051e9a038bSPaul E. McKenney 				srcu_schedule_cbs_sdp(sdp, do_norm
6061e9a038bSPaul E. McKenney 							   ? SRCU_INTERVAL
6071e9a038bSPaul E. McKenney 							   : 0);
6081e9a038bSPaul E. McKenney 				return;
609da915ad5SPaul E. McKenney 			}
6101e9a038bSPaul E. McKenney 			if (!do_norm)
6111e9a038bSPaul E. McKenney 				srcu_funnel_exp_start(sp, snp, s);
612da915ad5SPaul E. McKenney 			return;
613da915ad5SPaul E. McKenney 		}
614da915ad5SPaul E. McKenney 		snp->srcu_have_cbs[idx] = s;
615c7e88067SPaul E. McKenney 		if (snp == sdp->mynode)
616c7e88067SPaul E. McKenney 			snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
6171e9a038bSPaul E. McKenney 		if (!do_norm && ULONG_CMP_LT(snp->srcu_gp_seq_needed_exp, s))
6181e9a038bSPaul E. McKenney 			snp->srcu_gp_seq_needed_exp = s;
619da915ad5SPaul E. McKenney 		spin_unlock_irqrestore(&snp->lock, flags);
620da915ad5SPaul E. McKenney 	}
621da915ad5SPaul E. McKenney 
622da915ad5SPaul E. McKenney 	/* Top of tree, must ensure the grace period will be started. */
623da915ad5SPaul E. McKenney 	spin_lock_irqsave(&sp->gp_lock, flags);
624da915ad5SPaul E. McKenney 	if (ULONG_CMP_LT(sp->srcu_gp_seq_needed, s)) {
625da915ad5SPaul E. McKenney 		/*
626da915ad5SPaul E. McKenney 		 * Record need for grace period s.  Pair with load
627da915ad5SPaul E. McKenney 		 * acquire setting up for initialization.
628da915ad5SPaul E. McKenney 		 */
629da915ad5SPaul E. McKenney 		smp_store_release(&sp->srcu_gp_seq_needed, s); /*^^^*/
630da915ad5SPaul E. McKenney 	}
6311e9a038bSPaul E. McKenney 	if (!do_norm && ULONG_CMP_LT(sp->srcu_gp_seq_needed_exp, s))
6321e9a038bSPaul E. McKenney 		sp->srcu_gp_seq_needed_exp = s;
633da915ad5SPaul E. McKenney 
634da915ad5SPaul E. McKenney 	/* If grace period not already done and none in progress, start it. */
635da915ad5SPaul E. McKenney 	if (!rcu_seq_done(&sp->srcu_gp_seq, s) &&
636da915ad5SPaul E. McKenney 	    rcu_seq_state(sp->srcu_gp_seq) == SRCU_STATE_IDLE) {
637da915ad5SPaul E. McKenney 		WARN_ON_ONCE(ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed));
638da915ad5SPaul E. McKenney 		srcu_gp_start(sp);
639da915ad5SPaul E. McKenney 		queue_delayed_work(system_power_efficient_wq, &sp->work,
6401e9a038bSPaul E. McKenney 				   srcu_get_delay(sp));
641da915ad5SPaul E. McKenney 	}
642da915ad5SPaul E. McKenney 	spin_unlock_irqrestore(&sp->gp_lock, flags);
643da915ad5SPaul E. McKenney }
644da915ad5SPaul E. McKenney 
645da915ad5SPaul E. McKenney /*
646dad81a20SPaul E. McKenney  * Wait until all readers counted by array index idx complete, but
647dad81a20SPaul E. McKenney  * loop an additional time if there is an expedited grace period pending.
648da915ad5SPaul E. McKenney  * The caller must ensure that ->srcu_idx is not changed while checking.
649dad81a20SPaul E. McKenney  */
650dad81a20SPaul E. McKenney static bool try_check_zero(struct srcu_struct *sp, int idx, int trycount)
651dad81a20SPaul E. McKenney {
652dad81a20SPaul E. McKenney 	for (;;) {
653dad81a20SPaul E. McKenney 		if (srcu_readers_active_idx_check(sp, idx))
654dad81a20SPaul E. McKenney 			return true;
6551e9a038bSPaul E. McKenney 		if (--trycount + !srcu_get_delay(sp) <= 0)
656dad81a20SPaul E. McKenney 			return false;
657dad81a20SPaul E. McKenney 		udelay(SRCU_RETRY_CHECK_DELAY);
658dad81a20SPaul E. McKenney 	}
659dad81a20SPaul E. McKenney }
660dad81a20SPaul E. McKenney 
661dad81a20SPaul E. McKenney /*
662da915ad5SPaul E. McKenney  * Increment the ->srcu_idx counter so that future SRCU readers will
663da915ad5SPaul E. McKenney  * use the other rank of the ->srcu_(un)lock_count[] arrays.  This allows
664dad81a20SPaul E. McKenney  * us to wait for pre-existing readers in a starvation-free manner.
665dad81a20SPaul E. McKenney  */
666dad81a20SPaul E. McKenney static void srcu_flip(struct srcu_struct *sp)
667dad81a20SPaul E. McKenney {
668da915ad5SPaul E. McKenney 	WRITE_ONCE(sp->srcu_idx, sp->srcu_idx + 1);
669dad81a20SPaul E. McKenney 
670dad81a20SPaul E. McKenney 	/*
671dad81a20SPaul E. McKenney 	 * Ensure that if the updater misses an __srcu_read_unlock()
672dad81a20SPaul E. McKenney 	 * increment, that task's next __srcu_read_lock() will see the
673dad81a20SPaul E. McKenney 	 * above counter update.  Note that both this memory barrier
674dad81a20SPaul E. McKenney 	 * and the one in srcu_readers_active_idx_check() provide the
675dad81a20SPaul E. McKenney 	 * guarantee for __srcu_read_lock().
676dad81a20SPaul E. McKenney 	 */
677dad81a20SPaul E. McKenney 	smp_mb(); /* D */  /* Pairs with C. */
678dad81a20SPaul E. McKenney }
679dad81a20SPaul E. McKenney 
680dad81a20SPaul E. McKenney /*
681*2da4b2a7SPaul E. McKenney  * If SRCU is likely idle, return true, otherwise return false.
682*2da4b2a7SPaul E. McKenney  *
683*2da4b2a7SPaul E. McKenney  * Note that it is OK for several current from-idle requests for a new
684*2da4b2a7SPaul E. McKenney  * grace period from idle to specify expediting because they will all end
685*2da4b2a7SPaul E. McKenney  * up requesting the same grace period anyhow.  So no loss.
686*2da4b2a7SPaul E. McKenney  *
687*2da4b2a7SPaul E. McKenney  * Note also that if any CPU (including the current one) is still invoking
688*2da4b2a7SPaul E. McKenney  * callbacks, this function will nevertheless say "idle".  This is not
689*2da4b2a7SPaul E. McKenney  * ideal, but the overhead of checking all CPUs' callback lists is even
690*2da4b2a7SPaul E. McKenney  * less ideal, especially on large systems.  Furthermore, the wakeup
691*2da4b2a7SPaul E. McKenney  * can happen before the callback is fully removed, so we have no choice
692*2da4b2a7SPaul E. McKenney  * but to accept this type of error.
693*2da4b2a7SPaul E. McKenney  *
694*2da4b2a7SPaul E. McKenney  * This function is also subject to counter-wrap errors, but let's face
695*2da4b2a7SPaul E. McKenney  * it, if this function was preempted for enough time for the counters
696*2da4b2a7SPaul E. McKenney  * to wrap, it really doesn't matter whether or not we expedite the grace
697*2da4b2a7SPaul E. McKenney  * period.  The extra overhead of a needlessly expedited grace period is
698*2da4b2a7SPaul E. McKenney  * negligible when amoritized over that time period, and the extra latency
699*2da4b2a7SPaul E. McKenney  * of a needlessly non-expedited grace period is similarly negligible.
700*2da4b2a7SPaul E. McKenney  */
701*2da4b2a7SPaul E. McKenney static bool srcu_might_be_idle(struct srcu_struct *sp)
702*2da4b2a7SPaul E. McKenney {
703*2da4b2a7SPaul E. McKenney 	unsigned long flags;
704*2da4b2a7SPaul E. McKenney 	struct srcu_data *sdp;
705*2da4b2a7SPaul E. McKenney 	unsigned long curseq;
706*2da4b2a7SPaul E. McKenney 
707*2da4b2a7SPaul E. McKenney 	/* If the local srcu_data structure has callbacks, not idle.  */
708*2da4b2a7SPaul E. McKenney 	local_irq_save(flags);
709*2da4b2a7SPaul E. McKenney 	sdp = this_cpu_ptr(sp->sda);
710*2da4b2a7SPaul E. McKenney 	if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
711*2da4b2a7SPaul E. McKenney 		local_irq_restore(flags);
712*2da4b2a7SPaul E. McKenney 		return false; /* Callbacks already present, so not idle. */
713*2da4b2a7SPaul E. McKenney 	}
714*2da4b2a7SPaul E. McKenney 	local_irq_restore(flags);
715*2da4b2a7SPaul E. McKenney 
716*2da4b2a7SPaul E. McKenney 	/*
717*2da4b2a7SPaul E. McKenney 	 * No local callbacks, so probabalistically probe global state.
718*2da4b2a7SPaul E. McKenney 	 * Exact information would require acquiring locks, which would
719*2da4b2a7SPaul E. McKenney 	 * kill scalability, hence the probabalistic nature of the probe.
720*2da4b2a7SPaul E. McKenney 	 */
721*2da4b2a7SPaul E. McKenney 	curseq = rcu_seq_current(&sp->srcu_gp_seq);
722*2da4b2a7SPaul E. McKenney 	smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
723*2da4b2a7SPaul E. McKenney 	if (ULONG_CMP_LT(curseq, READ_ONCE(sp->srcu_gp_seq_needed)))
724*2da4b2a7SPaul E. McKenney 		return false; /* Grace period in progress, so not idle. */
725*2da4b2a7SPaul E. McKenney 	smp_mb(); /* Order ->srcu_gp_seq with prior access. */
726*2da4b2a7SPaul E. McKenney 	if (curseq != rcu_seq_current(&sp->srcu_gp_seq))
727*2da4b2a7SPaul E. McKenney 		return false; /* GP # changed, so not idle. */
728*2da4b2a7SPaul E. McKenney 	return true; /* With reasonable probability, idle! */
729*2da4b2a7SPaul E. McKenney }
730*2da4b2a7SPaul E. McKenney 
731*2da4b2a7SPaul E. McKenney /*
732da915ad5SPaul E. McKenney  * Enqueue an SRCU callback on the srcu_data structure associated with
733da915ad5SPaul E. McKenney  * the current CPU and the specified srcu_struct structure, initiating
734da915ad5SPaul E. McKenney  * grace-period processing if it is not already running.
735dad81a20SPaul E. McKenney  *
736dad81a20SPaul E. McKenney  * Note that all CPUs must agree that the grace period extended beyond
737dad81a20SPaul E. McKenney  * all pre-existing SRCU read-side critical section.  On systems with
738dad81a20SPaul E. McKenney  * more than one CPU, this means that when "func()" is invoked, each CPU
739dad81a20SPaul E. McKenney  * is guaranteed to have executed a full memory barrier since the end of
740dad81a20SPaul E. McKenney  * its last corresponding SRCU read-side critical section whose beginning
741dad81a20SPaul E. McKenney  * preceded the call to call_rcu().  It also means that each CPU executing
742dad81a20SPaul E. McKenney  * an SRCU read-side critical section that continues beyond the start of
743dad81a20SPaul E. McKenney  * "func()" must have executed a memory barrier after the call_rcu()
744dad81a20SPaul E. McKenney  * but before the beginning of that SRCU read-side critical section.
745dad81a20SPaul E. McKenney  * Note that these guarantees include CPUs that are offline, idle, or
746dad81a20SPaul E. McKenney  * executing in user mode, as well as CPUs that are executing in the kernel.
747dad81a20SPaul E. McKenney  *
748dad81a20SPaul E. McKenney  * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
749dad81a20SPaul E. McKenney  * resulting SRCU callback function "func()", then both CPU A and CPU
750dad81a20SPaul E. McKenney  * B are guaranteed to execute a full memory barrier during the time
751dad81a20SPaul E. McKenney  * interval between the call to call_rcu() and the invocation of "func()".
752dad81a20SPaul E. McKenney  * This guarantee applies even if CPU A and CPU B are the same CPU (but
753dad81a20SPaul E. McKenney  * again only if the system has more than one CPU).
754dad81a20SPaul E. McKenney  *
755dad81a20SPaul E. McKenney  * Of course, these guarantees apply only for invocations of call_srcu(),
756dad81a20SPaul E. McKenney  * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
757dad81a20SPaul E. McKenney  * srcu_struct structure.
758dad81a20SPaul E. McKenney  */
7591e9a038bSPaul E. McKenney void __call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
7601e9a038bSPaul E. McKenney 		 rcu_callback_t func, bool do_norm)
761dad81a20SPaul E. McKenney {
762dad81a20SPaul E. McKenney 	unsigned long flags;
7631e9a038bSPaul E. McKenney 	bool needexp = false;
764da915ad5SPaul E. McKenney 	bool needgp = false;
765da915ad5SPaul E. McKenney 	unsigned long s;
766da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
767dad81a20SPaul E. McKenney 
768da915ad5SPaul E. McKenney 	check_init_srcu_struct(sp);
769da915ad5SPaul E. McKenney 	rhp->func = func;
770da915ad5SPaul E. McKenney 	local_irq_save(flags);
771da915ad5SPaul E. McKenney 	sdp = this_cpu_ptr(sp->sda);
772da915ad5SPaul E. McKenney 	spin_lock(&sdp->lock);
773da915ad5SPaul E. McKenney 	rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp, false);
774da915ad5SPaul E. McKenney 	rcu_segcblist_advance(&sdp->srcu_cblist,
775da915ad5SPaul E. McKenney 			      rcu_seq_current(&sp->srcu_gp_seq));
776da915ad5SPaul E. McKenney 	s = rcu_seq_snap(&sp->srcu_gp_seq);
777da915ad5SPaul E. McKenney 	(void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s);
778da915ad5SPaul E. McKenney 	if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
779da915ad5SPaul E. McKenney 		sdp->srcu_gp_seq_needed = s;
780da915ad5SPaul E. McKenney 		needgp = true;
781dad81a20SPaul E. McKenney 	}
7821e9a038bSPaul E. McKenney 	if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
7831e9a038bSPaul E. McKenney 		sdp->srcu_gp_seq_needed_exp = s;
7841e9a038bSPaul E. McKenney 		needexp = true;
7851e9a038bSPaul E. McKenney 	}
786da915ad5SPaul E. McKenney 	spin_unlock_irqrestore(&sdp->lock, flags);
787da915ad5SPaul E. McKenney 	if (needgp)
7881e9a038bSPaul E. McKenney 		srcu_funnel_gp_start(sp, sdp, s, do_norm);
7891e9a038bSPaul E. McKenney 	else if (needexp)
7901e9a038bSPaul E. McKenney 		srcu_funnel_exp_start(sp, sdp->mynode, s);
7911e9a038bSPaul E. McKenney }
7921e9a038bSPaul E. McKenney 
7931e9a038bSPaul E. McKenney void call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
7941e9a038bSPaul E. McKenney 	       rcu_callback_t func)
7951e9a038bSPaul E. McKenney {
7961e9a038bSPaul E. McKenney 	__call_srcu(sp, rhp, func, true);
797dad81a20SPaul E. McKenney }
798dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(call_srcu);
799dad81a20SPaul E. McKenney 
800dad81a20SPaul E. McKenney /*
801dad81a20SPaul E. McKenney  * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
802dad81a20SPaul E. McKenney  */
8031e9a038bSPaul E. McKenney static void __synchronize_srcu(struct srcu_struct *sp, bool do_norm)
804dad81a20SPaul E. McKenney {
805dad81a20SPaul E. McKenney 	struct rcu_synchronize rcu;
806dad81a20SPaul E. McKenney 
807dad81a20SPaul E. McKenney 	RCU_LOCKDEP_WARN(lock_is_held(&sp->dep_map) ||
808dad81a20SPaul E. McKenney 			 lock_is_held(&rcu_bh_lock_map) ||
809dad81a20SPaul E. McKenney 			 lock_is_held(&rcu_lock_map) ||
810dad81a20SPaul E. McKenney 			 lock_is_held(&rcu_sched_lock_map),
811dad81a20SPaul E. McKenney 			 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
812dad81a20SPaul E. McKenney 
813dad81a20SPaul E. McKenney 	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
814dad81a20SPaul E. McKenney 		return;
815dad81a20SPaul E. McKenney 	might_sleep();
816da915ad5SPaul E. McKenney 	check_init_srcu_struct(sp);
817dad81a20SPaul E. McKenney 	init_completion(&rcu.completion);
818da915ad5SPaul E. McKenney 	init_rcu_head_on_stack(&rcu.head);
8191e9a038bSPaul E. McKenney 	__call_srcu(sp, &rcu.head, wakeme_after_rcu, do_norm);
820dad81a20SPaul E. McKenney 	wait_for_completion(&rcu.completion);
821da915ad5SPaul E. McKenney 	destroy_rcu_head_on_stack(&rcu.head);
822dad81a20SPaul E. McKenney }
823dad81a20SPaul E. McKenney 
824dad81a20SPaul E. McKenney /**
825dad81a20SPaul E. McKenney  * synchronize_srcu_expedited - Brute-force SRCU grace period
826dad81a20SPaul E. McKenney  * @sp: srcu_struct with which to synchronize.
827dad81a20SPaul E. McKenney  *
828dad81a20SPaul E. McKenney  * Wait for an SRCU grace period to elapse, but be more aggressive about
829dad81a20SPaul E. McKenney  * spinning rather than blocking when waiting.
830dad81a20SPaul E. McKenney  *
831dad81a20SPaul E. McKenney  * Note that synchronize_srcu_expedited() has the same deadlock and
832dad81a20SPaul E. McKenney  * memory-ordering properties as does synchronize_srcu().
833dad81a20SPaul E. McKenney  */
834dad81a20SPaul E. McKenney void synchronize_srcu_expedited(struct srcu_struct *sp)
835dad81a20SPaul E. McKenney {
8361e9a038bSPaul E. McKenney 	__synchronize_srcu(sp, rcu_gp_is_normal());
837dad81a20SPaul E. McKenney }
838dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
839dad81a20SPaul E. McKenney 
840dad81a20SPaul E. McKenney /**
841dad81a20SPaul E. McKenney  * synchronize_srcu - wait for prior SRCU read-side critical-section completion
842dad81a20SPaul E. McKenney  * @sp: srcu_struct with which to synchronize.
843dad81a20SPaul E. McKenney  *
844dad81a20SPaul E. McKenney  * Wait for the count to drain to zero of both indexes. To avoid the
845dad81a20SPaul E. McKenney  * possible starvation of synchronize_srcu(), it waits for the count of
846da915ad5SPaul E. McKenney  * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
847da915ad5SPaul E. McKenney  * and then flip the srcu_idx and wait for the count of the other index.
848dad81a20SPaul E. McKenney  *
849dad81a20SPaul E. McKenney  * Can block; must be called from process context.
850dad81a20SPaul E. McKenney  *
851dad81a20SPaul E. McKenney  * Note that it is illegal to call synchronize_srcu() from the corresponding
852dad81a20SPaul E. McKenney  * SRCU read-side critical section; doing so will result in deadlock.
853dad81a20SPaul E. McKenney  * However, it is perfectly legal to call synchronize_srcu() on one
854dad81a20SPaul E. McKenney  * srcu_struct from some other srcu_struct's read-side critical section,
855dad81a20SPaul E. McKenney  * as long as the resulting graph of srcu_structs is acyclic.
856dad81a20SPaul E. McKenney  *
857dad81a20SPaul E. McKenney  * There are memory-ordering constraints implied by synchronize_srcu().
858dad81a20SPaul E. McKenney  * On systems with more than one CPU, when synchronize_srcu() returns,
859dad81a20SPaul E. McKenney  * each CPU is guaranteed to have executed a full memory barrier since
860dad81a20SPaul E. McKenney  * the end of its last corresponding SRCU-sched read-side critical section
861dad81a20SPaul E. McKenney  * whose beginning preceded the call to synchronize_srcu().  In addition,
862dad81a20SPaul E. McKenney  * each CPU having an SRCU read-side critical section that extends beyond
863dad81a20SPaul E. McKenney  * the return from synchronize_srcu() is guaranteed to have executed a
864dad81a20SPaul E. McKenney  * full memory barrier after the beginning of synchronize_srcu() and before
865dad81a20SPaul E. McKenney  * the beginning of that SRCU read-side critical section.  Note that these
866dad81a20SPaul E. McKenney  * guarantees include CPUs that are offline, idle, or executing in user mode,
867dad81a20SPaul E. McKenney  * as well as CPUs that are executing in the kernel.
868dad81a20SPaul E. McKenney  *
869dad81a20SPaul E. McKenney  * Furthermore, if CPU A invoked synchronize_srcu(), which returned
870dad81a20SPaul E. McKenney  * to its caller on CPU B, then both CPU A and CPU B are guaranteed
871dad81a20SPaul E. McKenney  * to have executed a full memory barrier during the execution of
872dad81a20SPaul E. McKenney  * synchronize_srcu().  This guarantee applies even if CPU A and CPU B
873dad81a20SPaul E. McKenney  * are the same CPU, but again only if the system has more than one CPU.
874dad81a20SPaul E. McKenney  *
875dad81a20SPaul E. McKenney  * Of course, these memory-ordering guarantees apply only when
876dad81a20SPaul E. McKenney  * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
877dad81a20SPaul E. McKenney  * passed the same srcu_struct structure.
878*2da4b2a7SPaul E. McKenney  *
879*2da4b2a7SPaul E. McKenney  * If SRCU is likely idle, expedite the first request.  This semantic
880*2da4b2a7SPaul E. McKenney  * was provided by Classic SRCU, and is relied upon by its users, so TREE
881*2da4b2a7SPaul E. McKenney  * SRCU must also provide it.  Note that detecting idleness is heuristic
882*2da4b2a7SPaul E. McKenney  * and subject to both false positives and negatives.
883dad81a20SPaul E. McKenney  */
884dad81a20SPaul E. McKenney void synchronize_srcu(struct srcu_struct *sp)
885dad81a20SPaul E. McKenney {
886*2da4b2a7SPaul E. McKenney 	if (srcu_might_be_idle(sp) || rcu_gp_is_expedited())
887dad81a20SPaul E. McKenney 		synchronize_srcu_expedited(sp);
888dad81a20SPaul E. McKenney 	else
8891e9a038bSPaul E. McKenney 		__synchronize_srcu(sp, true);
890dad81a20SPaul E. McKenney }
891dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(synchronize_srcu);
892dad81a20SPaul E. McKenney 
893da915ad5SPaul E. McKenney /*
894da915ad5SPaul E. McKenney  * Callback function for srcu_barrier() use.
895da915ad5SPaul E. McKenney  */
896da915ad5SPaul E. McKenney static void srcu_barrier_cb(struct rcu_head *rhp)
897da915ad5SPaul E. McKenney {
898da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
899da915ad5SPaul E. McKenney 	struct srcu_struct *sp;
900da915ad5SPaul E. McKenney 
901da915ad5SPaul E. McKenney 	sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
902da915ad5SPaul E. McKenney 	sp = sdp->sp;
903da915ad5SPaul E. McKenney 	if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
904da915ad5SPaul E. McKenney 		complete(&sp->srcu_barrier_completion);
905da915ad5SPaul E. McKenney }
906da915ad5SPaul E. McKenney 
907dad81a20SPaul E. McKenney /**
908dad81a20SPaul E. McKenney  * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
909dad81a20SPaul E. McKenney  * @sp: srcu_struct on which to wait for in-flight callbacks.
910dad81a20SPaul E. McKenney  */
911dad81a20SPaul E. McKenney void srcu_barrier(struct srcu_struct *sp)
912dad81a20SPaul E. McKenney {
913da915ad5SPaul E. McKenney 	int cpu;
914da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
915da915ad5SPaul E. McKenney 	unsigned long s = rcu_seq_snap(&sp->srcu_barrier_seq);
916da915ad5SPaul E. McKenney 
917da915ad5SPaul E. McKenney 	check_init_srcu_struct(sp);
918da915ad5SPaul E. McKenney 	mutex_lock(&sp->srcu_barrier_mutex);
919da915ad5SPaul E. McKenney 	if (rcu_seq_done(&sp->srcu_barrier_seq, s)) {
920da915ad5SPaul E. McKenney 		smp_mb(); /* Force ordering following return. */
921da915ad5SPaul E. McKenney 		mutex_unlock(&sp->srcu_barrier_mutex);
922da915ad5SPaul E. McKenney 		return; /* Someone else did our work for us. */
923da915ad5SPaul E. McKenney 	}
924da915ad5SPaul E. McKenney 	rcu_seq_start(&sp->srcu_barrier_seq);
925da915ad5SPaul E. McKenney 	init_completion(&sp->srcu_barrier_completion);
926da915ad5SPaul E. McKenney 
927da915ad5SPaul E. McKenney 	/* Initial count prevents reaching zero until all CBs are posted. */
928da915ad5SPaul E. McKenney 	atomic_set(&sp->srcu_barrier_cpu_cnt, 1);
929da915ad5SPaul E. McKenney 
930da915ad5SPaul E. McKenney 	/*
931da915ad5SPaul E. McKenney 	 * Each pass through this loop enqueues a callback, but only
932da915ad5SPaul E. McKenney 	 * on CPUs already having callbacks enqueued.  Note that if
933da915ad5SPaul E. McKenney 	 * a CPU already has callbacks enqueue, it must have already
934da915ad5SPaul E. McKenney 	 * registered the need for a future grace period, so all we
935da915ad5SPaul E. McKenney 	 * need do is enqueue a callback that will use the same
936da915ad5SPaul E. McKenney 	 * grace period as the last callback already in the queue.
937da915ad5SPaul E. McKenney 	 */
938da915ad5SPaul E. McKenney 	for_each_possible_cpu(cpu) {
939da915ad5SPaul E. McKenney 		sdp = per_cpu_ptr(sp->sda, cpu);
940da915ad5SPaul E. McKenney 		spin_lock_irq(&sdp->lock);
941da915ad5SPaul E. McKenney 		atomic_inc(&sp->srcu_barrier_cpu_cnt);
942da915ad5SPaul E. McKenney 		sdp->srcu_barrier_head.func = srcu_barrier_cb;
943da915ad5SPaul E. McKenney 		if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
944da915ad5SPaul E. McKenney 					   &sdp->srcu_barrier_head, 0))
945da915ad5SPaul E. McKenney 			atomic_dec(&sp->srcu_barrier_cpu_cnt);
946da915ad5SPaul E. McKenney 		spin_unlock_irq(&sdp->lock);
947da915ad5SPaul E. McKenney 	}
948da915ad5SPaul E. McKenney 
949da915ad5SPaul E. McKenney 	/* Remove the initial count, at which point reaching zero can happen. */
950da915ad5SPaul E. McKenney 	if (atomic_dec_and_test(&sp->srcu_barrier_cpu_cnt))
951da915ad5SPaul E. McKenney 		complete(&sp->srcu_barrier_completion);
952da915ad5SPaul E. McKenney 	wait_for_completion(&sp->srcu_barrier_completion);
953da915ad5SPaul E. McKenney 
954da915ad5SPaul E. McKenney 	rcu_seq_end(&sp->srcu_barrier_seq);
955da915ad5SPaul E. McKenney 	mutex_unlock(&sp->srcu_barrier_mutex);
956dad81a20SPaul E. McKenney }
957dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_barrier);
958dad81a20SPaul E. McKenney 
959dad81a20SPaul E. McKenney /**
960dad81a20SPaul E. McKenney  * srcu_batches_completed - return batches completed.
961dad81a20SPaul E. McKenney  * @sp: srcu_struct on which to report batch completion.
962dad81a20SPaul E. McKenney  *
963dad81a20SPaul E. McKenney  * Report the number of batches, correlated with, but not necessarily
964dad81a20SPaul E. McKenney  * precisely the same as, the number of grace periods that have elapsed.
965dad81a20SPaul E. McKenney  */
966dad81a20SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *sp)
967dad81a20SPaul E. McKenney {
968da915ad5SPaul E. McKenney 	return sp->srcu_idx;
969dad81a20SPaul E. McKenney }
970dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_batches_completed);
971dad81a20SPaul E. McKenney 
972dad81a20SPaul E. McKenney /*
973da915ad5SPaul E. McKenney  * Core SRCU state machine.  Push state bits of ->srcu_gp_seq
974da915ad5SPaul E. McKenney  * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
975da915ad5SPaul E. McKenney  * completed in that state.
976dad81a20SPaul E. McKenney  */
977da915ad5SPaul E. McKenney static void srcu_advance_state(struct srcu_struct *sp)
978dad81a20SPaul E. McKenney {
979dad81a20SPaul E. McKenney 	int idx;
980dad81a20SPaul E. McKenney 
981da915ad5SPaul E. McKenney 	mutex_lock(&sp->srcu_gp_mutex);
982da915ad5SPaul E. McKenney 
983dad81a20SPaul E. McKenney 	/*
984dad81a20SPaul E. McKenney 	 * Because readers might be delayed for an extended period after
985da915ad5SPaul E. McKenney 	 * fetching ->srcu_idx for their index, at any point in time there
986dad81a20SPaul E. McKenney 	 * might well be readers using both idx=0 and idx=1.  We therefore
987dad81a20SPaul E. McKenney 	 * need to wait for readers to clear from both index values before
988dad81a20SPaul E. McKenney 	 * invoking a callback.
989dad81a20SPaul E. McKenney 	 *
990dad81a20SPaul E. McKenney 	 * The load-acquire ensures that we see the accesses performed
991dad81a20SPaul E. McKenney 	 * by the prior grace period.
992dad81a20SPaul E. McKenney 	 */
993dad81a20SPaul E. McKenney 	idx = rcu_seq_state(smp_load_acquire(&sp->srcu_gp_seq)); /* ^^^ */
994dad81a20SPaul E. McKenney 	if (idx == SRCU_STATE_IDLE) {
995da915ad5SPaul E. McKenney 		spin_lock_irq(&sp->gp_lock);
996da915ad5SPaul E. McKenney 		if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
997da915ad5SPaul E. McKenney 			WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq));
998da915ad5SPaul E. McKenney 			spin_unlock_irq(&sp->gp_lock);
999da915ad5SPaul E. McKenney 			mutex_unlock(&sp->srcu_gp_mutex);
1000dad81a20SPaul E. McKenney 			return;
1001dad81a20SPaul E. McKenney 		}
1002dad81a20SPaul E. McKenney 		idx = rcu_seq_state(READ_ONCE(sp->srcu_gp_seq));
1003dad81a20SPaul E. McKenney 		if (idx == SRCU_STATE_IDLE)
1004dad81a20SPaul E. McKenney 			srcu_gp_start(sp);
1005da915ad5SPaul E. McKenney 		spin_unlock_irq(&sp->gp_lock);
1006da915ad5SPaul E. McKenney 		if (idx != SRCU_STATE_IDLE) {
1007da915ad5SPaul E. McKenney 			mutex_unlock(&sp->srcu_gp_mutex);
1008dad81a20SPaul E. McKenney 			return; /* Someone else started the grace period. */
1009dad81a20SPaul E. McKenney 		}
1010da915ad5SPaul E. McKenney 	}
1011dad81a20SPaul E. McKenney 
1012dad81a20SPaul E. McKenney 	if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
1013da915ad5SPaul E. McKenney 		idx = 1 ^ (sp->srcu_idx & 1);
1014da915ad5SPaul E. McKenney 		if (!try_check_zero(sp, idx, 1)) {
1015da915ad5SPaul E. McKenney 			mutex_unlock(&sp->srcu_gp_mutex);
1016dad81a20SPaul E. McKenney 			return; /* readers present, retry later. */
1017da915ad5SPaul E. McKenney 		}
1018dad81a20SPaul E. McKenney 		srcu_flip(sp);
1019dad81a20SPaul E. McKenney 		rcu_seq_set_state(&sp->srcu_gp_seq, SRCU_STATE_SCAN2);
1020dad81a20SPaul E. McKenney 	}
1021dad81a20SPaul E. McKenney 
1022dad81a20SPaul E. McKenney 	if (rcu_seq_state(READ_ONCE(sp->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
1023dad81a20SPaul E. McKenney 
1024dad81a20SPaul E. McKenney 		/*
1025dad81a20SPaul E. McKenney 		 * SRCU read-side critical sections are normally short,
1026dad81a20SPaul E. McKenney 		 * so check at least twice in quick succession after a flip.
1027dad81a20SPaul E. McKenney 		 */
1028da915ad5SPaul E. McKenney 		idx = 1 ^ (sp->srcu_idx & 1);
1029da915ad5SPaul E. McKenney 		if (!try_check_zero(sp, idx, 2)) {
1030da915ad5SPaul E. McKenney 			mutex_unlock(&sp->srcu_gp_mutex);
1031da915ad5SPaul E. McKenney 			return; /* readers present, retry later. */
1032da915ad5SPaul E. McKenney 		}
1033da915ad5SPaul E. McKenney 		srcu_gp_end(sp);  /* Releases ->srcu_gp_mutex. */
1034dad81a20SPaul E. McKenney 	}
1035dad81a20SPaul E. McKenney }
1036dad81a20SPaul E. McKenney 
1037dad81a20SPaul E. McKenney /*
1038dad81a20SPaul E. McKenney  * Invoke a limited number of SRCU callbacks that have passed through
1039dad81a20SPaul E. McKenney  * their grace period.  If there are more to do, SRCU will reschedule
1040dad81a20SPaul E. McKenney  * the workqueue.  Note that needed memory barriers have been executed
1041dad81a20SPaul E. McKenney  * in this task's context by srcu_readers_active_idx_check().
1042dad81a20SPaul E. McKenney  */
1043da915ad5SPaul E. McKenney static void srcu_invoke_callbacks(struct work_struct *work)
1044dad81a20SPaul E. McKenney {
1045da915ad5SPaul E. McKenney 	bool more;
1046dad81a20SPaul E. McKenney 	struct rcu_cblist ready_cbs;
1047dad81a20SPaul E. McKenney 	struct rcu_head *rhp;
1048da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
1049da915ad5SPaul E. McKenney 	struct srcu_struct *sp;
1050dad81a20SPaul E. McKenney 
1051da915ad5SPaul E. McKenney 	sdp = container_of(work, struct srcu_data, work.work);
1052da915ad5SPaul E. McKenney 	sp = sdp->sp;
1053dad81a20SPaul E. McKenney 	rcu_cblist_init(&ready_cbs);
1054da915ad5SPaul E. McKenney 	spin_lock_irq(&sdp->lock);
1055da915ad5SPaul E. McKenney 	smp_mb(); /* Old grace periods before callback invocation! */
1056da915ad5SPaul E. McKenney 	rcu_segcblist_advance(&sdp->srcu_cblist,
1057da915ad5SPaul E. McKenney 			      rcu_seq_current(&sp->srcu_gp_seq));
1058da915ad5SPaul E. McKenney 	if (sdp->srcu_cblist_invoking ||
1059da915ad5SPaul E. McKenney 	    !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
1060da915ad5SPaul E. McKenney 		spin_unlock_irq(&sdp->lock);
1061da915ad5SPaul E. McKenney 		return;  /* Someone else on the job or nothing to do. */
1062da915ad5SPaul E. McKenney 	}
1063da915ad5SPaul E. McKenney 
1064da915ad5SPaul E. McKenney 	/* We are on the job!  Extract and invoke ready callbacks. */
1065da915ad5SPaul E. McKenney 	sdp->srcu_cblist_invoking = true;
1066da915ad5SPaul E. McKenney 	rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
1067da915ad5SPaul E. McKenney 	spin_unlock_irq(&sdp->lock);
1068dad81a20SPaul E. McKenney 	rhp = rcu_cblist_dequeue(&ready_cbs);
1069dad81a20SPaul E. McKenney 	for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
1070dad81a20SPaul E. McKenney 		local_bh_disable();
1071dad81a20SPaul E. McKenney 		rhp->func(rhp);
1072dad81a20SPaul E. McKenney 		local_bh_enable();
1073dad81a20SPaul E. McKenney 	}
1074da915ad5SPaul E. McKenney 
1075da915ad5SPaul E. McKenney 	/*
1076da915ad5SPaul E. McKenney 	 * Update counts, accelerate new callbacks, and if needed,
1077da915ad5SPaul E. McKenney 	 * schedule another round of callback invocation.
1078da915ad5SPaul E. McKenney 	 */
1079da915ad5SPaul E. McKenney 	spin_lock_irq(&sdp->lock);
1080da915ad5SPaul E. McKenney 	rcu_segcblist_insert_count(&sdp->srcu_cblist, &ready_cbs);
1081da915ad5SPaul E. McKenney 	(void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
1082da915ad5SPaul E. McKenney 				       rcu_seq_snap(&sp->srcu_gp_seq));
1083da915ad5SPaul E. McKenney 	sdp->srcu_cblist_invoking = false;
1084da915ad5SPaul E. McKenney 	more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
1085da915ad5SPaul E. McKenney 	spin_unlock_irq(&sdp->lock);
1086da915ad5SPaul E. McKenney 	if (more)
1087da915ad5SPaul E. McKenney 		srcu_schedule_cbs_sdp(sdp, 0);
1088dad81a20SPaul E. McKenney }
1089dad81a20SPaul E. McKenney 
1090dad81a20SPaul E. McKenney /*
1091dad81a20SPaul E. McKenney  * Finished one round of SRCU grace period.  Start another if there are
1092dad81a20SPaul E. McKenney  * more SRCU callbacks queued, otherwise put SRCU into not-running state.
1093dad81a20SPaul E. McKenney  */
1094dad81a20SPaul E. McKenney static void srcu_reschedule(struct srcu_struct *sp, unsigned long delay)
1095dad81a20SPaul E. McKenney {
1096da915ad5SPaul E. McKenney 	bool pushgp = true;
1097dad81a20SPaul E. McKenney 
1098da915ad5SPaul E. McKenney 	spin_lock_irq(&sp->gp_lock);
1099da915ad5SPaul E. McKenney 	if (ULONG_CMP_GE(sp->srcu_gp_seq, sp->srcu_gp_seq_needed)) {
1100da915ad5SPaul E. McKenney 		if (!WARN_ON_ONCE(rcu_seq_state(sp->srcu_gp_seq))) {
1101da915ad5SPaul E. McKenney 			/* All requests fulfilled, time to go idle. */
1102da915ad5SPaul E. McKenney 			pushgp = false;
1103dad81a20SPaul E. McKenney 		}
1104da915ad5SPaul E. McKenney 	} else if (!rcu_seq_state(sp->srcu_gp_seq)) {
1105da915ad5SPaul E. McKenney 		/* Outstanding request and no GP.  Start one. */
1106da915ad5SPaul E. McKenney 		srcu_gp_start(sp);
1107da915ad5SPaul E. McKenney 	}
1108da915ad5SPaul E. McKenney 	spin_unlock_irq(&sp->gp_lock);
1109dad81a20SPaul E. McKenney 
1110da915ad5SPaul E. McKenney 	if (pushgp)
1111dad81a20SPaul E. McKenney 		queue_delayed_work(system_power_efficient_wq, &sp->work, delay);
1112dad81a20SPaul E. McKenney }
1113dad81a20SPaul E. McKenney 
1114dad81a20SPaul E. McKenney /*
1115dad81a20SPaul E. McKenney  * This is the work-queue function that handles SRCU grace periods.
1116dad81a20SPaul E. McKenney  */
1117dad81a20SPaul E. McKenney void process_srcu(struct work_struct *work)
1118dad81a20SPaul E. McKenney {
1119dad81a20SPaul E. McKenney 	struct srcu_struct *sp;
1120dad81a20SPaul E. McKenney 
1121dad81a20SPaul E. McKenney 	sp = container_of(work, struct srcu_struct, work.work);
1122dad81a20SPaul E. McKenney 
1123da915ad5SPaul E. McKenney 	srcu_advance_state(sp);
11241e9a038bSPaul E. McKenney 	srcu_reschedule(sp, srcu_get_delay(sp));
1125dad81a20SPaul E. McKenney }
1126dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(process_srcu);
11277f6733c3SPaul E. McKenney 
11287f6733c3SPaul E. McKenney void srcutorture_get_gp_data(enum rcutorture_type test_type,
11297f6733c3SPaul E. McKenney 			     struct srcu_struct *sp, int *flags,
11301e9a038bSPaul E. McKenney 			     unsigned long *gpnum, unsigned long *completed)
11317f6733c3SPaul E. McKenney {
11327f6733c3SPaul E. McKenney 	if (test_type != SRCU_FLAVOR)
11337f6733c3SPaul E. McKenney 		return;
11347f6733c3SPaul E. McKenney 	*flags = 0;
11357f6733c3SPaul E. McKenney 	*completed = rcu_seq_ctr(sp->srcu_gp_seq);
11367f6733c3SPaul E. McKenney 	*gpnum = rcu_seq_ctr(sp->srcu_gp_seq_needed);
11377f6733c3SPaul E. McKenney }
11387f6733c3SPaul E. McKenney EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
1139