xref: /linux/kernel/rcu/srcutree.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1e7ee1501SPaul E. McKenney // SPDX-License-Identifier: GPL-2.0+
2dad81a20SPaul E. McKenney /*
3dad81a20SPaul E. McKenney  * Sleepable Read-Copy Update mechanism for mutual exclusion.
4dad81a20SPaul E. McKenney  *
5dad81a20SPaul E. McKenney  * Copyright (C) IBM Corporation, 2006
6dad81a20SPaul E. McKenney  * Copyright (C) Fujitsu, 2012
7dad81a20SPaul E. McKenney  *
865bb0dc4SSeongJae Park  * Authors: Paul McKenney <paulmck@linux.ibm.com>
9dad81a20SPaul E. McKenney  *	   Lai Jiangshan <laijs@cn.fujitsu.com>
10dad81a20SPaul E. McKenney  *
11dad81a20SPaul E. McKenney  * For detailed explanation of Read-Copy Update mechanism see -
12dad81a20SPaul E. McKenney  *		Documentation/RCU/ *.txt
13dad81a20SPaul E. McKenney  *
14dad81a20SPaul E. McKenney  */
15dad81a20SPaul E. McKenney 
16a7538352SJoe Perches #define pr_fmt(fmt) "rcu: " fmt
17a7538352SJoe Perches 
18dad81a20SPaul E. McKenney #include <linux/export.h>
19dad81a20SPaul E. McKenney #include <linux/mutex.h>
20dad81a20SPaul E. McKenney #include <linux/percpu.h>
21dad81a20SPaul E. McKenney #include <linux/preempt.h>
22dad81a20SPaul E. McKenney #include <linux/rcupdate_wait.h>
23dad81a20SPaul E. McKenney #include <linux/sched.h>
24dad81a20SPaul E. McKenney #include <linux/smp.h>
25dad81a20SPaul E. McKenney #include <linux/delay.h>
2622607d66SPaul E. McKenney #include <linux/module.h>
272ec30311SPaul E. McKenney #include <linux/slab.h>
28dad81a20SPaul E. McKenney #include <linux/srcu.h>
29dad81a20SPaul E. McKenney 
30dad81a20SPaul E. McKenney #include "rcu.h"
3145753c5fSIngo Molnar #include "rcu_segcblist.h"
32dad81a20SPaul E. McKenney 
330c8e0e3cSPaul E. McKenney /* Holdoff in nanoseconds for auto-expediting. */
340c8e0e3cSPaul E. McKenney #define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000)
350c8e0e3cSPaul E. McKenney static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF;
3622607d66SPaul E. McKenney module_param(exp_holdoff, ulong, 0444);
3722607d66SPaul E. McKenney 
38c350c008SPaul E. McKenney /* Overflow-check frequency.  N bits roughly says every 2**N grace periods. */
39c350c008SPaul E. McKenney static ulong counter_wrap_check = (ULONG_MAX >> 2);
40c350c008SPaul E. McKenney module_param(counter_wrap_check, ulong, 0444);
41c350c008SPaul E. McKenney 
42c69a00a1SPaul E. McKenney /*
43c69a00a1SPaul E. McKenney  * Control conversion to SRCU_SIZE_BIG:
44a57ffb3cSPaul E. McKenney  *    0: Don't convert at all.
45c69a00a1SPaul E. McKenney  *    1: Convert at init_srcu_struct() time.
46c69a00a1SPaul E. McKenney  *    2: Convert when rcutorture invokes srcu_torture_stats_print().
47a57ffb3cSPaul E. McKenney  *    3: Decide at boot time based on system shape (default).
489f2e91d9SPaul E. McKenney  * 0x1x: Convert when excessive contention encountered.
49c69a00a1SPaul E. McKenney  */
509f2e91d9SPaul E. McKenney #define SRCU_SIZING_NONE	0
519f2e91d9SPaul E. McKenney #define SRCU_SIZING_INIT	1
529f2e91d9SPaul E. McKenney #define SRCU_SIZING_TORTURE	2
539f2e91d9SPaul E. McKenney #define SRCU_SIZING_AUTO	3
549f2e91d9SPaul E. McKenney #define SRCU_SIZING_CONTEND	0x10
559f2e91d9SPaul E. McKenney #define SRCU_SIZING_IS(x) ((convert_to_big & ~SRCU_SIZING_CONTEND) == x)
569f2e91d9SPaul E. McKenney #define SRCU_SIZING_IS_NONE() (SRCU_SIZING_IS(SRCU_SIZING_NONE))
579f2e91d9SPaul E. McKenney #define SRCU_SIZING_IS_INIT() (SRCU_SIZING_IS(SRCU_SIZING_INIT))
589f2e91d9SPaul E. McKenney #define SRCU_SIZING_IS_TORTURE() (SRCU_SIZING_IS(SRCU_SIZING_TORTURE))
599f2e91d9SPaul E. McKenney #define SRCU_SIZING_IS_CONTEND() (convert_to_big & SRCU_SIZING_CONTEND)
60a57ffb3cSPaul E. McKenney static int convert_to_big = SRCU_SIZING_AUTO;
61c69a00a1SPaul E. McKenney module_param(convert_to_big, int, 0444);
62c69a00a1SPaul E. McKenney 
63a57ffb3cSPaul E. McKenney /* Number of CPUs to trigger init_srcu_struct()-time transition to big. */
64a57ffb3cSPaul E. McKenney static int big_cpu_lim __read_mostly = 128;
65a57ffb3cSPaul E. McKenney module_param(big_cpu_lim, int, 0444);
66a57ffb3cSPaul E. McKenney 
679f2e91d9SPaul E. McKenney /* Contention events per jiffy to initiate transition to big. */
689f2e91d9SPaul E. McKenney static int small_contention_lim __read_mostly = 100;
699f2e91d9SPaul E. McKenney module_param(small_contention_lim, int, 0444);
709f2e91d9SPaul E. McKenney 
71e0fcba9aSPaul E. McKenney /* Early-boot callback-management, so early that no lock is required! */
72e0fcba9aSPaul E. McKenney static LIST_HEAD(srcu_boot_list);
73e0fcba9aSPaul E. McKenney static bool __read_mostly srcu_init_done;
74e0fcba9aSPaul E. McKenney 
75da915ad5SPaul E. McKenney static void srcu_invoke_callbacks(struct work_struct *work);
76aacb5d91SPaul E. McKenney static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay);
770d8a1e83SPaul E. McKenney static void process_srcu(struct work_struct *work);
78e81baf4cSSebastian Andrzej Siewior static void srcu_delay_timer(struct timer_list *t);
79da915ad5SPaul E. McKenney 
80d6331980SPaul E. McKenney /* Wrappers for lock acquisition and release, see raw_spin_lock_rcu_node(). */
81d6331980SPaul E. McKenney #define spin_lock_rcu_node(p)							\
82d6331980SPaul E. McKenney do {										\
83d6331980SPaul E. McKenney 	spin_lock(&ACCESS_PRIVATE(p, lock));					\
84d6331980SPaul E. McKenney 	smp_mb__after_unlock_lock();						\
85d6331980SPaul E. McKenney } while (0)
86d6331980SPaul E. McKenney 
87d6331980SPaul E. McKenney #define spin_unlock_rcu_node(p) spin_unlock(&ACCESS_PRIVATE(p, lock))
88d6331980SPaul E. McKenney 
89d6331980SPaul E. McKenney #define spin_lock_irq_rcu_node(p)						\
90d6331980SPaul E. McKenney do {										\
91d6331980SPaul E. McKenney 	spin_lock_irq(&ACCESS_PRIVATE(p, lock));				\
92d6331980SPaul E. McKenney 	smp_mb__after_unlock_lock();						\
93d6331980SPaul E. McKenney } while (0)
94d6331980SPaul E. McKenney 
95d6331980SPaul E. McKenney #define spin_unlock_irq_rcu_node(p)						\
96d6331980SPaul E. McKenney 	spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
97d6331980SPaul E. McKenney 
98d6331980SPaul E. McKenney #define spin_lock_irqsave_rcu_node(p, flags)					\
99d6331980SPaul E. McKenney do {										\
100d6331980SPaul E. McKenney 	spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags);			\
101d6331980SPaul E. McKenney 	smp_mb__after_unlock_lock();						\
102d6331980SPaul E. McKenney } while (0)
103d6331980SPaul E. McKenney 
1049f2e91d9SPaul E. McKenney #define spin_trylock_irqsave_rcu_node(p, flags)					\
1059f2e91d9SPaul E. McKenney ({										\
1069f2e91d9SPaul E. McKenney 	bool ___locked = spin_trylock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
1079f2e91d9SPaul E. McKenney 										\
1089f2e91d9SPaul E. McKenney 	if (___locked)								\
1099f2e91d9SPaul E. McKenney 		smp_mb__after_unlock_lock();					\
1109f2e91d9SPaul E. McKenney 	___locked;								\
1119f2e91d9SPaul E. McKenney })
1129f2e91d9SPaul E. McKenney 
113d6331980SPaul E. McKenney #define spin_unlock_irqrestore_rcu_node(p, flags)				\
114d6331980SPaul E. McKenney 	spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags)			\
115d6331980SPaul E. McKenney 
116da915ad5SPaul E. McKenney /*
1172ec30311SPaul E. McKenney  * Initialize SRCU per-CPU data.  Note that statically allocated
118da915ad5SPaul E. McKenney  * srcu_struct structures might already have srcu_read_lock() and
119da915ad5SPaul E. McKenney  * srcu_read_unlock() running against them.  So if the is_static parameter
120da915ad5SPaul E. McKenney  * is set, don't initialize ->srcu_lock_count[] and ->srcu_unlock_count[].
121da915ad5SPaul E. McKenney  */
init_srcu_struct_data(struct srcu_struct * ssp)1222ec30311SPaul E. McKenney static void init_srcu_struct_data(struct srcu_struct *ssp)
1232ec30311SPaul E. McKenney {
1242ec30311SPaul E. McKenney 	int cpu;
1252ec30311SPaul E. McKenney 	struct srcu_data *sdp;
1262ec30311SPaul E. McKenney 
1272ec30311SPaul E. McKenney 	/*
1282ec30311SPaul E. McKenney 	 * Initialize the per-CPU srcu_data array, which feeds into the
1292ec30311SPaul E. McKenney 	 * leaves of the srcu_node tree.
1302ec30311SPaul E. McKenney 	 */
1312ec30311SPaul E. McKenney 	WARN_ON_ONCE(ARRAY_SIZE(sdp->srcu_lock_count) !=
1322ec30311SPaul E. McKenney 		     ARRAY_SIZE(sdp->srcu_unlock_count));
1332ec30311SPaul E. McKenney 	for_each_possible_cpu(cpu) {
1342ec30311SPaul E. McKenney 		sdp = per_cpu_ptr(ssp->sda, cpu);
1352ec30311SPaul E. McKenney 		spin_lock_init(&ACCESS_PRIVATE(sdp, lock));
1362ec30311SPaul E. McKenney 		rcu_segcblist_init(&sdp->srcu_cblist);
1372ec30311SPaul E. McKenney 		sdp->srcu_cblist_invoking = false;
13803200b5cSPaul E. McKenney 		sdp->srcu_gp_seq_needed = ssp->srcu_sup->srcu_gp_seq;
13903200b5cSPaul E. McKenney 		sdp->srcu_gp_seq_needed_exp = ssp->srcu_sup->srcu_gp_seq;
140*e53cef03SPaul E. McKenney 		sdp->srcu_barrier_head.next = &sdp->srcu_barrier_head;
1412ec30311SPaul E. McKenney 		sdp->mynode = NULL;
1422ec30311SPaul E. McKenney 		sdp->cpu = cpu;
1432ec30311SPaul E. McKenney 		INIT_WORK(&sdp->work, srcu_invoke_callbacks);
1442ec30311SPaul E. McKenney 		timer_setup(&sdp->delay_work, srcu_delay_timer, 0);
1452ec30311SPaul E. McKenney 		sdp->ssp = ssp;
1462ec30311SPaul E. McKenney 	}
1472ec30311SPaul E. McKenney }
1482ec30311SPaul E. McKenney 
149cbdc98e9SPaul E. McKenney /* Invalid seq state, used during snp node initialization */
150cbdc98e9SPaul E. McKenney #define SRCU_SNP_INIT_SEQ		0x2
151cbdc98e9SPaul E. McKenney 
152cbdc98e9SPaul E. McKenney /*
153cbdc98e9SPaul E. McKenney  * Check whether sequence number corresponding to snp node,
154cbdc98e9SPaul E. McKenney  * is invalid.
155cbdc98e9SPaul E. McKenney  */
srcu_invl_snp_seq(unsigned long s)156cbdc98e9SPaul E. McKenney static inline bool srcu_invl_snp_seq(unsigned long s)
157cbdc98e9SPaul E. McKenney {
15850be0c04SPingfan Liu 	return s == SRCU_SNP_INIT_SEQ;
159cbdc98e9SPaul E. McKenney }
160cbdc98e9SPaul E. McKenney 
1612ec30311SPaul E. McKenney /*
1622ec30311SPaul E. McKenney  * Allocated and initialize SRCU combining tree.  Returns @true if
1632ec30311SPaul E. McKenney  * allocation succeeded and @false otherwise.
1642ec30311SPaul E. McKenney  */
init_srcu_struct_nodes(struct srcu_struct * ssp,gfp_t gfp_flags)165c69a00a1SPaul E. McKenney static bool init_srcu_struct_nodes(struct srcu_struct *ssp, gfp_t gfp_flags)
166dad81a20SPaul E. McKenney {
167da915ad5SPaul E. McKenney 	int cpu;
168da915ad5SPaul E. McKenney 	int i;
169da915ad5SPaul E. McKenney 	int level = 0;
170da915ad5SPaul E. McKenney 	int levelspread[RCU_NUM_LVLS];
171da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
172da915ad5SPaul E. McKenney 	struct srcu_node *snp;
173da915ad5SPaul E. McKenney 	struct srcu_node *snp_first;
174da915ad5SPaul E. McKenney 
175b5befe84SFrederic Weisbecker 	/* Initialize geometry if it has not already been initialized. */
176b5befe84SFrederic Weisbecker 	rcu_init_geometry();
17795433f72SPaul E. McKenney 	ssp->srcu_sup->node = kcalloc(rcu_num_nodes, sizeof(*ssp->srcu_sup->node), gfp_flags);
17895433f72SPaul E. McKenney 	if (!ssp->srcu_sup->node)
1792ec30311SPaul E. McKenney 		return false;
180b5befe84SFrederic Weisbecker 
181da915ad5SPaul E. McKenney 	/* Work out the overall tree geometry. */
182208f41b1SPaul E. McKenney 	ssp->srcu_sup->level[0] = &ssp->srcu_sup->node[0];
183da915ad5SPaul E. McKenney 	for (i = 1; i < rcu_num_lvls; i++)
184208f41b1SPaul E. McKenney 		ssp->srcu_sup->level[i] = ssp->srcu_sup->level[i - 1] + num_rcu_lvl[i - 1];
185da915ad5SPaul E. McKenney 	rcu_init_levelspread(levelspread, num_rcu_lvl);
186da915ad5SPaul E. McKenney 
187da915ad5SPaul E. McKenney 	/* Each pass through this loop initializes one srcu_node structure. */
188aacb5d91SPaul E. McKenney 	srcu_for_each_node_breadth_first(ssp, snp) {
189d6331980SPaul E. McKenney 		spin_lock_init(&ACCESS_PRIVATE(snp, lock));
190c7e88067SPaul E. McKenney 		WARN_ON_ONCE(ARRAY_SIZE(snp->srcu_have_cbs) !=
191c7e88067SPaul E. McKenney 			     ARRAY_SIZE(snp->srcu_data_have_cbs));
192c7e88067SPaul E. McKenney 		for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {
193cbdc98e9SPaul E. McKenney 			snp->srcu_have_cbs[i] = SRCU_SNP_INIT_SEQ;
194c7e88067SPaul E. McKenney 			snp->srcu_data_have_cbs[i] = 0;
195c7e88067SPaul E. McKenney 		}
196cbdc98e9SPaul E. McKenney 		snp->srcu_gp_seq_needed_exp = SRCU_SNP_INIT_SEQ;
197da915ad5SPaul E. McKenney 		snp->grplo = -1;
198da915ad5SPaul E. McKenney 		snp->grphi = -1;
19995433f72SPaul E. McKenney 		if (snp == &ssp->srcu_sup->node[0]) {
200da915ad5SPaul E. McKenney 			/* Root node, special case. */
201da915ad5SPaul E. McKenney 			snp->srcu_parent = NULL;
202da915ad5SPaul E. McKenney 			continue;
203da915ad5SPaul E. McKenney 		}
204da915ad5SPaul E. McKenney 
205da915ad5SPaul E. McKenney 		/* Non-root node. */
206208f41b1SPaul E. McKenney 		if (snp == ssp->srcu_sup->level[level + 1])
207da915ad5SPaul E. McKenney 			level++;
208208f41b1SPaul E. McKenney 		snp->srcu_parent = ssp->srcu_sup->level[level - 1] +
209208f41b1SPaul E. McKenney 				   (snp - ssp->srcu_sup->level[level]) /
210da915ad5SPaul E. McKenney 				   levelspread[level - 1];
211da915ad5SPaul E. McKenney 	}
212da915ad5SPaul E. McKenney 
213da915ad5SPaul E. McKenney 	/*
214da915ad5SPaul E. McKenney 	 * Initialize the per-CPU srcu_data array, which feeds into the
215da915ad5SPaul E. McKenney 	 * leaves of the srcu_node tree.
216da915ad5SPaul E. McKenney 	 */
217da915ad5SPaul E. McKenney 	level = rcu_num_lvls - 1;
218208f41b1SPaul E. McKenney 	snp_first = ssp->srcu_sup->level[level];
219da915ad5SPaul E. McKenney 	for_each_possible_cpu(cpu) {
220aacb5d91SPaul E. McKenney 		sdp = per_cpu_ptr(ssp->sda, cpu);
221da915ad5SPaul E. McKenney 		sdp->mynode = &snp_first[cpu / levelspread[level]];
222da915ad5SPaul E. McKenney 		for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {
223da915ad5SPaul E. McKenney 			if (snp->grplo < 0)
224da915ad5SPaul E. McKenney 				snp->grplo = cpu;
225da915ad5SPaul E. McKenney 			snp->grphi = cpu;
226da915ad5SPaul E. McKenney 		}
227d8d5b7bfSDenis Arefev 		sdp->grpmask = 1UL << (cpu - sdp->mynode->grplo);
228da915ad5SPaul E. McKenney 	}
229a0d8cbd3SPaul E. McKenney 	smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_WAIT_BARRIER);
2302ec30311SPaul E. McKenney 	return true;
231da915ad5SPaul E. McKenney }
232da915ad5SPaul E. McKenney 
233da915ad5SPaul E. McKenney /*
234da915ad5SPaul E. McKenney  * Initialize non-compile-time initialized fields, including the
235994f7068SPaul E. McKenney  * associated srcu_node and srcu_data structures.  The is_static parameter
236994f7068SPaul E. McKenney  * tells us that ->sda has already been wired up to srcu_data.
237da915ad5SPaul E. McKenney  */
init_srcu_struct_fields(struct srcu_struct * ssp,bool is_static)238aacb5d91SPaul E. McKenney static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static)
239da915ad5SPaul E. McKenney {
24095433f72SPaul E. McKenney 	if (!is_static)
24195433f72SPaul E. McKenney 		ssp->srcu_sup = kzalloc(sizeof(*ssp->srcu_sup), GFP_KERNEL);
24295433f72SPaul E. McKenney 	if (!ssp->srcu_sup)
24395433f72SPaul E. McKenney 		return -ENOMEM;
2440839ade9SPaul E. McKenney 	if (!is_static)
245b3fb11f7SPaul E. McKenney 		spin_lock_init(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
246a0d8cbd3SPaul E. McKenney 	ssp->srcu_sup->srcu_size_state = SRCU_SIZE_SMALL;
24795433f72SPaul E. McKenney 	ssp->srcu_sup->node = NULL;
248574dc1a7SPaul E. McKenney 	mutex_init(&ssp->srcu_sup->srcu_cb_mutex);
249e3a6ab25SPaul E. McKenney 	mutex_init(&ssp->srcu_sup->srcu_gp_mutex);
250aacb5d91SPaul E. McKenney 	ssp->srcu_idx = 0;
25129bc83e4SJP Kobryn 	ssp->srcu_sup->srcu_gp_seq = SRCU_GP_SEQ_INITIAL_VAL;
252d20162e0SPaul E. McKenney 	ssp->srcu_sup->srcu_barrier_seq = 0;
253d20162e0SPaul E. McKenney 	mutex_init(&ssp->srcu_sup->srcu_barrier_mutex);
254d20162e0SPaul E. McKenney 	atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 0);
255fd1b3f8eSPaul E. McKenney 	INIT_DELAYED_WORK(&ssp->srcu_sup->work, process_srcu);
256660349acSPaul E. McKenney 	ssp->srcu_sup->sda_is_static = is_static;
257da915ad5SPaul E. McKenney 	if (!is_static)
258aacb5d91SPaul E. McKenney 		ssp->sda = alloc_percpu(struct srcu_data);
259f0a31b26SJoel Fernandes (Google) 	if (!ssp->sda)
260f0a31b26SJoel Fernandes (Google) 		goto err_free_sup;
2612ec30311SPaul E. McKenney 	init_srcu_struct_data(ssp);
26229bc83e4SJP Kobryn 	ssp->srcu_sup->srcu_gp_seq_needed_exp = SRCU_GP_SEQ_INITIAL_VAL;
26303200b5cSPaul E. McKenney 	ssp->srcu_sup->srcu_last_gp_end = ktime_get_mono_fast_ns();
264a0d8cbd3SPaul E. McKenney 	if (READ_ONCE(ssp->srcu_sup->srcu_size_state) == SRCU_SIZE_SMALL && SRCU_SIZING_IS_INIT()) {
265f0a31b26SJoel Fernandes (Google) 		if (!init_srcu_struct_nodes(ssp, GFP_ATOMIC))
266f0a31b26SJoel Fernandes (Google) 			goto err_free_sda;
267a0d8cbd3SPaul E. McKenney 		WRITE_ONCE(ssp->srcu_sup->srcu_size_state, SRCU_SIZE_BIG);
2682ec30311SPaul E. McKenney 	}
269fd1b3f8eSPaul E. McKenney 	ssp->srcu_sup->srcu_ssp = ssp;
27029bc83e4SJP Kobryn 	smp_store_release(&ssp->srcu_sup->srcu_gp_seq_needed,
27129bc83e4SJP Kobryn 			SRCU_GP_SEQ_INITIAL_VAL); /* Init done. */
27250edb988SPaul E. McKenney 	return 0;
273f0a31b26SJoel Fernandes (Google) 
274f0a31b26SJoel Fernandes (Google) err_free_sda:
275f0a31b26SJoel Fernandes (Google) 	if (!is_static) {
276f0a31b26SJoel Fernandes (Google) 		free_percpu(ssp->sda);
277f0a31b26SJoel Fernandes (Google) 		ssp->sda = NULL;
278f0a31b26SJoel Fernandes (Google) 	}
279f0a31b26SJoel Fernandes (Google) err_free_sup:
280f0a31b26SJoel Fernandes (Google) 	if (!is_static) {
281f0a31b26SJoel Fernandes (Google) 		kfree(ssp->srcu_sup);
282f0a31b26SJoel Fernandes (Google) 		ssp->srcu_sup = NULL;
283f0a31b26SJoel Fernandes (Google) 	}
284f0a31b26SJoel Fernandes (Google) 	return -ENOMEM;
285dad81a20SPaul E. McKenney }
286dad81a20SPaul E. McKenney 
287dad81a20SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
288dad81a20SPaul E. McKenney 
__init_srcu_struct(struct srcu_struct * ssp,const char * name,struct lock_class_key * key)289aacb5d91SPaul E. McKenney int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
290dad81a20SPaul E. McKenney 		       struct lock_class_key *key)
291dad81a20SPaul E. McKenney {
292dad81a20SPaul E. McKenney 	/* Don't re-initialize a lock while it is held. */
293aacb5d91SPaul E. McKenney 	debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));
294aacb5d91SPaul E. McKenney 	lockdep_init_map(&ssp->dep_map, name, key, 0);
295aacb5d91SPaul E. McKenney 	return init_srcu_struct_fields(ssp, false);
296dad81a20SPaul E. McKenney }
297dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__init_srcu_struct);
298dad81a20SPaul E. McKenney 
299dad81a20SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
300dad81a20SPaul E. McKenney 
301dad81a20SPaul E. McKenney /**
302dad81a20SPaul E. McKenney  * init_srcu_struct - initialize a sleep-RCU structure
303aacb5d91SPaul E. McKenney  * @ssp: structure to initialize.
304dad81a20SPaul E. McKenney  *
305dad81a20SPaul E. McKenney  * Must invoke this on a given srcu_struct before passing that srcu_struct
306dad81a20SPaul E. McKenney  * to any other function.  Each srcu_struct represents a separate domain
307dad81a20SPaul E. McKenney  * of SRCU protection.
308dad81a20SPaul E. McKenney  */
init_srcu_struct(struct srcu_struct * ssp)309aacb5d91SPaul E. McKenney int init_srcu_struct(struct srcu_struct *ssp)
310dad81a20SPaul E. McKenney {
311aacb5d91SPaul E. McKenney 	return init_srcu_struct_fields(ssp, false);
312dad81a20SPaul E. McKenney }
313dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(init_srcu_struct);
314dad81a20SPaul E. McKenney 
315dad81a20SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
316dad81a20SPaul E. McKenney 
317dad81a20SPaul E. McKenney /*
3189f2e91d9SPaul E. McKenney  * Initiate a transition to SRCU_SIZE_BIG with lock held.
3199f2e91d9SPaul E. McKenney  */
__srcu_transition_to_big(struct srcu_struct * ssp)3209f2e91d9SPaul E. McKenney static void __srcu_transition_to_big(struct srcu_struct *ssp)
3219f2e91d9SPaul E. McKenney {
322b3fb11f7SPaul E. McKenney 	lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
323a0d8cbd3SPaul E. McKenney 	smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_ALLOC);
3249f2e91d9SPaul E. McKenney }
3259f2e91d9SPaul E. McKenney 
3269f2e91d9SPaul E. McKenney /*
32799659f64SPaul E. McKenney  * Initiate an idempotent transition to SRCU_SIZE_BIG.
32899659f64SPaul E. McKenney  */
srcu_transition_to_big(struct srcu_struct * ssp)32999659f64SPaul E. McKenney static void srcu_transition_to_big(struct srcu_struct *ssp)
33099659f64SPaul E. McKenney {
33199659f64SPaul E. McKenney 	unsigned long flags;
33299659f64SPaul E. McKenney 
33399659f64SPaul E. McKenney 	/* Double-checked locking on ->srcu_size-state. */
334a0d8cbd3SPaul E. McKenney 	if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) != SRCU_SIZE_SMALL)
33599659f64SPaul E. McKenney 		return;
336b3fb11f7SPaul E. McKenney 	spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags);
337a0d8cbd3SPaul E. McKenney 	if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) != SRCU_SIZE_SMALL) {
338b3fb11f7SPaul E. McKenney 		spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
33999659f64SPaul E. McKenney 		return;
34099659f64SPaul E. McKenney 	}
3419f2e91d9SPaul E. McKenney 	__srcu_transition_to_big(ssp);
342b3fb11f7SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
34399659f64SPaul E. McKenney }
34499659f64SPaul E. McKenney 
34599659f64SPaul E. McKenney /*
346c2445d38SPaul E. McKenney  * Check to see if the just-encountered contention event justifies
347c2445d38SPaul E. McKenney  * a transition to SRCU_SIZE_BIG.
3489f2e91d9SPaul E. McKenney  */
spin_lock_irqsave_check_contention(struct srcu_struct * ssp)349c2445d38SPaul E. McKenney static void spin_lock_irqsave_check_contention(struct srcu_struct *ssp)
3509f2e91d9SPaul E. McKenney {
3519f2e91d9SPaul E. McKenney 	unsigned long j;
3529f2e91d9SPaul E. McKenney 
353a0d8cbd3SPaul E. McKenney 	if (!SRCU_SIZING_IS_CONTEND() || ssp->srcu_sup->srcu_size_state)
3549f2e91d9SPaul E. McKenney 		return;
3559f2e91d9SPaul E. McKenney 	j = jiffies;
3563b46679cSPaul E. McKenney 	if (ssp->srcu_sup->srcu_size_jiffies != j) {
3573b46679cSPaul E. McKenney 		ssp->srcu_sup->srcu_size_jiffies = j;
3583b46679cSPaul E. McKenney 		ssp->srcu_sup->srcu_n_lock_retries = 0;
3599f2e91d9SPaul E. McKenney 	}
3603b46679cSPaul E. McKenney 	if (++ssp->srcu_sup->srcu_n_lock_retries <= small_contention_lim)
3619f2e91d9SPaul E. McKenney 		return;
3629f2e91d9SPaul E. McKenney 	__srcu_transition_to_big(ssp);
3639f2e91d9SPaul E. McKenney }
3649f2e91d9SPaul E. McKenney 
3659f2e91d9SPaul E. McKenney /*
366c2445d38SPaul E. McKenney  * Acquire the specified srcu_data structure's ->lock, but check for
367c2445d38SPaul E. McKenney  * excessive contention, which results in initiation of a transition
368c2445d38SPaul E. McKenney  * to SRCU_SIZE_BIG.  But only if the srcutree.convert_to_big module
369c2445d38SPaul E. McKenney  * parameter permits this.
370c2445d38SPaul E. McKenney  */
spin_lock_irqsave_sdp_contention(struct srcu_data * sdp,unsigned long * flags)371c2445d38SPaul E. McKenney static void spin_lock_irqsave_sdp_contention(struct srcu_data *sdp, unsigned long *flags)
372c2445d38SPaul E. McKenney {
373c2445d38SPaul E. McKenney 	struct srcu_struct *ssp = sdp->ssp;
374c2445d38SPaul E. McKenney 
375c2445d38SPaul E. McKenney 	if (spin_trylock_irqsave_rcu_node(sdp, *flags))
376c2445d38SPaul E. McKenney 		return;
377b3fb11f7SPaul E. McKenney 	spin_lock_irqsave_rcu_node(ssp->srcu_sup, *flags);
378c2445d38SPaul E. McKenney 	spin_lock_irqsave_check_contention(ssp);
379b3fb11f7SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, *flags);
380c2445d38SPaul E. McKenney 	spin_lock_irqsave_rcu_node(sdp, *flags);
381c2445d38SPaul E. McKenney }
382c2445d38SPaul E. McKenney 
383c2445d38SPaul E. McKenney /*
384c2445d38SPaul E. McKenney  * Acquire the specified srcu_struct structure's ->lock, but check for
385c2445d38SPaul E. McKenney  * excessive contention, which results in initiation of a transition
386c2445d38SPaul E. McKenney  * to SRCU_SIZE_BIG.  But only if the srcutree.convert_to_big module
387c2445d38SPaul E. McKenney  * parameter permits this.
388c2445d38SPaul E. McKenney  */
spin_lock_irqsave_ssp_contention(struct srcu_struct * ssp,unsigned long * flags)389c2445d38SPaul E. McKenney static void spin_lock_irqsave_ssp_contention(struct srcu_struct *ssp, unsigned long *flags)
390c2445d38SPaul E. McKenney {
391b3fb11f7SPaul E. McKenney 	if (spin_trylock_irqsave_rcu_node(ssp->srcu_sup, *flags))
392c2445d38SPaul E. McKenney 		return;
393b3fb11f7SPaul E. McKenney 	spin_lock_irqsave_rcu_node(ssp->srcu_sup, *flags);
394c2445d38SPaul E. McKenney 	spin_lock_irqsave_check_contention(ssp);
395c2445d38SPaul E. McKenney }
396c2445d38SPaul E. McKenney 
397c2445d38SPaul E. McKenney /*
398da915ad5SPaul E. McKenney  * First-use initialization of statically allocated srcu_struct
399da915ad5SPaul E. McKenney  * structure.  Wiring up the combining tree is more than can be
400da915ad5SPaul E. McKenney  * done with compile-time initialization, so this check is added
401aacb5d91SPaul E. McKenney  * to each update-side SRCU primitive.  Use ssp->lock, which -is-
402da915ad5SPaul E. McKenney  * compile-time initialized, to resolve races involving multiple
403da915ad5SPaul E. McKenney  * CPUs trying to garner first-use privileges.
404da915ad5SPaul E. McKenney  */
check_init_srcu_struct(struct srcu_struct * ssp)405aacb5d91SPaul E. McKenney static void check_init_srcu_struct(struct srcu_struct *ssp)
406da915ad5SPaul E. McKenney {
407da915ad5SPaul E. McKenney 	unsigned long flags;
408da915ad5SPaul E. McKenney 
409da915ad5SPaul E. McKenney 	/* The smp_load_acquire() pairs with the smp_store_release(). */
41003200b5cSPaul E. McKenney 	if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed))) /*^^^*/
411da915ad5SPaul E. McKenney 		return; /* Already initialized. */
412b3fb11f7SPaul E. McKenney 	spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags);
41303200b5cSPaul E. McKenney 	if (!rcu_seq_state(ssp->srcu_sup->srcu_gp_seq_needed)) {
414b3fb11f7SPaul E. McKenney 		spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
415da915ad5SPaul E. McKenney 		return;
416da915ad5SPaul E. McKenney 	}
417aacb5d91SPaul E. McKenney 	init_srcu_struct_fields(ssp, true);
418b3fb11f7SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
419da915ad5SPaul E. McKenney }
420da915ad5SPaul E. McKenney 
421da915ad5SPaul E. McKenney /*
422da915ad5SPaul E. McKenney  * Returns approximate total of the readers' ->srcu_lock_count[] values
423da915ad5SPaul E. McKenney  * for the rank of per-CPU counters specified by idx.
424dad81a20SPaul E. McKenney  */
srcu_readers_lock_idx(struct srcu_struct * ssp,int idx)425aacb5d91SPaul E. McKenney static unsigned long srcu_readers_lock_idx(struct srcu_struct *ssp, int idx)
426dad81a20SPaul E. McKenney {
427dad81a20SPaul E. McKenney 	int cpu;
428dad81a20SPaul E. McKenney 	unsigned long sum = 0;
429dad81a20SPaul E. McKenney 
430dad81a20SPaul E. McKenney 	for_each_possible_cpu(cpu) {
431aacb5d91SPaul E. McKenney 		struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
432dad81a20SPaul E. McKenney 
4335d0f5953SPaul E. McKenney 		sum += atomic_long_read(&cpuc->srcu_lock_count[idx]);
434dad81a20SPaul E. McKenney 	}
435dad81a20SPaul E. McKenney 	return sum;
436dad81a20SPaul E. McKenney }
437dad81a20SPaul E. McKenney 
438dad81a20SPaul E. McKenney /*
439da915ad5SPaul E. McKenney  * Returns approximate total of the readers' ->srcu_unlock_count[] values
440da915ad5SPaul E. McKenney  * for the rank of per-CPU counters specified by idx.
441dad81a20SPaul E. McKenney  */
srcu_readers_unlock_idx(struct srcu_struct * ssp,int idx)442aacb5d91SPaul E. McKenney static unsigned long srcu_readers_unlock_idx(struct srcu_struct *ssp, int idx)
443dad81a20SPaul E. McKenney {
444dad81a20SPaul E. McKenney 	int cpu;
44536f65f1dSPaul E. McKenney 	unsigned long mask = 0;
446dad81a20SPaul E. McKenney 	unsigned long sum = 0;
447dad81a20SPaul E. McKenney 
448dad81a20SPaul E. McKenney 	for_each_possible_cpu(cpu) {
449aacb5d91SPaul E. McKenney 		struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
450dad81a20SPaul E. McKenney 
4515d0f5953SPaul E. McKenney 		sum += atomic_long_read(&cpuc->srcu_unlock_count[idx]);
45236f65f1dSPaul E. McKenney 		if (IS_ENABLED(CONFIG_PROVE_RCU))
45336f65f1dSPaul E. McKenney 			mask = mask | READ_ONCE(cpuc->srcu_nmi_safety);
454dad81a20SPaul E. McKenney 	}
45536f65f1dSPaul E. McKenney 	WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && (mask & (mask >> 1)),
45636f65f1dSPaul E. McKenney 		  "Mixed NMI-safe readers for srcu_struct at %ps.\n", ssp);
457dad81a20SPaul E. McKenney 	return sum;
458dad81a20SPaul E. McKenney }
459dad81a20SPaul E. McKenney 
460dad81a20SPaul E. McKenney /*
461dad81a20SPaul E. McKenney  * Return true if the number of pre-existing readers is determined to
462dad81a20SPaul E. McKenney  * be zero.
463dad81a20SPaul E. McKenney  */
srcu_readers_active_idx_check(struct srcu_struct * ssp,int idx)464aacb5d91SPaul E. McKenney static bool srcu_readers_active_idx_check(struct srcu_struct *ssp, int idx)
465dad81a20SPaul E. McKenney {
466dad81a20SPaul E. McKenney 	unsigned long unlocks;
467dad81a20SPaul E. McKenney 
468aacb5d91SPaul E. McKenney 	unlocks = srcu_readers_unlock_idx(ssp, idx);
469dad81a20SPaul E. McKenney 
470dad81a20SPaul E. McKenney 	/*
471dad81a20SPaul E. McKenney 	 * Make sure that a lock is always counted if the corresponding
472dad81a20SPaul E. McKenney 	 * unlock is counted. Needs to be a smp_mb() as the read side may
473dad81a20SPaul E. McKenney 	 * contain a read from a variable that is written to before the
474dad81a20SPaul E. McKenney 	 * synchronize_srcu() in the write side. In this case smp_mb()s
475dad81a20SPaul E. McKenney 	 * A and B act like the store buffering pattern.
476dad81a20SPaul E. McKenney 	 *
477dad81a20SPaul E. McKenney 	 * This smp_mb() also pairs with smp_mb() C to prevent accesses
478dad81a20SPaul E. McKenney 	 * after the synchronize_srcu() from being executed before the
479dad81a20SPaul E. McKenney 	 * grace period ends.
480dad81a20SPaul E. McKenney 	 */
481dad81a20SPaul E. McKenney 	smp_mb(); /* A */
482dad81a20SPaul E. McKenney 
483dad81a20SPaul E. McKenney 	/*
484dad81a20SPaul E. McKenney 	 * If the locks are the same as the unlocks, then there must have
4850cd4b50bSPaul E. McKenney 	 * been no readers on this index at some point in this function.
4860cd4b50bSPaul E. McKenney 	 * But there might be more readers, as a task might have read
4870cd4b50bSPaul E. McKenney 	 * the current ->srcu_idx but not yet have incremented its CPU's
4880cd4b50bSPaul E. McKenney 	 * ->srcu_lock_count[idx] counter.  In fact, it is possible
4890cd4b50bSPaul E. McKenney 	 * that most of the tasks have been preempted between fetching
4900cd4b50bSPaul E. McKenney 	 * ->srcu_idx and incrementing ->srcu_lock_count[idx].  And there
4910cd4b50bSPaul E. McKenney 	 * could be almost (ULONG_MAX / sizeof(struct task_struct)) tasks
4920cd4b50bSPaul E. McKenney 	 * in a system whose address space was fully populated with memory.
4930cd4b50bSPaul E. McKenney 	 * Call this quantity Nt.
494dad81a20SPaul E. McKenney 	 *
4950cd4b50bSPaul E. McKenney 	 * So suppose that the updater is preempted at this point in the
4960cd4b50bSPaul E. McKenney 	 * code for a long time.  That now-preempted updater has already
4970cd4b50bSPaul E. McKenney 	 * flipped ->srcu_idx (possibly during the preceding grace period),
4980cd4b50bSPaul E. McKenney 	 * done an smp_mb() (again, possibly during the preceding grace
4990cd4b50bSPaul E. McKenney 	 * period), and summed up the ->srcu_unlock_count[idx] counters.
5000cd4b50bSPaul E. McKenney 	 * How many times can a given one of the aforementioned Nt tasks
5010cd4b50bSPaul E. McKenney 	 * increment the old ->srcu_idx value's ->srcu_lock_count[idx]
5020cd4b50bSPaul E. McKenney 	 * counter, in the absence of nesting?
503881ec9d2SPaul E. McKenney 	 *
5040cd4b50bSPaul E. McKenney 	 * It can clearly do so once, given that it has already fetched
5050cd4b50bSPaul E. McKenney 	 * the old value of ->srcu_idx and is just about to use that value
5060cd4b50bSPaul E. McKenney 	 * to index its increment of ->srcu_lock_count[idx].  But as soon as
5070cd4b50bSPaul E. McKenney 	 * it leaves that SRCU read-side critical section, it will increment
5080cd4b50bSPaul E. McKenney 	 * ->srcu_unlock_count[idx], which must follow the updater's above
5090cd4b50bSPaul E. McKenney 	 * read from that same value.  Thus, as soon the reading task does
5100cd4b50bSPaul E. McKenney 	 * an smp_mb() and a later fetch from ->srcu_idx, that task will be
5110cd4b50bSPaul E. McKenney 	 * guaranteed to get the new index.  Except that the increment of
5120cd4b50bSPaul E. McKenney 	 * ->srcu_unlock_count[idx] in __srcu_read_unlock() is after the
5130cd4b50bSPaul E. McKenney 	 * smp_mb(), and the fetch from ->srcu_idx in __srcu_read_lock()
5140cd4b50bSPaul E. McKenney 	 * is before the smp_mb().  Thus, that task might not see the new
5150cd4b50bSPaul E. McKenney 	 * value of ->srcu_idx until the -second- __srcu_read_lock(),
5160cd4b50bSPaul E. McKenney 	 * which in turn means that this task might well increment
5170cd4b50bSPaul E. McKenney 	 * ->srcu_lock_count[idx] for the old value of ->srcu_idx twice,
5180cd4b50bSPaul E. McKenney 	 * not just once.
5190cd4b50bSPaul E. McKenney 	 *
5200cd4b50bSPaul E. McKenney 	 * However, it is important to note that a given smp_mb() takes
5210cd4b50bSPaul E. McKenney 	 * effect not just for the task executing it, but also for any
5220cd4b50bSPaul E. McKenney 	 * later task running on that same CPU.
5230cd4b50bSPaul E. McKenney 	 *
5240cd4b50bSPaul E. McKenney 	 * That is, there can be almost Nt + Nc further increments of
5250cd4b50bSPaul E. McKenney 	 * ->srcu_lock_count[idx] for the old index, where Nc is the number
5260cd4b50bSPaul E. McKenney 	 * of CPUs.  But this is OK because the size of the task_struct
5270cd4b50bSPaul E. McKenney 	 * structure limits the value of Nt and current systems limit Nc
5280cd4b50bSPaul E. McKenney 	 * to a few thousand.
5290cd4b50bSPaul E. McKenney 	 *
5300cd4b50bSPaul E. McKenney 	 * OK, but what about nesting?  This does impose a limit on
5310cd4b50bSPaul E. McKenney 	 * nesting of half of the size of the task_struct structure
5320cd4b50bSPaul E. McKenney 	 * (measured in bytes), which should be sufficient.  A late 2022
5330cd4b50bSPaul E. McKenney 	 * TREE01 rcutorture run reported this size to be no less than
5340cd4b50bSPaul E. McKenney 	 * 9408 bytes, allowing up to 4704 levels of nesting, which is
5350cd4b50bSPaul E. McKenney 	 * comfortably beyond excessive.  Especially on 64-bit systems,
5360cd4b50bSPaul E. McKenney 	 * which are unlikely to be configured with an address space fully
5370cd4b50bSPaul E. McKenney 	 * populated with memory, at least not anytime soon.
538dad81a20SPaul E. McKenney 	 */
539aacb5d91SPaul E. McKenney 	return srcu_readers_lock_idx(ssp, idx) == unlocks;
540dad81a20SPaul E. McKenney }
541dad81a20SPaul E. McKenney 
542dad81a20SPaul E. McKenney /**
543dad81a20SPaul E. McKenney  * srcu_readers_active - returns true if there are readers. and false
544dad81a20SPaul E. McKenney  *                       otherwise
545aacb5d91SPaul E. McKenney  * @ssp: which srcu_struct to count active readers (holding srcu_read_lock).
546dad81a20SPaul E. McKenney  *
547dad81a20SPaul E. McKenney  * Note that this is not an atomic primitive, and can therefore suffer
548dad81a20SPaul E. McKenney  * severe errors when invoked on an active srcu_struct.  That said, it
549dad81a20SPaul E. McKenney  * can be useful as an error check at cleanup time.
550dad81a20SPaul E. McKenney  */
srcu_readers_active(struct srcu_struct * ssp)551aacb5d91SPaul E. McKenney static bool srcu_readers_active(struct srcu_struct *ssp)
552dad81a20SPaul E. McKenney {
553dad81a20SPaul E. McKenney 	int cpu;
554dad81a20SPaul E. McKenney 	unsigned long sum = 0;
555dad81a20SPaul E. McKenney 
556dad81a20SPaul E. McKenney 	for_each_possible_cpu(cpu) {
557aacb5d91SPaul E. McKenney 		struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
558dad81a20SPaul E. McKenney 
5595d0f5953SPaul E. McKenney 		sum += atomic_long_read(&cpuc->srcu_lock_count[0]);
5605d0f5953SPaul E. McKenney 		sum += atomic_long_read(&cpuc->srcu_lock_count[1]);
5615d0f5953SPaul E. McKenney 		sum -= atomic_long_read(&cpuc->srcu_unlock_count[0]);
5625d0f5953SPaul E. McKenney 		sum -= atomic_long_read(&cpuc->srcu_unlock_count[1]);
563dad81a20SPaul E. McKenney 	}
564dad81a20SPaul E. McKenney 	return sum;
565dad81a20SPaul E. McKenney }
566dad81a20SPaul E. McKenney 
5674f2bfd94SNeeraj Upadhyay /*
5684f2bfd94SNeeraj Upadhyay  * We use an adaptive strategy for synchronize_srcu() and especially for
5694f2bfd94SNeeraj Upadhyay  * synchronize_srcu_expedited().  We spin for a fixed time period
5704f2bfd94SNeeraj Upadhyay  * (defined below, boot time configurable) to allow SRCU readers to exit
5714f2bfd94SNeeraj Upadhyay  * their read-side critical sections.  If there are still some readers
5724f2bfd94SNeeraj Upadhyay  * after one jiffy, we repeatedly block for one jiffy time periods.
5734f2bfd94SNeeraj Upadhyay  * The blocking time is increased as the grace-period age increases,
5744f2bfd94SNeeraj Upadhyay  * with max blocking time capped at 10 jiffies.
5754f2bfd94SNeeraj Upadhyay  */
5764f2bfd94SNeeraj Upadhyay #define SRCU_DEFAULT_RETRY_CHECK_DELAY		5
5774f2bfd94SNeeraj Upadhyay 
5784f2bfd94SNeeraj Upadhyay static ulong srcu_retry_check_delay = SRCU_DEFAULT_RETRY_CHECK_DELAY;
5794f2bfd94SNeeraj Upadhyay module_param(srcu_retry_check_delay, ulong, 0444);
5804f2bfd94SNeeraj Upadhyay 
581282d8998SPaul E. McKenney #define SRCU_INTERVAL		1		// Base delay if no expedited GPs pending.
582282d8998SPaul E. McKenney #define SRCU_MAX_INTERVAL	10		// Maximum incremental delay from slow readers.
5834f2bfd94SNeeraj Upadhyay 
5844f2bfd94SNeeraj Upadhyay #define SRCU_DEFAULT_MAX_NODELAY_PHASE_LO	3UL	// Lowmark on default per-GP-phase
5854f2bfd94SNeeraj Upadhyay 							// no-delay instances.
5864f2bfd94SNeeraj Upadhyay #define SRCU_DEFAULT_MAX_NODELAY_PHASE_HI	1000UL	// Highmark on default per-GP-phase
5874f2bfd94SNeeraj Upadhyay 							// no-delay instances.
5884f2bfd94SNeeraj Upadhyay 
5894f2bfd94SNeeraj Upadhyay #define SRCU_UL_CLAMP_LO(val, low)	((val) > (low) ? (val) : (low))
5904f2bfd94SNeeraj Upadhyay #define SRCU_UL_CLAMP_HI(val, high)	((val) < (high) ? (val) : (high))
5914f2bfd94SNeeraj Upadhyay #define SRCU_UL_CLAMP(val, low, high)	SRCU_UL_CLAMP_HI(SRCU_UL_CLAMP_LO((val), (low)), (high))
5924f2bfd94SNeeraj Upadhyay // per-GP-phase no-delay instances adjusted to allow non-sleeping poll upto
5934f2bfd94SNeeraj Upadhyay // one jiffies time duration. Mult by 2 is done to factor in the srcu_get_delay()
5944f2bfd94SNeeraj Upadhyay // called from process_srcu().
5954f2bfd94SNeeraj Upadhyay #define SRCU_DEFAULT_MAX_NODELAY_PHASE_ADJUSTED	\
5964f2bfd94SNeeraj Upadhyay 	(2UL * USEC_PER_SEC / HZ / SRCU_DEFAULT_RETRY_CHECK_DELAY)
5974f2bfd94SNeeraj Upadhyay 
5984f2bfd94SNeeraj Upadhyay // Maximum per-GP-phase consecutive no-delay instances.
5994f2bfd94SNeeraj Upadhyay #define SRCU_DEFAULT_MAX_NODELAY_PHASE	\
6004f2bfd94SNeeraj Upadhyay 	SRCU_UL_CLAMP(SRCU_DEFAULT_MAX_NODELAY_PHASE_ADJUSTED,	\
6014f2bfd94SNeeraj Upadhyay 		      SRCU_DEFAULT_MAX_NODELAY_PHASE_LO,	\
6024f2bfd94SNeeraj Upadhyay 		      SRCU_DEFAULT_MAX_NODELAY_PHASE_HI)
6034f2bfd94SNeeraj Upadhyay 
6044f2bfd94SNeeraj Upadhyay static ulong srcu_max_nodelay_phase = SRCU_DEFAULT_MAX_NODELAY_PHASE;
6054f2bfd94SNeeraj Upadhyay module_param(srcu_max_nodelay_phase, ulong, 0444);
6064f2bfd94SNeeraj Upadhyay 
6074f2bfd94SNeeraj Upadhyay // Maximum consecutive no-delay instances.
6084f2bfd94SNeeraj Upadhyay #define SRCU_DEFAULT_MAX_NODELAY	(SRCU_DEFAULT_MAX_NODELAY_PHASE > 100 ?	\
6094f2bfd94SNeeraj Upadhyay 					 SRCU_DEFAULT_MAX_NODELAY_PHASE : 100)
6104f2bfd94SNeeraj Upadhyay 
6114f2bfd94SNeeraj Upadhyay static ulong srcu_max_nodelay = SRCU_DEFAULT_MAX_NODELAY;
6124f2bfd94SNeeraj Upadhyay module_param(srcu_max_nodelay, ulong, 0444);
613dad81a20SPaul E. McKenney 
6141e9a038bSPaul E. McKenney /*
6151e9a038bSPaul E. McKenney  * Return grace-period delay, zero if there are expedited grace
6161e9a038bSPaul E. McKenney  * periods pending, SRCU_INTERVAL otherwise.
6171e9a038bSPaul E. McKenney  */
srcu_get_delay(struct srcu_struct * ssp)618aacb5d91SPaul E. McKenney static unsigned long srcu_get_delay(struct srcu_struct *ssp)
6191e9a038bSPaul E. McKenney {
6208f870e6eSPaul E. McKenney 	unsigned long gpstart;
6218f870e6eSPaul E. McKenney 	unsigned long j;
622282d8998SPaul E. McKenney 	unsigned long jbase = SRCU_INTERVAL;
623eabe7625SPaul E. McKenney 	struct srcu_usage *sup = ssp->srcu_sup;
624282d8998SPaul E. McKenney 
625eabe7625SPaul E. McKenney 	if (ULONG_CMP_LT(READ_ONCE(sup->srcu_gp_seq), READ_ONCE(sup->srcu_gp_seq_needed_exp)))
626282d8998SPaul E. McKenney 		jbase = 0;
627eabe7625SPaul E. McKenney 	if (rcu_seq_state(READ_ONCE(sup->srcu_gp_seq))) {
6288f870e6eSPaul E. McKenney 		j = jiffies - 1;
629eabe7625SPaul E. McKenney 		gpstart = READ_ONCE(sup->srcu_gp_start);
6308f870e6eSPaul E. McKenney 		if (time_after(j, gpstart))
6318f870e6eSPaul E. McKenney 			jbase += j - gpstart;
632282d8998SPaul E. McKenney 		if (!jbase) {
633c8c3ae83SPaul E. McKenney 			ASSERT_EXCLUSIVE_WRITER(sup->srcu_n_exp_nodelay);
634eabe7625SPaul E. McKenney 			WRITE_ONCE(sup->srcu_n_exp_nodelay, READ_ONCE(sup->srcu_n_exp_nodelay) + 1);
635eabe7625SPaul E. McKenney 			if (READ_ONCE(sup->srcu_n_exp_nodelay) > srcu_max_nodelay_phase)
636282d8998SPaul E. McKenney 				jbase = 1;
637282d8998SPaul E. McKenney 		}
6388f870e6eSPaul E. McKenney 	}
639282d8998SPaul E. McKenney 	return jbase > SRCU_MAX_INTERVAL ? SRCU_MAX_INTERVAL : jbase;
6401e9a038bSPaul E. McKenney }
6411e9a038bSPaul E. McKenney 
642f5ad3991SPaul E. McKenney /**
643f5ad3991SPaul E. McKenney  * cleanup_srcu_struct - deconstruct a sleep-RCU structure
644f5ad3991SPaul E. McKenney  * @ssp: structure to clean up.
645f5ad3991SPaul E. McKenney  *
646f5ad3991SPaul E. McKenney  * Must invoke this after you are finished using a given srcu_struct that
647f5ad3991SPaul E. McKenney  * was initialized via init_srcu_struct(), else you leak memory.
648f5ad3991SPaul E. McKenney  */
cleanup_srcu_struct(struct srcu_struct * ssp)649f5ad3991SPaul E. McKenney void cleanup_srcu_struct(struct srcu_struct *ssp)
650dad81a20SPaul E. McKenney {
651da915ad5SPaul E. McKenney 	int cpu;
6525ff8319fSPaul E. McKenney 	struct srcu_usage *sup = ssp->srcu_sup;
653da915ad5SPaul E. McKenney 
654aacb5d91SPaul E. McKenney 	if (WARN_ON(!srcu_get_delay(ssp)))
655f7194ac3SPaul E. McKenney 		return; /* Just leak it! */
656aacb5d91SPaul E. McKenney 	if (WARN_ON(srcu_readers_active(ssp)))
657f7194ac3SPaul E. McKenney 		return; /* Just leak it! */
6585ff8319fSPaul E. McKenney 	flush_delayed_work(&sup->work);
659e81baf4cSSebastian Andrzej Siewior 	for_each_possible_cpu(cpu) {
660e81baf4cSSebastian Andrzej Siewior 		struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
661e81baf4cSSebastian Andrzej Siewior 
662e81baf4cSSebastian Andrzej Siewior 		del_timer_sync(&sdp->delay_work);
663e81baf4cSSebastian Andrzej Siewior 		flush_work(&sdp->work);
6645cdfd174SPaul E. McKenney 		if (WARN_ON(rcu_segcblist_n_cbs(&sdp->srcu_cblist)))
6655cdfd174SPaul E. McKenney 			return; /* Forgot srcu_barrier(), so just leak it! */
666f7194ac3SPaul E. McKenney 	}
6675ff8319fSPaul E. McKenney 	if (WARN_ON(rcu_seq_state(READ_ONCE(sup->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
6685ff8319fSPaul E. McKenney 	    WARN_ON(rcu_seq_current(&sup->srcu_gp_seq) != sup->srcu_gp_seq_needed) ||
669aacb5d91SPaul E. McKenney 	    WARN_ON(srcu_readers_active(ssp))) {
6708ed00760SPaul E. McKenney 		pr_info("%s: Active srcu_struct %p read state: %d gp state: %lu/%lu\n",
6715ff8319fSPaul E. McKenney 			__func__, ssp, rcu_seq_state(READ_ONCE(sup->srcu_gp_seq)),
6725ff8319fSPaul E. McKenney 			rcu_seq_current(&sup->srcu_gp_seq), sup->srcu_gp_seq_needed);
673d7b0615cSPaul E. McKenney 		return; // Caller forgot to stop doing call_srcu()?
674d7b0615cSPaul E. McKenney 			// Or caller invoked start_poll_synchronize_srcu()
675d7b0615cSPaul E. McKenney 			// and then cleanup_srcu_struct() before that grace
676d7b0615cSPaul E. McKenney 			// period ended?
677dad81a20SPaul E. McKenney 	}
6785ff8319fSPaul E. McKenney 	kfree(sup->node);
6795ff8319fSPaul E. McKenney 	sup->node = NULL;
6805ff8319fSPaul E. McKenney 	sup->srcu_size_state = SRCU_SIZE_SMALL;
6815ff8319fSPaul E. McKenney 	if (!sup->sda_is_static) {
682aacb5d91SPaul E. McKenney 		free_percpu(ssp->sda);
683aacb5d91SPaul E. McKenney 		ssp->sda = NULL;
6845ff8319fSPaul E. McKenney 		kfree(sup);
68595433f72SPaul E. McKenney 		ssp->srcu_sup = NULL;
68646470cf8SPaul E. McKenney 	}
687dad81a20SPaul E. McKenney }
688f5ad3991SPaul E. McKenney EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
689dad81a20SPaul E. McKenney 
690e29a4915SFrederic Weisbecker #ifdef CONFIG_PROVE_RCU
691dad81a20SPaul E. McKenney /*
69227120e7dSPaul E. McKenney  * Check for consistent NMI safety.
69327120e7dSPaul E. McKenney  */
srcu_check_nmi_safety(struct srcu_struct * ssp,bool nmi_safe)694e29a4915SFrederic Weisbecker void srcu_check_nmi_safety(struct srcu_struct *ssp, bool nmi_safe)
69527120e7dSPaul E. McKenney {
69627120e7dSPaul E. McKenney 	int nmi_safe_mask = 1 << nmi_safe;
69727120e7dSPaul E. McKenney 	int old_nmi_safe_mask;
69827120e7dSPaul E. McKenney 	struct srcu_data *sdp;
69927120e7dSPaul E. McKenney 
7006b77bb9bSFrederic Weisbecker 	/* NMI-unsafe use in NMI is a bad sign */
7016b77bb9bSFrederic Weisbecker 	WARN_ON_ONCE(!nmi_safe && in_nmi());
70227120e7dSPaul E. McKenney 	sdp = raw_cpu_ptr(ssp->sda);
70327120e7dSPaul E. McKenney 	old_nmi_safe_mask = READ_ONCE(sdp->srcu_nmi_safety);
70427120e7dSPaul E. McKenney 	if (!old_nmi_safe_mask) {
70527120e7dSPaul E. McKenney 		WRITE_ONCE(sdp->srcu_nmi_safety, nmi_safe_mask);
70627120e7dSPaul E. McKenney 		return;
70727120e7dSPaul E. McKenney 	}
70827120e7dSPaul E. McKenney 	WARN_ONCE(old_nmi_safe_mask != nmi_safe_mask, "CPU %d old state %d new state %d\n", sdp->cpu, old_nmi_safe_mask, nmi_safe_mask);
70927120e7dSPaul E. McKenney }
710e29a4915SFrederic Weisbecker EXPORT_SYMBOL_GPL(srcu_check_nmi_safety);
711e29a4915SFrederic Weisbecker #endif /* CONFIG_PROVE_RCU */
71227120e7dSPaul E. McKenney 
71327120e7dSPaul E. McKenney /*
714dad81a20SPaul E. McKenney  * Counts the new reader in the appropriate per-CPU element of the
715cdf7abc4SPaolo Bonzini  * srcu_struct.
716dad81a20SPaul E. McKenney  * Returns an index that must be passed to the matching srcu_read_unlock().
717dad81a20SPaul E. McKenney  */
__srcu_read_lock(struct srcu_struct * ssp)718aacb5d91SPaul E. McKenney int __srcu_read_lock(struct srcu_struct *ssp)
719dad81a20SPaul E. McKenney {
720dad81a20SPaul E. McKenney 	int idx;
721dad81a20SPaul E. McKenney 
722aacb5d91SPaul E. McKenney 	idx = READ_ONCE(ssp->srcu_idx) & 0x1;
7235d0f5953SPaul E. McKenney 	this_cpu_inc(ssp->sda->srcu_lock_count[idx].counter);
724dad81a20SPaul E. McKenney 	smp_mb(); /* B */  /* Avoid leaking the critical section. */
725dad81a20SPaul E. McKenney 	return idx;
726dad81a20SPaul E. McKenney }
727dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_lock);
728dad81a20SPaul E. McKenney 
729dad81a20SPaul E. McKenney /*
730dad81a20SPaul E. McKenney  * Removes the count for the old reader from the appropriate per-CPU
731dad81a20SPaul E. McKenney  * element of the srcu_struct.  Note that this may well be a different
732dad81a20SPaul E. McKenney  * CPU than that which was incremented by the corresponding srcu_read_lock().
733dad81a20SPaul E. McKenney  */
__srcu_read_unlock(struct srcu_struct * ssp,int idx)734aacb5d91SPaul E. McKenney void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
735dad81a20SPaul E. McKenney {
736dad81a20SPaul E. McKenney 	smp_mb(); /* C */  /* Avoid leaking the critical section. */
7375d0f5953SPaul E. McKenney 	this_cpu_inc(ssp->sda->srcu_unlock_count[idx].counter);
738dad81a20SPaul E. McKenney }
739dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_unlock);
740dad81a20SPaul E. McKenney 
7412e83b879SPaul E. McKenney #ifdef CONFIG_NEED_SRCU_NMI_SAFE
7422e83b879SPaul E. McKenney 
7432e83b879SPaul E. McKenney /*
7442e83b879SPaul E. McKenney  * Counts the new reader in the appropriate per-CPU element of the
7452e83b879SPaul E. McKenney  * srcu_struct, but in an NMI-safe manner using RMW atomics.
7462e83b879SPaul E. McKenney  * Returns an index that must be passed to the matching srcu_read_unlock().
7472e83b879SPaul E. McKenney  */
__srcu_read_lock_nmisafe(struct srcu_struct * ssp)748e29a4915SFrederic Weisbecker int __srcu_read_lock_nmisafe(struct srcu_struct *ssp)
7492e83b879SPaul E. McKenney {
7502e83b879SPaul E. McKenney 	int idx;
7512e83b879SPaul E. McKenney 	struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
7522e83b879SPaul E. McKenney 
7532e83b879SPaul E. McKenney 	idx = READ_ONCE(ssp->srcu_idx) & 0x1;
7542e83b879SPaul E. McKenney 	atomic_long_inc(&sdp->srcu_lock_count[idx]);
7552e83b879SPaul E. McKenney 	smp_mb__after_atomic(); /* B */  /* Avoid leaking the critical section. */
7562e83b879SPaul E. McKenney 	return idx;
7572e83b879SPaul E. McKenney }
7582e83b879SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_lock_nmisafe);
7592e83b879SPaul E. McKenney 
7602e83b879SPaul E. McKenney /*
7612e83b879SPaul E. McKenney  * Removes the count for the old reader from the appropriate per-CPU
7622e83b879SPaul E. McKenney  * element of the srcu_struct.  Note that this may well be a different
7632e83b879SPaul E. McKenney  * CPU than that which was incremented by the corresponding srcu_read_lock().
7642e83b879SPaul E. McKenney  */
__srcu_read_unlock_nmisafe(struct srcu_struct * ssp,int idx)765e29a4915SFrederic Weisbecker void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
7662e83b879SPaul E. McKenney {
7672e83b879SPaul E. McKenney 	struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
7682e83b879SPaul E. McKenney 
7692e83b879SPaul E. McKenney 	smp_mb__before_atomic(); /* C */  /* Avoid leaking the critical section. */
7702e83b879SPaul E. McKenney 	atomic_long_inc(&sdp->srcu_unlock_count[idx]);
7712e83b879SPaul E. McKenney }
7722e83b879SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_unlock_nmisafe);
7732e83b879SPaul E. McKenney 
7742e83b879SPaul E. McKenney #endif // CONFIG_NEED_SRCU_NMI_SAFE
7752e83b879SPaul E. McKenney 
776dad81a20SPaul E. McKenney /*
777dad81a20SPaul E. McKenney  * Start an SRCU grace period.
778dad81a20SPaul E. McKenney  */
srcu_gp_start(struct srcu_struct * ssp)779aacb5d91SPaul E. McKenney static void srcu_gp_start(struct srcu_struct *ssp)
780dad81a20SPaul E. McKenney {
781dad81a20SPaul E. McKenney 	int state;
782dad81a20SPaul E. McKenney 
783b3fb11f7SPaul E. McKenney 	lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
78403200b5cSPaul E. McKenney 	WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed));
78503200b5cSPaul E. McKenney 	WRITE_ONCE(ssp->srcu_sup->srcu_gp_start, jiffies);
7863b46679cSPaul E. McKenney 	WRITE_ONCE(ssp->srcu_sup->srcu_n_exp_nodelay, 0);
7872da4b2a7SPaul E. McKenney 	smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
78803200b5cSPaul E. McKenney 	rcu_seq_start(&ssp->srcu_sup->srcu_gp_seq);
78903200b5cSPaul E. McKenney 	state = rcu_seq_state(ssp->srcu_sup->srcu_gp_seq);
790dad81a20SPaul E. McKenney 	WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
791dad81a20SPaul E. McKenney }
792dad81a20SPaul E. McKenney 
793da915ad5SPaul E. McKenney 
srcu_delay_timer(struct timer_list * t)794e81baf4cSSebastian Andrzej Siewior static void srcu_delay_timer(struct timer_list *t)
795da915ad5SPaul E. McKenney {
796e81baf4cSSebastian Andrzej Siewior 	struct srcu_data *sdp = container_of(t, struct srcu_data, delay_work);
797e81baf4cSSebastian Andrzej Siewior 
798e81baf4cSSebastian Andrzej Siewior 	queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work);
799da915ad5SPaul E. McKenney }
800da915ad5SPaul E. McKenney 
srcu_queue_delayed_work_on(struct srcu_data * sdp,unsigned long delay)801e81baf4cSSebastian Andrzej Siewior static void srcu_queue_delayed_work_on(struct srcu_data *sdp,
802da915ad5SPaul E. McKenney 				       unsigned long delay)
803da915ad5SPaul E. McKenney {
804e81baf4cSSebastian Andrzej Siewior 	if (!delay) {
805e81baf4cSSebastian Andrzej Siewior 		queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work);
806e81baf4cSSebastian Andrzej Siewior 		return;
807e81baf4cSSebastian Andrzej Siewior 	}
808da915ad5SPaul E. McKenney 
809e81baf4cSSebastian Andrzej Siewior 	timer_reduce(&sdp->delay_work, jiffies + delay);
810da915ad5SPaul E. McKenney }
811da915ad5SPaul E. McKenney 
812da915ad5SPaul E. McKenney /*
813da915ad5SPaul E. McKenney  * Schedule callback invocation for the specified srcu_data structure,
814da915ad5SPaul E. McKenney  * if possible, on the corresponding CPU.
815da915ad5SPaul E. McKenney  */
srcu_schedule_cbs_sdp(struct srcu_data * sdp,unsigned long delay)816da915ad5SPaul E. McKenney static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
817da915ad5SPaul E. McKenney {
818e81baf4cSSebastian Andrzej Siewior 	srcu_queue_delayed_work_on(sdp, delay);
819da915ad5SPaul E. McKenney }
820da915ad5SPaul E. McKenney 
821da915ad5SPaul E. McKenney /*
822da915ad5SPaul E. McKenney  * Schedule callback invocation for all srcu_data structures associated
823c7e88067SPaul E. McKenney  * with the specified srcu_node structure that have callbacks for the
824c7e88067SPaul E. McKenney  * just-completed grace period, the one corresponding to idx.  If possible,
825c7e88067SPaul E. McKenney  * schedule this invocation on the corresponding CPUs.
826da915ad5SPaul E. McKenney  */
srcu_schedule_cbs_snp(struct srcu_struct * ssp,struct srcu_node * snp,unsigned long mask,unsigned long delay)827aacb5d91SPaul E. McKenney static void srcu_schedule_cbs_snp(struct srcu_struct *ssp, struct srcu_node *snp,
8281e9a038bSPaul E. McKenney 				  unsigned long mask, unsigned long delay)
829da915ad5SPaul E. McKenney {
830da915ad5SPaul E. McKenney 	int cpu;
831da915ad5SPaul E. McKenney 
832c7e88067SPaul E. McKenney 	for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
833d8d5b7bfSDenis Arefev 		if (!(mask & (1UL << (cpu - snp->grplo))))
834c7e88067SPaul E. McKenney 			continue;
835aacb5d91SPaul E. McKenney 		srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, cpu), delay);
836da915ad5SPaul E. McKenney 	}
837c7e88067SPaul E. McKenney }
838da915ad5SPaul E. McKenney 
839da915ad5SPaul E. McKenney /*
840da915ad5SPaul E. McKenney  * Note the end of an SRCU grace period.  Initiates callback invocation
841da915ad5SPaul E. McKenney  * and starts a new grace period if needed.
842da915ad5SPaul E. McKenney  *
843da915ad5SPaul E. McKenney  * The ->srcu_cb_mutex acquisition does not protect any data, but
844da915ad5SPaul E. McKenney  * instead prevents more than one grace period from starting while we
845da915ad5SPaul E. McKenney  * are initiating callback invocation.  This allows the ->srcu_have_cbs[]
846da915ad5SPaul E. McKenney  * array to have a finite number of elements.
847da915ad5SPaul E. McKenney  */
srcu_gp_end(struct srcu_struct * ssp)848aacb5d91SPaul E. McKenney static void srcu_gp_end(struct srcu_struct *ssp)
849da915ad5SPaul E. McKenney {
8504f2bfd94SNeeraj Upadhyay 	unsigned long cbdelay = 1;
851da915ad5SPaul E. McKenney 	bool cbs;
8528ddbd883SIldar Ismagilov 	bool last_lvl;
853c350c008SPaul E. McKenney 	int cpu;
854da915ad5SPaul E. McKenney 	unsigned long gpseq;
855da915ad5SPaul E. McKenney 	int idx;
856c7e88067SPaul E. McKenney 	unsigned long mask;
857c350c008SPaul E. McKenney 	struct srcu_data *sdp;
858cbdc98e9SPaul E. McKenney 	unsigned long sgsne;
859da915ad5SPaul E. McKenney 	struct srcu_node *snp;
860e2f63836SPaul E. McKenney 	int ss_state;
8616c366522SPaul E. McKenney 	struct srcu_usage *sup = ssp->srcu_sup;
862da915ad5SPaul E. McKenney 
863da915ad5SPaul E. McKenney 	/* Prevent more than one additional grace period. */
8646c366522SPaul E. McKenney 	mutex_lock(&sup->srcu_cb_mutex);
865da915ad5SPaul E. McKenney 
866da915ad5SPaul E. McKenney 	/* End the current grace period. */
8676c366522SPaul E. McKenney 	spin_lock_irq_rcu_node(sup);
8686c366522SPaul E. McKenney 	idx = rcu_seq_state(sup->srcu_gp_seq);
869da915ad5SPaul E. McKenney 	WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
8706c366522SPaul E. McKenney 	if (ULONG_CMP_LT(READ_ONCE(sup->srcu_gp_seq), READ_ONCE(sup->srcu_gp_seq_needed_exp)))
8714f2bfd94SNeeraj Upadhyay 		cbdelay = 0;
8724f2bfd94SNeeraj Upadhyay 
8736c366522SPaul E. McKenney 	WRITE_ONCE(sup->srcu_last_gp_end, ktime_get_mono_fast_ns());
8746c366522SPaul E. McKenney 	rcu_seq_end(&sup->srcu_gp_seq);
8756c366522SPaul E. McKenney 	gpseq = rcu_seq_current(&sup->srcu_gp_seq);
8766c366522SPaul E. McKenney 	if (ULONG_CMP_LT(sup->srcu_gp_seq_needed_exp, gpseq))
8776c366522SPaul E. McKenney 		WRITE_ONCE(sup->srcu_gp_seq_needed_exp, gpseq);
8786c366522SPaul E. McKenney 	spin_unlock_irq_rcu_node(sup);
8796c366522SPaul E. McKenney 	mutex_unlock(&sup->srcu_gp_mutex);
880da915ad5SPaul E. McKenney 	/* A new grace period can start at this point.  But only one. */
881da915ad5SPaul E. McKenney 
882da915ad5SPaul E. McKenney 	/* Initiate callback invocation as needed. */
8836c366522SPaul E. McKenney 	ss_state = smp_load_acquire(&sup->srcu_size_state);
884c69a00a1SPaul E. McKenney 	if (ss_state < SRCU_SIZE_WAIT_BARRIER) {
8857f24626dSPingfan Liu 		srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, get_boot_cpu_id()),
8867f24626dSPingfan Liu 					cbdelay);
887994f7068SPaul E. McKenney 	} else {
888da915ad5SPaul E. McKenney 		idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
889aacb5d91SPaul E. McKenney 		srcu_for_each_node_breadth_first(ssp, snp) {
890d6331980SPaul E. McKenney 			spin_lock_irq_rcu_node(snp);
891da915ad5SPaul E. McKenney 			cbs = false;
8926c366522SPaul E. McKenney 			last_lvl = snp >= sup->level[rcu_num_lvls - 1];
8938ddbd883SIldar Ismagilov 			if (last_lvl)
894c69a00a1SPaul E. McKenney 				cbs = ss_state < SRCU_SIZE_BIG || snp->srcu_have_cbs[idx] == gpseq;
895da915ad5SPaul E. McKenney 			snp->srcu_have_cbs[idx] = gpseq;
896da915ad5SPaul E. McKenney 			rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
897cbdc98e9SPaul E. McKenney 			sgsne = snp->srcu_gp_seq_needed_exp;
898cbdc98e9SPaul E. McKenney 			if (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, gpseq))
8997ff8b450SPaul E. McKenney 				WRITE_ONCE(snp->srcu_gp_seq_needed_exp, gpseq);
900c69a00a1SPaul E. McKenney 			if (ss_state < SRCU_SIZE_BIG)
901c69a00a1SPaul E. McKenney 				mask = ~0;
902c69a00a1SPaul E. McKenney 			else
903c7e88067SPaul E. McKenney 				mask = snp->srcu_data_have_cbs[idx];
904c7e88067SPaul E. McKenney 			snp->srcu_data_have_cbs[idx] = 0;
905d6331980SPaul E. McKenney 			spin_unlock_irq_rcu_node(snp);
906a3883df3SPaul E. McKenney 			if (cbs)
907aacb5d91SPaul E. McKenney 				srcu_schedule_cbs_snp(ssp, snp, mask, cbdelay);
908994f7068SPaul E. McKenney 		}
909994f7068SPaul E. McKenney 	}
910c350c008SPaul E. McKenney 
911c350c008SPaul E. McKenney 	/* Occasionally prevent srcu_data counter wrap. */
912994f7068SPaul E. McKenney 	if (!(gpseq & counter_wrap_check))
913994f7068SPaul E. McKenney 		for_each_possible_cpu(cpu) {
914aacb5d91SPaul E. McKenney 			sdp = per_cpu_ptr(ssp->sda, cpu);
9154b56b0f5SPaul E. McKenney 			spin_lock_irq_rcu_node(sdp);
916994f7068SPaul E. McKenney 			if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed + 100))
917c350c008SPaul E. McKenney 				sdp->srcu_gp_seq_needed = gpseq;
918994f7068SPaul E. McKenney 			if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed_exp + 100))
919a35d13ecSIldar Ismagilov 				sdp->srcu_gp_seq_needed_exp = gpseq;
9204b56b0f5SPaul E. McKenney 			spin_unlock_irq_rcu_node(sdp);
921c350c008SPaul E. McKenney 		}
922da915ad5SPaul E. McKenney 
923da915ad5SPaul E. McKenney 	/* Callback initiation done, allow grace periods after next. */
9246c366522SPaul E. McKenney 	mutex_unlock(&sup->srcu_cb_mutex);
925da915ad5SPaul E. McKenney 
926da915ad5SPaul E. McKenney 	/* Start a new grace period if needed. */
9276c366522SPaul E. McKenney 	spin_lock_irq_rcu_node(sup);
9286c366522SPaul E. McKenney 	gpseq = rcu_seq_current(&sup->srcu_gp_seq);
929da915ad5SPaul E. McKenney 	if (!rcu_seq_state(gpseq) &&
9306c366522SPaul E. McKenney 	    ULONG_CMP_LT(gpseq, sup->srcu_gp_seq_needed)) {
931aacb5d91SPaul E. McKenney 		srcu_gp_start(ssp);
9326c366522SPaul E. McKenney 		spin_unlock_irq_rcu_node(sup);
933aacb5d91SPaul E. McKenney 		srcu_reschedule(ssp, 0);
934da915ad5SPaul E. McKenney 	} else {
9356c366522SPaul E. McKenney 		spin_unlock_irq_rcu_node(sup);
936da915ad5SPaul E. McKenney 	}
937e2f63836SPaul E. McKenney 
938e2f63836SPaul E. McKenney 	/* Transition to big if needed. */
939e2f63836SPaul E. McKenney 	if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
940e2f63836SPaul E. McKenney 		if (ss_state == SRCU_SIZE_ALLOC)
941c69a00a1SPaul E. McKenney 			init_srcu_struct_nodes(ssp, GFP_KERNEL);
942e2f63836SPaul E. McKenney 		else
9436c366522SPaul E. McKenney 			smp_store_release(&sup->srcu_size_state, ss_state + 1);
944e2f63836SPaul E. McKenney 	}
945da915ad5SPaul E. McKenney }
946da915ad5SPaul E. McKenney 
947da915ad5SPaul E. McKenney /*
9481e9a038bSPaul E. McKenney  * Funnel-locking scheme to scalably mediate many concurrent expedited
9491e9a038bSPaul E. McKenney  * grace-period requests.  This function is invoked for the first known
9501e9a038bSPaul E. McKenney  * expedited request for a grace period that has already been requested,
9511e9a038bSPaul E. McKenney  * but without expediting.  To start a completely new grace period,
9521e9a038bSPaul E. McKenney  * whether expedited or not, use srcu_funnel_gp_start() instead.
9531e9a038bSPaul E. McKenney  */
srcu_funnel_exp_start(struct srcu_struct * ssp,struct srcu_node * snp,unsigned long s)954aacb5d91SPaul E. McKenney static void srcu_funnel_exp_start(struct srcu_struct *ssp, struct srcu_node *snp,
9551e9a038bSPaul E. McKenney 				  unsigned long s)
9561e9a038bSPaul E. McKenney {
9571e9a038bSPaul E. McKenney 	unsigned long flags;
958cbdc98e9SPaul E. McKenney 	unsigned long sgsne;
9591e9a038bSPaul E. McKenney 
960994f7068SPaul E. McKenney 	if (snp)
9611e9a038bSPaul E. McKenney 		for (; snp != NULL; snp = snp->srcu_parent) {
962cbdc98e9SPaul E. McKenney 			sgsne = READ_ONCE(snp->srcu_gp_seq_needed_exp);
96303200b5cSPaul E. McKenney 			if (WARN_ON_ONCE(rcu_seq_done(&ssp->srcu_sup->srcu_gp_seq, s)) ||
964cbdc98e9SPaul E. McKenney 			    (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)))
9651e9a038bSPaul E. McKenney 				return;
966d6331980SPaul E. McKenney 			spin_lock_irqsave_rcu_node(snp, flags);
967cbdc98e9SPaul E. McKenney 			sgsne = snp->srcu_gp_seq_needed_exp;
968cbdc98e9SPaul E. McKenney 			if (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)) {
969d6331980SPaul E. McKenney 				spin_unlock_irqrestore_rcu_node(snp, flags);
9701e9a038bSPaul E. McKenney 				return;
9711e9a038bSPaul E. McKenney 			}
9721e9a038bSPaul E. McKenney 			WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
973d6331980SPaul E. McKenney 			spin_unlock_irqrestore_rcu_node(snp, flags);
9741e9a038bSPaul E. McKenney 		}
9759f2e91d9SPaul E. McKenney 	spin_lock_irqsave_ssp_contention(ssp, &flags);
97603200b5cSPaul E. McKenney 	if (ULONG_CMP_LT(ssp->srcu_sup->srcu_gp_seq_needed_exp, s))
97703200b5cSPaul E. McKenney 		WRITE_ONCE(ssp->srcu_sup->srcu_gp_seq_needed_exp, s);
978b3fb11f7SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
9791e9a038bSPaul E. McKenney }
9801e9a038bSPaul E. McKenney 
9811e9a038bSPaul E. McKenney /*
982da915ad5SPaul E. McKenney  * Funnel-locking scheme to scalably mediate many concurrent grace-period
983da915ad5SPaul E. McKenney  * requests.  The winner has to do the work of actually starting grace
984da915ad5SPaul E. McKenney  * period s.  Losers must either ensure that their desired grace-period
985da915ad5SPaul E. McKenney  * number is recorded on at least their leaf srcu_node structure, or they
986da915ad5SPaul E. McKenney  * must take steps to invoke their own callbacks.
98717294ce6SPaul E. McKenney  *
98817294ce6SPaul E. McKenney  * Note that this function also does the work of srcu_funnel_exp_start(),
98917294ce6SPaul E. McKenney  * in some cases by directly invoking it.
9901bafbfb3SPingfan Liu  *
9911bafbfb3SPingfan Liu  * The srcu read lock should be hold around this function. And s is a seq snap
9921bafbfb3SPingfan Liu  * after holding that lock.
993da915ad5SPaul E. McKenney  */
srcu_funnel_gp_start(struct srcu_struct * ssp,struct srcu_data * sdp,unsigned long s,bool do_norm)994aacb5d91SPaul E. McKenney static void srcu_funnel_gp_start(struct srcu_struct *ssp, struct srcu_data *sdp,
9951e9a038bSPaul E. McKenney 				 unsigned long s, bool do_norm)
996da915ad5SPaul E. McKenney {
997da915ad5SPaul E. McKenney 	unsigned long flags;
998da915ad5SPaul E. McKenney 	int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
999cbdc98e9SPaul E. McKenney 	unsigned long sgsne;
10007b9e9b58SPaul E. McKenney 	struct srcu_node *snp;
10010b56f953SNeeraj Upadhyay 	struct srcu_node *snp_leaf;
1002da915ad5SPaul E. McKenney 	unsigned long snp_seq;
1003cefc0a59SPaul E. McKenney 	struct srcu_usage *sup = ssp->srcu_sup;
1004da915ad5SPaul E. McKenney 
10050b56f953SNeeraj Upadhyay 	/* Ensure that snp node tree is fully initialized before traversing it */
1006cefc0a59SPaul E. McKenney 	if (smp_load_acquire(&sup->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
10070b56f953SNeeraj Upadhyay 		snp_leaf = NULL;
10080b56f953SNeeraj Upadhyay 	else
10090b56f953SNeeraj Upadhyay 		snp_leaf = sdp->mynode;
10100b56f953SNeeraj Upadhyay 
1011994f7068SPaul E. McKenney 	if (snp_leaf)
1012da915ad5SPaul E. McKenney 		/* Each pass through the loop does one level of the srcu_node tree. */
10137b9e9b58SPaul E. McKenney 		for (snp = snp_leaf; snp != NULL; snp = snp->srcu_parent) {
1014cefc0a59SPaul E. McKenney 			if (WARN_ON_ONCE(rcu_seq_done(&sup->srcu_gp_seq, s)) && snp != snp_leaf)
1015da915ad5SPaul E. McKenney 				return; /* GP already done and CBs recorded. */
1016d6331980SPaul E. McKenney 			spin_lock_irqsave_rcu_node(snp, flags);
1017da915ad5SPaul E. McKenney 			snp_seq = snp->srcu_have_cbs[idx];
1018cbdc98e9SPaul E. McKenney 			if (!srcu_invl_snp_seq(snp_seq) && ULONG_CMP_GE(snp_seq, s)) {
10197b9e9b58SPaul E. McKenney 				if (snp == snp_leaf && snp_seq == s)
1020c7e88067SPaul E. McKenney 					snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
1021d6331980SPaul E. McKenney 				spin_unlock_irqrestore_rcu_node(snp, flags);
10227b9e9b58SPaul E. McKenney 				if (snp == snp_leaf && snp_seq != s) {
1023aeb9b39bSPaul E. McKenney 					srcu_schedule_cbs_sdp(sdp, do_norm ? SRCU_INTERVAL : 0);
10241e9a038bSPaul E. McKenney 					return;
1025da915ad5SPaul E. McKenney 				}
10261e9a038bSPaul E. McKenney 				if (!do_norm)
1027aacb5d91SPaul E. McKenney 					srcu_funnel_exp_start(ssp, snp, s);
1028da915ad5SPaul E. McKenney 				return;
1029da915ad5SPaul E. McKenney 			}
1030da915ad5SPaul E. McKenney 			snp->srcu_have_cbs[idx] = s;
10317b9e9b58SPaul E. McKenney 			if (snp == snp_leaf)
1032c7e88067SPaul E. McKenney 				snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
1033cbdc98e9SPaul E. McKenney 			sgsne = snp->srcu_gp_seq_needed_exp;
1034cbdc98e9SPaul E. McKenney 			if (!do_norm && (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, s)))
10357ff8b450SPaul E. McKenney 				WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
1036d6331980SPaul E. McKenney 			spin_unlock_irqrestore_rcu_node(snp, flags);
1037da915ad5SPaul E. McKenney 		}
1038da915ad5SPaul E. McKenney 
1039da915ad5SPaul E. McKenney 	/* Top of tree, must ensure the grace period will be started. */
10409f2e91d9SPaul E. McKenney 	spin_lock_irqsave_ssp_contention(ssp, &flags);
1041cefc0a59SPaul E. McKenney 	if (ULONG_CMP_LT(sup->srcu_gp_seq_needed, s)) {
1042da915ad5SPaul E. McKenney 		/*
1043da915ad5SPaul E. McKenney 		 * Record need for grace period s.  Pair with load
1044da915ad5SPaul E. McKenney 		 * acquire setting up for initialization.
1045da915ad5SPaul E. McKenney 		 */
1046cefc0a59SPaul E. McKenney 		smp_store_release(&sup->srcu_gp_seq_needed, s); /*^^^*/
1047da915ad5SPaul E. McKenney 	}
1048cefc0a59SPaul E. McKenney 	if (!do_norm && ULONG_CMP_LT(sup->srcu_gp_seq_needed_exp, s))
1049cefc0a59SPaul E. McKenney 		WRITE_ONCE(sup->srcu_gp_seq_needed_exp, s);
1050da915ad5SPaul E. McKenney 
10511bafbfb3SPingfan Liu 	/* If grace period not already in progress, start it. */
1052cefc0a59SPaul E. McKenney 	if (!WARN_ON_ONCE(rcu_seq_done(&sup->srcu_gp_seq, s)) &&
1053cefc0a59SPaul E. McKenney 	    rcu_seq_state(sup->srcu_gp_seq) == SRCU_STATE_IDLE) {
1054cefc0a59SPaul E. McKenney 		WARN_ON_ONCE(ULONG_CMP_GE(sup->srcu_gp_seq, sup->srcu_gp_seq_needed));
1055aacb5d91SPaul E. McKenney 		srcu_gp_start(ssp);
1056ee5e2448SPaul E. McKenney 
1057ee5e2448SPaul E. McKenney 		// And how can that list_add() in the "else" clause
1058ee5e2448SPaul E. McKenney 		// possibly be safe for concurrent execution?  Well,
1059ee5e2448SPaul E. McKenney 		// it isn't.  And it does not have to be.  After all, it
1060ee5e2448SPaul E. McKenney 		// can only be executed during early boot when there is only
1061ee5e2448SPaul E. McKenney 		// the one boot CPU running with interrupts still disabled.
1062e0fcba9aSPaul E. McKenney 		if (likely(srcu_init_done))
1063cefc0a59SPaul E. McKenney 			queue_delayed_work(rcu_gp_wq, &sup->work,
1064282d8998SPaul E. McKenney 					   !!srcu_get_delay(ssp));
1065cefc0a59SPaul E. McKenney 		else if (list_empty(&sup->work.work.entry))
1066cefc0a59SPaul E. McKenney 			list_add(&sup->work.work.entry, &srcu_boot_list);
1067da915ad5SPaul E. McKenney 	}
1068cefc0a59SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(sup, flags);
1069da915ad5SPaul E. McKenney }
1070da915ad5SPaul E. McKenney 
1071da915ad5SPaul E. McKenney /*
1072dad81a20SPaul E. McKenney  * Wait until all readers counted by array index idx complete, but
1073dad81a20SPaul E. McKenney  * loop an additional time if there is an expedited grace period pending.
1074da915ad5SPaul E. McKenney  * The caller must ensure that ->srcu_idx is not changed while checking.
1075dad81a20SPaul E. McKenney  */
try_check_zero(struct srcu_struct * ssp,int idx,int trycount)1076aacb5d91SPaul E. McKenney static bool try_check_zero(struct srcu_struct *ssp, int idx, int trycount)
1077dad81a20SPaul E. McKenney {
10784f2bfd94SNeeraj Upadhyay 	unsigned long curdelay;
10794f2bfd94SNeeraj Upadhyay 
10804f2bfd94SNeeraj Upadhyay 	curdelay = !srcu_get_delay(ssp);
10814f2bfd94SNeeraj Upadhyay 
1082dad81a20SPaul E. McKenney 	for (;;) {
1083aacb5d91SPaul E. McKenney 		if (srcu_readers_active_idx_check(ssp, idx))
1084dad81a20SPaul E. McKenney 			return true;
10854f2bfd94SNeeraj Upadhyay 		if ((--trycount + curdelay) <= 0)
1086dad81a20SPaul E. McKenney 			return false;
10874f2bfd94SNeeraj Upadhyay 		udelay(srcu_retry_check_delay);
1088dad81a20SPaul E. McKenney 	}
1089dad81a20SPaul E. McKenney }
1090dad81a20SPaul E. McKenney 
1091dad81a20SPaul E. McKenney /*
1092da915ad5SPaul E. McKenney  * Increment the ->srcu_idx counter so that future SRCU readers will
1093da915ad5SPaul E. McKenney  * use the other rank of the ->srcu_(un)lock_count[] arrays.  This allows
1094dad81a20SPaul E. McKenney  * us to wait for pre-existing readers in a starvation-free manner.
1095dad81a20SPaul E. McKenney  */
srcu_flip(struct srcu_struct * ssp)1096aacb5d91SPaul E. McKenney static void srcu_flip(struct srcu_struct *ssp)
1097dad81a20SPaul E. McKenney {
1098881ec9d2SPaul E. McKenney 	/*
1099754aa642SJoel Fernandes (Google) 	 * Because the flip of ->srcu_idx is executed only if the
1100754aa642SJoel Fernandes (Google) 	 * preceding call to srcu_readers_active_idx_check() found that
1101754aa642SJoel Fernandes (Google) 	 * the ->srcu_unlock_count[] and ->srcu_lock_count[] sums matched
1102754aa642SJoel Fernandes (Google) 	 * and because that summing uses atomic_long_read(), there is
1103754aa642SJoel Fernandes (Google) 	 * ordering due to a control dependency between that summing and
1104754aa642SJoel Fernandes (Google) 	 * the WRITE_ONCE() in this call to srcu_flip().  This ordering
1105754aa642SJoel Fernandes (Google) 	 * ensures that if this updater saw a given reader's increment from
1106754aa642SJoel Fernandes (Google) 	 * __srcu_read_lock(), that reader was using a value of ->srcu_idx
1107754aa642SJoel Fernandes (Google) 	 * from before the previous call to srcu_flip(), which should be
1108754aa642SJoel Fernandes (Google) 	 * quite rare.  This ordering thus helps forward progress because
1109754aa642SJoel Fernandes (Google) 	 * the grace period could otherwise be delayed by additional
1110754aa642SJoel Fernandes (Google) 	 * calls to __srcu_read_lock() using that old (soon to be new)
1111754aa642SJoel Fernandes (Google) 	 * value of ->srcu_idx.
1112754aa642SJoel Fernandes (Google) 	 *
1113754aa642SJoel Fernandes (Google) 	 * This sum-equality check and ordering also ensures that if
1114754aa642SJoel Fernandes (Google) 	 * a given call to __srcu_read_lock() uses the new value of
1115754aa642SJoel Fernandes (Google) 	 * ->srcu_idx, this updater's earlier scans cannot have seen
1116754aa642SJoel Fernandes (Google) 	 * that reader's increments, which is all to the good, because
1117754aa642SJoel Fernandes (Google) 	 * this grace period need not wait on that reader.  After all,
1118754aa642SJoel Fernandes (Google) 	 * if those earlier scans had seen that reader, there would have
1119754aa642SJoel Fernandes (Google) 	 * been a sum mismatch and this code would not be reached.
1120754aa642SJoel Fernandes (Google) 	 *
1121754aa642SJoel Fernandes (Google) 	 * This means that the following smp_mb() is redundant, but
1122754aa642SJoel Fernandes (Google) 	 * it stays until either (1) Compilers learn about this sort of
1123754aa642SJoel Fernandes (Google) 	 * control dependency or (2) Some production workload running on
1124754aa642SJoel Fernandes (Google) 	 * a production system is unduly delayed by this slowpath smp_mb().
1125881ec9d2SPaul E. McKenney 	 */
1126881ec9d2SPaul E. McKenney 	smp_mb(); /* E */  /* Pairs with B and C. */
1127881ec9d2SPaul E. McKenney 
1128754aa642SJoel Fernandes (Google) 	WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); // Flip the counter.
1129dad81a20SPaul E. McKenney 
1130dad81a20SPaul E. McKenney 	/*
1131dad81a20SPaul E. McKenney 	 * Ensure that if the updater misses an __srcu_read_unlock()
1132dafc4d16SPaul E. McKenney 	 * increment, that task's __srcu_read_lock() following its next
1133dafc4d16SPaul E. McKenney 	 * __srcu_read_lock() or __srcu_read_unlock() will see the above
1134dafc4d16SPaul E. McKenney 	 * counter update.  Note that both this memory barrier and the
1135dafc4d16SPaul E. McKenney 	 * one in srcu_readers_active_idx_check() provide the guarantee
1136dafc4d16SPaul E. McKenney 	 * for __srcu_read_lock().
1137dad81a20SPaul E. McKenney 	 */
1138dad81a20SPaul E. McKenney 	smp_mb(); /* D */  /* Pairs with C. */
1139dad81a20SPaul E. McKenney }
1140dad81a20SPaul E. McKenney 
1141dad81a20SPaul E. McKenney /*
11422da4b2a7SPaul E. McKenney  * If SRCU is likely idle, return true, otherwise return false.
11432da4b2a7SPaul E. McKenney  *
11442da4b2a7SPaul E. McKenney  * Note that it is OK for several current from-idle requests for a new
11452da4b2a7SPaul E. McKenney  * grace period from idle to specify expediting because they will all end
11462da4b2a7SPaul E. McKenney  * up requesting the same grace period anyhow.  So no loss.
11472da4b2a7SPaul E. McKenney  *
11482da4b2a7SPaul E. McKenney  * Note also that if any CPU (including the current one) is still invoking
11492da4b2a7SPaul E. McKenney  * callbacks, this function will nevertheless say "idle".  This is not
11502da4b2a7SPaul E. McKenney  * ideal, but the overhead of checking all CPUs' callback lists is even
11512da4b2a7SPaul E. McKenney  * less ideal, especially on large systems.  Furthermore, the wakeup
11522da4b2a7SPaul E. McKenney  * can happen before the callback is fully removed, so we have no choice
11532da4b2a7SPaul E. McKenney  * but to accept this type of error.
11542da4b2a7SPaul E. McKenney  *
11552da4b2a7SPaul E. McKenney  * This function is also subject to counter-wrap errors, but let's face
11562da4b2a7SPaul E. McKenney  * it, if this function was preempted for enough time for the counters
11572da4b2a7SPaul E. McKenney  * to wrap, it really doesn't matter whether or not we expedite the grace
11582da4b2a7SPaul E. McKenney  * period.  The extra overhead of a needlessly expedited grace period is
11597fef6cffSEthon Paul  * negligible when amortized over that time period, and the extra latency
11602da4b2a7SPaul E. McKenney  * of a needlessly non-expedited grace period is similarly negligible.
11612da4b2a7SPaul E. McKenney  */
srcu_might_be_idle(struct srcu_struct * ssp)1162aacb5d91SPaul E. McKenney static bool srcu_might_be_idle(struct srcu_struct *ssp)
11632da4b2a7SPaul E. McKenney {
116422607d66SPaul E. McKenney 	unsigned long curseq;
11652da4b2a7SPaul E. McKenney 	unsigned long flags;
11662da4b2a7SPaul E. McKenney 	struct srcu_data *sdp;
116722607d66SPaul E. McKenney 	unsigned long t;
1168844a378dSPaul E. McKenney 	unsigned long tlast;
11692da4b2a7SPaul E. McKenney 
1170bde50d8fSSebastian Andrzej Siewior 	check_init_srcu_struct(ssp);
11712da4b2a7SPaul E. McKenney 	/* If the local srcu_data structure has callbacks, not idle.  */
1172bde50d8fSSebastian Andrzej Siewior 	sdp = raw_cpu_ptr(ssp->sda);
1173bde50d8fSSebastian Andrzej Siewior 	spin_lock_irqsave_rcu_node(sdp, flags);
11742da4b2a7SPaul E. McKenney 	if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
1175bde50d8fSSebastian Andrzej Siewior 		spin_unlock_irqrestore_rcu_node(sdp, flags);
11762da4b2a7SPaul E. McKenney 		return false; /* Callbacks already present, so not idle. */
11772da4b2a7SPaul E. McKenney 	}
1178bde50d8fSSebastian Andrzej Siewior 	spin_unlock_irqrestore_rcu_node(sdp, flags);
11792da4b2a7SPaul E. McKenney 
11802da4b2a7SPaul E. McKenney 	/*
1181a616aec9SIngo Molnar 	 * No local callbacks, so probabilistically probe global state.
11822da4b2a7SPaul E. McKenney 	 * Exact information would require acquiring locks, which would
1183a616aec9SIngo Molnar 	 * kill scalability, hence the probabilistic nature of the probe.
11842da4b2a7SPaul E. McKenney 	 */
118522607d66SPaul E. McKenney 
118622607d66SPaul E. McKenney 	/* First, see if enough time has passed since the last GP. */
118722607d66SPaul E. McKenney 	t = ktime_get_mono_fast_ns();
118803200b5cSPaul E. McKenney 	tlast = READ_ONCE(ssp->srcu_sup->srcu_last_gp_end);
118922607d66SPaul E. McKenney 	if (exp_holdoff == 0 ||
1190844a378dSPaul E. McKenney 	    time_in_range_open(t, tlast, tlast + exp_holdoff))
119122607d66SPaul E. McKenney 		return false; /* Too soon after last GP. */
119222607d66SPaul E. McKenney 
119322607d66SPaul E. McKenney 	/* Next, check for probable idleness. */
119403200b5cSPaul E. McKenney 	curseq = rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq);
11952da4b2a7SPaul E. McKenney 	smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
119603200b5cSPaul E. McKenney 	if (ULONG_CMP_LT(curseq, READ_ONCE(ssp->srcu_sup->srcu_gp_seq_needed)))
11972da4b2a7SPaul E. McKenney 		return false; /* Grace period in progress, so not idle. */
11982da4b2a7SPaul E. McKenney 	smp_mb(); /* Order ->srcu_gp_seq with prior access. */
119903200b5cSPaul E. McKenney 	if (curseq != rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq))
12002da4b2a7SPaul E. McKenney 		return false; /* GP # changed, so not idle. */
12012da4b2a7SPaul E. McKenney 	return true; /* With reasonable probability, idle! */
12022da4b2a7SPaul E. McKenney }
12032da4b2a7SPaul E. McKenney 
12042da4b2a7SPaul E. McKenney /*
1205a602538eSPaul E. McKenney  * SRCU callback function to leak a callback.
1206a602538eSPaul E. McKenney  */
srcu_leak_callback(struct rcu_head * rhp)1207a602538eSPaul E. McKenney static void srcu_leak_callback(struct rcu_head *rhp)
1208a602538eSPaul E. McKenney {
1209a602538eSPaul E. McKenney }
1210a602538eSPaul E. McKenney 
1211a602538eSPaul E. McKenney /*
121229d2bb94SPaul E. McKenney  * Start an SRCU grace period, and also queue the callback if non-NULL.
121329d2bb94SPaul E. McKenney  */
srcu_gp_start_if_needed(struct srcu_struct * ssp,struct rcu_head * rhp,bool do_norm)12145358c9faSPaul E. McKenney static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp,
12155358c9faSPaul E. McKenney 					     struct rcu_head *rhp, bool do_norm)
121629d2bb94SPaul E. McKenney {
121729d2bb94SPaul E. McKenney 	unsigned long flags;
121829d2bb94SPaul E. McKenney 	int idx;
121929d2bb94SPaul E. McKenney 	bool needexp = false;
122029d2bb94SPaul E. McKenney 	bool needgp = false;
122129d2bb94SPaul E. McKenney 	unsigned long s;
122229d2bb94SPaul E. McKenney 	struct srcu_data *sdp;
12230b56f953SNeeraj Upadhyay 	struct srcu_node *sdp_mynode;
12240b56f953SNeeraj Upadhyay 	int ss_state;
122529d2bb94SPaul E. McKenney 
12265358c9faSPaul E. McKenney 	check_init_srcu_struct(ssp);
1227ae3c0706SFrederic Weisbecker 	/*
1228ae3c0706SFrederic Weisbecker 	 * While starting a new grace period, make sure we are in an
1229ae3c0706SFrederic Weisbecker 	 * SRCU read-side critical section so that the grace-period
1230ae3c0706SFrederic Weisbecker 	 * sequence number cannot wrap around in the meantime.
1231ae3c0706SFrederic Weisbecker 	 */
1232e29a4915SFrederic Weisbecker 	idx = __srcu_read_lock_nmisafe(ssp);
1233a0d8cbd3SPaul E. McKenney 	ss_state = smp_load_acquire(&ssp->srcu_sup->srcu_size_state);
12340b56f953SNeeraj Upadhyay 	if (ss_state < SRCU_SIZE_WAIT_CALL)
12357f24626dSPingfan Liu 		sdp = per_cpu_ptr(ssp->sda, get_boot_cpu_id());
1236994f7068SPaul E. McKenney 	else
123729d2bb94SPaul E. McKenney 		sdp = raw_cpu_ptr(ssp->sda);
1238c2445d38SPaul E. McKenney 	spin_lock_irqsave_sdp_contention(sdp, &flags);
12395358c9faSPaul E. McKenney 	if (rhp)
124029d2bb94SPaul E. McKenney 		rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp);
12414a8e65b0SFrederic Weisbecker 	/*
124267050837SJoel Fernandes (Google) 	 * It's crucial to capture the snapshot 's' for acceleration before
124367050837SJoel Fernandes (Google) 	 * reading the current gp_seq that is used for advancing. This is
124467050837SJoel Fernandes (Google) 	 * essential because if the acceleration snapshot is taken after a
124567050837SJoel Fernandes (Google) 	 * failed advancement attempt, there's a risk that a grace period may
124667050837SJoel Fernandes (Google) 	 * conclude and a new one may start in the interim. If the snapshot is
124767050837SJoel Fernandes (Google) 	 * captured after this sequence of events, the acceleration snapshot 's'
124867050837SJoel Fernandes (Google) 	 * could be excessively advanced, leading to acceleration failure.
124967050837SJoel Fernandes (Google) 	 * In such a scenario, an 'acceleration leak' can occur, where new
125067050837SJoel Fernandes (Google) 	 * callbacks become indefinitely stuck in the RCU_NEXT_TAIL segment.
125167050837SJoel Fernandes (Google) 	 * Also note that encountering advancing failures is a normal
125267050837SJoel Fernandes (Google) 	 * occurrence when the grace period for RCU_WAIT_TAIL is in progress.
12534a8e65b0SFrederic Weisbecker 	 *
125467050837SJoel Fernandes (Google) 	 * To see this, consider the following events which occur if
125567050837SJoel Fernandes (Google) 	 * rcu_seq_snap() were to be called after advance:
12564a8e65b0SFrederic Weisbecker 	 *
12574a8e65b0SFrederic Weisbecker 	 *  1) The RCU_WAIT_TAIL segment has callbacks (gp_num = X + 4) and the
12584a8e65b0SFrederic Weisbecker 	 *     RCU_NEXT_READY_TAIL also has callbacks (gp_num = X + 8).
12594a8e65b0SFrederic Weisbecker 	 *
12604a8e65b0SFrederic Weisbecker 	 *  2) The grace period for RCU_WAIT_TAIL is seen as started but not
12614a8e65b0SFrederic Weisbecker 	 *     completed so rcu_seq_current() returns X + SRCU_STATE_SCAN1.
12624a8e65b0SFrederic Weisbecker 	 *
12634a8e65b0SFrederic Weisbecker 	 *  3) This value is passed to rcu_segcblist_advance() which can't move
12644a8e65b0SFrederic Weisbecker 	 *     any segment forward and fails.
12654a8e65b0SFrederic Weisbecker 	 *
12664a8e65b0SFrederic Weisbecker 	 *  4) srcu_gp_start_if_needed() still proceeds with callback acceleration.
12674a8e65b0SFrederic Weisbecker 	 *     But then the call to rcu_seq_snap() observes the grace period for the
12684a8e65b0SFrederic Weisbecker 	 *     RCU_WAIT_TAIL segment as completed and the subsequent one for the
12694a8e65b0SFrederic Weisbecker 	 *     RCU_NEXT_READY_TAIL segment as started (ie: X + 4 + SRCU_STATE_SCAN1)
12704a8e65b0SFrederic Weisbecker 	 *     so it returns a snapshot of the next grace period, which is X + 12.
12714a8e65b0SFrederic Weisbecker 	 *
12724a8e65b0SFrederic Weisbecker 	 *  5) The value of X + 12 is passed to rcu_segcblist_accelerate() but the
12734a8e65b0SFrederic Weisbecker 	 *     freshly enqueued callback in RCU_NEXT_TAIL can't move to
12744a8e65b0SFrederic Weisbecker 	 *     RCU_NEXT_READY_TAIL which already has callbacks for a previous grace
12754a8e65b0SFrederic Weisbecker 	 *     period (gp_num = X + 8). So acceleration fails.
12764a8e65b0SFrederic Weisbecker 	 */
12774a8e65b0SFrederic Weisbecker 	s = rcu_seq_snap(&ssp->srcu_sup->srcu_gp_seq);
127894c55b9eSFrederic Weisbecker 	if (rhp) {
127929d2bb94SPaul E. McKenney 		rcu_segcblist_advance(&sdp->srcu_cblist,
128003200b5cSPaul E. McKenney 				      rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
128167050837SJoel Fernandes (Google) 		/*
128267050837SJoel Fernandes (Google) 		 * Acceleration can never fail because the base current gp_seq
128367050837SJoel Fernandes (Google) 		 * used for acceleration is <= the value of gp_seq used for
128467050837SJoel Fernandes (Google) 		 * advancing. This means that RCU_NEXT_TAIL segment will
128567050837SJoel Fernandes (Google) 		 * always be able to be emptied by the acceleration into the
128667050837SJoel Fernandes (Google) 		 * RCU_NEXT_READY_TAIL or RCU_WAIT_TAIL segments.
128767050837SJoel Fernandes (Google) 		 */
128894c55b9eSFrederic Weisbecker 		WARN_ON_ONCE(!rcu_segcblist_accelerate(&sdp->srcu_cblist, s));
128994c55b9eSFrederic Weisbecker 	}
129029d2bb94SPaul E. McKenney 	if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
129129d2bb94SPaul E. McKenney 		sdp->srcu_gp_seq_needed = s;
129229d2bb94SPaul E. McKenney 		needgp = true;
129329d2bb94SPaul E. McKenney 	}
129429d2bb94SPaul E. McKenney 	if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
129529d2bb94SPaul E. McKenney 		sdp->srcu_gp_seq_needed_exp = s;
129629d2bb94SPaul E. McKenney 		needexp = true;
129729d2bb94SPaul E. McKenney 	}
129829d2bb94SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(sdp, flags);
12990b56f953SNeeraj Upadhyay 
13000b56f953SNeeraj Upadhyay 	/* Ensure that snp node tree is fully initialized before traversing it */
13010b56f953SNeeraj Upadhyay 	if (ss_state < SRCU_SIZE_WAIT_BARRIER)
13020b56f953SNeeraj Upadhyay 		sdp_mynode = NULL;
13030b56f953SNeeraj Upadhyay 	else
13040b56f953SNeeraj Upadhyay 		sdp_mynode = sdp->mynode;
13050b56f953SNeeraj Upadhyay 
130629d2bb94SPaul E. McKenney 	if (needgp)
130729d2bb94SPaul E. McKenney 		srcu_funnel_gp_start(ssp, sdp, s, do_norm);
130829d2bb94SPaul E. McKenney 	else if (needexp)
13090b56f953SNeeraj Upadhyay 		srcu_funnel_exp_start(ssp, sdp_mynode, s);
1310e29a4915SFrederic Weisbecker 	__srcu_read_unlock_nmisafe(ssp, idx);
13115358c9faSPaul E. McKenney 	return s;
131229d2bb94SPaul E. McKenney }
131329d2bb94SPaul E. McKenney 
131429d2bb94SPaul E. McKenney /*
1315da915ad5SPaul E. McKenney  * Enqueue an SRCU callback on the srcu_data structure associated with
1316da915ad5SPaul E. McKenney  * the current CPU and the specified srcu_struct structure, initiating
1317da915ad5SPaul E. McKenney  * grace-period processing if it is not already running.
1318dad81a20SPaul E. McKenney  *
1319dad81a20SPaul E. McKenney  * Note that all CPUs must agree that the grace period extended beyond
1320dad81a20SPaul E. McKenney  * all pre-existing SRCU read-side critical section.  On systems with
1321dad81a20SPaul E. McKenney  * more than one CPU, this means that when "func()" is invoked, each CPU
1322dad81a20SPaul E. McKenney  * is guaranteed to have executed a full memory barrier since the end of
1323dad81a20SPaul E. McKenney  * its last corresponding SRCU read-side critical section whose beginning
13245ef98a63SPaul E. McKenney  * preceded the call to call_srcu().  It also means that each CPU executing
1325dad81a20SPaul E. McKenney  * an SRCU read-side critical section that continues beyond the start of
13265ef98a63SPaul E. McKenney  * "func()" must have executed a memory barrier after the call_srcu()
1327dad81a20SPaul E. McKenney  * but before the beginning of that SRCU read-side critical section.
1328dad81a20SPaul E. McKenney  * Note that these guarantees include CPUs that are offline, idle, or
1329dad81a20SPaul E. McKenney  * executing in user mode, as well as CPUs that are executing in the kernel.
1330dad81a20SPaul E. McKenney  *
13315ef98a63SPaul E. McKenney  * Furthermore, if CPU A invoked call_srcu() and CPU B invoked the
1332dad81a20SPaul E. McKenney  * resulting SRCU callback function "func()", then both CPU A and CPU
1333dad81a20SPaul E. McKenney  * B are guaranteed to execute a full memory barrier during the time
13345ef98a63SPaul E. McKenney  * interval between the call to call_srcu() and the invocation of "func()".
1335dad81a20SPaul E. McKenney  * This guarantee applies even if CPU A and CPU B are the same CPU (but
1336dad81a20SPaul E. McKenney  * again only if the system has more than one CPU).
1337dad81a20SPaul E. McKenney  *
1338dad81a20SPaul E. McKenney  * Of course, these guarantees apply only for invocations of call_srcu(),
1339dad81a20SPaul E. McKenney  * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
1340dad81a20SPaul E. McKenney  * srcu_struct structure.
1341dad81a20SPaul E. McKenney  */
__call_srcu(struct srcu_struct * ssp,struct rcu_head * rhp,rcu_callback_t func,bool do_norm)134211b00045SJiang Biao static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
13431e9a038bSPaul E. McKenney 			rcu_callback_t func, bool do_norm)
1344dad81a20SPaul E. McKenney {
1345a602538eSPaul E. McKenney 	if (debug_rcu_head_queue(rhp)) {
1346a602538eSPaul E. McKenney 		/* Probable double call_srcu(), so leak the callback. */
1347a602538eSPaul E. McKenney 		WRITE_ONCE(rhp->func, srcu_leak_callback);
1348a602538eSPaul E. McKenney 		WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n");
1349a602538eSPaul E. McKenney 		return;
1350a602538eSPaul E. McKenney 	}
1351da915ad5SPaul E. McKenney 	rhp->func = func;
13525358c9faSPaul E. McKenney 	(void)srcu_gp_start_if_needed(ssp, rhp, do_norm);
13531e9a038bSPaul E. McKenney }
13541e9a038bSPaul E. McKenney 
13555a0465e1SPaul E. McKenney /**
13565a0465e1SPaul E. McKenney  * call_srcu() - Queue a callback for invocation after an SRCU grace period
1357aacb5d91SPaul E. McKenney  * @ssp: srcu_struct in queue the callback
135827fdb35fSPaul E. McKenney  * @rhp: structure to be used for queueing the SRCU callback.
13595a0465e1SPaul E. McKenney  * @func: function to be invoked after the SRCU grace period
13605a0465e1SPaul E. McKenney  *
13615a0465e1SPaul E. McKenney  * The callback function will be invoked some time after a full SRCU
13625a0465e1SPaul E. McKenney  * grace period elapses, in other words after all pre-existing SRCU
13635a0465e1SPaul E. McKenney  * read-side critical sections have completed.  However, the callback
13645a0465e1SPaul E. McKenney  * function might well execute concurrently with other SRCU read-side
13655a0465e1SPaul E. McKenney  * critical sections that started after call_srcu() was invoked.  SRCU
13665a0465e1SPaul E. McKenney  * read-side critical sections are delimited by srcu_read_lock() and
13675a0465e1SPaul E. McKenney  * srcu_read_unlock(), and may be nested.
13685a0465e1SPaul E. McKenney  *
13695a0465e1SPaul E. McKenney  * The callback will be invoked from process context, but must nevertheless
13705a0465e1SPaul E. McKenney  * be fast and must not block.
13715a0465e1SPaul E. McKenney  */
call_srcu(struct srcu_struct * ssp,struct rcu_head * rhp,rcu_callback_t func)1372aacb5d91SPaul E. McKenney void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
13731e9a038bSPaul E. McKenney 	       rcu_callback_t func)
13741e9a038bSPaul E. McKenney {
1375aacb5d91SPaul E. McKenney 	__call_srcu(ssp, rhp, func, true);
1376dad81a20SPaul E. McKenney }
1377dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(call_srcu);
1378dad81a20SPaul E. McKenney 
1379dad81a20SPaul E. McKenney /*
1380dad81a20SPaul E. McKenney  * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
1381dad81a20SPaul E. McKenney  */
__synchronize_srcu(struct srcu_struct * ssp,bool do_norm)1382aacb5d91SPaul E. McKenney static void __synchronize_srcu(struct srcu_struct *ssp, bool do_norm)
1383dad81a20SPaul E. McKenney {
1384dad81a20SPaul E. McKenney 	struct rcu_synchronize rcu;
1385dad81a20SPaul E. McKenney 
1386f0f44752SBoqun Feng 	srcu_lock_sync(&ssp->dep_map);
1387f0f44752SBoqun Feng 
1388f505d434SJakub Kicinski 	RCU_LOCKDEP_WARN(lockdep_is_held(ssp) ||
1389dad81a20SPaul E. McKenney 			 lock_is_held(&rcu_bh_lock_map) ||
1390dad81a20SPaul E. McKenney 			 lock_is_held(&rcu_lock_map) ||
1391dad81a20SPaul E. McKenney 			 lock_is_held(&rcu_sched_lock_map),
1392dad81a20SPaul E. McKenney 			 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
1393dad81a20SPaul E. McKenney 
1394dad81a20SPaul E. McKenney 	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
1395dad81a20SPaul E. McKenney 		return;
1396dad81a20SPaul E. McKenney 	might_sleep();
1397aacb5d91SPaul E. McKenney 	check_init_srcu_struct(ssp);
1398dad81a20SPaul E. McKenney 	init_completion(&rcu.completion);
1399da915ad5SPaul E. McKenney 	init_rcu_head_on_stack(&rcu.head);
1400aacb5d91SPaul E. McKenney 	__call_srcu(ssp, &rcu.head, wakeme_after_rcu, do_norm);
1401dad81a20SPaul E. McKenney 	wait_for_completion(&rcu.completion);
1402da915ad5SPaul E. McKenney 	destroy_rcu_head_on_stack(&rcu.head);
140335732cf9SPaul E. McKenney 
140435732cf9SPaul E. McKenney 	/*
140535732cf9SPaul E. McKenney 	 * Make sure that later code is ordered after the SRCU grace
1406d6331980SPaul E. McKenney 	 * period.  This pairs with the spin_lock_irq_rcu_node()
140735732cf9SPaul E. McKenney 	 * in srcu_invoke_callbacks().  Unlike Tree RCU, this is needed
140835732cf9SPaul E. McKenney 	 * because the current CPU might have been totally uninvolved with
140935732cf9SPaul E. McKenney 	 * (and thus unordered against) that grace period.
141035732cf9SPaul E. McKenney 	 */
141135732cf9SPaul E. McKenney 	smp_mb();
1412dad81a20SPaul E. McKenney }
1413dad81a20SPaul E. McKenney 
1414dad81a20SPaul E. McKenney /**
1415dad81a20SPaul E. McKenney  * synchronize_srcu_expedited - Brute-force SRCU grace period
1416aacb5d91SPaul E. McKenney  * @ssp: srcu_struct with which to synchronize.
1417dad81a20SPaul E. McKenney  *
1418dad81a20SPaul E. McKenney  * Wait for an SRCU grace period to elapse, but be more aggressive about
1419dad81a20SPaul E. McKenney  * spinning rather than blocking when waiting.
1420dad81a20SPaul E. McKenney  *
1421dad81a20SPaul E. McKenney  * Note that synchronize_srcu_expedited() has the same deadlock and
1422dad81a20SPaul E. McKenney  * memory-ordering properties as does synchronize_srcu().
1423dad81a20SPaul E. McKenney  */
synchronize_srcu_expedited(struct srcu_struct * ssp)1424aacb5d91SPaul E. McKenney void synchronize_srcu_expedited(struct srcu_struct *ssp)
1425dad81a20SPaul E. McKenney {
1426aacb5d91SPaul E. McKenney 	__synchronize_srcu(ssp, rcu_gp_is_normal());
1427dad81a20SPaul E. McKenney }
1428dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
1429dad81a20SPaul E. McKenney 
1430dad81a20SPaul E. McKenney /**
1431dad81a20SPaul E. McKenney  * synchronize_srcu - wait for prior SRCU read-side critical-section completion
1432aacb5d91SPaul E. McKenney  * @ssp: srcu_struct with which to synchronize.
1433dad81a20SPaul E. McKenney  *
1434dad81a20SPaul E. McKenney  * Wait for the count to drain to zero of both indexes. To avoid the
1435dad81a20SPaul E. McKenney  * possible starvation of synchronize_srcu(), it waits for the count of
1436da915ad5SPaul E. McKenney  * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
1437da915ad5SPaul E. McKenney  * and then flip the srcu_idx and wait for the count of the other index.
1438dad81a20SPaul E. McKenney  *
1439dad81a20SPaul E. McKenney  * Can block; must be called from process context.
1440dad81a20SPaul E. McKenney  *
1441dad81a20SPaul E. McKenney  * Note that it is illegal to call synchronize_srcu() from the corresponding
1442dad81a20SPaul E. McKenney  * SRCU read-side critical section; doing so will result in deadlock.
1443dad81a20SPaul E. McKenney  * However, it is perfectly legal to call synchronize_srcu() on one
1444dad81a20SPaul E. McKenney  * srcu_struct from some other srcu_struct's read-side critical section,
1445dad81a20SPaul E. McKenney  * as long as the resulting graph of srcu_structs is acyclic.
1446dad81a20SPaul E. McKenney  *
1447dad81a20SPaul E. McKenney  * There are memory-ordering constraints implied by synchronize_srcu().
1448dad81a20SPaul E. McKenney  * On systems with more than one CPU, when synchronize_srcu() returns,
1449dad81a20SPaul E. McKenney  * each CPU is guaranteed to have executed a full memory barrier since
14506eb95cc4SPaul E. McKenney  * the end of its last corresponding SRCU read-side critical section
1451dad81a20SPaul E. McKenney  * whose beginning preceded the call to synchronize_srcu().  In addition,
1452dad81a20SPaul E. McKenney  * each CPU having an SRCU read-side critical section that extends beyond
1453dad81a20SPaul E. McKenney  * the return from synchronize_srcu() is guaranteed to have executed a
1454dad81a20SPaul E. McKenney  * full memory barrier after the beginning of synchronize_srcu() and before
1455dad81a20SPaul E. McKenney  * the beginning of that SRCU read-side critical section.  Note that these
1456dad81a20SPaul E. McKenney  * guarantees include CPUs that are offline, idle, or executing in user mode,
1457dad81a20SPaul E. McKenney  * as well as CPUs that are executing in the kernel.
1458dad81a20SPaul E. McKenney  *
1459dad81a20SPaul E. McKenney  * Furthermore, if CPU A invoked synchronize_srcu(), which returned
1460dad81a20SPaul E. McKenney  * to its caller on CPU B, then both CPU A and CPU B are guaranteed
1461dad81a20SPaul E. McKenney  * to have executed a full memory barrier during the execution of
1462dad81a20SPaul E. McKenney  * synchronize_srcu().  This guarantee applies even if CPU A and CPU B
1463dad81a20SPaul E. McKenney  * are the same CPU, but again only if the system has more than one CPU.
1464dad81a20SPaul E. McKenney  *
1465dad81a20SPaul E. McKenney  * Of course, these memory-ordering guarantees apply only when
1466dad81a20SPaul E. McKenney  * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
1467dad81a20SPaul E. McKenney  * passed the same srcu_struct structure.
14682da4b2a7SPaul E. McKenney  *
14693d3a0d1bSPaul E. McKenney  * Implementation of these memory-ordering guarantees is similar to
14703d3a0d1bSPaul E. McKenney  * that of synchronize_rcu().
14713d3a0d1bSPaul E. McKenney  *
14722da4b2a7SPaul E. McKenney  * If SRCU is likely idle, expedite the first request.  This semantic
14732da4b2a7SPaul E. McKenney  * was provided by Classic SRCU, and is relied upon by its users, so TREE
14742da4b2a7SPaul E. McKenney  * SRCU must also provide it.  Note that detecting idleness is heuristic
14752da4b2a7SPaul E. McKenney  * and subject to both false positives and negatives.
1476dad81a20SPaul E. McKenney  */
synchronize_srcu(struct srcu_struct * ssp)1477aacb5d91SPaul E. McKenney void synchronize_srcu(struct srcu_struct *ssp)
1478dad81a20SPaul E. McKenney {
1479aacb5d91SPaul E. McKenney 	if (srcu_might_be_idle(ssp) || rcu_gp_is_expedited())
1480aacb5d91SPaul E. McKenney 		synchronize_srcu_expedited(ssp);
1481dad81a20SPaul E. McKenney 	else
1482aacb5d91SPaul E. McKenney 		__synchronize_srcu(ssp, true);
1483dad81a20SPaul E. McKenney }
1484dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(synchronize_srcu);
1485dad81a20SPaul E. McKenney 
14865358c9faSPaul E. McKenney /**
14875358c9faSPaul E. McKenney  * get_state_synchronize_srcu - Provide an end-of-grace-period cookie
14885358c9faSPaul E. McKenney  * @ssp: srcu_struct to provide cookie for.
14895358c9faSPaul E. McKenney  *
14905358c9faSPaul E. McKenney  * This function returns a cookie that can be passed to
14915358c9faSPaul E. McKenney  * poll_state_synchronize_srcu(), which will return true if a full grace
14925358c9faSPaul E. McKenney  * period has elapsed in the meantime.  It is the caller's responsibility
14935358c9faSPaul E. McKenney  * to make sure that grace period happens, for example, by invoking
14945358c9faSPaul E. McKenney  * call_srcu() after return from get_state_synchronize_srcu().
14955358c9faSPaul E. McKenney  */
get_state_synchronize_srcu(struct srcu_struct * ssp)14965358c9faSPaul E. McKenney unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp)
14975358c9faSPaul E. McKenney {
14985358c9faSPaul E. McKenney 	// Any prior manipulation of SRCU-protected data must happen
14995358c9faSPaul E. McKenney 	// before the load from ->srcu_gp_seq.
15005358c9faSPaul E. McKenney 	smp_mb();
150103200b5cSPaul E. McKenney 	return rcu_seq_snap(&ssp->srcu_sup->srcu_gp_seq);
15025358c9faSPaul E. McKenney }
15035358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(get_state_synchronize_srcu);
15045358c9faSPaul E. McKenney 
15055358c9faSPaul E. McKenney /**
15065358c9faSPaul E. McKenney  * start_poll_synchronize_srcu - Provide cookie and start grace period
15075358c9faSPaul E. McKenney  * @ssp: srcu_struct to provide cookie for.
15085358c9faSPaul E. McKenney  *
15095358c9faSPaul E. McKenney  * This function returns a cookie that can be passed to
15105358c9faSPaul E. McKenney  * poll_state_synchronize_srcu(), which will return true if a full grace
15115358c9faSPaul E. McKenney  * period has elapsed in the meantime.  Unlike get_state_synchronize_srcu(),
15125358c9faSPaul E. McKenney  * this function also ensures that any needed SRCU grace period will be
15135358c9faSPaul E. McKenney  * started.  This convenience does come at a cost in terms of CPU overhead.
15145358c9faSPaul E. McKenney  */
start_poll_synchronize_srcu(struct srcu_struct * ssp)15155358c9faSPaul E. McKenney unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp)
15165358c9faSPaul E. McKenney {
15175358c9faSPaul E. McKenney 	return srcu_gp_start_if_needed(ssp, NULL, true);
15185358c9faSPaul E. McKenney }
15195358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu);
15205358c9faSPaul E. McKenney 
15215358c9faSPaul E. McKenney /**
15225358c9faSPaul E. McKenney  * poll_state_synchronize_srcu - Has cookie's grace period ended?
15235358c9faSPaul E. McKenney  * @ssp: srcu_struct to provide cookie for.
15245358c9faSPaul E. McKenney  * @cookie: Return value from get_state_synchronize_srcu() or start_poll_synchronize_srcu().
15255358c9faSPaul E. McKenney  *
15265358c9faSPaul E. McKenney  * This function takes the cookie that was returned from either
15275358c9faSPaul E. McKenney  * get_state_synchronize_srcu() or start_poll_synchronize_srcu(), and
15285358c9faSPaul E. McKenney  * returns @true if an SRCU grace period elapsed since the time that the
15295358c9faSPaul E. McKenney  * cookie was created.
15304e7ccfaeSPaul E. McKenney  *
15314e7ccfaeSPaul E. McKenney  * Because cookies are finite in size, wrapping/overflow is possible.
15324e7ccfaeSPaul E. McKenney  * This is more pronounced on 32-bit systems where cookies are 32 bits,
15334e7ccfaeSPaul E. McKenney  * where in theory wrapping could happen in about 14 hours assuming
15344e7ccfaeSPaul E. McKenney  * 25-microsecond expedited SRCU grace periods.  However, a more likely
15354e7ccfaeSPaul E. McKenney  * overflow lower bound is on the order of 24 days in the case of
15364e7ccfaeSPaul E. McKenney  * one-millisecond SRCU grace periods.  Of course, wrapping in a 64-bit
15374e7ccfaeSPaul E. McKenney  * system requires geologic timespans, as in more than seven million years
15384e7ccfaeSPaul E. McKenney  * even for expedited SRCU grace periods.
15394e7ccfaeSPaul E. McKenney  *
15404e7ccfaeSPaul E. McKenney  * Wrapping/overflow is much more of an issue for CONFIG_SMP=n systems
15414e7ccfaeSPaul E. McKenney  * that also have CONFIG_PREEMPTION=n, which selects Tiny SRCU.  This uses
15424e7ccfaeSPaul E. McKenney  * a 16-bit cookie, which rcutorture routinely wraps in a matter of a
15434e7ccfaeSPaul E. McKenney  * few minutes.  If this proves to be a problem, this counter will be
15444e7ccfaeSPaul E. McKenney  * expanded to the same size as for Tree SRCU.
15455358c9faSPaul E. McKenney  */
poll_state_synchronize_srcu(struct srcu_struct * ssp,unsigned long cookie)15465358c9faSPaul E. McKenney bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie)
15475358c9faSPaul E. McKenney {
1548e206f33eSPaul E. McKenney 	if (cookie != SRCU_GET_STATE_COMPLETED &&
1549e206f33eSPaul E. McKenney 	    !rcu_seq_done(&ssp->srcu_sup->srcu_gp_seq, cookie))
15505358c9faSPaul E. McKenney 		return false;
15515358c9faSPaul E. McKenney 	// Ensure that the end of the SRCU grace period happens before
15525358c9faSPaul E. McKenney 	// any subsequent code that the caller might execute.
15535358c9faSPaul E. McKenney 	smp_mb(); // ^^^
15545358c9faSPaul E. McKenney 	return true;
15555358c9faSPaul E. McKenney }
15565358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu);
15575358c9faSPaul E. McKenney 
1558da915ad5SPaul E. McKenney /*
1559da915ad5SPaul E. McKenney  * Callback function for srcu_barrier() use.
1560da915ad5SPaul E. McKenney  */
srcu_barrier_cb(struct rcu_head * rhp)1561da915ad5SPaul E. McKenney static void srcu_barrier_cb(struct rcu_head *rhp)
1562da915ad5SPaul E. McKenney {
1563da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
1564aacb5d91SPaul E. McKenney 	struct srcu_struct *ssp;
1565da915ad5SPaul E. McKenney 
1566*e53cef03SPaul E. McKenney 	rhp->next = rhp; // Mark the callback as having been invoked.
1567da915ad5SPaul E. McKenney 	sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
1568aacb5d91SPaul E. McKenney 	ssp = sdp->ssp;
1569d20162e0SPaul E. McKenney 	if (atomic_dec_and_test(&ssp->srcu_sup->srcu_barrier_cpu_cnt))
1570d20162e0SPaul E. McKenney 		complete(&ssp->srcu_sup->srcu_barrier_completion);
1571da915ad5SPaul E. McKenney }
1572da915ad5SPaul E. McKenney 
1573994f7068SPaul E. McKenney /*
1574994f7068SPaul E. McKenney  * Enqueue an srcu_barrier() callback on the specified srcu_data
1575994f7068SPaul E. McKenney  * structure's ->cblist.  but only if that ->cblist already has at least one
1576994f7068SPaul E. McKenney  * callback enqueued.  Note that if a CPU already has callbacks enqueue,
1577994f7068SPaul E. McKenney  * it must have already registered the need for a future grace period,
1578994f7068SPaul E. McKenney  * so all we need do is enqueue a callback that will use the same grace
1579994f7068SPaul E. McKenney  * period as the last callback already in the queue.
1580994f7068SPaul E. McKenney  */
srcu_barrier_one_cpu(struct srcu_struct * ssp,struct srcu_data * sdp)1581994f7068SPaul E. McKenney static void srcu_barrier_one_cpu(struct srcu_struct *ssp, struct srcu_data *sdp)
1582994f7068SPaul E. McKenney {
1583994f7068SPaul E. McKenney 	spin_lock_irq_rcu_node(sdp);
1584d20162e0SPaul E. McKenney 	atomic_inc(&ssp->srcu_sup->srcu_barrier_cpu_cnt);
1585994f7068SPaul E. McKenney 	sdp->srcu_barrier_head.func = srcu_barrier_cb;
1586994f7068SPaul E. McKenney 	debug_rcu_head_queue(&sdp->srcu_barrier_head);
1587994f7068SPaul E. McKenney 	if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
1588994f7068SPaul E. McKenney 				   &sdp->srcu_barrier_head)) {
1589994f7068SPaul E. McKenney 		debug_rcu_head_unqueue(&sdp->srcu_barrier_head);
1590d20162e0SPaul E. McKenney 		atomic_dec(&ssp->srcu_sup->srcu_barrier_cpu_cnt);
1591994f7068SPaul E. McKenney 	}
1592994f7068SPaul E. McKenney 	spin_unlock_irq_rcu_node(sdp);
1593994f7068SPaul E. McKenney }
1594994f7068SPaul E. McKenney 
1595dad81a20SPaul E. McKenney /**
1596dad81a20SPaul E. McKenney  * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
1597aacb5d91SPaul E. McKenney  * @ssp: srcu_struct on which to wait for in-flight callbacks.
1598dad81a20SPaul E. McKenney  */
srcu_barrier(struct srcu_struct * ssp)1599aacb5d91SPaul E. McKenney void srcu_barrier(struct srcu_struct *ssp)
1600dad81a20SPaul E. McKenney {
1601da915ad5SPaul E. McKenney 	int cpu;
1602e2f63836SPaul E. McKenney 	int idx;
1603d20162e0SPaul E. McKenney 	unsigned long s = rcu_seq_snap(&ssp->srcu_sup->srcu_barrier_seq);
1604da915ad5SPaul E. McKenney 
1605aacb5d91SPaul E. McKenney 	check_init_srcu_struct(ssp);
1606d20162e0SPaul E. McKenney 	mutex_lock(&ssp->srcu_sup->srcu_barrier_mutex);
1607d20162e0SPaul E. McKenney 	if (rcu_seq_done(&ssp->srcu_sup->srcu_barrier_seq, s)) {
1608da915ad5SPaul E. McKenney 		smp_mb(); /* Force ordering following return. */
1609d20162e0SPaul E. McKenney 		mutex_unlock(&ssp->srcu_sup->srcu_barrier_mutex);
1610da915ad5SPaul E. McKenney 		return; /* Someone else did our work for us. */
1611da915ad5SPaul E. McKenney 	}
1612d20162e0SPaul E. McKenney 	rcu_seq_start(&ssp->srcu_sup->srcu_barrier_seq);
1613d20162e0SPaul E. McKenney 	init_completion(&ssp->srcu_sup->srcu_barrier_completion);
1614da915ad5SPaul E. McKenney 
1615da915ad5SPaul E. McKenney 	/* Initial count prevents reaching zero until all CBs are posted. */
1616d20162e0SPaul E. McKenney 	atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 1);
1617da915ad5SPaul E. McKenney 
1618e29a4915SFrederic Weisbecker 	idx = __srcu_read_lock_nmisafe(ssp);
1619a0d8cbd3SPaul E. McKenney 	if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
16207f24626dSPingfan Liu 		srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda,	get_boot_cpu_id()));
1621994f7068SPaul E. McKenney 	else
1622994f7068SPaul E. McKenney 		for_each_possible_cpu(cpu)
1623994f7068SPaul E. McKenney 			srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, cpu));
1624e29a4915SFrederic Weisbecker 	__srcu_read_unlock_nmisafe(ssp, idx);
1625da915ad5SPaul E. McKenney 
1626da915ad5SPaul E. McKenney 	/* Remove the initial count, at which point reaching zero can happen. */
1627d20162e0SPaul E. McKenney 	if (atomic_dec_and_test(&ssp->srcu_sup->srcu_barrier_cpu_cnt))
1628d20162e0SPaul E. McKenney 		complete(&ssp->srcu_sup->srcu_barrier_completion);
1629d20162e0SPaul E. McKenney 	wait_for_completion(&ssp->srcu_sup->srcu_barrier_completion);
1630da915ad5SPaul E. McKenney 
1631d20162e0SPaul E. McKenney 	rcu_seq_end(&ssp->srcu_sup->srcu_barrier_seq);
1632d20162e0SPaul E. McKenney 	mutex_unlock(&ssp->srcu_sup->srcu_barrier_mutex);
1633dad81a20SPaul E. McKenney }
1634dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_barrier);
1635dad81a20SPaul E. McKenney 
1636dad81a20SPaul E. McKenney /**
1637dad81a20SPaul E. McKenney  * srcu_batches_completed - return batches completed.
1638aacb5d91SPaul E. McKenney  * @ssp: srcu_struct on which to report batch completion.
1639dad81a20SPaul E. McKenney  *
1640dad81a20SPaul E. McKenney  * Report the number of batches, correlated with, but not necessarily
1641dad81a20SPaul E. McKenney  * precisely the same as, the number of grace periods that have elapsed.
1642dad81a20SPaul E. McKenney  */
srcu_batches_completed(struct srcu_struct * ssp)1643aacb5d91SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *ssp)
1644dad81a20SPaul E. McKenney {
164539f91504SPaul E. McKenney 	return READ_ONCE(ssp->srcu_idx);
1646dad81a20SPaul E. McKenney }
1647dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_batches_completed);
1648dad81a20SPaul E. McKenney 
1649dad81a20SPaul E. McKenney /*
1650da915ad5SPaul E. McKenney  * Core SRCU state machine.  Push state bits of ->srcu_gp_seq
1651da915ad5SPaul E. McKenney  * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
1652da915ad5SPaul E. McKenney  * completed in that state.
1653dad81a20SPaul E. McKenney  */
srcu_advance_state(struct srcu_struct * ssp)1654aacb5d91SPaul E. McKenney static void srcu_advance_state(struct srcu_struct *ssp)
1655dad81a20SPaul E. McKenney {
1656dad81a20SPaul E. McKenney 	int idx;
1657dad81a20SPaul E. McKenney 
1658e3a6ab25SPaul E. McKenney 	mutex_lock(&ssp->srcu_sup->srcu_gp_mutex);
1659da915ad5SPaul E. McKenney 
1660dad81a20SPaul E. McKenney 	/*
1661dad81a20SPaul E. McKenney 	 * Because readers might be delayed for an extended period after
1662da915ad5SPaul E. McKenney 	 * fetching ->srcu_idx for their index, at any point in time there
1663dad81a20SPaul E. McKenney 	 * might well be readers using both idx=0 and idx=1.  We therefore
1664dad81a20SPaul E. McKenney 	 * need to wait for readers to clear from both index values before
1665dad81a20SPaul E. McKenney 	 * invoking a callback.
1666dad81a20SPaul E. McKenney 	 *
1667dad81a20SPaul E. McKenney 	 * The load-acquire ensures that we see the accesses performed
1668dad81a20SPaul E. McKenney 	 * by the prior grace period.
1669dad81a20SPaul E. McKenney 	 */
167003200b5cSPaul E. McKenney 	idx = rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq)); /* ^^^ */
1671dad81a20SPaul E. McKenney 	if (idx == SRCU_STATE_IDLE) {
1672b3fb11f7SPaul E. McKenney 		spin_lock_irq_rcu_node(ssp->srcu_sup);
167303200b5cSPaul E. McKenney 		if (ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed)) {
167403200b5cSPaul E. McKenney 			WARN_ON_ONCE(rcu_seq_state(ssp->srcu_sup->srcu_gp_seq));
1675b3fb11f7SPaul E. McKenney 			spin_unlock_irq_rcu_node(ssp->srcu_sup);
1676e3a6ab25SPaul E. McKenney 			mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
1677dad81a20SPaul E. McKenney 			return;
1678dad81a20SPaul E. McKenney 		}
167903200b5cSPaul E. McKenney 		idx = rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq));
1680dad81a20SPaul E. McKenney 		if (idx == SRCU_STATE_IDLE)
1681aacb5d91SPaul E. McKenney 			srcu_gp_start(ssp);
1682b3fb11f7SPaul E. McKenney 		spin_unlock_irq_rcu_node(ssp->srcu_sup);
1683da915ad5SPaul E. McKenney 		if (idx != SRCU_STATE_IDLE) {
1684e3a6ab25SPaul E. McKenney 			mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
1685dad81a20SPaul E. McKenney 			return; /* Someone else started the grace period. */
1686dad81a20SPaul E. McKenney 		}
1687da915ad5SPaul E. McKenney 	}
1688dad81a20SPaul E. McKenney 
168903200b5cSPaul E. McKenney 	if (rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
1690aacb5d91SPaul E. McKenney 		idx = 1 ^ (ssp->srcu_idx & 1);
1691aacb5d91SPaul E. McKenney 		if (!try_check_zero(ssp, idx, 1)) {
1692e3a6ab25SPaul E. McKenney 			mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
1693dad81a20SPaul E. McKenney 			return; /* readers present, retry later. */
1694da915ad5SPaul E. McKenney 		}
1695aacb5d91SPaul E. McKenney 		srcu_flip(ssp);
1696b3fb11f7SPaul E. McKenney 		spin_lock_irq_rcu_node(ssp->srcu_sup);
169703200b5cSPaul E. McKenney 		rcu_seq_set_state(&ssp->srcu_sup->srcu_gp_seq, SRCU_STATE_SCAN2);
16983b46679cSPaul E. McKenney 		ssp->srcu_sup->srcu_n_exp_nodelay = 0;
1699b3fb11f7SPaul E. McKenney 		spin_unlock_irq_rcu_node(ssp->srcu_sup);
1700dad81a20SPaul E. McKenney 	}
1701dad81a20SPaul E. McKenney 
170203200b5cSPaul E. McKenney 	if (rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
1703dad81a20SPaul E. McKenney 
1704dad81a20SPaul E. McKenney 		/*
1705dad81a20SPaul E. McKenney 		 * SRCU read-side critical sections are normally short,
1706dad81a20SPaul E. McKenney 		 * so check at least twice in quick succession after a flip.
1707dad81a20SPaul E. McKenney 		 */
1708aacb5d91SPaul E. McKenney 		idx = 1 ^ (ssp->srcu_idx & 1);
1709aacb5d91SPaul E. McKenney 		if (!try_check_zero(ssp, idx, 2)) {
1710e3a6ab25SPaul E. McKenney 			mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
1711da915ad5SPaul E. McKenney 			return; /* readers present, retry later. */
1712da915ad5SPaul E. McKenney 		}
17133b46679cSPaul E. McKenney 		ssp->srcu_sup->srcu_n_exp_nodelay = 0;
1714aacb5d91SPaul E. McKenney 		srcu_gp_end(ssp);  /* Releases ->srcu_gp_mutex. */
1715dad81a20SPaul E. McKenney 	}
1716dad81a20SPaul E. McKenney }
1717dad81a20SPaul E. McKenney 
1718dad81a20SPaul E. McKenney /*
1719dad81a20SPaul E. McKenney  * Invoke a limited number of SRCU callbacks that have passed through
1720dad81a20SPaul E. McKenney  * their grace period.  If there are more to do, SRCU will reschedule
1721dad81a20SPaul E. McKenney  * the workqueue.  Note that needed memory barriers have been executed
1722dad81a20SPaul E. McKenney  * in this task's context by srcu_readers_active_idx_check().
1723dad81a20SPaul E. McKenney  */
srcu_invoke_callbacks(struct work_struct * work)1724da915ad5SPaul E. McKenney static void srcu_invoke_callbacks(struct work_struct *work)
1725dad81a20SPaul E. McKenney {
1726ae5c2341SJoel Fernandes (Google) 	long len;
1727da915ad5SPaul E. McKenney 	bool more;
1728dad81a20SPaul E. McKenney 	struct rcu_cblist ready_cbs;
1729dad81a20SPaul E. McKenney 	struct rcu_head *rhp;
1730da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
1731aacb5d91SPaul E. McKenney 	struct srcu_struct *ssp;
1732dad81a20SPaul E. McKenney 
1733e81baf4cSSebastian Andrzej Siewior 	sdp = container_of(work, struct srcu_data, work);
1734e81baf4cSSebastian Andrzej Siewior 
1735aacb5d91SPaul E. McKenney 	ssp = sdp->ssp;
1736dad81a20SPaul E. McKenney 	rcu_cblist_init(&ready_cbs);
1737d6331980SPaul E. McKenney 	spin_lock_irq_rcu_node(sdp);
17388a77f38bSFrederic Weisbecker 	WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL));
1739da915ad5SPaul E. McKenney 	rcu_segcblist_advance(&sdp->srcu_cblist,
174003200b5cSPaul E. McKenney 			      rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
1741c21357e4SFrederic Weisbecker 	/*
1742c21357e4SFrederic Weisbecker 	 * Although this function is theoretically re-entrant, concurrent
1743c21357e4SFrederic Weisbecker 	 * callbacks invocation is disallowed to avoid executing an SRCU barrier
1744c21357e4SFrederic Weisbecker 	 * too early.
1745c21357e4SFrederic Weisbecker 	 */
1746da915ad5SPaul E. McKenney 	if (sdp->srcu_cblist_invoking ||
1747da915ad5SPaul E. McKenney 	    !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
1748d6331980SPaul E. McKenney 		spin_unlock_irq_rcu_node(sdp);
1749da915ad5SPaul E. McKenney 		return;  /* Someone else on the job or nothing to do. */
1750da915ad5SPaul E. McKenney 	}
1751da915ad5SPaul E. McKenney 
1752da915ad5SPaul E. McKenney 	/* We are on the job!  Extract and invoke ready callbacks. */
1753da915ad5SPaul E. McKenney 	sdp->srcu_cblist_invoking = true;
1754da915ad5SPaul E. McKenney 	rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
1755ae5c2341SJoel Fernandes (Google) 	len = ready_cbs.len;
1756d6331980SPaul E. McKenney 	spin_unlock_irq_rcu_node(sdp);
1757dad81a20SPaul E. McKenney 	rhp = rcu_cblist_dequeue(&ready_cbs);
1758dad81a20SPaul E. McKenney 	for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
1759a602538eSPaul E. McKenney 		debug_rcu_head_unqueue(rhp);
17602cbc482dSZhen Lei 		debug_rcu_head_callback(rhp);
1761dad81a20SPaul E. McKenney 		local_bh_disable();
1762dad81a20SPaul E. McKenney 		rhp->func(rhp);
1763dad81a20SPaul E. McKenney 		local_bh_enable();
1764dad81a20SPaul E. McKenney 	}
1765ae5c2341SJoel Fernandes (Google) 	WARN_ON_ONCE(ready_cbs.len);
1766da915ad5SPaul E. McKenney 
1767da915ad5SPaul E. McKenney 	/*
1768da915ad5SPaul E. McKenney 	 * Update counts, accelerate new callbacks, and if needed,
1769da915ad5SPaul E. McKenney 	 * schedule another round of callback invocation.
1770da915ad5SPaul E. McKenney 	 */
1771d6331980SPaul E. McKenney 	spin_lock_irq_rcu_node(sdp);
1772ae5c2341SJoel Fernandes (Google) 	rcu_segcblist_add_len(&sdp->srcu_cblist, -len);
1773da915ad5SPaul E. McKenney 	sdp->srcu_cblist_invoking = false;
1774da915ad5SPaul E. McKenney 	more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
1775d6331980SPaul E. McKenney 	spin_unlock_irq_rcu_node(sdp);
1776c21357e4SFrederic Weisbecker 	/* An SRCU barrier or callbacks from previous nesting work pending */
1777da915ad5SPaul E. McKenney 	if (more)
1778da915ad5SPaul E. McKenney 		srcu_schedule_cbs_sdp(sdp, 0);
1779dad81a20SPaul E. McKenney }
1780dad81a20SPaul E. McKenney 
1781dad81a20SPaul E. McKenney /*
1782dad81a20SPaul E. McKenney  * Finished one round of SRCU grace period.  Start another if there are
1783dad81a20SPaul E. McKenney  * more SRCU callbacks queued, otherwise put SRCU into not-running state.
1784dad81a20SPaul E. McKenney  */
srcu_reschedule(struct srcu_struct * ssp,unsigned long delay)1785aacb5d91SPaul E. McKenney static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay)
1786dad81a20SPaul E. McKenney {
1787da915ad5SPaul E. McKenney 	bool pushgp = true;
1788dad81a20SPaul E. McKenney 
1789b3fb11f7SPaul E. McKenney 	spin_lock_irq_rcu_node(ssp->srcu_sup);
179003200b5cSPaul E. McKenney 	if (ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed)) {
179103200b5cSPaul E. McKenney 		if (!WARN_ON_ONCE(rcu_seq_state(ssp->srcu_sup->srcu_gp_seq))) {
1792da915ad5SPaul E. McKenney 			/* All requests fulfilled, time to go idle. */
1793da915ad5SPaul E. McKenney 			pushgp = false;
1794dad81a20SPaul E. McKenney 		}
179503200b5cSPaul E. McKenney 	} else if (!rcu_seq_state(ssp->srcu_sup->srcu_gp_seq)) {
1796da915ad5SPaul E. McKenney 		/* Outstanding request and no GP.  Start one. */
1797aacb5d91SPaul E. McKenney 		srcu_gp_start(ssp);
1798da915ad5SPaul E. McKenney 	}
1799b3fb11f7SPaul E. McKenney 	spin_unlock_irq_rcu_node(ssp->srcu_sup);
1800dad81a20SPaul E. McKenney 
1801da915ad5SPaul E. McKenney 	if (pushgp)
1802fd1b3f8eSPaul E. McKenney 		queue_delayed_work(rcu_gp_wq, &ssp->srcu_sup->work, delay);
1803dad81a20SPaul E. McKenney }
1804dad81a20SPaul E. McKenney 
1805dad81a20SPaul E. McKenney /*
1806dad81a20SPaul E. McKenney  * This is the work-queue function that handles SRCU grace periods.
1807dad81a20SPaul E. McKenney  */
process_srcu(struct work_struct * work)18080d8a1e83SPaul E. McKenney static void process_srcu(struct work_struct *work)
1809dad81a20SPaul E. McKenney {
1810282d8998SPaul E. McKenney 	unsigned long curdelay;
1811282d8998SPaul E. McKenney 	unsigned long j;
1812aacb5d91SPaul E. McKenney 	struct srcu_struct *ssp;
1813fd1b3f8eSPaul E. McKenney 	struct srcu_usage *sup;
1814dad81a20SPaul E. McKenney 
1815fd1b3f8eSPaul E. McKenney 	sup = container_of(work, struct srcu_usage, work.work);
1816fd1b3f8eSPaul E. McKenney 	ssp = sup->srcu_ssp;
1817dad81a20SPaul E. McKenney 
1818aacb5d91SPaul E. McKenney 	srcu_advance_state(ssp);
1819282d8998SPaul E. McKenney 	curdelay = srcu_get_delay(ssp);
1820282d8998SPaul E. McKenney 	if (curdelay) {
1821fd1b3f8eSPaul E. McKenney 		WRITE_ONCE(sup->reschedule_count, 0);
1822282d8998SPaul E. McKenney 	} else {
1823282d8998SPaul E. McKenney 		j = jiffies;
1824fd1b3f8eSPaul E. McKenney 		if (READ_ONCE(sup->reschedule_jiffies) == j) {
1825c8c3ae83SPaul E. McKenney 			ASSERT_EXCLUSIVE_WRITER(sup->reschedule_count);
1826fd1b3f8eSPaul E. McKenney 			WRITE_ONCE(sup->reschedule_count, READ_ONCE(sup->reschedule_count) + 1);
1827fd1b3f8eSPaul E. McKenney 			if (READ_ONCE(sup->reschedule_count) > srcu_max_nodelay)
1828282d8998SPaul E. McKenney 				curdelay = 1;
1829282d8998SPaul E. McKenney 		} else {
1830fd1b3f8eSPaul E. McKenney 			WRITE_ONCE(sup->reschedule_count, 1);
1831fd1b3f8eSPaul E. McKenney 			WRITE_ONCE(sup->reschedule_jiffies, j);
1832282d8998SPaul E. McKenney 		}
1833282d8998SPaul E. McKenney 	}
1834282d8998SPaul E. McKenney 	srcu_reschedule(ssp, curdelay);
1835dad81a20SPaul E. McKenney }
18367f6733c3SPaul E. McKenney 
srcutorture_get_gp_data(struct srcu_struct * ssp,int * flags,unsigned long * gp_seq)1837dddcddefSZqiang void srcutorture_get_gp_data(struct srcu_struct *ssp, int *flags,
1838aebc8264SPaul E. McKenney 			     unsigned long *gp_seq)
18397f6733c3SPaul E. McKenney {
18407f6733c3SPaul E. McKenney 	*flags = 0;
184103200b5cSPaul E. McKenney 	*gp_seq = rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq);
18427f6733c3SPaul E. McKenney }
18437f6733c3SPaul E. McKenney EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
18441f4f6da1SPaul E. McKenney 
18453bedebcfSPaul E. McKenney static const char * const srcu_size_state_name[] = {
18463bedebcfSPaul E. McKenney 	"SRCU_SIZE_SMALL",
18473bedebcfSPaul E. McKenney 	"SRCU_SIZE_ALLOC",
18483bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_BARRIER",
18493bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_CALL",
18503bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_CBS1",
18513bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_CBS2",
18523bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_CBS3",
18533bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_CBS4",
18543bedebcfSPaul E. McKenney 	"SRCU_SIZE_BIG",
18553bedebcfSPaul E. McKenney 	"SRCU_SIZE_???",
18563bedebcfSPaul E. McKenney };
18573bedebcfSPaul E. McKenney 
srcu_torture_stats_print(struct srcu_struct * ssp,char * tt,char * tf)1858aacb5d91SPaul E. McKenney void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf)
1859115a1a52SPaul E. McKenney {
1860115a1a52SPaul E. McKenney 	int cpu;
1861115a1a52SPaul E. McKenney 	int idx;
1862ac3748c6SPaul E. McKenney 	unsigned long s0 = 0, s1 = 0;
1863a0d8cbd3SPaul E. McKenney 	int ss_state = READ_ONCE(ssp->srcu_sup->srcu_size_state);
18643bedebcfSPaul E. McKenney 	int ss_state_idx = ss_state;
1865115a1a52SPaul E. McKenney 
1866aacb5d91SPaul E. McKenney 	idx = ssp->srcu_idx & 0x1;
18673bedebcfSPaul E. McKenney 	if (ss_state < 0 || ss_state >= ARRAY_SIZE(srcu_size_state_name))
18683bedebcfSPaul E. McKenney 		ss_state_idx = ARRAY_SIZE(srcu_size_state_name) - 1;
18694a230f80SPaul E. McKenney 	pr_alert("%s%s Tree SRCU g%ld state %d (%s)",
187003200b5cSPaul E. McKenney 		 tt, tf, rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq), ss_state,
18714a230f80SPaul E. McKenney 		 srcu_size_state_name[ss_state_idx]);
18724a230f80SPaul E. McKenney 	if (!ssp->sda) {
18734a230f80SPaul E. McKenney 		// Called after cleanup_srcu_struct(), perhaps.
18744a230f80SPaul E. McKenney 		pr_cont(" No per-CPU srcu_data structures (->sda == NULL).\n");
18754a230f80SPaul E. McKenney 	} else {
18764a230f80SPaul E. McKenney 		pr_cont(" per-CPU(idx=%d):", idx);
1877115a1a52SPaul E. McKenney 		for_each_possible_cpu(cpu) {
1878115a1a52SPaul E. McKenney 			unsigned long l0, l1;
1879115a1a52SPaul E. McKenney 			unsigned long u0, u1;
1880115a1a52SPaul E. McKenney 			long c0, c1;
18815ab07a8dSPaul E. McKenney 			struct srcu_data *sdp;
1882115a1a52SPaul E. McKenney 
1883aacb5d91SPaul E. McKenney 			sdp = per_cpu_ptr(ssp->sda, cpu);
18845d0f5953SPaul E. McKenney 			u0 = data_race(atomic_long_read(&sdp->srcu_unlock_count[!idx]));
18855d0f5953SPaul E. McKenney 			u1 = data_race(atomic_long_read(&sdp->srcu_unlock_count[idx]));
1886115a1a52SPaul E. McKenney 
1887115a1a52SPaul E. McKenney 			/*
1888115a1a52SPaul E. McKenney 			 * Make sure that a lock is always counted if the corresponding
1889115a1a52SPaul E. McKenney 			 * unlock is counted.
1890115a1a52SPaul E. McKenney 			 */
1891115a1a52SPaul E. McKenney 			smp_rmb();
1892115a1a52SPaul E. McKenney 
18935d0f5953SPaul E. McKenney 			l0 = data_race(atomic_long_read(&sdp->srcu_lock_count[!idx]));
18945d0f5953SPaul E. McKenney 			l1 = data_race(atomic_long_read(&sdp->srcu_lock_count[idx]));
1895115a1a52SPaul E. McKenney 
1896115a1a52SPaul E. McKenney 			c0 = l0 - u0;
1897115a1a52SPaul E. McKenney 			c1 = l1 - u1;
18987e210a65SPaul E. McKenney 			pr_cont(" %d(%ld,%ld %c)",
18997e210a65SPaul E. McKenney 				cpu, c0, c1,
19007e210a65SPaul E. McKenney 				"C."[rcu_segcblist_empty(&sdp->srcu_cblist)]);
1901ac3748c6SPaul E. McKenney 			s0 += c0;
1902ac3748c6SPaul E. McKenney 			s1 += c1;
1903115a1a52SPaul E. McKenney 		}
1904ac3748c6SPaul E. McKenney 		pr_cont(" T(%ld,%ld)\n", s0, s1);
19054a230f80SPaul E. McKenney 	}
19069f2e91d9SPaul E. McKenney 	if (SRCU_SIZING_IS_TORTURE())
190799659f64SPaul E. McKenney 		srcu_transition_to_big(ssp);
1908115a1a52SPaul E. McKenney }
1909115a1a52SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_torture_stats_print);
1910115a1a52SPaul E. McKenney 
srcu_bootup_announce(void)19111f4f6da1SPaul E. McKenney static int __init srcu_bootup_announce(void)
19121f4f6da1SPaul E. McKenney {
19131f4f6da1SPaul E. McKenney 	pr_info("Hierarchical SRCU implementation.\n");
19140c8e0e3cSPaul E. McKenney 	if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF)
19150c8e0e3cSPaul E. McKenney 		pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff);
19164f2bfd94SNeeraj Upadhyay 	if (srcu_retry_check_delay != SRCU_DEFAULT_RETRY_CHECK_DELAY)
19174f2bfd94SNeeraj Upadhyay 		pr_info("\tNon-default retry check delay of %lu us.\n", srcu_retry_check_delay);
19184f2bfd94SNeeraj Upadhyay 	if (srcu_max_nodelay != SRCU_DEFAULT_MAX_NODELAY)
19194f2bfd94SNeeraj Upadhyay 		pr_info("\tNon-default max no-delay of %lu.\n", srcu_max_nodelay);
19204f2bfd94SNeeraj Upadhyay 	pr_info("\tMax phase no-delay instances is %lu.\n", srcu_max_nodelay_phase);
19211f4f6da1SPaul E. McKenney 	return 0;
19221f4f6da1SPaul E. McKenney }
19231f4f6da1SPaul E. McKenney early_initcall(srcu_bootup_announce);
1924e0fcba9aSPaul E. McKenney 
srcu_init(void)1925e0fcba9aSPaul E. McKenney void __init srcu_init(void)
1926e0fcba9aSPaul E. McKenney {
1927fd1b3f8eSPaul E. McKenney 	struct srcu_usage *sup;
1928e0fcba9aSPaul E. McKenney 
1929a57ffb3cSPaul E. McKenney 	/* Decide on srcu_struct-size strategy. */
1930a57ffb3cSPaul E. McKenney 	if (SRCU_SIZING_IS(SRCU_SIZING_AUTO)) {
1931a57ffb3cSPaul E. McKenney 		if (nr_cpu_ids >= big_cpu_lim) {
1932a57ffb3cSPaul E. McKenney 			convert_to_big = SRCU_SIZING_INIT; // Don't bother waiting for contention.
1933a57ffb3cSPaul E. McKenney 			pr_info("%s: Setting srcu_struct sizes to big.\n", __func__);
1934a57ffb3cSPaul E. McKenney 		} else {
1935a57ffb3cSPaul E. McKenney 			convert_to_big = SRCU_SIZING_NONE | SRCU_SIZING_CONTEND;
1936a57ffb3cSPaul E. McKenney 			pr_info("%s: Setting srcu_struct sizes based on contention.\n", __func__);
1937a57ffb3cSPaul E. McKenney 		}
1938a57ffb3cSPaul E. McKenney 	}
1939a57ffb3cSPaul E. McKenney 
19408e9c01c7SFrederic Weisbecker 	/*
19418e9c01c7SFrederic Weisbecker 	 * Once that is set, call_srcu() can follow the normal path and
19428e9c01c7SFrederic Weisbecker 	 * queue delayed work. This must follow RCU workqueues creation
19438e9c01c7SFrederic Weisbecker 	 * and timers initialization.
19448e9c01c7SFrederic Weisbecker 	 */
1945e0fcba9aSPaul E. McKenney 	srcu_init_done = true;
1946e0fcba9aSPaul E. McKenney 	while (!list_empty(&srcu_boot_list)) {
1947fd1b3f8eSPaul E. McKenney 		sup = list_first_entry(&srcu_boot_list, struct srcu_usage,
19484e6ea4efSPaul E. McKenney 				      work.work.entry);
1949fd1b3f8eSPaul E. McKenney 		list_del_init(&sup->work.work.entry);
1950a0d8cbd3SPaul E. McKenney 		if (SRCU_SIZING_IS(SRCU_SIZING_INIT) &&
1951fd1b3f8eSPaul E. McKenney 		    sup->srcu_size_state == SRCU_SIZE_SMALL)
1952fd1b3f8eSPaul E. McKenney 			sup->srcu_size_state = SRCU_SIZE_ALLOC;
1953fd1b3f8eSPaul E. McKenney 		queue_work(rcu_gp_wq, &sup->work.work);
1954e0fcba9aSPaul E. McKenney 	}
1955e0fcba9aSPaul E. McKenney }
1956fe15b50cSPaul E. McKenney 
1957fe15b50cSPaul E. McKenney #ifdef CONFIG_MODULES
1958fe15b50cSPaul E. McKenney 
1959fe15b50cSPaul E. McKenney /* Initialize any global-scope srcu_struct structures used by this module. */
srcu_module_coming(struct module * mod)1960fe15b50cSPaul E. McKenney static int srcu_module_coming(struct module *mod)
1961fe15b50cSPaul E. McKenney {
1962fe15b50cSPaul E. McKenney 	int i;
1963f4d01a25SPaul E. McKenney 	struct srcu_struct *ssp;
1964fe15b50cSPaul E. McKenney 	struct srcu_struct **sspp = mod->srcu_struct_ptrs;
1965fe15b50cSPaul E. McKenney 
1966fe15b50cSPaul E. McKenney 	for (i = 0; i < mod->num_srcu_structs; i++) {
1967f4d01a25SPaul E. McKenney 		ssp = *(sspp++);
1968f4d01a25SPaul E. McKenney 		ssp->sda = alloc_percpu(struct srcu_data);
1969f4d01a25SPaul E. McKenney 		if (WARN_ON_ONCE(!ssp->sda))
1970f4d01a25SPaul E. McKenney 			return -ENOMEM;
1971fe15b50cSPaul E. McKenney 	}
1972fe15b50cSPaul E. McKenney 	return 0;
1973fe15b50cSPaul E. McKenney }
1974fe15b50cSPaul E. McKenney 
1975fe15b50cSPaul E. McKenney /* Clean up any global-scope srcu_struct structures used by this module. */
srcu_module_going(struct module * mod)1976fe15b50cSPaul E. McKenney static void srcu_module_going(struct module *mod)
1977fe15b50cSPaul E. McKenney {
1978fe15b50cSPaul E. McKenney 	int i;
1979f4d01a25SPaul E. McKenney 	struct srcu_struct *ssp;
1980fe15b50cSPaul E. McKenney 	struct srcu_struct **sspp = mod->srcu_struct_ptrs;
1981fe15b50cSPaul E. McKenney 
1982f4d01a25SPaul E. McKenney 	for (i = 0; i < mod->num_srcu_structs; i++) {
1983f4d01a25SPaul E. McKenney 		ssp = *(sspp++);
198403200b5cSPaul E. McKenney 		if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed)) &&
1985660349acSPaul E. McKenney 		    !WARN_ON_ONCE(!ssp->srcu_sup->sda_is_static))
1986f4d01a25SPaul E. McKenney 			cleanup_srcu_struct(ssp);
1987a7bf4d7cSPaul E. McKenney 		if (!WARN_ON(srcu_readers_active(ssp)))
1988f4d01a25SPaul E. McKenney 			free_percpu(ssp->sda);
1989f4d01a25SPaul E. McKenney 	}
1990fe15b50cSPaul E. McKenney }
1991fe15b50cSPaul E. McKenney 
1992fe15b50cSPaul E. McKenney /* Handle one module, either coming or going. */
srcu_module_notify(struct notifier_block * self,unsigned long val,void * data)1993fe15b50cSPaul E. McKenney static int srcu_module_notify(struct notifier_block *self,
1994fe15b50cSPaul E. McKenney 			      unsigned long val, void *data)
1995fe15b50cSPaul E. McKenney {
1996fe15b50cSPaul E. McKenney 	struct module *mod = data;
1997fe15b50cSPaul E. McKenney 	int ret = 0;
1998fe15b50cSPaul E. McKenney 
1999fe15b50cSPaul E. McKenney 	switch (val) {
2000fe15b50cSPaul E. McKenney 	case MODULE_STATE_COMING:
2001fe15b50cSPaul E. McKenney 		ret = srcu_module_coming(mod);
2002fe15b50cSPaul E. McKenney 		break;
2003fe15b50cSPaul E. McKenney 	case MODULE_STATE_GOING:
2004fe15b50cSPaul E. McKenney 		srcu_module_going(mod);
2005fe15b50cSPaul E. McKenney 		break;
2006fe15b50cSPaul E. McKenney 	default:
2007fe15b50cSPaul E. McKenney 		break;
2008fe15b50cSPaul E. McKenney 	}
2009fe15b50cSPaul E. McKenney 	return ret;
2010fe15b50cSPaul E. McKenney }
2011fe15b50cSPaul E. McKenney 
2012fe15b50cSPaul E. McKenney static struct notifier_block srcu_module_nb = {
2013fe15b50cSPaul E. McKenney 	.notifier_call = srcu_module_notify,
2014fe15b50cSPaul E. McKenney 	.priority = 0,
2015fe15b50cSPaul E. McKenney };
2016fe15b50cSPaul E. McKenney 
init_srcu_module_notifier(void)2017fe15b50cSPaul E. McKenney static __init int init_srcu_module_notifier(void)
2018fe15b50cSPaul E. McKenney {
2019fe15b50cSPaul E. McKenney 	int ret;
2020fe15b50cSPaul E. McKenney 
2021fe15b50cSPaul E. McKenney 	ret = register_module_notifier(&srcu_module_nb);
2022fe15b50cSPaul E. McKenney 	if (ret)
2023fe15b50cSPaul E. McKenney 		pr_warn("Failed to register srcu module notifier\n");
2024fe15b50cSPaul E. McKenney 	return ret;
2025fe15b50cSPaul E. McKenney }
2026fe15b50cSPaul E. McKenney late_initcall(init_srcu_module_notifier);
2027fe15b50cSPaul E. McKenney 
2028fe15b50cSPaul E. McKenney #endif /* #ifdef CONFIG_MODULES */
2029