]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/srcu.h
Merge tag 'kernel.sys.v5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner...
[mirror_ubuntu-jammy-kernel.git] / include / linux / srcu.h
CommitLineData
8c366db0 1/* SPDX-License-Identifier: GPL-2.0+ */
621934ee
PM
2/*
3 * Sleepable Read-Copy Update mechanism for mutual exclusion
4 *
621934ee 5 * Copyright (C) IBM Corporation, 2006
4e87b2d7 6 * Copyright (C) Fujitsu, 2012
621934ee 7 *
8c366db0 8 * Author: Paul McKenney <paulmck@linux.ibm.com>
4e87b2d7 9 * Lai Jiangshan <laijs@cn.fujitsu.com>
621934ee
PM
10 *
11 * For detailed explanation of Read-Copy Update mechanism see -
8660b7d8 12 * Documentation/RCU/ *.txt
621934ee
PM
13 *
14 */
15
eabc0694
AS
16#ifndef _LINUX_SRCU_H
17#define _LINUX_SRCU_H
18
d14aada8 19#include <linux/mutex.h>
ff195cb6 20#include <linux/rcupdate.h>
931ea9d1 21#include <linux/workqueue.h>
8660b7d8 22#include <linux/rcu_segcblist.h>
d14aada8 23
d8be8173 24struct srcu_struct;
c2a8ec07 25
632ee200
PM
26#ifdef CONFIG_DEBUG_LOCK_ALLOC
27
aacb5d91 28int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
632ee200
PM
29 struct lock_class_key *key);
30
aacb5d91 31#define init_srcu_struct(ssp) \
632ee200
PM
32({ \
33 static struct lock_class_key __srcu_key; \
34 \
aacb5d91 35 __init_srcu_struct((ssp), #ssp, &__srcu_key); \
632ee200
PM
36})
37
55c6659a 38#define __SRCU_DEP_MAP_INIT(srcu_name) .dep_map = { .name = #srcu_name },
632ee200
PM
39#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
40
aacb5d91 41int init_srcu_struct(struct srcu_struct *ssp);
632ee200 42
55c6659a 43#define __SRCU_DEP_MAP_INIT(srcu_name)
632ee200
PM
44#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
45
d8be8173
PM
46#ifdef CONFIG_TINY_SRCU
47#include <linux/srcutiny.h>
48#elif defined(CONFIG_TREE_SRCU)
49#include <linux/srcutree.h>
07f6e64b 50#elif defined(CONFIG_SRCU)
d8be8173 51#error "Unknown SRCU implementation specified to kernel configuration"
07f6e64b 52#else
07f6e64b
PM
53/* Dummy definition for things like notifiers. Actual use gets link error. */
54struct srcu_struct { };
d8be8173 55#endif
55c6659a 56
aacb5d91 57void call_srcu(struct srcu_struct *ssp, struct rcu_head *head,
931ea9d1 58 void (*func)(struct rcu_head *head));
f5ad3991 59void cleanup_srcu_struct(struct srcu_struct *ssp);
aacb5d91
PM
60int __srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp);
61void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases(ssp);
62void synchronize_srcu(struct srcu_struct *ssp);
8b5bd67c
PM
63unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp);
64unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp);
65bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie);
eabc0694 66
8e9c01c7
FW
67#ifdef CONFIG_SRCU
68void srcu_init(void);
69#else /* #ifdef CONFIG_SRCU */
70static inline void srcu_init(void) { }
71#endif /* #else #ifdef CONFIG_SRCU */
72
632ee200
PM
73#ifdef CONFIG_DEBUG_LOCK_ALLOC
74
75/**
76 * srcu_read_lock_held - might we be in SRCU read-side critical section?
aacb5d91 77 * @ssp: The srcu_struct structure to check
632ee200 78 *
d20200b5
PM
79 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU
80 * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC,
632ee200
PM
81 * this assumes we are in an SRCU read-side critical section unless it can
82 * prove otherwise.
ff195cb6 83 *
867f236b
PM
84 * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
85 * and while lockdep is disabled.
86 *
511a0868
LJ
87 * Note that SRCU is based on its own statemachine and it doesn't
88 * relies on normal RCU, it can be called from the CPU which
89 * is in the idle loop from an RCU point of view or offline.
632ee200 90 */
aacb5d91 91static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
632ee200 92{
867f236b 93 if (!debug_lockdep_rcu_enabled())
ff195cb6 94 return 1;
aacb5d91 95 return lock_is_held(&ssp->dep_map);
632ee200
PM
96}
97
98#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
99
aacb5d91 100static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
632ee200
PM
101{
102 return 1;
103}
104
105#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
106
c26d34a5 107/**
ca5ecddf
PM
108 * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing
109 * @p: the pointer to fetch and protect for later dereferencing
aacb5d91 110 * @ssp: pointer to the srcu_struct, which is used to check that we
ca5ecddf
PM
111 * really are in an SRCU read-side critical section.
112 * @c: condition to check for update-side use
c26d34a5 113 *
ca5ecddf
PM
114 * If PROVE_RCU is enabled, invoking this outside of an RCU read-side
115 * critical section will result in an RCU-lockdep splat, unless @c evaluates
116 * to 1. The @c argument will normally be a logical expression containing
117 * lockdep_is_held() calls.
c26d34a5 118 */
aacb5d91
PM
119#define srcu_dereference_check(p, ssp, c) \
120 __rcu_dereference_check((p), (c) || srcu_read_lock_held(ssp), __rcu)
ca5ecddf
PM
121
122/**
123 * srcu_dereference - fetch SRCU-protected pointer for later dereferencing
124 * @p: the pointer to fetch and protect for later dereferencing
aacb5d91 125 * @ssp: pointer to the srcu_struct, which is used to check that we
ca5ecddf
PM
126 * really are in an SRCU read-side critical section.
127 *
128 * Makes rcu_dereference_check() do the dirty work. If PROVE_RCU
129 * is enabled, invoking this outside of an RCU read-side critical
130 * section will result in an RCU-lockdep splat.
131 */
aacb5d91 132#define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0)
c26d34a5 133
0b764a6e
JFG
134/**
135 * srcu_dereference_notrace - no tracing and no lockdep calls from here
f3e763c3 136 * @p: the pointer to fetch and protect for later dereferencing
aacb5d91 137 * @ssp: pointer to the srcu_struct, which is used to check that we
f3e763c3 138 * really are in an SRCU read-side critical section.
0b764a6e 139 */
aacb5d91 140#define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1)
0b764a6e 141
632ee200
PM
142/**
143 * srcu_read_lock - register a new reader for an SRCU-protected structure.
aacb5d91 144 * @ssp: srcu_struct in which to register the new reader.
632ee200
PM
145 *
146 * Enter an SRCU read-side critical section. Note that SRCU read-side
73d4da4d
PM
147 * critical sections may be nested. However, it is illegal to
148 * call anything that waits on an SRCU grace period for the same
149 * srcu_struct, whether directly or indirectly. Please note that
150 * one way to indirectly wait on an SRCU grace period is to acquire
151 * a mutex that is held elsewhere while calling synchronize_srcu() or
152 * synchronize_srcu_expedited().
3842a083
PM
153 *
154 * Note that srcu_read_lock() and the matching srcu_read_unlock() must
155 * occur in the same context, for example, it is illegal to invoke
156 * srcu_read_unlock() in an irq handler if the matching srcu_read_lock()
157 * was invoked in process context.
632ee200 158 */
aacb5d91 159static inline int srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp)
632ee200 160{
49f5903b 161 int retval;
632ee200 162
aacb5d91
PM
163 retval = __srcu_read_lock(ssp);
164 rcu_lock_acquire(&(ssp)->dep_map);
632ee200
PM
165 return retval;
166}
167
1f45a4db
PM
168/* Used by tracing, cannot be traced and cannot invoke lockdep. */
169static inline notrace int
aacb5d91 170srcu_read_lock_notrace(struct srcu_struct *ssp) __acquires(ssp)
1f45a4db
PM
171{
172 int retval;
173
aacb5d91 174 retval = __srcu_read_lock(ssp);
1f45a4db
PM
175 return retval;
176}
177
632ee200
PM
178/**
179 * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
aacb5d91 180 * @ssp: srcu_struct in which to unregister the old reader.
632ee200
PM
181 * @idx: return value from corresponding srcu_read_lock().
182 *
183 * Exit an SRCU read-side critical section.
184 */
aacb5d91
PM
185static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx)
186 __releases(ssp)
632ee200 187{
c8ca1aa7 188 WARN_ON_ONCE(idx & ~0x1);
aacb5d91
PM
189 rcu_lock_release(&(ssp)->dep_map);
190 __srcu_read_unlock(ssp, idx);
632ee200
PM
191}
192
1f45a4db
PM
193/* Used by tracing, cannot be traced and cannot call lockdep. */
194static inline notrace void
aacb5d91 195srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases(ssp)
1f45a4db 196{
aacb5d91 197 __srcu_read_unlock(ssp, idx);
1f45a4db
PM
198}
199
ce332f66
MT
200/**
201 * smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock
202 *
203 * Converts the preceding srcu_read_unlock into a two-way memory barrier.
204 *
205 * Call this after srcu_read_unlock, to guarantee that all memory operations
206 * that occur after smp_mb__after_srcu_read_unlock will appear to happen after
207 * the preceding srcu_read_unlock.
208 */
209static inline void smp_mb__after_srcu_read_unlock(void)
210{
211 /* __srcu_read_unlock has smp_mb() internally so nothing to do here. */
212}
213
eabc0694 214#endif