Paul E. McKenney | 8c366db | 2019-01-17 10:39:22 -0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Sleepable Read-Copy Update mechanism for mutual exclusion |
| 4 | * |
Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 5 | * Copyright (C) IBM Corporation, 2006 |
Lai Jiangshan | 4e87b2d | 2012-10-13 01:14:14 +0800 | [diff] [blame] | 6 | * Copyright (C) Fujitsu, 2012 |
Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 7 | * |
Paul E. McKenney | 8c366db | 2019-01-17 10:39:22 -0800 | [diff] [blame] | 8 | * Author: Paul McKenney <paulmck@linux.ibm.com> |
Lai Jiangshan | 4e87b2d | 2012-10-13 01:14:14 +0800 | [diff] [blame] | 9 | * Lai Jiangshan <laijs@cn.fujitsu.com> |
Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 10 | * |
| 11 | * For detailed explanation of Read-Copy Update mechanism see - |
Paul E. McKenney | 8660b7d | 2017-03-13 16:48:18 -0700 | [diff] [blame] | 12 | * Documentation/RCU/ *.txt |
Paul E. McKenney | 621934e | 2006-10-04 02:17:02 -0700 | [diff] [blame] | 13 | * |
| 14 | */ |
| 15 | |
Alan Stern | eabc069 | 2006-10-04 02:17:04 -0700 | [diff] [blame] | 16 | #ifndef _LINUX_SRCU_H |
| 17 | #define _LINUX_SRCU_H |
| 18 | |
Paul E. McKenney | d14aada | 2010-04-19 22:24:22 -0700 | [diff] [blame] | 19 | #include <linux/mutex.h> |
Paul E. McKenney | ff195cb | 2011-10-07 18:22:04 +0200 | [diff] [blame] | 20 | #include <linux/rcupdate.h> |
Lai Jiangshan | 931ea9d | 2012-03-19 16:12:13 +0800 | [diff] [blame] | 21 | #include <linux/workqueue.h> |
Paul E. McKenney | 8660b7d | 2017-03-13 16:48:18 -0700 | [diff] [blame] | 22 | #include <linux/rcu_segcblist.h> |
Paul E. McKenney | d14aada | 2010-04-19 22:24:22 -0700 | [diff] [blame] | 23 | |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 24 | struct srcu_struct; |
Paul E. McKenney | c2a8ec0 | 2017-03-10 15:31:55 -0800 | [diff] [blame] | 25 | |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 26 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 27 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 28 | int __init_srcu_struct(struct srcu_struct *ssp, const char *name, |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 29 | struct lock_class_key *key); |
| 30 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 31 | #define init_srcu_struct(ssp) \ |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 32 | ({ \ |
| 33 | static struct lock_class_key __srcu_key; \ |
| 34 | \ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 35 | __init_srcu_struct((ssp), #ssp, &__srcu_key); \ |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 36 | }) |
| 37 | |
Lai Jiangshan | 55c6659a | 2012-10-13 01:14:16 +0800 | [diff] [blame] | 38 | #define __SRCU_DEP_MAP_INIT(srcu_name) .dep_map = { .name = #srcu_name }, |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 39 | #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 40 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 41 | int init_srcu_struct(struct srcu_struct *ssp); |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 42 | |
Lai Jiangshan | 55c6659a | 2012-10-13 01:14:16 +0800 | [diff] [blame] | 43 | #define __SRCU_DEP_MAP_INIT(srcu_name) |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 44 | #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 45 | |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 46 | #ifdef CONFIG_TINY_SRCU |
| 47 | #include <linux/srcutiny.h> |
| 48 | #elif defined(CONFIG_TREE_SRCU) |
| 49 | #include <linux/srcutree.h> |
Paul E. McKenney | 07f6e64 | 2017-04-28 13:53:04 -0700 | [diff] [blame] | 50 | #elif defined(CONFIG_SRCU) |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 51 | #error "Unknown SRCU implementation specified to kernel configuration" |
Paul E. McKenney | 07f6e64 | 2017-04-28 13:53:04 -0700 | [diff] [blame] | 52 | #else |
Paul E. McKenney | 07f6e64 | 2017-04-28 13:53:04 -0700 | [diff] [blame] | 53 | /* Dummy definition for things like notifiers. Actual use gets link error. */ |
| 54 | struct srcu_struct { }; |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 55 | #endif |
Lai Jiangshan | 55c6659a | 2012-10-13 01:14:16 +0800 | [diff] [blame] | 56 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 57 | void call_srcu(struct srcu_struct *ssp, struct rcu_head *head, |
Lai Jiangshan | 931ea9d | 2012-03-19 16:12:13 +0800 | [diff] [blame] | 58 | void (*func)(struct rcu_head *head)); |
Paul E. McKenney | f5ad399 | 2019-02-13 13:54:37 -0800 | [diff] [blame] | 59 | void cleanup_srcu_struct(struct srcu_struct *ssp); |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 60 | int __srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp); |
| 61 | void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases(ssp); |
| 62 | void synchronize_srcu(struct srcu_struct *ssp); |
Alan Stern | eabc069 | 2006-10-04 02:17:04 -0700 | [diff] [blame] | 63 | |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 64 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 65 | |
| 66 | /** |
| 67 | * srcu_read_lock_held - might we be in SRCU read-side critical section? |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 68 | * @ssp: The srcu_struct structure to check |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 69 | * |
Paul E. McKenney | d20200b | 2010-03-30 10:52:21 -0700 | [diff] [blame] | 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, |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 72 | * this assumes we are in an SRCU read-side critical section unless it can |
| 73 | * prove otherwise. |
Paul E. McKenney | ff195cb | 2011-10-07 18:22:04 +0200 | [diff] [blame] | 74 | * |
Paul E. McKenney | 867f236 | 2011-10-07 18:22:05 +0200 | [diff] [blame] | 75 | * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot |
| 76 | * and while lockdep is disabled. |
| 77 | * |
Lai Jiangshan | 511a086 | 2012-11-29 16:46:06 +0800 | [diff] [blame] | 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. |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 81 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 82 | static inline int srcu_read_lock_held(const struct srcu_struct *ssp) |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 83 | { |
Paul E. McKenney | 867f236 | 2011-10-07 18:22:05 +0200 | [diff] [blame] | 84 | if (!debug_lockdep_rcu_enabled()) |
Paul E. McKenney | ff195cb | 2011-10-07 18:22:04 +0200 | [diff] [blame] | 85 | return 1; |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 86 | return lock_is_held(&ssp->dep_map); |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 90 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 91 | static inline int srcu_read_lock_held(const struct srcu_struct *ssp) |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 92 | { |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 97 | |
| 98 | /** |
Paul E. McKenney | ca5ecdd | 2010-04-28 14:39:09 -0700 | [diff] [blame] | 99 | * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing |
| 100 | * @p: the pointer to fetch and protect for later dereferencing |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 101 | * @ssp: pointer to the srcu_struct, which is used to check that we |
Paul E. McKenney | ca5ecdd | 2010-04-28 14:39:09 -0700 | [diff] [blame] | 102 | * really are in an SRCU read-side critical section. |
| 103 | * @c: condition to check for update-side use |
Paul E. McKenney | c26d34a | 2010-02-22 17:04:46 -0800 | [diff] [blame] | 104 | * |
Paul E. McKenney | ca5ecdd | 2010-04-28 14:39:09 -0700 | [diff] [blame] | 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. |
Paul E. McKenney | c26d34a | 2010-02-22 17:04:46 -0800 | [diff] [blame] | 109 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 110 | #define srcu_dereference_check(p, ssp, c) \ |
| 111 | __rcu_dereference_check((p), (c) || srcu_read_lock_held(ssp), __rcu) |
Paul E. McKenney | ca5ecdd | 2010-04-28 14:39:09 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * srcu_dereference - fetch SRCU-protected pointer for later dereferencing |
| 115 | * @p: the pointer to fetch and protect for later dereferencing |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 116 | * @ssp: pointer to the srcu_struct, which is used to check that we |
Paul E. McKenney | ca5ecdd | 2010-04-28 14:39:09 -0700 | [diff] [blame] | 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 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 123 | #define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0) |
Paul E. McKenney | c26d34a | 2010-02-22 17:04:46 -0800 | [diff] [blame] | 124 | |
| 125 | /** |
Joel Fernandes (Google) | 0b764a6 | 2018-06-28 11:21:44 -0700 | [diff] [blame] | 126 | * srcu_dereference_notrace - no tracing and no lockdep calls from here |
Randy Dunlap | f3e763c | 2018-09-03 12:45:45 -0700 | [diff] [blame] | 127 | * @p: the pointer to fetch and protect for later dereferencing |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 128 | * @ssp: pointer to the srcu_struct, which is used to check that we |
Randy Dunlap | f3e763c | 2018-09-03 12:45:45 -0700 | [diff] [blame] | 129 | * really are in an SRCU read-side critical section. |
Joel Fernandes (Google) | 0b764a6 | 2018-06-28 11:21:44 -0700 | [diff] [blame] | 130 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 131 | #define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1) |
Joel Fernandes (Google) | 0b764a6 | 2018-06-28 11:21:44 -0700 | [diff] [blame] | 132 | |
| 133 | /** |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 134 | * srcu_read_lock - register a new reader for an SRCU-protected structure. |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 135 | * @ssp: srcu_struct in which to register the new reader. |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 136 | * |
| 137 | * Enter an SRCU read-side critical section. Note that SRCU read-side |
Paul E. McKenney | 73d4da4 | 2010-08-16 10:50:54 -0700 | [diff] [blame] | 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(). |
Paul E. McKenney | 3842a08 | 2011-11-28 10:42:42 -0800 | [diff] [blame] | 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. |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 149 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 150 | static inline int srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp) |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 151 | { |
Paul E. McKenney | 49f5903 | 2015-09-01 00:42:57 -0700 | [diff] [blame] | 152 | int retval; |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 153 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 154 | retval = __srcu_read_lock(ssp); |
| 155 | rcu_lock_acquire(&(ssp)->dep_map); |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 156 | return retval; |
| 157 | } |
| 158 | |
Paul McKenney | 1f45a4d | 2018-06-28 11:21:43 -0700 | [diff] [blame] | 159 | /* Used by tracing, cannot be traced and cannot invoke lockdep. */ |
| 160 | static inline notrace int |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 161 | srcu_read_lock_notrace(struct srcu_struct *ssp) __acquires(ssp) |
Paul McKenney | 1f45a4d | 2018-06-28 11:21:43 -0700 | [diff] [blame] | 162 | { |
| 163 | int retval; |
| 164 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 165 | retval = __srcu_read_lock(ssp); |
Paul McKenney | 1f45a4d | 2018-06-28 11:21:43 -0700 | [diff] [blame] | 166 | return retval; |
| 167 | } |
| 168 | |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 169 | /** |
| 170 | * srcu_read_unlock - unregister a old reader from an SRCU-protected structure. |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 171 | * @ssp: srcu_struct in which to unregister the old reader. |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 172 | * @idx: return value from corresponding srcu_read_lock(). |
| 173 | * |
| 174 | * Exit an SRCU read-side critical section. |
| 175 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 176 | static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx) |
| 177 | __releases(ssp) |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 178 | { |
Paul E. McKenney | c8ca1aa | 2018-11-30 10:06:46 -0800 | [diff] [blame] | 179 | WARN_ON_ONCE(idx & ~0x1); |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 180 | rcu_lock_release(&(ssp)->dep_map); |
| 181 | __srcu_read_unlock(ssp, idx); |
Paul E. McKenney | 632ee20 | 2010-02-22 17:04:45 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Paul McKenney | 1f45a4d | 2018-06-28 11:21:43 -0700 | [diff] [blame] | 184 | /* Used by tracing, cannot be traced and cannot call lockdep. */ |
| 185 | static inline notrace void |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 186 | srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases(ssp) |
Paul McKenney | 1f45a4d | 2018-06-28 11:21:43 -0700 | [diff] [blame] | 187 | { |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 188 | __srcu_read_unlock(ssp, idx); |
Paul McKenney | 1f45a4d | 2018-06-28 11:21:43 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Michael S. Tsirkin | ce332f6 | 2013-11-04 22:36:17 +0200 | [diff] [blame] | 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 | */ |
| 200 | static 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 Stern | eabc069 | 2006-10-04 02:17:04 -0700 | [diff] [blame] | 205 | #endif |