blob: 1d66e08e897d166cf70bc2f41cdc3c3bbb57cf45 [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>
Michel Lespinassefe6e6742013-05-07 06:45:59 -07007 * and Michel Lespinasse <walken@google.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9#include <linux/rwsem.h>
10#include <linux/sched.h>
11#include <linux/init.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Ingo Molnar4ea21762006-07-03 00:24:53 -070014/*
15 * Initialize an rwsem:
16 */
17void __init_rwsem(struct rw_semaphore *sem, const char *name,
18 struct lock_class_key *key)
19{
20#ifdef CONFIG_DEBUG_LOCK_ALLOC
21 /*
22 * Make sure we are not reinitializing a held semaphore:
23 */
24 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -040025 lockdep_init_map(&sem->dep_map, name, key, 0);
Ingo Molnar4ea21762006-07-03 00:24:53 -070026#endif
27 sem->count = RWSEM_UNLOCKED_VALUE;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010028 raw_spin_lock_init(&sem->wait_lock);
Ingo Molnar4ea21762006-07-03 00:24:53 -070029 INIT_LIST_HEAD(&sem->wait_list);
30}
31
32EXPORT_SYMBOL(__init_rwsem);
33
Michel Lespinassee2d57f72013-05-07 06:45:49 -070034enum rwsem_waiter_type {
35 RWSEM_WAITING_FOR_WRITE,
36 RWSEM_WAITING_FOR_READ
37};
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039struct rwsem_waiter {
40 struct list_head list;
41 struct task_struct *task;
Michel Lespinassee2d57f72013-05-07 06:45:49 -070042 enum rwsem_waiter_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
Michel Lespinassefe6e6742013-05-07 06:45:59 -070045enum rwsem_wake_type {
46 RWSEM_WAKE_ANY, /* Wake whatever's at head of wait list */
47 RWSEM_WAKE_READERS, /* Wake readers only */
48 RWSEM_WAKE_READ_OWNED /* Waker thread holds the read lock */
49};
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * handle the lock release when processes blocked on it that can now run
53 * - if we come here from up_xxxx(), then:
54 * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
55 * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
Michel Lespinasse345af7b2010-08-09 17:21:15 -070056 * - there must be someone on the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * - 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 downgrading is false
60 */
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -070061static struct rw_semaphore *
Michel Lespinassefe6e6742013-05-07 06:45:59 -070062__rwsem_do_wake(struct rw_semaphore *sem, enum rwsem_wake_type wake_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct rwsem_waiter *waiter;
65 struct task_struct *tsk;
66 struct list_head *next;
Davidlohr Buesob5f54182013-05-07 06:46:02 -070067 long oldcount, woken, loop, adjustment;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Michel Lespinasse345af7b2010-08-09 17:21:15 -070069 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
Michel Lespinasse8cf53222013-05-07 06:45:58 -070070 if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
Michel Lespinassefe6e6742013-05-07 06:45:59 -070071 if (wake_type == RWSEM_WAKE_ANY)
Michel Lespinasse8cf53222013-05-07 06:45:58 -070072 /* Wake writer at the front of the queue, but do not
73 * grant it the lock yet as we want other writers
74 * to be able to steal it. Readers, on the other hand,
75 * will block as they will notice the queued writer.
76 */
77 wake_up_process(waiter->task);
Michel Lespinasse345af7b2010-08-09 17:21:15 -070078 goto out;
Michel Lespinasse8cf53222013-05-07 06:45:58 -070079 }
Michel Lespinasse345af7b2010-08-09 17:21:15 -070080
Michel Lespinassefe6e6742013-05-07 06:45:59 -070081 /* Writers might steal the lock before we grant it to the next reader.
82 * We prefer to do the first reader grant before counting readers
83 * so we can bail out early if a writer stole the lock.
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -070084 */
Michel Lespinassefe6e6742013-05-07 06:45:59 -070085 adjustment = 0;
86 if (wake_type != RWSEM_WAKE_READ_OWNED) {
87 adjustment = RWSEM_ACTIVE_READ_BIAS;
88 try_reader_grant:
89 oldcount = rwsem_atomic_update(adjustment, sem) - adjustment;
90 if (unlikely(oldcount < RWSEM_WAITING_BIAS)) {
91 /* A writer stole the lock. Undo our reader grant. */
92 if (rwsem_atomic_update(-adjustment, sem) &
93 RWSEM_ACTIVE_MASK)
94 goto out;
95 /* Last active locker left. Retry waking readers. */
96 goto try_reader_grant;
97 }
98 }
Michel Lespinasse345af7b2010-08-09 17:21:15 -070099
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700100 /* Grant an infinite number of read locks to the readers at the front
101 * of the queue. Note we increment the 'active part' of the count by
102 * the number of readers before waking any processes up.
103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 woken = 0;
105 do {
106 woken++;
107
108 if (waiter->list.next == &sem->wait_list)
109 break;
110
111 waiter = list_entry(waiter->list.next,
112 struct rwsem_waiter, list);
113
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700114 } while (waiter->type != RWSEM_WAITING_FOR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700116 adjustment = woken * RWSEM_ACTIVE_READ_BIAS - adjustment;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700117 if (waiter->type != RWSEM_WAITING_FOR_WRITE)
Michel Lespinassefd41b332010-08-09 17:21:18 -0700118 /* hit end of list above */
119 adjustment -= RWSEM_WAITING_BIAS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700121 if (adjustment)
122 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 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100146__visible
Michel Lespinasse1e782772013-05-07 06:45:51 -0700147struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Davidlohr Buesob5f54182013-05-07 06:46:02 -0700149 long count, adjustment = -RWSEM_ACTIVE_READ_BIAS;
Michel Lespinassea8618a02010-08-09 17:21:20 -0700150 struct rwsem_waiter waiter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
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 Lespinasse25c39322013-05-07 06:46:00 -0700166 /* If there are no active locks, wake the front queued process(es).
167 *
168 * If there are no writers and we are first in the queue,
169 * wake our own waiter to join the existing active readers !
170 */
171 if (count == RWSEM_WAITING_BIAS ||
172 (count > RWSEM_WAITING_BIAS &&
173 adjustment != -RWSEM_ACTIVE_READ_BIAS))
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700174 sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100176 raw_spin_unlock_irq(&sem->wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /* wait to be given the lock */
Michel Lespinassef7dd1ce2013-05-07 06:45:50 -0700179 while (true) {
180 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinassea8618a02010-08-09 17:21:20 -0700181 if (!waiter.task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 break;
183 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185
186 tsk->state = TASK_RUNNING;
187
188 return sem;
189}
190
191/*
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700192 * wait until we successfully acquire the write lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100194__visible
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100195struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Davidlohr Buesob5f54182013-05-07 06:46:02 -0700197 long count, adjustment = -RWSEM_ACTIVE_WRITE_BIAS;
Michel Lespinasse1e782772013-05-07 06:45:51 -0700198 struct rwsem_waiter waiter;
199 struct task_struct *tsk = current;
Michel Lespinasse1e782772013-05-07 06:45:51 -0700200
201 /* set up my own style of waitqueue */
202 waiter.task = tsk;
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700203 waiter.type = RWSEM_WAITING_FOR_WRITE;
Michel Lespinasse1e782772013-05-07 06:45:51 -0700204
205 raw_spin_lock_irq(&sem->wait_lock);
206 if (list_empty(&sem->wait_list))
207 adjustment += RWSEM_WAITING_BIAS;
208 list_add_tail(&waiter.list, &sem->wait_list);
209
210 /* we're now waiting on the lock, but no longer actively locking */
211 count = rwsem_atomic_update(adjustment, sem);
212
Michel Lespinasseed00f642013-05-07 06:45:54 -0700213 /* If there were already threads queued before us and there are no
214 * active writers, the lock must be read owned; so we try to wake
215 * any read locks that were queued ahead of us. */
216 if (count > RWSEM_WAITING_BIAS &&
217 adjustment == -RWSEM_ACTIVE_WRITE_BIAS)
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700218 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READERS);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700219
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700220 /* wait until we successfully acquire the lock */
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700221 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700222 while (true) {
Michel Lespinasse9b0fc9c2013-05-07 06:45:57 -0700223 if (!(count & RWSEM_ACTIVE_MASK)) {
224 /* Try acquiring the write lock. */
225 count = RWSEM_ACTIVE_WRITE_BIAS;
226 if (!list_is_singular(&sem->wait_list))
227 count += RWSEM_WAITING_BIAS;
Davidlohr Bueso9607a852013-05-07 15:39:03 -0700228
229 if (sem->count == RWSEM_WAITING_BIAS &&
230 cmpxchg(&sem->count, RWSEM_WAITING_BIAS, count) ==
Michel Lespinasse5ede9722013-05-07 06:45:55 -0700231 RWSEM_WAITING_BIAS)
Michel Lespinasse9b0fc9c2013-05-07 06:45:57 -0700232 break;
233 }
Michel Lespinasse1e782772013-05-07 06:45:51 -0700234
Michel Lespinasse1e782772013-05-07 06:45:51 -0700235 raw_spin_unlock_irq(&sem->wait_lock);
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700236
237 /* Block until there are no active lockers. */
238 do {
239 schedule();
240 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinasse9b0fc9c2013-05-07 06:45:57 -0700241 } while ((count = sem->count) & RWSEM_ACTIVE_MASK);
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700242
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700243 raw_spin_lock_irq(&sem->wait_lock);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700244 }
245
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700246 list_del(&waiter.list);
247 raw_spin_unlock_irq(&sem->wait_lock);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700248 tsk->state = TASK_RUNNING;
249
250 return sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/*
254 * handle waking up a waiter on the semaphore
255 * - up_read/up_write has decremented the active part of count if we come here
256 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100257__visible
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100258struct rw_semaphore *rwsem_wake(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 /* do nothing if list empty */
265 if (!list_empty(&sem->wait_list))
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700266 sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100268 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return sem;
271}
272
273/*
274 * downgrade a write lock into a read lock
275 * - caller incremented waiting part of count and discovered it still negative
276 * - just wake up any readers at the front of the queue
277 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100278__visible
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100279struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 unsigned long flags;
282
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100283 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* do nothing if list empty */
286 if (!list_empty(&sem->wait_list))
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700287 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100289 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return sem;
292}
293
294EXPORT_SYMBOL(rwsem_down_read_failed);
295EXPORT_SYMBOL(rwsem_down_write_failed);
296EXPORT_SYMBOL(rwsem_wake);
297EXPORT_SYMBOL(rwsem_downgrade_wake);