1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2010-2017 Red Hat, Inc.
4 *
5 * hugetlbfs allows to overcommit hugepages and there are tunables in
6 * sysfs and procfs. The test here want to ensure it is possible to
7 * overcommit by either mmap or shared memory. Also ensure those
8 * reservation can be read/write, and several statistics work correctly.
9 *
10 * First, it resets nr_hugepages and nr_overcommit_hugepages. Then, set
11 * both to a specify value - N, and allocate N + %50 x N hugepages.
12 * Finally, it reads and writes every page. There are command options to
13 * choose either to manage hugepages from sysfs or procfs, and reserve
14 * them by mmap or shmget.
15 */
16
17 #include <string.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include "hugetlb.h"
21 #include "tst_safe_sysv_ipc.h"
22 #include "tst_test.h"
23
24 #define PROTECTION (PROT_READ | PROT_WRITE)
25 #define PATH_MEMINFO "/proc/meminfo"
26
27 static char path_sys_sz[BUFSIZ];
28 static char path_sys_sz_over[BUFSIZ];
29 static char path_sys_sz_free[BUFSIZ];
30 static char path_sys_sz_resv[BUFSIZ];
31 static char path_sys_sz_surp[BUFSIZ];
32 static char path_sys_sz_huge[BUFSIZ];
33
34 #define PATH_PROC_VM "/proc/sys/vm/"
35 #define PATH_PROC_OVER PATH_PROC_VM "nr_overcommit_hugepages"
36 #define PATH_PROC_HUGE PATH_PROC_VM "nr_hugepages"
37 #define PATH_SHMMAX "/proc/sys/kernel/shmmax"
38
39 /* Only ia64 requires this */
40 #ifdef __ia64__
41 #define ADDR (void *)(0x8000000000000000UL)
42 #define FLAGS (MAP_SHARED | MAP_FIXED)
43 #define SHMAT_FLAGS (SHM_RND)
44 #else
45 #define ADDR (void *)(0x0UL)
46 #define FLAGS (MAP_SHARED)
47 #define SHMAT_FLAGS (0)
48 #endif
49
50 #ifndef SHM_HUGETLB
51 #define SHM_HUGETLB 04000
52 #endif
53
54 #define NR_HPAGES 2
55 #define MOUNT_DIR "hugemmap05"
56 #define TEST_FILE MOUNT_DIR "/file"
57
58 static unsigned long long shmmax;
59 static char *path, *pathover;
60 static int key = -1, shmid = -1, fd = -1;
61 static int mounted, restore_shmmax, restore_overcomm_hgpgs;
62 static long hugepagesize, nr_overcommit_hugepages;
63 static long size = NR_HPAGES, length = (NR_HPAGES + NR_HPAGES/2) * 2;
64
65 char *opt_sysfs;
66 char *opt_alloc;
67 char *opt_shmid;
68
69 static void check_wr_bytes(void *addr);
70 static int checkproc(long act_val, char *string, long exp_val);
71 static int checksys(char *path, char *pattern, long exp_val);
72 static void init_sys_sz_paths(void);
73
test_overcommit(void)74 static void test_overcommit(void)
75 {
76 void *addr = NULL, *shmaddr = NULL;
77
78 if (opt_shmid) {
79 shmid = SAFE_SHMGET(key, (length / 2 * hugepagesize),
80 SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
81 } else {
82 fd = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR, 0755);
83 addr = SAFE_MMAP(ADDR, (length / 2 * hugepagesize),
84 PROTECTION, FLAGS, fd, 0);
85 }
86
87 if (opt_sysfs) {
88 tst_res(TINFO, "check sysfs before allocation.");
89 if (checksys(path_sys_sz_huge, "HugePages_Total", length / 2))
90 return;
91 if (checksys(path_sys_sz_free, "HugePages_Free", length / 2))
92 return;
93 if (checksys(path_sys_sz_surp, "HugePages_Surp",
94 length / 2 - size))
95 return;
96 if (checksys(path_sys_sz_resv, "HugePages_Rsvd", length / 2))
97 return;
98 } else {
99 tst_res(TINFO, "check /proc/meminfo before allocation.");
100 if (checkproc(SAFE_READ_MEMINFO("HugePages_Total:"),
101 "HugePages_Total", length / 2))
102 return;
103 if (checkproc(SAFE_READ_MEMINFO("HugePages_Free:"),
104 "HugePages_Free", length / 2))
105 return;
106 if (checkproc(SAFE_READ_MEMINFO("HugePages_Surp:"),
107 "HugePages_Surp", length / 2 - size))
108 return;
109 if (checkproc(SAFE_READ_MEMINFO("HugePages_Rsvd:"),
110 "HugePages_Rsvd", length / 2))
111 return;
112 }
113
114 if (opt_shmid) {
115 tst_res(TINFO, "shmid: 0x%x", shmid);
116 shmaddr = SAFE_SHMAT(shmid, ADDR, SHMAT_FLAGS);
117 check_wr_bytes(shmaddr);
118 } else {
119 check_wr_bytes(addr);
120 }
121
122 if (opt_sysfs) {
123 tst_res(TINFO, "check sysfs.");
124 if (checksys(path_sys_sz_huge, "HugePages_Total", length / 2))
125 return;
126 if (checksys(path_sys_sz_free, "HugePages_Free", 0))
127 return;
128 if (checksys(path_sys_sz_surp, "HugePages_Surp",
129 length / 2 - size))
130 return;
131 if (checksys(path_sys_sz_resv, "HugePages_Rsvd", 0))
132 return;
133 } else {
134 tst_res(TINFO, "check /proc/meminfo.");
135 if (checkproc(SAFE_READ_MEMINFO("HugePages_Total:"),
136 "HugePages_Total", length / 2))
137 return;
138 if (checkproc(SAFE_READ_MEMINFO("HugePages_Free:"),
139 "HugePages_Free", 0))
140 return;
141 if (checkproc(SAFE_READ_MEMINFO("HugePages_Surp:"),
142 "HugePages_Surp", length / 2 - size))
143 return;
144 if (checkproc(SAFE_READ_MEMINFO("HugePages_Rsvd:"),
145 "HugePages_Rsvd", 0))
146 return;
147 }
148
149 if (opt_shmid) {
150 SAFE_SHMDT(shmaddr);
151 SAFE_SHMCTL(shmid, IPC_RMID, NULL);
152 } else {
153 SAFE_MUNMAP(addr, (length / 2 * hugepagesize));
154 SAFE_CLOSE(fd);
155 SAFE_UNLINK(TEST_FILE);
156 }
157
158 tst_res(TPASS, "hugepages overcommit test pass");
159 }
160
cleanup(void)161 static void cleanup(void)
162 {
163 if (opt_shmid && shmid != -1)
164 SAFE_SHMCTL(shmid, IPC_RMID, NULL);
165
166 if (!opt_shmid && fd != -1) {
167 SAFE_CLOSE(fd);
168 SAFE_UNLINK(TEST_FILE);
169 }
170
171 if (mounted)
172 tst_umount(MOUNT_DIR);
173
174 if (restore_shmmax)
175 SAFE_FILE_PRINTF(PATH_SHMMAX, "%llu", shmmax);
176
177 if (restore_overcomm_hgpgs) {
178 tst_res(TINFO, "restore nr_overcommit_hugepages to %ld.",
179 nr_overcommit_hugepages);
180 SAFE_FILE_PRINTF(pathover, "%ld", nr_overcommit_hugepages);
181 }
182 }
183
setup(void)184 static void setup(void)
185 {
186 unsigned long hpages;
187
188 hugepagesize = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
189 init_sys_sz_paths();
190
191 if (opt_sysfs) {
192 path = path_sys_sz_huge;
193 pathover = path_sys_sz_over;
194 } else {
195 path = PATH_PROC_HUGE;
196 pathover = PATH_PROC_OVER;
197 }
198
199 if (opt_alloc) {
200 size = atoi(opt_alloc);
201 length = (size + size * 0.5) * 2;
202 }
203
204 if (opt_shmid) {
205 SAFE_FILE_SCANF(PATH_SHMMAX, "%llu", &shmmax);
206 if (shmmax < (unsigned long long)(length / 2 * hugepagesize)) {
207 restore_shmmax = 1;
208 SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld",
209 (length / 2 * hugepagesize));
210 }
211 }
212
213 /* Reset. */
214 SAFE_FILE_PRINTF(path, "%ld", size);
215 SAFE_FILE_SCANF(path, "%lu", &hpages);
216 if (hpages != size)
217 tst_brk(TCONF, "Not enough hugepages for testing!");
218
219 if (access(pathover, F_OK)) {
220 tst_brk(TCONF, "file %s does not exist in the system",
221 pathover);
222 }
223
224 SAFE_FILE_SCANF(pathover, "%ld", &nr_overcommit_hugepages);
225 tst_res(TINFO, "original nr_overcommit_hugepages is %ld",
226 nr_overcommit_hugepages);
227
228 /* Reset. */
229 SAFE_FILE_PRINTF(pathover, "%ld", size);
230 restore_overcomm_hgpgs = 1;
231
232 SAFE_MKDIR(MOUNT_DIR, 0700);
233 SAFE_MOUNT(NULL, MOUNT_DIR, "hugetlbfs", 0, NULL);
234 mounted = 1;
235
236 if (opt_shmid) {
237 /* Use /proc/meminfo to generate an IPC key. */
238 key = ftok(PATH_MEMINFO, strlen(PATH_MEMINFO));
239 if (key == -1)
240 tst_brk(TBROK | TERRNO, "ftok");
241 }
242 }
243
check_wr_bytes(void * addr)244 static void check_wr_bytes(void *addr)
245 {
246 long i;
247
248 memset((char *)addr, '\a', (length / 2 * hugepagesize));
249
250 tst_res(TINFO, "First hex is %x", *((unsigned int *)addr));
251 for (i = 0; i < (length / 2 * hugepagesize); i++) {
252 if (((char *)addr)[i] != '\a') {
253 tst_res(TFAIL, "mismatch at %ld", i);
254 break;
255 }
256 }
257 }
258
checksys(char * path,char * string,long exp_val)259 static int checksys(char *path, char *string, long exp_val)
260 {
261 long act_val;
262
263 SAFE_FILE_SCANF(path, "%ld", &act_val);
264 tst_res(TINFO, "%s is %ld.", string, act_val);
265 if (act_val != exp_val) {
266 tst_res(TFAIL, "%s is not %ld but %ld.", string, exp_val,
267 act_val);
268 return 1;
269 }
270 return 0;
271 }
272
checkproc(long act_val,char * pattern,long exp_val)273 static int checkproc(long act_val, char *pattern, long exp_val)
274 {
275 tst_res(TINFO, "%s is %ld.", pattern, act_val);
276 if (act_val != exp_val) {
277 tst_res(TFAIL, "%s is not %ld but %ld.",
278 pattern, exp_val, act_val);
279 return 1;
280 }
281 return 0;
282 }
283
init_sys_sz_paths(void)284 static void init_sys_sz_paths(void)
285 {
286 sprintf(path_sys_sz, "/sys/kernel/mm/hugepages/hugepages-%ldkB",
287 hugepagesize / 1024);
288 sprintf(path_sys_sz_over, "%s/nr_overcommit_hugepages", path_sys_sz);
289 sprintf(path_sys_sz_free, "%s/free_hugepages", path_sys_sz);
290 sprintf(path_sys_sz_resv, "%s/resv_hugepages", path_sys_sz);
291 sprintf(path_sys_sz_surp, "%s/surplus_hugepages", path_sys_sz);
292 sprintf(path_sys_sz_huge, "%s/nr_hugepages", path_sys_sz);
293 }
294
295 static struct tst_test test = {
296 .needs_root = 1,
297 .needs_tmpdir = 1,
298 .options = (struct tst_option[]) {
299 {"s", &opt_sysfs, "Setup hugepages from sysfs"},
300 {"m", &opt_shmid, "Reserve hugepages by shmget"},
301 {"a:", &opt_alloc, "Number of overcommint hugepages"},
302 {}
303 },
304 .setup = setup,
305 .cleanup = cleanup,
306 .test_all = test_overcommit,
307 .hugepages = {NR_HPAGES, TST_NEEDS},
308 };
309