• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fcntl.h>
25 #include <sys/time.h>
26 #include <string.h>
27 
28 #include "futextest.h"
29 #include "futex_utils.h"
30 #include "lapi/mmap.h"
31 #include "tst_safe_stdio.h"
32 #include "tst_safe_pthread.h"
33 
34 static futex_t *futex1, *futex2;
35 
36 static struct tst_ts to;
37 
38 static struct futex_test_variants variants[] = {
39 #if (__NR_futex != __LTP__NR_INVALID_SYSCALL)
40 	{ .fntype = FUTEX_FN_FUTEX, .tstype = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
41 #endif
42 
43 #if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL)
44 	{ .fntype = FUTEX_FN_FUTEX64, .tstype = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
45 #endif
46 };
47 
setup(void)48 static void setup(void)
49 {
50 	struct futex_test_variants *tv = &variants[tst_variant];
51 
52 	tst_res(TINFO, "Testing variant: %s", tv->desc);
53 	futex_supported_by_kernel(tv->fntype);
54 
55 	to = tst_ts_from_ns(tv->tstype, 30 * NSEC_PER_SEC);
56 }
57 
wait_thread1(void * arg LTP_ATTRIBUTE_UNUSED)58 static void *wait_thread1(void *arg LTP_ATTRIBUTE_UNUSED)
59 {
60 	struct futex_test_variants *tv = &variants[tst_variant];
61 
62 	futex_wait(tv->fntype, futex1, *futex1, &to, 0);
63 
64 	return NULL;
65 }
66 
wait_thread2(void * arg LTP_ATTRIBUTE_UNUSED)67 static void *wait_thread2(void *arg LTP_ATTRIBUTE_UNUSED)
68 {
69 	struct futex_test_variants *tv = &variants[tst_variant];
70 	int res;
71 
72 	errno = 0;
73 	res = futex_wait(tv->fntype, futex2, *futex2, &to, 0);
74 	if (!res)
75 		tst_res(TPASS, "Hi hydra, thread2 awake!");
76 	else
77 		tst_res(TFAIL | TERRNO, "Bug: wait_thread2 did not wake after 30 secs.");
78 
79 	return NULL;
80 }
81 
wakeup_thread2(void)82 static void wakeup_thread2(void)
83 {
84 	struct futex_test_variants *tv = &variants[tst_variant];
85 	void *addr;
86 	int hpsz, pgsz;
87 	pthread_t th1, th2;
88 
89 	hpsz = tst_get_hugepage_size();
90 	tst_res(TINFO, "Hugepagesize %i", hpsz);
91 
92 	/*allocate some shared memory*/
93 	addr = mmap(NULL, hpsz, PROT_WRITE | PROT_READ,
94 	            MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
95 
96 	if (addr == MAP_FAILED) {
97 		if (errno == ENOMEM)
98 			tst_brk(TCONF, "Cannot allocate hugepage, memory too fragmented?");
99 
100 		tst_brk(TBROK | TERRNO, "Cannot allocate hugepage");
101 	}
102 
103 	pgsz = getpagesize();
104 
105 	/*apply the first subpage to futex1*/
106 	futex1 = addr;
107 	*futex1 = 0;
108 	/*apply the second subpage to futex2*/
109 	futex2 = (futex_t *)((char *)addr + pgsz);
110 	*futex2 = 0;
111 
112 	/*thread1 block on futex1 first,then thread2 block on futex2*/
113 	SAFE_PTHREAD_CREATE(&th1, NULL, wait_thread1, NULL);
114 	SAFE_PTHREAD_CREATE(&th2, NULL, wait_thread2, NULL);
115 
116 	while (wait_for_threads(2))
117 		usleep(1000);
118 
119 	futex_wake(tv->fntype, futex2, 1, 0);
120 	SAFE_PTHREAD_JOIN(th2, NULL);
121 	futex_wake(tv->fntype, futex1, 1, 0);
122 	SAFE_PTHREAD_JOIN(th1, NULL);
123 
124 	SAFE_MUNMAP(addr, hpsz);
125 }
126 
127 static struct tst_test test = {
128 	.setup = setup,
129 	.test_all = wakeup_thread2,
130 	.test_variants = ARRAY_SIZE(variants),
131 	.needs_root = 1,
132 	.needs_tmpdir = 1,
133 	.hugepages = {1, TST_NEEDS},
134 };
135