aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/futex.c
diff options
context:
space:
mode:
authorPierre Ossman <drzeus@drzeus.cx>2008-12-31 13:56:05 -0500
committerPierre Ossman <drzeus@drzeus.cx>2008-12-31 13:56:05 -0500
commit418f19ea17a99421b22a64e101e14b6a16bed66d (patch)
tree7c21fcc368c63f1f9907deac6d16b30bd371792d /kernel/futex.c
parent98444d3dd975653a4a970ecc0dfc30918da92f60 (diff)
parentf6e10b865c3ea56bdaa8c6ecfee313b997900dbb (diff)
Merge branch 'master' of ../mmc
Diffstat (limited to 'kernel/futex.c')
-rw-r--r--kernel/futex.c371
1 files changed, 151 insertions, 220 deletions
diff --git a/kernel/futex.c b/kernel/futex.c
index 8af10027514b..7c6cbabe52b3 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -92,11 +92,12 @@ struct futex_pi_state {
92 * A futex_q has a woken state, just like tasks have TASK_RUNNING. 92 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
93 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0. 93 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
94 * The order of wakup is always to make the first condition true, then 94 * The order of wakup is always to make the first condition true, then
95 * wake up q->waiters, then make the second condition true. 95 * wake up q->waiter, then make the second condition true.
96 */ 96 */
97struct futex_q { 97struct futex_q {
98 struct plist_node list; 98 struct plist_node list;
99 wait_queue_head_t waiters; 99 /* There can only be a single waiter */
100 wait_queue_head_t waiter;
100 101
101 /* Which hash list lock to use: */ 102 /* Which hash list lock to use: */
102 spinlock_t *lock_ptr; 103 spinlock_t *lock_ptr;
@@ -123,24 +124,6 @@ struct futex_hash_bucket {
123static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS]; 124static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
124 125
125/* 126/*
126 * Take mm->mmap_sem, when futex is shared
127 */
128static inline void futex_lock_mm(struct rw_semaphore *fshared)
129{
130 if (fshared)
131 down_read(fshared);
132}
133
134/*
135 * Release mm->mmap_sem, when the futex is shared
136 */
137static inline void futex_unlock_mm(struct rw_semaphore *fshared)
138{
139 if (fshared)
140 up_read(fshared);
141}
142
143/*
144 * We hash on the keys returned from get_futex_key (see below). 127 * We hash on the keys returned from get_futex_key (see below).
145 */ 128 */
146static struct futex_hash_bucket *hash_futex(union futex_key *key) 129static struct futex_hash_bucket *hash_futex(union futex_key *key)
@@ -161,6 +144,45 @@ static inline int match_futex(union futex_key *key1, union futex_key *key2)
161 && key1->both.offset == key2->both.offset); 144 && key1->both.offset == key2->both.offset);
162} 145}
163 146
147/*
148 * Take a reference to the resource addressed by a key.
149 * Can be called while holding spinlocks.
150 *
151 */
152static void get_futex_key_refs(union futex_key *key)
153{
154 if (!key->both.ptr)
155 return;
156
157 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
158 case FUT_OFF_INODE:
159 atomic_inc(&key->shared.inode->i_count);
160 break;
161 case FUT_OFF_MMSHARED:
162 atomic_inc(&key->private.mm->mm_count);
163 break;
164 }
165}
166
167/*
168 * Drop a reference to the resource addressed by a key.
169 * The hash bucket spinlock must not be held.
170 */
171static void drop_futex_key_refs(union futex_key *key)
172{
173 if (!key->both.ptr)
174 return;
175
176 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
177 case FUT_OFF_INODE:
178 iput(key->shared.inode);
179 break;
180 case FUT_OFF_MMSHARED:
181 mmdrop(key->private.mm);
182 break;
183 }
184}
185
164/** 186/**
165 * get_futex_key - Get parameters which are the keys for a futex. 187 * get_futex_key - Get parameters which are the keys for a futex.
166 * @uaddr: virtual address of the futex 188 * @uaddr: virtual address of the futex
@@ -179,12 +201,10 @@ static inline int match_futex(union futex_key *key1, union futex_key *key2)
179 * For other futexes, it points to &current->mm->mmap_sem and 201 * For other futexes, it points to &current->mm->mmap_sem and
180 * caller must have taken the reader lock. but NOT any spinlocks. 202 * caller must have taken the reader lock. but NOT any spinlocks.
181 */ 203 */
182static int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared, 204static int get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key)
183 union futex_key *key)
184{ 205{
185 unsigned long address = (unsigned long)uaddr; 206 unsigned long address = (unsigned long)uaddr;
186 struct mm_struct *mm = current->mm; 207 struct mm_struct *mm = current->mm;
187 struct vm_area_struct *vma;
188 struct page *page; 208 struct page *page;
189 int err; 209 int err;
190 210
@@ -208,100 +228,50 @@ static int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared,
208 return -EFAULT; 228 return -EFAULT;
209 key->private.mm = mm; 229 key->private.mm = mm;
210 key->private.address = address; 230 key->private.address = address;
231 get_futex_key_refs(key);
211 return 0; 232 return 0;
212 } 233 }
213 /*
214 * The futex is hashed differently depending on whether
215 * it's in a shared or private mapping. So check vma first.
216 */
217 vma = find_extend_vma(mm, address);
218 if (unlikely(!vma))
219 return -EFAULT;
220 234
221 /* 235again:
222 * Permissions. 236 err = get_user_pages_fast(address, 1, 0, &page);
223 */ 237 if (err < 0)
224 if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ)) 238 return err;
225 return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES; 239
240 lock_page(page);
241 if (!page->mapping) {
242 unlock_page(page);
243 put_page(page);
244 goto again;
245 }
226 246
227 /* 247 /*
228 * Private mappings are handled in a simple way. 248 * Private mappings are handled in a simple way.
229 * 249 *
230 * NOTE: When userspace waits on a MAP_SHARED mapping, even if 250 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
231 * it's a read-only handle, it's expected that futexes attach to 251 * it's a read-only handle, it's expected that futexes attach to
232 * the object not the particular process. Therefore we use 252 * the object not the particular process.
233 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
234 * mappings of _writable_ handles.
235 */ 253 */
236 if (likely(!(vma->vm_flags & VM_MAYSHARE))) { 254 if (PageAnon(page)) {
237 key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */ 255 key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
238 key->private.mm = mm; 256 key->private.mm = mm;
239 key->private.address = address; 257 key->private.address = address;
240 return 0; 258 } else {
259 key->both.offset |= FUT_OFF_INODE; /* inode-based key */
260 key->shared.inode = page->mapping->host;
261 key->shared.pgoff = page->index;
241 } 262 }
242 263
243 /* 264 get_futex_key_refs(key);
244 * Linear file mappings are also simple.
245 */
246 key->shared.inode = vma->vm_file->f_path.dentry->d_inode;
247 key->both.offset |= FUT_OFF_INODE; /* inode-based key. */
248 if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
249 key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
250 + vma->vm_pgoff);
251 return 0;
252 }
253 265
254 /* 266 unlock_page(page);
255 * We could walk the page table to read the non-linear 267 put_page(page);
256 * pte, and get the page index without fetching the page 268 return 0;
257 * from swap. But that's a lot of code to duplicate here
258 * for a rare case, so we simply fetch the page.
259 */
260 err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
261 if (err >= 0) {
262 key->shared.pgoff =
263 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
264 put_page(page);
265 return 0;
266 }
267 return err;
268}
269
270/*
271 * Take a reference to the resource addressed by a key.
272 * Can be called while holding spinlocks.
273 *
274 */
275static void get_futex_key_refs(union futex_key *key)
276{
277 if (key->both.ptr == NULL)
278 return;
279 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
280 case FUT_OFF_INODE:
281 atomic_inc(&key->shared.inode->i_count);
282 break;
283 case FUT_OFF_MMSHARED:
284 atomic_inc(&key->private.mm->mm_count);
285 break;
286 }
287} 269}
288 270
289/* 271static inline
290 * Drop a reference to the resource addressed by a key. 272void put_futex_key(int fshared, union futex_key *key)
291 * The hash bucket spinlock must not be held.
292 */
293static void drop_futex_key_refs(union futex_key *key)
294{ 273{
295 if (!key->both.ptr) 274 drop_futex_key_refs(key);
296 return;
297 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
298 case FUT_OFF_INODE:
299 iput(key->shared.inode);
300 break;
301 case FUT_OFF_MMSHARED:
302 mmdrop(key->private.mm);
303 break;
304 }
305} 275}
306 276
307static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval) 277static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval)
@@ -328,10 +298,8 @@ static int get_futex_value_locked(u32 *dest, u32 __user *from)
328 298
329/* 299/*
330 * Fault handling. 300 * Fault handling.
331 * if fshared is non NULL, current->mm->mmap_sem is already held
332 */ 301 */
333static int futex_handle_fault(unsigned long address, 302static int futex_handle_fault(unsigned long address, int attempt)
334 struct rw_semaphore *fshared, int attempt)
335{ 303{
336 struct vm_area_struct * vma; 304 struct vm_area_struct * vma;
337 struct mm_struct *mm = current->mm; 305 struct mm_struct *mm = current->mm;
@@ -340,8 +308,7 @@ static int futex_handle_fault(unsigned long address,
340 if (attempt > 2) 308 if (attempt > 2)
341 return ret; 309 return ret;
342 310
343 if (!fshared) 311 down_read(&mm->mmap_sem);
344 down_read(&mm->mmap_sem);
345 vma = find_vma(mm, address); 312 vma = find_vma(mm, address);
346 if (vma && address >= vma->vm_start && 313 if (vma && address >= vma->vm_start &&
347 (vma->vm_flags & VM_WRITE)) { 314 (vma->vm_flags & VM_WRITE)) {
@@ -361,8 +328,7 @@ static int futex_handle_fault(unsigned long address,
361 current->min_flt++; 328 current->min_flt++;
362 } 329 }
363 } 330 }
364 if (!fshared) 331 up_read(&mm->mmap_sem);
365 up_read(&mm->mmap_sem);
366 return ret; 332 return ret;
367} 333}
368 334
@@ -385,6 +351,7 @@ static int refill_pi_state_cache(void)
385 /* pi_mutex gets initialized later */ 351 /* pi_mutex gets initialized later */
386 pi_state->owner = NULL; 352 pi_state->owner = NULL;
387 atomic_set(&pi_state->refcount, 1); 353 atomic_set(&pi_state->refcount, 1);
354 pi_state->key = FUTEX_KEY_INIT;
388 355
389 current->pi_state_cache = pi_state; 356 current->pi_state_cache = pi_state;
390 357
@@ -439,13 +406,20 @@ static void free_pi_state(struct futex_pi_state *pi_state)
439static struct task_struct * futex_find_get_task(pid_t pid) 406static struct task_struct * futex_find_get_task(pid_t pid)
440{ 407{
441 struct task_struct *p; 408 struct task_struct *p;
409 const struct cred *cred = current_cred(), *pcred;
442 410
443 rcu_read_lock(); 411 rcu_read_lock();
444 p = find_task_by_vpid(pid); 412 p = find_task_by_vpid(pid);
445 if (!p || ((current->euid != p->euid) && (current->euid != p->uid))) 413 if (!p) {
446 p = ERR_PTR(-ESRCH); 414 p = ERR_PTR(-ESRCH);
447 else 415 } else {
448 get_task_struct(p); 416 pcred = __task_cred(p);
417 if (cred->euid != pcred->euid &&
418 cred->euid != pcred->uid)
419 p = ERR_PTR(-ESRCH);
420 else
421 get_task_struct(p);
422 }
449 423
450 rcu_read_unlock(); 424 rcu_read_unlock();
451 425
@@ -462,7 +436,7 @@ void exit_pi_state_list(struct task_struct *curr)
462 struct list_head *next, *head = &curr->pi_state_list; 436 struct list_head *next, *head = &curr->pi_state_list;
463 struct futex_pi_state *pi_state; 437 struct futex_pi_state *pi_state;
464 struct futex_hash_bucket *hb; 438 struct futex_hash_bucket *hb;
465 union futex_key key; 439 union futex_key key = FUTEX_KEY_INIT;
466 440
467 if (!futex_cmpxchg_enabled) 441 if (!futex_cmpxchg_enabled)
468 return; 442 return;
@@ -607,7 +581,7 @@ static void wake_futex(struct futex_q *q)
607 * The lock in wake_up_all() is a crucial memory barrier after the 581 * The lock in wake_up_all() is a crucial memory barrier after the
608 * plist_del() and also before assigning to q->lock_ptr. 582 * plist_del() and also before assigning to q->lock_ptr.
609 */ 583 */
610 wake_up_all(&q->waiters); 584 wake_up(&q->waiter);
611 /* 585 /*
612 * The waiting task can free the futex_q as soon as this is written, 586 * The waiting task can free the futex_q as soon as this is written,
613 * without taking any locks. This must come last. 587 * without taking any locks. This must come last.
@@ -719,20 +693,17 @@ double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
719 * Wake up all waiters hashed on the physical page that is mapped 693 * Wake up all waiters hashed on the physical page that is mapped
720 * to this virtual address: 694 * to this virtual address:
721 */ 695 */
722static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared, 696static int futex_wake(u32 __user *uaddr, int fshared, int nr_wake, u32 bitset)
723 int nr_wake, u32 bitset)
724{ 697{
725 struct futex_hash_bucket *hb; 698 struct futex_hash_bucket *hb;
726 struct futex_q *this, *next; 699 struct futex_q *this, *next;
727 struct plist_head *head; 700 struct plist_head *head;
728 union futex_key key; 701 union futex_key key = FUTEX_KEY_INIT;
729 int ret; 702 int ret;
730 703
731 if (!bitset) 704 if (!bitset)
732 return -EINVAL; 705 return -EINVAL;
733 706
734 futex_lock_mm(fshared);
735
736 ret = get_futex_key(uaddr, fshared, &key); 707 ret = get_futex_key(uaddr, fshared, &key);
737 if (unlikely(ret != 0)) 708 if (unlikely(ret != 0))
738 goto out; 709 goto out;
@@ -760,7 +731,7 @@ static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
760 731
761 spin_unlock(&hb->lock); 732 spin_unlock(&hb->lock);
762out: 733out:
763 futex_unlock_mm(fshared); 734 put_futex_key(fshared, &key);
764 return ret; 735 return ret;
765} 736}
766 737
@@ -769,19 +740,16 @@ out:
769 * to this virtual address: 740 * to this virtual address:
770 */ 741 */
771static int 742static int
772futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared, 743futex_wake_op(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
773 u32 __user *uaddr2,
774 int nr_wake, int nr_wake2, int op) 744 int nr_wake, int nr_wake2, int op)
775{ 745{
776 union futex_key key1, key2; 746 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
777 struct futex_hash_bucket *hb1, *hb2; 747 struct futex_hash_bucket *hb1, *hb2;
778 struct plist_head *head; 748 struct plist_head *head;
779 struct futex_q *this, *next; 749 struct futex_q *this, *next;
780 int ret, op_ret, attempt = 0; 750 int ret, op_ret, attempt = 0;
781 751
782retryfull: 752retryfull:
783 futex_lock_mm(fshared);
784
785 ret = get_futex_key(uaddr1, fshared, &key1); 753 ret = get_futex_key(uaddr1, fshared, &key1);
786 if (unlikely(ret != 0)) 754 if (unlikely(ret != 0))
787 goto out; 755 goto out;
@@ -826,18 +794,12 @@ retry:
826 */ 794 */
827 if (attempt++) { 795 if (attempt++) {
828 ret = futex_handle_fault((unsigned long)uaddr2, 796 ret = futex_handle_fault((unsigned long)uaddr2,
829 fshared, attempt); 797 attempt);
830 if (ret) 798 if (ret)
831 goto out; 799 goto out;
832 goto retry; 800 goto retry;
833 } 801 }
834 802
835 /*
836 * If we would have faulted, release mmap_sem,
837 * fault it in and start all over again.
838 */
839 futex_unlock_mm(fshared);
840
841 ret = get_user(dummy, uaddr2); 803 ret = get_user(dummy, uaddr2);
842 if (ret) 804 if (ret)
843 return ret; 805 return ret;
@@ -873,7 +835,8 @@ retry:
873 if (hb1 != hb2) 835 if (hb1 != hb2)
874 spin_unlock(&hb2->lock); 836 spin_unlock(&hb2->lock);
875out: 837out:
876 futex_unlock_mm(fshared); 838 put_futex_key(fshared, &key2);
839 put_futex_key(fshared, &key1);
877 840
878 return ret; 841 return ret;
879} 842}
@@ -882,19 +845,16 @@ out:
882 * Requeue all waiters hashed on one physical page to another 845 * Requeue all waiters hashed on one physical page to another
883 * physical page. 846 * physical page.
884 */ 847 */
885static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared, 848static int futex_requeue(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
886 u32 __user *uaddr2,
887 int nr_wake, int nr_requeue, u32 *cmpval) 849 int nr_wake, int nr_requeue, u32 *cmpval)
888{ 850{
889 union futex_key key1, key2; 851 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
890 struct futex_hash_bucket *hb1, *hb2; 852 struct futex_hash_bucket *hb1, *hb2;
891 struct plist_head *head1; 853 struct plist_head *head1;
892 struct futex_q *this, *next; 854 struct futex_q *this, *next;
893 int ret, drop_count = 0; 855 int ret, drop_count = 0;
894 856
895 retry: 857 retry:
896 futex_lock_mm(fshared);
897
898 ret = get_futex_key(uaddr1, fshared, &key1); 858 ret = get_futex_key(uaddr1, fshared, &key1);
899 if (unlikely(ret != 0)) 859 if (unlikely(ret != 0))
900 goto out; 860 goto out;
@@ -917,12 +877,6 @@ static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
917 if (hb1 != hb2) 877 if (hb1 != hb2)
918 spin_unlock(&hb2->lock); 878 spin_unlock(&hb2->lock);
919 879
920 /*
921 * If we would have faulted, release mmap_sem, fault
922 * it in and start all over again.
923 */
924 futex_unlock_mm(fshared);
925
926 ret = get_user(curval, uaddr1); 880 ret = get_user(curval, uaddr1);
927 881
928 if (!ret) 882 if (!ret)
@@ -974,7 +928,8 @@ out_unlock:
974 drop_futex_key_refs(&key1); 928 drop_futex_key_refs(&key1);
975 929
976out: 930out:
977 futex_unlock_mm(fshared); 931 put_futex_key(fshared, &key2);
932 put_futex_key(fshared, &key1);
978 return ret; 933 return ret;
979} 934}
980 935
@@ -983,7 +938,7 @@ static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
983{ 938{
984 struct futex_hash_bucket *hb; 939 struct futex_hash_bucket *hb;
985 940
986 init_waitqueue_head(&q->waiters); 941 init_waitqueue_head(&q->waiter);
987 942
988 get_futex_key_refs(&q->key); 943 get_futex_key_refs(&q->key);
989 hb = hash_futex(&q->key); 944 hb = hash_futex(&q->key);
@@ -1096,8 +1051,7 @@ static void unqueue_me_pi(struct futex_q *q)
1096 * private futexes. 1051 * private futexes.
1097 */ 1052 */
1098static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, 1053static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
1099 struct task_struct *newowner, 1054 struct task_struct *newowner, int fshared)
1100 struct rw_semaphore *fshared)
1101{ 1055{
1102 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS; 1056 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
1103 struct futex_pi_state *pi_state = q->pi_state; 1057 struct futex_pi_state *pi_state = q->pi_state;
@@ -1176,7 +1130,7 @@ retry:
1176handle_fault: 1130handle_fault:
1177 spin_unlock(q->lock_ptr); 1131 spin_unlock(q->lock_ptr);
1178 1132
1179 ret = futex_handle_fault((unsigned long)uaddr, fshared, attempt++); 1133 ret = futex_handle_fault((unsigned long)uaddr, attempt++);
1180 1134
1181 spin_lock(q->lock_ptr); 1135 spin_lock(q->lock_ptr);
1182 1136
@@ -1196,12 +1150,13 @@ handle_fault:
1196 * In case we must use restart_block to restart a futex_wait, 1150 * In case we must use restart_block to restart a futex_wait,
1197 * we encode in the 'flags' shared capability 1151 * we encode in the 'flags' shared capability
1198 */ 1152 */
1199#define FLAGS_SHARED 1 1153#define FLAGS_SHARED 0x01
1154#define FLAGS_CLOCKRT 0x02
1200 1155
1201static long futex_wait_restart(struct restart_block *restart); 1156static long futex_wait_restart(struct restart_block *restart);
1202 1157
1203static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared, 1158static int futex_wait(u32 __user *uaddr, int fshared,
1204 u32 val, ktime_t *abs_time, u32 bitset) 1159 u32 val, ktime_t *abs_time, u32 bitset, int clockrt)
1205{ 1160{
1206 struct task_struct *curr = current; 1161 struct task_struct *curr = current;
1207 DECLARE_WAITQUEUE(wait, curr); 1162 DECLARE_WAITQUEUE(wait, curr);
@@ -1218,8 +1173,7 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1218 q.pi_state = NULL; 1173 q.pi_state = NULL;
1219 q.bitset = bitset; 1174 q.bitset = bitset;
1220 retry: 1175 retry:
1221 futex_lock_mm(fshared); 1176 q.key = FUTEX_KEY_INIT;
1222
1223 ret = get_futex_key(uaddr, fshared, &q.key); 1177 ret = get_futex_key(uaddr, fshared, &q.key);
1224 if (unlikely(ret != 0)) 1178 if (unlikely(ret != 0))
1225 goto out_release_sem; 1179 goto out_release_sem;
@@ -1251,12 +1205,6 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1251 if (unlikely(ret)) { 1205 if (unlikely(ret)) {
1252 queue_unlock(&q, hb); 1206 queue_unlock(&q, hb);
1253 1207
1254 /*
1255 * If we would have faulted, release mmap_sem, fault it in and
1256 * start all over again.
1257 */
1258 futex_unlock_mm(fshared);
1259
1260 ret = get_user(uval, uaddr); 1208 ret = get_user(uval, uaddr);
1261 1209
1262 if (!ret) 1210 if (!ret)
@@ -1271,12 +1219,6 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1271 queue_me(&q, hb); 1219 queue_me(&q, hb);
1272 1220
1273 /* 1221 /*
1274 * Now the futex is queued and we have checked the data, we
1275 * don't want to hold mmap_sem while we sleep.
1276 */
1277 futex_unlock_mm(fshared);
1278
1279 /*
1280 * There might have been scheduling since the queue_me(), as we 1222 * There might have been scheduling since the queue_me(), as we
1281 * cannot hold a spinlock across the get_user() in case it 1223 * cannot hold a spinlock across the get_user() in case it
1282 * faults, and we cannot just set TASK_INTERRUPTIBLE state when 1224 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
@@ -1287,7 +1229,7 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1287 1229
1288 /* add_wait_queue is the barrier after __set_current_state. */ 1230 /* add_wait_queue is the barrier after __set_current_state. */
1289 __set_current_state(TASK_INTERRUPTIBLE); 1231 __set_current_state(TASK_INTERRUPTIBLE);
1290 add_wait_queue(&q.waiters, &wait); 1232 add_wait_queue(&q.waiter, &wait);
1291 /* 1233 /*
1292 * !plist_node_empty() is safe here without any lock. 1234 * !plist_node_empty() is safe here without any lock.
1293 * q.lock_ptr != 0 is not safe, because of ordering against wakeup. 1235 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
@@ -1300,8 +1242,10 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1300 slack = current->timer_slack_ns; 1242 slack = current->timer_slack_ns;
1301 if (rt_task(current)) 1243 if (rt_task(current))
1302 slack = 0; 1244 slack = 0;
1303 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, 1245 hrtimer_init_on_stack(&t.timer,
1304 HRTIMER_MODE_ABS); 1246 clockrt ? CLOCK_REALTIME :
1247 CLOCK_MONOTONIC,
1248 HRTIMER_MODE_ABS);
1305 hrtimer_init_sleeper(&t, current); 1249 hrtimer_init_sleeper(&t, current);
1306 hrtimer_set_expires_range_ns(&t.timer, *abs_time, slack); 1250 hrtimer_set_expires_range_ns(&t.timer, *abs_time, slack);
1307 1251
@@ -1356,6 +1300,8 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1356 1300
1357 if (fshared) 1301 if (fshared)
1358 restart->futex.flags |= FLAGS_SHARED; 1302 restart->futex.flags |= FLAGS_SHARED;
1303 if (clockrt)
1304 restart->futex.flags |= FLAGS_CLOCKRT;
1359 return -ERESTART_RESTARTBLOCK; 1305 return -ERESTART_RESTARTBLOCK;
1360 } 1306 }
1361 1307
@@ -1363,7 +1309,7 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1363 queue_unlock(&q, hb); 1309 queue_unlock(&q, hb);
1364 1310
1365 out_release_sem: 1311 out_release_sem:
1366 futex_unlock_mm(fshared); 1312 put_futex_key(fshared, &q.key);
1367 return ret; 1313 return ret;
1368} 1314}
1369 1315
@@ -1371,15 +1317,16 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1371static long futex_wait_restart(struct restart_block *restart) 1317static long futex_wait_restart(struct restart_block *restart)
1372{ 1318{
1373 u32 __user *uaddr = (u32 __user *)restart->futex.uaddr; 1319 u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
1374 struct rw_semaphore *fshared = NULL; 1320 int fshared = 0;
1375 ktime_t t; 1321 ktime_t t;
1376 1322
1377 t.tv64 = restart->futex.time; 1323 t.tv64 = restart->futex.time;
1378 restart->fn = do_no_restart_syscall; 1324 restart->fn = do_no_restart_syscall;
1379 if (restart->futex.flags & FLAGS_SHARED) 1325 if (restart->futex.flags & FLAGS_SHARED)
1380 fshared = &current->mm->mmap_sem; 1326 fshared = 1;
1381 return (long)futex_wait(uaddr, fshared, restart->futex.val, &t, 1327 return (long)futex_wait(uaddr, fshared, restart->futex.val, &t,
1382 restart->futex.bitset); 1328 restart->futex.bitset,
1329 restart->futex.flags & FLAGS_CLOCKRT);
1383} 1330}
1384 1331
1385 1332
@@ -1389,7 +1336,7 @@ static long futex_wait_restart(struct restart_block *restart)
1389 * if there are waiters then it will block, it does PI, etc. (Due to 1336 * if there are waiters then it will block, it does PI, etc. (Due to
1390 * races the kernel might see a 0 value of the futex too.) 1337 * races the kernel might see a 0 value of the futex too.)
1391 */ 1338 */
1392static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared, 1339static int futex_lock_pi(u32 __user *uaddr, int fshared,
1393 int detect, ktime_t *time, int trylock) 1340 int detect, ktime_t *time, int trylock)
1394{ 1341{
1395 struct hrtimer_sleeper timeout, *to = NULL; 1342 struct hrtimer_sleeper timeout, *to = NULL;
@@ -1412,8 +1359,7 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1412 1359
1413 q.pi_state = NULL; 1360 q.pi_state = NULL;
1414 retry: 1361 retry:
1415 futex_lock_mm(fshared); 1362 q.key = FUTEX_KEY_INIT;
1416
1417 ret = get_futex_key(uaddr, fshared, &q.key); 1363 ret = get_futex_key(uaddr, fshared, &q.key);
1418 if (unlikely(ret != 0)) 1364 if (unlikely(ret != 0))
1419 goto out_release_sem; 1365 goto out_release_sem;
@@ -1502,7 +1448,6 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1502 * exit to complete. 1448 * exit to complete.
1503 */ 1449 */
1504 queue_unlock(&q, hb); 1450 queue_unlock(&q, hb);
1505 futex_unlock_mm(fshared);
1506 cond_resched(); 1451 cond_resched();
1507 goto retry; 1452 goto retry;
1508 1453
@@ -1534,12 +1479,6 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1534 */ 1479 */
1535 queue_me(&q, hb); 1480 queue_me(&q, hb);
1536 1481
1537 /*
1538 * Now the futex is queued and we have checked the data, we
1539 * don't want to hold mmap_sem while we sleep.
1540 */
1541 futex_unlock_mm(fshared);
1542
1543 WARN_ON(!q.pi_state); 1482 WARN_ON(!q.pi_state);
1544 /* 1483 /*
1545 * Block on the PI mutex: 1484 * Block on the PI mutex:
@@ -1552,7 +1491,6 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1552 ret = ret ? 0 : -EWOULDBLOCK; 1491 ret = ret ? 0 : -EWOULDBLOCK;
1553 } 1492 }
1554 1493
1555 futex_lock_mm(fshared);
1556 spin_lock(q.lock_ptr); 1494 spin_lock(q.lock_ptr);
1557 1495
1558 if (!ret) { 1496 if (!ret) {
@@ -1618,7 +1556,6 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1618 1556
1619 /* Unqueue and drop the lock */ 1557 /* Unqueue and drop the lock */
1620 unqueue_me_pi(&q); 1558 unqueue_me_pi(&q);
1621 futex_unlock_mm(fshared);
1622 1559
1623 if (to) 1560 if (to)
1624 destroy_hrtimer_on_stack(&to->timer); 1561 destroy_hrtimer_on_stack(&to->timer);
@@ -1628,34 +1565,30 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1628 queue_unlock(&q, hb); 1565 queue_unlock(&q, hb);
1629 1566
1630 out_release_sem: 1567 out_release_sem:
1631 futex_unlock_mm(fshared); 1568 put_futex_key(fshared, &q.key);
1632 if (to) 1569 if (to)
1633 destroy_hrtimer_on_stack(&to->timer); 1570 destroy_hrtimer_on_stack(&to->timer);
1634 return ret; 1571 return ret;
1635 1572
1636 uaddr_faulted: 1573 uaddr_faulted:
1637 /* 1574 /*
1638 * We have to r/w *(int __user *)uaddr, but we can't modify it 1575 * We have to r/w *(int __user *)uaddr, and we have to modify it
1639 * non-atomically. Therefore, if get_user below is not 1576 * atomically. Therefore, if we continue to fault after get_user()
1640 * enough, we need to handle the fault ourselves, while 1577 * below, we need to handle the fault ourselves, while still holding
1641 * still holding the mmap_sem. 1578 * the mmap_sem. This can occur if the uaddr is under contention as
1642 * 1579 * we have to drop the mmap_sem in order to call get_user().
1643 * ... and hb->lock. :-) --ANK
1644 */ 1580 */
1645 queue_unlock(&q, hb); 1581 queue_unlock(&q, hb);
1646 1582
1647 if (attempt++) { 1583 if (attempt++) {
1648 ret = futex_handle_fault((unsigned long)uaddr, fshared, 1584 ret = futex_handle_fault((unsigned long)uaddr, attempt);
1649 attempt);
1650 if (ret) 1585 if (ret)
1651 goto out_release_sem; 1586 goto out_release_sem;
1652 goto retry_unlocked; 1587 goto retry_unlocked;
1653 } 1588 }
1654 1589
1655 futex_unlock_mm(fshared);
1656
1657 ret = get_user(uval, uaddr); 1590 ret = get_user(uval, uaddr);
1658 if (!ret && (uval != -EFAULT)) 1591 if (!ret)
1659 goto retry; 1592 goto retry;
1660 1593
1661 if (to) 1594 if (to)
@@ -1668,13 +1601,13 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1668 * This is the in-kernel slowpath: we look up the PI state (if any), 1601 * This is the in-kernel slowpath: we look up the PI state (if any),
1669 * and do the rt-mutex unlock. 1602 * and do the rt-mutex unlock.
1670 */ 1603 */
1671static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared) 1604static int futex_unlock_pi(u32 __user *uaddr, int fshared)
1672{ 1605{
1673 struct futex_hash_bucket *hb; 1606 struct futex_hash_bucket *hb;
1674 struct futex_q *this, *next; 1607 struct futex_q *this, *next;
1675 u32 uval; 1608 u32 uval;
1676 struct plist_head *head; 1609 struct plist_head *head;
1677 union futex_key key; 1610 union futex_key key = FUTEX_KEY_INIT;
1678 int ret, attempt = 0; 1611 int ret, attempt = 0;
1679 1612
1680retry: 1613retry:
@@ -1685,10 +1618,6 @@ retry:
1685 */ 1618 */
1686 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current)) 1619 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current))
1687 return -EPERM; 1620 return -EPERM;
1688 /*
1689 * First take all the futex related locks:
1690 */
1691 futex_lock_mm(fshared);
1692 1621
1693 ret = get_futex_key(uaddr, fshared, &key); 1622 ret = get_futex_key(uaddr, fshared, &key);
1694 if (unlikely(ret != 0)) 1623 if (unlikely(ret != 0))
@@ -1747,34 +1676,30 @@ retry_unlocked:
1747out_unlock: 1676out_unlock:
1748 spin_unlock(&hb->lock); 1677 spin_unlock(&hb->lock);
1749out: 1678out:
1750 futex_unlock_mm(fshared); 1679 put_futex_key(fshared, &key);
1751 1680
1752 return ret; 1681 return ret;
1753 1682
1754pi_faulted: 1683pi_faulted:
1755 /* 1684 /*
1756 * We have to r/w *(int __user *)uaddr, but we can't modify it 1685 * We have to r/w *(int __user *)uaddr, and we have to modify it
1757 * non-atomically. Therefore, if get_user below is not 1686 * atomically. Therefore, if we continue to fault after get_user()
1758 * enough, we need to handle the fault ourselves, while 1687 * below, we need to handle the fault ourselves, while still holding
1759 * still holding the mmap_sem. 1688 * the mmap_sem. This can occur if the uaddr is under contention as
1760 * 1689 * we have to drop the mmap_sem in order to call get_user().
1761 * ... and hb->lock. --ANK
1762 */ 1690 */
1763 spin_unlock(&hb->lock); 1691 spin_unlock(&hb->lock);
1764 1692
1765 if (attempt++) { 1693 if (attempt++) {
1766 ret = futex_handle_fault((unsigned long)uaddr, fshared, 1694 ret = futex_handle_fault((unsigned long)uaddr, attempt);
1767 attempt);
1768 if (ret) 1695 if (ret)
1769 goto out; 1696 goto out;
1770 uval = 0; 1697 uval = 0;
1771 goto retry_unlocked; 1698 goto retry_unlocked;
1772 } 1699 }
1773 1700
1774 futex_unlock_mm(fshared);
1775
1776 ret = get_user(uval, uaddr); 1701 ret = get_user(uval, uaddr);
1777 if (!ret && (uval != -EFAULT)) 1702 if (!ret)
1778 goto retry; 1703 goto retry;
1779 1704
1780 return ret; 1705 return ret;
@@ -1829,6 +1754,7 @@ sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
1829{ 1754{
1830 struct robust_list_head __user *head; 1755 struct robust_list_head __user *head;
1831 unsigned long ret; 1756 unsigned long ret;
1757 const struct cred *cred = current_cred(), *pcred;
1832 1758
1833 if (!futex_cmpxchg_enabled) 1759 if (!futex_cmpxchg_enabled)
1834 return -ENOSYS; 1760 return -ENOSYS;
@@ -1844,8 +1770,10 @@ sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
1844 if (!p) 1770 if (!p)
1845 goto err_unlock; 1771 goto err_unlock;
1846 ret = -EPERM; 1772 ret = -EPERM;
1847 if ((current->euid != p->euid) && (current->euid != p->uid) && 1773 pcred = __task_cred(p);
1848 !capable(CAP_SYS_PTRACE)) 1774 if (cred->euid != pcred->euid &&
1775 cred->euid != pcred->uid &&
1776 !capable(CAP_SYS_PTRACE))
1849 goto err_unlock; 1777 goto err_unlock;
1850 head = p->robust_list; 1778 head = p->robust_list;
1851 rcu_read_unlock(); 1779 rcu_read_unlock();
@@ -1898,8 +1826,7 @@ retry:
1898 * PI futexes happens in exit_pi_state(): 1826 * PI futexes happens in exit_pi_state():
1899 */ 1827 */
1900 if (!pi && (uval & FUTEX_WAITERS)) 1828 if (!pi && (uval & FUTEX_WAITERS))
1901 futex_wake(uaddr, &curr->mm->mmap_sem, 1, 1829 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
1902 FUTEX_BITSET_MATCH_ANY);
1903 } 1830 }
1904 return 0; 1831 return 0;
1905} 1832}
@@ -1993,18 +1920,22 @@ void exit_robust_list(struct task_struct *curr)
1993long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout, 1920long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
1994 u32 __user *uaddr2, u32 val2, u32 val3) 1921 u32 __user *uaddr2, u32 val2, u32 val3)
1995{ 1922{
1996 int ret = -ENOSYS; 1923 int clockrt, ret = -ENOSYS;
1997 int cmd = op & FUTEX_CMD_MASK; 1924 int cmd = op & FUTEX_CMD_MASK;
1998 struct rw_semaphore *fshared = NULL; 1925 int fshared = 0;
1999 1926
2000 if (!(op & FUTEX_PRIVATE_FLAG)) 1927 if (!(op & FUTEX_PRIVATE_FLAG))
2001 fshared = &current->mm->mmap_sem; 1928 fshared = 1;
1929
1930 clockrt = op & FUTEX_CLOCK_REALTIME;
1931 if (clockrt && cmd != FUTEX_WAIT_BITSET)
1932 return -ENOSYS;
2002 1933
2003 switch (cmd) { 1934 switch (cmd) {
2004 case FUTEX_WAIT: 1935 case FUTEX_WAIT:
2005 val3 = FUTEX_BITSET_MATCH_ANY; 1936 val3 = FUTEX_BITSET_MATCH_ANY;
2006 case FUTEX_WAIT_BITSET: 1937 case FUTEX_WAIT_BITSET:
2007 ret = futex_wait(uaddr, fshared, val, timeout, val3); 1938 ret = futex_wait(uaddr, fshared, val, timeout, val3, clockrt);
2008 break; 1939 break;
2009 case FUTEX_WAKE: 1940 case FUTEX_WAKE:
2010 val3 = FUTEX_BITSET_MATCH_ANY; 1941 val3 = FUTEX_BITSET_MATCH_ANY;