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 <linux/mman.h>
15 #include "linux/kernel.h"
17 #include "test_util.h"
20 * Parses "[0-9]+[kmgt]?".
22 size_t parse_size(const char *size)
28 TEST_ASSERT(size && isdigit(size[0]), "Need at least one digit in '%s'", size);
30 base = strtoull(size, &scale, 0);
32 TEST_ASSERT(base != ULLONG_MAX, "Overflow parsing size!");
34 switch (tolower(*scale)) {
52 TEST_ASSERT(false, "Unknown size letter %c", *scale);
55 TEST_ASSERT((base << shift) >> shift == base, "Overflow scaling size!");
60 int64_t timespec_to_ns(struct timespec ts)
62 return (int64_t)ts.tv_nsec + 1000000000LL * (int64_t)ts.tv_sec;
65 struct timespec timespec_add_ns(struct timespec ts, int64_t ns)
69 res.tv_nsec = ts.tv_nsec + ns;
70 res.tv_sec = ts.tv_sec + res.tv_nsec / 1000000000LL;
71 res.tv_nsec %= 1000000000LL;
76 struct timespec timespec_add(struct timespec ts1, struct timespec ts2)
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);
83 struct timespec timespec_sub(struct timespec ts1, struct timespec ts2)
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);
90 struct timespec timespec_elapsed(struct timespec start)
94 clock_gettime(CLOCK_MONOTONIC, &end);
95 return timespec_sub(end, start);
98 struct timespec timespec_div(struct timespec ts, int divisor)
100 int64_t ns = timespec_to_ns(ts) / divisor;
102 return timespec_add_ns((struct timespec){0}, ns);
105 void print_skip(const char *fmt, ...)
113 puts(", skipping test");
116 bool thp_configured(void)
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");
128 size_t get_trans_hugepagesz(void)
133 TEST_ASSERT(thp_configured(), "THP is not configured in host kernel");
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");
138 fscanf(f, "%ld", &size);
144 size_t get_def_hugetlb_pagesz(void)
147 const char *tag = "Hugepagesize:";
150 f = fopen("/proc/meminfo", "r");
151 TEST_ASSERT(f != NULL, "Error in opening /proc/meminfo");
153 while (fgets(buf, sizeof(buf), f) != NULL) {
154 if (strstr(buf, tag) == buf) {
156 return strtoull(buf + strlen(tag), NULL, 10) << 10;
161 TEST_FAIL("HUGETLB is not configured in host kernel");
163 TEST_FAIL("Error in reading /proc/meminfo");
169 #define ANON_FLAGS (MAP_PRIVATE | MAP_ANONYMOUS)
170 #define ANON_HUGE_FLAGS (ANON_FLAGS | MAP_HUGETLB)
172 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)
174 static const struct vm_mem_backing_src_alias aliases[] = {
175 [VM_MEM_SRC_ANONYMOUS] = {
179 [VM_MEM_SRC_ANONYMOUS_THP] = {
180 .name = "anonymous_thp",
183 [VM_MEM_SRC_ANONYMOUS_HUGETLB] = {
184 .name = "anonymous_hugetlb",
185 .flag = ANON_HUGE_FLAGS,
187 [VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB] = {
188 .name = "anonymous_hugetlb_16kb",
189 .flag = ANON_HUGE_FLAGS | MAP_HUGE_16KB,
191 [VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB] = {
192 .name = "anonymous_hugetlb_64kb",
193 .flag = ANON_HUGE_FLAGS | MAP_HUGE_64KB,
195 [VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB] = {
196 .name = "anonymous_hugetlb_512kb",
197 .flag = ANON_HUGE_FLAGS | MAP_HUGE_512KB,
199 [VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB] = {
200 .name = "anonymous_hugetlb_1mb",
201 .flag = ANON_HUGE_FLAGS | MAP_HUGE_1MB,
203 [VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB] = {
204 .name = "anonymous_hugetlb_2mb",
205 .flag = ANON_HUGE_FLAGS | MAP_HUGE_2MB,
207 [VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB] = {
208 .name = "anonymous_hugetlb_8mb",
209 .flag = ANON_HUGE_FLAGS | MAP_HUGE_8MB,
211 [VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB] = {
212 .name = "anonymous_hugetlb_16mb",
213 .flag = ANON_HUGE_FLAGS | MAP_HUGE_16MB,
215 [VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB] = {
216 .name = "anonymous_hugetlb_32mb",
217 .flag = ANON_HUGE_FLAGS | MAP_HUGE_32MB,
219 [VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB] = {
220 .name = "anonymous_hugetlb_256mb",
221 .flag = ANON_HUGE_FLAGS | MAP_HUGE_256MB,
223 [VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB] = {
224 .name = "anonymous_hugetlb_512mb",
225 .flag = ANON_HUGE_FLAGS | MAP_HUGE_512MB,
227 [VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB] = {
228 .name = "anonymous_hugetlb_1gb",
229 .flag = ANON_HUGE_FLAGS | MAP_HUGE_1GB,
231 [VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB] = {
232 .name = "anonymous_hugetlb_2gb",
233 .flag = ANON_HUGE_FLAGS | MAP_HUGE_2GB,
235 [VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB] = {
236 .name = "anonymous_hugetlb_16gb",
237 .flag = ANON_HUGE_FLAGS | MAP_HUGE_16GB,
239 [VM_MEM_SRC_SHMEM] = {
243 [VM_MEM_SRC_SHARED_HUGETLB] = {
244 .name = "shared_hugetlb",
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
254 _Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES,
255 "Missing new backing src types?");
257 TEST_ASSERT(i < NUM_SRC_TYPES, "Backing src type ID %d too big", i);
262 #define MAP_HUGE_PAGE_SIZE(x) (1ULL << ((x >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK))
264 size_t get_backing_src_pagesz(uint32_t i)
266 uint32_t flag = vm_mem_backing_src_alias(i)->flag;
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();
278 return MAP_HUGE_PAGE_SIZE(flag);
282 void backing_src_help(void)
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);
291 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name)
295 for (i = 0; i < NUM_SRC_TYPES; i++)
296 if (!strcmp(type_name, vm_mem_backing_src_alias(i)->name))
300 TEST_FAIL("Unknown backing src type: %s", type_name);