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 #include "safe_macros.h"
55
56 #define MAPS_FILE "/proc/self/maps"
57
58 char *TCID = "vma01";
59 int TST_TOTAL = 1;
60
61 static void check_vma(void);
62 static void create_bighole(void);
63 static void *get_end_addr(void *addr_s);
64 static void check_status(int status);
65 static void setup(void);
66 static void cleanup(void);
67
68 static unsigned long ps;
69 static void *p;
70
main(int argc,char ** argv)71 int main(int argc, char **argv)
72 {
73 int lc;
74
75 tst_parse_opts(argc, argv, NULL, NULL);
76
77 ps = sysconf(_SC_PAGE_SIZE);
78 setup();
79
80 for (lc = 0; TEST_LOOPING(lc); lc++) {
81 tst_count = 0;
82
83 check_vma();
84 }
85 cleanup();
86 tst_exit();
87 }
88
check_vma(void)89 static void check_vma(void)
90 {
91 int status;
92 int topdown;
93 void *t, *u, *x;
94
95 create_bighole();
96 t = mmap(p, 3 * ps, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
97 if (t == MAP_FAILED)
98 tst_brkm(TBROK | TERRNO, cleanup, "mmap");
99 memset(t, 1, ps);
100
101 switch (fork()) {
102 case -1:
103 tst_brkm(TBROK | TERRNO, cleanup, "fork");
104 case 0:
105 memset(t, 2, ps);
106 u = mmap(t + 3 * ps, 3 * ps, PROT_WRITE,
107 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
108 if (u == MAP_FAILED) {
109 perror("mmap");
110 exit(255);
111 }
112 topdown = (u > t) ? 0 : 1;
113 printf("parent: t = %p\n", t);
114 printf("child : u = %p\n", u);
115 memset(u, 2, ps);
116
117 if (topdown) {
118 x = get_end_addr(u);
119 printf("child : x = %p\n", x);
120 if (x == t + 3 * ps)
121 exit(1);
122 else if (x == t && get_end_addr(x) == t + 3 * ps)
123 exit(0);
124 } else {
125 x = get_end_addr(t);
126 printf("child : x = %p\n", x);
127 if (x == t + 6 * ps)
128 exit(1);
129 else if (x == u && get_end_addr(x) == t + 6 * ps)
130 exit(0);
131 }
132 exit(255);
133 default:
134 SAFE_WAITPID(cleanup, -1, &status, 0);
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 SAFE_MUNMAP(cleanup, t, 9 * ps);
159 }
160
get_end_addr(void * addr_s)161 static void *get_end_addr(void *addr_s)
162 {
163 FILE *fp;
164 void *s, *t;
165 char buf[BUFSIZ];
166
167 fp = fopen(MAPS_FILE, "r");
168 if (fp == NULL)
169 tst_brkm(TBROK | TERRNO, cleanup, "fopen");
170 while (fgets(buf, BUFSIZ, fp) != NULL) {
171 if (sscanf(buf, "%p-%p ", &s, &t) != 2)
172 continue;
173 if (addr_s == s) {
174 tst_resm(TINFO, "s = %p, t = %p", s, t);
175 fclose(fp);
176 return t;
177 }
178 }
179 fclose(fp);
180 tst_brkm(TBROK, cleanup, "no matched s = %p found.", addr_s);
181 }
182
check_status(int status)183 static void check_status(int status)
184 {
185 switch (status) {
186 case 0:
187 tst_resm(TPASS, "two 3*ps VMAs found.");
188 break;
189 case 1:
190 if (tst_kvercmp(3, 0, 0) < 0) {
191 tst_resm(TCONF, "A single 6*ps VMA found. You may need"
192 " to back port kernel commit 965f55d "
193 "to fix this scalability issue.");
194 } else {
195 tst_resm(TFAIL, "A single 6*ps VMA found.");
196 }
197 break;
198 default:
199 tst_brkm(TBROK, cleanup, "unexpected VMA found.");
200 }
201 }
202
setup(void)203 static void setup(void)
204 {
205 tst_sig(FORK, DEF_HANDLER, cleanup);
206
207 TEST_PAUSE;
208 }
209
cleanup(void)210 static void cleanup(void)
211 {
212 }
213