]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* rwsem-spinlock.c: R/W semaphores: contention handling functions for |
2 | * generic spinlock implementation | |
3 | * | |
4 | * Copyright (c) 2001 David Howells (dhowells@redhat.com). | |
5 | * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de> | |
6 | * - Derived also from comments by Linus | |
7 | */ | |
8 | #include <linux/rwsem.h> | |
174cd4b1 | 9 | #include <linux/sched/signal.h> |
b17b0153 | 10 | #include <linux/sched/debug.h> |
8bc3bcc9 | 11 | #include <linux/export.h> |
1da177e4 | 12 | |
e2d57f78 ML |
13 | enum rwsem_waiter_type { |
14 | RWSEM_WAITING_FOR_WRITE, | |
15 | RWSEM_WAITING_FOR_READ | |
16 | }; | |
17 | ||
1da177e4 LT |
18 | struct rwsem_waiter { |
19 | struct list_head list; | |
20 | struct task_struct *task; | |
e2d57f78 | 21 | enum rwsem_waiter_type type; |
1da177e4 LT |
22 | }; |
23 | ||
29671f22 AW |
24 | int rwsem_is_locked(struct rw_semaphore *sem) |
25 | { | |
26 | int ret = 1; | |
27 | unsigned long flags; | |
28 | ||
ddb6c9b5 | 29 | if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) { |
13b9a962 | 30 | ret = (sem->count != 0); |
ddb6c9b5 | 31 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
29671f22 AW |
32 | } |
33 | return ret; | |
34 | } | |
35 | EXPORT_SYMBOL(rwsem_is_locked); | |
36 | ||
1da177e4 LT |
37 | /* |
38 | * initialise the semaphore | |
39 | */ | |
4ea2176d IM |
40 | void __init_rwsem(struct rw_semaphore *sem, const char *name, |
41 | struct lock_class_key *key) | |
1da177e4 | 42 | { |
4ea2176d IM |
43 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
44 | /* | |
45 | * Make sure we are not reinitializing a held semaphore: | |
46 | */ | |
47 | debug_check_no_locks_freed((void *)sem, sizeof(*sem)); | |
4dfbb9d8 | 48 | lockdep_init_map(&sem->dep_map, name, key, 0); |
4ea2176d | 49 | #endif |
13b9a962 | 50 | sem->count = 0; |
ddb6c9b5 | 51 | raw_spin_lock_init(&sem->wait_lock); |
1da177e4 | 52 | INIT_LIST_HEAD(&sem->wait_list); |
1da177e4 | 53 | } |
118d52da | 54 | EXPORT_SYMBOL(__init_rwsem); |
1da177e4 LT |
55 | |
56 | /* | |
57 | * handle the lock release when processes blocked on it that can now run | |
58 | * - if we come here, then: | |
59 | * - the 'active count' _reached_ zero | |
60 | * - the 'waiting count' is non-zero | |
61 | * - the spinlock must be held by the caller | |
62 | * - woken process blocks are discarded from the list after having task zeroed | |
63 | * - writers are only woken if wakewrite is non-zero | |
64 | */ | |
65 | static inline struct rw_semaphore * | |
66 | __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite) | |
67 | { | |
68 | struct rwsem_waiter *waiter; | |
69 | struct task_struct *tsk; | |
70 | int woken; | |
71 | ||
1da177e4 LT |
72 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); |
73 | ||
e2d57f78 | 74 | if (waiter->type == RWSEM_WAITING_FOR_WRITE) { |
8cf5322c ML |
75 | if (wakewrite) |
76 | /* Wake up a writer. Note that we do not grant it the | |
77 | * lock - it will have to acquire it when it runs. */ | |
78 | wake_up_process(waiter->task); | |
1da177e4 LT |
79 | goto out; |
80 | } | |
81 | ||
82 | /* grant an infinite number of read locks to the front of the queue */ | |
1da177e4 | 83 | woken = 0; |
8cf5322c | 84 | do { |
1da177e4 LT |
85 | struct list_head *next = waiter->list.next; |
86 | ||
87 | list_del(&waiter->list); | |
88 | tsk = waiter->task; | |
49e4b2bc DB |
89 | /* |
90 | * Make sure we do not wakeup the next reader before | |
91 | * setting the nil condition to grant the next reader; | |
92 | * otherwise we could miss the wakeup on the other | |
93 | * side and end up sleeping again. See the pairing | |
94 | * in rwsem_down_read_failed(). | |
95 | */ | |
d59dd462 | 96 | smp_mb(); |
1da177e4 LT |
97 | waiter->task = NULL; |
98 | wake_up_process(tsk); | |
99 | put_task_struct(tsk); | |
100 | woken++; | |
8cf5322c | 101 | if (next == &sem->wait_list) |
1da177e4 LT |
102 | break; |
103 | waiter = list_entry(next, struct rwsem_waiter, list); | |
8cf5322c | 104 | } while (waiter->type != RWSEM_WAITING_FOR_WRITE); |
1da177e4 | 105 | |
13b9a962 | 106 | sem->count += woken; |
1da177e4 LT |
107 | |
108 | out: | |
1da177e4 LT |
109 | return sem; |
110 | } | |
111 | ||
112 | /* | |
113 | * wake a single writer | |
114 | */ | |
115 | static inline struct rw_semaphore * | |
116 | __rwsem_wake_one_writer(struct rw_semaphore *sem) | |
117 | { | |
118 | struct rwsem_waiter *waiter; | |
1da177e4 LT |
119 | |
120 | waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list); | |
41ef8f82 | 121 | wake_up_process(waiter->task); |
1da177e4 | 122 | |
1da177e4 LT |
123 | return sem; |
124 | } | |
125 | ||
126 | /* | |
127 | * get a read lock on the semaphore | |
128 | */ | |
9f741cb8 | 129 | void __sched __down_read(struct rw_semaphore *sem) |
1da177e4 LT |
130 | { |
131 | struct rwsem_waiter waiter; | |
3eac4aba | 132 | unsigned long flags; |
1da177e4 | 133 | |
ddb6c9b5 | 134 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 135 | |
13b9a962 | 136 | if (sem->count >= 0 && list_empty(&sem->wait_list)) { |
1da177e4 | 137 | /* granted */ |
13b9a962 | 138 | sem->count++; |
ddb6c9b5 | 139 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 LT |
140 | goto out; |
141 | } | |
142 | ||
642fa448 | 143 | set_current_state(TASK_UNINTERRUPTIBLE); |
1da177e4 LT |
144 | |
145 | /* set up my own style of waitqueue */ | |
d269a8b8 | 146 | waiter.task = current; |
e2d57f78 | 147 | waiter.type = RWSEM_WAITING_FOR_READ; |
d269a8b8 | 148 | get_task_struct(current); |
1da177e4 LT |
149 | |
150 | list_add_tail(&waiter.list, &sem->wait_list); | |
151 | ||
152 | /* we don't need to touch the semaphore struct anymore */ | |
ddb6c9b5 | 153 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 LT |
154 | |
155 | /* wait to be given the lock */ | |
156 | for (;;) { | |
157 | if (!waiter.task) | |
158 | break; | |
159 | schedule(); | |
642fa448 | 160 | set_current_state(TASK_UNINTERRUPTIBLE); |
1da177e4 LT |
161 | } |
162 | ||
642fa448 | 163 | __set_current_state(TASK_RUNNING); |
1da177e4 | 164 | out: |
c4e05116 | 165 | ; |
1da177e4 LT |
166 | } |
167 | ||
168 | /* | |
169 | * trylock for reading -- returns 1 if successful, 0 if contention | |
170 | */ | |
9f741cb8 | 171 | int __down_read_trylock(struct rw_semaphore *sem) |
1da177e4 LT |
172 | { |
173 | unsigned long flags; | |
174 | int ret = 0; | |
175 | ||
1da177e4 | 176 | |
ddb6c9b5 | 177 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 178 | |
13b9a962 | 179 | if (sem->count >= 0 && list_empty(&sem->wait_list)) { |
1da177e4 | 180 | /* granted */ |
13b9a962 | 181 | sem->count++; |
1da177e4 LT |
182 | ret = 1; |
183 | } | |
184 | ||
ddb6c9b5 | 185 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 | 186 | |
1da177e4 LT |
187 | return ret; |
188 | } | |
189 | ||
190 | /* | |
191 | * get a write lock on the semaphore | |
1da177e4 | 192 | */ |
d4799608 | 193 | int __sched __down_write_common(struct rw_semaphore *sem, int state) |
1da177e4 LT |
194 | { |
195 | struct rwsem_waiter waiter; | |
3eac4aba | 196 | unsigned long flags; |
d4799608 | 197 | int ret = 0; |
1da177e4 | 198 | |
ddb6c9b5 | 199 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 200 | |
1da177e4 | 201 | /* set up my own style of waitqueue */ |
d269a8b8 | 202 | waiter.task = current; |
e2d57f78 | 203 | waiter.type = RWSEM_WAITING_FOR_WRITE; |
1da177e4 LT |
204 | list_add_tail(&waiter.list, &sem->wait_list); |
205 | ||
41ef8f82 | 206 | /* wait for someone to release the lock */ |
1da177e4 | 207 | for (;;) { |
41ef8f82 YL |
208 | /* |
209 | * That is the key to support write lock stealing: allows the | |
210 | * task already on CPU to get the lock soon rather than put | |
211 | * itself into sleep and waiting for system woke it or someone | |
212 | * else in the head of the wait list up. | |
213 | */ | |
13b9a962 | 214 | if (sem->count == 0) |
1da177e4 | 215 | break; |
d4799608 MH |
216 | if (signal_pending_state(state, current)) { |
217 | ret = -EINTR; | |
218 | goto out; | |
219 | } | |
642fa448 | 220 | set_current_state(state); |
41ef8f82 YL |
221 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
222 | schedule(); | |
223 | raw_spin_lock_irqsave(&sem->wait_lock, flags); | |
1da177e4 | 224 | } |
41ef8f82 | 225 | /* got the lock */ |
13b9a962 | 226 | sem->count = -1; |
d4799608 | 227 | out: |
41ef8f82 | 228 | list_del(&waiter.list); |
1da177e4 | 229 | |
41ef8f82 | 230 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
d4799608 MH |
231 | |
232 | return ret; | |
233 | } | |
234 | ||
235 | void __sched __down_write(struct rw_semaphore *sem) | |
236 | { | |
237 | __down_write_common(sem, TASK_UNINTERRUPTIBLE); | |
238 | } | |
239 | ||
240 | int __sched __down_write_killable(struct rw_semaphore *sem) | |
241 | { | |
242 | return __down_write_common(sem, TASK_KILLABLE); | |
1da177e4 LT |
243 | } |
244 | ||
245 | /* | |
246 | * trylock for writing -- returns 1 if successful, 0 if contention | |
247 | */ | |
9f741cb8 | 248 | int __down_write_trylock(struct rw_semaphore *sem) |
1da177e4 LT |
249 | { |
250 | unsigned long flags; | |
251 | int ret = 0; | |
252 | ||
ddb6c9b5 | 253 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 254 | |
13b9a962 | 255 | if (sem->count == 0) { |
41ef8f82 | 256 | /* got the lock */ |
13b9a962 | 257 | sem->count = -1; |
1da177e4 LT |
258 | ret = 1; |
259 | } | |
260 | ||
ddb6c9b5 | 261 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 | 262 | |
1da177e4 LT |
263 | return ret; |
264 | } | |
265 | ||
266 | /* | |
267 | * release a read lock on the semaphore | |
268 | */ | |
9f741cb8 | 269 | void __up_read(struct rw_semaphore *sem) |
1da177e4 LT |
270 | { |
271 | unsigned long flags; | |
272 | ||
ddb6c9b5 | 273 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 274 | |
13b9a962 | 275 | if (--sem->count == 0 && !list_empty(&sem->wait_list)) |
1da177e4 LT |
276 | sem = __rwsem_wake_one_writer(sem); |
277 | ||
ddb6c9b5 | 278 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 LT |
279 | } |
280 | ||
281 | /* | |
282 | * release a write lock on the semaphore | |
283 | */ | |
9f741cb8 | 284 | void __up_write(struct rw_semaphore *sem) |
1da177e4 LT |
285 | { |
286 | unsigned long flags; | |
287 | ||
ddb6c9b5 | 288 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 289 | |
13b9a962 | 290 | sem->count = 0; |
1da177e4 LT |
291 | if (!list_empty(&sem->wait_list)) |
292 | sem = __rwsem_do_wake(sem, 1); | |
293 | ||
ddb6c9b5 | 294 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 LT |
295 | } |
296 | ||
297 | /* | |
298 | * downgrade a write lock into a read lock | |
299 | * - just wake up any readers at the front of the queue | |
300 | */ | |
9f741cb8 | 301 | void __downgrade_write(struct rw_semaphore *sem) |
1da177e4 LT |
302 | { |
303 | unsigned long flags; | |
304 | ||
ddb6c9b5 | 305 | raw_spin_lock_irqsave(&sem->wait_lock, flags); |
1da177e4 | 306 | |
13b9a962 | 307 | sem->count = 1; |
1da177e4 LT |
308 | if (!list_empty(&sem->wait_list)) |
309 | sem = __rwsem_do_wake(sem, 0); | |
310 | ||
ddb6c9b5 | 311 | raw_spin_unlock_irqrestore(&sem->wait_lock, flags); |
1da177e4 LT |
312 | } |
313 |