1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2020 Petr Vorel <pvorel@suse.cz>
4f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2009-2020
5f08c3bdfSopenharmony_ci * Copyright (c) Crackerjack Project, 2007-2008, Hitachi, Ltd
6f08c3bdfSopenharmony_ci *
7f08c3bdfSopenharmony_ci * Authors:
8f08c3bdfSopenharmony_ci * Takahiro Yasui <takahiro.yasui.mp@hitachi.com>
9f08c3bdfSopenharmony_ci * Yumiko Sugita <yumiko.sugita.yf@hitachi.com>
10f08c3bdfSopenharmony_ci * Satoshi Fujiwara <sa-fuji@sdl.hitachi.co.jp>
11f08c3bdfSopenharmony_ci * Manas Kumar Nayak <maknayak@in.ibm.com> (original port to the legacy API)
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci/*\
15f08c3bdfSopenharmony_ci * [Description]
16f08c3bdfSopenharmony_ci *
17f08c3bdfSopenharmony_ci * Verify that get_mempolicy() returns a proper return errno for failure cases.
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include "config.h"
21f08c3bdfSopenharmony_ci#include "tst_test.h"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#ifdef HAVE_NUMA_V2
24f08c3bdfSopenharmony_ci#include <numa.h>
25f08c3bdfSopenharmony_ci#include <numaif.h>
26f08c3bdfSopenharmony_ci#include <errno.h>
27f08c3bdfSopenharmony_ci#include "tst_numa.h"
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci#define PAGES_ALLOCATED 16u
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#define POLICY_DESC_TEXT(x, y) .policy = x, .desc = "policy: "#x", "y
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic struct tst_nodemap *node;
34f08c3bdfSopenharmony_cistatic struct bitmask *nodemask;
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistruct test_case {
37f08c3bdfSopenharmony_ci	int policy;
38f08c3bdfSopenharmony_ci	const char *desc;
39f08c3bdfSopenharmony_ci	unsigned int flags;
40f08c3bdfSopenharmony_ci	int err;
41f08c3bdfSopenharmony_ci	char *addr;
42f08c3bdfSopenharmony_ci};
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic struct test_case tcase[] = {
45f08c3bdfSopenharmony_ci	{
46f08c3bdfSopenharmony_ci		POLICY_DESC_TEXT(MPOL_DEFAULT, "invalid address"),
47f08c3bdfSopenharmony_ci		.addr = NULL,
48f08c3bdfSopenharmony_ci		.err = EFAULT,
49f08c3bdfSopenharmony_ci		.flags = MPOL_F_ADDR,
50f08c3bdfSopenharmony_ci	},
51f08c3bdfSopenharmony_ci	{
52f08c3bdfSopenharmony_ci		POLICY_DESC_TEXT(MPOL_DEFAULT, "invalid flags, no target"),
53f08c3bdfSopenharmony_ci		.err = EINVAL,
54f08c3bdfSopenharmony_ci		.flags = -1,
55f08c3bdfSopenharmony_ci	},
56f08c3bdfSopenharmony_ci};
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic void setup(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	node = tst_get_nodemap(TST_NUMA_MEM, PAGES_ALLOCATED * getpagesize() / 1024);
61f08c3bdfSopenharmony_ci	if (node->cnt < 1)
62f08c3bdfSopenharmony_ci		tst_brk(TCONF, "test requires at least one NUMA memory node");
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	nodemask = numa_allocate_nodemask();
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic void cleanup(void)
68f08c3bdfSopenharmony_ci{
69f08c3bdfSopenharmony_ci	numa_free_nodemask(nodemask);
70f08c3bdfSopenharmony_ci	tst_nodemap_free(node);
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_cistatic void do_test(unsigned int i)
74f08c3bdfSopenharmony_ci{
75f08c3bdfSopenharmony_ci	struct test_case *tc = &tcase[i];
76f08c3bdfSopenharmony_ci	int policy;
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	TST_EXP_FAIL(get_mempolicy(&policy, nodemask->maskp, nodemask->size,
79f08c3bdfSopenharmony_ci				   tc->addr, tc->flags), tc->err, "%s", tc->desc);
80f08c3bdfSopenharmony_ci}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_cistatic struct tst_test test = {
83f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcase),
84f08c3bdfSopenharmony_ci	.test = do_test,
85f08c3bdfSopenharmony_ci	.setup = setup,
86f08c3bdfSopenharmony_ci	.cleanup = cleanup,
87f08c3bdfSopenharmony_ci};
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci#else
90f08c3bdfSopenharmony_ciTST_TEST_TCONF(NUMA_ERROR_MSG);
91f08c3bdfSopenharmony_ci#endif /* HAVE_NUMA_V2 */
92