blob: 144e53fa2d2794bc7ff74f8ceb5d70c2b845fce8 [file] [log] [blame]
Garrett Coopere5d98402010-12-08 00:36:58 -08001/*
Cyril Hrubis5efee332013-06-04 20:14:58 +02002 * Copyright (C) 2010 Red Hat, Inc.
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it would be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 *
11 * Further, this software is distributed without any warranty that it
12 * is free of the rightful claim of any third person regarding
13 * infringement or the like. Any license provided herein, whether
14 * implied or otherwise, applies only to this software file. Patent
15 * licenses, if any, provided herein do not apply to combinations of
16 * this program with other software, or any other product whatsoever.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24/*
Garrett Coopere5d98402010-12-08 00:36:58 -080025 * mmap/munmap /dev/zero: a common way of malloc()/free() anonymous
26 * memory on Solaris.
27 *
28 * The basic purpose of this is a to test if it is possible to map and
29 * unmap /dev/zero, and to read and write the mapping. Being inspired
30 * by two bugs in the past, the design of the test was added some
31 * variations based on the reproducers for them. It also accept an
32 * option to mmap/munmap anonymous pages.
33 *
34 * One is to trigger panic with transparent hugepage feature that
35 * split_huge_page is very strict in checking the rmap walk was
36 * perfect. Keep it strict because if page_mapcount isn't stable and
37 * just right, the __split_huge_page_refcount that follows the rmap
38 * walk could lead to erratic page_count()s for the subpages. The bug
39 * in fork lead to the rmap walk finding the parent huge-pmd twice
40 * instead of just one, because the anon_vma_chain objects of the
41 * child vma still point to the vma->vm_mm of the parent. That trips
42 * on the split_huge_page mapcount vs page_mapcount check leading to a
43 * BUG_ON.
44 *
45 * The other bug is mmap() of /dev/zero results in calling map_zero()
46 * which on RHEL5 maps the ZERO_PAGE in every PTE within that virtual
47 * address range. Since the application which maps a region from 5M to
48 * 16M in size is also multi-threaded the subsequent munmap() of
49 * /dev/zero results is TLB shootdowns to all other CPUs. When this
50 * happens thousands or millions of times the application performance
51 * is terrible. The mapping ZERO_PAGE in every pte within that virtual
52 * address range was an optimization to make the subsequent pagefault
53 * times faster on RHEL5 that has been removed/changed upstream.
Garrett Coopere5d98402010-12-08 00:36:58 -080054 */
55#include <sys/types.h>
56#include <sys/stat.h>
57#include <sys/wait.h>
58#include <sys/mman.h>
59#include <errno.h>
60#include <unistd.h>
61#include <stdlib.h>
62#include <stdio.h>
63#include <fcntl.h>
64#include "test.h"
Caspar Zhang74c3dd92011-03-02 18:06:10 +080065#include "config.h"
Garrett Coopere5d98402010-12-08 00:36:58 -080066
67#define SIZE (5*1024*1024)
Nageswara R Sastry2fbbc932011-09-12 17:25:19 +053068#define PATH_KSM "/sys/kernel/mm/ksm/"
Garrett Coopere5d98402010-12-08 00:36:58 -080069
70char *TCID = "mmap10";
71int TST_TOTAL = 1;
Cyril Hrubis877cbae2011-02-07 20:00:31 +010072
Wanlong Gao354ebb42012-12-07 10:10:04 +080073static int fd, opt_anon, opt_ksm;
Garrett Cooperd4f51f12011-03-15 05:00:11 -070074static long ps;
75static char *x;
Garrett Coopere5d98402010-12-08 00:36:58 -080076
77void setup(void);
Garrett Coopereb16c912010-12-19 10:04:37 -080078void cleanup(void);
Garrett Coopere5d98402010-12-08 00:36:58 -080079void mmapzero(void);
80void help(void);
81
82static option_t options[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 {"a", &opt_anon, NULL},
84 {"s", &opt_ksm, NULL},
85 {NULL, NULL, NULL}
Garrett Coopere5d98402010-12-08 00:36:58 -080086};
87
88int main(int argc, char *argv[])
89{
90 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020091 const char *msg;
Garrett Coopere5d98402010-12-08 00:36:58 -080092
93 msg = parse_opts(argc, argv, options, help);
94 if (msg != NULL)
Garrett Cooper53740502010-12-16 00:04:01 -080095 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooperd4f51f12011-03-15 05:00:11 -070096
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 if (opt_ksm) {
Nageswara R Sastry2fbbc932011-09-12 17:25:19 +053098 if (access(PATH_KSM, F_OK) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 tst_brkm(TCONF, NULL,
100 "KSM configuration is not enabled");
Garrett Cooperd4f51f12011-03-15 05:00:11 -0700101#ifdef HAVE_MADV_MERGEABLE
102 tst_resm(TINFO, "add to KSM regions.");
103#else
104 tst_brkm(TCONF, NULL, "MADV_MERGEABLE missing in sys/mman.h");
105#endif
106 }
107 if (opt_anon)
108 tst_resm(TINFO, "use anonymous pages.");
109 else
110 tst_resm(TINFO, "use /dev/zero.");
111
Garrett Coopere5d98402010-12-08 00:36:58 -0800112 setup();
Garrett Cooperd4f51f12011-03-15 05:00:11 -0700113
114 tst_resm(TINFO, "start tests.");
Garrett Coopere5d98402010-12-08 00:36:58 -0800115 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800116 tst_count = 0;
Garrett Coopere5d98402010-12-08 00:36:58 -0800117 mmapzero();
118 }
Garrett Cooperd4f51f12011-03-15 05:00:11 -0700119
Garrett Coopere5d98402010-12-08 00:36:58 -0800120 cleanup();
Garrett Coopereb16c912010-12-19 10:04:37 -0800121 tst_exit();
Garrett Coopere5d98402010-12-08 00:36:58 -0800122}
123
124void mmapzero(void)
125{
Garrett Coopere5d98402010-12-08 00:36:58 -0800126 int n;
127
128 if (opt_anon) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
130 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Garrett Coopere5d98402010-12-08 00:36:58 -0800131 } else {
Garrett Coopere5d98402010-12-08 00:36:58 -0800132 if ((fd = open("/dev/zero", O_RDWR, 0666)) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 tst_brkm(TBROK | TERRNO, cleanup, "open");
134 x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
135 MAP_PRIVATE, fd, 0);
Garrett Coopere5d98402010-12-08 00:36:58 -0800136 }
137 if (x == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800138 tst_brkm(TFAIL | TERRNO, cleanup, "mmap");
Garrett Cooperd4f51f12011-03-15 05:00:11 -0700139#ifdef HAVE_MADV_MERGEABLE
Garrett Coopere5d98402010-12-08 00:36:58 -0800140 if (opt_ksm) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 if (madvise(x, SIZE + SIZE - ps, MADV_MERGEABLE) == -1)
142 tst_brkm(TBROK | TERRNO, cleanup, "madvise");
Garrett Coopere5d98402010-12-08 00:36:58 -0800143 }
Garrett Cooperd4f51f12011-03-15 05:00:11 -0700144#endif
Garrett Coopere5d98402010-12-08 00:36:58 -0800145 x[SIZE] = 0;
146
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147 switch (n = fork()) {
Garrett Coopere5d98402010-12-08 00:36:58 -0800148 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Garrett Coopere5d98402010-12-08 00:36:58 -0800150 case 0:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
152 tst_brkm(TFAIL | TERRNO, cleanup, "munmap");
Garrett Coopere5d98402010-12-08 00:36:58 -0800153 exit(0);
154 default:
155 break;
156 }
Garrett Cooperd4f51f12011-03-15 05:00:11 -0700157
Wanlong Gao354ebb42012-12-07 10:10:04 +0800158 switch (n = fork()) {
Garrett Coopere5d98402010-12-08 00:36:58 -0800159 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Garrett Coopere5d98402010-12-08 00:36:58 -0800161 case 0:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800162 if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
163 tst_brkm(TFAIL | TERRNO, cleanup,
164 "subsequent munmap #1");
Garrett Coopere5d98402010-12-08 00:36:58 -0800165 exit(0);
166 default:
167 switch (n = fork()) {
168 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Garrett Coopere5d98402010-12-08 00:36:58 -0800170 case 0:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171 if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
172 tst_brkm(TFAIL | TERRNO, cleanup,
173 "subsequent munmap #2");
Garrett Coopere5d98402010-12-08 00:36:58 -0800174 exit(0);
175 default:
176 break;
177 }
178 break;
179 }
Garrett Cooperd4f51f12011-03-15 05:00:11 -0700180
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 if (munmap(x, SIZE + SIZE - ps) == -1)
182 tst_resm(TFAIL | TERRNO, "munmap all");
Garrett Cooperd4f51f12011-03-15 05:00:11 -0700183
Garrett Coopere5d98402010-12-08 00:36:58 -0800184 while (waitpid(-1, &n, WUNTRACED | WCONTINUED) > 0)
185 if (WEXITSTATUS(n) != 0)
186 tst_resm(TFAIL, "child exit status is %d",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 WEXITSTATUS(n));
Garrett Coopere5d98402010-12-08 00:36:58 -0800188}
189
190void cleanup(void)
191{
Garrett Coopere5d98402010-12-08 00:36:58 -0800192}
193
194void setup(void)
195{
Garrett Cooper40495b82011-01-19 00:48:39 -0800196 tst_require_root(NULL);
197
Garrett Coopere5d98402010-12-08 00:36:58 -0800198 tst_sig(FORK, DEF_HANDLER, cleanup);
199 TEST_PAUSE;
Garrett Cooperd4f51f12011-03-15 05:00:11 -0700200
201 if ((ps = sysconf(_SC_PAGESIZE)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800202 tst_brkm(TBROK | TERRNO, cleanup, "sysconf(_SC_PAGESIZE)");
Garrett Coopere5d98402010-12-08 00:36:58 -0800203}
204
205void help(void)
206{
207 printf(" -a Test anonymous pages\n");
208 printf(" -s Add to KSM regions\n");
Garrett Coopereb16c912010-12-19 10:04:37 -0800209}