blob: 4d87c51a95f0b0d6eaca46bc9a3e5fa33f9151a7 [file] [log] [blame]
subrata_modak8dfa1b32008-07-26 04:15:36 +00001/*
2 * Copyright (c) 2008 Vijay Kumar B. <vijaykumar@bravegnu.org>
3 *
4 * Based on testcases/kernel/syscalls/waitpid/waitpid01.c
5 * Original copyright message:
6 *
7 * Copyright (c) International Business Machines Corp., 2001
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modak8dfa1b32008-07-26 04:15:36 +000022 */
23
24/*
25 * NAME
26 * move_pages03.c
27 *
28 * DESCRIPTION
29 * Test movement of pages mapped by a process.
30 *
31 * ALGORITHM
32 * 1. Start the test case program as root.
33 * 2. Allocate a shared memory in NUMA node A.
34 * 3. Fork another process.
35 * 4. Use move_pages() to move the pages to NUMA node B, with the
36 * MPOL_MF_MOVE_ALL.
37 * 5. Check if all pages are in node B.
38 *
39 * USAGE: <for command-line>
40 * move_pages03 [-c n] [-i n] [-I x] [-P x] [-t]
41 * where, -c n : Run n copies concurrently.
42 * -i n : Execute test n times.
43 * -I x : Execute test for x seconds.
44 * -P x : Pause for x seconds between iterations.
45 * -t : Turn on syscall timing.
46 *
47 * History
48 * 05/2008 Vijay Kumar
49 * Initial Version.
50 *
51 * Restrictions
52 * None
53 */
54
55#include <sys/types.h>
56#include <sys/wait.h>
57#include <unistd.h>
58#include <signal.h>
59#include <semaphore.h>
60#include <errno.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080061#include "test.h"
subrata_modak8dfa1b32008-07-26 04:15:36 +000062#include "move_pages_support.h"
63
64#define TEST_PAGES 2
65#define TEST_NODES 2
66
67enum {
68 SEM_CHILD_SETUP,
69 SEM_PARENT_TEST,
70
71 MAX_SEMS
72};
73
74void setup(void);
75void cleanup(void);
76
77char *TCID = "move_pages03";
78int TST_TOTAL = 1;
subrata_modak8dfa1b32008-07-26 04:15:36 +000079
80/*
81 * child() - touches shared pages, and waits for signal from parent.
82 * @pages: shared pages allocated in parent
83 * @sem: semaphore to sync with parent
84 */
subrata_modak56207ce2009-03-23 13:35:39 +000085void child(void **pages, sem_t * sem)
subrata_modak8dfa1b32008-07-26 04:15:36 +000086{
87 int i;
88
89 for (i = 0; i < TEST_PAGES; i++) {
90 char *page;
91
92 page = pages[i];
93 page[0] = 0xAA;
94 }
95
96 /* Setup complete. Ask parent to continue. */
97 if (sem_post(&sem[SEM_CHILD_SETUP]) == -1)
yaberauneya4af190f2009-11-09 11:06:31 +000098 tst_resm(TWARN | TERRNO, "error post semaphore");
subrata_modak8dfa1b32008-07-26 04:15:36 +000099
100 /* Wait for testcase in parent to complete. */
101 if (sem_wait(&sem[SEM_PARENT_TEST]) == -1)
yaberauneya4af190f2009-11-09 11:06:31 +0000102 tst_resm(TWARN | TERRNO, "error wait semaphore");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000103
104 exit(0);
105}
106
107int main(int argc, char **argv)
108{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200109 const char *msg;
subrata_modak8dfa1b32008-07-26 04:15:36 +0000110
Garrett Cooper45e285d2010-11-22 12:19:25 -0800111 msg = parse_opts(argc, argv, NULL, NULL);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000112 if (msg != NULL) {
113 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800114
subrata_modak8dfa1b32008-07-26 04:15:36 +0000115 }
116
117 setup();
118
yaberauneyaef772532009-10-09 17:55:43 +0000119#if HAVE_NUMA_MOVE_PAGES
120 unsigned int i;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200121 int lc;
Jan Stancek134bea02012-06-28 11:03:18 +0200122 unsigned int from_node;
123 unsigned int to_node;
124 int ret;
125
Jan Stancekd534a442012-08-09 14:15:38 +0800126 ret = get_allowed_nodes(NH_MEMS, 2, &from_node, &to_node);
127 if (ret < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800128 tst_brkm(TBROK | TERRNO, cleanup, "get_allowed_nodes: %d", ret);
yaberauneyaef772532009-10-09 17:55:43 +0000129
subrata_modak8dfa1b32008-07-26 04:15:36 +0000130 /* check for looping state if -i option is given */
131 for (lc = 0; TEST_LOOPING(lc); lc++) {
132 void *pages[TEST_PAGES] = { 0 };
133 int nodes[TEST_PAGES];
134 int status[TEST_PAGES];
subrata_modak8dfa1b32008-07-26 04:15:36 +0000135 pid_t cpid;
136 sem_t *sem;
137
Caspar Zhangd59a6592013-03-07 14:59:12 +0800138 /* reset tst_count in case we are looping */
139 tst_count = 0;
subrata_modak8dfa1b32008-07-26 04:15:36 +0000140
subrata_modak56207ce2009-03-23 13:35:39 +0000141 ret = alloc_shared_pages_on_node(pages, TEST_PAGES, from_node);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000142 if (ret == -1)
143 continue;
144
subrata_modak8dfa1b32008-07-26 04:15:36 +0000145 for (i = 0; i < TEST_PAGES; i++) {
146 nodes[i] = to_node;
147 }
148
149 sem = alloc_sem(MAX_SEMS);
150 if (sem == NULL) {
151 goto err_free_pages;
152 }
153
154 /*
155 * Fork a child process so that the shared pages are
156 * now really shared between two processes.
157 */
158 cpid = fork();
159 if (cpid == -1) {
yaberauneya4af190f2009-11-09 11:06:31 +0000160 tst_resm(TBROK | TERRNO, "forking child failed");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000161 goto err_free_sem;
162 } else if (cpid == 0) {
163 child(pages, sem);
164 }
165
166 /* Wait for child to setup and signal. */
167 if (sem_wait(&sem[SEM_CHILD_SETUP]) == -1)
yaberauneya4af190f2009-11-09 11:06:31 +0000168 tst_resm(TWARN | TERRNO, "error wait semaphore");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000169
170 ret = numa_move_pages(0, TEST_PAGES, pages, nodes,
171 status, MPOL_MF_MOVE_ALL);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000172 if (ret != 0) {
Jan Stancek855da1d2014-02-25 16:09:56 +0100173 tst_resm(TFAIL|TERRNO, "move_pages failed");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000174 goto err_kill_child;
175 }
176
177 verify_pages_on_node(pages, status, TEST_PAGES, to_node);
178
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179err_kill_child:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000180 /* Test done. Ask child to terminate. */
181 if (sem_post(&sem[SEM_PARENT_TEST]) == -1)
yaberauneya4af190f2009-11-09 11:06:31 +0000182 tst_resm(TWARN | TERRNO, "error post semaphore");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000183 /* Read the status, no zombies! */
184 wait(NULL);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185err_free_sem:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000186 free_sem(sem, MAX_SEMS);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187err_free_pages:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000188 free_shared_pages(pages, TEST_PAGES);
189 }
yaberauneyaef772532009-10-09 17:55:43 +0000190#else
191 tst_resm(TCONF, "move_pages support not found.");
192#endif
subrata_modak8dfa1b32008-07-26 04:15:36 +0000193
194 cleanup();
subrata_modak8dfa1b32008-07-26 04:15:36 +0000195
Garrett Cooper2c282152010-12-16 00:55:50 -0800196 tst_exit();
subrata_modak8dfa1b32008-07-26 04:15:36 +0000197}
198
199/*
200 * setup() - performs all ONE TIME setup for this test
201 */
subrata_modak56207ce2009-03-23 13:35:39 +0000202void setup(void)
subrata_modak8dfa1b32008-07-26 04:15:36 +0000203{
Garrett Cooper2c282152010-12-16 00:55:50 -0800204
subrata_modak8dfa1b32008-07-26 04:15:36 +0000205 tst_sig(FORK, DEF_HANDLER, cleanup);
206
207 check_config(TEST_NODES);
208
209 /* Pause if that option was specified
210 * TEST_PAUSE contains the code to fork the test with the -c option.
211 */
212 TEST_PAUSE;
213}
214
215/*
216 * cleanup() - performs all ONE TIME cleanup for this test at completion
217 */
subrata_modak56207ce2009-03-23 13:35:39 +0000218void cleanup(void)
subrata_modak8dfa1b32008-07-26 04:15:36 +0000219{
subrata_modak8dfa1b32008-07-26 04:15:36 +0000220
Wanlong Gao354ebb42012-12-07 10:10:04 +0800221}