Merge tag '5.19-rc-ksmbd-server-fixes' of git://git.samba.org/ksmbd
[linux-2.6-block.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 119#define srcu_dereference_check(p, ssp, c) \
24ba5301
CHT
120 __rcu_dereference_check((p), __UNIQUE_ID(rcu), \
121 (c) || srcu_read_lock_held(ssp), __rcu)
ca5ecddf
PM
122
123/**
124 * srcu_dereference - fetch SRCU-protected pointer for later dereferencing
125 * @p: the pointer to fetch and protect for later dereferencing
aacb5d91 126 * @ssp: pointer to the srcu_struct, which is used to check that we
ca5ecddf
PM
127 * really are in an SRCU read-side critical section.
128 *
129 * Makes rcu_dereference_check() do the dirty work. If PROVE_RCU
130 * is enabled, invoking this outside of an RCU read-side critical
131 * section will result in an RCU-lockdep splat.
132 */
aacb5d91 133#define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0)
c26d34a5 134
0b764a6e
JFG
135/**
136 * srcu_dereference_notrace - no tracing and no lockdep calls from here
f3e763c3 137 * @p: the pointer to fetch and protect for later dereferencing
aacb5d91 138 * @ssp: pointer to the srcu_struct, which is used to check that we
f3e763c3 139 * really are in an SRCU read-side critical section.
0b764a6e 140 */
aacb5d91 141#define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1)
0b764a6e 142
632ee200
PM
143/**
144 * srcu_read_lock - register a new reader for an SRCU-protected structure.
aacb5d91 145 * @ssp: srcu_struct in which to register the new reader.
632ee200
PM
146 *
147 * Enter an SRCU read-side critical section. Note that SRCU read-side
73d4da4d
PM
148 * critical sections may be nested. However, it is illegal to
149 * call anything that waits on an SRCU grace period for the same
150 * srcu_struct, whether directly or indirectly. Please note that
151 * one way to indirectly wait on an SRCU grace period is to acquire
152 * a mutex that is held elsewhere while calling synchronize_srcu() or
153 * synchronize_srcu_expedited().
3842a083
PM
154 *
155 * Note that srcu_read_lock() and the matching srcu_read_unlock() must
156 * occur in the same context, for example, it is illegal to invoke
157 * srcu_read_unlock() in an irq handler if the matching srcu_read_lock()
158 * was invoked in process context.
632ee200 159 */
aacb5d91 160static inline int srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp)
632ee200 161{
49f5903b 162 int retval;
632ee200 163
aacb5d91
PM
164 retval = __srcu_read_lock(ssp);
165 rcu_lock_acquire(&(ssp)->dep_map);
632ee200
PM
166 return retval;
167}
168
1f45a4db
PM
169/* Used by tracing, cannot be traced and cannot invoke lockdep. */
170static inline notrace int
aacb5d91 171srcu_read_lock_notrace(struct srcu_struct *ssp) __acquires(ssp)
1f45a4db
PM
172{
173 int retval;
174
aacb5d91 175 retval = __srcu_read_lock(ssp);
1f45a4db
PM
176 return retval;
177}
178
632ee200
PM
179/**
180 * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
aacb5d91 181 * @ssp: srcu_struct in which to unregister the old reader.
632ee200
PM
182 * @idx: return value from corresponding srcu_read_lock().
183 *
184 * Exit an SRCU read-side critical section.
185 */
aacb5d91
PM
186static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx)
187 __releases(ssp)
632ee200 188{
c8ca1aa7 189 WARN_ON_ONCE(idx & ~0x1);
aacb5d91
PM
190 rcu_lock_release(&(ssp)->dep_map);
191 __srcu_read_unlock(ssp, idx);
632ee200
PM
192}
193
1f45a4db
PM
194/* Used by tracing, cannot be traced and cannot call lockdep. */
195static inline notrace void
aacb5d91 196srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases(ssp)
1f45a4db 197{
aacb5d91 198 __srcu_read_unlock(ssp, idx);
1f45a4db
PM
199}
200
ce332f66
MT
201/**
202 * smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock
203 *
204 * Converts the preceding srcu_read_unlock into a two-way memory barrier.
205 *
206 * Call this after srcu_read_unlock, to guarantee that all memory operations
207 * that occur after smp_mb__after_srcu_read_unlock will appear to happen after
208 * the preceding srcu_read_unlock.
209 */
210static inline void smp_mb__after_srcu_read_unlock(void)
211{
212 /* __srcu_read_unlock has smp_mb() internally so nothing to do here. */
213}
214
eabc0694 215#endif