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 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)
170 {
171 	static const struct vm_mem_backing_src_alias aliases[] = {
172 		[VM_MEM_SRC_ANONYMOUS] = {
173 			.name = "anonymous",
174 			.flag = 0,
175 		},
176 		[VM_MEM_SRC_ANONYMOUS_THP] = {
177 			.name = "anonymous_thp",
178 			.flag = 0,
179 		},
180 		[VM_MEM_SRC_ANONYMOUS_HUGETLB] = {
181 			.name = "anonymous_hugetlb",
182 			.flag = MAP_HUGETLB,
183 		},
184 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB] = {
185 			.name = "anonymous_hugetlb_16kb",
186 			.flag = MAP_HUGETLB | MAP_HUGE_16KB,
187 		},
188 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB] = {
189 			.name = "anonymous_hugetlb_64kb",
190 			.flag = MAP_HUGETLB | MAP_HUGE_64KB,
191 		},
192 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB] = {
193 			.name = "anonymous_hugetlb_512kb",
194 			.flag = MAP_HUGETLB | MAP_HUGE_512KB,
195 		},
196 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB] = {
197 			.name = "anonymous_hugetlb_1mb",
198 			.flag = MAP_HUGETLB | MAP_HUGE_1MB,
199 		},
200 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB] = {
201 			.name = "anonymous_hugetlb_2mb",
202 			.flag = MAP_HUGETLB | MAP_HUGE_2MB,
203 		},
204 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB] = {
205 			.name = "anonymous_hugetlb_8mb",
206 			.flag = MAP_HUGETLB | MAP_HUGE_8MB,
207 		},
208 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB] = {
209 			.name = "anonymous_hugetlb_16mb",
210 			.flag = MAP_HUGETLB | MAP_HUGE_16MB,
211 		},
212 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB] = {
213 			.name = "anonymous_hugetlb_32mb",
214 			.flag = MAP_HUGETLB | MAP_HUGE_32MB,
215 		},
216 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB] = {
217 			.name = "anonymous_hugetlb_256mb",
218 			.flag = MAP_HUGETLB | MAP_HUGE_256MB,
219 		},
220 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB] = {
221 			.name = "anonymous_hugetlb_512mb",
222 			.flag = MAP_HUGETLB | MAP_HUGE_512MB,
223 		},
224 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB] = {
225 			.name = "anonymous_hugetlb_1gb",
226 			.flag = MAP_HUGETLB | MAP_HUGE_1GB,
227 		},
228 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB] = {
229 			.name = "anonymous_hugetlb_2gb",
230 			.flag = MAP_HUGETLB | MAP_HUGE_2GB,
231 		},
232 		[VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB] = {
233 			.name = "anonymous_hugetlb_16gb",
234 			.flag = MAP_HUGETLB | MAP_HUGE_16GB,
235 		},
236 	};
237 	_Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES,
238 		       "Missing new backing src types?");
239 
240 	TEST_ASSERT(i < NUM_SRC_TYPES, "Backing src type ID %d too big", i);
241 
242 	return &aliases[i];
243 }
244 
245 #define MAP_HUGE_PAGE_SIZE(x) (1ULL << ((x >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK))
246 
247 size_t get_backing_src_pagesz(uint32_t i)
248 {
249 	uint32_t flag = vm_mem_backing_src_alias(i)->flag;
250 
251 	switch (i) {
252 	case VM_MEM_SRC_ANONYMOUS:
253 		return getpagesize();
254 	case VM_MEM_SRC_ANONYMOUS_THP:
255 		return get_trans_hugepagesz();
256 	case VM_MEM_SRC_ANONYMOUS_HUGETLB:
257 		return get_def_hugetlb_pagesz();
258 	default:
259 		return MAP_HUGE_PAGE_SIZE(flag);
260 	}
261 }
262 
263 void backing_src_help(void)
264 {
265 	int i;
266 
267 	printf("Available backing src types:\n");
268 	for (i = 0; i < NUM_SRC_TYPES; i++)
269 		printf("\t%s\n", vm_mem_backing_src_alias(i)->name);
270 }
271 
272 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name)
273 {
274 	int i;
275 
276 	for (i = 0; i < NUM_SRC_TYPES; i++)
277 		if (!strcmp(type_name, vm_mem_backing_src_alias(i)->name))
278 			return i;
279 
280 	backing_src_help();
281 	TEST_FAIL("Unknown backing src type: %s", type_name);
282 	return -1;
283 }
284