robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 1 | /* IBM Corporation */ |
| 2 | /* 01/02/2003 Port to LTP avenkat@us.ibm.com */ |
| 3 | /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ |
| 4 | /* |
| 5 | * Copyright (c) International Business Machines Corp., 2003 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * This test mmaps over the tail of the brk segment, growing and |
| 24 | * shrinking brk over holes, while changing from small to large and |
| 25 | * large to small virtual memory representations. After mmaping over the |
| 26 | * end of the brk segment, it increases the brk which should split |
| 27 | * it into two segments (i.e. |---brk---|-mmap-|--more brk--|). Next it |
| 28 | * decreases the brk segment to the end of the map, and finally decreases |
| 29 | * it some more. Then more vmsegments are created by punching holes in |
| 30 | * the brk segments with munmap. This should cause the vm system to use a |
| 31 | * large virtual address space object to keep track of this process. The |
| 32 | * above test is then repeated using the large process object. After |
| 33 | * this, the brk is shrunk to less than 1 page before exiting in order to |
| 34 | * test the code which compacts large address space objects. It also asks |
| 35 | * for a huge mmap which is refused. |
| 36 | */ |
| 37 | |
| 38 | #define _KMEMUSER |
| 39 | #include <sys/types.h> |
| 40 | #include <stdio.h> |
| 41 | #include <sys/mman.h> |
vapier | f81795e | 2006-02-15 06:28:58 +0000 | [diff] [blame] | 42 | #include <errno.h> |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 43 | #include <unistd.h> |
| 44 | #include <limits.h> |
| 45 | |
| 46 | /***** LTP Port *****/ |
| 47 | #include "test.h" |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 48 | #define FAILED 0 |
| 49 | #define PASSED 1 |
| 50 | |
| 51 | char *TCID = "mmapstress03"; |
| 52 | FILE *temp; |
| 53 | int TST_TOTAL = 1; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 54 | |
| 55 | int anyfail(); |
| 56 | void ok_exit(); |
| 57 | /***** ** ** *****/ |
| 58 | |
subrata_modak | ba205d2 | 2008-12-11 10:39:40 +0000 | [diff] [blame] | 59 | #define AS_SVSM_VSEG_MAX 48UL |
| 60 | #define AS_SVSM_MMAP_MAX 16UL |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 61 | |
subrata_modak | ba205d2 | 2008-12-11 10:39:40 +0000 | [diff] [blame] | 62 | #define EXTRA_VSEGS 2L |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 63 | #define NUM_SEGS (AS_SVSM_VSEG_MAX + EXTRA_VSEGS) |
| 64 | #define ERROR(M) (void)fprintf(stderr, "%s: errno = %d: " M "\n", progname, \ |
| 65 | errno) |
| 66 | #define NEG1 (char *)-1 |
subrata_modak | 36f6339 | 2009-01-06 05:39:35 +0000 | [diff] [blame] | 67 | #define POINTER_SIZE (sizeof(void *) << 3) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 68 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 69 | extern long sysconf(int name); |
| 70 | extern void exit(int); |
| 71 | extern time_t time(time_t *); |
| 72 | extern char *ctime(const time_t *); |
| 73 | static void do_test(caddr_t brk_max, long pagesize); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 74 | |
| 75 | static char *progname; |
| 76 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | /*ARGSUSED*/ int main(int argc, char *argv[]) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 78 | { |
| 79 | char *brk_max_addr, *hole_addr, *brk_start, *hole_start; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | size_t pagesize = (size_t) sysconf(_SC_PAGE_SIZE); |
| 81 | time_t t; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 82 | |
| 83 | progname = argv[0]; |
| 84 | |
| 85 | (void)time(&t); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 86 | // (void)printf("%s: Started %s", argv[0], ctime(&t)); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 87 | if ((brk_start = sbrk(0)) == NEG1) { |
| 88 | ERROR("initial sbrk failed"); |
| 89 | anyfail(); |
| 90 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | if ((u_long) brk_start % (u_long) pagesize) { |
| 92 | if (sbrk(pagesize - ((u_long) brk_start % (u_long) pagesize)) |
| 93 | == NEG1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 94 | ERROR("couldn't round up brk to a page boundary"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | /* The brk is now at the beginning of a page. */ |
| 99 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 100 | if ((hole_addr = hole_start = sbrk(NUM_SEGS * 2 * pagesize)) == NEG1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 101 | ERROR("couldn't brk large space for segments"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 103 | } |
| 104 | if ((brk_max_addr = sbrk(0)) == NEG1) { |
| 105 | ERROR("couldn't find top of brk"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 107 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 108 | do_test((caddr_t) brk_max_addr, pagesize); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 109 | |
| 110 | /* now make holes and repeat test */ |
| 111 | while (hole_addr + pagesize < brk_max_addr) { |
| 112 | if (munmap(hole_addr, pagesize) == -1) { |
| 113 | ERROR("failed to munmap odd hole in brk segment"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 115 | } |
| 116 | hole_addr += 2 * pagesize; |
| 117 | } |
| 118 | |
| 119 | if (brk_max_addr != sbrk(0)) { |
| 120 | ERROR("do_test should leave the top of brk where it began"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 122 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | do_test((caddr_t) brk_max_addr, pagesize); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 124 | |
| 125 | /* Shrink brk */ |
| 126 | if (sbrk(-NUM_SEGS * pagesize) == NEG1) { |
| 127 | ERROR("couldn't brk back over holes"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 129 | } |
| 130 | if ((brk_max_addr = sbrk(0)) == NEG1) { |
| 131 | ERROR("couldn't find top of break again"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 133 | } |
| 134 | /* sbrked over about half the holes */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 135 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | hole_addr = hole_start + pagesize; /* munmap the other pages */ |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 137 | while (hole_addr + pagesize < brk_max_addr) { |
| 138 | if (munmap(hole_addr, pagesize) == -1) { |
| 139 | ERROR("failed to munmap even hole in brk segment"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 141 | } |
| 142 | hole_addr += 2 * pagesize; |
| 143 | } |
| 144 | /* munmaped the rest of the brk except a little at the beginning */ |
| 145 | |
| 146 | if (brk(brk_start) == -1) { |
| 147 | ERROR("failed to completely remove brk"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 148 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 149 | } |
| 150 | if (sbrk(pagesize) == NEG1 || sbrk(-pagesize) == NEG1) { |
| 151 | ERROR("failed to fiddle with brk at the end"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 153 | } |
| 154 | /* Ask for a ridiculously large mmap region at a high address */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | if (mmap((caddr_t) (1UL << (POINTER_SIZE - 1)) - pagesize, |
| 156 | (size_t) ((1UL << (POINTER_SIZE - 1)) - pagesize), |
| 157 | PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_FIXED | MAP_SHARED, |
| 158 | 0, 0) |
| 159 | != (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 160 | ERROR("really large mmap didn't fail"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 161 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 162 | } |
subrata_modak | 428f6a3 | 2009-06-18 16:52:42 +0000 | [diff] [blame] | 163 | if (errno != ENOMEM && errno != EINVAL) { |
| 164 | ERROR("really large mmap didn't set errno = ENOMEM nor EINVAL"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 165 | anyfail(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 166 | } |
| 167 | (void)time(&t); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | // (void)printf("%s: Finished %s", argv[0], ctime(&t)); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 169 | ok_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 170 | tst_exit(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
| 174 | * do_test assumes that brk_max is a multiple of pagesize |
| 175 | */ |
| 176 | |
| 177 | static |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | void do_test(caddr_t brk_max, long pagesize) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 179 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | if (mmap((caddr_t) ((long)brk_max - 3 * pagesize), (2 * pagesize), |
| 181 | PROT_READ | PROT_WRITE, |
| 182 | MAP_ANONYMOUS | MAP_FIXED | MAP_PRIVATE, 0, 0) |
| 183 | == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 184 | ERROR("mmap failed"); |
| 185 | anyfail(); |
| 186 | } |
| 187 | /* extend mmap */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | if (mmap((caddr_t) ((long)brk_max - 2 * pagesize), (2 * pagesize), |
| 189 | PROT_READ | PROT_WRITE, |
| 190 | MAP_ANONYMOUS | MAP_FIXED | MAP_PRIVATE, 0, 0) |
| 191 | == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 192 | ERROR("mmap failed"); |
| 193 | anyfail(); |
| 194 | } |
| 195 | if (sbrk(pagesize) == NEG1) { |
| 196 | ERROR("sbrk failed to grow over mmaped region"); |
| 197 | anyfail(); |
| 198 | } |
| 199 | if (sbrk(-pagesize) == NEG1) { |
| 200 | ERROR("sbrk failed to shrink back to mmaped region"); |
| 201 | anyfail(); |
| 202 | } |
| 203 | if (sbrk(-pagesize) == NEG1) { |
| 204 | ERROR("sbrk failed to shrink over mmaped region more"); |
| 205 | anyfail(); |
| 206 | } |
| 207 | if (sbrk(-pagesize) == NEG1) { |
| 208 | ERROR("sbrk failed to shrink some more"); |
| 209 | anyfail(); |
| 210 | } |
| 211 | if (sbrk(2 * pagesize) == NEG1) { |
| 212 | ERROR("sbrk failed to change brk segment to original size"); |
| 213 | anyfail(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /***** LTP Port *****/ |
| 218 | void ok_exit() |
| 219 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 220 | tst_resm(TPASS, "Test passed"); |
| 221 | tst_exit(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 222 | } |
| 223 | |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 224 | int anyfail() |
| 225 | { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 226 | tst_brkm(TFAIL, NULL, "Test failed"); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 229 | /***** ** ** *****/ |