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 <paulmck@linux.ibm.com>
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 struct srcu_ctr __percpu *srcu_ctrp;
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_ctrp = &pcpu_name.srcu_ctrs[0], \
171 __SRCU_STRUCT_INIT_COMMON(name, usage_name) \
172 }
173
174 /*
175 * Define and initialize a srcu struct at build time.
176 * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
177 *
178 * Note that although DEFINE_STATIC_SRCU() hides the name from other
179 * files, the per-CPU variable rules nevertheless require that the
180 * chosen name be globally unique. These rules also prohibit use of
181 * DEFINE_STATIC_SRCU() within a function. If these rules are too
182 * restrictive, declare the srcu_struct manually. For example, in
183 * each file:
184 *
185 * static struct srcu_struct my_srcu;
186 *
187 * Then, before the first use of each my_srcu, manually initialize it:
188 *
189 * init_srcu_struct(&my_srcu);
190 *
191 * See include/linux/percpu-defs.h for the rules on per-CPU variables.
192 */
193 #ifdef MODULE
194 # define __DEFINE_SRCU(name, is_static) \
195 static struct srcu_usage name##_srcu_usage = __SRCU_USAGE_INIT(name##_srcu_usage); \
196 is_static struct srcu_struct name = __SRCU_STRUCT_INIT_MODULE(name, name##_srcu_usage); \
197 extern struct srcu_struct * const __srcu_struct_##name; \
198 struct srcu_struct * const __srcu_struct_##name \
199 __section("___srcu_struct_ptrs") = &name
200 #else
201 # define __DEFINE_SRCU(name, is_static) \
202 static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data); \
203 static struct srcu_usage name##_srcu_usage = __SRCU_USAGE_INIT(name##_srcu_usage); \
204 is_static struct srcu_struct name = \
205 __SRCU_STRUCT_INIT(name, name##_srcu_usage, name##_srcu_data)
206 #endif
207 #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
208 #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
209
210 int __srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp);
211 void synchronize_srcu_expedited(struct srcu_struct *ssp);
212 void srcu_barrier(struct srcu_struct *ssp);
213 void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf);
214
215 // Converts a per-CPU pointer to an ->srcu_ctrs[] array element to that
216 // element's index.
__srcu_ptr_to_ctr(struct srcu_struct * ssp,struct srcu_ctr __percpu * scpp)217 static inline bool __srcu_ptr_to_ctr(struct srcu_struct *ssp, struct srcu_ctr __percpu *scpp)
218 {
219 return scpp - &ssp->sda->srcu_ctrs[0];
220 }
221
222 // Converts an integer to a per-CPU pointer to the corresponding
223 // ->srcu_ctrs[] array element.
__srcu_ctr_to_ptr(struct srcu_struct * ssp,int idx)224 static inline struct srcu_ctr __percpu *__srcu_ctr_to_ptr(struct srcu_struct *ssp, int idx)
225 {
226 return &ssp->sda->srcu_ctrs[idx];
227 }
228
229 /*
230 * Counts the new reader in the appropriate per-CPU element of the
231 * srcu_struct. Returns a pointer that must be passed to the matching
232 * srcu_read_unlock_fast().
233 *
234 * Note that both this_cpu_inc() and atomic_long_inc() are RCU read-side
235 * critical sections either because they disables interrupts, because they
236 * are a single instruction, or because they are a read-modify-write atomic
237 * operation, depending on the whims of the architecture.
238 *
239 * This means that __srcu_read_lock_fast() is not all that fast
240 * on architectures that support NMIs but do not supply NMI-safe
241 * implementations of this_cpu_inc().
242 */
__srcu_read_lock_fast(struct srcu_struct * ssp)243 static inline struct srcu_ctr __percpu *__srcu_read_lock_fast(struct srcu_struct *ssp)
244 {
245 struct srcu_ctr __percpu *scp = READ_ONCE(ssp->srcu_ctrp);
246
247 RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_lock_fast().");
248 if (!IS_ENABLED(CONFIG_NEED_SRCU_NMI_SAFE))
249 this_cpu_inc(scp->srcu_locks.counter); /* Y */
250 else
251 atomic_long_inc(raw_cpu_ptr(&scp->srcu_locks)); /* Z */
252 barrier(); /* Avoid leaking the critical section. */
253 return scp;
254 }
255
256 /*
257 * Removes the count for the old reader from the appropriate
258 * per-CPU element of the srcu_struct. Note that this may well be a
259 * different CPU than that which was incremented by the corresponding
260 * srcu_read_lock_fast(), but it must be within the same task.
261 *
262 * Note that both this_cpu_inc() and atomic_long_inc() are RCU read-side
263 * critical sections either because they disables interrupts, because they
264 * are a single instruction, or because they are a read-modify-write atomic
265 * operation, depending on the whims of the architecture.
266 *
267 * This means that __srcu_read_unlock_fast() is not all that fast
268 * on architectures that support NMIs but do not supply NMI-safe
269 * implementations of this_cpu_inc().
270 */
__srcu_read_unlock_fast(struct srcu_struct * ssp,struct srcu_ctr __percpu * scp)271 static inline void __srcu_read_unlock_fast(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp)
272 {
273 barrier(); /* Avoid leaking the critical section. */
274 if (!IS_ENABLED(CONFIG_NEED_SRCU_NMI_SAFE))
275 this_cpu_inc(scp->srcu_unlocks.counter); /* Z */
276 else
277 atomic_long_inc(raw_cpu_ptr(&scp->srcu_unlocks)); /* Z */
278 RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_unlock_fast().");
279 }
280
281 /*
282 * Counts the new reader in the appropriate per-CPU element of the
283 * srcu_struct. Returns an index that must be passed to the matching
284 * srcu_read_unlock_lite().
285 *
286 * Note that this_cpu_inc() is an RCU read-side critical section either
287 * because it disables interrupts, because it is a single instruction,
288 * or because it is a read-modify-write atomic operation, depending on
289 * the whims of the architecture.
290 */
__srcu_read_lock_lite(struct srcu_struct * ssp)291 static inline int __srcu_read_lock_lite(struct srcu_struct *ssp)
292 {
293 struct srcu_ctr __percpu *scp = READ_ONCE(ssp->srcu_ctrp);
294
295 RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_lock_lite().");
296 this_cpu_inc(scp->srcu_locks.counter); /* Y */
297 barrier(); /* Avoid leaking the critical section. */
298 return __srcu_ptr_to_ctr(ssp, scp);
299 }
300
301 /*
302 * Removes the count for the old reader from the appropriate
303 * per-CPU element of the srcu_struct. Note that this may well be a
304 * different CPU than that which was incremented by the corresponding
305 * srcu_read_lock_lite(), but it must be within the same task.
306 *
307 * Note that this_cpu_inc() is an RCU read-side critical section either
308 * because it disables interrupts, because it is a single instruction,
309 * or because it is a read-modify-write atomic operation, depending on
310 * the whims of the architecture.
311 */
__srcu_read_unlock_lite(struct srcu_struct * ssp,int idx)312 static inline void __srcu_read_unlock_lite(struct srcu_struct *ssp, int idx)
313 {
314 barrier(); /* Avoid leaking the critical section. */
315 this_cpu_inc(__srcu_ctr_to_ptr(ssp, idx)->srcu_unlocks.counter); /* Z */
316 RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_unlock_lite().");
317 }
318
319 void __srcu_check_read_flavor(struct srcu_struct *ssp, int read_flavor);
320
321 // Record reader usage even for CONFIG_PROVE_RCU=n kernels. This is
322 // needed only for flavors that require grace-period smp_mb() calls to be
323 // promoted to synchronize_rcu().
srcu_check_read_flavor_force(struct srcu_struct * ssp,int read_flavor)324 static inline void srcu_check_read_flavor_force(struct srcu_struct *ssp, int read_flavor)
325 {
326 struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
327
328 if (likely(READ_ONCE(sdp->srcu_reader_flavor) & read_flavor))
329 return;
330
331 // Note that the cmpxchg() in __srcu_check_read_flavor() is fully ordered.
332 __srcu_check_read_flavor(ssp, read_flavor);
333 }
334
335 // Record non-_lite() usage only for CONFIG_PROVE_RCU=y kernels.
srcu_check_read_flavor(struct srcu_struct * ssp,int read_flavor)336 static inline void srcu_check_read_flavor(struct srcu_struct *ssp, int read_flavor)
337 {
338 if (IS_ENABLED(CONFIG_PROVE_RCU))
339 __srcu_check_read_flavor(ssp, read_flavor);
340 }
341
342 #endif
343