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.
10 #define _GNU_SOURCE /* for program_invocation_name */
14 #include <sys/syscall.h>
16 #include <asm/unistd.h>
20 #include <linux/bitmap.h>
21 #include <linux/bitops.h>
22 #include <linux/userfaultfd.h>
24 #include "memstress.h"
25 #include "processor.h"
26 #include "test_util.h"
27 #include "guest_modes.h"
29 #define DUMMY_MEMSLOT_INDEX 7
31 #define DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS 10
34 static int nr_vcpus = 1;
35 static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
37 static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)
39 struct kvm_vcpu *vcpu = vcpu_args->vcpu;
45 /* Let the guest access its memory until a stop signal is received */
46 while (!READ_ONCE(memstress_args.stop_vcpus)) {
47 ret = _vcpu_run(vcpu);
48 TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
50 if (get_ucall(vcpu, NULL) == UCALL_SYNC)
54 "Invalid guest sync status: exit_reason=%s\n",
55 exit_reason_str(run->exit_reason));
59 struct memslot_antagonist_args {
62 uint64_t nr_modifications;
65 static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
66 uint64_t nr_modifications)
68 uint64_t pages = max_t(int, vm->page_size, getpagesize()) / vm->page_size;
73 * Add the dummy memslot just below the memstress memslot, which is
74 * at the top of the guest physical address space.
76 gpa = memstress_args.gpa - pages * vm->page_size;
78 for (i = 0; i < nr_modifications; i++) {
80 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,
81 DUMMY_MEMSLOT_INDEX, pages, 0);
83 vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);
89 uint64_t nr_iterations;
90 bool partition_vcpu_memory_access;
93 static void run_test(enum vm_guest_mode mode, void *arg)
95 struct test_params *p = arg;
98 vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
100 p->partition_vcpu_memory_access);
102 pr_info("Finished creating vCPUs\n");
104 memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);
106 pr_info("Started all vCPUs\n");
108 add_remove_memslot(vm, p->delay, p->nr_iterations);
110 memstress_join_vcpu_threads(nr_vcpus);
111 pr_info("All vCPU threads joined\n");
113 memstress_destroy_vm(vm);
116 static void help(char *name)
119 printf("usage: %s [-h] [-m mode] [-d delay_usec]\n"
120 " [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
122 printf(" -d: add a delay between each iteration of adding and\n"
123 " deleting a memslot in usec.\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:b: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);
179 for_each_guest_mode(run_test, &p);