1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2015 Yi Zhang <wetpzy@gmail.com>
4 * Li Wang <liwang@redhat.com>
5 *
6 * DESCRIPTION:
7 *
8 * It is a regression test for commit:
9 * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/
10 * commit/?id=13d60f4
11 *
12 * The implementation of futex doesn't produce unique keys for futexes
13 * in shared huge pages, so threads waiting on different futexes may
14 * end up on the same wait list. This results in incorrect threads being
15 * woken by FUTEX_WAKE.
16 *
17 * Needs to be run as root unless there are already enough huge pages available.
18 * In the fail case, which happens in the CentOS-6.6 kernel (2.6.32-504.8.1),
19 * the tests hangs until it times out after a 30-second wait.
20 *
21 */
22
23 #include <stdio.h>
24 #include <sys/mman.h>
25 #include <fcntl.h>
26 #include <sys/time.h>
27 #include <string.h>
28
29 #include "futextest.h"
30 #include "futex_utils.h"
31 #include "lapi/mmap.h"
32 #include "tst_safe_stdio.h"
33 #include "tst_safe_pthread.h"
34
35 static futex_t *futex1, *futex2;
36
37 static struct tst_ts to;
38
39 static struct futex_test_variants variants[] = {
40 #if (__NR_futex != __LTP__NR_INVALID_SYSCALL)
41 { .fntype = FUTEX_FN_FUTEX, .tstype = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
42 #endif
43
44 #if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL)
45 { .fntype = FUTEX_FN_FUTEX64, .tstype = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
46 #endif
47 };
48
setup(void)49 static void setup(void)
50 {
51 if (tst_hugepages == 0)
52 tst_brk(TCONF, "No enough hugepages for testing.");
53
54 struct futex_test_variants *tv = &variants[tst_variant];
55
56 tst_res(TINFO, "Testing variant: %s", tv->desc);
57 futex_supported_by_kernel(tv->fntype);
58
59 to = tst_ts_from_ns(tv->tstype, 30 * NSEC_PER_SEC);
60 }
61
wait_thread1(void * arg LTP_ATTRIBUTE_UNUSED)62 static void *wait_thread1(void *arg LTP_ATTRIBUTE_UNUSED)
63 {
64 struct futex_test_variants *tv = &variants[tst_variant];
65
66 futex_wait(tv->fntype, futex1, *futex1, &to, 0);
67
68 return NULL;
69 }
70
wait_thread2(void * arg LTP_ATTRIBUTE_UNUSED)71 static void *wait_thread2(void *arg LTP_ATTRIBUTE_UNUSED)
72 {
73 struct futex_test_variants *tv = &variants[tst_variant];
74 int res;
75
76 errno = 0;
77 res = futex_wait(tv->fntype, futex2, *futex2, &to, 0);
78 if (!res)
79 tst_res(TPASS, "Hi hydra, thread2 awake!");
80 else
81 tst_res(TFAIL | TERRNO, "Bug: wait_thread2 did not wake after 30 secs.");
82
83 return NULL;
84 }
85
wakeup_thread2(void)86 static void wakeup_thread2(void)
87 {
88 struct futex_test_variants *tv = &variants[tst_variant];
89 void *addr;
90 int hpsz, pgsz;
91 pthread_t th1, th2;
92
93 hpsz = tst_get_hugepage_size();
94 tst_res(TINFO, "Hugepagesize %i", hpsz);
95
96 /*allocate some shared memory*/
97 addr = mmap(NULL, hpsz, PROT_WRITE | PROT_READ,
98 MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
99
100 if (addr == MAP_FAILED) {
101 if (errno == ENOMEM)
102 tst_brk(TCONF, "Cannot allocate hugepage, memory too fragmented?");
103
104 tst_brk(TBROK | TERRNO, "Cannot allocate hugepage");
105 }
106
107 pgsz = getpagesize();
108
109 /*apply the first subpage to futex1*/
110 futex1 = addr;
111 *futex1 = 0;
112 /*apply the second subpage to futex2*/
113 futex2 = (futex_t *)((char *)addr + pgsz);
114 *futex2 = 0;
115
116 /*thread1 block on futex1 first,then thread2 block on futex2*/
117 SAFE_PTHREAD_CREATE(&th1, NULL, wait_thread1, NULL);
118 SAFE_PTHREAD_CREATE(&th2, NULL, wait_thread2, NULL);
119
120 while (wait_for_threads(2))
121 usleep(1000);
122
123 futex_wake(tv->fntype, futex2, 1, 0);
124 SAFE_PTHREAD_JOIN(th2, NULL);
125 futex_wake(tv->fntype, futex1, 1, 0);
126 SAFE_PTHREAD_JOIN(th1, NULL);
127
128 SAFE_MUNMAP(addr, hpsz);
129 }
130
131 static struct tst_test test = {
132 .setup = setup,
133 .test_all = wakeup_thread2,
134 .test_variants = ARRAY_SIZE(variants),
135 .needs_root = 1,
136 .min_kver = "2.6.32",
137 .needs_tmpdir = 1,
138 .request_hugepages = 1,
139 };
140