Merge tag 'mm-hotfixes-stable-2023-05-03-16-27' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / include / linux / rwsem.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2/* rwsem.h: R/W semaphores, public interface
3 *
4 * Written by David Howells (dhowells@redhat.com).
5 * Derived from asm-i386/semaphore.h
6 */
7
8#ifndef _LINUX_RWSEM_H
9#define _LINUX_RWSEM_H
10
11#include <linux/linkage.h>
12
1da177e4 13#include <linux/types.h>
c16a87ce
TG
14#include <linux/list.h>
15#include <linux/spinlock.h>
60063497 16#include <linux/atomic.h>
d4799608 17#include <linux/err.h>
42254105
TG
18
19#ifdef CONFIG_DEBUG_LOCK_ALLOC
20# define __RWSEM_DEP_MAP_INIT(lockname) \
21 .dep_map = { \
22 .name = #lockname, \
23 .wait_type_inner = LD_WAIT_SLEEP, \
24 },
25#else
26# define __RWSEM_DEP_MAP_INIT(lockname)
27#endif
28
29#ifndef CONFIG_PREEMPT_RT
30
5db6c6fe 31#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
90631822 32#include <linux/osq_lock.h>
5db6c6fe 33#endif
1da177e4 34
364f784f
WL
35/*
36 * For an uncontended rwsem, count and owner are the only fields a task
37 * needs to touch when acquiring the rwsem. So they are put next to each
38 * other to increase the chance that they will share the same cacheline.
39 *
40 * In a contended rwsem, the owner is likely the most frequently accessed
41 * field in the structure as the optimistic waiter that holds the osq lock
42 * will spin on owner. For an embedded rwsem, other hot fields in the
43 * containing structure should be moved further away from the rwsem to
44 * reduce the chance that they will share the same cacheline causing
45 * cacheline bouncing problem.
46 */
1c8ed640 47struct rw_semaphore {
8ee62b18 48 atomic_long_t count;
4fc828e2 49 /*
94a9717b
WL
50 * Write owner or one of the read owners as well flags regarding
51 * the current state of the rwsem. Can be used as a speculative
52 * check to see if the write owner is running on the cpu.
4fc828e2 53 */
94a9717b 54 atomic_long_t owner;
c71fd893 55#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
364f784f 56 struct optimistic_spin_queue osq; /* spinner MCS lock */
4fc828e2 57#endif
364f784f
WL
58 raw_spinlock_t wait_lock;
59 struct list_head wait_list;
fce45cd4
DB
60#ifdef CONFIG_DEBUG_RWSEMS
61 void *magic;
62#endif
1c8ed640
TG
63#ifdef CONFIG_DEBUG_LOCK_ALLOC
64 struct lockdep_map dep_map;
65#endif
66};
67
41e5887f
TG
68/* In all implementations count != 0 means locked */
69static inline int rwsem_is_locked(struct rw_semaphore *sem)
70{
8ee62b18 71 return atomic_long_read(&sem->count) != 0;
41e5887f
TG
72}
73
46ad0840 74#define RWSEM_UNLOCKED_VALUE 0L
a9232dc5 75#define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
1da177e4 76
12249b34
TG
77/* Common initializer macros and functions */
78
fce45cd4 79#ifdef CONFIG_DEBUG_RWSEMS
a9232dc5 80# define __RWSEM_DEBUG_INIT(lockname) .magic = &lockname,
fce45cd4 81#else
a9232dc5 82# define __RWSEM_DEBUG_INIT(lockname)
fce45cd4
DB
83#endif
84
5db6c6fe 85#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
a9232dc5 86#define __RWSEM_OPT_INIT(lockname) .osq = OSQ_LOCK_UNLOCKED,
4fc828e2 87#else
ce069fc9 88#define __RWSEM_OPT_INIT(lockname)
4fc828e2 89#endif
12249b34 90
ce069fc9 91#define __RWSEM_INITIALIZER(name) \
a9232dc5 92 { __RWSEM_COUNT_INIT(name), \
94a9717b 93 .owner = ATOMIC_LONG_INIT(0), \
ce069fc9 94 __RWSEM_OPT_INIT(name) \
a9232dc5
AD
95 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\
96 .wait_list = LIST_HEAD_INIT((name).wait_list), \
97 __RWSEM_DEBUG_INIT(name) \
ce069fc9
JL
98 __RWSEM_DEP_MAP_INIT(name) }
99
12249b34
TG
100#define DECLARE_RWSEM(name) \
101 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
102
103extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
104 struct lock_class_key *key);
105
106#define init_rwsem(sem) \
107do { \
108 static struct lock_class_key __key; \
109 \
110 __init_rwsem((sem), #sem, &__key); \
111} while (0)
112
4a444b1f
JB
113/*
114 * This is the same regardless of which rwsem implementation that is being used.
e2db7592 115 * It is just a heuristic meant to be called by somebody already holding the
4a444b1f
JB
116 * rwsem to see if somebody from an incompatible type is wanting access to the
117 * lock.
118 */
119static inline int rwsem_is_contended(struct rw_semaphore *sem)
120{
121 return !list_empty(&sem->wait_list);
122}
123
42254105
TG
124#else /* !CONFIG_PREEMPT_RT */
125
126#include <linux/rwbase_rt.h>
127
128struct rw_semaphore {
129 struct rwbase_rt rwbase;
130#ifdef CONFIG_DEBUG_LOCK_ALLOC
131 struct lockdep_map dep_map;
132#endif
133};
134
135#define __RWSEM_INITIALIZER(name) \
136 { \
137 .rwbase = __RWBASE_INITIALIZER(name), \
138 __RWSEM_DEP_MAP_INIT(name) \
139 }
140
141#define DECLARE_RWSEM(lockname) \
142 struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
143
15eb7c88 144extern void __init_rwsem(struct rw_semaphore *rwsem, const char *name,
42254105 145 struct lock_class_key *key);
42254105
TG
146
147#define init_rwsem(sem) \
148do { \
149 static struct lock_class_key __key; \
150 \
15eb7c88 151 __init_rwsem((sem), #sem, &__key); \
42254105
TG
152} while (0)
153
154static __always_inline int rwsem_is_locked(struct rw_semaphore *sem)
155{
156 return rw_base_is_locked(&sem->rwbase);
157}
158
159static __always_inline int rwsem_is_contended(struct rw_semaphore *sem)
160{
161 return rw_base_is_contended(&sem->rwbase);
162}
163
164#endif /* CONFIG_PREEMPT_RT */
165
166/*
167 * The functions below are the same for all rwsem implementations including
168 * the RT specific variant.
169 */
170
1da177e4
LT
171/*
172 * lock for reading
173 */
4ea2176d 174extern void down_read(struct rw_semaphore *sem);
31784cff 175extern int __must_check down_read_interruptible(struct rw_semaphore *sem);
76f8507f 176extern int __must_check down_read_killable(struct rw_semaphore *sem);
1da177e4
LT
177
178/*
179 * trylock for reading -- returns 1 if successful, 0 if contention
180 */
4ea2176d 181extern int down_read_trylock(struct rw_semaphore *sem);
1da177e4
LT
182
183/*
184 * lock for writing
185 */
4ea2176d 186extern void down_write(struct rw_semaphore *sem);
916633a4 187extern int __must_check down_write_killable(struct rw_semaphore *sem);
1da177e4
LT
188
189/*
190 * trylock for writing -- returns 1 if successful, 0 if contention
191 */
4ea2176d 192extern int down_write_trylock(struct rw_semaphore *sem);
1da177e4
LT
193
194/*
195 * release a read lock
196 */
4ea2176d 197extern void up_read(struct rw_semaphore *sem);
1da177e4
LT
198
199/*
200 * release a write lock
201 */
4ea2176d 202extern void up_write(struct rw_semaphore *sem);
1da177e4
LT
203
204/*
205 * downgrade write lock to read lock
206 */
4ea2176d
IM
207extern void downgrade_write(struct rw_semaphore *sem);
208
209#ifdef CONFIG_DEBUG_LOCK_ALLOC
210/*
5fca80e8
IM
211 * nested locking. NOTE: rwsems are not allowed to recurse
212 * (which occurs if the same task tries to acquire the same
213 * lock instance multiple times), but multiple locks of the
214 * same lock class might be taken, if the order of the locks
215 * is always the same. This ordering rule can be expressed
216 * to lockdep via the _nested() APIs, but enumerating the
217 * subclasses that are used. (If the nesting relationship is
218 * static then another method for expressing nested locking is
219 * the explicit definition of lock class keys and the use of
220 * lockdep_set_class() at lock initialization time.
387b1468 221 * See Documentation/locking/lockdep-design.rst for more details.)
4ea2176d
IM
222 */
223extern void down_read_nested(struct rw_semaphore *sem, int subclass);
0f9368b5 224extern int __must_check down_read_killable_nested(struct rw_semaphore *sem, int subclass);
4ea2176d 225extern void down_write_nested(struct rw_semaphore *sem, int subclass);
887bddfa 226extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
1b963c81
JK
227extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
228
229# define down_write_nest_lock(sem, nest_lock) \
230do { \
231 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
232 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
d72d84ae 233} while (0)
1b963c81 234
84759c6d
KO
235/*
236 * Take/release a lock when not the owner will release it.
237 *
238 * [ This API should be avoided as much as possible - the
239 * proper abstraction for this case is completions. ]
240 */
241extern void down_read_non_owner(struct rw_semaphore *sem);
242extern void up_read_non_owner(struct rw_semaphore *sem);
4ea2176d
IM
243#else
244# define down_read_nested(sem, subclass) down_read(sem)
0f9368b5 245# define down_read_killable_nested(sem, subclass) down_read_killable(sem)
e65b9ad2 246# define down_write_nest_lock(sem, nest_lock) down_write(sem)
4ea2176d 247# define down_write_nested(sem, subclass) down_write(sem)
887bddfa 248# define down_write_killable_nested(sem, subclass) down_write_killable(sem)
84759c6d
KO
249# define down_read_non_owner(sem) down_read(sem)
250# define up_read_non_owner(sem) up_read(sem)
4ea2176d 251#endif
1da177e4 252
1da177e4 253#endif /* _LINUX_RWSEM_H */