1 // SPDX-License-Identifier: GPL-2.0-only
3 * tools/testing/selftests/kvm/lib/test_util.c
5 * Copyright (C) 2020, Google LLC.
14 #include <sys/syscall.h>
15 #include <linux/mman.h>
16 #include "linux/kernel.h"
18 #include "test_util.h"
21 * Parses "[0-9]+[kmgt]?".
23 size_t parse_size(const char *size)
29 TEST_ASSERT(size && isdigit(size[0]), "Need at least one digit in '%s'", size);
31 base = strtoull(size, &scale, 0);
33 TEST_ASSERT(base != ULLONG_MAX, "Overflow parsing size!");
35 switch (tolower(*scale)) {
53 TEST_ASSERT(false, "Unknown size letter %c", *scale);
56 TEST_ASSERT((base << shift) >> shift == base, "Overflow scaling size!");
61 int64_t timespec_to_ns(struct timespec ts)
63 return (int64_t)ts.tv_nsec + 1000000000LL * (int64_t)ts.tv_sec;
66 struct timespec timespec_add_ns(struct timespec ts, int64_t ns)
70 res.tv_nsec = ts.tv_nsec + ns;
71 res.tv_sec = ts.tv_sec + res.tv_nsec / 1000000000LL;
72 res.tv_nsec %= 1000000000LL;
77 struct timespec timespec_add(struct timespec ts1, struct timespec ts2)
79 int64_t ns1 = timespec_to_ns(ts1);
80 int64_t ns2 = timespec_to_ns(ts2);
81 return timespec_add_ns((struct timespec){0}, ns1 + ns2);
84 struct timespec timespec_sub(struct timespec ts1, struct timespec ts2)
86 int64_t ns1 = timespec_to_ns(ts1);
87 int64_t ns2 = timespec_to_ns(ts2);
88 return timespec_add_ns((struct timespec){0}, ns1 - ns2);
91 struct timespec timespec_elapsed(struct timespec start)
95 clock_gettime(CLOCK_MONOTONIC, &end);
96 return timespec_sub(end, start);
99 struct timespec timespec_div(struct timespec ts, int divisor)
101 int64_t ns = timespec_to_ns(ts) / divisor;
103 return timespec_add_ns((struct timespec){0}, ns);
106 void print_skip(const char *fmt, ...)
114 puts(", skipping test");
117 bool thp_configured(void)
122 ret = stat("/sys/kernel/mm/transparent_hugepage", &statbuf);
123 TEST_ASSERT(ret == 0 || (ret == -1 && errno == ENOENT),
124 "Error in stating /sys/kernel/mm/transparent_hugepage");
129 size_t get_trans_hugepagesz(void)
135 TEST_ASSERT(thp_configured(), "THP is not configured in host kernel");
137 f = fopen("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", "r");
138 TEST_ASSERT(f != NULL, "Error in opening transparent_hugepage/hpage_pmd_size");
140 ret = fscanf(f, "%ld", &size);
141 ret = fscanf(f, "%ld", &size);
142 TEST_ASSERT(ret < 1, "Error reading transparent_hugepage/hpage_pmd_size");
148 size_t get_def_hugetlb_pagesz(void)
151 const char *tag = "Hugepagesize:";
154 f = fopen("/proc/meminfo", "r");
155 TEST_ASSERT(f != NULL, "Error in opening /proc/meminfo");
157 while (fgets(buf, sizeof(buf), f) != NULL) {
158 if (strstr(buf, tag) == buf) {
160 return strtoull(buf + strlen(tag), NULL, 10) << 10;
165 TEST_FAIL("HUGETLB is not configured in host kernel");
167 TEST_FAIL("Error in reading /proc/meminfo");
173 #define ANON_FLAGS (MAP_PRIVATE | MAP_ANONYMOUS)
174 #define ANON_HUGE_FLAGS (ANON_FLAGS | MAP_HUGETLB)
176 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)
178 static const struct vm_mem_backing_src_alias aliases[] = {
179 [VM_MEM_SRC_ANONYMOUS] = {
183 [VM_MEM_SRC_ANONYMOUS_THP] = {
184 .name = "anonymous_thp",
187 [VM_MEM_SRC_ANONYMOUS_HUGETLB] = {
188 .name = "anonymous_hugetlb",
189 .flag = ANON_HUGE_FLAGS,
191 [VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB] = {
192 .name = "anonymous_hugetlb_16kb",
193 .flag = ANON_HUGE_FLAGS | MAP_HUGE_16KB,
195 [VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB] = {
196 .name = "anonymous_hugetlb_64kb",
197 .flag = ANON_HUGE_FLAGS | MAP_HUGE_64KB,
199 [VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB] = {
200 .name = "anonymous_hugetlb_512kb",
201 .flag = ANON_HUGE_FLAGS | MAP_HUGE_512KB,
203 [VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB] = {
204 .name = "anonymous_hugetlb_1mb",
205 .flag = ANON_HUGE_FLAGS | MAP_HUGE_1MB,
207 [VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB] = {
208 .name = "anonymous_hugetlb_2mb",
209 .flag = ANON_HUGE_FLAGS | MAP_HUGE_2MB,
211 [VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB] = {
212 .name = "anonymous_hugetlb_8mb",
213 .flag = ANON_HUGE_FLAGS | MAP_HUGE_8MB,
215 [VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB] = {
216 .name = "anonymous_hugetlb_16mb",
217 .flag = ANON_HUGE_FLAGS | MAP_HUGE_16MB,
219 [VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB] = {
220 .name = "anonymous_hugetlb_32mb",
221 .flag = ANON_HUGE_FLAGS | MAP_HUGE_32MB,
223 [VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB] = {
224 .name = "anonymous_hugetlb_256mb",
225 .flag = ANON_HUGE_FLAGS | MAP_HUGE_256MB,
227 [VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB] = {
228 .name = "anonymous_hugetlb_512mb",
229 .flag = ANON_HUGE_FLAGS | MAP_HUGE_512MB,
231 [VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB] = {
232 .name = "anonymous_hugetlb_1gb",
233 .flag = ANON_HUGE_FLAGS | MAP_HUGE_1GB,
235 [VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB] = {
236 .name = "anonymous_hugetlb_2gb",
237 .flag = ANON_HUGE_FLAGS | MAP_HUGE_2GB,
239 [VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB] = {
240 .name = "anonymous_hugetlb_16gb",
241 .flag = ANON_HUGE_FLAGS | MAP_HUGE_16GB,
243 [VM_MEM_SRC_SHMEM] = {
247 [VM_MEM_SRC_SHARED_HUGETLB] = {
248 .name = "shared_hugetlb",
250 * No MAP_HUGETLB, we use MFD_HUGETLB instead. Since
251 * we're using "file backed" memory, we need to specify
252 * this when the FD is created, not when the area is
258 _Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES,
259 "Missing new backing src types?");
261 TEST_ASSERT(i < NUM_SRC_TYPES, "Backing src type ID %d too big", i);
266 #define MAP_HUGE_PAGE_SIZE(x) (1ULL << ((x >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK))
268 size_t get_backing_src_pagesz(uint32_t i)
270 uint32_t flag = vm_mem_backing_src_alias(i)->flag;
273 case VM_MEM_SRC_ANONYMOUS:
274 case VM_MEM_SRC_SHMEM:
275 return getpagesize();
276 case VM_MEM_SRC_ANONYMOUS_THP:
277 return get_trans_hugepagesz();
278 case VM_MEM_SRC_ANONYMOUS_HUGETLB:
279 case VM_MEM_SRC_SHARED_HUGETLB:
280 return get_def_hugetlb_pagesz();
282 return MAP_HUGE_PAGE_SIZE(flag);
286 static void print_available_backing_src_types(const char *prefix)
290 printf("%sAvailable backing src types:\n", prefix);
292 for (i = 0; i < NUM_SRC_TYPES; i++)
293 printf("%s %s\n", prefix, vm_mem_backing_src_alias(i)->name);
296 void backing_src_help(const char *flag)
298 printf(" %s: specify the type of memory that should be used to\n"
299 " back the guest data region. (default: %s)\n",
300 flag, vm_mem_backing_src_alias(DEFAULT_VM_MEM_SRC)->name);
301 print_available_backing_src_types(" ");
304 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name)
308 for (i = 0; i < NUM_SRC_TYPES; i++)
309 if (!strcmp(type_name, vm_mem_backing_src_alias(i)->name))
312 print_available_backing_src_types("");
313 TEST_FAIL("Unknown backing src type: %s", type_name);
317 long get_run_delay(void)
323 sprintf(path, "/proc/%ld/schedstat", syscall(SYS_gettid));
324 fp = fopen(path, "r");
325 /* Return MIN_RUN_DELAY_NS upon failure just to be safe */
326 if (fscanf(fp, "%ld %ld ", &val[0], &val[1]) < 2)
327 val[1] = MIN_RUN_DELAY_NS;