• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2004
3  * Copyright (c) Linux Test Project, 2004-2017
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  */
15 
16 /*
17  * Test Name: hugemmap02
18  *
19  * Test Description: There is both a low hugepage region (at 2-3G for use by
20  * 32-bit processes) and a high hugepage region (at 1-1.5T).  The high region
21  * is always exclusively for hugepages, but the low region has to be activated
22  * before it can be used for hugepages.  When the kernel attempts to do a
23  * hugepage mapping in a 32-bit process it will automatically attempt to open
24  * the low region.  However, that will fail if there are any normal
25  * (non-hugepage) mappings in the region already.
26  *
27  * When run as a 64-bit process the kernel will still do a non-hugepage mapping
28  * in the low region, but the following hugepage mapping will succeed. This is
29  * because it comes from the high region, which is available to the 64-bit
30  * process.
31  *
32  * This test case is checking this behavior.
33  *
34  * HISTORY
35  *  04/2004 Written by Robbie Williamson
36  */
37 
38 #include <stdio.h>
39 #include <sys/mount.h>
40 #include <limits.h>
41 #include <sys/param.h>
42 #include "hugetlb.h"
43 
44 #define LOW_ADDR       0x80000000
45 #define LOW_ADDR2      0x90000000
46 
47 static struct tst_option options[] = {
48 	{"H:", &Hopt,   "-H   /..  Location of hugetlbfs, i.e.  -H /var/hugetlbfs"},
49 	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
50 	{NULL, NULL, NULL}
51 };
52 
53 static char TEMPFILE[MAXPATHLEN];
54 
55 static unsigned long *addr;
56 static unsigned long *addr2;
57 static unsigned long low_addr = LOW_ADDR;
58 static unsigned long low_addr2 = LOW_ADDR2;
59 static unsigned long *addrlist[5];
60 static int fildes;
61 static int nfildes;
62 static long hugepages = 128;
63 
test_hugemmap(void)64 static void test_hugemmap(void)
65 {
66 	int i;
67 	long page_sz, map_sz;
68 
69 	page_sz = getpagesize();
70 	map_sz = SAFE_READ_MEMINFO("Hugepagesize:") * 1024 * 2;
71 
72 	fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
73 
74 	nfildes = SAFE_OPEN("/dev/zero", O_RDONLY, 0666);
75 
76 	for (i = 0; i < 5; i++) {
77 		addr = mmap(0, 256 * 1024 * 1024, PROT_READ,
78 				MAP_SHARED, nfildes, 0);
79 		addrlist[i] = addr;
80 	}
81 
82 	while (range_is_mapped(low_addr, low_addr + map_sz) == 1) {
83 		low_addr = low_addr + 0x10000000;
84 
85 		if (low_addr < LOW_ADDR)
86 			tst_brk(TBROK | TERRNO, "no empty region to use");
87 	}
88 	/* mmap using normal pages and a low memory address */
89 	addr = mmap((void *)low_addr, page_sz, PROT_READ,
90 			MAP_SHARED | MAP_FIXED, nfildes, 0);
91 	if (addr == MAP_FAILED)
92 		tst_brk(TBROK | TERRNO, "mmap failed on nfildes");
93 
94 	while (range_is_mapped(low_addr2, low_addr2 + map_sz) == 1) {
95 		low_addr2 = low_addr2 + 0x10000000;
96 
97 		if (low_addr2 < LOW_ADDR2)
98 			tst_brk(TBROK | TERRNO, "no empty region to use");
99 	}
100 	/* Attempt to mmap a huge page into a low memory address */
101 	addr2 = mmap((void *)low_addr2, map_sz, PROT_READ | PROT_WRITE,
102 			MAP_SHARED, fildes, 0);
103 #if __WORDSIZE == 64 /* 64-bit process */
104 	if (addr2 == MAP_FAILED) {
105 		tst_res(TFAIL | TERRNO, "huge mmap failed unexpectedly"
106 				" with %s (64-bit)", TEMPFILE);
107 	} else {
108 		tst_res(TPASS, "huge mmap succeeded (64-bit)");
109 	}
110 #else /* 32-bit process */
111 	if (addr2 == MAP_FAILED)
112 		tst_res(TFAIL | TERRNO, "huge mmap failed unexpectedly"
113 				" with %s (32-bit)", TEMPFILE);
114 	else if (addr2 > 0) {
115 		tst_res(TCONF,
116 				"huge mmap failed to test the scenario");
117 	} else if (addr == 0)
118 		tst_res(TPASS, "huge mmap succeeded (32-bit)");
119 #endif
120 
121 	for (i = 0; i < 5; i++) {
122 		if (munmap(addrlist[i], 256 * 1024 * 1024) == -1)
123 			tst_res(TFAIL | TERRNO,
124 					"munmap of addrlist[%d] failed", i);
125 	}
126 
127 	if (munmap(addr2, map_sz) == -1)
128 		tst_res(TFAIL | TERRNO, "huge munmap failed");
129 	if (munmap(addr, page_sz) == -1)
130 		tst_res(TFAIL | TERRNO, "munmap failed");
131 
132 	close(nfildes);
133 	close(fildes);
134 }
135 
setup(void)136 static void setup(void)
137 {
138 	save_nr_hugepages();
139 
140 	if (!Hopt)
141 		Hopt = tst_get_tmpdir();
142 	SAFE_MOUNT("none", Hopt, "hugetlbfs", 0, NULL);
143 
144 	if (nr_opt)
145 		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
146 	set_sys_tune("nr_hugepages", hugepages, 1);
147 
148 	snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt, getpid());
149 }
150 
cleanup(void)151 static void cleanup(void)
152 {
153 	unlink(TEMPFILE);
154 	restore_nr_hugepages();
155 
156 	umount(Hopt);
157 }
158 
159 static struct tst_test test = {
160 	.needs_root = 1,
161 	.needs_tmpdir = 1,
162 	.options = options,
163 	.setup = setup,
164 	.cleanup = cleanup,
165 	.test_all = test_hugemmap,
166 };
167