Linux Test Project
 help / color / mirror / Atom feed
From: Pradeep Susarla <pradeep.susarla@gmail.com>
To: ltp@lists.linux.it
Cc: Pradeep Susarla <pradeep.susarla@gmail.com>
Subject: [LTP] [PATCH] migrate_pages01: Convert to new API
Date: Tue, 24 Nov 2026 13:12:37 +0000	[thread overview]
Message-ID: <20261124131237.53712-1-pradeep.susarla@gmail.com> (raw)

Rewrite the test using the new test API:

- Use TST_EXP_PASS() and TST_EXP_FAIL() instead of the legacy
  check_ret() and check_errno() helpers. Nothing else uses them, so
  remove them from migrate_pages_common.c, which then no longer needs
  test.h.
- Look up the allowed nodes once in setup() with get_allowed_nodes_arr()
  instead of calling is_numa() and get_allowed_nodes().
- Run the EPERM check in a SAFE_FORK() child and reap it with
  tst_reap_children().
- Replace tst_require_root() with .needs_root.
- Add a description of what is tested.

The tested cases are unchanged.

Signed-off-by: Pradeep Susarla <pradeep.susarla@gmail.com>
---

Notes:
    Tested on a single-node x86_64 VM (kernel 6.18): all 9 checks pass, also with -i 3.
    migrate_pages02 and migrate_pages03 still build against the trimmed
    migrate_pages_common.c. make check-migrate_pages01 is clean.

 .../syscalls/migrate_pages/migrate_pages01.c  | 313 +++++++-----------
 .../migrate_pages/migrate_pages_common.c      |  28 --
 .../migrate_pages/migrate_pages_common.h      |   2 -
 3 files changed, 128 insertions(+), 215 deletions(-)

diff --git a/testcases/kernel/syscalls/migrate_pages/migrate_pages01.c b/testcases/kernel/syscalls/migrate_pages/migrate_pages01.c
index 428ca9a4..5112d6d6 100644
--- a/testcases/kernel/syscalls/migrate_pages/migrate_pages01.c
+++ b/testcases/kernel/syscalls/migrate_pages/migrate_pages01.c
@@ -1,252 +1,195 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (C) 2012 Linux Test Project, Inc.
+ * Copyright (c) 2026 Pradeep Susarla <pradeep.susarla@gmail.com>
+ */
+
+/*\
+ * Verify the errors returned by :manpage:`migrate_pages(2)`:
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it
- * is free of the rightful claim of any third person regarding
- * infringement or the like.  Any license provided herein, whether
- * implied or otherwise, applies only to this software file.  Patent
- * licenses, if any, provided herein do not apply to combinations of
- * this program with other software, or any other product whatsoever.
+ * - ESRCH when pid is -1 or no process with that pid exists
+ * - EINVAL when maxnode is invalid
+ * - EFAULT when old_nodes and new_nodes point to an invalid address,
+ *   a PROT_NONE mapping or an unmapped page
+ * - EINVAL when new_nodes contains a node that is not allowed
+ * - EPERM when an unprivileged process tries to migrate the pages of a
+ *   process owned by another user
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
+ * Also verify that migrating the pages of the calling process from a node
+ * to the same node succeeds.
  */
 
-/*
- * errno tests for migrate_pages() syscall
- */
-#include <sys/types.h>
-#include <sys/syscall.h>
-#include <sys/wait.h>
-#include <sys/mman.h>
 #include <errno.h>
-#include <stdio.h>
+#include <pwd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <pwd.h>
+#include <sys/mman.h>
 
-#include "test.h"
-#include "tso_safe_macros.h"
+#include "tst_test.h"
 #include "lapi/syscalls.h"
 #include "numa_helper.h"
 #include "migrate_pages_common.h"
 
-char *TCID = "migrate_pages01";
-int TST_TOTAL = 1;
-
-option_t options[] = {
-	{NULL, NULL, NULL}
-};
-
 #ifdef HAVE_NUMA_V2
 
 static unsigned long *sane_old_nodes;
 static unsigned long *sane_new_nodes;
-static int sane_nodemask_size;
-static int sane_max_node;
-
-static void setup(void);
-static void cleanup(void);
+static unsigned long *invalid_new_nodes;
+static unsigned long sane_max_node;
+static size_t sane_nodemask_size;
+static int invalid_node = -1;
+static int *nodes;
+static int num_nodes;
+static struct passwd *ltpuser;
+
+static long sys_migrate_pages(pid_t pid, unsigned long maxnode,
+			      const unsigned long *old_nodes,
+			      const unsigned long *new_nodes)
+{
+	return tst_syscall(__NR_migrate_pages, pid, maxnode, old_nodes,
+			   new_nodes);
+}
 
-static void test_sane_nodes(void)
+static void verify_sane_nodes(void)
 {
-	tst_resm(TINFO, "test_empty_mask");
-	TEST(tst_syscall(__NR_migrate_pages, 0, sane_max_node,
-		     sane_old_nodes, sane_new_nodes));
-	check_ret(0);
+	TST_EXP_PASS(sys_migrate_pages(0, sane_max_node, sane_old_nodes,
+				       sane_new_nodes),
+		     "migrate_pages() from node %d to node %d",
+		     nodes[0], nodes[0]);
 }
 
-static void test_invalid_pid(void)
+static void verify_invalid_pid(void)
 {
-	pid_t invalid_pid = -1;
-
-	tst_resm(TINFO, "test_invalid_pid -1");
-	TEST(tst_syscall(__NR_migrate_pages, invalid_pid, sane_max_node,
-		     sane_old_nodes, sane_new_nodes));
-	check_ret(-1);
-	check_errno(ESRCH);
-
-	tst_resm(TINFO, "test_invalid_pid unused pid");
-	invalid_pid = tst_get_unused_pid(cleanup);
-	TEST(tst_syscall(__NR_migrate_pages, invalid_pid, sane_max_node,
-		     sane_old_nodes, sane_new_nodes));
-	check_ret(-1);
-	check_errno(ESRCH);
+	pid_t unused_pid = tst_get_unused_pid();
+
+	TST_EXP_FAIL(sys_migrate_pages(-1, sane_max_node, sane_old_nodes,
+				       sane_new_nodes),
+		     ESRCH, "migrate_pages() with pid -1");
+
+	TST_EXP_FAIL(sys_migrate_pages(unused_pid, sane_max_node,
+				       sane_old_nodes, sane_new_nodes),
+		     ESRCH, "migrate_pages() with unused pid %d", unused_pid);
 }
 
-static void test_invalid_masksize(void)
+static void verify_invalid_masksize(void)
 {
-	tst_resm(TINFO, "test_invalid_masksize");
-	TEST(tst_syscall(__NR_migrate_pages, 0, -1, sane_old_nodes,
-		     sane_new_nodes));
-	check_ret(-1);
-	check_errno(EINVAL);
+	TST_EXP_FAIL(sys_migrate_pages(0, -1, sane_old_nodes, sane_new_nodes),
+		     EINVAL, "migrate_pages() with maxnode -1");
 }
 
-static void test_invalid_mem(void)
+static void verify_invalid_mem(void)
 {
 	unsigned long *p;
+	int pagesize = getpagesize();
+
+	TST_EXP_FAIL(sys_migrate_pages(0, sane_max_node, (void *)-1,
+				       (void *)-1),
+		     EFAULT, "migrate_pages() with invalid address");
+
+	p = SAFE_MMAP(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
+		      -1, 0);
+	TST_EXP_FAIL(sys_migrate_pages(0, sane_max_node, p, p),
+		     EFAULT, "migrate_pages() with PROT_NONE mapping");
 
-	tst_resm(TINFO, "test_invalid_mem -1");
-	TEST(tst_syscall(__NR_migrate_pages, 0, sane_max_node, -1, -1));
-	check_ret(-1);
-	check_errno(EFAULT);
-
-	tst_resm(TINFO, "test_invalid_mem invalid prot");
-	p = mmap(NULL, getpagesize(), PROT_NONE,
-		 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
-	if (p == MAP_FAILED)
-		tst_brkm(TBROK | TERRNO, cleanup, "mmap");
-	TEST(tst_syscall(__NR_migrate_pages, 0, sane_max_node, p, p));
-	check_ret(-1);
-	check_errno(EFAULT);
-
-	SAFE_MUNMAP(cleanup, p, getpagesize());
-	tst_resm(TINFO, "test_invalid_mem unmmaped");
-	TEST(tst_syscall(__NR_migrate_pages, 0, sane_max_node, p, p));
-	check_ret(-1);
-	check_errno(EFAULT);
+	SAFE_MUNMAP(p, pagesize);
+	TST_EXP_FAIL(sys_migrate_pages(0, sane_max_node, p, p),
+		     EFAULT, "migrate_pages() with unmapped page");
 }
 
-static void test_invalid_nodes(void)
+static void verify_invalid_nodes(void)
 {
-	int *nodes;
-	int num_nodes, ret, i;
-	int invalid_node = 0;
-	unsigned long *old_nodes, *new_nodes;
-
-	tst_resm(TINFO, "test_invalid_nodes");
-	ret = get_allowed_nodes_arr(NH_MEMS, &num_nodes, &nodes);
-	if (ret < 0)
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "get_allowed_nodes_arr: %d", ret);
-
-	/* get first node which is not in nodes */
-	for (i = 0; i < num_nodes; i++, invalid_node++)
-		if (invalid_node != nodes[i])
-			break;
-	if (invalid_node < sane_max_node) {
-		old_nodes = SAFE_MALLOC(NULL, sane_nodemask_size);
-		new_nodes = SAFE_MALLOC(NULL, sane_nodemask_size);
-		memcpy(old_nodes, sane_old_nodes, sane_nodemask_size);
-		memset(new_nodes, 0, sane_nodemask_size);
-		set_bit(new_nodes, invalid_node, 1);
-
-		TEST(tst_syscall(__NR_migrate_pages, 0, sane_max_node,
-			     old_nodes, new_nodes));
-		check_ret(-1);
-		check_errno(EINVAL);
-		free(old_nodes);
-		free(new_nodes);
-	} else {
-		tst_resm(TCONF, "All possible nodes are present");
+	if (invalid_node < 0) {
+		tst_res(TCONF, "All possible nodes are allowed");
+		return;
 	}
 
-	free(nodes);
+	TST_EXP_FAIL(sys_migrate_pages(0, sane_max_node, sane_old_nodes,
+				       invalid_new_nodes),
+		     EINVAL, "migrate_pages() to not allowed node %d",
+		     invalid_node);
 }
 
-static void test_invalid_perm(void)
+static void verify_invalid_perm(void)
 {
-	char nobody_uid[] = "nobody";
-	struct passwd *ltpuser;
-	int status;
-	pid_t child_pid;
-	pid_t parent_pid;
-	int ret = 0;
-
-	tst_resm(TINFO, "test_invalid_perm");
-	parent_pid = getpid();
-	fflush(stdout);
-	child_pid = fork();
-	switch (child_pid) {
-	case -1:
-		tst_brkm(TBROK | TERRNO, cleanup, "fork");
-		break;
-	case 0:
-		ltpuser = getpwnam(nobody_uid);
-		if (ltpuser == NULL)
-			tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
-		SAFE_SETUID(NULL, ltpuser->pw_uid);
-		TEST(tst_syscall(__NR_migrate_pages, parent_pid,
-			     sane_max_node, sane_old_nodes, sane_new_nodes));
-		ret |= check_ret(-1);
-		ret |= check_errno(EPERM);
-		exit(ret);
-	default:
-		SAFE_WAITPID(cleanup, child_pid, &status, 0);
-		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
-			tst_resm(TFAIL, "child returns %d", status);
+	pid_t parent_pid = getpid();
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(ltpuser->pw_uid);
+		TST_EXP_FAIL(sys_migrate_pages(parent_pid, sane_max_node,
+					       sane_old_nodes, sane_new_nodes),
+			     EPERM, "migrate_pages() of root process as %s",
+			     ltpuser->pw_name);
+		exit(0);
 	}
+
+	tst_reap_children();
 }
 
-int main(int argc, char *argv[])
+static void run(void)
 {
-	int lc;
-
-	tst_parse_opts(argc, argv, options, NULL);
-
-	setup();
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		test_sane_nodes();
-		test_invalid_pid();
-		test_invalid_masksize();
-		test_invalid_mem();
-		test_invalid_nodes();
-		test_invalid_perm();
-	}
-	cleanup();
-	tst_exit();
+	verify_sane_nodes();
+	verify_invalid_pid();
+	verify_invalid_masksize();
+	verify_invalid_mem();
+	verify_invalid_nodes();
+	verify_invalid_perm();
 }
 
 static void setup(void)
 {
-	int node, ret;
+	int i, node = 0;
 
-	tst_require_root();
-	TEST(tst_syscall(__NR_migrate_pages, 0, 0, NULL, NULL));
+	tst_syscall(__NR_migrate_pages, 0, 0, NULL, NULL);
 
-	if (!is_numa(NULL, NH_MEMS, 1))
-		tst_brkm(TCONF, NULL, "requires NUMA with at least 1 node");
+	if (get_allowed_nodes_arr(NH_MEMS, &num_nodes, &nodes) < 0)
+		tst_brk(TBROK | TERRNO, "get_allowed_nodes_arr() failed");
 
-	ret = get_allowed_nodes(NH_MEMS, 1, &node);
-	if (ret < 0)
-		tst_brkm(TBROK | TERRNO, NULL, "get_allowed_nodes_arr: %d",
-			 ret);
+	if (num_nodes < 1)
+		tst_brk(TCONF, "requires NUMA with at least 1 node");
 
-	sane_max_node = LTP_ALIGN(get_max_node(), sizeof(unsigned long)*8);
+	sane_max_node = LTP_ALIGN(get_max_node(), sizeof(unsigned long) * 8);
 	sane_nodemask_size = sane_max_node / 8;
-	sane_old_nodes = SAFE_MALLOC(NULL, sane_nodemask_size);
-	sane_new_nodes = SAFE_MALLOC(NULL, sane_nodemask_size);
+
+	sane_old_nodes = SAFE_MALLOC(sane_nodemask_size);
+	sane_new_nodes = SAFE_MALLOC(sane_nodemask_size);
 	memset(sane_old_nodes, 0, sane_nodemask_size);
 	memset(sane_new_nodes, 0, sane_nodemask_size);
+	set_bit(sane_old_nodes, nodes[0], 1);
+	set_bit(sane_new_nodes, nodes[0], 1);
 
-	set_bit(sane_old_nodes, node, 1);
-	set_bit(sane_new_nodes, node, 1);
+	/* find the first node that is not allowed */
+	for (i = 0; i < num_nodes; i++, node++) {
+		if (node != nodes[i])
+			break;
+	}
+
+	if ((unsigned long)node < sane_max_node) {
+		invalid_node = node;
+		invalid_new_nodes = SAFE_MALLOC(sane_nodemask_size);
+		memset(invalid_new_nodes, 0, sane_nodemask_size);
+		set_bit(invalid_new_nodes, invalid_node, 1);
+	}
 
-	TEST_PAUSE;
+	ltpuser = SAFE_GETPWNAM("nobody");
 }
 
 static void cleanup(void)
 {
+	free(invalid_new_nodes);
 	free(sane_old_nodes);
 	free(sane_new_nodes);
+	free(nodes);
 }
 
+static struct tst_test test = {
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run,
+};
+
 #else
-int main(void)
-{
-	tst_brkm(TCONF, NULL, NUMA_ERROR_MSG);
-}
+TST_TEST_TCONF(NUMA_ERROR_MSG);
 #endif
diff --git a/testcases/kernel/syscalls/migrate_pages/migrate_pages_common.c b/testcases/kernel/syscalls/migrate_pages/migrate_pages_common.c
index 49f61949..7f6aeea7 100644
--- a/testcases/kernel/syscalls/migrate_pages/migrate_pages_common.c
+++ b/testcases/kernel/syscalls/migrate_pages/migrate_pages_common.c
@@ -22,7 +22,6 @@
  * 02110-1301, USA.
  */
 
-#include "test.h"
 #include "migrate_pages_common.h"
 
 void set_bit(unsigned long *b, unsigned int n, unsigned int v)
@@ -32,30 +31,3 @@ void set_bit(unsigned long *b, unsigned int n, unsigned int v)
 	else
 		b[n / bitsperlong] &= ~(1UL << (n % bitsperlong));
 }
-
-int check_ret(long expected_ret)
-{
-	if (expected_ret == TEST_RETURN) {
-		tst_resm(TPASS, "expected ret success: "
-			 "returned value = %ld", TEST_RETURN);
-		return 0;
-	} else
-		tst_resm(TFAIL, "unexpected failure - "
-			 "returned value = %ld, expected: %ld",
-			 TEST_RETURN, expected_ret);
-	return 1;
-}
-
-int check_errno(long expected_errno)
-{
-	if (TEST_ERRNO == expected_errno) {
-		tst_resm(TPASS | TTERRNO, "expected failure");
-		return 0;
-	} else if (TEST_ERRNO == 0)
-		tst_resm(TFAIL, "call succeeded unexpectedly");
-	else
-		tst_resm(TFAIL | TTERRNO, "unexpected failure - "
-			 "expected = %ld : %s, actual",
-			 expected_errno, strerror(expected_errno));
-	return 1;
-}
diff --git a/testcases/kernel/syscalls/migrate_pages/migrate_pages_common.h b/testcases/kernel/syscalls/migrate_pages/migrate_pages_common.h
index d821b07b..b9d15a55 100644
--- a/testcases/kernel/syscalls/migrate_pages/migrate_pages_common.h
+++ b/testcases/kernel/syscalls/migrate_pages/migrate_pages_common.h
@@ -28,6 +28,4 @@
 #define bitsperlong (8 * sizeof(unsigned long))
 
 void set_bit(unsigned long *b, unsigned int n, unsigned int v);
-int check_ret(long expected_ret);
-int check_errno(long expected_errno);
 #endif
-- 
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

             reply	other threads:[~2026-11-24 13:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 13:12 Pradeep Susarla [this message]
2026-11-24 16:29 ` [LTP] migrate_pages01: Convert to new API linuxtestproject.agent

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124131237.53712-1-pradeep.susarla@gmail.com \
    --to=pradeep.susarla@gmail.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox