blob: e432cc92c73de7d1ae73aa5b69486e8311547d82 [file] [log] [blame]
Paul E. McKenney8c366db2019-01-17 10:39:22 -08001/* SPDX-License-Identifier: GPL-2.0+ */
Paul E. McKenney621934e2006-10-04 02:17:02 -07002/*
3 * Sleepable Read-Copy Update mechanism for mutual exclusion
4 *
Paul E. McKenney621934e2006-10-04 02:17:02 -07005 * Copyright (C) IBM Corporation, 2006
Lai Jiangshan4e87b2d2012-10-13 01:14:14 +08006 * Copyright (C) Fujitsu, 2012
Paul E. McKenney621934e2006-10-04 02:17:02 -07007 *
Paul E. McKenney8c366db2019-01-17 10:39:22 -08008 * Author: Paul McKenney <paulmck@linux.ibm.com>
Lai Jiangshan4e87b2d2012-10-13 01:14:14 +08009 * Lai Jiangshan <laijs@cn.fujitsu.com>
Paul E. McKenney621934e2006-10-04 02:17:02 -070010 *
11 * For detailed explanation of Read-Copy Update mechanism see -
Paul E. McKenney8660b7d2017-03-13 16:48:18 -070012 * Documentation/RCU/ *.txt
Paul E. McKenney621934e2006-10-04 02:17:02 -070013 *
14 */
15
Alan Sterneabc0692006-10-04 02:17:04 -070016#ifndef _LINUX_SRCU_H
17#define _LINUX_SRCU_H
18
Paul E. McKenneyd14aada2010-04-19 22:24:22 -070019#include <linux/mutex.h>
Paul E. McKenneyff195cb2011-10-07 18:22:04 +020020#include <linux/rcupdate.h>
Lai Jiangshan931ea9d2012-03-19 16:12:13 +080021#include <linux/workqueue.h>
Paul E. McKenney8660b7d2017-03-13 16:48:18 -070022#include <linux/rcu_segcblist.h>
Paul E. McKenneyd14aada2010-04-19 22:24:22 -070023
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070024struct srcu_struct;
Paul E. McKenneyc2a8ec02017-03-10 15:31:55 -080025
Paul E. McKenney632ee202010-02-22 17:04:45 -080026#ifdef CONFIG_DEBUG_LOCK_ALLOC
27
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070028int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
Paul E. McKenney632ee202010-02-22 17:04:45 -080029 struct lock_class_key *key);
30
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070031#define init_srcu_struct(ssp) \
Paul E. McKenney632ee202010-02-22 17:04:45 -080032({ \
33 static struct lock_class_key __srcu_key; \
34 \
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070035 __init_srcu_struct((ssp), #ssp, &__srcu_key); \
Paul E. McKenney632ee202010-02-22 17:04:45 -080036})
37
Lai Jiangshan55c6659a2012-10-13 01:14:16 +080038#define __SRCU_DEP_MAP_INIT(srcu_name) .dep_map = { .name = #srcu_name },
Paul E. McKenney632ee202010-02-22 17:04:45 -080039#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
40
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070041int init_srcu_struct(struct srcu_struct *ssp);
Paul E. McKenney632ee202010-02-22 17:04:45 -080042
Lai Jiangshan55c6659a2012-10-13 01:14:16 +080043#define __SRCU_DEP_MAP_INIT(srcu_name)
Paul E. McKenney632ee202010-02-22 17:04:45 -080044#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
45
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070046#ifdef CONFIG_TINY_SRCU
47#include <linux/srcutiny.h>
48#elif defined(CONFIG_TREE_SRCU)
49#include <linux/srcutree.h>
Paul E. McKenney07f6e642017-04-28 13:53:04 -070050#elif defined(CONFIG_SRCU)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070051#error "Unknown SRCU implementation specified to kernel configuration"
Paul E. McKenney07f6e642017-04-28 13:53:04 -070052#else
Paul E. McKenney07f6e642017-04-28 13:53:04 -070053/* Dummy definition for things like notifiers. Actual use gets link error. */
54struct srcu_struct { };
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070055#endif
Lai Jiangshan55c6659a2012-10-13 01:14:16 +080056
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070057void call_srcu(struct srcu_struct *ssp, struct rcu_head *head,
Lai Jiangshan931ea9d2012-03-19 16:12:13 +080058 void (*func)(struct rcu_head *head));
Paul E. McKenneyf5ad3992019-02-13 13:54:37 -080059void cleanup_srcu_struct(struct srcu_struct *ssp);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070060int __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);
Alan Sterneabc0692006-10-04 02:17:04 -070063
Paul E. McKenney632ee202010-02-22 17:04:45 -080064#ifdef CONFIG_DEBUG_LOCK_ALLOC
65
66/**
67 * srcu_read_lock_held - might we be in SRCU read-side critical section?
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070068 * @ssp: The srcu_struct structure to check
Paul E. McKenney632ee202010-02-22 17:04:45 -080069 *
Paul E. McKenneyd20200b2010-03-30 10:52:21 -070070 * 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,
Paul E. McKenney632ee202010-02-22 17:04:45 -080072 * this assumes we are in an SRCU read-side critical section unless it can
73 * prove otherwise.
Paul E. McKenneyff195cb2011-10-07 18:22:04 +020074 *
Paul E. McKenney867f2362011-10-07 18:22:05 +020075 * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
76 * and while lockdep is disabled.
77 *
Lai Jiangshan511a0862012-11-29 16:46:06 +080078 * 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.
Paul E. McKenney632ee202010-02-22 17:04:45 -080081 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070082static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
Paul E. McKenney632ee202010-02-22 17:04:45 -080083{
Paul E. McKenney867f2362011-10-07 18:22:05 +020084 if (!debug_lockdep_rcu_enabled())
Paul E. McKenneyff195cb2011-10-07 18:22:04 +020085 return 1;
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070086 return lock_is_held(&ssp->dep_map);
Paul E. McKenney632ee202010-02-22 17:04:45 -080087}
88
89#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
90
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070091static inline int srcu_read_lock_held(const struct srcu_struct *ssp)
Paul E. McKenney632ee202010-02-22 17:04:45 -080092{
93 return 1;
94}
95
96#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
97
98/**
Paul E. McKenneyca5ecdd2010-04-28 14:39:09 -070099 * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing
100 * @p: the pointer to fetch and protect for later dereferencing
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700101 * @ssp: pointer to the srcu_struct, which is used to check that we
Paul E. McKenneyca5ecdd2010-04-28 14:39:09 -0700102 * really are in an SRCU read-side critical section.
103 * @c: condition to check for update-side use
Paul E. McKenneyc26d34a2010-02-22 17:04:46 -0800104 *
Paul E. McKenneyca5ecdd2010-04-28 14:39:09 -0700105 * 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.
Paul E. McKenneyc26d34a2010-02-22 17:04:46 -0800109 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700110#define srcu_dereference_check(p, ssp, c) \
111 __rcu_dereference_check((p), (c) || srcu_read_lock_held(ssp), __rcu)
Paul E. McKenneyca5ecdd2010-04-28 14:39:09 -0700112
113/**
114 * srcu_dereference - fetch SRCU-protected pointer for later dereferencing
115 * @p: the pointer to fetch and protect for later dereferencing
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700116 * @ssp: pointer to the srcu_struct, which is used to check that we
Paul E. McKenneyca5ecdd2010-04-28 14:39:09 -0700117 * 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 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700123#define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0)
Paul E. McKenneyc26d34a2010-02-22 17:04:46 -0800124
125/**
Joel Fernandes (Google)0b764a62018-06-28 11:21:44 -0700126 * srcu_dereference_notrace - no tracing and no lockdep calls from here
Randy Dunlapf3e763c2018-09-03 12:45:45 -0700127 * @p: the pointer to fetch and protect for later dereferencing
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700128 * @ssp: pointer to the srcu_struct, which is used to check that we
Randy Dunlapf3e763c2018-09-03 12:45:45 -0700129 * really are in an SRCU read-side critical section.
Joel Fernandes (Google)0b764a62018-06-28 11:21:44 -0700130 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700131#define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1)
Joel Fernandes (Google)0b764a62018-06-28 11:21:44 -0700132
133/**
Paul E. McKenney632ee202010-02-22 17:04:45 -0800134 * srcu_read_lock - register a new reader for an SRCU-protected structure.
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700135 * @ssp: srcu_struct in which to register the new reader.
Paul E. McKenney632ee202010-02-22 17:04:45 -0800136 *
137 * Enter an SRCU read-side critical section. Note that SRCU read-side
Paul E. McKenney73d4da42010-08-16 10:50:54 -0700138 * 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().
Paul E. McKenney3842a082011-11-28 10:42:42 -0800144 *
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.
Paul E. McKenney632ee202010-02-22 17:04:45 -0800149 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700150static inline int srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp)
Paul E. McKenney632ee202010-02-22 17:04:45 -0800151{
Paul E. McKenney49f59032015-09-01 00:42:57 -0700152 int retval;
Paul E. McKenney632ee202010-02-22 17:04:45 -0800153
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700154 retval = __srcu_read_lock(ssp);
155 rcu_lock_acquire(&(ssp)->dep_map);
Paul E. McKenney632ee202010-02-22 17:04:45 -0800156 return retval;
157}
158
Paul McKenney1f45a4d2018-06-28 11:21:43 -0700159/* Used by tracing, cannot be traced and cannot invoke lockdep. */
160static inline notrace int
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700161srcu_read_lock_notrace(struct srcu_struct *ssp) __acquires(ssp)
Paul McKenney1f45a4d2018-06-28 11:21:43 -0700162{
163 int retval;
164
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700165 retval = __srcu_read_lock(ssp);
Paul McKenney1f45a4d2018-06-28 11:21:43 -0700166 return retval;
167}
168
Paul E. McKenney632ee202010-02-22 17:04:45 -0800169/**
170 * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700171 * @ssp: srcu_struct in which to unregister the old reader.
Paul E. McKenney632ee202010-02-22 17:04:45 -0800172 * @idx: return value from corresponding srcu_read_lock().
173 *
174 * Exit an SRCU read-side critical section.
175 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700176static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx)
177 __releases(ssp)
Paul E. McKenney632ee202010-02-22 17:04:45 -0800178{
Paul E. McKenneyc8ca1aa2018-11-30 10:06:46 -0800179 WARN_ON_ONCE(idx & ~0x1);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700180 rcu_lock_release(&(ssp)->dep_map);
181 __srcu_read_unlock(ssp, idx);
Paul E. McKenney632ee202010-02-22 17:04:45 -0800182}
183
Paul McKenney1f45a4d2018-06-28 11:21:43 -0700184/* Used by tracing, cannot be traced and cannot call lockdep. */
185static inline notrace void
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700186srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases(ssp)
Paul McKenney1f45a4d2018-06-28 11:21:43 -0700187{
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700188 __srcu_read_unlock(ssp, idx);
Paul McKenney1f45a4d2018-06-28 11:21:43 -0700189}
190
Michael S. Tsirkince332f62013-11-04 22:36:17 +0200191/**
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
Alan Sterneabc0692006-10-04 02:17:04 -0700205#endif