blob: b4219ff87b8c6dc95ae85bb2a4f38118681c0df6 [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/*
Tim Chen3cf2f342014-05-02 12:53:57 -070015 * Guide to the rw_semaphore's count field for common values.
16 * (32-bit case illustrated, similar for 64-bit)
17 *
18 * 0x0000000X (1) X readers active or attempting lock, no writer waiting
19 * X = #active_readers + #readers attempting to lock
20 * (X*ACTIVE_BIAS)
21 *
22 * 0x00000000 rwsem is unlocked, and no one is waiting for the lock or
23 * attempting to read lock or write lock.
24 *
25 * 0xffff000X (1) X readers active or attempting lock, with waiters for lock
26 * X = #active readers + # readers attempting lock
27 * (X*ACTIVE_BIAS + WAITING_BIAS)
28 * (2) 1 writer attempting lock, no waiters for lock
29 * X-1 = #active readers + #readers attempting lock
30 * ((X-1)*ACTIVE_BIAS + ACTIVE_WRITE_BIAS)
31 * (3) 1 writer active, no waiters for lock
32 * X-1 = #active readers + #readers attempting lock
33 * ((X-1)*ACTIVE_BIAS + ACTIVE_WRITE_BIAS)
34 *
35 * 0xffff0001 (1) 1 reader active or attempting lock, waiters for lock
36 * (WAITING_BIAS + ACTIVE_BIAS)
37 * (2) 1 writer active or attempting lock, no waiters for lock
38 * (ACTIVE_WRITE_BIAS)
39 *
40 * 0xffff0000 (1) There are writers or readers queued but none active
41 * or in the process of attempting lock.
42 * (WAITING_BIAS)
43 * Note: writer can attempt to steal lock for this count by adding
44 * ACTIVE_WRITE_BIAS in cmpxchg and checking the old count
45 *
46 * 0xfffe0001 (1) 1 writer active, or attempting lock. Waiters on queue.
47 * (ACTIVE_WRITE_BIAS + WAITING_BIAS)
48 *
49 * Note: Readers attempt to lock by adding ACTIVE_BIAS in down_read and checking
50 * the count becomes more than 0 for successful lock acquisition,
51 * i.e. the case where there are only readers or nobody has lock.
52 * (1st and 2nd case above).
53 *
54 * Writers attempt to lock by adding ACTIVE_WRITE_BIAS in down_write and
55 * checking the count becomes ACTIVE_WRITE_BIAS for successful lock
56 * acquisition (i.e. nobody else has lock or attempts lock). If
57 * unsuccessful, in rwsem_down_write_failed, we'll check to see if there
58 * are only waiters but none active (5th case above), and attempt to
59 * steal the lock.
60 *
61 */
62
63/*
Ingo Molnar4ea21762006-07-03 00:24:53 -070064 * Initialize an rwsem:
65 */
66void __init_rwsem(struct rw_semaphore *sem, const char *name,
67 struct lock_class_key *key)
68{
69#ifdef CONFIG_DEBUG_LOCK_ALLOC
70 /*
71 * Make sure we are not reinitializing a held semaphore:
72 */
73 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -040074 lockdep_init_map(&sem->dep_map, name, key, 0);
Ingo Molnar4ea21762006-07-03 00:24:53 -070075#endif
76 sem->count = RWSEM_UNLOCKED_VALUE;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010077 raw_spin_lock_init(&sem->wait_lock);
Ingo Molnar4ea21762006-07-03 00:24:53 -070078 INIT_LIST_HEAD(&sem->wait_list);
79}
80
81EXPORT_SYMBOL(__init_rwsem);
82
Michel Lespinassee2d57f72013-05-07 06:45:49 -070083enum rwsem_waiter_type {
84 RWSEM_WAITING_FOR_WRITE,
85 RWSEM_WAITING_FOR_READ
86};
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088struct rwsem_waiter {
89 struct list_head list;
90 struct task_struct *task;
Michel Lespinassee2d57f72013-05-07 06:45:49 -070091 enum rwsem_waiter_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
Michel Lespinassefe6e6742013-05-07 06:45:59 -070094enum rwsem_wake_type {
95 RWSEM_WAKE_ANY, /* Wake whatever's at head of wait list */
96 RWSEM_WAKE_READERS, /* Wake readers only */
97 RWSEM_WAKE_READ_OWNED /* Waker thread holds the read lock */
98};
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
101 * handle the lock release when processes blocked on it that can now run
102 * - if we come here from up_xxxx(), then:
103 * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
104 * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700105 * - there must be someone on the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * - the spinlock must be held by the caller
107 * - woken process blocks are discarded from the list after having task zeroed
108 * - writers are only woken if downgrading is false
109 */
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700110static struct rw_semaphore *
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700111__rwsem_do_wake(struct rw_semaphore *sem, enum rwsem_wake_type wake_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct rwsem_waiter *waiter;
114 struct task_struct *tsk;
115 struct list_head *next;
Davidlohr Buesob5f54182013-05-07 06:46:02 -0700116 long oldcount, woken, loop, adjustment;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700118 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700119 if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700120 if (wake_type == RWSEM_WAKE_ANY)
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700121 /* Wake writer at the front of the queue, but do not
122 * grant it the lock yet as we want other writers
123 * to be able to steal it. Readers, on the other hand,
124 * will block as they will notice the queued writer.
125 */
126 wake_up_process(waiter->task);
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700127 goto out;
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700128 }
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700129
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700130 /* Writers might steal the lock before we grant it to the next reader.
131 * We prefer to do the first reader grant before counting readers
132 * so we can bail out early if a writer stole the lock.
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700133 */
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700134 adjustment = 0;
135 if (wake_type != RWSEM_WAKE_READ_OWNED) {
136 adjustment = RWSEM_ACTIVE_READ_BIAS;
137 try_reader_grant:
138 oldcount = rwsem_atomic_update(adjustment, sem) - adjustment;
139 if (unlikely(oldcount < RWSEM_WAITING_BIAS)) {
140 /* A writer stole the lock. Undo our reader grant. */
141 if (rwsem_atomic_update(-adjustment, sem) &
142 RWSEM_ACTIVE_MASK)
143 goto out;
144 /* Last active locker left. Retry waking readers. */
145 goto try_reader_grant;
146 }
147 }
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700148
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700149 /* Grant an infinite number of read locks to the readers at the front
150 * of the queue. Note we increment the 'active part' of the count by
151 * the number of readers before waking any processes up.
152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 woken = 0;
154 do {
155 woken++;
156
157 if (waiter->list.next == &sem->wait_list)
158 break;
159
160 waiter = list_entry(waiter->list.next,
161 struct rwsem_waiter, list);
162
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700163 } while (waiter->type != RWSEM_WAITING_FOR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700165 adjustment = woken * RWSEM_ACTIVE_READ_BIAS - adjustment;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700166 if (waiter->type != RWSEM_WAITING_FOR_WRITE)
Michel Lespinassefd41b332010-08-09 17:21:18 -0700167 /* hit end of list above */
168 adjustment -= RWSEM_WAITING_BIAS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700170 if (adjustment)
171 rwsem_atomic_add(adjustment, sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 next = sem->wait_list.next;
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700174 loop = woken;
175 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 waiter = list_entry(next, struct rwsem_waiter, list);
177 next = waiter->list.next;
178 tsk = waiter->task;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700179 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 waiter->task = NULL;
181 wake_up_process(tsk);
182 put_task_struct(tsk);
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700183 } while (--loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 sem->wait_list.next = next;
186 next->prev = &sem->wait_list;
187
188 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return sem;
Alex Shice6711f2013-02-05 21:11:55 +0800190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
Michel Lespinasse1e782772013-05-07 06:45:51 -0700193 * wait for the read lock to be granted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100195__visible
Michel Lespinasse1e782772013-05-07 06:45:51 -0700196struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Davidlohr Buesob5f54182013-05-07 06:46:02 -0700198 long count, adjustment = -RWSEM_ACTIVE_READ_BIAS;
Michel Lespinassea8618a02010-08-09 17:21:20 -0700199 struct rwsem_waiter waiter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* set up my own style of waitqueue */
Michel Lespinassea8618a02010-08-09 17:21:20 -0700203 waiter.task = tsk;
Michel Lespinasseda169222013-05-07 06:45:52 -0700204 waiter.type = RWSEM_WAITING_FOR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 get_task_struct(tsk);
206
Michel Lespinassef7dd1ce2013-05-07 06:45:50 -0700207 raw_spin_lock_irq(&sem->wait_lock);
Michel Lespinassefd41b332010-08-09 17:21:18 -0700208 if (list_empty(&sem->wait_list))
209 adjustment += RWSEM_WAITING_BIAS;
Michel Lespinassea8618a02010-08-09 17:21:20 -0700210 list_add_tail(&waiter.list, &sem->wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700212 /* we're now waiting on the lock, but no longer actively locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 count = rwsem_atomic_update(adjustment, sem);
214
Michel Lespinasse25c39322013-05-07 06:46:00 -0700215 /* If there are no active locks, wake the front queued process(es).
216 *
217 * If there are no writers and we are first in the queue,
218 * wake our own waiter to join the existing active readers !
219 */
220 if (count == RWSEM_WAITING_BIAS ||
221 (count > RWSEM_WAITING_BIAS &&
222 adjustment != -RWSEM_ACTIVE_READ_BIAS))
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700223 sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100225 raw_spin_unlock_irq(&sem->wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* wait to be given the lock */
Michel Lespinassef7dd1ce2013-05-07 06:45:50 -0700228 while (true) {
229 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinassea8618a02010-08-09 17:21:20 -0700230 if (!waiter.task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 break;
232 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 tsk->state = TASK_RUNNING;
236
237 return sem;
238}
239
240/*
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700241 * wait until we successfully acquire the write lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100243__visible
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100244struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Davidlohr Buesob5f54182013-05-07 06:46:02 -0700246 long count, adjustment = -RWSEM_ACTIVE_WRITE_BIAS;
Michel Lespinasse1e782772013-05-07 06:45:51 -0700247 struct rwsem_waiter waiter;
248 struct task_struct *tsk = current;
Michel Lespinasse1e782772013-05-07 06:45:51 -0700249
250 /* set up my own style of waitqueue */
251 waiter.task = tsk;
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700252 waiter.type = RWSEM_WAITING_FOR_WRITE;
Michel Lespinasse1e782772013-05-07 06:45:51 -0700253
254 raw_spin_lock_irq(&sem->wait_lock);
255 if (list_empty(&sem->wait_list))
256 adjustment += RWSEM_WAITING_BIAS;
257 list_add_tail(&waiter.list, &sem->wait_list);
258
259 /* we're now waiting on the lock, but no longer actively locking */
260 count = rwsem_atomic_update(adjustment, sem);
261
Michel Lespinasseed00f642013-05-07 06:45:54 -0700262 /* If there were already threads queued before us and there are no
263 * active writers, the lock must be read owned; so we try to wake
264 * any read locks that were queued ahead of us. */
265 if (count > RWSEM_WAITING_BIAS &&
266 adjustment == -RWSEM_ACTIVE_WRITE_BIAS)
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700267 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READERS);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700268
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700269 /* wait until we successfully acquire the lock */
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700270 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700271 while (true) {
Michel Lespinasse9b0fc9c2013-05-07 06:45:57 -0700272 if (!(count & RWSEM_ACTIVE_MASK)) {
273 /* Try acquiring the write lock. */
274 count = RWSEM_ACTIVE_WRITE_BIAS;
275 if (!list_is_singular(&sem->wait_list))
276 count += RWSEM_WAITING_BIAS;
Davidlohr Bueso9607a852013-05-07 15:39:03 -0700277
278 if (sem->count == RWSEM_WAITING_BIAS &&
279 cmpxchg(&sem->count, RWSEM_WAITING_BIAS, count) ==
Michel Lespinasse5ede9722013-05-07 06:45:55 -0700280 RWSEM_WAITING_BIAS)
Michel Lespinasse9b0fc9c2013-05-07 06:45:57 -0700281 break;
282 }
Michel Lespinasse1e782772013-05-07 06:45:51 -0700283
Michel Lespinasse1e782772013-05-07 06:45:51 -0700284 raw_spin_unlock_irq(&sem->wait_lock);
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700285
286 /* Block until there are no active lockers. */
287 do {
288 schedule();
289 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinasse9b0fc9c2013-05-07 06:45:57 -0700290 } while ((count = sem->count) & RWSEM_ACTIVE_MASK);
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700291
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700292 raw_spin_lock_irq(&sem->wait_lock);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700293 }
294
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700295 list_del(&waiter.list);
296 raw_spin_unlock_irq(&sem->wait_lock);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700297 tsk->state = TASK_RUNNING;
298
299 return sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302/*
303 * handle waking up a waiter on the semaphore
304 * - up_read/up_write has decremented the active part of count if we come here
305 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100306__visible
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100307struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 unsigned long flags;
310
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100311 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /* do nothing if list empty */
314 if (!list_empty(&sem->wait_list))
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700315 sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100317 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return sem;
320}
321
322/*
323 * downgrade a write lock into a read lock
324 * - caller incremented waiting part of count and discovered it still negative
325 * - just wake up any readers at the front of the queue
326 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100327__visible
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100328struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 unsigned long flags;
331
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100332 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* do nothing if list empty */
335 if (!list_empty(&sem->wait_list))
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700336 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100338 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return sem;
341}
342
343EXPORT_SYMBOL(rwsem_down_read_failed);
344EXPORT_SYMBOL(rwsem_down_write_failed);
345EXPORT_SYMBOL(rwsem_wake);
346EXPORT_SYMBOL(rwsem_downgrade_wake);