blob: 5f117f37ac0a8164ba98491421cc6073d38a4d3d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
9#include <linux/sched.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050010#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Michel Lespinassee2d57f72013-05-07 06:45:49 -070012enum rwsem_waiter_type {
13 RWSEM_WAITING_FOR_WRITE,
14 RWSEM_WAITING_FOR_READ
15};
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017struct rwsem_waiter {
18 struct list_head list;
19 struct task_struct *task;
Michel Lespinassee2d57f72013-05-07 06:45:49 -070020 enum rwsem_waiter_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021};
22
Amerigo Wang29671f22009-12-14 18:00:21 -080023int rwsem_is_locked(struct rw_semaphore *sem)
24{
25 int ret = 1;
26 unsigned long flags;
27
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010028 if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) {
Amerigo Wang29671f22009-12-14 18:00:21 -080029 ret = (sem->activity != 0);
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010030 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Amerigo Wang29671f22009-12-14 18:00:21 -080031 }
32 return ret;
33}
34EXPORT_SYMBOL(rwsem_is_locked);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * initialise the semaphore
38 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070039void __init_rwsem(struct rw_semaphore *sem, const char *name,
40 struct lock_class_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Ingo Molnar4ea21762006-07-03 00:24:53 -070042#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));
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -040047 lockdep_init_map(&sem->dep_map, name, key, 0);
Ingo Molnar4ea21762006-07-03 00:24:53 -070048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 sem->activity = 0;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010050 raw_spin_lock_init(&sem->wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 INIT_LIST_HEAD(&sem->wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Amerigo Wang118d52d2009-12-14 18:00:20 -080053EXPORT_SYMBOL(__init_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
72
73 if (!wakewrite) {
Michel Lespinassee2d57f72013-05-07 06:45:49 -070074 if (waiter->type == RWSEM_WAITING_FOR_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 goto out;
76 goto dont_wake_writers;
77 }
78
Yuanhan Liu41ef8f82013-02-01 18:59:16 +080079 /*
80 * as we support write lock stealing, we can't set sem->activity
81 * to -1 here to indicate we get the lock. Instead, we wake it up
82 * to let it go get it again.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
Michel Lespinassee2d57f72013-05-07 06:45:49 -070084 if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
Yuanhan Liu41ef8f82013-02-01 18:59:16 +080085 wake_up_process(waiter->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 goto out;
87 }
88
89 /* grant an infinite number of read locks to the front of the queue */
90 dont_wake_writers:
91 woken = 0;
Michel Lespinassee2d57f72013-05-07 06:45:49 -070092 while (waiter->type == RWSEM_WAITING_FOR_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct list_head *next = waiter->list.next;
94
95 list_del(&waiter->list);
96 tsk = waiter->task;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070097 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 waiter->task = NULL;
99 wake_up_process(tsk);
100 put_task_struct(tsk);
101 woken++;
102 if (list_empty(&sem->wait_list))
103 break;
104 waiter = list_entry(next, struct rwsem_waiter, list);
105 }
106
107 sem->activity += woken;
108
109 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return sem;
111}
112
113/*
114 * wake a single writer
115 */
116static inline struct rw_semaphore *
117__rwsem_wake_one_writer(struct rw_semaphore *sem)
118{
119 struct rwsem_waiter *waiter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800122 wake_up_process(waiter->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return sem;
125}
126
127/*
128 * get a read lock on the semaphore
129 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800130void __sched __down_read(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct rwsem_waiter waiter;
133 struct task_struct *tsk;
Kevin Hilman3eac4ab2010-04-07 11:52:46 -0700134 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100136 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
139 /* granted */
140 sem->activity++;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100141 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto out;
143 }
144
145 tsk = current;
146 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
147
148 /* set up my own style of waitqueue */
149 waiter.task = tsk;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700150 waiter.type = RWSEM_WAITING_FOR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 get_task_struct(tsk);
152
153 list_add_tail(&waiter.list, &sem->wait_list);
154
155 /* we don't need to touch the semaphore struct anymore */
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100156 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /* wait to be given the lock */
159 for (;;) {
160 if (!waiter.task)
161 break;
162 schedule();
163 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
164 }
165
166 tsk->state = TASK_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 out:
Ingo Molnarc4e05112006-07-03 00:24:29 -0700168 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/*
172 * trylock for reading -- returns 1 if successful, 0 if contention
173 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800174int __down_read_trylock(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 unsigned long flags;
177 int ret = 0;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100180 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
183 /* granted */
184 sem->activity++;
185 ret = 1;
186 }
187
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100188 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return ret;
191}
192
193/*
194 * get a write lock on the semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800196void __sched __down_write_nested(struct rw_semaphore *sem, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 struct rwsem_waiter waiter;
199 struct task_struct *tsk;
Kevin Hilman3eac4ab2010-04-07 11:52:46 -0700200 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100202 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* set up my own style of waitqueue */
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800205 tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 waiter.task = tsk;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700207 waiter.type = RWSEM_WAITING_FOR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 list_add_tail(&waiter.list, &sem->wait_list);
209
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800210 /* wait for someone to release the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 for (;;) {
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800212 /*
213 * That is the key to support write lock stealing: allows the
214 * task already on CPU to get the lock soon rather than put
215 * itself into sleep and waiting for system woke it or someone
216 * else in the head of the wait list up.
217 */
218 if (sem->activity == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800221 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
222 schedule();
223 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800225 /* got the lock */
226 sem->activity = -1;
227 list_del(&waiter.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800229 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800232void __sched __down_write(struct rw_semaphore *sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700233{
234 __down_write_nested(sem, 0);
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/*
238 * trylock for writing -- returns 1 if successful, 0 if contention
239 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800240int __down_write_trylock(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 unsigned long flags;
243 int ret = 0;
244
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100245 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800247 if (sem->activity == 0) {
248 /* got the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 sem->activity = -1;
250 ret = 1;
251 }
252
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100253 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return ret;
256}
257
258/*
259 * release a read lock on the semaphore
260 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800261void __up_read(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 unsigned long flags;
264
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100265 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if (--sem->activity == 0 && !list_empty(&sem->wait_list))
268 sem = __rwsem_wake_one_writer(sem);
269
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100270 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*
274 * release a write lock on the semaphore
275 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800276void __up_write(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 unsigned long flags;
279
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100280 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 sem->activity = 0;
283 if (!list_empty(&sem->wait_list))
284 sem = __rwsem_do_wake(sem, 1);
285
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100286 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
290 * downgrade a write lock into a read lock
291 * - just wake up any readers at the front of the queue
292 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800293void __downgrade_write(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 unsigned long flags;
296
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100297 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 sem->activity = 1;
300 if (!list_empty(&sem->wait_list))
301 sem = __rwsem_do_wake(sem, 0);
302
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100303 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305