Path: blob/master/tools/testing/selftests/kvm/memslot_modification_stress_test.c
38189 views
// SPDX-License-Identifier: GPL-2.01/*2* KVM memslot modification stress test3* Adapted from demand_paging_test.c4*5* Copyright (C) 2018, Red Hat, Inc.6* Copyright (C) 2020, Google, Inc.7*/8#include <stdio.h>9#include <stdlib.h>10#include <sys/syscall.h>11#include <unistd.h>12#include <asm/unistd.h>13#include <time.h>14#include <poll.h>15#include <pthread.h>16#include <linux/bitmap.h>17#include <linux/bitops.h>18#include <linux/userfaultfd.h>1920#include "memstress.h"21#include "processor.h"22#include "test_util.h"23#include "guest_modes.h"24#include "ucall_common.h"2526#define DUMMY_MEMSLOT_INDEX 72728#define DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS 10293031static int nr_vcpus = 1;32static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;3334static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)35{36struct kvm_vcpu *vcpu = vcpu_args->vcpu;37struct kvm_run *run;38int ret;3940run = vcpu->run;4142/* Let the guest access its memory until a stop signal is received */43while (!READ_ONCE(memstress_args.stop_vcpus)) {44ret = _vcpu_run(vcpu);45TEST_ASSERT(ret == 0, "vcpu_run failed: %d", ret);4647if (get_ucall(vcpu, NULL) == UCALL_SYNC)48continue;4950TEST_ASSERT(false,51"Invalid guest sync status: exit_reason=%s\n",52exit_reason_str(run->exit_reason));53}54}5556static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,57uint64_t nr_modifications)58{59uint64_t pages = max_t(int, vm->page_size, getpagesize()) / vm->page_size;60uint64_t gpa;61int i;6263/*64* Add the dummy memslot just below the memstress memslot, which is65* at the top of the guest physical address space.66*/67gpa = memstress_args.gpa - pages * vm->page_size;6869for (i = 0; i < nr_modifications; i++) {70usleep(delay);71vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,72DUMMY_MEMSLOT_INDEX, pages, 0);7374vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);75}76}7778struct test_params {79useconds_t delay;80uint64_t nr_iterations;81bool partition_vcpu_memory_access;82bool disable_slot_zap_quirk;83};8485static void run_test(enum vm_guest_mode mode, void *arg)86{87struct test_params *p = arg;88struct kvm_vm *vm;8990vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,91VM_MEM_SRC_ANONYMOUS,92p->partition_vcpu_memory_access);93#ifdef __x86_64__94if (p->disable_slot_zap_quirk)95vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_SLOT_ZAP_ALL);9697pr_info("Memslot zap quirk %s\n", p->disable_slot_zap_quirk ?98"disabled" : "enabled");99#endif100101pr_info("Finished creating vCPUs\n");102103memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);104105pr_info("Started all vCPUs\n");106107add_remove_memslot(vm, p->delay, p->nr_iterations);108109memstress_join_vcpu_threads(nr_vcpus);110pr_info("All vCPU threads joined\n");111112memstress_destroy_vm(vm);113}114115static void help(char *name)116{117puts("");118printf("usage: %s [-h] [-m mode] [-d delay_usec] [-q]\n"119" [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);120guest_modes_help();121printf(" -d: add a delay between each iteration of adding and\n"122" deleting a memslot in usec.\n");123printf(" -q: Disable memslot zap quirk.\n");124printf(" -b: specify the size of the memory region which should be\n"125" accessed by each vCPU. e.g. 10M or 3G.\n"126" Default: 1G\n");127printf(" -v: specify the number of vCPUs to run.\n");128printf(" -o: Overlap guest memory accesses instead of partitioning\n"129" them into a separate region of memory for each vCPU.\n");130printf(" -i: specify the number of iterations of adding and removing\n"131" a memslot.\n"132" Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);133puts("");134exit(0);135}136137int main(int argc, char *argv[])138{139int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);140int opt;141struct test_params p = {142.delay = 0,143.nr_iterations = DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,144.partition_vcpu_memory_access = true145};146147guest_modes_append_default();148149while ((opt = getopt(argc, argv, "hm:d:qb:v:oi:")) != -1) {150switch (opt) {151case 'm':152guest_modes_cmdline(optarg);153break;154case 'd':155p.delay = atoi_non_negative("Delay", optarg);156break;157case 'b':158guest_percpu_mem_size = parse_size(optarg);159break;160case 'v':161nr_vcpus = atoi_positive("Number of vCPUs", optarg);162TEST_ASSERT(nr_vcpus <= max_vcpus,163"Invalid number of vcpus, must be between 1 and %d",164max_vcpus);165break;166case 'o':167p.partition_vcpu_memory_access = false;168break;169case 'i':170p.nr_iterations = atoi_positive("Number of iterations", optarg);171break;172#ifdef __x86_64__173case 'q':174p.disable_slot_zap_quirk = true;175176TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &177KVM_X86_QUIRK_SLOT_ZAP_ALL);178break;179#endif180case 'h':181default:182help(argv[0]);183break;184}185}186187for_each_guest_mode(run_test, &p);188189return 0;190}191192193