xref: /linux-6.15/include/linux/srcutree.h (revision 56eb8be1)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Sleepable Read-Copy Update mechanism for mutual exclusion,
4  *	tree variant.
5  *
6  * Copyright (C) IBM Corporation, 2017
7  *
8  * Author: Paul McKenney <[email protected]>
9  */
10 
11 #ifndef _LINUX_SRCU_TREE_H
12 #define _LINUX_SRCU_TREE_H
13 
14 #include <linux/rcu_node_tree.h>
15 #include <linux/completion.h>
16 
17 struct srcu_node;
18 struct srcu_struct;
19 
20 /* One element of the srcu_data srcu_ctrs array. */
21 struct srcu_ctr {
22 	atomic_long_t srcu_locks;	/* Locks per CPU. */
23 	atomic_long_t srcu_unlocks;	/* Unlocks per CPU. */
24 };
25 
26 /*
27  * Per-CPU structure feeding into leaf srcu_node, similar in function
28  * to rcu_node.
29  */
30 struct srcu_data {
31 	/* Read-side state. */
32 	struct srcu_ctr srcu_ctrs[2];		/* Locks and unlocks per CPU. */
33 	int srcu_reader_flavor;			/* Reader flavor for srcu_struct structure? */
34 						/* Values: SRCU_READ_FLAVOR_.*  */
35 
36 	/* Update-side state. */
37 	spinlock_t __private lock ____cacheline_internodealigned_in_smp;
38 	struct rcu_segcblist srcu_cblist;	/* List of callbacks.*/
39 	unsigned long srcu_gp_seq_needed;	/* Furthest future GP needed. */
40 	unsigned long srcu_gp_seq_needed_exp;	/* Furthest future exp GP. */
41 	bool srcu_cblist_invoking;		/* Invoking these CBs? */
42 	struct timer_list delay_work;		/* Delay for CB invoking */
43 	struct work_struct work;		/* Context for CB invoking. */
44 	struct rcu_head srcu_barrier_head;	/* For srcu_barrier() use. */
45 	struct srcu_node *mynode;		/* Leaf srcu_node. */
46 	unsigned long grpmask;			/* Mask for leaf srcu_node */
47 						/*  ->srcu_data_have_cbs[]. */
48 	int cpu;
49 	struct srcu_struct *ssp;
50 };
51 
52 /*
53  * Node in SRCU combining tree, similar in function to rcu_data.
54  */
55 struct srcu_node {
56 	spinlock_t __private lock;
57 	unsigned long srcu_have_cbs[4];		/* GP seq for children having CBs, but only */
58 						/*  if greater than ->srcu_gp_seq. */
59 	unsigned long srcu_data_have_cbs[4];	/* Which srcu_data structs have CBs for given GP? */
60 	unsigned long srcu_gp_seq_needed_exp;	/* Furthest future exp GP. */
61 	struct srcu_node *srcu_parent;		/* Next up in tree. */
62 	int grplo;				/* Least CPU for node. */
63 	int grphi;				/* Biggest CPU for node. */
64 };
65 
66 /*
67  * Per-SRCU-domain structure, update-side data linked from srcu_struct.
68  */
69 struct srcu_usage {
70 	struct srcu_node *node;			/* Combining tree. */
71 	struct srcu_node *level[RCU_NUM_LVLS + 1];
72 						/* First node at each level. */
73 	int srcu_size_state;			/* Small-to-big transition state. */
74 	struct mutex srcu_cb_mutex;		/* Serialize CB preparation. */
75 	spinlock_t __private lock;		/* Protect counters and size state. */
76 	struct mutex srcu_gp_mutex;		/* Serialize GP work. */
77 	unsigned long srcu_gp_seq;		/* Grace-period seq #. */
78 	unsigned long srcu_gp_seq_needed;	/* Latest gp_seq needed. */
79 	unsigned long srcu_gp_seq_needed_exp;	/* Furthest future exp GP. */
80 	unsigned long srcu_gp_start;		/* Last GP start timestamp (jiffies) */
81 	unsigned long srcu_last_gp_end;		/* Last GP end timestamp (ns) */
82 	unsigned long srcu_size_jiffies;	/* Current contention-measurement interval. */
83 	unsigned long srcu_n_lock_retries;	/* Contention events in current interval. */
84 	unsigned long srcu_n_exp_nodelay;	/* # expedited no-delays in current GP phase. */
85 	bool sda_is_static;			/* May ->sda be passed to free_percpu()? */
86 	unsigned long srcu_barrier_seq;		/* srcu_barrier seq #. */
87 	struct mutex srcu_barrier_mutex;	/* Serialize barrier ops. */
88 	struct completion srcu_barrier_completion;
89 						/* Awaken barrier rq at end. */
90 	atomic_t srcu_barrier_cpu_cnt;		/* # CPUs not yet posting a */
91 						/*  callback for the barrier */
92 						/*  operation. */
93 	unsigned long reschedule_jiffies;
94 	unsigned long reschedule_count;
95 	struct delayed_work work;
96 	struct srcu_struct *srcu_ssp;
97 };
98 
99 /*
100  * Per-SRCU-domain structure, similar in function to rcu_state.
101  */
102 struct srcu_struct {
103 	unsigned int srcu_idx;			/* Current rdr array element. */
104 	struct srcu_data __percpu *sda;		/* Per-CPU srcu_data array. */
105 	struct lockdep_map dep_map;
106 	struct srcu_usage *srcu_sup;		/* Update-side data. */
107 };
108 
109 // Values for size state variable (->srcu_size_state).  Once the state
110 // has been set to SRCU_SIZE_ALLOC, the grace-period code advances through
111 // this state machine one step per grace period until the SRCU_SIZE_BIG state
112 // is reached.  Otherwise, the state machine remains in the SRCU_SIZE_SMALL
113 // state indefinitely.
114 #define SRCU_SIZE_SMALL		0	// No srcu_node combining tree, ->node == NULL
115 #define SRCU_SIZE_ALLOC		1	// An srcu_node tree is being allocated, initialized,
116 					//  and then referenced by ->node.  It will not be used.
117 #define SRCU_SIZE_WAIT_BARRIER	2	// The srcu_node tree starts being used by everything
118 					//  except call_srcu(), especially by srcu_barrier().
119 					//  By the end of this state, all CPUs and threads
120 					//  are aware of this tree's existence.
121 #define SRCU_SIZE_WAIT_CALL	3	// The srcu_node tree starts being used by call_srcu().
122 					//  By the end of this state, all of the call_srcu()
123 					//  invocations that were running on a non-boot CPU
124 					//  and using the boot CPU's callback queue will have
125 					//  completed.
126 #define SRCU_SIZE_WAIT_CBS1	4	// Don't trust the ->srcu_have_cbs[] grace-period
127 #define SRCU_SIZE_WAIT_CBS2	5	//  sequence elements or the ->srcu_data_have_cbs[]
128 #define SRCU_SIZE_WAIT_CBS3	6	//  CPU-bitmask elements until all four elements of
129 #define SRCU_SIZE_WAIT_CBS4	7	//  each array have been initialized.
130 #define SRCU_SIZE_BIG		8	// The srcu_node combining tree is fully initialized
131 					//  and all aspects of it are being put to use.
132 
133 /* Values for state variable (bottom bits of ->srcu_gp_seq). */
134 #define SRCU_STATE_IDLE		0
135 #define SRCU_STATE_SCAN1	1
136 #define SRCU_STATE_SCAN2	2
137 
138 /*
139  * Values for initializing gp sequence fields. Higher values allow wrap arounds to
140  * occur earlier.
141  * The second value with state is useful in the case of static initialization of
142  * srcu_usage where srcu_gp_seq_needed is expected to have some state value in its
143  * lower bits (or else it will appear to be already initialized within
144  * the call check_init_srcu_struct()).
145  */
146 #define SRCU_GP_SEQ_INITIAL_VAL ((0UL - 100UL) << RCU_SEQ_CTR_SHIFT)
147 #define SRCU_GP_SEQ_INITIAL_VAL_WITH_STATE (SRCU_GP_SEQ_INITIAL_VAL - 1)
148 
149 #define __SRCU_USAGE_INIT(name)									\
150 {												\
151 	.lock = __SPIN_LOCK_UNLOCKED(name.lock),						\
152 	.srcu_gp_seq = SRCU_GP_SEQ_INITIAL_VAL,							\
153 	.srcu_gp_seq_needed = SRCU_GP_SEQ_INITIAL_VAL_WITH_STATE,				\
154 	.srcu_gp_seq_needed_exp = SRCU_GP_SEQ_INITIAL_VAL,					\
155 	.work = __DELAYED_WORK_INITIALIZER(name.work, NULL, 0),					\
156 }
157 
158 #define __SRCU_STRUCT_INIT_COMMON(name, usage_name)						\
159 	.srcu_sup = &usage_name,								\
160 	__SRCU_DEP_MAP_INIT(name)
161 
162 #define __SRCU_STRUCT_INIT_MODULE(name, usage_name)						\
163 {												\
164 	__SRCU_STRUCT_INIT_COMMON(name, usage_name)						\
165 }
166 
167 #define __SRCU_STRUCT_INIT(name, usage_name, pcpu_name)						\
168 {												\
169 	.sda = &pcpu_name,									\
170 	__SRCU_STRUCT_INIT_COMMON(name, usage_name)						\
171 }
172 
173 /*
174  * Define and initialize a srcu struct at build time.
175  * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
176  *
177  * Note that although DEFINE_STATIC_SRCU() hides the name from other
178  * files, the per-CPU variable rules nevertheless require that the
179  * chosen name be globally unique.  These rules also prohibit use of
180  * DEFINE_STATIC_SRCU() within a function.  If these rules are too
181  * restrictive, declare the srcu_struct manually.  For example, in
182  * each file:
183  *
184  *	static struct srcu_struct my_srcu;
185  *
186  * Then, before the first use of each my_srcu, manually initialize it:
187  *
188  *	init_srcu_struct(&my_srcu);
189  *
190  * See include/linux/percpu-defs.h for the rules on per-CPU variables.
191  */
192 #ifdef MODULE
193 # define __DEFINE_SRCU(name, is_static)								\
194 	static struct srcu_usage name##_srcu_usage = __SRCU_USAGE_INIT(name##_srcu_usage);	\
195 	is_static struct srcu_struct name = __SRCU_STRUCT_INIT_MODULE(name, name##_srcu_usage);	\
196 	extern struct srcu_struct * const __srcu_struct_##name;					\
197 	struct srcu_struct * const __srcu_struct_##name						\
198 		__section("___srcu_struct_ptrs") = &name
199 #else
200 # define __DEFINE_SRCU(name, is_static)								\
201 	static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data);				\
202 	static struct srcu_usage name##_srcu_usage = __SRCU_USAGE_INIT(name##_srcu_usage);	\
203 	is_static struct srcu_struct name =							\
204 		__SRCU_STRUCT_INIT(name, name##_srcu_usage, name##_srcu_data)
205 #endif
206 #define DEFINE_SRCU(name)		__DEFINE_SRCU(name, /* not static */)
207 #define DEFINE_STATIC_SRCU(name)	__DEFINE_SRCU(name, static)
208 
209 void synchronize_srcu_expedited(struct srcu_struct *ssp);
210 void srcu_barrier(struct srcu_struct *ssp);
211 void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf);
212 
213 /*
214  * Counts the new reader in the appropriate per-CPU element of the
215  * srcu_struct.  Returns an index that must be passed to the matching
216  * srcu_read_unlock_lite().
217  *
218  * Note that this_cpu_inc() is an RCU read-side critical section either
219  * because it disables interrupts, because it is a single instruction,
220  * or because it is a read-modify-write atomic operation, depending on
221  * the whims of the architecture.
222  */
223 static inline int __srcu_read_lock_lite(struct srcu_struct *ssp)
224 {
225 	int idx;
226 
227 	RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_lock_lite().");
228 	idx = READ_ONCE(ssp->srcu_idx) & 0x1;
229 	this_cpu_inc(ssp->sda->srcu_ctrs[idx].srcu_locks.counter); /* Y */
230 	barrier(); /* Avoid leaking the critical section. */
231 	return idx;
232 }
233 
234 /*
235  * Removes the count for the old reader from the appropriate
236  * per-CPU element of the srcu_struct.  Note that this may well be a
237  * different CPU than that which was incremented by the corresponding
238  * srcu_read_lock_lite(), but it must be within the same task.
239  *
240  * Note that this_cpu_inc() is an RCU read-side critical section either
241  * because it disables interrupts, because it is a single instruction,
242  * or because it is a read-modify-write atomic operation, depending on
243  * the whims of the architecture.
244  */
245 static inline void __srcu_read_unlock_lite(struct srcu_struct *ssp, int idx)
246 {
247 	barrier();  /* Avoid leaking the critical section. */
248 	this_cpu_inc(ssp->sda->srcu_ctrs[idx].srcu_unlocks.counter);  /* Z */
249 	RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_unlock_lite().");
250 }
251 
252 void __srcu_check_read_flavor(struct srcu_struct *ssp, int read_flavor);
253 
254 // Record _lite() usage even for CONFIG_PROVE_RCU=n kernels.
255 static inline void srcu_check_read_flavor_lite(struct srcu_struct *ssp)
256 {
257 	struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
258 
259 	if (likely(READ_ONCE(sdp->srcu_reader_flavor) & SRCU_READ_FLAVOR_LITE))
260 		return;
261 
262 	// Note that the cmpxchg() in __srcu_check_read_flavor() is fully ordered.
263 	__srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_LITE);
264 }
265 
266 // Record non-_lite() usage only for CONFIG_PROVE_RCU=y kernels.
267 static inline void srcu_check_read_flavor(struct srcu_struct *ssp, int read_flavor)
268 {
269 	if (IS_ENABLED(CONFIG_PROVE_RCU))
270 		__srcu_check_read_flavor(ssp, read_flavor);
271 }
272 
273 #endif
274