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 * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
16 * enough at me, Linus for the original (flawed) idea, Matthew
17 * Kirkwood for proof-of-concept implementation.
19 * "The futexes are also cursed."
20 * "But they come in a choice of three flavours!"
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <linux/slab.h>
37 #include <linux/poll.h>
39 #include <linux/file.h>
40 #include <linux/jhash.h>
41 #include <linux/init.h>
42 #include <linux/futex.h>
43 #include <linux/mount.h>
44 #include <linux/pagemap.h>
45 #include <linux/syscalls.h>
46 #include <linux/signal.h>
47 #include <asm/futex.h>
49 #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
52 * Futexes are matched on equal values of this key.
53 * The key type depends on whether it's a shared or private mapping.
54 * Don't rearrange members without looking at hash_futex().
56 * offset is aligned to a multiple of sizeof(u32) (== 4) by definition.
57 * We set bit 0 to indicate if it's an inode-based key.
78 * We use this hashed waitqueue instead of a normal wait_queue_t, so
79 * we can wake only the relevant ones (hashed queues may be shared).
81 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
82 * It is considered woken when list_empty(&q->list) || q->lock_ptr == 0.
83 * The order of wakup is always to make the first condition true, then
84 * wake up q->waiters, then make the second condition true.
87 struct list_head list
;
88 wait_queue_head_t waiters
;
90 /* Which hash list lock to use. */
93 /* Key which the futex is hashed on. */
96 /* For fd, sigio sent using these. */
102 * Split the global futex_lock into every hash list lock.
104 struct futex_hash_bucket
{
106 struct list_head chain
;
109 static struct futex_hash_bucket futex_queues
[1<<FUTEX_HASHBITS
];
111 /* Futex-fs vfsmount entry: */
112 static struct vfsmount
*futex_mnt
;
115 * We hash on the keys returned from get_futex_key (see below).
117 static struct futex_hash_bucket
*hash_futex(union futex_key
*key
)
119 u32 hash
= jhash2((u32
*)&key
->both
.word
,
120 (sizeof(key
->both
.word
)+sizeof(key
->both
.ptr
))/4,
122 return &futex_queues
[hash
& ((1 << FUTEX_HASHBITS
)-1)];
126 * Return 1 if two futex_keys are equal, 0 otherwise.
128 static inline int match_futex(union futex_key
*key1
, union futex_key
*key2
)
130 return (key1
->both
.word
== key2
->both
.word
131 && key1
->both
.ptr
== key2
->both
.ptr
132 && key1
->both
.offset
== key2
->both
.offset
);
136 * Get parameters which are the keys for a futex.
138 * For shared mappings, it's (page->index, vma->vm_file->f_dentry->d_inode,
139 * offset_within_page). For private mappings, it's (uaddr, current->mm).
140 * We can usually work out the index without swapping in the page.
142 * Returns: 0, or negative error code.
143 * The key words are stored in *key on success.
145 * Should be called with ¤t->mm->mmap_sem but NOT any spinlocks.
147 static int get_futex_key(unsigned long uaddr
, union futex_key
*key
)
149 struct mm_struct
*mm
= current
->mm
;
150 struct vm_area_struct
*vma
;
155 * The futex address must be "naturally" aligned.
157 key
->both
.offset
= uaddr
% PAGE_SIZE
;
158 if (unlikely((key
->both
.offset
% sizeof(u32
)) != 0))
160 uaddr
-= key
->both
.offset
;
163 * The futex is hashed differently depending on whether
164 * it's in a shared or private mapping. So check vma first.
166 vma
= find_extend_vma(mm
, uaddr
);
173 if (unlikely((vma
->vm_flags
& (VM_IO
|VM_READ
)) != VM_READ
))
174 return (vma
->vm_flags
& VM_IO
) ? -EPERM
: -EACCES
;
177 * Private mappings are handled in a simple way.
179 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
180 * it's a read-only handle, it's expected that futexes attach to
181 * the object not the particular process. Therefore we use
182 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
183 * mappings of _writable_ handles.
185 if (likely(!(vma
->vm_flags
& VM_MAYSHARE
))) {
186 key
->private.mm
= mm
;
187 key
->private.uaddr
= uaddr
;
192 * Linear file mappings are also simple.
194 key
->shared
.inode
= vma
->vm_file
->f_dentry
->d_inode
;
195 key
->both
.offset
++; /* Bit 0 of offset indicates inode-based key. */
196 if (likely(!(vma
->vm_flags
& VM_NONLINEAR
))) {
197 key
->shared
.pgoff
= (((uaddr
- vma
->vm_start
) >> PAGE_SHIFT
)
203 * We could walk the page table to read the non-linear
204 * pte, and get the page index without fetching the page
205 * from swap. But that's a lot of code to duplicate here
206 * for a rare case, so we simply fetch the page.
208 err
= get_user_pages(current
, mm
, uaddr
, 1, 0, 0, &page
, NULL
);
211 page
->index
<< (PAGE_CACHE_SHIFT
- PAGE_SHIFT
);
219 * Take a reference to the resource addressed by a key.
220 * Can be called while holding spinlocks.
222 * NOTE: mmap_sem MUST be held between get_futex_key() and calling this
223 * function, if it is called at all. mmap_sem keeps key->shared.inode valid.
225 static inline void get_key_refs(union futex_key
*key
)
227 if (key
->both
.ptr
!= 0) {
228 if (key
->both
.offset
& 1)
229 atomic_inc(&key
->shared
.inode
->i_count
);
231 atomic_inc(&key
->private.mm
->mm_count
);
236 * Drop a reference to the resource addressed by a key.
237 * The hash bucket spinlock must not be held.
239 static void drop_key_refs(union futex_key
*key
)
241 if (key
->both
.ptr
!= 0) {
242 if (key
->both
.offset
& 1)
243 iput(key
->shared
.inode
);
245 mmdrop(key
->private.mm
);
249 static inline int get_futex_value_locked(int *dest
, int __user
*from
)
254 ret
= __copy_from_user_inatomic(dest
, from
, sizeof(int));
257 return ret
? -EFAULT
: 0;
261 * The hash bucket lock must be held when this is called.
262 * Afterwards, the futex_q must not be accessed.
264 static void wake_futex(struct futex_q
*q
)
266 list_del_init(&q
->list
);
268 send_sigio(&q
->filp
->f_owner
, q
->fd
, POLL_IN
);
270 * The lock in wake_up_all() is a crucial memory barrier after the
271 * list_del_init() and also before assigning to q->lock_ptr.
273 wake_up_all(&q
->waiters
);
275 * The waiting task can free the futex_q as soon as this is written,
276 * without taking any locks. This must come last.
278 * A memory barrier is required here to prevent the following store
279 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
280 * at the end of wake_up_all() does not prevent this store from
288 * Wake up all waiters hashed on the physical page that is mapped
289 * to this virtual address:
291 static int futex_wake(unsigned long uaddr
, int nr_wake
)
294 struct futex_hash_bucket
*bh
;
295 struct list_head
*head
;
296 struct futex_q
*this, *next
;
299 down_read(¤t
->mm
->mmap_sem
);
301 ret
= get_futex_key(uaddr
, &key
);
302 if (unlikely(ret
!= 0))
305 bh
= hash_futex(&key
);
306 spin_lock(&bh
->lock
);
309 list_for_each_entry_safe(this, next
, head
, list
) {
310 if (match_futex (&this->key
, &key
)) {
312 if (++ret
>= nr_wake
)
317 spin_unlock(&bh
->lock
);
319 up_read(¤t
->mm
->mmap_sem
);
324 * Wake up all waiters hashed on the physical page that is mapped
325 * to this virtual address:
327 static int futex_wake_op(unsigned long uaddr1
, unsigned long uaddr2
, int nr_wake
, int nr_wake2
, int op
)
329 union futex_key key1
, key2
;
330 struct futex_hash_bucket
*bh1
, *bh2
;
331 struct list_head
*head
;
332 struct futex_q
*this, *next
;
333 int ret
, op_ret
, attempt
= 0;
336 down_read(¤t
->mm
->mmap_sem
);
338 ret
= get_futex_key(uaddr1
, &key1
);
339 if (unlikely(ret
!= 0))
341 ret
= get_futex_key(uaddr2
, &key2
);
342 if (unlikely(ret
!= 0))
345 bh1
= hash_futex(&key1
);
346 bh2
= hash_futex(&key2
);
350 spin_lock(&bh1
->lock
);
351 spin_lock(&bh2
->lock
);
353 spin_lock(&bh1
->lock
);
355 op_ret
= futex_atomic_op_inuser(op
, (int __user
*)uaddr2
);
356 if (unlikely(op_ret
< 0)) {
359 spin_unlock(&bh1
->lock
);
361 spin_unlock(&bh2
->lock
);
364 /* we don't get EFAULT from MMU faults if we don't have an MMU,
365 * but we might get them from range checking */
370 if (unlikely(op_ret
!= -EFAULT
)) {
375 /* futex_atomic_op_inuser needs to both read and write
376 * *(int __user *)uaddr2, but we can't modify it
377 * non-atomically. Therefore, if get_user below is not
378 * enough, we need to handle the fault ourselves, while
379 * still holding the mmap_sem. */
381 struct vm_area_struct
* vma
;
382 struct mm_struct
*mm
= current
->mm
;
386 !(vma
= find_vma(mm
, uaddr2
)) ||
387 vma
->vm_start
> uaddr2
||
388 !(vma
->vm_flags
& VM_WRITE
))
391 switch (handle_mm_fault(mm
, vma
, uaddr2
, 1)) {
404 /* If we would have faulted, release mmap_sem,
405 * fault it in and start all over again. */
406 up_read(¤t
->mm
->mmap_sem
);
408 ret
= get_user(dummy
, (int __user
*)uaddr2
);
417 list_for_each_entry_safe(this, next
, head
, list
) {
418 if (match_futex (&this->key
, &key1
)) {
420 if (++ret
>= nr_wake
)
429 list_for_each_entry_safe(this, next
, head
, list
) {
430 if (match_futex (&this->key
, &key2
)) {
432 if (++op_ret
>= nr_wake2
)
439 spin_unlock(&bh1
->lock
);
441 spin_unlock(&bh2
->lock
);
443 up_read(¤t
->mm
->mmap_sem
);
448 * Requeue all waiters hashed on one physical page to another
451 static int futex_requeue(unsigned long uaddr1
, unsigned long uaddr2
,
452 int nr_wake
, int nr_requeue
, int *valp
)
454 union futex_key key1
, key2
;
455 struct futex_hash_bucket
*bh1
, *bh2
;
456 struct list_head
*head1
;
457 struct futex_q
*this, *next
;
458 int ret
, drop_count
= 0;
461 down_read(¤t
->mm
->mmap_sem
);
463 ret
= get_futex_key(uaddr1
, &key1
);
464 if (unlikely(ret
!= 0))
466 ret
= get_futex_key(uaddr2
, &key2
);
467 if (unlikely(ret
!= 0))
470 bh1
= hash_futex(&key1
);
471 bh2
= hash_futex(&key2
);
474 spin_lock(&bh1
->lock
);
475 spin_lock(&bh2
->lock
);
477 spin_lock(&bh1
->lock
);
479 if (likely(valp
!= NULL
)) {
482 ret
= get_futex_value_locked(&curval
, (int __user
*)uaddr1
);
485 spin_unlock(&bh1
->lock
);
487 spin_unlock(&bh2
->lock
);
489 /* If we would have faulted, release mmap_sem, fault
490 * it in and start all over again.
492 up_read(¤t
->mm
->mmap_sem
);
494 ret
= get_user(curval
, (int __user
*)uaddr1
);
501 if (curval
!= *valp
) {
508 list_for_each_entry_safe(this, next
, head1
, list
) {
509 if (!match_futex (&this->key
, &key1
))
511 if (++ret
<= nr_wake
) {
514 list_move_tail(&this->list
, &bh2
->chain
);
515 this->lock_ptr
= &bh2
->lock
;
520 if (ret
- nr_wake
>= nr_requeue
)
522 /* Make sure to stop if key1 == key2 */
523 if (head1
== &bh2
->chain
&& head1
!= &next
->list
)
529 spin_unlock(&bh1
->lock
);
531 spin_unlock(&bh2
->lock
);
533 /* drop_key_refs() must be called outside the spinlocks. */
534 while (--drop_count
>= 0)
535 drop_key_refs(&key1
);
538 up_read(¤t
->mm
->mmap_sem
);
542 /* The key must be already stored in q->key. */
543 static inline struct futex_hash_bucket
*
544 queue_lock(struct futex_q
*q
, int fd
, struct file
*filp
)
546 struct futex_hash_bucket
*bh
;
551 init_waitqueue_head(&q
->waiters
);
553 get_key_refs(&q
->key
);
554 bh
= hash_futex(&q
->key
);
555 q
->lock_ptr
= &bh
->lock
;
557 spin_lock(&bh
->lock
);
561 static inline void __queue_me(struct futex_q
*q
, struct futex_hash_bucket
*bh
)
563 list_add_tail(&q
->list
, &bh
->chain
);
564 spin_unlock(&bh
->lock
);
568 queue_unlock(struct futex_q
*q
, struct futex_hash_bucket
*bh
)
570 spin_unlock(&bh
->lock
);
571 drop_key_refs(&q
->key
);
575 * queue_me and unqueue_me must be called as a pair, each
576 * exactly once. They are called with the hashed spinlock held.
579 /* The key must be already stored in q->key. */
580 static void queue_me(struct futex_q
*q
, int fd
, struct file
*filp
)
582 struct futex_hash_bucket
*bh
;
583 bh
= queue_lock(q
, fd
, filp
);
587 /* Return 1 if we were still queued (ie. 0 means we were woken) */
588 static int unqueue_me(struct futex_q
*q
)
591 spinlock_t
*lock_ptr
;
593 /* In the common case we don't take the spinlock, which is nice. */
595 lock_ptr
= q
->lock_ptr
;
599 * q->lock_ptr can change between reading it and
600 * spin_lock(), causing us to take the wrong lock. This
601 * corrects the race condition.
603 * Reasoning goes like this: if we have the wrong lock,
604 * q->lock_ptr must have changed (maybe several times)
605 * between reading it and the spin_lock(). It can
606 * change again after the spin_lock() but only if it was
607 * already changed before the spin_lock(). It cannot,
608 * however, change back to the original value. Therefore
609 * we can detect whether we acquired the correct lock.
611 if (unlikely(lock_ptr
!= q
->lock_ptr
)) {
612 spin_unlock(lock_ptr
);
615 WARN_ON(list_empty(&q
->list
));
617 spin_unlock(lock_ptr
);
621 drop_key_refs(&q
->key
);
625 static int futex_wait(unsigned long uaddr
, int val
, unsigned long time
)
627 DECLARE_WAITQUEUE(wait
, current
);
630 struct futex_hash_bucket
*bh
;
633 down_read(¤t
->mm
->mmap_sem
);
635 ret
= get_futex_key(uaddr
, &q
.key
);
636 if (unlikely(ret
!= 0))
637 goto out_release_sem
;
639 bh
= queue_lock(&q
, -1, NULL
);
642 * Access the page AFTER the futex is queued.
643 * Order is important:
645 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
646 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
648 * The basic logical guarantee of a futex is that it blocks ONLY
649 * if cond(var) is known to be true at the time of blocking, for
650 * any cond. If we queued after testing *uaddr, that would open
651 * a race condition where we could block indefinitely with
652 * cond(var) false, which would violate the guarantee.
654 * A consequence is that futex_wait() can return zero and absorb
655 * a wakeup when *uaddr != val on entry to the syscall. This is
658 * We hold the mmap semaphore, so the mapping cannot have changed
659 * since we looked it up in get_futex_key.
662 ret
= get_futex_value_locked(&curval
, (int __user
*)uaddr
);
665 queue_unlock(&q
, bh
);
667 /* If we would have faulted, release mmap_sem, fault it in and
668 * start all over again.
670 up_read(¤t
->mm
->mmap_sem
);
672 ret
= get_user(curval
, (int __user
*)uaddr
);
680 queue_unlock(&q
, bh
);
681 goto out_release_sem
;
684 /* Only actually queue if *uaddr contained val. */
688 * Now the futex is queued and we have checked the data, we
689 * don't want to hold mmap_sem while we sleep.
691 up_read(¤t
->mm
->mmap_sem
);
694 * There might have been scheduling since the queue_me(), as we
695 * cannot hold a spinlock across the get_user() in case it
696 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
697 * queueing ourselves into the futex hash. This code thus has to
698 * rely on the futex_wake() code removing us from hash when it
702 /* add_wait_queue is the barrier after __set_current_state. */
703 __set_current_state(TASK_INTERRUPTIBLE
);
704 add_wait_queue(&q
.waiters
, &wait
);
706 * !list_empty() is safe here without any lock.
707 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
709 if (likely(!list_empty(&q
.list
)))
710 time
= schedule_timeout(time
);
711 __set_current_state(TASK_RUNNING
);
714 * NOTE: we don't remove ourselves from the waitqueue because
715 * we are the only user of it.
718 /* If we were woken (and unqueued), we succeeded, whatever. */
723 /* We expect signal_pending(current), but another thread may
724 * have handled it for us already. */
728 up_read(¤t
->mm
->mmap_sem
);
732 static int futex_close(struct inode
*inode
, struct file
*filp
)
734 struct futex_q
*q
= filp
->private_data
;
741 /* This is one-shot: once it's gone off you need a new fd */
742 static unsigned int futex_poll(struct file
*filp
,
743 struct poll_table_struct
*wait
)
745 struct futex_q
*q
= filp
->private_data
;
748 poll_wait(filp
, &q
->waiters
, wait
);
751 * list_empty() is safe here without any lock.
752 * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
754 if (list_empty(&q
->list
))
755 ret
= POLLIN
| POLLRDNORM
;
760 static struct file_operations futex_fops
= {
761 .release
= futex_close
,
766 * Signal allows caller to avoid the race which would occur if they
767 * set the sigio stuff up afterwards.
769 static int futex_fd(unsigned long uaddr
, int signal
)
776 if (!valid_signal(signal
))
779 ret
= get_unused_fd();
782 filp
= get_empty_filp();
788 filp
->f_op
= &futex_fops
;
789 filp
->f_vfsmnt
= mntget(futex_mnt
);
790 filp
->f_dentry
= dget(futex_mnt
->mnt_root
);
791 filp
->f_mapping
= filp
->f_dentry
->d_inode
->i_mapping
;
794 err
= f_setown(filp
, current
->pid
, 1);
798 filp
->f_owner
.signum
= signal
;
801 q
= kmalloc(sizeof(*q
), GFP_KERNEL
);
807 down_read(¤t
->mm
->mmap_sem
);
808 err
= get_futex_key(uaddr
, &q
->key
);
810 if (unlikely(err
!= 0)) {
811 up_read(¤t
->mm
->mmap_sem
);
817 * queue_me() must be called before releasing mmap_sem, because
818 * key->shared.inode needs to be referenced while holding it.
820 filp
->private_data
= q
;
822 queue_me(q
, ret
, filp
);
823 up_read(¤t
->mm
->mmap_sem
);
825 /* Now we map fd to filp, so userspace can access it */
826 fd_install(ret
, filp
);
837 * Support for robust futexes: the kernel cleans up held futexes at
840 * Implementation: user-space maintains a per-thread list of locks it
841 * is holding. Upon do_exit(), the kernel carefully walks this list,
842 * and marks all locks that are owned by this thread with the
843 * FUTEX_OWNER_DEAD bit, and wakes up a waiter (if any). The list is
844 * always manipulated with the lock held, so the list is private and
845 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
846 * field, to allow the kernel to clean up if the thread dies after
847 * acquiring the lock, but just before it could have added itself to
848 * the list. There can only be one such pending lock.
852 * sys_set_robust_list - set the robust-futex list head of a task
853 * @head: pointer to the list-head
854 * @len: length of the list-head, as userspace expects
857 sys_set_robust_list(struct robust_list_head __user
*head
,
861 * The kernel knows only one size for now:
863 if (unlikely(len
!= sizeof(*head
)))
866 current
->robust_list
= head
;
872 * sys_get_robust_list - get the robust-futex list head of a task
873 * @pid: pid of the process [zero for current task]
874 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
875 * @len_ptr: pointer to a length field, the kernel fills in the header size
878 sys_get_robust_list(int pid
, struct robust_list_head __user
**head_ptr
,
879 size_t __user
*len_ptr
)
881 struct robust_list_head
*head
;
885 head
= current
->robust_list
;
887 struct task_struct
*p
;
890 read_lock(&tasklist_lock
);
891 p
= find_task_by_pid(pid
);
895 if ((current
->euid
!= p
->euid
) && (current
->euid
!= p
->uid
) &&
896 !capable(CAP_SYS_PTRACE
))
898 head
= p
->robust_list
;
899 read_unlock(&tasklist_lock
);
902 if (put_user(sizeof(*head
), len_ptr
))
904 return put_user(head
, head_ptr
);
907 read_unlock(&tasklist_lock
);
913 * Process a futex-list entry, check whether it's owned by the
914 * dying task, and do notification if so:
916 int handle_futex_death(u32 __user
*uaddr
, struct task_struct
*curr
)
921 if (get_user(uval
, uaddr
))
924 if ((uval
& FUTEX_TID_MASK
) == curr
->pid
) {
926 * Ok, this dying thread is truly holding a futex
927 * of interest. Set the OWNER_DIED bit atomically
928 * via cmpxchg, and if the value had FUTEX_WAITERS
929 * set, wake up a waiter (if any). (We have to do a
930 * futex_wake() even if OWNER_DIED is already set -
931 * to handle the rare but possible case of recursive
932 * thread-death.) The rest of the cleanup is done in
935 if (futex_atomic_cmpxchg_inatomic(uaddr
, uval
,
936 uval
| FUTEX_OWNER_DIED
) != uval
)
939 if (uval
& FUTEX_WAITERS
)
940 futex_wake((unsigned long)uaddr
, 1);
946 * Walk curr->robust_list (very carefully, it's a userspace list!)
947 * and mark any locks found there dead, and notify any waiters.
949 * We silently return on any sign of list-walking problem.
951 void exit_robust_list(struct task_struct
*curr
)
953 struct robust_list_head __user
*head
= curr
->robust_list
;
954 struct robust_list __user
*entry
, *pending
;
955 unsigned int limit
= ROBUST_LIST_LIMIT
;
956 unsigned long futex_offset
;
959 * Fetch the list head (which was registered earlier, via
960 * sys_set_robust_list()):
962 if (get_user(entry
, &head
->list
.next
))
965 * Fetch the relative futex offset:
967 if (get_user(futex_offset
, &head
->futex_offset
))
970 * Fetch any possibly pending lock-add first, and handle it
973 if (get_user(pending
, &head
->list_op_pending
))
976 handle_futex_death((void *)pending
+ futex_offset
, curr
);
978 while (entry
!= &head
->list
) {
980 * A pending lock might already be on the list, so
981 * dont process it twice:
983 if (entry
!= pending
)
984 if (handle_futex_death((void *)entry
+ futex_offset
,
988 * Fetch the next entry in the list:
990 if (get_user(entry
, &entry
->next
))
993 * Avoid excessively long or circular lists:
1002 long do_futex(unsigned long uaddr
, int op
, int val
, unsigned long timeout
,
1003 unsigned long uaddr2
, int val2
, int val3
)
1009 ret
= futex_wait(uaddr
, val
, timeout
);
1012 ret
= futex_wake(uaddr
, val
);
1015 /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
1016 ret
= futex_fd(uaddr
, val
);
1019 ret
= futex_requeue(uaddr
, uaddr2
, val
, val2
, NULL
);
1021 case FUTEX_CMP_REQUEUE
:
1022 ret
= futex_requeue(uaddr
, uaddr2
, val
, val2
, &val3
);
1025 ret
= futex_wake_op(uaddr
, uaddr2
, val
, val2
, val3
);
1034 asmlinkage
long sys_futex(u32 __user
*uaddr
, int op
, int val
,
1035 struct timespec __user
*utime
, u32 __user
*uaddr2
,
1039 unsigned long timeout
= MAX_SCHEDULE_TIMEOUT
;
1042 if (utime
&& (op
== FUTEX_WAIT
)) {
1043 if (copy_from_user(&t
, utime
, sizeof(t
)) != 0)
1045 if (!timespec_valid(&t
))
1047 timeout
= timespec_to_jiffies(&t
) + 1;
1050 * requeue parameter in 'utime' if op == FUTEX_REQUEUE.
1052 if (op
>= FUTEX_REQUEUE
)
1053 val2
= (int) (unsigned long) utime
;
1055 return do_futex((unsigned long)uaddr
, op
, val
, timeout
,
1056 (unsigned long)uaddr2
, val2
, val3
);
1059 static int futexfs_get_sb(struct file_system_type
*fs_type
,
1060 int flags
, const char *dev_name
, void *data
,
1061 struct vfsmount
*mnt
)
1063 return get_sb_pseudo(fs_type
, "futex", NULL
, 0xBAD1DEA, mnt
);
1066 static struct file_system_type futex_fs_type
= {
1068 .get_sb
= futexfs_get_sb
,
1069 .kill_sb
= kill_anon_super
,
1072 static int __init
init(void)
1076 register_filesystem(&futex_fs_type
);
1077 futex_mnt
= kern_mount(&futex_fs_type
);
1079 for (i
= 0; i
< ARRAY_SIZE(futex_queues
); i
++) {
1080 INIT_LIST_HEAD(&futex_queues
[i
].chain
);
1081 spin_lock_init(&futex_queues
[i
].lock
);