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: hugemmap04
7 *
8 * Test Description:
9 * Verify that, a hugetlb mmap() succeeds when used to map the largest
10 * size possible.
11 *
12 * Expected Result:
13 * mmap() should succeed returning the address of the hugetlb mapped region.
14 * The number of free huge pages should decrease.
15 *
16 * Test:
17 * Loop if the proper options are given.
18 * Execute system call
19 * Check return code, if system call failed (return=-1)
20 * Log the errno and Issue a FAIL message.
21 *
22 * HISTORY
23 * 04/2004 Written by Robbie Williamson
24 */
25
26 #include <sys/mount.h>
27 #include <stdio.h>
28 #include <limits.h>
29 #include <sys/param.h>
30 #include "lapi/abisize.h"
31 #include "hugetlb.h"
32
33 static struct tst_option options[] = {
34 {"H:", &Hopt, "-H /.. Location of hugetlbfs, i.e. -H /var/hugetlbfs"},
35 {"s:", &nr_opt, "-s num Set the number of the been allocated hugepages"},
36 {NULL, NULL, NULL}
37 };
38
39 static char TEMPFILE[MAXPATHLEN];
40
41 static long *addr;
42 static long long mapsize;
43 static int fildes;
44 static long freepages;
45 static long beforetest;
46 static long aftertest;
47 static long hugepagesmapped;
48
test_hugemmap(void)49 static void test_hugemmap(void)
50 {
51 int huge_pagesize = 0;
52
53 /* Creat a temporary file used for huge mapping */
54 fildes = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
55
56 freepages = SAFE_READ_MEMINFO("HugePages_Free:");
57 beforetest = freepages;
58
59 huge_pagesize = SAFE_READ_MEMINFO("Hugepagesize:");
60 tst_res(TINFO, "Size of huge pages is %d KB", huge_pagesize);
61
62 #ifdef TST_ABI32
63 tst_res(TINFO, "Total amount of free huge pages is %ld",
64 freepages);
65 tst_res(TINFO, "Max number allowed for 1 mmap file in"
66 " 32-bits is 128");
67 if (freepages > 128)
68 freepages = 128;
69 #endif
70 mapsize = (long long)freepages * huge_pagesize * 1024;
71 addr = mmap(NULL, mapsize, PROT_READ | PROT_WRITE,
72 MAP_SHARED, fildes, 0);
73 if (addr == MAP_FAILED) {
74 tst_res(TFAIL | TERRNO, "mmap() Failed on %s",
75 TEMPFILE);
76 } else {
77 tst_res(TPASS,
78 "Succeeded mapping file using %ld pages",
79 freepages);
80
81 /* force to allocate page and change HugePages_Free */
82 *(int *)addr = 0;
83 /* Make sure the number of free huge pages AFTER testing decreased */
84 aftertest = SAFE_READ_MEMINFO("HugePages_Free:");
85 hugepagesmapped = beforetest - aftertest;
86 if (hugepagesmapped < 1)
87 tst_res(TWARN, "Number of HUGEPAGES_FREE stayed the"
88 " same. Okay if multiple copies running due"
89 " to test collision.");
90 }
91
92 munmap(addr, mapsize);
93 close(fildes);
94 }
95
setup(void)96 void setup(void)
97 {
98 if (tst_hugepages == 0)
99 tst_brk(TCONF, "Not enough hugepages for testing!");
100
101 if (!Hopt)
102 Hopt = tst_get_tmpdir();
103 SAFE_MOUNT("none", Hopt, "hugetlbfs", 0, NULL);
104
105 snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt, getpid());
106 }
107
cleanup(void)108 void cleanup(void)
109 {
110 unlink(TEMPFILE);
111 umount(Hopt);
112 }
113
114 static struct tst_test test = {
115 .needs_root = 1,
116 .needs_tmpdir = 1,
117 .options = options,
118 .setup = setup,
119 .cleanup = cleanup,
120 .test_all = test_hugemmap,
121 .request_hugepages = 128,
122 };
123