xref: /linux-6.15/kernel/rcu/srcutree.c (revision 8f870e6e)
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 <[email protected]>
9dad81a20SPaul E. McKenney  *	   Lai Jiangshan <[email protected]>
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  */
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;
1382ec30311SPaul E. McKenney 		sdp->srcu_gp_seq_needed = ssp->srcu_gp_seq;
1392ec30311SPaul E. McKenney 		sdp->srcu_gp_seq_needed_exp = ssp->srcu_gp_seq;
1402ec30311SPaul E. McKenney 		sdp->mynode = NULL;
1412ec30311SPaul E. McKenney 		sdp->cpu = cpu;
1422ec30311SPaul E. McKenney 		INIT_WORK(&sdp->work, srcu_invoke_callbacks);
1432ec30311SPaul E. McKenney 		timer_setup(&sdp->delay_work, srcu_delay_timer, 0);
1442ec30311SPaul E. McKenney 		sdp->ssp = ssp;
1452ec30311SPaul E. McKenney 	}
1462ec30311SPaul E. McKenney }
1472ec30311SPaul E. McKenney 
148cbdc98e9SPaul E. McKenney /* Invalid seq state, used during snp node initialization */
149cbdc98e9SPaul E. McKenney #define SRCU_SNP_INIT_SEQ		0x2
150cbdc98e9SPaul E. McKenney 
151cbdc98e9SPaul E. McKenney /*
152cbdc98e9SPaul E. McKenney  * Check whether sequence number corresponding to snp node,
153cbdc98e9SPaul E. McKenney  * is invalid.
154cbdc98e9SPaul E. McKenney  */
155cbdc98e9SPaul E. McKenney static inline bool srcu_invl_snp_seq(unsigned long s)
156cbdc98e9SPaul E. McKenney {
157cbdc98e9SPaul E. McKenney 	return rcu_seq_state(s) == SRCU_SNP_INIT_SEQ;
158cbdc98e9SPaul E. McKenney }
159cbdc98e9SPaul E. McKenney 
1602ec30311SPaul E. McKenney /*
1612ec30311SPaul E. McKenney  * Allocated and initialize SRCU combining tree.  Returns @true if
1622ec30311SPaul E. McKenney  * allocation succeeded and @false otherwise.
1632ec30311SPaul E. McKenney  */
164c69a00a1SPaul E. McKenney static bool init_srcu_struct_nodes(struct srcu_struct *ssp, gfp_t gfp_flags)
165dad81a20SPaul E. McKenney {
166da915ad5SPaul E. McKenney 	int cpu;
167da915ad5SPaul E. McKenney 	int i;
168da915ad5SPaul E. McKenney 	int level = 0;
169da915ad5SPaul E. McKenney 	int levelspread[RCU_NUM_LVLS];
170da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
171da915ad5SPaul E. McKenney 	struct srcu_node *snp;
172da915ad5SPaul E. McKenney 	struct srcu_node *snp_first;
173da915ad5SPaul E. McKenney 
174b5befe84SFrederic Weisbecker 	/* Initialize geometry if it has not already been initialized. */
175b5befe84SFrederic Weisbecker 	rcu_init_geometry();
176c69a00a1SPaul E. McKenney 	ssp->node = kcalloc(rcu_num_nodes, sizeof(*ssp->node), gfp_flags);
1772ec30311SPaul E. McKenney 	if (!ssp->node)
1782ec30311SPaul E. McKenney 		return false;
179b5befe84SFrederic Weisbecker 
180da915ad5SPaul E. McKenney 	/* Work out the overall tree geometry. */
181aacb5d91SPaul E. McKenney 	ssp->level[0] = &ssp->node[0];
182da915ad5SPaul E. McKenney 	for (i = 1; i < rcu_num_lvls; i++)
183aacb5d91SPaul E. McKenney 		ssp->level[i] = ssp->level[i - 1] + num_rcu_lvl[i - 1];
184da915ad5SPaul E. McKenney 	rcu_init_levelspread(levelspread, num_rcu_lvl);
185da915ad5SPaul E. McKenney 
186da915ad5SPaul E. McKenney 	/* Each pass through this loop initializes one srcu_node structure. */
187aacb5d91SPaul E. McKenney 	srcu_for_each_node_breadth_first(ssp, snp) {
188d6331980SPaul E. McKenney 		spin_lock_init(&ACCESS_PRIVATE(snp, lock));
189c7e88067SPaul E. McKenney 		WARN_ON_ONCE(ARRAY_SIZE(snp->srcu_have_cbs) !=
190c7e88067SPaul E. McKenney 			     ARRAY_SIZE(snp->srcu_data_have_cbs));
191c7e88067SPaul E. McKenney 		for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {
192cbdc98e9SPaul E. McKenney 			snp->srcu_have_cbs[i] = SRCU_SNP_INIT_SEQ;
193c7e88067SPaul E. McKenney 			snp->srcu_data_have_cbs[i] = 0;
194c7e88067SPaul E. McKenney 		}
195cbdc98e9SPaul E. McKenney 		snp->srcu_gp_seq_needed_exp = SRCU_SNP_INIT_SEQ;
196da915ad5SPaul E. McKenney 		snp->grplo = -1;
197da915ad5SPaul E. McKenney 		snp->grphi = -1;
198aacb5d91SPaul E. McKenney 		if (snp == &ssp->node[0]) {
199da915ad5SPaul E. McKenney 			/* Root node, special case. */
200da915ad5SPaul E. McKenney 			snp->srcu_parent = NULL;
201da915ad5SPaul E. McKenney 			continue;
202da915ad5SPaul E. McKenney 		}
203da915ad5SPaul E. McKenney 
204da915ad5SPaul E. McKenney 		/* Non-root node. */
205aacb5d91SPaul E. McKenney 		if (snp == ssp->level[level + 1])
206da915ad5SPaul E. McKenney 			level++;
207aacb5d91SPaul E. McKenney 		snp->srcu_parent = ssp->level[level - 1] +
208aacb5d91SPaul E. McKenney 				   (snp - ssp->level[level]) /
209da915ad5SPaul E. McKenney 				   levelspread[level - 1];
210da915ad5SPaul E. McKenney 	}
211da915ad5SPaul E. McKenney 
212da915ad5SPaul E. McKenney 	/*
213da915ad5SPaul E. McKenney 	 * Initialize the per-CPU srcu_data array, which feeds into the
214da915ad5SPaul E. McKenney 	 * leaves of the srcu_node tree.
215da915ad5SPaul E. McKenney 	 */
216da915ad5SPaul E. McKenney 	level = rcu_num_lvls - 1;
217aacb5d91SPaul E. McKenney 	snp_first = ssp->level[level];
218da915ad5SPaul E. McKenney 	for_each_possible_cpu(cpu) {
219aacb5d91SPaul E. McKenney 		sdp = per_cpu_ptr(ssp->sda, cpu);
220da915ad5SPaul E. McKenney 		sdp->mynode = &snp_first[cpu / levelspread[level]];
221da915ad5SPaul E. McKenney 		for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {
222da915ad5SPaul E. McKenney 			if (snp->grplo < 0)
223da915ad5SPaul E. McKenney 				snp->grplo = cpu;
224da915ad5SPaul E. McKenney 			snp->grphi = cpu;
225da915ad5SPaul E. McKenney 		}
226c7e88067SPaul E. McKenney 		sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
227da915ad5SPaul E. McKenney 	}
228994f7068SPaul E. McKenney 	smp_store_release(&ssp->srcu_size_state, SRCU_SIZE_WAIT_BARRIER);
2292ec30311SPaul E. McKenney 	return true;
230da915ad5SPaul E. McKenney }
231da915ad5SPaul E. McKenney 
232da915ad5SPaul E. McKenney /*
233da915ad5SPaul E. McKenney  * Initialize non-compile-time initialized fields, including the
234994f7068SPaul E. McKenney  * associated srcu_node and srcu_data structures.  The is_static parameter
235994f7068SPaul E. McKenney  * tells us that ->sda has already been wired up to srcu_data.
236da915ad5SPaul E. McKenney  */
237aacb5d91SPaul E. McKenney static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static)
238da915ad5SPaul E. McKenney {
239994f7068SPaul E. McKenney 	ssp->srcu_size_state = SRCU_SIZE_SMALL;
2402ec30311SPaul E. McKenney 	ssp->node = NULL;
241aacb5d91SPaul E. McKenney 	mutex_init(&ssp->srcu_cb_mutex);
242aacb5d91SPaul E. McKenney 	mutex_init(&ssp->srcu_gp_mutex);
243aacb5d91SPaul E. McKenney 	ssp->srcu_idx = 0;
244aacb5d91SPaul E. McKenney 	ssp->srcu_gp_seq = 0;
245aacb5d91SPaul E. McKenney 	ssp->srcu_barrier_seq = 0;
246aacb5d91SPaul E. McKenney 	mutex_init(&ssp->srcu_barrier_mutex);
247aacb5d91SPaul E. McKenney 	atomic_set(&ssp->srcu_barrier_cpu_cnt, 0);
248aacb5d91SPaul E. McKenney 	INIT_DELAYED_WORK(&ssp->work, process_srcu);
24946470cf8SPaul E. McKenney 	ssp->sda_is_static = is_static;
250da915ad5SPaul E. McKenney 	if (!is_static)
251aacb5d91SPaul E. McKenney 		ssp->sda = alloc_percpu(struct srcu_data);
25250edb988SPaul E. McKenney 	if (!ssp->sda)
25350edb988SPaul E. McKenney 		return -ENOMEM;
2542ec30311SPaul E. McKenney 	init_srcu_struct_data(ssp);
255c69a00a1SPaul E. McKenney 	ssp->srcu_gp_seq_needed_exp = 0;
256c69a00a1SPaul E. McKenney 	ssp->srcu_last_gp_end = ktime_get_mono_fast_ns();
2579f2e91d9SPaul E. McKenney 	if (READ_ONCE(ssp->srcu_size_state) == SRCU_SIZE_SMALL && SRCU_SIZING_IS_INIT()) {
258c69a00a1SPaul E. McKenney 		if (!init_srcu_struct_nodes(ssp, GFP_ATOMIC)) {
25946470cf8SPaul E. McKenney 			if (!ssp->sda_is_static) {
2602ec30311SPaul E. McKenney 				free_percpu(ssp->sda);
2612ec30311SPaul E. McKenney 				ssp->sda = NULL;
2622ec30311SPaul E. McKenney 				return -ENOMEM;
2632ec30311SPaul E. McKenney 			}
2642ec30311SPaul E. McKenney 		} else {
265c69a00a1SPaul E. McKenney 			WRITE_ONCE(ssp->srcu_size_state, SRCU_SIZE_BIG);
2662ec30311SPaul E. McKenney 		}
267c69a00a1SPaul E. McKenney 	}
268aacb5d91SPaul E. McKenney 	smp_store_release(&ssp->srcu_gp_seq_needed, 0); /* Init done. */
26950edb988SPaul E. McKenney 	return 0;
270dad81a20SPaul E. McKenney }
271dad81a20SPaul E. McKenney 
272dad81a20SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
273dad81a20SPaul E. McKenney 
274aacb5d91SPaul E. McKenney int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
275dad81a20SPaul E. McKenney 		       struct lock_class_key *key)
276dad81a20SPaul E. McKenney {
277dad81a20SPaul E. McKenney 	/* Don't re-initialize a lock while it is held. */
278aacb5d91SPaul E. McKenney 	debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));
279aacb5d91SPaul E. McKenney 	lockdep_init_map(&ssp->dep_map, name, key, 0);
280aacb5d91SPaul E. McKenney 	spin_lock_init(&ACCESS_PRIVATE(ssp, lock));
281aacb5d91SPaul E. McKenney 	return init_srcu_struct_fields(ssp, false);
282dad81a20SPaul E. McKenney }
283dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__init_srcu_struct);
284dad81a20SPaul E. McKenney 
285dad81a20SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
286dad81a20SPaul E. McKenney 
287dad81a20SPaul E. McKenney /**
288dad81a20SPaul E. McKenney  * init_srcu_struct - initialize a sleep-RCU structure
289aacb5d91SPaul E. McKenney  * @ssp: structure to initialize.
290dad81a20SPaul E. McKenney  *
291dad81a20SPaul E. McKenney  * Must invoke this on a given srcu_struct before passing that srcu_struct
292dad81a20SPaul E. McKenney  * to any other function.  Each srcu_struct represents a separate domain
293dad81a20SPaul E. McKenney  * of SRCU protection.
294dad81a20SPaul E. McKenney  */
295aacb5d91SPaul E. McKenney int init_srcu_struct(struct srcu_struct *ssp)
296dad81a20SPaul E. McKenney {
297aacb5d91SPaul E. McKenney 	spin_lock_init(&ACCESS_PRIVATE(ssp, lock));
298aacb5d91SPaul E. McKenney 	return init_srcu_struct_fields(ssp, false);
299dad81a20SPaul E. McKenney }
300dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(init_srcu_struct);
301dad81a20SPaul E. McKenney 
302dad81a20SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
303dad81a20SPaul E. McKenney 
304dad81a20SPaul E. McKenney /*
3059f2e91d9SPaul E. McKenney  * Initiate a transition to SRCU_SIZE_BIG with lock held.
3069f2e91d9SPaul E. McKenney  */
3079f2e91d9SPaul E. McKenney static void __srcu_transition_to_big(struct srcu_struct *ssp)
3089f2e91d9SPaul E. McKenney {
3099f2e91d9SPaul E. McKenney 	lockdep_assert_held(&ACCESS_PRIVATE(ssp, lock));
3109f2e91d9SPaul E. McKenney 	smp_store_release(&ssp->srcu_size_state, SRCU_SIZE_ALLOC);
3119f2e91d9SPaul E. McKenney }
3129f2e91d9SPaul E. McKenney 
3139f2e91d9SPaul E. McKenney /*
31499659f64SPaul E. McKenney  * Initiate an idempotent transition to SRCU_SIZE_BIG.
31599659f64SPaul E. McKenney  */
31699659f64SPaul E. McKenney static void srcu_transition_to_big(struct srcu_struct *ssp)
31799659f64SPaul E. McKenney {
31899659f64SPaul E. McKenney 	unsigned long flags;
31999659f64SPaul E. McKenney 
32099659f64SPaul E. McKenney 	/* Double-checked locking on ->srcu_size-state. */
32199659f64SPaul E. McKenney 	if (smp_load_acquire(&ssp->srcu_size_state) != SRCU_SIZE_SMALL)
32299659f64SPaul E. McKenney 		return;
32399659f64SPaul E. McKenney 	spin_lock_irqsave_rcu_node(ssp, flags);
32499659f64SPaul E. McKenney 	if (smp_load_acquire(&ssp->srcu_size_state) != SRCU_SIZE_SMALL) {
32599659f64SPaul E. McKenney 		spin_unlock_irqrestore_rcu_node(ssp, flags);
32699659f64SPaul E. McKenney 		return;
32799659f64SPaul E. McKenney 	}
3289f2e91d9SPaul E. McKenney 	__srcu_transition_to_big(ssp);
32999659f64SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(ssp, flags);
33099659f64SPaul E. McKenney }
33199659f64SPaul E. McKenney 
33299659f64SPaul E. McKenney /*
333c2445d38SPaul E. McKenney  * Check to see if the just-encountered contention event justifies
334c2445d38SPaul E. McKenney  * a transition to SRCU_SIZE_BIG.
3359f2e91d9SPaul E. McKenney  */
336c2445d38SPaul E. McKenney static void spin_lock_irqsave_check_contention(struct srcu_struct *ssp)
3379f2e91d9SPaul E. McKenney {
3389f2e91d9SPaul E. McKenney 	unsigned long j;
3399f2e91d9SPaul E. McKenney 
3409f2e91d9SPaul E. McKenney 	if (!SRCU_SIZING_IS_CONTEND() || ssp->srcu_size_state)
3419f2e91d9SPaul E. McKenney 		return;
3429f2e91d9SPaul E. McKenney 	j = jiffies;
3439f2e91d9SPaul E. McKenney 	if (ssp->srcu_size_jiffies != j) {
3449f2e91d9SPaul E. McKenney 		ssp->srcu_size_jiffies = j;
3459f2e91d9SPaul E. McKenney 		ssp->srcu_n_lock_retries = 0;
3469f2e91d9SPaul E. McKenney 	}
3479f2e91d9SPaul E. McKenney 	if (++ssp->srcu_n_lock_retries <= small_contention_lim)
3489f2e91d9SPaul E. McKenney 		return;
3499f2e91d9SPaul E. McKenney 	__srcu_transition_to_big(ssp);
3509f2e91d9SPaul E. McKenney }
3519f2e91d9SPaul E. McKenney 
3529f2e91d9SPaul E. McKenney /*
353c2445d38SPaul E. McKenney  * Acquire the specified srcu_data structure's ->lock, but check for
354c2445d38SPaul E. McKenney  * excessive contention, which results in initiation of a transition
355c2445d38SPaul E. McKenney  * to SRCU_SIZE_BIG.  But only if the srcutree.convert_to_big module
356c2445d38SPaul E. McKenney  * parameter permits this.
357c2445d38SPaul E. McKenney  */
358c2445d38SPaul E. McKenney static void spin_lock_irqsave_sdp_contention(struct srcu_data *sdp, unsigned long *flags)
359c2445d38SPaul E. McKenney {
360c2445d38SPaul E. McKenney 	struct srcu_struct *ssp = sdp->ssp;
361c2445d38SPaul E. McKenney 
362c2445d38SPaul E. McKenney 	if (spin_trylock_irqsave_rcu_node(sdp, *flags))
363c2445d38SPaul E. McKenney 		return;
364c2445d38SPaul E. McKenney 	spin_lock_irqsave_rcu_node(ssp, *flags);
365c2445d38SPaul E. McKenney 	spin_lock_irqsave_check_contention(ssp);
366c2445d38SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(ssp, *flags);
367c2445d38SPaul E. McKenney 	spin_lock_irqsave_rcu_node(sdp, *flags);
368c2445d38SPaul E. McKenney }
369c2445d38SPaul E. McKenney 
370c2445d38SPaul E. McKenney /*
371c2445d38SPaul E. McKenney  * Acquire the specified srcu_struct structure's ->lock, but check for
372c2445d38SPaul E. McKenney  * excessive contention, which results in initiation of a transition
373c2445d38SPaul E. McKenney  * to SRCU_SIZE_BIG.  But only if the srcutree.convert_to_big module
374c2445d38SPaul E. McKenney  * parameter permits this.
375c2445d38SPaul E. McKenney  */
376c2445d38SPaul E. McKenney static void spin_lock_irqsave_ssp_contention(struct srcu_struct *ssp, unsigned long *flags)
377c2445d38SPaul E. McKenney {
378c2445d38SPaul E. McKenney 	if (spin_trylock_irqsave_rcu_node(ssp, *flags))
379c2445d38SPaul E. McKenney 		return;
380c2445d38SPaul E. McKenney 	spin_lock_irqsave_rcu_node(ssp, *flags);
381c2445d38SPaul E. McKenney 	spin_lock_irqsave_check_contention(ssp);
382c2445d38SPaul E. McKenney }
383c2445d38SPaul E. McKenney 
384c2445d38SPaul E. McKenney /*
385da915ad5SPaul E. McKenney  * First-use initialization of statically allocated srcu_struct
386da915ad5SPaul E. McKenney  * structure.  Wiring up the combining tree is more than can be
387da915ad5SPaul E. McKenney  * done with compile-time initialization, so this check is added
388aacb5d91SPaul E. McKenney  * to each update-side SRCU primitive.  Use ssp->lock, which -is-
389da915ad5SPaul E. McKenney  * compile-time initialized, to resolve races involving multiple
390da915ad5SPaul E. McKenney  * CPUs trying to garner first-use privileges.
391da915ad5SPaul E. McKenney  */
392aacb5d91SPaul E. McKenney static void check_init_srcu_struct(struct srcu_struct *ssp)
393da915ad5SPaul E. McKenney {
394da915ad5SPaul E. McKenney 	unsigned long flags;
395da915ad5SPaul E. McKenney 
396da915ad5SPaul E. McKenney 	/* The smp_load_acquire() pairs with the smp_store_release(). */
397aacb5d91SPaul E. McKenney 	if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_gp_seq_needed))) /*^^^*/
398da915ad5SPaul E. McKenney 		return; /* Already initialized. */
399aacb5d91SPaul E. McKenney 	spin_lock_irqsave_rcu_node(ssp, flags);
400aacb5d91SPaul E. McKenney 	if (!rcu_seq_state(ssp->srcu_gp_seq_needed)) {
401aacb5d91SPaul E. McKenney 		spin_unlock_irqrestore_rcu_node(ssp, flags);
402da915ad5SPaul E. McKenney 		return;
403da915ad5SPaul E. McKenney 	}
404aacb5d91SPaul E. McKenney 	init_srcu_struct_fields(ssp, true);
405aacb5d91SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(ssp, flags);
406da915ad5SPaul E. McKenney }
407da915ad5SPaul E. McKenney 
408da915ad5SPaul E. McKenney /*
409da915ad5SPaul E. McKenney  * Returns approximate total of the readers' ->srcu_lock_count[] values
410da915ad5SPaul E. McKenney  * for the rank of per-CPU counters specified by idx.
411dad81a20SPaul E. McKenney  */
412aacb5d91SPaul E. McKenney static unsigned long srcu_readers_lock_idx(struct srcu_struct *ssp, int idx)
413dad81a20SPaul E. McKenney {
414dad81a20SPaul E. McKenney 	int cpu;
415dad81a20SPaul E. McKenney 	unsigned long sum = 0;
416dad81a20SPaul E. McKenney 
417dad81a20SPaul E. McKenney 	for_each_possible_cpu(cpu) {
418aacb5d91SPaul E. McKenney 		struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
419dad81a20SPaul E. McKenney 
420da915ad5SPaul E. McKenney 		sum += READ_ONCE(cpuc->srcu_lock_count[idx]);
421dad81a20SPaul E. McKenney 	}
422dad81a20SPaul E. McKenney 	return sum;
423dad81a20SPaul E. McKenney }
424dad81a20SPaul E. McKenney 
425dad81a20SPaul E. McKenney /*
426da915ad5SPaul E. McKenney  * Returns approximate total of the readers' ->srcu_unlock_count[] values
427da915ad5SPaul E. McKenney  * for the rank of per-CPU counters specified by idx.
428dad81a20SPaul E. McKenney  */
429aacb5d91SPaul E. McKenney static unsigned long srcu_readers_unlock_idx(struct srcu_struct *ssp, int idx)
430dad81a20SPaul E. McKenney {
431dad81a20SPaul E. McKenney 	int cpu;
432dad81a20SPaul E. McKenney 	unsigned long sum = 0;
433dad81a20SPaul E. McKenney 
434dad81a20SPaul E. McKenney 	for_each_possible_cpu(cpu) {
435aacb5d91SPaul E. McKenney 		struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
436dad81a20SPaul E. McKenney 
437da915ad5SPaul E. McKenney 		sum += READ_ONCE(cpuc->srcu_unlock_count[idx]);
438dad81a20SPaul E. McKenney 	}
439dad81a20SPaul E. McKenney 	return sum;
440dad81a20SPaul E. McKenney }
441dad81a20SPaul E. McKenney 
442dad81a20SPaul E. McKenney /*
443dad81a20SPaul E. McKenney  * Return true if the number of pre-existing readers is determined to
444dad81a20SPaul E. McKenney  * be zero.
445dad81a20SPaul E. McKenney  */
446aacb5d91SPaul E. McKenney static bool srcu_readers_active_idx_check(struct srcu_struct *ssp, int idx)
447dad81a20SPaul E. McKenney {
448dad81a20SPaul E. McKenney 	unsigned long unlocks;
449dad81a20SPaul E. McKenney 
450aacb5d91SPaul E. McKenney 	unlocks = srcu_readers_unlock_idx(ssp, idx);
451dad81a20SPaul E. McKenney 
452dad81a20SPaul E. McKenney 	/*
453dad81a20SPaul E. McKenney 	 * Make sure that a lock is always counted if the corresponding
454dad81a20SPaul E. McKenney 	 * unlock is counted. Needs to be a smp_mb() as the read side may
455dad81a20SPaul E. McKenney 	 * contain a read from a variable that is written to before the
456dad81a20SPaul E. McKenney 	 * synchronize_srcu() in the write side. In this case smp_mb()s
457dad81a20SPaul E. McKenney 	 * A and B act like the store buffering pattern.
458dad81a20SPaul E. McKenney 	 *
459dad81a20SPaul E. McKenney 	 * This smp_mb() also pairs with smp_mb() C to prevent accesses
460dad81a20SPaul E. McKenney 	 * after the synchronize_srcu() from being executed before the
461dad81a20SPaul E. McKenney 	 * grace period ends.
462dad81a20SPaul E. McKenney 	 */
463dad81a20SPaul E. McKenney 	smp_mb(); /* A */
464dad81a20SPaul E. McKenney 
465dad81a20SPaul E. McKenney 	/*
466dad81a20SPaul E. McKenney 	 * If the locks are the same as the unlocks, then there must have
467dad81a20SPaul E. McKenney 	 * been no readers on this index at some time in between. This does
468dad81a20SPaul E. McKenney 	 * not mean that there are no more readers, as one could have read
469dad81a20SPaul E. McKenney 	 * the current index but not have incremented the lock counter yet.
470dad81a20SPaul E. McKenney 	 *
471881ec9d2SPaul E. McKenney 	 * So suppose that the updater is preempted here for so long
472881ec9d2SPaul E. McKenney 	 * that more than ULONG_MAX non-nested readers come and go in
473881ec9d2SPaul E. McKenney 	 * the meantime.  It turns out that this cannot result in overflow
474881ec9d2SPaul E. McKenney 	 * because if a reader modifies its unlock count after we read it
475881ec9d2SPaul E. McKenney 	 * above, then that reader's next load of ->srcu_idx is guaranteed
476881ec9d2SPaul E. McKenney 	 * to get the new value, which will cause it to operate on the
477881ec9d2SPaul E. McKenney 	 * other bank of counters, where it cannot contribute to the
478881ec9d2SPaul E. McKenney 	 * overflow of these counters.  This means that there is a maximum
479881ec9d2SPaul E. McKenney 	 * of 2*NR_CPUS increments, which cannot overflow given current
480881ec9d2SPaul E. McKenney 	 * systems, especially not on 64-bit systems.
481881ec9d2SPaul E. McKenney 	 *
482881ec9d2SPaul E. McKenney 	 * OK, how about nesting?  This does impose a limit on nesting
483881ec9d2SPaul E. McKenney 	 * of floor(ULONG_MAX/NR_CPUS/2), which should be sufficient,
484881ec9d2SPaul E. McKenney 	 * especially on 64-bit systems.
485dad81a20SPaul E. McKenney 	 */
486aacb5d91SPaul E. McKenney 	return srcu_readers_lock_idx(ssp, idx) == unlocks;
487dad81a20SPaul E. McKenney }
488dad81a20SPaul E. McKenney 
489dad81a20SPaul E. McKenney /**
490dad81a20SPaul E. McKenney  * srcu_readers_active - returns true if there are readers. and false
491dad81a20SPaul E. McKenney  *                       otherwise
492aacb5d91SPaul E. McKenney  * @ssp: which srcu_struct to count active readers (holding srcu_read_lock).
493dad81a20SPaul E. McKenney  *
494dad81a20SPaul E. McKenney  * Note that this is not an atomic primitive, and can therefore suffer
495dad81a20SPaul E. McKenney  * severe errors when invoked on an active srcu_struct.  That said, it
496dad81a20SPaul E. McKenney  * can be useful as an error check at cleanup time.
497dad81a20SPaul E. McKenney  */
498aacb5d91SPaul E. McKenney static bool srcu_readers_active(struct srcu_struct *ssp)
499dad81a20SPaul E. McKenney {
500dad81a20SPaul E. McKenney 	int cpu;
501dad81a20SPaul E. McKenney 	unsigned long sum = 0;
502dad81a20SPaul E. McKenney 
503dad81a20SPaul E. McKenney 	for_each_possible_cpu(cpu) {
504aacb5d91SPaul E. McKenney 		struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
505dad81a20SPaul E. McKenney 
506da915ad5SPaul E. McKenney 		sum += READ_ONCE(cpuc->srcu_lock_count[0]);
507da915ad5SPaul E. McKenney 		sum += READ_ONCE(cpuc->srcu_lock_count[1]);
508da915ad5SPaul E. McKenney 		sum -= READ_ONCE(cpuc->srcu_unlock_count[0]);
509da915ad5SPaul E. McKenney 		sum -= READ_ONCE(cpuc->srcu_unlock_count[1]);
510dad81a20SPaul E. McKenney 	}
511dad81a20SPaul E. McKenney 	return sum;
512dad81a20SPaul E. McKenney }
513dad81a20SPaul E. McKenney 
514282d8998SPaul E. McKenney #define SRCU_INTERVAL		1	// Base delay if no expedited GPs pending.
515282d8998SPaul E. McKenney #define SRCU_MAX_INTERVAL	10	// Maximum incremental delay from slow readers.
516*8f870e6eSPaul E. McKenney #define SRCU_MAX_NODELAY_PHASE	3	// Maximum per-GP-phase consecutive no-delay instances.
517282d8998SPaul E. McKenney #define SRCU_MAX_NODELAY	100	// Maximum consecutive no-delay instances.
518dad81a20SPaul E. McKenney 
5191e9a038bSPaul E. McKenney /*
5201e9a038bSPaul E. McKenney  * Return grace-period delay, zero if there are expedited grace
5211e9a038bSPaul E. McKenney  * periods pending, SRCU_INTERVAL otherwise.
5221e9a038bSPaul E. McKenney  */
523aacb5d91SPaul E. McKenney static unsigned long srcu_get_delay(struct srcu_struct *ssp)
5241e9a038bSPaul E. McKenney {
525*8f870e6eSPaul E. McKenney 	unsigned long gpstart;
526*8f870e6eSPaul E. McKenney 	unsigned long j;
527282d8998SPaul E. McKenney 	unsigned long jbase = SRCU_INTERVAL;
528282d8998SPaul E. McKenney 
529cbdc98e9SPaul E. McKenney 	if (ULONG_CMP_LT(READ_ONCE(ssp->srcu_gp_seq), READ_ONCE(ssp->srcu_gp_seq_needed_exp)))
530282d8998SPaul E. McKenney 		jbase = 0;
531*8f870e6eSPaul E. McKenney 	if (rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq))) {
532*8f870e6eSPaul E. McKenney 		j = jiffies - 1;
533*8f870e6eSPaul E. McKenney 		gpstart = READ_ONCE(ssp->srcu_gp_start);
534*8f870e6eSPaul E. McKenney 		if (time_after(j, gpstart))
535*8f870e6eSPaul E. McKenney 			jbase += j - gpstart;
536282d8998SPaul E. McKenney 		if (!jbase) {
537282d8998SPaul E. McKenney 			WRITE_ONCE(ssp->srcu_n_exp_nodelay, READ_ONCE(ssp->srcu_n_exp_nodelay) + 1);
538282d8998SPaul E. McKenney 			if (READ_ONCE(ssp->srcu_n_exp_nodelay) > SRCU_MAX_NODELAY_PHASE)
539282d8998SPaul E. McKenney 				jbase = 1;
540282d8998SPaul E. McKenney 		}
541*8f870e6eSPaul E. McKenney 	}
542282d8998SPaul E. McKenney 	return jbase > SRCU_MAX_INTERVAL ? SRCU_MAX_INTERVAL : jbase;
5431e9a038bSPaul E. McKenney }
5441e9a038bSPaul E. McKenney 
545f5ad3991SPaul E. McKenney /**
546f5ad3991SPaul E. McKenney  * cleanup_srcu_struct - deconstruct a sleep-RCU structure
547f5ad3991SPaul E. McKenney  * @ssp: structure to clean up.
548f5ad3991SPaul E. McKenney  *
549f5ad3991SPaul E. McKenney  * Must invoke this after you are finished using a given srcu_struct that
550f5ad3991SPaul E. McKenney  * was initialized via init_srcu_struct(), else you leak memory.
551f5ad3991SPaul E. McKenney  */
552f5ad3991SPaul E. McKenney void cleanup_srcu_struct(struct srcu_struct *ssp)
553dad81a20SPaul E. McKenney {
554da915ad5SPaul E. McKenney 	int cpu;
555da915ad5SPaul E. McKenney 
556aacb5d91SPaul E. McKenney 	if (WARN_ON(!srcu_get_delay(ssp)))
557f7194ac3SPaul E. McKenney 		return; /* Just leak it! */
558aacb5d91SPaul E. McKenney 	if (WARN_ON(srcu_readers_active(ssp)))
559f7194ac3SPaul E. McKenney 		return; /* Just leak it! */
560aacb5d91SPaul E. McKenney 	flush_delayed_work(&ssp->work);
561e81baf4cSSebastian Andrzej Siewior 	for_each_possible_cpu(cpu) {
562e81baf4cSSebastian Andrzej Siewior 		struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
563e81baf4cSSebastian Andrzej Siewior 
564e81baf4cSSebastian Andrzej Siewior 		del_timer_sync(&sdp->delay_work);
565e81baf4cSSebastian Andrzej Siewior 		flush_work(&sdp->work);
5665cdfd174SPaul E. McKenney 		if (WARN_ON(rcu_segcblist_n_cbs(&sdp->srcu_cblist)))
5675cdfd174SPaul E. McKenney 			return; /* Forgot srcu_barrier(), so just leak it! */
568f7194ac3SPaul E. McKenney 	}
569aacb5d91SPaul E. McKenney 	if (WARN_ON(rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
5708ed00760SPaul E. McKenney 	    WARN_ON(rcu_seq_current(&ssp->srcu_gp_seq) != ssp->srcu_gp_seq_needed) ||
571aacb5d91SPaul E. McKenney 	    WARN_ON(srcu_readers_active(ssp))) {
5728ed00760SPaul E. McKenney 		pr_info("%s: Active srcu_struct %p read state: %d gp state: %lu/%lu\n",
5738ed00760SPaul E. McKenney 			__func__, ssp, rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)),
5748ed00760SPaul E. McKenney 			rcu_seq_current(&ssp->srcu_gp_seq), ssp->srcu_gp_seq_needed);
575dad81a20SPaul E. McKenney 		return; /* Caller forgot to stop doing call_srcu()? */
576dad81a20SPaul E. McKenney 	}
57746470cf8SPaul E. McKenney 	if (!ssp->sda_is_static) {
578aacb5d91SPaul E. McKenney 		free_percpu(ssp->sda);
579aacb5d91SPaul E. McKenney 		ssp->sda = NULL;
58046470cf8SPaul E. McKenney 	}
5812ec30311SPaul E. McKenney 	kfree(ssp->node);
5822ec30311SPaul E. McKenney 	ssp->node = NULL;
583994f7068SPaul E. McKenney 	ssp->srcu_size_state = SRCU_SIZE_SMALL;
584dad81a20SPaul E. McKenney }
585f5ad3991SPaul E. McKenney EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
586dad81a20SPaul E. McKenney 
587dad81a20SPaul E. McKenney /*
588dad81a20SPaul E. McKenney  * Counts the new reader in the appropriate per-CPU element of the
589cdf7abc4SPaolo Bonzini  * srcu_struct.
590dad81a20SPaul E. McKenney  * Returns an index that must be passed to the matching srcu_read_unlock().
591dad81a20SPaul E. McKenney  */
592aacb5d91SPaul E. McKenney int __srcu_read_lock(struct srcu_struct *ssp)
593dad81a20SPaul E. McKenney {
594dad81a20SPaul E. McKenney 	int idx;
595dad81a20SPaul E. McKenney 
596aacb5d91SPaul E. McKenney 	idx = READ_ONCE(ssp->srcu_idx) & 0x1;
597aacb5d91SPaul E. McKenney 	this_cpu_inc(ssp->sda->srcu_lock_count[idx]);
598dad81a20SPaul E. McKenney 	smp_mb(); /* B */  /* Avoid leaking the critical section. */
599dad81a20SPaul E. McKenney 	return idx;
600dad81a20SPaul E. McKenney }
601dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_lock);
602dad81a20SPaul E. McKenney 
603dad81a20SPaul E. McKenney /*
604dad81a20SPaul E. McKenney  * Removes the count for the old reader from the appropriate per-CPU
605dad81a20SPaul E. McKenney  * element of the srcu_struct.  Note that this may well be a different
606dad81a20SPaul E. McKenney  * CPU than that which was incremented by the corresponding srcu_read_lock().
607dad81a20SPaul E. McKenney  */
608aacb5d91SPaul E. McKenney void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
609dad81a20SPaul E. McKenney {
610dad81a20SPaul E. McKenney 	smp_mb(); /* C */  /* Avoid leaking the critical section. */
611aacb5d91SPaul E. McKenney 	this_cpu_inc(ssp->sda->srcu_unlock_count[idx]);
612dad81a20SPaul E. McKenney }
613dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(__srcu_read_unlock);
614dad81a20SPaul E. McKenney 
615dad81a20SPaul E. McKenney /*
616dad81a20SPaul E. McKenney  * We use an adaptive strategy for synchronize_srcu() and especially for
617dad81a20SPaul E. McKenney  * synchronize_srcu_expedited().  We spin for a fixed time period
618dad81a20SPaul E. McKenney  * (defined below) to allow SRCU readers to exit their read-side critical
619dad81a20SPaul E. McKenney  * sections.  If there are still some readers after a few microseconds,
620dad81a20SPaul E. McKenney  * we repeatedly block for 1-millisecond time periods.
621dad81a20SPaul E. McKenney  */
622dad81a20SPaul E. McKenney #define SRCU_RETRY_CHECK_DELAY		5
623dad81a20SPaul E. McKenney 
624dad81a20SPaul E. McKenney /*
625dad81a20SPaul E. McKenney  * Start an SRCU grace period.
626dad81a20SPaul E. McKenney  */
627aacb5d91SPaul E. McKenney static void srcu_gp_start(struct srcu_struct *ssp)
628dad81a20SPaul E. McKenney {
629586e31d5SLukas Bulwahn 	struct srcu_data *sdp;
630dad81a20SPaul E. McKenney 	int state;
631dad81a20SPaul E. McKenney 
632994f7068SPaul E. McKenney 	if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
633994f7068SPaul E. McKenney 		sdp = per_cpu_ptr(ssp->sda, 0);
634994f7068SPaul E. McKenney 	else
635994f7068SPaul E. McKenney 		sdp = this_cpu_ptr(ssp->sda);
636aacb5d91SPaul E. McKenney 	lockdep_assert_held(&ACCESS_PRIVATE(ssp, lock));
637aacb5d91SPaul E. McKenney 	WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed));
638eb4c2382SDennis Krein 	spin_lock_rcu_node(sdp);  /* Interrupts already disabled. */
639da915ad5SPaul E. McKenney 	rcu_segcblist_advance(&sdp->srcu_cblist,
640aacb5d91SPaul E. McKenney 			      rcu_seq_current(&ssp->srcu_gp_seq));
641da915ad5SPaul E. McKenney 	(void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
642aacb5d91SPaul E. McKenney 				       rcu_seq_snap(&ssp->srcu_gp_seq));
643eb4c2382SDennis Krein 	spin_unlock_rcu_node(sdp);  /* Interrupts remain disabled. */
644282d8998SPaul E. McKenney 	WRITE_ONCE(ssp->srcu_gp_start, jiffies);
645282d8998SPaul E. McKenney 	WRITE_ONCE(ssp->srcu_n_exp_nodelay, 0);
6462da4b2a7SPaul E. McKenney 	smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
647aacb5d91SPaul E. McKenney 	rcu_seq_start(&ssp->srcu_gp_seq);
64871042606SPaul E. McKenney 	state = rcu_seq_state(ssp->srcu_gp_seq);
649dad81a20SPaul E. McKenney 	WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
650dad81a20SPaul E. McKenney }
651dad81a20SPaul E. McKenney 
652da915ad5SPaul E. McKenney 
653e81baf4cSSebastian Andrzej Siewior static void srcu_delay_timer(struct timer_list *t)
654da915ad5SPaul E. McKenney {
655e81baf4cSSebastian Andrzej Siewior 	struct srcu_data *sdp = container_of(t, struct srcu_data, delay_work);
656e81baf4cSSebastian Andrzej Siewior 
657e81baf4cSSebastian Andrzej Siewior 	queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work);
658da915ad5SPaul E. McKenney }
659da915ad5SPaul E. McKenney 
660e81baf4cSSebastian Andrzej Siewior static void srcu_queue_delayed_work_on(struct srcu_data *sdp,
661da915ad5SPaul E. McKenney 				       unsigned long delay)
662da915ad5SPaul E. McKenney {
663e81baf4cSSebastian Andrzej Siewior 	if (!delay) {
664e81baf4cSSebastian Andrzej Siewior 		queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work);
665e81baf4cSSebastian Andrzej Siewior 		return;
666e81baf4cSSebastian Andrzej Siewior 	}
667da915ad5SPaul E. McKenney 
668e81baf4cSSebastian Andrzej Siewior 	timer_reduce(&sdp->delay_work, jiffies + delay);
669da915ad5SPaul E. McKenney }
670da915ad5SPaul E. McKenney 
671da915ad5SPaul E. McKenney /*
672da915ad5SPaul E. McKenney  * Schedule callback invocation for the specified srcu_data structure,
673da915ad5SPaul E. McKenney  * if possible, on the corresponding CPU.
674da915ad5SPaul E. McKenney  */
675da915ad5SPaul E. McKenney static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
676da915ad5SPaul E. McKenney {
677e81baf4cSSebastian Andrzej Siewior 	srcu_queue_delayed_work_on(sdp, delay);
678da915ad5SPaul E. McKenney }
679da915ad5SPaul E. McKenney 
680da915ad5SPaul E. McKenney /*
681da915ad5SPaul E. McKenney  * Schedule callback invocation for all srcu_data structures associated
682c7e88067SPaul E. McKenney  * with the specified srcu_node structure that have callbacks for the
683c7e88067SPaul E. McKenney  * just-completed grace period, the one corresponding to idx.  If possible,
684c7e88067SPaul E. McKenney  * schedule this invocation on the corresponding CPUs.
685da915ad5SPaul E. McKenney  */
686aacb5d91SPaul E. McKenney static void srcu_schedule_cbs_snp(struct srcu_struct *ssp, struct srcu_node *snp,
6871e9a038bSPaul E. McKenney 				  unsigned long mask, unsigned long delay)
688da915ad5SPaul E. McKenney {
689da915ad5SPaul E. McKenney 	int cpu;
690da915ad5SPaul E. McKenney 
691c7e88067SPaul E. McKenney 	for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
692c7e88067SPaul E. McKenney 		if (!(mask & (1 << (cpu - snp->grplo))))
693c7e88067SPaul E. McKenney 			continue;
694aacb5d91SPaul E. McKenney 		srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, cpu), delay);
695da915ad5SPaul E. McKenney 	}
696c7e88067SPaul E. McKenney }
697da915ad5SPaul E. McKenney 
698da915ad5SPaul E. McKenney /*
699da915ad5SPaul E. McKenney  * Note the end of an SRCU grace period.  Initiates callback invocation
700da915ad5SPaul E. McKenney  * and starts a new grace period if needed.
701da915ad5SPaul E. McKenney  *
702da915ad5SPaul E. McKenney  * The ->srcu_cb_mutex acquisition does not protect any data, but
703da915ad5SPaul E. McKenney  * instead prevents more than one grace period from starting while we
704da915ad5SPaul E. McKenney  * are initiating callback invocation.  This allows the ->srcu_have_cbs[]
705da915ad5SPaul E. McKenney  * array to have a finite number of elements.
706da915ad5SPaul E. McKenney  */
707aacb5d91SPaul E. McKenney static void srcu_gp_end(struct srcu_struct *ssp)
708da915ad5SPaul E. McKenney {
7091e9a038bSPaul E. McKenney 	unsigned long cbdelay;
710da915ad5SPaul E. McKenney 	bool cbs;
7118ddbd883SIldar Ismagilov 	bool last_lvl;
712c350c008SPaul E. McKenney 	int cpu;
713c350c008SPaul E. McKenney 	unsigned long flags;
714da915ad5SPaul E. McKenney 	unsigned long gpseq;
715da915ad5SPaul E. McKenney 	int idx;
716c7e88067SPaul E. McKenney 	unsigned long mask;
717c350c008SPaul E. McKenney 	struct srcu_data *sdp;
718cbdc98e9SPaul E. McKenney 	unsigned long sgsne;
719da915ad5SPaul E. McKenney 	struct srcu_node *snp;
720e2f63836SPaul E. McKenney 	int ss_state;
721da915ad5SPaul E. McKenney 
722da915ad5SPaul E. McKenney 	/* Prevent more than one additional grace period. */
723aacb5d91SPaul E. McKenney 	mutex_lock(&ssp->srcu_cb_mutex);
724da915ad5SPaul E. McKenney 
725da915ad5SPaul E. McKenney 	/* End the current grace period. */
726aacb5d91SPaul E. McKenney 	spin_lock_irq_rcu_node(ssp);
727aacb5d91SPaul E. McKenney 	idx = rcu_seq_state(ssp->srcu_gp_seq);
728da915ad5SPaul E. McKenney 	WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
729282d8998SPaul E. McKenney 	cbdelay = !!srcu_get_delay(ssp);
730844a378dSPaul E. McKenney 	WRITE_ONCE(ssp->srcu_last_gp_end, ktime_get_mono_fast_ns());
731aacb5d91SPaul E. McKenney 	rcu_seq_end(&ssp->srcu_gp_seq);
732aacb5d91SPaul E. McKenney 	gpseq = rcu_seq_current(&ssp->srcu_gp_seq);
733aacb5d91SPaul E. McKenney 	if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, gpseq))
7348c9e0cb3SPaul E. McKenney 		WRITE_ONCE(ssp->srcu_gp_seq_needed_exp, gpseq);
735aacb5d91SPaul E. McKenney 	spin_unlock_irq_rcu_node(ssp);
736aacb5d91SPaul E. McKenney 	mutex_unlock(&ssp->srcu_gp_mutex);
737da915ad5SPaul E. McKenney 	/* A new grace period can start at this point.  But only one. */
738da915ad5SPaul E. McKenney 
739da915ad5SPaul E. McKenney 	/* Initiate callback invocation as needed. */
740c69a00a1SPaul E. McKenney 	ss_state = smp_load_acquire(&ssp->srcu_size_state);
741c69a00a1SPaul E. McKenney 	if (ss_state < SRCU_SIZE_WAIT_BARRIER) {
742994f7068SPaul E. McKenney 		srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, 0), cbdelay);
743994f7068SPaul E. McKenney 	} else {
744da915ad5SPaul E. McKenney 		idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
745aacb5d91SPaul E. McKenney 		srcu_for_each_node_breadth_first(ssp, snp) {
746d6331980SPaul E. McKenney 			spin_lock_irq_rcu_node(snp);
747da915ad5SPaul E. McKenney 			cbs = false;
748aacb5d91SPaul E. McKenney 			last_lvl = snp >= ssp->level[rcu_num_lvls - 1];
7498ddbd883SIldar Ismagilov 			if (last_lvl)
750c69a00a1SPaul E. McKenney 				cbs = ss_state < SRCU_SIZE_BIG || snp->srcu_have_cbs[idx] == gpseq;
751da915ad5SPaul E. McKenney 			snp->srcu_have_cbs[idx] = gpseq;
752da915ad5SPaul E. McKenney 			rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
753cbdc98e9SPaul E. McKenney 			sgsne = snp->srcu_gp_seq_needed_exp;
754cbdc98e9SPaul E. McKenney 			if (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, gpseq))
7557ff8b450SPaul E. McKenney 				WRITE_ONCE(snp->srcu_gp_seq_needed_exp, gpseq);
756c69a00a1SPaul E. McKenney 			if (ss_state < SRCU_SIZE_BIG)
757c69a00a1SPaul E. McKenney 				mask = ~0;
758c69a00a1SPaul E. McKenney 			else
759c7e88067SPaul E. McKenney 				mask = snp->srcu_data_have_cbs[idx];
760c7e88067SPaul E. McKenney 			snp->srcu_data_have_cbs[idx] = 0;
761d6331980SPaul E. McKenney 			spin_unlock_irq_rcu_node(snp);
762a3883df3SPaul E. McKenney 			if (cbs)
763aacb5d91SPaul E. McKenney 				srcu_schedule_cbs_snp(ssp, snp, mask, cbdelay);
764994f7068SPaul E. McKenney 		}
765994f7068SPaul E. McKenney 	}
766c350c008SPaul E. McKenney 
767c350c008SPaul E. McKenney 	/* Occasionally prevent srcu_data counter wrap. */
768994f7068SPaul E. McKenney 	if (!(gpseq & counter_wrap_check))
769994f7068SPaul E. McKenney 		for_each_possible_cpu(cpu) {
770aacb5d91SPaul E. McKenney 			sdp = per_cpu_ptr(ssp->sda, cpu);
771d6331980SPaul E. McKenney 			spin_lock_irqsave_rcu_node(sdp, flags);
772994f7068SPaul E. McKenney 			if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed + 100))
773c350c008SPaul E. McKenney 				sdp->srcu_gp_seq_needed = gpseq;
774994f7068SPaul E. McKenney 			if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed_exp + 100))
775a35d13ecSIldar Ismagilov 				sdp->srcu_gp_seq_needed_exp = gpseq;
776d6331980SPaul E. McKenney 			spin_unlock_irqrestore_rcu_node(sdp, flags);
777c350c008SPaul E. McKenney 		}
778da915ad5SPaul E. McKenney 
779da915ad5SPaul E. McKenney 	/* Callback initiation done, allow grace periods after next. */
780aacb5d91SPaul E. McKenney 	mutex_unlock(&ssp->srcu_cb_mutex);
781da915ad5SPaul E. McKenney 
782da915ad5SPaul E. McKenney 	/* Start a new grace period if needed. */
783aacb5d91SPaul E. McKenney 	spin_lock_irq_rcu_node(ssp);
784aacb5d91SPaul E. McKenney 	gpseq = rcu_seq_current(&ssp->srcu_gp_seq);
785da915ad5SPaul E. McKenney 	if (!rcu_seq_state(gpseq) &&
786aacb5d91SPaul E. McKenney 	    ULONG_CMP_LT(gpseq, ssp->srcu_gp_seq_needed)) {
787aacb5d91SPaul E. McKenney 		srcu_gp_start(ssp);
788aacb5d91SPaul E. McKenney 		spin_unlock_irq_rcu_node(ssp);
789aacb5d91SPaul E. McKenney 		srcu_reschedule(ssp, 0);
790da915ad5SPaul E. McKenney 	} else {
791aacb5d91SPaul E. McKenney 		spin_unlock_irq_rcu_node(ssp);
792da915ad5SPaul E. McKenney 	}
793e2f63836SPaul E. McKenney 
794e2f63836SPaul E. McKenney 	/* Transition to big if needed. */
795e2f63836SPaul E. McKenney 	if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
796e2f63836SPaul E. McKenney 		if (ss_state == SRCU_SIZE_ALLOC)
797c69a00a1SPaul E. McKenney 			init_srcu_struct_nodes(ssp, GFP_KERNEL);
798e2f63836SPaul E. McKenney 		else
799e2f63836SPaul E. McKenney 			smp_store_release(&ssp->srcu_size_state, ss_state + 1);
800e2f63836SPaul E. McKenney 	}
801da915ad5SPaul E. McKenney }
802da915ad5SPaul E. McKenney 
803da915ad5SPaul E. McKenney /*
8041e9a038bSPaul E. McKenney  * Funnel-locking scheme to scalably mediate many concurrent expedited
8051e9a038bSPaul E. McKenney  * grace-period requests.  This function is invoked for the first known
8061e9a038bSPaul E. McKenney  * expedited request for a grace period that has already been requested,
8071e9a038bSPaul E. McKenney  * but without expediting.  To start a completely new grace period,
8081e9a038bSPaul E. McKenney  * whether expedited or not, use srcu_funnel_gp_start() instead.
8091e9a038bSPaul E. McKenney  */
810aacb5d91SPaul E. McKenney static void srcu_funnel_exp_start(struct srcu_struct *ssp, struct srcu_node *snp,
8111e9a038bSPaul E. McKenney 				  unsigned long s)
8121e9a038bSPaul E. McKenney {
8131e9a038bSPaul E. McKenney 	unsigned long flags;
814cbdc98e9SPaul E. McKenney 	unsigned long sgsne;
8151e9a038bSPaul E. McKenney 
816994f7068SPaul E. McKenney 	if (snp)
8171e9a038bSPaul E. McKenney 		for (; snp != NULL; snp = snp->srcu_parent) {
818cbdc98e9SPaul E. McKenney 			sgsne = READ_ONCE(snp->srcu_gp_seq_needed_exp);
819aacb5d91SPaul E. McKenney 			if (rcu_seq_done(&ssp->srcu_gp_seq, s) ||
820cbdc98e9SPaul E. McKenney 			    (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)))
8211e9a038bSPaul E. McKenney 				return;
822d6331980SPaul E. McKenney 			spin_lock_irqsave_rcu_node(snp, flags);
823cbdc98e9SPaul E. McKenney 			sgsne = snp->srcu_gp_seq_needed_exp;
824cbdc98e9SPaul E. McKenney 			if (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)) {
825d6331980SPaul E. McKenney 				spin_unlock_irqrestore_rcu_node(snp, flags);
8261e9a038bSPaul E. McKenney 				return;
8271e9a038bSPaul E. McKenney 			}
8281e9a038bSPaul E. McKenney 			WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
829d6331980SPaul E. McKenney 			spin_unlock_irqrestore_rcu_node(snp, flags);
8301e9a038bSPaul E. McKenney 		}
8319f2e91d9SPaul E. McKenney 	spin_lock_irqsave_ssp_contention(ssp, &flags);
832aacb5d91SPaul E. McKenney 	if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, s))
8338c9e0cb3SPaul E. McKenney 		WRITE_ONCE(ssp->srcu_gp_seq_needed_exp, s);
834aacb5d91SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(ssp, flags);
8351e9a038bSPaul E. McKenney }
8361e9a038bSPaul E. McKenney 
8371e9a038bSPaul E. McKenney /*
838da915ad5SPaul E. McKenney  * Funnel-locking scheme to scalably mediate many concurrent grace-period
839da915ad5SPaul E. McKenney  * requests.  The winner has to do the work of actually starting grace
840da915ad5SPaul E. McKenney  * period s.  Losers must either ensure that their desired grace-period
841da915ad5SPaul E. McKenney  * number is recorded on at least their leaf srcu_node structure, or they
842da915ad5SPaul E. McKenney  * must take steps to invoke their own callbacks.
84317294ce6SPaul E. McKenney  *
84417294ce6SPaul E. McKenney  * Note that this function also does the work of srcu_funnel_exp_start(),
84517294ce6SPaul E. McKenney  * in some cases by directly invoking it.
846da915ad5SPaul E. McKenney  */
847aacb5d91SPaul E. McKenney static void srcu_funnel_gp_start(struct srcu_struct *ssp, struct srcu_data *sdp,
8481e9a038bSPaul E. McKenney 				 unsigned long s, bool do_norm)
849da915ad5SPaul E. McKenney {
850da915ad5SPaul E. McKenney 	unsigned long flags;
851da915ad5SPaul E. McKenney 	int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
852cbdc98e9SPaul E. McKenney 	unsigned long sgsne;
8537b9e9b58SPaul E. McKenney 	struct srcu_node *snp;
8540b56f953SNeeraj Upadhyay 	struct srcu_node *snp_leaf;
855da915ad5SPaul E. McKenney 	unsigned long snp_seq;
856da915ad5SPaul E. McKenney 
8570b56f953SNeeraj Upadhyay 	/* Ensure that snp node tree is fully initialized before traversing it */
8580b56f953SNeeraj Upadhyay 	if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
8590b56f953SNeeraj Upadhyay 		snp_leaf = NULL;
8600b56f953SNeeraj Upadhyay 	else
8610b56f953SNeeraj Upadhyay 		snp_leaf = sdp->mynode;
8620b56f953SNeeraj Upadhyay 
863994f7068SPaul E. McKenney 	if (snp_leaf)
864da915ad5SPaul E. McKenney 		/* Each pass through the loop does one level of the srcu_node tree. */
8657b9e9b58SPaul E. McKenney 		for (snp = snp_leaf; snp != NULL; snp = snp->srcu_parent) {
8667b9e9b58SPaul E. McKenney 			if (rcu_seq_done(&ssp->srcu_gp_seq, s) && snp != snp_leaf)
867da915ad5SPaul E. McKenney 				return; /* GP already done and CBs recorded. */
868d6331980SPaul E. McKenney 			spin_lock_irqsave_rcu_node(snp, flags);
869da915ad5SPaul E. McKenney 			snp_seq = snp->srcu_have_cbs[idx];
870cbdc98e9SPaul E. McKenney 			if (!srcu_invl_snp_seq(snp_seq) && ULONG_CMP_GE(snp_seq, s)) {
8717b9e9b58SPaul E. McKenney 				if (snp == snp_leaf && snp_seq == s)
872c7e88067SPaul E. McKenney 					snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
873d6331980SPaul E. McKenney 				spin_unlock_irqrestore_rcu_node(snp, flags);
8747b9e9b58SPaul E. McKenney 				if (snp == snp_leaf && snp_seq != s) {
875aeb9b39bSPaul E. McKenney 					srcu_schedule_cbs_sdp(sdp, do_norm ? SRCU_INTERVAL : 0);
8761e9a038bSPaul E. McKenney 					return;
877da915ad5SPaul E. McKenney 				}
8781e9a038bSPaul E. McKenney 				if (!do_norm)
879aacb5d91SPaul E. McKenney 					srcu_funnel_exp_start(ssp, snp, s);
880da915ad5SPaul E. McKenney 				return;
881da915ad5SPaul E. McKenney 			}
882da915ad5SPaul E. McKenney 			snp->srcu_have_cbs[idx] = s;
8837b9e9b58SPaul E. McKenney 			if (snp == snp_leaf)
884c7e88067SPaul E. McKenney 				snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
885cbdc98e9SPaul E. McKenney 			sgsne = snp->srcu_gp_seq_needed_exp;
886cbdc98e9SPaul E. McKenney 			if (!do_norm && (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, s)))
8877ff8b450SPaul E. McKenney 				WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
888d6331980SPaul E. McKenney 			spin_unlock_irqrestore_rcu_node(snp, flags);
889da915ad5SPaul E. McKenney 		}
890da915ad5SPaul E. McKenney 
891da915ad5SPaul E. McKenney 	/* Top of tree, must ensure the grace period will be started. */
8929f2e91d9SPaul E. McKenney 	spin_lock_irqsave_ssp_contention(ssp, &flags);
893aacb5d91SPaul E. McKenney 	if (ULONG_CMP_LT(ssp->srcu_gp_seq_needed, s)) {
894da915ad5SPaul E. McKenney 		/*
895da915ad5SPaul E. McKenney 		 * Record need for grace period s.  Pair with load
896da915ad5SPaul E. McKenney 		 * acquire setting up for initialization.
897da915ad5SPaul E. McKenney 		 */
898aacb5d91SPaul E. McKenney 		smp_store_release(&ssp->srcu_gp_seq_needed, s); /*^^^*/
899da915ad5SPaul E. McKenney 	}
900aacb5d91SPaul E. McKenney 	if (!do_norm && ULONG_CMP_LT(ssp->srcu_gp_seq_needed_exp, s))
9018c9e0cb3SPaul E. McKenney 		WRITE_ONCE(ssp->srcu_gp_seq_needed_exp, s);
902da915ad5SPaul E. McKenney 
903da915ad5SPaul E. McKenney 	/* If grace period not already done and none in progress, start it. */
904aacb5d91SPaul E. McKenney 	if (!rcu_seq_done(&ssp->srcu_gp_seq, s) &&
905aacb5d91SPaul E. McKenney 	    rcu_seq_state(ssp->srcu_gp_seq) == SRCU_STATE_IDLE) {
906aacb5d91SPaul E. McKenney 		WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed));
907aacb5d91SPaul E. McKenney 		srcu_gp_start(ssp);
908ee5e2448SPaul E. McKenney 
909ee5e2448SPaul E. McKenney 		// And how can that list_add() in the "else" clause
910ee5e2448SPaul E. McKenney 		// possibly be safe for concurrent execution?  Well,
911ee5e2448SPaul E. McKenney 		// it isn't.  And it does not have to be.  After all, it
912ee5e2448SPaul E. McKenney 		// can only be executed during early boot when there is only
913ee5e2448SPaul E. McKenney 		// the one boot CPU running with interrupts still disabled.
914e0fcba9aSPaul E. McKenney 		if (likely(srcu_init_done))
915aacb5d91SPaul E. McKenney 			queue_delayed_work(rcu_gp_wq, &ssp->work,
916282d8998SPaul E. McKenney 					   !!srcu_get_delay(ssp));
917aacb5d91SPaul E. McKenney 		else if (list_empty(&ssp->work.work.entry))
918aacb5d91SPaul E. McKenney 			list_add(&ssp->work.work.entry, &srcu_boot_list);
919da915ad5SPaul E. McKenney 	}
920aacb5d91SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(ssp, flags);
921da915ad5SPaul E. McKenney }
922da915ad5SPaul E. McKenney 
923da915ad5SPaul E. McKenney /*
924dad81a20SPaul E. McKenney  * Wait until all readers counted by array index idx complete, but
925dad81a20SPaul E. McKenney  * loop an additional time if there is an expedited grace period pending.
926da915ad5SPaul E. McKenney  * The caller must ensure that ->srcu_idx is not changed while checking.
927dad81a20SPaul E. McKenney  */
928aacb5d91SPaul E. McKenney static bool try_check_zero(struct srcu_struct *ssp, int idx, int trycount)
929dad81a20SPaul E. McKenney {
930dad81a20SPaul E. McKenney 	for (;;) {
931aacb5d91SPaul E. McKenney 		if (srcu_readers_active_idx_check(ssp, idx))
932dad81a20SPaul E. McKenney 			return true;
933aacb5d91SPaul E. McKenney 		if (--trycount + !srcu_get_delay(ssp) <= 0)
934dad81a20SPaul E. McKenney 			return false;
935dad81a20SPaul E. McKenney 		udelay(SRCU_RETRY_CHECK_DELAY);
936dad81a20SPaul E. McKenney 	}
937dad81a20SPaul E. McKenney }
938dad81a20SPaul E. McKenney 
939dad81a20SPaul E. McKenney /*
940da915ad5SPaul E. McKenney  * Increment the ->srcu_idx counter so that future SRCU readers will
941da915ad5SPaul E. McKenney  * use the other rank of the ->srcu_(un)lock_count[] arrays.  This allows
942dad81a20SPaul E. McKenney  * us to wait for pre-existing readers in a starvation-free manner.
943dad81a20SPaul E. McKenney  */
944aacb5d91SPaul E. McKenney static void srcu_flip(struct srcu_struct *ssp)
945dad81a20SPaul E. McKenney {
946881ec9d2SPaul E. McKenney 	/*
947881ec9d2SPaul E. McKenney 	 * Ensure that if this updater saw a given reader's increment
948881ec9d2SPaul E. McKenney 	 * from __srcu_read_lock(), that reader was using an old value
949881ec9d2SPaul E. McKenney 	 * of ->srcu_idx.  Also ensure that if a given reader sees the
950881ec9d2SPaul E. McKenney 	 * new value of ->srcu_idx, this updater's earlier scans cannot
951881ec9d2SPaul E. McKenney 	 * have seen that reader's increments (which is OK, because this
952881ec9d2SPaul E. McKenney 	 * grace period need not wait on that reader).
953881ec9d2SPaul E. McKenney 	 */
954881ec9d2SPaul E. McKenney 	smp_mb(); /* E */  /* Pairs with B and C. */
955881ec9d2SPaul E. McKenney 
956aacb5d91SPaul E. McKenney 	WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
957dad81a20SPaul E. McKenney 
958dad81a20SPaul E. McKenney 	/*
959dad81a20SPaul E. McKenney 	 * Ensure that if the updater misses an __srcu_read_unlock()
960dad81a20SPaul E. McKenney 	 * increment, that task's next __srcu_read_lock() will see the
961dad81a20SPaul E. McKenney 	 * above counter update.  Note that both this memory barrier
962dad81a20SPaul E. McKenney 	 * and the one in srcu_readers_active_idx_check() provide the
963dad81a20SPaul E. McKenney 	 * guarantee for __srcu_read_lock().
964dad81a20SPaul E. McKenney 	 */
965dad81a20SPaul E. McKenney 	smp_mb(); /* D */  /* Pairs with C. */
966dad81a20SPaul E. McKenney }
967dad81a20SPaul E. McKenney 
968dad81a20SPaul E. McKenney /*
9692da4b2a7SPaul E. McKenney  * If SRCU is likely idle, return true, otherwise return false.
9702da4b2a7SPaul E. McKenney  *
9712da4b2a7SPaul E. McKenney  * Note that it is OK for several current from-idle requests for a new
9722da4b2a7SPaul E. McKenney  * grace period from idle to specify expediting because they will all end
9732da4b2a7SPaul E. McKenney  * up requesting the same grace period anyhow.  So no loss.
9742da4b2a7SPaul E. McKenney  *
9752da4b2a7SPaul E. McKenney  * Note also that if any CPU (including the current one) is still invoking
9762da4b2a7SPaul E. McKenney  * callbacks, this function will nevertheless say "idle".  This is not
9772da4b2a7SPaul E. McKenney  * ideal, but the overhead of checking all CPUs' callback lists is even
9782da4b2a7SPaul E. McKenney  * less ideal, especially on large systems.  Furthermore, the wakeup
9792da4b2a7SPaul E. McKenney  * can happen before the callback is fully removed, so we have no choice
9802da4b2a7SPaul E. McKenney  * but to accept this type of error.
9812da4b2a7SPaul E. McKenney  *
9822da4b2a7SPaul E. McKenney  * This function is also subject to counter-wrap errors, but let's face
9832da4b2a7SPaul E. McKenney  * it, if this function was preempted for enough time for the counters
9842da4b2a7SPaul E. McKenney  * to wrap, it really doesn't matter whether or not we expedite the grace
9852da4b2a7SPaul E. McKenney  * period.  The extra overhead of a needlessly expedited grace period is
9867fef6cffSEthon Paul  * negligible when amortized over that time period, and the extra latency
9872da4b2a7SPaul E. McKenney  * of a needlessly non-expedited grace period is similarly negligible.
9882da4b2a7SPaul E. McKenney  */
989aacb5d91SPaul E. McKenney static bool srcu_might_be_idle(struct srcu_struct *ssp)
9902da4b2a7SPaul E. McKenney {
99122607d66SPaul E. McKenney 	unsigned long curseq;
9922da4b2a7SPaul E. McKenney 	unsigned long flags;
9932da4b2a7SPaul E. McKenney 	struct srcu_data *sdp;
99422607d66SPaul E. McKenney 	unsigned long t;
995844a378dSPaul E. McKenney 	unsigned long tlast;
9962da4b2a7SPaul E. McKenney 
997bde50d8fSSebastian Andrzej Siewior 	check_init_srcu_struct(ssp);
9982da4b2a7SPaul E. McKenney 	/* If the local srcu_data structure has callbacks, not idle.  */
999bde50d8fSSebastian Andrzej Siewior 	sdp = raw_cpu_ptr(ssp->sda);
1000bde50d8fSSebastian Andrzej Siewior 	spin_lock_irqsave_rcu_node(sdp, flags);
10012da4b2a7SPaul E. McKenney 	if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
1002bde50d8fSSebastian Andrzej Siewior 		spin_unlock_irqrestore_rcu_node(sdp, flags);
10032da4b2a7SPaul E. McKenney 		return false; /* Callbacks already present, so not idle. */
10042da4b2a7SPaul E. McKenney 	}
1005bde50d8fSSebastian Andrzej Siewior 	spin_unlock_irqrestore_rcu_node(sdp, flags);
10062da4b2a7SPaul E. McKenney 
10072da4b2a7SPaul E. McKenney 	/*
1008a616aec9SIngo Molnar 	 * No local callbacks, so probabilistically probe global state.
10092da4b2a7SPaul E. McKenney 	 * Exact information would require acquiring locks, which would
1010a616aec9SIngo Molnar 	 * kill scalability, hence the probabilistic nature of the probe.
10112da4b2a7SPaul E. McKenney 	 */
101222607d66SPaul E. McKenney 
101322607d66SPaul E. McKenney 	/* First, see if enough time has passed since the last GP. */
101422607d66SPaul E. McKenney 	t = ktime_get_mono_fast_ns();
1015844a378dSPaul E. McKenney 	tlast = READ_ONCE(ssp->srcu_last_gp_end);
101622607d66SPaul E. McKenney 	if (exp_holdoff == 0 ||
1017844a378dSPaul E. McKenney 	    time_in_range_open(t, tlast, tlast + exp_holdoff))
101822607d66SPaul E. McKenney 		return false; /* Too soon after last GP. */
101922607d66SPaul E. McKenney 
102022607d66SPaul E. McKenney 	/* Next, check for probable idleness. */
1021aacb5d91SPaul E. McKenney 	curseq = rcu_seq_current(&ssp->srcu_gp_seq);
10222da4b2a7SPaul E. McKenney 	smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
1023aacb5d91SPaul E. McKenney 	if (ULONG_CMP_LT(curseq, READ_ONCE(ssp->srcu_gp_seq_needed)))
10242da4b2a7SPaul E. McKenney 		return false; /* Grace period in progress, so not idle. */
10252da4b2a7SPaul E. McKenney 	smp_mb(); /* Order ->srcu_gp_seq with prior access. */
1026aacb5d91SPaul E. McKenney 	if (curseq != rcu_seq_current(&ssp->srcu_gp_seq))
10272da4b2a7SPaul E. McKenney 		return false; /* GP # changed, so not idle. */
10282da4b2a7SPaul E. McKenney 	return true; /* With reasonable probability, idle! */
10292da4b2a7SPaul E. McKenney }
10302da4b2a7SPaul E. McKenney 
10312da4b2a7SPaul E. McKenney /*
1032a602538eSPaul E. McKenney  * SRCU callback function to leak a callback.
1033a602538eSPaul E. McKenney  */
1034a602538eSPaul E. McKenney static void srcu_leak_callback(struct rcu_head *rhp)
1035a602538eSPaul E. McKenney {
1036a602538eSPaul E. McKenney }
1037a602538eSPaul E. McKenney 
1038a602538eSPaul E. McKenney /*
103929d2bb94SPaul E. McKenney  * Start an SRCU grace period, and also queue the callback if non-NULL.
104029d2bb94SPaul E. McKenney  */
10415358c9faSPaul E. McKenney static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp,
10425358c9faSPaul E. McKenney 					     struct rcu_head *rhp, bool do_norm)
104329d2bb94SPaul E. McKenney {
104429d2bb94SPaul E. McKenney 	unsigned long flags;
104529d2bb94SPaul E. McKenney 	int idx;
104629d2bb94SPaul E. McKenney 	bool needexp = false;
104729d2bb94SPaul E. McKenney 	bool needgp = false;
104829d2bb94SPaul E. McKenney 	unsigned long s;
104929d2bb94SPaul E. McKenney 	struct srcu_data *sdp;
10500b56f953SNeeraj Upadhyay 	struct srcu_node *sdp_mynode;
10510b56f953SNeeraj Upadhyay 	int ss_state;
105229d2bb94SPaul E. McKenney 
10535358c9faSPaul E. McKenney 	check_init_srcu_struct(ssp);
105429d2bb94SPaul E. McKenney 	idx = srcu_read_lock(ssp);
10550b56f953SNeeraj Upadhyay 	ss_state = smp_load_acquire(&ssp->srcu_size_state);
10560b56f953SNeeraj Upadhyay 	if (ss_state < SRCU_SIZE_WAIT_CALL)
1057994f7068SPaul E. McKenney 		sdp = per_cpu_ptr(ssp->sda, 0);
1058994f7068SPaul E. McKenney 	else
105929d2bb94SPaul E. McKenney 		sdp = raw_cpu_ptr(ssp->sda);
1060c2445d38SPaul E. McKenney 	spin_lock_irqsave_sdp_contention(sdp, &flags);
10615358c9faSPaul E. McKenney 	if (rhp)
106229d2bb94SPaul E. McKenney 		rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp);
106329d2bb94SPaul E. McKenney 	rcu_segcblist_advance(&sdp->srcu_cblist,
106429d2bb94SPaul E. McKenney 			      rcu_seq_current(&ssp->srcu_gp_seq));
106529d2bb94SPaul E. McKenney 	s = rcu_seq_snap(&ssp->srcu_gp_seq);
106629d2bb94SPaul E. McKenney 	(void)rcu_segcblist_accelerate(&sdp->srcu_cblist, s);
106729d2bb94SPaul E. McKenney 	if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
106829d2bb94SPaul E. McKenney 		sdp->srcu_gp_seq_needed = s;
106929d2bb94SPaul E. McKenney 		needgp = true;
107029d2bb94SPaul E. McKenney 	}
107129d2bb94SPaul E. McKenney 	if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
107229d2bb94SPaul E. McKenney 		sdp->srcu_gp_seq_needed_exp = s;
107329d2bb94SPaul E. McKenney 		needexp = true;
107429d2bb94SPaul E. McKenney 	}
107529d2bb94SPaul E. McKenney 	spin_unlock_irqrestore_rcu_node(sdp, flags);
10760b56f953SNeeraj Upadhyay 
10770b56f953SNeeraj Upadhyay 	/* Ensure that snp node tree is fully initialized before traversing it */
10780b56f953SNeeraj Upadhyay 	if (ss_state < SRCU_SIZE_WAIT_BARRIER)
10790b56f953SNeeraj Upadhyay 		sdp_mynode = NULL;
10800b56f953SNeeraj Upadhyay 	else
10810b56f953SNeeraj Upadhyay 		sdp_mynode = sdp->mynode;
10820b56f953SNeeraj Upadhyay 
108329d2bb94SPaul E. McKenney 	if (needgp)
108429d2bb94SPaul E. McKenney 		srcu_funnel_gp_start(ssp, sdp, s, do_norm);
108529d2bb94SPaul E. McKenney 	else if (needexp)
10860b56f953SNeeraj Upadhyay 		srcu_funnel_exp_start(ssp, sdp_mynode, s);
108729d2bb94SPaul E. McKenney 	srcu_read_unlock(ssp, idx);
10885358c9faSPaul E. McKenney 	return s;
108929d2bb94SPaul E. McKenney }
109029d2bb94SPaul E. McKenney 
109129d2bb94SPaul E. McKenney /*
1092da915ad5SPaul E. McKenney  * Enqueue an SRCU callback on the srcu_data structure associated with
1093da915ad5SPaul E. McKenney  * the current CPU and the specified srcu_struct structure, initiating
1094da915ad5SPaul E. McKenney  * grace-period processing if it is not already running.
1095dad81a20SPaul E. McKenney  *
1096dad81a20SPaul E. McKenney  * Note that all CPUs must agree that the grace period extended beyond
1097dad81a20SPaul E. McKenney  * all pre-existing SRCU read-side critical section.  On systems with
1098dad81a20SPaul E. McKenney  * more than one CPU, this means that when "func()" is invoked, each CPU
1099dad81a20SPaul E. McKenney  * is guaranteed to have executed a full memory barrier since the end of
1100dad81a20SPaul E. McKenney  * its last corresponding SRCU read-side critical section whose beginning
11015ef98a63SPaul E. McKenney  * preceded the call to call_srcu().  It also means that each CPU executing
1102dad81a20SPaul E. McKenney  * an SRCU read-side critical section that continues beyond the start of
11035ef98a63SPaul E. McKenney  * "func()" must have executed a memory barrier after the call_srcu()
1104dad81a20SPaul E. McKenney  * but before the beginning of that SRCU read-side critical section.
1105dad81a20SPaul E. McKenney  * Note that these guarantees include CPUs that are offline, idle, or
1106dad81a20SPaul E. McKenney  * executing in user mode, as well as CPUs that are executing in the kernel.
1107dad81a20SPaul E. McKenney  *
11085ef98a63SPaul E. McKenney  * Furthermore, if CPU A invoked call_srcu() and CPU B invoked the
1109dad81a20SPaul E. McKenney  * resulting SRCU callback function "func()", then both CPU A and CPU
1110dad81a20SPaul E. McKenney  * B are guaranteed to execute a full memory barrier during the time
11115ef98a63SPaul E. McKenney  * interval between the call to call_srcu() and the invocation of "func()".
1112dad81a20SPaul E. McKenney  * This guarantee applies even if CPU A and CPU B are the same CPU (but
1113dad81a20SPaul E. McKenney  * again only if the system has more than one CPU).
1114dad81a20SPaul E. McKenney  *
1115dad81a20SPaul E. McKenney  * Of course, these guarantees apply only for invocations of call_srcu(),
1116dad81a20SPaul E. McKenney  * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
1117dad81a20SPaul E. McKenney  * srcu_struct structure.
1118dad81a20SPaul E. McKenney  */
111911b00045SJiang Biao static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
11201e9a038bSPaul E. McKenney 			rcu_callback_t func, bool do_norm)
1121dad81a20SPaul E. McKenney {
1122a602538eSPaul E. McKenney 	if (debug_rcu_head_queue(rhp)) {
1123a602538eSPaul E. McKenney 		/* Probable double call_srcu(), so leak the callback. */
1124a602538eSPaul E. McKenney 		WRITE_ONCE(rhp->func, srcu_leak_callback);
1125a602538eSPaul E. McKenney 		WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n");
1126a602538eSPaul E. McKenney 		return;
1127a602538eSPaul E. McKenney 	}
1128da915ad5SPaul E. McKenney 	rhp->func = func;
11295358c9faSPaul E. McKenney 	(void)srcu_gp_start_if_needed(ssp, rhp, do_norm);
11301e9a038bSPaul E. McKenney }
11311e9a038bSPaul E. McKenney 
11325a0465e1SPaul E. McKenney /**
11335a0465e1SPaul E. McKenney  * call_srcu() - Queue a callback for invocation after an SRCU grace period
1134aacb5d91SPaul E. McKenney  * @ssp: srcu_struct in queue the callback
113527fdb35fSPaul E. McKenney  * @rhp: structure to be used for queueing the SRCU callback.
11365a0465e1SPaul E. McKenney  * @func: function to be invoked after the SRCU grace period
11375a0465e1SPaul E. McKenney  *
11385a0465e1SPaul E. McKenney  * The callback function will be invoked some time after a full SRCU
11395a0465e1SPaul E. McKenney  * grace period elapses, in other words after all pre-existing SRCU
11405a0465e1SPaul E. McKenney  * read-side critical sections have completed.  However, the callback
11415a0465e1SPaul E. McKenney  * function might well execute concurrently with other SRCU read-side
11425a0465e1SPaul E. McKenney  * critical sections that started after call_srcu() was invoked.  SRCU
11435a0465e1SPaul E. McKenney  * read-side critical sections are delimited by srcu_read_lock() and
11445a0465e1SPaul E. McKenney  * srcu_read_unlock(), and may be nested.
11455a0465e1SPaul E. McKenney  *
11465a0465e1SPaul E. McKenney  * The callback will be invoked from process context, but must nevertheless
11475a0465e1SPaul E. McKenney  * be fast and must not block.
11485a0465e1SPaul E. McKenney  */
1149aacb5d91SPaul E. McKenney void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
11501e9a038bSPaul E. McKenney 	       rcu_callback_t func)
11511e9a038bSPaul E. McKenney {
1152aacb5d91SPaul E. McKenney 	__call_srcu(ssp, rhp, func, true);
1153dad81a20SPaul E. McKenney }
1154dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(call_srcu);
1155dad81a20SPaul E. McKenney 
1156dad81a20SPaul E. McKenney /*
1157dad81a20SPaul E. McKenney  * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
1158dad81a20SPaul E. McKenney  */
1159aacb5d91SPaul E. McKenney static void __synchronize_srcu(struct srcu_struct *ssp, bool do_norm)
1160dad81a20SPaul E. McKenney {
1161dad81a20SPaul E. McKenney 	struct rcu_synchronize rcu;
1162dad81a20SPaul E. McKenney 
1163f505d434SJakub Kicinski 	RCU_LOCKDEP_WARN(lockdep_is_held(ssp) ||
1164dad81a20SPaul E. McKenney 			 lock_is_held(&rcu_bh_lock_map) ||
1165dad81a20SPaul E. McKenney 			 lock_is_held(&rcu_lock_map) ||
1166dad81a20SPaul E. McKenney 			 lock_is_held(&rcu_sched_lock_map),
1167dad81a20SPaul E. McKenney 			 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
1168dad81a20SPaul E. McKenney 
1169dad81a20SPaul E. McKenney 	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
1170dad81a20SPaul E. McKenney 		return;
1171dad81a20SPaul E. McKenney 	might_sleep();
1172aacb5d91SPaul E. McKenney 	check_init_srcu_struct(ssp);
1173dad81a20SPaul E. McKenney 	init_completion(&rcu.completion);
1174da915ad5SPaul E. McKenney 	init_rcu_head_on_stack(&rcu.head);
1175aacb5d91SPaul E. McKenney 	__call_srcu(ssp, &rcu.head, wakeme_after_rcu, do_norm);
1176dad81a20SPaul E. McKenney 	wait_for_completion(&rcu.completion);
1177da915ad5SPaul E. McKenney 	destroy_rcu_head_on_stack(&rcu.head);
117835732cf9SPaul E. McKenney 
117935732cf9SPaul E. McKenney 	/*
118035732cf9SPaul E. McKenney 	 * Make sure that later code is ordered after the SRCU grace
1181d6331980SPaul E. McKenney 	 * period.  This pairs with the spin_lock_irq_rcu_node()
118235732cf9SPaul E. McKenney 	 * in srcu_invoke_callbacks().  Unlike Tree RCU, this is needed
118335732cf9SPaul E. McKenney 	 * because the current CPU might have been totally uninvolved with
118435732cf9SPaul E. McKenney 	 * (and thus unordered against) that grace period.
118535732cf9SPaul E. McKenney 	 */
118635732cf9SPaul E. McKenney 	smp_mb();
1187dad81a20SPaul E. McKenney }
1188dad81a20SPaul E. McKenney 
1189dad81a20SPaul E. McKenney /**
1190dad81a20SPaul E. McKenney  * synchronize_srcu_expedited - Brute-force SRCU grace period
1191aacb5d91SPaul E. McKenney  * @ssp: srcu_struct with which to synchronize.
1192dad81a20SPaul E. McKenney  *
1193dad81a20SPaul E. McKenney  * Wait for an SRCU grace period to elapse, but be more aggressive about
1194dad81a20SPaul E. McKenney  * spinning rather than blocking when waiting.
1195dad81a20SPaul E. McKenney  *
1196dad81a20SPaul E. McKenney  * Note that synchronize_srcu_expedited() has the same deadlock and
1197dad81a20SPaul E. McKenney  * memory-ordering properties as does synchronize_srcu().
1198dad81a20SPaul E. McKenney  */
1199aacb5d91SPaul E. McKenney void synchronize_srcu_expedited(struct srcu_struct *ssp)
1200dad81a20SPaul E. McKenney {
1201aacb5d91SPaul E. McKenney 	__synchronize_srcu(ssp, rcu_gp_is_normal());
1202dad81a20SPaul E. McKenney }
1203dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
1204dad81a20SPaul E. McKenney 
1205dad81a20SPaul E. McKenney /**
1206dad81a20SPaul E. McKenney  * synchronize_srcu - wait for prior SRCU read-side critical-section completion
1207aacb5d91SPaul E. McKenney  * @ssp: srcu_struct with which to synchronize.
1208dad81a20SPaul E. McKenney  *
1209dad81a20SPaul E. McKenney  * Wait for the count to drain to zero of both indexes. To avoid the
1210dad81a20SPaul E. McKenney  * possible starvation of synchronize_srcu(), it waits for the count of
1211da915ad5SPaul E. McKenney  * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
1212da915ad5SPaul E. McKenney  * and then flip the srcu_idx and wait for the count of the other index.
1213dad81a20SPaul E. McKenney  *
1214dad81a20SPaul E. McKenney  * Can block; must be called from process context.
1215dad81a20SPaul E. McKenney  *
1216dad81a20SPaul E. McKenney  * Note that it is illegal to call synchronize_srcu() from the corresponding
1217dad81a20SPaul E. McKenney  * SRCU read-side critical section; doing so will result in deadlock.
1218dad81a20SPaul E. McKenney  * However, it is perfectly legal to call synchronize_srcu() on one
1219dad81a20SPaul E. McKenney  * srcu_struct from some other srcu_struct's read-side critical section,
1220dad81a20SPaul E. McKenney  * as long as the resulting graph of srcu_structs is acyclic.
1221dad81a20SPaul E. McKenney  *
1222dad81a20SPaul E. McKenney  * There are memory-ordering constraints implied by synchronize_srcu().
1223dad81a20SPaul E. McKenney  * On systems with more than one CPU, when synchronize_srcu() returns,
1224dad81a20SPaul E. McKenney  * each CPU is guaranteed to have executed a full memory barrier since
12256eb95cc4SPaul E. McKenney  * the end of its last corresponding SRCU read-side critical section
1226dad81a20SPaul E. McKenney  * whose beginning preceded the call to synchronize_srcu().  In addition,
1227dad81a20SPaul E. McKenney  * each CPU having an SRCU read-side critical section that extends beyond
1228dad81a20SPaul E. McKenney  * the return from synchronize_srcu() is guaranteed to have executed a
1229dad81a20SPaul E. McKenney  * full memory barrier after the beginning of synchronize_srcu() and before
1230dad81a20SPaul E. McKenney  * the beginning of that SRCU read-side critical section.  Note that these
1231dad81a20SPaul E. McKenney  * guarantees include CPUs that are offline, idle, or executing in user mode,
1232dad81a20SPaul E. McKenney  * as well as CPUs that are executing in the kernel.
1233dad81a20SPaul E. McKenney  *
1234dad81a20SPaul E. McKenney  * Furthermore, if CPU A invoked synchronize_srcu(), which returned
1235dad81a20SPaul E. McKenney  * to its caller on CPU B, then both CPU A and CPU B are guaranteed
1236dad81a20SPaul E. McKenney  * to have executed a full memory barrier during the execution of
1237dad81a20SPaul E. McKenney  * synchronize_srcu().  This guarantee applies even if CPU A and CPU B
1238dad81a20SPaul E. McKenney  * are the same CPU, but again only if the system has more than one CPU.
1239dad81a20SPaul E. McKenney  *
1240dad81a20SPaul E. McKenney  * Of course, these memory-ordering guarantees apply only when
1241dad81a20SPaul E. McKenney  * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
1242dad81a20SPaul E. McKenney  * passed the same srcu_struct structure.
12432da4b2a7SPaul E. McKenney  *
12443d3a0d1bSPaul E. McKenney  * Implementation of these memory-ordering guarantees is similar to
12453d3a0d1bSPaul E. McKenney  * that of synchronize_rcu().
12463d3a0d1bSPaul E. McKenney  *
12472da4b2a7SPaul E. McKenney  * If SRCU is likely idle, expedite the first request.  This semantic
12482da4b2a7SPaul E. McKenney  * was provided by Classic SRCU, and is relied upon by its users, so TREE
12492da4b2a7SPaul E. McKenney  * SRCU must also provide it.  Note that detecting idleness is heuristic
12502da4b2a7SPaul E. McKenney  * and subject to both false positives and negatives.
1251dad81a20SPaul E. McKenney  */
1252aacb5d91SPaul E. McKenney void synchronize_srcu(struct srcu_struct *ssp)
1253dad81a20SPaul E. McKenney {
1254aacb5d91SPaul E. McKenney 	if (srcu_might_be_idle(ssp) || rcu_gp_is_expedited())
1255aacb5d91SPaul E. McKenney 		synchronize_srcu_expedited(ssp);
1256dad81a20SPaul E. McKenney 	else
1257aacb5d91SPaul E. McKenney 		__synchronize_srcu(ssp, true);
1258dad81a20SPaul E. McKenney }
1259dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(synchronize_srcu);
1260dad81a20SPaul E. McKenney 
12615358c9faSPaul E. McKenney /**
12625358c9faSPaul E. McKenney  * get_state_synchronize_srcu - Provide an end-of-grace-period cookie
12635358c9faSPaul E. McKenney  * @ssp: srcu_struct to provide cookie for.
12645358c9faSPaul E. McKenney  *
12655358c9faSPaul E. McKenney  * This function returns a cookie that can be passed to
12665358c9faSPaul E. McKenney  * poll_state_synchronize_srcu(), which will return true if a full grace
12675358c9faSPaul E. McKenney  * period has elapsed in the meantime.  It is the caller's responsibility
12685358c9faSPaul E. McKenney  * to make sure that grace period happens, for example, by invoking
12695358c9faSPaul E. McKenney  * call_srcu() after return from get_state_synchronize_srcu().
12705358c9faSPaul E. McKenney  */
12715358c9faSPaul E. McKenney unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp)
12725358c9faSPaul E. McKenney {
12735358c9faSPaul E. McKenney 	// Any prior manipulation of SRCU-protected data must happen
12745358c9faSPaul E. McKenney 	// before the load from ->srcu_gp_seq.
12755358c9faSPaul E. McKenney 	smp_mb();
12765358c9faSPaul E. McKenney 	return rcu_seq_snap(&ssp->srcu_gp_seq);
12775358c9faSPaul E. McKenney }
12785358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(get_state_synchronize_srcu);
12795358c9faSPaul E. McKenney 
12805358c9faSPaul E. McKenney /**
12815358c9faSPaul E. McKenney  * start_poll_synchronize_srcu - Provide cookie and start grace period
12825358c9faSPaul E. McKenney  * @ssp: srcu_struct to provide cookie for.
12835358c9faSPaul E. McKenney  *
12845358c9faSPaul E. McKenney  * This function returns a cookie that can be passed to
12855358c9faSPaul E. McKenney  * poll_state_synchronize_srcu(), which will return true if a full grace
12865358c9faSPaul E. McKenney  * period has elapsed in the meantime.  Unlike get_state_synchronize_srcu(),
12875358c9faSPaul E. McKenney  * this function also ensures that any needed SRCU grace period will be
12885358c9faSPaul E. McKenney  * started.  This convenience does come at a cost in terms of CPU overhead.
12895358c9faSPaul E. McKenney  */
12905358c9faSPaul E. McKenney unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp)
12915358c9faSPaul E. McKenney {
12925358c9faSPaul E. McKenney 	return srcu_gp_start_if_needed(ssp, NULL, true);
12935358c9faSPaul E. McKenney }
12945358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu);
12955358c9faSPaul E. McKenney 
12965358c9faSPaul E. McKenney /**
12975358c9faSPaul E. McKenney  * poll_state_synchronize_srcu - Has cookie's grace period ended?
12985358c9faSPaul E. McKenney  * @ssp: srcu_struct to provide cookie for.
12995358c9faSPaul E. McKenney  * @cookie: Return value from get_state_synchronize_srcu() or start_poll_synchronize_srcu().
13005358c9faSPaul E. McKenney  *
13015358c9faSPaul E. McKenney  * This function takes the cookie that was returned from either
13025358c9faSPaul E. McKenney  * get_state_synchronize_srcu() or start_poll_synchronize_srcu(), and
13035358c9faSPaul E. McKenney  * returns @true if an SRCU grace period elapsed since the time that the
13045358c9faSPaul E. McKenney  * cookie was created.
13054e7ccfaeSPaul E. McKenney  *
13064e7ccfaeSPaul E. McKenney  * Because cookies are finite in size, wrapping/overflow is possible.
13074e7ccfaeSPaul E. McKenney  * This is more pronounced on 32-bit systems where cookies are 32 bits,
13084e7ccfaeSPaul E. McKenney  * where in theory wrapping could happen in about 14 hours assuming
13094e7ccfaeSPaul E. McKenney  * 25-microsecond expedited SRCU grace periods.  However, a more likely
13104e7ccfaeSPaul E. McKenney  * overflow lower bound is on the order of 24 days in the case of
13114e7ccfaeSPaul E. McKenney  * one-millisecond SRCU grace periods.  Of course, wrapping in a 64-bit
13124e7ccfaeSPaul E. McKenney  * system requires geologic timespans, as in more than seven million years
13134e7ccfaeSPaul E. McKenney  * even for expedited SRCU grace periods.
13144e7ccfaeSPaul E. McKenney  *
13154e7ccfaeSPaul E. McKenney  * Wrapping/overflow is much more of an issue for CONFIG_SMP=n systems
13164e7ccfaeSPaul E. McKenney  * that also have CONFIG_PREEMPTION=n, which selects Tiny SRCU.  This uses
13174e7ccfaeSPaul E. McKenney  * a 16-bit cookie, which rcutorture routinely wraps in a matter of a
13184e7ccfaeSPaul E. McKenney  * few minutes.  If this proves to be a problem, this counter will be
13194e7ccfaeSPaul E. McKenney  * expanded to the same size as for Tree SRCU.
13205358c9faSPaul E. McKenney  */
13215358c9faSPaul E. McKenney bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie)
13225358c9faSPaul E. McKenney {
13235358c9faSPaul E. McKenney 	if (!rcu_seq_done(&ssp->srcu_gp_seq, cookie))
13245358c9faSPaul E. McKenney 		return false;
13255358c9faSPaul E. McKenney 	// Ensure that the end of the SRCU grace period happens before
13265358c9faSPaul E. McKenney 	// any subsequent code that the caller might execute.
13275358c9faSPaul E. McKenney 	smp_mb(); // ^^^
13285358c9faSPaul E. McKenney 	return true;
13295358c9faSPaul E. McKenney }
13305358c9faSPaul E. McKenney EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu);
13315358c9faSPaul E. McKenney 
1332da915ad5SPaul E. McKenney /*
1333da915ad5SPaul E. McKenney  * Callback function for srcu_barrier() use.
1334da915ad5SPaul E. McKenney  */
1335da915ad5SPaul E. McKenney static void srcu_barrier_cb(struct rcu_head *rhp)
1336da915ad5SPaul E. McKenney {
1337da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
1338aacb5d91SPaul E. McKenney 	struct srcu_struct *ssp;
1339da915ad5SPaul E. McKenney 
1340da915ad5SPaul E. McKenney 	sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
1341aacb5d91SPaul E. McKenney 	ssp = sdp->ssp;
1342aacb5d91SPaul E. McKenney 	if (atomic_dec_and_test(&ssp->srcu_barrier_cpu_cnt))
1343aacb5d91SPaul E. McKenney 		complete(&ssp->srcu_barrier_completion);
1344da915ad5SPaul E. McKenney }
1345da915ad5SPaul E. McKenney 
1346994f7068SPaul E. McKenney /*
1347994f7068SPaul E. McKenney  * Enqueue an srcu_barrier() callback on the specified srcu_data
1348994f7068SPaul E. McKenney  * structure's ->cblist.  but only if that ->cblist already has at least one
1349994f7068SPaul E. McKenney  * callback enqueued.  Note that if a CPU already has callbacks enqueue,
1350994f7068SPaul E. McKenney  * it must have already registered the need for a future grace period,
1351994f7068SPaul E. McKenney  * so all we need do is enqueue a callback that will use the same grace
1352994f7068SPaul E. McKenney  * period as the last callback already in the queue.
1353994f7068SPaul E. McKenney  */
1354994f7068SPaul E. McKenney static void srcu_barrier_one_cpu(struct srcu_struct *ssp, struct srcu_data *sdp)
1355994f7068SPaul E. McKenney {
1356994f7068SPaul E. McKenney 	spin_lock_irq_rcu_node(sdp);
1357994f7068SPaul E. McKenney 	atomic_inc(&ssp->srcu_barrier_cpu_cnt);
1358994f7068SPaul E. McKenney 	sdp->srcu_barrier_head.func = srcu_barrier_cb;
1359994f7068SPaul E. McKenney 	debug_rcu_head_queue(&sdp->srcu_barrier_head);
1360994f7068SPaul E. McKenney 	if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
1361994f7068SPaul E. McKenney 				   &sdp->srcu_barrier_head)) {
1362994f7068SPaul E. McKenney 		debug_rcu_head_unqueue(&sdp->srcu_barrier_head);
1363994f7068SPaul E. McKenney 		atomic_dec(&ssp->srcu_barrier_cpu_cnt);
1364994f7068SPaul E. McKenney 	}
1365994f7068SPaul E. McKenney 	spin_unlock_irq_rcu_node(sdp);
1366994f7068SPaul E. McKenney }
1367994f7068SPaul E. McKenney 
1368dad81a20SPaul E. McKenney /**
1369dad81a20SPaul E. McKenney  * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
1370aacb5d91SPaul E. McKenney  * @ssp: srcu_struct on which to wait for in-flight callbacks.
1371dad81a20SPaul E. McKenney  */
1372aacb5d91SPaul E. McKenney void srcu_barrier(struct srcu_struct *ssp)
1373dad81a20SPaul E. McKenney {
1374da915ad5SPaul E. McKenney 	int cpu;
1375e2f63836SPaul E. McKenney 	int idx;
1376aacb5d91SPaul E. McKenney 	unsigned long s = rcu_seq_snap(&ssp->srcu_barrier_seq);
1377da915ad5SPaul E. McKenney 
1378aacb5d91SPaul E. McKenney 	check_init_srcu_struct(ssp);
1379aacb5d91SPaul E. McKenney 	mutex_lock(&ssp->srcu_barrier_mutex);
1380aacb5d91SPaul E. McKenney 	if (rcu_seq_done(&ssp->srcu_barrier_seq, s)) {
1381da915ad5SPaul E. McKenney 		smp_mb(); /* Force ordering following return. */
1382aacb5d91SPaul E. McKenney 		mutex_unlock(&ssp->srcu_barrier_mutex);
1383da915ad5SPaul E. McKenney 		return; /* Someone else did our work for us. */
1384da915ad5SPaul E. McKenney 	}
1385aacb5d91SPaul E. McKenney 	rcu_seq_start(&ssp->srcu_barrier_seq);
1386aacb5d91SPaul E. McKenney 	init_completion(&ssp->srcu_barrier_completion);
1387da915ad5SPaul E. McKenney 
1388da915ad5SPaul E. McKenney 	/* Initial count prevents reaching zero until all CBs are posted. */
1389aacb5d91SPaul E. McKenney 	atomic_set(&ssp->srcu_barrier_cpu_cnt, 1);
1390da915ad5SPaul E. McKenney 
1391e2f63836SPaul E. McKenney 	idx = srcu_read_lock(ssp);
1392994f7068SPaul E. McKenney 	if (smp_load_acquire(&ssp->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
1393994f7068SPaul E. McKenney 		srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, 0));
1394994f7068SPaul E. McKenney 	else
1395994f7068SPaul E. McKenney 		for_each_possible_cpu(cpu)
1396994f7068SPaul E. McKenney 			srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, cpu));
1397e2f63836SPaul E. McKenney 	srcu_read_unlock(ssp, idx);
1398da915ad5SPaul E. McKenney 
1399da915ad5SPaul E. McKenney 	/* Remove the initial count, at which point reaching zero can happen. */
1400aacb5d91SPaul E. McKenney 	if (atomic_dec_and_test(&ssp->srcu_barrier_cpu_cnt))
1401aacb5d91SPaul E. McKenney 		complete(&ssp->srcu_barrier_completion);
1402aacb5d91SPaul E. McKenney 	wait_for_completion(&ssp->srcu_barrier_completion);
1403da915ad5SPaul E. McKenney 
1404aacb5d91SPaul E. McKenney 	rcu_seq_end(&ssp->srcu_barrier_seq);
1405aacb5d91SPaul E. McKenney 	mutex_unlock(&ssp->srcu_barrier_mutex);
1406dad81a20SPaul E. McKenney }
1407dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_barrier);
1408dad81a20SPaul E. McKenney 
1409dad81a20SPaul E. McKenney /**
1410dad81a20SPaul E. McKenney  * srcu_batches_completed - return batches completed.
1411aacb5d91SPaul E. McKenney  * @ssp: srcu_struct on which to report batch completion.
1412dad81a20SPaul E. McKenney  *
1413dad81a20SPaul E. McKenney  * Report the number of batches, correlated with, but not necessarily
1414dad81a20SPaul E. McKenney  * precisely the same as, the number of grace periods that have elapsed.
1415dad81a20SPaul E. McKenney  */
1416aacb5d91SPaul E. McKenney unsigned long srcu_batches_completed(struct srcu_struct *ssp)
1417dad81a20SPaul E. McKenney {
141839f91504SPaul E. McKenney 	return READ_ONCE(ssp->srcu_idx);
1419dad81a20SPaul E. McKenney }
1420dad81a20SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_batches_completed);
1421dad81a20SPaul E. McKenney 
1422dad81a20SPaul E. McKenney /*
1423da915ad5SPaul E. McKenney  * Core SRCU state machine.  Push state bits of ->srcu_gp_seq
1424da915ad5SPaul E. McKenney  * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
1425da915ad5SPaul E. McKenney  * completed in that state.
1426dad81a20SPaul E. McKenney  */
1427aacb5d91SPaul E. McKenney static void srcu_advance_state(struct srcu_struct *ssp)
1428dad81a20SPaul E. McKenney {
1429dad81a20SPaul E. McKenney 	int idx;
1430dad81a20SPaul E. McKenney 
1431aacb5d91SPaul E. McKenney 	mutex_lock(&ssp->srcu_gp_mutex);
1432da915ad5SPaul E. McKenney 
1433dad81a20SPaul E. McKenney 	/*
1434dad81a20SPaul E. McKenney 	 * Because readers might be delayed for an extended period after
1435da915ad5SPaul E. McKenney 	 * fetching ->srcu_idx for their index, at any point in time there
1436dad81a20SPaul E. McKenney 	 * might well be readers using both idx=0 and idx=1.  We therefore
1437dad81a20SPaul E. McKenney 	 * need to wait for readers to clear from both index values before
1438dad81a20SPaul E. McKenney 	 * invoking a callback.
1439dad81a20SPaul E. McKenney 	 *
1440dad81a20SPaul E. McKenney 	 * The load-acquire ensures that we see the accesses performed
1441dad81a20SPaul E. McKenney 	 * by the prior grace period.
1442dad81a20SPaul E. McKenney 	 */
1443aacb5d91SPaul E. McKenney 	idx = rcu_seq_state(smp_load_acquire(&ssp->srcu_gp_seq)); /* ^^^ */
1444dad81a20SPaul E. McKenney 	if (idx == SRCU_STATE_IDLE) {
1445aacb5d91SPaul E. McKenney 		spin_lock_irq_rcu_node(ssp);
1446aacb5d91SPaul E. McKenney 		if (ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)) {
1447aacb5d91SPaul E. McKenney 			WARN_ON_ONCE(rcu_seq_state(ssp->srcu_gp_seq));
1448aacb5d91SPaul E. McKenney 			spin_unlock_irq_rcu_node(ssp);
1449aacb5d91SPaul E. McKenney 			mutex_unlock(&ssp->srcu_gp_mutex);
1450dad81a20SPaul E. McKenney 			return;
1451dad81a20SPaul E. McKenney 		}
1452aacb5d91SPaul E. McKenney 		idx = rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq));
1453dad81a20SPaul E. McKenney 		if (idx == SRCU_STATE_IDLE)
1454aacb5d91SPaul E. McKenney 			srcu_gp_start(ssp);
1455aacb5d91SPaul E. McKenney 		spin_unlock_irq_rcu_node(ssp);
1456da915ad5SPaul E. McKenney 		if (idx != SRCU_STATE_IDLE) {
1457aacb5d91SPaul E. McKenney 			mutex_unlock(&ssp->srcu_gp_mutex);
1458dad81a20SPaul E. McKenney 			return; /* Someone else started the grace period. */
1459dad81a20SPaul E. McKenney 		}
1460da915ad5SPaul E. McKenney 	}
1461dad81a20SPaul E. McKenney 
1462aacb5d91SPaul E. McKenney 	if (rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
1463aacb5d91SPaul E. McKenney 		idx = 1 ^ (ssp->srcu_idx & 1);
1464aacb5d91SPaul E. McKenney 		if (!try_check_zero(ssp, idx, 1)) {
1465aacb5d91SPaul E. McKenney 			mutex_unlock(&ssp->srcu_gp_mutex);
1466dad81a20SPaul E. McKenney 			return; /* readers present, retry later. */
1467da915ad5SPaul E. McKenney 		}
1468aacb5d91SPaul E. McKenney 		srcu_flip(ssp);
146971042606SPaul E. McKenney 		spin_lock_irq_rcu_node(ssp);
1470aacb5d91SPaul E. McKenney 		rcu_seq_set_state(&ssp->srcu_gp_seq, SRCU_STATE_SCAN2);
1471282d8998SPaul E. McKenney 		ssp->srcu_n_exp_nodelay = 0;
147271042606SPaul E. McKenney 		spin_unlock_irq_rcu_node(ssp);
1473dad81a20SPaul E. McKenney 	}
1474dad81a20SPaul E. McKenney 
1475aacb5d91SPaul E. McKenney 	if (rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
1476dad81a20SPaul E. McKenney 
1477dad81a20SPaul E. McKenney 		/*
1478dad81a20SPaul E. McKenney 		 * SRCU read-side critical sections are normally short,
1479dad81a20SPaul E. McKenney 		 * so check at least twice in quick succession after a flip.
1480dad81a20SPaul E. McKenney 		 */
1481aacb5d91SPaul E. McKenney 		idx = 1 ^ (ssp->srcu_idx & 1);
1482aacb5d91SPaul E. McKenney 		if (!try_check_zero(ssp, idx, 2)) {
1483aacb5d91SPaul E. McKenney 			mutex_unlock(&ssp->srcu_gp_mutex);
1484da915ad5SPaul E. McKenney 			return; /* readers present, retry later. */
1485da915ad5SPaul E. McKenney 		}
1486282d8998SPaul E. McKenney 		ssp->srcu_n_exp_nodelay = 0;
1487aacb5d91SPaul E. McKenney 		srcu_gp_end(ssp);  /* Releases ->srcu_gp_mutex. */
1488dad81a20SPaul E. McKenney 	}
1489dad81a20SPaul E. McKenney }
1490dad81a20SPaul E. McKenney 
1491dad81a20SPaul E. McKenney /*
1492dad81a20SPaul E. McKenney  * Invoke a limited number of SRCU callbacks that have passed through
1493dad81a20SPaul E. McKenney  * their grace period.  If there are more to do, SRCU will reschedule
1494dad81a20SPaul E. McKenney  * the workqueue.  Note that needed memory barriers have been executed
1495dad81a20SPaul E. McKenney  * in this task's context by srcu_readers_active_idx_check().
1496dad81a20SPaul E. McKenney  */
1497da915ad5SPaul E. McKenney static void srcu_invoke_callbacks(struct work_struct *work)
1498dad81a20SPaul E. McKenney {
1499ae5c2341SJoel Fernandes (Google) 	long len;
1500da915ad5SPaul E. McKenney 	bool more;
1501dad81a20SPaul E. McKenney 	struct rcu_cblist ready_cbs;
1502dad81a20SPaul E. McKenney 	struct rcu_head *rhp;
1503da915ad5SPaul E. McKenney 	struct srcu_data *sdp;
1504aacb5d91SPaul E. McKenney 	struct srcu_struct *ssp;
1505dad81a20SPaul E. McKenney 
1506e81baf4cSSebastian Andrzej Siewior 	sdp = container_of(work, struct srcu_data, work);
1507e81baf4cSSebastian Andrzej Siewior 
1508aacb5d91SPaul E. McKenney 	ssp = sdp->ssp;
1509dad81a20SPaul E. McKenney 	rcu_cblist_init(&ready_cbs);
1510d6331980SPaul E. McKenney 	spin_lock_irq_rcu_node(sdp);
1511da915ad5SPaul E. McKenney 	rcu_segcblist_advance(&sdp->srcu_cblist,
1512aacb5d91SPaul E. McKenney 			      rcu_seq_current(&ssp->srcu_gp_seq));
1513da915ad5SPaul E. McKenney 	if (sdp->srcu_cblist_invoking ||
1514da915ad5SPaul E. McKenney 	    !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
1515d6331980SPaul E. McKenney 		spin_unlock_irq_rcu_node(sdp);
1516da915ad5SPaul E. McKenney 		return;  /* Someone else on the job or nothing to do. */
1517da915ad5SPaul E. McKenney 	}
1518da915ad5SPaul E. McKenney 
1519da915ad5SPaul E. McKenney 	/* We are on the job!  Extract and invoke ready callbacks. */
1520da915ad5SPaul E. McKenney 	sdp->srcu_cblist_invoking = true;
1521da915ad5SPaul E. McKenney 	rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
1522ae5c2341SJoel Fernandes (Google) 	len = ready_cbs.len;
1523d6331980SPaul E. McKenney 	spin_unlock_irq_rcu_node(sdp);
1524dad81a20SPaul E. McKenney 	rhp = rcu_cblist_dequeue(&ready_cbs);
1525dad81a20SPaul E. McKenney 	for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
1526a602538eSPaul E. McKenney 		debug_rcu_head_unqueue(rhp);
1527dad81a20SPaul E. McKenney 		local_bh_disable();
1528dad81a20SPaul E. McKenney 		rhp->func(rhp);
1529dad81a20SPaul E. McKenney 		local_bh_enable();
1530dad81a20SPaul E. McKenney 	}
1531ae5c2341SJoel Fernandes (Google) 	WARN_ON_ONCE(ready_cbs.len);
1532da915ad5SPaul E. McKenney 
1533da915ad5SPaul E. McKenney 	/*
1534da915ad5SPaul E. McKenney 	 * Update counts, accelerate new callbacks, and if needed,
1535da915ad5SPaul E. McKenney 	 * schedule another round of callback invocation.
1536da915ad5SPaul E. McKenney 	 */
1537d6331980SPaul E. McKenney 	spin_lock_irq_rcu_node(sdp);
1538ae5c2341SJoel Fernandes (Google) 	rcu_segcblist_add_len(&sdp->srcu_cblist, -len);
1539da915ad5SPaul E. McKenney 	(void)rcu_segcblist_accelerate(&sdp->srcu_cblist,
1540aacb5d91SPaul E. McKenney 				       rcu_seq_snap(&ssp->srcu_gp_seq));
1541da915ad5SPaul E. McKenney 	sdp->srcu_cblist_invoking = false;
1542da915ad5SPaul E. McKenney 	more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
1543d6331980SPaul E. McKenney 	spin_unlock_irq_rcu_node(sdp);
1544da915ad5SPaul E. McKenney 	if (more)
1545da915ad5SPaul E. McKenney 		srcu_schedule_cbs_sdp(sdp, 0);
1546dad81a20SPaul E. McKenney }
1547dad81a20SPaul E. McKenney 
1548dad81a20SPaul E. McKenney /*
1549dad81a20SPaul E. McKenney  * Finished one round of SRCU grace period.  Start another if there are
1550dad81a20SPaul E. McKenney  * more SRCU callbacks queued, otherwise put SRCU into not-running state.
1551dad81a20SPaul E. McKenney  */
1552aacb5d91SPaul E. McKenney static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay)
1553dad81a20SPaul E. McKenney {
1554da915ad5SPaul E. McKenney 	bool pushgp = true;
1555dad81a20SPaul E. McKenney 
1556aacb5d91SPaul E. McKenney 	spin_lock_irq_rcu_node(ssp);
1557aacb5d91SPaul E. McKenney 	if (ULONG_CMP_GE(ssp->srcu_gp_seq, ssp->srcu_gp_seq_needed)) {
1558aacb5d91SPaul E. McKenney 		if (!WARN_ON_ONCE(rcu_seq_state(ssp->srcu_gp_seq))) {
1559da915ad5SPaul E. McKenney 			/* All requests fulfilled, time to go idle. */
1560da915ad5SPaul E. McKenney 			pushgp = false;
1561dad81a20SPaul E. McKenney 		}
1562aacb5d91SPaul E. McKenney 	} else if (!rcu_seq_state(ssp->srcu_gp_seq)) {
1563da915ad5SPaul E. McKenney 		/* Outstanding request and no GP.  Start one. */
1564aacb5d91SPaul E. McKenney 		srcu_gp_start(ssp);
1565da915ad5SPaul E. McKenney 	}
1566aacb5d91SPaul E. McKenney 	spin_unlock_irq_rcu_node(ssp);
1567dad81a20SPaul E. McKenney 
1568da915ad5SPaul E. McKenney 	if (pushgp)
1569aacb5d91SPaul E. McKenney 		queue_delayed_work(rcu_gp_wq, &ssp->work, delay);
1570dad81a20SPaul E. McKenney }
1571dad81a20SPaul E. McKenney 
1572dad81a20SPaul E. McKenney /*
1573dad81a20SPaul E. McKenney  * This is the work-queue function that handles SRCU grace periods.
1574dad81a20SPaul E. McKenney  */
15750d8a1e83SPaul E. McKenney static void process_srcu(struct work_struct *work)
1576dad81a20SPaul E. McKenney {
1577282d8998SPaul E. McKenney 	unsigned long curdelay;
1578282d8998SPaul E. McKenney 	unsigned long j;
1579aacb5d91SPaul E. McKenney 	struct srcu_struct *ssp;
1580dad81a20SPaul E. McKenney 
1581aacb5d91SPaul E. McKenney 	ssp = container_of(work, struct srcu_struct, work.work);
1582dad81a20SPaul E. McKenney 
1583aacb5d91SPaul E. McKenney 	srcu_advance_state(ssp);
1584282d8998SPaul E. McKenney 	curdelay = srcu_get_delay(ssp);
1585282d8998SPaul E. McKenney 	if (curdelay) {
1586282d8998SPaul E. McKenney 		WRITE_ONCE(ssp->reschedule_count, 0);
1587282d8998SPaul E. McKenney 	} else {
1588282d8998SPaul E. McKenney 		j = jiffies;
1589282d8998SPaul E. McKenney 		if (READ_ONCE(ssp->reschedule_jiffies) == j) {
1590282d8998SPaul E. McKenney 			WRITE_ONCE(ssp->reschedule_count, READ_ONCE(ssp->reschedule_count) + 1);
1591282d8998SPaul E. McKenney 			if (READ_ONCE(ssp->reschedule_count) > SRCU_MAX_NODELAY)
1592282d8998SPaul E. McKenney 				curdelay = 1;
1593282d8998SPaul E. McKenney 		} else {
1594282d8998SPaul E. McKenney 			WRITE_ONCE(ssp->reschedule_count, 1);
1595282d8998SPaul E. McKenney 			WRITE_ONCE(ssp->reschedule_jiffies, j);
1596282d8998SPaul E. McKenney 		}
1597282d8998SPaul E. McKenney 	}
1598282d8998SPaul E. McKenney 	srcu_reschedule(ssp, curdelay);
1599dad81a20SPaul E. McKenney }
16007f6733c3SPaul E. McKenney 
16017f6733c3SPaul E. McKenney void srcutorture_get_gp_data(enum rcutorture_type test_type,
1602aacb5d91SPaul E. McKenney 			     struct srcu_struct *ssp, int *flags,
1603aebc8264SPaul E. McKenney 			     unsigned long *gp_seq)
16047f6733c3SPaul E. McKenney {
16057f6733c3SPaul E. McKenney 	if (test_type != SRCU_FLAVOR)
16067f6733c3SPaul E. McKenney 		return;
16077f6733c3SPaul E. McKenney 	*flags = 0;
1608aacb5d91SPaul E. McKenney 	*gp_seq = rcu_seq_current(&ssp->srcu_gp_seq);
16097f6733c3SPaul E. McKenney }
16107f6733c3SPaul E. McKenney EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
16111f4f6da1SPaul E. McKenney 
16123bedebcfSPaul E. McKenney static const char * const srcu_size_state_name[] = {
16133bedebcfSPaul E. McKenney 	"SRCU_SIZE_SMALL",
16143bedebcfSPaul E. McKenney 	"SRCU_SIZE_ALLOC",
16153bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_BARRIER",
16163bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_CALL",
16173bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_CBS1",
16183bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_CBS2",
16193bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_CBS3",
16203bedebcfSPaul E. McKenney 	"SRCU_SIZE_WAIT_CBS4",
16213bedebcfSPaul E. McKenney 	"SRCU_SIZE_BIG",
16223bedebcfSPaul E. McKenney 	"SRCU_SIZE_???",
16233bedebcfSPaul E. McKenney };
16243bedebcfSPaul E. McKenney 
1625aacb5d91SPaul E. McKenney void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf)
1626115a1a52SPaul E. McKenney {
1627115a1a52SPaul E. McKenney 	int cpu;
1628115a1a52SPaul E. McKenney 	int idx;
1629ac3748c6SPaul E. McKenney 	unsigned long s0 = 0, s1 = 0;
16303bedebcfSPaul E. McKenney 	int ss_state = READ_ONCE(ssp->srcu_size_state);
16313bedebcfSPaul E. McKenney 	int ss_state_idx = ss_state;
1632115a1a52SPaul E. McKenney 
1633aacb5d91SPaul E. McKenney 	idx = ssp->srcu_idx & 0x1;
16343bedebcfSPaul E. McKenney 	if (ss_state < 0 || ss_state >= ARRAY_SIZE(srcu_size_state_name))
16353bedebcfSPaul E. McKenney 		ss_state_idx = ARRAY_SIZE(srcu_size_state_name) - 1;
16364a230f80SPaul E. McKenney 	pr_alert("%s%s Tree SRCU g%ld state %d (%s)",
16373bedebcfSPaul E. McKenney 		 tt, tf, rcu_seq_current(&ssp->srcu_gp_seq), ss_state,
16384a230f80SPaul E. McKenney 		 srcu_size_state_name[ss_state_idx]);
16394a230f80SPaul E. McKenney 	if (!ssp->sda) {
16404a230f80SPaul E. McKenney 		// Called after cleanup_srcu_struct(), perhaps.
16414a230f80SPaul E. McKenney 		pr_cont(" No per-CPU srcu_data structures (->sda == NULL).\n");
16424a230f80SPaul E. McKenney 	} else {
16434a230f80SPaul E. McKenney 		pr_cont(" per-CPU(idx=%d):", idx);
1644115a1a52SPaul E. McKenney 		for_each_possible_cpu(cpu) {
1645115a1a52SPaul E. McKenney 			unsigned long l0, l1;
1646115a1a52SPaul E. McKenney 			unsigned long u0, u1;
1647115a1a52SPaul E. McKenney 			long c0, c1;
16485ab07a8dSPaul E. McKenney 			struct srcu_data *sdp;
1649115a1a52SPaul E. McKenney 
1650aacb5d91SPaul E. McKenney 			sdp = per_cpu_ptr(ssp->sda, cpu);
1651b68c6146SPaul E. McKenney 			u0 = data_race(sdp->srcu_unlock_count[!idx]);
1652b68c6146SPaul E. McKenney 			u1 = data_race(sdp->srcu_unlock_count[idx]);
1653115a1a52SPaul E. McKenney 
1654115a1a52SPaul E. McKenney 			/*
1655115a1a52SPaul E. McKenney 			 * Make sure that a lock is always counted if the corresponding
1656115a1a52SPaul E. McKenney 			 * unlock is counted.
1657115a1a52SPaul E. McKenney 			 */
1658115a1a52SPaul E. McKenney 			smp_rmb();
1659115a1a52SPaul E. McKenney 
1660b68c6146SPaul E. McKenney 			l0 = data_race(sdp->srcu_lock_count[!idx]);
1661b68c6146SPaul E. McKenney 			l1 = data_race(sdp->srcu_lock_count[idx]);
1662115a1a52SPaul E. McKenney 
1663115a1a52SPaul E. McKenney 			c0 = l0 - u0;
1664115a1a52SPaul E. McKenney 			c1 = l1 - u1;
16657e210a65SPaul E. McKenney 			pr_cont(" %d(%ld,%ld %c)",
16667e210a65SPaul E. McKenney 				cpu, c0, c1,
16677e210a65SPaul E. McKenney 				"C."[rcu_segcblist_empty(&sdp->srcu_cblist)]);
1668ac3748c6SPaul E. McKenney 			s0 += c0;
1669ac3748c6SPaul E. McKenney 			s1 += c1;
1670115a1a52SPaul E. McKenney 		}
1671ac3748c6SPaul E. McKenney 		pr_cont(" T(%ld,%ld)\n", s0, s1);
16724a230f80SPaul E. McKenney 	}
16739f2e91d9SPaul E. McKenney 	if (SRCU_SIZING_IS_TORTURE())
167499659f64SPaul E. McKenney 		srcu_transition_to_big(ssp);
1675115a1a52SPaul E. McKenney }
1676115a1a52SPaul E. McKenney EXPORT_SYMBOL_GPL(srcu_torture_stats_print);
1677115a1a52SPaul E. McKenney 
16781f4f6da1SPaul E. McKenney static int __init srcu_bootup_announce(void)
16791f4f6da1SPaul E. McKenney {
16801f4f6da1SPaul E. McKenney 	pr_info("Hierarchical SRCU implementation.\n");
16810c8e0e3cSPaul E. McKenney 	if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF)
16820c8e0e3cSPaul E. McKenney 		pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff);
16831f4f6da1SPaul E. McKenney 	return 0;
16841f4f6da1SPaul E. McKenney }
16851f4f6da1SPaul E. McKenney early_initcall(srcu_bootup_announce);
1686e0fcba9aSPaul E. McKenney 
1687e0fcba9aSPaul E. McKenney void __init srcu_init(void)
1688e0fcba9aSPaul E. McKenney {
1689aacb5d91SPaul E. McKenney 	struct srcu_struct *ssp;
1690e0fcba9aSPaul E. McKenney 
1691a57ffb3cSPaul E. McKenney 	/* Decide on srcu_struct-size strategy. */
1692a57ffb3cSPaul E. McKenney 	if (SRCU_SIZING_IS(SRCU_SIZING_AUTO)) {
1693a57ffb3cSPaul E. McKenney 		if (nr_cpu_ids >= big_cpu_lim) {
1694a57ffb3cSPaul E. McKenney 			convert_to_big = SRCU_SIZING_INIT; // Don't bother waiting for contention.
1695a57ffb3cSPaul E. McKenney 			pr_info("%s: Setting srcu_struct sizes to big.\n", __func__);
1696a57ffb3cSPaul E. McKenney 		} else {
1697a57ffb3cSPaul E. McKenney 			convert_to_big = SRCU_SIZING_NONE | SRCU_SIZING_CONTEND;
1698a57ffb3cSPaul E. McKenney 			pr_info("%s: Setting srcu_struct sizes based on contention.\n", __func__);
1699a57ffb3cSPaul E. McKenney 		}
1700a57ffb3cSPaul E. McKenney 	}
1701a57ffb3cSPaul E. McKenney 
17028e9c01c7SFrederic Weisbecker 	/*
17038e9c01c7SFrederic Weisbecker 	 * Once that is set, call_srcu() can follow the normal path and
17048e9c01c7SFrederic Weisbecker 	 * queue delayed work. This must follow RCU workqueues creation
17058e9c01c7SFrederic Weisbecker 	 * and timers initialization.
17068e9c01c7SFrederic Weisbecker 	 */
1707e0fcba9aSPaul E. McKenney 	srcu_init_done = true;
1708e0fcba9aSPaul E. McKenney 	while (!list_empty(&srcu_boot_list)) {
1709aacb5d91SPaul E. McKenney 		ssp = list_first_entry(&srcu_boot_list, struct srcu_struct,
17104e6ea4efSPaul E. McKenney 				      work.work.entry);
1711aacb5d91SPaul E. McKenney 		list_del_init(&ssp->work.work.entry);
1712a57ffb3cSPaul E. McKenney 		if (SRCU_SIZING_IS(SRCU_SIZING_INIT) && ssp->srcu_size_state == SRCU_SIZE_SMALL)
1713a57ffb3cSPaul E. McKenney 			ssp->srcu_size_state = SRCU_SIZE_ALLOC;
1714aacb5d91SPaul E. McKenney 		queue_work(rcu_gp_wq, &ssp->work.work);
1715e0fcba9aSPaul E. McKenney 	}
1716e0fcba9aSPaul E. McKenney }
1717fe15b50cSPaul E. McKenney 
1718fe15b50cSPaul E. McKenney #ifdef CONFIG_MODULES
1719fe15b50cSPaul E. McKenney 
1720fe15b50cSPaul E. McKenney /* Initialize any global-scope srcu_struct structures used by this module. */
1721fe15b50cSPaul E. McKenney static int srcu_module_coming(struct module *mod)
1722fe15b50cSPaul E. McKenney {
1723fe15b50cSPaul E. McKenney 	int i;
1724fe15b50cSPaul E. McKenney 	struct srcu_struct **sspp = mod->srcu_struct_ptrs;
1725fe15b50cSPaul E. McKenney 	int ret;
1726fe15b50cSPaul E. McKenney 
1727fe15b50cSPaul E. McKenney 	for (i = 0; i < mod->num_srcu_structs; i++) {
1728fe15b50cSPaul E. McKenney 		ret = init_srcu_struct(*(sspp++));
1729fe15b50cSPaul E. McKenney 		if (WARN_ON_ONCE(ret))
1730fe15b50cSPaul E. McKenney 			return ret;
1731fe15b50cSPaul E. McKenney 	}
1732fe15b50cSPaul E. McKenney 	return 0;
1733fe15b50cSPaul E. McKenney }
1734fe15b50cSPaul E. McKenney 
1735fe15b50cSPaul E. McKenney /* Clean up any global-scope srcu_struct structures used by this module. */
1736fe15b50cSPaul E. McKenney static void srcu_module_going(struct module *mod)
1737fe15b50cSPaul E. McKenney {
1738fe15b50cSPaul E. McKenney 	int i;
1739fe15b50cSPaul E. McKenney 	struct srcu_struct **sspp = mod->srcu_struct_ptrs;
1740fe15b50cSPaul E. McKenney 
1741fe15b50cSPaul E. McKenney 	for (i = 0; i < mod->num_srcu_structs; i++)
1742fe15b50cSPaul E. McKenney 		cleanup_srcu_struct(*(sspp++));
1743fe15b50cSPaul E. McKenney }
1744fe15b50cSPaul E. McKenney 
1745fe15b50cSPaul E. McKenney /* Handle one module, either coming or going. */
1746fe15b50cSPaul E. McKenney static int srcu_module_notify(struct notifier_block *self,
1747fe15b50cSPaul E. McKenney 			      unsigned long val, void *data)
1748fe15b50cSPaul E. McKenney {
1749fe15b50cSPaul E. McKenney 	struct module *mod = data;
1750fe15b50cSPaul E. McKenney 	int ret = 0;
1751fe15b50cSPaul E. McKenney 
1752fe15b50cSPaul E. McKenney 	switch (val) {
1753fe15b50cSPaul E. McKenney 	case MODULE_STATE_COMING:
1754fe15b50cSPaul E. McKenney 		ret = srcu_module_coming(mod);
1755fe15b50cSPaul E. McKenney 		break;
1756fe15b50cSPaul E. McKenney 	case MODULE_STATE_GOING:
1757fe15b50cSPaul E. McKenney 		srcu_module_going(mod);
1758fe15b50cSPaul E. McKenney 		break;
1759fe15b50cSPaul E. McKenney 	default:
1760fe15b50cSPaul E. McKenney 		break;
1761fe15b50cSPaul E. McKenney 	}
1762fe15b50cSPaul E. McKenney 	return ret;
1763fe15b50cSPaul E. McKenney }
1764fe15b50cSPaul E. McKenney 
1765fe15b50cSPaul E. McKenney static struct notifier_block srcu_module_nb = {
1766fe15b50cSPaul E. McKenney 	.notifier_call = srcu_module_notify,
1767fe15b50cSPaul E. McKenney 	.priority = 0,
1768fe15b50cSPaul E. McKenney };
1769fe15b50cSPaul E. McKenney 
1770fe15b50cSPaul E. McKenney static __init int init_srcu_module_notifier(void)
1771fe15b50cSPaul E. McKenney {
1772fe15b50cSPaul E. McKenney 	int ret;
1773fe15b50cSPaul E. McKenney 
1774fe15b50cSPaul E. McKenney 	ret = register_module_notifier(&srcu_module_nb);
1775fe15b50cSPaul E. McKenney 	if (ret)
1776fe15b50cSPaul E. McKenney 		pr_warn("Failed to register srcu module notifier\n");
1777fe15b50cSPaul E. McKenney 	return ret;
1778fe15b50cSPaul E. McKenney }
1779fe15b50cSPaul E. McKenney late_initcall(init_srcu_module_notifier);
1780fe15b50cSPaul E. McKenney 
1781fe15b50cSPaul E. McKenney #endif /* #ifdef CONFIG_MODULES */
1782