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