• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MOUNT_DIR "hugemmap05"
55 #define TEST_FILE MOUNT_DIR "/file"
56 
57 static unsigned long long shmmax;
58 static char *path, *pathover;
59 static int key = -1, shmid = -1, fd = -1;
60 static int mounted, restore_shmmax, restore_nr_hgpgs, restore_overcomm_hgpgs;
61 static long hugepagesize, nr_hugepages, nr_overcommit_hugepages;
62 static long size = 128, length = 384;
63 
64 char *opt_sysfs;
65 char *opt_alloc;
66 char *opt_shmid;
67 static struct tst_option options[] = {
68 	{"s",  &opt_sysfs, "-s        Setup hugepages from sysfs"},
69 	{"m",  &opt_shmid, "-m        Reserve hugepages by shmget"},
70 	{"a:", &opt_alloc, "-a        Number of overcommint hugepages"},
71 	{NULL, NULL, NULL}
72 };
73 
74 static void check_wr_bytes(void *addr);
75 static int checkproc(long act_val, char *string, long exp_val);
76 static int checksys(char *path, char *pattern, long exp_val);
77 static void init_sys_sz_paths(void);
78 
test_overcommit(void)79 static void test_overcommit(void)
80 {
81 	void *addr = NULL, *shmaddr = NULL;
82 
83 	if (opt_shmid) {
84 		shmid = SAFE_SHMGET(key, (length / 2 * hugepagesize),
85 				 SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
86 	} else {
87 		fd = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR, 0755);
88 		addr = SAFE_MMAP(ADDR, (length / 2 * hugepagesize),
89 				 PROTECTION, FLAGS, fd, 0);
90 	}
91 
92 	if (opt_sysfs) {
93 		tst_res(TINFO, "check sysfs before allocation.");
94 		if (checksys(path_sys_sz_huge, "HugePages_Total", length / 2))
95 			return;
96 		if (checksys(path_sys_sz_free, "HugePages_Free", length / 2))
97 			return;
98 		if (checksys(path_sys_sz_surp, "HugePages_Surp",
99 			     length / 2 - size))
100 			return;
101 		if (checksys(path_sys_sz_resv, "HugePages_Rsvd", length / 2))
102 			return;
103 	} else {
104 		tst_res(TINFO, "check /proc/meminfo before allocation.");
105 		if (checkproc(SAFE_READ_MEMINFO("HugePages_Total:"),
106 			      "HugePages_Total", length / 2))
107 			return;
108 		if (checkproc(SAFE_READ_MEMINFO("HugePages_Free:"),
109 			      "HugePages_Free", length / 2))
110 			return;
111 		if (checkproc(SAFE_READ_MEMINFO("HugePages_Surp:"),
112 			      "HugePages_Surp", length / 2 - size))
113 			return;
114 		if (checkproc(SAFE_READ_MEMINFO("HugePages_Rsvd:"),
115 			      "HugePages_Rsvd", length / 2))
116 			return;
117 	}
118 
119 	if (opt_shmid) {
120 		tst_res(TINFO, "shmid: 0x%x", shmid);
121 		shmaddr = SAFE_SHMAT(shmid, ADDR, SHMAT_FLAGS);
122 		check_wr_bytes(shmaddr);
123 	} else {
124 		check_wr_bytes(addr);
125 	}
126 
127 	if (opt_sysfs) {
128 		tst_res(TINFO, "check sysfs.");
129 		if (checksys(path_sys_sz_huge, "HugePages_Total", length / 2))
130 			return;
131 		if (checksys(path_sys_sz_free, "HugePages_Free", 0))
132 			return;
133 		if (checksys(path_sys_sz_surp, "HugePages_Surp",
134 			     length / 2 - size))
135 			return;
136 		if (checksys(path_sys_sz_resv, "HugePages_Rsvd", 0))
137 			return;
138 	} else {
139 		tst_res(TINFO, "check /proc/meminfo.");
140 		if (checkproc(SAFE_READ_MEMINFO("HugePages_Total:"),
141 			      "HugePages_Total", length / 2))
142 			return;
143 		if (checkproc(SAFE_READ_MEMINFO("HugePages_Free:"),
144 			      "HugePages_Free", 0))
145 			return;
146 		if (checkproc(SAFE_READ_MEMINFO("HugePages_Surp:"),
147 			      "HugePages_Surp", length / 2 - size))
148 			return;
149 		if (checkproc(SAFE_READ_MEMINFO("HugePages_Rsvd:"),
150 			      "HugePages_Rsvd", 0))
151 			return;
152 	}
153 
154 	if (opt_shmid) {
155 		SAFE_SHMDT(shmaddr);
156 		SAFE_SHMCTL(shmid, IPC_RMID, NULL);
157 	} else {
158 		SAFE_MUNMAP(addr, (length / 2 * hugepagesize));
159 		SAFE_CLOSE(fd);
160 		SAFE_UNLINK(TEST_FILE);
161 	}
162 
163 	tst_res(TPASS, "hugepages overcommit test pass");
164 }
165 
cleanup(void)166 static void cleanup(void)
167 {
168 	if (opt_shmid && shmid != -1)
169 		SAFE_SHMCTL(shmid, IPC_RMID, NULL);
170 
171 	if (!opt_shmid && fd != -1) {
172 		SAFE_CLOSE(fd);
173 		SAFE_UNLINK(TEST_FILE);
174 	}
175 
176 	if (mounted)
177 		tst_umount(MOUNT_DIR);
178 
179 	if (restore_nr_hgpgs) {
180 		tst_res(TINFO, "restore nr_hugepages to %ld.", nr_hugepages);
181 		SAFE_FILE_PRINTF(path, "%ld", nr_hugepages);
182 	}
183 
184 	if (restore_shmmax)
185 		SAFE_FILE_PRINTF(PATH_SHMMAX, "%llu", shmmax);
186 
187 	if (restore_overcomm_hgpgs) {
188 		tst_res(TINFO, "restore nr_overcommit_hugepages to %ld.",
189 			nr_overcommit_hugepages);
190 		SAFE_FILE_PRINTF(pathover, "%ld", nr_overcommit_hugepages);
191 	}
192 }
193 
setup(void)194 static void setup(void)
195 {
196 	check_hugepage();
197 	hugepagesize = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
198 	init_sys_sz_paths();
199 
200 	if (opt_sysfs) {
201 		path = path_sys_sz_huge;
202 		pathover = path_sys_sz_over;
203 	} else {
204 		path = PATH_PROC_HUGE;
205 		pathover = PATH_PROC_OVER;
206 	}
207 
208 	if (opt_alloc) {
209 		size = atoi(opt_alloc);
210 		length = (size + size * 0.5) * 2;
211 	}
212 
213 	if (opt_shmid) {
214 		SAFE_FILE_SCANF(PATH_SHMMAX, "%llu", &shmmax);
215 		if (shmmax < (unsigned long long)(length / 2 * hugepagesize)) {
216 			restore_shmmax = 1;
217 			SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld",
218 					(length / 2 * hugepagesize));
219 		}
220 	}
221 
222 	SAFE_FILE_SCANF(path, "%ld", &nr_hugepages);
223 	tst_res(TINFO, "original nr_hugepages is %ld", nr_hugepages);
224 
225 	/* Reset. */
226 	SAFE_FILE_PRINTF(path, "%ld", size);
227 	restore_nr_hgpgs = 1;
228 
229 	if (access(pathover, F_OK)) {
230 		tst_brk(TCONF, "file %s does not exist in the system",
231 			pathover);
232 	}
233 
234 	SAFE_FILE_SCANF(pathover, "%ld", &nr_overcommit_hugepages);
235 	tst_res(TINFO, "original nr_overcommit_hugepages is %ld",
236 		nr_overcommit_hugepages);
237 
238 	/* Reset. */
239 	SAFE_FILE_PRINTF(pathover, "%ld", size);
240 	restore_overcomm_hgpgs = 1;
241 
242 	SAFE_MKDIR(MOUNT_DIR, 0700);
243 	SAFE_MOUNT(NULL, MOUNT_DIR, "hugetlbfs", 0, NULL);
244 	mounted = 1;
245 
246 	if (opt_shmid) {
247 		/* Use /proc/meminfo to generate an IPC key. */
248 		key = ftok(PATH_MEMINFO, strlen(PATH_MEMINFO));
249 		if (key == -1)
250 			tst_brk(TBROK | TERRNO, "ftok");
251 	}
252 }
253 
check_wr_bytes(void * addr)254 static void check_wr_bytes(void *addr)
255 {
256 	long i;
257 
258 	memset((char *)addr, '\a', (length / 2 * hugepagesize));
259 
260 	tst_res(TINFO, "First hex is %x", *((unsigned int *)addr));
261 	for (i = 0; i < (length / 2 * hugepagesize); i++) {
262 		if (((char *)addr)[i] != '\a') {
263 			tst_res(TFAIL, "mismatch at %ld", i);
264 			break;
265 		}
266 	}
267 }
268 
checksys(char * path,char * string,long exp_val)269 static int checksys(char *path, char *string, long exp_val)
270 {
271 	long act_val;
272 
273 	SAFE_FILE_SCANF(path, "%ld", &act_val);
274 	tst_res(TINFO, "%s is %ld.", string, act_val);
275 	if (act_val != exp_val) {
276 		tst_res(TFAIL, "%s is not %ld but %ld.", string, exp_val,
277 			act_val);
278 		return 1;
279 	}
280 	return 0;
281 }
282 
checkproc(long act_val,char * pattern,long exp_val)283 static int checkproc(long act_val, char *pattern, long exp_val)
284 {
285 	tst_res(TINFO, "%s is %ld.", pattern, act_val);
286 	if (act_val != exp_val) {
287 		tst_res(TFAIL, "%s is not %ld but %ld.",
288 			pattern, exp_val, act_val);
289 		return 1;
290 	}
291 	return 0;
292 }
293 
init_sys_sz_paths(void)294 static void init_sys_sz_paths(void)
295 {
296 	sprintf(path_sys_sz, "/sys/kernel/mm/hugepages/hugepages-%ldkB",
297 		hugepagesize / 1024);
298 	sprintf(path_sys_sz_over, "%s/nr_overcommit_hugepages", path_sys_sz);
299 	sprintf(path_sys_sz_free, "%s/free_hugepages", path_sys_sz);
300 	sprintf(path_sys_sz_resv, "%s/resv_hugepages", path_sys_sz);
301 	sprintf(path_sys_sz_surp, "%s/surplus_hugepages", path_sys_sz);
302 	sprintf(path_sys_sz_huge, "%s/nr_hugepages", path_sys_sz);
303 }
304 
305 static struct tst_test test = {
306 	.needs_root = 1,
307 	.needs_tmpdir = 1,
308 	.options = options,
309 	.setup = setup,
310 	.cleanup = cleanup,
311 	.test_all = test_overcommit,
312 };
313