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