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