blob: 29514645536fde241e02cece3ebb8b45ee2c0c9e [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_pages05.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, without
36 * the MPOL_MF_MOVE_ALL.
37 * 5. Check if the corresponding status is set to -EACCES.
38 *
39 * USAGE: <for command-line>
40 * move_pages05 [-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 <errno.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080059#include "test.h"
subrata_modak8dfa1b32008-07-26 04:15:36 +000060#include "move_pages_support.h"
61
62#define SHARED_PAGE 0
63#define N_SHARED_PAGES 1
64#define UNSHARED_PAGE 1
65#define N_UNSHARED_PAGES 1
66#define N_TEST_PAGES (N_SHARED_PAGES + N_UNSHARED_PAGES)
67#define N_TEST_NODES 2
68
69enum {
70 SEM_CHILD_SETUP,
71 SEM_PARENT_TEST,
72
73 MAX_SEMS
74};
75
76void setup(void);
77void cleanup(void);
78
79char *TCID = "move_pages05";
80int TST_TOTAL = 1;
subrata_modak8dfa1b32008-07-26 04:15:36 +000081
82/*
83 * child() - touches pages, and waits for signal from parent.
84 * @pages: shared pages allocated in parent
85 */
subrata_modak56207ce2009-03-23 13:35:39 +000086void child(void **pages, sem_t * sem)
subrata_modak8dfa1b32008-07-26 04:15:36 +000087{
88 int i;
89
90 for (i = 0; i < N_TEST_PAGES; i++) {
91 char *page;
92
93 page = pages[i];
94 page[0] = 0xAA;
95 }
96
97 /* Setup complete. Ask parent to continue. */
98 if (sem_post(&sem[SEM_CHILD_SETUP]) == -1)
yaberauneya4af190f2009-11-09 11:06:31 +000099 tst_resm(TWARN | TERRNO, "error post semaphore");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000100
101 /* Wait for testcase in parent to complete. */
102 if (sem_wait(&sem[SEM_PARENT_TEST]) == -1)
yaberauneya4af190f2009-11-09 11:06:31 +0000103 tst_resm(TWARN | TERRNO, "error waiting for semaphore");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000104
105 exit(0);
106}
107
108int main(int argc, char **argv)
109{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200110 const char *msg;
subrata_modak8dfa1b32008-07-26 04:15:36 +0000111
Garrett Cooper45e285d2010-11-22 12:19:25 -0800112 msg = parse_opts(argc, argv, NULL, NULL);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000113 if (msg != NULL) {
114 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800115
subrata_modak8dfa1b32008-07-26 04:15:36 +0000116 }
117
118 setup();
119
yaberauneyaef772532009-10-09 17:55:43 +0000120#if HAVE_NUMA_MOVE_PAGES
121 unsigned int i;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200122 int lc;
Jan Stancek134bea02012-06-28 11:03:18 +0200123 unsigned int from_node;
124 unsigned int to_node;
125 int ret;
126
Jan Stancekd534a442012-08-09 14:15:38 +0800127 ret = get_allowed_nodes(NH_MEMS, 2, &from_node, &to_node);
128 if (ret < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 tst_brkm(TBROK | TERRNO, cleanup, "get_allowed_nodes: %d", ret);
yaberauneyaef772532009-10-09 17:55:43 +0000130
subrata_modak8dfa1b32008-07-26 04:15:36 +0000131 /* check for looping state if -i option is given */
132 for (lc = 0; TEST_LOOPING(lc); lc++) {
133 void *pages[N_TEST_PAGES] = { 0 };
134 int nodes[N_TEST_PAGES];
135 int status[N_TEST_PAGES];
subrata_modak8dfa1b32008-07-26 04:15:36 +0000136 pid_t cpid;
137 sem_t *sem;
138
Caspar Zhangd59a6592013-03-07 14:59:12 +0800139 /* reset tst_count in case we are looping */
140 tst_count = 0;
subrata_modak8dfa1b32008-07-26 04:15:36 +0000141
142 ret = alloc_shared_pages_on_node(pages + SHARED_PAGE,
subrata_modak56207ce2009-03-23 13:35:39 +0000143 N_SHARED_PAGES, from_node);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000144 if (ret == -1)
145 continue;
146
147 ret = alloc_pages_on_node(pages + UNSHARED_PAGE,
subrata_modak56207ce2009-03-23 13:35:39 +0000148 N_UNSHARED_PAGES, from_node);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000149 if (ret == -1)
150 goto err_free_shared;
151
152 for (i = 0; i < N_TEST_PAGES; i++) {
153 nodes[i] = to_node;
154 }
155
156 sem = alloc_sem(MAX_SEMS);
157 if (sem == NULL) {
158 goto err_free_unshared;
159 }
160
161 /*
162 * Fork a child process so that the shared pages are
163 * now really shared between two processes.
164 */
165 cpid = fork();
166 if (cpid == -1) {
167 tst_resm(TBROK, "forking child failed");
168 goto err_free_sem;
169 } else if (cpid == 0) {
170 child(pages, sem);
171 }
172
173 /* Wait for child to setup and signal. */
174 if (sem_wait(&sem[SEM_CHILD_SETUP]) == -1)
yaberauneya4af190f2009-11-09 11:06:31 +0000175 tst_resm(TWARN | TERRNO, "error wait semaphore");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000176
177 ret = numa_move_pages(0, N_TEST_PAGES, pages, nodes,
178 status, MPOL_MF_MOVE);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000179 if (ret == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 tst_resm(TFAIL | TERRNO,
181 "move_pages unexpectedly failed");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000182 goto err_kill_child;
183 }
184
185 if (status[SHARED_PAGE] == -EACCES)
186 tst_resm(TPASS, "status[%d] set to expected -EACCES",
187 SHARED_PAGE);
188 else
189 tst_resm(TFAIL, "status[%d] is %d",
190 SHARED_PAGE, status[SHARED_PAGE]);
191
Wanlong Gao354ebb42012-12-07 10:10:04 +0800192err_kill_child:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000193 /* Test done. Ask child to terminate. */
194 if (sem_post(&sem[SEM_PARENT_TEST]) == -1)
yaberauneya4af190f2009-11-09 11:06:31 +0000195 tst_resm(TWARN | TERRNO, "error post semaphore");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000196 /* Read the status, no zombies! */
197 wait(NULL);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800198err_free_sem:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000199 free_sem(sem, MAX_SEMS);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800200err_free_unshared:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000201 free_pages(pages + UNSHARED_PAGE, N_UNSHARED_PAGES);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800202err_free_shared:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000203 free_shared_pages(pages + SHARED_PAGE, N_SHARED_PAGES);
204 }
yaberauneyaef772532009-10-09 17:55:43 +0000205#else
206 tst_resm(TCONF, "move_pages support not found.");
207#endif
subrata_modak8dfa1b32008-07-26 04:15:36 +0000208
209 cleanup();
subrata_modak8dfa1b32008-07-26 04:15:36 +0000210
Garrett Cooper2c282152010-12-16 00:55:50 -0800211 tst_exit();
subrata_modak8dfa1b32008-07-26 04:15:36 +0000212}
213
214/*
215 * setup() - performs all ONE TIME setup for this test
216 */
subrata_modak56207ce2009-03-23 13:35:39 +0000217void setup(void)
subrata_modak8dfa1b32008-07-26 04:15:36 +0000218{
Garrett Cooper2c282152010-12-16 00:55:50 -0800219
subrata_modak8dfa1b32008-07-26 04:15:36 +0000220 tst_sig(FORK, DEF_HANDLER, cleanup);
221
222 check_config(N_TEST_NODES);
223
224 /* Pause if that option was specified
225 * TEST_PAUSE contains the code to fork the test with the -c option.
226 */
227 TEST_PAUSE;
228}
229
230/*
231 * cleanup() - performs all ONE TIME cleanup for this test at completion
232 */
subrata_modak56207ce2009-03-23 13:35:39 +0000233void cleanup(void)
subrata_modak8dfa1b32008-07-26 04:15:36 +0000234{
subrata_modak8dfa1b32008-07-26 04:15:36 +0000235
Wanlong Gao354ebb42012-12-07 10:10:04 +0800236}