1 /*
2 * Copyright (c) International Business Machines Corp., 2004
3 * Copyright (c) Linux Test Project, 2004-2017
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 */
15
16 /*
17 * Test Name: hugemmap01
18 *
19 * Test Description:
20 * Verify that, mmap() succeeds when used to map a file in a hugetlbfs.
21 *
22 * Expected Result:
23 * mmap() should succeed returning the address of the hugetlb mapped region.
24 * The number of free huge pages should decrease.
25 *
26 * Test:
27 * Loop if the proper options are given.
28 * Execute system call
29 * Check return code, if system call failed (return=-1)
30 * Log the errno and Issue a FAIL message.
31 *
32 * HISTORY
33 * 04/2004 Written by Robbie Williamson
34 */
35
36 #include <stdio.h>
37 #include <sys/mount.h>
38 #include <limits.h>
39 #include <sys/param.h>
40 #include "hugetlb.h"
41
42 static struct tst_option options[] = {
43 {"H:", &Hopt, "-H /.. Location of hugetlbfs, i.e. -H /var/hugetlbfs"},
44 {"s:", &nr_opt, "-s num Set the number of the been allocated hugepages"},
45 {NULL, NULL, NULL}
46 };
47
48 static long *addr;
49 static int fildes;
50 static long beforetest;
51 static long aftertest;
52 static long hugepagesmapped;
53 static long hugepages = 128;
54 static char TEMPFILE[MAXPATHLEN];
55
test_hugemmap(void)56 static void test_hugemmap(void)
57 {
58 long page_sz = 0;
59
60 fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
61
62 beforetest = SAFE_READ_MEMINFO("HugePages_Free:");
63
64 page_sz = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
65
66 addr = mmap(NULL, page_sz, PROT_READ | PROT_WRITE,
67 MAP_SHARED, fildes, 0);
68
69 if (addr == MAP_FAILED) {
70 tst_res(TFAIL | TERRNO, "mmap() Failed on %s",
71 TEMPFILE);
72 } else {
73 tst_res(TPASS, "call succeeded");
74
75 /* force to allocate page and change HugePages_Free */
76 *(int *)addr = 0;
77 /* Make sure the number of free huge pages AFTER testing decreased */
78 aftertest = SAFE_READ_MEMINFO("HugePages_Free:");
79 hugepagesmapped = beforetest - aftertest;
80 if (hugepagesmapped < 1)
81 tst_res(TWARN, "Number of HUGEPAGES_FREE stayed the"
82 " same. Okay if multiple copies running due"
83 " to test collision.");
84 munmap(addr, page_sz);
85 }
86
87 close(fildes);
88 }
89
setup(void)90 void setup(void)
91 {
92 save_nr_hugepages();
93
94 if (!Hopt)
95 Hopt = tst_get_tmpdir();
96 SAFE_MOUNT("none", Hopt, "hugetlbfs", 0, NULL);
97
98 if (nr_opt)
99 hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
100 set_sys_tune("nr_hugepages", hugepages, 1);
101
102 snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt, getpid());
103 }
104
cleanup(void)105 void cleanup(void)
106 {
107 unlink(TEMPFILE);
108 restore_nr_hugepages();
109
110 umount(Hopt);
111 }
112
113 static struct tst_test test = {
114 .needs_root = 1,
115 .needs_tmpdir = 1,
116 .options = options,
117 .setup = setup,
118 .cleanup = cleanup,
119 .test_all = test_hugemmap,
120 };
121