1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* rwsem.h: R/W semaphores, public interface
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>
24 * For an uncontended rwsem, count and owner are the only fields a task
25 * needs to touch when acquiring the rwsem. So they are put next to each
26 * other to increase the chance that they will share the same cacheline.
28 * In a contended rwsem, the owner is likely the most frequently accessed
29 * field in the structure as the optimistic waiter that holds the osq lock
30 * will spin on owner. For an embedded rwsem, other hot fields in the
31 * containing structure should be moved further away from the rwsem to
32 * reduce the chance that they will share the same cacheline causing
33 * cacheline bouncing problem.
38 * Write owner or one of the read owners as well flags regarding
39 * the current state of the rwsem. Can be used as a speculative
40 * check to see if the write owner is running on the cpu.
43 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
44 struct optimistic_spin_queue osq; /* spinner MCS lock */
46 raw_spinlock_t wait_lock;
47 struct list_head wait_list;
48 #ifdef CONFIG_DEBUG_LOCK_ALLOC
49 struct lockdep_map dep_map;
54 * Setting all bits of the owner field except bit 0 will indicate
55 * that the rwsem is writer-owned with an unknown owner.
57 #define RWSEM_OWNER_UNKNOWN (-2L)
59 /* In all implementations count != 0 means locked */
60 static inline int rwsem_is_locked(struct rw_semaphore *sem)
62 return atomic_long_read(&sem->count) != 0;
65 #define RWSEM_UNLOCKED_VALUE 0L
66 #define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
68 /* Common initializer macros and functions */
70 #ifdef CONFIG_DEBUG_LOCK_ALLOC
71 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
73 # define __RWSEM_DEP_MAP_INIT(lockname)
76 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
77 #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED
79 #define __RWSEM_OPT_INIT(lockname)
82 #define __RWSEM_INITIALIZER(name) \
83 { __RWSEM_INIT_COUNT(name), \
84 .owner = ATOMIC_LONG_INIT(0), \
85 .wait_list = LIST_HEAD_INIT((name).wait_list), \
86 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
87 __RWSEM_OPT_INIT(name) \
88 __RWSEM_DEP_MAP_INIT(name) }
90 #define DECLARE_RWSEM(name) \
91 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
93 extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
94 struct lock_class_key *key);
96 #define init_rwsem(sem) \
98 static struct lock_class_key __key; \
100 __init_rwsem((sem), #sem, &__key); \
104 * This is the same regardless of which rwsem implementation that is being used.
105 * It is just a heuristic meant to be called by somebody alreadying holding the
106 * rwsem to see if somebody from an incompatible type is wanting access to the
109 static inline int rwsem_is_contended(struct rw_semaphore *sem)
111 return !list_empty(&sem->wait_list);
117 extern void down_read(struct rw_semaphore *sem);
118 extern int __must_check down_read_killable(struct rw_semaphore *sem);
121 * trylock for reading -- returns 1 if successful, 0 if contention
123 extern int down_read_trylock(struct rw_semaphore *sem);
128 extern void down_write(struct rw_semaphore *sem);
129 extern int __must_check down_write_killable(struct rw_semaphore *sem);
132 * trylock for writing -- returns 1 if successful, 0 if contention
134 extern int down_write_trylock(struct rw_semaphore *sem);
137 * release a read lock
139 extern void up_read(struct rw_semaphore *sem);
142 * release a write lock
144 extern void up_write(struct rw_semaphore *sem);
147 * downgrade write lock to read lock
149 extern void downgrade_write(struct rw_semaphore *sem);
151 #ifdef CONFIG_DEBUG_LOCK_ALLOC
153 * nested locking. NOTE: rwsems are not allowed to recurse
154 * (which occurs if the same task tries to acquire the same
155 * lock instance multiple times), but multiple locks of the
156 * same lock class might be taken, if the order of the locks
157 * is always the same. This ordering rule can be expressed
158 * to lockdep via the _nested() APIs, but enumerating the
159 * subclasses that are used. (If the nesting relationship is
160 * static then another method for expressing nested locking is
161 * the explicit definition of lock class keys and the use of
162 * lockdep_set_class() at lock initialization time.
163 * See Documentation/locking/lockdep-design.txt for more details.)
165 extern void down_read_nested(struct rw_semaphore *sem, int subclass);
166 extern void down_write_nested(struct rw_semaphore *sem, int subclass);
167 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
168 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
170 # define down_write_nest_lock(sem, nest_lock) \
172 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
173 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
177 * Take/release a lock when not the owner will release it.
179 * [ This API should be avoided as much as possible - the
180 * proper abstraction for this case is completions. ]
182 extern void down_read_non_owner(struct rw_semaphore *sem);
183 extern void up_read_non_owner(struct rw_semaphore *sem);
185 # define down_read_nested(sem, subclass) down_read(sem)
186 # define down_write_nest_lock(sem, nest_lock) down_write(sem)
187 # define down_write_nested(sem, subclass) down_write(sem)
188 # define down_write_killable_nested(sem, subclass) down_write_killable(sem)
189 # define down_read_non_owner(sem) down_read(sem)
190 # define up_read_non_owner(sem) up_read(sem)
193 #endif /* _LINUX_RWSEM_H */