blob: 7542afbb22b368c9c693408b9233421b6a89746b [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
12struct rwsem_waiter {
13 struct list_head list;
14 struct task_struct *task;
15 unsigned int flags;
16#define RWSEM_WAITING_FOR_READ 0x00000001
17#define RWSEM_WAITING_FOR_WRITE 0x00000002
18};
19
Amerigo Wang29671f22009-12-14 18:00:21 -080020int rwsem_is_locked(struct rw_semaphore *sem)
21{
22 int ret = 1;
23 unsigned long flags;
24
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010025 if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) {
Amerigo Wang29671f22009-12-14 18:00:21 -080026 ret = (sem->activity != 0);
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010027 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Amerigo Wang29671f22009-12-14 18:00:21 -080028 }
29 return ret;
30}
31EXPORT_SYMBOL(rwsem_is_locked);
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * initialise the semaphore
35 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070036void __init_rwsem(struct rw_semaphore *sem, const char *name,
37 struct lock_class_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Ingo Molnar4ea21762006-07-03 00:24:53 -070039#ifdef CONFIG_DEBUG_LOCK_ALLOC
40 /*
41 * Make sure we are not reinitializing a held semaphore:
42 */
43 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -040044 lockdep_init_map(&sem->dep_map, name, key, 0);
Ingo Molnar4ea21762006-07-03 00:24:53 -070045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 sem->activity = 0;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010047 raw_spin_lock_init(&sem->wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 INIT_LIST_HEAD(&sem->wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
Amerigo Wang118d52d2009-12-14 18:00:20 -080050EXPORT_SYMBOL(__init_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
53 * handle the lock release when processes blocked on it that can now run
54 * - if we come here, then:
55 * - the 'active count' _reached_ zero
56 * - the 'waiting count' is non-zero
57 * - the spinlock must be held by the caller
58 * - woken process blocks are discarded from the list after having task zeroed
59 * - writers are only woken if wakewrite is non-zero
60 */
61static inline struct rw_semaphore *
62__rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
63{
64 struct rwsem_waiter *waiter;
65 struct task_struct *tsk;
66 int woken;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
69
70 if (!wakewrite) {
71 if (waiter->flags & RWSEM_WAITING_FOR_WRITE)
72 goto out;
73 goto dont_wake_writers;
74 }
75
Yuanhan Liu41ef8f82013-02-01 18:59:16 +080076 /*
77 * as we support write lock stealing, we can't set sem->activity
78 * to -1 here to indicate we get the lock. Instead, we wake it up
79 * to let it go get it again.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81 if (waiter->flags & RWSEM_WAITING_FOR_WRITE) {
Yuanhan Liu41ef8f82013-02-01 18:59:16 +080082 wake_up_process(waiter->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 goto out;
84 }
85
86 /* grant an infinite number of read locks to the front of the queue */
87 dont_wake_writers:
88 woken = 0;
89 while (waiter->flags & RWSEM_WAITING_FOR_READ) {
90 struct list_head *next = waiter->list.next;
91
92 list_del(&waiter->list);
93 tsk = waiter->task;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -070094 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 waiter->task = NULL;
96 wake_up_process(tsk);
97 put_task_struct(tsk);
98 woken++;
99 if (list_empty(&sem->wait_list))
100 break;
101 waiter = list_entry(next, struct rwsem_waiter, list);
102 }
103
104 sem->activity += woken;
105
106 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return sem;
108}
109
110/*
111 * wake a single writer
112 */
113static inline struct rw_semaphore *
114__rwsem_wake_one_writer(struct rw_semaphore *sem)
115{
116 struct rwsem_waiter *waiter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800119 wake_up_process(waiter->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return sem;
122}
123
124/*
125 * get a read lock on the semaphore
126 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800127void __sched __down_read(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct rwsem_waiter waiter;
130 struct task_struct *tsk;
Kevin Hilman3eac4ab2010-04-07 11:52:46 -0700131 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100133 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
136 /* granted */
137 sem->activity++;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100138 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 goto out;
140 }
141
142 tsk = current;
143 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
144
145 /* set up my own style of waitqueue */
146 waiter.task = tsk;
147 waiter.flags = RWSEM_WAITING_FOR_READ;
148 get_task_struct(tsk);
149
150 list_add_tail(&waiter.list, &sem->wait_list);
151
152 /* we don't need to touch the semaphore struct anymore */
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100153 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* wait to be given the lock */
156 for (;;) {
157 if (!waiter.task)
158 break;
159 schedule();
160 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
161 }
162
163 tsk->state = TASK_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 out:
Ingo Molnarc4e05112006-07-03 00:24:29 -0700165 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168/*
169 * trylock for reading -- returns 1 if successful, 0 if contention
170 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800171int __down_read_trylock(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 unsigned long flags;
174 int ret = 0;
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100177 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
180 /* granted */
181 sem->activity++;
182 ret = 1;
183 }
184
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100185 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return ret;
188}
189
190/*
191 * get a write lock on the semaphore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800193void __sched __down_write_nested(struct rw_semaphore *sem, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 struct rwsem_waiter waiter;
196 struct task_struct *tsk;
Kevin Hilman3eac4ab2010-04-07 11:52:46 -0700197 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100199 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* set up my own style of waitqueue */
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800202 tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 waiter.task = tsk;
204 waiter.flags = RWSEM_WAITING_FOR_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 list_add_tail(&waiter.list, &sem->wait_list);
206
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800207 /* wait for someone to release the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 for (;;) {
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800209 /*
210 * That is the key to support write lock stealing: allows the
211 * task already on CPU to get the lock soon rather than put
212 * itself into sleep and waiting for system woke it or someone
213 * else in the head of the wait list up.
214 */
215 if (sem->activity == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800218 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
219 schedule();
220 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800222 /* got the lock */
223 sem->activity = -1;
224 list_del(&waiter.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800226 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800229void __sched __down_write(struct rw_semaphore *sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700230{
231 __down_write_nested(sem, 0);
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*
235 * trylock for writing -- returns 1 if successful, 0 if contention
236 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800237int __down_write_trylock(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 unsigned long flags;
240 int ret = 0;
241
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100242 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Yuanhan Liu41ef8f82013-02-01 18:59:16 +0800244 if (sem->activity == 0) {
245 /* got the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 sem->activity = -1;
247 ret = 1;
248 }
249
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100250 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return ret;
253}
254
255/*
256 * release a read lock on the semaphore
257 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800258void __up_read(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 unsigned long flags;
261
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100262 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (--sem->activity == 0 && !list_empty(&sem->wait_list))
265 sem = __rwsem_wake_one_writer(sem);
266
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100267 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/*
271 * release a write lock on the semaphore
272 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800273void __up_write(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 unsigned long flags;
276
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100277 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 sem->activity = 0;
280 if (!list_empty(&sem->wait_list))
281 sem = __rwsem_do_wake(sem, 1);
282
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100283 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286/*
287 * downgrade a write lock into a read lock
288 * - just wake up any readers at the front of the queue
289 */
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800290void __downgrade_write(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 unsigned long flags;
293
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100294 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 sem->activity = 1;
297 if (!list_empty(&sem->wait_list))
298 sem = __rwsem_do_wake(sem, 0);
299
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100300 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302