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