Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 2 | * Copyright (C) 2010 Red Hat, Inc. |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of version 2 of the GNU General Public |
| 5 | * License as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it would be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | * |
| 11 | * Further, this software is distributed without any warranty that it |
| 12 | * is free of the rightful claim of any third person regarding |
| 13 | * infringement or the like. Any license provided herein, whether |
| 14 | * implied or otherwise, applies only to this software file. Patent |
| 15 | * licenses, if any, provided herein do not apply to combinations of |
| 16 | * this program with other software, or any other product whatsoever. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 21 | * 02110-1301, USA. |
| 22 | */ |
| 23 | |
| 24 | /* |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 25 | * mmap/munmap /dev/zero: a common way of malloc()/free() anonymous |
| 26 | * memory on Solaris. |
| 27 | * |
| 28 | * The basic purpose of this is a to test if it is possible to map and |
| 29 | * unmap /dev/zero, and to read and write the mapping. Being inspired |
| 30 | * by two bugs in the past, the design of the test was added some |
| 31 | * variations based on the reproducers for them. It also accept an |
| 32 | * option to mmap/munmap anonymous pages. |
| 33 | * |
| 34 | * One is to trigger panic with transparent hugepage feature that |
| 35 | * split_huge_page is very strict in checking the rmap walk was |
| 36 | * perfect. Keep it strict because if page_mapcount isn't stable and |
| 37 | * just right, the __split_huge_page_refcount that follows the rmap |
| 38 | * walk could lead to erratic page_count()s for the subpages. The bug |
| 39 | * in fork lead to the rmap walk finding the parent huge-pmd twice |
| 40 | * instead of just one, because the anon_vma_chain objects of the |
| 41 | * child vma still point to the vma->vm_mm of the parent. That trips |
| 42 | * on the split_huge_page mapcount vs page_mapcount check leading to a |
| 43 | * BUG_ON. |
| 44 | * |
| 45 | * The other bug is mmap() of /dev/zero results in calling map_zero() |
| 46 | * which on RHEL5 maps the ZERO_PAGE in every PTE within that virtual |
| 47 | * address range. Since the application which maps a region from 5M to |
| 48 | * 16M in size is also multi-threaded the subsequent munmap() of |
| 49 | * /dev/zero results is TLB shootdowns to all other CPUs. When this |
| 50 | * happens thousands or millions of times the application performance |
| 51 | * is terrible. The mapping ZERO_PAGE in every pte within that virtual |
| 52 | * address range was an optimization to make the subsequent pagefault |
| 53 | * times faster on RHEL5 that has been removed/changed upstream. |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 54 | */ |
| 55 | #include <sys/types.h> |
| 56 | #include <sys/stat.h> |
| 57 | #include <sys/wait.h> |
| 58 | #include <sys/mman.h> |
| 59 | #include <errno.h> |
| 60 | #include <unistd.h> |
| 61 | #include <stdlib.h> |
| 62 | #include <stdio.h> |
| 63 | #include <fcntl.h> |
| 64 | #include "test.h" |
Caspar Zhang | 74c3dd9 | 2011-03-02 18:06:10 +0800 | [diff] [blame] | 65 | #include "config.h" |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 66 | |
| 67 | #define SIZE (5*1024*1024) |
Nageswara R Sastry | 2fbbc93 | 2011-09-12 17:25:19 +0530 | [diff] [blame] | 68 | #define PATH_KSM "/sys/kernel/mm/ksm/" |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 69 | |
| 70 | char *TCID = "mmap10"; |
| 71 | int TST_TOTAL = 1; |
Cyril Hrubis | 877cbae | 2011-02-07 20:00:31 +0100 | [diff] [blame] | 72 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 73 | static int fd, opt_anon, opt_ksm; |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 74 | static long ps; |
| 75 | static char *x; |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 76 | |
| 77 | void setup(void); |
Garrett Cooper | eb16c91 | 2010-12-19 10:04:37 -0800 | [diff] [blame] | 78 | void cleanup(void); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 79 | void mmapzero(void); |
| 80 | void help(void); |
| 81 | |
| 82 | static option_t options[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | {"a", &opt_anon, NULL}, |
| 84 | {"s", &opt_ksm, NULL}, |
| 85 | {NULL, NULL, NULL} |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | int main(int argc, char *argv[]) |
| 89 | { |
| 90 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 91 | const char *msg; |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 92 | |
| 93 | msg = parse_opts(argc, argv, options, help); |
| 94 | if (msg != NULL) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 95 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 96 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 97 | if (opt_ksm) { |
Nageswara R Sastry | 2fbbc93 | 2011-09-12 17:25:19 +0530 | [diff] [blame] | 98 | if (access(PATH_KSM, F_OK) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | tst_brkm(TCONF, NULL, |
| 100 | "KSM configuration is not enabled"); |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 101 | #ifdef HAVE_MADV_MERGEABLE |
| 102 | tst_resm(TINFO, "add to KSM regions."); |
| 103 | #else |
| 104 | tst_brkm(TCONF, NULL, "MADV_MERGEABLE missing in sys/mman.h"); |
| 105 | #endif |
| 106 | } |
| 107 | if (opt_anon) |
| 108 | tst_resm(TINFO, "use anonymous pages."); |
| 109 | else |
| 110 | tst_resm(TINFO, "use /dev/zero."); |
| 111 | |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 112 | setup(); |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 113 | |
| 114 | tst_resm(TINFO, "start tests."); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 115 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 116 | tst_count = 0; |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 117 | mmapzero(); |
| 118 | } |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 119 | |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 120 | cleanup(); |
Garrett Cooper | eb16c91 | 2010-12-19 10:04:37 -0800 | [diff] [blame] | 121 | tst_exit(); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void mmapzero(void) |
| 125 | { |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 126 | int n; |
| 127 | |
| 128 | if (opt_anon) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 129 | x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE, |
| 130 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 131 | } else { |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 132 | if ((fd = open("/dev/zero", O_RDWR, 0666)) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | tst_brkm(TBROK | TERRNO, cleanup, "open"); |
| 134 | x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE, |
| 135 | MAP_PRIVATE, fd, 0); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 136 | } |
| 137 | if (x == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 138 | tst_brkm(TFAIL | TERRNO, cleanup, "mmap"); |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 139 | #ifdef HAVE_MADV_MERGEABLE |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 140 | if (opt_ksm) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | if (madvise(x, SIZE + SIZE - ps, MADV_MERGEABLE) == -1) |
| 142 | tst_brkm(TBROK | TERRNO, cleanup, "madvise"); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 143 | } |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 144 | #endif |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 145 | x[SIZE] = 0; |
| 146 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 147 | switch (n = fork()) { |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 148 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 149 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 150 | case 0: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1) |
| 152 | tst_brkm(TFAIL | TERRNO, cleanup, "munmap"); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 153 | exit(0); |
| 154 | default: |
| 155 | break; |
| 156 | } |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 157 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 158 | switch (n = fork()) { |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 159 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 160 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 161 | case 0: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 162 | if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1) |
| 163 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 164 | "subsequent munmap #1"); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 165 | exit(0); |
| 166 | default: |
| 167 | switch (n = fork()) { |
| 168 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 169 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 170 | case 0: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 171 | if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1) |
| 172 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 173 | "subsequent munmap #2"); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 174 | exit(0); |
| 175 | default: |
| 176 | break; |
| 177 | } |
| 178 | break; |
| 179 | } |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 180 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 181 | if (munmap(x, SIZE + SIZE - ps) == -1) |
| 182 | tst_resm(TFAIL | TERRNO, "munmap all"); |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 183 | |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 184 | while (waitpid(-1, &n, WUNTRACED | WCONTINUED) > 0) |
| 185 | if (WEXITSTATUS(n) != 0) |
| 186 | tst_resm(TFAIL, "child exit status is %d", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 187 | WEXITSTATUS(n)); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void cleanup(void) |
| 191 | { |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void setup(void) |
| 195 | { |
Garrett Cooper | 40495b8 | 2011-01-19 00:48:39 -0800 | [diff] [blame] | 196 | tst_require_root(NULL); |
| 197 | |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 198 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 199 | TEST_PAUSE; |
Garrett Cooper | d4f51f1 | 2011-03-15 05:00:11 -0700 | [diff] [blame] | 200 | |
| 201 | if ((ps = sysconf(_SC_PAGESIZE)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 202 | tst_brkm(TBROK | TERRNO, cleanup, "sysconf(_SC_PAGESIZE)"); |
Garrett Cooper | e5d9840 | 2010-12-08 00:36:58 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void help(void) |
| 206 | { |
| 207 | printf(" -a Test anonymous pages\n"); |
| 208 | printf(" -s Add to KSM regions\n"); |
Garrett Cooper | eb16c91 | 2010-12-19 10:04:37 -0800 | [diff] [blame] | 209 | } |