blob: 79e5f52dd3ef6ef28b5266e2e5f11fb770a6749b [file] [log] [blame]
Caspar Zhange864b322011-08-11 22:03:59 +08001/*
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"
Caspar Zhange864b322011-08-11 22:03:59 +080054
55#define MAPS_FILE "/proc/self/maps"
56
57char *TCID = "vma01";
58int TST_TOTAL = 1;
59
60static void check_vma(void);
Caspar Zhang53481232012-06-25 15:33:03 +080061static void create_bighole(void);
Caspar Zhang6fe2a012012-06-25 15:33:02 +080062static void *get_end_addr(void *addr_s);
Caspar Zhang3f2ed412011-08-25 14:45:12 +080063static void check_status(int status);
Caspar Zhange864b322011-08-11 22:03:59 +080064static void setup(void);
65static void cleanup(void);
66
67static unsigned long ps;
Caspar Zhang53481232012-06-25 15:33:03 +080068static void *p;
Caspar Zhange864b322011-08-11 22:03:59 +080069
70int main(int argc, char **argv)
71{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020072 const char *msg;
Caspar Zhange864b322011-08-11 22:03:59 +080073 int lc;
74
75 msg = parse_opts(argc, argv, NULL, NULL);
76 if (msg != NULL)
77 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
78
79 ps = sysconf(_SC_PAGE_SIZE);
80 setup();
81
82 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080083 tst_count = 0;
Caspar Zhange864b322011-08-11 22:03:59 +080084
85 check_vma();
86 }
87 cleanup();
88 tst_exit();
89}
90
91static void check_vma(void)
92{
93 int status;
Caspar Zhang6fe2a012012-06-25 15:33:02 +080094 int topdown;
95 void *t, *u, *x;
Caspar Zhange864b322011-08-11 22:03:59 +080096
Caspar Zhang53481232012-06-25 15:33:03 +080097 create_bighole();
Wanlong Gao354ebb42012-12-07 10:10:04 +080098 t = mmap(p, 3 * ps, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Caspar Zhange864b322011-08-11 22:03:59 +080099 if (t == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100 tst_brkm(TBROK | TERRNO, cleanup, "mmap");
Caspar Zhange864b322011-08-11 22:03:59 +0800101 memset(t, 1, ps);
102
Caspar Zhange864b322011-08-11 22:03:59 +0800103 switch (fork()) {
104 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Caspar Zhange864b322011-08-11 22:03:59 +0800106 case 0:
107 memset(t, 2, ps);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 u = mmap(t + 3 * ps, 3 * ps, PROT_WRITE,
109 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Caspar Zhang3f2ed412011-08-25 14:45:12 +0800110 if (u == MAP_FAILED) {
Caspar Zhang6fe2a012012-06-25 15:33:02 +0800111 perror("mmap");
Caspar Zhang3f2ed412011-08-25 14:45:12 +0800112 exit(255);
113 }
Caspar Zhang6fe2a012012-06-25 15:33:02 +0800114 topdown = (u > t) ? 0 : 1;
Caspar Zhangfb005172011-09-27 17:27:18 +0800115 printf("parent: t = %p\n", t);
116 printf("child : u = %p\n", u);
117 memset(u, 2, ps);
Caspar Zhang3f2ed412011-08-25 14:45:12 +0800118
Caspar Zhang6fe2a012012-06-25 15:33:02 +0800119 if (topdown) {
120 x = get_end_addr(u);
121 printf("child : x = %p\n", x);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 if (x == t + 3 * ps)
Caspar Zhang6fe2a012012-06-25 15:33:02 +0800123 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 else if (x == t && get_end_addr(x) == t + 3 * ps)
Caspar Zhang6fe2a012012-06-25 15:33:02 +0800125 exit(0);
126 } else {
127 x = get_end_addr(t);
128 printf("child : x = %p\n", x);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 if (x == t + 6 * ps)
Caspar Zhang6fe2a012012-06-25 15:33:02 +0800130 exit(1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 else if (x == u && get_end_addr(x) == t + 6 * ps)
Caspar Zhang3f2ed412011-08-25 14:45:12 +0800132 exit(0);
133 }
134 exit(255);
Caspar Zhange864b322011-08-11 22:03:59 +0800135 default:
136 if (waitpid(-1, &status, 0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Caspar Zhang3f2ed412011-08-25 14:45:12 +0800138 if (!WIFEXITED(status))
139 tst_brkm(TBROK, cleanup, "child exited abnormally.");
140 check_status(WEXITSTATUS(status));
Caspar Zhange864b322011-08-11 22:03:59 +0800141 break;
142 }
Caspar Zhange864b322011-08-11 22:03:59 +0800143}
144
Caspar Zhang53481232012-06-25 15:33:03 +0800145static void create_bighole(void)
146{
147 void *t;
148
149 /*
150 * |-3*ps-|
151 * |------|------|------| hole
152 * t p
153 * |======|------| top-down alloc
154 * |------|======| bottom-up alloc
155 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 t = mmap(NULL, 9 * ps, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Caspar Zhang53481232012-06-25 15:33:03 +0800157 if (t == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800158 tst_brkm(TBROK | TERRNO, cleanup, "mmap");
Caspar Zhang53481232012-06-25 15:33:03 +0800159 memset(t, 'a', ps);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 p = t + 3 * ps;
161 if (munmap(t, 9 * ps) == -1)
162 tst_brkm(TBROK | TERRNO, cleanup, "munmap");
Caspar Zhang53481232012-06-25 15:33:03 +0800163}
164
Caspar Zhang6fe2a012012-06-25 15:33:02 +0800165static void *get_end_addr(void *addr_s)
Caspar Zhange864b322011-08-11 22:03:59 +0800166{
167 FILE *fp;
168 void *s, *t;
169 char buf[BUFSIZ];
170
Caspar Zhang6fe2a012012-06-25 15:33:02 +0800171 fp = fopen(MAPS_FILE, "r");
Caspar Zhange864b322011-08-11 22:03:59 +0800172 if (fp == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800173 tst_brkm(TBROK | TERRNO, cleanup, "fopen");
Caspar Zhange864b322011-08-11 22:03:59 +0800174 while (fgets(buf, BUFSIZ, fp) != NULL) {
175 if (sscanf(buf, "%p-%p ", &s, &t) != 2)
176 continue;
177 if (addr_s == s) {
178 tst_resm(TINFO, "s = %p, t = %p", s, t);
179 fclose(fp);
180 return t;
181 }
182 }
183 fclose(fp);
184 tst_brkm(TBROK, cleanup, "no matched s = %p found.", addr_s);
185}
186
Caspar Zhang3f2ed412011-08-25 14:45:12 +0800187static void check_status(int status)
188{
189 switch (status) {
190 case 0:
191 tst_resm(TPASS, "two 3*ps VMAs found.");
192 break;
193 case 1:
Wanlong Gaob69a6712013-07-04 12:31:27 +0800194 if (tst_kvercmp(3, 0, 0) < 0) {
195 tst_resm(TCONF, "A single 6*ps VMA found. You may need"
196 " to back port kernel commit 965f55d "
197 "to fix this scalability issue.");
198 } else {
199 tst_resm(TFAIL, "A single 6*ps VMA found.");
200 }
Caspar Zhang3f2ed412011-08-25 14:45:12 +0800201 break;
202 default:
203 tst_brkm(TBROK, cleanup, "unexpected VMA found.");
204 }
205}
206
Caspar Zhange864b322011-08-11 22:03:59 +0800207static void setup(void)
208{
209 tst_sig(FORK, DEF_HANDLER, cleanup);
210
211 TEST_PAUSE;
212}
213
214static void cleanup(void)
215{
Caspar Zhange864b322011-08-11 22:03:59 +0800216}