xref: /linux/include/linux/srcu.h (revision 83684c4e4d62cb02b2e4d0d18963d1035439278e)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Sleepable Read-Copy Update mechanism for mutual exclusion
4  *
5  * Copyright (C) IBM Corporation, 2006
6  * Copyright (C) Fujitsu, 2012
7  *
8  * Author: Paul McKenney <paulmck@linux.ibm.com>
9  *	   Lai Jiangshan <laijs@cn.fujitsu.com>
10  *
11  * For detailed explanation of Read-Copy Update mechanism see -
12  *		Documentation/RCU/ *.txt
13  *
14  */
15 
16 #ifndef _LINUX_SRCU_H
17 #define _LINUX_SRCU_H
18 
19 #include <linux/mutex.h>
20 #include <linux/rcupdate.h>
21 #include <linux/workqueue.h>
22 #include <linux/rcu_segcblist.h>
23 
24 context_lock_struct(srcu_struct, __reentrant_ctx_lock);
25 
26 #ifdef CONFIG_DEBUG_LOCK_ALLOC
27 
28 int init_srcu_struct_lockdep(struct srcu_struct *ssp, const char *name,
29 				     struct lock_class_key *key);
__init_srcu_struct(struct srcu_struct * ssp,const char * name,struct lock_class_key * key)30 static inline int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
31 				     struct lock_class_key *key)
32 {
33 	return init_srcu_struct_lockdep(ssp, name, key);
34 }
35 #ifndef CONFIG_TINY_SRCU
36 int __init_srcu_struct_fast(struct srcu_struct *ssp, const char *name, struct lock_class_key *key);
37 int __init_srcu_struct_fast_updown(struct srcu_struct *ssp, const char *name,
38 				   struct lock_class_key *key);
39 #endif // #ifndef CONFIG_TINY_SRCU
40 
41 #define init_srcu_struct_fast(ssp) \
42 ({ \
43 	static struct lock_class_key __srcu_key; \
44 	\
45 	__init_srcu_struct_fast((ssp), #ssp, &__srcu_key); \
46 })
47 
48 #define init_srcu_struct_fast_updown(ssp) \
49 ({ \
50 	static struct lock_class_key __srcu_key; \
51 	\
52 	__init_srcu_struct_fast_updown((ssp), #ssp, &__srcu_key); \
53 })
54 
55 #define __SRCU_DEP_MAP_INIT(srcu_name)	.dep_map = { .name = #srcu_name },
56 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
57 
58 int init_srcu_struct_generic(struct srcu_struct *ssp);
__init_srcu_struct(struct srcu_struct * ssp,const char * name,struct lock_class_key * key)59 static inline int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
60 				     struct lock_class_key *key)
61 {
62 	return init_srcu_struct_generic(ssp);
63 }
64 #ifndef CONFIG_TINY_SRCU
65 int init_srcu_struct_fast(struct srcu_struct *ssp);
66 int init_srcu_struct_fast_updown(struct srcu_struct *ssp);
67 #endif // #ifndef CONFIG_TINY_SRCU
68 
69 #define __SRCU_DEP_MAP_INIT(srcu_name)
70 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
71 
72 #define init_srcu_struct(ssp) \
73 ({ \
74 	static struct lock_class_key __srcu_key; \
75 	\
76 	__init_srcu_struct((ssp), #ssp, &__srcu_key); \
77 })
78 
79 /* Values for SRCU Tree srcu_data ->srcu_reader_flavor, but also used by rcutorture. */
80 #define SRCU_READ_FLAVOR_NORMAL		0x1		// srcu_read_lock().
81 #define SRCU_READ_FLAVOR_NMI		0x2		// srcu_read_lock_nmisafe().
82 //					0x4		// SRCU-lite is no longer with us.
83 #define SRCU_READ_FLAVOR_FAST		0x4		// srcu_read_lock_fast(), also NMI-safe.
84 #define SRCU_READ_FLAVOR_FAST_UPDOWN	0x8		// srcu_read_lock_fast_updown().
85 #define SRCU_READ_FLAVOR_ALL		(SRCU_READ_FLAVOR_NORMAL | SRCU_READ_FLAVOR_NMI | \
86 					 SRCU_READ_FLAVOR_FAST | SRCU_READ_FLAVOR_FAST_UPDOWN)
87 						// All of the above.
88 #define SRCU_READ_FLAVOR_SLOWGP		(SRCU_READ_FLAVOR_FAST | SRCU_READ_FLAVOR_FAST_UPDOWN)
89 						// Flavors requiring synchronize_rcu()
90 						// instead of smp_mb().
91 void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases_shared(ssp);
92 
93 #ifdef CONFIG_TINY_SRCU
94 #include <linux/srcutiny.h>
95 #elif defined(CONFIG_TREE_SRCU)
96 #include <linux/srcutree.h>
97 #else
98 #error "Unknown SRCU implementation specified to kernel configuration"
99 #endif
100 
101 void call_srcu(struct srcu_struct *ssp, struct rcu_head *head,
102 		void (*func)(struct rcu_head *head));
103 void cleanup_srcu_struct(struct srcu_struct *ssp);
104 void synchronize_srcu(struct srcu_struct *ssp);
105 
106 #define SRCU_GET_STATE_COMPLETED 0x1
107 
108 /**
109  * get_completed_synchronize_srcu - Return a pre-completed polled state cookie
110  *
111  * Returns a value that poll_state_synchronize_srcu() will always treat
112  * as a cookie whose grace period has already completed.
113  */
get_completed_synchronize_srcu(void)114 static inline unsigned long get_completed_synchronize_srcu(void)
115 {
116 	return SRCU_GET_STATE_COMPLETED;
117 }
118 
119 unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp);
120 unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp);
121 bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie);
122 
123 // Maximum number of unsigned long values corresponding to
124 // not-yet-completed SRCU grace periods.
125 #define NUM_ACTIVE_SRCU_POLL_OLDSTATE 2
126 
127 /**
128  * same_state_synchronize_srcu - Are two old-state values identical?
129  * @oldstate1: First old-state value.
130  * @oldstate2: Second old-state value.
131  *
132  * The two old-state values must have been obtained from either
133  * get_state_synchronize_srcu(), start_poll_synchronize_srcu(), or
134  * get_completed_synchronize_srcu().  Returns @true if the two values are
135  * identical and @false otherwise.  This allows structures whose lifetimes
136  * are tracked by old-state values to push these values to a list header,
137  * allowing those structures to be slightly smaller.
138  */
same_state_synchronize_srcu(unsigned long oldstate1,unsigned long oldstate2)139 static inline bool same_state_synchronize_srcu(unsigned long oldstate1, unsigned long oldstate2)
140 {
141 	return oldstate1 == oldstate2;
142 }
143 
144 #ifdef CONFIG_NEED_SRCU_NMI_SAFE
145 int __srcu_read_lock_nmisafe(struct srcu_struct *ssp) __acquires_shared(ssp);
146 void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx) __releases_shared(ssp);
147 #else
__srcu_read_lock_nmisafe(struct srcu_struct * ssp)148 static inline int __srcu_read_lock_nmisafe(struct srcu_struct *ssp)
149 	__acquires_shared(ssp)
150 {
151 	return __srcu_read_lock(ssp);
152 }
__srcu_read_unlock_nmisafe(struct srcu_struct * ssp,int idx)153 static inline void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
154 	__releases_shared(ssp)
155 {
156 	__srcu_read_unlock(ssp, idx);
157 }
158 #endif /* CONFIG_NEED_SRCU_NMI_SAFE */
159 
160 void srcu_init(void);
161 
162 #ifdef CONFIG_DEBUG_LOCK_ALLOC
163 
164 /**
165  * srcu_read_lock_held - might we be in SRCU read-side critical section?
166  * @ssp: The srcu_struct structure to check
167  *
168  * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU
169  * read-side critical section.  In absence of CONFIG_DEBUG_LOCK_ALLOC,
170  * this assumes we are in an SRCU read-side critical section unless it can
171  * prove otherwise.
172  *
173  * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
174  * and while lockdep is disabled.
175  *
176  * Note that SRCU is based on its own statemachine and it doesn't
177  * relies on normal RCU, it can be called from the CPU which
178  * is in the idle loop from an RCU point of view or offline.
179  */
srcu_read_lock_held(const struct srcu_struct * ssp)180 static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
181 {
182 	if (!debug_lockdep_rcu_enabled())
183 		return 1;
184 	return lock_is_held(&ssp->dep_map);
185 }
186 
187 /*
188  * Annotations provide deadlock detection for SRCU.
189  *
190  * Similar to other lockdep annotations, except there is an additional
191  * srcu_lock_sync(), which is basically an empty *write*-side critical section,
192  * see lock_sync() for more information.
193  */
194 
195 /* Annotates a srcu_read_lock() */
srcu_lock_acquire(struct lockdep_map * map)196 static inline void srcu_lock_acquire(struct lockdep_map *map)
197 {
198 	lock_map_acquire_read(map);
199 }
200 
201 /* Annotates a srcu_read_lock() */
srcu_lock_release(struct lockdep_map * map)202 static inline void srcu_lock_release(struct lockdep_map *map)
203 {
204 	lock_map_release(map);
205 }
206 
207 /* Annotates a synchronize_srcu() */
srcu_lock_sync(struct lockdep_map * map)208 static inline void srcu_lock_sync(struct lockdep_map *map)
209 {
210 	lock_map_sync(map);
211 }
212 
213 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
214 
srcu_read_lock_held(const struct srcu_struct * ssp)215 static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
216 {
217 	return 1;
218 }
219 
220 #define srcu_lock_acquire(m) do { } while (0)
221 #define srcu_lock_release(m) do { } while (0)
222 #define srcu_lock_sync(m) do { } while (0)
223 
224 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
225 
226 /*
227  * No-op helper to denote that ssp must be held. Because SRCU-protected pointers
228  * should still be marked with __rcu_guarded, and we do not want to mark them
229  * with __guarded_by(ssp) as it would complicate annotations for writers, we
230  * choose the following strategy: srcu_dereference_check() calls this helper
231  * that checks that the passed ssp is held, and then fake-acquires 'RCU'.
232  */
__srcu_read_lock_must_hold(const struct srcu_struct * ssp)233 static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __must_hold_shared(ssp) { }
234 
235 /**
236  * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing
237  * @p: the pointer to fetch and protect for later dereferencing
238  * @ssp: pointer to the srcu_struct, which is used to check that we
239  *	really are in an SRCU read-side critical section.
240  * @c: condition to check for update-side use
241  *
242  * If PROVE_RCU is enabled, invoking this outside of an RCU read-side
243  * critical section will result in an RCU-lockdep splat, unless @c evaluates
244  * to 1.  The @c argument will normally be a logical expression containing
245  * lockdep_is_held() calls.
246  */
247 #define srcu_dereference_check(p, ssp, c)					\
248 ({										\
249 	__srcu_read_lock_must_hold(ssp);					\
250 	__acquire_shared_ctx_lock(RCU);					\
251 	__auto_type __v = __rcu_dereference_check((p), __UNIQUE_ID(rcu),	\
252 				(c) || srcu_read_lock_held(ssp), __rcu);	\
253 	__release_shared_ctx_lock(RCU);					\
254 	__v;									\
255 })
256 
257 /**
258  * srcu_dereference - fetch SRCU-protected pointer for later dereferencing
259  * @p: the pointer to fetch and protect for later dereferencing
260  * @ssp: pointer to the srcu_struct, which is used to check that we
261  *	really are in an SRCU read-side critical section.
262  *
263  * Makes rcu_dereference_check() do the dirty work.  If PROVE_RCU
264  * is enabled, invoking this outside of an RCU read-side critical
265  * section will result in an RCU-lockdep splat.
266  */
267 #define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0)
268 
269 /**
270  * srcu_dereference_notrace - no tracing and no lockdep calls from here
271  * @p: the pointer to fetch and protect for later dereferencing
272  * @ssp: pointer to the srcu_struct, which is used to check that we
273  *	really are in an SRCU read-side critical section.
274  */
275 #define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1)
276 
277 /**
278  * srcu_read_lock - register a new reader for an SRCU-protected structure.
279  * @ssp: srcu_struct in which to register the new reader.
280  *
281  * Enter an SRCU read-side critical section.  Note that SRCU read-side
282  * critical sections may be nested.  However, it is illegal to
283  * call anything that waits on an SRCU grace period for the same
284  * srcu_struct, whether directly or indirectly.  Please note that
285  * one way to indirectly wait on an SRCU grace period is to acquire
286  * a mutex that is held elsewhere while calling synchronize_srcu() or
287  * synchronize_srcu_expedited().
288  *
289  * The return value from srcu_read_lock() is guaranteed to be
290  * non-negative.  This value must be passed unaltered to the matching
291  * srcu_read_unlock().  Note that srcu_read_lock() and the matching
292  * srcu_read_unlock() must occur in the same context, for example, it is
293  * illegal to invoke srcu_read_unlock() in an irq handler if the matching
294  * srcu_read_lock() was invoked in process context.  Or, for that matter to
295  * invoke srcu_read_unlock() from one task and the matching srcu_read_lock()
296  * from another.
297  */
srcu_read_lock(struct srcu_struct * ssp)298 static inline int srcu_read_lock(struct srcu_struct *ssp)
299 	__acquires_shared(ssp)
300 {
301 	int retval;
302 
303 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL);
304 	retval = __srcu_read_lock(ssp);
305 	srcu_lock_acquire(&ssp->dep_map);
306 	return retval;
307 }
308 
309 /**
310  * srcu_read_lock_fast - register a new reader for an SRCU-protected structure.
311  * @ssp: srcu_struct in which to register the new reader.
312  *
313  * Enter an SRCU read-side critical section, but for a light-weight
314  * smp_mb()-free reader.  See srcu_read_lock() for more information.  This
315  * function is NMI-safe, in a manner similar to srcu_read_lock_nmisafe().
316  *
317  * For srcu_read_lock_fast() to be used on an srcu_struct structure,
318  * that structure must have been defined using either DEFINE_SRCU_FAST()
319  * or DEFINE_STATIC_SRCU_FAST() on the one hand or initialized with
320  * init_srcu_struct_fast() on the other.  Such an srcu_struct structure
321  * cannot be passed to any non-fast variant of srcu_read_{,un}lock() or
322  * srcu_{down,up}_read().  In kernels built with CONFIG_PROVE_RCU=y,
323  * __srcu_check_read_flavor() will complain bitterly if you ignore this
324  * restriction.
325  *
326  * Grace-period auto-expediting is disabled for SRCU-fast srcu_struct
327  * structures because SRCU-fast expedited grace periods invoke
328  * synchronize_rcu_expedited(), IPIs and all.  If you need expedited
329  * SRCU-fast grace periods, use synchronize_srcu_expedited().
330  *
331  * The srcu_read_lock_fast() function can be invoked only from those
332  * contexts where RCU is watching, that is, from contexts where it would
333  * be legal to invoke rcu_read_lock().  Otherwise, lockdep will complain.
334  */
srcu_read_lock_fast(struct srcu_struct * ssp)335 static inline struct srcu_ctr __percpu *srcu_read_lock_fast(struct srcu_struct *ssp) __acquires_shared(ssp)
336 	__acquires_shared(ssp)
337 {
338 	struct srcu_ctr __percpu *retval;
339 
340 	RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_lock_fast().");
341 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST);
342 	retval = __srcu_read_lock_fast(ssp);
343 	rcu_try_lock_acquire(&ssp->dep_map);
344 	return retval;
345 }
346 
347 /**
348  * srcu_read_lock_fast_updown - register a new reader for an SRCU-fast-updown structure.
349  * @ssp: srcu_struct in which to register the new reader.
350  *
351  * Enter an SRCU read-side critical section, but for a light-weight
352  * smp_mb()-free reader.  See srcu_read_lock() for more information.
353  * This function is compatible with srcu_down_read_fast(), but is not
354  * NMI-safe.
355  *
356  * For srcu_read_lock_fast_updown() to be used on an srcu_struct
357  * structure, that structure must have been defined using either
358  * DEFINE_SRCU_FAST_UPDOWN() or DEFINE_STATIC_SRCU_FAST_UPDOWN() on the one
359  * hand or initialized with init_srcu_struct_fast_updown() on the other.
360  * Such an srcu_struct structure cannot be passed to any non-fast-updown
361  * variant of srcu_read_{,un}lock() or srcu_{down,up}_read().  In kernels
362  * built with CONFIG_PROVE_RCU=y, __srcu_check_read_flavor() will complain
363  * bitterly if you ignore this * restriction.
364  *
365  * Grace-period auto-expediting is disabled for SRCU-fast-updown
366  * srcu_struct structures because SRCU-fast-updown expedited grace periods
367  * invoke synchronize_rcu_expedited(), IPIs and all.  If you need expedited
368  * SRCU-fast-updown grace periods, use synchronize_srcu_expedited().
369  *
370  * The srcu_read_lock_fast_updown() function can be invoked only from
371  * those contexts where RCU is watching, that is, from contexts where
372  * it would be legal to invoke rcu_read_lock().  Otherwise, lockdep will
373  * complain.
374  */
srcu_read_lock_fast_updown(struct srcu_struct * ssp)375 static inline struct srcu_ctr __percpu *srcu_read_lock_fast_updown(struct srcu_struct *ssp)
376 	__acquires_shared(ssp)
377 {
378 	struct srcu_ctr __percpu *retval;
379 
380 	RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_lock_fast_updown().");
381 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST_UPDOWN);
382 	retval = __srcu_read_lock_fast_updown(ssp);
383 	rcu_try_lock_acquire(&ssp->dep_map);
384 	return retval;
385 }
386 
387 /*
388  * Used by tracing, cannot be traced and cannot call lockdep.
389  * See srcu_read_lock_fast() for more information.
390  */
srcu_read_lock_fast_notrace(struct srcu_struct * ssp)391 static inline struct srcu_ctr __percpu *srcu_read_lock_fast_notrace(struct srcu_struct *ssp)
392 	__acquires_shared(ssp)
393 {
394 	struct srcu_ctr __percpu *retval;
395 
396 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST);
397 	retval = __srcu_read_lock_fast(ssp);
398 	return retval;
399 }
400 
401 /**
402  * srcu_down_read_fast - register a new reader for an SRCU-protected structure.
403  * @ssp: srcu_struct in which to register the new reader.
404  *
405  * Enter a semaphore-like SRCU read-side critical section, but for
406  * a light-weight smp_mb()-free reader.  See srcu_read_lock_fast() and
407  * srcu_down_read() for more information.
408  *
409  * The same srcu_struct may be used concurrently by srcu_down_read_fast()
410  * and srcu_read_lock_fast().  However, the same definition/initialization
411  * requirements called out for srcu_read_lock_fast_updown() apply.
412  */
srcu_down_read_fast(struct srcu_struct * ssp)413 static inline struct srcu_ctr __percpu *srcu_down_read_fast(struct srcu_struct *ssp) __acquires_shared(ssp)
414 {
415 	WARN_ON_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && in_nmi());
416 	RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_down_read_fast().");
417 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST_UPDOWN);
418 	return __srcu_read_lock_fast_updown(ssp);
419 }
420 
421 /**
422  * srcu_read_lock_nmisafe - register a new reader for an SRCU-protected structure.
423  * @ssp: srcu_struct in which to register the new reader.
424  *
425  * Enter an SRCU read-side critical section, but in an NMI-safe manner.
426  * See srcu_read_lock() for more information.
427  *
428  * If srcu_read_lock_nmisafe() is ever used on an srcu_struct structure,
429  * then none of the other flavors may be used, whether before, during,
430  * or after.
431  */
srcu_read_lock_nmisafe(struct srcu_struct * ssp)432 static inline int srcu_read_lock_nmisafe(struct srcu_struct *ssp)
433 	__acquires_shared(ssp)
434 {
435 	int retval;
436 
437 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NMI);
438 	retval = __srcu_read_lock_nmisafe(ssp);
439 	rcu_try_lock_acquire(&ssp->dep_map);
440 	return retval;
441 }
442 
443 /* Used by tracing, cannot be traced and cannot invoke lockdep. */
444 static inline notrace int
srcu_read_lock_notrace(struct srcu_struct * ssp)445 srcu_read_lock_notrace(struct srcu_struct *ssp)
446 	__acquires_shared(ssp)
447 {
448 	int retval;
449 
450 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL);
451 	retval = __srcu_read_lock(ssp);
452 	return retval;
453 }
454 
455 /**
456  * srcu_down_read - register a new reader for an SRCU-protected structure.
457  * @ssp: srcu_struct in which to register the new reader.
458  *
459  * Enter a semaphore-like SRCU read-side critical section.  Note that
460  * SRCU read-side critical sections may be nested.  However, it is
461  * illegal to call anything that waits on an SRCU grace period for the
462  * same srcu_struct, whether directly or indirectly.  Please note that
463  * one way to indirectly wait on an SRCU grace period is to acquire
464  * a mutex that is held elsewhere while calling synchronize_srcu() or
465  * synchronize_srcu_expedited().  But if you want lockdep to help you
466  * keep this stuff straight, you should instead use srcu_read_lock().
467  *
468  * The semaphore-like nature of srcu_down_read() means that the matching
469  * srcu_up_read() can be invoked from some other context, for example,
470  * from some other task or from an irq handler.  However, neither
471  * srcu_down_read() nor srcu_up_read() may be invoked from an NMI handler.
472  *
473  * Calls to srcu_down_read() may be nested, similar to the manner in
474  * which calls to down_read() may be nested.  The same srcu_struct may be
475  * used concurrently by srcu_down_read() and srcu_read_lock().
476  */
srcu_down_read(struct srcu_struct * ssp)477 static inline int srcu_down_read(struct srcu_struct *ssp)
478 	__acquires_shared(ssp)
479 {
480 	WARN_ON_ONCE(in_nmi());
481 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL);
482 	return __srcu_read_lock(ssp);
483 }
484 
485 /**
486  * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
487  * @ssp: srcu_struct in which to unregister the old reader.
488  * @idx: return value from corresponding srcu_read_lock().
489  *
490  * Exit an SRCU read-side critical section.
491  */
srcu_read_unlock(struct srcu_struct * ssp,int idx)492 static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx)
493 	__releases_shared(ssp)
494 {
495 	WARN_ON_ONCE(idx & ~0x1);
496 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL);
497 	srcu_lock_release(&ssp->dep_map);
498 	__srcu_read_unlock(ssp, idx);
499 }
500 
501 /**
502  * srcu_read_unlock_fast - unregister a old reader from an SRCU-protected structure.
503  * @ssp: srcu_struct in which to unregister the old reader.
504  * @scp: return value from corresponding srcu_read_lock_fast().
505  *
506  * Exit a light-weight SRCU read-side critical section.
507  */
srcu_read_unlock_fast(struct srcu_struct * ssp,struct srcu_ctr __percpu * scp)508 static inline void srcu_read_unlock_fast(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp)
509 	__releases_shared(ssp)
510 {
511 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST);
512 	srcu_lock_release(&ssp->dep_map);
513 	__srcu_read_unlock_fast(ssp, scp);
514 	RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_unlock_fast().");
515 }
516 
517 /**
518  * srcu_read_unlock_fast_updown - unregister a old reader from an SRCU-fast-updown structure.
519  * @ssp: srcu_struct in which to unregister the old reader.
520  * @scp: return value from corresponding srcu_read_lock_fast_updown().
521  *
522  * Exit an SRCU-fast-updown read-side critical section.
523  */
524 static inline void
srcu_read_unlock_fast_updown(struct srcu_struct * ssp,struct srcu_ctr __percpu * scp)525 srcu_read_unlock_fast_updown(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp) __releases_shared(ssp)
526 {
527 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST_UPDOWN);
528 	srcu_lock_release(&ssp->dep_map);
529 	__srcu_read_unlock_fast_updown(ssp, scp);
530 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
531 			 "RCU must be watching srcu_read_unlock_fast_updown().");
532 }
533 
534 /*
535  * Used by tracing, cannot be traced and cannot call lockdep.
536  * See srcu_read_unlock_fast() for more information.
537  */
srcu_read_unlock_fast_notrace(struct srcu_struct * ssp,struct srcu_ctr __percpu * scp)538 static inline void srcu_read_unlock_fast_notrace(struct srcu_struct *ssp,
539 						 struct srcu_ctr __percpu *scp) __releases_shared(ssp)
540 {
541 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST);
542 	__srcu_read_unlock_fast(ssp, scp);
543 }
544 
545 /**
546  * srcu_up_read_fast - unregister a old reader from an SRCU-protected structure.
547  * @ssp: srcu_struct in which to unregister the old reader.
548  * @scp: return value from corresponding srcu_read_lock_fast().
549  *
550  * Exit an SRCU read-side critical section, but not necessarily from
551  * the same context as the maching srcu_down_read_fast().
552  */
srcu_up_read_fast(struct srcu_struct * ssp,struct srcu_ctr __percpu * scp)553 static inline void srcu_up_read_fast(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp)
554 	__releases_shared(ssp)
555 {
556 	WARN_ON_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && in_nmi());
557 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST_UPDOWN);
558 	__srcu_read_unlock_fast_updown(ssp, scp);
559 	RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_up_read_fast_updown().");
560 }
561 
562 /**
563  * srcu_read_unlock_nmisafe - unregister a old reader from an SRCU-protected structure.
564  * @ssp: srcu_struct in which to unregister the old reader.
565  * @idx: return value from corresponding srcu_read_lock_nmisafe().
566  *
567  * Exit an SRCU read-side critical section, but in an NMI-safe manner.
568  */
srcu_read_unlock_nmisafe(struct srcu_struct * ssp,int idx)569 static inline void srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
570 	__releases_shared(ssp)
571 {
572 	WARN_ON_ONCE(idx & ~0x1);
573 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NMI);
574 	rcu_lock_release(&ssp->dep_map);
575 	__srcu_read_unlock_nmisafe(ssp, idx);
576 }
577 
578 /* Used by tracing, cannot be traced and cannot call lockdep. */
579 static inline notrace void
srcu_read_unlock_notrace(struct srcu_struct * ssp,int idx)580 srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases_shared(ssp)
581 {
582 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL);
583 	__srcu_read_unlock(ssp, idx);
584 }
585 
586 /**
587  * srcu_up_read - unregister a old reader from an SRCU-protected structure.
588  * @ssp: srcu_struct in which to unregister the old reader.
589  * @idx: return value from corresponding srcu_read_lock().
590  *
591  * Exit an SRCU read-side critical section, but not necessarily from
592  * the same context as the maching srcu_down_read().
593  */
srcu_up_read(struct srcu_struct * ssp,int idx)594 static inline void srcu_up_read(struct srcu_struct *ssp, int idx)
595 	__releases_shared(ssp)
596 {
597 	WARN_ON_ONCE(idx & ~0x1);
598 	WARN_ON_ONCE(in_nmi());
599 	srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL);
600 	__srcu_read_unlock(ssp, idx);
601 }
602 
603 /**
604  * smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock
605  *
606  * Converts the preceding srcu_read_unlock into a two-way memory barrier.
607  *
608  * Call this after srcu_read_unlock, to guarantee that all memory operations
609  * that occur after smp_mb__after_srcu_read_unlock will appear to happen after
610  * the preceding srcu_read_unlock.
611  */
smp_mb__after_srcu_read_unlock(void)612 static inline void smp_mb__after_srcu_read_unlock(void)
613 {
614 	/* __srcu_read_unlock has smp_mb() internally so nothing to do here. */
615 }
616 
617 /**
618  * smp_mb__after_srcu_read_lock - ensure full ordering after srcu_read_lock
619  *
620  * Converts the preceding srcu_read_lock into a two-way memory barrier.
621  *
622  * Call this after srcu_read_lock, to guarantee that all memory operations
623  * that occur after smp_mb__after_srcu_read_lock will appear to happen after
624  * the preceding srcu_read_lock.
625  */
smp_mb__after_srcu_read_lock(void)626 static inline void smp_mb__after_srcu_read_lock(void)
627 {
628 	/* __srcu_read_lock has smp_mb() internally so nothing to do here. */
629 }
630 
631 DEFINE_LOCK_GUARD_1(srcu, struct srcu_struct,
632 		    _T->idx = srcu_read_lock(_T->lock),
633 		    srcu_read_unlock(_T->lock, _T->idx),
634 		    int idx)
635 DECLARE_LOCK_GUARD_1_ATTRS(srcu, __acquires_shared(_T), __releases_shared(*(struct srcu_struct **)_T))
636 #define class_srcu_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(srcu, _T)
637 
638 DEFINE_LOCK_GUARD_1(srcu_fast, struct srcu_struct,
639 		    _T->scp = srcu_read_lock_fast(_T->lock),
640 		    srcu_read_unlock_fast(_T->lock, _T->scp),
641 		    struct srcu_ctr __percpu *scp)
642 DECLARE_LOCK_GUARD_1_ATTRS(srcu_fast, __acquires_shared(_T), __releases_shared(*(struct srcu_struct **)_T))
643 #define class_srcu_fast_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(srcu_fast, _T)
644 
645 DEFINE_LOCK_GUARD_1(srcu_fast_notrace, struct srcu_struct,
646 		    _T->scp = srcu_read_lock_fast_notrace(_T->lock),
647 		    srcu_read_unlock_fast_notrace(_T->lock, _T->scp),
648 		    struct srcu_ctr __percpu *scp)
649 DECLARE_LOCK_GUARD_1_ATTRS(srcu_fast_notrace, __acquires_shared(_T), __releases_shared(*(struct srcu_struct **)_T))
650 #define class_srcu_fast_notrace_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(srcu_fast_notrace, _T)
651 
652 DEFINE_LOCK_GUARD_1(srcu_fast_updown, struct srcu_struct,
653 		    _T->scp = srcu_read_lock_fast_updown(_T->lock),
654 		    srcu_read_unlock_fast_updown(_T->lock, _T->scp),
655 		    struct srcu_ctr __percpu *scp)
656 DECLARE_LOCK_GUARD_1_ATTRS(srcu_fast_updown, __acquires_shared(_T), __releases_shared(*(struct srcu_struct **)_T))
657 #define class_srcu_fast_updown_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(srcu_fast_updown, _T)
658 
659 #endif
660