1 /* 2 * Copyright (C) 2012 Linux Test Project, Inc. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of version 2 of the GNU General Public 6 * License as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it 13 * is free of the rightful claim of any third person regarding 14 * infringement or the like. Any license provided herein, whether 15 * implied or otherwise, applies only to this software file. Patent 16 * licenses, if any, provided herein do not apply to combinations of 17 * this program with other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 22 * 02110-1301, USA. 23 */ 24 25 /* 26 * errno tests for migrate_pages() syscall 27 */ 28 #include <sys/types.h> 29 #include <sys/syscall.h> 30 #include <sys/wait.h> 31 #include <sys/mman.h> 32 #include <errno.h> 33 #if HAVE_NUMA_H 34 #include <numa.h> 35 #endif 36 #if HAVE_NUMAIF_H 37 #include <numaif.h> 38 #endif 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 #include <pwd.h> 44 #include "config.h" 45 #include "test.h" 46 #include "safe_macros.h" 47 #include "linux_syscall_numbers.h" 48 #include "numa_helper.h" 49 #include "migrate_pages_common.h" 50 51 char *TCID = "migrate_pages01"; 52 int TST_TOTAL = 1; 53 54 option_t options[] = { 55 {NULL, NULL, NULL} 56 }; 57 58 #if defined(__NR_migrate_pages) && HAVE_NUMA_H && HAVE_NUMAIF_H 59 static unsigned long *sane_old_nodes; 60 static unsigned long *sane_new_nodes; 61 static int sane_nodemask_size; 62 static int sane_max_node; 63 64 static void setup(void); 65 static void cleanup(void); 66 67 static void test_sane_nodes(void) 68 { 69 tst_resm(TINFO, "test_empty_mask"); 70 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, 71 sane_old_nodes, sane_new_nodes)); 72 check_ret(0); 73 } 74 75 static void test_invalid_pid(void) 76 { 77 pid_t invalid_pid = -1; 78 79 tst_resm(TINFO, "test_invalid_pid -1"); 80 TEST(ltp_syscall(__NR_migrate_pages, invalid_pid, sane_max_node, 81 sane_old_nodes, sane_new_nodes)); 82 check_ret(-1); 83 check_errno(ESRCH); 84 85 tst_resm(TINFO, "test_invalid_pid unused pid"); 86 invalid_pid = tst_get_unused_pid(cleanup); 87 TEST(ltp_syscall(__NR_migrate_pages, invalid_pid, sane_max_node, 88 sane_old_nodes, sane_new_nodes)); 89 check_ret(-1); 90 check_errno(ESRCH); 91 } 92 93 static void test_invalid_masksize(void) 94 { 95 tst_resm(TINFO, "test_invalid_masksize"); 96 TEST(ltp_syscall(__NR_migrate_pages, 0, -1, sane_old_nodes, 97 sane_new_nodes)); 98 check_ret(-1); 99 check_errno(EINVAL); 100 } 101 102 static void test_invalid_mem(void) 103 { 104 unsigned long *p; 105 106 tst_resm(TINFO, "test_invalid_mem -1"); 107 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, -1, -1)); 108 check_ret(-1); 109 check_errno(EFAULT); 110 111 tst_resm(TINFO, "test_invalid_mem invalid prot"); 112 p = mmap(NULL, getpagesize(), PROT_NONE, 113 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); 114 if (p == MAP_FAILED) 115 tst_brkm(TBROK | TERRNO, cleanup, "mmap"); 116 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, p, p)); 117 check_ret(-1); 118 check_errno(EFAULT); 119 120 if (munmap(p, getpagesize()) < 0) 121 tst_brkm(TBROK | TERRNO, cleanup, "munmap"); 122 tst_resm(TINFO, "test_invalid_mem unmmaped"); 123 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, p, p)); 124 check_ret(-1); 125 check_errno(EFAULT); 126 } 127 128 static void test_invalid_nodes(void) 129 { 130 int *nodes; 131 int num_nodes, ret, i; 132 int invalid_node = 0; 133 unsigned long *old_nodes, *new_nodes; 134 135 tst_resm(TINFO, "test_invalid_nodes"); 136 ret = get_allowed_nodes_arr(NH_MEMS, &num_nodes, &nodes); 137 if (ret < 0) 138 tst_brkm(TBROK | TERRNO, cleanup, 139 "get_allowed_nodes_arr: %d", ret); 140 141 /* get first node which is not in nodes */ 142 for (i = 0; i < num_nodes; i++, invalid_node++) 143 if (invalid_node != nodes[i]) 144 break; 145 if (invalid_node < sane_max_node) { 146 old_nodes = SAFE_MALLOC(NULL, sane_nodemask_size); 147 new_nodes = SAFE_MALLOC(NULL, sane_nodemask_size); 148 memcpy(old_nodes, sane_old_nodes, sane_nodemask_size); 149 memset(new_nodes, 0, sane_nodemask_size); 150 set_bit(new_nodes, invalid_node, 1); 151 152 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, 153 old_nodes, new_nodes)); 154 check_ret(-1); 155 check_errno(EINVAL); 156 free(old_nodes); 157 free(new_nodes); 158 } else { 159 tst_resm(TCONF, "All possible nodes are present"); 160 } 161 162 free(nodes); 163 } 164 165 static void test_invalid_perm(void) 166 { 167 char nobody_uid[] = "nobody"; 168 struct passwd *ltpuser; 169 int status; 170 pid_t child_pid; 171 pid_t parent_pid; 172 int ret = 0; 173 174 tst_resm(TINFO, "test_invalid_perm"); 175 parent_pid = getpid(); 176 fflush(stdout); 177 child_pid = fork(); 178 switch (child_pid) { 179 case -1: 180 tst_brkm(TBROK | TERRNO, cleanup, "fork"); 181 break; 182 case 0: 183 ltpuser = getpwnam(nobody_uid); 184 if (ltpuser == NULL) 185 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed"); 186 if (setuid(ltpuser->pw_uid) == -1) 187 tst_brkm(TBROK | TERRNO, NULL, 188 "setuid(%u) failed", ltpuser->pw_uid); 189 TEST(ltp_syscall(__NR_migrate_pages, parent_pid, 190 sane_max_node, sane_old_nodes, sane_new_nodes)); 191 ret |= check_ret(-1); 192 ret |= check_errno(EPERM); 193 exit(ret); 194 default: 195 if (waitpid(child_pid, &status, 0) == -1) 196 tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); 197 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) 198 tst_resm(TFAIL, "child returns %d", status); 199 } 200 } 201 202 int main(int argc, char *argv[]) 203 { 204 int lc; 205 206 tst_parse_opts(argc, argv, options, NULL); 207 208 setup(); 209 for (lc = 0; TEST_LOOPING(lc); lc++) { 210 tst_count = 0; 211 test_sane_nodes(); 212 test_invalid_pid(); 213 test_invalid_masksize(); 214 test_invalid_mem(); 215 test_invalid_nodes(); 216 test_invalid_perm(); 217 } 218 cleanup(); 219 tst_exit(); 220 } 221 222 static void setup(void) 223 { 224 int node, ret; 225 226 tst_require_root(); 227 TEST(ltp_syscall(__NR_migrate_pages, 0, 0, NULL, NULL)); 228 229 if (!is_numa(NULL, NH_MEMS, 1)) 230 tst_brkm(TCONF, NULL, "requires NUMA with at least 1 node"); 231 232 ret = get_allowed_nodes(NH_MEMS, 1, &node); 233 if (ret < 0) 234 tst_brkm(TBROK | TERRNO, NULL, "get_allowed_nodes_arr: %d", 235 ret); 236 237 sane_max_node = LTP_ALIGN(get_max_node(), sizeof(unsigned long)*8); 238 sane_nodemask_size = sane_max_node / 8; 239 sane_old_nodes = SAFE_MALLOC(NULL, sane_nodemask_size); 240 sane_new_nodes = SAFE_MALLOC(NULL, sane_nodemask_size); 241 memset(sane_old_nodes, 0, sane_nodemask_size); 242 memset(sane_new_nodes, 0, sane_nodemask_size); 243 244 set_bit(sane_old_nodes, node, 1); 245 set_bit(sane_new_nodes, node, 1); 246 247 TEST_PAUSE; 248 } 249 250 static void cleanup(void) 251 { 252 free(sane_old_nodes); 253 free(sane_new_nodes); 254 } 255 256 #else /* __NR_migrate_pages */ 257 int main(void) 258 { 259 tst_brkm(TCONF, NULL, "System doesn't support __NR_migrate_pages" 260 " or libnuma is not available"); 261 } 262 #endif 263