Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 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 "usctest.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); |
Caspar Zhang | 5348123 | 2012-06-25 15:33:03 +0800 | [diff] [blame] | 62 | static void create_bighole(void); |
Caspar Zhang | 6fe2a01 | 2012-06-25 15:33:02 +0800 | [diff] [blame] | 63 | static void *get_end_addr(void *addr_s); |
Caspar Zhang | 3f2ed41 | 2011-08-25 14:45:12 +0800 | [diff] [blame] | 64 | static void check_status(int status); |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 65 | static void setup(void); |
| 66 | static void cleanup(void); |
| 67 | |
| 68 | static unsigned long ps; |
Caspar Zhang | 5348123 | 2012-06-25 15:33:03 +0800 | [diff] [blame] | 69 | static void *p; |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 70 | |
| 71 | int main(int argc, char **argv) |
| 72 | { |
| 73 | char *msg; |
| 74 | int lc; |
| 75 | |
| 76 | msg = parse_opts(argc, argv, NULL, NULL); |
| 77 | if (msg != NULL) |
| 78 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 79 | |
| 80 | ps = sysconf(_SC_PAGE_SIZE); |
| 81 | setup(); |
| 82 | |
| 83 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame^] | 84 | tst_count = 0; |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 85 | |
| 86 | check_vma(); |
| 87 | } |
| 88 | cleanup(); |
| 89 | tst_exit(); |
| 90 | } |
| 91 | |
| 92 | static void check_vma(void) |
| 93 | { |
| 94 | int status; |
Caspar Zhang | 6fe2a01 | 2012-06-25 15:33:02 +0800 | [diff] [blame] | 95 | int topdown; |
| 96 | void *t, *u, *x; |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 97 | |
Caspar Zhang | 5348123 | 2012-06-25 15:33:03 +0800 | [diff] [blame] | 98 | create_bighole(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | t = mmap(p, 3 * ps, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 100 | if (t == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 101 | tst_brkm(TBROK | TERRNO, cleanup, "mmap"); |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 102 | memset(t, 1, ps); |
| 103 | |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 104 | switch (fork()) { |
| 105 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 107 | case 0: |
| 108 | memset(t, 2, ps); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 109 | u = mmap(t + 3 * ps, 3 * ps, PROT_WRITE, |
| 110 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
Caspar Zhang | 3f2ed41 | 2011-08-25 14:45:12 +0800 | [diff] [blame] | 111 | if (u == MAP_FAILED) { |
Caspar Zhang | 6fe2a01 | 2012-06-25 15:33:02 +0800 | [diff] [blame] | 112 | perror("mmap"); |
Caspar Zhang | 3f2ed41 | 2011-08-25 14:45:12 +0800 | [diff] [blame] | 113 | exit(255); |
| 114 | } |
Caspar Zhang | 6fe2a01 | 2012-06-25 15:33:02 +0800 | [diff] [blame] | 115 | topdown = (u > t) ? 0 : 1; |
Caspar Zhang | fb00517 | 2011-09-27 17:27:18 +0800 | [diff] [blame] | 116 | printf("parent: t = %p\n", t); |
| 117 | printf("child : u = %p\n", u); |
| 118 | memset(u, 2, ps); |
Caspar Zhang | 3f2ed41 | 2011-08-25 14:45:12 +0800 | [diff] [blame] | 119 | |
Caspar Zhang | 6fe2a01 | 2012-06-25 15:33:02 +0800 | [diff] [blame] | 120 | if (topdown) { |
| 121 | x = get_end_addr(u); |
| 122 | printf("child : x = %p\n", x); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | if (x == t + 3 * ps) |
Caspar Zhang | 6fe2a01 | 2012-06-25 15:33:02 +0800 | [diff] [blame] | 124 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | else if (x == t && get_end_addr(x) == t + 3 * ps) |
Caspar Zhang | 6fe2a01 | 2012-06-25 15:33:02 +0800 | [diff] [blame] | 126 | exit(0); |
| 127 | } else { |
| 128 | x = get_end_addr(t); |
| 129 | printf("child : x = %p\n", x); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | if (x == t + 6 * ps) |
Caspar Zhang | 6fe2a01 | 2012-06-25 15:33:02 +0800 | [diff] [blame] | 131 | exit(1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | else if (x == u && get_end_addr(x) == t + 6 * ps) |
Caspar Zhang | 3f2ed41 | 2011-08-25 14:45:12 +0800 | [diff] [blame] | 133 | exit(0); |
| 134 | } |
| 135 | exit(255); |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 136 | default: |
| 137 | if (waitpid(-1, &status, 0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 138 | tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); |
Caspar Zhang | 3f2ed41 | 2011-08-25 14:45:12 +0800 | [diff] [blame] | 139 | if (!WIFEXITED(status)) |
| 140 | tst_brkm(TBROK, cleanup, "child exited abnormally."); |
| 141 | check_status(WEXITSTATUS(status)); |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 142 | break; |
| 143 | } |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 144 | } |
| 145 | |
Caspar Zhang | 5348123 | 2012-06-25 15:33:03 +0800 | [diff] [blame] | 146 | static void create_bighole(void) |
| 147 | { |
| 148 | void *t; |
| 149 | |
| 150 | /* |
| 151 | * |-3*ps-| |
| 152 | * |------|------|------| hole |
| 153 | * t p |
| 154 | * |======|------| top-down alloc |
| 155 | * |------|======| bottom-up alloc |
| 156 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | t = mmap(NULL, 9 * ps, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
Caspar Zhang | 5348123 | 2012-06-25 15:33:03 +0800 | [diff] [blame] | 158 | if (t == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | tst_brkm(TBROK | TERRNO, cleanup, "mmap"); |
Caspar Zhang | 5348123 | 2012-06-25 15:33:03 +0800 | [diff] [blame] | 160 | memset(t, 'a', ps); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 161 | p = t + 3 * ps; |
| 162 | if (munmap(t, 9 * ps) == -1) |
| 163 | tst_brkm(TBROK | TERRNO, cleanup, "munmap"); |
Caspar Zhang | 5348123 | 2012-06-25 15:33:03 +0800 | [diff] [blame] | 164 | } |
| 165 | |
Caspar Zhang | 6fe2a01 | 2012-06-25 15:33:02 +0800 | [diff] [blame] | 166 | static void *get_end_addr(void *addr_s) |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 167 | { |
| 168 | FILE *fp; |
| 169 | void *s, *t; |
| 170 | char buf[BUFSIZ]; |
| 171 | |
Caspar Zhang | 6fe2a01 | 2012-06-25 15:33:02 +0800 | [diff] [blame] | 172 | fp = fopen(MAPS_FILE, "r"); |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 173 | if (fp == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | tst_brkm(TBROK | TERRNO, cleanup, "fopen"); |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 175 | while (fgets(buf, BUFSIZ, fp) != NULL) { |
| 176 | if (sscanf(buf, "%p-%p ", &s, &t) != 2) |
| 177 | continue; |
| 178 | if (addr_s == s) { |
| 179 | tst_resm(TINFO, "s = %p, t = %p", s, t); |
| 180 | fclose(fp); |
| 181 | return t; |
| 182 | } |
| 183 | } |
| 184 | fclose(fp); |
| 185 | tst_brkm(TBROK, cleanup, "no matched s = %p found.", addr_s); |
| 186 | } |
| 187 | |
Caspar Zhang | 3f2ed41 | 2011-08-25 14:45:12 +0800 | [diff] [blame] | 188 | static void check_status(int status) |
| 189 | { |
| 190 | switch (status) { |
| 191 | case 0: |
| 192 | tst_resm(TPASS, "two 3*ps VMAs found."); |
| 193 | break; |
| 194 | case 1: |
| 195 | tst_resm(TFAIL, "A single 6*ps VMA found."); |
| 196 | break; |
| 197 | default: |
| 198 | tst_brkm(TBROK, cleanup, "unexpected VMA found."); |
| 199 | } |
| 200 | } |
| 201 | |
Caspar Zhang | e864b32 | 2011-08-11 22:03:59 +0800 | [diff] [blame] | 202 | static void setup(void) |
| 203 | { |
| 204 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 205 | |
| 206 | TEST_PAUSE; |
| 207 | } |
| 208 | |
| 209 | static void cleanup(void) |
| 210 | { |
| 211 | TEST_CLEANUP; |
| 212 | } |