]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
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 | ||
1da177e4 LT |
13 | #include <linux/types.h> |
14 | #include <linux/kernel.h> | |
c16a87ce TG |
15 | #include <linux/list.h> |
16 | #include <linux/spinlock.h> | |
60063497 | 17 | #include <linux/atomic.h> |
d4799608 | 18 | #include <linux/err.h> |
5db6c6fe | 19 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
90631822 | 20 | #include <linux/osq_lock.h> |
5db6c6fe | 21 | #endif |
1da177e4 | 22 | |
364f784f WL |
23 | /* |
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. | |
27 | * | |
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. | |
34 | */ | |
1c8ed640 | 35 | struct rw_semaphore { |
8ee62b18 | 36 | atomic_long_t count; |
4fc828e2 | 37 | /* |
94a9717b WL |
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. | |
4fc828e2 | 41 | */ |
94a9717b | 42 | atomic_long_t owner; |
c71fd893 | 43 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
364f784f | 44 | struct optimistic_spin_queue osq; /* spinner MCS lock */ |
4fc828e2 | 45 | #endif |
364f784f WL |
46 | raw_spinlock_t wait_lock; |
47 | struct list_head wait_list; | |
fce45cd4 DB |
48 | #ifdef CONFIG_DEBUG_RWSEMS |
49 | void *magic; | |
50 | #endif | |
1c8ed640 TG |
51 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
52 | struct lockdep_map dep_map; | |
53 | #endif | |
54 | }; | |
55 | ||
41e5887f TG |
56 | /* In all implementations count != 0 means locked */ |
57 | static inline int rwsem_is_locked(struct rw_semaphore *sem) | |
58 | { | |
8ee62b18 | 59 | return atomic_long_read(&sem->count) != 0; |
41e5887f TG |
60 | } |
61 | ||
46ad0840 | 62 | #define RWSEM_UNLOCKED_VALUE 0L |
a9232dc5 | 63 | #define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) |
1da177e4 | 64 | |
12249b34 TG |
65 | /* Common initializer macros and functions */ |
66 | ||
67 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
de8f5e4f | 68 | # define __RWSEM_DEP_MAP_INIT(lockname) \ |
a9232dc5 | 69 | .dep_map = { \ |
de8f5e4f PZ |
70 | .name = #lockname, \ |
71 | .wait_type_inner = LD_WAIT_SLEEP, \ | |
a9232dc5 | 72 | }, |
12249b34 TG |
73 | #else |
74 | # define __RWSEM_DEP_MAP_INIT(lockname) | |
75 | #endif | |
76 | ||
fce45cd4 | 77 | #ifdef CONFIG_DEBUG_RWSEMS |
a9232dc5 | 78 | # define __RWSEM_DEBUG_INIT(lockname) .magic = &lockname, |
fce45cd4 | 79 | #else |
a9232dc5 | 80 | # define __RWSEM_DEBUG_INIT(lockname) |
fce45cd4 DB |
81 | #endif |
82 | ||
5db6c6fe | 83 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
a9232dc5 | 84 | #define __RWSEM_OPT_INIT(lockname) .osq = OSQ_LOCK_UNLOCKED, |
4fc828e2 | 85 | #else |
ce069fc9 | 86 | #define __RWSEM_OPT_INIT(lockname) |
4fc828e2 | 87 | #endif |
12249b34 | 88 | |
ce069fc9 | 89 | #define __RWSEM_INITIALIZER(name) \ |
a9232dc5 | 90 | { __RWSEM_COUNT_INIT(name), \ |
94a9717b | 91 | .owner = ATOMIC_LONG_INIT(0), \ |
ce069fc9 | 92 | __RWSEM_OPT_INIT(name) \ |
a9232dc5 AD |
93 | .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\ |
94 | .wait_list = LIST_HEAD_INIT((name).wait_list), \ | |
95 | __RWSEM_DEBUG_INIT(name) \ | |
ce069fc9 JL |
96 | __RWSEM_DEP_MAP_INIT(name) } |
97 | ||
12249b34 TG |
98 | #define DECLARE_RWSEM(name) \ |
99 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) | |
100 | ||
101 | extern void __init_rwsem(struct rw_semaphore *sem, const char *name, | |
102 | struct lock_class_key *key); | |
103 | ||
104 | #define init_rwsem(sem) \ | |
105 | do { \ | |
106 | static struct lock_class_key __key; \ | |
107 | \ | |
108 | __init_rwsem((sem), #sem, &__key); \ | |
109 | } while (0) | |
110 | ||
4a444b1f JB |
111 | /* |
112 | * This is the same regardless of which rwsem implementation that is being used. | |
e2db7592 | 113 | * It is just a heuristic meant to be called by somebody already holding the |
4a444b1f JB |
114 | * rwsem to see if somebody from an incompatible type is wanting access to the |
115 | * lock. | |
116 | */ | |
117 | static inline int rwsem_is_contended(struct rw_semaphore *sem) | |
118 | { | |
119 | return !list_empty(&sem->wait_list); | |
120 | } | |
121 | ||
1da177e4 LT |
122 | /* |
123 | * lock for reading | |
124 | */ | |
4ea2176d | 125 | extern void down_read(struct rw_semaphore *sem); |
31784cff | 126 | extern int __must_check down_read_interruptible(struct rw_semaphore *sem); |
76f8507f | 127 | extern int __must_check down_read_killable(struct rw_semaphore *sem); |
1da177e4 LT |
128 | |
129 | /* | |
130 | * trylock for reading -- returns 1 if successful, 0 if contention | |
131 | */ | |
4ea2176d | 132 | extern int down_read_trylock(struct rw_semaphore *sem); |
1da177e4 LT |
133 | |
134 | /* | |
135 | * lock for writing | |
136 | */ | |
4ea2176d | 137 | extern void down_write(struct rw_semaphore *sem); |
916633a4 | 138 | extern int __must_check down_write_killable(struct rw_semaphore *sem); |
1da177e4 LT |
139 | |
140 | /* | |
141 | * trylock for writing -- returns 1 if successful, 0 if contention | |
142 | */ | |
4ea2176d | 143 | extern int down_write_trylock(struct rw_semaphore *sem); |
1da177e4 LT |
144 | |
145 | /* | |
146 | * release a read lock | |
147 | */ | |
4ea2176d | 148 | extern void up_read(struct rw_semaphore *sem); |
1da177e4 LT |
149 | |
150 | /* | |
151 | * release a write lock | |
152 | */ | |
4ea2176d | 153 | extern void up_write(struct rw_semaphore *sem); |
1da177e4 LT |
154 | |
155 | /* | |
156 | * downgrade write lock to read lock | |
157 | */ | |
4ea2176d IM |
158 | extern void downgrade_write(struct rw_semaphore *sem); |
159 | ||
160 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
161 | /* | |
5fca80e8 IM |
162 | * nested locking. NOTE: rwsems are not allowed to recurse |
163 | * (which occurs if the same task tries to acquire the same | |
164 | * lock instance multiple times), but multiple locks of the | |
165 | * same lock class might be taken, if the order of the locks | |
166 | * is always the same. This ordering rule can be expressed | |
167 | * to lockdep via the _nested() APIs, but enumerating the | |
168 | * subclasses that are used. (If the nesting relationship is | |
169 | * static then another method for expressing nested locking is | |
170 | * the explicit definition of lock class keys and the use of | |
171 | * lockdep_set_class() at lock initialization time. | |
387b1468 | 172 | * See Documentation/locking/lockdep-design.rst for more details.) |
4ea2176d IM |
173 | */ |
174 | extern void down_read_nested(struct rw_semaphore *sem, int subclass); | |
0f9368b5 | 175 | extern int __must_check down_read_killable_nested(struct rw_semaphore *sem, int subclass); |
4ea2176d | 176 | extern void down_write_nested(struct rw_semaphore *sem, int subclass); |
887bddfa | 177 | extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass); |
1b963c81 JK |
178 | extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock); |
179 | ||
180 | # define down_write_nest_lock(sem, nest_lock) \ | |
181 | do { \ | |
182 | typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \ | |
183 | _down_write_nest_lock(sem, &(nest_lock)->dep_map); \ | |
184 | } while (0); | |
185 | ||
84759c6d KO |
186 | /* |
187 | * Take/release a lock when not the owner will release it. | |
188 | * | |
189 | * [ This API should be avoided as much as possible - the | |
190 | * proper abstraction for this case is completions. ] | |
191 | */ | |
192 | extern void down_read_non_owner(struct rw_semaphore *sem); | |
193 | extern void up_read_non_owner(struct rw_semaphore *sem); | |
4ea2176d IM |
194 | #else |
195 | # define down_read_nested(sem, subclass) down_read(sem) | |
0f9368b5 | 196 | # define down_read_killable_nested(sem, subclass) down_read_killable(sem) |
e65b9ad2 | 197 | # define down_write_nest_lock(sem, nest_lock) down_write(sem) |
4ea2176d | 198 | # define down_write_nested(sem, subclass) down_write(sem) |
887bddfa | 199 | # define down_write_killable_nested(sem, subclass) down_write_killable(sem) |
84759c6d KO |
200 | # define down_read_non_owner(sem) down_read(sem) |
201 | # define up_read_non_owner(sem) up_read(sem) | |
4ea2176d | 202 | #endif |
1da177e4 | 203 | |
1da177e4 | 204 | #endif /* _LINUX_RWSEM_H */ |