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_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>
59 #include "test.h"
60 #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
69 enum {
70 SEM_CHILD_SETUP,
71 SEM_PARENT_TEST,
72
73 MAX_SEMS
74 };
75
76 void setup(void);
77 void cleanup(void);
78
79 char *TCID = "move_pages05";
80 int TST_TOTAL = 1;
81
82 /*
83 * child() - touches pages, and waits for signal from parent.
84 * @pages: shared pages allocated in 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 < 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)
99 tst_resm(TWARN | TERRNO, "error post semaphore");
100
101 /* Wait for testcase in parent to complete. */
102 if (sem_wait(&sem[SEM_PARENT_TEST]) == -1)
103 tst_resm(TWARN | TERRNO, "error waiting for semaphore");
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[N_TEST_PAGES] = { 0 };
129 int nodes[N_TEST_PAGES];
130 int status[N_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 + SHARED_PAGE,
138 N_SHARED_PAGES, from_node);
139 if (ret == -1)
140 continue;
141
142 ret = alloc_pages_on_node(pages + UNSHARED_PAGE,
143 N_UNSHARED_PAGES, from_node);
144 if (ret == -1)
145 goto err_free_shared;
146
147 for (i = 0; i < N_TEST_PAGES; i++) {
148 nodes[i] = to_node;
149 }
150
151 sem = alloc_sem(MAX_SEMS);
152 if (sem == NULL) {
153 goto err_free_unshared;
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");
163 goto err_free_sem;
164 } else if (cpid == 0) {
165 child(pages, sem);
166 }
167
168 /* Wait for child to setup and signal. */
169 if (sem_wait(&sem[SEM_CHILD_SETUP]) == -1)
170 tst_resm(TWARN | TERRNO, "error wait semaphore");
171
172 ret = numa_move_pages(0, N_TEST_PAGES, pages, nodes,
173 status, MPOL_MF_MOVE);
174 if (ret == -1) {
175 tst_resm(TFAIL | TERRNO,
176 "move_pages unexpectedly failed");
177 goto err_kill_child;
178 }
179
180 if (status[SHARED_PAGE] == -EACCES)
181 tst_resm(TPASS, "status[%d] set to expected -EACCES",
182 SHARED_PAGE);
183 else
184 tst_resm(TFAIL, "status[%d] is %d",
185 SHARED_PAGE, status[SHARED_PAGE]);
186
187 err_kill_child:
188 /* Test done. Ask child to terminate. */
189 if (sem_post(&sem[SEM_PARENT_TEST]) == -1)
190 tst_resm(TWARN | TERRNO, "error post semaphore");
191 /* Read the status, no zombies! */
192 wait(NULL);
193 err_free_sem:
194 free_sem(sem, MAX_SEMS);
195 err_free_unshared:
196 free_pages(pages + UNSHARED_PAGE, N_UNSHARED_PAGES);
197 err_free_shared:
198 free_shared_pages(pages + SHARED_PAGE, N_SHARED_PAGES);
199 }
200 #else
201 tst_resm(TCONF, "test requires libnuma >= 2 and it's development packages");
202 #endif
203
204 cleanup();
205
206 tst_exit();
207 }
208
209 /*
210 * setup() - performs all ONE TIME setup for this test
211 */
setup(void)212 void setup(void)
213 {
214
215 tst_sig(FORK, DEF_HANDLER, cleanup);
216
217 check_config(N_TEST_NODES);
218
219 /* Pause if that option was specified
220 * TEST_PAUSE contains the code to fork the test with the -c option.
221 */
222 TEST_PAUSE;
223 }
224
225 /*
226 * cleanup() - performs all ONE TIME cleanup for this test at completion
227 */
cleanup(void)228 void cleanup(void)
229 {
230
231 }
232