Skip to contentSkip to conversation

syscalls/get_mempolicy01: Rewrite to new API

PATCH Superseded Petr Vorel 2 participants 5 messages Dec 8, 2020 – Jan 6, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH 1/1 syscalls/get_mempolicy01: Rewrite to new API

Also finally remove common_j_h.c and include_j_h.h, which are no longer needed.

---
Hi,

not sure if invalid address is no longer meaningful (after removing fix for ia64 and use NULL).

I also removed check for __NR_get_mempolicy. Originally added in LTP in 07e750af6 (2012), but that was added in kernel before 2.6.12-rc2.

Kind regards,
Petr

 .../syscalls/get_mempolicy/get_mempolicy01.c  | 489 ++++++++----------
 testcases/kernel/syscalls/utils/common_j_h.c  | 260 ----------
 testcases/kernel/syscalls/utils/include_j_h.h | 143 -----
 3 files changed, 216 insertions(+), 676 deletions(-)
 delete mode 100644 testcases/kernel/syscalls/utils/common_j_h.c
 delete mode 100644 testcases/kernel/syscalls/utils/include_j_h.h
testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +216 -273
index fcb677986..d530e2bad 100644
@@ -1,320 +1,263 @@
-/******************************************************************************/
-/* Copyright (c) Crackerjack Project., 2007-2008 ,Hitachi, Ltd		      */
-/*	Author(s): Takahiro Yasui <[email protected]>,	      */
-/*		   Yumiko Sugita <[email protected]>,	      */
-/*		   Satoshi Fujiwara <[email protected]>		      */
-/*									      */
-/* This program is free software;  you can redistribute it and/or modify      */
-/* it under the terms of the GNU General Public License as published by	      */
-/* the Free Software Foundation; either version 2 of the License, or	      */
-/* (at your option) any later version.					      */
-/*									      */
-/* This program is distributed in the hope that it will be useful,	      */
-/* but WITHOUT ANY WARRANTY;  without even the implied warranty of	      */
-/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See		      */
-/* the GNU General Public License for more details.			      */
-/*									      */
-/* You should have received a copy of the GNU General Public License	      */
-/* along with this program;  if not, write to the Free Software		      */
-/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
-/*									      */
-/******************************************************************************/
-/******************************************************************************/
-/*									      */
-/* File:	get_mempolicy01.c					      */
-/*									      */
-/* Description: This tests the get_mempolicy() syscall			      */
-/*									      */
-/* Usage:  <for command-line>						      */
-/* get_mempolicy01 [-c n] [-e][-i n] [-I x] [-p x] [-t]			      */
-/*	where,	-c n : Run n copies concurrently.			      */
-/*		-e   : Turn on errno logging.				      */
-/*		-i n : Execute test n times.				      */
-/*		-I x : Execute test for x seconds.			      */
-/*		-P x : Pause for x seconds between iterations.		      */
-/*		-t   : Turn on syscall timing.				      */
-/*									      */
-/* Total Tests: 1							      */
-/*									      */
-/* Test Name:	get_mempolicy01						      */
-/* History:	Porting from Crackerjack to LTP is done by		      */
-/*		Manas Kumar Nayak [email protected]>			      */
-/******************************************************************************/
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Petr Vorel <[email protected]>
+ * Copyright (c) Linux Test Project, 2009-2020
+ * Copyright (c) Crackerjack Project, 2007-2008, Hitachi, Ltd
+ *
+ * Authors:
+ * Takahiro Yasui <[email protected]>
+ * Yumiko Sugita <[email protected]>
+ * Satoshi Fujiwara <[email protected]>
+ * Manas Kumar Nayak <[email protected]> (original port to the legacy API)
+ */
 
 #include "config.h"
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <getopt.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <libgen.h>
-#if HAVE_NUMA_H
+#include "tst_test.h"
+
+#ifdef HAVE_NUMA_V2
 #include <numa.h>
-#endif
-#if HAVE_NUMAIF_H
 #include <numaif.h>
-#endif
 
-#include "test.h"
-#include "lapi/syscalls.h"
-#include "include_j_h.h"
-#include "common_j_h.c"
+#include <errno.h>
 #include "numa_helper.h"
-
-char *TCID = "get_mempolicy01";
-int TST_TOTAL = 1;
-
-#ifdef HAVE_NUMA_V2
+#include "tst_numa.h"
+#include "lapi/syscalls.h"
 
 #define MEM_LENGTH	(4 * 1024 * 1024)
 
-static int testno;
+#define POLICY_DESC(x) .policy = x, .desc = "policy: "#x
+#define POLICY_DESC_TEXT(x, y) .policy = x, .desc = "policy: "#x", "y
+#define POLICY_DESC_FLAGS(x, y) .policy = x, .flags = y, .desc = "policy: "#x", flags: "#y
+#define POLICY_DESC_FLAGS_TEXT(x, y, z) .policy = x, .flags = y, .desc = "policy: "#x", flags: "#y", "z
 
-enum test_type {
-	DEFAULT,		/* get default policy */
-	ADDR,			/* get policy of memory which include mapped address */
-	INVALID_POINTER,
-	INVALID_FLAGS,
-};
+static struct bitmask *nodemask, *getnodemask, *empty_nodemask;
 
-enum from_node {
-	NONE,
-	SELF,
-};
+static void test_set_mempolicy_default(unsigned int i);
+static void test_set_mempolicy_none(unsigned int i);
+static int test_mbind_none(unsigned int i);
+static int test_mbind_default(unsigned int i);
+static int set_invalid_addr(unsigned int i);
 
 struct test_case {
-	int ttype;
 	int policy;
-	int from_node;
+	const char *desc;
+	unsigned int flags;
 	int ret;
 	int err;
+	char *addr;
+	int (*pre_test)(unsigned int i);
+	void (*test)(unsigned int i);
+	struct bitmask **exp_nodemask;
 };
 
-/* Test cases
- *
- *   test status of errors on man page
- *
- *   (NONE)	     man page hadn't been completed.
- *
- *   test status of errors NOT on man page
- *
- *   EFAULT	     v (invalid address)
- *   EINVAL	     v (invalid parameters)
- */
 static struct test_case tcase[] = {
-	{			/* case00 */
-	 .ttype = DEFAULT,
-	 .policy = MPOL_DEFAULT,
-	 .from_node = NONE,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case01 */
-	 .ttype = DEFAULT,
-	 .policy = MPOL_BIND,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case02 */
-	 .ttype = DEFAULT,
-	 .policy = MPOL_INTERLEAVE,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case03 */
-	 .ttype = DEFAULT,
-	 .policy = MPOL_PREFERRED,
-	 .from_node = NONE,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case04 */
-	 .ttype = DEFAULT,
-	 .policy = MPOL_PREFERRED,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case05 */
-	 .ttype = ADDR,
-	 .policy = MPOL_DEFAULT,
-	 .from_node = NONE,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case06 */
-	 .ttype = ADDR,
-	 .policy = MPOL_BIND,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case07 */
-	 .ttype = ADDR,
-	 .policy = MPOL_INTERLEAVE,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case08 */
-	 .ttype = ADDR,
-	 .policy = MPOL_PREFERRED,
-	 .from_node = NONE,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case09 */
-	 .ttype = ADDR,
-	 .policy = MPOL_PREFERRED,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case10 */
-	 .ttype = INVALID_POINTER,
-	 .policy = MPOL_DEFAULT,
-	 .from_node = NONE,
-	 .ret = -1,
-	 .err = EFAULT,
-	 },
-	{			/* case11 */
-	 .ttype = INVALID_FLAGS,
-	 .policy = MPOL_DEFAULT,
-	 .from_node = NONE,
-	 .ret = -1,
-	 .err = EINVAL,
-	 },
+	{
+		POLICY_DESC_TEXT(MPOL_DEFAULT, "no target"),
+		.ret = 0,
+		.err = 0,
+		.test = test_set_mempolicy_none,
+		.exp_nodemask = &empty_nodemask,
+	},
+	{
+		POLICY_DESC(MPOL_BIND),
+		.ret = 0,
+		.err = 0,
+		.test = test_set_mempolicy_default,
+	},
+	{
+		POLICY_DESC(MPOL_INTERLEAVE),
+		.ret = 0,
+		.err = 0,
+		.test = test_set_mempolicy_default,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		POLICY_DESC_TEXT(MPOL_PREFERRED, "no target"),
+		.ret = 0,
+		.err = 0,
+		.test = test_set_mempolicy_none,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		.test = test_set_mempolicy_default,
+		POLICY_DESC(MPOL_PREFERRED),
+		.ret = 0,
+		.err = 0,
+	},
+	{
+		POLICY_DESC_FLAGS_TEXT(MPOL_DEFAULT, MPOL_F_ADDR, "no target"),
+		.ret = 0,
+		.err = 0,
+		.pre_test = test_mbind_none,
+		.test = test_set_mempolicy_none,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		POLICY_DESC_FLAGS(MPOL_BIND, MPOL_F_ADDR),
+		.pre_test = test_mbind_default,
+		.ret = 0,
+		.err = 0,
+	},
+	{
+		POLICY_DESC_FLAGS(MPOL_INTERLEAVE, MPOL_F_ADDR),
+		.ret = 0,
+		.err = 0,
+		.pre_test = test_mbind_default,
+		.test = test_set_mempolicy_default,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		POLICY_DESC_FLAGS_TEXT(MPOL_PREFERRED, MPOL_F_ADDR, "no target"),
+		.ret = 0,
+		.err = 0,
+		.pre_test = test_mbind_none,
+		.test = test_set_mempolicy_none,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		POLICY_DESC_FLAGS(MPOL_PREFERRED, MPOL_F_ADDR),
+		.ret = 0,
+		.err = 0,
+		.pre_test = test_mbind_default,
+		.test = test_set_mempolicy_default,
+	},
+	{
+		.flags = MPOL_F_ADDR,
+		POLICY_DESC_TEXT(MPOL_DEFAULT, "invalid address"),
+		.ret = -1,
+		.err = EFAULT,
+		.pre_test = set_invalid_addr,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		POLICY_DESC_TEXT(MPOL_DEFAULT, "invalid flags, no target"),
+		.flags = -1,
+		.ret = -1,
+		.err = EINVAL,
+		.test = test_set_mempolicy_none,
+		.exp_nodemask = &nodemask,
+	},
 };
 
-static int do_test(struct test_case *tc);
-static void setup(void);
-static void cleanup(void);
+static void test_set_mempolicy_default(unsigned int i)
+{
+	struct test_case *tc = &tcase[i];
+
+	TEST(set_mempolicy(tc->policy, nodemask->maskp, nodemask->size));
+}
 
-int main(int argc, char **argv)
+static void test_set_mempolicy_none(unsigned int i)
 {
-	int i, ret, lc;
+	struct test_case *tc = &tcase[i];
 
-	setup();
+	TEST(set_mempolicy(tc->policy, NULL, 0));
+}
+
+static int test_mbind(unsigned int i, unsigned long *maskp, unsigned long size)
+{
+	struct test_case *tc = &tcase[i];
 
-	ret = 0;
-	testno = (int)ARRAY_SIZE(tcase);
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	tc->addr = SAFE_MMAP(NULL, MEM_LENGTH, PROT_READ | PROT_WRITE,
+			     MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
 
-		for (i = 0; i < testno; i++) {
-			tst_resm(TINFO, "(case%02d) START", i);
-			ret = do_test(&tcase[i]);
-			tst_resm((ret == 0 ? TPASS : TFAIL | TERRNO),
-				 "(case%02d) END", i);
-		}
+	TEST(mbind(tc->addr, MEM_LENGTH, tc->policy, maskp, size, 0));
+
+	if (TST_RET < 0) {
+		tst_res(TFAIL | TERRNO, "mbind() failed");
+		return -1;
 	}
 
-	cleanup();
-	tst_exit();
+	return 0;
 }
 
-static int do_test(struct test_case *tc)
+static int test_mbind_none(unsigned int i)
 {
-	int ret, err, result, cmp_ok;
-	int policy, flags;
-	struct bitmask *nodemask = numa_allocate_nodemask();
-	struct bitmask *getnodemask = numa_allocate_nodemask();
-	char *p = NULL;
-	unsigned long len = MEM_LENGTH;
-	int test_node = -1;
+	return test_mbind(i, NULL, 0);
+}
 
-	ret = get_allowed_nodes(NH_MEMS, 1, &test_node);
-	if (ret < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "get_allowed_nodes: %d", ret);
-	numa_bitmask_setbit(nodemask, test_node);
-	switch (tc->ttype) {
-	case DEFAULT:
-		flags = 0;
-		p = NULL;
-		if (tc->from_node == NONE)
-			TEST(ltp_syscall(__NR_set_mempolicy, tc->policy,
-				NULL, 0));
-		else
-			TEST(ltp_syscall(__NR_set_mempolicy, tc->policy,
-				nodemask->maskp, nodemask->size));
-		if (TEST_RETURN < 0) {
-			tst_resm(TBROK | TERRNO, "set_mempolicy");
-			return -1;
-		}
+static int test_mbind_default(unsigned int i)
+{
+	return test_mbind(i, nodemask->maskp, nodemask->size);
+}
 
-		break;
-	default:
-		flags = MPOL_F_ADDR;
-		p = mmap(NULL, len, PROT_READ | PROT_WRITE,
-			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
-		if (p == MAP_FAILED)
-			tst_brkm(TBROK | TERRNO, cleanup, "mmap");
-		if (tc->from_node == NONE)
-			TEST(ltp_syscall(__NR_mbind, p, len, tc->policy,
-				NULL, 0, 0));
-		else
-			TEST(ltp_syscall(__NR_mbind, p, len, tc->policy,
-				nodemask->maskp, nodemask->size, 0));
-		if (TEST_RETURN < 0) {
-			tst_resm(TBROK | TERRNO, "mbind");
-			return -1;
-		}
+static int set_invalid_addr(unsigned int i)
+{
+	struct test_case *tc = &tcase[i];
+	int ret;
 
-		if (tc->ttype == INVALID_POINTER)
-#ifdef __ia64__
-			p = (char *)0x8000000000000000UL;
-#else
-			p = 0;
-#endif
+	ret = test_mbind_none(i);
+	tc->addr = NULL;
 
-		if (tc->ttype == INVALID_FLAGS)
-			flags = -1;
-	}
-	errno = 0;
-	cmp_ok = 1;
-	TEST(ret = ltp_syscall(__NR_get_mempolicy, &policy, getnodemask->maskp,
-			   getnodemask->size, p, flags));
-	err = TEST_ERRNO;
-	if (ret < 0)
-		goto TEST_END;
-
-	/* if policy == MPOL_DEFAULT, get_mempolicy doesn't return nodemask */
-	if (tc->policy == MPOL_DEFAULT)
-		numa_bitmask_clearall(nodemask);
-	cmp_ok = (tc->policy == policy && (tc->from_node == NONE ||
-					   numa_bitmask_equal(nodemask,
-							      getnodemask)));
-TEST_END:
-	result = (err != tc->err) || !cmp_ok;
-	PRINT_RESULT_CMP(0, tc->ret, tc->err, ret, err, cmp_ok);
-	return result;
+	return ret;
 }
 
-static void cleanup(void)
+
+static void setup(void)
 {
-	tst_rmdir();
+	if (!is_numa(NULL, NH_MEMS, 1))
+		tst_brk(TCONF, "requires NUMA with at least 1 node");
 }
 
-static void setup(void)
+static void setup_node(void)
 {
-	/* check syscall availability */
-	ltp_syscall(__NR_get_mempolicy, NULL, NULL, 0, NULL, 0);
+	int test_node = -1;
 
-	if (!is_numa(NULL, NH_MEMS, 1))
-		tst_brkm(TCONF, NULL, "requires NUMA with at least 1 node");
+	if (get_allowed_nodes(NH_MEMS, 1, &test_node) < 0)
+		tst_brk(TBROK | TERRNO, "get_allowed_nodes failed");
 
-	TEST_PAUSE;
-	tst_tmpdir();
+	nodemask = numa_allocate_nodemask();
+	getnodemask = numa_allocate_nodemask();
+	numa_bitmask_setbit(nodemask, test_node);
 }
 
-#else
-int main(void)
+static void do_test(unsigned int i)
 {
-	tst_brkm(TCONF, NULL, NUMA_ERROR_MSG);
+	struct test_case *tc = &tcase[i];
+	int policy;
+
+	tst_res(TINFO, "test #%d: %s", (i+1), tc->desc);
+
+	setup_node();
+
+	if (tc->pre_test)
+		if (tc->pre_test(i) == -1)
+			return;
+
+	if (tc->test) {
+		tc->test(i);
+
+		if (TST_RET < 0) {
+			tst_res(TFAIL | TERRNO, ".test failed");
+			return;
+		}
+	}
+
+	TEST(get_mempolicy(&policy, getnodemask->maskp, getnodemask->size,
+			   tc->addr, tc->flags));
+
+	if (TST_RET == -1 && tc->policy == MPOL_DEFAULT)
+		numa_bitmask_clearall(nodemask);
+
+	if (tc->ret != TST_RET) {
+		tst_res(TFAIL, "wrong return code: %ld, expected: %d",
+			TST_RET, tc->ret);
+		return;
+	}
+	if (TST_RET == -1 && TST_ERR != tc->err) {
+		tst_res(TFAIL | TTERRNO, "expected errno: %s, got",
+			tst_strerrno(tc->err));
+		return;
+	}
+
+	tst_res(TPASS, "Test passed");
 }
-#endif
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcase),
+	.test = do_test,
+	.setup = setup,
+};
+
+#else
+TST_TEST_TCONF(NUMA_ERROR_MSG);
+#endif /* HAVE_NUMA_V2 */
testcases/kernel/syscalls/utils/common_j_h.c deleted +0 -260

260 lines not on this page · raw message

testcases/kernel/syscalls/utils/include_j_h.h deleted +0 -143

143 lines not on this page · raw message

Signature · 1 line
2.29.2
author
#1 lore raw

PATCH 1/1 syscalls/get_mempolicy01: Rewrite to new API

Hi,

Quoted 2 lines

not sure if invalid address is no longer meaningful (after removing fix for ia64 and use NULL).

FYI tst_get_bad_addr(), which would be more portable, didn't work. Test is designed to use NULL to get EFAULT.

Kind regards,
Petr

#2 lore raw

PATCH 1/1 syscalls/get_mempolicy01: Rewrite to new API

In reply to Petr Vorel

Hi!

testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c line 12

Quoted 12 lines
testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +12 -0
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Petr Vorel <[email protected]>
+ * Copyright (c) Linux Test Project, 2009-2020
+ * Copyright (c) Crackerjack Project, 2007-2008, Hitachi, Ltd
+ *
+ * Authors:
+ * Takahiro Yasui <[email protected]>
+ * Yumiko Sugita <[email protected]>
+ * Satoshi Fujiwara <[email protected]>
+ * Manas Kumar Nayak <[email protected]> (original port to the legacy API)
+ */

Missing test description?

Quoted 408 lines

#include "config.h"

testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +148 -221
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <getopt.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <libgen.h>
-#if HAVE_NUMA_H
+#include "tst_test.h"
+
+#ifdef HAVE_NUMA_V2
 #include <numa.h>
-#endif
-#if HAVE_NUMAIF_H
 #include <numaif.h>
-#endif
 
-#include "test.h"
-#include "lapi/syscalls.h"
-#include "include_j_h.h"
-#include "common_j_h.c"
+#include <errno.h>
 #include "numa_helper.h"
-
-char *TCID = "get_mempolicy01";
-int TST_TOTAL = 1;
-
-#ifdef HAVE_NUMA_V2
+#include "tst_numa.h"
+#include "lapi/syscalls.h"
 
 #define MEM_LENGTH	(4 * 1024 * 1024)
 
-static int testno;
+#define POLICY_DESC(x) .policy = x, .desc = "policy: "#x
+#define POLICY_DESC_TEXT(x, y) .policy = x, .desc = "policy: "#x", "y
+#define POLICY_DESC_FLAGS(x, y) .policy = x, .flags = y, .desc = "policy: "#x", flags: "#y
+#define POLICY_DESC_FLAGS_TEXT(x, y, z) .policy = x, .flags = y, .desc = "policy: "#x", flags: "#y", "z
 
-enum test_type {
-	DEFAULT,		/* get default policy */
-	ADDR,			/* get policy of memory which include mapped address */
-	INVALID_POINTER,
-	INVALID_FLAGS,
-};
+static struct bitmask *nodemask, *getnodemask, *empty_nodemask;
 
-enum from_node {
-	NONE,
-	SELF,
-};
+static void test_set_mempolicy_default(unsigned int i);
+static void test_set_mempolicy_none(unsigned int i);
+static int test_mbind_none(unsigned int i);
+static int test_mbind_default(unsigned int i);
+static int set_invalid_addr(unsigned int i);
 
 struct test_case {
-	int ttype;
 	int policy;
-	int from_node;
+	const char *desc;
+	unsigned int flags;
 	int ret;
 	int err;
+	char *addr;
+	int (*pre_test)(unsigned int i);
+	void (*test)(unsigned int i);
+	struct bitmask **exp_nodemask;
 };
 
-/* Test cases
- *
- *   test status of errors on man page
- *
- *   (NONE)	     man page hadn't been completed.
- *
- *   test status of errors NOT on man page
- *
- *   EFAULT	     v (invalid address)
- *   EINVAL	     v (invalid parameters)
- */
 static struct test_case tcase[] = {
-	{			/* case00 */
-	 .ttype = DEFAULT,
-	 .policy = MPOL_DEFAULT,
-	 .from_node = NONE,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case01 */
-	 .ttype = DEFAULT,
-	 .policy = MPOL_BIND,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case02 */
-	 .ttype = DEFAULT,
-	 .policy = MPOL_INTERLEAVE,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case03 */
-	 .ttype = DEFAULT,
-	 .policy = MPOL_PREFERRED,
-	 .from_node = NONE,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case04 */
-	 .ttype = DEFAULT,
-	 .policy = MPOL_PREFERRED,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case05 */
-	 .ttype = ADDR,
-	 .policy = MPOL_DEFAULT,
-	 .from_node = NONE,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case06 */
-	 .ttype = ADDR,
-	 .policy = MPOL_BIND,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case07 */
-	 .ttype = ADDR,
-	 .policy = MPOL_INTERLEAVE,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case08 */
-	 .ttype = ADDR,
-	 .policy = MPOL_PREFERRED,
-	 .from_node = NONE,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case09 */
-	 .ttype = ADDR,
-	 .policy = MPOL_PREFERRED,
-	 .from_node = SELF,
-	 .ret = 0,
-	 .err = 0,
-	 },
-	{			/* case10 */
-	 .ttype = INVALID_POINTER,
-	 .policy = MPOL_DEFAULT,
-	 .from_node = NONE,
-	 .ret = -1,
-	 .err = EFAULT,
-	 },
-	{			/* case11 */
-	 .ttype = INVALID_FLAGS,
-	 .policy = MPOL_DEFAULT,
-	 .from_node = NONE,
-	 .ret = -1,
-	 .err = EINVAL,
-	 },
+	{
+		POLICY_DESC_TEXT(MPOL_DEFAULT, "no target"),
+		.ret = 0,
+		.err = 0,
+		.test = test_set_mempolicy_none,
+		.exp_nodemask = &empty_nodemask,
+	},
+	{
+		POLICY_DESC(MPOL_BIND),
+		.ret = 0,
+		.err = 0,
+		.test = test_set_mempolicy_default,
+	},
+	{
+		POLICY_DESC(MPOL_INTERLEAVE),
+		.ret = 0,
+		.err = 0,
+		.test = test_set_mempolicy_default,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		POLICY_DESC_TEXT(MPOL_PREFERRED, "no target"),
+		.ret = 0,
+		.err = 0,
+		.test = test_set_mempolicy_none,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		.test = test_set_mempolicy_default,
+		POLICY_DESC(MPOL_PREFERRED),
+		.ret = 0,
+		.err = 0,
+	},
+	{
+		POLICY_DESC_FLAGS_TEXT(MPOL_DEFAULT, MPOL_F_ADDR, "no target"),
+		.ret = 0,
+		.err = 0,
+		.pre_test = test_mbind_none,
+		.test = test_set_mempolicy_none,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		POLICY_DESC_FLAGS(MPOL_BIND, MPOL_F_ADDR),
+		.pre_test = test_mbind_default,
+		.ret = 0,
+		.err = 0,
+	},
+	{
+		POLICY_DESC_FLAGS(MPOL_INTERLEAVE, MPOL_F_ADDR),
+		.ret = 0,
+		.err = 0,
+		.pre_test = test_mbind_default,
+		.test = test_set_mempolicy_default,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		POLICY_DESC_FLAGS_TEXT(MPOL_PREFERRED, MPOL_F_ADDR, "no target"),
+		.ret = 0,
+		.err = 0,
+		.pre_test = test_mbind_none,
+		.test = test_set_mempolicy_none,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		POLICY_DESC_FLAGS(MPOL_PREFERRED, MPOL_F_ADDR),
+		.ret = 0,
+		.err = 0,
+		.pre_test = test_mbind_default,
+		.test = test_set_mempolicy_default,
+	},
+	{
+		.flags = MPOL_F_ADDR,
+		POLICY_DESC_TEXT(MPOL_DEFAULT, "invalid address"),
+		.ret = -1,
+		.err = EFAULT,
+		.pre_test = set_invalid_addr,
+		.exp_nodemask = &nodemask,
+	},
+	{
+		POLICY_DESC_TEXT(MPOL_DEFAULT, "invalid flags, no target"),
+		.flags = -1,
+		.ret = -1,
+		.err = EINVAL,
+		.test = test_set_mempolicy_none,
+		.exp_nodemask = &nodemask,
+	},
 };
 
-static int do_test(struct test_case *tc);
-static void setup(void);
-static void cleanup(void);
+static void test_set_mempolicy_default(unsigned int i)
+{
+	struct test_case *tc = &tcase[i];
+
+	TEST(set_mempolicy(tc->policy, nodemask->maskp, nodemask->size));
+}
 
-int main(int argc, char **argv)
+static void test_set_mempolicy_none(unsigned int i)
 {
-	int i, ret, lc;
+	struct test_case *tc = &tcase[i];
 
-	setup();
+	TEST(set_mempolicy(tc->policy, NULL, 0));
+}
+
+static int test_mbind(unsigned int i, unsigned long *maskp, unsigned long size)
+{
+	struct test_case *tc = &tcase[i];
 
-	ret = 0;
-	testno = (int)ARRAY_SIZE(tcase);
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	tc->addr = SAFE_MMAP(NULL, MEM_LENGTH, PROT_READ | PROT_WRITE,
+			     MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
 
-		for (i = 0; i < testno; i++) {
-			tst_resm(TINFO, "(case%02d) START", i);
-			ret = do_test(&tcase[i]);
-			tst_resm((ret == 0 ? TPASS : TFAIL | TERRNO),
-				 "(case%02d) END", i);
-		}
+	TEST(mbind(tc->addr, MEM_LENGTH, tc->policy, maskp, size, 0));
+
+	if (TST_RET < 0) {
+		tst_res(TFAIL | TERRNO, "mbind() failed");
+		return -1;
 	}
 
-	cleanup();
-	tst_exit();
+	return 0;
 }
 
-static int do_test(struct test_case *tc)
+static int test_mbind_none(unsigned int i)
 {
-	int ret, err, result, cmp_ok;
-	int policy, flags;
-	struct bitmask *nodemask = numa_allocate_nodemask();
-	struct bitmask *getnodemask = numa_allocate_nodemask();
-	char *p = NULL;
-	unsigned long len = MEM_LENGTH;
-	int test_node = -1;
+	return test_mbind(i, NULL, 0);
+}
 
-	ret = get_allowed_nodes(NH_MEMS, 1, &test_node);
-	if (ret < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "get_allowed_nodes: %d", ret);
-	numa_bitmask_setbit(nodemask, test_node);
-	switch (tc->ttype) {
-	case DEFAULT:
-		flags = 0;
-		p = NULL;
-		if (tc->from_node == NONE)
-			TEST(ltp_syscall(__NR_set_mempolicy, tc->policy,
-				NULL, 0));
-		else
-			TEST(ltp_syscall(__NR_set_mempolicy, tc->policy,
-				nodemask->maskp, nodemask->size));
-		if (TEST_RETURN < 0) {
-			tst_resm(TBROK | TERRNO, "set_mempolicy");
-			return -1;
-		}
+static int test_mbind_default(unsigned int i)
+{
+	return test_mbind(i, nodemask->maskp, nodemask->size);
+}
 
-		break;
-	default:
-		flags = MPOL_F_ADDR;
-		p = mmap(NULL, len, PROT_READ | PROT_WRITE,
-			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
-		if (p == MAP_FAILED)
-			tst_brkm(TBROK | TERRNO, cleanup, "mmap");
-		if (tc->from_node == NONE)
-			TEST(ltp_syscall(__NR_mbind, p, len, tc->policy,
-				NULL, 0, 0));
-		else
-			TEST(ltp_syscall(__NR_mbind, p, len, tc->policy,
-				nodemask->maskp, nodemask->size, 0));
-		if (TEST_RETURN < 0) {
-			tst_resm(TBROK | TERRNO, "mbind");
-			return -1;
-		}
+static int set_invalid_addr(unsigned int i)
+{
+	struct test_case *tc = &tcase[i];
+	int ret;
 
-		if (tc->ttype == INVALID_POINTER)
-#ifdef __ia64__
-			p = (char *)0x8000000000000000UL;
-#else
-			p = 0;
-#endif
+	ret = test_mbind_none(i);
+	tc->addr = NULL;
 
-		if (tc->ttype == INVALID_FLAGS)
-			flags = -1;
-	}
-	errno = 0;
-	cmp_ok = 1;
-	TEST(ret = ltp_syscall(__NR_get_mempolicy, &policy, getnodemask->maskp,
-			   getnodemask->size, p, flags));
-	err = TEST_ERRNO;
-	if (ret < 0)
-		goto TEST_END;
-
-	/* if policy == MPOL_DEFAULT, get_mempolicy doesn't return nodemask */
-	if (tc->policy == MPOL_DEFAULT)
-		numa_bitmask_clearall(nodemask);
-	cmp_ok = (tc->policy == policy && (tc->from_node == NONE ||
-					   numa_bitmask_equal(nodemask,
-							      getnodemask)));
-TEST_END:
-	result = (err != tc->err) || !cmp_ok;
-	PRINT_RESULT_CMP(0, tc->ret, tc->err, ret, err, cmp_ok);
-	return result;
+	return ret;
 }
 
-static void cleanup(void)
+
+static void setup(void)
 {
-	tst_rmdir();
+	if (!is_numa(NULL, NH_MEMS, 1))
+		tst_brk(TCONF, "requires NUMA with at least 1 node");
 }
 
-static void setup(void)

testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c line 206

Quoted 10 lines
testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +4 -4
+static void setup_node(void)
 {
-	/* check syscall availability */
-	ltp_syscall(__NR_get_mempolicy, NULL, NULL, 0, NULL, 0);
+	int test_node = -1;
 
-	if (!is_numa(NULL, NH_MEMS, 1))
-		tst_brkm(TCONF, NULL, "requires NUMA with at least 1 node");
+	if (get_allowed_nodes(NH_MEMS, 1, &test_node) < 0)
+		tst_brk(TBROK | TERRNO, "get_allowed_nodes failed");

The is_numa() and get_allowed_nodes() is deprecated API, we do have new tst_get_nodemap() function that replaces them. See set_mempolicy() testcases for reference.

Quoted 21 lines
testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +13 -5
-	TEST_PAUSE;
-	tst_tmpdir();
+	nodemask = numa_allocate_nodemask();
+	getnodemask = numa_allocate_nodemask();
+	numa_bitmask_setbit(nodemask, test_node);
 }
 
-#else
-int main(void)
+static void do_test(unsigned int i)
 {
-	tst_brkm(TCONF, NULL, NUMA_ERROR_MSG);
+	struct test_case *tc = &tcase[i];
+	int policy;
+
+	tst_res(TINFO, "test #%d: %s", (i+1), tc->desc);
+
+	setup_node();
+
+	if (tc->pre_test)
+		if (tc->pre_test(i) == -1)

testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c line 233

Quoted 10 lines
testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +10 -0
+			return;
+
+	if (tc->test) {
+		tc->test(i);
+
+		if (TST_RET < 0) {
+			tst_res(TFAIL | TERRNO, ".test failed");
+			return;
+		}
+	}

We call test_mbind() here for each iteration which calls mmap() and the memory is never freed. Which means that this will fail sooner or later with the -i option.

Why can't we allocate all the blocks with different mempolicy and or/bind the memory once in the test setup instead? We can keep the callback in-place as they are we just need to loop over them in the setup() instead. Also I would rename them to alloc, setup, or something like that so that it's clear that they are just preparing the environment and not doing the actuall test.

Also I would pass the struct test_case pointer to these instead of i since they convert the i to the testcase pointer as the first thing anyways.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#3 lore raw

PATCH 1/1 syscalls/get_mempolicy01: Rewrite to new API

Hi Cyril,

...

Quoted 8 lines

testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c line 206

Quoted 4 lines
testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +2 -2
-	if (!is_numa(NULL, NH_MEMS, 1))
-		tst_brkm(TCONF, NULL, "requires NUMA with at least 1 node");
+	if (get_allowed_nodes(NH_MEMS, 1, &test_node) < 0)
+		tst_brk(TBROK | TERRNO, "get_allowed_nodes failed");

The is_numa() and get_allowed_nodes() is deprecated API, we do have new tst_get_nodemap() function that replaces them. See set_mempolicy() testcases for reference.

Thanks!

Quoted 36 lines
Quoted 21 lines
testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +13 -5
-	TEST_PAUSE;
-	tst_tmpdir();
+	nodemask = numa_allocate_nodemask();
+	getnodemask = numa_allocate_nodemask();
+	numa_bitmask_setbit(nodemask, test_node);
 }
 
-#else
-int main(void)
+static void do_test(unsigned int i)
 {
-	tst_brkm(TCONF, NULL, NUMA_ERROR_MSG);
+	struct test_case *tc = &tcase[i];
+	int policy;
+
+	tst_res(TINFO, "test #%d: %s", (i+1), tc->desc);
+
+	setup_node();
+
+	if (tc->pre_test)
+		if (tc->pre_test(i) == -1)

testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c line 233

Quoted 10 lines
testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +10 -0
+			return;
+
+	if (tc->test) {
+		tc->test(i);
+
+		if (TST_RET < 0) {
+			tst_res(TFAIL | TERRNO, ".test failed");
+			return;
+		}
+	}

We call test_mbind() here for each iteration which calls mmap() and the memory is never freed. Which means that this will fail sooner or later with the -i option.

Quoted 6 lines

Why can't we allocate all the blocks with different mempolicy and or/bind the memory once in the test setup instead? We can keep the callback in-place as they are we just need to loop over them in the setup() instead. Also I would rename them to alloc, setup, or something like that so that it's clear that they are just preparing the environment and not doing the actuall test.

Thanks for catching this. I actually run it more with -i500,
but it looks laptop has enough memory :). Anyway, what you suggest is obviously much better solution, thanks!

Quoted 3 lines

Also I would pass the struct test_case pointer to these instead of i since they convert the i to the testcase pointer as the first thing anyways.

+1

Kind regards,
Petr

author
#4 lore raw

PATCH 1/1 syscalls/get_mempolicy01: Rewrite to new API

In reply to Cyril Hrubis

Hi Cyril,

...

Quoted 27 lines
Quoted 12 lines
testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +10 -1
+static void do_test(unsigned int i)
 {
-	tst_brkm(TCONF, NULL, NUMA_ERROR_MSG);
+	struct test_case *tc = &tcase[i];
+	int policy;
+
+	tst_res(TINFO, "test #%d: %s", (i+1), tc->desc);
+
+	setup_node();
+
+	if (tc->pre_test)
+		if (tc->pre_test(i) == -1)

testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c line 233

Quoted 10 lines
testcases/kernel/syscalls/get_mempolicy/get_mempolicy01.c +10 -0
+			return;
+
+	if (tc->test) {
+		tc->test(i);
+
+		if (TST_RET < 0) {
+			tst_res(TFAIL | TERRNO, ".test failed");
+			return;
+		}
+	}

We call test_mbind() here for each iteration which calls mmap() and the memory is never freed. Which means that this will fail sooner or later with the -i option.

Quoted 6 lines

Why can't we allocate all the blocks with different mempolicy and or/bind the memory once in the test setup instead? We can keep the callback in-place as they are we just need to loop over them in the setup() instead. Also I would rename them to alloc, setup, or something like that so that it's clear that they are just preparing the environment and not doing the actuall test.

OK, I'll loop over ARRAY_SIZE(tcase) in the setup().
But the original code first set NUMA node (setup_node()), then did the preparation (SAFE_MMAP() and mbind()) and then call set_mempolicy().

Do you suggest instead of testing just take "safe" approach - tst_brk(TBROK) on any failure as it's in the setup?

Kind regards,
Petr