blob: fb658af1c12c7f0e5ce56fbbb8c313a889b119eb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* rwsem.c: R/W semaphores: contention handling functions
2 *
3 * Written by David Howells (dhowells@redhat.com).
4 * Derived from arch/i386/kernel/semaphore.c
Alex Shice6711f2013-02-05 21:11:55 +08005 *
6 * Writer lock-stealing by Alex Shi <alex.shi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8#include <linux/rwsem.h>
9#include <linux/sched.h>
10#include <linux/init.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050011#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Ingo Molnar4ea21762006-07-03 00:24:53 -070013/*
14 * Initialize an rwsem:
15 */
16void __init_rwsem(struct rw_semaphore *sem, const char *name,
17 struct lock_class_key *key)
18{
19#ifdef CONFIG_DEBUG_LOCK_ALLOC
20 /*
21 * Make sure we are not reinitializing a held semaphore:
22 */
23 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -040024 lockdep_init_map(&sem->dep_map, name, key, 0);
Ingo Molnar4ea21762006-07-03 00:24:53 -070025#endif
26 sem->count = RWSEM_UNLOCKED_VALUE;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010027 raw_spin_lock_init(&sem->wait_lock);
Ingo Molnar4ea21762006-07-03 00:24:53 -070028 INIT_LIST_HEAD(&sem->wait_list);
29}
30
31EXPORT_SYMBOL(__init_rwsem);
32
Michel Lespinassee2d57f72013-05-07 06:45:49 -070033enum rwsem_waiter_type {
34 RWSEM_WAITING_FOR_WRITE,
35 RWSEM_WAITING_FOR_READ
36};
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct rwsem_waiter {
39 struct list_head list;
40 struct task_struct *task;
Michel Lespinassee2d57f72013-05-07 06:45:49 -070041 enum rwsem_waiter_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -070044/* Wake types for __rwsem_do_wake(). Note that RWSEM_WAKE_NO_ACTIVE and
45 * RWSEM_WAKE_READ_OWNED imply that the spinlock must have been kept held
46 * since the rwsem value was observed.
47 */
48#define RWSEM_WAKE_ANY 0 /* Wake whatever's at head of wait list */
49#define RWSEM_WAKE_NO_ACTIVE 1 /* rwsem was observed with no active thread */
50#define RWSEM_WAKE_READ_OWNED 2 /* rwsem was observed to be read owned */
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * handle the lock release when processes blocked on it that can now run
54 * - if we come here from up_xxxx(), then:
55 * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
56 * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
Michel Lespinasse345af7b2010-08-09 17:21:15 -070057 * - there must be someone on the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * - the spinlock must be held by the caller
59 * - woken process blocks are discarded from the list after having task zeroed
60 * - writers are only woken if downgrading is false
61 */
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -070062static struct rw_semaphore *
63__rwsem_do_wake(struct rw_semaphore *sem, int wake_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct rwsem_waiter *waiter;
66 struct task_struct *tsk;
67 struct list_head *next;
Alex Shice6711f2013-02-05 21:11:55 +080068 signed long woken, loop, adjustment;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Michel Lespinasse345af7b2010-08-09 17:21:15 -070070 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
Michel Lespinassee2d57f72013-05-07 06:45:49 -070071 if (waiter->type != RWSEM_WAITING_FOR_WRITE)
Michel Lespinasse345af7b2010-08-09 17:21:15 -070072 goto readers_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -070074 if (wake_type == RWSEM_WAKE_READ_OWNED)
Michel Lespinasse424acaa2010-08-09 17:21:19 -070075 /* Another active reader was observed, so wakeup is not
76 * likely to succeed. Save the atomic op.
77 */
Michel Lespinasse345af7b2010-08-09 17:21:15 -070078 goto out;
79
Alex Shice6711f2013-02-05 21:11:55 +080080 /* Wake up the writing waiter and let the task grab the sem: */
81 wake_up_process(waiter->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 goto out;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 readers_only:
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -070085 /* If we come here from up_xxxx(), another thread might have reached
86 * rwsem_down_failed_common() before we acquired the spinlock and
87 * woken up a waiter, making it now active. We prefer to check for
88 * this first in order to not spend too much time with the spinlock
89 * held if we're not going to be able to wake up readers in the end.
90 *
91 * Note that we do not need to update the rwsem count: any writer
92 * trying to acquire rwsem will run rwsem_down_write_failed() due
93 * to the waiting threads and block trying to acquire the spinlock.
94 *
95 * We use a dummy atomic update in order to acquire the cache line
96 * exclusively since we expect to succeed and run the final rwsem
97 * count adjustment pretty soon.
98 */
99 if (wake_type == RWSEM_WAKE_ANY &&
Michel Lespinasse424acaa2010-08-09 17:21:19 -0700100 rwsem_atomic_update(0, sem) < RWSEM_WAITING_BIAS)
101 /* Someone grabbed the sem for write already */
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700102 goto out;
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700103
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700104 /* Grant an infinite number of read locks to the readers at the front
105 * of the queue. Note we increment the 'active part' of the count by
106 * the number of readers before waking any processes up.
107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 woken = 0;
109 do {
110 woken++;
111
112 if (waiter->list.next == &sem->wait_list)
113 break;
114
115 waiter = list_entry(waiter->list.next,
116 struct rwsem_waiter, list);
117
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700118 } while (waiter->type != RWSEM_WAITING_FOR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Michel Lespinassefd41b332010-08-09 17:21:18 -0700120 adjustment = woken * RWSEM_ACTIVE_READ_BIAS;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700121 if (waiter->type != RWSEM_WAITING_FOR_WRITE)
Michel Lespinassefd41b332010-08-09 17:21:18 -0700122 /* hit end of list above */
123 adjustment -= RWSEM_WAITING_BIAS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Michel Lespinassefd41b332010-08-09 17:21:18 -0700125 rwsem_atomic_add(adjustment, sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 next = sem->wait_list.next;
Michel Lespinassefd41b332010-08-09 17:21:18 -0700128 for (loop = woken; loop > 0; loop--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 waiter = list_entry(next, struct rwsem_waiter, list);
130 next = waiter->list.next;
131 tsk = waiter->task;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700132 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 waiter->task = NULL;
134 wake_up_process(tsk);
135 put_task_struct(tsk);
136 }
137
138 sem->wait_list.next = next;
139 next->prev = &sem->wait_list;
140
141 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return sem;
Alex Shice6711f2013-02-05 21:11:55 +0800143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Alex Shice6711f2013-02-05 21:11:55 +0800145/* Try to get write sem, caller holds sem->wait_lock: */
146static int try_get_writer_sem(struct rw_semaphore *sem,
147 struct rwsem_waiter *waiter)
148{
149 struct rwsem_waiter *fwaiter;
150 long oldcount, adjustment;
151
152 /* only steal when first waiter is writing */
153 fwaiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700154 if (fwaiter->type != RWSEM_WAITING_FOR_WRITE)
Alex Shice6711f2013-02-05 21:11:55 +0800155 return 0;
156
157 adjustment = RWSEM_ACTIVE_WRITE_BIAS;
158 /* Only one waiter in the queue: */
159 if (fwaiter == waiter && waiter->list.next == &sem->wait_list)
160 adjustment -= RWSEM_WAITING_BIAS;
161
162try_again_write:
163 oldcount = rwsem_atomic_update(adjustment, sem) - adjustment;
164 if (!(oldcount & RWSEM_ACTIVE_MASK)) {
165 /* No active lock: */
166 struct task_struct *tsk = waiter->task;
167
168 list_del(&waiter->list);
169 smp_mb();
170 put_task_struct(tsk);
171 tsk->state = TASK_RUNNING;
172 return 1;
173 }
174 /* some one grabbed the sem already */
Michel Lespinassefd41b332010-08-09 17:21:18 -0700175 if (rwsem_atomic_update(-adjustment, sem) & RWSEM_ACTIVE_MASK)
Alex Shice6711f2013-02-05 21:11:55 +0800176 return 0;
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700177 goto try_again_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/*
Michel Lespinasse1e782772013-05-07 06:45:51 -0700181 * wait for the read lock to be granted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Michel Lespinasse1e782772013-05-07 06:45:51 -0700183struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Michel Lespinasse1e782772013-05-07 06:45:51 -0700185 enum rwsem_waiter_type type = RWSEM_WAITING_FOR_READ;
186 signed long adjustment = -RWSEM_ACTIVE_READ_BIAS;
Michel Lespinassea8618a02010-08-09 17:21:20 -0700187 struct rwsem_waiter waiter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct task_struct *tsk = current;
189 signed long count;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* set up my own style of waitqueue */
Michel Lespinassea8618a02010-08-09 17:21:20 -0700192 waiter.task = tsk;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700193 waiter.type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 get_task_struct(tsk);
195
Michel Lespinassef7dd1ce2013-05-07 06:45:50 -0700196 raw_spin_lock_irq(&sem->wait_lock);
Michel Lespinassefd41b332010-08-09 17:21:18 -0700197 if (list_empty(&sem->wait_list))
198 adjustment += RWSEM_WAITING_BIAS;
Michel Lespinassea8618a02010-08-09 17:21:20 -0700199 list_add_tail(&waiter.list, &sem->wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700201 /* we're now waiting on the lock, but no longer actively locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 count = rwsem_atomic_update(adjustment, sem);
203
Michel Lespinasse424acaa2010-08-09 17:21:19 -0700204 /* If there are no active locks, wake the front queued process(es) up.
205 *
206 * Alternatively, if we're called from a failed down_write(), there
207 * were already threads queued before us and there are no active
208 * writers, the lock must be read owned; so we try to wake any read
209 * locks that were queued ahead of us. */
210 if (count == RWSEM_WAITING_BIAS)
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700211 sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE);
Michel Lespinasse424acaa2010-08-09 17:21:19 -0700212 else if (count > RWSEM_WAITING_BIAS &&
213 adjustment == -RWSEM_ACTIVE_WRITE_BIAS)
214 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100216 raw_spin_unlock_irq(&sem->wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /* wait to be given the lock */
Michel Lespinassef7dd1ce2013-05-07 06:45:50 -0700219 while (true) {
220 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinassea8618a02010-08-09 17:21:20 -0700221 if (!waiter.task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
Alex Shice6711f2013-02-05 21:11:55 +0800223
224 raw_spin_lock_irq(&sem->wait_lock);
225 /* Try to get the writer sem, may steal from the head writer: */
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700226 if (type == RWSEM_WAITING_FOR_WRITE)
Alex Shice6711f2013-02-05 21:11:55 +0800227 if (try_get_writer_sem(sem, &waiter)) {
228 raw_spin_unlock_irq(&sem->wait_lock);
229 return sem;
230 }
231 raw_spin_unlock_irq(&sem->wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 tsk->state = TASK_RUNNING;
236
237 return sem;
238}
239
240/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * wait for the write lock to be granted
242 */
Thomas Gleixnerd12337542011-01-26 21:32:01 +0100243struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Michel Lespinasse1e782772013-05-07 06:45:51 -0700245 enum rwsem_waiter_type type = RWSEM_WAITING_FOR_WRITE;
246 signed long adjustment = -RWSEM_ACTIVE_WRITE_BIAS;
247 struct rwsem_waiter waiter;
248 struct task_struct *tsk = current;
249 signed long count;
250
251 /* set up my own style of waitqueue */
252 waiter.task = tsk;
253 waiter.type = type;
254 get_task_struct(tsk);
255
256 raw_spin_lock_irq(&sem->wait_lock);
257 if (list_empty(&sem->wait_list))
258 adjustment += RWSEM_WAITING_BIAS;
259 list_add_tail(&waiter.list, &sem->wait_list);
260
261 /* we're now waiting on the lock, but no longer actively locking */
262 count = rwsem_atomic_update(adjustment, sem);
263
264 /* If there are no active locks, wake the front queued process(es) up.
265 *
266 * Alternatively, if we're called from a failed down_write(), there
267 * were already threads queued before us and there are no active
268 * writers, the lock must be read owned; so we try to wake any read
269 * locks that were queued ahead of us. */
270 if (count == RWSEM_WAITING_BIAS)
271 sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE);
272 else if (count > RWSEM_WAITING_BIAS &&
273 adjustment == -RWSEM_ACTIVE_WRITE_BIAS)
274 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
275
276 raw_spin_unlock_irq(&sem->wait_lock);
277
278 /* wait to be given the lock */
279 while (true) {
280 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
281 if (!waiter.task)
282 break;
283
284 raw_spin_lock_irq(&sem->wait_lock);
285 /* Try to get the writer sem, may steal from the head writer: */
286 if (type == RWSEM_WAITING_FOR_WRITE)
287 if (try_get_writer_sem(sem, &waiter)) {
288 raw_spin_unlock_irq(&sem->wait_lock);
289 return sem;
290 }
291 raw_spin_unlock_irq(&sem->wait_lock);
292 schedule();
293 }
294
295 tsk->state = TASK_RUNNING;
296
297 return sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/*
301 * handle waking up a waiter on the semaphore
302 * - up_read/up_write has decremented the active part of count if we come here
303 */
Thomas Gleixnerd12337542011-01-26 21:32:01 +0100304struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 unsigned long flags;
307
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100308 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* do nothing if list empty */
311 if (!list_empty(&sem->wait_list))
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700312 sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100314 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return sem;
317}
318
319/*
320 * downgrade a write lock into a read lock
321 * - caller incremented waiting part of count and discovered it still negative
322 * - just wake up any readers at the front of the queue
323 */
Thomas Gleixnerd12337542011-01-26 21:32:01 +0100324struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 unsigned long flags;
327
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100328 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* do nothing if list empty */
331 if (!list_empty(&sem->wait_list))
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700332 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100334 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return sem;
337}
338
339EXPORT_SYMBOL(rwsem_down_read_failed);
340EXPORT_SYMBOL(rwsem_down_write_failed);
341EXPORT_SYMBOL(rwsem_wake);
342EXPORT_SYMBOL(rwsem_downgrade_wake);