Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This is a reproducer from mainline commit |
| 3 | * 9d8cebd4bcd7c3878462fdfda34bbcdeb4df7ef4: |
| 4 | * |
| 5 | * "Strangely, current mbind() doesn't merge vma with neighbor vma |
| 6 | * although it's possible. Unfortunately, many vma can reduce |
| 7 | * performance..." |
| 8 | * |
| 9 | * Copyright (C) 2010 Red Hat, Inc. |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of version 2 of the GNU General Public |
| 12 | * License as published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it would be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | * |
| 18 | * Further, this software is distributed without any warranty that it |
| 19 | * is free of the rightful claim of any third person regarding |
| 20 | * infringement or the like. Any license provided herein, whether |
| 21 | * implied or otherwise, applies only to this software file. Patent |
| 22 | * licenses, if any, provided herein do not apply to combinations of |
| 23 | * this program with other software, or any other product whatsoever. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write the Free Software |
| 27 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 28 | * 02110-1301, USA. |
| 29 | */ |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 30 | #include "config.h" |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/mman.h> |
| 33 | #include <errno.h> |
| 34 | #if HAVE_NUMA_H |
| 35 | #include <numa.h> |
| 36 | #endif |
| 37 | #if HAVE_NUMAIF_H |
| 38 | #include <numaif.h> |
| 39 | #endif |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <unistd.h> |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 44 | #include "test.h" |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 45 | #include "safe_macros.h" |
| 46 | #include "numa_helper.h" |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 47 | |
Caspar Zhang | ecc0bf8 | 2011-08-01 09:26:09 +0800 | [diff] [blame] | 48 | char *TCID = "vma02"; |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 49 | int TST_TOTAL = 1; |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 50 | |
| 51 | #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \ |
| 52 | && HAVE_MPOL_CONSTANTS |
Garrett Cooper | 7b426ce | 2011-04-21 23:36:26 -0700 | [diff] [blame] | 53 | #if defined(LIBNUMA_API_VERSION) && LIBNUMA_API_VERSION == 2 |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 54 | static unsigned long pagesize; |
| 55 | static int opt_node; |
| 56 | static char *opt_nodestr; |
| 57 | static option_t options[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 58 | {"n:", &opt_node, &opt_nodestr}, |
| 59 | {NULL, NULL, NULL} |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static void usage(void); |
| 63 | |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 64 | int main(int argc, char **argv) |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 65 | { |
| 66 | FILE *fp; |
Caspar Zhang | 6f864d9 | 2011-08-01 09:26:08 +0800 | [diff] [blame] | 67 | void *addr, *start, *end, *lastend; |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 68 | int node, err, lc; |
Caspar Zhang | 6f864d9 | 2011-08-01 09:26:08 +0800 | [diff] [blame] | 69 | char buf[BUFSIZ]; |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 70 | struct bitmask *nmask = numa_allocate_nodemask(); |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 71 | const char *msg; |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 72 | |
| 73 | pagesize = getpagesize(); |
| 74 | msg = parse_opts(argc, argv, options, usage); |
| 75 | if (msg != NULL) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 76 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 77 | |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 78 | if (opt_node) { |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 79 | node = SAFE_STRTOL(NULL, opt_nodestr, 1, LONG_MAX); |
| 80 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 81 | err = get_allowed_nodes(NH_MEMS | NH_MEMS, 1, &node); |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 82 | if (err == -3) |
| 83 | tst_brkm(TCONF, NULL, "requires at least one node."); |
| 84 | else if (err < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 85 | tst_brkm(TBROK | TERRNO, NULL, "get_allowed_nodes"); |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 86 | } |
| 87 | numa_bitmask_setbit(nmask, node); |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 88 | |
| 89 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 90 | tst_count = 0; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | addr = mmap(NULL, pagesize * 3, PROT_WRITE, |
| 92 | MAP_ANON | MAP_PRIVATE, 0, 0); |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 93 | if (addr == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 94 | tst_brkm(TBROK | TERRNO, NULL, "mmap"); |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 95 | |
| 96 | tst_resm(TINFO, "pid = %d addr = %p", getpid(), addr); |
| 97 | /* make page populate */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | memset(addr, 0, pagesize * 3); |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 99 | |
| 100 | /* first mbind */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 101 | err = mbind(addr + pagesize, pagesize, MPOL_BIND, nmask->maskp, |
| 102 | nmask->size, MPOL_MF_MOVE_ALL); |
Garrett Cooper | 14d4960 | 2011-04-11 23:47:45 -0700 | [diff] [blame] | 103 | if (err != 0) { |
| 104 | if (errno != ENOSYS) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | tst_brkm(TBROK | TERRNO, NULL, "mbind1"); |
Garrett Cooper | 14d4960 | 2011-04-11 23:47:45 -0700 | [diff] [blame] | 106 | else |
| 107 | tst_brkm(TCONF, NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 108 | "mbind syscall not implemented on this system."); |
Garrett Cooper | 14d4960 | 2011-04-11 23:47:45 -0700 | [diff] [blame] | 109 | } |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 110 | |
| 111 | /* second mbind */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 112 | err = mbind(addr, pagesize * 3, MPOL_DEFAULT, NULL, 0, 0); |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 113 | if (err != 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | tst_brkm(TBROK | TERRNO, NULL, "mbind2"); |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 115 | |
| 116 | /* /proc/self/maps in the form of |
| 117 | "00400000-00406000 r-xp 00000000". */ |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 118 | fp = fopen("/proc/self/maps", "r"); |
| 119 | if (fp == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 120 | tst_brkm(TBROK | TERRNO, NULL, "fopen"); |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 121 | |
| 122 | while (fgets(buf, BUFSIZ, fp) != NULL) { |
Caspar Zhang | 6f864d9 | 2011-08-01 09:26:08 +0800 | [diff] [blame] | 123 | if (sscanf(buf, "%p-%p ", &start, &end) != 2) |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 124 | continue; |
Caspar Zhang | 6f864d9 | 2011-08-01 09:26:08 +0800 | [diff] [blame] | 125 | |
| 126 | if (start == addr) { |
| 127 | tst_resm(TINFO, "start = %p, end = %p", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | start, end); |
| 129 | if (end == addr + pagesize * 3) { |
Caspar Zhang | 6f864d9 | 2011-08-01 09:26:08 +0800 | [diff] [blame] | 130 | tst_resm(TPASS, "only 1 VMA."); |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | lastend = end; |
| 135 | while (fgets(buf, BUFSIZ, fp) != NULL) { |
| 136 | /* No more VMAs, break */ |
| 137 | if (sscanf(buf, "%p-%p ", &start, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 138 | &end) != 2) |
Caspar Zhang | 6f864d9 | 2011-08-01 09:26:08 +0800 | [diff] [blame] | 139 | break; |
| 140 | tst_resm(TINFO, "start = %p, end = %p", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | start, end); |
Caspar Zhang | 6f864d9 | 2011-08-01 09:26:08 +0800 | [diff] [blame] | 142 | |
| 143 | /* more VMAs found */ |
| 144 | if (start == lastend) |
| 145 | lastend = end; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 146 | if (end == addr + pagesize * 3) { |
Caspar Zhang | 6f864d9 | 2011-08-01 09:26:08 +0800 | [diff] [blame] | 147 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 148 | ">1 unmerged VMAs."); |
Caspar Zhang | 6f864d9 | 2011-08-01 09:26:08 +0800 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | if (end != addr + pagesize * 3) |
Caspar Zhang | 6f864d9 | 2011-08-01 09:26:08 +0800 | [diff] [blame] | 153 | tst_resm(TFAIL, "no matched VMAs."); |
| 154 | break; |
| 155 | } |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 156 | } |
| 157 | fclose(fp); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 158 | if (munmap(addr, pagesize * 3) == -1) |
| 159 | tst_brkm(TWARN | TERRNO, NULL, "munmap"); |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 160 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 161 | tst_exit(); |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void usage(void) |
| 165 | { |
| 166 | printf(" -n Number of NUMA nodes\n"); |
| 167 | } |
Garrett Cooper | 7b426ce | 2011-04-21 23:36:26 -0700 | [diff] [blame] | 168 | #else /* libnuma v1 */ |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 169 | int main(void) |
| 170 | { |
Garrett Cooper | 203573c | 2011-05-04 09:55:59 -0700 | [diff] [blame] | 171 | tst_brkm(TCONF, NULL, "XXX: test is only supported on libnuma v2."); |
Garrett Cooper | 7b426ce | 2011-04-21 23:36:26 -0700 | [diff] [blame] | 172 | } |
| 173 | #endif |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 174 | #else /* no NUMA */ |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 175 | int main(void) |
| 176 | { |
Garrett Cooper | 203573c | 2011-05-04 09:55:59 -0700 | [diff] [blame] | 177 | tst_brkm(TCONF, NULL, "no NUMA development packages installed."); |
Garrett Cooper | 05de741 | 2010-12-08 00:40:35 -0800 | [diff] [blame] | 178 | } |
Garrett Cooper | 14d4960 | 2011-04-11 23:47:45 -0700 | [diff] [blame] | 179 | #endif |