1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tools/testing/selftests/kvm/lib/test_util.c
4  *
5  * Copyright (C) 2020, Google LLC.
6  */
7 
8 #include <assert.h>
9 #include <ctype.h>
10 #include <limits.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include <sys/stat.h>
14 #include <linux/mman.h>
15 #include "linux/kernel.h"
16 
17 #include "test_util.h"
18 
19 /*
20  * Parses "[0-9]+[kmgt]?".
21  */
22 size_t parse_size(const char *size)
23 {
24 	size_t base;
25 	char *scale;
26 	int shift = 0;
27 
28 	TEST_ASSERT(size && isdigit(size[0]), "Need at least one digit in '%s'", size);
29 
30 	base = strtoull(size, &scale, 0);
31 
32 	TEST_ASSERT(base != ULLONG_MAX, "Overflow parsing size!");
33 
34 	switch (tolower(*scale)) {
35 	case 't':
36 		shift = 40;
37 		break;
38 	case 'g':
39 		shift = 30;
40 		break;
41 	case 'm':
42 		shift = 20;
43 		break;
44 	case 'k':
45 		shift = 10;
46 		break;
47 	case 'b':
48 	case '\0':
49 		shift = 0;
50 		break;
51 	default:
52 		TEST_ASSERT(false, "Unknown size letter %c", *scale);
53 	}
54 
55 	TEST_ASSERT((base << shift) >> shift == base, "Overflow scaling size!");
56 
57 	return base << shift;
58 }
59 
60 int64_t timespec_to_ns(struct timespec ts)
61 {
62 	return (int64_t)ts.tv_nsec + 1000000000LL * (int64_t)ts.tv_sec;
63 }
64 
65 struct timespec timespec_add_ns(struct timespec ts, int64_t ns)
66 {
67 	struct timespec res;
68 
69 	res.tv_nsec = ts.tv_nsec + ns;
70 	res.tv_sec = ts.tv_sec + res.tv_nsec / 1000000000LL;
71 	res.tv_nsec %= 1000000000LL;
72 
73 	return res;
74 }
75 
76 struct timespec timespec_add(struct timespec ts1, struct timespec ts2)
77 {
78 	int64_t ns1 = timespec_to_ns(ts1);
79 	int64_t ns2 = timespec_to_ns(ts2);
80 	return timespec_add_ns((struct timespec){0}, ns1 + ns2);
81 }
82 
83 struct timespec timespec_sub(struct timespec ts1, struct timespec ts2)
84 {
85 	int64_t ns1 = timespec_to_ns(ts1);
86 	int64_t ns2 = timespec_to_ns(ts2);
87 	return timespec_add_ns((struct timespec){0}, ns1 - ns2);
88 }
89 
90 struct timespec timespec_elapsed(struct timespec start)
91 {
92 	struct timespec end;
93 
94 	clock_gettime(CLOCK_MONOTONIC, &end);
95 	return timespec_sub(end, start);
96 }
97 
98 struct timespec timespec_div(struct timespec ts, int divisor)
99 {
100 	int64_t ns = timespec_to_ns(ts) / divisor;
101 
102 	return timespec_add_ns((struct timespec){0}, ns);
103 }
104 
105 void print_skip(const char *fmt, ...)
106 {
107 	va_list ap;
108 
109 	assert(fmt);
110 	va_start(ap, fmt);
111 	vprintf(fmt, ap);
112 	va_end(ap);
113 	puts(", skipping test");
114 }
115 
116 bool thp_configured(void)
117 {
118 	int ret;
119 	struct stat statbuf;
120 
121 	ret = stat("/sys/kernel/mm/transparent_hugepage", &statbuf);
122 	TEST_ASSERT(ret == 0 || (ret == -1 && errno == ENOENT),
123 		    "Error in stating /sys/kernel/mm/transparent_hugepage");
124 
125 	return ret == 0;
126 }
127 
128 size_t get_trans_hugepagesz(void)
129 {
130 	size_t size;
131 	FILE *f;
132 
133 	TEST_ASSERT(thp_configured(), "THP is not configured in host kernel");
134 
135 	f = fopen("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", "r");
136 	TEST_ASSERT(f != NULL, "Error in opening transparent_hugepage/hpage_pmd_size");
137 
138 	fscanf(f, "%ld", &size);
139 	fclose(f);
140 
141 	return size;
142 }
143 
144 size_t get_def_hugetlb_pagesz(void)
145 {
146 	char buf[64];
147 	const char *tag = "Hugepagesize:";
148 	FILE *f;
149 
150 	f = fopen("/proc/meminfo", "r");
151 	TEST_ASSERT(f != NULL, "Error in opening /proc/meminfo");
152 
153 	while (fgets(buf, sizeof(buf), f) != NULL) {
154 		if (strstr(buf, tag) == buf) {
155 			fclose(f);
156 			return strtoull(buf + strlen(tag), NULL, 10) << 10;
157 		}
158 	}
159 
160 	if (feof(f))
161 		TEST_FAIL("HUGETLB is not configured in host kernel");
162 	else
163 		TEST_FAIL("Error in reading /proc/meminfo");
164 
165 	fclose(f);
166 	return 0;
167 }
168 
169 #define ANON_FLAGS	(MAP_PRIVATE | MAP_ANONYMOUS)
170 #define ANON_HUGE_FLAGS	(ANON_FLAGS | MAP_HUGETLB)
171 
172 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)
173 {
174 	static const struct vm_mem_backing_src_alias aliases[] = {
175 		[VM_MEM_SRC_ANONYMOUS] = {
176 			.name = "anonymous",
177 			.flag = ANON_FLAGS,
178 		},
179 		[VM_MEM_SRC_ANONYMOUS_THP] = {
180 			.name = "anonymous_thp",
181 			.flag = ANON_FLAGS,
182 		},
183 		[VM_MEM_SRC_ANONYMOUS_HUGETLB] = {
184 			.name = "anonymous_hugetlb",
185 			.flag = ANON_HUGE_FLAGS,
186 		},
187 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB] = {
188 			.name = "anonymous_hugetlb_16kb",
189 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_16KB,
190 		},
191 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB] = {
192 			.name = "anonymous_hugetlb_64kb",
193 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_64KB,
194 		},
195 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB] = {
196 			.name = "anonymous_hugetlb_512kb",
197 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_512KB,
198 		},
199 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB] = {
200 			.name = "anonymous_hugetlb_1mb",
201 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_1MB,
202 		},
203 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB] = {
204 			.name = "anonymous_hugetlb_2mb",
205 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_2MB,
206 		},
207 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB] = {
208 			.name = "anonymous_hugetlb_8mb",
209 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_8MB,
210 		},
211 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB] = {
212 			.name = "anonymous_hugetlb_16mb",
213 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_16MB,
214 		},
215 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB] = {
216 			.name = "anonymous_hugetlb_32mb",
217 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_32MB,
218 		},
219 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB] = {
220 			.name = "anonymous_hugetlb_256mb",
221 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_256MB,
222 		},
223 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB] = {
224 			.name = "anonymous_hugetlb_512mb",
225 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_512MB,
226 		},
227 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB] = {
228 			.name = "anonymous_hugetlb_1gb",
229 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_1GB,
230 		},
231 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB] = {
232 			.name = "anonymous_hugetlb_2gb",
233 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_2GB,
234 		},
235 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB] = {
236 			.name = "anonymous_hugetlb_16gb",
237 			.flag = ANON_HUGE_FLAGS | MAP_HUGE_16GB,
238 		},
239 		[VM_MEM_SRC_SHMEM] = {
240 			.name = "shmem",
241 			.flag = MAP_SHARED,
242 		},
243 		[VM_MEM_SRC_SHARED_HUGETLB] = {
244 			.name = "shared_hugetlb",
245 			/*
246 			 * No MAP_HUGETLB, we use MFD_HUGETLB instead. Since
247 			 * we're using "file backed" memory, we need to specify
248 			 * this when the FD is created, not when the area is
249 			 * mapped.
250 			 */
251 			.flag = MAP_SHARED,
252 		},
253 	};
254 	_Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES,
255 		       "Missing new backing src types?");
256 
257 	TEST_ASSERT(i < NUM_SRC_TYPES, "Backing src type ID %d too big", i);
258 
259 	return &aliases[i];
260 }
261 
262 #define MAP_HUGE_PAGE_SIZE(x) (1ULL << ((x >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK))
263 
264 size_t get_backing_src_pagesz(uint32_t i)
265 {
266 	uint32_t flag = vm_mem_backing_src_alias(i)->flag;
267 
268 	switch (i) {
269 	case VM_MEM_SRC_ANONYMOUS:
270 	case VM_MEM_SRC_SHMEM:
271 		return getpagesize();
272 	case VM_MEM_SRC_ANONYMOUS_THP:
273 		return get_trans_hugepagesz();
274 	case VM_MEM_SRC_ANONYMOUS_HUGETLB:
275 	case VM_MEM_SRC_SHARED_HUGETLB:
276 		return get_def_hugetlb_pagesz();
277 	default:
278 		return MAP_HUGE_PAGE_SIZE(flag);
279 	}
280 }
281 
282 void backing_src_help(void)
283 {
284 	int i;
285 
286 	printf("Available backing src types:\n");
287 	for (i = 0; i < NUM_SRC_TYPES; i++)
288 		printf("\t%s\n", vm_mem_backing_src_alias(i)->name);
289 }
290 
291 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name)
292 {
293 	int i;
294 
295 	for (i = 0; i < NUM_SRC_TYPES; i++)
296 		if (!strcmp(type_name, vm_mem_backing_src_alias(i)->name))
297 			return i;
298 
299 	backing_src_help();
300 	TEST_FAIL("Unknown backing src type: %s", type_name);
301 	return -1;
302 }
303