]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/srcu.h
Merge tag 'x86-urgent-2020-08-30' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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);
eabc0694 63
632ee200
PM
64#ifdef CONFIG_DEBUG_LOCK_ALLOC
65
66/**
67 * srcu_read_lock_held - might we be in SRCU read-side critical section?
aacb5d91 68 * @ssp: The srcu_struct structure to check
632ee200 69 *
d20200b5
PM
70 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU
71 * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC,
632ee200
PM
72 * this assumes we are in an SRCU read-side critical section unless it can
73 * prove otherwise.
ff195cb6 74 *
867f236b
PM
75 * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
76 * and while lockdep is disabled.
77 *
511a0868
LJ
78 * Note that SRCU is based on its own statemachine and it doesn't
79 * relies on normal RCU, it can be called from the CPU which
80 * is in the idle loop from an RCU point of view or offline.
632ee200 81 */
aacb5d91 82static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
632ee200 83{
867f236b 84 if (!debug_lockdep_rcu_enabled())
ff195cb6 85 return 1;
aacb5d91 86 return lock_is_held(&ssp->dep_map);
632ee200
PM
87}
88
89#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
90
aacb5d91 91static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
632ee200
PM
92{
93 return 1;
94}
95
96#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
97
c26d34a5 98/**
ca5ecddf
PM
99 * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing
100 * @p: the pointer to fetch and protect for later dereferencing
aacb5d91 101 * @ssp: pointer to the srcu_struct, which is used to check that we
ca5ecddf
PM
102 * really are in an SRCU read-side critical section.
103 * @c: condition to check for update-side use
c26d34a5 104 *
ca5ecddf
PM
105 * If PROVE_RCU is enabled, invoking this outside of an RCU read-side
106 * critical section will result in an RCU-lockdep splat, unless @c evaluates
107 * to 1. The @c argument will normally be a logical expression containing
108 * lockdep_is_held() calls.
c26d34a5 109 */
aacb5d91
PM
110#define srcu_dereference_check(p, ssp, c) \
111 __rcu_dereference_check((p), (c) || srcu_read_lock_held(ssp), __rcu)
ca5ecddf
PM
112
113/**
114 * srcu_dereference - fetch SRCU-protected pointer for later dereferencing
115 * @p: the pointer to fetch and protect for later dereferencing
aacb5d91 116 * @ssp: pointer to the srcu_struct, which is used to check that we
ca5ecddf
PM
117 * really are in an SRCU read-side critical section.
118 *
119 * Makes rcu_dereference_check() do the dirty work. If PROVE_RCU
120 * is enabled, invoking this outside of an RCU read-side critical
121 * section will result in an RCU-lockdep splat.
122 */
aacb5d91 123#define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0)
c26d34a5 124
0b764a6e
JFG
125/**
126 * srcu_dereference_notrace - no tracing and no lockdep calls from here
f3e763c3 127 * @p: the pointer to fetch and protect for later dereferencing
aacb5d91 128 * @ssp: pointer to the srcu_struct, which is used to check that we
f3e763c3 129 * really are in an SRCU read-side critical section.
0b764a6e 130 */
aacb5d91 131#define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1)
0b764a6e 132
632ee200
PM
133/**
134 * srcu_read_lock - register a new reader for an SRCU-protected structure.
aacb5d91 135 * @ssp: srcu_struct in which to register the new reader.
632ee200
PM
136 *
137 * Enter an SRCU read-side critical section. Note that SRCU read-side
73d4da4d
PM
138 * critical sections may be nested. However, it is illegal to
139 * call anything that waits on an SRCU grace period for the same
140 * srcu_struct, whether directly or indirectly. Please note that
141 * one way to indirectly wait on an SRCU grace period is to acquire
142 * a mutex that is held elsewhere while calling synchronize_srcu() or
143 * synchronize_srcu_expedited().
3842a083
PM
144 *
145 * Note that srcu_read_lock() and the matching srcu_read_unlock() must
146 * occur in the same context, for example, it is illegal to invoke
147 * srcu_read_unlock() in an irq handler if the matching srcu_read_lock()
148 * was invoked in process context.
632ee200 149 */
aacb5d91 150static inline int srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp)
632ee200 151{
49f5903b 152 int retval;
632ee200 153
aacb5d91
PM
154 retval = __srcu_read_lock(ssp);
155 rcu_lock_acquire(&(ssp)->dep_map);
632ee200
PM
156 return retval;
157}
158
1f45a4db
PM
159/* Used by tracing, cannot be traced and cannot invoke lockdep. */
160static inline notrace int
aacb5d91 161srcu_read_lock_notrace(struct srcu_struct *ssp) __acquires(ssp)
1f45a4db
PM
162{
163 int retval;
164
aacb5d91 165 retval = __srcu_read_lock(ssp);
1f45a4db
PM
166 return retval;
167}
168
632ee200
PM
169/**
170 * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
aacb5d91 171 * @ssp: srcu_struct in which to unregister the old reader.
632ee200
PM
172 * @idx: return value from corresponding srcu_read_lock().
173 *
174 * Exit an SRCU read-side critical section.
175 */
aacb5d91
PM
176static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx)
177 __releases(ssp)
632ee200 178{
c8ca1aa7 179 WARN_ON_ONCE(idx & ~0x1);
aacb5d91
PM
180 rcu_lock_release(&(ssp)->dep_map);
181 __srcu_read_unlock(ssp, idx);
632ee200
PM
182}
183
1f45a4db
PM
184/* Used by tracing, cannot be traced and cannot call lockdep. */
185static inline notrace void
aacb5d91 186srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases(ssp)
1f45a4db 187{
aacb5d91 188 __srcu_read_unlock(ssp, idx);
1f45a4db
PM
189}
190
ce332f66
MT
191/**
192 * smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock
193 *
194 * Converts the preceding srcu_read_unlock into a two-way memory barrier.
195 *
196 * Call this after srcu_read_unlock, to guarantee that all memory operations
197 * that occur after smp_mb__after_srcu_read_unlock will appear to happen after
198 * the preceding srcu_read_unlock.
199 */
200static inline void smp_mb__after_srcu_read_unlock(void)
201{
202 /* __srcu_read_unlock has smp_mb() internally so nothing to do here. */
203}
204
eabc0694 205#endif