xref: /linux-6.15/include/linux/srcu.h (revision 0c53dd8b)
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 {
34621934eeSPaul E. McKenney 	int c[2];
35621934eeSPaul E. McKenney };
36621934eeSPaul E. McKenney 
37621934eeSPaul E. McKenney struct srcu_struct {
38621934eeSPaul E. McKenney 	int completed;
3943cf38ebSTejun Heo 	struct srcu_struct_array __percpu *per_cpu_ref;
40621934eeSPaul E. McKenney 	struct mutex mutex;
41632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
42632ee200SPaul E. McKenney 	struct lockdep_map dep_map;
43632ee200SPaul E. McKenney #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
44621934eeSPaul E. McKenney };
45621934eeSPaul E. McKenney 
46621934eeSPaul E. McKenney #ifndef CONFIG_PREEMPT
47621934eeSPaul E. McKenney #define srcu_barrier() barrier()
48621934eeSPaul E. McKenney #else /* #ifndef CONFIG_PREEMPT */
49621934eeSPaul E. McKenney #define srcu_barrier()
50621934eeSPaul E. McKenney #endif /* #else #ifndef CONFIG_PREEMPT */
51621934eeSPaul E. McKenney 
52632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
53632ee200SPaul E. McKenney 
54632ee200SPaul E. McKenney int __init_srcu_struct(struct srcu_struct *sp, const char *name,
55632ee200SPaul E. McKenney 		       struct lock_class_key *key);
56632ee200SPaul E. McKenney 
57632ee200SPaul E. McKenney #define init_srcu_struct(sp) \
58632ee200SPaul E. McKenney ({ \
59632ee200SPaul E. McKenney 	static struct lock_class_key __srcu_key; \
60632ee200SPaul E. McKenney 	\
61632ee200SPaul E. McKenney 	__init_srcu_struct((sp), #sp, &__srcu_key); \
62632ee200SPaul E. McKenney })
63632ee200SPaul E. McKenney 
64632ee200SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
65632ee200SPaul E. McKenney 
66e6a92013SAlan Stern int init_srcu_struct(struct srcu_struct *sp);
67632ee200SPaul E. McKenney 
68632ee200SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
69632ee200SPaul E. McKenney 
70621934eeSPaul E. McKenney void cleanup_srcu_struct(struct srcu_struct *sp);
71632ee200SPaul E. McKenney int __srcu_read_lock(struct srcu_struct *sp) __acquires(sp);
72632ee200SPaul E. McKenney void __srcu_read_unlock(struct srcu_struct *sp, int idx) __releases(sp);
73621934eeSPaul E. McKenney void synchronize_srcu(struct srcu_struct *sp);
740cd397d3SPaul E. McKenney void synchronize_srcu_expedited(struct srcu_struct *sp);
75621934eeSPaul E. McKenney long srcu_batches_completed(struct srcu_struct *sp);
76eabc0694SAlan Stern 
77632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC
78632ee200SPaul E. McKenney 
79632ee200SPaul E. McKenney /**
80632ee200SPaul E. McKenney  * srcu_read_lock_held - might we be in SRCU read-side critical section?
81632ee200SPaul E. McKenney  *
82d20200b5SPaul E. McKenney  * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU
83d20200b5SPaul E. McKenney  * read-side critical section.  In absence of CONFIG_DEBUG_LOCK_ALLOC,
84632ee200SPaul E. McKenney  * this assumes we are in an SRCU read-side critical section unless it can
85632ee200SPaul E. McKenney  * prove otherwise.
86ff195cb6SPaul E. McKenney  *
87867f236bSPaul E. McKenney  * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
88867f236bSPaul E. McKenney  * and while lockdep is disabled.
89867f236bSPaul E. McKenney  *
90ff195cb6SPaul E. McKenney  * Note that if the CPU is in the idle loop from an RCU point of view
91ff195cb6SPaul E. McKenney  * (ie: that we are in the section between rcu_idle_enter() and
92ff195cb6SPaul E. McKenney  * rcu_idle_exit()) then srcu_read_lock_held() returns false even if
93ff195cb6SPaul E. McKenney  * the CPU did an srcu_read_lock().  The reason for this is that RCU
94ff195cb6SPaul E. McKenney  * ignores CPUs that are in such a section, considering these as in
95ff195cb6SPaul E. McKenney  * extended quiescent state, so such a CPU is effectively never in an
96ff195cb6SPaul E. McKenney  * RCU read-side critical section regardless of what RCU primitives it
97ff195cb6SPaul E. McKenney  * invokes.  This state of affairs is required --- we need to keep an
98ff195cb6SPaul E. McKenney  * RCU-free window in idle where the CPU may possibly enter into low
99ff195cb6SPaul E. McKenney  * power mode. This way we can notice an extended quiescent state to
100ff195cb6SPaul E. McKenney  * other CPUs that started a grace period. Otherwise we would delay any
101ff195cb6SPaul E. McKenney  * grace period as long as we run in the idle task.
102632ee200SPaul E. McKenney  */
103632ee200SPaul E. McKenney static inline int srcu_read_lock_held(struct srcu_struct *sp)
104632ee200SPaul E. McKenney {
105ff195cb6SPaul E. McKenney 	if (rcu_is_cpu_idle())
106ff195cb6SPaul E. McKenney 		return 0;
107ff195cb6SPaul E. McKenney 
108867f236bSPaul E. McKenney 	if (!debug_lockdep_rcu_enabled())
109632ee200SPaul E. McKenney 		return 1;
110ff195cb6SPaul E. McKenney 
111ff195cb6SPaul E. McKenney 	return lock_is_held(&sp->dep_map);
112632ee200SPaul E. McKenney }
113632ee200SPaul E. McKenney 
114632ee200SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
115632ee200SPaul E. McKenney 
116632ee200SPaul E. McKenney static inline int srcu_read_lock_held(struct srcu_struct *sp)
117632ee200SPaul E. McKenney {
118632ee200SPaul E. McKenney 	return 1;
119632ee200SPaul E. McKenney }
120632ee200SPaul E. McKenney 
121632ee200SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
122632ee200SPaul E. McKenney 
123632ee200SPaul E. McKenney /**
124ca5ecddfSPaul E. McKenney  * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing
125ca5ecddfSPaul E. McKenney  * @p: the pointer to fetch and protect for later dereferencing
126ca5ecddfSPaul E. McKenney  * @sp: pointer to the srcu_struct, which is used to check that we
127ca5ecddfSPaul E. McKenney  *	really are in an SRCU read-side critical section.
128ca5ecddfSPaul E. McKenney  * @c: condition to check for update-side use
129c26d34a5SPaul E. McKenney  *
130ca5ecddfSPaul E. McKenney  * If PROVE_RCU is enabled, invoking this outside of an RCU read-side
131ca5ecddfSPaul E. McKenney  * critical section will result in an RCU-lockdep splat, unless @c evaluates
132ca5ecddfSPaul E. McKenney  * to 1.  The @c argument will normally be a logical expression containing
133ca5ecddfSPaul E. McKenney  * lockdep_is_held() calls.
134c26d34a5SPaul E. McKenney  */
135ca5ecddfSPaul E. McKenney #define srcu_dereference_check(p, sp, c) \
136ca5ecddfSPaul E. McKenney 	__rcu_dereference_check((p), srcu_read_lock_held(sp) || (c), __rcu)
137ca5ecddfSPaul E. McKenney 
138ca5ecddfSPaul E. McKenney /**
139ca5ecddfSPaul E. McKenney  * srcu_dereference - fetch SRCU-protected pointer for later dereferencing
140ca5ecddfSPaul E. McKenney  * @p: the pointer to fetch and protect for later dereferencing
141ca5ecddfSPaul E. McKenney  * @sp: pointer to the srcu_struct, which is used to check that we
142ca5ecddfSPaul E. McKenney  *	really are in an SRCU read-side critical section.
143ca5ecddfSPaul E. McKenney  *
144ca5ecddfSPaul E. McKenney  * Makes rcu_dereference_check() do the dirty work.  If PROVE_RCU
145ca5ecddfSPaul E. McKenney  * is enabled, invoking this outside of an RCU read-side critical
146ca5ecddfSPaul E. McKenney  * section will result in an RCU-lockdep splat.
147ca5ecddfSPaul E. McKenney  */
148ca5ecddfSPaul E. McKenney #define srcu_dereference(p, sp) srcu_dereference_check((p), (sp), 0)
149c26d34a5SPaul E. McKenney 
150c26d34a5SPaul E. McKenney /**
151632ee200SPaul E. McKenney  * srcu_read_lock - register a new reader for an SRCU-protected structure.
152632ee200SPaul E. McKenney  * @sp: srcu_struct in which to register the new reader.
153632ee200SPaul E. McKenney  *
154632ee200SPaul E. McKenney  * Enter an SRCU read-side critical section.  Note that SRCU read-side
15573d4da4dSPaul E. McKenney  * critical sections may be nested.  However, it is illegal to
15673d4da4dSPaul E. McKenney  * call anything that waits on an SRCU grace period for the same
15773d4da4dSPaul E. McKenney  * srcu_struct, whether directly or indirectly.  Please note that
15873d4da4dSPaul E. McKenney  * one way to indirectly wait on an SRCU grace period is to acquire
15973d4da4dSPaul E. McKenney  * a mutex that is held elsewhere while calling synchronize_srcu() or
16073d4da4dSPaul E. McKenney  * synchronize_srcu_expedited().
161632ee200SPaul E. McKenney  */
162632ee200SPaul E. McKenney static inline int srcu_read_lock(struct srcu_struct *sp) __acquires(sp)
163632ee200SPaul E. McKenney {
164632ee200SPaul E. McKenney 	int retval = __srcu_read_lock(sp);
165632ee200SPaul E. McKenney 
166ff195cb6SPaul E. McKenney 	rcu_lock_acquire(&(sp)->dep_map);
167632ee200SPaul E. McKenney 	return retval;
168632ee200SPaul E. McKenney }
169632ee200SPaul E. McKenney 
170632ee200SPaul E. McKenney /**
171632ee200SPaul E. McKenney  * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
172632ee200SPaul E. McKenney  * @sp: srcu_struct in which to unregister the old reader.
173632ee200SPaul E. McKenney  * @idx: return value from corresponding srcu_read_lock().
174632ee200SPaul E. McKenney  *
175632ee200SPaul E. McKenney  * Exit an SRCU read-side critical section.
176632ee200SPaul E. McKenney  */
177632ee200SPaul E. McKenney static inline void srcu_read_unlock(struct srcu_struct *sp, int idx)
178632ee200SPaul E. McKenney 	__releases(sp)
179632ee200SPaul E. McKenney {
180ff195cb6SPaul E. McKenney 	rcu_lock_release(&(sp)->dep_map);
181632ee200SPaul E. McKenney 	__srcu_read_unlock(sp, idx);
182632ee200SPaul E. McKenney }
183632ee200SPaul E. McKenney 
184*0c53dd8bSPaul E. McKenney /**
185*0c53dd8bSPaul E. McKenney  * srcu_read_lock_raw - register a new reader for an SRCU-protected structure.
186*0c53dd8bSPaul E. McKenney  * @sp: srcu_struct in which to register the new reader.
187*0c53dd8bSPaul E. McKenney  *
188*0c53dd8bSPaul E. McKenney  * Enter an SRCU read-side critical section.  Similar to srcu_read_lock(),
189*0c53dd8bSPaul E. McKenney  * but avoids the RCU-lockdep checking.  This means that it is legal to
190*0c53dd8bSPaul E. McKenney  * use srcu_read_lock_raw() in one context, for example, in an exception
191*0c53dd8bSPaul E. McKenney  * handler, and then have the matching srcu_read_unlock_raw() in another
192*0c53dd8bSPaul E. McKenney  * context, for example in the task that took the exception.
193*0c53dd8bSPaul E. McKenney  *
194*0c53dd8bSPaul E. McKenney  * However, the entire SRCU read-side critical section must reside within a
195*0c53dd8bSPaul E. McKenney  * single task.  For example, beware of using srcu_read_lock_raw() in
196*0c53dd8bSPaul E. McKenney  * a device interrupt handler and srcu_read_unlock() in the interrupted
197*0c53dd8bSPaul E. McKenney  * task:  This will not work if interrupts are threaded.
198*0c53dd8bSPaul E. McKenney  */
199*0c53dd8bSPaul E. McKenney static inline int srcu_read_lock_raw(struct srcu_struct *sp)
200*0c53dd8bSPaul E. McKenney {
201*0c53dd8bSPaul E. McKenney 	unsigned long flags;
202*0c53dd8bSPaul E. McKenney 	int ret;
203*0c53dd8bSPaul E. McKenney 
204*0c53dd8bSPaul E. McKenney 	local_irq_save(flags);
205*0c53dd8bSPaul E. McKenney 	ret =  __srcu_read_lock(sp);
206*0c53dd8bSPaul E. McKenney 	local_irq_restore(flags);
207*0c53dd8bSPaul E. McKenney 	return ret;
208*0c53dd8bSPaul E. McKenney }
209*0c53dd8bSPaul E. McKenney 
210*0c53dd8bSPaul E. McKenney /**
211*0c53dd8bSPaul E. McKenney  * srcu_read_unlock_raw - unregister reader from an SRCU-protected structure.
212*0c53dd8bSPaul E. McKenney  * @sp: srcu_struct in which to unregister the old reader.
213*0c53dd8bSPaul E. McKenney  * @idx: return value from corresponding srcu_read_lock_raw().
214*0c53dd8bSPaul E. McKenney  *
215*0c53dd8bSPaul E. McKenney  * Exit an SRCU read-side critical section without lockdep-RCU checking.
216*0c53dd8bSPaul E. McKenney  * See srcu_read_lock_raw() for more details.
217*0c53dd8bSPaul E. McKenney  */
218*0c53dd8bSPaul E. McKenney static inline void srcu_read_unlock_raw(struct srcu_struct *sp, int idx)
219*0c53dd8bSPaul E. McKenney {
220*0c53dd8bSPaul E. McKenney 	unsigned long flags;
221*0c53dd8bSPaul E. McKenney 
222*0c53dd8bSPaul E. McKenney 	local_irq_save(flags);
223*0c53dd8bSPaul E. McKenney 	__srcu_read_unlock(sp, idx);
224*0c53dd8bSPaul E. McKenney 	local_irq_restore(flags);
225*0c53dd8bSPaul E. McKenney }
226*0c53dd8bSPaul E. McKenney 
227eabc0694SAlan Stern #endif
228