1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 /*
7 * We are testing set_mempolicy() with MPOL_INTERLEAVE.
8 *
9 * The test tries different subsets of memory nodes, sets the mask with
10 * memopolicy, and checks that the memory was interleaved between the nodes
11 * accordingly.
12 */
13
14 #include <errno.h>
15 #include "config.h"
16 #ifdef HAVE_NUMA_V2
17 # include <numa.h>
18 # include <numaif.h>
19 #endif
20 #include "tst_test.h"
21 #include "tst_numa.h"
22
23 #ifdef HAVE_NUMA_V2
24
25 #include "set_mempolicy.h"
26
27 #define ALLOC_ON_NODE 8
28
29 static size_t page_size;
30 static struct tst_nodemap *nodes;
31
setup(void)32 static void setup(void)
33 {
34 page_size = getpagesize();
35
36 nodes = tst_get_nodemap(TST_NUMA_MEM, 2 * ALLOC_ON_NODE * page_size / 1024);
37 if (nodes->cnt <= 1)
38 tst_brk(TCONF, "Test requires at least two NUMA memory nodes");
39 }
40
cleanup(void)41 static void cleanup(void)
42 {
43 tst_nodemap_free(nodes);
44 }
45
alloc_and_check(size_t size,unsigned int * exp_alloc)46 static void alloc_and_check(size_t size, unsigned int *exp_alloc)
47 {
48 unsigned int i;
49 const char *prefix = "child: ";
50
51 if (SAFE_FORK()) {
52 prefix = "parent: ";
53 tst_reap_children();
54 }
55
56 tst_nodemap_reset_counters(nodes);
57 alloc_fault_count(nodes, NULL, size * page_size);
58
59 for (i = 0; i < nodes->cnt; i++) {
60 if (nodes->counters[i] == exp_alloc[i]) {
61 tst_res(TPASS, "%sNode %u allocated %u",
62 prefix, nodes->map[i], exp_alloc[i]);
63 } else {
64 tst_res(TFAIL, "%sNode %u allocated %u, expected %u",
65 prefix, nodes->map[i], nodes->counters[i],
66 exp_alloc[i]);
67 }
68 }
69 }
70
verify_set_mempolicy(unsigned int n)71 static void verify_set_mempolicy(unsigned int n)
72 {
73 struct bitmask *bm = numa_allocate_nodemask();
74 unsigned int exp_alloc[nodes->cnt];
75 unsigned int alloc_per_node = n ? ALLOC_ON_NODE : 2;
76 unsigned int alloc_on_nodes = n ? 2 : nodes->cnt;
77 unsigned int alloc_total = alloc_per_node * alloc_on_nodes;
78 unsigned int i;
79
80 memset(exp_alloc, 0, sizeof(exp_alloc));
81
82 for (i = 0; i < alloc_on_nodes; i++) {
83 exp_alloc[i] = alloc_per_node;
84 numa_bitmask_setbit(bm, nodes->map[i]);
85 }
86
87 TEST(set_mempolicy(MPOL_INTERLEAVE, bm->maskp, bm->size+1));
88
89 tst_res(TINFO, "Allocating on nodes 1-%u - %u pages",
90 alloc_on_nodes, alloc_total);
91
92 if (TST_RET) {
93 tst_res(TFAIL | TTERRNO,
94 "set_mempolicy(MPOL_INTERLEAVE)");
95 return;
96 }
97
98 tst_res(TPASS, "set_mempolicy(MPOL_INTERLEAVE)");
99
100 numa_free_nodemask(bm);
101
102 alloc_and_check(alloc_total, exp_alloc);
103 }
104
105 static struct tst_test test = {
106 .setup = setup,
107 .cleanup = cleanup,
108 .test = verify_set_mempolicy,
109 .tcnt = 2,
110 .forks_child = 1,
111 .needs_checkpoints = 1,
112 };
113
114 #else
115
116 TST_TEST_TCONF(NUMA_ERROR_MSG);
117
118 #endif /* HAVE_NUMA_V2 */
119