blob: 4d62ad9c4a1c801f2ae7b468c4cd6abf72aa2024 [file] [log] [blame]
Jan Stancek6ce219c2012-10-26 15:31:17 +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 * use migrate_pages() and check that address is on correct node
27 * 1. process A can migrate its non-shared mem with CAP_SYS_NICE
28 * 2. process A can migrate its non-shared mem without CAP_SYS_NICE
29 * 3. process A can migrate shared mem only with CAP_SYS_NICE
30 * 4. process A can migrate non-shared mem in process B with same effective uid
31 * 5. process A can migrate non-shared mem in process B with CAP_SYS_NICE
32 */
33#include <sys/types.h>
34#include <sys/syscall.h>
35#include <sys/wait.h>
36#include <sys/mman.h>
37#include <errno.h>
38#if HAVE_NUMA_H
39#include <numa.h>
40#endif
41#if HAVE_NUMAIF_H
42#include <numaif.h>
43#endif
44#include <stdio.h>
45#include <stdlib.h>
46#include <unistd.h>
47#include <pwd.h>
48#include "config.h"
49#include "test.h"
Jan Stancek6ce219c2012-10-26 15:31:17 +020050#include "safe_macros.h"
51#include "linux_syscall_numbers.h"
52#include "numa_helper.h"
53#include "migrate_pages_common.h"
54
55/*
56 * This is an estimated minimum of free mem required to migrate this
57 * process to another node as migrate_pages will fail if there is not
58 * enough free space on node. While running this test on x86_64
59 * it used ~2048 pages (total VM, not just RSS). Considering ia64 as
60 * architecture with largest (non-huge) page size (16k), this limit
61 * is set to 2048*16k == 32M.
62 */
63#define NODE_MIN_FREEMEM (32*1024*1024)
64
65char *TCID = "migrate_pages02";
Wanlong Gao354ebb42012-12-07 10:10:04 +080066int TST_TOTAL = 1;
Jan Stancek6ce219c2012-10-26 15:31:17 +020067
68#if defined(__NR_migrate_pages) && HAVE_NUMA_H && HAVE_NUMAIF_H
69static const char nobody_uid[] = "nobody";
70static struct passwd *ltpuser;
71static int *nodes, nodeA, nodeB;
72static int num_nodes;
73
74static void setup(void);
75static void cleanup(void);
76
77option_t options[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080078 {NULL, NULL, NULL}
Jan Stancek6ce219c2012-10-26 15:31:17 +020079};
80
81static void print_mem_stats(pid_t pid, int node)
82{
83 char s[64];
84 long long node_size, freep;
85
86 if (pid == 0)
87 pid = getpid();
88
89 tst_resm(TINFO, "mem_stats pid: %d, node: %d", pid, node);
90
91 /* dump pid's VM info */
92 sprintf(s, "cat /proc/%d/status", pid);
93 system(s);
94 sprintf(s, "cat /proc/%d/numa_maps", pid);
95 system(s);
96
97 /* dump node free mem */
98 node_size = numa_node_size64(node, &freep);
99 tst_resm(TINFO, "Node id: %d, size: %lld, free: %lld",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100 node, node_size, freep);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200101}
102
103static int migrate_to_node(pid_t pid, int node)
104{
105 unsigned long nodemask_size, max_node;
106 unsigned long *old_nodes, *new_nodes;
107 int i;
108
109 tst_resm(TINFO, "pid(%d) migrate pid %d to node -> %d",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 getpid(), pid, node);
Stanislav Kholmanskikh89b24972013-08-27 16:49:51 +0400111 max_node = LTP_ALIGN(get_max_node(), sizeof(unsigned long)*8);
112 nodemask_size = max_node / 8;
Jan Stancek6ce219c2012-10-26 15:31:17 +0200113 old_nodes = SAFE_MALLOC(NULL, nodemask_size);
114 new_nodes = SAFE_MALLOC(NULL, nodemask_size);
115
116 memset(old_nodes, 0, nodemask_size);
117 memset(new_nodes, 0, nodemask_size);
118 for (i = 0; i < num_nodes; i++)
119 set_bit(old_nodes, nodes[i], 1);
120 set_bit(new_nodes, node, 1);
121
Jan Stancek359980f2013-02-15 10:16:05 +0100122 TEST(ltp_syscall(__NR_migrate_pages, pid, max_node, old_nodes,
123 new_nodes));
Jan Stancek6ce219c2012-10-26 15:31:17 +0200124 if (TEST_RETURN != 0) {
125 if (TEST_RETURN < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 tst_resm(TFAIL | TERRNO, "migrate_pages failed "
127 "ret: %ld, ", TEST_RETURN);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200128 else
129 tst_resm(TWARN, "migrate_pages could not migrate all "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 "pages, not migrated: %ld", TEST_RETURN);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200131 print_mem_stats(pid, node);
132 }
133 free(old_nodes);
134 free(new_nodes);
135 return TEST_RETURN;
136}
137
138static int addr_on_node(void *addr)
139{
140 int node;
141 int ret;
142
Jan Stancek359980f2013-02-15 10:16:05 +0100143 ret = ltp_syscall(__NR_get_mempolicy, &node, NULL, (unsigned long)0,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 (unsigned long)addr, MPOL_F_NODE | MPOL_F_ADDR);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200145 if (ret == -1) {
146 tst_resm(TBROK | TERRNO, "error getting memory policy "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147 "for page %p", addr);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200148 }
149 return node;
150}
151
152static int check_addr_on_node(void *addr, int exp_node)
153{
154 int node;
155
156 node = addr_on_node(addr);
157 if (node == exp_node) {
158 tst_resm(TPASS, "pid(%d) addr %p is on expected node: %d",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 getpid(), addr, exp_node);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200160 return 0;
161 } else {
162 tst_resm(TFAIL, "pid(%d) addr %p not on expected node: %d "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 ", expected %d", getpid(), addr, node, exp_node);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200164 print_mem_stats(0, exp_node);
165 return 1;
166 }
167}
168
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169static void test_migrate_current_process(int node1, int node2, int cap_sys_nice)
Jan Stancek6ce219c2012-10-26 15:31:17 +0200170{
171 char *testp, *testp2;
172 int ret, status;
173 pid_t child;
174
175 /* parent can migrate its non-shared memory */
176 tst_resm(TINFO, "current_process, cap_sys_nice: %d", cap_sys_nice);
177 testp = SAFE_MALLOC(NULL, getpagesize());
178 testp[0] = 0;
179 tst_resm(TINFO, "private anonymous: %p", testp);
180 migrate_to_node(0, node2);
181 check_addr_on_node(testp, node2);
182 migrate_to_node(0, node1);
183 check_addr_on_node(testp, node1);
184 free(testp);
185
186 /* parent can migrate shared memory with CAP_SYS_NICE */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 testp2 = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
188 MAP_ANONYMOUS | MAP_SHARED, 0, 0);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200189 if (testp2 == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200191 testp2[0] = 1;
192 tst_resm(TINFO, "shared anonymous: %p", testp2);
193 migrate_to_node(0, node2);
194 check_addr_on_node(testp2, node2);
195
196 /* shared mem is on node2, try to migrate in child to node1 */
197 fflush(stdout);
198 child = fork();
199 switch (child) {
200 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200202 break;
203 case 0:
204 tst_resm(TINFO, "child shared anonymous, cap_sys_nice: %d",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800205 cap_sys_nice);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200206 testp = SAFE_MALLOC(NULL, getpagesize());
207 testp[0] = 1;
208 testp2[0] = 1;
209 if (!cap_sys_nice)
210 if (seteuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800211 tst_brkm(TBROK | TERRNO, NULL,
212 "seteuid failed");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200213
214 migrate_to_node(0, node1);
215 /* child can migrate non-shared memory */
216 ret = check_addr_on_node(testp, node1);
217
218 free(testp);
219 munmap(testp2, getpagesize());
220 exit(ret);
221 default:
222 if (waitpid(child, &status, 0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800223 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200224 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
225 tst_resm(TFAIL, "child returns %d", status);
226 if (cap_sys_nice)
227 /* child can migrate shared memory only
228 * with CAP_SYS_NICE */
229 check_addr_on_node(testp2, node1);
230 else
231 check_addr_on_node(testp2, node2);
232 munmap(testp2, getpagesize());
233 }
234}
235
Wanlong Gao354ebb42012-12-07 10:10:04 +0800236static void test_migrate_other_process(int node1, int node2, int cap_sys_nice)
Jan Stancek6ce219c2012-10-26 15:31:17 +0200237{
238 char *testp;
239 int status, ret, tmp;
240 pid_t child;
241 int child_ready[2];
242 int pages_migrated[2];
243
244 /* setup pipes to synchronize child/parent */
245 if (pipe(child_ready) == -1)
246 tst_resm(TBROK | TERRNO, "pipe #1 failed");
247 if (pipe(pages_migrated) == -1)
248 tst_resm(TBROK | TERRNO, "pipe #2 failed");
249
250 tst_resm(TINFO, "other_process, cap_sys_nice: %d", cap_sys_nice);
251
252 fflush(stdout);
253 child = fork();
254 switch (child) {
255 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800256 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200257 break;
258 case 0:
259 close(child_ready[0]);
260 close(pages_migrated[1]);
261
262 testp = SAFE_MALLOC(NULL, getpagesize());
263 testp[0] = 0;
264
265 /* make sure we are on node1 */
266 migrate_to_node(0, node1);
267 check_addr_on_node(testp, node1);
268
269 if (seteuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800270 tst_brkm(TBROK | TERRNO, NULL, "seteuid failed");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200271
272 /* signal parent it's OK to migrate child and wait */
273 if (write(child_ready[1], &tmp, 1) != 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800274 tst_brkm(TBROK | TERRNO, NULL, "write #1 failed");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200275 if (read(pages_migrated[0], &tmp, 1) != 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800276 tst_brkm(TBROK | TERRNO, NULL, "read #1 failed");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200277
278 /* parent can migrate child process with same euid */
279 /* parent can migrate child process with CAP_SYS_NICE */
280 ret = check_addr_on_node(testp, node2);
281
282 free(testp);
283 close(child_ready[1]);
284 close(pages_migrated[0]);
285 exit(ret);
286 default:
287 close(child_ready[1]);
288 close(pages_migrated[0]);
289
290 if (!cap_sys_nice)
291 if (seteuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800292 tst_brkm(TBROK | TERRNO, NULL,
293 "seteuid failed");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200294
295 /* wait until child is ready on node1, then migrate and
296 * signal to check current node */
297 if (read(child_ready[0], &tmp, 1) != 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800298 tst_brkm(TBROK | TERRNO, NULL, "read #2 failed");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200299 migrate_to_node(child, node2);
300 if (write(pages_migrated[1], &tmp, 1) != 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800301 tst_brkm(TBROK | TERRNO, NULL, "write #2 failed");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200302
303 if (waitpid(child, &status, 0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800304 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200305 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
306 tst_resm(TFAIL, "child returns %d", status);
307 close(child_ready[0]);
308 close(pages_migrated[1]);
309
310 /* reset euid, so this testcase can be used in loop */
311 if (!cap_sys_nice)
312 if (seteuid(0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800313 tst_brkm(TBROK | TERRNO, NULL,
314 "seteuid failed");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200315 }
316}
317
318int main(int argc, char *argv[])
319{
320 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200321 const char *msg;
Jan Stancek6ce219c2012-10-26 15:31:17 +0200322
323 msg = parse_opts(argc, argv, options, NULL);
324 if (msg != NULL)
325 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
326
327 setup();
328 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800329 tst_count = 0;
Jan Stancek6ce219c2012-10-26 15:31:17 +0200330 test_migrate_current_process(nodeA, nodeB, 1);
331 test_migrate_current_process(nodeA, nodeB, 0);
332 test_migrate_other_process(nodeA, nodeB, 1);
333 test_migrate_other_process(nodeA, nodeB, 0);
334 }
335 cleanup();
336 tst_exit();
337}
338
339static void setup(void)
340{
341 int ret, i, j;
342 int pagesize = getpagesize();
343 void *p;
344
345 tst_require_root(NULL);
Jan Stancek359980f2013-02-15 10:16:05 +0100346 TEST(ltp_syscall(__NR_migrate_pages, 0, 0, NULL, NULL));
Jan Stancek6ce219c2012-10-26 15:31:17 +0200347
348 if (numa_available() == -1)
349 tst_brkm(TCONF, NULL, "NUMA not available");
350
351 ret = get_allowed_nodes_arr(NH_MEMS, &num_nodes, &nodes);
352 if (ret < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800353 tst_brkm(TBROK | TERRNO, NULL, "get_allowed_nodes(): %d", ret);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200354
355 if (num_nodes < 2)
356 tst_brkm(TCONF, NULL, "at least 2 allowed NUMA nodes"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800357 " are required");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200358 else if (tst_kvercmp(2, 6, 18) < 0)
359 tst_brkm(TCONF, NULL, "2.6.18 or greater kernel required");
360
361 /*
362 * find 2 nodes, which can hold NODE_MIN_FREEMEM bytes
363 * The reason is that:
364 * 1. migrate_pages() is expected to succeed
365 * 2. this test avoids hitting:
366 * Bug 870326 - migrate_pages() reports success, but pages are
367 * not moved to desired node
368 * https://bugzilla.redhat.com/show_bug.cgi?id=870326
369 */
370 nodeA = nodeB = -1;
371 for (i = 0; i < num_nodes; i++) {
372 p = numa_alloc_onnode(NODE_MIN_FREEMEM, nodes[i]);
373 if (p == NULL)
374 break;
375 memset(p, 0xff, NODE_MIN_FREEMEM);
376
377 j = 0;
378 while (j < NODE_MIN_FREEMEM) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800379 if (addr_on_node(p + j) != nodes[i])
Jan Stancek6ce219c2012-10-26 15:31:17 +0200380 break;
381 j += pagesize;
382 }
383 numa_free(p, NODE_MIN_FREEMEM);
384
385 if (j >= NODE_MIN_FREEMEM) {
386 if (nodeA == -1)
387 nodeA = nodes[i];
388 else if (nodeB == -1)
389 nodeB = nodes[i];
390 else
391 break;
392 }
393 }
394
395 if (nodeA == -1 || nodeB == -1)
396 tst_brkm(TCONF, NULL, "at least 2 NUMA nodes with "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800397 "free mem > %d are needed", NODE_MIN_FREEMEM);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200398 tst_resm(TINFO, "Using nodes: %d %d", nodeA, nodeB);
399
400 ltpuser = getpwnam(nobody_uid);
401 if (ltpuser == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800402 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200403
404 TEST_PAUSE;
405}
406
407static void cleanup(void)
408{
409 free(nodes);
Jan Stancek6ce219c2012-10-26 15:31:17 +0200410}
411
412#else /* __NR_migrate_pages */
413int main(void)
414{
415 tst_brkm(TCONF, NULL, "System doesn't support __NR_migrate_pages"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800416 " or libnuma is not available");
Jan Stancek6ce219c2012-10-26 15:31:17 +0200417}
418#endif