• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015  Yi Zhang <wetpzy@gmail.com>
3  *                     Li Wang <liwang@redhat.com>
4  *
5  * Licensed under the GNU GPLv2 or later.
6  * This program is free software;  you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14  * the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program;  if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20  /* DESCRIPTION:
21  *
22  *   It is a regression test for commit:
23  *   http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/
24  *   commit/?id=13d60f4
25  *
26  *   The implementation of futex doesn't produce unique keys for futexes
27  *   in shared huge pages, so threads waiting on different futexes may
28  *   end up on the same wait list. This results in incorrect threads being
29  *   woken by FUTEX_WAKE.
30  *
31  *   Needs to be run as root unless there are already enough huge pages available.
32  *   In the fail case, which happens in the CentOS-6.6 kernel (2.6.32-504.8.1),
33  *   the tests hangs until it times out after a 30-second wait.
34  *
35  */
36 
37 #include <stdio.h>
38 #include <sys/mman.h>
39 #include <fcntl.h>
40 #include <pthread.h>
41 #include <errno.h>
42 #include <sys/time.h>
43 #include <string.h>
44 
45 #include "test.h"
46 #include "safe_macros.h"
47 #include "futextest.h"
48 #include "futex_utils.h"
49 #include "lapi/mmap.h"
50 
51 #define PATH_MEMINFO "/proc/meminfo"
52 #define PATH_NR_HUGEPAGES "/proc/sys/vm/nr_hugepages"
53 #define PATH_HUGEPAGES	"/sys/kernel/mm/hugepages/"
54 
55 const char *TCID = "futex_wake04";
56 const int TST_TOTAL = 1;
57 
58 static futex_t *futex1, *futex2;
59 
60 static struct timespec to = {.tv_sec = 30, .tv_nsec = 0};
61 
62 static long orig_hugepages;
63 
setup(void)64 static void setup(void)
65 {
66 	tst_require_root();
67 
68 	if ((tst_kvercmp(2, 6, 32)) < 0) {
69 		tst_brkm(TCONF, NULL, "This test can only run on kernels "
70 			"that are 2.6.32 or higher");
71 	}
72 
73 	if (access(PATH_HUGEPAGES, F_OK))
74 		tst_brkm(TCONF, NULL, "Huge page is not supported.");
75 
76 	tst_tmpdir();
77 
78 	SAFE_FILE_SCANF(NULL, PATH_NR_HUGEPAGES, "%ld", &orig_hugepages);
79 
80 	if (orig_hugepages <= 0)
81 		SAFE_FILE_PRINTF(NULL, PATH_NR_HUGEPAGES, "%d", 1);
82 
83 	TEST_PAUSE;
84 }
85 
cleanup(void)86 static void cleanup(void)
87 {
88 	if (orig_hugepages <= 0)
89 		SAFE_FILE_PRINTF(NULL, PATH_NR_HUGEPAGES, "%ld", orig_hugepages);
90 
91 	tst_rmdir();
92 }
93 
read_hugepagesize(void)94 static int read_hugepagesize(void)
95 {
96 	FILE *fp;
97 	char line[BUFSIZ], buf[BUFSIZ];
98 	int val;
99 
100 	fp = SAFE_FOPEN(cleanup, PATH_MEMINFO, "r");
101 	while (fgets(line, BUFSIZ, fp) != NULL) {
102 		if (sscanf(line, "%64s %d", buf, &val) == 2)
103 			if (strcmp(buf, "Hugepagesize:") == 0) {
104 				SAFE_FCLOSE(cleanup, fp);
105 				return 1024 * val;
106 			}
107 	}
108 
109 	SAFE_FCLOSE(cleanup, fp);
110 	tst_brkm(TBROK, NULL, "can't find \"%s\" in %s",
111 			"Hugepagesize:", PATH_MEMINFO);
112 }
113 
wait_thread1(void * arg LTP_ATTRIBUTE_UNUSED)114 static void *wait_thread1(void *arg LTP_ATTRIBUTE_UNUSED)
115 {
116 	futex_wait(futex1, *futex1, &to, 0);
117 
118 	return NULL;
119 }
120 
wait_thread2(void * arg LTP_ATTRIBUTE_UNUSED)121 static void *wait_thread2(void *arg LTP_ATTRIBUTE_UNUSED)
122 {
123 	int res;
124 
125 	res = futex_wait(futex2, *futex2, &to, 0);
126 	if (!res)
127 		tst_resm(TPASS, "Hi hydra, thread2 awake!");
128 	else
129 		tst_resm(TFAIL, "Bug: wait_thread2 did not wake after 30 secs.");
130 
131 	return NULL;
132 }
133 
wakeup_thread2(void)134 static void wakeup_thread2(void)
135 {
136 	void *addr;
137 	int hpsz, pgsz, res;
138 	pthread_t th1, th2;
139 
140 	hpsz = read_hugepagesize();
141 	tst_resm(TINFO, "Hugepagesize %i", hpsz);
142 
143 	/*allocate some shared memory*/
144 	addr = mmap(NULL, hpsz, PROT_WRITE | PROT_READ,
145 	            MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
146 
147 	if (addr == MAP_FAILED) {
148 		if (errno == ENOMEM) {
149 			tst_brkm(TCONF, NULL,
150 				 "Cannot allocate hugepage, memory too fragmented?");
151 		}
152 
153 		tst_brkm(TBROK | TERRNO, NULL, "Cannot allocate hugepage");
154 	}
155 
156 	pgsz = getpagesize();
157 
158 	/*apply the first subpage to futex1*/
159 	futex1 = addr;
160 	*futex1 = 0;
161 	/*apply the second subpage to futex2*/
162 	futex2 = (futex_t *)((char *)addr + pgsz);
163 	*futex2 = 0;
164 
165 	/*thread1 block on futex1 first,then thread2 block on futex2*/
166 	res = pthread_create(&th1, NULL, wait_thread1, NULL);
167 	if (res) {
168 		tst_brkm(TBROK, NULL, "pthread_create(): %s",
169 				tst_strerrno(res));
170 	}
171 
172 	res = pthread_create(&th2, NULL, wait_thread2, NULL);
173 	if (res) {
174 		tst_brkm(TBROK, NULL, "pthread_create(): %s",
175 				tst_strerrno(res));
176 	}
177 
178 	while (wait_for_threads(2))
179 		usleep(1000);
180 
181 	futex_wake(futex2, 1, 0);
182 
183 	res = pthread_join(th2, NULL);
184 	if (res)
185 		tst_brkm(TBROK, NULL, "pthread_join(): %s", tst_strerrno(res));
186 
187 	futex_wake(futex1, 1, 0);
188 
189 	res = pthread_join(th1, NULL);
190 	if (res)
191 		tst_brkm(TBROK, NULL, "pthread_join(): %s", tst_strerrno(res));
192 
193 	SAFE_MUNMAP(NULL, addr, hpsz);
194 }
195 
main(int argc,char * argv[])196 int main(int argc, char *argv[])
197 {
198 	int lc;
199 
200 	tst_parse_opts(argc, argv, NULL, NULL);
201 
202 	setup();
203 
204 	for (lc = 0; TEST_LOOPING(lc); lc++)
205 		wakeup_thread2();
206 
207 	cleanup();
208 	tst_exit();
209 }
210