blob: ee0bf1afaebe43f3ecb9e3963f2e18a7d15b6e21 [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"
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_pages11";
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)
98 tst_resm(TWARN, "error post semaphore: %s", strerror(errno));
99
100 /* Wait for testcase in parent to complete. */
101 if (sem_wait(&sem[SEM_PARENT_TEST]) == -1)
102 tst_resm(TWARN, "error wait semaphore: %s", strerror(errno));
103
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) {
160 tst_resm(TBROK, "forking child failed: %s",
161 strerror(errno));
162 goto err_free_sem;
163 } else if (cpid == 0) {
164 child(pages, sem);
165 }
166
167 /* Wait for child to setup and signal. */
168 if (sem_wait(&sem[SEM_CHILD_SETUP]) == -1)
169 tst_resm(TWARN, "error wait semaphore: %s",
170 strerror(errno));
171
172 ret = numa_move_pages(0, TEST_PAGES, pages, nodes,
173 status, MPOL_MF_MOVE_ALL);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000174 if (ret == -1 && errno == EPERM)
175 tst_resm(TPASS, "move_pages failed with "
subrata_modak56207ce2009-03-23 13:35:39 +0000176 "EPERM as expected");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000177 else
Jan Stancek855da1d2014-02-25 16:09:56 +0100178 tst_resm(TFAIL|TERRNO, "move_pages did not fail "
179 "with EPERM ret: %d", ret);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000180
181 /* Test done. Ask child to terminate. */
182 if (sem_post(&sem[SEM_PARENT_TEST]) == -1)
183 tst_resm(TWARN, "error post semaphore: %s",
184 strerror(errno));
185 /* Read the status, no zombies! */
186 wait(NULL);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187err_free_sem:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000188 free_sem(sem, MAX_SEMS);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800189err_free_pages:
subrata_modak8dfa1b32008-07-26 04:15:36 +0000190 free_shared_pages(pages, TEST_PAGES);
191 }
yaberauneyaef772532009-10-09 17:55:43 +0000192#else
193 tst_resm(TCONF, "move_pages support not found.");
194#endif
subrata_modak8dfa1b32008-07-26 04:15:36 +0000195
196 cleanup();
subrata_modak8dfa1b32008-07-26 04:15:36 +0000197
Garrett Cooper2c282152010-12-16 00:55:50 -0800198 tst_exit();
subrata_modak8dfa1b32008-07-26 04:15:36 +0000199}
200
201/*
202 * setup() - performs all ONE TIME setup for this test
203 */
subrata_modak56207ce2009-03-23 13:35:39 +0000204void setup(void)
subrata_modak8dfa1b32008-07-26 04:15:36 +0000205{
206 struct passwd *ltpuser;
207
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200208 tst_require_root(NULL);
209
subrata_modak8dfa1b32008-07-26 04:15:36 +0000210 tst_sig(FORK, DEF_HANDLER, cleanup);
211
212 check_config(TEST_NODES);
213
subrata_modak8dfa1b32008-07-26 04:15:36 +0000214 if ((ltpuser = getpwnam("nobody")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100215 tst_brkm(TBROK, NULL, "'nobody' user not present");
subrata_modak8dfa1b32008-07-26 04:15:36 +0000216 }
217
218 if (seteuid(ltpuser->pw_uid) == -1) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100219 tst_brkm(TBROK, NULL, "setting uid to %d failed",
220 ltpuser->pw_uid);
subrata_modak8dfa1b32008-07-26 04:15:36 +0000221 }
222
223 /* Pause if that option was specified
224 * TEST_PAUSE contains the code to fork the test with the -c option.
225 */
226 TEST_PAUSE;
227}
228
229/*
230 * cleanup() - performs all ONE TIME cleanup for this test at completion
231 */
subrata_modak56207ce2009-03-23 13:35:39 +0000232void cleanup(void)
subrata_modak8dfa1b32008-07-26 04:15:36 +0000233{
subrata_modak8dfa1b32008-07-26 04:15:36 +0000234
Wanlong Gao354ebb42012-12-07 10:10:04 +0800235}