blob: 58c5d7c5d28e1d6c80569423dec234e4de0799fa [file] [log] [blame]
robbiew9a1e0a32004-04-19 21:47:15 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2004
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 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew9a1e0a32004-04-19 21:47:15 +000018 */
19
20/*
21 * Test Name: hugemmap01
22 *
23 * Test Description:
24 * Verify that, mmap() succeeds when used to map a file in a hugetlbfs.
25 *
26 * Expected Result:
27 * mmap() should succeed returning the address of the hugetlb mapped region.
28 * The number of free huge pages should decrease.
29 *
30 * Algorithm:
31 * Setup:
32 * Setup signal handling.
33 * Pause for SIGUSR1 if option specified.
34 * Create temporary directory.
35 *
Wanlong Gaoa172abf2012-02-06 11:16:23 +080036 * Test:
37 * Loop if the proper options are given.
38 * Execute system call
39 * Check return code, if system call failed (return=-1)
40 * Log the errno and Issue a FAIL message.
41 * Cleanup:
42 * Print timing stats if options given
43 * Delete the temporary directory created.
robbiew9a1e0a32004-04-19 21:47:15 +000044 *
45 * HISTORY
Wanlong Gaoa172abf2012-02-06 11:16:23 +080046 * 04/2004 Written by Robbie Williamson
robbiew9a1e0a32004-04-19 21:47:15 +000047 */
48
Caspar Zhang79667fa2012-03-12 14:41:46 +080049#include <sys/types.h>
50#include <sys/mman.h>
51#include <sys/mount.h>
52#include <sys/stat.h>
robbiew9a1e0a32004-04-19 21:47:15 +000053#include <errno.h>
robbiew9a1e0a32004-04-19 21:47:15 +000054#include <fcntl.h>
robbiew9a1e0a32004-04-19 21:47:15 +000055#include <signal.h>
56#include <stdint.h>
Caspar Zhang79667fa2012-03-12 14:41:46 +080057#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
robbiew9a1e0a32004-04-19 21:47:15 +000061
62#include "test.h"
Wanlong Gao56d368c2012-02-10 11:16:22 +080063#include "safe_macros.h"
Caspar Zhang79667fa2012-03-12 14:41:46 +080064#include "mem.h"
robbiew9a1e0a32004-04-19 21:47:15 +000065
Wanlong Gaod8728dc2012-02-09 21:58:00 +080066static char TEMPFILE[MAXPATHLEN];
robbiew9a1e0a32004-04-19 21:47:15 +000067
Wanlong Gaoa172abf2012-02-06 11:16:23 +080068char *TCID = "hugemmap01";
69int TST_TOTAL = 1;
70static long *addr;
71static int fildes;
72static char *Hopt;
Wanlong Gaod8728dc2012-02-09 21:58:00 +080073static char *nr_opt;
Caspar Zhang51725fc2012-03-13 11:02:55 +080074static long beforetest;
75static long aftertest;
76static long hugepagesmapped;
Wanlong Gaod8728dc2012-02-09 21:58:00 +080077static long hugepages = 128;
78static long orig_hugepages;
robbiew9a1e0a32004-04-19 21:47:15 +000079
Wanlong Gaoa172abf2012-02-06 11:16:23 +080080static void help(void);
robbiew9a1e0a32004-04-19 21:47:15 +000081
Wanlong Gaoa172abf2012-02-06 11:16:23 +080082int main(int ac, char **av)
vapier5ccd6132006-02-11 04:53:34 +000083{
Wanlong Gaoa172abf2012-02-06 11:16:23 +080084 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020085 const char *msg;
Wanlong Gaoa172abf2012-02-06 11:16:23 +080086 int Hflag = 0;
Caspar Zhang51725fc2012-03-13 11:02:55 +080087 long page_sz = 0;
Wanlong Gaod8728dc2012-02-09 21:58:00 +080088 int sflag = 0;
vapier5ccd6132006-02-11 04:53:34 +000089
Wanlong Gaoa172abf2012-02-06 11:16:23 +080090 option_t options[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080091 {"H:", &Hflag, &Hopt},
92 {"s:", &sflag, &nr_opt},
93 {NULL, NULL, NULL}
Wanlong Gaoa172abf2012-02-06 11:16:23 +080094 };
subrata_modak1e9bad92008-08-11 11:05:28 +000095
robbiew9a1e0a32004-04-19 21:47:15 +000096 msg = parse_opts(ac, av, options, &help);
Wanlong Gaoa172abf2012-02-06 11:16:23 +080097 if (msg != NULL)
98 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s,"
99 "use -help", msg);
robbiew9a1e0a32004-04-19 21:47:15 +0000100
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800101 if (!Hflag) {
102 tst_tmpdir();
Cyril Hrubis9c31ad22014-05-14 17:15:39 +0200103 Hopt = tst_get_tmpdir();
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800104 }
Wanlong Gao56d368c2012-02-10 11:16:22 +0800105 if (sflag)
106 hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
subrata_modakbdbaec52009-02-26 12:14:51 +0000107
robbiew9a1e0a32004-04-19 21:47:15 +0000108 setup();
109
robbiew9a1e0a32004-04-19 21:47:15 +0000110 for (lc = 0; TEST_LOOPING(lc); lc++) {
Wanlong Gaoa172abf2012-02-06 11:16:23 +0800111 /* Creat a temporary file used for mapping */
112 fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666);
113 if (fildes < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 tst_brkm(TFAIL | TERRNO, cleanup, "open %s failed",
Wanlong Gaoa172abf2012-02-06 11:16:23 +0800115 TEMPFILE);
Garrett Cooper2c282152010-12-16 00:55:50 -0800116
Caspar Zhangd59a6592013-03-07 14:59:12 +0800117 tst_count = 0;
robbiew9a1e0a32004-04-19 21:47:15 +0000118
119 /* Note the number of free huge pages BEFORE testing */
Caspar Zhang51725fc2012-03-13 11:02:55 +0800120 beforetest = read_meminfo("HugePages_Free:");
robbiew9a1e0a32004-04-19 21:47:15 +0000121
subrata_modak1e9bad92008-08-11 11:05:28 +0000122 /* Note the size of huge page size BEFORE testing */
Caspar Zhang51725fc2012-03-13 11:02:55 +0800123 page_sz = read_meminfo("Hugepagesize:") * 1024;
subrata_modak1e9bad92008-08-11 11:05:28 +0000124
Caspar Zhang51725fc2012-03-13 11:02:55 +0800125 addr = mmap(NULL, page_sz, PROT_READ | PROT_WRITE,
robbiew9a1e0a32004-04-19 21:47:15 +0000126 MAP_SHARED, fildes, 0);
robbiew9a1e0a32004-04-19 21:47:15 +0000127 if (addr == MAP_FAILED) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800128 tst_resm(TFAIL | TERRNO, "mmap() Failed on %s",
129 TEMPFILE);
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800130 close(fildes);
robbiew9a1e0a32004-04-19 21:47:15 +0000131 continue;
132 } else {
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800133 close(fildes);
robbiew9a1e0a32004-04-19 21:47:15 +0000134 tst_resm(TPASS, "call succeeded");
mreed108c6e72c2006-09-26 15:33:51 +0000135 /* force to allocate page and change HugePages_Free */
Wanlong Gaoa172abf2012-02-06 11:16:23 +0800136 *(int *)addr = 0;
robbiew9a1e0a32004-04-19 21:47:15 +0000137 }
138
Wanlong Gaoa172abf2012-02-06 11:16:23 +0800139 /*
140 * Make sure the number of free huge pages
141 * AFTER testing decreased
142 */
Caspar Zhang51725fc2012-03-13 11:02:55 +0800143 aftertest = read_meminfo("HugePages_Free:");
robbiew9a1e0a32004-04-19 21:47:15 +0000144 hugepagesmapped = beforetest - aftertest;
Wanlong Gaoa172abf2012-02-06 11:16:23 +0800145 if (hugepagesmapped < 1)
146 tst_resm(TWARN, "Number of HUGEPAGES_FREE stayed the"
147 " same. Okay if multiple copies running due"
148 " to test collision.");
149
robbiew9a1e0a32004-04-19 21:47:15 +0000150 /* Clean up things in case we are looping */
151 /* Unmap the mapped memory */
Caspar Zhang51725fc2012-03-13 11:02:55 +0800152 if (munmap(addr, page_sz) != 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
Wanlong Gaoa172abf2012-02-06 11:16:23 +0800154
robbiew9a1e0a32004-04-19 21:47:15 +0000155 close(fildes);
Garrett Cooper2c282152010-12-16 00:55:50 -0800156 }
robbiew9a1e0a32004-04-19 21:47:15 +0000157
robbiew9a1e0a32004-04-19 21:47:15 +0000158 cleanup();
Caspar Zhang80dd7422011-04-12 15:06:26 +0800159 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800160}
robbiew9a1e0a32004-04-19 21:47:15 +0000161
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800162void setup(void)
robbiew9a1e0a32004-04-19 21:47:15 +0000163{
robbiew9a1e0a32004-04-19 21:47:15 +0000164 TEST_PAUSE;
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800165 tst_require_root(NULL);
166 if (mount("none", Hopt, "hugetlbfs", 0, NULL) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 tst_brkm(TBROK | TERRNO, NULL, "mount failed on %s", Hopt);
robbiew9a1e0a32004-04-19 21:47:15 +0000168
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800169 orig_hugepages = get_sys_tune("nr_hugepages");
170 set_sys_tune("nr_hugepages", hugepages, 1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171 snprintf(TEMPFILE, sizeof(TEMPFILE), "%s/mmapfile%d", Hopt, getpid());
robbiew9a1e0a32004-04-19 21:47:15 +0000172}
173
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800174void cleanup(void)
robbiew9a1e0a32004-04-19 21:47:15 +0000175{
robbiew9a1e0a32004-04-19 21:47:15 +0000176 unlink(TEMPFILE);
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800177 set_sys_tune("nr_hugepages", orig_hugepages, 0);
robbiew9a1e0a32004-04-19 21:47:15 +0000178
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800179 umount(Hopt);
180 tst_rmdir();
Caspar Zhang80dd7422011-04-12 15:06:26 +0800181}
Wanlong Gaoa172abf2012-02-06 11:16:23 +0800182
183static void help(void)
184{
185 printf(" -H /.. Location of hugetlbfs, i.e. -H /var/hugetlbfs\n");
Wanlong Gaod8728dc2012-02-09 21:58:00 +0800186 printf(" -s num Set the number of the been allocated hugepages\n");
Wanlong Gaoa172abf2012-02-06 11:16:23 +0800187}