• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * overcommit hugetlbfs and check the statistics.
3  *
4  * hugetlbfs allows to overcommit hugepages and there are tunables in
5  * sysfs and procfs. The test here want to ensure it is possible to
6  * overcommit by either mmap or shared memory. Also ensure those
7  * reservation can be read/write, and several statistics work correctly.
8  *
9  * First, it resets nr_hugepages and nr_overcommit_hugepages. Then, set
10  * both to a specify value - N, and allocate N + %50 x N hugepages.
11  * Finally, it reads and writes every page. There are command options to
12  * choose either to manage hugepages from sysfs or procfs, and reserve
13  * them by mmap or shmget.
14  *
15  * Copyright (C) 2010  Red Hat, Inc.
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of version 2 of the GNU General Public
18  * License as published by the Free Software Foundation.
19  *
20  * This program is distributed in the hope that it would be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  *
24  * Further, this software is distributed without any warranty that it
25  * is free of the rightful claim of any third person regarding
26  * infringement or the like.  Any license provided herein, whether
27  * implied or otherwise, applies only to this software file.  Patent
28  * licenses, if any, provided herein do not apply to combinations of
29  * this program with other software, or any other product whatsoever.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write the Free Software
33  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34  * 02110-1301, USA.
35  */
36 #include <sys/mman.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <sys/mount.h>
40 #include <sys/shm.h>
41 #include <sys/ipc.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include "test.h"
51 #include "hugetlb.h"
52 
53 #define PROTECTION		(PROT_READ | PROT_WRITE)
54 #define PATH_MEMINFO		"/proc/meminfo"
55 
56 char path_sys_sz[BUFSIZ];
57 char path_sys_sz_over[BUFSIZ];
58 char path_sys_sz_free[BUFSIZ];
59 char path_sys_sz_resv[BUFSIZ];
60 char path_sys_sz_surp[BUFSIZ];
61 char path_sys_sz_huge[BUFSIZ];
62 
63 #define PATH_PROC_VM		"/proc/sys/vm/"
64 #define PATH_PROC_OVER		PATH_PROC_VM "nr_overcommit_hugepages"
65 #define PATH_PROC_HUGE		PATH_PROC_VM "nr_hugepages"
66 #define PATH_SHMMAX		"/proc/sys/kernel/shmmax"
67 
68 /* Only ia64 requires this */
69 #ifdef __ia64__
70 #define ADDR (void *)(0x8000000000000000UL)
71 #define FLAGS (MAP_SHARED | MAP_FIXED)
72 #define SHMAT_FLAGS (SHM_RND)
73 #else
74 #define ADDR (void *)(0x0UL)
75 #define FLAGS (MAP_SHARED)
76 #define SHMAT_FLAGS (0)
77 #endif
78 
79 #ifndef SHM_HUGETLB
80 #define SHM_HUGETLB 04000
81 #endif
82 
83 char *TCID = "hugemmap05";
84 int TST_TOTAL = 1, tst_count;
85 static char nr_hugepages[BUFSIZ], nr_overcommit_hugepages[BUFSIZ];
86 static char buf[BUFSIZ], line[BUFSIZ], path[BUFSIZ], pathover[BUFSIZ];
87 static char shmmax[BUFSIZ];
88 static char *opt_allocstr;
89 static int hugepagesize;	/* in Bytes */
90 static int opt_sysfs, opt_alloc;
91 static int shmid = -1;
92 static int restore_shmmax;
93 static size_t size = 128, length = 384;
94 static option_t options[] = {
95 	{"s", &opt_sysfs, NULL},
96 	{"m", &shmid, NULL},
97 	{"a:", &opt_alloc, &opt_allocstr},
98 	{NULL, NULL, NULL}
99 };
100 
101 static void setup(void);
102 static void cleanup(void);
103 static void overcommit(void);
104 static void write_bytes(void *addr);
105 static void read_bytes(void *addr);
106 static int lookup(char *line, char *pattern);
107 static void usage(void);
108 static int checkproc(FILE * fp, char *string, int value);
109 static int checksys(char *path, char *pattern, int value);
110 static void init_hugepagesize(void);
111 static void init_sys_sz_paths(void);
112 
main(int argc,char * argv[])113 int main(int argc, char *argv[])
114 {
115 	int lc;
116 
117 	init_hugepagesize();
118 	init_sys_sz_paths();
119 
120 	tst_parse_opts(argc, argv, options, usage);
121 	if (opt_sysfs) {
122 		strncpy(path, path_sys_sz_huge, strlen(path_sys_sz_huge) + 1);
123 		strncpy(pathover, path_sys_sz_over,
124 			strlen(path_sys_sz_over) + 1);
125 	} else {
126 		strncpy(path, PATH_PROC_HUGE, strlen(PATH_PROC_HUGE) + 1);
127 		strncpy(pathover, PATH_PROC_OVER, strlen(PATH_PROC_OVER) + 1);
128 	}
129 	if (opt_alloc) {
130 		size = atoi(opt_allocstr);
131 		length = (int)(size + size * 0.5) * 2;
132 	}
133 	setup();
134 	for (lc = 0; TEST_LOOPING(lc); lc++) {
135 		tst_count = 0;
136 		overcommit();
137 	}
138 	cleanup();
139 	tst_exit();
140 }
141 
overcommit(void)142 static void overcommit(void)
143 {
144 	void *addr = NULL, *shmaddr = NULL;
145 	int fd = -1, key = -1;
146 	char s[BUFSIZ];
147 	FILE *fp;
148 
149 	if (shmid != -1) {
150 		/* Use /proc/meminfo to generate an IPC key. */
151 		key = ftok(PATH_MEMINFO, strlen(PATH_MEMINFO));
152 		if (key == -1)
153 			tst_brkm(TBROK | TERRNO, cleanup, "ftok");
154 		shmid = shmget(key, (long)(length / 2 * hugepagesize),
155 			       SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
156 		if (shmid == -1)
157 			tst_brkm(TBROK | TERRNO, cleanup, "shmget");
158 	} else {
159 		/* XXX (garrcoop): memory leak. */
160 		snprintf(s, BUFSIZ, "%s/hugemmap05/file", tst_get_tmpdir());
161 		fd = open(s, O_CREAT | O_RDWR, 0755);
162 		if (fd == -1)
163 			tst_brkm(TBROK | TERRNO, cleanup, "open");
164 		addr = mmap(ADDR, (long)(length / 2 * hugepagesize), PROTECTION,
165 			    FLAGS, fd, 0);
166 		if (addr == MAP_FAILED) {
167 			close(fd);
168 			tst_brkm(TBROK | TERRNO, cleanup, "mmap");
169 		}
170 	}
171 
172 	if (opt_sysfs) {
173 		tst_resm(TINFO, "check sysfs before allocation.");
174 		if (checksys(path_sys_sz_huge, "HugePages_Total",
175 			     length / 2) != 0)
176 			return;
177 		if (checksys(path_sys_sz_free, "HugePages_Free",
178 			     length / 2) != 0)
179 			return;
180 		if (checksys(path_sys_sz_surp, "HugePages_Surp",
181 			     length / 2 - size) != 0)
182 			return;
183 		if (checksys(path_sys_sz_resv, "HugePages_Rsvd",
184 			     length / 2) != 0)
185 			return;
186 	} else {
187 		tst_resm(TINFO, "check /proc/meminfo before allocation.");
188 		fp = fopen(PATH_MEMINFO, "r");
189 		if (fp == NULL)
190 			tst_brkm(TBROK | TERRNO, cleanup, "fopen");
191 		if (checkproc(fp, "HugePages_Total", length / 2) != 0)
192 			return;
193 		if (checkproc(fp, "HugePages_Free", length / 2) != 0)
194 			return;
195 		if (checkproc(fp, "HugePages_Surp", length / 2 - size) != 0)
196 			return;
197 		if (checkproc(fp, "HugePages_Rsvd", length / 2) != 0)
198 			return;
199 		fclose(fp);
200 	}
201 	if (shmid != -1) {
202 		tst_resm(TINFO, "shmid: 0x%x", shmid);
203 		shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS);
204 		if (shmaddr == (void *)-1)
205 			tst_brkm(TBROK | TERRNO, cleanup, "shmat");
206 		write_bytes(shmaddr);
207 		read_bytes(shmaddr);
208 	} else {
209 		write_bytes(addr);
210 		read_bytes(addr);
211 	}
212 	if (opt_sysfs) {
213 		tst_resm(TINFO, "check sysfs.");
214 		if (checksys(path_sys_sz_huge, "HugePages_Total",
215 			     length / 2) != 0)
216 			return;
217 		if (checksys(path_sys_sz_free, "HugePages_Free", 0)
218 		    != 0)
219 			return;
220 		if (checksys(path_sys_sz_surp, "HugePages_Surp",
221 			     length / 2 - size) != 0)
222 			return;
223 		if (checksys(path_sys_sz_resv, "HugePages_Rsvd", 0)
224 		    != 0)
225 			return;
226 	} else {
227 		tst_resm(TINFO, "check /proc/meminfo.");
228 		fp = fopen(PATH_MEMINFO, "r");
229 		if (fp == NULL)
230 			tst_brkm(TBROK | TERRNO, cleanup, "fopen");
231 		if (checkproc(fp, "HugePages_Total", length / 2) != 0)
232 			return;
233 		if (checkproc(fp, "HugePages_Free", 0) != 0)
234 			return;
235 		if (checkproc(fp, "HugePages_Surp", length / 2 - size) != 0)
236 			return;
237 		if (checkproc(fp, "HugePages_Rsvd", 0) != 0)
238 			return;
239 		fclose(fp);
240 	}
241 	if (shmid != -1) {
242 		if (shmdt(shmaddr) != 0)
243 			tst_brkm(TBROK | TERRNO, cleanup, "shmdt");
244 	} else {
245 		munmap(addr, (long)(length / 2 * hugepagesize));
246 		close(fd);
247 		unlink(s);
248 	}
249 }
250 
cleanup(void)251 static void cleanup(void)
252 {
253 	int fd;
254 
255 	if (restore_shmmax) {
256 		fd = open(PATH_SHMMAX, O_WRONLY);
257 		if (fd == -1)
258 			tst_resm(TWARN | TERRNO, "open");
259 		if (write(fd, shmmax, strlen(shmmax)) != (ssize_t)strlen(shmmax))
260 			tst_resm(TWARN | TERRNO, "write");
261 		close(fd);
262 	}
263 	fd = open(path, O_WRONLY);
264 	if (fd == -1)
265 		tst_resm(TWARN | TERRNO, "open");
266 	tst_resm(TINFO, "restore nr_hugepages to %s.", nr_hugepages);
267 	if (write(fd, nr_hugepages,
268 		  strlen(nr_hugepages)) != (ssize_t)strlen(nr_hugepages))
269 		tst_resm(TWARN | TERRNO, "write");
270 	close(fd);
271 
272 	fd = open(pathover, O_WRONLY);
273 	if (fd == -1)
274 		tst_resm(TWARN | TERRNO, "open");
275 	tst_resm(TINFO, "restore nr_overcommit_hugepages to %s.",
276 		 nr_overcommit_hugepages);
277 	if (write(fd, nr_overcommit_hugepages, strlen(nr_overcommit_hugepages))
278 	    != (ssize_t)strlen(nr_overcommit_hugepages))
279 		tst_resm(TWARN | TERRNO, "write");
280 	close(fd);
281 
282 	/* XXX (garrcoop): memory leak. */
283 	snprintf(buf, BUFSIZ, "%s/hugemmap05", tst_get_tmpdir());
284 	if (umount(buf) == -1)
285 		tst_resm(TWARN | TERRNO, "umount");
286 	if (shmid != -1) {
287 		tst_resm(TINFO, "shmdt cleaning");
288 		shmctl(shmid, IPC_RMID, NULL);
289 	}
290 	tst_rmdir();
291 }
292 
setup(void)293 static void setup(void)
294 {
295 	FILE *fp;
296 	int fd;
297 	struct stat stat_buf;
298 
299 	tst_require_root();
300 
301 	if (stat(pathover, &stat_buf) == -1) {
302 		if (errno == ENOENT || errno == ENOTDIR)
303 			tst_brkm(TCONF, NULL,
304 				 "file %s does not exist in the system",
305 				 pathover);
306 	}
307 
308 	tst_sig(FORK, DEF_HANDLER, cleanup);
309 	TEST_PAUSE;
310 	tst_tmpdir();
311 
312 	if (shmid != -1) {
313 		fp = fopen(PATH_SHMMAX, "r");
314 		if (fp == NULL)
315 			tst_brkm(TBROK | TERRNO, cleanup, "fopen");
316 		if (fgets(shmmax, BUFSIZ, fp) == NULL)
317 			tst_brkm(TBROK | TERRNO, cleanup, "fgets");
318 		fclose(fp);
319 
320 		if (atol(shmmax) < (long)(length / 2 * hugepagesize)) {
321 			restore_shmmax = 1;
322 			fd = open(PATH_SHMMAX, O_RDWR);
323 			if (fd == -1)
324 				tst_brkm(TBROK | TERRNO, cleanup, "open");
325 			snprintf(buf, BUFSIZ, "%ld",
326 				 (long)(length / 2 * hugepagesize));
327 			if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
328 				tst_brkm(TBROK | TERRNO, cleanup,
329 					 "failed to change shmmax.");
330 		}
331 	}
332 	fp = fopen(path, "r+");
333 	if (fp == NULL)
334 		tst_brkm(TBROK | TERRNO, cleanup, "fopen");
335 	if (fgets(nr_hugepages, BUFSIZ, fp) == NULL)
336 		tst_brkm(TBROK | TERRNO, cleanup, "fgets");
337 	fclose(fp);
338 	/* Remove trailing newline. */
339 	nr_hugepages[strlen(nr_hugepages) - 1] = '\0';
340 	tst_resm(TINFO, "original nr_hugepages is %s", nr_hugepages);
341 
342 	fd = open(path, O_RDWR);
343 	if (fd == -1)
344 		tst_brkm(TBROK | TERRNO, cleanup, "open");
345 	/* Reset. */
346 	if (write(fd, "0", 1) != 1)
347 		tst_brkm(TBROK | TERRNO, cleanup, "write");
348 	if (lseek(fd, 0, SEEK_SET) == -1)
349 		tst_brkm(TBROK | TERRNO, cleanup, "lseek");
350 	snprintf(buf, BUFSIZ, "%zd", size);
351 	if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
352 		tst_brkm(TBROK | TERRNO, cleanup,
353 			 "failed to change nr_hugepages.");
354 	close(fd);
355 
356 	fp = fopen(pathover, "r+");
357 	if (fp == NULL)
358 		tst_brkm(TBROK | TERRNO, cleanup, "fopen");
359 	if (fgets(nr_overcommit_hugepages, BUFSIZ, fp) == NULL)
360 		tst_brkm(TBROK | TERRNO, cleanup, "fgets");
361 	fclose(fp);
362 	nr_overcommit_hugepages[strlen(nr_overcommit_hugepages) - 1] = '\0';
363 	tst_resm(TINFO, "original nr_overcommit_hugepages is %s",
364 		 nr_overcommit_hugepages);
365 
366 	fd = open(pathover, O_RDWR);
367 	if (fd == -1)
368 		tst_brkm(TBROK | TERRNO, cleanup, "open");
369 	/* Reset. */
370 	if (write(fd, "0", 1) != 1)
371 		tst_brkm(TBROK | TERRNO, cleanup, "write");
372 	if (lseek(fd, 0, SEEK_SET) == -1)
373 		tst_brkm(TBROK | TERRNO, cleanup, "lseek");
374 	snprintf(buf, BUFSIZ, "%zd", size);
375 	if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
376 		tst_brkm(TBROK | TERRNO, cleanup,
377 			 "failed to change nr_hugepages.");
378 	close(fd);
379 
380 	/* XXX (garrcoop): memory leak. */
381 	snprintf(buf, BUFSIZ, "%s/hugemmap05", tst_get_tmpdir());
382 	if (mkdir(buf, 0700) == -1)
383 		tst_brkm(TBROK | TERRNO, cleanup, "mkdir");
384 	if (mount(NULL, buf, "hugetlbfs", 0, NULL) == -1)
385 		tst_brkm(TBROK | TERRNO, cleanup, "mount");
386 }
387 
write_bytes(void * addr)388 static void write_bytes(void *addr)
389 {
390 	long i;
391 
392 	for (i = 0; i < (long)(length / 2 * hugepagesize); i++)
393 		((char *)addr)[i] = '\a';
394 }
395 
read_bytes(void * addr)396 static void read_bytes(void *addr)
397 {
398 	long i;
399 
400 	tst_resm(TINFO, "First hex is %x", *((unsigned int *)addr));
401 	for (i = 0; i < (long)(length / 2 * hugepagesize); i++) {
402 		if (((char *)addr)[i] != '\a') {
403 			tst_resm(TFAIL, "mismatch at %ld", i);
404 			break;
405 		}
406 	}
407 }
408 
409 /* Lookup a pattern and get the value from file */
lookup(char * line,char * pattern)410 static int lookup(char *line, char *pattern)
411 {
412 	char buf2[BUFSIZ];
413 
414 	/* empty line */
415 	if (line[0] == '\0')
416 		return 0;
417 
418 	snprintf(buf2, BUFSIZ, "%s: %%s", pattern);
419 	if (sscanf(line, buf2, buf) != 1)
420 		return 0;
421 
422 	return 1;
423 }
424 
usage(void)425 static void usage(void)
426 {
427 	printf("  -s      Setup hugepages from sysfs\n");
428 	printf("  -m      Reserve hugepages by shmget\n");
429 	printf("  -a      Number of overcommint hugepages\n");
430 }
431 
checksys(char * path,char * string,int value)432 static int checksys(char *path, char *string, int value)
433 {
434 	FILE *fp;
435 
436 	fp = fopen(path, "r");
437 	if (fp == NULL)
438 		tst_brkm(TBROK | TERRNO, cleanup, "fopen");
439 	if (fgets(buf, BUFSIZ, fp) == NULL)
440 		tst_brkm(TBROK | TERRNO, cleanup, "fgets");
441 	tst_resm(TINFO, "%s is %d.", string, atoi(buf));
442 	if (atoi(buf) != value) {
443 		tst_resm(TFAIL, "%s is not %d but %d.", string, value,
444 			 atoi(buf));
445 		fclose(fp);
446 		return 1;
447 	}
448 	fclose(fp);
449 	return 0;
450 }
451 
checkproc(FILE * fp,char * pattern,int value)452 static int checkproc(FILE * fp, char *pattern, int value)
453 {
454 	memset(buf, -1, BUFSIZ);
455 	rewind(fp);
456 	while (fgets(line, BUFSIZ, fp) != NULL)
457 		if (lookup(line, pattern))
458 			break;
459 
460 	tst_resm(TINFO, "%s is %d.", pattern, atoi(buf));
461 	if (atoi(buf) != value) {
462 		tst_resm(TFAIL, "%s is not %d but %d.", pattern, value,
463 			 atoi(buf));
464 		return 1;
465 	}
466 	return 0;
467 }
468 
init_hugepagesize(void)469 static void init_hugepagesize(void)
470 {
471 	FILE *fp;
472 
473 	check_hugepage();
474 	memset(buf, -1, BUFSIZ);
475 	fp = fopen(PATH_MEMINFO, "r");
476 	if (fp == NULL)
477 		tst_brkm(TBROK, NULL, "can't open %s", PATH_MEMINFO);
478 	while (fgets(line, BUFSIZ, fp) != NULL) {
479 		if (lookup(line, "Hugepagesize")) {
480 			tst_resm(TINFO, "Hugepagesize is %s kB", buf);
481 			hugepagesize = atoi(buf) * 1024;
482 			fclose(fp);
483 			return;
484 		}
485 	}
486 	tst_brkm(TBROK, NULL, "get Hugepagesize failed.");
487 }
488 
489 /*
490  * It's not easy to #define tunable file paths via sysfs,
491  * use function init_hugepagesize and global variable instead.
492  */
init_sys_sz_paths(void)493 static void init_sys_sz_paths(void)
494 {
495 	sprintf(path_sys_sz, "/sys/kernel/mm/hugepages/hugepages-%dkB",
496 		hugepagesize / 1024);
497 	sprintf(path_sys_sz_over, "%s/nr_overcommit_hugepages", path_sys_sz);
498 	sprintf(path_sys_sz_free, "%s/free_hugepages", path_sys_sz);
499 	sprintf(path_sys_sz_resv, "%s/resv_hugepages", path_sys_sz);
500 	sprintf(path_sys_sz_surp, "%s/surplus_hugepages", path_sys_sz);
501 	sprintf(path_sys_sz_huge, "%s/nr_hugepages", path_sys_sz);
502 }
503