linux/tools/testing/selftests/kvm/memslot_modification_stress_test.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * KVM memslot modification stress test
   4 * Adapted from demand_paging_test.c
   5 *
   6 * Copyright (C) 2018, Red Hat, Inc.
   7 * Copyright (C) 2020, Google, Inc.
   8 */
   9
  10#define _GNU_SOURCE /* for program_invocation_name */
  11
  12#include <stdio.h>
  13#include <stdlib.h>
  14#include <sys/syscall.h>
  15#include <unistd.h>
  16#include <asm/unistd.h>
  17#include <time.h>
  18#include <poll.h>
  19#include <pthread.h>
  20#include <linux/bitmap.h>
  21#include <linux/bitops.h>
  22#include <linux/userfaultfd.h>
  23
  24#include "perf_test_util.h"
  25#include "processor.h"
  26#include "test_util.h"
  27#include "guest_modes.h"
  28
  29#define DUMMY_MEMSLOT_INDEX 7
  30
  31#define DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS 10
  32
  33
  34static int nr_vcpus = 1;
  35static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
  36
  37static bool run_vcpus = true;
  38
  39static void *vcpu_worker(void *data)
  40{
  41        int ret;
  42        struct perf_test_vcpu_args *vcpu_args =
  43                (struct perf_test_vcpu_args *)data;
  44        int vcpu_id = vcpu_args->vcpu_id;
  45        struct kvm_vm *vm = perf_test_args.vm;
  46        struct kvm_run *run;
  47
  48        vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
  49        run = vcpu_state(vm, vcpu_id);
  50
  51        /* Let the guest access its memory until a stop signal is received */
  52        while (READ_ONCE(run_vcpus)) {
  53                ret = _vcpu_run(vm, vcpu_id);
  54                TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
  55
  56                if (get_ucall(vm, vcpu_id, NULL) == UCALL_SYNC)
  57                        continue;
  58
  59                TEST_ASSERT(false,
  60                            "Invalid guest sync status: exit_reason=%s\n",
  61                            exit_reason_str(run->exit_reason));
  62        }
  63
  64        return NULL;
  65}
  66
  67struct memslot_antagonist_args {
  68        struct kvm_vm *vm;
  69        useconds_t delay;
  70        uint64_t nr_modifications;
  71};
  72
  73static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
  74                               uint64_t nr_modifications)
  75{
  76        const uint64_t pages = 1;
  77        uint64_t gpa;
  78        int i;
  79
  80        /*
  81         * Add the dummy memslot just below the perf_test_util memslot, which is
  82         * at the top of the guest physical address space.
  83         */
  84        gpa = guest_test_phys_mem - pages * vm_get_page_size(vm);
  85
  86        for (i = 0; i < nr_modifications; i++) {
  87                usleep(delay);
  88                vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,
  89                                            DUMMY_MEMSLOT_INDEX, pages, 0);
  90
  91                vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);
  92        }
  93}
  94
  95struct test_params {
  96        useconds_t memslot_modification_delay;
  97        uint64_t nr_memslot_modifications;
  98        bool partition_vcpu_memory_access;
  99};
 100
 101static void run_test(enum vm_guest_mode mode, void *arg)
 102{
 103        struct test_params *p = arg;
 104        pthread_t *vcpu_threads;
 105        struct kvm_vm *vm;
 106        int vcpu_id;
 107
 108        vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size,
 109                                 VM_MEM_SRC_ANONYMOUS);
 110
 111        perf_test_args.wr_fract = 1;
 112
 113        vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
 114        TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 115
 116        perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
 117                              p->partition_vcpu_memory_access);
 118
 119        /* Export the shared variables to the guest */
 120        sync_global_to_guest(vm, perf_test_args);
 121
 122        pr_info("Finished creating vCPUs\n");
 123
 124        for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
 125                pthread_create(&vcpu_threads[vcpu_id], NULL, vcpu_worker,
 126                               &perf_test_args.vcpu_args[vcpu_id]);
 127
 128        pr_info("Started all vCPUs\n");
 129
 130        add_remove_memslot(vm, p->memslot_modification_delay,
 131                           p->nr_memslot_modifications);
 132
 133        run_vcpus = false;
 134
 135        /* Wait for the vcpu threads to quit */
 136        for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
 137                pthread_join(vcpu_threads[vcpu_id], NULL);
 138
 139        pr_info("All vCPU threads joined\n");
 140
 141        ucall_uninit(vm);
 142        kvm_vm_free(vm);
 143
 144        free(vcpu_threads);
 145}
 146
 147static void help(char *name)
 148{
 149        puts("");
 150        printf("usage: %s [-h] [-m mode] [-d delay_usec]\n"
 151               "          [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
 152        guest_modes_help();
 153        printf(" -d: add a delay between each iteration of adding and\n"
 154               "     deleting a memslot in usec.\n");
 155        printf(" -b: specify the size of the memory region which should be\n"
 156               "     accessed by each vCPU. e.g. 10M or 3G.\n"
 157               "     Default: 1G\n");
 158        printf(" -v: specify the number of vCPUs to run.\n");
 159        printf(" -o: Overlap guest memory accesses instead of partitioning\n"
 160               "     them into a separate region of memory for each vCPU.\n");
 161        printf(" -i: specify the number of iterations of adding and removing\n"
 162               "     a memslot.\n"
 163               "     Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);
 164        puts("");
 165        exit(0);
 166}
 167
 168int main(int argc, char *argv[])
 169{
 170        int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
 171        int opt;
 172        struct test_params p = {
 173                .memslot_modification_delay = 0,
 174                .nr_memslot_modifications =
 175                        DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,
 176                .partition_vcpu_memory_access = true
 177        };
 178
 179        guest_modes_append_default();
 180
 181        while ((opt = getopt(argc, argv, "hm:d:b:v:oi:")) != -1) {
 182                switch (opt) {
 183                case 'm':
 184                        guest_modes_cmdline(optarg);
 185                        break;
 186                case 'd':
 187                        p.memslot_modification_delay = strtoul(optarg, NULL, 0);
 188                        TEST_ASSERT(p.memslot_modification_delay >= 0,
 189                                    "A negative delay is not supported.");
 190                        break;
 191                case 'b':
 192                        guest_percpu_mem_size = parse_size(optarg);
 193                        break;
 194                case 'v':
 195                        nr_vcpus = atoi(optarg);
 196                        TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
 197                                    "Invalid number of vcpus, must be between 1 and %d",
 198                                    max_vcpus);
 199                        break;
 200                case 'o':
 201                        p.partition_vcpu_memory_access = false;
 202                        break;
 203                case 'i':
 204                        p.nr_memslot_modifications = atoi(optarg);
 205                        break;
 206                case 'h':
 207                default:
 208                        help(argv[0]);
 209                        break;
 210                }
 211        }
 212
 213        for_each_guest_mode(run_test, &p);
 214
 215        return 0;
 216}
 217