1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Sleepable Read-Copy Update mechanism for mutual exclusion
5 * Copyright (C) IBM Corporation, 2006
6 * Copyright (C) Fujitsu, 2012
11 * For detailed explanation of Read-Copy Update mechanism see -
12 * Documentation/RCU/ *.txt
19 #include <linux/mutex.h>
20 #include <linux/rcupdate.h>
21 #include <linux/workqueue.h>
22 #include <linux/rcu_segcblist.h>
26 #ifdef CONFIG_DEBUG_LOCK_ALLOC
28 int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
29 struct lock_class_key *key);
31 #define init_srcu_struct(ssp) \
33 static struct lock_class_key __srcu_key; \
35 __init_srcu_struct((ssp), #ssp, &__srcu_key); \
38 #define __SRCU_DEP_MAP_INIT(srcu_name) .dep_map = { .name = #srcu_name },
39 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
41 int init_srcu_struct(struct srcu_struct *ssp);
43 #define __SRCU_DEP_MAP_INIT(srcu_name)
44 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
46 #ifdef CONFIG_TINY_SRCU
47 #include <linux/srcutiny.h>
48 #elif defined(CONFIG_TREE_SRCU)
49 #include <linux/srcutree.h>
51 #error "Unknown SRCU implementation specified to kernel configuration"
54 void call_srcu(struct srcu_struct *ssp, struct rcu_head *head,
55 void (*func)(struct rcu_head *head));
56 void cleanup_srcu_struct(struct srcu_struct *ssp);
57 int __srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp);
58 void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases(ssp);
59 void synchronize_srcu(struct srcu_struct *ssp);
61 #define SRCU_GET_STATE_COMPLETED 0x1
64 * get_completed_synchronize_srcu - Return a pre-completed polled state cookie
66 * Returns a value that poll_state_synchronize_srcu() will always treat
67 * as a cookie whose grace period has already completed.
69 static inline unsigned long get_completed_synchronize_srcu(void)
71 return SRCU_GET_STATE_COMPLETED;
74 unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp);
75 unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp);
76 bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie);
78 // Maximum number of unsigned long values corresponding to
79 // not-yet-completed SRCU grace periods.
80 #define NUM_ACTIVE_SRCU_POLL_OLDSTATE 2
83 * same_state_synchronize_srcu - Are two old-state values identical?
84 * @oldstate1: First old-state value.
85 * @oldstate2: Second old-state value.
87 * The two old-state values must have been obtained from either
88 * get_state_synchronize_srcu(), start_poll_synchronize_srcu(), or
89 * get_completed_synchronize_srcu(). Returns @true if the two values are
90 * identical and @false otherwise. This allows structures whose lifetimes
91 * are tracked by old-state values to push these values to a list header,
92 * allowing those structures to be slightly smaller.
94 static inline bool same_state_synchronize_srcu(unsigned long oldstate1, unsigned long oldstate2)
96 return oldstate1 == oldstate2;
99 #ifdef CONFIG_NEED_SRCU_NMI_SAFE
100 int __srcu_read_lock_nmisafe(struct srcu_struct *ssp) __acquires(ssp);
101 void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx) __releases(ssp);
103 static inline int __srcu_read_lock_nmisafe(struct srcu_struct *ssp)
105 return __srcu_read_lock(ssp);
107 static inline void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
109 __srcu_read_unlock(ssp, idx);
111 #endif /* CONFIG_NEED_SRCU_NMI_SAFE */
113 void srcu_init(void);
115 #ifdef CONFIG_DEBUG_LOCK_ALLOC
118 * srcu_read_lock_held - might we be in SRCU read-side critical section?
119 * @ssp: The srcu_struct structure to check
121 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU
122 * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC,
123 * this assumes we are in an SRCU read-side critical section unless it can
126 * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
127 * and while lockdep is disabled.
129 * Note that SRCU is based on its own statemachine and it doesn't
130 * relies on normal RCU, it can be called from the CPU which
131 * is in the idle loop from an RCU point of view or offline.
133 static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
135 if (!debug_lockdep_rcu_enabled())
137 return lock_is_held(&ssp->dep_map);
141 * Annotations provide deadlock detection for SRCU.
143 * Similar to other lockdep annotations, except there is an additional
144 * srcu_lock_sync(), which is basically an empty *write*-side critical section,
145 * see lock_sync() for more information.
148 /* Annotates a srcu_read_lock() */
149 static inline void srcu_lock_acquire(struct lockdep_map *map)
151 lock_map_acquire_read(map);
154 /* Annotates a srcu_read_lock() */
155 static inline void srcu_lock_release(struct lockdep_map *map)
157 lock_map_release(map);
160 /* Annotates a synchronize_srcu() */
161 static inline void srcu_lock_sync(struct lockdep_map *map)
166 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
168 static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
173 #define srcu_lock_acquire(m) do { } while (0)
174 #define srcu_lock_release(m) do { } while (0)
175 #define srcu_lock_sync(m) do { } while (0)
177 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
179 #define SRCU_NMI_UNKNOWN 0x0
180 #define SRCU_NMI_UNSAFE 0x1
181 #define SRCU_NMI_SAFE 0x2
183 #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_TREE_SRCU)
184 void srcu_check_nmi_safety(struct srcu_struct *ssp, bool nmi_safe);
186 static inline void srcu_check_nmi_safety(struct srcu_struct *ssp,
192 * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing
193 * @p: the pointer to fetch and protect for later dereferencing
194 * @ssp: pointer to the srcu_struct, which is used to check that we
195 * really are in an SRCU read-side critical section.
196 * @c: condition to check for update-side use
198 * If PROVE_RCU is enabled, invoking this outside of an RCU read-side
199 * critical section will result in an RCU-lockdep splat, unless @c evaluates
200 * to 1. The @c argument will normally be a logical expression containing
201 * lockdep_is_held() calls.
203 #define srcu_dereference_check(p, ssp, c) \
204 __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
205 (c) || srcu_read_lock_held(ssp), __rcu)
208 * srcu_dereference - fetch SRCU-protected pointer for later dereferencing
209 * @p: the pointer to fetch and protect for later dereferencing
210 * @ssp: pointer to the srcu_struct, which is used to check that we
211 * really are in an SRCU read-side critical section.
213 * Makes rcu_dereference_check() do the dirty work. If PROVE_RCU
214 * is enabled, invoking this outside of an RCU read-side critical
215 * section will result in an RCU-lockdep splat.
217 #define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0)
220 * srcu_dereference_notrace - no tracing and no lockdep calls from here
221 * @p: the pointer to fetch and protect for later dereferencing
222 * @ssp: pointer to the srcu_struct, which is used to check that we
223 * really are in an SRCU read-side critical section.
225 #define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1)
228 * srcu_read_lock - register a new reader for an SRCU-protected structure.
229 * @ssp: srcu_struct in which to register the new reader.
231 * Enter an SRCU read-side critical section. Note that SRCU read-side
232 * critical sections may be nested. However, it is illegal to
233 * call anything that waits on an SRCU grace period for the same
234 * srcu_struct, whether directly or indirectly. Please note that
235 * one way to indirectly wait on an SRCU grace period is to acquire
236 * a mutex that is held elsewhere while calling synchronize_srcu() or
237 * synchronize_srcu_expedited().
239 * Note that srcu_read_lock() and the matching srcu_read_unlock() must
240 * occur in the same context, for example, it is illegal to invoke
241 * srcu_read_unlock() in an irq handler if the matching srcu_read_lock()
242 * was invoked in process context.
244 static inline int srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp)
248 srcu_check_nmi_safety(ssp, false);
249 retval = __srcu_read_lock(ssp);
250 srcu_lock_acquire(&ssp->dep_map);
255 * srcu_read_lock_nmisafe - register a new reader for an SRCU-protected structure.
256 * @ssp: srcu_struct in which to register the new reader.
258 * Enter an SRCU read-side critical section, but in an NMI-safe manner.
259 * See srcu_read_lock() for more information.
261 static inline int srcu_read_lock_nmisafe(struct srcu_struct *ssp) __acquires(ssp)
265 srcu_check_nmi_safety(ssp, true);
266 retval = __srcu_read_lock_nmisafe(ssp);
267 rcu_try_lock_acquire(&ssp->dep_map);
271 /* Used by tracing, cannot be traced and cannot invoke lockdep. */
272 static inline notrace int
273 srcu_read_lock_notrace(struct srcu_struct *ssp) __acquires(ssp)
277 srcu_check_nmi_safety(ssp, false);
278 retval = __srcu_read_lock(ssp);
283 * srcu_down_read - register a new reader for an SRCU-protected structure.
284 * @ssp: srcu_struct in which to register the new reader.
286 * Enter a semaphore-like SRCU read-side critical section. Note that
287 * SRCU read-side critical sections may be nested. However, it is
288 * illegal to call anything that waits on an SRCU grace period for the
289 * same srcu_struct, whether directly or indirectly. Please note that
290 * one way to indirectly wait on an SRCU grace period is to acquire
291 * a mutex that is held elsewhere while calling synchronize_srcu() or
292 * synchronize_srcu_expedited(). But if you want lockdep to help you
293 * keep this stuff straight, you should instead use srcu_read_lock().
295 * The semaphore-like nature of srcu_down_read() means that the matching
296 * srcu_up_read() can be invoked from some other context, for example,
297 * from some other task or from an irq handler. However, neither
298 * srcu_down_read() nor srcu_up_read() may be invoked from an NMI handler.
300 * Calls to srcu_down_read() may be nested, similar to the manner in
301 * which calls to down_read() may be nested.
303 static inline int srcu_down_read(struct srcu_struct *ssp) __acquires(ssp)
305 WARN_ON_ONCE(in_nmi());
306 srcu_check_nmi_safety(ssp, false);
307 return __srcu_read_lock(ssp);
311 * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
312 * @ssp: srcu_struct in which to unregister the old reader.
313 * @idx: return value from corresponding srcu_read_lock().
315 * Exit an SRCU read-side critical section.
317 static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx)
320 WARN_ON_ONCE(idx & ~0x1);
321 srcu_check_nmi_safety(ssp, false);
322 srcu_lock_release(&ssp->dep_map);
323 __srcu_read_unlock(ssp, idx);
327 * srcu_read_unlock_nmisafe - unregister a old reader from an SRCU-protected structure.
328 * @ssp: srcu_struct in which to unregister the old reader.
329 * @idx: return value from corresponding srcu_read_lock().
331 * Exit an SRCU read-side critical section, but in an NMI-safe manner.
333 static inline void srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
336 WARN_ON_ONCE(idx & ~0x1);
337 srcu_check_nmi_safety(ssp, true);
338 rcu_lock_release(&ssp->dep_map);
339 __srcu_read_unlock_nmisafe(ssp, idx);
342 /* Used by tracing, cannot be traced and cannot call lockdep. */
343 static inline notrace void
344 srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases(ssp)
346 srcu_check_nmi_safety(ssp, false);
347 __srcu_read_unlock(ssp, idx);
351 * srcu_up_read - unregister a old reader from an SRCU-protected structure.
352 * @ssp: srcu_struct in which to unregister the old reader.
353 * @idx: return value from corresponding srcu_read_lock().
355 * Exit an SRCU read-side critical section, but not necessarily from
356 * the same context as the maching srcu_down_read().
358 static inline void srcu_up_read(struct srcu_struct *ssp, int idx)
361 WARN_ON_ONCE(idx & ~0x1);
362 WARN_ON_ONCE(in_nmi());
363 srcu_check_nmi_safety(ssp, false);
364 __srcu_read_unlock(ssp, idx);
368 * smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock
370 * Converts the preceding srcu_read_unlock into a two-way memory barrier.
372 * Call this after srcu_read_unlock, to guarantee that all memory operations
373 * that occur after smp_mb__after_srcu_read_unlock will appear to happen after
374 * the preceding srcu_read_unlock.
376 static inline void smp_mb__after_srcu_read_unlock(void)
378 /* __srcu_read_unlock has smp_mb() internally so nothing to do here. */
382 * smp_mb__after_srcu_read_lock - ensure full ordering after srcu_read_lock
384 * Converts the preceding srcu_read_lock into a two-way memory barrier.
386 * Call this after srcu_read_lock, to guarantee that all memory operations
387 * that occur after smp_mb__after_srcu_read_lock will appear to happen after
388 * the preceding srcu_read_lock.
390 static inline void smp_mb__after_srcu_read_lock(void)
392 /* __srcu_read_lock has smp_mb() internally so nothing to do here. */
395 DEFINE_LOCK_GUARD_1(srcu, struct srcu_struct,
396 _T->idx = srcu_read_lock(_T->lock),
397 srcu_read_unlock(_T->lock, _T->idx),