Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 2 | /* |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame] | 3 | * The least significant 2 bits of the owner value has the following |
| 4 | * meanings when set. |
| 5 | * - RWSEM_READER_OWNED (bit 0): The rwsem is owned by readers |
| 6 | * - RWSEM_ANONYMOUSLY_OWNED (bit 1): The rwsem is anonymously owned, |
| 7 | * i.e. the owner(s) cannot be readily determined. It can be reader |
| 8 | * owned or the owning writer is indeterminate. |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 9 | * |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame] | 10 | * When a writer acquires a rwsem, it puts its task_struct pointer |
| 11 | * into the owner field. It is cleared after an unlock. |
| 12 | * |
| 13 | * When a reader acquires a rwsem, it will also puts its task_struct |
| 14 | * pointer into the owner field with both the RWSEM_READER_OWNED and |
| 15 | * RWSEM_ANONYMOUSLY_OWNED bits set. On unlock, the owner field will |
| 16 | * largely be left untouched. So for a free or reader-owned rwsem, |
| 17 | * the owner value may contain information about the last reader that |
| 18 | * acquires the rwsem. The anonymous bit is set because that particular |
| 19 | * reader may or may not still own the lock. |
| 20 | * |
| 21 | * That information may be helpful in debugging cases where the system |
| 22 | * seems to hang on a reader owned rwsem especially if only one reader |
| 23 | * is involved. Ideally we would like to track all the readers that own |
| 24 | * a rwsem, but the overhead is simply too big. |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 25 | */ |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame] | 26 | #define RWSEM_READER_OWNED (1UL << 0) |
| 27 | #define RWSEM_ANONYMOUSLY_OWNED (1UL << 1) |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 28 | |
Waiman Long | 5149cba | 2018-03-30 17:27:58 -0400 | [diff] [blame] | 29 | #ifdef CONFIG_DEBUG_RWSEMS |
Waiman Long | 3b4ba66 | 2019-04-04 13:43:15 -0400 | [diff] [blame^] | 30 | # define DEBUG_RWSEMS_WARN_ON(c, sem) do { \ |
| 31 | if (WARN_ONCE(c, "DEBUG_RWSEMS_WARN_ON(%s): count = 0x%lx, owner = 0x%lx, curr 0x%lx, list %sempty\n",\ |
| 32 | #c, atomic_long_read(&(sem)->count), \ |
| 33 | (long)((sem)->owner), (long)current, \ |
| 34 | list_empty(&(sem)->wait_list) ? "" : "not ")) \ |
| 35 | debug_locks_off(); \ |
| 36 | } while (0) |
Waiman Long | 5149cba | 2018-03-30 17:27:58 -0400 | [diff] [blame] | 37 | #else |
Waiman Long | 3b4ba66 | 2019-04-04 13:43:15 -0400 | [diff] [blame^] | 38 | # define DEBUG_RWSEMS_WARN_ON(c, sem) |
Waiman Long | 5149cba | 2018-03-30 17:27:58 -0400 | [diff] [blame] | 39 | #endif |
| 40 | |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 41 | /* |
| 42 | * R/W semaphores originally for PPC using the stuff in lib/rwsem.c. |
| 43 | * Adapted largely from include/asm-i386/rwsem.h |
| 44 | * by Paul Mackerras <paulus@samba.org>. |
| 45 | */ |
| 46 | |
| 47 | /* |
| 48 | * the semaphore definition |
| 49 | */ |
| 50 | #ifdef CONFIG_64BIT |
| 51 | # define RWSEM_ACTIVE_MASK 0xffffffffL |
| 52 | #else |
| 53 | # define RWSEM_ACTIVE_MASK 0x0000ffffL |
| 54 | #endif |
| 55 | |
| 56 | #define RWSEM_ACTIVE_BIAS 0x00000001L |
| 57 | #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1) |
| 58 | #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS |
| 59 | #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) |
| 60 | |
Davidlohr Bueso | 7a215f8 | 2015-01-30 01:14:25 -0800 | [diff] [blame] | 61 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
Waiman Long | fb6a44f | 2016-05-17 21:26:20 -0400 | [diff] [blame] | 62 | /* |
| 63 | * All writes to owner are protected by WRITE_ONCE() to make sure that |
| 64 | * store tearing can't happen as optimistic spinners may read and use |
| 65 | * the owner value concurrently without lock. Read from owner, however, |
| 66 | * may not need READ_ONCE() as long as the pointer value is only used |
| 67 | * for comparison and isn't being dereferenced. |
| 68 | */ |
Davidlohr Bueso | 7a215f8 | 2015-01-30 01:14:25 -0800 | [diff] [blame] | 69 | static inline void rwsem_set_owner(struct rw_semaphore *sem) |
| 70 | { |
Waiman Long | fb6a44f | 2016-05-17 21:26:20 -0400 | [diff] [blame] | 71 | WRITE_ONCE(sem->owner, current); |
Davidlohr Bueso | 7a215f8 | 2015-01-30 01:14:25 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static inline void rwsem_clear_owner(struct rw_semaphore *sem) |
| 75 | { |
Waiman Long | fb6a44f | 2016-05-17 21:26:20 -0400 | [diff] [blame] | 76 | WRITE_ONCE(sem->owner, NULL); |
Davidlohr Bueso | 7a215f8 | 2015-01-30 01:14:25 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame] | 79 | /* |
| 80 | * The task_struct pointer of the last owning reader will be left in |
| 81 | * the owner field. |
| 82 | * |
| 83 | * Note that the owner value just indicates the task has owned the rwsem |
| 84 | * previously, it may not be the real owner or one of the real owners |
| 85 | * anymore when that field is examined, so take it with a grain of salt. |
| 86 | */ |
| 87 | static inline void __rwsem_set_reader_owned(struct rw_semaphore *sem, |
| 88 | struct task_struct *owner) |
| 89 | { |
| 90 | unsigned long val = (unsigned long)owner | RWSEM_READER_OWNED |
| 91 | | RWSEM_ANONYMOUSLY_OWNED; |
| 92 | |
| 93 | WRITE_ONCE(sem->owner, (struct task_struct *)val); |
| 94 | } |
| 95 | |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 96 | static inline void rwsem_set_reader_owned(struct rw_semaphore *sem) |
| 97 | { |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame] | 98 | __rwsem_set_reader_owned(sem, current); |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Waiman Long | d7d760e | 2018-05-15 17:49:50 -0400 | [diff] [blame] | 101 | /* |
| 102 | * Return true if the a rwsem waiter can spin on the rwsem's owner |
| 103 | * and steal the lock, i.e. the lock is not anonymously owned. |
| 104 | * N.B. !owner is considered spinnable. |
| 105 | */ |
| 106 | static inline bool is_rwsem_owner_spinnable(struct task_struct *owner) |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 107 | { |
Waiman Long | d7d760e | 2018-05-15 17:49:50 -0400 | [diff] [blame] | 108 | return !((unsigned long)owner & RWSEM_ANONYMOUSLY_OWNED); |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Waiman Long | d7d760e | 2018-05-15 17:49:50 -0400 | [diff] [blame] | 111 | /* |
| 112 | * Return true if rwsem is owned by an anonymous writer or readers. |
| 113 | */ |
| 114 | static inline bool rwsem_has_anonymous_owner(struct task_struct *owner) |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 115 | { |
Waiman Long | d7d760e | 2018-05-15 17:49:50 -0400 | [diff] [blame] | 116 | return (unsigned long)owner & RWSEM_ANONYMOUSLY_OWNED; |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 117 | } |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame] | 118 | |
| 119 | #ifdef CONFIG_DEBUG_RWSEMS |
| 120 | /* |
| 121 | * With CONFIG_DEBUG_RWSEMS configured, it will make sure that if there |
| 122 | * is a task pointer in owner of a reader-owned rwsem, it will be the |
| 123 | * real owner or one of the real owners. The only exception is when the |
| 124 | * unlock is done by up_read_non_owner(). |
| 125 | */ |
| 126 | #define rwsem_clear_reader_owned rwsem_clear_reader_owned |
| 127 | static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) |
| 128 | { |
| 129 | unsigned long val = (unsigned long)current | RWSEM_READER_OWNED |
| 130 | | RWSEM_ANONYMOUSLY_OWNED; |
| 131 | if (READ_ONCE(sem->owner) == (struct task_struct *)val) |
| 132 | cmpxchg_relaxed((unsigned long *)&sem->owner, val, |
| 133 | RWSEM_READER_OWNED | RWSEM_ANONYMOUSLY_OWNED); |
| 134 | } |
| 135 | #endif |
| 136 | |
Davidlohr Bueso | 7a215f8 | 2015-01-30 01:14:25 -0800 | [diff] [blame] | 137 | #else |
| 138 | static inline void rwsem_set_owner(struct rw_semaphore *sem) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | static inline void rwsem_clear_owner(struct rw_semaphore *sem) |
| 143 | { |
| 144 | } |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 145 | |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame] | 146 | static inline void __rwsem_set_reader_owned(struct rw_semaphore *sem, |
| 147 | struct task_struct *owner) |
| 148 | { |
| 149 | } |
| 150 | |
Waiman Long | 19c5d69 | 2016-05-17 21:26:19 -0400 | [diff] [blame] | 151 | static inline void rwsem_set_reader_owned(struct rw_semaphore *sem) |
| 152 | { |
| 153 | } |
Davidlohr Bueso | 7a215f8 | 2015-01-30 01:14:25 -0800 | [diff] [blame] | 154 | #endif |
Waiman Long | 925b9cd | 2018-09-06 16:18:34 -0400 | [diff] [blame] | 155 | |
| 156 | #ifndef rwsem_clear_reader_owned |
| 157 | static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem) |
| 158 | { |
| 159 | } |
| 160 | #endif |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 161 | |
Waiman Long | 12a30a7 | 2019-04-04 13:43:12 -0400 | [diff] [blame] | 162 | extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem); |
| 163 | extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem); |
| 164 | extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem); |
| 165 | extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem); |
| 166 | extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem); |
| 167 | extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem); |
| 168 | |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 169 | /* |
| 170 | * lock for reading |
| 171 | */ |
| 172 | static inline void __down_read(struct rw_semaphore *sem) |
| 173 | { |
Waiman Long | a68e2c4 | 2019-04-04 13:43:14 -0400 | [diff] [blame] | 174 | if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0)) { |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 175 | rwsem_down_read_failed(sem); |
Waiman Long | a68e2c4 | 2019-04-04 13:43:14 -0400 | [diff] [blame] | 176 | DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & |
Waiman Long | 3b4ba66 | 2019-04-04 13:43:15 -0400 | [diff] [blame^] | 177 | RWSEM_READER_OWNED), sem); |
Waiman Long | a68e2c4 | 2019-04-04 13:43:14 -0400 | [diff] [blame] | 178 | } else { |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 179 | rwsem_set_reader_owned(sem); |
Waiman Long | a68e2c4 | 2019-04-04 13:43:14 -0400 | [diff] [blame] | 180 | } |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static inline int __down_read_killable(struct rw_semaphore *sem) |
| 184 | { |
| 185 | if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0)) { |
| 186 | if (IS_ERR(rwsem_down_read_failed_killable(sem))) |
| 187 | return -EINTR; |
Waiman Long | a68e2c4 | 2019-04-04 13:43:14 -0400 | [diff] [blame] | 188 | DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & |
Waiman Long | 3b4ba66 | 2019-04-04 13:43:15 -0400 | [diff] [blame^] | 189 | RWSEM_READER_OWNED), sem); |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 190 | } else { |
| 191 | rwsem_set_reader_owned(sem); |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 192 | } |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static inline int __down_read_trylock(struct rw_semaphore *sem) |
| 197 | { |
Waiman Long | ddb20d1 | 2019-03-22 10:30:08 -0400 | [diff] [blame] | 198 | /* |
| 199 | * Optimize for the case when the rwsem is not locked at all. |
| 200 | */ |
| 201 | long tmp = RWSEM_UNLOCKED_VALUE; |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 202 | |
Waiman Long | ddb20d1 | 2019-03-22 10:30:08 -0400 | [diff] [blame] | 203 | do { |
| 204 | if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp, |
| 205 | tmp + RWSEM_ACTIVE_READ_BIAS)) { |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 206 | rwsem_set_reader_owned(sem); |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 207 | return 1; |
| 208 | } |
Waiman Long | ddb20d1 | 2019-03-22 10:30:08 -0400 | [diff] [blame] | 209 | } while (tmp >= 0); |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * lock for writing |
| 215 | */ |
| 216 | static inline void __down_write(struct rw_semaphore *sem) |
| 217 | { |
| 218 | long tmp; |
| 219 | |
| 220 | tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS, |
| 221 | &sem->count); |
| 222 | if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS)) |
| 223 | rwsem_down_write_failed(sem); |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 224 | rwsem_set_owner(sem); |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static inline int __down_write_killable(struct rw_semaphore *sem) |
| 228 | { |
| 229 | long tmp; |
| 230 | |
| 231 | tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS, |
| 232 | &sem->count); |
| 233 | if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS)) |
| 234 | if (IS_ERR(rwsem_down_write_failed_killable(sem))) |
| 235 | return -EINTR; |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 236 | rwsem_set_owner(sem); |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static inline int __down_write_trylock(struct rw_semaphore *sem) |
| 241 | { |
| 242 | long tmp; |
| 243 | |
| 244 | tmp = atomic_long_cmpxchg_acquire(&sem->count, RWSEM_UNLOCKED_VALUE, |
| 245 | RWSEM_ACTIVE_WRITE_BIAS); |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 246 | if (tmp == RWSEM_UNLOCKED_VALUE) { |
| 247 | rwsem_set_owner(sem); |
| 248 | return true; |
| 249 | } |
| 250 | return false; |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* |
| 254 | * unlock after reading |
| 255 | */ |
| 256 | static inline void __up_read(struct rw_semaphore *sem) |
| 257 | { |
| 258 | long tmp; |
| 259 | |
Waiman Long | 3b4ba66 | 2019-04-04 13:43:15 -0400 | [diff] [blame^] | 260 | DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED), |
| 261 | sem); |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 262 | rwsem_clear_reader_owned(sem); |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 263 | tmp = atomic_long_dec_return_release(&sem->count); |
| 264 | if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0)) |
| 265 | rwsem_wake(sem); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * unlock after writing |
| 270 | */ |
| 271 | static inline void __up_write(struct rw_semaphore *sem) |
| 272 | { |
Waiman Long | 3b4ba66 | 2019-04-04 13:43:15 -0400 | [diff] [blame^] | 273 | DEBUG_RWSEMS_WARN_ON(sem->owner != current, sem); |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 274 | rwsem_clear_owner(sem); |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 275 | if (unlikely(atomic_long_sub_return_release(RWSEM_ACTIVE_WRITE_BIAS, |
| 276 | &sem->count) < 0)) |
| 277 | rwsem_wake(sem); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * downgrade write lock to read lock |
| 282 | */ |
| 283 | static inline void __downgrade_write(struct rw_semaphore *sem) |
| 284 | { |
| 285 | long tmp; |
| 286 | |
| 287 | /* |
| 288 | * When downgrading from exclusive to shared ownership, |
| 289 | * anything inside the write-locked region cannot leak |
| 290 | * into the read side. In contrast, anything in the |
| 291 | * read-locked region is ok to be re-ordered into the |
| 292 | * write side. As such, rely on RELEASE semantics. |
| 293 | */ |
Waiman Long | 3b4ba66 | 2019-04-04 13:43:15 -0400 | [diff] [blame^] | 294 | DEBUG_RWSEMS_WARN_ON(sem->owner != current, sem); |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 295 | tmp = atomic_long_add_return_release(-RWSEM_WAITING_BIAS, &sem->count); |
Waiman Long | c7580c1 | 2019-04-04 13:43:11 -0400 | [diff] [blame] | 296 | rwsem_set_reader_owned(sem); |
Waiman Long | 46ad084 | 2019-03-22 10:30:06 -0400 | [diff] [blame] | 297 | if (tmp < 0) |
| 298 | rwsem_downgrade_wake(sem); |
| 299 | } |