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/syscalls.h>
38#include <linux/freezer.h>
39#include <linux/memblock.h>
40#include <linux/fault-inject.h>
41#include <linux/time_namespace.h>
42
43#include <asm/futex.h>
44
45#include "locking/rtmutex_common.h"
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147#ifdef CONFIG_HAVE_FUTEX_CMPXCHG
148#define futex_cmpxchg_enabled 1
149#else
150static int __read_mostly futex_cmpxchg_enabled;
151#endif
152
153
154
155
156
157#ifdef CONFIG_MMU
158# define FLAGS_SHARED 0x01
159#else
160
161
162
163
164# define FLAGS_SHARED 0x00
165#endif
166#define FLAGS_CLOCKRT 0x02
167#define FLAGS_HAS_TIMEOUT 0x04
168
169
170
171
172struct futex_pi_state {
173
174
175
176
177 struct list_head list;
178
179
180
181
182 struct rt_mutex pi_mutex;
183
184 struct task_struct *owner;
185 refcount_t refcount;
186
187 union futex_key key;
188} __randomize_layout;
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212struct futex_q {
213 struct plist_node list;
214
215 struct task_struct *task;
216 spinlock_t *lock_ptr;
217 union futex_key key;
218 struct futex_pi_state *pi_state;
219 struct rt_mutex_waiter *rt_waiter;
220 union futex_key *requeue_pi_key;
221 u32 bitset;
222} __randomize_layout;
223
224static const struct futex_q futex_q_init = {
225
226 .key = FUTEX_KEY_INIT,
227 .bitset = FUTEX_BITSET_MATCH_ANY
228};
229
230
231
232
233
234
235struct futex_hash_bucket {
236 atomic_t waiters;
237 spinlock_t lock;
238 struct plist_head chain;
239} ____cacheline_aligned_in_smp;
240
241
242
243
244
245
246static struct {
247 struct futex_hash_bucket *queues;
248 unsigned long hashsize;
249} __futex_data __read_mostly __aligned(2*sizeof(long));
250#define futex_queues (__futex_data.queues)
251#define futex_hashsize (__futex_data.hashsize)
252
253
254
255
256
257#ifdef CONFIG_FAIL_FUTEX
258
259static struct {
260 struct fault_attr attr;
261
262 bool ignore_private;
263} fail_futex = {
264 .attr = FAULT_ATTR_INITIALIZER,
265 .ignore_private = false,
266};
267
268static int __init setup_fail_futex(char *str)
269{
270 return setup_fault_attr(&fail_futex.attr, str);
271}
272__setup("fail_futex=", setup_fail_futex);
273
274static bool should_fail_futex(bool fshared)
275{
276 if (fail_futex.ignore_private && !fshared)
277 return false;
278
279 return should_fail(&fail_futex.attr, 1);
280}
281
282#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
283
284static int __init fail_futex_debugfs(void)
285{
286 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
287 struct dentry *dir;
288
289 dir = fault_create_debugfs_attr("fail_futex", NULL,
290 &fail_futex.attr);
291 if (IS_ERR(dir))
292 return PTR_ERR(dir);
293
294 debugfs_create_bool("ignore-private", mode, dir,
295 &fail_futex.ignore_private);
296 return 0;
297}
298
299late_initcall(fail_futex_debugfs);
300
301#endif
302
303#else
304static inline bool should_fail_futex(bool fshared)
305{
306 return false;
307}
308#endif
309
310#ifdef CONFIG_COMPAT
311static void compat_exit_robust_list(struct task_struct *curr);
312#endif
313
314
315
316
317static inline void hb_waiters_inc(struct futex_hash_bucket *hb)
318{
319#ifdef CONFIG_SMP
320 atomic_inc(&hb->waiters);
321
322
323
324 smp_mb__after_atomic();
325#endif
326}
327
328
329
330
331
332static inline void hb_waiters_dec(struct futex_hash_bucket *hb)
333{
334#ifdef CONFIG_SMP
335 atomic_dec(&hb->waiters);
336#endif
337}
338
339static inline int hb_waiters_pending(struct futex_hash_bucket *hb)
340{
341#ifdef CONFIG_SMP
342
343
344
345 smp_mb();
346 return atomic_read(&hb->waiters);
347#else
348 return 1;
349#endif
350}
351
352
353
354
355
356
357
358
359static struct futex_hash_bucket *hash_futex(union futex_key *key)
360{
361 u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4,
362 key->both.offset);
363
364 return &futex_queues[hash & (futex_hashsize - 1)];
365}
366
367
368
369
370
371
372
373
374
375static inline int match_futex(union futex_key *key1, union futex_key *key2)
376{
377 return (key1 && key2
378 && key1->both.word == key2->both.word
379 && key1->both.ptr == key2->both.ptr
380 && key1->both.offset == key2->both.offset);
381}
382
383enum futex_access {
384 FUTEX_READ,
385 FUTEX_WRITE
386};
387
388
389
390
391
392
393
394
395
396
397
398static inline struct hrtimer_sleeper *
399futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout,
400 int flags, u64 range_ns)
401{
402 if (!time)
403 return NULL;
404
405 hrtimer_init_sleeper_on_stack(timeout, (flags & FLAGS_CLOCKRT) ?
406 CLOCK_REALTIME : CLOCK_MONOTONIC,
407 HRTIMER_MODE_ABS);
408
409
410
411
412 hrtimer_set_expires_range_ns(&timeout->timer, *time, range_ns);
413
414 return timeout;
415}
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435static u64 get_inode_sequence_number(struct inode *inode)
436{
437 static atomic64_t i_seq;
438 u64 old;
439
440
441 old = atomic64_read(&inode->i_sequence);
442 if (likely(old))
443 return old;
444
445 for (;;) {
446 u64 new = atomic64_add_return(1, &i_seq);
447 if (WARN_ON_ONCE(!new))
448 continue;
449
450 old = atomic64_cmpxchg_relaxed(&inode->i_sequence, 0, new);
451 if (old)
452 return old;
453 return new;
454 }
455}
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484static int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
485 enum futex_access rw)
486{
487 unsigned long address = (unsigned long)uaddr;
488 struct mm_struct *mm = current->mm;
489 struct page *page, *tail;
490 struct address_space *mapping;
491 int err, ro = 0;
492
493
494
495
496 key->both.offset = address % PAGE_SIZE;
497 if (unlikely((address % sizeof(u32)) != 0))
498 return -EINVAL;
499 address -= key->both.offset;
500
501 if (unlikely(!access_ok(uaddr, sizeof(u32))))
502 return -EFAULT;
503
504 if (unlikely(should_fail_futex(fshared)))
505 return -EFAULT;
506
507
508
509
510
511
512
513
514 if (!fshared) {
515 key->private.mm = mm;
516 key->private.address = address;
517 return 0;
518 }
519
520again:
521
522 if (unlikely(should_fail_futex(true)))
523 return -EFAULT;
524
525 err = get_user_pages_fast(address, 1, FOLL_WRITE, &page);
526
527
528
529
530 if (err == -EFAULT && rw == FUTEX_READ) {
531 err = get_user_pages_fast(address, 1, 0, &page);
532 ro = 1;
533 }
534 if (err < 0)
535 return err;
536 else
537 err = 0;
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557 tail = page;
558 page = compound_head(page);
559 mapping = READ_ONCE(page->mapping);
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576 if (unlikely(!mapping)) {
577 int shmem_swizzled;
578
579
580
581
582
583
584 lock_page(page);
585 shmem_swizzled = PageSwapCache(page) || page->mapping;
586 unlock_page(page);
587 put_page(page);
588
589 if (shmem_swizzled)
590 goto again;
591
592 return -EFAULT;
593 }
594
595
596
597
598
599
600
601
602
603
604
605 if (PageAnon(page)) {
606
607
608
609
610 if (unlikely(should_fail_futex(true)) || ro) {
611 err = -EFAULT;
612 goto out;
613 }
614
615 key->both.offset |= FUT_OFF_MMSHARED;
616 key->private.mm = mm;
617 key->private.address = address;
618
619 } else {
620 struct inode *inode;
621
622
623
624
625
626
627
628
629
630
631
632
633 rcu_read_lock();
634
635 if (READ_ONCE(page->mapping) != mapping) {
636 rcu_read_unlock();
637 put_page(page);
638
639 goto again;
640 }
641
642 inode = READ_ONCE(mapping->host);
643 if (!inode) {
644 rcu_read_unlock();
645 put_page(page);
646
647 goto again;
648 }
649
650 key->both.offset |= FUT_OFF_INODE;
651 key->shared.i_seq = get_inode_sequence_number(inode);
652 key->shared.pgoff = page_to_pgoff(tail);
653 rcu_read_unlock();
654 }
655
656out:
657 put_page(page);
658 return err;
659}
660
661
662
663
664
665
666
667
668
669
670
671
672
673static int fault_in_user_writeable(u32 __user *uaddr)
674{
675 struct mm_struct *mm = current->mm;
676 int ret;
677
678 mmap_read_lock(mm);
679 ret = fixup_user_fault(mm, (unsigned long)uaddr,
680 FAULT_FLAG_WRITE, NULL);
681 mmap_read_unlock(mm);
682
683 return ret < 0 ? ret : 0;
684}
685
686
687
688
689
690
691
692
693static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
694 union futex_key *key)
695{
696 struct futex_q *this;
697
698 plist_for_each_entry(this, &hb->chain, list) {
699 if (match_futex(&this->key, key))
700 return this;
701 }
702 return NULL;
703}
704
705static int cmpxchg_futex_value_locked(u32 *curval, u32 __user *uaddr,
706 u32 uval, u32 newval)
707{
708 int ret;
709
710 pagefault_disable();
711 ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
712 pagefault_enable();
713
714 return ret;
715}
716
717static int get_futex_value_locked(u32 *dest, u32 __user *from)
718{
719 int ret;
720
721 pagefault_disable();
722 ret = __get_user(*dest, from);
723 pagefault_enable();
724
725 return ret ? -EFAULT : 0;
726}
727
728
729
730
731
732static int refill_pi_state_cache(void)
733{
734 struct futex_pi_state *pi_state;
735
736 if (likely(current->pi_state_cache))
737 return 0;
738
739 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
740
741 if (!pi_state)
742 return -ENOMEM;
743
744 INIT_LIST_HEAD(&pi_state->list);
745
746 pi_state->owner = NULL;
747 refcount_set(&pi_state->refcount, 1);
748 pi_state->key = FUTEX_KEY_INIT;
749
750 current->pi_state_cache = pi_state;
751
752 return 0;
753}
754
755static struct futex_pi_state *alloc_pi_state(void)
756{
757 struct futex_pi_state *pi_state = current->pi_state_cache;
758
759 WARN_ON(!pi_state);
760 current->pi_state_cache = NULL;
761
762 return pi_state;
763}
764
765static void pi_state_update_owner(struct futex_pi_state *pi_state,
766 struct task_struct *new_owner)
767{
768 struct task_struct *old_owner = pi_state->owner;
769
770 lockdep_assert_held(&pi_state->pi_mutex.wait_lock);
771
772 if (old_owner) {
773 raw_spin_lock(&old_owner->pi_lock);
774 WARN_ON(list_empty(&pi_state->list));
775 list_del_init(&pi_state->list);
776 raw_spin_unlock(&old_owner->pi_lock);
777 }
778
779 if (new_owner) {
780 raw_spin_lock(&new_owner->pi_lock);
781 WARN_ON(!list_empty(&pi_state->list));
782 list_add(&pi_state->list, &new_owner->pi_state_list);
783 pi_state->owner = new_owner;
784 raw_spin_unlock(&new_owner->pi_lock);
785 }
786}
787
788static void get_pi_state(struct futex_pi_state *pi_state)
789{
790 WARN_ON_ONCE(!refcount_inc_not_zero(&pi_state->refcount));
791}
792
793
794
795
796
797static void put_pi_state(struct futex_pi_state *pi_state)
798{
799 if (!pi_state)
800 return;
801
802 if (!refcount_dec_and_test(&pi_state->refcount))
803 return;
804
805
806
807
808
809 if (pi_state->owner) {
810 unsigned long flags;
811
812 raw_spin_lock_irqsave(&pi_state->pi_mutex.wait_lock, flags);
813 pi_state_update_owner(pi_state, NULL);
814 rt_mutex_proxy_unlock(&pi_state->pi_mutex);
815 raw_spin_unlock_irqrestore(&pi_state->pi_mutex.wait_lock, flags);
816 }
817
818 if (current->pi_state_cache) {
819 kfree(pi_state);
820 } else {
821
822
823
824
825
826 pi_state->owner = NULL;
827 refcount_set(&pi_state->refcount, 1);
828 current->pi_state_cache = pi_state;
829 }
830}
831
832#ifdef CONFIG_FUTEX_PI
833
834
835
836
837
838
839static void exit_pi_state_list(struct task_struct *curr)
840{
841 struct list_head *next, *head = &curr->pi_state_list;
842 struct futex_pi_state *pi_state;
843 struct futex_hash_bucket *hb;
844 union futex_key key = FUTEX_KEY_INIT;
845
846 if (!futex_cmpxchg_enabled)
847 return;
848
849
850
851
852
853 raw_spin_lock_irq(&curr->pi_lock);
854 while (!list_empty(head)) {
855 next = head->next;
856 pi_state = list_entry(next, struct futex_pi_state, list);
857 key = pi_state->key;
858 hb = hash_futex(&key);
859
860
861
862
863
864
865
866
867
868
869
870 if (!refcount_inc_not_zero(&pi_state->refcount)) {
871 raw_spin_unlock_irq(&curr->pi_lock);
872 cpu_relax();
873 raw_spin_lock_irq(&curr->pi_lock);
874 continue;
875 }
876 raw_spin_unlock_irq(&curr->pi_lock);
877
878 spin_lock(&hb->lock);
879 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
880 raw_spin_lock(&curr->pi_lock);
881
882
883
884
885 if (head->next != next) {
886
887 raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
888 spin_unlock(&hb->lock);
889 put_pi_state(pi_state);
890 continue;
891 }
892
893 WARN_ON(pi_state->owner != curr);
894 WARN_ON(list_empty(&pi_state->list));
895 list_del_init(&pi_state->list);
896 pi_state->owner = NULL;
897
898 raw_spin_unlock(&curr->pi_lock);
899 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
900 spin_unlock(&hb->lock);
901
902 rt_mutex_futex_unlock(&pi_state->pi_mutex);
903 put_pi_state(pi_state);
904
905 raw_spin_lock_irq(&curr->pi_lock);
906 }
907 raw_spin_unlock_irq(&curr->pi_lock);
908}
909#else
910static inline void exit_pi_state_list(struct task_struct *curr) { }
911#endif
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003static int attach_to_pi_state(u32 __user *uaddr, u32 uval,
1004 struct futex_pi_state *pi_state,
1005 struct futex_pi_state **ps)
1006{
1007 pid_t pid = uval & FUTEX_TID_MASK;
1008 u32 uval2;
1009 int ret;
1010
1011
1012
1013
1014 if (unlikely(!pi_state))
1015 return -EINVAL;
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029 WARN_ON(!refcount_read(&pi_state->refcount));
1030
1031
1032
1033
1034
1035 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
1036
1037
1038
1039
1040
1041
1042
1043 if (get_futex_value_locked(&uval2, uaddr))
1044 goto out_efault;
1045
1046 if (uval != uval2)
1047 goto out_eagain;
1048
1049
1050
1051
1052 if (uval & FUTEX_OWNER_DIED) {
1053
1054
1055
1056
1057
1058 if (!pi_state->owner) {
1059
1060
1061
1062
1063 if (pid)
1064 goto out_einval;
1065
1066
1067
1068 goto out_attach;
1069 }
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079 if (!pid)
1080 goto out_attach;
1081 } else {
1082
1083
1084
1085
1086 if (!pi_state->owner)
1087 goto out_einval;
1088 }
1089
1090
1091
1092
1093
1094
1095 if (pid != task_pid_vnr(pi_state->owner))
1096 goto out_einval;
1097
1098out_attach:
1099 get_pi_state(pi_state);
1100 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
1101 *ps = pi_state;
1102 return 0;
1103
1104out_einval:
1105 ret = -EINVAL;
1106 goto out_error;
1107
1108out_eagain:
1109 ret = -EAGAIN;
1110 goto out_error;
1111
1112out_efault:
1113 ret = -EFAULT;
1114 goto out_error;
1115
1116out_error:
1117 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
1118 return ret;
1119}
1120
1121
1122
1123
1124
1125
1126
1127
1128static void wait_for_owner_exiting(int ret, struct task_struct *exiting)
1129{
1130 if (ret != -EBUSY) {
1131 WARN_ON_ONCE(exiting);
1132 return;
1133 }
1134
1135 if (WARN_ON_ONCE(ret == -EBUSY && !exiting))
1136 return;
1137
1138 mutex_lock(&exiting->futex_exit_mutex);
1139
1140
1141
1142
1143
1144
1145
1146
1147 mutex_unlock(&exiting->futex_exit_mutex);
1148
1149 put_task_struct(exiting);
1150}
1151
1152static int handle_exit_race(u32 __user *uaddr, u32 uval,
1153 struct task_struct *tsk)
1154{
1155 u32 uval2;
1156
1157
1158
1159
1160
1161 if (tsk && tsk->futex_state != FUTEX_STATE_DEAD)
1162 return -EBUSY;
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193 if (get_futex_value_locked(&uval2, uaddr))
1194 return -EFAULT;
1195
1196
1197 if (uval2 != uval)
1198 return -EAGAIN;
1199
1200
1201
1202
1203
1204
1205 return -ESRCH;
1206}
1207
1208
1209
1210
1211
1212static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key,
1213 struct futex_pi_state **ps,
1214 struct task_struct **exiting)
1215{
1216 pid_t pid = uval & FUTEX_TID_MASK;
1217 struct futex_pi_state *pi_state;
1218 struct task_struct *p;
1219
1220
1221
1222
1223
1224
1225
1226
1227 if (!pid)
1228 return -EAGAIN;
1229 p = find_get_task_by_vpid(pid);
1230 if (!p)
1231 return handle_exit_race(uaddr, uval, NULL);
1232
1233 if (unlikely(p->flags & PF_KTHREAD)) {
1234 put_task_struct(p);
1235 return -EPERM;
1236 }
1237
1238
1239
1240
1241
1242
1243 raw_spin_lock_irq(&p->pi_lock);
1244 if (unlikely(p->futex_state != FUTEX_STATE_OK)) {
1245
1246
1247
1248
1249
1250 int ret = handle_exit_race(uaddr, uval, p);
1251
1252 raw_spin_unlock_irq(&p->pi_lock);
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262 if (ret == -EBUSY)
1263 *exiting = p;
1264 else
1265 put_task_struct(p);
1266 return ret;
1267 }
1268
1269
1270
1271
1272
1273
1274
1275 pi_state = alloc_pi_state();
1276
1277
1278
1279
1280
1281 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
1282
1283
1284 pi_state->key = *key;
1285
1286 WARN_ON(!list_empty(&pi_state->list));
1287 list_add(&pi_state->list, &p->pi_state_list);
1288
1289
1290
1291
1292 pi_state->owner = p;
1293 raw_spin_unlock_irq(&p->pi_lock);
1294
1295 put_task_struct(p);
1296
1297 *ps = pi_state;
1298
1299 return 0;
1300}
1301
1302static int lookup_pi_state(u32 __user *uaddr, u32 uval,
1303 struct futex_hash_bucket *hb,
1304 union futex_key *key, struct futex_pi_state **ps,
1305 struct task_struct **exiting)
1306{
1307 struct futex_q *top_waiter = futex_top_waiter(hb, key);
1308
1309
1310
1311
1312
1313 if (top_waiter)
1314 return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
1315
1316
1317
1318
1319
1320 return attach_to_pi_owner(uaddr, uval, key, ps, exiting);
1321}
1322
1323static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
1324{
1325 int err;
1326 u32 curval;
1327
1328 if (unlikely(should_fail_futex(true)))
1329 return -EFAULT;
1330
1331 err = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
1332 if (unlikely(err))
1333 return err;
1334
1335
1336 return curval != uval ? -EAGAIN : 0;
1337}
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
1364 union futex_key *key,
1365 struct futex_pi_state **ps,
1366 struct task_struct *task,
1367 struct task_struct **exiting,
1368 int set_waiters)
1369{
1370 u32 uval, newval, vpid = task_pid_vnr(task);
1371 struct futex_q *top_waiter;
1372 int ret;
1373
1374
1375
1376
1377
1378 if (get_futex_value_locked(&uval, uaddr))
1379 return -EFAULT;
1380
1381 if (unlikely(should_fail_futex(true)))
1382 return -EFAULT;
1383
1384
1385
1386
1387 if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
1388 return -EDEADLK;
1389
1390 if ((unlikely(should_fail_futex(true))))
1391 return -EDEADLK;
1392
1393
1394
1395
1396
1397 top_waiter = futex_top_waiter(hb, key);
1398 if (top_waiter)
1399 return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
1400
1401
1402
1403
1404
1405
1406
1407 if (!(uval & FUTEX_TID_MASK)) {
1408
1409
1410
1411
1412 newval = uval & FUTEX_OWNER_DIED;
1413 newval |= vpid;
1414
1415
1416 if (set_waiters)
1417 newval |= FUTEX_WAITERS;
1418
1419 ret = lock_pi_update_atomic(uaddr, uval, newval);
1420
1421 return ret < 0 ? ret : 1;
1422 }
1423
1424
1425
1426
1427
1428
1429 newval = uval | FUTEX_WAITERS;
1430 ret = lock_pi_update_atomic(uaddr, uval, newval);
1431 if (ret)
1432 return ret;
1433
1434
1435
1436
1437
1438 return attach_to_pi_owner(uaddr, newval, key, ps, exiting);
1439}
1440
1441
1442
1443
1444
1445
1446
1447static void __unqueue_futex(struct futex_q *q)
1448{
1449 struct futex_hash_bucket *hb;
1450
1451 if (WARN_ON_SMP(!q->lock_ptr) || WARN_ON(plist_node_empty(&q->list)))
1452 return;
1453 lockdep_assert_held(q->lock_ptr);
1454
1455 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
1456 plist_del(&q->list, &hb->chain);
1457 hb_waiters_dec(hb);
1458}
1459
1460
1461
1462
1463
1464
1465
1466static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
1467{
1468 struct task_struct *p = q->task;
1469
1470 if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
1471 return;
1472
1473 get_task_struct(p);
1474 __unqueue_futex(q);
1475
1476
1477
1478
1479
1480
1481
1482 smp_store_release(&q->lock_ptr, NULL);
1483
1484
1485
1486
1487
1488 wake_q_add_safe(wake_q, p);
1489}
1490
1491
1492
1493
1494static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_pi_state *pi_state)
1495{
1496 u32 curval, newval;
1497 struct rt_mutex_waiter *top_waiter;
1498 struct task_struct *new_owner;
1499 bool postunlock = false;
1500 DEFINE_WAKE_Q(wake_q);
1501 int ret = 0;
1502
1503 top_waiter = rt_mutex_top_waiter(&pi_state->pi_mutex);
1504 if (WARN_ON_ONCE(!top_waiter)) {
1505
1506
1507
1508
1509
1510
1511
1512
1513 ret = -EAGAIN;
1514 goto out_unlock;
1515 }
1516
1517 new_owner = top_waiter->task;
1518
1519
1520
1521
1522
1523
1524 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
1525
1526 if (unlikely(should_fail_futex(true))) {
1527 ret = -EFAULT;
1528 goto out_unlock;
1529 }
1530
1531 ret = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
1532 if (!ret && (curval != uval)) {
1533
1534
1535
1536
1537
1538
1539 if ((FUTEX_TID_MASK & curval) == uval)
1540 ret = -EAGAIN;
1541 else
1542 ret = -EINVAL;
1543 }
1544
1545 if (!ret) {
1546
1547
1548
1549
1550
1551 pi_state_update_owner(pi_state, new_owner);
1552 postunlock = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1553 }
1554
1555out_unlock:
1556 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
1557
1558 if (postunlock)
1559 rt_mutex_postunlock(&wake_q);
1560
1561 return ret;
1562}
1563
1564
1565
1566
1567static inline void
1568double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1569{
1570 if (hb1 <= hb2) {
1571 spin_lock(&hb1->lock);
1572 if (hb1 < hb2)
1573 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
1574 } else {
1575 spin_lock(&hb2->lock);
1576 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
1577 }
1578}
1579
1580static inline void
1581double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1582{
1583 spin_unlock(&hb1->lock);
1584 if (hb1 != hb2)
1585 spin_unlock(&hb2->lock);
1586}
1587
1588
1589
1590
1591static int
1592futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
1593{
1594 struct futex_hash_bucket *hb;
1595 struct futex_q *this, *next;
1596 union futex_key key = FUTEX_KEY_INIT;
1597 int ret;
1598 DEFINE_WAKE_Q(wake_q);
1599
1600 if (!bitset)
1601 return -EINVAL;
1602
1603 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_READ);
1604 if (unlikely(ret != 0))
1605 return ret;
1606
1607 hb = hash_futex(&key);
1608
1609
1610 if (!hb_waiters_pending(hb))
1611 return ret;
1612
1613 spin_lock(&hb->lock);
1614
1615 plist_for_each_entry_safe(this, next, &hb->chain, list) {
1616 if (match_futex (&this->key, &key)) {
1617 if (this->pi_state || this->rt_waiter) {
1618 ret = -EINVAL;
1619 break;
1620 }
1621
1622
1623 if (!(this->bitset & bitset))
1624 continue;
1625
1626 mark_wake_futex(&wake_q, this);
1627 if (++ret >= nr_wake)
1628 break;
1629 }
1630 }
1631
1632 spin_unlock(&hb->lock);
1633 wake_up_q(&wake_q);
1634 return ret;
1635}
1636
1637static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
1638{
1639 unsigned int op = (encoded_op & 0x70000000) >> 28;
1640 unsigned int cmp = (encoded_op & 0x0f000000) >> 24;
1641 int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
1642 int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
1643 int oldval, ret;
1644
1645 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
1646 if (oparg < 0 || oparg > 31) {
1647 char comm[sizeof(current->comm)];
1648
1649
1650
1651
1652 pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
1653 get_task_comm(comm, current), oparg);
1654 oparg &= 31;
1655 }
1656 oparg = 1 << oparg;
1657 }
1658
1659 pagefault_disable();
1660 ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
1661 pagefault_enable();
1662 if (ret)
1663 return ret;
1664
1665 switch (cmp) {
1666 case FUTEX_OP_CMP_EQ:
1667 return oldval == cmparg;
1668 case FUTEX_OP_CMP_NE:
1669 return oldval != cmparg;
1670 case FUTEX_OP_CMP_LT:
1671 return oldval < cmparg;
1672 case FUTEX_OP_CMP_GE:
1673 return oldval >= cmparg;
1674 case FUTEX_OP_CMP_LE:
1675 return oldval <= cmparg;
1676 case FUTEX_OP_CMP_GT:
1677 return oldval > cmparg;
1678 default:
1679 return -ENOSYS;
1680 }
1681}
1682
1683
1684
1685
1686
1687static int
1688futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
1689 int nr_wake, int nr_wake2, int op)
1690{
1691 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
1692 struct futex_hash_bucket *hb1, *hb2;
1693 struct futex_q *this, *next;
1694 int ret, op_ret;
1695 DEFINE_WAKE_Q(wake_q);
1696
1697retry:
1698 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, FUTEX_READ);
1699 if (unlikely(ret != 0))
1700 return ret;
1701 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, FUTEX_WRITE);
1702 if (unlikely(ret != 0))
1703 return ret;
1704
1705 hb1 = hash_futex(&key1);
1706 hb2 = hash_futex(&key2);
1707
1708retry_private:
1709 double_lock_hb(hb1, hb2);
1710 op_ret = futex_atomic_op_inuser(op, uaddr2);
1711 if (unlikely(op_ret < 0)) {
1712 double_unlock_hb(hb1, hb2);
1713
1714 if (!IS_ENABLED(CONFIG_MMU) ||
1715 unlikely(op_ret != -EFAULT && op_ret != -EAGAIN)) {
1716
1717
1718
1719
1720 ret = op_ret;
1721 return ret;
1722 }
1723
1724 if (op_ret == -EFAULT) {
1725 ret = fault_in_user_writeable(uaddr2);
1726 if (ret)
1727 return ret;
1728 }
1729
1730 cond_resched();
1731 if (!(flags & FLAGS_SHARED))
1732 goto retry_private;
1733 goto retry;
1734 }
1735
1736 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
1737 if (match_futex (&this->key, &key1)) {
1738 if (this->pi_state || this->rt_waiter) {
1739 ret = -EINVAL;
1740 goto out_unlock;
1741 }
1742 mark_wake_futex(&wake_q, this);
1743 if (++ret >= nr_wake)
1744 break;
1745 }
1746 }
1747
1748 if (op_ret > 0) {
1749 op_ret = 0;
1750 plist_for_each_entry_safe(this, next, &hb2->chain, list) {
1751 if (match_futex (&this->key, &key2)) {
1752 if (this->pi_state || this->rt_waiter) {
1753 ret = -EINVAL;
1754 goto out_unlock;
1755 }
1756 mark_wake_futex(&wake_q, this);
1757 if (++op_ret >= nr_wake2)
1758 break;
1759 }
1760 }
1761 ret += op_ret;
1762 }
1763
1764out_unlock:
1765 double_unlock_hb(hb1, hb2);
1766 wake_up_q(&wake_q);
1767 return ret;
1768}
1769
1770
1771
1772
1773
1774
1775
1776
1777static inline
1778void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
1779 struct futex_hash_bucket *hb2, union futex_key *key2)
1780{
1781
1782
1783
1784
1785
1786 if (likely(&hb1->chain != &hb2->chain)) {
1787 plist_del(&q->list, &hb1->chain);
1788 hb_waiters_dec(hb1);
1789 hb_waiters_inc(hb2);
1790 plist_add(&q->list, &hb2->chain);
1791 q->lock_ptr = &hb2->lock;
1792 }
1793 q->key = *key2;
1794}
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810static inline
1811void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
1812 struct futex_hash_bucket *hb)
1813{
1814 q->key = *key;
1815
1816 __unqueue_futex(q);
1817
1818 WARN_ON(!q->rt_waiter);
1819 q->rt_waiter = NULL;
1820
1821 q->lock_ptr = &hb->lock;
1822
1823 wake_up_state(q->task, TASK_NORMAL);
1824}
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852static int
1853futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1,
1854 struct futex_hash_bucket *hb2, union futex_key *key1,
1855 union futex_key *key2, struct futex_pi_state **ps,
1856 struct task_struct **exiting, int set_waiters)
1857{
1858 struct futex_q *top_waiter = NULL;
1859 u32 curval;
1860 int ret, vpid;
1861
1862 if (get_futex_value_locked(&curval, pifutex))
1863 return -EFAULT;
1864
1865 if (unlikely(should_fail_futex(true)))
1866 return -EFAULT;
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876 top_waiter = futex_top_waiter(hb1, key1);
1877
1878
1879 if (!top_waiter)
1880 return 0;
1881
1882
1883 if (!match_futex(top_waiter->requeue_pi_key, key2))
1884 return -EINVAL;
1885
1886
1887
1888
1889
1890
1891 vpid = task_pid_vnr(top_waiter->task);
1892 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
1893 exiting, set_waiters);
1894 if (ret == 1) {
1895 requeue_pi_wake_futex(top_waiter, key2, hb2);
1896 return vpid;
1897 }
1898 return ret;
1899}
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
1920 u32 __user *uaddr2, int nr_wake, int nr_requeue,
1921 u32 *cmpval, int requeue_pi)
1922{
1923 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
1924 int task_count = 0, ret;
1925 struct futex_pi_state *pi_state = NULL;
1926 struct futex_hash_bucket *hb1, *hb2;
1927 struct futex_q *this, *next;
1928 DEFINE_WAKE_Q(wake_q);
1929
1930 if (nr_wake < 0 || nr_requeue < 0)
1931 return -EINVAL;
1932
1933
1934
1935
1936
1937
1938
1939 if (!IS_ENABLED(CONFIG_FUTEX_PI) && requeue_pi)
1940 return -ENOSYS;
1941
1942 if (requeue_pi) {
1943
1944
1945
1946
1947 if (uaddr1 == uaddr2)
1948 return -EINVAL;
1949
1950
1951
1952
1953
1954 if (refill_pi_state_cache())
1955 return -ENOMEM;
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966 if (nr_wake != 1)
1967 return -EINVAL;
1968 }
1969
1970retry:
1971 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, FUTEX_READ);
1972 if (unlikely(ret != 0))
1973 return ret;
1974 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
1975 requeue_pi ? FUTEX_WRITE : FUTEX_READ);
1976 if (unlikely(ret != 0))
1977 return ret;
1978
1979
1980
1981
1982
1983 if (requeue_pi && match_futex(&key1, &key2))
1984 return -EINVAL;
1985
1986 hb1 = hash_futex(&key1);
1987 hb2 = hash_futex(&key2);
1988
1989retry_private:
1990 hb_waiters_inc(hb2);
1991 double_lock_hb(hb1, hb2);
1992
1993 if (likely(cmpval != NULL)) {
1994 u32 curval;
1995
1996 ret = get_futex_value_locked(&curval, uaddr1);
1997
1998 if (unlikely(ret)) {
1999 double_unlock_hb(hb1, hb2);
2000 hb_waiters_dec(hb2);
2001
2002 ret = get_user(curval, uaddr1);
2003 if (ret)
2004 return ret;
2005
2006 if (!(flags & FLAGS_SHARED))
2007 goto retry_private;
2008
2009 goto retry;
2010 }
2011 if (curval != *cmpval) {
2012 ret = -EAGAIN;
2013 goto out_unlock;
2014 }
2015 }
2016
2017 if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
2018 struct task_struct *exiting = NULL;
2019
2020
2021
2022
2023
2024
2025
2026 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
2027 &key2, &pi_state,
2028 &exiting, nr_requeue);
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039 if (ret > 0) {
2040 WARN_ON(pi_state);
2041 task_count++;
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054 ret = lookup_pi_state(uaddr2, ret, hb2, &key2,
2055 &pi_state, &exiting);
2056 }
2057
2058 switch (ret) {
2059 case 0:
2060
2061 break;
2062
2063
2064 case -EFAULT:
2065 double_unlock_hb(hb1, hb2);
2066 hb_waiters_dec(hb2);
2067 ret = fault_in_user_writeable(uaddr2);
2068 if (!ret)
2069 goto retry;
2070 return ret;
2071 case -EBUSY:
2072 case -EAGAIN:
2073
2074
2075
2076
2077
2078
2079 double_unlock_hb(hb1, hb2);
2080 hb_waiters_dec(hb2);
2081
2082
2083
2084
2085
2086 wait_for_owner_exiting(ret, exiting);
2087 cond_resched();
2088 goto retry;
2089 default:
2090 goto out_unlock;
2091 }
2092 }
2093
2094 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
2095 if (task_count - nr_wake >= nr_requeue)
2096 break;
2097
2098 if (!match_futex(&this->key, &key1))
2099 continue;
2100
2101
2102
2103
2104
2105
2106
2107
2108 if ((requeue_pi && !this->rt_waiter) ||
2109 (!requeue_pi && this->rt_waiter) ||
2110 this->pi_state) {
2111 ret = -EINVAL;
2112 break;
2113 }
2114
2115
2116
2117
2118
2119
2120 if (++task_count <= nr_wake && !requeue_pi) {
2121 mark_wake_futex(&wake_q, this);
2122 continue;
2123 }
2124
2125
2126 if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
2127 ret = -EINVAL;
2128 break;
2129 }
2130
2131
2132
2133
2134
2135 if (requeue_pi) {
2136
2137
2138
2139
2140
2141 get_pi_state(pi_state);
2142 this->pi_state = pi_state;
2143 ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
2144 this->rt_waiter,
2145 this->task);
2146 if (ret == 1) {
2147
2148
2149
2150
2151
2152
2153
2154
2155 requeue_pi_wake_futex(this, &key2, hb2);
2156 continue;
2157 } else if (ret) {
2158
2159
2160
2161
2162
2163
2164
2165
2166 this->pi_state = NULL;
2167 put_pi_state(pi_state);
2168
2169
2170
2171
2172 break;
2173 }
2174 }
2175 requeue_futex(this, hb1, hb2, &key2);
2176 }
2177
2178
2179
2180
2181
2182
2183 put_pi_state(pi_state);
2184
2185out_unlock:
2186 double_unlock_hb(hb1, hb2);
2187 wake_up_q(&wake_q);
2188 hb_waiters_dec(hb2);
2189 return ret ? ret : task_count;
2190}
2191
2192
2193static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
2194 __acquires(&hb->lock)
2195{
2196 struct futex_hash_bucket *hb;
2197
2198 hb = hash_futex(&q->key);
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208 hb_waiters_inc(hb);
2209
2210 q->lock_ptr = &hb->lock;
2211
2212 spin_lock(&hb->lock);
2213 return hb;
2214}
2215
2216static inline void
2217queue_unlock(struct futex_hash_bucket *hb)
2218 __releases(&hb->lock)
2219{
2220 spin_unlock(&hb->lock);
2221 hb_waiters_dec(hb);
2222}
2223
2224static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
2225{
2226 int prio;
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236 prio = min(current->normal_prio, MAX_RT_PRIO);
2237
2238 plist_node_init(&q->list, prio);
2239 plist_add(&q->list, &hb->chain);
2240 q->task = current;
2241}
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
2256 __releases(&hb->lock)
2257{
2258 __queue_me(q, hb);
2259 spin_unlock(&hb->lock);
2260}
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273static int unqueue_me(struct futex_q *q)
2274{
2275 spinlock_t *lock_ptr;
2276 int ret = 0;
2277
2278
2279retry:
2280
2281
2282
2283
2284
2285 lock_ptr = READ_ONCE(q->lock_ptr);
2286 if (lock_ptr != NULL) {
2287 spin_lock(lock_ptr);
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301 if (unlikely(lock_ptr != q->lock_ptr)) {
2302 spin_unlock(lock_ptr);
2303 goto retry;
2304 }
2305 __unqueue_futex(q);
2306
2307 BUG_ON(q->pi_state);
2308
2309 spin_unlock(lock_ptr);
2310 ret = 1;
2311 }
2312
2313 return ret;
2314}
2315
2316
2317
2318
2319
2320static void unqueue_me_pi(struct futex_q *q)
2321{
2322 __unqueue_futex(q);
2323
2324 BUG_ON(!q->pi_state);
2325 put_pi_state(q->pi_state);
2326 q->pi_state = NULL;
2327}
2328
2329static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
2330 struct task_struct *argowner)
2331{
2332 struct futex_pi_state *pi_state = q->pi_state;
2333 struct task_struct *oldowner, *newowner;
2334 u32 uval, curval, newval, newtid;
2335 int err = 0;
2336
2337 oldowner = pi_state->owner;
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362retry:
2363 if (!argowner) {
2364 if (oldowner != current) {
2365
2366
2367
2368
2369 return 0;
2370 }
2371
2372 if (__rt_mutex_futex_trylock(&pi_state->pi_mutex)) {
2373
2374 return 1;
2375 }
2376
2377
2378
2379
2380
2381 newowner = rt_mutex_owner(&pi_state->pi_mutex);
2382
2383
2384
2385
2386
2387
2388
2389
2390 if (unlikely(!newowner)) {
2391 err = -EAGAIN;
2392 goto handle_err;
2393 }
2394 } else {
2395 WARN_ON_ONCE(argowner != current);
2396 if (oldowner == current) {
2397
2398
2399
2400
2401 return 1;
2402 }
2403 newowner = argowner;
2404 }
2405
2406 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
2407
2408 if (!pi_state->owner)
2409 newtid |= FUTEX_OWNER_DIED;
2410
2411 err = get_futex_value_locked(&uval, uaddr);
2412 if (err)
2413 goto handle_err;
2414
2415 for (;;) {
2416 newval = (uval & FUTEX_OWNER_DIED) | newtid;
2417
2418 err = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
2419 if (err)
2420 goto handle_err;
2421
2422 if (curval == uval)
2423 break;
2424 uval = curval;
2425 }
2426
2427
2428
2429
2430
2431 pi_state_update_owner(pi_state, newowner);
2432
2433 return argowner == current;
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448handle_err:
2449 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
2450 spin_unlock(q->lock_ptr);
2451
2452 switch (err) {
2453 case -EFAULT:
2454 err = fault_in_user_writeable(uaddr);
2455 break;
2456
2457 case -EAGAIN:
2458 cond_resched();
2459 err = 0;
2460 break;
2461
2462 default:
2463 WARN_ON_ONCE(1);
2464 break;
2465 }
2466
2467 spin_lock(q->lock_ptr);
2468 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
2469
2470
2471
2472
2473 if (pi_state->owner != oldowner)
2474 return argowner == current;
2475
2476
2477 if (!err)
2478 goto retry;
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496 pi_state_update_owner(pi_state, rt_mutex_owner(&pi_state->pi_mutex));
2497
2498 return err;
2499}
2500
2501static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
2502 struct task_struct *argowner)
2503{
2504 struct futex_pi_state *pi_state = q->pi_state;
2505 int ret;
2506
2507 lockdep_assert_held(q->lock_ptr);
2508
2509 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
2510 ret = __fixup_pi_state_owner(uaddr, q, argowner);
2511 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
2512 return ret;
2513}
2514
2515static long futex_wait_restart(struct restart_block *restart);
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
2533{
2534 if (locked) {
2535
2536
2537
2538
2539
2540
2541
2542
2543 if (q->pi_state->owner != current)
2544 return fixup_pi_state_owner(uaddr, q, current);
2545 return 1;
2546 }
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556 if (q->pi_state->owner == current)
2557 return fixup_pi_state_owner(uaddr, q, NULL);
2558
2559
2560
2561
2562
2563 if (WARN_ON_ONCE(rt_mutex_owner(&q->pi_state->pi_mutex) == current))
2564 return fixup_pi_state_owner(uaddr, q, current);
2565
2566 return 0;
2567}
2568
2569
2570
2571
2572
2573
2574
2575static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
2576 struct hrtimer_sleeper *timeout)
2577{
2578
2579
2580
2581
2582
2583
2584 set_current_state(TASK_INTERRUPTIBLE);
2585 queue_me(q, hb);
2586
2587
2588 if (timeout)
2589 hrtimer_sleeper_start_expires(timeout, HRTIMER_MODE_ABS);
2590
2591
2592
2593
2594
2595 if (likely(!plist_node_empty(&q->list))) {
2596
2597
2598
2599
2600
2601 if (!timeout || timeout->task)
2602 freezable_schedule();
2603 }
2604 __set_current_state(TASK_RUNNING);
2605}
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
2625 struct futex_q *q, struct futex_hash_bucket **hb)
2626{
2627 u32 uval;
2628 int ret;
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648retry:
2649 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, FUTEX_READ);
2650 if (unlikely(ret != 0))
2651 return ret;
2652
2653retry_private:
2654 *hb = queue_lock(q);
2655
2656 ret = get_futex_value_locked(&uval, uaddr);
2657
2658 if (ret) {
2659 queue_unlock(*hb);
2660
2661 ret = get_user(uval, uaddr);
2662 if (ret)
2663 return ret;
2664
2665 if (!(flags & FLAGS_SHARED))
2666 goto retry_private;
2667
2668 goto retry;
2669 }
2670
2671 if (uval != val) {
2672 queue_unlock(*hb);
2673 ret = -EWOULDBLOCK;
2674 }
2675
2676 return ret;
2677}
2678
2679static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
2680 ktime_t *abs_time, u32 bitset)
2681{
2682 struct hrtimer_sleeper timeout, *to;
2683 struct restart_block *restart;
2684 struct futex_hash_bucket *hb;
2685 struct futex_q q = futex_q_init;
2686 int ret;
2687
2688 if (!bitset)
2689 return -EINVAL;
2690 q.bitset = bitset;
2691
2692 to = futex_setup_timer(abs_time, &timeout, flags,
2693 current->timer_slack_ns);
2694retry:
2695
2696
2697
2698
2699 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
2700 if (ret)
2701 goto out;
2702
2703
2704 futex_wait_queue_me(hb, &q, to);
2705
2706
2707 ret = 0;
2708
2709 if (!unqueue_me(&q))
2710 goto out;
2711 ret = -ETIMEDOUT;
2712 if (to && !to->task)
2713 goto out;
2714
2715
2716
2717
2718
2719 if (!signal_pending(current))
2720 goto retry;
2721
2722 ret = -ERESTARTSYS;
2723 if (!abs_time)
2724 goto out;
2725
2726 restart = ¤t->restart_block;
2727 restart->futex.uaddr = uaddr;
2728 restart->futex.val = val;
2729 restart->futex.time = *abs_time;
2730 restart->futex.bitset = bitset;
2731 restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
2732
2733 ret = set_restart_fn(restart, futex_wait_restart);
2734
2735out:
2736 if (to) {
2737 hrtimer_cancel(&to->timer);
2738 destroy_hrtimer_on_stack(&to->timer);
2739 }
2740 return ret;
2741}
2742
2743
2744static long futex_wait_restart(struct restart_block *restart)
2745{
2746 u32 __user *uaddr = restart->futex.uaddr;
2747 ktime_t t, *tp = NULL;
2748
2749 if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
2750 t = restart->futex.time;
2751 tp = &t;
2752 }
2753 restart->fn = do_no_restart_syscall;
2754
2755 return (long)futex_wait(uaddr, restart->futex.flags,
2756 restart->futex.val, tp, restart->futex.bitset);
2757}
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
2770 ktime_t *time, int trylock)
2771{
2772 struct hrtimer_sleeper timeout, *to;
2773 struct task_struct *exiting = NULL;
2774 struct rt_mutex_waiter rt_waiter;
2775 struct futex_hash_bucket *hb;
2776 struct futex_q q = futex_q_init;
2777 int res, ret;
2778
2779 if (!IS_ENABLED(CONFIG_FUTEX_PI))
2780 return -ENOSYS;
2781
2782 if (refill_pi_state_cache())
2783 return -ENOMEM;
2784
2785 to = futex_setup_timer(time, &timeout, flags, 0);
2786
2787retry:
2788 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, FUTEX_WRITE);
2789 if (unlikely(ret != 0))
2790 goto out;
2791
2792retry_private:
2793 hb = queue_lock(&q);
2794
2795 ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current,
2796 &exiting, 0);
2797 if (unlikely(ret)) {
2798
2799
2800
2801
2802 switch (ret) {
2803 case 1:
2804
2805 ret = 0;
2806 goto out_unlock_put_key;
2807 case -EFAULT:
2808 goto uaddr_faulted;
2809 case -EBUSY:
2810 case -EAGAIN:
2811
2812
2813
2814
2815
2816
2817 queue_unlock(hb);
2818
2819
2820
2821
2822
2823 wait_for_owner_exiting(ret, exiting);
2824 cond_resched();
2825 goto retry;
2826 default:
2827 goto out_unlock_put_key;
2828 }
2829 }
2830
2831 WARN_ON(!q.pi_state);
2832
2833
2834
2835
2836 __queue_me(&q, hb);
2837
2838 if (trylock) {
2839 ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex);
2840
2841 ret = ret ? 0 : -EWOULDBLOCK;
2842 goto no_block;
2843 }
2844
2845 rt_mutex_init_waiter(&rt_waiter);
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860 raw_spin_lock_irq(&q.pi_state->pi_mutex.wait_lock);
2861 spin_unlock(q.lock_ptr);
2862
2863
2864
2865
2866
2867 ret = __rt_mutex_start_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter, current);
2868 raw_spin_unlock_irq(&q.pi_state->pi_mutex.wait_lock);
2869
2870 if (ret) {
2871 if (ret == 1)
2872 ret = 0;
2873 goto cleanup;
2874 }
2875
2876 if (unlikely(to))
2877 hrtimer_sleeper_start_expires(to, HRTIMER_MODE_ABS);
2878
2879 ret = rt_mutex_wait_proxy_lock(&q.pi_state->pi_mutex, to, &rt_waiter);
2880
2881cleanup:
2882 spin_lock(q.lock_ptr);
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892 if (ret && !rt_mutex_cleanup_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter))
2893 ret = 0;
2894
2895no_block:
2896
2897
2898
2899
2900 res = fixup_owner(uaddr, &q, !ret);
2901
2902
2903
2904
2905 if (res)
2906 ret = (res < 0) ? res : 0;
2907
2908 unqueue_me_pi(&q);
2909 spin_unlock(q.lock_ptr);
2910 goto out;
2911
2912out_unlock_put_key:
2913 queue_unlock(hb);
2914
2915out:
2916 if (to) {
2917 hrtimer_cancel(&to->timer);
2918 destroy_hrtimer_on_stack(&to->timer);
2919 }
2920 return ret != -EINTR ? ret : -ERESTARTNOINTR;
2921
2922uaddr_faulted:
2923 queue_unlock(hb);
2924
2925 ret = fault_in_user_writeable(uaddr);
2926 if (ret)
2927 goto out;
2928
2929 if (!(flags & FLAGS_SHARED))
2930 goto retry_private;
2931
2932 goto retry;
2933}
2934
2935
2936
2937
2938
2939
2940static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
2941{
2942 u32 curval, uval, vpid = task_pid_vnr(current);
2943 union futex_key key = FUTEX_KEY_INIT;
2944 struct futex_hash_bucket *hb;
2945 struct futex_q *top_waiter;
2946 int ret;
2947
2948 if (!IS_ENABLED(CONFIG_FUTEX_PI))
2949 return -ENOSYS;
2950
2951retry:
2952 if (get_user(uval, uaddr))
2953 return -EFAULT;
2954
2955
2956
2957 if ((uval & FUTEX_TID_MASK) != vpid)
2958 return -EPERM;
2959
2960 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_WRITE);
2961 if (ret)
2962 return ret;
2963
2964 hb = hash_futex(&key);
2965 spin_lock(&hb->lock);
2966
2967
2968
2969
2970
2971
2972 top_waiter = futex_top_waiter(hb, &key);
2973 if (top_waiter) {
2974 struct futex_pi_state *pi_state = top_waiter->pi_state;
2975
2976 ret = -EINVAL;
2977 if (!pi_state)
2978 goto out_unlock;
2979
2980
2981
2982
2983
2984 if (pi_state->owner != current)
2985 goto out_unlock;
2986
2987 get_pi_state(pi_state);
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
2999 spin_unlock(&hb->lock);
3000
3001
3002 ret = wake_futex_pi(uaddr, uval, pi_state);
3003
3004 put_pi_state(pi_state);
3005
3006
3007
3008
3009 if (!ret)
3010 return ret;
3011
3012
3013
3014
3015 if (ret == -EFAULT)
3016 goto pi_faulted;
3017
3018
3019
3020
3021 if (ret == -EAGAIN)
3022 goto pi_retry;
3023
3024
3025
3026
3027 return ret;
3028 }
3029
3030
3031
3032
3033
3034
3035
3036
3037 if ((ret = cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))) {
3038 spin_unlock(&hb->lock);
3039 switch (ret) {
3040 case -EFAULT:
3041 goto pi_faulted;
3042
3043 case -EAGAIN:
3044 goto pi_retry;
3045
3046 default:
3047 WARN_ON_ONCE(1);
3048 return ret;
3049 }
3050 }
3051
3052
3053
3054
3055 ret = (curval == uval) ? 0 : -EAGAIN;
3056
3057out_unlock:
3058 spin_unlock(&hb->lock);
3059 return ret;
3060
3061pi_retry:
3062 cond_resched();
3063 goto retry;
3064
3065pi_faulted:
3066
3067 ret = fault_in_user_writeable(uaddr);
3068 if (!ret)
3069 goto retry;
3070
3071 return ret;
3072}
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090static inline
3091int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
3092 struct futex_q *q, union futex_key *key2,
3093 struct hrtimer_sleeper *timeout)
3094{
3095 int ret = 0;
3096
3097
3098
3099
3100
3101
3102
3103
3104 if (!match_futex(&q->key, key2)) {
3105 WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
3106
3107
3108
3109
3110 plist_del(&q->list, &hb->chain);
3111 hb_waiters_dec(hb);
3112
3113
3114 ret = -EWOULDBLOCK;
3115 if (timeout && !timeout->task)
3116 ret = -ETIMEDOUT;
3117 else if (signal_pending(current))
3118 ret = -ERESTARTNOINTR;
3119 }
3120 return ret;
3121}
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
3164 u32 val, ktime_t *abs_time, u32 bitset,
3165 u32 __user *uaddr2)
3166{
3167 struct hrtimer_sleeper timeout, *to;
3168 struct rt_mutex_waiter rt_waiter;
3169 struct futex_hash_bucket *hb;
3170 union futex_key key2 = FUTEX_KEY_INIT;
3171 struct futex_q q = futex_q_init;
3172 int res, ret;
3173
3174 if (!IS_ENABLED(CONFIG_FUTEX_PI))
3175 return -ENOSYS;
3176
3177 if (uaddr == uaddr2)
3178 return -EINVAL;
3179
3180 if (!bitset)
3181 return -EINVAL;
3182
3183 to = futex_setup_timer(abs_time, &timeout, flags,
3184 current->timer_slack_ns);
3185
3186
3187
3188
3189
3190 rt_mutex_init_waiter(&rt_waiter);
3191
3192 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, FUTEX_WRITE);
3193 if (unlikely(ret != 0))
3194 goto out;
3195
3196 q.bitset = bitset;
3197 q.rt_waiter = &rt_waiter;
3198 q.requeue_pi_key = &key2;
3199
3200
3201
3202
3203
3204 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
3205 if (ret)
3206 goto out;
3207
3208
3209
3210
3211
3212 if (match_futex(&q.key, &key2)) {
3213 queue_unlock(hb);
3214 ret = -EINVAL;
3215 goto out;
3216 }
3217
3218
3219 futex_wait_queue_me(hb, &q, to);
3220
3221 spin_lock(&hb->lock);
3222 ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
3223 spin_unlock(&hb->lock);
3224 if (ret)
3225 goto out;
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240 if (!q.rt_waiter) {
3241 if (q.pi_state && (q.pi_state->owner != current)) {
3242 spin_lock(q.lock_ptr);
3243 ret = fixup_owner(uaddr2, &q, true);
3244
3245
3246
3247
3248 put_pi_state(q.pi_state);
3249 spin_unlock(q.lock_ptr);
3250
3251
3252
3253
3254 ret = ret < 0 ? ret : 0;
3255 }
3256 } else {
3257 struct rt_mutex *pi_mutex;
3258
3259
3260
3261
3262
3263
3264 WARN_ON(!q.pi_state);
3265 pi_mutex = &q.pi_state->pi_mutex;
3266 ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
3267
3268 spin_lock(q.lock_ptr);
3269 if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
3270 ret = 0;
3271
3272 debug_rt_mutex_free_waiter(&rt_waiter);
3273
3274
3275
3276
3277 res = fixup_owner(uaddr2, &q, !ret);
3278
3279
3280
3281
3282 if (res)
3283 ret = (res < 0) ? res : 0;
3284
3285 unqueue_me_pi(&q);
3286 spin_unlock(q.lock_ptr);
3287 }
3288
3289 if (ret == -EINTR) {
3290
3291
3292
3293
3294
3295
3296
3297 ret = -EWOULDBLOCK;
3298 }
3299
3300out:
3301 if (to) {
3302 hrtimer_cancel(&to->timer);
3303 destroy_hrtimer_on_stack(&to->timer);
3304 }
3305 return ret;
3306}
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
3329 size_t, len)
3330{
3331 if (!futex_cmpxchg_enabled)
3332 return -ENOSYS;
3333
3334
3335
3336 if (unlikely(len != sizeof(*head)))
3337 return -EINVAL;
3338
3339 current->robust_list = head;
3340
3341 return 0;
3342}
3343
3344
3345
3346
3347
3348
3349
3350SYSCALL_DEFINE3(get_robust_list, int, pid,
3351 struct robust_list_head __user * __user *, head_ptr,
3352 size_t __user *, len_ptr)
3353{
3354 struct robust_list_head __user *head;
3355 unsigned long ret;
3356 struct task_struct *p;
3357
3358 if (!futex_cmpxchg_enabled)
3359 return -ENOSYS;
3360
3361 rcu_read_lock();
3362
3363 ret = -ESRCH;
3364 if (!pid)
3365 p = current;
3366 else {
3367 p = find_task_by_vpid(pid);
3368 if (!p)
3369 goto err_unlock;
3370 }
3371
3372 ret = -EPERM;
3373 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
3374 goto err_unlock;
3375
3376 head = p->robust_list;
3377 rcu_read_unlock();
3378
3379 if (put_user(sizeof(*head), len_ptr))
3380 return -EFAULT;
3381 return put_user(head, head_ptr);
3382
3383err_unlock:
3384 rcu_read_unlock();
3385
3386 return ret;
3387}
3388
3389
3390#define HANDLE_DEATH_PENDING true
3391#define HANDLE_DEATH_LIST false
3392
3393
3394
3395
3396
3397static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr,
3398 bool pi, bool pending_op)
3399{
3400 u32 uval, nval, mval;
3401 int err;
3402
3403
3404 if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
3405 return -1;
3406
3407retry:
3408 if (get_user(uval, uaddr))
3409 return -1;
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442 if (pending_op && !pi && !uval) {
3443 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
3444 return 0;
3445 }
3446
3447 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(curr))
3448 return 0;
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471 if ((err = cmpxchg_futex_value_locked(&nval, uaddr, uval, mval))) {
3472 switch (err) {
3473 case -EFAULT:
3474 if (fault_in_user_writeable(uaddr))
3475 return -1;
3476 goto retry;
3477
3478 case -EAGAIN:
3479 cond_resched();
3480 goto retry;
3481
3482 default:
3483 WARN_ON_ONCE(1);
3484 return err;
3485 }
3486 }
3487
3488 if (nval != uval)
3489 goto retry;
3490
3491
3492
3493
3494
3495 if (!pi && (uval & FUTEX_WAITERS))
3496 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
3497
3498 return 0;
3499}
3500
3501
3502
3503
3504static inline int fetch_robust_entry(struct robust_list __user **entry,
3505 struct robust_list __user * __user *head,
3506 unsigned int *pi)
3507{
3508 unsigned long uentry;
3509
3510 if (get_user(uentry, (unsigned long __user *)head))
3511 return -EFAULT;
3512
3513 *entry = (void __user *)(uentry & ~1UL);
3514 *pi = uentry & 1;
3515
3516 return 0;
3517}
3518
3519
3520
3521
3522
3523
3524
3525static void exit_robust_list(struct task_struct *curr)
3526{
3527 struct robust_list_head __user *head = curr->robust_list;
3528 struct robust_list __user *entry, *next_entry, *pending;
3529 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3530 unsigned int next_pi;
3531 unsigned long futex_offset;
3532 int rc;
3533
3534 if (!futex_cmpxchg_enabled)
3535 return;
3536
3537
3538
3539
3540
3541 if (fetch_robust_entry(&entry, &head->list.next, &pi))
3542 return;
3543
3544
3545
3546 if (get_user(futex_offset, &head->futex_offset))
3547 return;
3548
3549
3550
3551
3552 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
3553 return;
3554
3555 next_entry = NULL;
3556 while (entry != &head->list) {
3557
3558
3559
3560
3561 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
3562
3563
3564
3565
3566 if (entry != pending) {
3567 if (handle_futex_death((void __user *)entry + futex_offset,
3568 curr, pi, HANDLE_DEATH_LIST))
3569 return;
3570 }
3571 if (rc)
3572 return;
3573 entry = next_entry;
3574 pi = next_pi;
3575
3576
3577
3578 if (!--limit)
3579 break;
3580
3581 cond_resched();
3582 }
3583
3584 if (pending) {
3585 handle_futex_death((void __user *)pending + futex_offset,
3586 curr, pip, HANDLE_DEATH_PENDING);
3587 }
3588}
3589
3590static void futex_cleanup(struct task_struct *tsk)
3591{
3592 if (unlikely(tsk->robust_list)) {
3593 exit_robust_list(tsk);
3594 tsk->robust_list = NULL;
3595 }
3596
3597#ifdef CONFIG_COMPAT
3598 if (unlikely(tsk->compat_robust_list)) {
3599 compat_exit_robust_list(tsk);
3600 tsk->compat_robust_list = NULL;
3601 }
3602#endif
3603
3604 if (unlikely(!list_empty(&tsk->pi_state_list)))
3605 exit_pi_state_list(tsk);
3606}
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625void futex_exit_recursive(struct task_struct *tsk)
3626{
3627
3628 if (tsk->futex_state == FUTEX_STATE_EXITING)
3629 mutex_unlock(&tsk->futex_exit_mutex);
3630 tsk->futex_state = FUTEX_STATE_DEAD;
3631}
3632
3633static void futex_cleanup_begin(struct task_struct *tsk)
3634{
3635
3636
3637
3638
3639
3640
3641 mutex_lock(&tsk->futex_exit_mutex);
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654 raw_spin_lock_irq(&tsk->pi_lock);
3655 tsk->futex_state = FUTEX_STATE_EXITING;
3656 raw_spin_unlock_irq(&tsk->pi_lock);
3657}
3658
3659static void futex_cleanup_end(struct task_struct *tsk, int state)
3660{
3661
3662
3663
3664
3665 tsk->futex_state = state;
3666
3667
3668
3669
3670 mutex_unlock(&tsk->futex_exit_mutex);
3671}
3672
3673void futex_exec_release(struct task_struct *tsk)
3674{
3675
3676
3677
3678
3679
3680
3681
3682 futex_cleanup_begin(tsk);
3683 futex_cleanup(tsk);
3684
3685
3686
3687
3688 futex_cleanup_end(tsk, FUTEX_STATE_OK);
3689}
3690
3691void futex_exit_release(struct task_struct *tsk)
3692{
3693 futex_cleanup_begin(tsk);
3694 futex_cleanup(tsk);
3695 futex_cleanup_end(tsk, FUTEX_STATE_DEAD);
3696}
3697
3698long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
3699 u32 __user *uaddr2, u32 val2, u32 val3)
3700{
3701 int cmd = op & FUTEX_CMD_MASK;
3702 unsigned int flags = 0;
3703
3704 if (!(op & FUTEX_PRIVATE_FLAG))
3705 flags |= FLAGS_SHARED;
3706
3707 if (op & FUTEX_CLOCK_REALTIME) {
3708 flags |= FLAGS_CLOCKRT;
3709 if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI &&
3710 cmd != FUTEX_LOCK_PI2)
3711 return -ENOSYS;
3712 }
3713
3714 switch (cmd) {
3715 case FUTEX_LOCK_PI:
3716 case FUTEX_LOCK_PI2:
3717 case FUTEX_UNLOCK_PI:
3718 case FUTEX_TRYLOCK_PI:
3719 case FUTEX_WAIT_REQUEUE_PI:
3720 case FUTEX_CMP_REQUEUE_PI:
3721 if (!futex_cmpxchg_enabled)
3722 return -ENOSYS;
3723 }
3724
3725 switch (cmd) {
3726 case FUTEX_WAIT:
3727 val3 = FUTEX_BITSET_MATCH_ANY;
3728 fallthrough;
3729 case FUTEX_WAIT_BITSET:
3730 return futex_wait(uaddr, flags, val, timeout, val3);
3731 case FUTEX_WAKE:
3732 val3 = FUTEX_BITSET_MATCH_ANY;
3733 fallthrough;
3734 case FUTEX_WAKE_BITSET:
3735 return futex_wake(uaddr, flags, val, val3);
3736 case FUTEX_REQUEUE:
3737 return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
3738 case FUTEX_CMP_REQUEUE:
3739 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
3740 case FUTEX_WAKE_OP:
3741 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
3742 case FUTEX_LOCK_PI:
3743 flags |= FLAGS_CLOCKRT;
3744 fallthrough;
3745 case FUTEX_LOCK_PI2:
3746 return futex_lock_pi(uaddr, flags, timeout, 0);
3747 case FUTEX_UNLOCK_PI:
3748 return futex_unlock_pi(uaddr, flags);
3749 case FUTEX_TRYLOCK_PI:
3750 return futex_lock_pi(uaddr, flags, NULL, 1);
3751 case FUTEX_WAIT_REQUEUE_PI:
3752 val3 = FUTEX_BITSET_MATCH_ANY;
3753 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
3754 uaddr2);
3755 case FUTEX_CMP_REQUEUE_PI:
3756 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
3757 }
3758 return -ENOSYS;
3759}
3760
3761static __always_inline bool futex_cmd_has_timeout(u32 cmd)
3762{
3763 switch (cmd) {
3764 case FUTEX_WAIT:
3765 case FUTEX_LOCK_PI:
3766 case FUTEX_LOCK_PI2:
3767 case FUTEX_WAIT_BITSET:
3768 case FUTEX_WAIT_REQUEUE_PI:
3769 return true;
3770 }
3771 return false;
3772}
3773
3774static __always_inline int
3775futex_init_timeout(u32 cmd, u32 op, struct timespec64 *ts, ktime_t *t)
3776{
3777 if (!timespec64_valid(ts))
3778 return -EINVAL;
3779
3780 *t = timespec64_to_ktime(*ts);
3781 if (cmd == FUTEX_WAIT)
3782 *t = ktime_add_safe(ktime_get(), *t);
3783 else if (cmd != FUTEX_LOCK_PI && !(op & FUTEX_CLOCK_REALTIME))
3784 *t = timens_ktime_to_host(CLOCK_MONOTONIC, *t);
3785 return 0;
3786}
3787
3788SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3789 const struct __kernel_timespec __user *, utime,
3790 u32 __user *, uaddr2, u32, val3)
3791{
3792 int ret, cmd = op & FUTEX_CMD_MASK;
3793 ktime_t t, *tp = NULL;
3794 struct timespec64 ts;
3795
3796 if (utime && futex_cmd_has_timeout(cmd)) {
3797 if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
3798 return -EFAULT;
3799 if (get_timespec64(&ts, utime))
3800 return -EFAULT;
3801 ret = futex_init_timeout(cmd, op, &ts, &t);
3802 if (ret)
3803 return ret;
3804 tp = &t;
3805 }
3806
3807 return do_futex(uaddr, op, val, tp, uaddr2, (unsigned long)utime, val3);
3808}
3809
3810#ifdef CONFIG_COMPAT
3811
3812
3813
3814static inline int
3815compat_fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
3816 compat_uptr_t __user *head, unsigned int *pi)
3817{
3818 if (get_user(*uentry, head))
3819 return -EFAULT;
3820
3821 *entry = compat_ptr((*uentry) & ~1);
3822 *pi = (unsigned int)(*uentry) & 1;
3823
3824 return 0;
3825}
3826
3827static void __user *futex_uaddr(struct robust_list __user *entry,
3828 compat_long_t futex_offset)
3829{
3830 compat_uptr_t base = ptr_to_compat(entry);
3831 void __user *uaddr = compat_ptr(base + futex_offset);
3832
3833 return uaddr;
3834}
3835
3836
3837
3838
3839
3840
3841
3842static void compat_exit_robust_list(struct task_struct *curr)
3843{
3844 struct compat_robust_list_head __user *head = curr->compat_robust_list;
3845 struct robust_list __user *entry, *next_entry, *pending;
3846 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3847 unsigned int next_pi;
3848 compat_uptr_t uentry, next_uentry, upending;
3849 compat_long_t futex_offset;
3850 int rc;
3851
3852 if (!futex_cmpxchg_enabled)
3853 return;
3854
3855
3856
3857
3858
3859 if (compat_fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
3860 return;
3861
3862
3863
3864 if (get_user(futex_offset, &head->futex_offset))
3865 return;
3866
3867
3868
3869
3870 if (compat_fetch_robust_entry(&upending, &pending,
3871 &head->list_op_pending, &pip))
3872 return;
3873
3874 next_entry = NULL;
3875 while (entry != (struct robust_list __user *) &head->list) {
3876
3877
3878
3879
3880 rc = compat_fetch_robust_entry(&next_uentry, &next_entry,
3881 (compat_uptr_t __user *)&entry->next, &next_pi);
3882
3883
3884
3885
3886 if (entry != pending) {
3887 void __user *uaddr = futex_uaddr(entry, futex_offset);
3888
3889 if (handle_futex_death(uaddr, curr, pi,
3890 HANDLE_DEATH_LIST))
3891 return;
3892 }
3893 if (rc)
3894 return;
3895 uentry = next_uentry;
3896 entry = next_entry;
3897 pi = next_pi;
3898
3899
3900
3901 if (!--limit)
3902 break;
3903
3904 cond_resched();
3905 }
3906 if (pending) {
3907 void __user *uaddr = futex_uaddr(pending, futex_offset);
3908
3909 handle_futex_death(uaddr, curr, pip, HANDLE_DEATH_PENDING);
3910 }
3911}
3912
3913COMPAT_SYSCALL_DEFINE2(set_robust_list,
3914 struct compat_robust_list_head __user *, head,
3915 compat_size_t, len)
3916{
3917 if (!futex_cmpxchg_enabled)
3918 return -ENOSYS;
3919
3920 if (unlikely(len != sizeof(*head)))
3921 return -EINVAL;
3922
3923 current->compat_robust_list = head;
3924
3925 return 0;
3926}
3927
3928COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
3929 compat_uptr_t __user *, head_ptr,
3930 compat_size_t __user *, len_ptr)
3931{
3932 struct compat_robust_list_head __user *head;
3933 unsigned long ret;
3934 struct task_struct *p;
3935
3936 if (!futex_cmpxchg_enabled)
3937 return -ENOSYS;
3938
3939 rcu_read_lock();
3940
3941 ret = -ESRCH;
3942 if (!pid)
3943 p = current;
3944 else {
3945 p = find_task_by_vpid(pid);
3946 if (!p)
3947 goto err_unlock;
3948 }
3949
3950 ret = -EPERM;
3951 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
3952 goto err_unlock;
3953
3954 head = p->compat_robust_list;
3955 rcu_read_unlock();
3956
3957 if (put_user(sizeof(*head), len_ptr))
3958 return -EFAULT;
3959 return put_user(ptr_to_compat(head), head_ptr);
3960
3961err_unlock:
3962 rcu_read_unlock();
3963
3964 return ret;
3965}
3966#endif
3967
3968#ifdef CONFIG_COMPAT_32BIT_TIME
3969SYSCALL_DEFINE6(futex_time32, u32 __user *, uaddr, int, op, u32, val,
3970 const struct old_timespec32 __user *, utime, u32 __user *, uaddr2,
3971 u32, val3)
3972{
3973 int ret, cmd = op & FUTEX_CMD_MASK;
3974 ktime_t t, *tp = NULL;
3975 struct timespec64 ts;
3976
3977 if (utime && futex_cmd_has_timeout(cmd)) {
3978 if (get_old_timespec32(&ts, utime))
3979 return -EFAULT;
3980 ret = futex_init_timeout(cmd, op, &ts, &t);
3981 if (ret)
3982 return ret;
3983 tp = &t;
3984 }
3985
3986 return do_futex(uaddr, op, val, tp, uaddr2, (unsigned long)utime, val3);
3987}
3988#endif
3989
3990static void __init futex_detect_cmpxchg(void)
3991{
3992#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
3993 u32 curval;
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005 if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
4006 futex_cmpxchg_enabled = 1;
4007#endif
4008}
4009
4010static int __init futex_init(void)
4011{
4012 unsigned int futex_shift;
4013 unsigned long i;
4014
4015#if CONFIG_BASE_SMALL
4016 futex_hashsize = 16;
4017#else
4018 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
4019#endif
4020
4021 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
4022 futex_hashsize, 0,
4023 futex_hashsize < 256 ? HASH_SMALL : 0,
4024 &futex_shift, NULL,
4025 futex_hashsize, futex_hashsize);
4026 futex_hashsize = 1UL << futex_shift;
4027
4028 futex_detect_cmpxchg();
4029
4030 for (i = 0; i < futex_hashsize; i++) {
4031 atomic_set(&futex_queues[i].waiters, 0);
4032 plist_head_init(&futex_queues[i].chain);
4033 spin_lock_init(&futex_queues[i].lock);
4034 }
4035
4036 return 0;
4037}
4038core_initcall(futex_init);
4039