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