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