1 // SPDX-License-Identifier: GPL-2.0
3 * KVM memslot modification stress test
4 * Adapted from demand_paging_test.c
6 * Copyright (C) 2018, Red Hat, Inc.
7 * Copyright (C) 2020, Google, Inc.
11 #include <sys/syscall.h>
13 #include <asm/unistd.h>
17 #include <linux/bitmap.h>
18 #include <linux/bitops.h>
19 #include <linux/userfaultfd.h>
21 #include "memstress.h"
22 #include "processor.h"
23 #include "test_util.h"
24 #include "guest_modes.h"
26 #define DUMMY_MEMSLOT_INDEX 7
28 #define DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS 10
31 static int nr_vcpus = 1;
32 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
34 static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)
36 struct kvm_vcpu *vcpu = vcpu_args->vcpu;
42 /* Let the guest access its memory until a stop signal is received */
43 while (!READ_ONCE(memstress_args.stop_vcpus)) {
44 ret = _vcpu_run(vcpu);
45 TEST_ASSERT(ret == 0, "vcpu_run failed: %d", ret);
47 if (get_ucall(vcpu, NULL) == UCALL_SYNC)
51 "Invalid guest sync status: exit_reason=%s\n",
52 exit_reason_str(run->exit_reason));
56 static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
57 uint64_t nr_modifications)
59 uint64_t pages = max_t(int, vm->page_size, getpagesize()) / vm->page_size;
64 * Add the dummy memslot just below the memstress memslot, which is
65 * at the top of the guest physical address space.
67 gpa = memstress_args.gpa - pages * vm->page_size;
69 for (i = 0; i < nr_modifications; i++) {
71 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,
72 DUMMY_MEMSLOT_INDEX, pages, 0);
74 vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);
80 uint64_t nr_iterations;
81 bool partition_vcpu_memory_access;
82 bool disable_slot_zap_quirk;
85 static void run_test(enum vm_guest_mode mode, void *arg)
87 struct test_params *p = arg;
90 vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
92 p->partition_vcpu_memory_access);
94 if (p->disable_slot_zap_quirk)
95 vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_SLOT_ZAP_ALL);
97 pr_info("Memslot zap quirk %s\n", p->disable_slot_zap_quirk ?
98 "disabled" : "enabled");
101 pr_info("Finished creating vCPUs\n");
103 memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);
105 pr_info("Started all vCPUs\n");
107 add_remove_memslot(vm, p->delay, p->nr_iterations);
109 memstress_join_vcpu_threads(nr_vcpus);
110 pr_info("All vCPU threads joined\n");
112 memstress_destroy_vm(vm);
115 static void help(char *name)
118 printf("usage: %s [-h] [-m mode] [-d delay_usec] [-q]\n"
119 " [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
121 printf(" -d: add a delay between each iteration of adding and\n"
122 " deleting a memslot in usec.\n");
123 printf(" -q: Disable memslot zap quirk.\n");
124 printf(" -b: specify the size of the memory region which should be\n"
125 " accessed by each vCPU. e.g. 10M or 3G.\n"
127 printf(" -v: specify the number of vCPUs to run.\n");
128 printf(" -o: Overlap guest memory accesses instead of partitioning\n"
129 " them into a separate region of memory for each vCPU.\n");
130 printf(" -i: specify the number of iterations of adding and removing\n"
132 " Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);
137 int main(int argc, char *argv[])
139 int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
141 struct test_params p = {
143 .nr_iterations = DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,
144 .partition_vcpu_memory_access = true
147 guest_modes_append_default();
149 while ((opt = getopt(argc, argv, "hm:d:qb:v:oi:")) != -1) {
152 guest_modes_cmdline(optarg);
155 p.delay = atoi_non_negative("Delay", optarg);
158 guest_percpu_mem_size = parse_size(optarg);
161 nr_vcpus = atoi_positive("Number of vCPUs", optarg);
162 TEST_ASSERT(nr_vcpus <= max_vcpus,
163 "Invalid number of vcpus, must be between 1 and %d",
167 p.partition_vcpu_memory_access = false;
170 p.nr_iterations = atoi_positive("Number of iterations", optarg);
174 p.disable_slot_zap_quirk = true;
176 TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
177 KVM_X86_QUIRK_SLOT_ZAP_ALL);
187 for_each_guest_mode(run_test, &p);