Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Out Of Memory when changing cpuset's mems on NUMA. There was a |
| 3 | * problem reported upstream that the allocator may see an empty |
| 4 | * nodemask when changing cpuset's mems. |
| 5 | * http://lkml.org/lkml/2010/5/4/77 |
| 6 | * http://lkml.org/lkml/2010/5/4/79 |
| 7 | * http://lkml.org/lkml/2010/5/4/80 |
| 8 | * This test is based on the reproducers for the above issue. |
| 9 | * |
| 10 | * Copyright (C) 2010 Red Hat, Inc. |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of version 2 of the GNU General Public |
| 13 | * License as published by the Free Software Foundation. |
| 14 | * |
| 15 | * This program is distributed in the hope that it would be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 18 | * |
| 19 | * Further, this software is distributed without any warranty that it |
| 20 | * is free of the rightful claim of any third person regarding |
| 21 | * infringement or the like. Any license provided herein, whether |
| 22 | * implied or otherwise, applies only to this software file. Patent |
| 23 | * licenses, if any, provided herein do not apply to combinations of |
| 24 | * this program with other software, or any other product whatsoever. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write the Free Software |
| 28 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 29 | * 02110-1301, USA. |
| 30 | */ |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 31 | #include "config.h" |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/mman.h> |
| 34 | #include <sys/mount.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/wait.h> |
| 37 | #include <ctype.h> |
| 38 | #include <err.h> |
| 39 | #include <errno.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <math.h> |
| 42 | #if HAVE_NUMAIF_H |
| 43 | #include <numaif.h> |
| 44 | #endif |
| 45 | #include <signal.h> |
| 46 | #include <stdarg.h> |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <unistd.h> |
| 50 | |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 51 | #include "test.h" |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 52 | #include "mem.h" |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 53 | #include "numa_helper.h" |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 54 | |
| 55 | char *TCID = "cpuset01"; |
| 56 | int TST_TOTAL = 1; |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 57 | |
| 58 | #if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \ |
| 59 | && HAVE_MPOL_CONSTANTS |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 60 | volatile int end; |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 61 | static int *nodes; |
| 62 | static int nnodes; |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 63 | static long ncpus; |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 64 | |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 65 | static void testcpuset(void); |
| 66 | static void sighandler(int signo LTP_ATTRIBUTE_UNUSED); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 67 | static int mem_hog(void); |
| 68 | static int mem_hog_cpuset(int ntasks); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 69 | static long count_cpu(void); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 70 | |
| 71 | int main(int argc, char *argv[]) |
| 72 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 73 | const char *msg; |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 74 | |
| 75 | msg = parse_opts(argc, argv, NULL, NULL); |
| 76 | if (msg != NULL) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 77 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 78 | |
| 79 | ncpus = count_cpu(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | if (get_allowed_nodes_arr(NH_MEMS | NH_CPUS, &nnodes, &nodes) < 0) |
| 81 | tst_brkm(TBROK | TERRNO, NULL, "get_allowed_nodes_arr"); |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 82 | if (nnodes <= 1) |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 83 | tst_brkm(TCONF, NULL, "requires a NUMA system."); |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 84 | |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 85 | setup(); |
| 86 | testcpuset(); |
| 87 | cleanup(); |
Caspar Zhang | cd8fb15 | 2012-02-29 16:14:24 +0800 | [diff] [blame] | 88 | tst_exit(); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 91 | static void testcpuset(void) |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 92 | { |
| 93 | int lc; |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 94 | int child, i, status; |
Lans Zhang | cb2967b | 2013-04-17 16:35:27 +0800 | [diff] [blame] | 95 | unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 }; |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 96 | char mems[BUFSIZ], buf[BUFSIZ]; |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 97 | |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 98 | read_cpuset_files(CPATH, "cpus", buf); |
| 99 | write_cpuset_files(CPATH_NEW, "cpus", buf); |
| 100 | read_cpuset_files(CPATH, "mems", mems); |
| 101 | write_cpuset_files(CPATH_NEW, "mems", mems); |
Zhouping Liu | e096339 | 2013-04-23 15:12:55 +0800 | [diff] [blame] | 102 | SAFE_FILE_PRINTF(cleanup, CPATH_NEW "/tasks", "%d", getpid()); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 103 | |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 104 | switch (child = fork()) { |
Caspar Zhang | 05697c4 | 2012-02-20 19:20:57 +0800 | [diff] [blame] | 105 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Caspar Zhang | 05697c4 | 2012-02-20 19:20:57 +0800 | [diff] [blame] | 107 | case 0: |
Lans Zhang | cb2967b | 2013-04-17 16:35:27 +0800 | [diff] [blame] | 108 | for (i = 0; i < nnodes; i++) { |
| 109 | if (nodes[i] >= MAXNODES) |
| 110 | continue; |
| 111 | set_node(nmask, nodes[i]); |
| 112 | } |
| 113 | if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | tst_brkm(TBROK | TERRNO, cleanup, "set_mempolicy"); |
Caspar Zhang | cd8fb15 | 2012-02-29 16:14:24 +0800 | [diff] [blame] | 115 | exit(mem_hog_cpuset(ncpus > 1 ? ncpus : 1)); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 116 | } |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 117 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 118 | tst_count = 0; |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 119 | snprintf(buf, BUFSIZ, "%d", nodes[0]); |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 120 | write_cpuset_files(CPATH_NEW, "mems", buf); |
Caspar Zhang | a98ac19 | 2012-08-09 14:15:41 +0800 | [diff] [blame] | 121 | snprintf(buf, BUFSIZ, "%d", nodes[1]); |
Caspar Zhang | ff92e94 | 2012-02-29 10:58:15 +0800 | [diff] [blame] | 122 | write_cpuset_files(CPATH_NEW, "mems", buf); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 123 | } |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 124 | |
| 125 | if (waitpid(child, &status, WUNTRACED | WCONTINUED) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 126 | tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 127 | if (WEXITSTATUS(status) != 0) |
| 128 | tst_resm(TFAIL, "child exit status is %d", WEXITSTATUS(status)); |
| 129 | } |
| 130 | |
| 131 | void setup(void) |
| 132 | { |
Garrett Cooper | 40495b8 | 2011-01-19 00:48:39 -0800 | [diff] [blame] | 133 | tst_require_root(NULL); |
| 134 | |
Zhouping Liu | 263f695 | 2012-02-27 16:02:53 +0800 | [diff] [blame] | 135 | if (tst_kvercmp(2, 6, 32) < 0) |
| 136 | tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required"); |
| 137 | |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 138 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 139 | TEST_PAUSE; |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 140 | |
| 141 | mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 142 | } |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 143 | |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 144 | void cleanup(void) |
| 145 | { |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 146 | umount_mem(CPATH, CPATH_NEW); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 149 | static void sighandler(int signo LTP_ATTRIBUTE_UNUSED) |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 150 | { |
| 151 | end = 1; |
| 152 | } |
| 153 | |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 154 | static int mem_hog(void) |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 155 | { |
| 156 | long pagesize; |
| 157 | unsigned long *addr; |
| 158 | int ret = 0; |
| 159 | |
| 160 | pagesize = getpagesize(); |
| 161 | while (!end) { |
| 162 | addr = mmap(NULL, pagesize * 10, PROT_READ | PROT_WRITE, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 163 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 164 | if (addr == MAP_FAILED) { |
| 165 | ret = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 166 | tst_resm(TFAIL | TERRNO, "mmap"); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | memset(addr, 0xF7, pagesize * 10); |
| 170 | munmap(addr, pagesize * 10); |
| 171 | } |
| 172 | return ret; |
| 173 | } |
| 174 | |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 175 | static int mem_hog_cpuset(int ntasks) |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 176 | { |
Caspar Zhang | c406ee4 | 2012-02-29 17:41:57 +0800 | [diff] [blame] | 177 | int i, lc, status, ret = 0; |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 178 | struct sigaction sa; |
Caspar Zhang | c406ee4 | 2012-02-29 17:41:57 +0800 | [diff] [blame] | 179 | pid_t *pids; |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 180 | |
| 181 | if (ntasks <= 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | tst_brkm(TBROK | TERRNO, cleanup, "ntasks is small."); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 183 | sa.sa_handler = sighandler; |
| 184 | if (sigemptyset(&sa.sa_mask) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | tst_brkm(TBROK | TERRNO, cleanup, "sigemptyset"); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 186 | sa.sa_flags = 0; |
| 187 | if (sigaction(SIGUSR1, &sa, NULL) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 188 | tst_brkm(TBROK | TERRNO, cleanup, "sigaction"); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 189 | |
Caspar Zhang | c406ee4 | 2012-02-29 17:41:57 +0800 | [diff] [blame] | 190 | pids = malloc(sizeof(pid_t) * ntasks); |
| 191 | if (pids == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 192 | tst_brkm(TBROK | TERRNO, cleanup, "malloc"); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 193 | for (i = 0; i < ntasks; i++) { |
Caspar Zhang | c406ee4 | 2012-02-29 17:41:57 +0800 | [diff] [blame] | 194 | switch (pids[i] = fork()) { |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 195 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 196 | tst_resm(TFAIL | TERRNO, "fork %d", pids[i]); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 197 | ret = 1; |
| 198 | break; |
| 199 | case 0: |
| 200 | ret = mem_hog(); |
| 201 | exit(ret); |
| 202 | default: |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 203 | break; |
| 204 | } |
| 205 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 206 | for (lc = 0; TEST_LOOPING(lc); lc++) ; |
Caspar Zhang | c406ee4 | 2012-02-29 17:41:57 +0800 | [diff] [blame] | 207 | while (i--) { |
| 208 | if (kill(pids[i], SIGUSR1) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 209 | tst_resm(TFAIL | TERRNO, "kill %d", pids[i]); |
Caspar Zhang | c406ee4 | 2012-02-29 17:41:57 +0800 | [diff] [blame] | 210 | ret = 1; |
| 211 | } |
| 212 | } |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 213 | while (waitpid(-1, &status, WUNTRACED | WCONTINUED) > 0) { |
Caspar Zhang | cd8fb15 | 2012-02-29 16:14:24 +0800 | [diff] [blame] | 214 | if (WIFEXITED(status)) { |
Caspar Zhang | eaa7978 | 2012-02-29 17:12:53 +0800 | [diff] [blame] | 215 | if (WEXITSTATUS(status) != 0) { |
Caspar Zhang | cd8fb15 | 2012-02-29 16:14:24 +0800 | [diff] [blame] | 216 | tst_resm(TFAIL, "child exit status is %d", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 217 | WEXITSTATUS(status)); |
Caspar Zhang | eaa7978 | 2012-02-29 17:12:53 +0800 | [diff] [blame] | 218 | ret = 1; |
| 219 | } |
Caspar Zhang | cd8fb15 | 2012-02-29 16:14:24 +0800 | [diff] [blame] | 220 | } else if (WIFSIGNALED(status)) { |
| 221 | tst_resm(TFAIL, "child caught signal %d", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 222 | WTERMSIG(status)); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 223 | ret = 1; |
| 224 | } |
| 225 | } |
| 226 | return ret; |
| 227 | } |
| 228 | |
Zhouping Liu | 8e6aafc | 2012-02-27 16:02:51 +0800 | [diff] [blame] | 229 | static long count_cpu(void) |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 230 | { |
| 231 | int ncpus = 0; |
| 232 | |
Caspar Zhang | 05697c4 | 2012-02-20 19:20:57 +0800 | [diff] [blame] | 233 | while (path_exist(PATH_SYS_SYSTEM "/cpu/cpu%d", ncpus)) |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 234 | ncpus++; |
| 235 | |
| 236 | return ncpus; |
| 237 | } |
| 238 | |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 239 | #else /* no NUMA */ |
Caspar Zhang | 05697c4 | 2012-02-20 19:20:57 +0800 | [diff] [blame] | 240 | int main(void) |
| 241 | { |
| 242 | tst_brkm(TCONF, NULL, "no NUMA development packages installed."); |
Garrett Cooper | 717fb2c | 2010-12-08 08:44:59 -0800 | [diff] [blame] | 243 | } |
Garrett Cooper | 99854e0 | 2011-01-18 01:40:10 -0800 | [diff] [blame] | 244 | #endif |