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/kernel.h>
15 #include <linux/list.h>
16 #include <linux/spinlock.h>
17 #include <linux/atomic.h>
18 #include <linux/err.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 enum rwsem_waiter_type {
73 RWSEM_WAITING_FOR_WRITE,
74 RWSEM_WAITING_FOR_READ
75 };
76
77 struct rwsem_waiter {
78 struct list_head list;
79 struct task_struct *task;
80 enum rwsem_waiter_type type;
81 unsigned long timeout;
82 unsigned long last_rowner;
83
84 /* Writer only, not initialized in reader */
85 bool handoff_set;
86 };
87
88 /* In all implementations count != 0 means locked */
rwsem_is_locked(struct rw_semaphore * sem)89 static inline int rwsem_is_locked(struct rw_semaphore *sem)
90 {
91 return atomic_long_read(&sem->count) != 0;
92 }
93
94 #define RWSEM_UNLOCKED_VALUE 0L
95 #define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
96
97 /* Common initializer macros and functions */
98
99 #ifdef CONFIG_DEBUG_RWSEMS
100 # define __RWSEM_DEBUG_INIT(lockname) .magic = &lockname,
101 #else
102 # define __RWSEM_DEBUG_INIT(lockname)
103 #endif
104
105 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
106 #define __RWSEM_OPT_INIT(lockname) .osq = OSQ_LOCK_UNLOCKED,
107 #else
108 #define __RWSEM_OPT_INIT(lockname)
109 #endif
110
111 #define __RWSEM_INITIALIZER(name) \
112 { __RWSEM_COUNT_INIT(name), \
113 .owner = ATOMIC_LONG_INIT(0), \
114 __RWSEM_OPT_INIT(name) \
115 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\
116 .wait_list = LIST_HEAD_INIT((name).wait_list), \
117 __RWSEM_DEBUG_INIT(name) \
118 __RWSEM_DEP_MAP_INIT(name) }
119
120 #define DECLARE_RWSEM(name) \
121 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
122
123 extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
124 struct lock_class_key *key);
125
126 #define init_rwsem(sem) \
127 do { \
128 static struct lock_class_key __key; \
129 \
130 __init_rwsem((sem), #sem, &__key); \
131 } while (0)
132
133 /*
134 * This is the same regardless of which rwsem implementation that is being used.
135 * It is just a heuristic meant to be called by somebody already holding the
136 * rwsem to see if somebody from an incompatible type is wanting access to the
137 * lock.
138 */
rwsem_is_contended(struct rw_semaphore * sem)139 static inline int rwsem_is_contended(struct rw_semaphore *sem)
140 {
141 return !list_empty(&sem->wait_list);
142 }
143
144 #else /* !CONFIG_PREEMPT_RT */
145
146 #include <linux/rwbase_rt.h>
147
148 struct rw_semaphore {
149 struct rwbase_rt rwbase;
150 #ifdef CONFIG_DEBUG_LOCK_ALLOC
151 struct lockdep_map dep_map;
152 #endif
153 };
154
155 #define __RWSEM_INITIALIZER(name) \
156 { \
157 .rwbase = __RWBASE_INITIALIZER(name), \
158 __RWSEM_DEP_MAP_INIT(name) \
159 }
160
161 #define DECLARE_RWSEM(lockname) \
162 struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
163
164 extern void __init_rwsem(struct rw_semaphore *rwsem, const char *name,
165 struct lock_class_key *key);
166
167 #define init_rwsem(sem) \
168 do { \
169 static struct lock_class_key __key; \
170 \
171 __init_rwsem((sem), #sem, &__key); \
172 } while (0)
173
rwsem_is_locked(struct rw_semaphore * sem)174 static __always_inline int rwsem_is_locked(struct rw_semaphore *sem)
175 {
176 return rw_base_is_locked(&sem->rwbase);
177 }
178
rwsem_is_contended(struct rw_semaphore * sem)179 static __always_inline int rwsem_is_contended(struct rw_semaphore *sem)
180 {
181 return rw_base_is_contended(&sem->rwbase);
182 }
183
184 #endif /* CONFIG_PREEMPT_RT */
185
186 /*
187 * The functions below are the same for all rwsem implementations including
188 * the RT specific variant.
189 */
190
191 /*
192 * lock for reading
193 */
194 extern void down_read(struct rw_semaphore *sem);
195 extern int __must_check down_read_interruptible(struct rw_semaphore *sem);
196 extern int __must_check down_read_killable(struct rw_semaphore *sem);
197
198 /*
199 * trylock for reading -- returns 1 if successful, 0 if contention
200 */
201 extern int down_read_trylock(struct rw_semaphore *sem);
202
203 /*
204 * lock for writing
205 */
206 extern void down_write(struct rw_semaphore *sem);
207 extern int __must_check down_write_killable(struct rw_semaphore *sem);
208
209 /*
210 * trylock for writing -- returns 1 if successful, 0 if contention
211 */
212 extern int down_write_trylock(struct rw_semaphore *sem);
213
214 /*
215 * release a read lock
216 */
217 extern void up_read(struct rw_semaphore *sem);
218
219 /*
220 * release a write lock
221 */
222 extern void up_write(struct rw_semaphore *sem);
223
224 /*
225 * downgrade write lock to read lock
226 */
227 extern void downgrade_write(struct rw_semaphore *sem);
228
229 #ifdef CONFIG_DEBUG_LOCK_ALLOC
230 /*
231 * nested locking. NOTE: rwsems are not allowed to recurse
232 * (which occurs if the same task tries to acquire the same
233 * lock instance multiple times), but multiple locks of the
234 * same lock class might be taken, if the order of the locks
235 * is always the same. This ordering rule can be expressed
236 * to lockdep via the _nested() APIs, but enumerating the
237 * subclasses that are used. (If the nesting relationship is
238 * static then another method for expressing nested locking is
239 * the explicit definition of lock class keys and the use of
240 * lockdep_set_class() at lock initialization time.
241 * See Documentation/locking/lockdep-design.rst for more details.)
242 */
243 extern void down_read_nested(struct rw_semaphore *sem, int subclass);
244 extern int __must_check down_read_killable_nested(struct rw_semaphore *sem, int subclass);
245 extern void down_write_nested(struct rw_semaphore *sem, int subclass);
246 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
247 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
248
249 # define down_write_nest_lock(sem, nest_lock) \
250 do { \
251 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
252 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
253 } while (0);
254
255 /*
256 * Take/release a lock when not the owner will release it.
257 *
258 * [ This API should be avoided as much as possible - the
259 * proper abstraction for this case is completions. ]
260 */
261 extern void down_read_non_owner(struct rw_semaphore *sem);
262 extern void up_read_non_owner(struct rw_semaphore *sem);
263 #else
264 # define down_read_nested(sem, subclass) down_read(sem)
265 # define down_read_killable_nested(sem, subclass) down_read_killable(sem)
266 # define down_write_nest_lock(sem, nest_lock) down_write(sem)
267 # define down_write_nested(sem, subclass) down_write(sem)
268 # define down_write_killable_nested(sem, subclass) down_write_killable(sem)
269 # define down_read_non_owner(sem) down_read(sem)
270 # define up_read_non_owner(sem) up_read(sem)
271 #endif
272
273 #endif /* _LINUX_RWSEM_H */
274