1 /*
2  * 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 /*
25  * 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.
54  */
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"
65 #include "config.h"
66 
67 #define SIZE (5*1024*1024)
68 #define PATH_KSM "/sys/kernel/mm/ksm/"
69 
70 char *TCID = "mmap10";
71 int TST_TOTAL = 1;
72 
73 static int fd, opt_anon, opt_ksm;
74 static long ps;
75 static char *x;
76 
77 void setup(void);
78 void cleanup(void);
79 void mmapzero(void);
80 void help(void);
81 
82 static option_t options[] = {
83 	{"a", &opt_anon, NULL},
84 	{"s", &opt_ksm, NULL},
85 	{NULL, NULL, NULL}
86 };
87 
main(int argc,char * argv[])88 int main(int argc, char *argv[])
89 {
90 	int lc;
91 
92 	tst_parse_opts(argc, argv, options, help);
93 
94 	if (opt_ksm) {
95 		if (access(PATH_KSM, F_OK) == -1)
96 			tst_brkm(TCONF, NULL,
97 				 "KSM configuration is not enabled");
98 #ifdef HAVE_MADV_MERGEABLE
99 		tst_resm(TINFO, "add to KSM regions.");
100 #else
101 		tst_brkm(TCONF, NULL, "MADV_MERGEABLE missing in sys/mman.h");
102 #endif
103 	}
104 	if (opt_anon)
105 		tst_resm(TINFO, "use anonymous pages.");
106 	else
107 		tst_resm(TINFO, "use /dev/zero.");
108 
109 	setup();
110 
111 	tst_resm(TINFO, "start tests.");
112 	for (lc = 0; TEST_LOOPING(lc); lc++) {
113 		tst_count = 0;
114 		mmapzero();
115 	}
116 
117 	cleanup();
118 	tst_exit();
119 }
120 
mmapzero(void)121 void mmapzero(void)
122 {
123 	int n;
124 
125 	if (opt_anon) {
126 		x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
127 			 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
128 	} else {
129 		if ((fd = open("/dev/zero", O_RDWR, 0666)) < 0)
130 			tst_brkm(TBROK | TERRNO, cleanup, "open");
131 		x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
132 			 MAP_PRIVATE, fd, 0);
133 	}
134 	if (x == MAP_FAILED)
135 		tst_brkm(TFAIL | TERRNO, cleanup, "mmap");
136 #ifdef HAVE_MADV_MERGEABLE
137 	if (opt_ksm) {
138 		if (madvise(x, SIZE + SIZE - ps, MADV_MERGEABLE) == -1)
139 			tst_brkm(TBROK | TERRNO, cleanup, "madvise");
140 	}
141 #endif
142 	x[SIZE] = 0;
143 
144 	switch (n = fork()) {
145 	case -1:
146 		tst_brkm(TBROK | TERRNO, cleanup, "fork");
147 	case 0:
148 		if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
149 			tst_brkm(TFAIL | TERRNO, cleanup, "munmap");
150 		exit(0);
151 	default:
152 		break;
153 	}
154 
155 	switch (n = fork()) {
156 	case -1:
157 		tst_brkm(TBROK | TERRNO, cleanup, "fork");
158 	case 0:
159 		if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
160 			tst_brkm(TFAIL | TERRNO, cleanup,
161 				 "subsequent munmap #1");
162 		exit(0);
163 	default:
164 		switch (n = fork()) {
165 		case -1:
166 			tst_brkm(TBROK | TERRNO, cleanup, "fork");
167 		case 0:
168 			if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
169 				tst_brkm(TFAIL | TERRNO, cleanup,
170 					 "subsequent munmap #2");
171 			exit(0);
172 		default:
173 			break;
174 		}
175 		break;
176 	}
177 
178 	if (munmap(x, SIZE + SIZE - ps) == -1)
179 		tst_resm(TFAIL | TERRNO, "munmap all");
180 
181 	while (waitpid(-1, &n, WUNTRACED | WCONTINUED) > 0)
182 		if (WEXITSTATUS(n) != 0)
183 			tst_resm(TFAIL, "child exit status is %d",
184 				 WEXITSTATUS(n));
185 }
186 
cleanup(void)187 void cleanup(void)
188 {
189 }
190 
setup(void)191 void setup(void)
192 {
193 	tst_require_root();
194 
195 	tst_sig(FORK, DEF_HANDLER, cleanup);
196 	TEST_PAUSE;
197 
198 	if ((ps = sysconf(_SC_PAGESIZE)) == -1)
199 		tst_brkm(TBROK | TERRNO, cleanup, "sysconf(_SC_PAGESIZE)");
200 }
201 
help(void)202 void help(void)
203 {
204 	printf("  -a      Test anonymous pages\n");
205 	printf("  -s      Add to KSM regions\n");
206 }
207