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