1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
5 */
6
7 /*
8 * We are testing set_mempolicy() with MPOL_BIND and MPOL_PREFERRED backed by a
9 * file on each supported filesystem.
10 */
11
12 #include <errno.h>
13 #include "config.h"
14 #ifdef HAVE_NUMA_V2
15 # include <numaif.h>
16 # include <numa.h>
17 #endif
18 #include "tst_test.h"
19 #include "tst_numa.h"
20
21 #define MNTPOINT "mntpoint"
22 #define PAGES_ALLOCATED 16u
23
24 #ifdef HAVE_NUMA_V2
25
26 #include "set_mempolicy.h"
27
28 static size_t page_size;
29 static struct tst_nodemap *nodes;
30
setup(void)31 static void setup(void)
32 {
33 page_size = getpagesize();
34
35 nodes = tst_get_nodemap(TST_NUMA_MEM, 2 * PAGES_ALLOCATED * page_size / 1024);
36 if (nodes->cnt <= 1)
37 tst_brk(TCONF, "Test requires at least two NUMA memory nodes");
38 }
39
cleanup(void)40 static void cleanup(void)
41 {
42 tst_nodemap_free(nodes);
43 }
44
verify_mempolicy(unsigned int node,int mode)45 static void verify_mempolicy(unsigned int node, int mode)
46 {
47 struct bitmask *bm = numa_allocate_nodemask();
48 unsigned int i;
49
50 numa_bitmask_setbit(bm, node);
51
52 TEST(set_mempolicy(mode, bm->maskp, bm->size+1));
53
54 if (TST_RET) {
55 tst_res(TFAIL | TTERRNO,
56 "set_mempolicy(%s) node %u",
57 tst_numa_mode_name(mode), node);
58 return;
59 }
60
61 tst_res(TPASS, "set_mempolicy(%s) node %u",
62 tst_numa_mode_name(mode), node);
63
64 numa_free_nodemask(bm);
65
66 tst_nodemap_reset_counters(nodes);
67 alloc_fault_count(nodes, MNTPOINT "/numa-test-file", PAGES_ALLOCATED * page_size);
68
69 for (i = 0; i < nodes->cnt; i++) {
70 if (nodes->map[i] == node) {
71 if (nodes->counters[i] == PAGES_ALLOCATED) {
72 tst_res(TPASS, "Node %u allocated %u",
73 node, PAGES_ALLOCATED);
74 } else {
75 tst_res(TFAIL, "Node %u allocated %u, expected %u",
76 node, nodes->counters[i], PAGES_ALLOCATED);
77 }
78 continue;
79 }
80
81 if (nodes->counters[i]) {
82 tst_res(TFAIL, "Node %u allocated %u, expected 0",
83 node, nodes->counters[i]);
84 }
85 }
86 }
87
verify_set_mempolicy(unsigned int n)88 static void verify_set_mempolicy(unsigned int n)
89 {
90 unsigned int i;
91 int mode = n ? MPOL_PREFERRED : MPOL_BIND;
92
93 for (i = 0; i < nodes->cnt; i++)
94 verify_mempolicy(nodes->map[i], mode);
95 }
96
97 static struct tst_test test = {
98 .setup = setup,
99 .cleanup = cleanup,
100 .test = verify_set_mempolicy,
101 .tcnt = 2,
102 .needs_root = 1,
103 .all_filesystems = 1,
104 .mntpoint = MNTPOINT,
105 .forks_child = 1,
106 .needs_checkpoints = 1,
107 };
108
109 #else
110
111 TST_TEST_TCONF(NUMA_ERROR_MSG);
112
113 #endif /* HAVE_NUMA_V2 */
114