blob: 64877f5294e35b194232b1dca59e0db84d48d32c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Waiman Long19c5d692016-05-17 21:26:19 -04002/*
Waiman Long925b9cd2018-09-06 16:18:34 -04003 * 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 Long19c5d692016-05-17 21:26:19 -04009 *
Waiman Long925b9cd2018-09-06 16:18:34 -040010 * 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 Long19c5d692016-05-17 21:26:19 -040025 */
Waiman Longa8654592019-04-04 13:43:19 -040026#include "lock_events.h"
27
Waiman Long925b9cd2018-09-06 16:18:34 -040028#define RWSEM_READER_OWNED (1UL << 0)
29#define RWSEM_ANONYMOUSLY_OWNED (1UL << 1)
Waiman Long19c5d692016-05-17 21:26:19 -040030
Waiman Long5149cba2018-03-30 17:27:58 -040031#ifdef CONFIG_DEBUG_RWSEMS
Waiman Long3b4ba662019-04-04 13:43:15 -040032# define DEBUG_RWSEMS_WARN_ON(c, sem) do { \
Waiman Long26536e72019-04-13 13:22:44 -040033 if (!debug_locks_silent && \
34 WARN_ONCE(c, "DEBUG_RWSEMS_WARN_ON(%s): count = 0x%lx, owner = 0x%lx, curr 0x%lx, list %sempty\n",\
Waiman Long3b4ba662019-04-04 13:43:15 -040035 #c, atomic_long_read(&(sem)->count), \
36 (long)((sem)->owner), (long)current, \
37 list_empty(&(sem)->wait_list) ? "" : "not ")) \
38 debug_locks_off(); \
39 } while (0)
Waiman Long5149cba2018-03-30 17:27:58 -040040#else
Waiman Long3b4ba662019-04-04 13:43:15 -040041# define DEBUG_RWSEMS_WARN_ON(c, sem)
Waiman Long5149cba2018-03-30 17:27:58 -040042#endif
43
Waiman Long46ad0842019-03-22 10:30:06 -040044/*
45 * R/W semaphores originally for PPC using the stuff in lib/rwsem.c.
46 * Adapted largely from include/asm-i386/rwsem.h
47 * by Paul Mackerras <paulus@samba.org>.
48 */
49
50/*
51 * the semaphore definition
52 */
53#ifdef CONFIG_64BIT
54# define RWSEM_ACTIVE_MASK 0xffffffffL
55#else
56# define RWSEM_ACTIVE_MASK 0x0000ffffL
57#endif
58
59#define RWSEM_ACTIVE_BIAS 0x00000001L
60#define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
61#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
62#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
63
Davidlohr Bueso7a215f82015-01-30 01:14:25 -080064#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Waiman Longfb6a44f2016-05-17 21:26:20 -040065/*
66 * All writes to owner are protected by WRITE_ONCE() to make sure that
67 * store tearing can't happen as optimistic spinners may read and use
68 * the owner value concurrently without lock. Read from owner, however,
69 * may not need READ_ONCE() as long as the pointer value is only used
70 * for comparison and isn't being dereferenced.
71 */
Davidlohr Bueso7a215f82015-01-30 01:14:25 -080072static inline void rwsem_set_owner(struct rw_semaphore *sem)
73{
Waiman Longfb6a44f2016-05-17 21:26:20 -040074 WRITE_ONCE(sem->owner, current);
Davidlohr Bueso7a215f82015-01-30 01:14:25 -080075}
76
77static inline void rwsem_clear_owner(struct rw_semaphore *sem)
78{
Waiman Longfb6a44f2016-05-17 21:26:20 -040079 WRITE_ONCE(sem->owner, NULL);
Davidlohr Bueso7a215f82015-01-30 01:14:25 -080080}
81
Waiman Long925b9cd2018-09-06 16:18:34 -040082/*
83 * The task_struct pointer of the last owning reader will be left in
84 * the owner field.
85 *
86 * Note that the owner value just indicates the task has owned the rwsem
87 * previously, it may not be the real owner or one of the real owners
88 * anymore when that field is examined, so take it with a grain of salt.
89 */
90static inline void __rwsem_set_reader_owned(struct rw_semaphore *sem,
91 struct task_struct *owner)
92{
93 unsigned long val = (unsigned long)owner | RWSEM_READER_OWNED
94 | RWSEM_ANONYMOUSLY_OWNED;
95
96 WRITE_ONCE(sem->owner, (struct task_struct *)val);
97}
98
Waiman Long19c5d692016-05-17 21:26:19 -040099static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
100{
Waiman Long925b9cd2018-09-06 16:18:34 -0400101 __rwsem_set_reader_owned(sem, current);
Waiman Long19c5d692016-05-17 21:26:19 -0400102}
103
Waiman Longd7d760e2018-05-15 17:49:50 -0400104/*
105 * Return true if the a rwsem waiter can spin on the rwsem's owner
106 * and steal the lock, i.e. the lock is not anonymously owned.
107 * N.B. !owner is considered spinnable.
108 */
109static inline bool is_rwsem_owner_spinnable(struct task_struct *owner)
Waiman Long19c5d692016-05-17 21:26:19 -0400110{
Waiman Longd7d760e2018-05-15 17:49:50 -0400111 return !((unsigned long)owner & RWSEM_ANONYMOUSLY_OWNED);
Waiman Long19c5d692016-05-17 21:26:19 -0400112}
113
Waiman Longd7d760e2018-05-15 17:49:50 -0400114/*
115 * Return true if rwsem is owned by an anonymous writer or readers.
116 */
117static inline bool rwsem_has_anonymous_owner(struct task_struct *owner)
Waiman Long19c5d692016-05-17 21:26:19 -0400118{
Waiman Longd7d760e2018-05-15 17:49:50 -0400119 return (unsigned long)owner & RWSEM_ANONYMOUSLY_OWNED;
Waiman Long19c5d692016-05-17 21:26:19 -0400120}
Waiman Long925b9cd2018-09-06 16:18:34 -0400121
122#ifdef CONFIG_DEBUG_RWSEMS
123/*
124 * With CONFIG_DEBUG_RWSEMS configured, it will make sure that if there
125 * is a task pointer in owner of a reader-owned rwsem, it will be the
126 * real owner or one of the real owners. The only exception is when the
127 * unlock is done by up_read_non_owner().
128 */
129#define rwsem_clear_reader_owned rwsem_clear_reader_owned
130static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem)
131{
132 unsigned long val = (unsigned long)current | RWSEM_READER_OWNED
133 | RWSEM_ANONYMOUSLY_OWNED;
134 if (READ_ONCE(sem->owner) == (struct task_struct *)val)
135 cmpxchg_relaxed((unsigned long *)&sem->owner, val,
136 RWSEM_READER_OWNED | RWSEM_ANONYMOUSLY_OWNED);
137}
138#endif
139
Davidlohr Bueso7a215f82015-01-30 01:14:25 -0800140#else
141static inline void rwsem_set_owner(struct rw_semaphore *sem)
142{
143}
144
145static inline void rwsem_clear_owner(struct rw_semaphore *sem)
146{
147}
Waiman Long19c5d692016-05-17 21:26:19 -0400148
Waiman Long925b9cd2018-09-06 16:18:34 -0400149static inline void __rwsem_set_reader_owned(struct rw_semaphore *sem,
150 struct task_struct *owner)
151{
152}
153
Waiman Long19c5d692016-05-17 21:26:19 -0400154static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
155{
156}
Davidlohr Bueso7a215f82015-01-30 01:14:25 -0800157#endif
Waiman Long925b9cd2018-09-06 16:18:34 -0400158
159#ifndef rwsem_clear_reader_owned
160static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem)
161{
162}
163#endif
Waiman Long46ad0842019-03-22 10:30:06 -0400164
Waiman Long12a30a72019-04-04 13:43:12 -0400165extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
166extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem);
167extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
168extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
169extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
170extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
171
Waiman Long46ad0842019-03-22 10:30:06 -0400172/*
173 * lock for reading
174 */
175static inline void __down_read(struct rw_semaphore *sem)
176{
Waiman Longa68e2c42019-04-04 13:43:14 -0400177 if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0)) {
Waiman Long46ad0842019-03-22 10:30:06 -0400178 rwsem_down_read_failed(sem);
Waiman Longa68e2c42019-04-04 13:43:14 -0400179 DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner &
Waiman Long3b4ba662019-04-04 13:43:15 -0400180 RWSEM_READER_OWNED), sem);
Waiman Longa68e2c42019-04-04 13:43:14 -0400181 } else {
Waiman Longc7580c12019-04-04 13:43:11 -0400182 rwsem_set_reader_owned(sem);
Waiman Longa68e2c42019-04-04 13:43:14 -0400183 }
Waiman Long46ad0842019-03-22 10:30:06 -0400184}
185
186static inline int __down_read_killable(struct rw_semaphore *sem)
187{
188 if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0)) {
189 if (IS_ERR(rwsem_down_read_failed_killable(sem)))
190 return -EINTR;
Waiman Longa68e2c42019-04-04 13:43:14 -0400191 DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner &
Waiman Long3b4ba662019-04-04 13:43:15 -0400192 RWSEM_READER_OWNED), sem);
Waiman Longc7580c12019-04-04 13:43:11 -0400193 } else {
194 rwsem_set_reader_owned(sem);
Waiman Long46ad0842019-03-22 10:30:06 -0400195 }
Waiman Long46ad0842019-03-22 10:30:06 -0400196 return 0;
197}
198
199static inline int __down_read_trylock(struct rw_semaphore *sem)
200{
Waiman Longddb20d12019-03-22 10:30:08 -0400201 /*
202 * Optimize for the case when the rwsem is not locked at all.
203 */
204 long tmp = RWSEM_UNLOCKED_VALUE;
Waiman Long46ad0842019-03-22 10:30:06 -0400205
Waiman Longa8654592019-04-04 13:43:19 -0400206 lockevent_inc(rwsem_rtrylock);
Waiman Longddb20d12019-03-22 10:30:08 -0400207 do {
208 if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
209 tmp + RWSEM_ACTIVE_READ_BIAS)) {
Waiman Longc7580c12019-04-04 13:43:11 -0400210 rwsem_set_reader_owned(sem);
Waiman Long46ad0842019-03-22 10:30:06 -0400211 return 1;
212 }
Waiman Longddb20d12019-03-22 10:30:08 -0400213 } while (tmp >= 0);
Waiman Long46ad0842019-03-22 10:30:06 -0400214 return 0;
215}
216
217/*
218 * lock for writing
219 */
220static inline void __down_write(struct rw_semaphore *sem)
221{
222 long tmp;
223
224 tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS,
225 &sem->count);
226 if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
227 rwsem_down_write_failed(sem);
Waiman Longc7580c12019-04-04 13:43:11 -0400228 rwsem_set_owner(sem);
Waiman Long46ad0842019-03-22 10:30:06 -0400229}
230
231static inline int __down_write_killable(struct rw_semaphore *sem)
232{
233 long tmp;
234
235 tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS,
236 &sem->count);
237 if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
238 if (IS_ERR(rwsem_down_write_failed_killable(sem)))
239 return -EINTR;
Waiman Longc7580c12019-04-04 13:43:11 -0400240 rwsem_set_owner(sem);
Waiman Long46ad0842019-03-22 10:30:06 -0400241 return 0;
242}
243
244static inline int __down_write_trylock(struct rw_semaphore *sem)
245{
246 long tmp;
247
Waiman Longa8654592019-04-04 13:43:19 -0400248 lockevent_inc(rwsem_wtrylock);
Waiman Long46ad0842019-03-22 10:30:06 -0400249 tmp = atomic_long_cmpxchg_acquire(&sem->count, RWSEM_UNLOCKED_VALUE,
250 RWSEM_ACTIVE_WRITE_BIAS);
Waiman Longc7580c12019-04-04 13:43:11 -0400251 if (tmp == RWSEM_UNLOCKED_VALUE) {
252 rwsem_set_owner(sem);
253 return true;
254 }
255 return false;
Waiman Long46ad0842019-03-22 10:30:06 -0400256}
257
258/*
259 * unlock after reading
260 */
261static inline void __up_read(struct rw_semaphore *sem)
262{
263 long tmp;
264
Waiman Long3b4ba662019-04-04 13:43:15 -0400265 DEBUG_RWSEMS_WARN_ON(!((unsigned long)sem->owner & RWSEM_READER_OWNED),
266 sem);
Waiman Longc7580c12019-04-04 13:43:11 -0400267 rwsem_clear_reader_owned(sem);
Waiman Long46ad0842019-03-22 10:30:06 -0400268 tmp = atomic_long_dec_return_release(&sem->count);
269 if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0))
270 rwsem_wake(sem);
271}
272
273/*
274 * unlock after writing
275 */
276static inline void __up_write(struct rw_semaphore *sem)
277{
Waiman Long3b4ba662019-04-04 13:43:15 -0400278 DEBUG_RWSEMS_WARN_ON(sem->owner != current, sem);
Waiman Longc7580c12019-04-04 13:43:11 -0400279 rwsem_clear_owner(sem);
Waiman Long46ad0842019-03-22 10:30:06 -0400280 if (unlikely(atomic_long_sub_return_release(RWSEM_ACTIVE_WRITE_BIAS,
281 &sem->count) < 0))
282 rwsem_wake(sem);
283}
284
285/*
286 * downgrade write lock to read lock
287 */
288static inline void __downgrade_write(struct rw_semaphore *sem)
289{
290 long tmp;
291
292 /*
293 * When downgrading from exclusive to shared ownership,
294 * anything inside the write-locked region cannot leak
295 * into the read side. In contrast, anything in the
296 * read-locked region is ok to be re-ordered into the
297 * write side. As such, rely on RELEASE semantics.
298 */
Waiman Long3b4ba662019-04-04 13:43:15 -0400299 DEBUG_RWSEMS_WARN_ON(sem->owner != current, sem);
Waiman Long46ad0842019-03-22 10:30:06 -0400300 tmp = atomic_long_add_return_release(-RWSEM_WAITING_BIAS, &sem->count);
Waiman Longc7580c12019-04-04 13:43:11 -0400301 rwsem_set_reader_owned(sem);
Waiman Long46ad0842019-03-22 10:30:06 -0400302 if (tmp < 0)
303 rwsem_downgrade_wake(sem);
304}