1 /*
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
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
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>
60 #include <pwd.h>
61 #include "test.h"
62 #include "safe_macros.h"
63 #include "move_pages_support.h"
64
65 #define TEST_PAGES 2
66 #define TEST_NODES 2
67
68 enum {
69 SEM_CHILD_SETUP,
70 SEM_PARENT_TEST,
71
72 MAX_SEMS
73 };
74
75 void setup(void);
76 void cleanup(void);
77
78 char *TCID = "move_pages11";
79 int TST_TOTAL = 1;
80
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 */
child(void ** pages,sem_t * sem)86 void child(void **pages, sem_t * sem)
87 {
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
main(int argc,char ** argv)108 int main(int argc, char **argv)
109 {
110
111 tst_parse_opts(argc, argv, NULL, NULL);
112
113 setup();
114
115 #ifdef HAVE_NUMA_V2
116 unsigned int i;
117 int lc;
118 unsigned int from_node;
119 unsigned int to_node;
120 int ret;
121
122 ret = get_allowed_nodes(NH_MEMS, 2, &from_node, &to_node);
123 if (ret < 0)
124 tst_brkm(TBROK | TERRNO, cleanup, "get_allowed_nodes: %d", ret);
125
126 /* check for looping state if -i option is given */
127 for (lc = 0; TEST_LOOPING(lc); lc++) {
128 void *pages[TEST_PAGES] = { 0 };
129 int nodes[TEST_PAGES];
130 int status[TEST_PAGES];
131 pid_t cpid;
132 sem_t *sem;
133
134 /* reset tst_count in case we are looping */
135 tst_count = 0;
136
137 ret = alloc_shared_pages_on_node(pages, TEST_PAGES, from_node);
138 if (ret == -1)
139 continue;
140
141 for (i = 0; i < TEST_PAGES; i++) {
142 nodes[i] = to_node;
143 }
144
145 sem = alloc_sem(MAX_SEMS);
146 if (sem == NULL) {
147 goto err_free_pages;
148 }
149
150 /*
151 * Fork a child process so that the shared pages are
152 * now really shared between two processes.
153 */
154 cpid = fork();
155 if (cpid == -1) {
156 tst_resm(TBROK, "forking child failed: %s",
157 strerror(errno));
158 goto err_free_sem;
159 } else if (cpid == 0) {
160 child(pages, sem);
161 }
162
163 /* Wait for child to setup and signal. */
164 if (sem_wait(&sem[SEM_CHILD_SETUP]) == -1)
165 tst_resm(TWARN, "error wait semaphore: %s",
166 strerror(errno));
167
168 ret = numa_move_pages(0, TEST_PAGES, pages, nodes,
169 status, MPOL_MF_MOVE_ALL);
170 if (ret == -1 && errno == EPERM)
171 tst_resm(TPASS, "move_pages failed with "
172 "EPERM as expected");
173 else
174 tst_resm(TFAIL|TERRNO, "move_pages did not fail "
175 "with EPERM ret: %d", ret);
176
177 /* Test done. Ask child to terminate. */
178 if (sem_post(&sem[SEM_PARENT_TEST]) == -1)
179 tst_resm(TWARN, "error post semaphore: %s",
180 strerror(errno));
181 /* Read the status, no zombies! */
182 wait(NULL);
183 err_free_sem:
184 free_sem(sem, MAX_SEMS);
185 err_free_pages:
186 free_shared_pages(pages, TEST_PAGES);
187 }
188 #else
189 tst_resm(TCONF, NUMA_ERROR_MSG);
190 #endif
191
192 cleanup();
193
194 tst_exit();
195 }
196
197 /*
198 * setup() - performs all ONE TIME setup for this test
199 */
setup(void)200 void setup(void)
201 {
202 struct passwd *ltpuser;
203
204 tst_require_root();
205
206 tst_sig(FORK, DEF_HANDLER, cleanup);
207
208 check_config(TEST_NODES);
209
210 if ((ltpuser = getpwnam("nobody")) == NULL) {
211 tst_brkm(TBROK, NULL, "'nobody' user not present");
212 }
213
214 SAFE_SETEUID(NULL, ltpuser->pw_uid);
215
216 /* Pause if that option was specified
217 * TEST_PAUSE contains the code to fork the test with the -c option.
218 */
219 TEST_PAUSE;
220 }
221
222 /*
223 * cleanup() - performs all ONE TIME cleanup for this test at completion
224 */
cleanup(void)225 void cleanup(void)
226 {
227
228 }
229