2 * Fast Userspace Mutexes (which I call "Futexes!").
3 * (C) Rusty Russell, IBM 2002
5 * Generalized futexes, futex requeueing, misc fixes by Ingo Molnar
6 * (C) Copyright 2003 Red Hat Inc, All Rights Reserved
8 * Removed page pinning, fix privately mapped COW pages and other cleanups
9 * (C) Copyright 2003, 2004 Jamie Lokier
11 * Robust futex support started by Ingo Molnar
12 * (C) Copyright 2006 Red Hat Inc, All Rights Reserved
13 * Thanks to Thomas Gleixner for suggestions, analysis and fixes.
15 * PI-futex support started by Ingo Molnar and Thomas Gleixner
16 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
17 * Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
19 * PRIVATE futexes by Eric Dumazet
20 * Copyright (C) 2007 Eric Dumazet <dada1@cosmosbay.com>
22 * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
23 * enough at me, Linus for the original (flawed) idea, Matthew
24 * Kirkwood for proof-of-concept implementation.
26 * "The futexes are also cursed."
27 * "But they come in a choice of three flavours!"
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
43 #include <linux/slab.h>
44 #include <linux/poll.h>
46 #include <linux/file.h>
47 #include <linux/jhash.h>
48 #include <linux/init.h>
49 #include <linux/futex.h>
50 #include <linux/mount.h>
51 #include <linux/pagemap.h>
52 #include <linux/syscalls.h>
53 #include <linux/signal.h>
54 #include <linux/module.h>
55 #include <asm/futex.h>
57 #include "rtmutex_common.h"
59 #ifdef CONFIG_DEBUG_RT_MUTEXES
60 # include "rtmutex-debug.h"
65 #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
68 * Priority Inheritance state:
70 struct futex_pi_state
{
72 * list of 'owned' pi_state instances - these have to be
73 * cleaned up in do_exit() if the task exits prematurely:
75 struct list_head list
;
80 struct rt_mutex pi_mutex
;
82 struct task_struct
*owner
;
89 * We use this hashed waitqueue instead of a normal wait_queue_t, so
90 * we can wake only the relevant ones (hashed queues may be shared).
92 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
93 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
94 * The order of wakup is always to make the first condition true, then
95 * wake up q->waiters, then make the second condition true.
98 struct plist_node list
;
99 wait_queue_head_t waiters
;
101 /* Which hash list lock to use: */
102 spinlock_t
*lock_ptr
;
104 /* Key which the futex is hashed on: */
107 /* For fd, sigio sent using these: */
111 /* Optional priority inheritance state: */
112 struct futex_pi_state
*pi_state
;
113 struct task_struct
*task
;
116 * This waiter is used in case of requeue from a
117 * normal futex to a PI-futex
119 struct rt_mutex_waiter waiter
;
123 * Split the global futex_lock into every hash list lock.
125 struct futex_hash_bucket
{
127 struct plist_head chain
;
130 static struct futex_hash_bucket futex_queues
[1<<FUTEX_HASHBITS
];
132 /* Futex-fs vfsmount entry: */
133 static struct vfsmount
*futex_mnt
;
136 * We hash on the keys returned from get_futex_key (see below).
138 static struct futex_hash_bucket
*hash_futex(union futex_key
*key
)
140 u32 hash
= jhash2((u32
*)&key
->both
.word
,
141 (sizeof(key
->both
.word
)+sizeof(key
->both
.ptr
))/4,
143 return &futex_queues
[hash
& ((1 << FUTEX_HASHBITS
)-1)];
147 * Return 1 if two futex_keys are equal, 0 otherwise.
149 static inline int match_futex(union futex_key
*key1
, union futex_key
*key2
)
151 return (key1
->both
.word
== key2
->both
.word
152 && key1
->both
.ptr
== key2
->both
.ptr
153 && key1
->both
.offset
== key2
->both
.offset
);
157 * get_futex_key - Get parameters which are the keys for a futex.
158 * @uaddr: virtual address of the futex
159 * @shared: NULL for a PROCESS_PRIVATE futex,
160 * ¤t->mm->mmap_sem for a PROCESS_SHARED futex
161 * @key: address where result is stored.
163 * Returns a negative error code or 0
164 * The key words are stored in *key on success.
166 * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
167 * offset_within_page). For private mappings, it's (uaddr, current->mm).
168 * We can usually work out the index without swapping in the page.
170 * fshared is NULL for PROCESS_PRIVATE futexes
171 * For other futexes, it points to ¤t->mm->mmap_sem and
172 * caller must have taken the reader lock. but NOT any spinlocks.
174 int get_futex_key(u32 __user
*uaddr
, struct rw_semaphore
*fshared
,
175 union futex_key
*key
)
177 unsigned long address
= (unsigned long)uaddr
;
178 struct mm_struct
*mm
= current
->mm
;
179 struct vm_area_struct
*vma
;
184 * The futex address must be "naturally" aligned.
186 key
->both
.offset
= address
% PAGE_SIZE
;
187 if (unlikely((address
% sizeof(u32
)) != 0))
189 address
-= key
->both
.offset
;
192 * PROCESS_PRIVATE futexes are fast.
193 * As the mm cannot disappear under us and the 'key' only needs
194 * virtual address, we dont even have to find the underlying vma.
195 * Note : We do have to check 'uaddr' is a valid user address,
196 * but access_ok() should be faster than find_vma()
199 if (unlikely(!access_ok(VERIFY_WRITE
, uaddr
, sizeof(u32
))))
201 key
->private.mm
= mm
;
202 key
->private.address
= address
;
206 * The futex is hashed differently depending on whether
207 * it's in a shared or private mapping. So check vma first.
209 vma
= find_extend_vma(mm
, address
);
216 if (unlikely((vma
->vm_flags
& (VM_IO
|VM_READ
)) != VM_READ
))
217 return (vma
->vm_flags
& VM_IO
) ? -EPERM
: -EACCES
;
219 /* Save the user address in the ley */
223 * Private mappings are handled in a simple way.
225 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
226 * it's a read-only handle, it's expected that futexes attach to
227 * the object not the particular process. Therefore we use
228 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
229 * mappings of _writable_ handles.
231 if (likely(!(vma
->vm_flags
& VM_MAYSHARE
))) {
232 key
->both
.offset
|= FUT_OFF_MMSHARED
; /* reference taken on mm */
233 key
->private.mm
= mm
;
234 key
->private.address
= address
;
239 * Linear file mappings are also simple.
241 key
->shared
.inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
242 key
->both
.offset
|= FUT_OFF_INODE
; /* inode-based key. */
243 if (likely(!(vma
->vm_flags
& VM_NONLINEAR
))) {
244 key
->shared
.pgoff
= (((address
- vma
->vm_start
) >> PAGE_SHIFT
)
250 * We could walk the page table to read the non-linear
251 * pte, and get the page index without fetching the page
252 * from swap. But that's a lot of code to duplicate here
253 * for a rare case, so we simply fetch the page.
255 err
= get_user_pages(current
, mm
, address
, 1, 0, 0, &page
, NULL
);
258 page
->index
<< (PAGE_CACHE_SHIFT
- PAGE_SHIFT
);
264 EXPORT_SYMBOL_GPL(get_futex_key
);
267 * Take a reference to the resource addressed by a key.
268 * Can be called while holding spinlocks.
271 inline void get_futex_key_refs(union futex_key
*key
)
273 if (key
->both
.ptr
== 0)
275 switch (key
->both
.offset
& (FUT_OFF_INODE
|FUT_OFF_MMSHARED
)) {
277 atomic_inc(&key
->shared
.inode
->i_count
);
279 case FUT_OFF_MMSHARED
:
280 atomic_inc(&key
->private.mm
->mm_count
);
284 EXPORT_SYMBOL_GPL(get_futex_key_refs
);
287 * Drop a reference to the resource addressed by a key.
288 * The hash bucket spinlock must not be held.
290 void drop_futex_key_refs(union futex_key
*key
)
292 if (key
->both
.ptr
== 0)
294 switch (key
->both
.offset
& (FUT_OFF_INODE
|FUT_OFF_MMSHARED
)) {
296 iput(key
->shared
.inode
);
298 case FUT_OFF_MMSHARED
:
299 mmdrop(key
->private.mm
);
303 EXPORT_SYMBOL_GPL(drop_futex_key_refs
);
305 static inline int get_futex_value_locked(u32
*dest
, u32 __user
*from
)
310 ret
= __copy_from_user_inatomic(dest
, from
, sizeof(u32
));
313 return ret
? -EFAULT
: 0;
318 * if fshared is non NULL, current->mm->mmap_sem is already held
320 static int futex_handle_fault(unsigned long address
,
321 struct rw_semaphore
*fshared
, int attempt
)
323 struct vm_area_struct
* vma
;
324 struct mm_struct
*mm
= current
->mm
;
331 down_read(&mm
->mmap_sem
);
332 vma
= find_vma(mm
, address
);
333 if (vma
&& address
>= vma
->vm_start
&&
334 (vma
->vm_flags
& VM_WRITE
)) {
335 switch (handle_mm_fault(mm
, vma
, address
, 1)) {
347 up_read(&mm
->mmap_sem
);
354 static int refill_pi_state_cache(void)
356 struct futex_pi_state
*pi_state
;
358 if (likely(current
->pi_state_cache
))
361 pi_state
= kzalloc(sizeof(*pi_state
), GFP_KERNEL
);
366 INIT_LIST_HEAD(&pi_state
->list
);
367 /* pi_mutex gets initialized later */
368 pi_state
->owner
= NULL
;
369 atomic_set(&pi_state
->refcount
, 1);
371 current
->pi_state_cache
= pi_state
;
376 static struct futex_pi_state
* alloc_pi_state(void)
378 struct futex_pi_state
*pi_state
= current
->pi_state_cache
;
381 current
->pi_state_cache
= NULL
;
386 static void free_pi_state(struct futex_pi_state
*pi_state
)
388 if (!atomic_dec_and_test(&pi_state
->refcount
))
392 * If pi_state->owner is NULL, the owner is most probably dying
393 * and has cleaned up the pi_state already
395 if (pi_state
->owner
) {
396 spin_lock_irq(&pi_state
->owner
->pi_lock
);
397 list_del_init(&pi_state
->list
);
398 spin_unlock_irq(&pi_state
->owner
->pi_lock
);
400 rt_mutex_proxy_unlock(&pi_state
->pi_mutex
, pi_state
->owner
);
403 if (current
->pi_state_cache
)
407 * pi_state->list is already empty.
408 * clear pi_state->owner.
409 * refcount is at 0 - put it back to 1.
411 pi_state
->owner
= NULL
;
412 atomic_set(&pi_state
->refcount
, 1);
413 current
->pi_state_cache
= pi_state
;
418 * Look up the task based on what TID userspace gave us.
421 static struct task_struct
* futex_find_get_task(pid_t pid
)
423 struct task_struct
*p
;
426 p
= find_task_by_pid(pid
);
429 if ((current
->euid
!= p
->euid
) && (current
->euid
!= p
->uid
)) {
441 * This task is holding PI mutexes at exit time => bad.
442 * Kernel cleans up PI-state, but userspace is likely hosed.
443 * (Robust-futex cleanup is separate and might save the day for userspace.)
445 void exit_pi_state_list(struct task_struct
*curr
)
447 struct list_head
*next
, *head
= &curr
->pi_state_list
;
448 struct futex_pi_state
*pi_state
;
449 struct futex_hash_bucket
*hb
;
453 * We are a ZOMBIE and nobody can enqueue itself on
454 * pi_state_list anymore, but we have to be careful
455 * versus waiters unqueueing themselves:
457 spin_lock_irq(&curr
->pi_lock
);
458 while (!list_empty(head
)) {
461 pi_state
= list_entry(next
, struct futex_pi_state
, list
);
463 hb
= hash_futex(&key
);
464 spin_unlock_irq(&curr
->pi_lock
);
466 spin_lock(&hb
->lock
);
468 spin_lock_irq(&curr
->pi_lock
);
470 * We dropped the pi-lock, so re-check whether this
471 * task still owns the PI-state:
473 if (head
->next
!= next
) {
474 spin_unlock(&hb
->lock
);
478 WARN_ON(pi_state
->owner
!= curr
);
479 WARN_ON(list_empty(&pi_state
->list
));
480 list_del_init(&pi_state
->list
);
481 pi_state
->owner
= NULL
;
482 spin_unlock_irq(&curr
->pi_lock
);
484 rt_mutex_unlock(&pi_state
->pi_mutex
);
486 spin_unlock(&hb
->lock
);
488 spin_lock_irq(&curr
->pi_lock
);
490 spin_unlock_irq(&curr
->pi_lock
);
494 lookup_pi_state(u32 uval
, struct futex_hash_bucket
*hb
,
495 union futex_key
*key
, struct futex_pi_state
**ps
)
497 struct futex_pi_state
*pi_state
= NULL
;
498 struct futex_q
*this, *next
;
499 struct plist_head
*head
;
500 struct task_struct
*p
;
501 pid_t pid
= uval
& FUTEX_TID_MASK
;
505 plist_for_each_entry_safe(this, next
, head
, list
) {
506 if (match_futex(&this->key
, key
)) {
508 * Another waiter already exists - bump up
509 * the refcount and return its pi_state:
511 pi_state
= this->pi_state
;
513 * Userspace might have messed up non PI and PI futexes
515 if (unlikely(!pi_state
))
518 WARN_ON(!atomic_read(&pi_state
->refcount
));
519 WARN_ON(pid
&& pi_state
->owner
&&
520 pi_state
->owner
->pid
!= pid
);
522 atomic_inc(&pi_state
->refcount
);
530 * We are the first waiter - try to look up the real owner and attach
531 * the new pi_state to it, but bail out when TID = 0
535 p
= futex_find_get_task(pid
);
540 * We need to look at the task state flags to figure out,
541 * whether the task is exiting. To protect against the do_exit
542 * change of the task flags, we do this protected by
545 spin_lock_irq(&p
->pi_lock
);
546 if (unlikely(p
->flags
& PF_EXITING
)) {
548 * The task is on the way out. When PF_EXITPIDONE is
549 * set, we know that the task has finished the
552 int ret
= (p
->flags
& PF_EXITPIDONE
) ? -ESRCH
: -EAGAIN
;
554 spin_unlock_irq(&p
->pi_lock
);
559 pi_state
= alloc_pi_state();
562 * Initialize the pi_mutex in locked state and make 'p'
565 rt_mutex_init_proxy_locked(&pi_state
->pi_mutex
, p
);
567 /* Store the key for possible exit cleanups: */
568 pi_state
->key
= *key
;
570 WARN_ON(!list_empty(&pi_state
->list
));
571 list_add(&pi_state
->list
, &p
->pi_state_list
);
573 spin_unlock_irq(&p
->pi_lock
);
583 * The hash bucket lock must be held when this is called.
584 * Afterwards, the futex_q must not be accessed.
586 static void wake_futex(struct futex_q
*q
)
588 plist_del(&q
->list
, &q
->list
.plist
);
590 send_sigio(&q
->filp
->f_owner
, q
->fd
, POLL_IN
);
592 * The lock in wake_up_all() is a crucial memory barrier after the
593 * plist_del() and also before assigning to q->lock_ptr.
595 wake_up_all(&q
->waiters
);
597 * The waiting task can free the futex_q as soon as this is written,
598 * without taking any locks. This must come last.
600 * A memory barrier is required here to prevent the following store
601 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
602 * at the end of wake_up_all() does not prevent this store from
609 static int wake_futex_pi(u32 __user
*uaddr
, u32 uval
, struct futex_q
*this)
611 struct task_struct
*new_owner
;
612 struct futex_pi_state
*pi_state
= this->pi_state
;
618 spin_lock(&pi_state
->pi_mutex
.wait_lock
);
619 new_owner
= rt_mutex_next_owner(&pi_state
->pi_mutex
);
622 * This happens when we have stolen the lock and the original
623 * pending owner did not enqueue itself back on the rt_mutex.
624 * Thats not a tragedy. We know that way, that a lock waiter
625 * is on the fly. We make the futex_q waiter the pending owner.
628 new_owner
= this->task
;
631 * We pass it to the next owner. (The WAITERS bit is always
632 * kept enabled while there is PI state around. We must also
633 * preserve the owner died bit.)
635 if (!(uval
& FUTEX_OWNER_DIED
)) {
638 newval
= FUTEX_WAITERS
| new_owner
->pid
;
639 /* Keep the FUTEX_WAITER_REQUEUED flag if it was set */
640 newval
|= (uval
& FUTEX_WAITER_REQUEUED
);
643 curval
= futex_atomic_cmpxchg_inatomic(uaddr
, uval
, newval
);
646 if (curval
== -EFAULT
)
651 spin_unlock(&pi_state
->pi_mutex
.wait_lock
);
656 spin_lock_irq(&pi_state
->owner
->pi_lock
);
657 WARN_ON(list_empty(&pi_state
->list
));
658 list_del_init(&pi_state
->list
);
659 spin_unlock_irq(&pi_state
->owner
->pi_lock
);
661 spin_lock_irq(&new_owner
->pi_lock
);
662 WARN_ON(!list_empty(&pi_state
->list
));
663 list_add(&pi_state
->list
, &new_owner
->pi_state_list
);
664 pi_state
->owner
= new_owner
;
665 spin_unlock_irq(&new_owner
->pi_lock
);
667 spin_unlock(&pi_state
->pi_mutex
.wait_lock
);
668 rt_mutex_unlock(&pi_state
->pi_mutex
);
673 static int unlock_futex_pi(u32 __user
*uaddr
, u32 uval
)
678 * There is no waiter, so we unlock the futex. The owner died
679 * bit has not to be preserved here. We are the owner:
682 oldval
= futex_atomic_cmpxchg_inatomic(uaddr
, uval
, 0);
685 if (oldval
== -EFAULT
)
694 * Express the locking dependencies for lockdep:
697 double_lock_hb(struct futex_hash_bucket
*hb1
, struct futex_hash_bucket
*hb2
)
700 spin_lock(&hb1
->lock
);
702 spin_lock_nested(&hb2
->lock
, SINGLE_DEPTH_NESTING
);
703 } else { /* hb1 > hb2 */
704 spin_lock(&hb2
->lock
);
705 spin_lock_nested(&hb1
->lock
, SINGLE_DEPTH_NESTING
);
710 * Wake up all waiters hashed on the physical page that is mapped
711 * to this virtual address:
713 static int futex_wake(u32 __user
*uaddr
, struct rw_semaphore
*fshared
,
716 struct futex_hash_bucket
*hb
;
717 struct futex_q
*this, *next
;
718 struct plist_head
*head
;
725 ret
= get_futex_key(uaddr
, fshared
, &key
);
726 if (unlikely(ret
!= 0))
729 hb
= hash_futex(&key
);
730 spin_lock(&hb
->lock
);
733 plist_for_each_entry_safe(this, next
, head
, list
) {
734 if (match_futex (&this->key
, &key
)) {
735 if (this->pi_state
) {
740 if (++ret
>= nr_wake
)
745 spin_unlock(&hb
->lock
);
753 * Called from futex_requeue_pi.
754 * Set FUTEX_WAITERS and FUTEX_WAITER_REQUEUED flags on the
755 * PI-futex value; search its associated pi_state if an owner exist
756 * or create a new one without owner.
759 lookup_pi_state_for_requeue(u32 __user
*uaddr
, struct futex_hash_bucket
*hb
,
760 union futex_key
*key
,
761 struct futex_pi_state
**pi_state
)
763 u32 curval
, uval
, newval
;
767 * We can't handle a fault cleanly because we can't
768 * release the locks here. Simply return the fault.
770 if (get_futex_value_locked(&curval
, uaddr
))
773 /* set the flags FUTEX_WAITERS and FUTEX_WAITER_REQUEUED */
774 if ((curval
& (FUTEX_WAITERS
| FUTEX_WAITER_REQUEUED
))
775 != (FUTEX_WAITERS
| FUTEX_WAITER_REQUEUED
)) {
777 * No waiters yet, we prepare the futex to have some waiters.
781 newval
= uval
| FUTEX_WAITERS
| FUTEX_WAITER_REQUEUED
;
784 curval
= futex_atomic_cmpxchg_inatomic(uaddr
, uval
, newval
);
787 if (unlikely(curval
== -EFAULT
))
789 if (unlikely(curval
!= uval
))
793 if (!(curval
& FUTEX_TID_MASK
)
794 || lookup_pi_state(curval
, hb
, key
, pi_state
)) {
795 /* the futex has no owner (yet) or the lookup failed:
796 allocate one pi_state without owner */
798 *pi_state
= alloc_pi_state();
800 /* Already stores the key: */
801 (*pi_state
)->key
= *key
;
803 /* init the mutex without owner */
804 __rt_mutex_init(&(*pi_state
)->pi_mutex
, NULL
);
811 * Keep the first nr_wake waiter from futex1, wake up one,
812 * and requeue the next nr_requeue waiters following hashed on
813 * one physical page to another physical page (PI-futex uaddr2)
815 static int futex_requeue_pi(u32 __user
*uaddr1
,
816 struct rw_semaphore
*fshared
,
818 int nr_wake
, int nr_requeue
, u32
*cmpval
)
820 union futex_key key1
, key2
;
821 struct futex_hash_bucket
*hb1
, *hb2
;
822 struct plist_head
*head1
;
823 struct futex_q
*this, *next
;
824 struct futex_pi_state
*pi_state2
= NULL
;
825 struct rt_mutex_waiter
*waiter
, *top_waiter
= NULL
;
826 struct rt_mutex
*lock2
= NULL
;
827 int ret
, drop_count
= 0;
829 if (refill_pi_state_cache())
834 * First take all the futex related locks:
839 ret
= get_futex_key(uaddr1
, fshared
, &key1
);
840 if (unlikely(ret
!= 0))
842 ret
= get_futex_key(uaddr2
, fshared
, &key2
);
843 if (unlikely(ret
!= 0))
846 hb1
= hash_futex(&key1
);
847 hb2
= hash_futex(&key2
);
849 double_lock_hb(hb1
, hb2
);
851 if (likely(cmpval
!= NULL
)) {
854 ret
= get_futex_value_locked(&curval
, uaddr1
);
857 spin_unlock(&hb1
->lock
);
859 spin_unlock(&hb2
->lock
);
862 * If we would have faulted, release mmap_sem, fault
863 * it in and start all over again.
868 ret
= get_user(curval
, uaddr1
);
875 if (curval
!= *cmpval
) {
882 plist_for_each_entry_safe(this, next
, head1
, list
) {
883 if (!match_futex (&this->key
, &key1
))
885 if (++ret
<= nr_wake
) {
889 * FIRST: get and set the pi_state
893 /* do this only the first time we requeue someone */
894 s
= lookup_pi_state_for_requeue(uaddr2
, hb2
,
901 lock2
= &pi_state2
->pi_mutex
;
902 spin_lock(&lock2
->wait_lock
);
904 /* Save the top waiter of the wait_list */
905 if (rt_mutex_has_waiters(lock2
))
906 top_waiter
= rt_mutex_top_waiter(lock2
);
908 atomic_inc(&pi_state2
->refcount
);
911 this->pi_state
= pi_state2
;
914 * SECOND: requeue futex_q to the correct hashbucket
918 * If key1 and key2 hash to the same bucket, no need to
921 if (likely(head1
!= &hb2
->chain
)) {
922 plist_del(&this->list
, &hb1
->chain
);
923 plist_add(&this->list
, &hb2
->chain
);
924 this->lock_ptr
= &hb2
->lock
;
925 #ifdef CONFIG_DEBUG_PI_LIST
926 this->list
.plist
.lock
= &hb2
->lock
;
930 get_futex_key_refs(&key2
);
935 * THIRD: queue it to lock2
937 spin_lock_irq(&this->task
->pi_lock
);
938 waiter
= &this->waiter
;
939 waiter
->task
= this->task
;
940 waiter
->lock
= lock2
;
941 plist_node_init(&waiter
->list_entry
, this->task
->prio
);
942 plist_node_init(&waiter
->pi_list_entry
, this->task
->prio
);
943 plist_add(&waiter
->list_entry
, &lock2
->wait_list
);
944 this->task
->pi_blocked_on
= waiter
;
945 spin_unlock_irq(&this->task
->pi_lock
);
947 if (ret
- nr_wake
>= nr_requeue
)
952 /* If we've requeued some tasks and the top_waiter of the rt_mutex
953 has changed, we must adjust the priority of the owner, if any */
955 struct task_struct
*owner
= rt_mutex_owner(lock2
);
957 (top_waiter
!= (waiter
= rt_mutex_top_waiter(lock2
)))) {
960 spin_lock_irq(&owner
->pi_lock
);
962 plist_del(&top_waiter
->pi_list_entry
, &owner
->pi_waiters
);
965 * There was no waiters before the requeue,
966 * the flag must be updated
968 mark_rt_mutex_waiters(lock2
);
970 plist_add(&waiter
->pi_list_entry
, &owner
->pi_waiters
);
971 __rt_mutex_adjust_prio(owner
);
972 if (owner
->pi_blocked_on
) {
974 get_task_struct(owner
);
977 spin_unlock_irq(&owner
->pi_lock
);
978 spin_unlock(&lock2
->wait_lock
);
981 rt_mutex_adjust_prio_chain(owner
, 0, lock2
, NULL
,
984 /* No owner or the top_waiter does not change */
985 mark_rt_mutex_waiters(lock2
);
986 spin_unlock(&lock2
->wait_lock
);
991 spin_unlock(&hb1
->lock
);
993 spin_unlock(&hb2
->lock
);
995 /* drop_futex_key_refs() must be called outside the spinlocks. */
996 while (--drop_count
>= 0)
997 drop_futex_key_refs(&key1
);
1006 * Wake up all waiters hashed on the physical page that is mapped
1007 * to this virtual address:
1010 futex_wake_op(u32 __user
*uaddr1
, struct rw_semaphore
*fshared
,
1012 int nr_wake
, int nr_wake2
, int op
)
1014 union futex_key key1
, key2
;
1015 struct futex_hash_bucket
*hb1
, *hb2
;
1016 struct plist_head
*head
;
1017 struct futex_q
*this, *next
;
1018 int ret
, op_ret
, attempt
= 0;
1024 ret
= get_futex_key(uaddr1
, fshared
, &key1
);
1025 if (unlikely(ret
!= 0))
1027 ret
= get_futex_key(uaddr2
, fshared
, &key2
);
1028 if (unlikely(ret
!= 0))
1031 hb1
= hash_futex(&key1
);
1032 hb2
= hash_futex(&key2
);
1035 double_lock_hb(hb1
, hb2
);
1037 op_ret
= futex_atomic_op_inuser(op
, uaddr2
);
1038 if (unlikely(op_ret
< 0)) {
1041 spin_unlock(&hb1
->lock
);
1043 spin_unlock(&hb2
->lock
);
1047 * we don't get EFAULT from MMU faults if we don't have an MMU,
1048 * but we might get them from range checking
1054 if (unlikely(op_ret
!= -EFAULT
)) {
1060 * futex_atomic_op_inuser needs to both read and write
1061 * *(int __user *)uaddr2, but we can't modify it
1062 * non-atomically. Therefore, if get_user below is not
1063 * enough, we need to handle the fault ourselves, while
1064 * still holding the mmap_sem.
1067 ret
= futex_handle_fault((unsigned long)uaddr2
,
1075 * If we would have faulted, release mmap_sem,
1076 * fault it in and start all over again.
1081 ret
= get_user(dummy
, uaddr2
);
1090 plist_for_each_entry_safe(this, next
, head
, list
) {
1091 if (match_futex (&this->key
, &key1
)) {
1093 if (++ret
>= nr_wake
)
1102 plist_for_each_entry_safe(this, next
, head
, list
) {
1103 if (match_futex (&this->key
, &key2
)) {
1105 if (++op_ret
>= nr_wake2
)
1112 spin_unlock(&hb1
->lock
);
1114 spin_unlock(&hb2
->lock
);
1122 * Requeue all waiters hashed on one physical page to another
1125 static int futex_requeue(u32 __user
*uaddr1
, struct rw_semaphore
*fshared
,
1127 int nr_wake
, int nr_requeue
, u32
*cmpval
)
1129 union futex_key key1
, key2
;
1130 struct futex_hash_bucket
*hb1
, *hb2
;
1131 struct plist_head
*head1
;
1132 struct futex_q
*this, *next
;
1133 int ret
, drop_count
= 0;
1139 ret
= get_futex_key(uaddr1
, fshared
, &key1
);
1140 if (unlikely(ret
!= 0))
1142 ret
= get_futex_key(uaddr2
, fshared
, &key2
);
1143 if (unlikely(ret
!= 0))
1146 hb1
= hash_futex(&key1
);
1147 hb2
= hash_futex(&key2
);
1149 double_lock_hb(hb1
, hb2
);
1151 if (likely(cmpval
!= NULL
)) {
1154 ret
= get_futex_value_locked(&curval
, uaddr1
);
1156 if (unlikely(ret
)) {
1157 spin_unlock(&hb1
->lock
);
1159 spin_unlock(&hb2
->lock
);
1162 * If we would have faulted, release mmap_sem, fault
1163 * it in and start all over again.
1168 ret
= get_user(curval
, uaddr1
);
1175 if (curval
!= *cmpval
) {
1181 head1
= &hb1
->chain
;
1182 plist_for_each_entry_safe(this, next
, head1
, list
) {
1183 if (!match_futex (&this->key
, &key1
))
1185 if (++ret
<= nr_wake
) {
1189 * If key1 and key2 hash to the same bucket, no need to
1192 if (likely(head1
!= &hb2
->chain
)) {
1193 plist_del(&this->list
, &hb1
->chain
);
1194 plist_add(&this->list
, &hb2
->chain
);
1195 this->lock_ptr
= &hb2
->lock
;
1196 #ifdef CONFIG_DEBUG_PI_LIST
1197 this->list
.plist
.lock
= &hb2
->lock
;
1201 get_futex_key_refs(&key2
);
1204 if (ret
- nr_wake
>= nr_requeue
)
1210 spin_unlock(&hb1
->lock
);
1212 spin_unlock(&hb2
->lock
);
1214 /* drop_futex_key_refs() must be called outside the spinlocks. */
1215 while (--drop_count
>= 0)
1216 drop_futex_key_refs(&key1
);
1224 /* The key must be already stored in q->key. */
1225 static inline struct futex_hash_bucket
*
1226 queue_lock(struct futex_q
*q
, int fd
, struct file
*filp
)
1228 struct futex_hash_bucket
*hb
;
1233 init_waitqueue_head(&q
->waiters
);
1235 get_futex_key_refs(&q
->key
);
1236 hb
= hash_futex(&q
->key
);
1237 q
->lock_ptr
= &hb
->lock
;
1239 spin_lock(&hb
->lock
);
1243 static inline void __queue_me(struct futex_q
*q
, struct futex_hash_bucket
*hb
)
1248 * The priority used to register this element is
1249 * - either the real thread-priority for the real-time threads
1250 * (i.e. threads with a priority lower than MAX_RT_PRIO)
1251 * - or MAX_RT_PRIO for non-RT threads.
1252 * Thus, all RT-threads are woken first in priority order, and
1253 * the others are woken last, in FIFO order.
1255 prio
= min(current
->normal_prio
, MAX_RT_PRIO
);
1257 plist_node_init(&q
->list
, prio
);
1258 #ifdef CONFIG_DEBUG_PI_LIST
1259 q
->list
.plist
.lock
= &hb
->lock
;
1261 plist_add(&q
->list
, &hb
->chain
);
1263 spin_unlock(&hb
->lock
);
1267 queue_unlock(struct futex_q
*q
, struct futex_hash_bucket
*hb
)
1269 spin_unlock(&hb
->lock
);
1270 drop_futex_key_refs(&q
->key
);
1274 * queue_me and unqueue_me must be called as a pair, each
1275 * exactly once. They are called with the hashed spinlock held.
1278 /* The key must be already stored in q->key. */
1279 static void queue_me(struct futex_q
*q
, int fd
, struct file
*filp
)
1281 struct futex_hash_bucket
*hb
;
1283 hb
= queue_lock(q
, fd
, filp
);
1287 /* Return 1 if we were still queued (ie. 0 means we were woken) */
1288 static int unqueue_me(struct futex_q
*q
)
1290 spinlock_t
*lock_ptr
;
1293 /* In the common case we don't take the spinlock, which is nice. */
1295 lock_ptr
= q
->lock_ptr
;
1297 if (lock_ptr
!= 0) {
1298 spin_lock(lock_ptr
);
1300 * q->lock_ptr can change between reading it and
1301 * spin_lock(), causing us to take the wrong lock. This
1302 * corrects the race condition.
1304 * Reasoning goes like this: if we have the wrong lock,
1305 * q->lock_ptr must have changed (maybe several times)
1306 * between reading it and the spin_lock(). It can
1307 * change again after the spin_lock() but only if it was
1308 * already changed before the spin_lock(). It cannot,
1309 * however, change back to the original value. Therefore
1310 * we can detect whether we acquired the correct lock.
1312 if (unlikely(lock_ptr
!= q
->lock_ptr
)) {
1313 spin_unlock(lock_ptr
);
1316 WARN_ON(plist_node_empty(&q
->list
));
1317 plist_del(&q
->list
, &q
->list
.plist
);
1319 BUG_ON(q
->pi_state
);
1321 spin_unlock(lock_ptr
);
1325 drop_futex_key_refs(&q
->key
);
1330 * PI futexes can not be requeued and must remove themself from the
1331 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
1334 static void unqueue_me_pi(struct futex_q
*q
)
1336 WARN_ON(plist_node_empty(&q
->list
));
1337 plist_del(&q
->list
, &q
->list
.plist
);
1339 BUG_ON(!q
->pi_state
);
1340 free_pi_state(q
->pi_state
);
1343 spin_unlock(q
->lock_ptr
);
1345 drop_futex_key_refs(&q
->key
);
1349 * Fixup the pi_state owner with current.
1351 * Must be called with hash bucket lock held and mm->sem held for non
1354 static int fixup_pi_state_owner(u32 __user
*uaddr
, struct futex_q
*q
,
1355 struct task_struct
*curr
)
1357 u32 newtid
= curr
->pid
| FUTEX_WAITERS
;
1358 struct futex_pi_state
*pi_state
= q
->pi_state
;
1359 u32 uval
, curval
, newval
;
1363 if (pi_state
->owner
!= NULL
) {
1364 spin_lock_irq(&pi_state
->owner
->pi_lock
);
1365 WARN_ON(list_empty(&pi_state
->list
));
1366 list_del_init(&pi_state
->list
);
1367 spin_unlock_irq(&pi_state
->owner
->pi_lock
);
1369 newtid
|= FUTEX_OWNER_DIED
;
1371 pi_state
->owner
= curr
;
1373 spin_lock_irq(&curr
->pi_lock
);
1374 WARN_ON(!list_empty(&pi_state
->list
));
1375 list_add(&pi_state
->list
, &curr
->pi_state_list
);
1376 spin_unlock_irq(&curr
->pi_lock
);
1379 * We own it, so we have to replace the pending owner
1380 * TID. This must be atomic as we have preserve the
1381 * owner died bit here.
1383 ret
= get_futex_value_locked(&uval
, uaddr
);
1386 newval
= (uval
& FUTEX_OWNER_DIED
) | newtid
;
1387 newval
|= (uval
& FUTEX_WAITER_REQUEUED
);
1389 pagefault_disable();
1390 curval
= futex_atomic_cmpxchg_inatomic(uaddr
,
1394 if (curval
== -EFAULT
)
1404 * In case we must use restart_block to restart a futex_wait,
1405 * we encode in the 'arg3' shared capability
1407 #define ARG3_SHARED 1
1409 static long futex_wait_restart(struct restart_block
*restart
);
1410 static int futex_wait(u32 __user
*uaddr
, struct rw_semaphore
*fshared
,
1411 u32 val
, ktime_t
*abs_time
)
1413 struct task_struct
*curr
= current
;
1414 DECLARE_WAITQUEUE(wait
, curr
);
1415 struct futex_hash_bucket
*hb
;
1419 struct hrtimer_sleeper t
, *to
= NULL
;
1427 ret
= get_futex_key(uaddr
, fshared
, &q
.key
);
1428 if (unlikely(ret
!= 0))
1429 goto out_release_sem
;
1431 hb
= queue_lock(&q
, -1, NULL
);
1434 * Access the page AFTER the futex is queued.
1435 * Order is important:
1437 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
1438 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
1440 * The basic logical guarantee of a futex is that it blocks ONLY
1441 * if cond(var) is known to be true at the time of blocking, for
1442 * any cond. If we queued after testing *uaddr, that would open
1443 * a race condition where we could block indefinitely with
1444 * cond(var) false, which would violate the guarantee.
1446 * A consequence is that futex_wait() can return zero and absorb
1447 * a wakeup when *uaddr != val on entry to the syscall. This is
1450 * for shared futexes, we hold the mmap semaphore, so the mapping
1451 * cannot have changed since we looked it up in get_futex_key.
1453 ret
= get_futex_value_locked(&uval
, uaddr
);
1455 if (unlikely(ret
)) {
1456 queue_unlock(&q
, hb
);
1459 * If we would have faulted, release mmap_sem, fault it in and
1460 * start all over again.
1465 ret
= get_user(uval
, uaddr
);
1473 goto out_unlock_release_sem
;
1476 * This rt_mutex_waiter structure is prepared here and will
1477 * be used only if this task is requeued from a normal futex to
1478 * a PI-futex with futex_requeue_pi.
1480 debug_rt_mutex_init_waiter(&q
.waiter
);
1481 q
.waiter
.task
= NULL
;
1483 /* Only actually queue if *uaddr contained val. */
1487 * Now the futex is queued and we have checked the data, we
1488 * don't want to hold mmap_sem while we sleep.
1494 * There might have been scheduling since the queue_me(), as we
1495 * cannot hold a spinlock across the get_user() in case it
1496 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
1497 * queueing ourselves into the futex hash. This code thus has to
1498 * rely on the futex_wake() code removing us from hash when it
1502 /* add_wait_queue is the barrier after __set_current_state. */
1503 __set_current_state(TASK_INTERRUPTIBLE
);
1504 add_wait_queue(&q
.waiters
, &wait
);
1506 * !plist_node_empty() is safe here without any lock.
1507 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
1509 if (likely(!plist_node_empty(&q
.list
))) {
1514 hrtimer_init(&t
.timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1515 hrtimer_init_sleeper(&t
, current
);
1516 t
.timer
.expires
= *abs_time
;
1518 hrtimer_start(&t
.timer
, t
.timer
.expires
, HRTIMER_MODE_ABS
);
1521 * the timer could have already expired, in which
1522 * case current would be flagged for rescheduling.
1523 * Don't bother calling schedule.
1528 hrtimer_cancel(&t
.timer
);
1530 /* Flag if a timeout occured */
1531 rem
= (t
.task
== NULL
);
1534 __set_current_state(TASK_RUNNING
);
1537 * NOTE: we don't remove ourselves from the waitqueue because
1538 * we are the only user of it.
1543 * We were woken but have been requeued on a PI-futex.
1544 * We have to complete the lock acquisition by taking
1548 struct rt_mutex
*lock
= &q
.pi_state
->pi_mutex
;
1550 spin_lock(&lock
->wait_lock
);
1551 if (unlikely(q
.waiter
.task
)) {
1552 remove_waiter(lock
, &q
.waiter
);
1554 spin_unlock(&lock
->wait_lock
);
1559 ret
= rt_mutex_timed_lock(lock
, to
, 1);
1563 spin_lock(q
.lock_ptr
);
1566 * Got the lock. We might not be the anticipated owner if we
1567 * did a lock-steal - fix up the PI-state in that case.
1569 if (!ret
&& q
.pi_state
->owner
!= curr
) {
1571 * We MUST play with the futex we were requeued on,
1572 * NOT the current futex.
1573 * We can retrieve it from the key of the pi_state
1575 uaddr
= q
.pi_state
->key
.uaddr
;
1577 ret
= fixup_pi_state_owner(uaddr
, &q
, curr
);
1580 * Catch the rare case, where the lock was released
1581 * when we were on the way back before we locked
1584 if (ret
&& q
.pi_state
->owner
== curr
) {
1585 if (rt_mutex_trylock(&q
.pi_state
->pi_mutex
))
1590 /* Unqueue and drop the lock */
1595 debug_rt_mutex_free_waiter(&q
.waiter
);
1600 debug_rt_mutex_free_waiter(&q
.waiter
);
1602 /* If we were woken (and unqueued), we succeeded, whatever. */
1603 if (!unqueue_me(&q
))
1609 * We expect signal_pending(current), but another thread may
1610 * have handled it for us already.
1613 return -ERESTARTSYS
;
1615 struct restart_block
*restart
;
1616 restart
= ¤t_thread_info()->restart_block
;
1617 restart
->fn
= futex_wait_restart
;
1618 restart
->arg0
= (unsigned long)uaddr
;
1619 restart
->arg1
= (unsigned long)val
;
1620 restart
->arg2
= (unsigned long)abs_time
;
1623 restart
->arg3
|= ARG3_SHARED
;
1624 return -ERESTART_RESTARTBLOCK
;
1627 out_unlock_release_sem
:
1628 queue_unlock(&q
, hb
);
1637 static long futex_wait_restart(struct restart_block
*restart
)
1639 u32 __user
*uaddr
= (u32 __user
*)restart
->arg0
;
1640 u32 val
= (u32
)restart
->arg1
;
1641 ktime_t
*abs_time
= (ktime_t
*)restart
->arg2
;
1642 struct rw_semaphore
*fshared
= NULL
;
1644 restart
->fn
= do_no_restart_syscall
;
1645 if (restart
->arg3
& ARG3_SHARED
)
1646 fshared
= ¤t
->mm
->mmap_sem
;
1647 return (long)futex_wait(uaddr
, fshared
, val
, abs_time
);
1651 static void set_pi_futex_owner(struct futex_hash_bucket
*hb
,
1652 union futex_key
*key
, struct task_struct
*p
)
1654 struct plist_head
*head
;
1655 struct futex_q
*this, *next
;
1656 struct futex_pi_state
*pi_state
= NULL
;
1657 struct rt_mutex
*lock
;
1659 /* Search a waiter that should already exists */
1663 plist_for_each_entry_safe(this, next
, head
, list
) {
1664 if (match_futex (&this->key
, key
)) {
1665 pi_state
= this->pi_state
;
1672 /* set p as pi_state's owner */
1673 lock
= &pi_state
->pi_mutex
;
1675 spin_lock(&lock
->wait_lock
);
1676 spin_lock_irq(&p
->pi_lock
);
1678 list_add(&pi_state
->list
, &p
->pi_state_list
);
1679 pi_state
->owner
= p
;
1682 /* set p as pi_mutex's owner */
1683 debug_rt_mutex_proxy_lock(lock
, p
);
1684 WARN_ON(rt_mutex_owner(lock
));
1685 rt_mutex_set_owner(lock
, p
, 0);
1686 rt_mutex_deadlock_account_lock(lock
, p
);
1688 plist_add(&rt_mutex_top_waiter(lock
)->pi_list_entry
,
1690 __rt_mutex_adjust_prio(p
);
1692 spin_unlock_irq(&p
->pi_lock
);
1693 spin_unlock(&lock
->wait_lock
);
1697 * Userspace tried a 0 -> TID atomic transition of the futex value
1698 * and failed. The kernel side here does the whole locking operation:
1699 * if there are waiters then it will block, it does PI, etc. (Due to
1700 * races the kernel might see a 0 value of the futex too.)
1702 static int futex_lock_pi(u32 __user
*uaddr
, struct rw_semaphore
*fshared
,
1703 int detect
, ktime_t
*time
, int trylock
)
1705 struct hrtimer_sleeper timeout
, *to
= NULL
;
1706 struct task_struct
*curr
= current
;
1707 struct futex_hash_bucket
*hb
;
1708 u32 uval
, newval
, curval
;
1710 int ret
, lock_taken
, ownerdied
= 0, attempt
= 0;
1712 if (refill_pi_state_cache())
1717 hrtimer_init(&to
->timer
, CLOCK_REALTIME
, HRTIMER_MODE_ABS
);
1718 hrtimer_init_sleeper(to
, current
);
1719 to
->timer
.expires
= *time
;
1727 ret
= get_futex_key(uaddr
, fshared
, &q
.key
);
1728 if (unlikely(ret
!= 0))
1729 goto out_release_sem
;
1732 hb
= queue_lock(&q
, -1, NULL
);
1735 ret
= lock_taken
= 0;
1738 * To avoid races, we attempt to take the lock here again
1739 * (by doing a 0 -> TID atomic cmpxchg), while holding all
1740 * the locks. It will most likely not succeed.
1742 newval
= current
->pid
;
1744 pagefault_disable();
1745 curval
= futex_atomic_cmpxchg_inatomic(uaddr
, 0, newval
);
1748 if (unlikely(curval
== -EFAULT
))
1752 * Detect deadlocks. In case of REQUEUE_PI this is a valid
1753 * situation and we return success to user space.
1755 if (unlikely((curval
& FUTEX_TID_MASK
) == current
->pid
)) {
1756 if (!(curval
& FUTEX_WAITER_REQUEUED
))
1758 goto out_unlock_release_sem
;
1762 * Surprise - we got the lock. Just return to userspace:
1764 if (unlikely(!curval
))
1765 goto out_unlock_release_sem
;
1770 * Set the WAITERS flag, so the owner will know it has someone
1771 * to wake at next unlock
1773 newval
= curval
| FUTEX_WAITERS
;
1776 * There are two cases, where a futex might have no owner (the
1777 * owner TID is 0): OWNER_DIED or REQUEUE. We take over the
1778 * futex in this case. We also do an unconditional take over,
1779 * when the owner of the futex died.
1781 * This is safe as we are protected by the hash bucket lock !
1783 if (unlikely(ownerdied
|| !(curval
& FUTEX_TID_MASK
))) {
1784 /* Keep the OWNER_DIED and REQUEUE bits */
1785 newval
= (curval
& ~FUTEX_TID_MASK
) | current
->pid
;
1790 pagefault_disable();
1791 curval
= futex_atomic_cmpxchg_inatomic(uaddr
, uval
, newval
);
1794 if (unlikely(curval
== -EFAULT
))
1796 if (unlikely(curval
!= uval
))
1800 * We took the lock due to requeue or owner died take over.
1802 if (unlikely(lock_taken
)) {
1803 /* For requeue we need to fixup the pi_futex */
1804 if (curval
& FUTEX_WAITER_REQUEUED
)
1805 set_pi_futex_owner(hb
, &q
.key
, curr
);
1806 goto out_unlock_release_sem
;
1810 * We dont have the lock. Look up the PI state (or create it if
1811 * we are the first waiter):
1813 ret
= lookup_pi_state(uval
, hb
, &q
.key
, &q
.pi_state
);
1815 if (unlikely(ret
)) {
1820 * Task is exiting and we just wait for the
1823 queue_unlock(&q
, hb
);
1831 * No owner found for this futex. Check if the
1832 * OWNER_DIED bit is set to figure out whether
1833 * this is a robust futex or not.
1835 if (get_futex_value_locked(&curval
, uaddr
))
1839 * We simply start over in case of a robust
1840 * futex. The code above will take the futex
1843 if (curval
& FUTEX_OWNER_DIED
) {
1848 goto out_unlock_release_sem
;
1853 * Only actually queue now that the atomic ops are done:
1858 * Now the futex is queued and we have checked the data, we
1859 * don't want to hold mmap_sem while we sleep.
1864 WARN_ON(!q
.pi_state
);
1866 * Block on the PI mutex:
1869 ret
= rt_mutex_timed_lock(&q
.pi_state
->pi_mutex
, to
, 1);
1871 ret
= rt_mutex_trylock(&q
.pi_state
->pi_mutex
);
1872 /* Fixup the trylock return value: */
1873 ret
= ret
? 0 : -EWOULDBLOCK
;
1878 spin_lock(q
.lock_ptr
);
1882 * Got the lock. We might not be the anticipated owner
1883 * if we did a lock-steal - fix up the PI-state in
1886 if (q
.pi_state
->owner
!= curr
)
1887 ret
= fixup_pi_state_owner(uaddr
, &q
, curr
);
1890 * Catch the rare case, where the lock was released
1891 * when we were on the way back before we locked the
1894 if (q
.pi_state
->owner
== curr
&&
1895 rt_mutex_trylock(&q
.pi_state
->pi_mutex
)) {
1899 * Paranoia check. If we did not take the lock
1900 * in the trylock above, then we should not be
1901 * the owner of the rtmutex, neither the real
1902 * nor the pending one:
1904 if (rt_mutex_owner(&q
.pi_state
->pi_mutex
) == curr
)
1905 printk(KERN_ERR
"futex_lock_pi: ret = %d "
1906 "pi-mutex: %p pi-state %p\n", ret
,
1907 q
.pi_state
->pi_mutex
.owner
,
1912 /* Unqueue and drop the lock */
1917 return ret
!= -EINTR
? ret
: -ERESTARTNOINTR
;
1919 out_unlock_release_sem
:
1920 queue_unlock(&q
, hb
);
1929 * We have to r/w *(int __user *)uaddr, but we can't modify it
1930 * non-atomically. Therefore, if get_user below is not
1931 * enough, we need to handle the fault ourselves, while
1932 * still holding the mmap_sem.
1934 * ... and hb->lock. :-) --ANK
1936 queue_unlock(&q
, hb
);
1939 ret
= futex_handle_fault((unsigned long)uaddr
, fshared
,
1942 goto out_release_sem
;
1943 goto retry_unlocked
;
1949 ret
= get_user(uval
, uaddr
);
1950 if (!ret
&& (uval
!= -EFAULT
))
1957 * Userspace attempted a TID -> 0 atomic transition, and failed.
1958 * This is the in-kernel slowpath: we look up the PI state (if any),
1959 * and do the rt-mutex unlock.
1961 static int futex_unlock_pi(u32 __user
*uaddr
, struct rw_semaphore
*fshared
)
1963 struct futex_hash_bucket
*hb
;
1964 struct futex_q
*this, *next
;
1966 struct plist_head
*head
;
1967 union futex_key key
;
1968 int ret
, attempt
= 0;
1971 if (get_user(uval
, uaddr
))
1974 * We release only a lock we actually own:
1976 if ((uval
& FUTEX_TID_MASK
) != current
->pid
)
1979 * First take all the futex related locks:
1984 ret
= get_futex_key(uaddr
, fshared
, &key
);
1985 if (unlikely(ret
!= 0))
1988 hb
= hash_futex(&key
);
1990 spin_lock(&hb
->lock
);
1993 * To avoid races, try to do the TID -> 0 atomic transition
1994 * again. If it succeeds then we can return without waking
1997 if (!(uval
& FUTEX_OWNER_DIED
)) {
1998 pagefault_disable();
1999 uval
= futex_atomic_cmpxchg_inatomic(uaddr
, current
->pid
, 0);
2003 if (unlikely(uval
== -EFAULT
))
2006 * Rare case: we managed to release the lock atomically,
2007 * no need to wake anyone else up:
2009 if (unlikely(uval
== current
->pid
))
2013 * Ok, other tasks may need to be woken up - check waiters
2014 * and do the wakeup if necessary:
2018 plist_for_each_entry_safe(this, next
, head
, list
) {
2019 if (!match_futex (&this->key
, &key
))
2021 ret
= wake_futex_pi(uaddr
, uval
, this);
2023 * The atomic access to the futex value
2024 * generated a pagefault, so retry the
2025 * user-access and the wakeup:
2032 * No waiters - kernel unlocks the futex:
2034 if (!(uval
& FUTEX_OWNER_DIED
)) {
2035 ret
= unlock_futex_pi(uaddr
, uval
);
2041 spin_unlock(&hb
->lock
);
2050 * We have to r/w *(int __user *)uaddr, but we can't modify it
2051 * non-atomically. Therefore, if get_user below is not
2052 * enough, we need to handle the fault ourselves, while
2053 * still holding the mmap_sem.
2055 * ... and hb->lock. --ANK
2057 spin_unlock(&hb
->lock
);
2060 ret
= futex_handle_fault((unsigned long)uaddr
, fshared
,
2064 goto retry_unlocked
;
2070 ret
= get_user(uval
, uaddr
);
2071 if (!ret
&& (uval
!= -EFAULT
))
2077 static int futex_close(struct inode
*inode
, struct file
*filp
)
2079 struct futex_q
*q
= filp
->private_data
;
2087 /* This is one-shot: once it's gone off you need a new fd */
2088 static unsigned int futex_poll(struct file
*filp
,
2089 struct poll_table_struct
*wait
)
2091 struct futex_q
*q
= filp
->private_data
;
2094 poll_wait(filp
, &q
->waiters
, wait
);
2097 * plist_node_empty() is safe here without any lock.
2098 * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
2100 if (plist_node_empty(&q
->list
))
2101 ret
= POLLIN
| POLLRDNORM
;
2106 static const struct file_operations futex_fops
= {
2107 .release
= futex_close
,
2112 * Signal allows caller to avoid the race which would occur if they
2113 * set the sigio stuff up afterwards.
2115 static int futex_fd(u32 __user
*uaddr
, int signal
)
2120 struct rw_semaphore
*fshared
;
2121 static unsigned long printk_interval
;
2123 if (printk_timed_ratelimit(&printk_interval
, 60 * 60 * 1000)) {
2124 printk(KERN_WARNING
"Process `%s' used FUTEX_FD, which "
2125 "will be removed from the kernel in June 2007\n",
2130 if (!valid_signal(signal
))
2133 ret
= get_unused_fd();
2136 filp
= get_empty_filp();
2142 filp
->f_op
= &futex_fops
;
2143 filp
->f_path
.mnt
= mntget(futex_mnt
);
2144 filp
->f_path
.dentry
= dget(futex_mnt
->mnt_root
);
2145 filp
->f_mapping
= filp
->f_path
.dentry
->d_inode
->i_mapping
;
2148 err
= __f_setown(filp
, task_pid(current
), PIDTYPE_PID
, 1);
2152 filp
->f_owner
.signum
= signal
;
2155 q
= kmalloc(sizeof(*q
), GFP_KERNEL
);
2162 fshared
= ¤t
->mm
->mmap_sem
;
2164 err
= get_futex_key(uaddr
, fshared
, &q
->key
);
2166 if (unlikely(err
!= 0)) {
2173 * queue_me() must be called before releasing mmap_sem, because
2174 * key->shared.inode needs to be referenced while holding it.
2176 filp
->private_data
= q
;
2178 queue_me(q
, ret
, filp
);
2181 /* Now we map fd to filp, so userspace can access it */
2182 fd_install(ret
, filp
);
2193 * Support for robust futexes: the kernel cleans up held futexes at
2196 * Implementation: user-space maintains a per-thread list of locks it
2197 * is holding. Upon do_exit(), the kernel carefully walks this list,
2198 * and marks all locks that are owned by this thread with the
2199 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
2200 * always manipulated with the lock held, so the list is private and
2201 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
2202 * field, to allow the kernel to clean up if the thread dies after
2203 * acquiring the lock, but just before it could have added itself to
2204 * the list. There can only be one such pending lock.
2208 * sys_set_robust_list - set the robust-futex list head of a task
2209 * @head: pointer to the list-head
2210 * @len: length of the list-head, as userspace expects
2213 sys_set_robust_list(struct robust_list_head __user
*head
,
2217 * The kernel knows only one size for now:
2219 if (unlikely(len
!= sizeof(*head
)))
2222 current
->robust_list
= head
;
2228 * sys_get_robust_list - get the robust-futex list head of a task
2229 * @pid: pid of the process [zero for current task]
2230 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
2231 * @len_ptr: pointer to a length field, the kernel fills in the header size
2234 sys_get_robust_list(int pid
, struct robust_list_head __user
* __user
*head_ptr
,
2235 size_t __user
*len_ptr
)
2237 struct robust_list_head __user
*head
;
2241 head
= current
->robust_list
;
2243 struct task_struct
*p
;
2247 p
= find_task_by_pid(pid
);
2251 if ((current
->euid
!= p
->euid
) && (current
->euid
!= p
->uid
) &&
2252 !capable(CAP_SYS_PTRACE
))
2254 head
= p
->robust_list
;
2258 if (put_user(sizeof(*head
), len_ptr
))
2260 return put_user(head
, head_ptr
);
2269 * Process a futex-list entry, check whether it's owned by the
2270 * dying task, and do notification if so:
2272 int handle_futex_death(u32 __user
*uaddr
, struct task_struct
*curr
, int pi
)
2274 u32 uval
, nval
, mval
;
2277 if (get_user(uval
, uaddr
))
2280 if ((uval
& FUTEX_TID_MASK
) == curr
->pid
) {
2282 * Ok, this dying thread is truly holding a futex
2283 * of interest. Set the OWNER_DIED bit atomically
2284 * via cmpxchg, and if the value had FUTEX_WAITERS
2285 * set, wake up a waiter (if any). (We have to do a
2286 * futex_wake() even if OWNER_DIED is already set -
2287 * to handle the rare but possible case of recursive
2288 * thread-death.) The rest of the cleanup is done in
2291 mval
= (uval
& FUTEX_WAITERS
) | FUTEX_OWNER_DIED
;
2292 /* Also keep the FUTEX_WAITER_REQUEUED flag if set */
2293 mval
|= (uval
& FUTEX_WAITER_REQUEUED
);
2294 nval
= futex_atomic_cmpxchg_inatomic(uaddr
, uval
, mval
);
2296 if (nval
== -EFAULT
)
2303 * Wake robust non-PI futexes here. The wakeup of
2304 * PI futexes happens in exit_pi_state():
2307 if (uval
& FUTEX_WAITERS
)
2308 futex_wake(uaddr
, &curr
->mm
->mmap_sem
, 1);
2315 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
2317 static inline int fetch_robust_entry(struct robust_list __user
**entry
,
2318 struct robust_list __user
* __user
*head
,
2321 unsigned long uentry
;
2323 if (get_user(uentry
, (unsigned long __user
*)head
))
2326 *entry
= (void __user
*)(uentry
& ~1UL);
2333 * Walk curr->robust_list (very carefully, it's a userspace list!)
2334 * and mark any locks found there dead, and notify any waiters.
2336 * We silently return on any sign of list-walking problem.
2338 void exit_robust_list(struct task_struct
*curr
)
2340 struct robust_list_head __user
*head
= curr
->robust_list
;
2341 struct robust_list __user
*entry
, *pending
;
2342 unsigned int limit
= ROBUST_LIST_LIMIT
, pi
, pip
;
2343 unsigned long futex_offset
;
2346 * Fetch the list head (which was registered earlier, via
2347 * sys_set_robust_list()):
2349 if (fetch_robust_entry(&entry
, &head
->list
.next
, &pi
))
2352 * Fetch the relative futex offset:
2354 if (get_user(futex_offset
, &head
->futex_offset
))
2357 * Fetch any possibly pending lock-add first, and handle it
2360 if (fetch_robust_entry(&pending
, &head
->list_op_pending
, &pip
))
2364 handle_futex_death((void __user
*)pending
+ futex_offset
,
2367 while (entry
!= &head
->list
) {
2369 * A pending lock might already be on the list, so
2370 * don't process it twice:
2372 if (entry
!= pending
)
2373 if (handle_futex_death((void __user
*)entry
+ futex_offset
,
2377 * Fetch the next entry in the list:
2379 if (fetch_robust_entry(&entry
, &entry
->next
, &pi
))
2382 * Avoid excessively long or circular lists:
2391 long do_futex(u32 __user
*uaddr
, int op
, u32 val
, ktime_t
*timeout
,
2392 u32 __user
*uaddr2
, u32 val2
, u32 val3
)
2395 int cmd
= op
& FUTEX_CMD_MASK
;
2396 struct rw_semaphore
*fshared
= NULL
;
2398 if (!(op
& FUTEX_PRIVATE_FLAG
))
2399 fshared
= ¤t
->mm
->mmap_sem
;
2403 ret
= futex_wait(uaddr
, fshared
, val
, timeout
);
2406 ret
= futex_wake(uaddr
, fshared
, val
);
2409 /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
2410 ret
= futex_fd(uaddr
, val
);
2413 ret
= futex_requeue(uaddr
, fshared
, uaddr2
, val
, val2
, NULL
);
2415 case FUTEX_CMP_REQUEUE
:
2416 ret
= futex_requeue(uaddr
, fshared
, uaddr2
, val
, val2
, &val3
);
2419 ret
= futex_wake_op(uaddr
, fshared
, uaddr2
, val
, val2
, val3
);
2422 ret
= futex_lock_pi(uaddr
, fshared
, val
, timeout
, 0);
2424 case FUTEX_UNLOCK_PI
:
2425 ret
= futex_unlock_pi(uaddr
, fshared
);
2427 case FUTEX_TRYLOCK_PI
:
2428 ret
= futex_lock_pi(uaddr
, fshared
, 0, timeout
, 1);
2430 case FUTEX_CMP_REQUEUE_PI
:
2431 ret
= futex_requeue_pi(uaddr
, fshared
, uaddr2
, val
, val2
, &val3
);
2440 asmlinkage
long sys_futex(u32 __user
*uaddr
, int op
, u32 val
,
2441 struct timespec __user
*utime
, u32 __user
*uaddr2
,
2445 ktime_t t
, *tp
= NULL
;
2447 int cmd
= op
& FUTEX_CMD_MASK
;
2449 if (utime
&& (cmd
== FUTEX_WAIT
|| cmd
== FUTEX_LOCK_PI
)) {
2450 if (copy_from_user(&ts
, utime
, sizeof(ts
)) != 0)
2452 if (!timespec_valid(&ts
))
2455 t
= timespec_to_ktime(ts
);
2456 if (cmd
== FUTEX_WAIT
)
2457 t
= ktime_add(ktime_get(), t
);
2461 * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
2463 if (cmd
== FUTEX_REQUEUE
|| cmd
== FUTEX_CMP_REQUEUE
2464 || cmd
== FUTEX_CMP_REQUEUE_PI
)
2465 val2
= (u32
) (unsigned long) utime
;
2467 return do_futex(uaddr
, op
, val
, tp
, uaddr2
, val2
, val3
);
2470 static int futexfs_get_sb(struct file_system_type
*fs_type
,
2471 int flags
, const char *dev_name
, void *data
,
2472 struct vfsmount
*mnt
)
2474 return get_sb_pseudo(fs_type
, "futex", NULL
, 0xBAD1DEA, mnt
);
2477 static struct file_system_type futex_fs_type
= {
2479 .get_sb
= futexfs_get_sb
,
2480 .kill_sb
= kill_anon_super
,
2483 static int __init
init(void)
2485 int i
= register_filesystem(&futex_fs_type
);
2490 futex_mnt
= kern_mount(&futex_fs_type
);
2491 if (IS_ERR(futex_mnt
)) {
2492 unregister_filesystem(&futex_fs_type
);
2493 return PTR_ERR(futex_mnt
);
2496 for (i
= 0; i
< ARRAY_SIZE(futex_queues
); i
++) {
2497 plist_head_init(&futex_queues
[i
].chain
, &futex_queues
[i
].lock
);
2498 spin_lock_init(&futex_queues
[i
].lock
);