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