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