1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *   Copyright (c) 2008 Vijay Kumar B. <vijaykumar@bravegnu.org>
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci *   Based on testcases/kernel/syscalls/waitpid/waitpid01.c
5f08c3bdfSopenharmony_ci *   Original copyright message:
6f08c3bdfSopenharmony_ci *
7f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
10f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
11f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
12f08c3bdfSopenharmony_ci *   (at your option) any later version.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
15f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
16f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
20f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
21f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22f08c3bdfSopenharmony_ci */
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci/*
25f08c3bdfSopenharmony_ci * NAME
26f08c3bdfSopenharmony_ci *	move_pages11.c
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci * DESCRIPTION
29f08c3bdfSopenharmony_ci *      Failure when trying move shared pages.
30f08c3bdfSopenharmony_ci *
31f08c3bdfSopenharmony_ci * ALGORITHM
32f08c3bdfSopenharmony_ci *      1. Allocate a shared memory in NUMA node A.
33f08c3bdfSopenharmony_ci *      2. Fork another process.
34f08c3bdfSopenharmony_ci *      3. Use move_pages() to move the pages to NUMA node B, with the
35f08c3bdfSopenharmony_ci *         MPOL_MF_MOVE_ALL.
36f08c3bdfSopenharmony_ci *      4. Check if errno is set to EPERM.
37f08c3bdfSopenharmony_ci *
38f08c3bdfSopenharmony_ci * USAGE:  <for command-line>
39f08c3bdfSopenharmony_ci *      move_pages11 [-c n] [-i n] [-I x] [-P x] [-t]
40f08c3bdfSopenharmony_ci *      where,  -c n : Run n copies concurrently.
41f08c3bdfSopenharmony_ci *              -i n : Execute test n times.
42f08c3bdfSopenharmony_ci *              -I x : Execute test for x seconds.
43f08c3bdfSopenharmony_ci *              -P x : Pause for x seconds between iterations.
44f08c3bdfSopenharmony_ci *              -t   : Turn on syscall timing.
45f08c3bdfSopenharmony_ci *
46f08c3bdfSopenharmony_ci * History
47f08c3bdfSopenharmony_ci *	05/2008 Vijay Kumar
48f08c3bdfSopenharmony_ci *		Initial Version.
49f08c3bdfSopenharmony_ci *
50f08c3bdfSopenharmony_ci * Restrictions
51f08c3bdfSopenharmony_ci *	None
52f08c3bdfSopenharmony_ci */
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci#include <sys/types.h>
55f08c3bdfSopenharmony_ci#include <sys/wait.h>
56f08c3bdfSopenharmony_ci#include <unistd.h>
57f08c3bdfSopenharmony_ci#include <signal.h>
58f08c3bdfSopenharmony_ci#include <semaphore.h>
59f08c3bdfSopenharmony_ci#include <errno.h>
60f08c3bdfSopenharmony_ci#include <pwd.h>
61f08c3bdfSopenharmony_ci#include "test.h"
62f08c3bdfSopenharmony_ci#include "safe_macros.h"
63f08c3bdfSopenharmony_ci#include "move_pages_support.h"
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci#define TEST_PAGES 2
66f08c3bdfSopenharmony_ci#define TEST_NODES 2
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_cienum {
69f08c3bdfSopenharmony_ci	SEM_CHILD_SETUP,
70f08c3bdfSopenharmony_ci	SEM_PARENT_TEST,
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	MAX_SEMS
73f08c3bdfSopenharmony_ci};
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_civoid setup(void);
76f08c3bdfSopenharmony_civoid cleanup(void);
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cichar *TCID = "move_pages11";
79f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci/*
82f08c3bdfSopenharmony_ci * child() - touches shared pages, and waits for signal from parent.
83f08c3bdfSopenharmony_ci * @pages: shared pages allocated in parent
84f08c3bdfSopenharmony_ci * @sem: semaphore to sync with parent
85f08c3bdfSopenharmony_ci */
86f08c3bdfSopenharmony_civoid child(void **pages, sem_t * sem)
87f08c3bdfSopenharmony_ci{
88f08c3bdfSopenharmony_ci	int i;
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	for (i = 0; i < TEST_PAGES; i++) {
91f08c3bdfSopenharmony_ci		char *page;
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci		page = pages[i];
94f08c3bdfSopenharmony_ci		page[0] = 0xAA;
95f08c3bdfSopenharmony_ci	}
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	/* Setup complete. Ask parent to continue. */
98f08c3bdfSopenharmony_ci	if (sem_post(&sem[SEM_CHILD_SETUP]) == -1)
99f08c3bdfSopenharmony_ci		tst_resm(TWARN, "error post semaphore: %s", strerror(errno));
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	/* Wait for testcase in parent to complete. */
102f08c3bdfSopenharmony_ci	if (sem_wait(&sem[SEM_PARENT_TEST]) == -1)
103f08c3bdfSopenharmony_ci		tst_resm(TWARN, "error wait semaphore: %s", strerror(errno));
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	exit(0);
106f08c3bdfSopenharmony_ci}
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ciint main(int argc, char **argv)
109f08c3bdfSopenharmony_ci{
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci	tst_parse_opts(argc, argv, NULL, NULL);
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ci	setup();
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci#ifdef HAVE_NUMA_V2
116f08c3bdfSopenharmony_ci	unsigned int i;
117f08c3bdfSopenharmony_ci	int lc;
118f08c3bdfSopenharmony_ci	unsigned int from_node;
119f08c3bdfSopenharmony_ci	unsigned int to_node;
120f08c3bdfSopenharmony_ci	int ret;
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	ret = get_allowed_nodes(NH_MEMS, 2, &from_node, &to_node);
123f08c3bdfSopenharmony_ci	if (ret < 0)
124f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "get_allowed_nodes: %d", ret);
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_ci	/* check for looping state if -i option is given */
127f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
128f08c3bdfSopenharmony_ci		void *pages[TEST_PAGES] = { 0 };
129f08c3bdfSopenharmony_ci		int nodes[TEST_PAGES];
130f08c3bdfSopenharmony_ci		int status[TEST_PAGES];
131f08c3bdfSopenharmony_ci		pid_t cpid;
132f08c3bdfSopenharmony_ci		sem_t *sem;
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci		/* reset tst_count in case we are looping */
135f08c3bdfSopenharmony_ci		tst_count = 0;
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci		ret = alloc_shared_pages_on_node(pages, TEST_PAGES, from_node);
138f08c3bdfSopenharmony_ci		if (ret == -1)
139f08c3bdfSopenharmony_ci			continue;
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_ci		for (i = 0; i < TEST_PAGES; i++) {
142f08c3bdfSopenharmony_ci			nodes[i] = to_node;
143f08c3bdfSopenharmony_ci		}
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci		sem = alloc_sem(MAX_SEMS);
146f08c3bdfSopenharmony_ci		if (sem == NULL) {
147f08c3bdfSopenharmony_ci			goto err_free_pages;
148f08c3bdfSopenharmony_ci		}
149f08c3bdfSopenharmony_ci
150f08c3bdfSopenharmony_ci		/*
151f08c3bdfSopenharmony_ci		 * Fork a child process so that the shared pages are
152f08c3bdfSopenharmony_ci		 * now really shared between two processes.
153f08c3bdfSopenharmony_ci		 */
154f08c3bdfSopenharmony_ci		cpid = fork();
155f08c3bdfSopenharmony_ci		if (cpid == -1) {
156f08c3bdfSopenharmony_ci			tst_resm(TBROK, "forking child failed: %s",
157f08c3bdfSopenharmony_ci				 strerror(errno));
158f08c3bdfSopenharmony_ci			goto err_free_sem;
159f08c3bdfSopenharmony_ci		} else if (cpid == 0) {
160f08c3bdfSopenharmony_ci			child(pages, sem);
161f08c3bdfSopenharmony_ci		}
162f08c3bdfSopenharmony_ci
163f08c3bdfSopenharmony_ci		/* Wait for child to setup and signal. */
164f08c3bdfSopenharmony_ci		if (sem_wait(&sem[SEM_CHILD_SETUP]) == -1)
165f08c3bdfSopenharmony_ci			tst_resm(TWARN, "error wait semaphore: %s",
166f08c3bdfSopenharmony_ci				 strerror(errno));
167f08c3bdfSopenharmony_ci
168f08c3bdfSopenharmony_ci		ret = numa_move_pages(0, TEST_PAGES, pages, nodes,
169f08c3bdfSopenharmony_ci				      status, MPOL_MF_MOVE_ALL);
170f08c3bdfSopenharmony_ci		if (ret == -1 && errno == EPERM)
171f08c3bdfSopenharmony_ci			tst_resm(TPASS, "move_pages failed with "
172f08c3bdfSopenharmony_ci				 "EPERM as expected");
173f08c3bdfSopenharmony_ci		else
174f08c3bdfSopenharmony_ci			tst_resm(TFAIL|TERRNO, "move_pages did not fail "
175f08c3bdfSopenharmony_ci				 "with EPERM ret: %d", ret);
176f08c3bdfSopenharmony_ci
177f08c3bdfSopenharmony_ci		/* Test done. Ask child to terminate. */
178f08c3bdfSopenharmony_ci		if (sem_post(&sem[SEM_PARENT_TEST]) == -1)
179f08c3bdfSopenharmony_ci			tst_resm(TWARN, "error post semaphore: %s",
180f08c3bdfSopenharmony_ci				 strerror(errno));
181f08c3bdfSopenharmony_ci		/* Read the status, no zombies! */
182f08c3bdfSopenharmony_ci		wait(NULL);
183f08c3bdfSopenharmony_cierr_free_sem:
184f08c3bdfSopenharmony_ci		free_sem(sem, MAX_SEMS);
185f08c3bdfSopenharmony_cierr_free_pages:
186f08c3bdfSopenharmony_ci		free_shared_pages(pages, TEST_PAGES);
187f08c3bdfSopenharmony_ci	}
188f08c3bdfSopenharmony_ci#else
189f08c3bdfSopenharmony_ci	tst_resm(TCONF, NUMA_ERROR_MSG);
190f08c3bdfSopenharmony_ci#endif
191f08c3bdfSopenharmony_ci
192f08c3bdfSopenharmony_ci	cleanup();
193f08c3bdfSopenharmony_ci
194f08c3bdfSopenharmony_ci	tst_exit();
195f08c3bdfSopenharmony_ci}
196f08c3bdfSopenharmony_ci
197f08c3bdfSopenharmony_ci/*
198f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test
199f08c3bdfSopenharmony_ci */
200f08c3bdfSopenharmony_civoid setup(void)
201f08c3bdfSopenharmony_ci{
202f08c3bdfSopenharmony_ci	struct passwd *ltpuser;
203f08c3bdfSopenharmony_ci
204f08c3bdfSopenharmony_ci	tst_require_root();
205f08c3bdfSopenharmony_ci
206f08c3bdfSopenharmony_ci	tst_sig(FORK, DEF_HANDLER, cleanup);
207f08c3bdfSopenharmony_ci
208f08c3bdfSopenharmony_ci	check_config(TEST_NODES);
209f08c3bdfSopenharmony_ci
210f08c3bdfSopenharmony_ci	if ((ltpuser = getpwnam("nobody")) == NULL) {
211f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "'nobody' user not present");
212f08c3bdfSopenharmony_ci	}
213f08c3bdfSopenharmony_ci
214f08c3bdfSopenharmony_ci	SAFE_SETEUID(NULL, ltpuser->pw_uid);
215f08c3bdfSopenharmony_ci
216f08c3bdfSopenharmony_ci	/* Pause if that option was specified
217f08c3bdfSopenharmony_ci	 * TEST_PAUSE contains the code to fork the test with the -c option.
218f08c3bdfSopenharmony_ci	 */
219f08c3bdfSopenharmony_ci	TEST_PAUSE;
220f08c3bdfSopenharmony_ci}
221f08c3bdfSopenharmony_ci
222f08c3bdfSopenharmony_ci/*
223f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at completion
224f08c3bdfSopenharmony_ci */
225f08c3bdfSopenharmony_civoid cleanup(void)
226f08c3bdfSopenharmony_ci{
227f08c3bdfSopenharmony_ci
228f08c3bdfSopenharmony_ci}
229