allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / kernel / futex.c
blob251a1d5c715c285c3b90a47704eb7732c98e33be
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 * 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>
45 #include <linux/fs.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 #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
62 * Priority Inheritance state:
64 struct futex_pi_state {
66 * list of 'owned' pi_state instances - these have to be
67 * cleaned up in do_exit() if the task exits prematurely:
69 struct list_head list;
72 * The PI object:
74 struct rt_mutex pi_mutex;
76 struct task_struct *owner;
77 atomic_t refcount;
79 union futex_key key;
83 * We use this hashed waitqueue instead of a normal wait_queue_t, so
84 * we can wake only the relevant ones (hashed queues may be shared).
86 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
87 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
88 * The order of wakup is always to make the first condition true, then
89 * wake up q->waiters, then make the second condition true.
91 struct futex_q {
92 struct plist_node list;
93 wait_queue_head_t waiters;
95 /* Which hash list lock to use: */
96 spinlock_t *lock_ptr;
98 /* Key which the futex is hashed on: */
99 union futex_key key;
101 /* Optional priority inheritance state: */
102 struct futex_pi_state *pi_state;
103 struct task_struct *task;
107 * Split the global futex_lock into every hash list lock.
109 struct futex_hash_bucket {
110 spinlock_t lock;
111 struct plist_head chain;
114 static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
117 * We hash on the keys returned from get_futex_key (see below).
119 static struct futex_hash_bucket *hash_futex(union futex_key *key)
121 u32 hash = jhash2((u32*)&key->both.word,
122 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
123 key->both.offset);
124 return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
128 * Return 1 if two futex_keys are equal, 0 otherwise.
130 static inline int match_futex(union futex_key *key1, union futex_key *key2)
132 return (key1->both.word == key2->both.word
133 && key1->both.ptr == key2->both.ptr
134 && key1->both.offset == key2->both.offset);
138 * get_futex_key - Get parameters which are the keys for a futex.
139 * @uaddr: virtual address of the futex
140 * @shared: NULL for a PROCESS_PRIVATE futex,
141 * &current->mm->mmap_sem for a PROCESS_SHARED futex
142 * @key: address where result is stored.
144 * Returns a negative error code or 0
145 * The key words are stored in *key on success.
147 * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
148 * offset_within_page). For private mappings, it's (uaddr, current->mm).
149 * We can usually work out the index without swapping in the page.
151 * fshared is NULL for PROCESS_PRIVATE futexes
152 * For other futexes, it points to &current->mm->mmap_sem and
153 * caller must have taken the reader lock. but NOT any spinlocks.
155 int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared,
156 union futex_key *key)
158 unsigned long address = (unsigned long)uaddr;
159 struct mm_struct *mm = current->mm;
160 struct vm_area_struct *vma;
161 struct page *page;
162 int err;
165 * The futex address must be "naturally" aligned.
167 key->both.offset = address % PAGE_SIZE;
168 if (unlikely((address % sizeof(u32)) != 0))
169 return -EINVAL;
170 address -= key->both.offset;
173 * PROCESS_PRIVATE futexes are fast.
174 * As the mm cannot disappear under us and the 'key' only needs
175 * virtual address, we dont even have to find the underlying vma.
176 * Note : We do have to check 'uaddr' is a valid user address,
177 * but access_ok() should be faster than find_vma()
179 if (!fshared) {
180 if (unlikely(!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))))
181 return -EFAULT;
182 key->private.mm = mm;
183 key->private.address = address;
184 return 0;
187 * The futex is hashed differently depending on whether
188 * it's in a shared or private mapping. So check vma first.
190 vma = find_extend_vma(mm, address);
191 if (unlikely(!vma))
192 return -EFAULT;
195 * Permissions.
197 if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
198 return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;
201 * Private mappings are handled in a simple way.
203 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
204 * it's a read-only handle, it's expected that futexes attach to
205 * the object not the particular process. Therefore we use
206 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
207 * mappings of _writable_ handles.
209 if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
210 key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */
211 key->private.mm = mm;
212 key->private.address = address;
213 return 0;
217 * Linear file mappings are also simple.
219 key->shared.inode = vma->vm_file->f_path.dentry->d_inode;
220 key->both.offset |= FUT_OFF_INODE; /* inode-based key. */
221 if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
222 key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
223 + vma->vm_pgoff);
224 return 0;
228 * We could walk the page table to read the non-linear
229 * pte, and get the page index without fetching the page
230 * from swap. But that's a lot of code to duplicate here
231 * for a rare case, so we simply fetch the page.
233 err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
234 if (err >= 0) {
235 key->shared.pgoff =
236 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
237 put_page(page);
238 return 0;
240 return err;
242 EXPORT_SYMBOL_GPL(get_futex_key);
245 * Take a reference to the resource addressed by a key.
246 * Can be called while holding spinlocks.
249 inline void get_futex_key_refs(union futex_key *key)
251 if (key->both.ptr == 0)
252 return;
253 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
254 case FUT_OFF_INODE:
255 atomic_inc(&key->shared.inode->i_count);
256 break;
257 case FUT_OFF_MMSHARED:
258 atomic_inc(&key->private.mm->mm_count);
259 break;
262 EXPORT_SYMBOL_GPL(get_futex_key_refs);
265 * Drop a reference to the resource addressed by a key.
266 * The hash bucket spinlock must not be held.
268 void drop_futex_key_refs(union futex_key *key)
270 if (key->both.ptr == 0)
271 return;
272 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
273 case FUT_OFF_INODE:
274 iput(key->shared.inode);
275 break;
276 case FUT_OFF_MMSHARED:
277 mmdrop(key->private.mm);
278 break;
281 EXPORT_SYMBOL_GPL(drop_futex_key_refs);
283 static inline int get_futex_value_locked(u32 *dest, u32 __user *from)
285 int ret;
287 pagefault_disable();
288 ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
289 pagefault_enable();
291 return ret ? -EFAULT : 0;
295 * Fault handling.
296 * if fshared is non NULL, current->mm->mmap_sem is already held
298 static int futex_handle_fault(unsigned long address,
299 struct rw_semaphore *fshared, int attempt)
301 struct vm_area_struct * vma;
302 struct mm_struct *mm = current->mm;
303 int ret = -EFAULT;
305 if (attempt > 2)
306 return ret;
308 if (!fshared)
309 down_read(&mm->mmap_sem);
310 vma = find_vma(mm, address);
311 if (vma && address >= vma->vm_start &&
312 (vma->vm_flags & VM_WRITE)) {
313 int fault;
314 fault = handle_mm_fault(mm, vma, address, 1);
315 if (unlikely((fault & VM_FAULT_ERROR))) {
316 #if 0
317 /* XXX: let's do this when we verify it is OK */
318 if (ret & VM_FAULT_OOM)
319 ret = -ENOMEM;
320 #endif
321 } else {
322 ret = 0;
323 if (fault & VM_FAULT_MAJOR)
324 current->maj_flt++;
325 else
326 current->min_flt++;
329 if (!fshared)
330 up_read(&mm->mmap_sem);
331 return ret;
335 * PI code:
337 static int refill_pi_state_cache(void)
339 struct futex_pi_state *pi_state;
341 if (likely(current->pi_state_cache))
342 return 0;
344 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
346 if (!pi_state)
347 return -ENOMEM;
349 INIT_LIST_HEAD(&pi_state->list);
350 /* pi_mutex gets initialized later */
351 pi_state->owner = NULL;
352 atomic_set(&pi_state->refcount, 1);
354 current->pi_state_cache = pi_state;
356 return 0;
359 static struct futex_pi_state * alloc_pi_state(void)
361 struct futex_pi_state *pi_state = current->pi_state_cache;
363 WARN_ON(!pi_state);
364 current->pi_state_cache = NULL;
366 return pi_state;
369 static void free_pi_state(struct futex_pi_state *pi_state)
371 if (!atomic_dec_and_test(&pi_state->refcount))
372 return;
375 * If pi_state->owner is NULL, the owner is most probably dying
376 * and has cleaned up the pi_state already
378 if (pi_state->owner) {
379 spin_lock_irq(&pi_state->owner->pi_lock);
380 list_del_init(&pi_state->list);
381 spin_unlock_irq(&pi_state->owner->pi_lock);
383 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
386 if (current->pi_state_cache)
387 kfree(pi_state);
388 else {
390 * pi_state->list is already empty.
391 * clear pi_state->owner.
392 * refcount is at 0 - put it back to 1.
394 pi_state->owner = NULL;
395 atomic_set(&pi_state->refcount, 1);
396 current->pi_state_cache = pi_state;
401 * Look up the task based on what TID userspace gave us.
402 * We dont trust it.
404 static struct task_struct * futex_find_get_task(pid_t pid)
406 struct task_struct *p;
408 rcu_read_lock();
409 p = find_task_by_pid(pid);
411 if (!p || ((current->euid != p->euid) && (current->euid != p->uid)))
412 p = ERR_PTR(-ESRCH);
413 else
414 get_task_struct(p);
416 rcu_read_unlock();
418 return p;
422 * This task is holding PI mutexes at exit time => bad.
423 * Kernel cleans up PI-state, but userspace is likely hosed.
424 * (Robust-futex cleanup is separate and might save the day for userspace.)
426 void exit_pi_state_list(struct task_struct *curr)
428 struct list_head *next, *head = &curr->pi_state_list;
429 struct futex_pi_state *pi_state;
430 struct futex_hash_bucket *hb;
431 union futex_key key;
434 * We are a ZOMBIE and nobody can enqueue itself on
435 * pi_state_list anymore, but we have to be careful
436 * versus waiters unqueueing themselves:
438 spin_lock_irq(&curr->pi_lock);
439 while (!list_empty(head)) {
441 next = head->next;
442 pi_state = list_entry(next, struct futex_pi_state, list);
443 key = pi_state->key;
444 hb = hash_futex(&key);
445 spin_unlock_irq(&curr->pi_lock);
447 spin_lock(&hb->lock);
449 spin_lock_irq(&curr->pi_lock);
451 * We dropped the pi-lock, so re-check whether this
452 * task still owns the PI-state:
454 if (head->next != next) {
455 spin_unlock(&hb->lock);
456 continue;
459 WARN_ON(pi_state->owner != curr);
460 WARN_ON(list_empty(&pi_state->list));
461 list_del_init(&pi_state->list);
462 pi_state->owner = NULL;
463 spin_unlock_irq(&curr->pi_lock);
465 rt_mutex_unlock(&pi_state->pi_mutex);
467 spin_unlock(&hb->lock);
469 spin_lock_irq(&curr->pi_lock);
471 spin_unlock_irq(&curr->pi_lock);
474 static int
475 lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
476 union futex_key *key, struct futex_pi_state **ps)
478 struct futex_pi_state *pi_state = NULL;
479 struct futex_q *this, *next;
480 struct plist_head *head;
481 struct task_struct *p;
482 pid_t pid = uval & FUTEX_TID_MASK;
484 head = &hb->chain;
486 plist_for_each_entry_safe(this, next, head, list) {
487 if (match_futex(&this->key, key)) {
489 * Another waiter already exists - bump up
490 * the refcount and return its pi_state:
492 pi_state = this->pi_state;
494 * Userspace might have messed up non PI and PI futexes
496 if (unlikely(!pi_state))
497 return -EINVAL;
499 WARN_ON(!atomic_read(&pi_state->refcount));
500 WARN_ON(pid && pi_state->owner &&
501 pi_state->owner->pid != pid);
503 atomic_inc(&pi_state->refcount);
504 *ps = pi_state;
506 return 0;
511 * We are the first waiter - try to look up the real owner and attach
512 * the new pi_state to it, but bail out when TID = 0
514 if (!pid)
515 return -ESRCH;
516 p = futex_find_get_task(pid);
517 if (IS_ERR(p))
518 return PTR_ERR(p);
521 * We need to look at the task state flags to figure out,
522 * whether the task is exiting. To protect against the do_exit
523 * change of the task flags, we do this protected by
524 * p->pi_lock:
526 spin_lock_irq(&p->pi_lock);
527 if (unlikely(p->flags & PF_EXITING)) {
529 * The task is on the way out. When PF_EXITPIDONE is
530 * set, we know that the task has finished the
531 * cleanup:
533 int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
535 spin_unlock_irq(&p->pi_lock);
536 put_task_struct(p);
537 return ret;
540 pi_state = alloc_pi_state();
543 * Initialize the pi_mutex in locked state and make 'p'
544 * the owner of it:
546 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
548 /* Store the key for possible exit cleanups: */
549 pi_state->key = *key;
551 WARN_ON(!list_empty(&pi_state->list));
552 list_add(&pi_state->list, &p->pi_state_list);
553 pi_state->owner = p;
554 spin_unlock_irq(&p->pi_lock);
556 put_task_struct(p);
558 *ps = pi_state;
560 return 0;
564 * The hash bucket lock must be held when this is called.
565 * Afterwards, the futex_q must not be accessed.
567 static void wake_futex(struct futex_q *q)
569 plist_del(&q->list, &q->list.plist);
571 * The lock in wake_up_all() is a crucial memory barrier after the
572 * plist_del() and also before assigning to q->lock_ptr.
574 wake_up_all(&q->waiters);
576 * The waiting task can free the futex_q as soon as this is written,
577 * without taking any locks. This must come last.
579 * A memory barrier is required here to prevent the following store
580 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
581 * at the end of wake_up_all() does not prevent this store from
582 * moving.
584 smp_wmb();
585 q->lock_ptr = NULL;
588 static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
590 struct task_struct *new_owner;
591 struct futex_pi_state *pi_state = this->pi_state;
592 u32 curval, newval;
594 if (!pi_state)
595 return -EINVAL;
597 spin_lock(&pi_state->pi_mutex.wait_lock);
598 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
601 * This happens when we have stolen the lock and the original
602 * pending owner did not enqueue itself back on the rt_mutex.
603 * Thats not a tragedy. We know that way, that a lock waiter
604 * is on the fly. We make the futex_q waiter the pending owner.
606 if (!new_owner)
607 new_owner = this->task;
610 * We pass it to the next owner. (The WAITERS bit is always
611 * kept enabled while there is PI state around. We must also
612 * preserve the owner died bit.)
614 if (!(uval & FUTEX_OWNER_DIED)) {
615 int ret = 0;
617 newval = FUTEX_WAITERS | new_owner->pid;
619 pagefault_disable();
620 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
621 pagefault_enable();
623 if (curval == -EFAULT)
624 ret = -EFAULT;
625 if (curval != uval)
626 ret = -EINVAL;
627 if (ret) {
628 spin_unlock(&pi_state->pi_mutex.wait_lock);
629 return ret;
633 spin_lock_irq(&pi_state->owner->pi_lock);
634 WARN_ON(list_empty(&pi_state->list));
635 list_del_init(&pi_state->list);
636 spin_unlock_irq(&pi_state->owner->pi_lock);
638 spin_lock_irq(&new_owner->pi_lock);
639 WARN_ON(!list_empty(&pi_state->list));
640 list_add(&pi_state->list, &new_owner->pi_state_list);
641 pi_state->owner = new_owner;
642 spin_unlock_irq(&new_owner->pi_lock);
644 spin_unlock(&pi_state->pi_mutex.wait_lock);
645 rt_mutex_unlock(&pi_state->pi_mutex);
647 return 0;
650 static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
652 u32 oldval;
655 * There is no waiter, so we unlock the futex. The owner died
656 * bit has not to be preserved here. We are the owner:
658 pagefault_disable();
659 oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0);
660 pagefault_enable();
662 if (oldval == -EFAULT)
663 return oldval;
664 if (oldval != uval)
665 return -EAGAIN;
667 return 0;
671 * Express the locking dependencies for lockdep:
673 static inline void
674 double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
676 if (hb1 <= hb2) {
677 spin_lock(&hb1->lock);
678 if (hb1 < hb2)
679 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
680 } else { /* hb1 > hb2 */
681 spin_lock(&hb2->lock);
682 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
687 * Wake up all waiters hashed on the physical page that is mapped
688 * to this virtual address:
690 static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
691 int nr_wake)
693 struct futex_hash_bucket *hb;
694 struct futex_q *this, *next;
695 struct plist_head *head;
696 union futex_key key;
697 int ret;
699 if (fshared)
700 down_read(fshared);
702 ret = get_futex_key(uaddr, fshared, &key);
703 if (unlikely(ret != 0))
704 goto out;
706 hb = hash_futex(&key);
707 spin_lock(&hb->lock);
708 head = &hb->chain;
710 plist_for_each_entry_safe(this, next, head, list) {
711 if (match_futex (&this->key, &key)) {
712 if (this->pi_state) {
713 ret = -EINVAL;
714 break;
716 wake_futex(this);
717 if (++ret >= nr_wake)
718 break;
722 spin_unlock(&hb->lock);
723 out:
724 if (fshared)
725 up_read(fshared);
726 return ret;
730 * Wake up all waiters hashed on the physical page that is mapped
731 * to this virtual address:
733 static int
734 futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared,
735 u32 __user *uaddr2,
736 int nr_wake, int nr_wake2, int op)
738 union futex_key key1, key2;
739 struct futex_hash_bucket *hb1, *hb2;
740 struct plist_head *head;
741 struct futex_q *this, *next;
742 int ret, op_ret, attempt = 0;
744 retryfull:
745 if (fshared)
746 down_read(fshared);
748 ret = get_futex_key(uaddr1, fshared, &key1);
749 if (unlikely(ret != 0))
750 goto out;
751 ret = get_futex_key(uaddr2, fshared, &key2);
752 if (unlikely(ret != 0))
753 goto out;
755 hb1 = hash_futex(&key1);
756 hb2 = hash_futex(&key2);
758 retry:
759 double_lock_hb(hb1, hb2);
761 op_ret = futex_atomic_op_inuser(op, uaddr2);
762 if (unlikely(op_ret < 0)) {
763 u32 dummy;
765 spin_unlock(&hb1->lock);
766 if (hb1 != hb2)
767 spin_unlock(&hb2->lock);
769 #ifndef CONFIG_MMU
771 * we don't get EFAULT from MMU faults if we don't have an MMU,
772 * but we might get them from range checking
774 ret = op_ret;
775 goto out;
776 #endif
778 if (unlikely(op_ret != -EFAULT)) {
779 ret = op_ret;
780 goto out;
784 * futex_atomic_op_inuser needs to both read and write
785 * *(int __user *)uaddr2, but we can't modify it
786 * non-atomically. Therefore, if get_user below is not
787 * enough, we need to handle the fault ourselves, while
788 * still holding the mmap_sem.
790 if (attempt++) {
791 ret = futex_handle_fault((unsigned long)uaddr2,
792 fshared, attempt);
793 if (ret)
794 goto out;
795 goto retry;
799 * If we would have faulted, release mmap_sem,
800 * fault it in and start all over again.
802 if (fshared)
803 up_read(fshared);
805 ret = get_user(dummy, uaddr2);
806 if (ret)
807 return ret;
809 goto retryfull;
812 head = &hb1->chain;
814 plist_for_each_entry_safe(this, next, head, list) {
815 if (match_futex (&this->key, &key1)) {
816 wake_futex(this);
817 if (++ret >= nr_wake)
818 break;
822 if (op_ret > 0) {
823 head = &hb2->chain;
825 op_ret = 0;
826 plist_for_each_entry_safe(this, next, head, list) {
827 if (match_futex (&this->key, &key2)) {
828 wake_futex(this);
829 if (++op_ret >= nr_wake2)
830 break;
833 ret += op_ret;
836 spin_unlock(&hb1->lock);
837 if (hb1 != hb2)
838 spin_unlock(&hb2->lock);
839 out:
840 if (fshared)
841 up_read(fshared);
842 return ret;
846 * Requeue all waiters hashed on one physical page to another
847 * physical page.
849 static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
850 u32 __user *uaddr2,
851 int nr_wake, int nr_requeue, u32 *cmpval)
853 union futex_key key1, key2;
854 struct futex_hash_bucket *hb1, *hb2;
855 struct plist_head *head1;
856 struct futex_q *this, *next;
857 int ret, drop_count = 0;
859 retry:
860 if (fshared)
861 down_read(fshared);
863 ret = get_futex_key(uaddr1, fshared, &key1);
864 if (unlikely(ret != 0))
865 goto out;
866 ret = get_futex_key(uaddr2, fshared, &key2);
867 if (unlikely(ret != 0))
868 goto out;
870 hb1 = hash_futex(&key1);
871 hb2 = hash_futex(&key2);
873 double_lock_hb(hb1, hb2);
875 if (likely(cmpval != NULL)) {
876 u32 curval;
878 ret = get_futex_value_locked(&curval, uaddr1);
880 if (unlikely(ret)) {
881 spin_unlock(&hb1->lock);
882 if (hb1 != hb2)
883 spin_unlock(&hb2->lock);
886 * If we would have faulted, release mmap_sem, fault
887 * it in and start all over again.
889 if (fshared)
890 up_read(fshared);
892 ret = get_user(curval, uaddr1);
894 if (!ret)
895 goto retry;
897 return ret;
899 if (curval != *cmpval) {
900 ret = -EAGAIN;
901 goto out_unlock;
905 head1 = &hb1->chain;
906 plist_for_each_entry_safe(this, next, head1, list) {
907 if (!match_futex (&this->key, &key1))
908 continue;
909 if (++ret <= nr_wake) {
910 wake_futex(this);
911 } else {
913 * If key1 and key2 hash to the same bucket, no need to
914 * requeue.
916 if (likely(head1 != &hb2->chain)) {
917 plist_del(&this->list, &hb1->chain);
918 plist_add(&this->list, &hb2->chain);
919 this->lock_ptr = &hb2->lock;
920 #ifdef CONFIG_DEBUG_PI_LIST
921 this->list.plist.lock = &hb2->lock;
922 #endif
924 this->key = key2;
925 get_futex_key_refs(&key2);
926 drop_count++;
928 if (ret - nr_wake >= nr_requeue)
929 break;
933 out_unlock:
934 spin_unlock(&hb1->lock);
935 if (hb1 != hb2)
936 spin_unlock(&hb2->lock);
938 /* drop_futex_key_refs() must be called outside the spinlocks. */
939 while (--drop_count >= 0)
940 drop_futex_key_refs(&key1);
942 out:
943 if (fshared)
944 up_read(fshared);
945 return ret;
948 /* The key must be already stored in q->key. */
949 static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
951 struct futex_hash_bucket *hb;
953 init_waitqueue_head(&q->waiters);
955 get_futex_key_refs(&q->key);
956 hb = hash_futex(&q->key);
957 q->lock_ptr = &hb->lock;
959 spin_lock(&hb->lock);
960 return hb;
963 static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
965 int prio;
968 * The priority used to register this element is
969 * - either the real thread-priority for the real-time threads
970 * (i.e. threads with a priority lower than MAX_RT_PRIO)
971 * - or MAX_RT_PRIO for non-RT threads.
972 * Thus, all RT-threads are woken first in priority order, and
973 * the others are woken last, in FIFO order.
975 prio = min(current->normal_prio, MAX_RT_PRIO);
977 plist_node_init(&q->list, prio);
978 #ifdef CONFIG_DEBUG_PI_LIST
979 q->list.plist.lock = &hb->lock;
980 #endif
981 plist_add(&q->list, &hb->chain);
982 q->task = current;
983 spin_unlock(&hb->lock);
986 static inline void
987 queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
989 spin_unlock(&hb->lock);
990 drop_futex_key_refs(&q->key);
994 * queue_me and unqueue_me must be called as a pair, each
995 * exactly once. They are called with the hashed spinlock held.
998 /* Return 1 if we were still queued (ie. 0 means we were woken) */
999 static int unqueue_me(struct futex_q *q)
1001 spinlock_t *lock_ptr;
1002 int ret = 0;
1004 /* In the common case we don't take the spinlock, which is nice. */
1005 retry:
1006 lock_ptr = q->lock_ptr;
1007 barrier();
1008 if (lock_ptr != 0) {
1009 spin_lock(lock_ptr);
1011 * q->lock_ptr can change between reading it and
1012 * spin_lock(), causing us to take the wrong lock. This
1013 * corrects the race condition.
1015 * Reasoning goes like this: if we have the wrong lock,
1016 * q->lock_ptr must have changed (maybe several times)
1017 * between reading it and the spin_lock(). It can
1018 * change again after the spin_lock() but only if it was
1019 * already changed before the spin_lock(). It cannot,
1020 * however, change back to the original value. Therefore
1021 * we can detect whether we acquired the correct lock.
1023 if (unlikely(lock_ptr != q->lock_ptr)) {
1024 spin_unlock(lock_ptr);
1025 goto retry;
1027 WARN_ON(plist_node_empty(&q->list));
1028 plist_del(&q->list, &q->list.plist);
1030 BUG_ON(q->pi_state);
1032 spin_unlock(lock_ptr);
1033 ret = 1;
1036 drop_futex_key_refs(&q->key);
1037 return ret;
1041 * PI futexes can not be requeued and must remove themself from the
1042 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
1043 * and dropped here.
1045 static void unqueue_me_pi(struct futex_q *q)
1047 WARN_ON(plist_node_empty(&q->list));
1048 plist_del(&q->list, &q->list.plist);
1050 BUG_ON(!q->pi_state);
1051 free_pi_state(q->pi_state);
1052 q->pi_state = NULL;
1054 spin_unlock(q->lock_ptr);
1056 drop_futex_key_refs(&q->key);
1060 * Fixup the pi_state owner with current.
1062 * Must be called with hash bucket lock held and mm->sem held for non
1063 * private futexes.
1065 static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
1066 struct task_struct *curr)
1068 u32 newtid = curr->pid | FUTEX_WAITERS;
1069 struct futex_pi_state *pi_state = q->pi_state;
1070 u32 uval, curval, newval;
1071 int ret;
1073 /* Owner died? */
1074 if (pi_state->owner != NULL) {
1075 spin_lock_irq(&pi_state->owner->pi_lock);
1076 WARN_ON(list_empty(&pi_state->list));
1077 list_del_init(&pi_state->list);
1078 spin_unlock_irq(&pi_state->owner->pi_lock);
1079 } else
1080 newtid |= FUTEX_OWNER_DIED;
1082 pi_state->owner = curr;
1084 spin_lock_irq(&curr->pi_lock);
1085 WARN_ON(!list_empty(&pi_state->list));
1086 list_add(&pi_state->list, &curr->pi_state_list);
1087 spin_unlock_irq(&curr->pi_lock);
1090 * We own it, so we have to replace the pending owner
1091 * TID. This must be atomic as we have preserve the
1092 * owner died bit here.
1094 ret = get_futex_value_locked(&uval, uaddr);
1096 while (!ret) {
1097 newval = (uval & FUTEX_OWNER_DIED) | newtid;
1099 pagefault_disable();
1100 curval = futex_atomic_cmpxchg_inatomic(uaddr,
1101 uval, newval);
1102 pagefault_enable();
1104 if (curval == -EFAULT)
1105 ret = -EFAULT;
1106 if (curval == uval)
1107 break;
1108 uval = curval;
1110 return ret;
1114 * In case we must use restart_block to restart a futex_wait,
1115 * we encode in the 'flags' shared capability
1117 #define FLAGS_SHARED 1
1119 static long futex_wait_restart(struct restart_block *restart);
1120 static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1121 u32 val, ktime_t *abs_time)
1123 struct task_struct *curr = current;
1124 DECLARE_WAITQUEUE(wait, curr);
1125 struct futex_hash_bucket *hb;
1126 struct futex_q q;
1127 u32 uval;
1128 int ret;
1129 struct hrtimer_sleeper t;
1130 int rem = 0;
1132 q.pi_state = NULL;
1133 retry:
1134 if (fshared)
1135 down_read(fshared);
1137 ret = get_futex_key(uaddr, fshared, &q.key);
1138 if (unlikely(ret != 0))
1139 goto out_release_sem;
1141 hb = queue_lock(&q);
1144 * Access the page AFTER the futex is queued.
1145 * Order is important:
1147 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
1148 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
1150 * The basic logical guarantee of a futex is that it blocks ONLY
1151 * if cond(var) is known to be true at the time of blocking, for
1152 * any cond. If we queued after testing *uaddr, that would open
1153 * a race condition where we could block indefinitely with
1154 * cond(var) false, which would violate the guarantee.
1156 * A consequence is that futex_wait() can return zero and absorb
1157 * a wakeup when *uaddr != val on entry to the syscall. This is
1158 * rare, but normal.
1160 * for shared futexes, we hold the mmap semaphore, so the mapping
1161 * cannot have changed since we looked it up in get_futex_key.
1163 ret = get_futex_value_locked(&uval, uaddr);
1165 if (unlikely(ret)) {
1166 queue_unlock(&q, hb);
1169 * If we would have faulted, release mmap_sem, fault it in and
1170 * start all over again.
1172 if (fshared)
1173 up_read(fshared);
1175 ret = get_user(uval, uaddr);
1177 if (!ret)
1178 goto retry;
1179 return ret;
1181 ret = -EWOULDBLOCK;
1182 if (uval != val)
1183 goto out_unlock_release_sem;
1185 /* Only actually queue if *uaddr contained val. */
1186 queue_me(&q, hb);
1189 * Now the futex is queued and we have checked the data, we
1190 * don't want to hold mmap_sem while we sleep.
1192 if (fshared)
1193 up_read(fshared);
1196 * There might have been scheduling since the queue_me(), as we
1197 * cannot hold a spinlock across the get_user() in case it
1198 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
1199 * queueing ourselves into the futex hash. This code thus has to
1200 * rely on the futex_wake() code removing us from hash when it
1201 * wakes us up.
1204 /* add_wait_queue is the barrier after __set_current_state. */
1205 __set_current_state(TASK_INTERRUPTIBLE);
1206 add_wait_queue(&q.waiters, &wait);
1208 * !plist_node_empty() is safe here without any lock.
1209 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
1211 if (likely(!plist_node_empty(&q.list))) {
1212 if (!abs_time)
1213 schedule();
1214 else {
1215 hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1216 hrtimer_init_sleeper(&t, current);
1217 t.timer.expires = *abs_time;
1219 hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS);
1222 * the timer could have already expired, in which
1223 * case current would be flagged for rescheduling.
1224 * Don't bother calling schedule.
1226 if (likely(t.task))
1227 schedule();
1229 hrtimer_cancel(&t.timer);
1231 /* Flag if a timeout occured */
1232 rem = (t.task == NULL);
1235 __set_current_state(TASK_RUNNING);
1238 * NOTE: we don't remove ourselves from the waitqueue because
1239 * we are the only user of it.
1242 /* If we were woken (and unqueued), we succeeded, whatever. */
1243 if (!unqueue_me(&q))
1244 return 0;
1245 if (rem)
1246 return -ETIMEDOUT;
1249 * We expect signal_pending(current), but another thread may
1250 * have handled it for us already.
1252 if (!abs_time)
1253 return -ERESTARTSYS;
1254 else {
1255 struct restart_block *restart;
1256 restart = &current_thread_info()->restart_block;
1257 restart->fn = futex_wait_restart;
1258 restart->futex.uaddr = (u32 *)uaddr;
1259 restart->futex.val = val;
1260 restart->futex.time = abs_time->tv64;
1261 restart->futex.flags = 0;
1263 if (fshared)
1264 restart->futex.flags |= FLAGS_SHARED;
1265 return -ERESTART_RESTARTBLOCK;
1268 out_unlock_release_sem:
1269 queue_unlock(&q, hb);
1271 out_release_sem:
1272 if (fshared)
1273 up_read(fshared);
1274 return ret;
1278 static long futex_wait_restart(struct restart_block *restart)
1280 u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
1281 struct rw_semaphore *fshared = NULL;
1282 ktime_t t;
1284 t.tv64 = restart->futex.time;
1285 restart->fn = do_no_restart_syscall;
1286 if (restart->futex.flags & FLAGS_SHARED)
1287 fshared = &current->mm->mmap_sem;
1288 return (long)futex_wait(uaddr, fshared, restart->futex.val, &t);
1293 * Userspace tried a 0 -> TID atomic transition of the futex value
1294 * and failed. The kernel side here does the whole locking operation:
1295 * if there are waiters then it will block, it does PI, etc. (Due to
1296 * races the kernel might see a 0 value of the futex too.)
1298 static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1299 int detect, ktime_t *time, int trylock)
1301 struct hrtimer_sleeper timeout, *to = NULL;
1302 struct task_struct *curr = current;
1303 struct futex_hash_bucket *hb;
1304 u32 uval, newval, curval;
1305 struct futex_q q;
1306 int ret, lock_taken, ownerdied = 0, attempt = 0;
1308 if (refill_pi_state_cache())
1309 return -ENOMEM;
1311 if (time) {
1312 to = &timeout;
1313 hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
1314 hrtimer_init_sleeper(to, current);
1315 to->timer.expires = *time;
1318 q.pi_state = NULL;
1319 retry:
1320 if (fshared)
1321 down_read(fshared);
1323 ret = get_futex_key(uaddr, fshared, &q.key);
1324 if (unlikely(ret != 0))
1325 goto out_release_sem;
1327 retry_unlocked:
1328 hb = queue_lock(&q);
1330 retry_locked:
1331 ret = lock_taken = 0;
1334 * To avoid races, we attempt to take the lock here again
1335 * (by doing a 0 -> TID atomic cmpxchg), while holding all
1336 * the locks. It will most likely not succeed.
1338 newval = current->pid;
1340 pagefault_disable();
1341 curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval);
1342 pagefault_enable();
1344 if (unlikely(curval == -EFAULT))
1345 goto uaddr_faulted;
1348 * Detect deadlocks. In case of REQUEUE_PI this is a valid
1349 * situation and we return success to user space.
1351 if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) {
1352 ret = -EDEADLK;
1353 goto out_unlock_release_sem;
1357 * Surprise - we got the lock. Just return to userspace:
1359 if (unlikely(!curval))
1360 goto out_unlock_release_sem;
1362 uval = curval;
1365 * Set the WAITERS flag, so the owner will know it has someone
1366 * to wake at next unlock
1368 newval = curval | FUTEX_WAITERS;
1371 * There are two cases, where a futex might have no owner (the
1372 * owner TID is 0): OWNER_DIED. We take over the futex in this
1373 * case. We also do an unconditional take over, when the owner
1374 * of the futex died.
1376 * This is safe as we are protected by the hash bucket lock !
1378 if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
1379 /* Keep the OWNER_DIED bit */
1380 newval = (curval & ~FUTEX_TID_MASK) | current->pid;
1381 ownerdied = 0;
1382 lock_taken = 1;
1385 pagefault_disable();
1386 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
1387 pagefault_enable();
1389 if (unlikely(curval == -EFAULT))
1390 goto uaddr_faulted;
1391 if (unlikely(curval != uval))
1392 goto retry_locked;
1395 * We took the lock due to owner died take over.
1397 if (unlikely(lock_taken))
1398 goto out_unlock_release_sem;
1401 * We dont have the lock. Look up the PI state (or create it if
1402 * we are the first waiter):
1404 ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
1406 if (unlikely(ret)) {
1407 switch (ret) {
1409 case -EAGAIN:
1411 * Task is exiting and we just wait for the
1412 * exit to complete.
1414 queue_unlock(&q, hb);
1415 if (fshared)
1416 up_read(fshared);
1417 cond_resched();
1418 goto retry;
1420 case -ESRCH:
1422 * No owner found for this futex. Check if the
1423 * OWNER_DIED bit is set to figure out whether
1424 * this is a robust futex or not.
1426 if (get_futex_value_locked(&curval, uaddr))
1427 goto uaddr_faulted;
1430 * We simply start over in case of a robust
1431 * futex. The code above will take the futex
1432 * and return happy.
1434 if (curval & FUTEX_OWNER_DIED) {
1435 ownerdied = 1;
1436 goto retry_locked;
1438 default:
1439 goto out_unlock_release_sem;
1444 * Only actually queue now that the atomic ops are done:
1446 queue_me(&q, hb);
1449 * Now the futex is queued and we have checked the data, we
1450 * don't want to hold mmap_sem while we sleep.
1452 if (fshared)
1453 up_read(fshared);
1455 WARN_ON(!q.pi_state);
1457 * Block on the PI mutex:
1459 if (!trylock)
1460 ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
1461 else {
1462 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
1463 /* Fixup the trylock return value: */
1464 ret = ret ? 0 : -EWOULDBLOCK;
1467 if (fshared)
1468 down_read(fshared);
1469 spin_lock(q.lock_ptr);
1471 if (!ret) {
1473 * Got the lock. We might not be the anticipated owner
1474 * if we did a lock-steal - fix up the PI-state in
1475 * that case:
1477 if (q.pi_state->owner != curr)
1478 ret = fixup_pi_state_owner(uaddr, &q, curr);
1479 } else {
1481 * Catch the rare case, where the lock was released
1482 * when we were on the way back before we locked the
1483 * hash bucket.
1485 if (q.pi_state->owner == curr &&
1486 rt_mutex_trylock(&q.pi_state->pi_mutex)) {
1487 ret = 0;
1488 } else {
1490 * Paranoia check. If we did not take the lock
1491 * in the trylock above, then we should not be
1492 * the owner of the rtmutex, neither the real
1493 * nor the pending one:
1495 if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr)
1496 printk(KERN_ERR "futex_lock_pi: ret = %d "
1497 "pi-mutex: %p pi-state %p\n", ret,
1498 q.pi_state->pi_mutex.owner,
1499 q.pi_state->owner);
1503 /* Unqueue and drop the lock */
1504 unqueue_me_pi(&q);
1505 if (fshared)
1506 up_read(fshared);
1508 return ret != -EINTR ? ret : -ERESTARTNOINTR;
1510 out_unlock_release_sem:
1511 queue_unlock(&q, hb);
1513 out_release_sem:
1514 if (fshared)
1515 up_read(fshared);
1516 return ret;
1518 uaddr_faulted:
1520 * We have to r/w *(int __user *)uaddr, but we can't modify it
1521 * non-atomically. Therefore, if get_user below is not
1522 * enough, we need to handle the fault ourselves, while
1523 * still holding the mmap_sem.
1525 * ... and hb->lock. :-) --ANK
1527 queue_unlock(&q, hb);
1529 if (attempt++) {
1530 ret = futex_handle_fault((unsigned long)uaddr, fshared,
1531 attempt);
1532 if (ret)
1533 goto out_release_sem;
1534 goto retry_unlocked;
1537 if (fshared)
1538 up_read(fshared);
1540 ret = get_user(uval, uaddr);
1541 if (!ret && (uval != -EFAULT))
1542 goto retry;
1544 return ret;
1548 * Userspace attempted a TID -> 0 atomic transition, and failed.
1549 * This is the in-kernel slowpath: we look up the PI state (if any),
1550 * and do the rt-mutex unlock.
1552 static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared)
1554 struct futex_hash_bucket *hb;
1555 struct futex_q *this, *next;
1556 u32 uval;
1557 struct plist_head *head;
1558 union futex_key key;
1559 int ret, attempt = 0;
1561 retry:
1562 if (get_user(uval, uaddr))
1563 return -EFAULT;
1565 * We release only a lock we actually own:
1567 if ((uval & FUTEX_TID_MASK) != current->pid)
1568 return -EPERM;
1570 * First take all the futex related locks:
1572 if (fshared)
1573 down_read(fshared);
1575 ret = get_futex_key(uaddr, fshared, &key);
1576 if (unlikely(ret != 0))
1577 goto out;
1579 hb = hash_futex(&key);
1580 retry_unlocked:
1581 spin_lock(&hb->lock);
1584 * To avoid races, try to do the TID -> 0 atomic transition
1585 * again. If it succeeds then we can return without waking
1586 * anyone else up:
1588 if (!(uval & FUTEX_OWNER_DIED)) {
1589 pagefault_disable();
1590 uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0);
1591 pagefault_enable();
1594 if (unlikely(uval == -EFAULT))
1595 goto pi_faulted;
1597 * Rare case: we managed to release the lock atomically,
1598 * no need to wake anyone else up:
1600 if (unlikely(uval == current->pid))
1601 goto out_unlock;
1604 * Ok, other tasks may need to be woken up - check waiters
1605 * and do the wakeup if necessary:
1607 head = &hb->chain;
1609 plist_for_each_entry_safe(this, next, head, list) {
1610 if (!match_futex (&this->key, &key))
1611 continue;
1612 ret = wake_futex_pi(uaddr, uval, this);
1614 * The atomic access to the futex value
1615 * generated a pagefault, so retry the
1616 * user-access and the wakeup:
1618 if (ret == -EFAULT)
1619 goto pi_faulted;
1620 goto out_unlock;
1623 * No waiters - kernel unlocks the futex:
1625 if (!(uval & FUTEX_OWNER_DIED)) {
1626 ret = unlock_futex_pi(uaddr, uval);
1627 if (ret == -EFAULT)
1628 goto pi_faulted;
1631 out_unlock:
1632 spin_unlock(&hb->lock);
1633 out:
1634 if (fshared)
1635 up_read(fshared);
1637 return ret;
1639 pi_faulted:
1641 * We have to r/w *(int __user *)uaddr, but we can't modify it
1642 * non-atomically. Therefore, if get_user below is not
1643 * enough, we need to handle the fault ourselves, while
1644 * still holding the mmap_sem.
1646 * ... and hb->lock. --ANK
1648 spin_unlock(&hb->lock);
1650 if (attempt++) {
1651 ret = futex_handle_fault((unsigned long)uaddr, fshared,
1652 attempt);
1653 if (ret)
1654 goto out;
1655 uval = 0;
1656 goto retry_unlocked;
1659 if (fshared)
1660 up_read(fshared);
1662 ret = get_user(uval, uaddr);
1663 if (!ret && (uval != -EFAULT))
1664 goto retry;
1666 return ret;
1670 * Support for robust futexes: the kernel cleans up held futexes at
1671 * thread exit time.
1673 * Implementation: user-space maintains a per-thread list of locks it
1674 * is holding. Upon do_exit(), the kernel carefully walks this list,
1675 * and marks all locks that are owned by this thread with the
1676 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
1677 * always manipulated with the lock held, so the list is private and
1678 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
1679 * field, to allow the kernel to clean up if the thread dies after
1680 * acquiring the lock, but just before it could have added itself to
1681 * the list. There can only be one such pending lock.
1685 * sys_set_robust_list - set the robust-futex list head of a task
1686 * @head: pointer to the list-head
1687 * @len: length of the list-head, as userspace expects
1689 asmlinkage long
1690 sys_set_robust_list(struct robust_list_head __user *head,
1691 size_t len)
1694 * The kernel knows only one size for now:
1696 if (unlikely(len != sizeof(*head)))
1697 return -EINVAL;
1699 current->robust_list = head;
1701 return 0;
1705 * sys_get_robust_list - get the robust-futex list head of a task
1706 * @pid: pid of the process [zero for current task]
1707 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
1708 * @len_ptr: pointer to a length field, the kernel fills in the header size
1710 asmlinkage long
1711 sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
1712 size_t __user *len_ptr)
1714 struct robust_list_head __user *head;
1715 unsigned long ret;
1717 if (!pid)
1718 head = current->robust_list;
1719 else {
1720 struct task_struct *p;
1722 ret = -ESRCH;
1723 rcu_read_lock();
1724 p = find_task_by_pid(pid);
1725 if (!p)
1726 goto err_unlock;
1727 ret = -EPERM;
1728 if ((current->euid != p->euid) && (current->euid != p->uid) &&
1729 !capable(CAP_SYS_PTRACE))
1730 goto err_unlock;
1731 head = p->robust_list;
1732 rcu_read_unlock();
1735 if (put_user(sizeof(*head), len_ptr))
1736 return -EFAULT;
1737 return put_user(head, head_ptr);
1739 err_unlock:
1740 rcu_read_unlock();
1742 return ret;
1746 * Process a futex-list entry, check whether it's owned by the
1747 * dying task, and do notification if so:
1749 int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
1751 u32 uval, nval, mval;
1753 retry:
1754 if (get_user(uval, uaddr))
1755 return -1;
1757 if ((uval & FUTEX_TID_MASK) == curr->pid) {
1759 * Ok, this dying thread is truly holding a futex
1760 * of interest. Set the OWNER_DIED bit atomically
1761 * via cmpxchg, and if the value had FUTEX_WAITERS
1762 * set, wake up a waiter (if any). (We have to do a
1763 * futex_wake() even if OWNER_DIED is already set -
1764 * to handle the rare but possible case of recursive
1765 * thread-death.) The rest of the cleanup is done in
1766 * userspace.
1768 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1769 nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
1771 if (nval == -EFAULT)
1772 return -1;
1774 if (nval != uval)
1775 goto retry;
1778 * Wake robust non-PI futexes here. The wakeup of
1779 * PI futexes happens in exit_pi_state():
1781 if (!pi) {
1782 if (uval & FUTEX_WAITERS)
1783 futex_wake(uaddr, &curr->mm->mmap_sem, 1);
1786 return 0;
1790 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
1792 static inline int fetch_robust_entry(struct robust_list __user **entry,
1793 struct robust_list __user * __user *head,
1794 int *pi)
1796 unsigned long uentry;
1798 if (get_user(uentry, (unsigned long __user *)head))
1799 return -EFAULT;
1801 *entry = (void __user *)(uentry & ~1UL);
1802 *pi = uentry & 1;
1804 return 0;
1808 * Walk curr->robust_list (very carefully, it's a userspace list!)
1809 * and mark any locks found there dead, and notify any waiters.
1811 * We silently return on any sign of list-walking problem.
1813 void exit_robust_list(struct task_struct *curr)
1815 struct robust_list_head __user *head = curr->robust_list;
1816 struct robust_list __user *entry, *pending;
1817 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
1818 unsigned long futex_offset;
1821 * Fetch the list head (which was registered earlier, via
1822 * sys_set_robust_list()):
1824 if (fetch_robust_entry(&entry, &head->list.next, &pi))
1825 return;
1827 * Fetch the relative futex offset:
1829 if (get_user(futex_offset, &head->futex_offset))
1830 return;
1832 * Fetch any possibly pending lock-add first, and handle it
1833 * if it exists:
1835 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
1836 return;
1838 if (pending)
1839 handle_futex_death((void __user *)pending + futex_offset,
1840 curr, pip);
1842 while (entry != &head->list) {
1844 * A pending lock might already be on the list, so
1845 * don't process it twice:
1847 if (entry != pending)
1848 if (handle_futex_death((void __user *)entry + futex_offset,
1849 curr, pi))
1850 return;
1852 * Fetch the next entry in the list:
1854 if (fetch_robust_entry(&entry, &entry->next, &pi))
1855 return;
1857 * Avoid excessively long or circular lists:
1859 if (!--limit)
1860 break;
1862 cond_resched();
1866 long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
1867 u32 __user *uaddr2, u32 val2, u32 val3)
1869 int ret;
1870 int cmd = op & FUTEX_CMD_MASK;
1871 struct rw_semaphore *fshared = NULL;
1873 if (!(op & FUTEX_PRIVATE_FLAG))
1874 fshared = &current->mm->mmap_sem;
1876 switch (cmd) {
1877 case FUTEX_WAIT:
1878 ret = futex_wait(uaddr, fshared, val, timeout);
1879 break;
1880 case FUTEX_WAKE:
1881 ret = futex_wake(uaddr, fshared, val);
1882 break;
1883 case FUTEX_REQUEUE:
1884 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
1885 break;
1886 case FUTEX_CMP_REQUEUE:
1887 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
1888 break;
1889 case FUTEX_WAKE_OP:
1890 ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
1891 break;
1892 case FUTEX_LOCK_PI:
1893 ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
1894 break;
1895 case FUTEX_UNLOCK_PI:
1896 ret = futex_unlock_pi(uaddr, fshared);
1897 break;
1898 case FUTEX_TRYLOCK_PI:
1899 ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
1900 break;
1901 default:
1902 ret = -ENOSYS;
1904 return ret;
1908 asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
1909 struct timespec __user *utime, u32 __user *uaddr2,
1910 u32 val3)
1912 struct timespec ts;
1913 ktime_t t, *tp = NULL;
1914 u32 val2 = 0;
1915 int cmd = op & FUTEX_CMD_MASK;
1917 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
1918 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
1919 return -EFAULT;
1920 if (!timespec_valid(&ts))
1921 return -EINVAL;
1923 t = timespec_to_ktime(ts);
1924 if (cmd == FUTEX_WAIT)
1925 t = ktime_add(ktime_get(), t);
1926 tp = &t;
1929 * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
1930 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
1932 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
1933 cmd == FUTEX_WAKE_OP)
1934 val2 = (u32) (unsigned long) utime;
1936 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
1939 static int __init init(void)
1941 int i;
1943 for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
1944 plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
1945 spin_lock_init(&futex_queues[i].lock);
1948 return 0;
1950 __initcall(init);