• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * vma01 - test not merging a VMA which cloned from parent process
3  *
4  * This program is used for testing the following upstream commit:
5  * 965f55dea0e331152fa53941a51e4e16f9f06fae
6  *
7  * The cloned VMA shares the anon_vma lock with the parent process's
8  * VMA. If we do the merge, more vmas (even the new range is only
9  * for current process) use the perent process's anon_vma lock. This
10  * introduces scalability issues. find_mergeable_anon_vma() already
11  * considers this case.
12  *
13  * This test program clones VMA and checks /proc/self/maps file, on
14  * an unpatched kernel, there is a single 6*ps VMA for the child
15  * like this:
16  *
17  * 7fee32989000-7fee3298f000 -w-p 00000000 00:00 0
18  *
19  * On a patched kernel, there are two 3*ps VMAs like this:
20  *
21  * 7f55bbd47000-7f55bbd4a000 -w-p 00000000 00:00 0
22  * 7f55bbd4a000-7f55bbd4d000 -w-p 00000000 00:00 0
23  *
24  * Copyright (C) 2011  Red Hat, Inc.
25  * This program is free software; you can redistribute it and/or
26  * modify it under the terms of version 2 of the GNU General Public
27  * License as published by the Free Software Foundation.
28  *
29  * This program is distributed in the hope that it would be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Further, this software is distributed without any warranty that it
34  * is free of the rightful claim of any third person regarding
35  * infringement or the like.  Any license provided herein, whether
36  * implied or otherwise, applies only to this software file.  Patent
37  * licenses, if any, provided herein do not apply to combinations of
38  * this program with other software, or any other product whatsoever.
39  *
40  * You should have received a copy of the GNU General Public License
41  * along with this program; if not, write the Free Software
42  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
43  * 02110-1301, USA.
44  */
45 #include <sys/types.h>
46 #include <sys/mman.h>
47 #include <sys/wait.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "test.h"
54 
55 #define MAPS_FILE "/proc/self/maps"
56 
57 char *TCID = "vma01";
58 int TST_TOTAL = 1;
59 
60 static void check_vma(void);
61 static void create_bighole(void);
62 static void *get_end_addr(void *addr_s);
63 static void check_status(int status);
64 static void setup(void);
65 static void cleanup(void);
66 
67 static unsigned long ps;
68 static void *p;
69 
main(int argc,char ** argv)70 int main(int argc, char **argv)
71 {
72 	int lc;
73 
74 	tst_parse_opts(argc, argv, NULL, NULL);
75 
76 	ps = sysconf(_SC_PAGE_SIZE);
77 	setup();
78 
79 	for (lc = 0; TEST_LOOPING(lc); lc++) {
80 		tst_count = 0;
81 
82 		check_vma();
83 	}
84 	cleanup();
85 	tst_exit();
86 }
87 
check_vma(void)88 static void check_vma(void)
89 {
90 	int status;
91 	int topdown;
92 	void *t, *u, *x;
93 
94 	create_bighole();
95 	t = mmap(p, 3 * ps, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
96 	if (t == MAP_FAILED)
97 		tst_brkm(TBROK | TERRNO, cleanup, "mmap");
98 	memset(t, 1, ps);
99 
100 	switch (fork()) {
101 	case -1:
102 		tst_brkm(TBROK | TERRNO, cleanup, "fork");
103 	case 0:
104 		memset(t, 2, ps);
105 		u = mmap(t + 3 * ps, 3 * ps, PROT_WRITE,
106 			 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
107 		if (u == MAP_FAILED) {
108 			perror("mmap");
109 			exit(255);
110 		}
111 		topdown = (u > t) ? 0 : 1;
112 		printf("parent: t = %p\n", t);
113 		printf("child : u = %p\n", u);
114 		memset(u, 2, ps);
115 
116 		if (topdown) {
117 			x = get_end_addr(u);
118 			printf("child : x = %p\n", x);
119 			if (x == t + 3 * ps)
120 				exit(1);
121 			else if (x == t && get_end_addr(x) == t + 3 * ps)
122 				exit(0);
123 		} else {
124 			x = get_end_addr(t);
125 			printf("child : x = %p\n", x);
126 			if (x == t + 6 * ps)
127 				exit(1);
128 			else if (x == u && get_end_addr(x) == t + 6 * ps)
129 				exit(0);
130 		}
131 		exit(255);
132 	default:
133 		if (waitpid(-1, &status, 0) == -1)
134 			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
135 		if (!WIFEXITED(status))
136 			tst_brkm(TBROK, cleanup, "child exited abnormally.");
137 		check_status(WEXITSTATUS(status));
138 		break;
139 	}
140 }
141 
create_bighole(void)142 static void create_bighole(void)
143 {
144 	void *t;
145 
146 	/*
147 	 * |-3*ps-|
148 	 * |------|------|------| hole
149 	 * t      p
150 	 * |======|------|        top-down alloc
151 	 *        |------|======| bottom-up alloc
152 	 */
153 	t = mmap(NULL, 9 * ps, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
154 	if (t == MAP_FAILED)
155 		tst_brkm(TBROK | TERRNO, cleanup, "mmap");
156 	memset(t, 'a', ps);
157 	p = t + 3 * ps;
158 	if (munmap(t, 9 * ps) == -1)
159 		tst_brkm(TBROK | TERRNO, cleanup, "munmap");
160 }
161 
get_end_addr(void * addr_s)162 static void *get_end_addr(void *addr_s)
163 {
164 	FILE *fp;
165 	void *s, *t;
166 	char buf[BUFSIZ];
167 
168 	fp = fopen(MAPS_FILE, "r");
169 	if (fp == NULL)
170 		tst_brkm(TBROK | TERRNO, cleanup, "fopen");
171 	while (fgets(buf, BUFSIZ, fp) != NULL) {
172 		if (sscanf(buf, "%p-%p ", &s, &t) != 2)
173 			continue;
174 		if (addr_s == s) {
175 			tst_resm(TINFO, "s = %p, t = %p", s, t);
176 			fclose(fp);
177 			return t;
178 		}
179 	}
180 	fclose(fp);
181 	tst_brkm(TBROK, cleanup, "no matched s = %p found.", addr_s);
182 }
183 
check_status(int status)184 static void check_status(int status)
185 {
186 	switch (status) {
187 	case 0:
188 		tst_resm(TPASS, "two 3*ps VMAs found.");
189 		break;
190 	case 1:
191 		if (tst_kvercmp(3, 0, 0) < 0) {
192 			tst_resm(TCONF, "A single 6*ps VMA found. You may need"
193 					" to back port kernel commit 965f55d "
194 					"to fix this scalability issue.");
195 		} else {
196 			tst_resm(TFAIL, "A single 6*ps VMA found.");
197 		}
198 		break;
199 	default:
200 		tst_brkm(TBROK, cleanup, "unexpected VMA found.");
201 	}
202 }
203 
setup(void)204 static void setup(void)
205 {
206 	tst_sig(FORK, DEF_HANDLER, cleanup);
207 
208 	TEST_PAUSE;
209 }
210 
cleanup(void)211 static void cleanup(void)
212 {
213 }
214