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 LT |
22 | |
23 | struct rw_semaphore; | |
24 | ||
25 | #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK | |
26 | #include <linux/rwsem-spinlock.h> /* use a generic implementation */ | |
8ee62b18 | 27 | #define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE |
1da177e4 | 28 | #else |
1c8ed640 TG |
29 | /* All arch specific implementations share the same struct */ |
30 | struct rw_semaphore { | |
8ee62b18 | 31 | atomic_long_t count; |
4fc828e2 | 32 | struct list_head wait_list; |
ce069fc9 | 33 | raw_spinlock_t wait_lock; |
5db6c6fe | 34 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
ce069fc9 | 35 | struct optimistic_spin_queue osq; /* spinner MCS lock */ |
4fc828e2 DB |
36 | /* |
37 | * Write owner. Used as a speculative check to see | |
38 | * if the owner is running on the cpu. | |
39 | */ | |
40 | struct task_struct *owner; | |
4fc828e2 | 41 | #endif |
1c8ed640 TG |
42 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
43 | struct lockdep_map dep_map; | |
44 | #endif | |
45 | }; | |
46 | ||
77a60e75 WL |
47 | /* |
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. | |
50 | */ | |
51 | #define RWSEM_OWNER_UNKNOWN ((struct task_struct *)-1L) | |
52 | ||
d1233754 | 53 | extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem); |
83ced169 | 54 | extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem); |
d1233754 | 55 | extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem); |
d4799608 | 56 | extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem); |
d1233754 TG |
57 | extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *); |
58 | extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem); | |
aac72277 | 59 | |
1c8ed640 TG |
60 | /* Include the arch specific part */ |
61 | #include <asm/rwsem.h> | |
41e5887f TG |
62 | |
63 | /* In all implementations count != 0 means locked */ | |
64 | static inline int rwsem_is_locked(struct rw_semaphore *sem) | |
65 | { | |
8ee62b18 | 66 | return atomic_long_read(&sem->count) != 0; |
41e5887f TG |
67 | } |
68 | ||
8ee62b18 | 69 | #define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) |
1da177e4 LT |
70 | #endif |
71 | ||
12249b34 TG |
72 | /* Common initializer macros and functions */ |
73 | ||
74 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
75 | # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname } | |
76 | #else | |
77 | # define __RWSEM_DEP_MAP_INIT(lockname) | |
78 | #endif | |
79 | ||
5db6c6fe | 80 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
ce069fc9 | 81 | #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL |
4fc828e2 | 82 | #else |
ce069fc9 | 83 | #define __RWSEM_OPT_INIT(lockname) |
4fc828e2 | 84 | #endif |
12249b34 | 85 | |
ce069fc9 | 86 | #define __RWSEM_INITIALIZER(name) \ |
8ee62b18 | 87 | { __RWSEM_INIT_COUNT(name), \ |
ce069fc9 JL |
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) } | |
92 | ||
12249b34 TG |
93 | #define DECLARE_RWSEM(name) \ |
94 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) | |
95 | ||
96 | extern void __init_rwsem(struct rw_semaphore *sem, const char *name, | |
97 | struct lock_class_key *key); | |
98 | ||
99 | #define init_rwsem(sem) \ | |
100 | do { \ | |
101 | static struct lock_class_key __key; \ | |
102 | \ | |
103 | __init_rwsem((sem), #sem, &__key); \ | |
104 | } while (0) | |
105 | ||
4a444b1f JB |
106 | /* |
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 | |
110 | * lock. | |
111 | */ | |
112 | static inline int rwsem_is_contended(struct rw_semaphore *sem) | |
113 | { | |
114 | return !list_empty(&sem->wait_list); | |
115 | } | |
116 | ||
1da177e4 LT |
117 | /* |
118 | * lock for reading | |
119 | */ | |
4ea2176d | 120 | extern void down_read(struct rw_semaphore *sem); |
1da177e4 LT |
121 | |
122 | /* | |
123 | * trylock for reading -- returns 1 if successful, 0 if contention | |
124 | */ | |
4ea2176d | 125 | extern int down_read_trylock(struct rw_semaphore *sem); |
1da177e4 LT |
126 | |
127 | /* | |
128 | * lock for writing | |
129 | */ | |
4ea2176d | 130 | extern void down_write(struct rw_semaphore *sem); |
916633a4 | 131 | extern int __must_check down_write_killable(struct rw_semaphore *sem); |
1da177e4 LT |
132 | |
133 | /* | |
134 | * trylock for writing -- returns 1 if successful, 0 if contention | |
135 | */ | |
4ea2176d | 136 | extern int down_write_trylock(struct rw_semaphore *sem); |
1da177e4 LT |
137 | |
138 | /* | |
139 | * release a read lock | |
140 | */ | |
4ea2176d | 141 | extern void up_read(struct rw_semaphore *sem); |
1da177e4 LT |
142 | |
143 | /* | |
144 | * release a write lock | |
145 | */ | |
4ea2176d | 146 | extern void up_write(struct rw_semaphore *sem); |
1da177e4 LT |
147 | |
148 | /* | |
149 | * downgrade write lock to read lock | |
150 | */ | |
4ea2176d IM |
151 | extern void downgrade_write(struct rw_semaphore *sem); |
152 | ||
153 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
154 | /* | |
5fca80e8 IM |
155 | * nested locking. NOTE: rwsems are not allowed to recurse |
156 | * (which occurs if the same task tries to acquire the same | |
157 | * lock instance multiple times), but multiple locks of the | |
158 | * same lock class might be taken, if the order of the locks | |
159 | * is always the same. This ordering rule can be expressed | |
160 | * to lockdep via the _nested() APIs, but enumerating the | |
161 | * subclasses that are used. (If the nesting relationship is | |
162 | * static then another method for expressing nested locking is | |
163 | * the explicit definition of lock class keys and the use of | |
164 | * lockdep_set_class() at lock initialization time. | |
214e0aed | 165 | * See Documentation/locking/lockdep-design.txt for more details.) |
4ea2176d IM |
166 | */ |
167 | extern void down_read_nested(struct rw_semaphore *sem, int subclass); | |
168 | extern void down_write_nested(struct rw_semaphore *sem, int subclass); | |
887bddfa | 169 | extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass); |
1b963c81 JK |
170 | extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock); |
171 | ||
172 | # define down_write_nest_lock(sem, nest_lock) \ | |
173 | do { \ | |
174 | typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \ | |
175 | _down_write_nest_lock(sem, &(nest_lock)->dep_map); \ | |
176 | } while (0); | |
177 | ||
84759c6d KO |
178 | /* |
179 | * Take/release a lock when not the owner will release it. | |
180 | * | |
181 | * [ This API should be avoided as much as possible - the | |
182 | * proper abstraction for this case is completions. ] | |
183 | */ | |
184 | extern void down_read_non_owner(struct rw_semaphore *sem); | |
185 | extern void up_read_non_owner(struct rw_semaphore *sem); | |
4ea2176d IM |
186 | #else |
187 | # define down_read_nested(sem, subclass) down_read(sem) | |
e65b9ad2 | 188 | # define down_write_nest_lock(sem, nest_lock) down_write(sem) |
4ea2176d | 189 | # define down_write_nested(sem, subclass) down_write(sem) |
887bddfa | 190 | # define down_write_killable_nested(sem, subclass) down_write_killable(sem) |
84759c6d KO |
191 | # define down_read_non_owner(sem) down_read(sem) |
192 | # define up_read_non_owner(sem) up_read(sem) | |
4ea2176d | 193 | #endif |
1da177e4 | 194 | |
1da177e4 | 195 | #endif /* _LINUX_RWSEM_H */ |