1 // SPDX-License-Identifier: GPL-2.0
3 * KVM demand paging test
4 * Adapted from dirty_log_test.c
6 * Copyright (C) 2018, Red Hat, Inc.
7 * Copyright (C) 2019, Google, Inc.
10 #define _GNU_SOURCE /* for pipe2 */
18 #include <linux/userfaultfd.h>
19 #include <sys/syscall.h>
22 #include "test_util.h"
23 #include "perf_test_util.h"
24 #include "guest_modes.h"
26 #ifdef __NR_userfaultfd
28 #ifdef PRINT_PER_PAGE_UPDATES
29 #define PER_PAGE_DEBUG(...) printf(__VA_ARGS__)
31 #define PER_PAGE_DEBUG(...) _no_printf(__VA_ARGS__)
34 #ifdef PRINT_PER_VCPU_UPDATES
35 #define PER_VCPU_DEBUG(...) printf(__VA_ARGS__)
37 #define PER_VCPU_DEBUG(...) _no_printf(__VA_ARGS__)
40 static int nr_vcpus = 1;
41 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
42 static size_t demand_paging_size;
43 static char *guest_data_prototype;
45 static void *vcpu_worker(void *data)
48 struct perf_test_vcpu_args *vcpu_args = (struct perf_test_vcpu_args *)data;
49 int vcpu_id = vcpu_args->vcpu_id;
50 struct kvm_vm *vm = perf_test_args.vm;
52 struct timespec start;
53 struct timespec ts_diff;
55 run = vcpu_state(vm, vcpu_id);
57 clock_gettime(CLOCK_MONOTONIC, &start);
59 /* Let the guest access its memory */
60 ret = _vcpu_run(vm, vcpu_id);
61 TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
62 if (get_ucall(vm, vcpu_id, NULL) != UCALL_SYNC) {
64 "Invalid guest sync status: exit_reason=%s\n",
65 exit_reason_str(run->exit_reason));
68 ts_diff = timespec_elapsed(start);
69 PER_VCPU_DEBUG("vCPU %d execution time: %ld.%.9lds\n", vcpu_id,
70 ts_diff.tv_sec, ts_diff.tv_nsec);
75 static int handle_uffd_page_request(int uffd_mode, int uffd, uint64_t addr)
77 pid_t tid = syscall(__NR_gettid);
78 struct timespec start;
79 struct timespec ts_diff;
82 clock_gettime(CLOCK_MONOTONIC, &start);
84 if (uffd_mode == UFFDIO_REGISTER_MODE_MISSING) {
85 struct uffdio_copy copy;
87 copy.src = (uint64_t)guest_data_prototype;
89 copy.len = demand_paging_size;
92 r = ioctl(uffd, UFFDIO_COPY, ©);
94 pr_info("Failed UFFDIO_COPY in 0x%lx from thread %d with errno: %d\n",
98 } else if (uffd_mode == UFFDIO_REGISTER_MODE_MINOR) {
99 struct uffdio_continue cont = {0};
101 cont.range.start = addr;
102 cont.range.len = demand_paging_size;
104 r = ioctl(uffd, UFFDIO_CONTINUE, &cont);
106 pr_info("Failed UFFDIO_CONTINUE in 0x%lx from thread %d with errno: %d\n",
111 TEST_FAIL("Invalid uffd mode %d", uffd_mode);
114 ts_diff = timespec_elapsed(start);
116 PER_PAGE_DEBUG("UFFD page-in %d \t%ld ns\n", tid,
117 timespec_to_ns(ts_diff));
118 PER_PAGE_DEBUG("Paged in %ld bytes at 0x%lx from thread %d\n",
119 demand_paging_size, addr, tid);
124 bool quit_uffd_thread;
126 struct uffd_handler_args {
133 static void *uffd_handler_thread_fn(void *arg)
135 struct uffd_handler_args *uffd_args = (struct uffd_handler_args *)arg;
136 int uffd = uffd_args->uffd;
137 int pipefd = uffd_args->pipefd;
138 useconds_t delay = uffd_args->delay;
140 struct timespec start;
141 struct timespec ts_diff;
143 clock_gettime(CLOCK_MONOTONIC, &start);
144 while (!quit_uffd_thread) {
146 struct pollfd pollfd[2];
152 pollfd[0].events = POLLIN;
153 pollfd[1].fd = pipefd;
154 pollfd[1].events = POLLIN;
156 r = poll(pollfd, 2, -1);
166 pr_info("Polling uffd returned %d", r);
170 if (pollfd[0].revents & POLLERR) {
171 pr_info("uffd revents has POLLERR");
175 if (pollfd[1].revents & POLLIN) {
176 r = read(pollfd[1].fd, &tmp_chr, 1);
178 "Error reading pipefd in UFFD thread\n");
182 if (!pollfd[0].revents & POLLIN)
185 r = read(uffd, &msg, sizeof(msg));
189 pr_info("Read of uffd got errno %d\n", errno);
193 if (r != sizeof(msg)) {
194 pr_info("Read on uffd returned unexpected size: %d bytes", r);
198 if (!(msg.event & UFFD_EVENT_PAGEFAULT))
203 addr = msg.arg.pagefault.address;
204 r = handle_uffd_page_request(uffd_args->uffd_mode, uffd, addr);
210 ts_diff = timespec_elapsed(start);
211 PER_VCPU_DEBUG("userfaulted %ld pages over %ld.%.9lds. (%f/sec)\n",
212 pages, ts_diff.tv_sec, ts_diff.tv_nsec,
213 pages / ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0));
218 static void setup_demand_paging(struct kvm_vm *vm,
219 pthread_t *uffd_handler_thread, int pipefd,
220 int uffd_mode, useconds_t uffd_delay,
221 struct uffd_handler_args *uffd_args,
222 void *hva, void *alias, uint64_t len)
224 bool is_minor = (uffd_mode == UFFDIO_REGISTER_MODE_MINOR);
226 struct uffdio_api uffdio_api;
227 struct uffdio_register uffdio_register;
228 uint64_t expected_ioctls = ((uint64_t) 1) << _UFFDIO_COPY;
230 PER_PAGE_DEBUG("Userfaultfd %s mode, faults resolved with %s\n",
231 is_minor ? "MINOR" : "MISSING",
232 is_minor ? "UFFDIO_CONINUE" : "UFFDIO_COPY");
234 /* In order to get minor faults, prefault via the alias. */
238 expected_ioctls = ((uint64_t) 1) << _UFFDIO_CONTINUE;
240 TEST_ASSERT(alias != NULL, "Alias required for minor faults");
241 for (p = 0; p < (len / demand_paging_size); ++p) {
242 memcpy(alias + (p * demand_paging_size),
243 guest_data_prototype, demand_paging_size);
247 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
248 TEST_ASSERT(uffd >= 0, "uffd creation failed, errno: %d", errno);
250 uffdio_api.api = UFFD_API;
251 uffdio_api.features = 0;
252 TEST_ASSERT(ioctl(uffd, UFFDIO_API, &uffdio_api) != -1,
253 "ioctl UFFDIO_API failed: %" PRIu64,
254 (uint64_t)uffdio_api.api);
256 uffdio_register.range.start = (uint64_t)hva;
257 uffdio_register.range.len = len;
258 uffdio_register.mode = uffd_mode;
259 TEST_ASSERT(ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) != -1,
260 "ioctl UFFDIO_REGISTER failed");
261 TEST_ASSERT((uffdio_register.ioctls & expected_ioctls) ==
262 expected_ioctls, "missing userfaultfd ioctls");
264 uffd_args->uffd_mode = uffd_mode;
265 uffd_args->uffd = uffd;
266 uffd_args->pipefd = pipefd;
267 uffd_args->delay = uffd_delay;
268 pthread_create(uffd_handler_thread, NULL, uffd_handler_thread_fn,
271 PER_VCPU_DEBUG("Created uffd thread for HVA range [%p, %p)\n",
277 useconds_t uffd_delay;
278 enum vm_mem_backing_src_type src_type;
279 bool partition_vcpu_memory_access;
282 static void run_test(enum vm_guest_mode mode, void *arg)
284 struct test_params *p = arg;
285 pthread_t *vcpu_threads;
286 pthread_t *uffd_handler_threads = NULL;
287 struct uffd_handler_args *uffd_args = NULL;
288 struct timespec start;
289 struct timespec ts_diff;
295 vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
298 perf_test_args.wr_fract = 1;
300 demand_paging_size = get_backing_src_pagesz(p->src_type);
302 guest_data_prototype = malloc(demand_paging_size);
303 TEST_ASSERT(guest_data_prototype,
304 "Failed to allocate buffer for guest data pattern");
305 memset(guest_data_prototype, 0xAB, demand_paging_size);
307 vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
308 TEST_ASSERT(vcpu_threads, "Memory allocation failed");
310 perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
311 p->partition_vcpu_memory_access);
314 uffd_handler_threads =
315 malloc(nr_vcpus * sizeof(*uffd_handler_threads));
316 TEST_ASSERT(uffd_handler_threads, "Memory allocation failed");
318 uffd_args = malloc(nr_vcpus * sizeof(*uffd_args));
319 TEST_ASSERT(uffd_args, "Memory allocation failed");
321 pipefds = malloc(sizeof(int) * nr_vcpus * 2);
322 TEST_ASSERT(pipefds, "Unable to allocate memory for pipefd");
324 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
328 uint64_t vcpu_mem_size;
331 if (p->partition_vcpu_memory_access) {
332 vcpu_gpa = guest_test_phys_mem +
333 (vcpu_id * guest_percpu_mem_size);
334 vcpu_mem_size = guest_percpu_mem_size;
336 vcpu_gpa = guest_test_phys_mem;
337 vcpu_mem_size = guest_percpu_mem_size * nr_vcpus;
339 PER_VCPU_DEBUG("Added VCPU %d with test mem gpa [%lx, %lx)\n",
340 vcpu_id, vcpu_gpa, vcpu_gpa + vcpu_mem_size);
342 /* Cache the host addresses of the region */
343 vcpu_hva = addr_gpa2hva(vm, vcpu_gpa);
344 vcpu_alias = addr_gpa2alias(vm, vcpu_gpa);
347 * Set up user fault fd to handle demand paging
350 r = pipe2(&pipefds[vcpu_id * 2],
351 O_CLOEXEC | O_NONBLOCK);
352 TEST_ASSERT(!r, "Failed to set up pipefd");
354 setup_demand_paging(vm, &uffd_handler_threads[vcpu_id],
355 pipefds[vcpu_id * 2], p->uffd_mode,
356 p->uffd_delay, &uffd_args[vcpu_id],
357 vcpu_hva, vcpu_alias,
362 /* Export the shared variables to the guest */
363 sync_global_to_guest(vm, perf_test_args);
365 pr_info("Finished creating vCPUs and starting uffd threads\n");
367 clock_gettime(CLOCK_MONOTONIC, &start);
369 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
370 pthread_create(&vcpu_threads[vcpu_id], NULL, vcpu_worker,
371 &perf_test_args.vcpu_args[vcpu_id]);
374 pr_info("Started all vCPUs\n");
376 /* Wait for the vcpu threads to quit */
377 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
378 pthread_join(vcpu_threads[vcpu_id], NULL);
379 PER_VCPU_DEBUG("Joined thread for vCPU %d\n", vcpu_id);
382 ts_diff = timespec_elapsed(start);
384 pr_info("All vCPU threads joined\n");
389 /* Tell the user fault fd handler threads to quit */
390 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
391 r = write(pipefds[vcpu_id * 2 + 1], &c, 1);
392 TEST_ASSERT(r == 1, "Unable to write to pipefd");
394 pthread_join(uffd_handler_threads[vcpu_id], NULL);
398 pr_info("Total guest execution time: %ld.%.9lds\n",
399 ts_diff.tv_sec, ts_diff.tv_nsec);
400 pr_info("Overall demand paging rate: %f pgs/sec\n",
401 perf_test_args.vcpu_args[0].pages * nr_vcpus /
402 ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0));
404 perf_test_destroy_vm(vm);
406 free(guest_data_prototype);
409 free(uffd_handler_threads);
415 static void help(char *name)
418 printf("usage: %s [-h] [-m vm_mode] [-u uffd_mode] [-d uffd_delay_usec]\n"
419 " [-b memory] [-t type] [-v vcpus] [-o]\n", name);
421 printf(" -u: use userfaultfd to handle vCPU page faults. Mode is a\n"
422 " UFFD registration mode: 'MISSING' or 'MINOR'.\n");
423 printf(" -d: add a delay in usec to the User Fault\n"
424 " FD handler to simulate demand paging\n"
425 " overheads. Ignored without -u.\n");
426 printf(" -b: specify the size of the memory region which should be\n"
427 " demand paged by each vCPU. e.g. 10M or 3G.\n"
429 printf(" -t: The type of backing memory to use. Default: anonymous\n");
431 printf(" -v: specify the number of vCPUs to run.\n");
432 printf(" -o: Overlap guest memory accesses instead of partitioning\n"
433 " them into a separate region of memory for each vCPU.\n");
438 int main(int argc, char *argv[])
440 int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
441 struct test_params p = {
442 .src_type = VM_MEM_SRC_ANONYMOUS,
443 .partition_vcpu_memory_access = true,
447 guest_modes_append_default();
449 while ((opt = getopt(argc, argv, "hm:u:d:b:t:v:o")) != -1) {
452 guest_modes_cmdline(optarg);
455 if (!strcmp("MISSING", optarg))
456 p.uffd_mode = UFFDIO_REGISTER_MODE_MISSING;
457 else if (!strcmp("MINOR", optarg))
458 p.uffd_mode = UFFDIO_REGISTER_MODE_MINOR;
459 TEST_ASSERT(p.uffd_mode, "UFFD mode must be 'MISSING' or 'MINOR'.");
462 p.uffd_delay = strtoul(optarg, NULL, 0);
463 TEST_ASSERT(p.uffd_delay >= 0, "A negative UFFD delay is not supported.");
466 guest_percpu_mem_size = parse_size(optarg);
469 p.src_type = parse_backing_src_type(optarg);
472 nr_vcpus = atoi(optarg);
473 TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
474 "Invalid number of vcpus, must be between 1 and %d", max_vcpus);
477 p.partition_vcpu_memory_access = false;
486 if (p.uffd_mode == UFFDIO_REGISTER_MODE_MINOR &&
487 !backing_src_is_shared(p.src_type)) {
488 TEST_FAIL("userfaultfd MINOR mode requires shared memory; pick a different -t");
491 for_each_guest_mode(run_test, &p);
496 #else /* __NR_userfaultfd */
498 #warning "missing __NR_userfaultfd definition"
502 print_skip("__NR_userfaultfd must be present for userfaultfd test");
506 #endif /* __NR_userfaultfd */