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 */
17 #include <linux/userfaultfd.h>
18 #include <sys/syscall.h>
21 #include "test_util.h"
22 #include "perf_test_util.h"
23 #include "guest_modes.h"
25 #ifdef __NR_userfaultfd
27 #ifdef PRINT_PER_PAGE_UPDATES
28 #define PER_PAGE_DEBUG(...) printf(__VA_ARGS__)
30 #define PER_PAGE_DEBUG(...) _no_printf(__VA_ARGS__)
33 #ifdef PRINT_PER_VCPU_UPDATES
34 #define PER_VCPU_DEBUG(...) printf(__VA_ARGS__)
36 #define PER_VCPU_DEBUG(...) _no_printf(__VA_ARGS__)
39 static int nr_vcpus = 1;
40 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
41 static char *guest_data_prototype;
43 static void *vcpu_worker(void *data)
46 struct perf_test_vcpu_args *vcpu_args = (struct perf_test_vcpu_args *)data;
47 int vcpu_id = vcpu_args->vcpu_id;
48 struct kvm_vm *vm = perf_test_args.vm;
50 struct timespec start;
51 struct timespec ts_diff;
53 vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
54 run = vcpu_state(vm, vcpu_id);
56 clock_gettime(CLOCK_MONOTONIC, &start);
58 /* Let the guest access its memory */
59 ret = _vcpu_run(vm, vcpu_id);
60 TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
61 if (get_ucall(vm, vcpu_id, NULL) != UCALL_SYNC) {
63 "Invalid guest sync status: exit_reason=%s\n",
64 exit_reason_str(run->exit_reason));
67 ts_diff = timespec_elapsed(start);
68 PER_VCPU_DEBUG("vCPU %d execution time: %ld.%.9lds\n", vcpu_id,
69 ts_diff.tv_sec, ts_diff.tv_nsec);
74 static int handle_uffd_page_request(int uffd, uint64_t addr)
77 struct timespec start;
78 struct timespec ts_diff;
79 struct uffdio_copy copy;
82 tid = syscall(__NR_gettid);
84 copy.src = (uint64_t)guest_data_prototype;
86 copy.len = perf_test_args.host_page_size;
89 clock_gettime(CLOCK_MONOTONIC, &start);
91 r = ioctl(uffd, UFFDIO_COPY, ©);
93 pr_info("Failed Paged in 0x%lx from thread %d with errno: %d\n",
98 ts_diff = timespec_elapsed(start);
100 PER_PAGE_DEBUG("UFFDIO_COPY %d \t%ld ns\n", tid,
101 timespec_to_ns(ts_diff));
102 PER_PAGE_DEBUG("Paged in %ld bytes at 0x%lx from thread %d\n",
103 perf_test_args.host_page_size, addr, tid);
108 bool quit_uffd_thread;
110 struct uffd_handler_args {
116 static void *uffd_handler_thread_fn(void *arg)
118 struct uffd_handler_args *uffd_args = (struct uffd_handler_args *)arg;
119 int uffd = uffd_args->uffd;
120 int pipefd = uffd_args->pipefd;
121 useconds_t delay = uffd_args->delay;
123 struct timespec start;
124 struct timespec ts_diff;
126 clock_gettime(CLOCK_MONOTONIC, &start);
127 while (!quit_uffd_thread) {
129 struct pollfd pollfd[2];
135 pollfd[0].events = POLLIN;
136 pollfd[1].fd = pipefd;
137 pollfd[1].events = POLLIN;
139 r = poll(pollfd, 2, -1);
149 pr_info("Polling uffd returned %d", r);
153 if (pollfd[0].revents & POLLERR) {
154 pr_info("uffd revents has POLLERR");
158 if (pollfd[1].revents & POLLIN) {
159 r = read(pollfd[1].fd, &tmp_chr, 1);
161 "Error reading pipefd in UFFD thread\n");
165 if (!pollfd[0].revents & POLLIN)
168 r = read(uffd, &msg, sizeof(msg));
172 pr_info("Read of uffd got errno %d\n", errno);
176 if (r != sizeof(msg)) {
177 pr_info("Read on uffd returned unexpected size: %d bytes", r);
181 if (!(msg.event & UFFD_EVENT_PAGEFAULT))
186 addr = msg.arg.pagefault.address;
187 r = handle_uffd_page_request(uffd, addr);
193 ts_diff = timespec_elapsed(start);
194 PER_VCPU_DEBUG("userfaulted %ld pages over %ld.%.9lds. (%f/sec)\n",
195 pages, ts_diff.tv_sec, ts_diff.tv_nsec,
196 pages / ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0));
201 static int setup_demand_paging(struct kvm_vm *vm,
202 pthread_t *uffd_handler_thread, int pipefd,
203 useconds_t uffd_delay,
204 struct uffd_handler_args *uffd_args,
205 void *hva, uint64_t len)
208 struct uffdio_api uffdio_api;
209 struct uffdio_register uffdio_register;
211 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
213 pr_info("uffd creation failed\n");
217 uffdio_api.api = UFFD_API;
218 uffdio_api.features = 0;
219 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) {
220 pr_info("ioctl uffdio_api failed\n");
224 uffdio_register.range.start = (uint64_t)hva;
225 uffdio_register.range.len = len;
226 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
227 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1) {
228 pr_info("ioctl uffdio_register failed\n");
232 if ((uffdio_register.ioctls & UFFD_API_RANGE_IOCTLS) !=
233 UFFD_API_RANGE_IOCTLS) {
234 pr_info("unexpected userfaultfd ioctl set\n");
238 uffd_args->uffd = uffd;
239 uffd_args->pipefd = pipefd;
240 uffd_args->delay = uffd_delay;
241 pthread_create(uffd_handler_thread, NULL, uffd_handler_thread_fn,
244 PER_VCPU_DEBUG("Created uffd thread for HVA range [%p, %p)\n",
252 useconds_t uffd_delay;
253 bool partition_vcpu_memory_access;
256 static void run_test(enum vm_guest_mode mode, void *arg)
258 struct test_params *p = arg;
259 pthread_t *vcpu_threads;
260 pthread_t *uffd_handler_threads = NULL;
261 struct uffd_handler_args *uffd_args = NULL;
262 struct timespec start;
263 struct timespec ts_diff;
269 vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size,
270 VM_MEM_SRC_ANONYMOUS);
272 perf_test_args.wr_fract = 1;
274 guest_data_prototype = malloc(perf_test_args.host_page_size);
275 TEST_ASSERT(guest_data_prototype,
276 "Failed to allocate buffer for guest data pattern");
277 memset(guest_data_prototype, 0xAB, perf_test_args.host_page_size);
279 vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
280 TEST_ASSERT(vcpu_threads, "Memory allocation failed");
282 perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
283 p->partition_vcpu_memory_access);
286 uffd_handler_threads =
287 malloc(nr_vcpus * sizeof(*uffd_handler_threads));
288 TEST_ASSERT(uffd_handler_threads, "Memory allocation failed");
290 uffd_args = malloc(nr_vcpus * sizeof(*uffd_args));
291 TEST_ASSERT(uffd_args, "Memory allocation failed");
293 pipefds = malloc(sizeof(int) * nr_vcpus * 2);
294 TEST_ASSERT(pipefds, "Unable to allocate memory for pipefd");
296 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
299 uint64_t vcpu_mem_size;
302 if (p->partition_vcpu_memory_access) {
303 vcpu_gpa = guest_test_phys_mem +
304 (vcpu_id * guest_percpu_mem_size);
305 vcpu_mem_size = guest_percpu_mem_size;
307 vcpu_gpa = guest_test_phys_mem;
308 vcpu_mem_size = guest_percpu_mem_size * nr_vcpus;
310 PER_VCPU_DEBUG("Added VCPU %d with test mem gpa [%lx, %lx)\n",
311 vcpu_id, vcpu_gpa, vcpu_gpa + vcpu_mem_size);
313 /* Cache the HVA pointer of the region */
314 vcpu_hva = addr_gpa2hva(vm, vcpu_gpa);
317 * Set up user fault fd to handle demand paging
320 r = pipe2(&pipefds[vcpu_id * 2],
321 O_CLOEXEC | O_NONBLOCK);
322 TEST_ASSERT(!r, "Failed to set up pipefd");
324 r = setup_demand_paging(vm,
325 &uffd_handler_threads[vcpu_id],
326 pipefds[vcpu_id * 2],
327 p->uffd_delay, &uffd_args[vcpu_id],
328 vcpu_hva, vcpu_mem_size);
334 /* Export the shared variables to the guest */
335 sync_global_to_guest(vm, perf_test_args);
337 pr_info("Finished creating vCPUs and starting uffd threads\n");
339 clock_gettime(CLOCK_MONOTONIC, &start);
341 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
342 pthread_create(&vcpu_threads[vcpu_id], NULL, vcpu_worker,
343 &perf_test_args.vcpu_args[vcpu_id]);
346 pr_info("Started all vCPUs\n");
348 /* Wait for the vcpu threads to quit */
349 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
350 pthread_join(vcpu_threads[vcpu_id], NULL);
351 PER_VCPU_DEBUG("Joined thread for vCPU %d\n", vcpu_id);
354 ts_diff = timespec_elapsed(start);
356 pr_info("All vCPU threads joined\n");
361 /* Tell the user fault fd handler threads to quit */
362 for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
363 r = write(pipefds[vcpu_id * 2 + 1], &c, 1);
364 TEST_ASSERT(r == 1, "Unable to write to pipefd");
366 pthread_join(uffd_handler_threads[vcpu_id], NULL);
370 pr_info("Total guest execution time: %ld.%.9lds\n",
371 ts_diff.tv_sec, ts_diff.tv_nsec);
372 pr_info("Overall demand paging rate: %f pgs/sec\n",
373 perf_test_args.vcpu_args[0].pages * nr_vcpus /
374 ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / 100000000.0));
376 perf_test_destroy_vm(vm);
378 free(guest_data_prototype);
381 free(uffd_handler_threads);
387 static void help(char *name)
390 printf("usage: %s [-h] [-m mode] [-u] [-d uffd_delay_usec]\n"
391 " [-b memory] [-v vcpus] [-o]\n", name);
393 printf(" -u: use User Fault FD to handle vCPU page\n"
395 printf(" -d: add a delay in usec to the User Fault\n"
396 " FD handler to simulate demand paging\n"
397 " overheads. Ignored without -u.\n");
398 printf(" -b: specify the size of the memory region which should be\n"
399 " demand paged by each vCPU. e.g. 10M or 3G.\n"
401 printf(" -v: specify the number of vCPUs to run.\n");
402 printf(" -o: Overlap guest memory accesses instead of partitioning\n"
403 " them into a separate region of memory for each vCPU.\n");
408 int main(int argc, char *argv[])
410 int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
411 struct test_params p = {
412 .partition_vcpu_memory_access = true,
416 guest_modes_append_default();
418 while ((opt = getopt(argc, argv, "hm:ud:b:v:o")) != -1) {
421 guest_modes_cmdline(optarg);
427 p.uffd_delay = strtoul(optarg, NULL, 0);
428 TEST_ASSERT(p.uffd_delay >= 0, "A negative UFFD delay is not supported.");
431 guest_percpu_mem_size = parse_size(optarg);
434 nr_vcpus = atoi(optarg);
435 TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
436 "Invalid number of vcpus, must be between 1 and %d", max_vcpus);
439 p.partition_vcpu_memory_access = false;
448 for_each_guest_mode(run_test, &p);
453 #else /* __NR_userfaultfd */
455 #warning "missing __NR_userfaultfd definition"
459 print_skip("__NR_userfaultfd must be present for userfaultfd test");
463 #endif /* __NR_userfaultfd */