blob: dacc32142fccaec5222ba82c01801ed7768a819f [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>
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -07008 *
9 * Optimistic spinning by Tim Chen <tim.c.chen@intel.com>
10 * and Davidlohr Bueso <davidlohr@hp.com>. Based on mutexes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12#include <linux/rwsem.h>
13#include <linux/sched.h>
14#include <linux/init.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050015#include <linux/export.h>
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070016#include <linux/sched/rt.h>
17
18#include "mcs_spinlock.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Ingo Molnar4ea21762006-07-03 00:24:53 -070020/*
Tim Chen3cf2f342014-05-02 12:53:57 -070021 * Guide to the rw_semaphore's count field for common values.
22 * (32-bit case illustrated, similar for 64-bit)
23 *
24 * 0x0000000X (1) X readers active or attempting lock, no writer waiting
25 * X = #active_readers + #readers attempting to lock
26 * (X*ACTIVE_BIAS)
27 *
28 * 0x00000000 rwsem is unlocked, and no one is waiting for the lock or
29 * attempting to read lock or write lock.
30 *
31 * 0xffff000X (1) X readers active or attempting lock, with waiters for lock
32 * X = #active readers + # readers attempting lock
33 * (X*ACTIVE_BIAS + WAITING_BIAS)
34 * (2) 1 writer attempting lock, no waiters for lock
35 * X-1 = #active readers + #readers attempting lock
36 * ((X-1)*ACTIVE_BIAS + ACTIVE_WRITE_BIAS)
37 * (3) 1 writer active, no waiters for lock
38 * X-1 = #active readers + #readers attempting lock
39 * ((X-1)*ACTIVE_BIAS + ACTIVE_WRITE_BIAS)
40 *
41 * 0xffff0001 (1) 1 reader active or attempting lock, waiters for lock
42 * (WAITING_BIAS + ACTIVE_BIAS)
43 * (2) 1 writer active or attempting lock, no waiters for lock
44 * (ACTIVE_WRITE_BIAS)
45 *
46 * 0xffff0000 (1) There are writers or readers queued but none active
47 * or in the process of attempting lock.
48 * (WAITING_BIAS)
49 * Note: writer can attempt to steal lock for this count by adding
50 * ACTIVE_WRITE_BIAS in cmpxchg and checking the old count
51 *
52 * 0xfffe0001 (1) 1 writer active, or attempting lock. Waiters on queue.
53 * (ACTIVE_WRITE_BIAS + WAITING_BIAS)
54 *
55 * Note: Readers attempt to lock by adding ACTIVE_BIAS in down_read and checking
56 * the count becomes more than 0 for successful lock acquisition,
57 * i.e. the case where there are only readers or nobody has lock.
58 * (1st and 2nd case above).
59 *
60 * Writers attempt to lock by adding ACTIVE_WRITE_BIAS in down_write and
61 * checking the count becomes ACTIVE_WRITE_BIAS for successful lock
62 * acquisition (i.e. nobody else has lock or attempts lock). If
63 * unsuccessful, in rwsem_down_write_failed, we'll check to see if there
64 * are only waiters but none active (5th case above), and attempt to
65 * steal the lock.
66 *
67 */
68
69/*
Ingo Molnar4ea21762006-07-03 00:24:53 -070070 * Initialize an rwsem:
71 */
72void __init_rwsem(struct rw_semaphore *sem, const char *name,
73 struct lock_class_key *key)
74{
75#ifdef CONFIG_DEBUG_LOCK_ALLOC
76 /*
77 * Make sure we are not reinitializing a held semaphore:
78 */
79 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -040080 lockdep_init_map(&sem->dep_map, name, key, 0);
Ingo Molnar4ea21762006-07-03 00:24:53 -070081#endif
82 sem->count = RWSEM_UNLOCKED_VALUE;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010083 raw_spin_lock_init(&sem->wait_lock);
Ingo Molnar4ea21762006-07-03 00:24:53 -070084 INIT_LIST_HEAD(&sem->wait_list);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070085#ifdef CONFIG_SMP
86 sem->owner = NULL;
87 sem->osq = NULL;
88#endif
Ingo Molnar4ea21762006-07-03 00:24:53 -070089}
90
91EXPORT_SYMBOL(__init_rwsem);
92
Michel Lespinassee2d57f72013-05-07 06:45:49 -070093enum rwsem_waiter_type {
94 RWSEM_WAITING_FOR_WRITE,
95 RWSEM_WAITING_FOR_READ
96};
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct rwsem_waiter {
99 struct list_head list;
100 struct task_struct *task;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700101 enum rwsem_waiter_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700104enum rwsem_wake_type {
105 RWSEM_WAKE_ANY, /* Wake whatever's at head of wait list */
106 RWSEM_WAKE_READERS, /* Wake readers only */
107 RWSEM_WAKE_READ_OWNED /* Waker thread holds the read lock */
108};
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * handle the lock release when processes blocked on it that can now run
112 * - if we come here from up_xxxx(), then:
113 * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
114 * - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700115 * - there must be someone on the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * - the spinlock must be held by the caller
117 * - woken process blocks are discarded from the list after having task zeroed
118 * - writers are only woken if downgrading is false
119 */
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700120static struct rw_semaphore *
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700121__rwsem_do_wake(struct rw_semaphore *sem, enum rwsem_wake_type wake_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 struct rwsem_waiter *waiter;
124 struct task_struct *tsk;
125 struct list_head *next;
Davidlohr Buesob5f54182013-05-07 06:46:02 -0700126 long oldcount, woken, loop, adjustment;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700128 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700129 if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700130 if (wake_type == RWSEM_WAKE_ANY)
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700131 /* Wake writer at the front of the queue, but do not
132 * grant it the lock yet as we want other writers
133 * to be able to steal it. Readers, on the other hand,
134 * will block as they will notice the queued writer.
135 */
136 wake_up_process(waiter->task);
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700137 goto out;
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700138 }
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700139
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700140 /* Writers might steal the lock before we grant it to the next reader.
141 * We prefer to do the first reader grant before counting readers
142 * so we can bail out early if a writer stole the lock.
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700143 */
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700144 adjustment = 0;
145 if (wake_type != RWSEM_WAKE_READ_OWNED) {
146 adjustment = RWSEM_ACTIVE_READ_BIAS;
147 try_reader_grant:
148 oldcount = rwsem_atomic_update(adjustment, sem) - adjustment;
149 if (unlikely(oldcount < RWSEM_WAITING_BIAS)) {
150 /* A writer stole the lock. Undo our reader grant. */
151 if (rwsem_atomic_update(-adjustment, sem) &
152 RWSEM_ACTIVE_MASK)
153 goto out;
154 /* Last active locker left. Retry waking readers. */
155 goto try_reader_grant;
156 }
157 }
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700158
Michel Lespinasse345af7b2010-08-09 17:21:15 -0700159 /* Grant an infinite number of read locks to the readers at the front
160 * of the queue. Note we increment the 'active part' of the count by
161 * the number of readers before waking any processes up.
162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 woken = 0;
164 do {
165 woken++;
166
167 if (waiter->list.next == &sem->wait_list)
168 break;
169
170 waiter = list_entry(waiter->list.next,
171 struct rwsem_waiter, list);
172
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700173 } while (waiter->type != RWSEM_WAITING_FOR_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700175 adjustment = woken * RWSEM_ACTIVE_READ_BIAS - adjustment;
Michel Lespinassee2d57f72013-05-07 06:45:49 -0700176 if (waiter->type != RWSEM_WAITING_FOR_WRITE)
Michel Lespinassefd41b332010-08-09 17:21:18 -0700177 /* hit end of list above */
178 adjustment -= RWSEM_WAITING_BIAS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700180 if (adjustment)
181 rwsem_atomic_add(adjustment, sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 next = sem->wait_list.next;
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700184 loop = woken;
185 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 waiter = list_entry(next, struct rwsem_waiter, list);
187 next = waiter->list.next;
188 tsk = waiter->task;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700189 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 waiter->task = NULL;
191 wake_up_process(tsk);
192 put_task_struct(tsk);
Michel Lespinasse8cf53222013-05-07 06:45:58 -0700193 } while (--loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 sem->wait_list.next = next;
196 next->prev = &sem->wait_list;
197
198 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return sem;
Alex Shice6711f2013-02-05 21:11:55 +0800200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700203 * Wait for the read lock to be granted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100205__visible
Michel Lespinasse1e782772013-05-07 06:45:51 -0700206struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Davidlohr Buesob5f54182013-05-07 06:46:02 -0700208 long count, adjustment = -RWSEM_ACTIVE_READ_BIAS;
Michel Lespinassea8618a02010-08-09 17:21:20 -0700209 struct rwsem_waiter waiter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* set up my own style of waitqueue */
Michel Lespinassea8618a02010-08-09 17:21:20 -0700213 waiter.task = tsk;
Michel Lespinasseda169222013-05-07 06:45:52 -0700214 waiter.type = RWSEM_WAITING_FOR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 get_task_struct(tsk);
216
Michel Lespinassef7dd1ce2013-05-07 06:45:50 -0700217 raw_spin_lock_irq(&sem->wait_lock);
Michel Lespinassefd41b332010-08-09 17:21:18 -0700218 if (list_empty(&sem->wait_list))
219 adjustment += RWSEM_WAITING_BIAS;
Michel Lespinassea8618a02010-08-09 17:21:20 -0700220 list_add_tail(&waiter.list, &sem->wait_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700222 /* we're now waiting on the lock, but no longer actively locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 count = rwsem_atomic_update(adjustment, sem);
224
Michel Lespinasse25c39322013-05-07 06:46:00 -0700225 /* If there are no active locks, wake the front queued process(es).
226 *
227 * If there are no writers and we are first in the queue,
228 * wake our own waiter to join the existing active readers !
229 */
230 if (count == RWSEM_WAITING_BIAS ||
231 (count > RWSEM_WAITING_BIAS &&
232 adjustment != -RWSEM_ACTIVE_READ_BIAS))
Michel Lespinassefe6e6742013-05-07 06:45:59 -0700233 sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100235 raw_spin_unlock_irq(&sem->wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /* wait to be given the lock */
Michel Lespinassef7dd1ce2013-05-07 06:45:50 -0700238 while (true) {
239 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Michel Lespinassea8618a02010-08-09 17:21:20 -0700240 if (!waiter.task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 break;
242 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
245 tsk->state = TASK_RUNNING;
246
247 return sem;
248}
249
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700250static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem)
251{
252 if (!(count & RWSEM_ACTIVE_MASK)) {
253 /* try acquiring the write lock */
254 if (sem->count == RWSEM_WAITING_BIAS &&
255 cmpxchg(&sem->count, RWSEM_WAITING_BIAS,
256 RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) {
257 if (!list_is_singular(&sem->wait_list))
258 rwsem_atomic_update(RWSEM_WAITING_BIAS, sem);
259 return true;
260 }
261 }
262 return false;
263}
264
265#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700267 * Try to acquire write lock before the writer has been put on wait queue.
268 */
269static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem)
270{
271 long old, count = ACCESS_ONCE(sem->count);
272
273 while (true) {
274 if (!(count == 0 || count == RWSEM_WAITING_BIAS))
275 return false;
276
277 old = cmpxchg(&sem->count, count, count + RWSEM_ACTIVE_WRITE_BIAS);
278 if (old == count)
279 return true;
280
281 count = old;
282 }
283}
284
285static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem)
286{
287 struct task_struct *owner;
288 bool on_cpu = true;
289
290 if (need_resched())
291 return 0;
292
293 rcu_read_lock();
294 owner = ACCESS_ONCE(sem->owner);
295 if (owner)
296 on_cpu = owner->on_cpu;
297 rcu_read_unlock();
298
299 /*
300 * If sem->owner is not set, the rwsem owner may have
301 * just acquired it and not set the owner yet or the rwsem
302 * has been released.
303 */
304 return on_cpu;
305}
306
307static inline bool owner_running(struct rw_semaphore *sem,
308 struct task_struct *owner)
309{
310 if (sem->owner != owner)
311 return false;
312
313 /*
314 * Ensure we emit the owner->on_cpu, dereference _after_ checking
315 * sem->owner still matches owner, if that fails, owner might
316 * point to free()d memory, if it still matches, the rcu_read_lock()
317 * ensures the memory stays valid.
318 */
319 barrier();
320
321 return owner->on_cpu;
322}
323
324static noinline
325bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner)
326{
327 rcu_read_lock();
328 while (owner_running(sem, owner)) {
329 if (need_resched())
330 break;
331
332 arch_mutex_cpu_relax();
333 }
334 rcu_read_unlock();
335
336 /*
337 * We break out the loop above on need_resched() or when the
338 * owner changed, which is a sign for heavy contention. Return
339 * success only when sem->owner is NULL.
340 */
341 return sem->owner == NULL;
342}
343
344static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
345{
346 struct task_struct *owner;
347 bool taken = false;
348
349 preempt_disable();
350
351 /* sem->wait_lock should not be held when doing optimistic spinning */
352 if (!rwsem_can_spin_on_owner(sem))
353 goto done;
354
355 if (!osq_lock(&sem->osq))
356 goto done;
357
358 while (true) {
359 owner = ACCESS_ONCE(sem->owner);
360 if (owner && !rwsem_spin_on_owner(sem, owner))
361 break;
362
363 /* wait_lock will be acquired if write_lock is obtained */
364 if (rwsem_try_write_lock_unqueued(sem)) {
365 taken = true;
366 break;
367 }
368
369 /*
370 * When there's no owner, we might have preempted between the
371 * owner acquiring the lock and setting the owner field. If
372 * we're an RT task that will live-lock because we won't let
373 * the owner complete.
374 */
375 if (!owner && (need_resched() || rt_task(current)))
376 break;
377
378 /*
379 * The cpu_relax() call is a compiler barrier which forces
380 * everything in this loop to be re-loaded. We don't need
381 * memory barriers as we'll eventually observe the right
382 * values at the cost of a few extra spins.
383 */
384 arch_mutex_cpu_relax();
385 }
386 osq_unlock(&sem->osq);
387done:
388 preempt_enable();
389 return taken;
390}
391
392#else
393static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
394{
395 return false;
396}
397#endif
398
399/*
400 * Wait until we successfully acquire the write lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100402__visible
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100403struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700405 long count;
406 bool waiting = true; /* any queued threads before us */
Michel Lespinasse1e782772013-05-07 06:45:51 -0700407 struct rwsem_waiter waiter;
Michel Lespinasse1e782772013-05-07 06:45:51 -0700408
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700409 /* undo write bias from down_write operation, stop active locking */
410 count = rwsem_atomic_update(-RWSEM_ACTIVE_WRITE_BIAS, sem);
411
412 /* do optimistic spinning and steal lock if possible */
413 if (rwsem_optimistic_spin(sem))
414 return sem;
415
416 /*
417 * Optimistic spinning failed, proceed to the slowpath
418 * and block until we can acquire the sem.
419 */
420 waiter.task = current;
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700421 waiter.type = RWSEM_WAITING_FOR_WRITE;
Michel Lespinasse1e782772013-05-07 06:45:51 -0700422
423 raw_spin_lock_irq(&sem->wait_lock);
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700424
425 /* account for this before adding a new element to the list */
Michel Lespinasse1e782772013-05-07 06:45:51 -0700426 if (list_empty(&sem->wait_list))
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700427 waiting = false;
428
Michel Lespinasse1e782772013-05-07 06:45:51 -0700429 list_add_tail(&waiter.list, &sem->wait_list);
430
431 /* we're now waiting on the lock, but no longer actively locking */
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700432 if (waiting) {
433 count = ACCESS_ONCE(sem->count);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700434
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700435 /*
Andrew Morton0cc3d012014-06-04 20:19:48 +0200436 * If there were already threads queued before us and there are
437 * no active writers, the lock must be read owned; so we try to
438 * wake any read locks that were queued ahead of us.
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700439 */
440 if (count > RWSEM_WAITING_BIAS)
441 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READERS);
442
443 } else
444 count = rwsem_atomic_update(RWSEM_WAITING_BIAS, sem);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700445
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700446 /* wait until we successfully acquire the lock */
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700447 set_current_state(TASK_UNINTERRUPTIBLE);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700448 while (true) {
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700449 if (rwsem_try_write_lock(count, sem))
450 break;
Michel Lespinasse1e782772013-05-07 06:45:51 -0700451 raw_spin_unlock_irq(&sem->wait_lock);
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700452
453 /* Block until there are no active lockers. */
454 do {
455 schedule();
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700456 set_current_state(TASK_UNINTERRUPTIBLE);
Michel Lespinasse9b0fc9c2013-05-07 06:45:57 -0700457 } while ((count = sem->count) & RWSEM_ACTIVE_MASK);
Michel Lespinassea7d2c572013-05-07 06:45:56 -0700458
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700459 raw_spin_lock_irq(&sem->wait_lock);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700460 }
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -0700461 __set_current_state(TASK_RUNNING);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700462
Michel Lespinasse023fe4f2013-05-07 06:45:53 -0700463 list_del(&waiter.list);
464 raw_spin_unlock_irq(&sem->wait_lock);
Michel Lespinasse1e782772013-05-07 06:45:51 -0700465
466 return sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/*
470 * handle waking up a waiter on the semaphore
471 * - up_read/up_write has decremented the active part of count if we come here
472 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100473__visible
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100474struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 unsigned long flags;
477
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100478 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 /* do nothing if list empty */
481 if (!list_empty(&sem->wait_list))
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700482 sem = __rwsem_do_wake(sem, RWSEM_WAKE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100484 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return sem;
487}
488
489/*
490 * downgrade a write lock into a read lock
491 * - caller incremented waiting part of count and discovered it still negative
492 * - just wake up any readers at the front of the queue
493 */
Andi Kleen3ebae4f2014-02-08 08:52:05 +0100494__visible
Thomas Gleixnerd1233752011-01-26 21:32:01 +0100495struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 unsigned long flags;
498
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100499 raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /* do nothing if list empty */
502 if (!list_empty(&sem->wait_list))
Michel Lespinasse70bdc6e2010-08-09 17:21:17 -0700503 sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +0100505 raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return sem;
508}
509
510EXPORT_SYMBOL(rwsem_down_read_failed);
511EXPORT_SYMBOL(rwsem_down_write_failed);
512EXPORT_SYMBOL(rwsem_wake);
513EXPORT_SYMBOL(rwsem_downgrade_wake);