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#include <linux/compat.h>
35#include <linux/jhash.h>
36#include <linux/pagemap.h>
37#include <linux/memblock.h>
38#include <linux/fault-inject.h>
39#include <linux/slab.h>
40
41#include "futex.h"
42#include "../locking/rtmutex_common.h"
43
44
45
46
47
48
49static struct {
50 struct futex_hash_bucket *queues;
51 unsigned long hashsize;
52} __futex_data __read_mostly __aligned(2*sizeof(long));
53#define futex_queues (__futex_data.queues)
54#define futex_hashsize (__futex_data.hashsize)
55
56
57
58
59
60#ifdef CONFIG_FAIL_FUTEX
61
62static struct {
63 struct fault_attr attr;
64
65 bool ignore_private;
66} fail_futex = {
67 .attr = FAULT_ATTR_INITIALIZER,
68 .ignore_private = false,
69};
70
71static int __init setup_fail_futex(char *str)
72{
73 return setup_fault_attr(&fail_futex.attr, str);
74}
75__setup("fail_futex=", setup_fail_futex);
76
77bool should_fail_futex(bool fshared)
78{
79 if (fail_futex.ignore_private && !fshared)
80 return false;
81
82 return should_fail(&fail_futex.attr, 1);
83}
84
85#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
86
87static int __init fail_futex_debugfs(void)
88{
89 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
90 struct dentry *dir;
91
92 dir = fault_create_debugfs_attr("fail_futex", NULL,
93 &fail_futex.attr);
94 if (IS_ERR(dir))
95 return PTR_ERR(dir);
96
97 debugfs_create_bool("ignore-private", mode, dir,
98 &fail_futex.ignore_private);
99 return 0;
100}
101
102late_initcall(fail_futex_debugfs);
103
104#endif
105
106#endif
107
108
109
110
111
112
113
114
115struct futex_hash_bucket *futex_hash(union futex_key *key)
116{
117 u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4,
118 key->both.offset);
119
120 return &futex_queues[hash & (futex_hashsize - 1)];
121}
122
123
124
125
126
127
128
129
130
131
132
133
134struct hrtimer_sleeper *
135futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout,
136 int flags, u64 range_ns)
137{
138 if (!time)
139 return NULL;
140
141 hrtimer_init_sleeper_on_stack(timeout, (flags & FLAGS_CLOCKRT) ?
142 CLOCK_REALTIME : CLOCK_MONOTONIC,
143 HRTIMER_MODE_ABS);
144
145
146
147
148 hrtimer_set_expires_range_ns(&timeout->timer, *time, range_ns);
149
150 return timeout;
151}
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171static u64 get_inode_sequence_number(struct inode *inode)
172{
173 static atomic64_t i_seq;
174 u64 old;
175
176
177 old = atomic64_read(&inode->i_sequence);
178 if (likely(old))
179 return old;
180
181 for (;;) {
182 u64 new = atomic64_add_return(1, &i_seq);
183 if (WARN_ON_ONCE(!new))
184 continue;
185
186 old = atomic64_cmpxchg_relaxed(&inode->i_sequence, 0, new);
187 if (old)
188 return old;
189 return new;
190 }
191}
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
221 enum futex_access rw)
222{
223 unsigned long address = (unsigned long)uaddr;
224 struct mm_struct *mm = current->mm;
225 struct page *page, *tail;
226 struct address_space *mapping;
227 int err, ro = 0;
228
229
230
231
232 key->both.offset = address % PAGE_SIZE;
233 if (unlikely((address % sizeof(u32)) != 0))
234 return -EINVAL;
235 address -= key->both.offset;
236
237 if (unlikely(!access_ok(uaddr, sizeof(u32))))
238 return -EFAULT;
239
240 if (unlikely(should_fail_futex(fshared)))
241 return -EFAULT;
242
243
244
245
246
247
248
249
250 if (!fshared) {
251 key->private.mm = mm;
252 key->private.address = address;
253 return 0;
254 }
255
256again:
257
258 if (unlikely(should_fail_futex(true)))
259 return -EFAULT;
260
261 err = get_user_pages_fast(address, 1, FOLL_WRITE, &page);
262
263
264
265
266 if (err == -EFAULT && rw == FUTEX_READ) {
267 err = get_user_pages_fast(address, 1, 0, &page);
268 ro = 1;
269 }
270 if (err < 0)
271 return err;
272 else
273 err = 0;
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 tail = page;
294 page = compound_head(page);
295 mapping = READ_ONCE(page->mapping);
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312 if (unlikely(!mapping)) {
313 int shmem_swizzled;
314
315
316
317
318
319
320 lock_page(page);
321 shmem_swizzled = PageSwapCache(page) || page->mapping;
322 unlock_page(page);
323 put_page(page);
324
325 if (shmem_swizzled)
326 goto again;
327
328 return -EFAULT;
329 }
330
331
332
333
334
335
336
337
338
339
340
341 if (PageAnon(page)) {
342
343
344
345
346 if (unlikely(should_fail_futex(true)) || ro) {
347 err = -EFAULT;
348 goto out;
349 }
350
351 key->both.offset |= FUT_OFF_MMSHARED;
352 key->private.mm = mm;
353 key->private.address = address;
354
355 } else {
356 struct inode *inode;
357
358
359
360
361
362
363
364
365
366
367
368
369 rcu_read_lock();
370
371 if (READ_ONCE(page->mapping) != mapping) {
372 rcu_read_unlock();
373 put_page(page);
374
375 goto again;
376 }
377
378 inode = READ_ONCE(mapping->host);
379 if (!inode) {
380 rcu_read_unlock();
381 put_page(page);
382
383 goto again;
384 }
385
386 key->both.offset |= FUT_OFF_INODE;
387 key->shared.i_seq = get_inode_sequence_number(inode);
388 key->shared.pgoff = page_to_pgoff(tail);
389 rcu_read_unlock();
390 }
391
392out:
393 put_page(page);
394 return err;
395}
396
397
398
399
400
401
402
403
404
405
406
407
408
409int fault_in_user_writeable(u32 __user *uaddr)
410{
411 struct mm_struct *mm = current->mm;
412 int ret;
413
414 mmap_read_lock(mm);
415 ret = fixup_user_fault(mm, (unsigned long)uaddr,
416 FAULT_FLAG_WRITE, NULL);
417 mmap_read_unlock(mm);
418
419 return ret < 0 ? ret : 0;
420}
421
422
423
424
425
426
427
428
429struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb, union futex_key *key)
430{
431 struct futex_q *this;
432
433 plist_for_each_entry(this, &hb->chain, list) {
434 if (futex_match(&this->key, key))
435 return this;
436 }
437 return NULL;
438}
439
440int futex_cmpxchg_value_locked(u32 *curval, u32 __user *uaddr, u32 uval, u32 newval)
441{
442 int ret;
443
444 pagefault_disable();
445 ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
446 pagefault_enable();
447
448 return ret;
449}
450
451int futex_get_value_locked(u32 *dest, u32 __user *from)
452{
453 int ret;
454
455 pagefault_disable();
456 ret = __get_user(*dest, from);
457 pagefault_enable();
458
459 return ret ? -EFAULT : 0;
460}
461
462
463
464
465
466
467
468
469void wait_for_owner_exiting(int ret, struct task_struct *exiting)
470{
471 if (ret != -EBUSY) {
472 WARN_ON_ONCE(exiting);
473 return;
474 }
475
476 if (WARN_ON_ONCE(ret == -EBUSY && !exiting))
477 return;
478
479 mutex_lock(&exiting->futex_exit_mutex);
480
481
482
483
484
485
486
487
488 mutex_unlock(&exiting->futex_exit_mutex);
489
490 put_task_struct(exiting);
491}
492
493
494
495
496
497
498
499void __futex_unqueue(struct futex_q *q)
500{
501 struct futex_hash_bucket *hb;
502
503 if (WARN_ON_SMP(!q->lock_ptr) || WARN_ON(plist_node_empty(&q->list)))
504 return;
505 lockdep_assert_held(q->lock_ptr);
506
507 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
508 plist_del(&q->list, &hb->chain);
509 futex_hb_waiters_dec(hb);
510}
511
512
513struct futex_hash_bucket *futex_q_lock(struct futex_q *q)
514 __acquires(&hb->lock)
515{
516 struct futex_hash_bucket *hb;
517
518 hb = futex_hash(&q->key);
519
520
521
522
523
524
525
526
527
528 futex_hb_waiters_inc(hb);
529
530 q->lock_ptr = &hb->lock;
531
532 spin_lock(&hb->lock);
533 return hb;
534}
535
536void futex_q_unlock(struct futex_hash_bucket *hb)
537 __releases(&hb->lock)
538{
539 spin_unlock(&hb->lock);
540 futex_hb_waiters_dec(hb);
541}
542
543void __futex_queue(struct futex_q *q, struct futex_hash_bucket *hb)
544{
545 int prio;
546
547
548
549
550
551
552
553
554
555 prio = min(current->normal_prio, MAX_RT_PRIO);
556
557 plist_node_init(&q->list, prio);
558 plist_add(&q->list, &hb->chain);
559 q->task = current;
560}
561
562
563
564
565
566
567
568
569
570
571
572
573int futex_unqueue(struct futex_q *q)
574{
575 spinlock_t *lock_ptr;
576 int ret = 0;
577
578
579retry:
580
581
582
583
584
585 lock_ptr = READ_ONCE(q->lock_ptr);
586 if (lock_ptr != NULL) {
587 spin_lock(lock_ptr);
588
589
590
591
592
593
594
595
596
597
598
599
600
601 if (unlikely(lock_ptr != q->lock_ptr)) {
602 spin_unlock(lock_ptr);
603 goto retry;
604 }
605 __futex_unqueue(q);
606
607 BUG_ON(q->pi_state);
608
609 spin_unlock(lock_ptr);
610 ret = 1;
611 }
612
613 return ret;
614}
615
616
617
618
619
620void futex_unqueue_pi(struct futex_q *q)
621{
622 __futex_unqueue(q);
623
624 BUG_ON(!q->pi_state);
625 put_pi_state(q->pi_state);
626 q->pi_state = NULL;
627}
628
629
630#define HANDLE_DEATH_PENDING true
631#define HANDLE_DEATH_LIST false
632
633
634
635
636
637static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr,
638 bool pi, bool pending_op)
639{
640 u32 uval, nval, mval;
641 int err;
642
643
644 if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
645 return -1;
646
647retry:
648 if (get_user(uval, uaddr))
649 return -1;
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682 if (pending_op && !pi && !uval) {
683 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
684 return 0;
685 }
686
687 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(curr))
688 return 0;
689
690
691
692
693
694
695
696
697
698
699
700 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
701
702
703
704
705
706
707
708
709
710
711 if ((err = futex_cmpxchg_value_locked(&nval, uaddr, uval, mval))) {
712 switch (err) {
713 case -EFAULT:
714 if (fault_in_user_writeable(uaddr))
715 return -1;
716 goto retry;
717
718 case -EAGAIN:
719 cond_resched();
720 goto retry;
721
722 default:
723 WARN_ON_ONCE(1);
724 return err;
725 }
726 }
727
728 if (nval != uval)
729 goto retry;
730
731
732
733
734
735 if (!pi && (uval & FUTEX_WAITERS))
736 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
737
738 return 0;
739}
740
741
742
743
744static inline int fetch_robust_entry(struct robust_list __user **entry,
745 struct robust_list __user * __user *head,
746 unsigned int *pi)
747{
748 unsigned long uentry;
749
750 if (get_user(uentry, (unsigned long __user *)head))
751 return -EFAULT;
752
753 *entry = (void __user *)(uentry & ~1UL);
754 *pi = uentry & 1;
755
756 return 0;
757}
758
759
760
761
762
763
764
765static void exit_robust_list(struct task_struct *curr)
766{
767 struct robust_list_head __user *head = curr->robust_list;
768 struct robust_list __user *entry, *next_entry, *pending;
769 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
770 unsigned int next_pi;
771 unsigned long futex_offset;
772 int rc;
773
774
775
776
777
778 if (fetch_robust_entry(&entry, &head->list.next, &pi))
779 return;
780
781
782
783 if (get_user(futex_offset, &head->futex_offset))
784 return;
785
786
787
788
789 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
790 return;
791
792 next_entry = NULL;
793 while (entry != &head->list) {
794
795
796
797
798 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
799
800
801
802
803 if (entry != pending) {
804 if (handle_futex_death((void __user *)entry + futex_offset,
805 curr, pi, HANDLE_DEATH_LIST))
806 return;
807 }
808 if (rc)
809 return;
810 entry = next_entry;
811 pi = next_pi;
812
813
814
815 if (!--limit)
816 break;
817
818 cond_resched();
819 }
820
821 if (pending) {
822 handle_futex_death((void __user *)pending + futex_offset,
823 curr, pip, HANDLE_DEATH_PENDING);
824 }
825}
826
827#ifdef CONFIG_COMPAT
828static void __user *futex_uaddr(struct robust_list __user *entry,
829 compat_long_t futex_offset)
830{
831 compat_uptr_t base = ptr_to_compat(entry);
832 void __user *uaddr = compat_ptr(base + futex_offset);
833
834 return uaddr;
835}
836
837
838
839
840static inline int
841compat_fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
842 compat_uptr_t __user *head, unsigned int *pi)
843{
844 if (get_user(*uentry, head))
845 return -EFAULT;
846
847 *entry = compat_ptr((*uentry) & ~1);
848 *pi = (unsigned int)(*uentry) & 1;
849
850 return 0;
851}
852
853
854
855
856
857
858
859static void compat_exit_robust_list(struct task_struct *curr)
860{
861 struct compat_robust_list_head __user *head = curr->compat_robust_list;
862 struct robust_list __user *entry, *next_entry, *pending;
863 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
864 unsigned int next_pi;
865 compat_uptr_t uentry, next_uentry, upending;
866 compat_long_t futex_offset;
867 int rc;
868
869
870
871
872
873 if (compat_fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
874 return;
875
876
877
878 if (get_user(futex_offset, &head->futex_offset))
879 return;
880
881
882
883
884 if (compat_fetch_robust_entry(&upending, &pending,
885 &head->list_op_pending, &pip))
886 return;
887
888 next_entry = NULL;
889 while (entry != (struct robust_list __user *) &head->list) {
890
891
892
893
894 rc = compat_fetch_robust_entry(&next_uentry, &next_entry,
895 (compat_uptr_t __user *)&entry->next, &next_pi);
896
897
898
899
900 if (entry != pending) {
901 void __user *uaddr = futex_uaddr(entry, futex_offset);
902
903 if (handle_futex_death(uaddr, curr, pi,
904 HANDLE_DEATH_LIST))
905 return;
906 }
907 if (rc)
908 return;
909 uentry = next_uentry;
910 entry = next_entry;
911 pi = next_pi;
912
913
914
915 if (!--limit)
916 break;
917
918 cond_resched();
919 }
920 if (pending) {
921 void __user *uaddr = futex_uaddr(pending, futex_offset);
922
923 handle_futex_death(uaddr, curr, pip, HANDLE_DEATH_PENDING);
924 }
925}
926#endif
927
928#ifdef CONFIG_FUTEX_PI
929
930
931
932
933
934
935static void exit_pi_state_list(struct task_struct *curr)
936{
937 struct list_head *next, *head = &curr->pi_state_list;
938 struct futex_pi_state *pi_state;
939 struct futex_hash_bucket *hb;
940 union futex_key key = FUTEX_KEY_INIT;
941
942
943
944
945
946
947 raw_spin_lock_irq(&curr->pi_lock);
948 while (!list_empty(head)) {
949 next = head->next;
950 pi_state = list_entry(next, struct futex_pi_state, list);
951 key = pi_state->key;
952 hb = futex_hash(&key);
953
954
955
956
957
958
959
960
961
962
963
964 if (!refcount_inc_not_zero(&pi_state->refcount)) {
965 raw_spin_unlock_irq(&curr->pi_lock);
966 cpu_relax();
967 raw_spin_lock_irq(&curr->pi_lock);
968 continue;
969 }
970 raw_spin_unlock_irq(&curr->pi_lock);
971
972 spin_lock(&hb->lock);
973 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
974 raw_spin_lock(&curr->pi_lock);
975
976
977
978
979 if (head->next != next) {
980
981 raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
982 spin_unlock(&hb->lock);
983 put_pi_state(pi_state);
984 continue;
985 }
986
987 WARN_ON(pi_state->owner != curr);
988 WARN_ON(list_empty(&pi_state->list));
989 list_del_init(&pi_state->list);
990 pi_state->owner = NULL;
991
992 raw_spin_unlock(&curr->pi_lock);
993 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
994 spin_unlock(&hb->lock);
995
996 rt_mutex_futex_unlock(&pi_state->pi_mutex);
997 put_pi_state(pi_state);
998
999 raw_spin_lock_irq(&curr->pi_lock);
1000 }
1001 raw_spin_unlock_irq(&curr->pi_lock);
1002}
1003#else
1004static inline void exit_pi_state_list(struct task_struct *curr) { }
1005#endif
1006
1007static void futex_cleanup(struct task_struct *tsk)
1008{
1009 if (unlikely(tsk->robust_list)) {
1010 exit_robust_list(tsk);
1011 tsk->robust_list = NULL;
1012 }
1013
1014#ifdef CONFIG_COMPAT
1015 if (unlikely(tsk->compat_robust_list)) {
1016 compat_exit_robust_list(tsk);
1017 tsk->compat_robust_list = NULL;
1018 }
1019#endif
1020
1021 if (unlikely(!list_empty(&tsk->pi_state_list)))
1022 exit_pi_state_list(tsk);
1023}
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042void futex_exit_recursive(struct task_struct *tsk)
1043{
1044
1045 if (tsk->futex_state == FUTEX_STATE_EXITING)
1046 mutex_unlock(&tsk->futex_exit_mutex);
1047 tsk->futex_state = FUTEX_STATE_DEAD;
1048}
1049
1050static void futex_cleanup_begin(struct task_struct *tsk)
1051{
1052
1053
1054
1055
1056
1057
1058 mutex_lock(&tsk->futex_exit_mutex);
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071 raw_spin_lock_irq(&tsk->pi_lock);
1072 tsk->futex_state = FUTEX_STATE_EXITING;
1073 raw_spin_unlock_irq(&tsk->pi_lock);
1074}
1075
1076static void futex_cleanup_end(struct task_struct *tsk, int state)
1077{
1078
1079
1080
1081
1082 tsk->futex_state = state;
1083
1084
1085
1086
1087 mutex_unlock(&tsk->futex_exit_mutex);
1088}
1089
1090void futex_exec_release(struct task_struct *tsk)
1091{
1092
1093
1094
1095
1096
1097
1098
1099 futex_cleanup_begin(tsk);
1100 futex_cleanup(tsk);
1101
1102
1103
1104
1105 futex_cleanup_end(tsk, FUTEX_STATE_OK);
1106}
1107
1108void futex_exit_release(struct task_struct *tsk)
1109{
1110 futex_cleanup_begin(tsk);
1111 futex_cleanup(tsk);
1112 futex_cleanup_end(tsk, FUTEX_STATE_DEAD);
1113}
1114
1115static int __init futex_init(void)
1116{
1117 unsigned int futex_shift;
1118 unsigned long i;
1119
1120#if CONFIG_BASE_SMALL
1121 futex_hashsize = 16;
1122#else
1123 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
1124#endif
1125
1126 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
1127 futex_hashsize, 0,
1128 futex_hashsize < 256 ? HASH_SMALL : 0,
1129 &futex_shift, NULL,
1130 futex_hashsize, futex_hashsize);
1131 futex_hashsize = 1UL << futex_shift;
1132
1133 for (i = 0; i < futex_hashsize; i++) {
1134 atomic_set(&futex_queues[i].waiters, 0);
1135 plist_head_init(&futex_queues[i].chain);
1136 spin_lock_init(&futex_queues[i].lock);
1137 }
1138
1139 return 0;
1140}
1141core_initcall(futex_init);
1142