xref: /linux/tools/testing/memblock/tests/common.c (revision 6957416ae43de1830174f527b5b8cf737401a8dd)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include "tests/common.h"
3 #include <string.h>
4 #include <getopt.h>
5 #include <linux/memory_hotplug.h>
6 #include <linux/build_bug.h>
7 
8 #define PREFIXES_MAX				15
9 #define DELIM					": "
10 #define BASIS					10000
11 
12 static struct test_memory memory_block;
13 static const char __maybe_unused *prefixes[PREFIXES_MAX];
14 static int __maybe_unused nr_prefixes;
15 
16 static const char *short_opts = "hmv";
17 static const struct option long_opts[] = {
18 	{"help", 0, NULL, 'h'},
19 	{"movable-node", 0, NULL, 'm'},
20 	{"verbose", 0, NULL, 'v'},
21 	{NULL, 0, NULL, 0}
22 };
23 
24 static const char * const help_opts[] = {
25 	"display this help message and exit",
26 	"disallow allocations from regions marked as hotplugged\n\t\t\t"
27 		"by simulating enabling the \"movable_node\" kernel\n\t\t\t"
28 		"parameter",
29 	"enable verbose output, which includes the name of the\n\t\t\t"
30 		"memblock function being tested, the name of the test,\n\t\t\t"
31 		"and whether the test passed or failed."
32 };
33 
34 static int verbose;
35 
36 /* sets global variable returned by movable_node_is_enabled() stub */
37 bool movable_node_enabled;
38 
39 void reset_memblock_regions(void)
40 {
41 	memset(memblock.memory.regions, 0,
42 	       memblock.memory.cnt * sizeof(struct memblock_region));
43 	memblock.memory.cnt     = 0;
44 	memblock.memory.max	= INIT_MEMBLOCK_REGIONS;
45 	memblock.memory.total_size = 0;
46 
47 	memset(memblock.reserved.regions, 0,
48 	       memblock.reserved.cnt * sizeof(struct memblock_region));
49 	memblock.reserved.cnt   = 0;
50 	memblock.reserved.max	= INIT_MEMBLOCK_RESERVED_REGIONS;
51 	memblock.reserved.total_size = 0;
52 }
53 
54 void reset_memblock_attributes(void)
55 {
56 	memblock.memory.name	= "memory";
57 	memblock.reserved.name	= "reserved";
58 	memblock.bottom_up	= false;
59 	memblock.current_limit	= MEMBLOCK_ALLOC_ANYWHERE;
60 }
61 
62 static inline void fill_memblock(void)
63 {
64 	memset(memory_block.base, 1, PHYS_MEM_SIZE);
65 }
66 
67 void setup_memblock(void)
68 {
69 	reset_memblock_regions();
70 	memblock_add((phys_addr_t)(uintptr_t)memory_block.base, MEM_SIZE);
71 	fill_memblock();
72 }
73 
74 /**
75  * setup_numa_memblock:
76  * Set up a memory layout with multiple NUMA nodes in a previously allocated
77  * dummy physical memory.
78  * @node_fracs: an array representing the fraction of MEM_SIZE contained in
79  *              each node in basis point units (one hundredth of 1% or 1/10000).
80  *              For example, if node 0 should contain 1/8 of MEM_SIZE,
81  *              node_fracs[0] = 1250.
82  *
83  * The nids will be set to 0 through NUMA_NODES - 1.
84  */
85 void setup_numa_memblock(const unsigned int node_fracs[])
86 {
87 	phys_addr_t base;
88 	int flags;
89 
90 	reset_memblock_regions();
91 	base = (phys_addr_t)(uintptr_t)memory_block.base;
92 	flags = (movable_node_is_enabled()) ? MEMBLOCK_NONE : MEMBLOCK_HOTPLUG;
93 
94 	for (int i = 0; i < NUMA_NODES; i++) {
95 		assert(node_fracs[i] <= BASIS);
96 		phys_addr_t size = MEM_SIZE * node_fracs[i] / BASIS;
97 
98 		memblock_add_node(base, size, i, flags);
99 		base += size;
100 	}
101 	fill_memblock();
102 }
103 
104 void dummy_physical_memory_init(void)
105 {
106 	memory_block.base = malloc(PHYS_MEM_SIZE);
107 	assert(memory_block.base);
108 	fill_memblock();
109 }
110 
111 void dummy_physical_memory_cleanup(void)
112 {
113 	free(memory_block.base);
114 }
115 
116 phys_addr_t dummy_physical_memory_base(void)
117 {
118 	return (phys_addr_t)(uintptr_t)memory_block.base;
119 }
120 
121 phys_addr_t dummy_physical_memory_low_limit(void)
122 {
123 	/* The low limit is halfway through memory registered by setup_memblock(). */
124 	return dummy_physical_memory_base() + MEM_SIZE / 2;
125 }
126 
127 static void usage(const char *prog)
128 {
129 	BUILD_BUG_ON(ARRAY_SIZE(help_opts) != ARRAY_SIZE(long_opts) - 1);
130 
131 	printf("Usage: %s [-%s]\n", prog, short_opts);
132 
133 	for (int i = 0; long_opts[i].name; i++) {
134 		printf("  -%c, --%-12s\t%s\n", long_opts[i].val,
135 		       long_opts[i].name, help_opts[i]);
136 	}
137 
138 	exit(1);
139 }
140 
141 void parse_args(int argc, char **argv)
142 {
143 	int c;
144 
145 	while ((c = getopt_long_only(argc, argv, short_opts, long_opts,
146 				     NULL)) != -1) {
147 		switch (c) {
148 		case 'm':
149 			movable_node_enabled = true;
150 			break;
151 		case 'v':
152 			verbose = 1;
153 			break;
154 		default:
155 			usage(argv[0]);
156 		}
157 	}
158 }
159 
160 void print_prefixes(const char *postfix)
161 {
162 	for (int i = 0; i < nr_prefixes; i++)
163 		test_print("%s%s", prefixes[i], DELIM);
164 	test_print(postfix);
165 }
166 
167 void test_fail(void)
168 {
169 	if (verbose) {
170 		ksft_test_result_fail(": ");
171 		print_prefixes("failed\n");
172 	}
173 }
174 
175 void test_pass(void)
176 {
177 	if (verbose) {
178 		ksft_test_result_pass(": ");
179 		print_prefixes("passed\n");
180 	}
181 }
182 
183 void test_print(const char *fmt, ...)
184 {
185 	if (verbose) {
186 		int saved_errno = errno;
187 		va_list args;
188 
189 		va_start(args, fmt);
190 		errno = saved_errno;
191 		vprintf(fmt, args);
192 		va_end(args);
193 	}
194 }
195 
196 void prefix_reset(void)
197 {
198 	memset(prefixes, 0, PREFIXES_MAX * sizeof(char *));
199 	nr_prefixes = 0;
200 }
201 
202 void prefix_push(const char *prefix)
203 {
204 	assert(nr_prefixes < PREFIXES_MAX);
205 	prefixes[nr_prefixes] = prefix;
206 	nr_prefixes++;
207 }
208 
209 void prefix_pop(void)
210 {
211 	if (nr_prefixes > 0) {
212 		prefixes[nr_prefixes - 1] = 0;
213 		nr_prefixes--;
214 	}
215 }
216