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 <linux/magic.h>
56 #include <linux/pid.h>
57 #include <linux/nsproxy.h>
59 #include <asm/futex.h>
61 #include "rtmutex_common.h"
63 #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
66 * Priority Inheritance state:
68 struct futex_pi_state {
70 * list of 'owned' pi_state instances - these have to be
71 * cleaned up in do_exit() if the task exits prematurely:
73 struct list_head list;
78 struct rt_mutex pi_mutex;
80 struct task_struct *owner;
87 * We use this hashed waitqueue instead of a normal wait_queue_t, so
88 * we can wake only the relevant ones (hashed queues may be shared).
90 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
91 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
92 * The order of wakup is always to make the first condition true, then
93 * wake up q->waiters, then make the second condition true.
96 struct plist_node list;
97 wait_queue_head_t waiters;
99 /* Which hash list lock to use: */
100 spinlock_t *lock_ptr;
102 /* Key which the futex is hashed on: */
105 /* For fd, sigio sent using these: */
109 /* Optional priority inheritance state: */
110 struct futex_pi_state *pi_state;
111 struct task_struct *task;
115 * Split the global futex_lock into every hash list lock.
117 struct futex_hash_bucket {
119 struct plist_head chain;
122 static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
124 /* Futex-fs vfsmount entry: */
125 static struct vfsmount *futex_mnt;
128 * Take mm->mmap_sem, when futex is shared
130 static inline void futex_lock_mm(struct rw_semaphore *fshared)
137 * Release mm->mmap_sem, when the futex is shared
139 static inline void futex_unlock_mm(struct rw_semaphore *fshared)
146 * We hash on the keys returned from get_futex_key (see below).
148 static struct futex_hash_bucket *hash_futex(union futex_key *key)
150 u32 hash = jhash2((u32*)&key->both.word,
151 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
153 return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
157 * Return 1 if two futex_keys are equal, 0 otherwise.
159 static inline int match_futex(union futex_key *key1, union futex_key *key2)
161 return (key1->both.word == key2->both.word
162 && key1->both.ptr == key2->both.ptr
163 && key1->both.offset == key2->both.offset);
167 * get_futex_key - Get parameters which are the keys for a futex.
168 * @uaddr: virtual address of the futex
169 * @shared: NULL for a PROCESS_PRIVATE futex,
170 * ¤t->mm->mmap_sem for a PROCESS_SHARED futex
171 * @key: address where result is stored.
173 * Returns a negative error code or 0
174 * The key words are stored in *key on success.
176 * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
177 * offset_within_page). For private mappings, it's (uaddr, current->mm).
178 * We can usually work out the index without swapping in the page.
180 * fshared is NULL for PROCESS_PRIVATE futexes
181 * For other futexes, it points to ¤t->mm->mmap_sem and
182 * caller must have taken the reader lock. but NOT any spinlocks.
184 int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared,
185 union futex_key *key)
187 unsigned long address = (unsigned long)uaddr;
188 struct mm_struct *mm = current->mm;
189 struct vm_area_struct *vma;
194 * The futex address must be "naturally" aligned.
196 key->both.offset = address % PAGE_SIZE;
197 if (unlikely((address % sizeof(u32)) != 0))
199 address -= key->both.offset;
202 * PROCESS_PRIVATE futexes are fast.
203 * As the mm cannot disappear under us and the 'key' only needs
204 * virtual address, we dont even have to find the underlying vma.
205 * Note : We do have to check 'uaddr' is a valid user address,
206 * but access_ok() should be faster than find_vma()
209 if (unlikely(!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))))
211 key->private.mm = mm;
212 key->private.address = address;
216 * The futex is hashed differently depending on whether
217 * it's in a shared or private mapping. So check vma first.
219 vma = find_extend_vma(mm, address);
226 if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
227 return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;
230 * Private mappings are handled in a simple way.
232 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
233 * it's a read-only handle, it's expected that futexes attach to
234 * the object not the particular process. Therefore we use
235 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
236 * mappings of _writable_ handles.
238 if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
239 key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */
240 key->private.mm = mm;
241 key->private.address = address;
246 * Linear file mappings are also simple.
248 key->shared.inode = vma->vm_file->f_path.dentry->d_inode;
249 key->both.offset |= FUT_OFF_INODE; /* inode-based key. */
250 if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
251 key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
257 * We could walk the page table to read the non-linear
258 * pte, and get the page index without fetching the page
259 * from swap. But that's a lot of code to duplicate here
260 * for a rare case, so we simply fetch the page.
262 err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
265 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
271 EXPORT_SYMBOL_GPL(get_futex_key);
274 * Take a reference to the resource addressed by a key.
275 * Can be called while holding spinlocks.
278 inline void get_futex_key_refs(union futex_key *key)
280 if (key->both.ptr == 0)
282 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
284 atomic_inc(&key->shared.inode->i_count);
286 case FUT_OFF_MMSHARED:
287 atomic_inc(&key->private.mm->mm_count);
291 EXPORT_SYMBOL_GPL(get_futex_key_refs);
294 * Drop a reference to the resource addressed by a key.
295 * The hash bucket spinlock must not be held.
297 void drop_futex_key_refs(union futex_key *key)
301 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
303 iput(key->shared.inode);
305 case FUT_OFF_MMSHARED:
306 mmdrop(key->private.mm);
310 EXPORT_SYMBOL_GPL(drop_futex_key_refs);
312 static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval)
317 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
323 static int get_futex_value_locked(u32 *dest, u32 __user *from)
328 ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
331 return ret ? -EFAULT : 0;
336 * if fshared is non NULL, current->mm->mmap_sem is already held
338 static int futex_handle_fault(unsigned long address,
339 struct rw_semaphore *fshared, int attempt)
341 struct vm_area_struct * vma;
342 struct mm_struct *mm = current->mm;
349 down_read(&mm->mmap_sem);
350 vma = find_vma(mm, address);
351 if (vma && address >= vma->vm_start &&
352 (vma->vm_flags & VM_WRITE)) {
354 fault = handle_mm_fault(mm, vma, address, 1);
355 if (unlikely((fault & VM_FAULT_ERROR))) {
357 /* XXX: let's do this when we verify it is OK */
358 if (ret & VM_FAULT_OOM)
363 if (fault & VM_FAULT_MAJOR)
370 up_read(&mm->mmap_sem);
377 static int refill_pi_state_cache(void)
379 struct futex_pi_state *pi_state;
381 if (likely(current->pi_state_cache))
384 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
389 INIT_LIST_HEAD(&pi_state->list);
390 /* pi_mutex gets initialized later */
391 pi_state->owner = NULL;
392 atomic_set(&pi_state->refcount, 1);
394 current->pi_state_cache = pi_state;
399 static struct futex_pi_state * alloc_pi_state(void)
401 struct futex_pi_state *pi_state = current->pi_state_cache;
404 current->pi_state_cache = NULL;
409 static void free_pi_state(struct futex_pi_state *pi_state)
411 if (!atomic_dec_and_test(&pi_state->refcount))
415 * If pi_state->owner is NULL, the owner is most probably dying
416 * and has cleaned up the pi_state already
418 if (pi_state->owner) {
419 spin_lock_irq(&pi_state->owner->pi_lock);
420 list_del_init(&pi_state->list);
421 spin_unlock_irq(&pi_state->owner->pi_lock);
423 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
426 if (current->pi_state_cache)
430 * pi_state->list is already empty.
431 * clear pi_state->owner.
432 * refcount is at 0 - put it back to 1.
434 pi_state->owner = NULL;
435 atomic_set(&pi_state->refcount, 1);
436 current->pi_state_cache = pi_state;
441 * Look up the task based on what TID userspace gave us.
444 static struct task_struct * futex_find_get_task(pid_t pid)
446 struct task_struct *p;
449 p = find_task_by_pid_ns(pid,
450 current->nsproxy->pid_ns);
452 if (!p || ((current->euid != p->euid) && (current->euid != p->uid)))
463 * This task is holding PI mutexes at exit time => bad.
464 * Kernel cleans up PI-state, but userspace is likely hosed.
465 * (Robust-futex cleanup is separate and might save the day for userspace.)
467 void exit_pi_state_list(struct task_struct *curr)
469 struct list_head *next, *head = &curr->pi_state_list;
470 struct futex_pi_state *pi_state;
471 struct futex_hash_bucket *hb;
475 * We are a ZOMBIE and nobody can enqueue itself on
476 * pi_state_list anymore, but we have to be careful
477 * versus waiters unqueueing themselves:
479 spin_lock_irq(&curr->pi_lock);
480 while (!list_empty(head)) {
483 pi_state = list_entry(next, struct futex_pi_state, list);
485 hb = hash_futex(&key);
486 spin_unlock_irq(&curr->pi_lock);
488 spin_lock(&hb->lock);
490 spin_lock_irq(&curr->pi_lock);
492 * We dropped the pi-lock, so re-check whether this
493 * task still owns the PI-state:
495 if (head->next != next) {
496 spin_unlock(&hb->lock);
500 WARN_ON(pi_state->owner != curr);
501 WARN_ON(list_empty(&pi_state->list));
502 list_del_init(&pi_state->list);
503 pi_state->owner = NULL;
504 spin_unlock_irq(&curr->pi_lock);
506 rt_mutex_unlock(&pi_state->pi_mutex);
508 spin_unlock(&hb->lock);
510 spin_lock_irq(&curr->pi_lock);
512 spin_unlock_irq(&curr->pi_lock);
516 lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
517 union futex_key *key, struct futex_pi_state **ps)
519 struct futex_pi_state *pi_state = NULL;
520 struct futex_q *this, *next;
521 struct plist_head *head;
522 struct task_struct *p;
523 pid_t pid = uval & FUTEX_TID_MASK;
527 plist_for_each_entry_safe(this, next, head, list) {
528 if (match_futex(&this->key, key)) {
530 * Another waiter already exists - bump up
531 * the refcount and return its pi_state:
533 pi_state = this->pi_state;
535 * Userspace might have messed up non PI and PI futexes
537 if (unlikely(!pi_state))
540 WARN_ON(!atomic_read(&pi_state->refcount));
541 WARN_ON(pid && pi_state->owner &&
542 pi_state->owner->pid != pid);
544 atomic_inc(&pi_state->refcount);
552 * We are the first waiter - try to look up the real owner and attach
553 * the new pi_state to it, but bail out when TID = 0
557 p = futex_find_get_task(pid);
562 * We need to look at the task state flags to figure out,
563 * whether the task is exiting. To protect against the do_exit
564 * change of the task flags, we do this protected by
567 spin_lock_irq(&p->pi_lock);
568 if (unlikely(p->flags & PF_EXITING)) {
570 * The task is on the way out. When PF_EXITPIDONE is
571 * set, we know that the task has finished the
574 int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
576 spin_unlock_irq(&p->pi_lock);
581 pi_state = alloc_pi_state();
584 * Initialize the pi_mutex in locked state and make 'p'
587 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
589 /* Store the key for possible exit cleanups: */
590 pi_state->key = *key;
592 WARN_ON(!list_empty(&pi_state->list));
593 list_add(&pi_state->list, &p->pi_state_list);
595 spin_unlock_irq(&p->pi_lock);
605 * The hash bucket lock must be held when this is called.
606 * Afterwards, the futex_q must not be accessed.
608 static void wake_futex(struct futex_q *q)
610 plist_del(&q->list, &q->list.plist);
612 send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
614 * The lock in wake_up_all() is a crucial memory barrier after the
615 * plist_del() and also before assigning to q->lock_ptr.
617 wake_up_all(&q->waiters);
619 * The waiting task can free the futex_q as soon as this is written,
620 * without taking any locks. This must come last.
622 * A memory barrier is required here to prevent the following store
623 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
624 * at the end of wake_up_all() does not prevent this store from
631 static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
633 struct task_struct *new_owner;
634 struct futex_pi_state *pi_state = this->pi_state;
640 spin_lock(&pi_state->pi_mutex.wait_lock);
641 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
644 * This happens when we have stolen the lock and the original
645 * pending owner did not enqueue itself back on the rt_mutex.
646 * Thats not a tragedy. We know that way, that a lock waiter
647 * is on the fly. We make the futex_q waiter the pending owner.
650 new_owner = this->task;
653 * We pass it to the next owner. (The WAITERS bit is always
654 * kept enabled while there is PI state around. We must also
655 * preserve the owner died bit.)
657 if (!(uval & FUTEX_OWNER_DIED)) {
660 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
662 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
664 if (curval == -EFAULT)
669 spin_unlock(&pi_state->pi_mutex.wait_lock);
674 spin_lock_irq(&pi_state->owner->pi_lock);
675 WARN_ON(list_empty(&pi_state->list));
676 list_del_init(&pi_state->list);
677 spin_unlock_irq(&pi_state->owner->pi_lock);
679 spin_lock_irq(&new_owner->pi_lock);
680 WARN_ON(!list_empty(&pi_state->list));
681 list_add(&pi_state->list, &new_owner->pi_state_list);
682 pi_state->owner = new_owner;
683 spin_unlock_irq(&new_owner->pi_lock);
685 spin_unlock(&pi_state->pi_mutex.wait_lock);
686 rt_mutex_unlock(&pi_state->pi_mutex);
691 static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
696 * There is no waiter, so we unlock the futex. The owner died
697 * bit has not to be preserved here. We are the owner:
699 oldval = cmpxchg_futex_value_locked(uaddr, uval, 0);
701 if (oldval == -EFAULT)
710 * Express the locking dependencies for lockdep:
713 double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
716 spin_lock(&hb1->lock);
718 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
719 } else { /* hb1 > hb2 */
720 spin_lock(&hb2->lock);
721 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
726 * Wake up all waiters hashed on the physical page that is mapped
727 * to this virtual address:
729 static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
732 struct futex_hash_bucket *hb;
733 struct futex_q *this, *next;
734 struct plist_head *head;
738 futex_lock_mm(fshared);
740 ret = get_futex_key(uaddr, fshared, &key);
741 if (unlikely(ret != 0))
744 hb = hash_futex(&key);
745 spin_lock(&hb->lock);
748 plist_for_each_entry_safe(this, next, head, list) {
749 if (match_futex (&this->key, &key)) {
750 if (this->pi_state) {
755 if (++ret >= nr_wake)
760 spin_unlock(&hb->lock);
762 futex_unlock_mm(fshared);
767 * Wake up all waiters hashed on the physical page that is mapped
768 * to this virtual address:
771 futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared,
773 int nr_wake, int nr_wake2, int op)
775 union futex_key key1, key2;
776 struct futex_hash_bucket *hb1, *hb2;
777 struct plist_head *head;
778 struct futex_q *this, *next;
779 int ret, op_ret, attempt = 0;
782 futex_lock_mm(fshared);
784 ret = get_futex_key(uaddr1, fshared, &key1);
785 if (unlikely(ret != 0))
787 ret = get_futex_key(uaddr2, fshared, &key2);
788 if (unlikely(ret != 0))
791 hb1 = hash_futex(&key1);
792 hb2 = hash_futex(&key2);
795 double_lock_hb(hb1, hb2);
797 op_ret = futex_atomic_op_inuser(op, uaddr2);
798 if (unlikely(op_ret < 0)) {
801 spin_unlock(&hb1->lock);
803 spin_unlock(&hb2->lock);
807 * we don't get EFAULT from MMU faults if we don't have an MMU,
808 * but we might get them from range checking
814 if (unlikely(op_ret != -EFAULT)) {
820 * futex_atomic_op_inuser needs to both read and write
821 * *(int __user *)uaddr2, but we can't modify it
822 * non-atomically. Therefore, if get_user below is not
823 * enough, we need to handle the fault ourselves, while
824 * still holding the mmap_sem.
827 ret = futex_handle_fault((unsigned long)uaddr2,
835 * If we would have faulted, release mmap_sem,
836 * fault it in and start all over again.
838 futex_unlock_mm(fshared);
840 ret = get_user(dummy, uaddr2);
849 plist_for_each_entry_safe(this, next, head, list) {
850 if (match_futex (&this->key, &key1)) {
852 if (++ret >= nr_wake)
861 plist_for_each_entry_safe(this, next, head, list) {
862 if (match_futex (&this->key, &key2)) {
864 if (++op_ret >= nr_wake2)
871 spin_unlock(&hb1->lock);
873 spin_unlock(&hb2->lock);
875 futex_unlock_mm(fshared);
881 * Requeue all waiters hashed on one physical page to another
884 static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
886 int nr_wake, int nr_requeue, u32 *cmpval)
888 union futex_key key1, key2;
889 struct futex_hash_bucket *hb1, *hb2;
890 struct plist_head *head1;
891 struct futex_q *this, *next;
892 int ret, drop_count = 0;
895 futex_lock_mm(fshared);
897 ret = get_futex_key(uaddr1, fshared, &key1);
898 if (unlikely(ret != 0))
900 ret = get_futex_key(uaddr2, fshared, &key2);
901 if (unlikely(ret != 0))
904 hb1 = hash_futex(&key1);
905 hb2 = hash_futex(&key2);
907 double_lock_hb(hb1, hb2);
909 if (likely(cmpval != NULL)) {
912 ret = get_futex_value_locked(&curval, uaddr1);
915 spin_unlock(&hb1->lock);
917 spin_unlock(&hb2->lock);
920 * If we would have faulted, release mmap_sem, fault
921 * it in and start all over again.
923 futex_unlock_mm(fshared);
925 ret = get_user(curval, uaddr1);
932 if (curval != *cmpval) {
939 plist_for_each_entry_safe(this, next, head1, list) {
940 if (!match_futex (&this->key, &key1))
942 if (++ret <= nr_wake) {
946 * If key1 and key2 hash to the same bucket, no need to
949 if (likely(head1 != &hb2->chain)) {
950 plist_del(&this->list, &hb1->chain);
951 plist_add(&this->list, &hb2->chain);
952 this->lock_ptr = &hb2->lock;
953 #ifdef CONFIG_DEBUG_PI_LIST
954 this->list.plist.lock = &hb2->lock;
958 get_futex_key_refs(&key2);
961 if (ret - nr_wake >= nr_requeue)
967 spin_unlock(&hb1->lock);
969 spin_unlock(&hb2->lock);
971 /* drop_futex_key_refs() must be called outside the spinlocks. */
972 while (--drop_count >= 0)
973 drop_futex_key_refs(&key1);
976 futex_unlock_mm(fshared);
980 /* The key must be already stored in q->key. */
981 static inline struct futex_hash_bucket *
982 queue_lock(struct futex_q *q, int fd, struct file *filp)
984 struct futex_hash_bucket *hb;
989 init_waitqueue_head(&q->waiters);
991 get_futex_key_refs(&q->key);
992 hb = hash_futex(&q->key);
993 q->lock_ptr = &hb->lock;
995 spin_lock(&hb->lock);
999 static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
1004 * The priority used to register this element is
1005 * - either the real thread-priority for the real-time threads
1006 * (i.e. threads with a priority lower than MAX_RT_PRIO)
1007 * - or MAX_RT_PRIO for non-RT threads.
1008 * Thus, all RT-threads are woken first in priority order, and
1009 * the others are woken last, in FIFO order.
1011 prio = min(current->normal_prio, MAX_RT_PRIO);
1013 plist_node_init(&q->list, prio);
1014 #ifdef CONFIG_DEBUG_PI_LIST
1015 q->list.plist.lock = &hb->lock;
1017 plist_add(&q->list, &hb->chain);
1019 spin_unlock(&hb->lock);
1023 queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
1025 spin_unlock(&hb->lock);
1026 drop_futex_key_refs(&q->key);
1030 * queue_me and unqueue_me must be called as a pair, each
1031 * exactly once. They are called with the hashed spinlock held.
1034 /* The key must be already stored in q->key. */
1035 static void queue_me(struct futex_q *q, int fd, struct file *filp)
1037 struct futex_hash_bucket *hb;
1039 hb = queue_lock(q, fd, filp);
1043 /* Return 1 if we were still queued (ie. 0 means we were woken) */
1044 static int unqueue_me(struct futex_q *q)
1046 spinlock_t *lock_ptr;
1049 /* In the common case we don't take the spinlock, which is nice. */
1051 lock_ptr = q->lock_ptr;
1053 if (lock_ptr != NULL) {
1054 spin_lock(lock_ptr);
1056 * q->lock_ptr can change between reading it and
1057 * spin_lock(), causing us to take the wrong lock. This
1058 * corrects the race condition.
1060 * Reasoning goes like this: if we have the wrong lock,
1061 * q->lock_ptr must have changed (maybe several times)
1062 * between reading it and the spin_lock(). It can
1063 * change again after the spin_lock() but only if it was
1064 * already changed before the spin_lock(). It cannot,
1065 * however, change back to the original value. Therefore
1066 * we can detect whether we acquired the correct lock.
1068 if (unlikely(lock_ptr != q->lock_ptr)) {
1069 spin_unlock(lock_ptr);
1072 WARN_ON(plist_node_empty(&q->list));
1073 plist_del(&q->list, &q->list.plist);
1075 BUG_ON(q->pi_state);
1077 spin_unlock(lock_ptr);
1081 drop_futex_key_refs(&q->key);
1086 * PI futexes can not be requeued and must remove themself from the
1087 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
1090 static void unqueue_me_pi(struct futex_q *q)
1092 WARN_ON(plist_node_empty(&q->list));
1093 plist_del(&q->list, &q->list.plist);
1095 BUG_ON(!q->pi_state);
1096 free_pi_state(q->pi_state);
1099 spin_unlock(q->lock_ptr);
1101 drop_futex_key_refs(&q->key);
1105 * Fixup the pi_state owner with current.
1107 * Must be called with hash bucket lock held and mm->sem held for non
1110 static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
1111 struct task_struct *curr)
1113 u32 newtid = task_pid_vnr(curr) | FUTEX_WAITERS;
1114 struct futex_pi_state *pi_state = q->pi_state;
1115 u32 uval, curval, newval;
1119 if (pi_state->owner != NULL) {
1120 spin_lock_irq(&pi_state->owner->pi_lock);
1121 WARN_ON(list_empty(&pi_state->list));
1122 list_del_init(&pi_state->list);
1123 spin_unlock_irq(&pi_state->owner->pi_lock);
1125 newtid |= FUTEX_OWNER_DIED;
1127 pi_state->owner = curr;
1129 spin_lock_irq(&curr->pi_lock);
1130 WARN_ON(!list_empty(&pi_state->list));
1131 list_add(&pi_state->list, &curr->pi_state_list);
1132 spin_unlock_irq(&curr->pi_lock);
1135 * We own it, so we have to replace the pending owner
1136 * TID. This must be atomic as we have preserve the
1137 * owner died bit here.
1139 ret = get_futex_value_locked(&uval, uaddr);
1142 newval = (uval & FUTEX_OWNER_DIED) | newtid;
1144 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
1146 if (curval == -EFAULT)
1156 * In case we must use restart_block to restart a futex_wait,
1157 * we encode in the 'arg3' shared capability
1159 #define ARG3_SHARED 1
1161 static long futex_wait_restart(struct restart_block *restart);
1163 static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1164 u32 val, ktime_t *abs_time)
1166 struct task_struct *curr = current;
1167 DECLARE_WAITQUEUE(wait, curr);
1168 struct futex_hash_bucket *hb;
1172 struct hrtimer_sleeper t;
1177 futex_lock_mm(fshared);
1179 ret = get_futex_key(uaddr, fshared, &q.key);
1180 if (unlikely(ret != 0))
1181 goto out_release_sem;
1183 hb = queue_lock(&q, -1, NULL);
1186 * Access the page AFTER the futex is queued.
1187 * Order is important:
1189 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
1190 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
1192 * The basic logical guarantee of a futex is that it blocks ONLY
1193 * if cond(var) is known to be true at the time of blocking, for
1194 * any cond. If we queued after testing *uaddr, that would open
1195 * a race condition where we could block indefinitely with
1196 * cond(var) false, which would violate the guarantee.
1198 * A consequence is that futex_wait() can return zero and absorb
1199 * a wakeup when *uaddr != val on entry to the syscall. This is
1202 * for shared futexes, we hold the mmap semaphore, so the mapping
1203 * cannot have changed since we looked it up in get_futex_key.
1205 ret = get_futex_value_locked(&uval, uaddr);
1207 if (unlikely(ret)) {
1208 queue_unlock(&q, hb);
1211 * If we would have faulted, release mmap_sem, fault it in and
1212 * start all over again.
1214 futex_unlock_mm(fshared);
1216 ret = get_user(uval, uaddr);
1224 goto out_unlock_release_sem;
1226 /* Only actually queue if *uaddr contained val. */
1230 * Now the futex is queued and we have checked the data, we
1231 * don't want to hold mmap_sem while we sleep.
1233 futex_unlock_mm(fshared);
1236 * There might have been scheduling since the queue_me(), as we
1237 * cannot hold a spinlock across the get_user() in case it
1238 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
1239 * queueing ourselves into the futex hash. This code thus has to
1240 * rely on the futex_wake() code removing us from hash when it
1244 /* add_wait_queue is the barrier after __set_current_state. */
1245 __set_current_state(TASK_INTERRUPTIBLE);
1246 add_wait_queue(&q.waiters, &wait);
1248 * !plist_node_empty() is safe here without any lock.
1249 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
1251 if (likely(!plist_node_empty(&q.list))) {
1255 hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1256 hrtimer_init_sleeper(&t, current);
1257 t.timer.expires = *abs_time;
1259 hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS);
1262 * the timer could have already expired, in which
1263 * case current would be flagged for rescheduling.
1264 * Don't bother calling schedule.
1269 hrtimer_cancel(&t.timer);
1271 /* Flag if a timeout occured */
1272 rem = (t.task == NULL);
1275 __set_current_state(TASK_RUNNING);
1278 * NOTE: we don't remove ourselves from the waitqueue because
1279 * we are the only user of it.
1282 /* If we were woken (and unqueued), we succeeded, whatever. */
1283 if (!unqueue_me(&q))
1289 * We expect signal_pending(current), but another thread may
1290 * have handled it for us already.
1293 return -ERESTARTSYS;
1295 struct restart_block *restart;
1296 restart = ¤t_thread_info()->restart_block;
1297 restart->fn = futex_wait_restart;
1298 restart->arg0 = (unsigned long)uaddr;
1299 restart->arg1 = (unsigned long)val;
1300 restart->arg2 = (unsigned long)abs_time;
1303 restart->arg3 |= ARG3_SHARED;
1304 return -ERESTART_RESTARTBLOCK;
1307 out_unlock_release_sem:
1308 queue_unlock(&q, hb);
1311 futex_unlock_mm(fshared);
1316 static long futex_wait_restart(struct restart_block *restart)
1318 u32 __user *uaddr = (u32 __user *)restart->arg0;
1319 u32 val = (u32)restart->arg1;
1320 ktime_t *abs_time = (ktime_t *)restart->arg2;
1321 struct rw_semaphore *fshared = NULL;
1323 restart->fn = do_no_restart_syscall;
1324 if (restart->arg3 & ARG3_SHARED)
1325 fshared = ¤t->mm->mmap_sem;
1326 return (long)futex_wait(uaddr, fshared, val, abs_time);
1331 * Userspace tried a 0 -> TID atomic transition of the futex value
1332 * and failed. The kernel side here does the whole locking operation:
1333 * if there are waiters then it will block, it does PI, etc. (Due to
1334 * races the kernel might see a 0 value of the futex too.)
1336 static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1337 int detect, ktime_t *time, int trylock)
1339 struct hrtimer_sleeper timeout, *to = NULL;
1340 struct task_struct *curr = current;
1341 struct futex_hash_bucket *hb;
1342 u32 uval, newval, curval;
1344 int ret, lock_taken, ownerdied = 0, attempt = 0;
1346 if (refill_pi_state_cache())
1351 hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
1352 hrtimer_init_sleeper(to, current);
1353 to->timer.expires = *time;
1358 futex_lock_mm(fshared);
1360 ret = get_futex_key(uaddr, fshared, &q.key);
1361 if (unlikely(ret != 0))
1362 goto out_release_sem;
1365 hb = queue_lock(&q, -1, NULL);
1368 ret = lock_taken = 0;
1371 * To avoid races, we attempt to take the lock here again
1372 * (by doing a 0 -> TID atomic cmpxchg), while holding all
1373 * the locks. It will most likely not succeed.
1375 newval = task_pid_vnr(current);
1377 curval = cmpxchg_futex_value_locked(uaddr, 0, newval);
1379 if (unlikely(curval == -EFAULT))
1383 * Detect deadlocks. In case of REQUEUE_PI this is a valid
1384 * situation and we return success to user space.
1386 if (unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(current))) {
1388 goto out_unlock_release_sem;
1392 * Surprise - we got the lock. Just return to userspace:
1394 if (unlikely(!curval))
1395 goto out_unlock_release_sem;
1400 * Set the WAITERS flag, so the owner will know it has someone
1401 * to wake at next unlock
1403 newval = curval | FUTEX_WAITERS;
1406 * There are two cases, where a futex might have no owner (the
1407 * owner TID is 0): OWNER_DIED. We take over the futex in this
1408 * case. We also do an unconditional take over, when the owner
1409 * of the futex died.
1411 * This is safe as we are protected by the hash bucket lock !
1413 if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
1414 /* Keep the OWNER_DIED bit */
1415 newval = (curval & ~FUTEX_TID_MASK) | task_pid_vnr(current);
1420 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
1422 if (unlikely(curval == -EFAULT))
1424 if (unlikely(curval != uval))
1428 * We took the lock due to owner died take over.
1430 if (unlikely(lock_taken))
1431 goto out_unlock_release_sem;
1434 * We dont have the lock. Look up the PI state (or create it if
1435 * we are the first waiter):
1437 ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
1439 if (unlikely(ret)) {
1444 * Task is exiting and we just wait for the
1447 queue_unlock(&q, hb);
1448 futex_unlock_mm(fshared);
1454 * No owner found for this futex. Check if the
1455 * OWNER_DIED bit is set to figure out whether
1456 * this is a robust futex or not.
1458 if (get_futex_value_locked(&curval, uaddr))
1462 * We simply start over in case of a robust
1463 * futex. The code above will take the futex
1466 if (curval & FUTEX_OWNER_DIED) {
1471 goto out_unlock_release_sem;
1476 * Only actually queue now that the atomic ops are done:
1481 * Now the futex is queued and we have checked the data, we
1482 * don't want to hold mmap_sem while we sleep.
1484 futex_unlock_mm(fshared);
1486 WARN_ON(!q.pi_state);
1488 * Block on the PI mutex:
1491 ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
1493 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
1494 /* Fixup the trylock return value: */
1495 ret = ret ? 0 : -EWOULDBLOCK;
1498 futex_lock_mm(fshared);
1499 spin_lock(q.lock_ptr);
1503 * Got the lock. We might not be the anticipated owner
1504 * if we did a lock-steal - fix up the PI-state in
1507 if (q.pi_state->owner != curr)
1508 ret = fixup_pi_state_owner(uaddr, &q, curr);
1511 * Catch the rare case, where the lock was released
1512 * when we were on the way back before we locked the
1515 if (q.pi_state->owner == curr &&
1516 rt_mutex_trylock(&q.pi_state->pi_mutex)) {
1520 * Paranoia check. If we did not take the lock
1521 * in the trylock above, then we should not be
1522 * the owner of the rtmutex, neither the real
1523 * nor the pending one:
1525 if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr)
1526 printk(KERN_ERR "futex_lock_pi: ret = %d "
1527 "pi-mutex: %p pi-state %p\n", ret,
1528 q.pi_state->pi_mutex.owner,
1533 /* Unqueue and drop the lock */
1535 futex_unlock_mm(fshared);
1537 return ret != -EINTR ? ret : -ERESTARTNOINTR;
1539 out_unlock_release_sem:
1540 queue_unlock(&q, hb);
1543 futex_unlock_mm(fshared);
1548 * We have to r/w *(int __user *)uaddr, but we can't modify it
1549 * non-atomically. Therefore, if get_user below is not
1550 * enough, we need to handle the fault ourselves, while
1551 * still holding the mmap_sem.
1553 * ... and hb->lock. :-) --ANK
1555 queue_unlock(&q, hb);
1558 ret = futex_handle_fault((unsigned long)uaddr, fshared,
1561 goto out_release_sem;
1562 goto retry_unlocked;
1565 futex_unlock_mm(fshared);
1567 ret = get_user(uval, uaddr);
1568 if (!ret && (uval != -EFAULT))
1575 * Userspace attempted a TID -> 0 atomic transition, and failed.
1576 * This is the in-kernel slowpath: we look up the PI state (if any),
1577 * and do the rt-mutex unlock.
1579 static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared)
1581 struct futex_hash_bucket *hb;
1582 struct futex_q *this, *next;
1584 struct plist_head *head;
1585 union futex_key key;
1586 int ret, attempt = 0;
1589 if (get_user(uval, uaddr))
1592 * We release only a lock we actually own:
1594 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current))
1597 * First take all the futex related locks:
1599 futex_lock_mm(fshared);
1601 ret = get_futex_key(uaddr, fshared, &key);
1602 if (unlikely(ret != 0))
1605 hb = hash_futex(&key);
1607 spin_lock(&hb->lock);
1610 * To avoid races, try to do the TID -> 0 atomic transition
1611 * again. If it succeeds then we can return without waking
1614 if (!(uval & FUTEX_OWNER_DIED))
1615 uval = cmpxchg_futex_value_locked(uaddr, task_pid_vnr(current), 0);
1618 if (unlikely(uval == -EFAULT))
1621 * Rare case: we managed to release the lock atomically,
1622 * no need to wake anyone else up:
1624 if (unlikely(uval == task_pid_vnr(current)))
1628 * Ok, other tasks may need to be woken up - check waiters
1629 * and do the wakeup if necessary:
1633 plist_for_each_entry_safe(this, next, head, list) {
1634 if (!match_futex (&this->key, &key))
1636 ret = wake_futex_pi(uaddr, uval, this);
1638 * The atomic access to the futex value
1639 * generated a pagefault, so retry the
1640 * user-access and the wakeup:
1647 * No waiters - kernel unlocks the futex:
1649 if (!(uval & FUTEX_OWNER_DIED)) {
1650 ret = unlock_futex_pi(uaddr, uval);
1656 spin_unlock(&hb->lock);
1658 futex_unlock_mm(fshared);
1664 * We have to r/w *(int __user *)uaddr, but we can't modify it
1665 * non-atomically. Therefore, if get_user below is not
1666 * enough, we need to handle the fault ourselves, while
1667 * still holding the mmap_sem.
1669 * ... and hb->lock. --ANK
1671 spin_unlock(&hb->lock);
1674 ret = futex_handle_fault((unsigned long)uaddr, fshared,
1679 goto retry_unlocked;
1682 futex_unlock_mm(fshared);
1684 ret = get_user(uval, uaddr);
1685 if (!ret && (uval != -EFAULT))
1691 static int futex_close(struct inode *inode, struct file *filp)
1693 struct futex_q *q = filp->private_data;
1701 /* This is one-shot: once it's gone off you need a new fd */
1702 static unsigned int futex_poll(struct file *filp,
1703 struct poll_table_struct *wait)
1705 struct futex_q *q = filp->private_data;
1708 poll_wait(filp, &q->waiters, wait);
1711 * plist_node_empty() is safe here without any lock.
1712 * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
1714 if (plist_node_empty(&q->list))
1715 ret = POLLIN | POLLRDNORM;
1720 static const struct file_operations futex_fops = {
1721 .release = futex_close,
1726 * Signal allows caller to avoid the race which would occur if they
1727 * set the sigio stuff up afterwards.
1729 static int futex_fd(u32 __user *uaddr, int signal)
1734 struct rw_semaphore *fshared;
1735 static unsigned long printk_interval;
1737 if (printk_timed_ratelimit(&printk_interval, 60 * 60 * 1000)) {
1738 printk(KERN_WARNING "Process `%s' used FUTEX_FD, which "
1739 "will be removed from the kernel in June 2007\n",
1744 if (!valid_signal(signal))
1747 ret = get_unused_fd();
1750 filp = get_empty_filp();
1756 filp->f_op = &futex_fops;
1757 filp->f_path.mnt = mntget(futex_mnt);
1758 filp->f_path.dentry = dget(futex_mnt->mnt_root);
1759 filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
1762 err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1);
1766 filp->f_owner.signum = signal;
1769 q = kmalloc(sizeof(*q), GFP_KERNEL);
1776 fshared = ¤t->mm->mmap_sem;
1778 err = get_futex_key(uaddr, fshared, &q->key);
1780 if (unlikely(err != 0)) {
1787 * queue_me() must be called before releasing mmap_sem, because
1788 * key->shared.inode needs to be referenced while holding it.
1790 filp->private_data = q;
1792 queue_me(q, ret, filp);
1795 /* Now we map fd to filp, so userspace can access it */
1796 fd_install(ret, filp);
1807 * Support for robust futexes: the kernel cleans up held futexes at
1810 * Implementation: user-space maintains a per-thread list of locks it
1811 * is holding. Upon do_exit(), the kernel carefully walks this list,
1812 * and marks all locks that are owned by this thread with the
1813 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
1814 * always manipulated with the lock held, so the list is private and
1815 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
1816 * field, to allow the kernel to clean up if the thread dies after
1817 * acquiring the lock, but just before it could have added itself to
1818 * the list. There can only be one such pending lock.
1822 * sys_set_robust_list - set the robust-futex list head of a task
1823 * @head: pointer to the list-head
1824 * @len: length of the list-head, as userspace expects
1827 sys_set_robust_list(struct robust_list_head __user *head,
1831 * The kernel knows only one size for now:
1833 if (unlikely(len != sizeof(*head)))
1836 current->robust_list = head;
1842 * sys_get_robust_list - get the robust-futex list head of a task
1843 * @pid: pid of the process [zero for current task]
1844 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
1845 * @len_ptr: pointer to a length field, the kernel fills in the header size
1848 sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
1849 size_t __user *len_ptr)
1851 struct robust_list_head __user *head;
1855 head = current->robust_list;
1857 struct task_struct *p;
1861 p = find_task_by_pid_ns(pid,
1862 current->nsproxy->pid_ns);
1866 if ((current->euid != p->euid) && (current->euid != p->uid) &&
1867 !capable(CAP_SYS_PTRACE))
1869 head = p->robust_list;
1873 if (put_user(sizeof(*head), len_ptr))
1875 return put_user(head, head_ptr);
1884 * Process a futex-list entry, check whether it's owned by the
1885 * dying task, and do notification if so:
1887 int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
1889 u32 uval, nval, mval;
1892 if (get_user(uval, uaddr))
1895 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
1897 * Ok, this dying thread is truly holding a futex
1898 * of interest. Set the OWNER_DIED bit atomically
1899 * via cmpxchg, and if the value had FUTEX_WAITERS
1900 * set, wake up a waiter (if any). (We have to do a
1901 * futex_wake() even if OWNER_DIED is already set -
1902 * to handle the rare but possible case of recursive
1903 * thread-death.) The rest of the cleanup is done in
1906 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1907 nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
1909 if (nval == -EFAULT)
1916 * Wake robust non-PI futexes here. The wakeup of
1917 * PI futexes happens in exit_pi_state():
1919 if (!pi && (uval & FUTEX_WAITERS))
1920 futex_wake(uaddr, &curr->mm->mmap_sem, 1);
1926 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
1928 static inline int fetch_robust_entry(struct robust_list __user **entry,
1929 struct robust_list __user * __user *head,
1932 unsigned long uentry;
1934 if (get_user(uentry, (unsigned long __user *)head))
1937 *entry = (void __user *)(uentry & ~1UL);
1944 * Walk curr->robust_list (very carefully, it's a userspace list!)
1945 * and mark any locks found there dead, and notify any waiters.
1947 * We silently return on any sign of list-walking problem.
1949 void exit_robust_list(struct task_struct *curr)
1951 struct robust_list_head __user *head = curr->robust_list;
1952 struct robust_list __user *entry, *next_entry, *pending;
1953 unsigned int limit = ROBUST_LIST_LIMIT, pi, next_pi, pip;
1954 unsigned long futex_offset;
1958 * Fetch the list head (which was registered earlier, via
1959 * sys_set_robust_list()):
1961 if (fetch_robust_entry(&entry, &head->list.next, &pi))
1964 * Fetch the relative futex offset:
1966 if (get_user(futex_offset, &head->futex_offset))
1969 * Fetch any possibly pending lock-add first, and handle it
1972 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
1975 next_entry = NULL; /* avoid warning with gcc */
1976 while (entry != &head->list) {
1978 * Fetch the next entry in the list before calling
1979 * handle_futex_death:
1981 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
1983 * A pending lock might already be on the list, so
1984 * don't process it twice:
1986 if (entry != pending)
1987 if (handle_futex_death((void __user *)entry + futex_offset,
1995 * Avoid excessively long or circular lists:
2004 handle_futex_death((void __user *)pending + futex_offset,
2008 long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
2009 u32 __user *uaddr2, u32 val2, u32 val3)
2012 int cmd = op & FUTEX_CMD_MASK;
2013 struct rw_semaphore *fshared = NULL;
2015 if (!(op & FUTEX_PRIVATE_FLAG))
2016 fshared = ¤t->mm->mmap_sem;
2020 ret = futex_wait(uaddr, fshared, val, timeout);
2023 ret = futex_wake(uaddr, fshared, val);
2026 /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
2027 ret = futex_fd(uaddr, val);
2030 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
2032 case FUTEX_CMP_REQUEUE:
2033 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
2036 ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
2039 ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
2041 case FUTEX_UNLOCK_PI:
2042 ret = futex_unlock_pi(uaddr, fshared);
2044 case FUTEX_TRYLOCK_PI:
2045 ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
2054 asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
2055 struct timespec __user *utime, u32 __user *uaddr2,
2059 ktime_t t, *tp = NULL;
2061 int cmd = op & FUTEX_CMD_MASK;
2063 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
2064 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
2066 if (!timespec_valid(&ts))
2069 t = timespec_to_ktime(ts);
2070 if (cmd == FUTEX_WAIT)
2071 t = ktime_add(ktime_get(), t);
2075 * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
2076 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
2078 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
2079 cmd == FUTEX_WAKE_OP)
2080 val2 = (u32) (unsigned long) utime;
2082 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
2085 static int futexfs_get_sb(struct file_system_type *fs_type,
2086 int flags, const char *dev_name, void *data,
2087 struct vfsmount *mnt)
2089 return get_sb_pseudo(fs_type, "futex", NULL, FUTEXFS_SUPER_MAGIC, mnt);
2092 static struct file_system_type futex_fs_type = {
2094 .get_sb = futexfs_get_sb,
2095 .kill_sb = kill_anon_super,
2098 static int __init init(void)
2100 int i = register_filesystem(&futex_fs_type);
2105 futex_mnt = kern_mount(&futex_fs_type);
2106 if (IS_ERR(futex_mnt)) {
2107 unregister_filesystem(&futex_fs_type);
2108 return PTR_ERR(futex_mnt);
2111 for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
2112 plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
2113 spin_lock_init(&futex_queues[i].lock);