xref: /linux-6.15/include/linux/srcu.h (revision cef50120)
1621934eeSPaul E. McKenney /*
2621934eeSPaul E. McKenney  * Sleepable Read-Copy Update mechanism for mutual exclusion
3621934eeSPaul E. McKenney  *
4621934eeSPaul E. McKenney  * This program is free software; you can redistribute it and/or modify
5621934eeSPaul E. McKenney  * it under the terms of the GNU General Public License as published by
6621934eeSPaul E. McKenney  * the Free Software Foundation; either version 2 of the License, or
7621934eeSPaul E. McKenney  * (at your option) any later version.
8621934eeSPaul E. McKenney  *
9621934eeSPaul E. McKenney  * This program is distributed in the hope that it will be useful,
10621934eeSPaul E. McKenney  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11621934eeSPaul E. McKenney  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12621934eeSPaul E. McKenney  * GNU General Public License for more details.
13621934eeSPaul E. McKenney  *
14621934eeSPaul E. McKenney  * You should have received a copy of the GNU General Public License
15621934eeSPaul E. McKenney  * along with this program; if not, write to the Free Software
16621934eeSPaul E. McKenney  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17621934eeSPaul E. McKenney  *
18621934eeSPaul E. McKenney  * Copyright (C) IBM Corporation, 2006
19621934eeSPaul E. McKenney  *
20621934eeSPaul E. McKenney  * Author: Paul McKenney <[email protected]>
21621934eeSPaul E. McKenney  *
22621934eeSPaul E. McKenney  * For detailed explanation of Read-Copy Update mechanism see -
23621934eeSPaul E. McKenney  * 		Documentation/RCU/ *.txt
24621934eeSPaul E. McKenney  *
25621934eeSPaul E. McKenney  */
26621934eeSPaul E. McKenney 
27eabc0694SAlan Stern #ifndef _LINUX_SRCU_H
28eabc0694SAlan Stern #define _LINUX_SRCU_H
29eabc0694SAlan Stern 
30d14aada8SPaul E. McKenney #include <linux/mutex.h>
31ff195cb6SPaul E. McKenney #include <linux/rcupdate.h>
32d14aada8SPaul E. McKenney 
33621934eeSPaul E. McKenney struct srcu_struct_array {
34*cef50120SPaul E. McKenney 	unsigned long c[2];
35621934eeSPaul E. McKenney };
36621934eeSPaul E. McKenney 
37*cef50120SPaul E. McKenney /* Bit definitions for field ->c above and ->snap below. */
38*cef50120SPaul E. McKenney #define SRCU_USAGE_BITS		2
39*cef50120SPaul E. McKenney #define SRCU_REF_MASK		(ULONG_MAX >> SRCU_USAGE_BITS)
40*cef50120SPaul E. McKenney #define SRCU_USAGE_COUNT	(SRCU_REF_MASK + 1)
41*cef50120SPaul E. McKenney 
42621934eeSPaul E. McKenney struct srcu_struct {
43*cef50120SPaul E. McKenney 	unsigned completed;
4443cf38ebSTejun Heo 	struct srcu_struct_array __percpu *per_cpu_ref;
45621934eeSPaul E. McKenney 	struct mutex mutex;
46*cef50120SPaul E. McKenney 	unsigned long snap[NR_CPUS];
47632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
48632ee200SPaul E. McKenney 	struct lockdep_map dep_map;
49632ee200SPaul E. McKenney #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
50621934eeSPaul E. McKenney };
51621934eeSPaul E. McKenney 
52621934eeSPaul E. McKenney #ifndef CONFIG_PREEMPT
53621934eeSPaul E. McKenney #define srcu_barrier() barrier()
54621934eeSPaul E. McKenney #else /* #ifndef CONFIG_PREEMPT */
55621934eeSPaul E. McKenney #define srcu_barrier()
56621934eeSPaul E. McKenney #endif /* #else #ifndef CONFIG_PREEMPT */
57621934eeSPaul E. McKenney 
58632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
59632ee200SPaul E. McKenney 
60632ee200SPaul E. McKenney int __init_srcu_struct(struct srcu_struct *sp, const char *name,
61632ee200SPaul E. McKenney 		       struct lock_class_key *key);
62632ee200SPaul E. McKenney 
63632ee200SPaul E. McKenney #define init_srcu_struct(sp) \
64632ee200SPaul E. McKenney ({ \
65632ee200SPaul E. McKenney 	static struct lock_class_key __srcu_key; \
66632ee200SPaul E. McKenney 	\
67632ee200SPaul E. McKenney 	__init_srcu_struct((sp), #sp, &__srcu_key); \
68632ee200SPaul E. McKenney })
69632ee200SPaul E. McKenney 
70632ee200SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
71632ee200SPaul E. McKenney 
72e6a92013SAlan Stern int init_srcu_struct(struct srcu_struct *sp);
73632ee200SPaul E. McKenney 
74632ee200SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
75632ee200SPaul E. McKenney 
76621934eeSPaul E. McKenney void cleanup_srcu_struct(struct srcu_struct *sp);
77632ee200SPaul E. McKenney int __srcu_read_lock(struct srcu_struct *sp) __acquires(sp);
78632ee200SPaul E. McKenney void __srcu_read_unlock(struct srcu_struct *sp, int idx) __releases(sp);
79621934eeSPaul E. McKenney void synchronize_srcu(struct srcu_struct *sp);
800cd397d3SPaul E. McKenney void synchronize_srcu_expedited(struct srcu_struct *sp);
81621934eeSPaul E. McKenney long srcu_batches_completed(struct srcu_struct *sp);
82eabc0694SAlan Stern 
83632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
84632ee200SPaul E. McKenney 
85632ee200SPaul E. McKenney /**
86632ee200SPaul E. McKenney  * srcu_read_lock_held - might we be in SRCU read-side critical section?
87632ee200SPaul E. McKenney  *
88d20200b5SPaul E. McKenney  * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU
89d20200b5SPaul E. McKenney  * read-side critical section.  In absence of CONFIG_DEBUG_LOCK_ALLOC,
90632ee200SPaul E. McKenney  * this assumes we are in an SRCU read-side critical section unless it can
91632ee200SPaul E. McKenney  * prove otherwise.
92ff195cb6SPaul E. McKenney  *
93867f236bSPaul E. McKenney  * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
94867f236bSPaul E. McKenney  * and while lockdep is disabled.
95867f236bSPaul E. McKenney  *
96ff195cb6SPaul E. McKenney  * Note that if the CPU is in the idle loop from an RCU point of view
97ff195cb6SPaul E. McKenney  * (ie: that we are in the section between rcu_idle_enter() and
98ff195cb6SPaul E. McKenney  * rcu_idle_exit()) then srcu_read_lock_held() returns false even if
99ff195cb6SPaul E. McKenney  * the CPU did an srcu_read_lock().  The reason for this is that RCU
100ff195cb6SPaul E. McKenney  * ignores CPUs that are in such a section, considering these as in
101ff195cb6SPaul E. McKenney  * extended quiescent state, so such a CPU is effectively never in an
102ff195cb6SPaul E. McKenney  * RCU read-side critical section regardless of what RCU primitives it
103ff195cb6SPaul E. McKenney  * invokes.  This state of affairs is required --- we need to keep an
104ff195cb6SPaul E. McKenney  * RCU-free window in idle where the CPU may possibly enter into low
105ff195cb6SPaul E. McKenney  * power mode. This way we can notice an extended quiescent state to
106ff195cb6SPaul E. McKenney  * other CPUs that started a grace period. Otherwise we would delay any
107ff195cb6SPaul E. McKenney  * grace period as long as we run in the idle task.
108c0d6d01bSPaul E. McKenney  *
109c0d6d01bSPaul E. McKenney  * Similarly, we avoid claiming an SRCU read lock held if the current
110c0d6d01bSPaul E. McKenney  * CPU is offline.
111632ee200SPaul E. McKenney  */
112632ee200SPaul E. McKenney static inline int srcu_read_lock_held(struct srcu_struct *sp)
113632ee200SPaul E. McKenney {
114867f236bSPaul E. McKenney 	if (!debug_lockdep_rcu_enabled())
115632ee200SPaul E. McKenney 		return 1;
116c0d6d01bSPaul E. McKenney 	if (rcu_is_cpu_idle())
117c0d6d01bSPaul E. McKenney 		return 0;
118c0d6d01bSPaul E. McKenney 	if (!rcu_lockdep_current_cpu_online())
119c0d6d01bSPaul E. McKenney 		return 0;
120ff195cb6SPaul E. McKenney 	return lock_is_held(&sp->dep_map);
121632ee200SPaul E. McKenney }
122632ee200SPaul E. McKenney 
123632ee200SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
124632ee200SPaul E. McKenney 
125632ee200SPaul E. McKenney static inline int srcu_read_lock_held(struct srcu_struct *sp)
126632ee200SPaul E. McKenney {
127632ee200SPaul E. McKenney 	return 1;
128632ee200SPaul E. McKenney }
129632ee200SPaul E. McKenney 
130632ee200SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
131632ee200SPaul E. McKenney 
132632ee200SPaul E. McKenney /**
133ca5ecddfSPaul E. McKenney  * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing
134ca5ecddfSPaul E. McKenney  * @p: the pointer to fetch and protect for later dereferencing
135ca5ecddfSPaul E. McKenney  * @sp: pointer to the srcu_struct, which is used to check that we
136ca5ecddfSPaul E. McKenney  *	really are in an SRCU read-side critical section.
137ca5ecddfSPaul E. McKenney  * @c: condition to check for update-side use
138c26d34a5SPaul E. McKenney  *
139ca5ecddfSPaul E. McKenney  * If PROVE_RCU is enabled, invoking this outside of an RCU read-side
140ca5ecddfSPaul E. McKenney  * critical section will result in an RCU-lockdep splat, unless @c evaluates
141ca5ecddfSPaul E. McKenney  * to 1.  The @c argument will normally be a logical expression containing
142ca5ecddfSPaul E. McKenney  * lockdep_is_held() calls.
143c26d34a5SPaul E. McKenney  */
144ca5ecddfSPaul E. McKenney #define srcu_dereference_check(p, sp, c) \
145ca5ecddfSPaul E. McKenney 	__rcu_dereference_check((p), srcu_read_lock_held(sp) || (c), __rcu)
146ca5ecddfSPaul E. McKenney 
147ca5ecddfSPaul E. McKenney /**
148ca5ecddfSPaul E. McKenney  * srcu_dereference - fetch SRCU-protected pointer for later dereferencing
149ca5ecddfSPaul E. McKenney  * @p: the pointer to fetch and protect for later dereferencing
150ca5ecddfSPaul E. McKenney  * @sp: pointer to the srcu_struct, which is used to check that we
151ca5ecddfSPaul E. McKenney  *	really are in an SRCU read-side critical section.
152ca5ecddfSPaul E. McKenney  *
153ca5ecddfSPaul E. McKenney  * Makes rcu_dereference_check() do the dirty work.  If PROVE_RCU
154ca5ecddfSPaul E. McKenney  * is enabled, invoking this outside of an RCU read-side critical
155ca5ecddfSPaul E. McKenney  * section will result in an RCU-lockdep splat.
156ca5ecddfSPaul E. McKenney  */
157ca5ecddfSPaul E. McKenney #define srcu_dereference(p, sp) srcu_dereference_check((p), (sp), 0)
158c26d34a5SPaul E. McKenney 
159c26d34a5SPaul E. McKenney /**
160632ee200SPaul E. McKenney  * srcu_read_lock - register a new reader for an SRCU-protected structure.
161632ee200SPaul E. McKenney  * @sp: srcu_struct in which to register the new reader.
162632ee200SPaul E. McKenney  *
163632ee200SPaul E. McKenney  * Enter an SRCU read-side critical section.  Note that SRCU read-side
16473d4da4dSPaul E. McKenney  * critical sections may be nested.  However, it is illegal to
16573d4da4dSPaul E. McKenney  * call anything that waits on an SRCU grace period for the same
16673d4da4dSPaul E. McKenney  * srcu_struct, whether directly or indirectly.  Please note that
16773d4da4dSPaul E. McKenney  * one way to indirectly wait on an SRCU grace period is to acquire
16873d4da4dSPaul E. McKenney  * a mutex that is held elsewhere while calling synchronize_srcu() or
16973d4da4dSPaul E. McKenney  * synchronize_srcu_expedited().
1703842a083SPaul E. McKenney  *
1713842a083SPaul E. McKenney  * Note that srcu_read_lock() and the matching srcu_read_unlock() must
1723842a083SPaul E. McKenney  * occur in the same context, for example, it is illegal to invoke
1733842a083SPaul E. McKenney  * srcu_read_unlock() in an irq handler if the matching srcu_read_lock()
1743842a083SPaul E. McKenney  * was invoked in process context.
175632ee200SPaul E. McKenney  */
176632ee200SPaul E. McKenney static inline int srcu_read_lock(struct srcu_struct *sp) __acquires(sp)
177632ee200SPaul E. McKenney {
178632ee200SPaul E. McKenney 	int retval = __srcu_read_lock(sp);
179632ee200SPaul E. McKenney 
180ff195cb6SPaul E. McKenney 	rcu_lock_acquire(&(sp)->dep_map);
181bde23c68SHeiko Carstens 	rcu_lockdep_assert(!rcu_is_cpu_idle(),
182bde23c68SHeiko Carstens 			   "srcu_read_lock() used illegally while idle");
183632ee200SPaul E. McKenney 	return retval;
184632ee200SPaul E. McKenney }
185632ee200SPaul E. McKenney 
186632ee200SPaul E. McKenney /**
187632ee200SPaul E. McKenney  * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
188632ee200SPaul E. McKenney  * @sp: srcu_struct in which to unregister the old reader.
189632ee200SPaul E. McKenney  * @idx: return value from corresponding srcu_read_lock().
190632ee200SPaul E. McKenney  *
191632ee200SPaul E. McKenney  * Exit an SRCU read-side critical section.
192632ee200SPaul E. McKenney  */
193632ee200SPaul E. McKenney static inline void srcu_read_unlock(struct srcu_struct *sp, int idx)
194632ee200SPaul E. McKenney 	__releases(sp)
195632ee200SPaul E. McKenney {
196bde23c68SHeiko Carstens 	rcu_lockdep_assert(!rcu_is_cpu_idle(),
197bde23c68SHeiko Carstens 			   "srcu_read_unlock() used illegally while idle");
198ff195cb6SPaul E. McKenney 	rcu_lock_release(&(sp)->dep_map);
199632ee200SPaul E. McKenney 	__srcu_read_unlock(sp, idx);
200632ee200SPaul E. McKenney }
201632ee200SPaul E. McKenney 
2020c53dd8bSPaul E. McKenney /**
2030c53dd8bSPaul E. McKenney  * srcu_read_lock_raw - register a new reader for an SRCU-protected structure.
2040c53dd8bSPaul E. McKenney  * @sp: srcu_struct in which to register the new reader.
2050c53dd8bSPaul E. McKenney  *
2060c53dd8bSPaul E. McKenney  * Enter an SRCU read-side critical section.  Similar to srcu_read_lock(),
2070c53dd8bSPaul E. McKenney  * but avoids the RCU-lockdep checking.  This means that it is legal to
2080c53dd8bSPaul E. McKenney  * use srcu_read_lock_raw() in one context, for example, in an exception
2090c53dd8bSPaul E. McKenney  * handler, and then have the matching srcu_read_unlock_raw() in another
2100c53dd8bSPaul E. McKenney  * context, for example in the task that took the exception.
2110c53dd8bSPaul E. McKenney  *
2120c53dd8bSPaul E. McKenney  * However, the entire SRCU read-side critical section must reside within a
2130c53dd8bSPaul E. McKenney  * single task.  For example, beware of using srcu_read_lock_raw() in
2140c53dd8bSPaul E. McKenney  * a device interrupt handler and srcu_read_unlock() in the interrupted
2150c53dd8bSPaul E. McKenney  * task:  This will not work if interrupts are threaded.
2160c53dd8bSPaul E. McKenney  */
2170c53dd8bSPaul E. McKenney static inline int srcu_read_lock_raw(struct srcu_struct *sp)
2180c53dd8bSPaul E. McKenney {
2190c53dd8bSPaul E. McKenney 	unsigned long flags;
2200c53dd8bSPaul E. McKenney 	int ret;
2210c53dd8bSPaul E. McKenney 
2220c53dd8bSPaul E. McKenney 	local_irq_save(flags);
2230c53dd8bSPaul E. McKenney 	ret =  __srcu_read_lock(sp);
2240c53dd8bSPaul E. McKenney 	local_irq_restore(flags);
2250c53dd8bSPaul E. McKenney 	return ret;
2260c53dd8bSPaul E. McKenney }
2270c53dd8bSPaul E. McKenney 
2280c53dd8bSPaul E. McKenney /**
2290c53dd8bSPaul E. McKenney  * srcu_read_unlock_raw - unregister reader from an SRCU-protected structure.
2300c53dd8bSPaul E. McKenney  * @sp: srcu_struct in which to unregister the old reader.
2310c53dd8bSPaul E. McKenney  * @idx: return value from corresponding srcu_read_lock_raw().
2320c53dd8bSPaul E. McKenney  *
2330c53dd8bSPaul E. McKenney  * Exit an SRCU read-side critical section without lockdep-RCU checking.
2340c53dd8bSPaul E. McKenney  * See srcu_read_lock_raw() for more details.
2350c53dd8bSPaul E. McKenney  */
2360c53dd8bSPaul E. McKenney static inline void srcu_read_unlock_raw(struct srcu_struct *sp, int idx)
2370c53dd8bSPaul E. McKenney {
2380c53dd8bSPaul E. McKenney 	unsigned long flags;
2390c53dd8bSPaul E. McKenney 
2400c53dd8bSPaul E. McKenney 	local_irq_save(flags);
2410c53dd8bSPaul E. McKenney 	__srcu_read_unlock(sp, idx);
2420c53dd8bSPaul E. McKenney 	local_irq_restore(flags);
2430c53dd8bSPaul E. McKenney }
2440c53dd8bSPaul E. McKenney 
245eabc0694SAlan Stern #endif
246