aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/futex.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/futex.c')
-rw-r--r--kernel/futex.c1091
1 files changed, 939 insertions, 152 deletions
diff --git a/kernel/futex.c b/kernel/futex.c
index 5699c512057b..cf0c8e21d1ab 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -12,6 +12,10 @@
12 * (C) Copyright 2006 Red Hat Inc, All Rights Reserved 12 * (C) Copyright 2006 Red Hat Inc, All Rights Reserved
13 * Thanks to Thomas Gleixner for suggestions, analysis and fixes. 13 * Thanks to Thomas Gleixner for suggestions, analysis and fixes.
14 * 14 *
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>
18 *
15 * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly 19 * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
16 * enough at me, Linus for the original (flawed) idea, Matthew 20 * enough at me, Linus for the original (flawed) idea, Matthew
17 * Kirkwood for proof-of-concept implementation. 21 * Kirkwood for proof-of-concept implementation.
@@ -46,6 +50,8 @@
46#include <linux/signal.h> 50#include <linux/signal.h>
47#include <asm/futex.h> 51#include <asm/futex.h>
48 52
53#include "rtmutex_common.h"
54
49#define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8) 55#define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
50 56
51/* 57/*
@@ -63,7 +69,7 @@ union futex_key {
63 int offset; 69 int offset;
64 } shared; 70 } shared;
65 struct { 71 struct {
66 unsigned long uaddr; 72 unsigned long address;
67 struct mm_struct *mm; 73 struct mm_struct *mm;
68 int offset; 74 int offset;
69 } private; 75 } private;
@@ -75,6 +81,27 @@ union futex_key {
75}; 81};
76 82
77/* 83/*
84 * Priority Inheritance state:
85 */
86struct futex_pi_state {
87 /*
88 * list of 'owned' pi_state instances - these have to be
89 * cleaned up in do_exit() if the task exits prematurely:
90 */
91 struct list_head list;
92
93 /*
94 * The PI object:
95 */
96 struct rt_mutex pi_mutex;
97
98 struct task_struct *owner;
99 atomic_t refcount;
100
101 union futex_key key;
102};
103
104/*
78 * We use this hashed waitqueue instead of a normal wait_queue_t, so 105 * 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). 106 * we can wake only the relevant ones (hashed queues may be shared).
80 * 107 *
@@ -87,15 +114,19 @@ struct futex_q {
87 struct list_head list; 114 struct list_head list;
88 wait_queue_head_t waiters; 115 wait_queue_head_t waiters;
89 116
90 /* Which hash list lock to use. */ 117 /* Which hash list lock to use: */
91 spinlock_t *lock_ptr; 118 spinlock_t *lock_ptr;
92 119
93 /* Key which the futex is hashed on. */ 120 /* Key which the futex is hashed on: */
94 union futex_key key; 121 union futex_key key;
95 122
96 /* For fd, sigio sent using these. */ 123 /* For fd, sigio sent using these: */
97 int fd; 124 int fd;
98 struct file *filp; 125 struct file *filp;
126
127 /* Optional priority inheritance state: */
128 struct futex_pi_state *pi_state;
129 struct task_struct *task;
99}; 130};
100 131
101/* 132/*
@@ -144,8 +175,9 @@ static inline int match_futex(union futex_key *key1, union futex_key *key2)
144 * 175 *
145 * Should be called with &current->mm->mmap_sem but NOT any spinlocks. 176 * Should be called with &current->mm->mmap_sem but NOT any spinlocks.
146 */ 177 */
147static int get_futex_key(unsigned long uaddr, union futex_key *key) 178static int get_futex_key(u32 __user *uaddr, union futex_key *key)
148{ 179{
180 unsigned long address = (unsigned long)uaddr;
149 struct mm_struct *mm = current->mm; 181 struct mm_struct *mm = current->mm;
150 struct vm_area_struct *vma; 182 struct vm_area_struct *vma;
151 struct page *page; 183 struct page *page;
@@ -154,16 +186,16 @@ static int get_futex_key(unsigned long uaddr, union futex_key *key)
154 /* 186 /*
155 * The futex address must be "naturally" aligned. 187 * The futex address must be "naturally" aligned.
156 */ 188 */
157 key->both.offset = uaddr % PAGE_SIZE; 189 key->both.offset = address % PAGE_SIZE;
158 if (unlikely((key->both.offset % sizeof(u32)) != 0)) 190 if (unlikely((key->both.offset % sizeof(u32)) != 0))
159 return -EINVAL; 191 return -EINVAL;
160 uaddr -= key->both.offset; 192 address -= key->both.offset;
161 193
162 /* 194 /*
163 * The futex is hashed differently depending on whether 195 * The futex is hashed differently depending on whether
164 * it's in a shared or private mapping. So check vma first. 196 * it's in a shared or private mapping. So check vma first.
165 */ 197 */
166 vma = find_extend_vma(mm, uaddr); 198 vma = find_extend_vma(mm, address);
167 if (unlikely(!vma)) 199 if (unlikely(!vma))
168 return -EFAULT; 200 return -EFAULT;
169 201
@@ -184,7 +216,7 @@ static int get_futex_key(unsigned long uaddr, union futex_key *key)
184 */ 216 */
185 if (likely(!(vma->vm_flags & VM_MAYSHARE))) { 217 if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
186 key->private.mm = mm; 218 key->private.mm = mm;
187 key->private.uaddr = uaddr; 219 key->private.address = address;
188 return 0; 220 return 0;
189 } 221 }
190 222
@@ -194,7 +226,7 @@ static int get_futex_key(unsigned long uaddr, union futex_key *key)
194 key->shared.inode = vma->vm_file->f_dentry->d_inode; 226 key->shared.inode = vma->vm_file->f_dentry->d_inode;
195 key->both.offset++; /* Bit 0 of offset indicates inode-based key. */ 227 key->both.offset++; /* Bit 0 of offset indicates inode-based key. */
196 if (likely(!(vma->vm_flags & VM_NONLINEAR))) { 228 if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
197 key->shared.pgoff = (((uaddr - vma->vm_start) >> PAGE_SHIFT) 229 key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
198 + vma->vm_pgoff); 230 + vma->vm_pgoff);
199 return 0; 231 return 0;
200 } 232 }
@@ -205,7 +237,7 @@ static int get_futex_key(unsigned long uaddr, union futex_key *key)
205 * from swap. But that's a lot of code to duplicate here 237 * from swap. But that's a lot of code to duplicate here
206 * for a rare case, so we simply fetch the page. 238 * for a rare case, so we simply fetch the page.
207 */ 239 */
208 err = get_user_pages(current, mm, uaddr, 1, 0, 0, &page, NULL); 240 err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
209 if (err >= 0) { 241 if (err >= 0) {
210 key->shared.pgoff = 242 key->shared.pgoff =
211 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); 243 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
@@ -246,18 +278,250 @@ static void drop_key_refs(union futex_key *key)
246 } 278 }
247} 279}
248 280
249static inline int get_futex_value_locked(int *dest, int __user *from) 281static inline int get_futex_value_locked(u32 *dest, u32 __user *from)
250{ 282{
251 int ret; 283 int ret;
252 284
253 inc_preempt_count(); 285 inc_preempt_count();
254 ret = __copy_from_user_inatomic(dest, from, sizeof(int)); 286 ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
255 dec_preempt_count(); 287 dec_preempt_count();
256 288
257 return ret ? -EFAULT : 0; 289 return ret ? -EFAULT : 0;
258} 290}
259 291
260/* 292/*
293 * Fault handling. Called with current->mm->mmap_sem held.
294 */
295static int futex_handle_fault(unsigned long address, int attempt)
296{
297 struct vm_area_struct * vma;
298 struct mm_struct *mm = current->mm;
299
300 if (attempt >= 2 || !(vma = find_vma(mm, address)) ||
301 vma->vm_start > address || !(vma->vm_flags & VM_WRITE))
302 return -EFAULT;
303
304 switch (handle_mm_fault(mm, vma, address, 1)) {
305 case VM_FAULT_MINOR:
306 current->min_flt++;
307 break;
308 case VM_FAULT_MAJOR:
309 current->maj_flt++;
310 break;
311 default:
312 return -EFAULT;
313 }
314 return 0;
315}
316
317/*
318 * PI code:
319 */
320static int refill_pi_state_cache(void)
321{
322 struct futex_pi_state *pi_state;
323
324 if (likely(current->pi_state_cache))
325 return 0;
326
327 pi_state = kmalloc(sizeof(*pi_state), GFP_KERNEL);
328
329 if (!pi_state)
330 return -ENOMEM;
331
332 memset(pi_state, 0, sizeof(*pi_state));
333 INIT_LIST_HEAD(&pi_state->list);
334 /* pi_mutex gets initialized later */
335 pi_state->owner = NULL;
336 atomic_set(&pi_state->refcount, 1);
337
338 current->pi_state_cache = pi_state;
339
340 return 0;
341}
342
343static struct futex_pi_state * alloc_pi_state(void)
344{
345 struct futex_pi_state *pi_state = current->pi_state_cache;
346
347 WARN_ON(!pi_state);
348 current->pi_state_cache = NULL;
349
350 return pi_state;
351}
352
353static void free_pi_state(struct futex_pi_state *pi_state)
354{
355 if (!atomic_dec_and_test(&pi_state->refcount))
356 return;
357
358 /*
359 * If pi_state->owner is NULL, the owner is most probably dying
360 * and has cleaned up the pi_state already
361 */
362 if (pi_state->owner) {
363 spin_lock_irq(&pi_state->owner->pi_lock);
364 list_del_init(&pi_state->list);
365 spin_unlock_irq(&pi_state->owner->pi_lock);
366
367 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
368 }
369
370 if (current->pi_state_cache)
371 kfree(pi_state);
372 else {
373 /*
374 * pi_state->list is already empty.
375 * clear pi_state->owner.
376 * refcount is at 0 - put it back to 1.
377 */
378 pi_state->owner = NULL;
379 atomic_set(&pi_state->refcount, 1);
380 current->pi_state_cache = pi_state;
381 }
382}
383
384/*
385 * Look up the task based on what TID userspace gave us.
386 * We dont trust it.
387 */
388static struct task_struct * futex_find_get_task(pid_t pid)
389{
390 struct task_struct *p;
391
392 read_lock(&tasklist_lock);
393 p = find_task_by_pid(pid);
394 if (!p)
395 goto out_unlock;
396 if ((current->euid != p->euid) && (current->euid != p->uid)) {
397 p = NULL;
398 goto out_unlock;
399 }
400 if (p->state == EXIT_ZOMBIE || p->exit_state == EXIT_ZOMBIE) {
401 p = NULL;
402 goto out_unlock;
403 }
404 get_task_struct(p);
405out_unlock:
406 read_unlock(&tasklist_lock);
407
408 return p;
409}
410
411/*
412 * This task is holding PI mutexes at exit time => bad.
413 * Kernel cleans up PI-state, but userspace is likely hosed.
414 * (Robust-futex cleanup is separate and might save the day for userspace.)
415 */
416void exit_pi_state_list(struct task_struct *curr)
417{
418 struct futex_hash_bucket *hb;
419 struct list_head *next, *head = &curr->pi_state_list;
420 struct futex_pi_state *pi_state;
421 union futex_key key;
422
423 /*
424 * We are a ZOMBIE and nobody can enqueue itself on
425 * pi_state_list anymore, but we have to be careful
426 * versus waiters unqueueing themselfs
427 */
428 spin_lock_irq(&curr->pi_lock);
429 while (!list_empty(head)) {
430
431 next = head->next;
432 pi_state = list_entry(next, struct futex_pi_state, list);
433 key = pi_state->key;
434 spin_unlock_irq(&curr->pi_lock);
435
436 hb = hash_futex(&key);
437 spin_lock(&hb->lock);
438
439 spin_lock_irq(&curr->pi_lock);
440 if (head->next != next) {
441 spin_unlock(&hb->lock);
442 continue;
443 }
444
445 list_del_init(&pi_state->list);
446
447 WARN_ON(pi_state->owner != curr);
448
449 pi_state->owner = NULL;
450 spin_unlock_irq(&curr->pi_lock);
451
452 rt_mutex_unlock(&pi_state->pi_mutex);
453
454 spin_unlock(&hb->lock);
455
456 spin_lock_irq(&curr->pi_lock);
457 }
458 spin_unlock_irq(&curr->pi_lock);
459}
460
461static int
462lookup_pi_state(u32 uval, struct futex_hash_bucket *hb, struct futex_q *me)
463{
464 struct futex_pi_state *pi_state = NULL;
465 struct futex_q *this, *next;
466 struct list_head *head;
467 struct task_struct *p;
468 pid_t pid;
469
470 head = &hb->chain;
471
472 list_for_each_entry_safe(this, next, head, list) {
473 if (match_futex (&this->key, &me->key)) {
474 /*
475 * Another waiter already exists - bump up
476 * the refcount and return its pi_state:
477 */
478 pi_state = this->pi_state;
479 /*
480 * Userspace might have messed up non PI and PI futexes
481 */
482 if (unlikely(!pi_state))
483 return -EINVAL;
484
485 atomic_inc(&pi_state->refcount);
486 me->pi_state = pi_state;
487
488 return 0;
489 }
490 }
491
492 /*
493 * We are the first waiter - try to look up the real owner and
494 * attach the new pi_state to it:
495 */
496 pid = uval & FUTEX_TID_MASK;
497 p = futex_find_get_task(pid);
498 if (!p)
499 return -ESRCH;
500
501 pi_state = alloc_pi_state();
502
503 /*
504 * Initialize the pi_mutex in locked state and make 'p'
505 * the owner of it:
506 */
507 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
508
509 /* Store the key for possible exit cleanups: */
510 pi_state->key = me->key;
511
512 spin_lock_irq(&p->pi_lock);
513 list_add(&pi_state->list, &p->pi_state_list);
514 pi_state->owner = p;
515 spin_unlock_irq(&p->pi_lock);
516
517 put_task_struct(p);
518
519 me->pi_state = pi_state;
520
521 return 0;
522}
523
524/*
261 * The hash bucket lock must be held when this is called. 525 * The hash bucket lock must be held when this is called.
262 * Afterwards, the futex_q must not be accessed. 526 * Afterwards, the futex_q must not be accessed.
263 */ 527 */
@@ -284,16 +548,96 @@ static void wake_futex(struct futex_q *q)
284 q->lock_ptr = NULL; 548 q->lock_ptr = NULL;
285} 549}
286 550
551static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
552{
553 struct task_struct *new_owner;
554 struct futex_pi_state *pi_state = this->pi_state;
555 u32 curval, newval;
556
557 if (!pi_state)
558 return -EINVAL;
559
560 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
561
562 /*
563 * This happens when we have stolen the lock and the original
564 * pending owner did not enqueue itself back on the rt_mutex.
565 * Thats not a tragedy. We know that way, that a lock waiter
566 * is on the fly. We make the futex_q waiter the pending owner.
567 */
568 if (!new_owner)
569 new_owner = this->task;
570
571 /*
572 * We pass it to the next owner. (The WAITERS bit is always
573 * kept enabled while there is PI state around. We must also
574 * preserve the owner died bit.)
575 */
576 newval = (uval & FUTEX_OWNER_DIED) | FUTEX_WAITERS | new_owner->pid;
577
578 inc_preempt_count();
579 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
580 dec_preempt_count();
581
582 if (curval == -EFAULT)
583 return -EFAULT;
584 if (curval != uval)
585 return -EINVAL;
586
587 list_del_init(&pi_state->owner->pi_state_list);
588 list_add(&pi_state->list, &new_owner->pi_state_list);
589 pi_state->owner = new_owner;
590 rt_mutex_unlock(&pi_state->pi_mutex);
591
592 return 0;
593}
594
595static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
596{
597 u32 oldval;
598
599 /*
600 * There is no waiter, so we unlock the futex. The owner died
601 * bit has not to be preserved here. We are the owner:
602 */
603 inc_preempt_count();
604 oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0);
605 dec_preempt_count();
606
607 if (oldval == -EFAULT)
608 return oldval;
609 if (oldval != uval)
610 return -EAGAIN;
611
612 return 0;
613}
614
615/*
616 * Express the locking dependencies for lockdep:
617 */
618static inline void
619double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
620{
621 if (hb1 <= hb2) {
622 spin_lock(&hb1->lock);
623 if (hb1 < hb2)
624 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
625 } else { /* hb1 > hb2 */
626 spin_lock(&hb2->lock);
627 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
628 }
629}
630
287/* 631/*
288 * Wake up all waiters hashed on the physical page that is mapped 632 * Wake up all waiters hashed on the physical page that is mapped
289 * to this virtual address: 633 * to this virtual address:
290 */ 634 */
291static int futex_wake(unsigned long uaddr, int nr_wake) 635static int futex_wake(u32 __user *uaddr, int nr_wake)
292{ 636{
293 union futex_key key; 637 struct futex_hash_bucket *hb;
294 struct futex_hash_bucket *bh;
295 struct list_head *head;
296 struct futex_q *this, *next; 638 struct futex_q *this, *next;
639 struct list_head *head;
640 union futex_key key;
297 int ret; 641 int ret;
298 642
299 down_read(&current->mm->mmap_sem); 643 down_read(&current->mm->mmap_sem);
@@ -302,19 +646,23 @@ static int futex_wake(unsigned long uaddr, int nr_wake)
302 if (unlikely(ret != 0)) 646 if (unlikely(ret != 0))
303 goto out; 647 goto out;
304 648
305 bh = hash_futex(&key); 649 hb = hash_futex(&key);
306 spin_lock(&bh->lock); 650 spin_lock(&hb->lock);
307 head = &bh->chain; 651 head = &hb->chain;
308 652
309 list_for_each_entry_safe(this, next, head, list) { 653 list_for_each_entry_safe(this, next, head, list) {
310 if (match_futex (&this->key, &key)) { 654 if (match_futex (&this->key, &key)) {
655 if (this->pi_state) {
656 ret = -EINVAL;
657 break;
658 }
311 wake_futex(this); 659 wake_futex(this);
312 if (++ret >= nr_wake) 660 if (++ret >= nr_wake)
313 break; 661 break;
314 } 662 }
315 } 663 }
316 664
317 spin_unlock(&bh->lock); 665 spin_unlock(&hb->lock);
318out: 666out:
319 up_read(&current->mm->mmap_sem); 667 up_read(&current->mm->mmap_sem);
320 return ret; 668 return ret;
@@ -324,10 +672,12 @@ out:
324 * Wake up all waiters hashed on the physical page that is mapped 672 * Wake up all waiters hashed on the physical page that is mapped
325 * to this virtual address: 673 * to this virtual address:
326 */ 674 */
327static int futex_wake_op(unsigned long uaddr1, unsigned long uaddr2, int nr_wake, int nr_wake2, int op) 675static int
676futex_wake_op(u32 __user *uaddr1, u32 __user *uaddr2,
677 int nr_wake, int nr_wake2, int op)
328{ 678{
329 union futex_key key1, key2; 679 union futex_key key1, key2;
330 struct futex_hash_bucket *bh1, *bh2; 680 struct futex_hash_bucket *hb1, *hb2;
331 struct list_head *head; 681 struct list_head *head;
332 struct futex_q *this, *next; 682 struct futex_q *this, *next;
333 int ret, op_ret, attempt = 0; 683 int ret, op_ret, attempt = 0;
@@ -342,27 +692,25 @@ retryfull:
342 if (unlikely(ret != 0)) 692 if (unlikely(ret != 0))
343 goto out; 693 goto out;
344 694
345 bh1 = hash_futex(&key1); 695 hb1 = hash_futex(&key1);
346 bh2 = hash_futex(&key2); 696 hb2 = hash_futex(&key2);
347 697
348retry: 698retry:
349 if (bh1 < bh2) 699 double_lock_hb(hb1, hb2);
350 spin_lock(&bh1->lock);
351 spin_lock(&bh2->lock);
352 if (bh1 > bh2)
353 spin_lock(&bh1->lock);
354 700
355 op_ret = futex_atomic_op_inuser(op, (int __user *)uaddr2); 701 op_ret = futex_atomic_op_inuser(op, uaddr2);
356 if (unlikely(op_ret < 0)) { 702 if (unlikely(op_ret < 0)) {
357 int dummy; 703 u32 dummy;
358 704
359 spin_unlock(&bh1->lock); 705 spin_unlock(&hb1->lock);
360 if (bh1 != bh2) 706 if (hb1 != hb2)
361 spin_unlock(&bh2->lock); 707 spin_unlock(&hb2->lock);
362 708
363#ifndef CONFIG_MMU 709#ifndef CONFIG_MMU
364 /* we don't get EFAULT from MMU faults if we don't have an MMU, 710 /*
365 * but we might get them from range checking */ 711 * we don't get EFAULT from MMU faults if we don't have an MMU,
712 * but we might get them from range checking
713 */
366 ret = op_ret; 714 ret = op_ret;
367 goto out; 715 goto out;
368#endif 716#endif
@@ -372,47 +720,34 @@ retry:
372 goto out; 720 goto out;
373 } 721 }
374 722
375 /* futex_atomic_op_inuser needs to both read and write 723 /*
724 * futex_atomic_op_inuser needs to both read and write
376 * *(int __user *)uaddr2, but we can't modify it 725 * *(int __user *)uaddr2, but we can't modify it
377 * non-atomically. Therefore, if get_user below is not 726 * non-atomically. Therefore, if get_user below is not
378 * enough, we need to handle the fault ourselves, while 727 * enough, we need to handle the fault ourselves, while
379 * still holding the mmap_sem. */ 728 * still holding the mmap_sem.
729 */
380 if (attempt++) { 730 if (attempt++) {
381 struct vm_area_struct * vma; 731 if (futex_handle_fault((unsigned long)uaddr2,
382 struct mm_struct *mm = current->mm; 732 attempt))
383
384 ret = -EFAULT;
385 if (attempt >= 2 ||
386 !(vma = find_vma(mm, uaddr2)) ||
387 vma->vm_start > uaddr2 ||
388 !(vma->vm_flags & VM_WRITE))
389 goto out;
390
391 switch (handle_mm_fault(mm, vma, uaddr2, 1)) {
392 case VM_FAULT_MINOR:
393 current->min_flt++;
394 break;
395 case VM_FAULT_MAJOR:
396 current->maj_flt++;
397 break;
398 default:
399 goto out; 733 goto out;
400 }
401 goto retry; 734 goto retry;
402 } 735 }
403 736
404 /* If we would have faulted, release mmap_sem, 737 /*
405 * fault it in and start all over again. */ 738 * If we would have faulted, release mmap_sem,
739 * fault it in and start all over again.
740 */
406 up_read(&current->mm->mmap_sem); 741 up_read(&current->mm->mmap_sem);
407 742
408 ret = get_user(dummy, (int __user *)uaddr2); 743 ret = get_user(dummy, uaddr2);
409 if (ret) 744 if (ret)
410 return ret; 745 return ret;
411 746
412 goto retryfull; 747 goto retryfull;
413 } 748 }
414 749
415 head = &bh1->chain; 750 head = &hb1->chain;
416 751
417 list_for_each_entry_safe(this, next, head, list) { 752 list_for_each_entry_safe(this, next, head, list) {
418 if (match_futex (&this->key, &key1)) { 753 if (match_futex (&this->key, &key1)) {
@@ -423,7 +758,7 @@ retry:
423 } 758 }
424 759
425 if (op_ret > 0) { 760 if (op_ret > 0) {
426 head = &bh2->chain; 761 head = &hb2->chain;
427 762
428 op_ret = 0; 763 op_ret = 0;
429 list_for_each_entry_safe(this, next, head, list) { 764 list_for_each_entry_safe(this, next, head, list) {
@@ -436,9 +771,9 @@ retry:
436 ret += op_ret; 771 ret += op_ret;
437 } 772 }
438 773
439 spin_unlock(&bh1->lock); 774 spin_unlock(&hb1->lock);
440 if (bh1 != bh2) 775 if (hb1 != hb2)
441 spin_unlock(&bh2->lock); 776 spin_unlock(&hb2->lock);
442out: 777out:
443 up_read(&current->mm->mmap_sem); 778 up_read(&current->mm->mmap_sem);
444 return ret; 779 return ret;
@@ -448,11 +783,11 @@ out:
448 * Requeue all waiters hashed on one physical page to another 783 * Requeue all waiters hashed on one physical page to another
449 * physical page. 784 * physical page.
450 */ 785 */
451static int futex_requeue(unsigned long uaddr1, unsigned long uaddr2, 786static int futex_requeue(u32 __user *uaddr1, u32 __user *uaddr2,
452 int nr_wake, int nr_requeue, int *valp) 787 int nr_wake, int nr_requeue, u32 *cmpval)
453{ 788{
454 union futex_key key1, key2; 789 union futex_key key1, key2;
455 struct futex_hash_bucket *bh1, *bh2; 790 struct futex_hash_bucket *hb1, *hb2;
456 struct list_head *head1; 791 struct list_head *head1;
457 struct futex_q *this, *next; 792 struct futex_q *this, *next;
458 int ret, drop_count = 0; 793 int ret, drop_count = 0;
@@ -467,68 +802,68 @@ static int futex_requeue(unsigned long uaddr1, unsigned long uaddr2,
467 if (unlikely(ret != 0)) 802 if (unlikely(ret != 0))
468 goto out; 803 goto out;
469 804
470 bh1 = hash_futex(&key1); 805 hb1 = hash_futex(&key1);
471 bh2 = hash_futex(&key2); 806 hb2 = hash_futex(&key2);
472 807
473 if (bh1 < bh2) 808 double_lock_hb(hb1, hb2);
474 spin_lock(&bh1->lock);
475 spin_lock(&bh2->lock);
476 if (bh1 > bh2)
477 spin_lock(&bh1->lock);
478 809
479 if (likely(valp != NULL)) { 810 if (likely(cmpval != NULL)) {
480 int curval; 811 u32 curval;
481 812
482 ret = get_futex_value_locked(&curval, (int __user *)uaddr1); 813 ret = get_futex_value_locked(&curval, uaddr1);
483 814
484 if (unlikely(ret)) { 815 if (unlikely(ret)) {
485 spin_unlock(&bh1->lock); 816 spin_unlock(&hb1->lock);
486 if (bh1 != bh2) 817 if (hb1 != hb2)
487 spin_unlock(&bh2->lock); 818 spin_unlock(&hb2->lock);
488 819
489 /* If we would have faulted, release mmap_sem, fault 820 /*
821 * If we would have faulted, release mmap_sem, fault
490 * it in and start all over again. 822 * it in and start all over again.
491 */ 823 */
492 up_read(&current->mm->mmap_sem); 824 up_read(&current->mm->mmap_sem);
493 825
494 ret = get_user(curval, (int __user *)uaddr1); 826 ret = get_user(curval, uaddr1);
495 827
496 if (!ret) 828 if (!ret)
497 goto retry; 829 goto retry;
498 830
499 return ret; 831 return ret;
500 } 832 }
501 if (curval != *valp) { 833 if (curval != *cmpval) {
502 ret = -EAGAIN; 834 ret = -EAGAIN;
503 goto out_unlock; 835 goto out_unlock;
504 } 836 }
505 } 837 }
506 838
507 head1 = &bh1->chain; 839 head1 = &hb1->chain;
508 list_for_each_entry_safe(this, next, head1, list) { 840 list_for_each_entry_safe(this, next, head1, list) {
509 if (!match_futex (&this->key, &key1)) 841 if (!match_futex (&this->key, &key1))
510 continue; 842 continue;
511 if (++ret <= nr_wake) { 843 if (++ret <= nr_wake) {
512 wake_futex(this); 844 wake_futex(this);
513 } else { 845 } else {
514 list_move_tail(&this->list, &bh2->chain); 846 /*
515 this->lock_ptr = &bh2->lock; 847 * If key1 and key2 hash to the same bucket, no need to
848 * requeue.
849 */
850 if (likely(head1 != &hb2->chain)) {
851 list_move_tail(&this->list, &hb2->chain);
852 this->lock_ptr = &hb2->lock;
853 }
516 this->key = key2; 854 this->key = key2;
517 get_key_refs(&key2); 855 get_key_refs(&key2);
518 drop_count++; 856 drop_count++;
519 857
520 if (ret - nr_wake >= nr_requeue) 858 if (ret - nr_wake >= nr_requeue)
521 break; 859 break;
522 /* Make sure to stop if key1 == key2 */
523 if (head1 == &bh2->chain && head1 != &next->list)
524 head1 = &this->list;
525 } 860 }
526 } 861 }
527 862
528out_unlock: 863out_unlock:
529 spin_unlock(&bh1->lock); 864 spin_unlock(&hb1->lock);
530 if (bh1 != bh2) 865 if (hb1 != hb2)
531 spin_unlock(&bh2->lock); 866 spin_unlock(&hb2->lock);
532 867
533 /* drop_key_refs() must be called outside the spinlocks. */ 868 /* drop_key_refs() must be called outside the spinlocks. */
534 while (--drop_count >= 0) 869 while (--drop_count >= 0)
@@ -543,7 +878,7 @@ out:
543static inline struct futex_hash_bucket * 878static inline struct futex_hash_bucket *
544queue_lock(struct futex_q *q, int fd, struct file *filp) 879queue_lock(struct futex_q *q, int fd, struct file *filp)
545{ 880{
546 struct futex_hash_bucket *bh; 881 struct futex_hash_bucket *hb;
547 882
548 q->fd = fd; 883 q->fd = fd;
549 q->filp = filp; 884 q->filp = filp;
@@ -551,23 +886,24 @@ queue_lock(struct futex_q *q, int fd, struct file *filp)
551 init_waitqueue_head(&q->waiters); 886 init_waitqueue_head(&q->waiters);
552 887
553 get_key_refs(&q->key); 888 get_key_refs(&q->key);
554 bh = hash_futex(&q->key); 889 hb = hash_futex(&q->key);
555 q->lock_ptr = &bh->lock; 890 q->lock_ptr = &hb->lock;
556 891
557 spin_lock(&bh->lock); 892 spin_lock(&hb->lock);
558 return bh; 893 return hb;
559} 894}
560 895
561static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *bh) 896static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
562{ 897{
563 list_add_tail(&q->list, &bh->chain); 898 list_add_tail(&q->list, &hb->chain);
564 spin_unlock(&bh->lock); 899 q->task = current;
900 spin_unlock(&hb->lock);
565} 901}
566 902
567static inline void 903static inline void
568queue_unlock(struct futex_q *q, struct futex_hash_bucket *bh) 904queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
569{ 905{
570 spin_unlock(&bh->lock); 906 spin_unlock(&hb->lock);
571 drop_key_refs(&q->key); 907 drop_key_refs(&q->key);
572} 908}
573 909
@@ -579,16 +915,17 @@ queue_unlock(struct futex_q *q, struct futex_hash_bucket *bh)
579/* The key must be already stored in q->key. */ 915/* The key must be already stored in q->key. */
580static void queue_me(struct futex_q *q, int fd, struct file *filp) 916static void queue_me(struct futex_q *q, int fd, struct file *filp)
581{ 917{
582 struct futex_hash_bucket *bh; 918 struct futex_hash_bucket *hb;
583 bh = queue_lock(q, fd, filp); 919
584 __queue_me(q, bh); 920 hb = queue_lock(q, fd, filp);
921 __queue_me(q, hb);
585} 922}
586 923
587/* Return 1 if we were still queued (ie. 0 means we were woken) */ 924/* Return 1 if we were still queued (ie. 0 means we were woken) */
588static int unqueue_me(struct futex_q *q) 925static int unqueue_me(struct futex_q *q)
589{ 926{
590 int ret = 0;
591 spinlock_t *lock_ptr; 927 spinlock_t *lock_ptr;
928 int ret = 0;
592 929
593 /* In the common case we don't take the spinlock, which is nice. */ 930 /* In the common case we don't take the spinlock, which is nice. */
594 retry: 931 retry:
@@ -614,6 +951,9 @@ static int unqueue_me(struct futex_q *q)
614 } 951 }
615 WARN_ON(list_empty(&q->list)); 952 WARN_ON(list_empty(&q->list));
616 list_del(&q->list); 953 list_del(&q->list);
954
955 BUG_ON(q->pi_state);
956
617 spin_unlock(lock_ptr); 957 spin_unlock(lock_ptr);
618 ret = 1; 958 ret = 1;
619 } 959 }
@@ -622,21 +962,42 @@ static int unqueue_me(struct futex_q *q)
622 return ret; 962 return ret;
623} 963}
624 964
625static int futex_wait(unsigned long uaddr, int val, unsigned long time) 965/*
966 * PI futexes can not be requeued and must remove themself from the
967 * hash bucket. The hash bucket lock is held on entry and dropped here.
968 */
969static void unqueue_me_pi(struct futex_q *q, struct futex_hash_bucket *hb)
626{ 970{
627 DECLARE_WAITQUEUE(wait, current); 971 WARN_ON(list_empty(&q->list));
628 int ret, curval; 972 list_del(&q->list);
973
974 BUG_ON(!q->pi_state);
975 free_pi_state(q->pi_state);
976 q->pi_state = NULL;
977
978 spin_unlock(&hb->lock);
979
980 drop_key_refs(&q->key);
981}
982
983static int futex_wait(u32 __user *uaddr, u32 val, unsigned long time)
984{
985 struct task_struct *curr = current;
986 DECLARE_WAITQUEUE(wait, curr);
987 struct futex_hash_bucket *hb;
629 struct futex_q q; 988 struct futex_q q;
630 struct futex_hash_bucket *bh; 989 u32 uval;
990 int ret;
631 991
992 q.pi_state = NULL;
632 retry: 993 retry:
633 down_read(&current->mm->mmap_sem); 994 down_read(&curr->mm->mmap_sem);
634 995
635 ret = get_futex_key(uaddr, &q.key); 996 ret = get_futex_key(uaddr, &q.key);
636 if (unlikely(ret != 0)) 997 if (unlikely(ret != 0))
637 goto out_release_sem; 998 goto out_release_sem;
638 999
639 bh = queue_lock(&q, -1, NULL); 1000 hb = queue_lock(&q, -1, NULL);
640 1001
641 /* 1002 /*
642 * Access the page AFTER the futex is queued. 1003 * Access the page AFTER the futex is queued.
@@ -658,37 +1019,35 @@ static int futex_wait(unsigned long uaddr, int val, unsigned long time)
658 * We hold the mmap semaphore, so the mapping cannot have changed 1019 * We hold the mmap semaphore, so the mapping cannot have changed
659 * since we looked it up in get_futex_key. 1020 * since we looked it up in get_futex_key.
660 */ 1021 */
661 1022 ret = get_futex_value_locked(&uval, uaddr);
662 ret = get_futex_value_locked(&curval, (int __user *)uaddr);
663 1023
664 if (unlikely(ret)) { 1024 if (unlikely(ret)) {
665 queue_unlock(&q, bh); 1025 queue_unlock(&q, hb);
666 1026
667 /* If we would have faulted, release mmap_sem, fault it in and 1027 /*
1028 * If we would have faulted, release mmap_sem, fault it in and
668 * start all over again. 1029 * start all over again.
669 */ 1030 */
670 up_read(&current->mm->mmap_sem); 1031 up_read(&curr->mm->mmap_sem);
671 1032
672 ret = get_user(curval, (int __user *)uaddr); 1033 ret = get_user(uval, uaddr);
673 1034
674 if (!ret) 1035 if (!ret)
675 goto retry; 1036 goto retry;
676 return ret; 1037 return ret;
677 } 1038 }
678 if (curval != val) { 1039 ret = -EWOULDBLOCK;
679 ret = -EWOULDBLOCK; 1040 if (uval != val)
680 queue_unlock(&q, bh); 1041 goto out_unlock_release_sem;
681 goto out_release_sem;
682 }
683 1042
684 /* Only actually queue if *uaddr contained val. */ 1043 /* Only actually queue if *uaddr contained val. */
685 __queue_me(&q, bh); 1044 __queue_me(&q, hb);
686 1045
687 /* 1046 /*
688 * Now the futex is queued and we have checked the data, we 1047 * Now the futex is queued and we have checked the data, we
689 * don't want to hold mmap_sem while we sleep. 1048 * don't want to hold mmap_sem while we sleep.
690 */ 1049 */
691 up_read(&current->mm->mmap_sem); 1050 up_read(&curr->mm->mmap_sem);
692 1051
693 /* 1052 /*
694 * There might have been scheduling since the queue_me(), as we 1053 * There might have been scheduling since the queue_me(), as we
@@ -720,12 +1079,421 @@ static int futex_wait(unsigned long uaddr, int val, unsigned long time)
720 return 0; 1079 return 0;
721 if (time == 0) 1080 if (time == 0)
722 return -ETIMEDOUT; 1081 return -ETIMEDOUT;
723 /* We expect signal_pending(current), but another thread may 1082 /*
724 * have handled it for us already. */ 1083 * We expect signal_pending(current), but another thread may
1084 * have handled it for us already.
1085 */
725 return -EINTR; 1086 return -EINTR;
726 1087
1088 out_unlock_release_sem:
1089 queue_unlock(&q, hb);
1090
727 out_release_sem: 1091 out_release_sem:
1092 up_read(&curr->mm->mmap_sem);
1093 return ret;
1094}
1095
1096/*
1097 * Userspace tried a 0 -> TID atomic transition of the futex value
1098 * and failed. The kernel side here does the whole locking operation:
1099 * if there are waiters then it will block, it does PI, etc. (Due to
1100 * races the kernel might see a 0 value of the futex too.)
1101 */
1102static int do_futex_lock_pi(u32 __user *uaddr, int detect, int trylock,
1103 struct hrtimer_sleeper *to)
1104{
1105 struct task_struct *curr = current;
1106 struct futex_hash_bucket *hb;
1107 u32 uval, newval, curval;
1108 struct futex_q q;
1109 int ret, attempt = 0;
1110
1111 if (refill_pi_state_cache())
1112 return -ENOMEM;
1113
1114 q.pi_state = NULL;
1115 retry:
1116 down_read(&curr->mm->mmap_sem);
1117
1118 ret = get_futex_key(uaddr, &q.key);
1119 if (unlikely(ret != 0))
1120 goto out_release_sem;
1121
1122 hb = queue_lock(&q, -1, NULL);
1123
1124 retry_locked:
1125 /*
1126 * To avoid races, we attempt to take the lock here again
1127 * (by doing a 0 -> TID atomic cmpxchg), while holding all
1128 * the locks. It will most likely not succeed.
1129 */
1130 newval = current->pid;
1131
1132 inc_preempt_count();
1133 curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval);
1134 dec_preempt_count();
1135
1136 if (unlikely(curval == -EFAULT))
1137 goto uaddr_faulted;
1138
1139 /* We own the lock already */
1140 if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) {
1141 if (!detect && 0)
1142 force_sig(SIGKILL, current);
1143 ret = -EDEADLK;
1144 goto out_unlock_release_sem;
1145 }
1146
1147 /*
1148 * Surprise - we got the lock. Just return
1149 * to userspace:
1150 */
1151 if (unlikely(!curval))
1152 goto out_unlock_release_sem;
1153
1154 uval = curval;
1155 newval = uval | FUTEX_WAITERS;
1156
1157 inc_preempt_count();
1158 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
1159 dec_preempt_count();
1160
1161 if (unlikely(curval == -EFAULT))
1162 goto uaddr_faulted;
1163 if (unlikely(curval != uval))
1164 goto retry_locked;
1165
1166 /*
1167 * We dont have the lock. Look up the PI state (or create it if
1168 * we are the first waiter):
1169 */
1170 ret = lookup_pi_state(uval, hb, &q);
1171
1172 if (unlikely(ret)) {
1173 /*
1174 * There were no waiters and the owner task lookup
1175 * failed. When the OWNER_DIED bit is set, then we
1176 * know that this is a robust futex and we actually
1177 * take the lock. This is safe as we are protected by
1178 * the hash bucket lock. We also set the waiters bit
1179 * unconditionally here, to simplify glibc handling of
1180 * multiple tasks racing to acquire the lock and
1181 * cleanup the problems which were left by the dead
1182 * owner.
1183 */
1184 if (curval & FUTEX_OWNER_DIED) {
1185 uval = newval;
1186 newval = current->pid |
1187 FUTEX_OWNER_DIED | FUTEX_WAITERS;
1188
1189 inc_preempt_count();
1190 curval = futex_atomic_cmpxchg_inatomic(uaddr,
1191 uval, newval);
1192 dec_preempt_count();
1193
1194 if (unlikely(curval == -EFAULT))
1195 goto uaddr_faulted;
1196 if (unlikely(curval != uval))
1197 goto retry_locked;
1198 ret = 0;
1199 }
1200 goto out_unlock_release_sem;
1201 }
1202
1203 /*
1204 * Only actually queue now that the atomic ops are done:
1205 */
1206 __queue_me(&q, hb);
1207
1208 /*
1209 * Now the futex is queued and we have checked the data, we
1210 * don't want to hold mmap_sem while we sleep.
1211 */
1212 up_read(&curr->mm->mmap_sem);
1213
1214 WARN_ON(!q.pi_state);
1215 /*
1216 * Block on the PI mutex:
1217 */
1218 if (!trylock)
1219 ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
1220 else {
1221 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
1222 /* Fixup the trylock return value: */
1223 ret = ret ? 0 : -EWOULDBLOCK;
1224 }
1225
1226 down_read(&curr->mm->mmap_sem);
1227 spin_lock(q.lock_ptr);
1228
1229 /*
1230 * Got the lock. We might not be the anticipated owner if we
1231 * did a lock-steal - fix up the PI-state in that case.
1232 */
1233 if (!ret && q.pi_state->owner != curr) {
1234 u32 newtid = current->pid | FUTEX_WAITERS;
1235
1236 /* Owner died? */
1237 if (q.pi_state->owner != NULL) {
1238 spin_lock_irq(&q.pi_state->owner->pi_lock);
1239 list_del_init(&q.pi_state->list);
1240 spin_unlock_irq(&q.pi_state->owner->pi_lock);
1241 } else
1242 newtid |= FUTEX_OWNER_DIED;
1243
1244 q.pi_state->owner = current;
1245
1246 spin_lock_irq(&current->pi_lock);
1247 list_add(&q.pi_state->list, &current->pi_state_list);
1248 spin_unlock_irq(&current->pi_lock);
1249
1250 /* Unqueue and drop the lock */
1251 unqueue_me_pi(&q, hb);
1252 up_read(&curr->mm->mmap_sem);
1253 /*
1254 * We own it, so we have to replace the pending owner
1255 * TID. This must be atomic as we have preserve the
1256 * owner died bit here.
1257 */
1258 ret = get_user(uval, uaddr);
1259 while (!ret) {
1260 newval = (uval & FUTEX_OWNER_DIED) | newtid;
1261 curval = futex_atomic_cmpxchg_inatomic(uaddr,
1262 uval, newval);
1263 if (curval == -EFAULT)
1264 ret = -EFAULT;
1265 if (curval == uval)
1266 break;
1267 uval = curval;
1268 }
1269 } else {
1270 /*
1271 * Catch the rare case, where the lock was released
1272 * when we were on the way back before we locked
1273 * the hash bucket.
1274 */
1275 if (ret && q.pi_state->owner == curr) {
1276 if (rt_mutex_trylock(&q.pi_state->pi_mutex))
1277 ret = 0;
1278 }
1279 /* Unqueue and drop the lock */
1280 unqueue_me_pi(&q, hb);
1281 up_read(&curr->mm->mmap_sem);
1282 }
1283
1284 if (!detect && ret == -EDEADLK && 0)
1285 force_sig(SIGKILL, current);
1286
1287 return ret;
1288
1289 out_unlock_release_sem:
1290 queue_unlock(&q, hb);
1291
1292 out_release_sem:
1293 up_read(&curr->mm->mmap_sem);
1294 return ret;
1295
1296 uaddr_faulted:
1297 /*
1298 * We have to r/w *(int __user *)uaddr, but we can't modify it
1299 * non-atomically. Therefore, if get_user below is not
1300 * enough, we need to handle the fault ourselves, while
1301 * still holding the mmap_sem.
1302 */
1303 if (attempt++) {
1304 if (futex_handle_fault((unsigned long)uaddr, attempt))
1305 goto out_unlock_release_sem;
1306
1307 goto retry_locked;
1308 }
1309
1310 queue_unlock(&q, hb);
1311 up_read(&curr->mm->mmap_sem);
1312
1313 ret = get_user(uval, uaddr);
1314 if (!ret && (uval != -EFAULT))
1315 goto retry;
1316
1317 return ret;
1318}
1319
1320/*
1321 * Restart handler
1322 */
1323static long futex_lock_pi_restart(struct restart_block *restart)
1324{
1325 struct hrtimer_sleeper timeout, *to = NULL;
1326 int ret;
1327
1328 restart->fn = do_no_restart_syscall;
1329
1330 if (restart->arg2 || restart->arg3) {
1331 to = &timeout;
1332 hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_ABS);
1333 hrtimer_init_sleeper(to, current);
1334 to->timer.expires.tv64 = ((u64)restart->arg1 << 32) |
1335 (u64) restart->arg0;
1336 }
1337
1338 pr_debug("lock_pi restart: %p, %d (%d)\n",
1339 (u32 __user *)restart->arg0, current->pid);
1340
1341 ret = do_futex_lock_pi((u32 __user *)restart->arg0, restart->arg1,
1342 0, to);
1343
1344 if (ret != -EINTR)
1345 return ret;
1346
1347 restart->fn = futex_lock_pi_restart;
1348
1349 /* The other values are filled in */
1350 return -ERESTART_RESTARTBLOCK;
1351}
1352
1353/*
1354 * Called from the syscall entry below.
1355 */
1356static int futex_lock_pi(u32 __user *uaddr, int detect, unsigned long sec,
1357 long nsec, int trylock)
1358{
1359 struct hrtimer_sleeper timeout, *to = NULL;
1360 struct restart_block *restart;
1361 int ret;
1362
1363 if (sec != MAX_SCHEDULE_TIMEOUT) {
1364 to = &timeout;
1365 hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_ABS);
1366 hrtimer_init_sleeper(to, current);
1367 to->timer.expires = ktime_set(sec, nsec);
1368 }
1369
1370 ret = do_futex_lock_pi(uaddr, detect, trylock, to);
1371
1372 if (ret != -EINTR)
1373 return ret;
1374
1375 pr_debug("lock_pi interrupted: %p, %d (%d)\n", uaddr, current->pid);
1376
1377 restart = &current_thread_info()->restart_block;
1378 restart->fn = futex_lock_pi_restart;
1379 restart->arg0 = (unsigned long) uaddr;
1380 restart->arg1 = detect;
1381 if (to) {
1382 restart->arg2 = to->timer.expires.tv64 & 0xFFFFFFFF;
1383 restart->arg3 = to->timer.expires.tv64 >> 32;
1384 } else
1385 restart->arg2 = restart->arg3 = 0;
1386
1387 return -ERESTART_RESTARTBLOCK;
1388}
1389
1390/*
1391 * Userspace attempted a TID -> 0 atomic transition, and failed.
1392 * This is the in-kernel slowpath: we look up the PI state (if any),
1393 * and do the rt-mutex unlock.
1394 */
1395static int futex_unlock_pi(u32 __user *uaddr)
1396{
1397 struct futex_hash_bucket *hb;
1398 struct futex_q *this, *next;
1399 u32 uval;
1400 struct list_head *head;
1401 union futex_key key;
1402 int ret, attempt = 0;
1403
1404retry:
1405 if (get_user(uval, uaddr))
1406 return -EFAULT;
1407 /*
1408 * We release only a lock we actually own:
1409 */
1410 if ((uval & FUTEX_TID_MASK) != current->pid)
1411 return -EPERM;
1412 /*
1413 * First take all the futex related locks:
1414 */
1415 down_read(&current->mm->mmap_sem);
1416
1417 ret = get_futex_key(uaddr, &key);
1418 if (unlikely(ret != 0))
1419 goto out;
1420
1421 hb = hash_futex(&key);
1422 spin_lock(&hb->lock);
1423
1424retry_locked:
1425 /*
1426 * To avoid races, try to do the TID -> 0 atomic transition
1427 * again. If it succeeds then we can return without waking
1428 * anyone else up:
1429 */
1430 inc_preempt_count();
1431 uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0);
1432 dec_preempt_count();
1433
1434 if (unlikely(uval == -EFAULT))
1435 goto pi_faulted;
1436 /*
1437 * Rare case: we managed to release the lock atomically,
1438 * no need to wake anyone else up:
1439 */
1440 if (unlikely(uval == current->pid))
1441 goto out_unlock;
1442
1443 /*
1444 * Ok, other tasks may need to be woken up - check waiters
1445 * and do the wakeup if necessary:
1446 */
1447 head = &hb->chain;
1448
1449 list_for_each_entry_safe(this, next, head, list) {
1450 if (!match_futex (&this->key, &key))
1451 continue;
1452 ret = wake_futex_pi(uaddr, uval, this);
1453 /*
1454 * The atomic access to the futex value
1455 * generated a pagefault, so retry the
1456 * user-access and the wakeup:
1457 */
1458 if (ret == -EFAULT)
1459 goto pi_faulted;
1460 goto out_unlock;
1461 }
1462 /*
1463 * No waiters - kernel unlocks the futex:
1464 */
1465 ret = unlock_futex_pi(uaddr, uval);
1466 if (ret == -EFAULT)
1467 goto pi_faulted;
1468
1469out_unlock:
1470 spin_unlock(&hb->lock);
1471out:
728 up_read(&current->mm->mmap_sem); 1472 up_read(&current->mm->mmap_sem);
1473
1474 return ret;
1475
1476pi_faulted:
1477 /*
1478 * We have to r/w *(int __user *)uaddr, but we can't modify it
1479 * non-atomically. Therefore, if get_user below is not
1480 * enough, we need to handle the fault ourselves, while
1481 * still holding the mmap_sem.
1482 */
1483 if (attempt++) {
1484 if (futex_handle_fault((unsigned long)uaddr, attempt))
1485 goto out_unlock;
1486
1487 goto retry_locked;
1488 }
1489
1490 spin_unlock(&hb->lock);
1491 up_read(&current->mm->mmap_sem);
1492
1493 ret = get_user(uval, uaddr);
1494 if (!ret && (uval != -EFAULT))
1495 goto retry;
1496
729 return ret; 1497 return ret;
730} 1498}
731 1499
@@ -735,6 +1503,7 @@ static int futex_close(struct inode *inode, struct file *filp)
735 1503
736 unqueue_me(q); 1504 unqueue_me(q);
737 kfree(q); 1505 kfree(q);
1506
738 return 0; 1507 return 0;
739} 1508}
740 1509
@@ -766,7 +1535,7 @@ static struct file_operations futex_fops = {
766 * Signal allows caller to avoid the race which would occur if they 1535 * Signal allows caller to avoid the race which would occur if they
767 * set the sigio stuff up afterwards. 1536 * set the sigio stuff up afterwards.
768 */ 1537 */
769static int futex_fd(unsigned long uaddr, int signal) 1538static int futex_fd(u32 __user *uaddr, int signal)
770{ 1539{
771 struct futex_q *q; 1540 struct futex_q *q;
772 struct file *filp; 1541 struct file *filp;
@@ -803,6 +1572,7 @@ static int futex_fd(unsigned long uaddr, int signal)
803 err = -ENOMEM; 1572 err = -ENOMEM;
804 goto error; 1573 goto error;
805 } 1574 }
1575 q->pi_state = NULL;
806 1576
807 down_read(&current->mm->mmap_sem); 1577 down_read(&current->mm->mmap_sem);
808 err = get_futex_key(uaddr, &q->key); 1578 err = get_futex_key(uaddr, &q->key);
@@ -840,7 +1610,7 @@ error:
840 * Implementation: user-space maintains a per-thread list of locks it 1610 * Implementation: user-space maintains a per-thread list of locks it
841 * is holding. Upon do_exit(), the kernel carefully walks this list, 1611 * is holding. Upon do_exit(), the kernel carefully walks this list,
842 * and marks all locks that are owned by this thread with the 1612 * 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 1613 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
844 * always manipulated with the lock held, so the list is private and 1614 * always manipulated with the lock held, so the list is private and
845 * per-thread. Userspace also maintains a per-thread 'list_op_pending' 1615 * 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 1616 * field, to allow the kernel to clean up if the thread dies after
@@ -915,7 +1685,7 @@ err_unlock:
915 */ 1685 */
916int handle_futex_death(u32 __user *uaddr, struct task_struct *curr) 1686int handle_futex_death(u32 __user *uaddr, struct task_struct *curr)
917{ 1687{
918 u32 uval; 1688 u32 uval, nval;
919 1689
920retry: 1690retry:
921 if (get_user(uval, uaddr)) 1691 if (get_user(uval, uaddr))
@@ -932,12 +1702,16 @@ retry:
932 * thread-death.) The rest of the cleanup is done in 1702 * thread-death.) The rest of the cleanup is done in
933 * userspace. 1703 * userspace.
934 */ 1704 */
935 if (futex_atomic_cmpxchg_inatomic(uaddr, uval, 1705 nval = futex_atomic_cmpxchg_inatomic(uaddr, uval,
936 uval | FUTEX_OWNER_DIED) != uval) 1706 uval | FUTEX_OWNER_DIED);
1707 if (nval == -EFAULT)
1708 return -1;
1709
1710 if (nval != uval)
937 goto retry; 1711 goto retry;
938 1712
939 if (uval & FUTEX_WAITERS) 1713 if (uval & FUTEX_WAITERS)
940 futex_wake((unsigned long)uaddr, 1); 1714 futex_wake(uaddr, 1);
941 } 1715 }
942 return 0; 1716 return 0;
943} 1717}
@@ -978,7 +1752,7 @@ void exit_robust_list(struct task_struct *curr)
978 while (entry != &head->list) { 1752 while (entry != &head->list) {
979 /* 1753 /*
980 * A pending lock might already be on the list, so 1754 * A pending lock might already be on the list, so
981 * dont process it twice: 1755 * don't process it twice:
982 */ 1756 */
983 if (entry != pending) 1757 if (entry != pending)
984 if (handle_futex_death((void *)entry + futex_offset, 1758 if (handle_futex_death((void *)entry + futex_offset,
@@ -999,8 +1773,8 @@ void exit_robust_list(struct task_struct *curr)
999 } 1773 }
1000} 1774}
1001 1775
1002long do_futex(unsigned long uaddr, int op, int val, unsigned long timeout, 1776long do_futex(u32 __user *uaddr, int op, u32 val, unsigned long timeout,
1003 unsigned long uaddr2, int val2, int val3) 1777 u32 __user *uaddr2, u32 val2, u32 val3)
1004{ 1778{
1005 int ret; 1779 int ret;
1006 1780
@@ -1024,6 +1798,15 @@ long do_futex(unsigned long uaddr, int op, int val, unsigned long timeout,
1024 case FUTEX_WAKE_OP: 1798 case FUTEX_WAKE_OP:
1025 ret = futex_wake_op(uaddr, uaddr2, val, val2, val3); 1799 ret = futex_wake_op(uaddr, uaddr2, val, val2, val3);
1026 break; 1800 break;
1801 case FUTEX_LOCK_PI:
1802 ret = futex_lock_pi(uaddr, val, timeout, val2, 0);
1803 break;
1804 case FUTEX_UNLOCK_PI:
1805 ret = futex_unlock_pi(uaddr);
1806 break;
1807 case FUTEX_TRYLOCK_PI:
1808 ret = futex_lock_pi(uaddr, 0, timeout, val2, 1);
1809 break;
1027 default: 1810 default:
1028 ret = -ENOSYS; 1811 ret = -ENOSYS;
1029 } 1812 }
@@ -1031,36 +1814,40 @@ long do_futex(unsigned long uaddr, int op, int val, unsigned long timeout,
1031} 1814}
1032 1815
1033 1816
1034asmlinkage long sys_futex(u32 __user *uaddr, int op, int val, 1817asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
1035 struct timespec __user *utime, u32 __user *uaddr2, 1818 struct timespec __user *utime, u32 __user *uaddr2,
1036 int val3) 1819 u32 val3)
1037{ 1820{
1038 struct timespec t; 1821 struct timespec t;
1039 unsigned long timeout = MAX_SCHEDULE_TIMEOUT; 1822 unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
1040 int val2 = 0; 1823 u32 val2 = 0;
1041 1824
1042 if (utime && (op == FUTEX_WAIT)) { 1825 if (utime && (op == FUTEX_WAIT || op == FUTEX_LOCK_PI)) {
1043 if (copy_from_user(&t, utime, sizeof(t)) != 0) 1826 if (copy_from_user(&t, utime, sizeof(t)) != 0)
1044 return -EFAULT; 1827 return -EFAULT;
1045 if (!timespec_valid(&t)) 1828 if (!timespec_valid(&t))
1046 return -EINVAL; 1829 return -EINVAL;
1047 timeout = timespec_to_jiffies(&t) + 1; 1830 if (op == FUTEX_WAIT)
1831 timeout = timespec_to_jiffies(&t) + 1;
1832 else {
1833 timeout = t.tv_sec;
1834 val2 = t.tv_nsec;
1835 }
1048 } 1836 }
1049 /* 1837 /*
1050 * requeue parameter in 'utime' if op == FUTEX_REQUEUE. 1838 * requeue parameter in 'utime' if op == FUTEX_REQUEUE.
1051 */ 1839 */
1052 if (op >= FUTEX_REQUEUE) 1840 if (op == FUTEX_REQUEUE || op == FUTEX_CMP_REQUEUE)
1053 val2 = (int) (unsigned long) utime; 1841 val2 = (u32) (unsigned long) utime;
1054 1842
1055 return do_futex((unsigned long)uaddr, op, val, timeout, 1843 return do_futex(uaddr, op, val, timeout, uaddr2, val2, val3);
1056 (unsigned long)uaddr2, val2, val3);
1057} 1844}
1058 1845
1059static struct super_block * 1846static int futexfs_get_sb(struct file_system_type *fs_type,
1060futexfs_get_sb(struct file_system_type *fs_type, 1847 int flags, const char *dev_name, void *data,
1061 int flags, const char *dev_name, void *data) 1848 struct vfsmount *mnt)
1062{ 1849{
1063 return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA); 1850 return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt);
1064} 1851}
1065 1852
1066static struct file_system_type futex_fs_type = { 1853static struct file_system_type futex_fs_type = {