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 "memstress.h"
24 #include "guest_modes.h"
25 #include "userfaultfd_util.h"
27 #ifdef __NR_userfaultfd
29 static int nr_vcpus = 1;
30 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
32 static size_t demand_paging_size;
33 static char *guest_data_prototype;
35 static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)
37 struct kvm_vcpu *vcpu = vcpu_args->vcpu;
38 int vcpu_idx = vcpu_args->vcpu_idx;
39 struct kvm_run *run = vcpu->run;
40 struct timespec start;
41 struct timespec ts_diff;
44 clock_gettime(CLOCK_MONOTONIC, &start);
46 /* Let the guest access its memory */
47 ret = _vcpu_run(vcpu);
48 TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
49 if (get_ucall(vcpu, NULL) != UCALL_SYNC) {
51 "Invalid guest sync status: exit_reason=%s\n",
52 exit_reason_str(run->exit_reason));
55 ts_diff = timespec_elapsed(start);
56 PER_VCPU_DEBUG("vCPU %d execution time: %ld.%.9lds\n", vcpu_idx,
57 ts_diff.tv_sec, ts_diff.tv_nsec);
60 static int handle_uffd_page_request(int uffd_mode, int uffd,
63 pid_t tid = syscall(__NR_gettid);
64 uint64_t addr = msg->arg.pagefault.address;
65 struct timespec start;
66 struct timespec ts_diff;
69 clock_gettime(CLOCK_MONOTONIC, &start);
71 if (uffd_mode == UFFDIO_REGISTER_MODE_MISSING) {
72 struct uffdio_copy copy;
74 copy.src = (uint64_t)guest_data_prototype;
76 copy.len = demand_paging_size;
79 r = ioctl(uffd, UFFDIO_COPY, ©);
81 pr_info("Failed UFFDIO_COPY in 0x%lx from thread %d with errno: %d\n",
85 } else if (uffd_mode == UFFDIO_REGISTER_MODE_MINOR) {
86 struct uffdio_continue cont = {0};
88 cont.range.start = addr;
89 cont.range.len = demand_paging_size;
91 r = ioctl(uffd, UFFDIO_CONTINUE, &cont);
93 pr_info("Failed UFFDIO_CONTINUE in 0x%lx from thread %d with errno: %d\n",
98 TEST_FAIL("Invalid uffd mode %d", uffd_mode);
101 ts_diff = timespec_elapsed(start);
103 PER_PAGE_DEBUG("UFFD page-in %d \t%ld ns\n", tid,
104 timespec_to_ns(ts_diff));
105 PER_PAGE_DEBUG("Paged in %ld bytes at 0x%lx from thread %d\n",
106 demand_paging_size, addr, tid);
113 useconds_t uffd_delay;
114 enum vm_mem_backing_src_type src_type;
115 bool partition_vcpu_memory_access;
118 static void prefault_mem(void *alias, uint64_t len)
122 TEST_ASSERT(alias != NULL, "Alias required for minor faults");
123 for (p = 0; p < (len / demand_paging_size); ++p) {
124 memcpy(alias + (p * demand_paging_size),
125 guest_data_prototype, demand_paging_size);
129 static void run_test(enum vm_guest_mode mode, void *arg)
131 struct memstress_vcpu_args *vcpu_args;
132 struct test_params *p = arg;
133 struct uffd_desc **uffd_descs = NULL;
134 struct timespec start;
135 struct timespec ts_diff;
139 vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
140 p->src_type, p->partition_vcpu_memory_access);
142 demand_paging_size = get_backing_src_pagesz(p->src_type);
144 guest_data_prototype = malloc(demand_paging_size);
145 TEST_ASSERT(guest_data_prototype,
146 "Failed to allocate buffer for guest data pattern");
147 memset(guest_data_prototype, 0xAB, demand_paging_size);
149 if (p->uffd_mode == UFFDIO_REGISTER_MODE_MINOR) {
150 for (i = 0; i < nr_vcpus; i++) {
151 vcpu_args = &memstress_args.vcpu_args[i];
152 prefault_mem(addr_gpa2alias(vm, vcpu_args->gpa),
153 vcpu_args->pages * memstress_args.guest_page_size);
158 uffd_descs = malloc(nr_vcpus * sizeof(struct uffd_desc *));
159 TEST_ASSERT(uffd_descs, "Memory allocation failed");
160 for (i = 0; i < nr_vcpus; i++) {
163 vcpu_args = &memstress_args.vcpu_args[i];
165 /* Cache the host addresses of the region */
166 vcpu_hva = addr_gpa2hva(vm, vcpu_args->gpa);
168 * Set up user fault fd to handle demand paging
171 uffd_descs[i] = uffd_setup_demand_paging(
172 p->uffd_mode, p->uffd_delay, vcpu_hva,
173 vcpu_args->pages * memstress_args.guest_page_size,
174 &handle_uffd_page_request);
178 pr_info("Finished creating vCPUs and starting uffd threads\n");
180 clock_gettime(CLOCK_MONOTONIC, &start);
181 memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);
182 pr_info("Started all vCPUs\n");
184 memstress_join_vcpu_threads(nr_vcpus);
185 ts_diff = timespec_elapsed(start);
186 pr_info("All vCPU threads joined\n");
189 /* Tell the user fault fd handler threads to quit */
190 for (i = 0; i < nr_vcpus; i++)
191 uffd_stop_demand_paging(uffd_descs[i]);
194 pr_info("Total guest execution time: %ld.%.9lds\n",
195 ts_diff.tv_sec, ts_diff.tv_nsec);
196 pr_info("Overall demand paging rate: %f pgs/sec\n",
197 memstress_args.vcpu_args[0].pages * nr_vcpus /
198 ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / NSEC_PER_SEC));
200 memstress_destroy_vm(vm);
202 free(guest_data_prototype);
207 static void help(char *name)
210 printf("usage: %s [-h] [-m vm_mode] [-u uffd_mode] [-d uffd_delay_usec]\n"
211 " [-b memory] [-s type] [-v vcpus] [-c cpu_list] [-o]\n", name);
213 printf(" -u: use userfaultfd to handle vCPU page faults. Mode is a\n"
214 " UFFD registration mode: 'MISSING' or 'MINOR'.\n");
215 kvm_print_vcpu_pinning_help();
216 printf(" -d: add a delay in usec to the User Fault\n"
217 " FD handler to simulate demand paging\n"
218 " overheads. Ignored without -u.\n");
219 printf(" -b: specify the size of the memory region which should be\n"
220 " demand paged by each vCPU. e.g. 10M or 3G.\n"
222 backing_src_help("-s");
223 printf(" -v: specify the number of vCPUs to run.\n");
224 printf(" -o: Overlap guest memory accesses instead of partitioning\n"
225 " them into a separate region of memory for each vCPU.\n");
230 int main(int argc, char *argv[])
232 int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
233 const char *cpulist = NULL;
234 struct test_params p = {
235 .src_type = DEFAULT_VM_MEM_SRC,
236 .partition_vcpu_memory_access = true,
240 guest_modes_append_default();
242 while ((opt = getopt(argc, argv, "hm:u:d:b:s:v:c:o")) != -1) {
245 guest_modes_cmdline(optarg);
248 if (!strcmp("MISSING", optarg))
249 p.uffd_mode = UFFDIO_REGISTER_MODE_MISSING;
250 else if (!strcmp("MINOR", optarg))
251 p.uffd_mode = UFFDIO_REGISTER_MODE_MINOR;
252 TEST_ASSERT(p.uffd_mode, "UFFD mode must be 'MISSING' or 'MINOR'.");
255 p.uffd_delay = strtoul(optarg, NULL, 0);
256 TEST_ASSERT(p.uffd_delay >= 0, "A negative UFFD delay is not supported.");
259 guest_percpu_mem_size = parse_size(optarg);
262 p.src_type = parse_backing_src_type(optarg);
265 nr_vcpus = atoi_positive("Number of vCPUs", optarg);
266 TEST_ASSERT(nr_vcpus <= max_vcpus,
267 "Invalid number of vcpus, must be between 1 and %d", max_vcpus);
273 p.partition_vcpu_memory_access = false;
282 if (p.uffd_mode == UFFDIO_REGISTER_MODE_MINOR &&
283 !backing_src_is_shared(p.src_type)) {
284 TEST_FAIL("userfaultfd MINOR mode requires shared memory; pick a different -s");
288 kvm_parse_vcpu_pinning(cpulist, memstress_args.vcpu_to_pcpu,
290 memstress_args.pin_vcpus = true;
293 for_each_guest_mode(run_test, &p);
298 #else /* __NR_userfaultfd */
300 #warning "missing __NR_userfaultfd definition"
304 print_skip("__NR_userfaultfd must be present for userfaultfd test");
308 #endif /* __NR_userfaultfd */