• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * SPDX-License-Identifier: GPL-2.0-or-later
3  *
4  * Copyright (c) 2019 Cyril Hrubis <chrubis@suse.cz>
5  */
6 
7 /*
8  * We are testing mbind() with MPOL_BIND, MPOL_PREFERRED and MPOL_INTERLEAVE
9  *
10  * For each node with memory we set its bit in nodemask with set_mempolicy()
11  * and verify that memory has been faulted accordingly.
12  */
13 
14 #include <errno.h>
15 #include "config.h"
16 #ifdef HAVE_NUMA_H
17 # include <numa.h>
18 # include <numaif.h>
19 # include "mbind.h"
20 #endif
21 #include "tst_test.h"
22 #include "tst_numa.h"
23 
24 #ifdef HAVE_NUMA_V2
25 
26 static size_t page_size;
27 static struct tst_nodemap *nodes;
28 
29 #define PAGES_ALLOCATED 16u
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_policy(unsigned int node,int mode,unsigned flag)45 static void verify_policy(unsigned int node, int mode, unsigned flag)
46 {
47 	struct bitmask *bm = numa_allocate_nodemask();
48 	unsigned int i;
49 	void *ptr;
50 	pid_t pid;
51 	unsigned long size = PAGES_ALLOCATED * page_size;
52 
53 	numa_bitmask_setbit(bm, node);
54 
55 	ptr = tst_numa_map(NULL, size);
56 
57 	TEST(mbind(ptr, size, mode, bm->maskp, bm->size + 1, flag));
58 
59 	numa_free_nodemask(bm);
60 
61 	if (TST_RET) {
62 		tst_res(TFAIL | TTERRNO,
63 		        "mbind(%s, %s) node %u",
64 		        tst_numa_mode_name(mode), mbind_flag_name(flag), node);
65 		return;
66 	}
67 
68 	tst_res(TPASS, "mbind(%s, %s) node %u",
69 	        tst_numa_mode_name(mode), mbind_flag_name(flag), node);
70 
71 	const char *prefix = "child: ";
72 
73 	pid = SAFE_FORK();
74 	if (pid) {
75 		prefix = "parent: ";
76 		tst_reap_children();
77 	}
78 
79 	tst_nodemap_reset_counters(nodes);
80 	tst_numa_fault(ptr, size);
81 	tst_nodemap_count_pages(nodes, ptr, size);
82 	tst_numa_unmap(ptr, size);
83 
84 	int fail = 0;
85 
86 	for (i = 0; i < nodes->cnt; i++) {
87 		if (nodes->map[i] == node) {
88 			if (nodes->counters[i] == PAGES_ALLOCATED) {
89 				tst_res(TPASS, "%sNode %u allocated %u",
90 				        prefix, node, PAGES_ALLOCATED);
91 			} else {
92 				tst_res(TFAIL, "%sNode %u allocated %u, expected %u",
93 				        prefix, node, nodes->counters[i],
94 				        PAGES_ALLOCATED);
95 				fail = 1;
96 			}
97 			continue;
98 		}
99 
100 		if (nodes->counters[i]) {
101 			tst_res(TFAIL, "%sNode %u allocated %u, expected 0",
102 			        prefix, i, nodes->counters[i]);
103 			fail = 1;
104 		}
105 	}
106 
107 	if (fail)
108 		tst_nodemap_print_counters(nodes);
109 
110 	if (!pid)
111 		exit(0);
112 }
113 
114 static const int modes[] = {
115 	MPOL_PREFERRED,
116 	MPOL_BIND,
117 	MPOL_INTERLEAVE,
118 };
119 
verify_mbind(unsigned int n)120 static void verify_mbind(unsigned int n)
121 {
122 	unsigned int i;
123 
124 	for (i = 0; i < nodes->cnt; i++) {
125 		verify_policy(nodes->map[i], modes[n], 0);
126 		verify_policy(nodes->map[i], modes[n], MPOL_MF_STRICT);
127 	}
128 }
129 
130 static struct tst_test test = {
131 	.setup = setup,
132 	.cleanup = cleanup,
133 	.test = verify_mbind,
134 	.tcnt = ARRAY_SIZE(modes),
135 	.forks_child = 1,
136 };
137 
138 #else
139 
140 TST_TEST_TCONF(NUMA_ERROR_MSG);
141 
142 #endif /* HAVE_NUMA_H */
143