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_pages04.c
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci * DESCRIPTION
29f08c3bdfSopenharmony_ci *      Failure when page does not exit.
30f08c3bdfSopenharmony_ci *
31f08c3bdfSopenharmony_ci * ALGORITHM
32f08c3bdfSopenharmony_ci *
33f08c3bdfSopenharmony_ci *      1. Pass zero page (allocated, but not written to) as one of the
34f08c3bdfSopenharmony_ci *         page addresses to move_pages().
35f08c3bdfSopenharmony_ci *      2. Check if the corresponding status is set to:
36f08c3bdfSopenharmony_ci *         -ENOENT for kernels < 4.3
37f08c3bdfSopenharmony_ci *         -EFAULT for kernels >= 4.3 [1]
38f08c3bdfSopenharmony_ci *
39f08c3bdfSopenharmony_ci * [1]
40f08c3bdfSopenharmony_ci * d899844e9c98 "mm: fix status code which move_pages() returns for zero page"
41f08c3bdfSopenharmony_ci *
42f08c3bdfSopenharmony_ci * USAGE:  <for command-line>
43f08c3bdfSopenharmony_ci *      move_pages04 [-c n] [-i n] [-I x] [-P x] [-t]
44f08c3bdfSopenharmony_ci *      where,  -c n : Run n copies concurrently.
45f08c3bdfSopenharmony_ci *              -i n : Execute test n times.
46f08c3bdfSopenharmony_ci *              -I x : Execute test for x seconds.
47f08c3bdfSopenharmony_ci *              -P x : Pause for x seconds between iterations.
48f08c3bdfSopenharmony_ci *              -t   : Turn on syscall timing.
49f08c3bdfSopenharmony_ci *
50f08c3bdfSopenharmony_ci * History
51f08c3bdfSopenharmony_ci *	05/2008 Vijay Kumar
52f08c3bdfSopenharmony_ci *		Initial Version.
53f08c3bdfSopenharmony_ci *
54f08c3bdfSopenharmony_ci * Restrictions
55f08c3bdfSopenharmony_ci *	None
56f08c3bdfSopenharmony_ci */
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci#include <sys/mman.h>
59f08c3bdfSopenharmony_ci#include <sys/types.h>
60f08c3bdfSopenharmony_ci#include <sys/wait.h>
61f08c3bdfSopenharmony_ci#include <unistd.h>
62f08c3bdfSopenharmony_ci#include <signal.h>
63f08c3bdfSopenharmony_ci#include <errno.h>
64f08c3bdfSopenharmony_ci#include "test.h"
65f08c3bdfSopenharmony_ci#include "move_pages_support.h"
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci#define TEST_PAGES 2
68f08c3bdfSopenharmony_ci#define TEST_NODES 2
69f08c3bdfSopenharmony_ci#define TOUCHED_PAGES 1
70f08c3bdfSopenharmony_ci#define UNTOUCHED_PAGE (TEST_PAGES - 1)
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_civoid setup(void);
73f08c3bdfSopenharmony_civoid cleanup(void);
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_cichar *TCID = "move_pages04";
76f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_citypedef void (*sighandler_t) (int);
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ciint main(int argc, char **argv)
81f08c3bdfSopenharmony_ci{
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	tst_parse_opts(argc, argv, NULL, NULL);
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	setup();
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci#ifdef HAVE_NUMA_V2
88f08c3bdfSopenharmony_ci	unsigned int i;
89f08c3bdfSopenharmony_ci	int lc;
90f08c3bdfSopenharmony_ci	unsigned int from_node;
91f08c3bdfSopenharmony_ci	unsigned int to_node;
92f08c3bdfSopenharmony_ci	int ret, exp_status;
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	if ((tst_kvercmp(4, 3, 0)) >= 0)
95f08c3bdfSopenharmony_ci		exp_status = -EFAULT;
96f08c3bdfSopenharmony_ci	else
97f08c3bdfSopenharmony_ci		exp_status = -ENOENT;
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	ret = get_allowed_nodes(NH_MEMS, 2, &from_node, &to_node);
100f08c3bdfSopenharmony_ci	if (ret < 0)
101f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "get_allowed_nodes: %d", ret);
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	/* check for looping state if -i option is given */
104f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
105f08c3bdfSopenharmony_ci		void *pages[TEST_PAGES] = { 0 };
106f08c3bdfSopenharmony_ci		int nodes[TEST_PAGES];
107f08c3bdfSopenharmony_ci		int status[TEST_PAGES];
108f08c3bdfSopenharmony_ci		unsigned long onepage = get_page_size();
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci		/* reset tst_count in case we are looping */
111f08c3bdfSopenharmony_ci		tst_count = 0;
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ci		ret = alloc_pages_on_node(pages, TOUCHED_PAGES, from_node);
114f08c3bdfSopenharmony_ci		if (ret == -1)
115f08c3bdfSopenharmony_ci			continue;
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci		/* Allocate page and do not touch it. */
118f08c3bdfSopenharmony_ci		pages[UNTOUCHED_PAGE] = numa_alloc_onnode(onepage, from_node);
119f08c3bdfSopenharmony_ci		if (pages[UNTOUCHED_PAGE] == NULL) {
120f08c3bdfSopenharmony_ci			tst_resm(TBROK, "failed allocating page on node %d",
121f08c3bdfSopenharmony_ci				 from_node);
122f08c3bdfSopenharmony_ci			goto err_free_pages;
123f08c3bdfSopenharmony_ci		}
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_ci		for (i = 0; i < TEST_PAGES; i++)
126f08c3bdfSopenharmony_ci			nodes[i] = to_node;
127f08c3bdfSopenharmony_ci
128f08c3bdfSopenharmony_ci		ret = numa_move_pages(0, TEST_PAGES, pages, nodes,
129f08c3bdfSopenharmony_ci				      status, MPOL_MF_MOVE);
130f08c3bdfSopenharmony_ci		if (ret == -1) {
131f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TERRNO,
132f08c3bdfSopenharmony_ci				 "move_pages unexpectedly failed");
133f08c3bdfSopenharmony_ci			goto err_free_pages;
134f08c3bdfSopenharmony_ci		} else if (ret > 0) {
135f08c3bdfSopenharmony_ci			tst_resm(TINFO, "move_pages() returned %d", ret);
136f08c3bdfSopenharmony_ci		}
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci		if (status[UNTOUCHED_PAGE] == exp_status) {
139f08c3bdfSopenharmony_ci			tst_resm(TPASS, "status[%d] has expected value",
140f08c3bdfSopenharmony_ci				 UNTOUCHED_PAGE);
141f08c3bdfSopenharmony_ci		} else {
142f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "status[%d] is %s, expected %s",
143f08c3bdfSopenharmony_ci				UNTOUCHED_PAGE,
144f08c3bdfSopenharmony_ci				tst_strerrno(-status[UNTOUCHED_PAGE]),
145f08c3bdfSopenharmony_ci				tst_strerrno(-exp_status));
146f08c3bdfSopenharmony_ci		}
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_cierr_free_pages:
149f08c3bdfSopenharmony_ci		/* This is capable of freeing both the touched and
150f08c3bdfSopenharmony_ci		 * untouched pages.
151f08c3bdfSopenharmony_ci		 */
152f08c3bdfSopenharmony_ci		free_pages(pages, TEST_PAGES);
153f08c3bdfSopenharmony_ci	}
154f08c3bdfSopenharmony_ci#else
155f08c3bdfSopenharmony_ci	tst_resm(TCONF, NUMA_ERROR_MSG);
156f08c3bdfSopenharmony_ci#endif
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	cleanup();
159f08c3bdfSopenharmony_ci	tst_exit();
160f08c3bdfSopenharmony_ci}
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_ci/*
163f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test
164f08c3bdfSopenharmony_ci */
165f08c3bdfSopenharmony_civoid setup(void)
166f08c3bdfSopenharmony_ci{
167f08c3bdfSopenharmony_ci
168f08c3bdfSopenharmony_ci	tst_sig(FORK, DEF_HANDLER, cleanup);
169f08c3bdfSopenharmony_ci
170f08c3bdfSopenharmony_ci	check_config(TEST_NODES);
171f08c3bdfSopenharmony_ci
172f08c3bdfSopenharmony_ci	/* Pause if that option was specified
173f08c3bdfSopenharmony_ci	 * TEST_PAUSE contains the code to fork the test with the -c option.
174f08c3bdfSopenharmony_ci	 */
175f08c3bdfSopenharmony_ci	TEST_PAUSE;
176f08c3bdfSopenharmony_ci}
177f08c3bdfSopenharmony_ci
178f08c3bdfSopenharmony_ci/*
179f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at completion
180f08c3bdfSopenharmony_ci */
181f08c3bdfSopenharmony_civoid cleanup(void)
182f08c3bdfSopenharmony_ci{
183f08c3bdfSopenharmony_ci
184f08c3bdfSopenharmony_ci}
185