blob: 97d64804842507c9ad9cd5a4fb2b60ed88ec5ff2 [file] [log] [blame]
Jan Stancek5765e2c2012-10-26 15:31:16 +02001/*
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"
Jan Stancek5765e2c2012-10-26 15:31:16 +020046#include "safe_macros.h"
47#include "linux_syscall_numbers.h"
48#include "numa_helper.h"
49#include "migrate_pages_common.h"
50
51char *TCID = "migrate_pages01";
Wanlong Gao354ebb42012-12-07 10:10:04 +080052int TST_TOTAL = 1;
Jan Stancek5765e2c2012-10-26 15:31:16 +020053
54option_t options[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080055 {NULL, NULL, NULL}
Jan Stancek5765e2c2012-10-26 15:31:16 +020056};
57
Jan Stancek6ce219c2012-10-26 15:31:17 +020058#if defined(__NR_migrate_pages) && HAVE_NUMA_H && HAVE_NUMAIF_H
Jan Stancek5765e2c2012-10-26 15:31:16 +020059static unsigned long *sane_old_nodes;
60static unsigned long *sane_new_nodes;
61static int sane_nodemask_size;
62static int sane_max_node;
63
64static void setup(void);
65static void cleanup(void);
66
67static void test_sane_nodes(void)
68{
69 tst_resm(TINFO, "test_empty_mask");
Jan Stancek359980f2013-02-15 10:16:05 +010070 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node,
Wanlong Gao354ebb42012-12-07 10:10:04 +080071 sane_old_nodes, sane_new_nodes));
Jan Stancek5765e2c2012-10-26 15:31:16 +020072 check_ret(0);
73}
74
75static void test_invalid_pid(void)
76{
Jan Stancek5765e2c2012-10-26 15:31:16 +020077 pid_t invalid_pid = -1;
78
79 tst_resm(TINFO, "test_invalid_pid -1");
Jan Stancek359980f2013-02-15 10:16:05 +010080 TEST(ltp_syscall(__NR_migrate_pages, invalid_pid, sane_max_node,
Wanlong Gao354ebb42012-12-07 10:10:04 +080081 sane_old_nodes, sane_new_nodes));
Jan Stancek5765e2c2012-10-26 15:31:16 +020082 check_ret(-1);
83 check_errno(ESRCH);
84
Stanislav Kholmanskikh23b37f32014-06-30 14:48:24 +040085 tst_resm(TINFO, "test_invalid_pid unused pid");
86 invalid_pid = tst_get_unused_pid(cleanup);
Jan Stancek359980f2013-02-15 10:16:05 +010087 TEST(ltp_syscall(__NR_migrate_pages, invalid_pid, sane_max_node,
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 sane_old_nodes, sane_new_nodes));
Jan Stancek5765e2c2012-10-26 15:31:16 +020089 check_ret(-1);
90 check_errno(ESRCH);
91}
92
93static void test_invalid_masksize(void)
94{
95 tst_resm(TINFO, "test_invalid_masksize");
Jan Stancek359980f2013-02-15 10:16:05 +010096 TEST(ltp_syscall(__NR_migrate_pages, 0, -1, sane_old_nodes,
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 sane_new_nodes));
Jan Stancek5765e2c2012-10-26 15:31:16 +020098 check_ret(-1);
99 check_errno(EINVAL);
100}
101
102static void test_invalid_mem(void)
103{
104 unsigned long *p;
105
106 tst_resm(TINFO, "test_invalid_mem -1");
Jan Stancek359980f2013-02-15 10:16:05 +0100107 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, -1, -1));
Jan Stancek5765e2c2012-10-26 15:31:16 +0200108 check_ret(-1);
109 check_errno(EFAULT);
110
111 tst_resm(TINFO, "test_invalid_mem invalid prot");
112 p = mmap(NULL, getpagesize(), PROT_NONE,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
Jan Stancek5765e2c2012-10-26 15:31:16 +0200114 if (p == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800115 tst_brkm(TBROK | TERRNO, cleanup, "mmap");
Jan Stancek359980f2013-02-15 10:16:05 +0100116 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, p, p));
Jan Stancek5765e2c2012-10-26 15:31:16 +0200117 check_ret(-1);
118 check_errno(EFAULT);
119
120 if (munmap(p, getpagesize()) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 tst_brkm(TBROK | TERRNO, cleanup, "munmap");
Jan Stancek5765e2c2012-10-26 15:31:16 +0200122 tst_resm(TINFO, "test_invalid_mem unmmaped");
Jan Stancek359980f2013-02-15 10:16:05 +0100123 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, p, p));
Jan Stancek5765e2c2012-10-26 15:31:16 +0200124 check_ret(-1);
125 check_errno(EFAULT);
126}
127
128static 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)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800138 tst_brkm(TBROK | TERRNO, cleanup,
139 "get_allowed_nodes_arr: %d", ret);
Jan Stancek5765e2c2012-10-26 15:31:16 +0200140
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
Jan Stancek359980f2013-02-15 10:16:05 +0100152 TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 old_nodes, new_nodes));
Jan Stancek5765e2c2012-10-26 15:31:16 +0200154 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
165static 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:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Jan Stancek5765e2c2012-10-26 15:31:16 +0200181 break;
182 case 0:
183 ltpuser = getpwnam(nobody_uid);
184 if (ltpuser == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
Jan Stancek5765e2c2012-10-26 15:31:16 +0200186 if (setuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 tst_brkm(TBROK | TERRNO, NULL,
188 "setuid(%u) failed", ltpuser->pw_uid);
Jan Stancek359980f2013-02-15 10:16:05 +0100189 TEST(ltp_syscall(__NR_migrate_pages, parent_pid,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 sane_max_node, sane_old_nodes, sane_new_nodes));
Jan Stancek5765e2c2012-10-26 15:31:16 +0200191 ret |= check_ret(-1);
192 ret |= check_errno(EPERM);
193 exit(ret);
194 default:
195 if (waitpid(child_pid, &status, 0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800196 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Jan Stancek5765e2c2012-10-26 15:31:16 +0200197 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
198 tst_resm(TFAIL, "child returns %d", status);
199 }
200}
201
202int main(int argc, char *argv[])
203{
204 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200205 const char *msg;
Jan Stancek5765e2c2012-10-26 15:31:16 +0200206
207 msg = parse_opts(argc, argv, options, NULL);
208 if (msg != NULL)
209 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
210
211 setup();
212 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800213 tst_count = 0;
Jan Stancek5765e2c2012-10-26 15:31:16 +0200214 test_sane_nodes();
215 test_invalid_pid();
216 test_invalid_masksize();
217 test_invalid_mem();
218 test_invalid_nodes();
219 test_invalid_perm();
220 }
221 cleanup();
222 tst_exit();
223}
224
225static void setup(void)
226{
227 int node, ret;
228
229 tst_require_root(NULL);
Jan Stancek359980f2013-02-15 10:16:05 +0100230 TEST(ltp_syscall(__NR_migrate_pages, 0, 0, NULL, NULL));
Jan Stancek5765e2c2012-10-26 15:31:16 +0200231
232 if (numa_available() == -1)
233 tst_brkm(TCONF, NULL, "NUMA not available");
234
235 ret = get_allowed_nodes(NH_MEMS, 1, &node);
236 if (ret < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800237 tst_brkm(TBROK | TERRNO, NULL, "get_allowed_nodes_arr: %d",
238 ret);
Jan Stancek5765e2c2012-10-26 15:31:16 +0200239
Stanislav Kholmanskikh89b24972013-08-27 16:49:51 +0400240 sane_max_node = LTP_ALIGN(get_max_node(), sizeof(unsigned long)*8);
241 sane_nodemask_size = sane_max_node / 8;
Jan Stancek5765e2c2012-10-26 15:31:16 +0200242 sane_old_nodes = SAFE_MALLOC(NULL, sane_nodemask_size);
243 sane_new_nodes = SAFE_MALLOC(NULL, sane_nodemask_size);
244 memset(sane_old_nodes, 0, sane_nodemask_size);
245 memset(sane_new_nodes, 0, sane_nodemask_size);
246
247 set_bit(sane_old_nodes, node, 1);
248 set_bit(sane_new_nodes, node, 1);
249
250 TEST_PAUSE;
251}
252
253static void cleanup(void)
254{
255 free(sane_old_nodes);
256 free(sane_new_nodes);
Jan Stancek5765e2c2012-10-26 15:31:16 +0200257}
258
259#else /* __NR_migrate_pages */
260int main(void)
261{
262 tst_brkm(TCONF, NULL, "System doesn't support __NR_migrate_pages"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800263 " or libnuma is not available");
Jan Stancek5765e2c2012-10-26 15:31:16 +0200264}
265#endif