Path: blob/master/tools/testing/selftests/kvm/demand_paging_test.c
38189 views
// SPDX-License-Identifier: GPL-2.01/*2* KVM demand paging test3* Adapted from dirty_log_test.c4*5* Copyright (C) 2018, Red Hat, Inc.6* Copyright (C) 2019, Google, Inc.7*/8#include <inttypes.h>9#include <stdio.h>10#include <stdlib.h>11#include <time.h>12#include <pthread.h>13#include <linux/userfaultfd.h>14#include <sys/syscall.h>1516#include "kvm_util.h"17#include "test_util.h"18#include "memstress.h"19#include "guest_modes.h"20#include "ucall_common.h"21#include "userfaultfd_util.h"2223#ifdef __NR_userfaultfd2425static int nr_vcpus = 1;26static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;2728static size_t demand_paging_size;29static char *guest_data_prototype;3031static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)32{33struct kvm_vcpu *vcpu = vcpu_args->vcpu;34int vcpu_idx = vcpu_args->vcpu_idx;35struct kvm_run *run = vcpu->run;36struct timespec start;37struct timespec ts_diff;38int ret;3940clock_gettime(CLOCK_MONOTONIC, &start);4142/* Let the guest access its memory */43ret = _vcpu_run(vcpu);44TEST_ASSERT(ret == 0, "vcpu_run failed: %d", ret);45if (get_ucall(vcpu, NULL) != UCALL_SYNC) {46TEST_ASSERT(false,47"Invalid guest sync status: exit_reason=%s",48exit_reason_str(run->exit_reason));49}5051ts_diff = timespec_elapsed(start);52PER_VCPU_DEBUG("vCPU %d execution time: %ld.%.9lds\n", vcpu_idx,53ts_diff.tv_sec, ts_diff.tv_nsec);54}5556static int handle_uffd_page_request(int uffd_mode, int uffd,57struct uffd_msg *msg)58{59pid_t tid = syscall(__NR_gettid);60uint64_t addr = msg->arg.pagefault.address;61struct timespec start;62struct timespec ts_diff;63int r;6465clock_gettime(CLOCK_MONOTONIC, &start);6667if (uffd_mode == UFFDIO_REGISTER_MODE_MISSING) {68struct uffdio_copy copy;6970copy.src = (uint64_t)guest_data_prototype;71copy.dst = addr;72copy.len = demand_paging_size;73copy.mode = 0;7475r = ioctl(uffd, UFFDIO_COPY, ©);76/*77* With multiple vCPU threads fault on a single page and there are78* multiple readers for the UFFD, at least one of the UFFDIO_COPYs79* will fail with EEXIST: handle that case without signaling an80* error.81*82* Note that this also suppress any EEXISTs occurring from,83* e.g., the first UFFDIO_COPY/CONTINUEs on a page. That never84* happens here, but a realistic VMM might potentially maintain85* some external state to correctly surface EEXISTs to userspace86* (or prevent duplicate COPY/CONTINUEs in the first place).87*/88if (r == -1 && errno != EEXIST) {89pr_info("Failed UFFDIO_COPY in 0x%lx from thread %d, errno = %d\n",90addr, tid, errno);91return r;92}93} else if (uffd_mode == UFFDIO_REGISTER_MODE_MINOR) {94struct uffdio_continue cont = {0};9596cont.range.start = addr;97cont.range.len = demand_paging_size;9899r = ioctl(uffd, UFFDIO_CONTINUE, &cont);100/*101* With multiple vCPU threads fault on a single page and there are102* multiple readers for the UFFD, at least one of the UFFDIO_COPYs103* will fail with EEXIST: handle that case without signaling an104* error.105*106* Note that this also suppress any EEXISTs occurring from,107* e.g., the first UFFDIO_COPY/CONTINUEs on a page. That never108* happens here, but a realistic VMM might potentially maintain109* some external state to correctly surface EEXISTs to userspace110* (or prevent duplicate COPY/CONTINUEs in the first place).111*/112if (r == -1 && errno != EEXIST) {113pr_info("Failed UFFDIO_CONTINUE in 0x%lx, thread %d, errno = %d\n",114addr, tid, errno);115return r;116}117} else {118TEST_FAIL("Invalid uffd mode %d", uffd_mode);119}120121ts_diff = timespec_elapsed(start);122123PER_PAGE_DEBUG("UFFD page-in %d \t%ld ns\n", tid,124timespec_to_ns(ts_diff));125PER_PAGE_DEBUG("Paged in %ld bytes at 0x%lx from thread %d\n",126demand_paging_size, addr, tid);127128return 0;129}130131struct test_params {132int uffd_mode;133bool single_uffd;134useconds_t uffd_delay;135int readers_per_uffd;136enum vm_mem_backing_src_type src_type;137bool partition_vcpu_memory_access;138};139140static void prefault_mem(void *alias, uint64_t len)141{142size_t p;143144TEST_ASSERT(alias != NULL, "Alias required for minor faults");145for (p = 0; p < (len / demand_paging_size); ++p) {146memcpy(alias + (p * demand_paging_size),147guest_data_prototype, demand_paging_size);148}149}150151static void run_test(enum vm_guest_mode mode, void *arg)152{153struct memstress_vcpu_args *vcpu_args;154struct test_params *p = arg;155struct uffd_desc **uffd_descs = NULL;156uint64_t uffd_region_size;157struct timespec start;158struct timespec ts_diff;159double vcpu_paging_rate;160struct kvm_vm *vm;161int i, num_uffds = 0;162163vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,164p->src_type, p->partition_vcpu_memory_access);165166demand_paging_size = get_backing_src_pagesz(p->src_type);167168guest_data_prototype = malloc(demand_paging_size);169TEST_ASSERT(guest_data_prototype,170"Failed to allocate buffer for guest data pattern");171memset(guest_data_prototype, 0xAB, demand_paging_size);172173if (p->uffd_mode == UFFDIO_REGISTER_MODE_MINOR) {174num_uffds = p->single_uffd ? 1 : nr_vcpus;175for (i = 0; i < num_uffds; i++) {176vcpu_args = &memstress_args.vcpu_args[i];177prefault_mem(addr_gpa2alias(vm, vcpu_args->gpa),178vcpu_args->pages * memstress_args.guest_page_size);179}180}181182if (p->uffd_mode) {183num_uffds = p->single_uffd ? 1 : nr_vcpus;184uffd_region_size = nr_vcpus * guest_percpu_mem_size / num_uffds;185186uffd_descs = malloc(num_uffds * sizeof(struct uffd_desc *));187TEST_ASSERT(uffd_descs, "Memory allocation failed");188for (i = 0; i < num_uffds; i++) {189struct memstress_vcpu_args *vcpu_args;190void *vcpu_hva;191192vcpu_args = &memstress_args.vcpu_args[i];193194/* Cache the host addresses of the region */195vcpu_hva = addr_gpa2hva(vm, vcpu_args->gpa);196/*197* Set up user fault fd to handle demand paging198* requests.199*/200uffd_descs[i] = uffd_setup_demand_paging(201p->uffd_mode, p->uffd_delay, vcpu_hva,202uffd_region_size,203p->readers_per_uffd,204&handle_uffd_page_request);205}206}207208pr_info("Finished creating vCPUs and starting uffd threads\n");209210clock_gettime(CLOCK_MONOTONIC, &start);211memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);212pr_info("Started all vCPUs\n");213214memstress_join_vcpu_threads(nr_vcpus);215ts_diff = timespec_elapsed(start);216pr_info("All vCPU threads joined\n");217218if (p->uffd_mode) {219/* Tell the user fault fd handler threads to quit */220for (i = 0; i < num_uffds; i++)221uffd_stop_demand_paging(uffd_descs[i]);222}223224pr_info("Total guest execution time:\t%ld.%.9lds\n",225ts_diff.tv_sec, ts_diff.tv_nsec);226227vcpu_paging_rate = memstress_args.vcpu_args[0].pages /228((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / NSEC_PER_SEC);229pr_info("Per-vcpu demand paging rate:\t%f pgs/sec/vcpu\n",230vcpu_paging_rate);231pr_info("Overall demand paging rate:\t%f pgs/sec\n",232vcpu_paging_rate * nr_vcpus);233234memstress_destroy_vm(vm);235236free(guest_data_prototype);237if (p->uffd_mode)238free(uffd_descs);239}240241static void help(char *name)242{243puts("");244printf("usage: %s [-h] [-m vm_mode] [-u uffd_mode] [-a]\n"245" [-d uffd_delay_usec] [-r readers_per_uffd] [-b memory]\n"246" [-s type] [-v vcpus] [-c cpu_list] [-o]\n", name);247guest_modes_help();248printf(" -u: use userfaultfd to handle vCPU page faults. Mode is a\n"249" UFFD registration mode: 'MISSING' or 'MINOR'.\n");250kvm_print_vcpu_pinning_help();251printf(" -a: Use a single userfaultfd for all of guest memory, instead of\n"252" creating one for each region paged by a unique vCPU\n"253" Set implicitly with -o, and no effect without -u.\n");254printf(" -d: add a delay in usec to the User Fault\n"255" FD handler to simulate demand paging\n"256" overheads. Ignored without -u.\n");257printf(" -r: Set the number of reader threads per uffd.\n");258printf(" -b: specify the size of the memory region which should be\n"259" demand paged by each vCPU. e.g. 10M or 3G.\n"260" Default: 1G\n");261backing_src_help("-s");262printf(" -v: specify the number of vCPUs to run.\n");263printf(" -o: Overlap guest memory accesses instead of partitioning\n"264" them into a separate region of memory for each vCPU.\n");265puts("");266exit(0);267}268269int main(int argc, char *argv[])270{271int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);272const char *cpulist = NULL;273struct test_params p = {274.src_type = DEFAULT_VM_MEM_SRC,275.partition_vcpu_memory_access = true,276.readers_per_uffd = 1,277.single_uffd = false,278};279int opt;280281guest_modes_append_default();282283while ((opt = getopt(argc, argv, "ahom:u:d:b:s:v:c:r:")) != -1) {284switch (opt) {285case 'm':286guest_modes_cmdline(optarg);287break;288case 'u':289if (!strcmp("MISSING", optarg))290p.uffd_mode = UFFDIO_REGISTER_MODE_MISSING;291else if (!strcmp("MINOR", optarg))292p.uffd_mode = UFFDIO_REGISTER_MODE_MINOR;293TEST_ASSERT(p.uffd_mode, "UFFD mode must be 'MISSING' or 'MINOR'.");294break;295case 'a':296p.single_uffd = true;297break;298case 'd':299p.uffd_delay = strtoul(optarg, NULL, 0);300TEST_ASSERT(p.uffd_delay >= 0, "A negative UFFD delay is not supported.");301break;302case 'b':303guest_percpu_mem_size = parse_size(optarg);304break;305case 's':306p.src_type = parse_backing_src_type(optarg);307break;308case 'v':309nr_vcpus = atoi_positive("Number of vCPUs", optarg);310TEST_ASSERT(nr_vcpus <= max_vcpus,311"Invalid number of vcpus, must be between 1 and %d", max_vcpus);312break;313case 'c':314cpulist = optarg;315break;316case 'o':317p.partition_vcpu_memory_access = false;318p.single_uffd = true;319break;320case 'r':321p.readers_per_uffd = atoi(optarg);322TEST_ASSERT(p.readers_per_uffd >= 1,323"Invalid number of readers per uffd %d: must be >=1",324p.readers_per_uffd);325break;326case 'h':327default:328help(argv[0]);329break;330}331}332333if (p.uffd_mode == UFFDIO_REGISTER_MODE_MINOR &&334!backing_src_is_shared(p.src_type)) {335TEST_FAIL("userfaultfd MINOR mode requires shared memory; pick a different -s");336}337338if (cpulist) {339kvm_parse_vcpu_pinning(cpulist, memstress_args.vcpu_to_pcpu,340nr_vcpus);341memstress_args.pin_vcpus = true;342}343344for_each_guest_mode(run_test, &p);345346return 0;347}348349#else /* __NR_userfaultfd */350351#warning "missing __NR_userfaultfd definition"352353int main(void)354{355print_skip("__NR_userfaultfd must be present for userfaultfd test");356return KSFT_SKIP;357}358359#endif /* __NR_userfaultfd */360361362