1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* rwsem.h: R/W semaphores, public interface
4 * Written by David Howells (dhowells@redhat.com).
5 * Derived from asm-i386/semaphore.h
11 #include <linux/linkage.h>
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>
20 #ifdef CONFIG_DEBUG_LOCK_ALLOC
21 # define __RWSEM_DEP_MAP_INIT(lockname) \
24 .wait_type_inner = LD_WAIT_SLEEP, \
27 # define __RWSEM_DEP_MAP_INIT(lockname)
30 #ifndef CONFIG_PREEMPT_RT
32 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
33 #include <linux/osq_lock.h>
37 * For an uncontended rwsem, count and owner are the only fields a task
38 * needs to touch when acquiring the rwsem. So they are put next to each
39 * other to increase the chance that they will share the same cacheline.
41 * In a contended rwsem, the owner is likely the most frequently accessed
42 * field in the structure as the optimistic waiter that holds the osq lock
43 * will spin on owner. For an embedded rwsem, other hot fields in the
44 * containing structure should be moved further away from the rwsem to
45 * reduce the chance that they will share the same cacheline causing
46 * cacheline bouncing problem.
51 * Write owner or one of the read owners as well flags regarding
52 * the current state of the rwsem. Can be used as a speculative
53 * check to see if the write owner is running on the cpu.
56 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
57 struct optimistic_spin_queue osq; /* spinner MCS lock */
59 raw_spinlock_t wait_lock;
60 struct list_head wait_list;
61 #ifdef CONFIG_DEBUG_RWSEMS
64 #ifdef CONFIG_DEBUG_LOCK_ALLOC
65 struct lockdep_map dep_map;
69 /* In all implementations count != 0 means locked */
70 static inline int rwsem_is_locked(struct rw_semaphore *sem)
72 return atomic_long_read(&sem->count) != 0;
75 #define RWSEM_UNLOCKED_VALUE 0L
76 #define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
78 /* Common initializer macros and functions */
80 #ifdef CONFIG_DEBUG_RWSEMS
81 # define __RWSEM_DEBUG_INIT(lockname) .magic = &lockname,
83 # define __RWSEM_DEBUG_INIT(lockname)
86 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
87 #define __RWSEM_OPT_INIT(lockname) .osq = OSQ_LOCK_UNLOCKED,
89 #define __RWSEM_OPT_INIT(lockname)
92 #define __RWSEM_INITIALIZER(name) \
93 { __RWSEM_COUNT_INIT(name), \
94 .owner = ATOMIC_LONG_INIT(0), \
95 __RWSEM_OPT_INIT(name) \
96 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\
97 .wait_list = LIST_HEAD_INIT((name).wait_list), \
98 __RWSEM_DEBUG_INIT(name) \
99 __RWSEM_DEP_MAP_INIT(name) }
101 #define DECLARE_RWSEM(name) \
102 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
104 extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
105 struct lock_class_key *key);
107 #define init_rwsem(sem) \
109 static struct lock_class_key __key; \
111 __init_rwsem((sem), #sem, &__key); \
115 * This is the same regardless of which rwsem implementation that is being used.
116 * It is just a heuristic meant to be called by somebody already holding the
117 * rwsem to see if somebody from an incompatible type is wanting access to the
120 static inline int rwsem_is_contended(struct rw_semaphore *sem)
122 return !list_empty(&sem->wait_list);
125 #else /* !CONFIG_PREEMPT_RT */
127 #include <linux/rwbase_rt.h>
129 struct rw_semaphore {
130 struct rwbase_rt rwbase;
131 #ifdef CONFIG_DEBUG_LOCK_ALLOC
132 struct lockdep_map dep_map;
136 #define __RWSEM_INITIALIZER(name) \
138 .rwbase = __RWBASE_INITIALIZER(name), \
139 __RWSEM_DEP_MAP_INIT(name) \
142 #define DECLARE_RWSEM(lockname) \
143 struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
145 #ifdef CONFIG_DEBUG_LOCK_ALLOC
146 extern void __rwsem_init(struct rw_semaphore *rwsem, const char *name,
147 struct lock_class_key *key);
149 static inline void __rwsem_init(struct rw_semaphore *rwsem, const char *name,
150 struct lock_class_key *key)
155 #define init_rwsem(sem) \
157 static struct lock_class_key __key; \
159 init_rwbase_rt(&(sem)->rwbase); \
160 __rwsem_init((sem), #sem, &__key); \
163 static __always_inline int rwsem_is_locked(struct rw_semaphore *sem)
165 return rw_base_is_locked(&sem->rwbase);
168 static __always_inline int rwsem_is_contended(struct rw_semaphore *sem)
170 return rw_base_is_contended(&sem->rwbase);
173 #endif /* CONFIG_PREEMPT_RT */
176 * The functions below are the same for all rwsem implementations including
177 * the RT specific variant.
183 extern void down_read(struct rw_semaphore *sem);
184 extern int __must_check down_read_interruptible(struct rw_semaphore *sem);
185 extern int __must_check down_read_killable(struct rw_semaphore *sem);
188 * trylock for reading -- returns 1 if successful, 0 if contention
190 extern int down_read_trylock(struct rw_semaphore *sem);
195 extern void down_write(struct rw_semaphore *sem);
196 extern int __must_check down_write_killable(struct rw_semaphore *sem);
199 * trylock for writing -- returns 1 if successful, 0 if contention
201 extern int down_write_trylock(struct rw_semaphore *sem);
204 * release a read lock
206 extern void up_read(struct rw_semaphore *sem);
209 * release a write lock
211 extern void up_write(struct rw_semaphore *sem);
214 * downgrade write lock to read lock
216 extern void downgrade_write(struct rw_semaphore *sem);
218 #ifdef CONFIG_DEBUG_LOCK_ALLOC
220 * nested locking. NOTE: rwsems are not allowed to recurse
221 * (which occurs if the same task tries to acquire the same
222 * lock instance multiple times), but multiple locks of the
223 * same lock class might be taken, if the order of the locks
224 * is always the same. This ordering rule can be expressed
225 * to lockdep via the _nested() APIs, but enumerating the
226 * subclasses that are used. (If the nesting relationship is
227 * static then another method for expressing nested locking is
228 * the explicit definition of lock class keys and the use of
229 * lockdep_set_class() at lock initialization time.
230 * See Documentation/locking/lockdep-design.rst for more details.)
232 extern void down_read_nested(struct rw_semaphore *sem, int subclass);
233 extern int __must_check down_read_killable_nested(struct rw_semaphore *sem, int subclass);
234 extern void down_write_nested(struct rw_semaphore *sem, int subclass);
235 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
236 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
238 # define down_write_nest_lock(sem, nest_lock) \
240 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
241 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
245 * Take/release a lock when not the owner will release it.
247 * [ This API should be avoided as much as possible - the
248 * proper abstraction for this case is completions. ]
250 extern void down_read_non_owner(struct rw_semaphore *sem);
251 extern void up_read_non_owner(struct rw_semaphore *sem);
253 # define down_read_nested(sem, subclass) down_read(sem)
254 # define down_read_killable_nested(sem, subclass) down_read_killable(sem)
255 # define down_write_nest_lock(sem, nest_lock) down_write(sem)
256 # define down_write_nested(sem, subclass) down_write(sem)
257 # define down_write_killable_nested(sem, subclass) down_write_killable(sem)
258 # define down_read_non_owner(sem) down_read(sem)
259 # define up_read_non_owner(sem) up_read(sem)
262 #endif /* _LINUX_RWSEM_H */