xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mmap/mmap10.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2010  Red Hat, Inc.
3*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
4*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of version 2 of the GNU General Public
5*49cdfc7eSAndroid Build Coastguard Worker  * License as published by the Free Software Foundation.
6*49cdfc7eSAndroid Build Coastguard Worker  *
7*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful,
8*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Further, this software is distributed without any warranty that it
12*49cdfc7eSAndroid Build Coastguard Worker  * is free of the rightful claim of any third person regarding
13*49cdfc7eSAndroid Build Coastguard Worker  * infringement or the like.  Any license provided herein, whether
14*49cdfc7eSAndroid Build Coastguard Worker  * implied or otherwise, applies only to this software file.  Patent
15*49cdfc7eSAndroid Build Coastguard Worker  * licenses, if any, provided herein do not apply to combinations of
16*49cdfc7eSAndroid Build Coastguard Worker  * this program with other software, or any other product whatsoever.
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
19*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write the Free Software
20*49cdfc7eSAndroid Build Coastguard Worker  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21*49cdfc7eSAndroid Build Coastguard Worker  * 02110-1301, USA.
22*49cdfc7eSAndroid Build Coastguard Worker  */
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker /*
25*49cdfc7eSAndroid Build Coastguard Worker  * mmap/munmap /dev/zero: a common way of malloc()/free() anonymous
26*49cdfc7eSAndroid Build Coastguard Worker  * memory on Solaris.
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  * The basic purpose of this is a to test if it is possible to map and
29*49cdfc7eSAndroid Build Coastguard Worker  * unmap /dev/zero, and to read and write the mapping. Being inspired
30*49cdfc7eSAndroid Build Coastguard Worker  * by two bugs in the past, the design of the test was added some
31*49cdfc7eSAndroid Build Coastguard Worker  * variations based on the reproducers for them. It also accept an
32*49cdfc7eSAndroid Build Coastguard Worker  * option to mmap/munmap anonymous pages.
33*49cdfc7eSAndroid Build Coastguard Worker  *
34*49cdfc7eSAndroid Build Coastguard Worker  * One is to trigger panic with transparent hugepage feature that
35*49cdfc7eSAndroid Build Coastguard Worker  * split_huge_page is very strict in checking the rmap walk was
36*49cdfc7eSAndroid Build Coastguard Worker  * perfect. Keep it strict because if page_mapcount isn't stable and
37*49cdfc7eSAndroid Build Coastguard Worker  * just right, the __split_huge_page_refcount that follows the rmap
38*49cdfc7eSAndroid Build Coastguard Worker  * walk could lead to erratic page_count()s for the subpages. The bug
39*49cdfc7eSAndroid Build Coastguard Worker  * in fork lead to the rmap walk finding the parent huge-pmd twice
40*49cdfc7eSAndroid Build Coastguard Worker  * instead of just one, because the anon_vma_chain objects of the
41*49cdfc7eSAndroid Build Coastguard Worker  * child vma still point to the vma->vm_mm of the parent. That trips
42*49cdfc7eSAndroid Build Coastguard Worker  * on the split_huge_page mapcount vs page_mapcount check leading to a
43*49cdfc7eSAndroid Build Coastguard Worker  * BUG_ON.
44*49cdfc7eSAndroid Build Coastguard Worker  *
45*49cdfc7eSAndroid Build Coastguard Worker  * The other bug is mmap() of /dev/zero results in calling map_zero()
46*49cdfc7eSAndroid Build Coastguard Worker  * which on RHEL5 maps the ZERO_PAGE in every PTE within that virtual
47*49cdfc7eSAndroid Build Coastguard Worker  * address range. Since the application which maps a region from 5M to
48*49cdfc7eSAndroid Build Coastguard Worker  * 16M in size is also multi-threaded the subsequent munmap() of
49*49cdfc7eSAndroid Build Coastguard Worker  * /dev/zero results is TLB shootdowns to all other CPUs. When this
50*49cdfc7eSAndroid Build Coastguard Worker  * happens thousands or millions of times the application performance
51*49cdfc7eSAndroid Build Coastguard Worker  * is terrible. The mapping ZERO_PAGE in every pte within that virtual
52*49cdfc7eSAndroid Build Coastguard Worker  * address range was an optimization to make the subsequent pagefault
53*49cdfc7eSAndroid Build Coastguard Worker  * times faster on RHEL5 that has been removed/changed upstream.
54*49cdfc7eSAndroid Build Coastguard Worker  */
55*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
56*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
57*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
58*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
59*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
60*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
61*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
62*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
63*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
64*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
65*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker #define SIZE (5*1024*1024)
68*49cdfc7eSAndroid Build Coastguard Worker #define PATH_KSM "/sys/kernel/mm/ksm/"
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mmap10";
71*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker static int fd, opt_anon, opt_ksm;
74*49cdfc7eSAndroid Build Coastguard Worker static long ps;
75*49cdfc7eSAndroid Build Coastguard Worker static char *x;
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
78*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
79*49cdfc7eSAndroid Build Coastguard Worker void mmapzero(void);
80*49cdfc7eSAndroid Build Coastguard Worker void help(void);
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker static option_t options[] = {
83*49cdfc7eSAndroid Build Coastguard Worker 	{"a", &opt_anon, NULL},
84*49cdfc7eSAndroid Build Coastguard Worker 	{"s", &opt_ksm, NULL},
85*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, NULL, NULL}
86*49cdfc7eSAndroid Build Coastguard Worker };
87*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])88*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, options, help);
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	if (opt_ksm) {
95*49cdfc7eSAndroid Build Coastguard Worker 		if (access(PATH_KSM, F_OK) == -1)
96*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TCONF, NULL,
97*49cdfc7eSAndroid Build Coastguard Worker 				 "KSM configuration is not enabled");
98*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_DECL_MADV_MERGEABLE
99*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "add to KSM regions.");
100*49cdfc7eSAndroid Build Coastguard Worker #else
101*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TCONF, NULL, "MADV_MERGEABLE missing in sys/mman.h");
102*49cdfc7eSAndroid Build Coastguard Worker #endif
103*49cdfc7eSAndroid Build Coastguard Worker 	}
104*49cdfc7eSAndroid Build Coastguard Worker 	if (opt_anon)
105*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "use anonymous pages.");
106*49cdfc7eSAndroid Build Coastguard Worker 	else
107*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "use /dev/zero.");
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 	setup();
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "start tests.");
112*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
113*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
114*49cdfc7eSAndroid Build Coastguard Worker 		mmapzero();
115*49cdfc7eSAndroid Build Coastguard Worker 	}
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
118*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker 
mmapzero(void)121*49cdfc7eSAndroid Build Coastguard Worker void mmapzero(void)
122*49cdfc7eSAndroid Build Coastguard Worker {
123*49cdfc7eSAndroid Build Coastguard Worker 	int n;
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 	if (opt_anon) {
126*49cdfc7eSAndroid Build Coastguard Worker 		x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
127*49cdfc7eSAndroid Build Coastguard Worker 			 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
128*49cdfc7eSAndroid Build Coastguard Worker 	} else {
129*49cdfc7eSAndroid Build Coastguard Worker 		if ((fd = open("/dev/zero", O_RDWR, 0666)) < 0)
130*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "open");
131*49cdfc7eSAndroid Build Coastguard Worker 		x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
132*49cdfc7eSAndroid Build Coastguard Worker 			 MAP_PRIVATE, fd, 0);
133*49cdfc7eSAndroid Build Coastguard Worker 	}
134*49cdfc7eSAndroid Build Coastguard Worker 	if (x == MAP_FAILED)
135*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TERRNO, cleanup, "mmap");
136*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_DECL_MADV_MERGEABLE
137*49cdfc7eSAndroid Build Coastguard Worker 	if (opt_ksm) {
138*49cdfc7eSAndroid Build Coastguard Worker 		if (madvise(x, SIZE + SIZE - ps, MADV_MERGEABLE) == -1)
139*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "madvise");
140*49cdfc7eSAndroid Build Coastguard Worker 	}
141*49cdfc7eSAndroid Build Coastguard Worker #endif
142*49cdfc7eSAndroid Build Coastguard Worker 	x[SIZE] = 0;
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 	switch (n = fork()) {
145*49cdfc7eSAndroid Build Coastguard Worker 	case -1:
146*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "fork");
147*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
148*49cdfc7eSAndroid Build Coastguard Worker 		if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
149*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL | TERRNO, cleanup, "munmap");
150*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
151*49cdfc7eSAndroid Build Coastguard Worker 	default:
152*49cdfc7eSAndroid Build Coastguard Worker 		break;
153*49cdfc7eSAndroid Build Coastguard Worker 	}
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker 	switch (n = fork()) {
156*49cdfc7eSAndroid Build Coastguard Worker 	case -1:
157*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "fork");
158*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
159*49cdfc7eSAndroid Build Coastguard Worker 		if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
160*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL | TERRNO, cleanup,
161*49cdfc7eSAndroid Build Coastguard Worker 				 "subsequent munmap #1");
162*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
163*49cdfc7eSAndroid Build Coastguard Worker 	default:
164*49cdfc7eSAndroid Build Coastguard Worker 		switch (n = fork()) {
165*49cdfc7eSAndroid Build Coastguard Worker 		case -1:
166*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "fork");
167*49cdfc7eSAndroid Build Coastguard Worker 		case 0:
168*49cdfc7eSAndroid Build Coastguard Worker 			if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
169*49cdfc7eSAndroid Build Coastguard Worker 				tst_brkm(TFAIL | TERRNO, cleanup,
170*49cdfc7eSAndroid Build Coastguard Worker 					 "subsequent munmap #2");
171*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
172*49cdfc7eSAndroid Build Coastguard Worker 		default:
173*49cdfc7eSAndroid Build Coastguard Worker 			break;
174*49cdfc7eSAndroid Build Coastguard Worker 		}
175*49cdfc7eSAndroid Build Coastguard Worker 		break;
176*49cdfc7eSAndroid Build Coastguard Worker 	}
177*49cdfc7eSAndroid Build Coastguard Worker 
178*49cdfc7eSAndroid Build Coastguard Worker 	if (munmap(x, SIZE + SIZE - ps) == -1)
179*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL | TERRNO, "munmap all");
180*49cdfc7eSAndroid Build Coastguard Worker 
181*49cdfc7eSAndroid Build Coastguard Worker 	while (waitpid(-1, &n, WUNTRACED | WCONTINUED) > 0)
182*49cdfc7eSAndroid Build Coastguard Worker 		if (WEXITSTATUS(n) != 0)
183*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "child exit status is %d",
184*49cdfc7eSAndroid Build Coastguard Worker 				 WEXITSTATUS(n));
185*49cdfc7eSAndroid Build Coastguard Worker }
186*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)187*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
188*49cdfc7eSAndroid Build Coastguard Worker {
189*49cdfc7eSAndroid Build Coastguard Worker }
190*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)191*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
192*49cdfc7eSAndroid Build Coastguard Worker {
193*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
194*49cdfc7eSAndroid Build Coastguard Worker 
195*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
196*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
197*49cdfc7eSAndroid Build Coastguard Worker 
198*49cdfc7eSAndroid Build Coastguard Worker 	if ((ps = sysconf(_SC_PAGESIZE)) == -1)
199*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "sysconf(_SC_PAGESIZE)");
200*49cdfc7eSAndroid Build Coastguard Worker }
201*49cdfc7eSAndroid Build Coastguard Worker 
help(void)202*49cdfc7eSAndroid Build Coastguard Worker void help(void)
203*49cdfc7eSAndroid Build Coastguard Worker {
204*49cdfc7eSAndroid Build Coastguard Worker 	printf("  -a      Test anonymous pages\n");
205*49cdfc7eSAndroid Build Coastguard Worker 	printf("  -s      Add to KSM regions\n");
206*49cdfc7eSAndroid Build Coastguard Worker }
207