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>
19 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
20 #include <linux/osq_lock.h>
25 #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
26 #include <linux/rwsem-spinlock.h> /* use a generic implementation */
27 #define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE
29 /* All arch specific implementations share the same struct */
32 struct list_head wait_list;
33 raw_spinlock_t wait_lock;
34 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
35 struct optimistic_spin_queue osq; /* spinner MCS lock */
37 * Write owner. Used as a speculative check to see
38 * if the owner is running on the cpu.
40 struct task_struct *owner;
42 #ifdef CONFIG_DEBUG_LOCK_ALLOC
43 struct lockdep_map dep_map;
48 * Setting bit 0 of the owner field with other non-zero bits will indicate
49 * that the rwsem is writer-owned with an unknown owner.
51 #define RWSEM_OWNER_UNKNOWN ((struct task_struct *)-1L)
53 extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
54 extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem);
55 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
56 extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
57 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
58 extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
60 /* Include the arch specific part */
61 #include <asm/rwsem.h>
63 /* In all implementations count != 0 means locked */
64 static inline int rwsem_is_locked(struct rw_semaphore *sem)
66 return atomic_long_read(&sem->count) != 0;
69 #define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
72 /* Common initializer macros and functions */
74 #ifdef CONFIG_DEBUG_LOCK_ALLOC
75 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
77 # define __RWSEM_DEP_MAP_INIT(lockname)
80 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
81 #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
83 #define __RWSEM_OPT_INIT(lockname)
86 #define __RWSEM_INITIALIZER(name) \
87 { __RWSEM_INIT_COUNT(name), \
88 .wait_list = LIST_HEAD_INIT((name).wait_list), \
89 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
90 __RWSEM_OPT_INIT(name) \
91 __RWSEM_DEP_MAP_INIT(name) }
93 #define DECLARE_RWSEM(name) \
94 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
96 extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
97 struct lock_class_key *key);
99 #define init_rwsem(sem) \
101 static struct lock_class_key __key; \
103 __init_rwsem((sem), #sem, &__key); \
107 * This is the same regardless of which rwsem implementation that is being used.
108 * It is just a heuristic meant to be called by somebody alreadying holding the
109 * rwsem to see if somebody from an incompatible type is wanting access to the
112 static inline int rwsem_is_contended(struct rw_semaphore *sem)
114 return !list_empty(&sem->wait_list);
120 extern void down_read(struct rw_semaphore *sem);
121 extern int __must_check down_read_killable(struct rw_semaphore *sem);
124 * trylock for reading -- returns 1 if successful, 0 if contention
126 extern int down_read_trylock(struct rw_semaphore *sem);
131 extern void down_write(struct rw_semaphore *sem);
132 extern int __must_check down_write_killable(struct rw_semaphore *sem);
135 * trylock for writing -- returns 1 if successful, 0 if contention
137 extern int down_write_trylock(struct rw_semaphore *sem);
140 * release a read lock
142 extern void up_read(struct rw_semaphore *sem);
145 * release a write lock
147 extern void up_write(struct rw_semaphore *sem);
150 * downgrade write lock to read lock
152 extern void downgrade_write(struct rw_semaphore *sem);
154 #ifdef CONFIG_DEBUG_LOCK_ALLOC
156 * nested locking. NOTE: rwsems are not allowed to recurse
157 * (which occurs if the same task tries to acquire the same
158 * lock instance multiple times), but multiple locks of the
159 * same lock class might be taken, if the order of the locks
160 * is always the same. This ordering rule can be expressed
161 * to lockdep via the _nested() APIs, but enumerating the
162 * subclasses that are used. (If the nesting relationship is
163 * static then another method for expressing nested locking is
164 * the explicit definition of lock class keys and the use of
165 * lockdep_set_class() at lock initialization time.
166 * See Documentation/locking/lockdep-design.txt for more details.)
168 extern void down_read_nested(struct rw_semaphore *sem, int subclass);
169 extern void down_write_nested(struct rw_semaphore *sem, int subclass);
170 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
171 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
173 # define down_write_nest_lock(sem, nest_lock) \
175 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
176 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
180 * Take/release a lock when not the owner will release it.
182 * [ This API should be avoided as much as possible - the
183 * proper abstraction for this case is completions. ]
185 extern void down_read_non_owner(struct rw_semaphore *sem);
186 extern void up_read_non_owner(struct rw_semaphore *sem);
188 # define down_read_nested(sem, subclass) down_read(sem)
189 # define down_write_nest_lock(sem, nest_lock) down_write(sem)
190 # define down_write_nested(sem, subclass) down_write(sem)
191 # define down_write_killable_nested(sem, subclass) down_write_killable(sem)
192 # define down_read_non_owner(sem) down_read(sem)
193 # define up_read_non_owner(sem) up_read(sem)
196 #endif /* _LINUX_RWSEM_H */