[CPUFREQ] Fix up compile of cpufreq_stats
[linux-2.6/mini2440.git] / kernel / futex.c
blobca05fe6a70b24a2921901505c99dd7eb105b3df7
1 /*
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 * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
12 * enough at me, Linus for the original (flawed) idea, Matthew
13 * Kirkwood for proof-of-concept implementation.
15 * "The futexes are also cursed."
16 * "But they come in a choice of three flavours!"
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/fs.h>
35 #include <linux/file.h>
36 #include <linux/jhash.h>
37 #include <linux/init.h>
38 #include <linux/futex.h>
39 #include <linux/mount.h>
40 #include <linux/pagemap.h>
41 #include <linux/syscalls.h>
42 #include <linux/signal.h>
43 #include <asm/futex.h>
45 #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
48 * Futexes are matched on equal values of this key.
49 * The key type depends on whether it's a shared or private mapping.
50 * Don't rearrange members without looking at hash_futex().
52 * offset is aligned to a multiple of sizeof(u32) (== 4) by definition.
53 * We set bit 0 to indicate if it's an inode-based key.
55 union futex_key {
56 struct {
57 unsigned long pgoff;
58 struct inode *inode;
59 int offset;
60 } shared;
61 struct {
62 unsigned long uaddr;
63 struct mm_struct *mm;
64 int offset;
65 } private;
66 struct {
67 unsigned long word;
68 void *ptr;
69 int offset;
70 } both;
74 * We use this hashed waitqueue instead of a normal wait_queue_t, so
75 * we can wake only the relevant ones (hashed queues may be shared).
77 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
78 * It is considered woken when list_empty(&q->list) || q->lock_ptr == 0.
79 * The order of wakup is always to make the first condition true, then
80 * wake up q->waiters, then make the second condition true.
82 struct futex_q {
83 struct list_head list;
84 wait_queue_head_t waiters;
86 /* Which hash list lock to use. */
87 spinlock_t *lock_ptr;
89 /* Key which the futex is hashed on. */
90 union futex_key key;
92 /* For fd, sigio sent using these. */
93 int fd;
94 struct file *filp;
98 * Split the global futex_lock into every hash list lock.
100 struct futex_hash_bucket {
101 spinlock_t lock;
102 struct list_head chain;
105 static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
107 /* Futex-fs vfsmount entry: */
108 static struct vfsmount *futex_mnt;
111 * We hash on the keys returned from get_futex_key (see below).
113 static struct futex_hash_bucket *hash_futex(union futex_key *key)
115 u32 hash = jhash2((u32*)&key->both.word,
116 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
117 key->both.offset);
118 return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
122 * Return 1 if two futex_keys are equal, 0 otherwise.
124 static inline int match_futex(union futex_key *key1, union futex_key *key2)
126 return (key1->both.word == key2->both.word
127 && key1->both.ptr == key2->both.ptr
128 && key1->both.offset == key2->both.offset);
132 * Get parameters which are the keys for a futex.
134 * For shared mappings, it's (page->index, vma->vm_file->f_dentry->d_inode,
135 * offset_within_page). For private mappings, it's (uaddr, current->mm).
136 * We can usually work out the index without swapping in the page.
138 * Returns: 0, or negative error code.
139 * The key words are stored in *key on success.
141 * Should be called with &current->mm->mmap_sem but NOT any spinlocks.
143 static int get_futex_key(unsigned long uaddr, union futex_key *key)
145 struct mm_struct *mm = current->mm;
146 struct vm_area_struct *vma;
147 struct page *page;
148 int err;
151 * The futex address must be "naturally" aligned.
153 key->both.offset = uaddr % PAGE_SIZE;
154 if (unlikely((key->both.offset % sizeof(u32)) != 0))
155 return -EINVAL;
156 uaddr -= key->both.offset;
159 * The futex is hashed differently depending on whether
160 * it's in a shared or private mapping. So check vma first.
162 vma = find_extend_vma(mm, uaddr);
163 if (unlikely(!vma))
164 return -EFAULT;
167 * Permissions.
169 if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
170 return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;
173 * Private mappings are handled in a simple way.
175 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
176 * it's a read-only handle, it's expected that futexes attach to
177 * the object not the particular process. Therefore we use
178 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
179 * mappings of _writable_ handles.
181 if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
182 key->private.mm = mm;
183 key->private.uaddr = uaddr;
184 return 0;
188 * Linear file mappings are also simple.
190 key->shared.inode = vma->vm_file->f_dentry->d_inode;
191 key->both.offset++; /* Bit 0 of offset indicates inode-based key. */
192 if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
193 key->shared.pgoff = (((uaddr - vma->vm_start) >> PAGE_SHIFT)
194 + vma->vm_pgoff);
195 return 0;
199 * We could walk the page table to read the non-linear
200 * pte, and get the page index without fetching the page
201 * from swap. But that's a lot of code to duplicate here
202 * for a rare case, so we simply fetch the page.
206 * Do a quick atomic lookup first - this is the fastpath.
208 spin_lock(&current->mm->page_table_lock);
209 page = follow_page(mm, uaddr, 0);
210 if (likely(page != NULL)) {
211 key->shared.pgoff =
212 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
213 spin_unlock(&current->mm->page_table_lock);
214 return 0;
216 spin_unlock(&current->mm->page_table_lock);
219 * Do it the general way.
221 err = get_user_pages(current, mm, uaddr, 1, 0, 0, &page, NULL);
222 if (err >= 0) {
223 key->shared.pgoff =
224 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
225 put_page(page);
226 return 0;
228 return err;
232 * Take a reference to the resource addressed by a key.
233 * Can be called while holding spinlocks.
235 * NOTE: mmap_sem MUST be held between get_futex_key() and calling this
236 * function, if it is called at all. mmap_sem keeps key->shared.inode valid.
238 static inline void get_key_refs(union futex_key *key)
240 if (key->both.ptr != 0) {
241 if (key->both.offset & 1)
242 atomic_inc(&key->shared.inode->i_count);
243 else
244 atomic_inc(&key->private.mm->mm_count);
249 * Drop a reference to the resource addressed by a key.
250 * The hash bucket spinlock must not be held.
252 static void drop_key_refs(union futex_key *key)
254 if (key->both.ptr != 0) {
255 if (key->both.offset & 1)
256 iput(key->shared.inode);
257 else
258 mmdrop(key->private.mm);
262 static inline int get_futex_value_locked(int *dest, int __user *from)
264 int ret;
266 inc_preempt_count();
267 ret = __copy_from_user_inatomic(dest, from, sizeof(int));
268 dec_preempt_count();
270 return ret ? -EFAULT : 0;
274 * The hash bucket lock must be held when this is called.
275 * Afterwards, the futex_q must not be accessed.
277 static void wake_futex(struct futex_q *q)
279 list_del_init(&q->list);
280 if (q->filp)
281 send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
283 * The lock in wake_up_all() is a crucial memory barrier after the
284 * list_del_init() and also before assigning to q->lock_ptr.
286 wake_up_all(&q->waiters);
288 * The waiting task can free the futex_q as soon as this is written,
289 * without taking any locks. This must come last.
291 q->lock_ptr = NULL;
295 * Wake up all waiters hashed on the physical page that is mapped
296 * to this virtual address:
298 static int futex_wake(unsigned long uaddr, int nr_wake)
300 union futex_key key;
301 struct futex_hash_bucket *bh;
302 struct list_head *head;
303 struct futex_q *this, *next;
304 int ret;
306 down_read(&current->mm->mmap_sem);
308 ret = get_futex_key(uaddr, &key);
309 if (unlikely(ret != 0))
310 goto out;
312 bh = hash_futex(&key);
313 spin_lock(&bh->lock);
314 head = &bh->chain;
316 list_for_each_entry_safe(this, next, head, list) {
317 if (match_futex (&this->key, &key)) {
318 wake_futex(this);
319 if (++ret >= nr_wake)
320 break;
324 spin_unlock(&bh->lock);
325 out:
326 up_read(&current->mm->mmap_sem);
327 return ret;
331 * Wake up all waiters hashed on the physical page that is mapped
332 * to this virtual address:
334 static int futex_wake_op(unsigned long uaddr1, unsigned long uaddr2, int nr_wake, int nr_wake2, int op)
336 union futex_key key1, key2;
337 struct futex_hash_bucket *bh1, *bh2;
338 struct list_head *head;
339 struct futex_q *this, *next;
340 int ret, op_ret, attempt = 0;
342 retryfull:
343 down_read(&current->mm->mmap_sem);
345 ret = get_futex_key(uaddr1, &key1);
346 if (unlikely(ret != 0))
347 goto out;
348 ret = get_futex_key(uaddr2, &key2);
349 if (unlikely(ret != 0))
350 goto out;
352 bh1 = hash_futex(&key1);
353 bh2 = hash_futex(&key2);
355 retry:
356 if (bh1 < bh2)
357 spin_lock(&bh1->lock);
358 spin_lock(&bh2->lock);
359 if (bh1 > bh2)
360 spin_lock(&bh1->lock);
362 op_ret = futex_atomic_op_inuser(op, (int __user *)uaddr2);
363 if (unlikely(op_ret < 0)) {
364 int dummy;
366 spin_unlock(&bh1->lock);
367 if (bh1 != bh2)
368 spin_unlock(&bh2->lock);
370 /* futex_atomic_op_inuser needs to both read and write
371 * *(int __user *)uaddr2, but we can't modify it
372 * non-atomically. Therefore, if get_user below is not
373 * enough, we need to handle the fault ourselves, while
374 * still holding the mmap_sem. */
375 if (attempt++) {
376 struct vm_area_struct * vma;
377 struct mm_struct *mm = current->mm;
379 ret = -EFAULT;
380 if (attempt >= 2 ||
381 !(vma = find_vma(mm, uaddr2)) ||
382 vma->vm_start > uaddr2 ||
383 !(vma->vm_flags & VM_WRITE))
384 goto out;
386 switch (handle_mm_fault(mm, vma, uaddr2, 1)) {
387 case VM_FAULT_MINOR:
388 current->min_flt++;
389 break;
390 case VM_FAULT_MAJOR:
391 current->maj_flt++;
392 break;
393 default:
394 goto out;
396 goto retry;
399 /* If we would have faulted, release mmap_sem,
400 * fault it in and start all over again. */
401 up_read(&current->mm->mmap_sem);
403 ret = get_user(dummy, (int __user *)uaddr2);
404 if (ret)
405 return ret;
407 goto retryfull;
410 head = &bh1->chain;
412 list_for_each_entry_safe(this, next, head, list) {
413 if (match_futex (&this->key, &key1)) {
414 wake_futex(this);
415 if (++ret >= nr_wake)
416 break;
420 if (op_ret > 0) {
421 head = &bh2->chain;
423 op_ret = 0;
424 list_for_each_entry_safe(this, next, head, list) {
425 if (match_futex (&this->key, &key2)) {
426 wake_futex(this);
427 if (++op_ret >= nr_wake2)
428 break;
431 ret += op_ret;
434 spin_unlock(&bh1->lock);
435 if (bh1 != bh2)
436 spin_unlock(&bh2->lock);
437 out:
438 up_read(&current->mm->mmap_sem);
439 return ret;
443 * Requeue all waiters hashed on one physical page to another
444 * physical page.
446 static int futex_requeue(unsigned long uaddr1, unsigned long uaddr2,
447 int nr_wake, int nr_requeue, int *valp)
449 union futex_key key1, key2;
450 struct futex_hash_bucket *bh1, *bh2;
451 struct list_head *head1;
452 struct futex_q *this, *next;
453 int ret, drop_count = 0;
455 retry:
456 down_read(&current->mm->mmap_sem);
458 ret = get_futex_key(uaddr1, &key1);
459 if (unlikely(ret != 0))
460 goto out;
461 ret = get_futex_key(uaddr2, &key2);
462 if (unlikely(ret != 0))
463 goto out;
465 bh1 = hash_futex(&key1);
466 bh2 = hash_futex(&key2);
468 if (bh1 < bh2)
469 spin_lock(&bh1->lock);
470 spin_lock(&bh2->lock);
471 if (bh1 > bh2)
472 spin_lock(&bh1->lock);
474 if (likely(valp != NULL)) {
475 int curval;
477 ret = get_futex_value_locked(&curval, (int __user *)uaddr1);
479 if (unlikely(ret)) {
480 spin_unlock(&bh1->lock);
481 if (bh1 != bh2)
482 spin_unlock(&bh2->lock);
484 /* If we would have faulted, release mmap_sem, fault
485 * it in and start all over again.
487 up_read(&current->mm->mmap_sem);
489 ret = get_user(curval, (int __user *)uaddr1);
491 if (!ret)
492 goto retry;
494 return ret;
496 if (curval != *valp) {
497 ret = -EAGAIN;
498 goto out_unlock;
502 head1 = &bh1->chain;
503 list_for_each_entry_safe(this, next, head1, list) {
504 if (!match_futex (&this->key, &key1))
505 continue;
506 if (++ret <= nr_wake) {
507 wake_futex(this);
508 } else {
509 list_move_tail(&this->list, &bh2->chain);
510 this->lock_ptr = &bh2->lock;
511 this->key = key2;
512 get_key_refs(&key2);
513 drop_count++;
515 if (ret - nr_wake >= nr_requeue)
516 break;
517 /* Make sure to stop if key1 == key2 */
518 if (head1 == &bh2->chain && head1 != &next->list)
519 head1 = &this->list;
523 out_unlock:
524 spin_unlock(&bh1->lock);
525 if (bh1 != bh2)
526 spin_unlock(&bh2->lock);
528 /* drop_key_refs() must be called outside the spinlocks. */
529 while (--drop_count >= 0)
530 drop_key_refs(&key1);
532 out:
533 up_read(&current->mm->mmap_sem);
534 return ret;
537 /* The key must be already stored in q->key. */
538 static inline struct futex_hash_bucket *
539 queue_lock(struct futex_q *q, int fd, struct file *filp)
541 struct futex_hash_bucket *bh;
543 q->fd = fd;
544 q->filp = filp;
546 init_waitqueue_head(&q->waiters);
548 get_key_refs(&q->key);
549 bh = hash_futex(&q->key);
550 q->lock_ptr = &bh->lock;
552 spin_lock(&bh->lock);
553 return bh;
556 static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *bh)
558 list_add_tail(&q->list, &bh->chain);
559 spin_unlock(&bh->lock);
562 static inline void
563 queue_unlock(struct futex_q *q, struct futex_hash_bucket *bh)
565 spin_unlock(&bh->lock);
566 drop_key_refs(&q->key);
570 * queue_me and unqueue_me must be called as a pair, each
571 * exactly once. They are called with the hashed spinlock held.
574 /* The key must be already stored in q->key. */
575 static void queue_me(struct futex_q *q, int fd, struct file *filp)
577 struct futex_hash_bucket *bh;
578 bh = queue_lock(q, fd, filp);
579 __queue_me(q, bh);
582 /* Return 1 if we were still queued (ie. 0 means we were woken) */
583 static int unqueue_me(struct futex_q *q)
585 int ret = 0;
586 spinlock_t *lock_ptr;
588 /* In the common case we don't take the spinlock, which is nice. */
589 retry:
590 lock_ptr = q->lock_ptr;
591 if (lock_ptr != 0) {
592 spin_lock(lock_ptr);
594 * q->lock_ptr can change between reading it and
595 * spin_lock(), causing us to take the wrong lock. This
596 * corrects the race condition.
598 * Reasoning goes like this: if we have the wrong lock,
599 * q->lock_ptr must have changed (maybe several times)
600 * between reading it and the spin_lock(). It can
601 * change again after the spin_lock() but only if it was
602 * already changed before the spin_lock(). It cannot,
603 * however, change back to the original value. Therefore
604 * we can detect whether we acquired the correct lock.
606 if (unlikely(lock_ptr != q->lock_ptr)) {
607 spin_unlock(lock_ptr);
608 goto retry;
610 WARN_ON(list_empty(&q->list));
611 list_del(&q->list);
612 spin_unlock(lock_ptr);
613 ret = 1;
616 drop_key_refs(&q->key);
617 return ret;
620 static int futex_wait(unsigned long uaddr, int val, unsigned long time)
622 DECLARE_WAITQUEUE(wait, current);
623 int ret, curval;
624 struct futex_q q;
625 struct futex_hash_bucket *bh;
627 retry:
628 down_read(&current->mm->mmap_sem);
630 ret = get_futex_key(uaddr, &q.key);
631 if (unlikely(ret != 0))
632 goto out_release_sem;
634 bh = queue_lock(&q, -1, NULL);
637 * Access the page AFTER the futex is queued.
638 * Order is important:
640 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
641 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
643 * The basic logical guarantee of a futex is that it blocks ONLY
644 * if cond(var) is known to be true at the time of blocking, for
645 * any cond. If we queued after testing *uaddr, that would open
646 * a race condition where we could block indefinitely with
647 * cond(var) false, which would violate the guarantee.
649 * A consequence is that futex_wait() can return zero and absorb
650 * a wakeup when *uaddr != val on entry to the syscall. This is
651 * rare, but normal.
653 * We hold the mmap semaphore, so the mapping cannot have changed
654 * since we looked it up in get_futex_key.
657 ret = get_futex_value_locked(&curval, (int __user *)uaddr);
659 if (unlikely(ret)) {
660 queue_unlock(&q, bh);
662 /* If we would have faulted, release mmap_sem, fault it in and
663 * start all over again.
665 up_read(&current->mm->mmap_sem);
667 ret = get_user(curval, (int __user *)uaddr);
669 if (!ret)
670 goto retry;
671 return ret;
673 if (curval != val) {
674 ret = -EWOULDBLOCK;
675 queue_unlock(&q, bh);
676 goto out_release_sem;
679 /* Only actually queue if *uaddr contained val. */
680 __queue_me(&q, bh);
683 * Now the futex is queued and we have checked the data, we
684 * don't want to hold mmap_sem while we sleep.
686 up_read(&current->mm->mmap_sem);
689 * There might have been scheduling since the queue_me(), as we
690 * cannot hold a spinlock across the get_user() in case it
691 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
692 * queueing ourselves into the futex hash. This code thus has to
693 * rely on the futex_wake() code removing us from hash when it
694 * wakes us up.
697 /* add_wait_queue is the barrier after __set_current_state. */
698 __set_current_state(TASK_INTERRUPTIBLE);
699 add_wait_queue(&q.waiters, &wait);
701 * !list_empty() is safe here without any lock.
702 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
704 if (likely(!list_empty(&q.list)))
705 time = schedule_timeout(time);
706 __set_current_state(TASK_RUNNING);
709 * NOTE: we don't remove ourselves from the waitqueue because
710 * we are the only user of it.
713 /* If we were woken (and unqueued), we succeeded, whatever. */
714 if (!unqueue_me(&q))
715 return 0;
716 if (time == 0)
717 return -ETIMEDOUT;
718 /* We expect signal_pending(current), but another thread may
719 * have handled it for us already. */
720 return -EINTR;
722 out_release_sem:
723 up_read(&current->mm->mmap_sem);
724 return ret;
727 static int futex_close(struct inode *inode, struct file *filp)
729 struct futex_q *q = filp->private_data;
731 unqueue_me(q);
732 kfree(q);
733 return 0;
736 /* This is one-shot: once it's gone off you need a new fd */
737 static unsigned int futex_poll(struct file *filp,
738 struct poll_table_struct *wait)
740 struct futex_q *q = filp->private_data;
741 int ret = 0;
743 poll_wait(filp, &q->waiters, wait);
746 * list_empty() is safe here without any lock.
747 * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
749 if (list_empty(&q->list))
750 ret = POLLIN | POLLRDNORM;
752 return ret;
755 static struct file_operations futex_fops = {
756 .release = futex_close,
757 .poll = futex_poll,
761 * Signal allows caller to avoid the race which would occur if they
762 * set the sigio stuff up afterwards.
764 static int futex_fd(unsigned long uaddr, int signal)
766 struct futex_q *q;
767 struct file *filp;
768 int ret, err;
770 ret = -EINVAL;
771 if (!valid_signal(signal))
772 goto out;
774 ret = get_unused_fd();
775 if (ret < 0)
776 goto out;
777 filp = get_empty_filp();
778 if (!filp) {
779 put_unused_fd(ret);
780 ret = -ENFILE;
781 goto out;
783 filp->f_op = &futex_fops;
784 filp->f_vfsmnt = mntget(futex_mnt);
785 filp->f_dentry = dget(futex_mnt->mnt_root);
786 filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
788 if (signal) {
789 err = f_setown(filp, current->pid, 1);
790 if (err < 0) {
791 goto error;
793 filp->f_owner.signum = signal;
796 q = kmalloc(sizeof(*q), GFP_KERNEL);
797 if (!q) {
798 err = -ENOMEM;
799 goto error;
802 down_read(&current->mm->mmap_sem);
803 err = get_futex_key(uaddr, &q->key);
805 if (unlikely(err != 0)) {
806 up_read(&current->mm->mmap_sem);
807 kfree(q);
808 goto error;
812 * queue_me() must be called before releasing mmap_sem, because
813 * key->shared.inode needs to be referenced while holding it.
815 filp->private_data = q;
817 queue_me(q, ret, filp);
818 up_read(&current->mm->mmap_sem);
820 /* Now we map fd to filp, so userspace can access it */
821 fd_install(ret, filp);
822 out:
823 return ret;
824 error:
825 put_unused_fd(ret);
826 put_filp(filp);
827 ret = err;
828 goto out;
831 long do_futex(unsigned long uaddr, int op, int val, unsigned long timeout,
832 unsigned long uaddr2, int val2, int val3)
834 int ret;
836 switch (op) {
837 case FUTEX_WAIT:
838 ret = futex_wait(uaddr, val, timeout);
839 break;
840 case FUTEX_WAKE:
841 ret = futex_wake(uaddr, val);
842 break;
843 case FUTEX_FD:
844 /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
845 ret = futex_fd(uaddr, val);
846 break;
847 case FUTEX_REQUEUE:
848 ret = futex_requeue(uaddr, uaddr2, val, val2, NULL);
849 break;
850 case FUTEX_CMP_REQUEUE:
851 ret = futex_requeue(uaddr, uaddr2, val, val2, &val3);
852 break;
853 case FUTEX_WAKE_OP:
854 ret = futex_wake_op(uaddr, uaddr2, val, val2, val3);
855 break;
856 default:
857 ret = -ENOSYS;
859 return ret;
863 asmlinkage long sys_futex(u32 __user *uaddr, int op, int val,
864 struct timespec __user *utime, u32 __user *uaddr2,
865 int val3)
867 struct timespec t;
868 unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
869 int val2 = 0;
871 if ((op == FUTEX_WAIT) && utime) {
872 if (copy_from_user(&t, utime, sizeof(t)) != 0)
873 return -EFAULT;
874 timeout = timespec_to_jiffies(&t) + 1;
877 * requeue parameter in 'utime' if op == FUTEX_REQUEUE.
879 if (op >= FUTEX_REQUEUE)
880 val2 = (int) (unsigned long) utime;
882 return do_futex((unsigned long)uaddr, op, val, timeout,
883 (unsigned long)uaddr2, val2, val3);
886 static struct super_block *
887 futexfs_get_sb(struct file_system_type *fs_type,
888 int flags, const char *dev_name, void *data)
890 return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA);
893 static struct file_system_type futex_fs_type = {
894 .name = "futexfs",
895 .get_sb = futexfs_get_sb,
896 .kill_sb = kill_anon_super,
899 static int __init init(void)
901 unsigned int i;
903 register_filesystem(&futex_fs_type);
904 futex_mnt = kern_mount(&futex_fs_type);
906 for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
907 INIT_LIST_HEAD(&futex_queues[i].chain);
908 spin_lock_init(&futex_queues[i].lock);
910 return 0;
912 __initcall(init);