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