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/list.h>
15 #include <linux/spinlock.h>
16 #include <linux/atomic.h>
17 #include <linux/err.h>
18 #include <linux/cleanup.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 extern void __init_rwsem(struct rw_semaphore *rwsem, const char *name,
146 struct lock_class_key *key);
148 #define init_rwsem(sem) \
150 static struct lock_class_key __key; \
152 __init_rwsem((sem), #sem, &__key); \
155 static __always_inline int rwsem_is_locked(struct rw_semaphore *sem)
157 return rw_base_is_locked(&sem->rwbase);
160 static __always_inline int rwsem_is_contended(struct rw_semaphore *sem)
162 return rw_base_is_contended(&sem->rwbase);
165 #endif /* CONFIG_PREEMPT_RT */
168 * The functions below are the same for all rwsem implementations including
169 * the RT specific variant.
175 extern void down_read(struct rw_semaphore *sem);
176 extern int __must_check down_read_interruptible(struct rw_semaphore *sem);
177 extern int __must_check down_read_killable(struct rw_semaphore *sem);
180 * trylock for reading -- returns 1 if successful, 0 if contention
182 extern int down_read_trylock(struct rw_semaphore *sem);
187 extern void down_write(struct rw_semaphore *sem);
188 extern int __must_check down_write_killable(struct rw_semaphore *sem);
191 * trylock for writing -- returns 1 if successful, 0 if contention
193 extern int down_write_trylock(struct rw_semaphore *sem);
196 * release a read lock
198 extern void up_read(struct rw_semaphore *sem);
201 * release a write lock
203 extern void up_write(struct rw_semaphore *sem);
205 DEFINE_GUARD(rwsem_read, struct rw_semaphore *, down_read(_T), up_read(_T))
206 DEFINE_GUARD(rwsem_write, struct rw_semaphore *, down_write(_T), up_write(_T))
208 DEFINE_FREE(up_read, struct rw_semaphore *, if (_T) up_read(_T))
209 DEFINE_FREE(up_write, struct rw_semaphore *, if (_T) up_write(_T))
213 * downgrade write lock to read lock
215 extern void downgrade_write(struct rw_semaphore *sem);
217 #ifdef CONFIG_DEBUG_LOCK_ALLOC
219 * nested locking. NOTE: rwsems are not allowed to recurse
220 * (which occurs if the same task tries to acquire the same
221 * lock instance multiple times), but multiple locks of the
222 * same lock class might be taken, if the order of the locks
223 * is always the same. This ordering rule can be expressed
224 * to lockdep via the _nested() APIs, but enumerating the
225 * subclasses that are used. (If the nesting relationship is
226 * static then another method for expressing nested locking is
227 * the explicit definition of lock class keys and the use of
228 * lockdep_set_class() at lock initialization time.
229 * See Documentation/locking/lockdep-design.rst for more details.)
231 extern void down_read_nested(struct rw_semaphore *sem, int subclass);
232 extern int __must_check down_read_killable_nested(struct rw_semaphore *sem, int subclass);
233 extern void down_write_nested(struct rw_semaphore *sem, int subclass);
234 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
235 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
237 # define down_write_nest_lock(sem, nest_lock) \
239 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
240 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
244 * Take/release a lock when not the owner will release it.
246 * [ This API should be avoided as much as possible - the
247 * proper abstraction for this case is completions. ]
249 extern void down_read_non_owner(struct rw_semaphore *sem);
250 extern void up_read_non_owner(struct rw_semaphore *sem);
252 # define down_read_nested(sem, subclass) down_read(sem)
253 # define down_read_killable_nested(sem, subclass) down_read_killable(sem)
254 # define down_write_nest_lock(sem, nest_lock) down_write(sem)
255 # define down_write_nested(sem, subclass) down_write(sem)
256 # define down_write_killable_nested(sem, subclass) down_write_killable(sem)
257 # define down_read_non_owner(sem) down_read(sem)
258 # define up_read_non_owner(sem) up_read(sem)
261 #endif /* _LINUX_RWSEM_H */