1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#include <linux/slab.h>
44#include <linux/poll.h>
45#include <linux/fs.h>
46#include <linux/file.h>
47#include <linux/jhash.h>
48#include <linux/init.h>
49#include <linux/futex.h>
50#include <linux/mount.h>
51#include <linux/pagemap.h>
52#include <linux/syscalls.h>
53#include <linux/signal.h>
54#include <linux/module.h>
55#include <linux/magic.h>
56#include <linux/pid.h>
57#include <linux/nsproxy.h>
58
59#include <asm/futex.h>
60
61#include "rtmutex_common.h"
62
63int __read_mostly futex_cmpxchg_enabled;
64
65#define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
66
67
68
69
70struct futex_pi_state {
71
72
73
74
75 struct list_head list;
76
77
78
79
80 struct rt_mutex pi_mutex;
81
82 struct task_struct *owner;
83 atomic_t refcount;
84
85 union futex_key key;
86};
87
88
89
90
91
92
93
94
95
96
97struct futex_q {
98 struct plist_node list;
99
100 wait_queue_head_t waiter;
101
102
103 spinlock_t *lock_ptr;
104
105
106 union futex_key key;
107
108
109 struct futex_pi_state *pi_state;
110 struct task_struct *task;
111
112
113 u32 bitset;
114};
115
116
117
118
119struct futex_hash_bucket {
120 spinlock_t lock;
121 struct plist_head chain;
122};
123
124static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
125
126
127
128
129static struct futex_hash_bucket *hash_futex(union futex_key *key)
130{
131 u32 hash = jhash2((u32*)&key->both.word,
132 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
133 key->both.offset);
134 return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
135}
136
137
138
139
140static inline int match_futex(union futex_key *key1, union futex_key *key2)
141{
142 return (key1->both.word == key2->both.word
143 && key1->both.ptr == key2->both.ptr
144 && key1->both.offset == key2->both.offset);
145}
146
147
148
149
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
169
170
171static void drop_futex_key_refs(union futex_key *key)
172{
173 if (!key->both.ptr) {
174
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207static int get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key)
208{
209 unsigned long address = (unsigned long)uaddr;
210 struct mm_struct *mm = current->mm;
211 struct page *page;
212 int err;
213
214
215
216
217 key->both.offset = address % PAGE_SIZE;
218 if (unlikely((address % sizeof(u32)) != 0))
219 return -EINVAL;
220 address -= key->both.offset;
221
222
223
224
225
226
227
228
229 if (!fshared) {
230 if (unlikely(!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))))
231 return -EFAULT;
232 key->private.mm = mm;
233 key->private.address = address;
234 get_futex_key_refs(key);
235 return 0;
236 }
237
238again:
239 err = get_user_pages_fast(address, 1, 0, &page);
240 if (err < 0)
241 return err;
242
243 lock_page(page);
244 if (!page->mapping) {
245 unlock_page(page);
246 put_page(page);
247 goto again;
248 }
249
250
251
252
253
254
255
256
257 if (PageAnon(page)) {
258 key->both.offset |= FUT_OFF_MMSHARED;
259 key->private.mm = mm;
260 key->private.address = address;
261 } else {
262 key->both.offset |= FUT_OFF_INODE;
263 key->shared.inode = page->mapping->host;
264 key->shared.pgoff = page->index;
265 }
266
267 get_futex_key_refs(key);
268
269 unlock_page(page);
270 put_page(page);
271 return 0;
272}
273
274static inline
275void put_futex_key(int fshared, union futex_key *key)
276{
277 drop_futex_key_refs(key);
278}
279
280static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval)
281{
282 u32 curval;
283
284 pagefault_disable();
285 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
286 pagefault_enable();
287
288 return curval;
289}
290
291static int get_futex_value_locked(u32 *dest, u32 __user *from)
292{
293 int ret;
294
295 pagefault_disable();
296 ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
297 pagefault_enable();
298
299 return ret ? -EFAULT : 0;
300}
301
302
303
304
305static int futex_handle_fault(unsigned long address, int attempt)
306{
307 struct vm_area_struct * vma;
308 struct mm_struct *mm = current->mm;
309 int ret = -EFAULT;
310
311 if (attempt > 2)
312 return ret;
313
314 down_read(&mm->mmap_sem);
315 vma = find_vma(mm, address);
316 if (vma && address >= vma->vm_start &&
317 (vma->vm_flags & VM_WRITE)) {
318 int fault;
319 fault = handle_mm_fault(mm, vma, address, 1);
320 if (unlikely((fault & VM_FAULT_ERROR))) {
321#if 0
322
323 if (ret & VM_FAULT_OOM)
324 ret = -ENOMEM;
325#endif
326 } else {
327 ret = 0;
328 if (fault & VM_FAULT_MAJOR)
329 current->maj_flt++;
330 else
331 current->min_flt++;
332 }
333 }
334 up_read(&mm->mmap_sem);
335 return ret;
336}
337
338
339
340
341static int refill_pi_state_cache(void)
342{
343 struct futex_pi_state *pi_state;
344
345 if (likely(current->pi_state_cache))
346 return 0;
347
348 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
349
350 if (!pi_state)
351 return -ENOMEM;
352
353 INIT_LIST_HEAD(&pi_state->list);
354
355 pi_state->owner = NULL;
356 atomic_set(&pi_state->refcount, 1);
357 pi_state->key = FUTEX_KEY_INIT;
358
359 current->pi_state_cache = pi_state;
360
361 return 0;
362}
363
364static struct futex_pi_state * alloc_pi_state(void)
365{
366 struct futex_pi_state *pi_state = current->pi_state_cache;
367
368 WARN_ON(!pi_state);
369 current->pi_state_cache = NULL;
370
371 return pi_state;
372}
373
374static void free_pi_state(struct futex_pi_state *pi_state)
375{
376 if (!atomic_dec_and_test(&pi_state->refcount))
377 return;
378
379
380
381
382
383 if (pi_state->owner) {
384 spin_lock_irq(&pi_state->owner->pi_lock);
385 list_del_init(&pi_state->list);
386 spin_unlock_irq(&pi_state->owner->pi_lock);
387
388 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
389 }
390
391 if (current->pi_state_cache)
392 kfree(pi_state);
393 else {
394
395
396
397
398
399 pi_state->owner = NULL;
400 atomic_set(&pi_state->refcount, 1);
401 current->pi_state_cache = pi_state;
402 }
403}
404
405
406
407
408
409static struct task_struct * futex_find_get_task(pid_t pid)
410{
411 struct task_struct *p;
412 const struct cred *cred = current_cred(), *pcred;
413
414 rcu_read_lock();
415 p = find_task_by_vpid(pid);
416 if (!p) {
417 p = ERR_PTR(-ESRCH);
418 } else {
419 pcred = __task_cred(p);
420 if (cred->euid != pcred->euid &&
421 cred->euid != pcred->uid)
422 p = ERR_PTR(-ESRCH);
423 else
424 get_task_struct(p);
425 }
426
427 rcu_read_unlock();
428
429 return p;
430}
431
432
433
434
435
436
437void exit_pi_state_list(struct task_struct *curr)
438{
439 struct list_head *next, *head = &curr->pi_state_list;
440 struct futex_pi_state *pi_state;
441 struct futex_hash_bucket *hb;
442 union futex_key key = FUTEX_KEY_INIT;
443
444 if (!futex_cmpxchg_enabled)
445 return;
446
447
448
449
450
451 spin_lock_irq(&curr->pi_lock);
452 while (!list_empty(head)) {
453
454 next = head->next;
455 pi_state = list_entry(next, struct futex_pi_state, list);
456 key = pi_state->key;
457 hb = hash_futex(&key);
458 spin_unlock_irq(&curr->pi_lock);
459
460 spin_lock(&hb->lock);
461
462 spin_lock_irq(&curr->pi_lock);
463
464
465
466
467 if (head->next != next) {
468 spin_unlock(&hb->lock);
469 continue;
470 }
471
472 WARN_ON(pi_state->owner != curr);
473 WARN_ON(list_empty(&pi_state->list));
474 list_del_init(&pi_state->list);
475 pi_state->owner = NULL;
476 spin_unlock_irq(&curr->pi_lock);
477
478 rt_mutex_unlock(&pi_state->pi_mutex);
479
480 spin_unlock(&hb->lock);
481
482 spin_lock_irq(&curr->pi_lock);
483 }
484 spin_unlock_irq(&curr->pi_lock);
485}
486
487static int
488lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
489 union futex_key *key, struct futex_pi_state **ps)
490{
491 struct futex_pi_state *pi_state = NULL;
492 struct futex_q *this, *next;
493 struct plist_head *head;
494 struct task_struct *p;
495 pid_t pid = uval & FUTEX_TID_MASK;
496
497 head = &hb->chain;
498
499 plist_for_each_entry_safe(this, next, head, list) {
500 if (match_futex(&this->key, key)) {
501
502
503
504
505 pi_state = this->pi_state;
506
507
508
509 if (unlikely(!pi_state))
510 return -EINVAL;
511
512 WARN_ON(!atomic_read(&pi_state->refcount));
513 WARN_ON(pid && pi_state->owner &&
514 pi_state->owner->pid != pid);
515
516 atomic_inc(&pi_state->refcount);
517 *ps = pi_state;
518
519 return 0;
520 }
521 }
522
523
524
525
526
527 if (!pid)
528 return -ESRCH;
529 p = futex_find_get_task(pid);
530 if (IS_ERR(p))
531 return PTR_ERR(p);
532
533
534
535
536
537
538
539 spin_lock_irq(&p->pi_lock);
540 if (unlikely(p->flags & PF_EXITING)) {
541
542
543
544
545
546 int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
547
548 spin_unlock_irq(&p->pi_lock);
549 put_task_struct(p);
550 return ret;
551 }
552
553 pi_state = alloc_pi_state();
554
555
556
557
558
559 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
560
561
562 pi_state->key = *key;
563
564 WARN_ON(!list_empty(&pi_state->list));
565 list_add(&pi_state->list, &p->pi_state_list);
566 pi_state->owner = p;
567 spin_unlock_irq(&p->pi_lock);
568
569 put_task_struct(p);
570
571 *ps = pi_state;
572
573 return 0;
574}
575
576
577
578
579
580static void wake_futex(struct futex_q *q)
581{
582 plist_del(&q->list, &q->list.plist);
583
584
585
586
587 wake_up(&q->waiter);
588
589
590
591
592
593
594
595
596
597 smp_wmb();
598 q->lock_ptr = NULL;
599}
600
601static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
602{
603 struct task_struct *new_owner;
604 struct futex_pi_state *pi_state = this->pi_state;
605 u32 curval, newval;
606
607 if (!pi_state)
608 return -EINVAL;
609
610 spin_lock(&pi_state->pi_mutex.wait_lock);
611 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
612
613
614
615
616
617
618
619 if (!new_owner)
620 new_owner = this->task;
621
622
623
624
625
626
627 if (!(uval & FUTEX_OWNER_DIED)) {
628 int ret = 0;
629
630 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
631
632 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
633
634 if (curval == -EFAULT)
635 ret = -EFAULT;
636 else if (curval != uval)
637 ret = -EINVAL;
638 if (ret) {
639 spin_unlock(&pi_state->pi_mutex.wait_lock);
640 return ret;
641 }
642 }
643
644 spin_lock_irq(&pi_state->owner->pi_lock);
645 WARN_ON(list_empty(&pi_state->list));
646 list_del_init(&pi_state->list);
647 spin_unlock_irq(&pi_state->owner->pi_lock);
648
649 spin_lock_irq(&new_owner->pi_lock);
650 WARN_ON(!list_empty(&pi_state->list));
651 list_add(&pi_state->list, &new_owner->pi_state_list);
652 pi_state->owner = new_owner;
653 spin_unlock_irq(&new_owner->pi_lock);
654
655 spin_unlock(&pi_state->pi_mutex.wait_lock);
656 rt_mutex_unlock(&pi_state->pi_mutex);
657
658 return 0;
659}
660
661static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
662{
663 u32 oldval;
664
665
666
667
668
669 oldval = cmpxchg_futex_value_locked(uaddr, uval, 0);
670
671 if (oldval == -EFAULT)
672 return oldval;
673 if (oldval != uval)
674 return -EAGAIN;
675
676 return 0;
677}
678
679
680
681
682static inline void
683double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
684{
685 if (hb1 <= hb2) {
686 spin_lock(&hb1->lock);
687 if (hb1 < hb2)
688 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
689 } else {
690 spin_lock(&hb2->lock);
691 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
692 }
693}
694
695
696
697
698
699static int futex_wake(u32 __user *uaddr, int fshared, int nr_wake, u32 bitset)
700{
701 struct futex_hash_bucket *hb;
702 struct futex_q *this, *next;
703 struct plist_head *head;
704 union futex_key key = FUTEX_KEY_INIT;
705 int ret;
706
707 if (!bitset)
708 return -EINVAL;
709
710 ret = get_futex_key(uaddr, fshared, &key);
711 if (unlikely(ret != 0))
712 goto out;
713
714 hb = hash_futex(&key);
715 spin_lock(&hb->lock);
716 head = &hb->chain;
717
718 plist_for_each_entry_safe(this, next, head, list) {
719 if (match_futex (&this->key, &key)) {
720 if (this->pi_state) {
721 ret = -EINVAL;
722 break;
723 }
724
725
726 if (!(this->bitset & bitset))
727 continue;
728
729 wake_futex(this);
730 if (++ret >= nr_wake)
731 break;
732 }
733 }
734
735 spin_unlock(&hb->lock);
736 put_futex_key(fshared, &key);
737out:
738 return ret;
739}
740
741
742
743
744
745static int
746futex_wake_op(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
747 int nr_wake, int nr_wake2, int op)
748{
749 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
750 struct futex_hash_bucket *hb1, *hb2;
751 struct plist_head *head;
752 struct futex_q *this, *next;
753 int ret, op_ret, attempt = 0;
754
755retryfull:
756 ret = get_futex_key(uaddr1, fshared, &key1);
757 if (unlikely(ret != 0))
758 goto out;
759 ret = get_futex_key(uaddr2, fshared, &key2);
760 if (unlikely(ret != 0))
761 goto out_put_key1;
762
763 hb1 = hash_futex(&key1);
764 hb2 = hash_futex(&key2);
765
766retry:
767 double_lock_hb(hb1, hb2);
768
769 op_ret = futex_atomic_op_inuser(op, uaddr2);
770 if (unlikely(op_ret < 0)) {
771 u32 dummy;
772
773 spin_unlock(&hb1->lock);
774 if (hb1 != hb2)
775 spin_unlock(&hb2->lock);
776
777#ifndef CONFIG_MMU
778
779
780
781
782 ret = op_ret;
783 goto out_put_keys;
784#endif
785
786 if (unlikely(op_ret != -EFAULT)) {
787 ret = op_ret;
788 goto out_put_keys;
789 }
790
791
792
793
794
795
796
797
798 if (attempt++) {
799 ret = futex_handle_fault((unsigned long)uaddr2,
800 attempt);
801 if (ret)
802 goto out_put_keys;
803 goto retry;
804 }
805
806 ret = get_user(dummy, uaddr2);
807 if (ret)
808 return ret;
809
810 goto retryfull;
811 }
812
813 head = &hb1->chain;
814
815 plist_for_each_entry_safe(this, next, head, list) {
816 if (match_futex (&this->key, &key1)) {
817 wake_futex(this);
818 if (++ret >= nr_wake)
819 break;
820 }
821 }
822
823 if (op_ret > 0) {
824 head = &hb2->chain;
825
826 op_ret = 0;
827 plist_for_each_entry_safe(this, next, head, list) {
828 if (match_futex (&this->key, &key2)) {
829 wake_futex(this);
830 if (++op_ret >= nr_wake2)
831 break;
832 }
833 }
834 ret += op_ret;
835 }
836
837 spin_unlock(&hb1->lock);
838 if (hb1 != hb2)
839 spin_unlock(&hb2->lock);
840out_put_keys:
841 put_futex_key(fshared, &key2);
842out_put_key1:
843 put_futex_key(fshared, &key1);
844out:
845 return ret;
846}
847
848
849
850
851
852static int futex_requeue(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
853 int nr_wake, int nr_requeue, u32 *cmpval)
854{
855 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
856 struct futex_hash_bucket *hb1, *hb2;
857 struct plist_head *head1;
858 struct futex_q *this, *next;
859 int ret, drop_count = 0;
860
861retry:
862 ret = get_futex_key(uaddr1, fshared, &key1);
863 if (unlikely(ret != 0))
864 goto out;
865 ret = get_futex_key(uaddr2, fshared, &key2);
866 if (unlikely(ret != 0))
867 goto out_put_key1;
868
869 hb1 = hash_futex(&key1);
870 hb2 = hash_futex(&key2);
871
872 double_lock_hb(hb1, hb2);
873
874 if (likely(cmpval != NULL)) {
875 u32 curval;
876
877 ret = get_futex_value_locked(&curval, uaddr1);
878
879 if (unlikely(ret)) {
880 spin_unlock(&hb1->lock);
881 if (hb1 != hb2)
882 spin_unlock(&hb2->lock);
883
884 ret = get_user(curval, uaddr1);
885
886 if (!ret)
887 goto retry;
888
889 goto out_put_keys;
890 }
891 if (curval != *cmpval) {
892 ret = -EAGAIN;
893 goto out_unlock;
894 }
895 }
896
897 head1 = &hb1->chain;
898 plist_for_each_entry_safe(this, next, head1, list) {
899 if (!match_futex (&this->key, &key1))
900 continue;
901 if (++ret <= nr_wake) {
902 wake_futex(this);
903 } else {
904
905
906
907
908 if (likely(head1 != &hb2->chain)) {
909 plist_del(&this->list, &hb1->chain);
910 plist_add(&this->list, &hb2->chain);
911 this->lock_ptr = &hb2->lock;
912#ifdef CONFIG_DEBUG_PI_LIST
913 this->list.plist.lock = &hb2->lock;
914#endif
915 }
916 this->key = key2;
917 get_futex_key_refs(&key2);
918 drop_count++;
919
920 if (ret - nr_wake >= nr_requeue)
921 break;
922 }
923 }
924
925out_unlock:
926 spin_unlock(&hb1->lock);
927 if (hb1 != hb2)
928 spin_unlock(&hb2->lock);
929
930
931 while (--drop_count >= 0)
932 drop_futex_key_refs(&key1);
933
934out_put_keys:
935 put_futex_key(fshared, &key2);
936out_put_key1:
937 put_futex_key(fshared, &key1);
938out:
939 return ret;
940}
941
942
943static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
944{
945 struct futex_hash_bucket *hb;
946
947 init_waitqueue_head(&q->waiter);
948
949 get_futex_key_refs(&q->key);
950 hb = hash_futex(&q->key);
951 q->lock_ptr = &hb->lock;
952
953 spin_lock(&hb->lock);
954 return hb;
955}
956
957static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
958{
959 int prio;
960
961
962
963
964
965
966
967
968
969 prio = min(current->normal_prio, MAX_RT_PRIO);
970
971 plist_node_init(&q->list, prio);
972#ifdef CONFIG_DEBUG_PI_LIST
973 q->list.plist.lock = &hb->lock;
974#endif
975 plist_add(&q->list, &hb->chain);
976 q->task = current;
977 spin_unlock(&hb->lock);
978}
979
980static inline void
981queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
982{
983 spin_unlock(&hb->lock);
984 drop_futex_key_refs(&q->key);
985}
986
987
988
989
990
991
992
993static int unqueue_me(struct futex_q *q)
994{
995 spinlock_t *lock_ptr;
996 int ret = 0;
997
998
999retry:
1000 lock_ptr = q->lock_ptr;
1001 barrier();
1002 if (lock_ptr != NULL) {
1003 spin_lock(lock_ptr);
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017 if (unlikely(lock_ptr != q->lock_ptr)) {
1018 spin_unlock(lock_ptr);
1019 goto retry;
1020 }
1021 WARN_ON(plist_node_empty(&q->list));
1022 plist_del(&q->list, &q->list.plist);
1023
1024 BUG_ON(q->pi_state);
1025
1026 spin_unlock(lock_ptr);
1027 ret = 1;
1028 }
1029
1030 drop_futex_key_refs(&q->key);
1031 return ret;
1032}
1033
1034
1035
1036
1037
1038
1039static void unqueue_me_pi(struct futex_q *q)
1040{
1041 WARN_ON(plist_node_empty(&q->list));
1042 plist_del(&q->list, &q->list.plist);
1043
1044 BUG_ON(!q->pi_state);
1045 free_pi_state(q->pi_state);
1046 q->pi_state = NULL;
1047
1048 spin_unlock(q->lock_ptr);
1049
1050 drop_futex_key_refs(&q->key);
1051}
1052
1053
1054
1055
1056
1057
1058
1059static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
1060 struct task_struct *newowner, int fshared)
1061{
1062 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
1063 struct futex_pi_state *pi_state = q->pi_state;
1064 struct task_struct *oldowner = pi_state->owner;
1065 u32 uval, curval, newval;
1066 int ret, attempt = 0;
1067
1068
1069 if (!pi_state->owner)
1070 newtid |= FUTEX_OWNER_DIED;
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091retry:
1092 if (get_futex_value_locked(&uval, uaddr))
1093 goto handle_fault;
1094
1095 while (1) {
1096 newval = (uval & FUTEX_OWNER_DIED) | newtid;
1097
1098 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
1099
1100 if (curval == -EFAULT)
1101 goto handle_fault;
1102 if (curval == uval)
1103 break;
1104 uval = curval;
1105 }
1106
1107
1108
1109
1110
1111 if (pi_state->owner != NULL) {
1112 spin_lock_irq(&pi_state->owner->pi_lock);
1113 WARN_ON(list_empty(&pi_state->list));
1114 list_del_init(&pi_state->list);
1115 spin_unlock_irq(&pi_state->owner->pi_lock);
1116 }
1117
1118 pi_state->owner = newowner;
1119
1120 spin_lock_irq(&newowner->pi_lock);
1121 WARN_ON(!list_empty(&pi_state->list));
1122 list_add(&pi_state->list, &newowner->pi_state_list);
1123 spin_unlock_irq(&newowner->pi_lock);
1124 return 0;
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136handle_fault:
1137 spin_unlock(q->lock_ptr);
1138
1139 ret = futex_handle_fault((unsigned long)uaddr, attempt++);
1140
1141 spin_lock(q->lock_ptr);
1142
1143
1144
1145
1146 if (pi_state->owner != oldowner)
1147 return 0;
1148
1149 if (ret)
1150 return ret;
1151
1152 goto retry;
1153}
1154
1155
1156
1157
1158
1159#define FLAGS_SHARED 0x01
1160#define FLAGS_CLOCKRT 0x02
1161
1162static long futex_wait_restart(struct restart_block *restart);
1163
1164static int futex_wait(u32 __user *uaddr, int fshared,
1165 u32 val, ktime_t *abs_time, u32 bitset, int clockrt)
1166{
1167 struct task_struct *curr = current;
1168 struct restart_block *restart;
1169 DECLARE_WAITQUEUE(wait, curr);
1170 struct futex_hash_bucket *hb;
1171 struct futex_q q;
1172 u32 uval;
1173 int ret;
1174 struct hrtimer_sleeper t;
1175 int rem = 0;
1176
1177 if (!bitset)
1178 return -EINVAL;
1179
1180 q.pi_state = NULL;
1181 q.bitset = bitset;
1182retry:
1183 q.key = FUTEX_KEY_INIT;
1184 ret = get_futex_key(uaddr, fshared, &q.key);
1185 if (unlikely(ret != 0))
1186 goto out;
1187
1188 hb = queue_lock(&q);
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210 ret = get_futex_value_locked(&uval, uaddr);
1211
1212 if (unlikely(ret)) {
1213 queue_unlock(&q, hb);
1214 put_futex_key(fshared, &q.key);
1215
1216 ret = get_user(uval, uaddr);
1217
1218 if (!ret)
1219 goto retry;
1220 goto out;
1221 }
1222 ret = -EWOULDBLOCK;
1223 if (unlikely(uval != val)) {
1224 queue_unlock(&q, hb);
1225 goto out_put_key;
1226 }
1227
1228
1229 queue_me(&q, hb);
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241 __set_current_state(TASK_INTERRUPTIBLE);
1242 add_wait_queue(&q.waiter, &wait);
1243
1244
1245
1246
1247 if (likely(!plist_node_empty(&q.list))) {
1248 if (!abs_time)
1249 schedule();
1250 else {
1251 unsigned long slack;
1252 slack = current->timer_slack_ns;
1253 if (rt_task(current))
1254 slack = 0;
1255 hrtimer_init_on_stack(&t.timer,
1256 clockrt ? CLOCK_REALTIME :
1257 CLOCK_MONOTONIC,
1258 HRTIMER_MODE_ABS);
1259 hrtimer_init_sleeper(&t, current);
1260 hrtimer_set_expires_range_ns(&t.timer, *abs_time, slack);
1261
1262 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
1263 if (!hrtimer_active(&t.timer))
1264 t.task = NULL;
1265
1266
1267
1268
1269
1270
1271 if (likely(t.task))
1272 schedule();
1273
1274 hrtimer_cancel(&t.timer);
1275
1276
1277 rem = (t.task == NULL);
1278
1279 destroy_hrtimer_on_stack(&t.timer);
1280 }
1281 }
1282 __set_current_state(TASK_RUNNING);
1283
1284
1285
1286
1287
1288
1289
1290 ret = 0;
1291 if (!unqueue_me(&q))
1292 goto out_put_key;
1293 ret = -ETIMEDOUT;
1294 if (rem)
1295 goto out_put_key;
1296
1297
1298
1299
1300
1301 ret = -ERESTARTSYS;
1302 if (!abs_time)
1303 goto out_put_key;
1304
1305 restart = ¤t_thread_info()->restart_block;
1306 restart->fn = futex_wait_restart;
1307 restart->futex.uaddr = (u32 *)uaddr;
1308 restart->futex.val = val;
1309 restart->futex.time = abs_time->tv64;
1310 restart->futex.bitset = bitset;
1311 restart->futex.flags = 0;
1312
1313 if (fshared)
1314 restart->futex.flags |= FLAGS_SHARED;
1315 if (clockrt)
1316 restart->futex.flags |= FLAGS_CLOCKRT;
1317
1318 ret = -ERESTART_RESTARTBLOCK;
1319
1320out_put_key:
1321 put_futex_key(fshared, &q.key);
1322out:
1323 return ret;
1324}
1325
1326
1327static long futex_wait_restart(struct restart_block *restart)
1328{
1329 u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
1330 int fshared = 0;
1331 ktime_t t;
1332
1333 t.tv64 = restart->futex.time;
1334 restart->fn = do_no_restart_syscall;
1335 if (restart->futex.flags & FLAGS_SHARED)
1336 fshared = 1;
1337 return (long)futex_wait(uaddr, fshared, restart->futex.val, &t,
1338 restart->futex.bitset,
1339 restart->futex.flags & FLAGS_CLOCKRT);
1340}
1341
1342
1343
1344
1345
1346
1347
1348
1349static int futex_lock_pi(u32 __user *uaddr, int fshared,
1350 int detect, ktime_t *time, int trylock)
1351{
1352 struct hrtimer_sleeper timeout, *to = NULL;
1353 struct task_struct *curr = current;
1354 struct futex_hash_bucket *hb;
1355 u32 uval, newval, curval;
1356 struct futex_q q;
1357 int ret, lock_taken, ownerdied = 0, attempt = 0;
1358
1359 if (refill_pi_state_cache())
1360 return -ENOMEM;
1361
1362 if (time) {
1363 to = &timeout;
1364 hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
1365 HRTIMER_MODE_ABS);
1366 hrtimer_init_sleeper(to, current);
1367 hrtimer_set_expires(&to->timer, *time);
1368 }
1369
1370 q.pi_state = NULL;
1371retry:
1372 q.key = FUTEX_KEY_INIT;
1373 ret = get_futex_key(uaddr, fshared, &q.key);
1374 if (unlikely(ret != 0))
1375 goto out;
1376
1377retry_unlocked:
1378 hb = queue_lock(&q);
1379
1380retry_locked:
1381 ret = lock_taken = 0;
1382
1383
1384
1385
1386
1387
1388 newval = task_pid_vnr(current);
1389
1390 curval = cmpxchg_futex_value_locked(uaddr, 0, newval);
1391
1392 if (unlikely(curval == -EFAULT))
1393 goto uaddr_faulted;
1394
1395
1396
1397
1398
1399 if (unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(current))) {
1400 ret = -EDEADLK;
1401 goto out_unlock_put_key;
1402 }
1403
1404
1405
1406
1407 if (unlikely(!curval))
1408 goto out_unlock_put_key;
1409
1410 uval = curval;
1411
1412
1413
1414
1415
1416 newval = curval | FUTEX_WAITERS;
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426 if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
1427
1428 newval = (curval & ~FUTEX_TID_MASK) | task_pid_vnr(current);
1429 ownerdied = 0;
1430 lock_taken = 1;
1431 }
1432
1433 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
1434
1435 if (unlikely(curval == -EFAULT))
1436 goto uaddr_faulted;
1437 if (unlikely(curval != uval))
1438 goto retry_locked;
1439
1440
1441
1442
1443 if (unlikely(lock_taken))
1444 goto out_unlock_put_key;
1445
1446
1447
1448
1449
1450 ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
1451
1452 if (unlikely(ret)) {
1453 switch (ret) {
1454
1455 case -EAGAIN:
1456
1457
1458
1459
1460 queue_unlock(&q, hb);
1461 cond_resched();
1462 goto retry;
1463
1464 case -ESRCH:
1465
1466
1467
1468
1469
1470 if (get_futex_value_locked(&curval, uaddr))
1471 goto uaddr_faulted;
1472
1473
1474
1475
1476
1477
1478 if (curval & FUTEX_OWNER_DIED) {
1479 ownerdied = 1;
1480 goto retry_locked;
1481 }
1482 default:
1483 goto out_unlock_put_key;
1484 }
1485 }
1486
1487
1488
1489
1490 queue_me(&q, hb);
1491
1492 WARN_ON(!q.pi_state);
1493
1494
1495
1496 if (!trylock)
1497 ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
1498 else {
1499 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
1500
1501 ret = ret ? 0 : -EWOULDBLOCK;
1502 }
1503
1504 spin_lock(q.lock_ptr);
1505
1506 if (!ret) {
1507
1508
1509
1510
1511
1512 if (q.pi_state->owner != curr)
1513 ret = fixup_pi_state_owner(uaddr, &q, curr, fshared);
1514 } else {
1515
1516
1517
1518
1519
1520 if (q.pi_state->owner == curr) {
1521
1522
1523
1524
1525
1526
1527 if (rt_mutex_trylock(&q.pi_state->pi_mutex))
1528 ret = 0;
1529 else {
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541 struct task_struct *owner;
1542 int res;
1543
1544 owner = rt_mutex_owner(&q.pi_state->pi_mutex);
1545 res = fixup_pi_state_owner(uaddr, &q, owner,
1546 fshared);
1547
1548
1549 if (res)
1550 ret = res;
1551 }
1552 } else {
1553
1554
1555
1556
1557
1558
1559 if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr)
1560 printk(KERN_ERR "futex_lock_pi: ret = %d "
1561 "pi-mutex: %p pi-state %p\n", ret,
1562 q.pi_state->pi_mutex.owner,
1563 q.pi_state->owner);
1564 }
1565 }
1566
1567
1568 unqueue_me_pi(&q);
1569
1570 if (to)
1571 destroy_hrtimer_on_stack(&to->timer);
1572 return ret != -EINTR ? ret : -ERESTARTNOINTR;
1573
1574out_unlock_put_key:
1575 queue_unlock(&q, hb);
1576
1577out_put_key:
1578 put_futex_key(fshared, &q.key);
1579out:
1580 if (to)
1581 destroy_hrtimer_on_stack(&to->timer);
1582 return ret;
1583
1584uaddr_faulted:
1585
1586
1587
1588
1589
1590
1591
1592 queue_unlock(&q, hb);
1593
1594 if (attempt++) {
1595 ret = futex_handle_fault((unsigned long)uaddr, attempt);
1596 if (ret)
1597 goto out_put_key;
1598 goto retry_unlocked;
1599 }
1600
1601 ret = get_user(uval, uaddr);
1602 if (!ret)
1603 goto retry;
1604
1605 if (to)
1606 destroy_hrtimer_on_stack(&to->timer);
1607 return ret;
1608}
1609
1610
1611
1612
1613
1614
1615static int futex_unlock_pi(u32 __user *uaddr, int fshared)
1616{
1617 struct futex_hash_bucket *hb;
1618 struct futex_q *this, *next;
1619 u32 uval;
1620 struct plist_head *head;
1621 union futex_key key = FUTEX_KEY_INIT;
1622 int ret, attempt = 0;
1623
1624retry:
1625 if (get_user(uval, uaddr))
1626 return -EFAULT;
1627
1628
1629
1630 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current))
1631 return -EPERM;
1632
1633 ret = get_futex_key(uaddr, fshared, &key);
1634 if (unlikely(ret != 0))
1635 goto out;
1636
1637 hb = hash_futex(&key);
1638retry_unlocked:
1639 spin_lock(&hb->lock);
1640
1641
1642
1643
1644
1645
1646 if (!(uval & FUTEX_OWNER_DIED))
1647 uval = cmpxchg_futex_value_locked(uaddr, task_pid_vnr(current), 0);
1648
1649
1650 if (unlikely(uval == -EFAULT))
1651 goto pi_faulted;
1652
1653
1654
1655
1656 if (unlikely(uval == task_pid_vnr(current)))
1657 goto out_unlock;
1658
1659
1660
1661
1662
1663 head = &hb->chain;
1664
1665 plist_for_each_entry_safe(this, next, head, list) {
1666 if (!match_futex (&this->key, &key))
1667 continue;
1668 ret = wake_futex_pi(uaddr, uval, this);
1669
1670
1671
1672
1673
1674 if (ret == -EFAULT)
1675 goto pi_faulted;
1676 goto out_unlock;
1677 }
1678
1679
1680
1681 if (!(uval & FUTEX_OWNER_DIED)) {
1682 ret = unlock_futex_pi(uaddr, uval);
1683 if (ret == -EFAULT)
1684 goto pi_faulted;
1685 }
1686
1687out_unlock:
1688 spin_unlock(&hb->lock);
1689 put_futex_key(fshared, &key);
1690
1691out:
1692 return ret;
1693
1694pi_faulted:
1695
1696
1697
1698
1699
1700
1701
1702 spin_unlock(&hb->lock);
1703
1704 if (attempt++) {
1705 ret = futex_handle_fault((unsigned long)uaddr, attempt);
1706 if (ret)
1707 goto out;
1708 uval = 0;
1709 goto retry_unlocked;
1710 }
1711
1712 ret = get_user(uval, uaddr);
1713 if (!ret)
1714 goto retry;
1715
1716 return ret;
1717}
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
1740 size_t, len)
1741{
1742 if (!futex_cmpxchg_enabled)
1743 return -ENOSYS;
1744
1745
1746
1747 if (unlikely(len != sizeof(*head)))
1748 return -EINVAL;
1749
1750 current->robust_list = head;
1751
1752 return 0;
1753}
1754
1755
1756
1757
1758
1759
1760
1761SYSCALL_DEFINE3(get_robust_list, int, pid,
1762 struct robust_list_head __user * __user *, head_ptr,
1763 size_t __user *, len_ptr)
1764{
1765 struct robust_list_head __user *head;
1766 unsigned long ret;
1767 const struct cred *cred = current_cred(), *pcred;
1768
1769 if (!futex_cmpxchg_enabled)
1770 return -ENOSYS;
1771
1772 if (!pid)
1773 head = current->robust_list;
1774 else {
1775 struct task_struct *p;
1776
1777 ret = -ESRCH;
1778 rcu_read_lock();
1779 p = find_task_by_vpid(pid);
1780 if (!p)
1781 goto err_unlock;
1782 ret = -EPERM;
1783 pcred = __task_cred(p);
1784 if (cred->euid != pcred->euid &&
1785 cred->euid != pcred->uid &&
1786 !capable(CAP_SYS_PTRACE))
1787 goto err_unlock;
1788 head = p->robust_list;
1789 rcu_read_unlock();
1790 }
1791
1792 if (put_user(sizeof(*head), len_ptr))
1793 return -EFAULT;
1794 return put_user(head, head_ptr);
1795
1796err_unlock:
1797 rcu_read_unlock();
1798
1799 return ret;
1800}
1801
1802
1803
1804
1805
1806int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
1807{
1808 u32 uval, nval, mval;
1809
1810retry:
1811 if (get_user(uval, uaddr))
1812 return -1;
1813
1814 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1826 nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
1827
1828 if (nval == -EFAULT)
1829 return -1;
1830
1831 if (nval != uval)
1832 goto retry;
1833
1834
1835
1836
1837
1838 if (!pi && (uval & FUTEX_WAITERS))
1839 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
1840 }
1841 return 0;
1842}
1843
1844
1845
1846
1847static inline int fetch_robust_entry(struct robust_list __user **entry,
1848 struct robust_list __user * __user *head,
1849 int *pi)
1850{
1851 unsigned long uentry;
1852
1853 if (get_user(uentry, (unsigned long __user *)head))
1854 return -EFAULT;
1855
1856 *entry = (void __user *)(uentry & ~1UL);
1857 *pi = uentry & 1;
1858
1859 return 0;
1860}
1861
1862
1863
1864
1865
1866
1867
1868void exit_robust_list(struct task_struct *curr)
1869{
1870 struct robust_list_head __user *head = curr->robust_list;
1871 struct robust_list __user *entry, *next_entry, *pending;
1872 unsigned int limit = ROBUST_LIST_LIMIT, pi, next_pi, pip;
1873 unsigned long futex_offset;
1874 int rc;
1875
1876 if (!futex_cmpxchg_enabled)
1877 return;
1878
1879
1880
1881
1882
1883 if (fetch_robust_entry(&entry, &head->list.next, &pi))
1884 return;
1885
1886
1887
1888 if (get_user(futex_offset, &head->futex_offset))
1889 return;
1890
1891
1892
1893
1894 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
1895 return;
1896
1897 next_entry = NULL;
1898 while (entry != &head->list) {
1899
1900
1901
1902
1903 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
1904
1905
1906
1907
1908 if (entry != pending)
1909 if (handle_futex_death((void __user *)entry + futex_offset,
1910 curr, pi))
1911 return;
1912 if (rc)
1913 return;
1914 entry = next_entry;
1915 pi = next_pi;
1916
1917
1918
1919 if (!--limit)
1920 break;
1921
1922 cond_resched();
1923 }
1924
1925 if (pending)
1926 handle_futex_death((void __user *)pending + futex_offset,
1927 curr, pip);
1928}
1929
1930long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
1931 u32 __user *uaddr2, u32 val2, u32 val3)
1932{
1933 int clockrt, ret = -ENOSYS;
1934 int cmd = op & FUTEX_CMD_MASK;
1935 int fshared = 0;
1936
1937 if (!(op & FUTEX_PRIVATE_FLAG))
1938 fshared = 1;
1939
1940 clockrt = op & FUTEX_CLOCK_REALTIME;
1941 if (clockrt && cmd != FUTEX_WAIT_BITSET)
1942 return -ENOSYS;
1943
1944 switch (cmd) {
1945 case FUTEX_WAIT:
1946 val3 = FUTEX_BITSET_MATCH_ANY;
1947 case FUTEX_WAIT_BITSET:
1948 ret = futex_wait(uaddr, fshared, val, timeout, val3, clockrt);
1949 break;
1950 case FUTEX_WAKE:
1951 val3 = FUTEX_BITSET_MATCH_ANY;
1952 case FUTEX_WAKE_BITSET:
1953 ret = futex_wake(uaddr, fshared, val, val3);
1954 break;
1955 case FUTEX_REQUEUE:
1956 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
1957 break;
1958 case FUTEX_CMP_REQUEUE:
1959 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
1960 break;
1961 case FUTEX_WAKE_OP:
1962 ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
1963 break;
1964 case FUTEX_LOCK_PI:
1965 if (futex_cmpxchg_enabled)
1966 ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
1967 break;
1968 case FUTEX_UNLOCK_PI:
1969 if (futex_cmpxchg_enabled)
1970 ret = futex_unlock_pi(uaddr, fshared);
1971 break;
1972 case FUTEX_TRYLOCK_PI:
1973 if (futex_cmpxchg_enabled)
1974 ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
1975 break;
1976 default:
1977 ret = -ENOSYS;
1978 }
1979 return ret;
1980}
1981
1982
1983SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
1984 struct timespec __user *, utime, u32 __user *, uaddr2,
1985 u32, val3)
1986{
1987 struct timespec ts;
1988 ktime_t t, *tp = NULL;
1989 u32 val2 = 0;
1990 int cmd = op & FUTEX_CMD_MASK;
1991
1992 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
1993 cmd == FUTEX_WAIT_BITSET)) {
1994 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
1995 return -EFAULT;
1996 if (!timespec_valid(&ts))
1997 return -EINVAL;
1998
1999 t = timespec_to_ktime(ts);
2000 if (cmd == FUTEX_WAIT)
2001 t = ktime_add_safe(ktime_get(), t);
2002 tp = &t;
2003 }
2004
2005
2006
2007
2008 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
2009 cmd == FUTEX_WAKE_OP)
2010 val2 = (u32) (unsigned long) utime;
2011
2012 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
2013}
2014
2015static int __init futex_init(void)
2016{
2017 u32 curval;
2018 int i;
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030 curval = cmpxchg_futex_value_locked(NULL, 0, 0);
2031 if (curval == -EFAULT)
2032 futex_cmpxchg_enabled = 1;
2033
2034 for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
2035 plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
2036 spin_lock_init(&futex_queues[i].lock);
2037 }
2038
2039 return 0;
2040}
2041__initcall(futex_init);
2042