aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/futex.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/futex.c')
-rw-r--r--kernel/futex.c413
1 files changed, 171 insertions, 242 deletions
diff --git a/kernel/futex.c b/kernel/futex.c
index 4fe790e89d0f..002aa189eb09 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,48 @@ 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 /* If we're here then we tried to put a key we failed to get */
175 WARN_ON_ONCE(1);
176 return;
177 }
178
179 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
180 case FUT_OFF_INODE:
181 iput(key->shared.inode);
182 break;
183 case FUT_OFF_MMSHARED:
184 mmdrop(key->private.mm);
185 break;
186 }
187}
188
164/** 189/**
165 * get_futex_key - Get parameters which are the keys for a futex. 190 * get_futex_key - Get parameters which are the keys for a futex.
166 * @uaddr: virtual address of the futex 191 * @uaddr: virtual address of the futex
@@ -179,12 +204,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 204 * For other futexes, it points to &current->mm->mmap_sem and
180 * caller must have taken the reader lock. but NOT any spinlocks. 205 * caller must have taken the reader lock. but NOT any spinlocks.
181 */ 206 */
182static int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared, 207static int get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key)
183 union futex_key *key)
184{ 208{
185 unsigned long address = (unsigned long)uaddr; 209 unsigned long address = (unsigned long)uaddr;
186 struct mm_struct *mm = current->mm; 210 struct mm_struct *mm = current->mm;
187 struct vm_area_struct *vma;
188 struct page *page; 211 struct page *page;
189 int err; 212 int err;
190 213
@@ -208,100 +231,50 @@ static int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared,
208 return -EFAULT; 231 return -EFAULT;
209 key->private.mm = mm; 232 key->private.mm = mm;
210 key->private.address = address; 233 key->private.address = address;
234 get_futex_key_refs(key);
211 return 0; 235 return 0;
212 } 236 }
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 237
221 /* 238again:
222 * Permissions. 239 err = get_user_pages_fast(address, 1, 0, &page);
223 */ 240 if (err < 0)
224 if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ)) 241 return err;
225 return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES; 242
243 lock_page(page);
244 if (!page->mapping) {
245 unlock_page(page);
246 put_page(page);
247 goto again;
248 }
226 249
227 /* 250 /*
228 * Private mappings are handled in a simple way. 251 * Private mappings are handled in a simple way.
229 * 252 *
230 * NOTE: When userspace waits on a MAP_SHARED mapping, even if 253 * 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 254 * it's a read-only handle, it's expected that futexes attach to
232 * the object not the particular process. Therefore we use 255 * the object not the particular process.
233 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
234 * mappings of _writable_ handles.
235 */ 256 */
236 if (likely(!(vma->vm_flags & VM_MAYSHARE))) { 257 if (PageAnon(page)) {
237 key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */ 258 key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
238 key->private.mm = mm; 259 key->private.mm = mm;
239 key->private.address = address; 260 key->private.address = address;
240 return 0; 261 } else {
241 } 262 key->both.offset |= FUT_OFF_INODE; /* inode-based key */
242 263 key->shared.inode = page->mapping->host;
243 /* 264 key->shared.pgoff = page->index;
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 } 265 }
253 266
254 /* 267 get_futex_key_refs(key);
255 * We could walk the page table to read the non-linear
256 * pte, and get the page index without fetching the page
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 268
270/* 269 unlock_page(page);
271 * Take a reference to the resource addressed by a key. 270 put_page(page);
272 * Can be called while holding spinlocks. 271 return 0;
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} 272}
288 273
289/* 274static inline
290 * Drop a reference to the resource addressed by a key. 275void 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{ 276{
295 if (!key->both.ptr) 277 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} 278}
306 279
307static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval) 280static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval)
@@ -328,10 +301,8 @@ static int get_futex_value_locked(u32 *dest, u32 __user *from)
328 301
329/* 302/*
330 * Fault handling. 303 * Fault handling.
331 * if fshared is non NULL, current->mm->mmap_sem is already held
332 */ 304 */
333static int futex_handle_fault(unsigned long address, 305static int futex_handle_fault(unsigned long address, int attempt)
334 struct rw_semaphore *fshared, int attempt)
335{ 306{
336 struct vm_area_struct * vma; 307 struct vm_area_struct * vma;
337 struct mm_struct *mm = current->mm; 308 struct mm_struct *mm = current->mm;
@@ -340,8 +311,7 @@ static int futex_handle_fault(unsigned long address,
340 if (attempt > 2) 311 if (attempt > 2)
341 return ret; 312 return ret;
342 313
343 if (!fshared) 314 down_read(&mm->mmap_sem);
344 down_read(&mm->mmap_sem);
345 vma = find_vma(mm, address); 315 vma = find_vma(mm, address);
346 if (vma && address >= vma->vm_start && 316 if (vma && address >= vma->vm_start &&
347 (vma->vm_flags & VM_WRITE)) { 317 (vma->vm_flags & VM_WRITE)) {
@@ -361,8 +331,7 @@ static int futex_handle_fault(unsigned long address,
361 current->min_flt++; 331 current->min_flt++;
362 } 332 }
363 } 333 }
364 if (!fshared) 334 up_read(&mm->mmap_sem);
365 up_read(&mm->mmap_sem);
366 return ret; 335 return ret;
367} 336}
368 337
@@ -385,6 +354,7 @@ static int refill_pi_state_cache(void)
385 /* pi_mutex gets initialized later */ 354 /* pi_mutex gets initialized later */
386 pi_state->owner = NULL; 355 pi_state->owner = NULL;
387 atomic_set(&pi_state->refcount, 1); 356 atomic_set(&pi_state->refcount, 1);
357 pi_state->key = FUTEX_KEY_INIT;
388 358
389 current->pi_state_cache = pi_state; 359 current->pi_state_cache = pi_state;
390 360
@@ -469,7 +439,7 @@ void exit_pi_state_list(struct task_struct *curr)
469 struct list_head *next, *head = &curr->pi_state_list; 439 struct list_head *next, *head = &curr->pi_state_list;
470 struct futex_pi_state *pi_state; 440 struct futex_pi_state *pi_state;
471 struct futex_hash_bucket *hb; 441 struct futex_hash_bucket *hb;
472 union futex_key key; 442 union futex_key key = FUTEX_KEY_INIT;
473 443
474 if (!futex_cmpxchg_enabled) 444 if (!futex_cmpxchg_enabled)
475 return; 445 return;
@@ -614,7 +584,7 @@ static void wake_futex(struct futex_q *q)
614 * The lock in wake_up_all() is a crucial memory barrier after the 584 * The lock in wake_up_all() is a crucial memory barrier after the
615 * plist_del() and also before assigning to q->lock_ptr. 585 * plist_del() and also before assigning to q->lock_ptr.
616 */ 586 */
617 wake_up_all(&q->waiters); 587 wake_up(&q->waiter);
618 /* 588 /*
619 * The waiting task can free the futex_q as soon as this is written, 589 * The waiting task can free the futex_q as soon as this is written,
620 * without taking any locks. This must come last. 590 * without taking any locks. This must come last.
@@ -726,20 +696,17 @@ double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
726 * Wake up all waiters hashed on the physical page that is mapped 696 * Wake up all waiters hashed on the physical page that is mapped
727 * to this virtual address: 697 * to this virtual address:
728 */ 698 */
729static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared, 699static int futex_wake(u32 __user *uaddr, int fshared, int nr_wake, u32 bitset)
730 int nr_wake, u32 bitset)
731{ 700{
732 struct futex_hash_bucket *hb; 701 struct futex_hash_bucket *hb;
733 struct futex_q *this, *next; 702 struct futex_q *this, *next;
734 struct plist_head *head; 703 struct plist_head *head;
735 union futex_key key; 704 union futex_key key = FUTEX_KEY_INIT;
736 int ret; 705 int ret;
737 706
738 if (!bitset) 707 if (!bitset)
739 return -EINVAL; 708 return -EINVAL;
740 709
741 futex_lock_mm(fshared);
742
743 ret = get_futex_key(uaddr, fshared, &key); 710 ret = get_futex_key(uaddr, fshared, &key);
744 if (unlikely(ret != 0)) 711 if (unlikely(ret != 0))
745 goto out; 712 goto out;
@@ -766,8 +733,8 @@ static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
766 } 733 }
767 734
768 spin_unlock(&hb->lock); 735 spin_unlock(&hb->lock);
736 put_futex_key(fshared, &key);
769out: 737out:
770 futex_unlock_mm(fshared);
771 return ret; 738 return ret;
772} 739}
773 740
@@ -776,25 +743,22 @@ out:
776 * to this virtual address: 743 * to this virtual address:
777 */ 744 */
778static int 745static int
779futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared, 746futex_wake_op(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
780 u32 __user *uaddr2,
781 int nr_wake, int nr_wake2, int op) 747 int nr_wake, int nr_wake2, int op)
782{ 748{
783 union futex_key key1, key2; 749 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
784 struct futex_hash_bucket *hb1, *hb2; 750 struct futex_hash_bucket *hb1, *hb2;
785 struct plist_head *head; 751 struct plist_head *head;
786 struct futex_q *this, *next; 752 struct futex_q *this, *next;
787 int ret, op_ret, attempt = 0; 753 int ret, op_ret, attempt = 0;
788 754
789retryfull: 755retryfull:
790 futex_lock_mm(fshared);
791
792 ret = get_futex_key(uaddr1, fshared, &key1); 756 ret = get_futex_key(uaddr1, fshared, &key1);
793 if (unlikely(ret != 0)) 757 if (unlikely(ret != 0))
794 goto out; 758 goto out;
795 ret = get_futex_key(uaddr2, fshared, &key2); 759 ret = get_futex_key(uaddr2, fshared, &key2);
796 if (unlikely(ret != 0)) 760 if (unlikely(ret != 0))
797 goto out; 761 goto out_put_key1;
798 762
799 hb1 = hash_futex(&key1); 763 hb1 = hash_futex(&key1);
800 hb2 = hash_futex(&key2); 764 hb2 = hash_futex(&key2);
@@ -816,12 +780,12 @@ retry:
816 * but we might get them from range checking 780 * but we might get them from range checking
817 */ 781 */
818 ret = op_ret; 782 ret = op_ret;
819 goto out; 783 goto out_put_keys;
820#endif 784#endif
821 785
822 if (unlikely(op_ret != -EFAULT)) { 786 if (unlikely(op_ret != -EFAULT)) {
823 ret = op_ret; 787 ret = op_ret;
824 goto out; 788 goto out_put_keys;
825 } 789 }
826 790
827 /* 791 /*
@@ -833,18 +797,12 @@ retry:
833 */ 797 */
834 if (attempt++) { 798 if (attempt++) {
835 ret = futex_handle_fault((unsigned long)uaddr2, 799 ret = futex_handle_fault((unsigned long)uaddr2,
836 fshared, attempt); 800 attempt);
837 if (ret) 801 if (ret)
838 goto out; 802 goto out_put_keys;
839 goto retry; 803 goto retry;
840 } 804 }
841 805
842 /*
843 * If we would have faulted, release mmap_sem,
844 * fault it in and start all over again.
845 */
846 futex_unlock_mm(fshared);
847
848 ret = get_user(dummy, uaddr2); 806 ret = get_user(dummy, uaddr2);
849 if (ret) 807 if (ret)
850 return ret; 808 return ret;
@@ -879,9 +837,11 @@ retry:
879 spin_unlock(&hb1->lock); 837 spin_unlock(&hb1->lock);
880 if (hb1 != hb2) 838 if (hb1 != hb2)
881 spin_unlock(&hb2->lock); 839 spin_unlock(&hb2->lock);
840out_put_keys:
841 put_futex_key(fshared, &key2);
842out_put_key1:
843 put_futex_key(fshared, &key1);
882out: 844out:
883 futex_unlock_mm(fshared);
884
885 return ret; 845 return ret;
886} 846}
887 847
@@ -889,25 +849,22 @@ out:
889 * Requeue all waiters hashed on one physical page to another 849 * Requeue all waiters hashed on one physical page to another
890 * physical page. 850 * physical page.
891 */ 851 */
892static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared, 852static int futex_requeue(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
893 u32 __user *uaddr2,
894 int nr_wake, int nr_requeue, u32 *cmpval) 853 int nr_wake, int nr_requeue, u32 *cmpval)
895{ 854{
896 union futex_key key1, key2; 855 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
897 struct futex_hash_bucket *hb1, *hb2; 856 struct futex_hash_bucket *hb1, *hb2;
898 struct plist_head *head1; 857 struct plist_head *head1;
899 struct futex_q *this, *next; 858 struct futex_q *this, *next;
900 int ret, drop_count = 0; 859 int ret, drop_count = 0;
901 860
902 retry: 861retry:
903 futex_lock_mm(fshared);
904
905 ret = get_futex_key(uaddr1, fshared, &key1); 862 ret = get_futex_key(uaddr1, fshared, &key1);
906 if (unlikely(ret != 0)) 863 if (unlikely(ret != 0))
907 goto out; 864 goto out;
908 ret = get_futex_key(uaddr2, fshared, &key2); 865 ret = get_futex_key(uaddr2, fshared, &key2);
909 if (unlikely(ret != 0)) 866 if (unlikely(ret != 0))
910 goto out; 867 goto out_put_key1;
911 868
912 hb1 = hash_futex(&key1); 869 hb1 = hash_futex(&key1);
913 hb2 = hash_futex(&key2); 870 hb2 = hash_futex(&key2);
@@ -924,18 +881,12 @@ static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
924 if (hb1 != hb2) 881 if (hb1 != hb2)
925 spin_unlock(&hb2->lock); 882 spin_unlock(&hb2->lock);
926 883
927 /*
928 * If we would have faulted, release mmap_sem, fault
929 * it in and start all over again.
930 */
931 futex_unlock_mm(fshared);
932
933 ret = get_user(curval, uaddr1); 884 ret = get_user(curval, uaddr1);
934 885
935 if (!ret) 886 if (!ret)
936 goto retry; 887 goto retry;
937 888
938 return ret; 889 goto out_put_keys;
939 } 890 }
940 if (curval != *cmpval) { 891 if (curval != *cmpval) {
941 ret = -EAGAIN; 892 ret = -EAGAIN;
@@ -980,8 +931,11 @@ out_unlock:
980 while (--drop_count >= 0) 931 while (--drop_count >= 0)
981 drop_futex_key_refs(&key1); 932 drop_futex_key_refs(&key1);
982 933
934out_put_keys:
935 put_futex_key(fshared, &key2);
936out_put_key1:
937 put_futex_key(fshared, &key1);
983out: 938out:
984 futex_unlock_mm(fshared);
985 return ret; 939 return ret;
986} 940}
987 941
@@ -990,7 +944,7 @@ static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
990{ 944{
991 struct futex_hash_bucket *hb; 945 struct futex_hash_bucket *hb;
992 946
993 init_waitqueue_head(&q->waiters); 947 init_waitqueue_head(&q->waiter);
994 948
995 get_futex_key_refs(&q->key); 949 get_futex_key_refs(&q->key);
996 hb = hash_futex(&q->key); 950 hb = hash_futex(&q->key);
@@ -1042,7 +996,7 @@ static int unqueue_me(struct futex_q *q)
1042 int ret = 0; 996 int ret = 0;
1043 997
1044 /* In the common case we don't take the spinlock, which is nice. */ 998 /* In the common case we don't take the spinlock, which is nice. */
1045 retry: 999retry:
1046 lock_ptr = q->lock_ptr; 1000 lock_ptr = q->lock_ptr;
1047 barrier(); 1001 barrier();
1048 if (lock_ptr != NULL) { 1002 if (lock_ptr != NULL) {
@@ -1103,8 +1057,7 @@ static void unqueue_me_pi(struct futex_q *q)
1103 * private futexes. 1057 * private futexes.
1104 */ 1058 */
1105static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, 1059static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
1106 struct task_struct *newowner, 1060 struct task_struct *newowner, int fshared)
1107 struct rw_semaphore *fshared)
1108{ 1061{
1109 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS; 1062 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
1110 struct futex_pi_state *pi_state = q->pi_state; 1063 struct futex_pi_state *pi_state = q->pi_state;
@@ -1183,7 +1136,7 @@ retry:
1183handle_fault: 1136handle_fault:
1184 spin_unlock(q->lock_ptr); 1137 spin_unlock(q->lock_ptr);
1185 1138
1186 ret = futex_handle_fault((unsigned long)uaddr, fshared, attempt++); 1139 ret = futex_handle_fault((unsigned long)uaddr, attempt++);
1187 1140
1188 spin_lock(q->lock_ptr); 1141 spin_lock(q->lock_ptr);
1189 1142
@@ -1203,12 +1156,13 @@ handle_fault:
1203 * In case we must use restart_block to restart a futex_wait, 1156 * In case we must use restart_block to restart a futex_wait,
1204 * we encode in the 'flags' shared capability 1157 * we encode in the 'flags' shared capability
1205 */ 1158 */
1206#define FLAGS_SHARED 1 1159#define FLAGS_SHARED 0x01
1160#define FLAGS_CLOCKRT 0x02
1207 1161
1208static long futex_wait_restart(struct restart_block *restart); 1162static long futex_wait_restart(struct restart_block *restart);
1209 1163
1210static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared, 1164static int futex_wait(u32 __user *uaddr, int fshared,
1211 u32 val, ktime_t *abs_time, u32 bitset) 1165 u32 val, ktime_t *abs_time, u32 bitset, int clockrt)
1212{ 1166{
1213 struct task_struct *curr = current; 1167 struct task_struct *curr = current;
1214 DECLARE_WAITQUEUE(wait, curr); 1168 DECLARE_WAITQUEUE(wait, curr);
@@ -1224,12 +1178,11 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1224 1178
1225 q.pi_state = NULL; 1179 q.pi_state = NULL;
1226 q.bitset = bitset; 1180 q.bitset = bitset;
1227 retry: 1181retry:
1228 futex_lock_mm(fshared); 1182 q.key = FUTEX_KEY_INIT;
1229
1230 ret = get_futex_key(uaddr, fshared, &q.key); 1183 ret = get_futex_key(uaddr, fshared, &q.key);
1231 if (unlikely(ret != 0)) 1184 if (unlikely(ret != 0))
1232 goto out_release_sem; 1185 goto out;
1233 1186
1234 hb = queue_lock(&q); 1187 hb = queue_lock(&q);
1235 1188
@@ -1257,12 +1210,7 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1257 1210
1258 if (unlikely(ret)) { 1211 if (unlikely(ret)) {
1259 queue_unlock(&q, hb); 1212 queue_unlock(&q, hb);
1260 1213 put_futex_key(fshared, &q.key);
1261 /*
1262 * If we would have faulted, release mmap_sem, fault it in and
1263 * start all over again.
1264 */
1265 futex_unlock_mm(fshared);
1266 1214
1267 ret = get_user(uval, uaddr); 1215 ret = get_user(uval, uaddr);
1268 1216
@@ -1272,18 +1220,12 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1272 } 1220 }
1273 ret = -EWOULDBLOCK; 1221 ret = -EWOULDBLOCK;
1274 if (uval != val) 1222 if (uval != val)
1275 goto out_unlock_release_sem; 1223 goto out_unlock_put_key;
1276 1224
1277 /* Only actually queue if *uaddr contained val. */ 1225 /* Only actually queue if *uaddr contained val. */
1278 queue_me(&q, hb); 1226 queue_me(&q, hb);
1279 1227
1280 /* 1228 /*
1281 * Now the futex is queued and we have checked the data, we
1282 * don't want to hold mmap_sem while we sleep.
1283 */
1284 futex_unlock_mm(fshared);
1285
1286 /*
1287 * There might have been scheduling since the queue_me(), as we 1229 * There might have been scheduling since the queue_me(), as we
1288 * cannot hold a spinlock across the get_user() in case it 1230 * cannot hold a spinlock across the get_user() in case it
1289 * faults, and we cannot just set TASK_INTERRUPTIBLE state when 1231 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
@@ -1294,7 +1236,7 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1294 1236
1295 /* add_wait_queue is the barrier after __set_current_state. */ 1237 /* add_wait_queue is the barrier after __set_current_state. */
1296 __set_current_state(TASK_INTERRUPTIBLE); 1238 __set_current_state(TASK_INTERRUPTIBLE);
1297 add_wait_queue(&q.waiters, &wait); 1239 add_wait_queue(&q.waiter, &wait);
1298 /* 1240 /*
1299 * !plist_node_empty() is safe here without any lock. 1241 * !plist_node_empty() is safe here without any lock.
1300 * q.lock_ptr != 0 is not safe, because of ordering against wakeup. 1242 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
@@ -1307,8 +1249,10 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1307 slack = current->timer_slack_ns; 1249 slack = current->timer_slack_ns;
1308 if (rt_task(current)) 1250 if (rt_task(current))
1309 slack = 0; 1251 slack = 0;
1310 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, 1252 hrtimer_init_on_stack(&t.timer,
1311 HRTIMER_MODE_ABS); 1253 clockrt ? CLOCK_REALTIME :
1254 CLOCK_MONOTONIC,
1255 HRTIMER_MODE_ABS);
1312 hrtimer_init_sleeper(&t, current); 1256 hrtimer_init_sleeper(&t, current);
1313 hrtimer_set_expires_range_ns(&t.timer, *abs_time, slack); 1257 hrtimer_set_expires_range_ns(&t.timer, *abs_time, slack);
1314 1258
@@ -1363,14 +1307,16 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1363 1307
1364 if (fshared) 1308 if (fshared)
1365 restart->futex.flags |= FLAGS_SHARED; 1309 restart->futex.flags |= FLAGS_SHARED;
1310 if (clockrt)
1311 restart->futex.flags |= FLAGS_CLOCKRT;
1366 return -ERESTART_RESTARTBLOCK; 1312 return -ERESTART_RESTARTBLOCK;
1367 } 1313 }
1368 1314
1369 out_unlock_release_sem: 1315out_unlock_put_key:
1370 queue_unlock(&q, hb); 1316 queue_unlock(&q, hb);
1317 put_futex_key(fshared, &q.key);
1371 1318
1372 out_release_sem: 1319out:
1373 futex_unlock_mm(fshared);
1374 return ret; 1320 return ret;
1375} 1321}
1376 1322
@@ -1378,15 +1324,16 @@ static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1378static long futex_wait_restart(struct restart_block *restart) 1324static long futex_wait_restart(struct restart_block *restart)
1379{ 1325{
1380 u32 __user *uaddr = (u32 __user *)restart->futex.uaddr; 1326 u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
1381 struct rw_semaphore *fshared = NULL; 1327 int fshared = 0;
1382 ktime_t t; 1328 ktime_t t;
1383 1329
1384 t.tv64 = restart->futex.time; 1330 t.tv64 = restart->futex.time;
1385 restart->fn = do_no_restart_syscall; 1331 restart->fn = do_no_restart_syscall;
1386 if (restart->futex.flags & FLAGS_SHARED) 1332 if (restart->futex.flags & FLAGS_SHARED)
1387 fshared = &current->mm->mmap_sem; 1333 fshared = 1;
1388 return (long)futex_wait(uaddr, fshared, restart->futex.val, &t, 1334 return (long)futex_wait(uaddr, fshared, restart->futex.val, &t,
1389 restart->futex.bitset); 1335 restart->futex.bitset,
1336 restart->futex.flags & FLAGS_CLOCKRT);
1390} 1337}
1391 1338
1392 1339
@@ -1396,7 +1343,7 @@ static long futex_wait_restart(struct restart_block *restart)
1396 * if there are waiters then it will block, it does PI, etc. (Due to 1343 * if there are waiters then it will block, it does PI, etc. (Due to
1397 * races the kernel might see a 0 value of the futex too.) 1344 * races the kernel might see a 0 value of the futex too.)
1398 */ 1345 */
1399static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared, 1346static int futex_lock_pi(u32 __user *uaddr, int fshared,
1400 int detect, ktime_t *time, int trylock) 1347 int detect, ktime_t *time, int trylock)
1401{ 1348{
1402 struct hrtimer_sleeper timeout, *to = NULL; 1349 struct hrtimer_sleeper timeout, *to = NULL;
@@ -1418,17 +1365,16 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1418 } 1365 }
1419 1366
1420 q.pi_state = NULL; 1367 q.pi_state = NULL;
1421 retry: 1368retry:
1422 futex_lock_mm(fshared); 1369 q.key = FUTEX_KEY_INIT;
1423
1424 ret = get_futex_key(uaddr, fshared, &q.key); 1370 ret = get_futex_key(uaddr, fshared, &q.key);
1425 if (unlikely(ret != 0)) 1371 if (unlikely(ret != 0))
1426 goto out_release_sem; 1372 goto out;
1427 1373
1428 retry_unlocked: 1374retry_unlocked:
1429 hb = queue_lock(&q); 1375 hb = queue_lock(&q);
1430 1376
1431 retry_locked: 1377retry_locked:
1432 ret = lock_taken = 0; 1378 ret = lock_taken = 0;
1433 1379
1434 /* 1380 /*
@@ -1449,14 +1395,14 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1449 */ 1395 */
1450 if (unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(current))) { 1396 if (unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(current))) {
1451 ret = -EDEADLK; 1397 ret = -EDEADLK;
1452 goto out_unlock_release_sem; 1398 goto out_unlock_put_key;
1453 } 1399 }
1454 1400
1455 /* 1401 /*
1456 * Surprise - we got the lock. Just return to userspace: 1402 * Surprise - we got the lock. Just return to userspace:
1457 */ 1403 */
1458 if (unlikely(!curval)) 1404 if (unlikely(!curval))
1459 goto out_unlock_release_sem; 1405 goto out_unlock_put_key;
1460 1406
1461 uval = curval; 1407 uval = curval;
1462 1408
@@ -1492,7 +1438,7 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1492 * We took the lock due to owner died take over. 1438 * We took the lock due to owner died take over.
1493 */ 1439 */
1494 if (unlikely(lock_taken)) 1440 if (unlikely(lock_taken))
1495 goto out_unlock_release_sem; 1441 goto out_unlock_put_key;
1496 1442
1497 /* 1443 /*
1498 * We dont have the lock. Look up the PI state (or create it if 1444 * We dont have the lock. Look up the PI state (or create it if
@@ -1509,7 +1455,6 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1509 * exit to complete. 1455 * exit to complete.
1510 */ 1456 */
1511 queue_unlock(&q, hb); 1457 queue_unlock(&q, hb);
1512 futex_unlock_mm(fshared);
1513 cond_resched(); 1458 cond_resched();
1514 goto retry; 1459 goto retry;
1515 1460
@@ -1532,7 +1477,7 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1532 goto retry_locked; 1477 goto retry_locked;
1533 } 1478 }
1534 default: 1479 default:
1535 goto out_unlock_release_sem; 1480 goto out_unlock_put_key;
1536 } 1481 }
1537 } 1482 }
1538 1483
@@ -1541,12 +1486,6 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1541 */ 1486 */
1542 queue_me(&q, hb); 1487 queue_me(&q, hb);
1543 1488
1544 /*
1545 * Now the futex is queued and we have checked the data, we
1546 * don't want to hold mmap_sem while we sleep.
1547 */
1548 futex_unlock_mm(fshared);
1549
1550 WARN_ON(!q.pi_state); 1489 WARN_ON(!q.pi_state);
1551 /* 1490 /*
1552 * Block on the PI mutex: 1491 * Block on the PI mutex:
@@ -1559,7 +1498,6 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1559 ret = ret ? 0 : -EWOULDBLOCK; 1498 ret = ret ? 0 : -EWOULDBLOCK;
1560 } 1499 }
1561 1500
1562 futex_lock_mm(fshared);
1563 spin_lock(q.lock_ptr); 1501 spin_lock(q.lock_ptr);
1564 1502
1565 if (!ret) { 1503 if (!ret) {
@@ -1625,44 +1563,40 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1625 1563
1626 /* Unqueue and drop the lock */ 1564 /* Unqueue and drop the lock */
1627 unqueue_me_pi(&q); 1565 unqueue_me_pi(&q);
1628 futex_unlock_mm(fshared);
1629 1566
1630 if (to) 1567 if (to)
1631 destroy_hrtimer_on_stack(&to->timer); 1568 destroy_hrtimer_on_stack(&to->timer);
1632 return ret != -EINTR ? ret : -ERESTARTNOINTR; 1569 return ret != -EINTR ? ret : -ERESTARTNOINTR;
1633 1570
1634 out_unlock_release_sem: 1571out_unlock_put_key:
1635 queue_unlock(&q, hb); 1572 queue_unlock(&q, hb);
1636 1573
1637 out_release_sem: 1574out_put_key:
1638 futex_unlock_mm(fshared); 1575 put_futex_key(fshared, &q.key);
1576out:
1639 if (to) 1577 if (to)
1640 destroy_hrtimer_on_stack(&to->timer); 1578 destroy_hrtimer_on_stack(&to->timer);
1641 return ret; 1579 return ret;
1642 1580
1643 uaddr_faulted: 1581uaddr_faulted:
1644 /* 1582 /*
1645 * We have to r/w *(int __user *)uaddr, but we can't modify it 1583 * We have to r/w *(int __user *)uaddr, and we have to modify it
1646 * non-atomically. Therefore, if get_user below is not 1584 * atomically. Therefore, if we continue to fault after get_user()
1647 * enough, we need to handle the fault ourselves, while 1585 * below, we need to handle the fault ourselves, while still holding
1648 * still holding the mmap_sem. 1586 * the mmap_sem. This can occur if the uaddr is under contention as
1649 * 1587 * we have to drop the mmap_sem in order to call get_user().
1650 * ... and hb->lock. :-) --ANK
1651 */ 1588 */
1652 queue_unlock(&q, hb); 1589 queue_unlock(&q, hb);
1653 1590
1654 if (attempt++) { 1591 if (attempt++) {
1655 ret = futex_handle_fault((unsigned long)uaddr, fshared, 1592 ret = futex_handle_fault((unsigned long)uaddr, attempt);
1656 attempt);
1657 if (ret) 1593 if (ret)
1658 goto out_release_sem; 1594 goto out_put_key;
1659 goto retry_unlocked; 1595 goto retry_unlocked;
1660 } 1596 }
1661 1597
1662 futex_unlock_mm(fshared);
1663
1664 ret = get_user(uval, uaddr); 1598 ret = get_user(uval, uaddr);
1665 if (!ret && (uval != -EFAULT)) 1599 if (!ret)
1666 goto retry; 1600 goto retry;
1667 1601
1668 if (to) 1602 if (to)
@@ -1675,13 +1609,13 @@ static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1675 * This is the in-kernel slowpath: we look up the PI state (if any), 1609 * This is the in-kernel slowpath: we look up the PI state (if any),
1676 * and do the rt-mutex unlock. 1610 * and do the rt-mutex unlock.
1677 */ 1611 */
1678static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared) 1612static int futex_unlock_pi(u32 __user *uaddr, int fshared)
1679{ 1613{
1680 struct futex_hash_bucket *hb; 1614 struct futex_hash_bucket *hb;
1681 struct futex_q *this, *next; 1615 struct futex_q *this, *next;
1682 u32 uval; 1616 u32 uval;
1683 struct plist_head *head; 1617 struct plist_head *head;
1684 union futex_key key; 1618 union futex_key key = FUTEX_KEY_INIT;
1685 int ret, attempt = 0; 1619 int ret, attempt = 0;
1686 1620
1687retry: 1621retry:
@@ -1692,10 +1626,6 @@ retry:
1692 */ 1626 */
1693 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current)) 1627 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current))
1694 return -EPERM; 1628 return -EPERM;
1695 /*
1696 * First take all the futex related locks:
1697 */
1698 futex_lock_mm(fshared);
1699 1629
1700 ret = get_futex_key(uaddr, fshared, &key); 1630 ret = get_futex_key(uaddr, fshared, &key);
1701 if (unlikely(ret != 0)) 1631 if (unlikely(ret != 0))
@@ -1753,35 +1683,31 @@ retry_unlocked:
1753 1683
1754out_unlock: 1684out_unlock:
1755 spin_unlock(&hb->lock); 1685 spin_unlock(&hb->lock);
1756out: 1686 put_futex_key(fshared, &key);
1757 futex_unlock_mm(fshared);
1758 1687
1688out:
1759 return ret; 1689 return ret;
1760 1690
1761pi_faulted: 1691pi_faulted:
1762 /* 1692 /*
1763 * We have to r/w *(int __user *)uaddr, but we can't modify it 1693 * We have to r/w *(int __user *)uaddr, and we have to modify it
1764 * non-atomically. Therefore, if get_user below is not 1694 * atomically. Therefore, if we continue to fault after get_user()
1765 * enough, we need to handle the fault ourselves, while 1695 * below, we need to handle the fault ourselves, while still holding
1766 * still holding the mmap_sem. 1696 * the mmap_sem. This can occur if the uaddr is under contention as
1767 * 1697 * we have to drop the mmap_sem in order to call get_user().
1768 * ... and hb->lock. --ANK
1769 */ 1698 */
1770 spin_unlock(&hb->lock); 1699 spin_unlock(&hb->lock);
1771 1700
1772 if (attempt++) { 1701 if (attempt++) {
1773 ret = futex_handle_fault((unsigned long)uaddr, fshared, 1702 ret = futex_handle_fault((unsigned long)uaddr, attempt);
1774 attempt);
1775 if (ret) 1703 if (ret)
1776 goto out; 1704 goto out;
1777 uval = 0; 1705 uval = 0;
1778 goto retry_unlocked; 1706 goto retry_unlocked;
1779 } 1707 }
1780 1708
1781 futex_unlock_mm(fshared);
1782
1783 ret = get_user(uval, uaddr); 1709 ret = get_user(uval, uaddr);
1784 if (!ret && (uval != -EFAULT)) 1710 if (!ret)
1785 goto retry; 1711 goto retry;
1786 1712
1787 return ret; 1713 return ret;
@@ -1908,8 +1834,7 @@ retry:
1908 * PI futexes happens in exit_pi_state(): 1834 * PI futexes happens in exit_pi_state():
1909 */ 1835 */
1910 if (!pi && (uval & FUTEX_WAITERS)) 1836 if (!pi && (uval & FUTEX_WAITERS))
1911 futex_wake(uaddr, &curr->mm->mmap_sem, 1, 1837 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
1912 FUTEX_BITSET_MATCH_ANY);
1913 } 1838 }
1914 return 0; 1839 return 0;
1915} 1840}
@@ -2003,18 +1928,22 @@ void exit_robust_list(struct task_struct *curr)
2003long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout, 1928long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
2004 u32 __user *uaddr2, u32 val2, u32 val3) 1929 u32 __user *uaddr2, u32 val2, u32 val3)
2005{ 1930{
2006 int ret = -ENOSYS; 1931 int clockrt, ret = -ENOSYS;
2007 int cmd = op & FUTEX_CMD_MASK; 1932 int cmd = op & FUTEX_CMD_MASK;
2008 struct rw_semaphore *fshared = NULL; 1933 int fshared = 0;
2009 1934
2010 if (!(op & FUTEX_PRIVATE_FLAG)) 1935 if (!(op & FUTEX_PRIVATE_FLAG))
2011 fshared = &current->mm->mmap_sem; 1936 fshared = 1;
1937
1938 clockrt = op & FUTEX_CLOCK_REALTIME;
1939 if (clockrt && cmd != FUTEX_WAIT_BITSET)
1940 return -ENOSYS;
2012 1941
2013 switch (cmd) { 1942 switch (cmd) {
2014 case FUTEX_WAIT: 1943 case FUTEX_WAIT:
2015 val3 = FUTEX_BITSET_MATCH_ANY; 1944 val3 = FUTEX_BITSET_MATCH_ANY;
2016 case FUTEX_WAIT_BITSET: 1945 case FUTEX_WAIT_BITSET:
2017 ret = futex_wait(uaddr, fshared, val, timeout, val3); 1946 ret = futex_wait(uaddr, fshared, val, timeout, val3, clockrt);
2018 break; 1947 break;
2019 case FUTEX_WAKE: 1948 case FUTEX_WAKE:
2020 val3 = FUTEX_BITSET_MATCH_ANY; 1949 val3 = FUTEX_BITSET_MATCH_ANY;