[6ccf55]: testcases / kernel / mem / vma / vma01.c Maximize Restore History

Download this file

vma01.c    181 lines (160 with data), 4.5 kB

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