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 char TEMPFILE[MAXPATHLEN];
38
39 static unsigned long *addr;
40 static unsigned long *addr2;
41 static unsigned long low_addr = LOW_ADDR;
42 static unsigned long low_addr2 = LOW_ADDR2;
43 static unsigned long *addrlist[5];
44 static int fildes;
45 static int nfildes;
46
test_hugemmap(void)47 static void test_hugemmap(void)
48 {
49 int i;
50 long page_sz, map_sz;
51
52 page_sz = getpagesize();
53 map_sz = SAFE_READ_MEMINFO("Hugepagesize:") * 1024 * 2;
54
55 fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
56
57 nfildes = SAFE_OPEN("/dev/zero", O_RDONLY, 0666);
58
59 for (i = 0; i < 5; i++) {
60 addr = mmap(0, 256 * 1024 * 1024, PROT_READ,
61 MAP_SHARED, nfildes, 0);
62 addrlist[i] = addr;
63 }
64
65 while (range_is_mapped(low_addr, low_addr + map_sz) == 1) {
66 low_addr = low_addr + 0x10000000;
67
68 if (low_addr < LOW_ADDR)
69 tst_brk(TBROK | TERRNO, "no empty region to use");
70 }
71 /* mmap using normal pages and a low memory address */
72 addr = mmap((void *)low_addr, page_sz, PROT_READ,
73 MAP_SHARED | MAP_FIXED, nfildes, 0);
74 if (addr == MAP_FAILED)
75 tst_brk(TBROK | TERRNO, "mmap failed on nfildes");
76
77 while (range_is_mapped(low_addr2, low_addr2 + map_sz) == 1) {
78 low_addr2 = low_addr2 + 0x10000000;
79
80 if (low_addr2 < LOW_ADDR2)
81 tst_brk(TBROK | TERRNO, "no empty region to use");
82 }
83 /* Attempt to mmap a huge page into a low memory address */
84 addr2 = mmap((void *)low_addr2, map_sz, PROT_READ | PROT_WRITE,
85 MAP_SHARED, fildes, 0);
86 #ifdef TST_ABI64 /* 64-bit process */
87 if (addr2 == MAP_FAILED) {
88 tst_res(TFAIL | TERRNO, "huge mmap failed unexpectedly"
89 " with %s (64-bit)", TEMPFILE);
90 } else {
91 tst_res(TPASS, "huge mmap succeeded (64-bit)");
92 }
93 #else /* 32-bit process */
94 if (addr2 == MAP_FAILED)
95 tst_res(TFAIL | TERRNO, "huge mmap failed unexpectedly"
96 " with %s (32-bit)", TEMPFILE);
97 else if (addr2 > 0) {
98 tst_res(TCONF,
99 "huge mmap failed to test the scenario");
100 } else if (addr == 0)
101 tst_res(TPASS, "huge mmap succeeded (32-bit)");
102 #endif
103
104 for (i = 0; i < 5; i++) {
105 if (munmap(addrlist[i], 256 * 1024 * 1024) == -1)
106 tst_res(TFAIL | TERRNO,
107 "munmap of addrlist[%d] failed", i);
108 }
109
110 if (munmap(addr2, map_sz) == -1)
111 tst_res(TFAIL | TERRNO, "huge munmap failed");
112 if (munmap(addr, page_sz) == -1)
113 tst_res(TFAIL | TERRNO, "munmap failed");
114
115 close(nfildes);
116 close(fildes);
117 }
118
setup(void)119 static void setup(void)
120 {
121 if (tst_hugepages == 0)
122 tst_brk(TCONF, "Not enough hugepages for testing.");
123
124 if (!Hopt)
125 Hopt = tst_get_tmpdir();
126 SAFE_MOUNT("none", Hopt, "hugetlbfs", 0, NULL);
127
128 snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt, getpid());
129 }
130
cleanup(void)131 static void cleanup(void)
132 {
133 unlink(TEMPFILE);
134 umount(Hopt);
135 }
136
137 static struct tst_test test = {
138 .needs_root = 1,
139 .needs_tmpdir = 1,
140 .options = (struct tst_option[]) {
141 {"H:", &Hopt, "Location of hugetlbfs, i.e. -H /var/hugetlbfs"},
142 {"s:", &nr_opt, "Set the number of the been allocated hugepages"},
143 {}
144 },
145 .setup = setup,
146 .cleanup = cleanup,
147 .test_all = test_hugemmap,
148 .request_hugepages = 128,
149 };
150