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
55const char *TCID = "futex_wake04";
56const int TST_TOTAL = 1;
57
58static futex_t *futex1, *futex2;
59
60static struct timespec to = {.tv_sec = 30, .tv_nsec = 0};
61
62static long orig_hugepages;
63
64static 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	SAFE_FILE_PRINTF(NULL, PATH_NR_HUGEPAGES, "%d", 1);
80
81	TEST_PAUSE;
82}
83
84static void cleanup(void)
85{
86	SAFE_FILE_PRINTF(NULL, PATH_NR_HUGEPAGES, "%ld", orig_hugepages);
87
88	tst_rmdir();
89}
90
91static int read_hugepagesize(void)
92{
93	FILE *fp;
94	char line[BUFSIZ], buf[BUFSIZ];
95	int val;
96
97	fp = SAFE_FOPEN(cleanup, PATH_MEMINFO, "r");
98	while (fgets(line, BUFSIZ, fp) != NULL) {
99		if (sscanf(line, "%64s %d", buf, &val) == 2)
100			if (strcmp(buf, "Hugepagesize:") == 0) {
101				SAFE_FCLOSE(cleanup, fp);
102				return 1024 * val;
103			}
104	}
105
106	SAFE_FCLOSE(cleanup, fp);
107	tst_brkm(TBROK, NULL, "can't find \"%s\" in %s",
108			"Hugepagesize:", PATH_MEMINFO);
109}
110
111static void *wait_thread1(void *arg LTP_ATTRIBUTE_UNUSED)
112{
113	futex_wait(futex1, *futex1, &to, 0);
114
115	return NULL;
116}
117
118static void *wait_thread2(void *arg LTP_ATTRIBUTE_UNUSED)
119{
120	int res;
121
122	res = futex_wait(futex2, *futex2, &to, 0);
123	if (!res)
124		tst_resm(TPASS, "Hi hydra, thread2 awake!");
125	else
126		tst_resm(TFAIL, "Bug: wait_thread2 did not wake after 30 secs.");
127
128	return NULL;
129}
130
131static void wakeup_thread2(void)
132{
133	void *addr;
134	int hpsz, pgsz, res;
135	pthread_t th1, th2;
136
137	hpsz = read_hugepagesize();
138	tst_resm(TINFO, "Hugepagesize %i", hpsz);
139
140	/*allocate some shared memory*/
141	addr = mmap(NULL, hpsz, PROT_WRITE | PROT_READ,
142	            MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
143
144	if (addr == MAP_FAILED) {
145		if (errno == ENOMEM) {
146			tst_brkm(TCONF, NULL,
147				 "Cannot allocate hugepage, memory too fragmented?");
148		}
149
150		tst_brkm(TBROK | TERRNO, NULL, "Cannot allocate hugepage");
151	}
152
153	pgsz = getpagesize();
154
155	/*apply the first subpage to futex1*/
156	futex1 = addr;
157	*futex1 = 0;
158	/*apply the second subpage to futex2*/
159	futex2 = (futex_t *)((char *)addr + pgsz);
160	*futex2 = 0;
161
162	/*thread1 block on futex1 first,then thread2 block on futex2*/
163	res = pthread_create(&th1, NULL, wait_thread1, NULL);
164	if (res) {
165		tst_brkm(TBROK, NULL, "pthread_create(): %s",
166				tst_strerrno(res));
167	}
168
169	res = pthread_create(&th2, NULL, wait_thread2, NULL);
170	if (res) {
171		tst_brkm(TBROK, NULL, "pthread_create(): %s",
172				tst_strerrno(res));
173	}
174
175	while (wait_for_threads(2))
176		usleep(100);
177
178	futex_wake(futex2, 1, 0);
179
180	res = pthread_join(th2, NULL);
181	if (res)
182		tst_brkm(TBROK, NULL, "pthread_join(): %s", tst_strerrno(res));
183
184	futex_wake(futex1, 1, 0);
185
186	res = pthread_join(th1, NULL);
187	if (res)
188		tst_brkm(TBROK, NULL, "pthread_join(): %s", tst_strerrno(res));
189
190	SAFE_MUNMAP(NULL, addr, hpsz);
191}
192
193int main(int argc, char *argv[])
194{
195	int lc;
196
197	tst_parse_opts(argc, argv, NULL, NULL);
198
199	setup();
200
201	for (lc = 0; TEST_LOOPING(lc); lc++)
202		wakeup_thread2();
203
204	cleanup();
205	tst_exit();
206}
207