blob: 094d548148b30993530f0e5d30c659695d96c189 [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_pages11.c
27 *
28 * DESCRIPTION
29 * Failure when trying move shared pages.
30 *
31 * ALGORITHM
32 * 1. Allocate a shared memory in NUMA node A.
33 * 2. Fork another process.
34 * 3. Use move_pages() to move the pages to NUMA node B, with the
35 * MPOL_MF_MOVE_ALL.
36 * 4. Check if errno is set to EPERM.
37 *
38 * USAGE: <for command-line>
39 * move_pages11 [-c n] [-i n] [-I x] [-P x] [-t]
40 * where, -c n : Run n copies concurrently.
41 * -i n : Execute test n times.
42 * -I x : Execute test for x seconds.
43 * -P x : Pause for x seconds between iterations.
44 * -t : Turn on syscall timing.
45 *
46 * History
47 * 05/2008 Vijay Kumar
48 * Initial Version.
49 *
50 * Restrictions
51 * None
52 */
53
54#include <sys/types.h>
55#include <sys/wait.h>
56#include <unistd.h>
57#include <signal.h>
58#include <semaphore.h>
59#include <errno.h>
subrata_modak8dfa1b32008-07-26 04:15:36 +000060#include <pwd.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080061#include "test.h"
62#include "usctest.h"
subrata_modak8dfa1b32008-07-26 04:15:36 +000063#include "move_pages_support.h"
64
65#define TEST_PAGES 2
66#define TEST_NODES 2
67
68enum {
69 SEM_CHILD_SETUP,
70 SEM_PARENT_TEST,
71
72 MAX_SEMS
73};
74
75void setup(void);
76void cleanup(void);
77
78char *TCID = "move_pages11";
79int TST_TOTAL = 1;
subrata_modak8dfa1b32008-07-26 04:15:36 +000080
81/*
82 * child() - touches shared pages, and waits for signal from parent.
83 * @pages: shared pages allocated in parent
84 * @sem: semaphore to sync with 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 < 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)
99 tst_resm(TWARN, "error post semaphore: %s", strerror(errno));
100
101 /* Wait for testcase in parent to complete. */
102 if (sem_wait(&sem[SEM_PARENT_TEST]) == -1)
103 tst_resm(TWARN, "error wait semaphore: %s", strerror(errno));
104
105 exit(0);
106}
107
108int main(int argc, char **argv)
109{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200110 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);
115 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800116
subrata_modak8dfa1b32008-07-26 04:15:36 +0000117 }
118
119 setup();
120
yaberauneyaef772532009-10-09 17:55:43 +0000121#if HAVE_NUMA_MOVE_PAGES
122 unsigned int i;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200123 int lc;
Jan Stancek134bea02012-06-28 11:03:18 +0200124 unsigned int from_node;
125 unsigned int to_node;
126 int ret;
127
Jan Stancekd534a442012-08-09 14:15:38 +0800128 ret = get_allowed_nodes(NH_MEMS, 2, &from_node, &to_node);
129 if (ret < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 tst_brkm(TBROK | TERRNO, cleanup, "get_allowed_nodes: %d", ret);
yaberauneyaef772532009-10-09 17:55:43 +0000131
subrata_modak8dfa1b32008-07-26 04:15:36 +0000132 /* check for looping state if -i option is given */
133 for (lc = 0; TEST_LOOPING(lc); lc++) {
134 void *pages[TEST_PAGES] = { 0 };
135 int nodes[TEST_PAGES];
136 int status[TEST_PAGES];
subrata_modak8dfa1b32008-07-26 04:15:36 +0000137 pid_t cpid;
138 sem_t *sem;
139
Caspar Zhangd59a6592013-03-07 14:59:12 +0800140 /* reset tst_count in case we are looping */
141 tst_count = 0;
subrata_modak8dfa1b32008-07-26 04:15:36 +0000142
subrata_modak56207ce2009-03-23 13:35:39 +0000143 ret = alloc_shared_pages_on_node(pages, TEST_PAGES, from_node);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000144 if (ret == -1)
145 continue;
146
subrata_modak8dfa1b32008-07-26 04:15:36 +0000147 for (i = 0; i < TEST_PAGES; i++) {
148 nodes[i] = to_node;
149 }
150
151 sem = alloc_sem(MAX_SEMS);
152 if (sem == NULL) {
153 goto err_free_pages;
154 }
155
156 /*
157 * Fork a child process so that the shared pages are
158 * now really shared between two processes.
159 */
160 cpid = fork();
161 if (cpid == -1) {
162 tst_resm(TBROK, "forking child failed: %s",
163 strerror(errno));
164 goto err_free_sem;
165 } else if (cpid == 0) {
166 child(pages, sem);
167 }
168
169 /* Wait for child to setup and signal. */
170 if (sem_wait(&sem[SEM_CHILD_SETUP]) == -1)
171 tst_resm(TWARN, "error wait semaphore: %s",
172 strerror(errno));
173
174 ret = numa_move_pages(0, TEST_PAGES, pages, nodes,
175 status, MPOL_MF_MOVE_ALL);
176 TEST_ERRNO = errno;
177 if (ret == -1 && errno == EPERM)
178 tst_resm(TPASS, "move_pages failed with "
subrata_modak56207ce2009-03-23 13:35:39 +0000179 "EPERM as expected");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000180 else
181 tst_resm(TFAIL, "move_pages did not fail "
182 "with EPERM");
183
184 /* Test done. Ask child to terminate. */
185 if (sem_post(&sem[SEM_PARENT_TEST]) == -1)
186 tst_resm(TWARN, "error post semaphore: %s",
187 strerror(errno));
188 /* Read the status, no zombies! */
189 wait(NULL);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190err_free_sem:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000191 free_sem(sem, MAX_SEMS);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800192err_free_pages:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000193 free_shared_pages(pages, TEST_PAGES);
194 }
yaberauneyaef772532009-10-09 17:55:43 +0000195#else
196 tst_resm(TCONF, "move_pages support not found.");
197#endif
subrata_modak8dfa1b32008-07-26 04:15:36 +0000198
199 cleanup();
subrata_modak8dfa1b32008-07-26 04:15:36 +0000200
Garrett Cooper2c282152010-12-16 00:55:50 -0800201 tst_exit();
subrata_modak8dfa1b32008-07-26 04:15:36 +0000202}
203
204/*
205 * setup() - performs all ONE TIME setup for this test
206 */
subrata_modak56207ce2009-03-23 13:35:39 +0000207void setup(void)
subrata_modak8dfa1b32008-07-26 04:15:36 +0000208{
209 struct passwd *ltpuser;
210
subrata_modak8dfa1b32008-07-26 04:15:36 +0000211 tst_sig(FORK, DEF_HANDLER, cleanup);
212
213 check_config(TEST_NODES);
214
215 if (geteuid() != 0) {
216 tst_resm(TBROK, "test must be run as root");
217 tst_exit();
218 }
219
220 if ((ltpuser = getpwnam("nobody")) == NULL) {
221 tst_resm(TBROK, "'nobody' user not present");
222 tst_exit();
223 }
224
225 if (seteuid(ltpuser->pw_uid) == -1) {
226 tst_resm(TBROK, "setting uid to %d failed", ltpuser->pw_uid);
227 tst_exit();
228 }
229
230 /* Pause if that option was specified
231 * TEST_PAUSE contains the code to fork the test with the -c option.
232 */
233 TEST_PAUSE;
234}
235
236/*
237 * cleanup() - performs all ONE TIME cleanup for this test at completion
238 */
subrata_modak56207ce2009-03-23 13:35:39 +0000239void cleanup(void)
subrata_modak8dfa1b32008-07-26 04:15:36 +0000240{
241 /*
242 * print timing stats if that option was specified.
243 * print errno log if that option was specified.
244 */
245 TEST_CLEANUP;
246
Wanlong Gao354ebb42012-12-07 10:10:04 +0800247}