blob: 9a675fa9d78eb70ad93944506ae53adb4c6f3e3e [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 Lespinasse8cf53222013-05-07 06:45:58 -070071 if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
72 if (wake_type != RWSEM_WAKE_READ_OWNED)
73 /* Wake writer at the front of the queue, but do not
74 * grant it the lock yet as we want other writers
75 * to be able to steal it. Readers, on the other hand,
76 * will block as they will notice the queued writer.
77 */
78 wake_up_process(waiter->task);
Michel Lespinasse345af7b2010-08-09 17:21:15 -070079 goto out;
Michel Lespinasse8cf53222013-05-07 06:45:58 -070080 }
Michel Lespinasse345af7b2010-08-09 17:21:15 -070081
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -070082 /* If we come here from up_xxxx(), another thread might have reached
83 * rwsem_down_failed_common() before we acquired the spinlock and
84 * woken up a waiter, making it now active. We prefer to check for
85 * this first in order to not spend too much time with the spinlock
86 * held if we're not going to be able to wake up readers in the end.
87 *
88 * Note that we do not need to update the rwsem count: any writer
89 * trying to acquire rwsem will run rwsem_down_write_failed() due
90 * to the waiting threads and block trying to acquire the spinlock.
91 *
92 * We use a dummy atomic update in order to acquire the cache line
93 * exclusively since we expect to succeed and run the final rwsem
94 * count adjustment pretty soon.
95 */
96 if (wake_type == RWSEM_WAKE_ANY &&
Michel Lespinasse424acaa2010-08-09 17:21:19 -070097 rwsem_atomic_update(0, sem) < RWSEM_WAITING_BIAS)
98 /* Someone grabbed the sem for write already */
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -070099 goto out;
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700100
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700101 /* Grant an infinite number of read locks to the readers at the front
102 * of the queue. Note we increment the 'active part' of the count by
103 * the number of readers before waking any processes up.
104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 woken = 0;
106 do {
107 woken++;
108
109 if (waiter->list.next == &sem->wait_list)
110 break;
111
112 waiter = list_entry(waiter->list.next,
113 struct rwsem_waiter, list);
114
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700115 } while (waiter->type != RWSEM_WAITING_FOR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Michel Lespinassefd41b332010-08-09 17:21:18 -0700117 adjustment = woken * RWSEM_ACTIVE_READ_BIAS;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700118 if (waiter->type != RWSEM_WAITING_FOR_WRITE)
Michel Lespinassefd41b332010-08-09 17:21:18 -0700119 /* hit end of list above */
120 adjustment -= RWSEM_WAITING_BIAS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Michel Lespinassefd41b332010-08-09 17:21:18 -0700122 rwsem_atomic_add(adjustment, sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 next = sem->wait_list.next;
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700125 loop = woken;
126 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 waiter = list_entry(next, struct rwsem_waiter, list);
128 next = waiter->list.next;
129 tsk = waiter->task;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700130 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 waiter->task = NULL;
132 wake_up_process(tsk);
133 put_task_struct(tsk);
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700134 } while (--loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 sem->wait_list.next = next;
137 next->prev = &sem->wait_list;
138
139 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return sem;
Alex Shice6711f2013-02-05 21:11:55 +0800141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
Michel Lespinasse1e782772013-05-07 06:45:51 -0700144 * wait for the read lock to be granted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
Michel Lespinasse1e782772013-05-07 06:45:51 -0700146struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Michel Lespinasse1e782772013-05-07 06:45:51 -0700148 signed long adjustment = -RWSEM_ACTIVE_READ_BIAS;
Michel Lespinassea8618a02010-08-09 17:21:20 -0700149 struct rwsem_waiter waiter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct task_struct *tsk = current;
151 signed long count;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* set up my own style of waitqueue */
Michel Lespinassea8618a02010-08-09 17:21:20 -0700154 waiter.task = tsk;
Michel Lespinasseda169222013-05-07 06:45:52 -0700155 waiter.type = RWSEM_WAITING_FOR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 get_task_struct(tsk);
157
Michel Lespinassef7dd1ce2013-05-07 06:45:50 -0700158 raw_spin_lock_irq(&sem->wait_lock);
Michel Lespinassefd41b332010-08-09 17:21:18 -0700159 if (list_empty(&sem->wait_list))
160 adjustment += RWSEM_WAITING_BIAS;
Michel Lespinassea8618a02010-08-09 17:21:20 -0700161 list_add_tail(&waiter.list, &sem->wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700163 /* we're now waiting on the lock, but no longer actively locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 count = rwsem_atomic_update(adjustment, sem);
165
Michel Lespinasseda169222013-05-07 06:45:52 -0700166 /* If there are no active locks, wake the front queued process(es). */
Michel Lespinasse424acaa2010-08-09 17:21:19 -0700167 if (count == RWSEM_WAITING_BIAS)
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700168 sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100170 raw_spin_unlock_irq(&sem->wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* wait to be given the lock */
Michel Lespinassef7dd1ce2013-05-07 06:45:50 -0700173 while (true) {
174 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinassea8618a02010-08-09 17:21:20 -0700175 if (!waiter.task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
180 tsk->state = TASK_RUNNING;
181
182 return sem;
183}
184
185/*
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700186 * wait until we successfully acquire the write lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100188struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Michel Lespinasse1e782772013-05-07 06:45:51 -0700190 signed long adjustment = -RWSEM_ACTIVE_WRITE_BIAS;
191 struct rwsem_waiter waiter;
192 struct task_struct *tsk = current;
193 signed long count;
194
195 /* set up my own style of waitqueue */
196 waiter.task = tsk;
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700197 waiter.type = RWSEM_WAITING_FOR_WRITE;
Michel Lespinasse1e782772013-05-07 06:45:51 -0700198
199 raw_spin_lock_irq(&sem->wait_lock);
200 if (list_empty(&sem->wait_list))
201 adjustment += RWSEM_WAITING_BIAS;
202 list_add_tail(&waiter.list, &sem->wait_list);
203
204 /* we're now waiting on the lock, but no longer actively locking */
205 count = rwsem_atomic_update(adjustment, sem);
206
Michel Lespinasseed00f642013-05-07 06:45:54 -0700207 /* If there were already threads queued before us and there are no
208 * active writers, the lock must be read owned; so we try to wake
209 * any read locks that were queued ahead of us. */
210 if (count > RWSEM_WAITING_BIAS &&
211 adjustment == -RWSEM_ACTIVE_WRITE_BIAS)
Michel Lespinasse1e782772013-05-07 06:45:51 -0700212 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
213
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700214 /* wait until we successfully acquire the lock */
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700215 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700216 while (true) {
Michel Lespinasse9b0fc9c2013-05-07 06:45:57 -0700217 if (!(count & RWSEM_ACTIVE_MASK)) {
218 /* Try acquiring the write lock. */
219 count = RWSEM_ACTIVE_WRITE_BIAS;
220 if (!list_is_singular(&sem->wait_list))
221 count += RWSEM_WAITING_BIAS;
222 if (cmpxchg(&sem->count, RWSEM_WAITING_BIAS, count) ==
Michel Lespinasse5ede9722013-05-07 06:45:55 -0700223 RWSEM_WAITING_BIAS)
Michel Lespinasse9b0fc9c2013-05-07 06:45:57 -0700224 break;
225 }
Michel Lespinasse1e782772013-05-07 06:45:51 -0700226
Michel Lespinasse1e782772013-05-07 06:45:51 -0700227 raw_spin_unlock_irq(&sem->wait_lock);
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700228
229 /* Block until there are no active lockers. */
230 do {
231 schedule();
232 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinasse9b0fc9c2013-05-07 06:45:57 -0700233 } while ((count = sem->count) & RWSEM_ACTIVE_MASK);
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700234
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700235 raw_spin_lock_irq(&sem->wait_lock);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700236 }
237
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700238 list_del(&waiter.list);
239 raw_spin_unlock_irq(&sem->wait_lock);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700240 tsk->state = TASK_RUNNING;
241
242 return sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/*
246 * handle waking up a waiter on the semaphore
247 * - up_read/up_write has decremented the active part of count if we come here
248 */
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100249struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 unsigned long flags;
252
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100253 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /* do nothing if list empty */
256 if (!list_empty(&sem->wait_list))
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700257 sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100259 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return sem;
262}
263
264/*
265 * downgrade a write lock into a read lock
266 * - caller incremented waiting part of count and discovered it still negative
267 * - just wake up any readers at the front of the queue
268 */
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100269struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 unsigned long flags;
272
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100273 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* do nothing if list empty */
276 if (!list_empty(&sem->wait_list))
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700277 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100279 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return sem;
282}
283
284EXPORT_SYMBOL(rwsem_down_read_failed);
285EXPORT_SYMBOL(rwsem_down_write_failed);
286EXPORT_SYMBOL(rwsem_wake);
287EXPORT_SYMBOL(rwsem_downgrade_wake);