memslot_modification_stress_test.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/syscall.h>
  12. #include <unistd.h>
  13. #include <asm/unistd.h>
  14. #include <time.h>
  15. #include <poll.h>
  16. #include <pthread.h>
  17. #include <linux/bitmap.h>
  18. #include <linux/bitops.h>
  19. #include <linux/userfaultfd.h>
  20. #include "memstress.h"
  21. #include "processor.h"
  22. #include "test_util.h"
  23. #include "guest_modes.h"
  24. #define DUMMY_MEMSLOT_INDEX 7
  25. #define DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS 10
  26. static int nr_vcpus = 1;
  27. static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
  28. static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)
  29. {
  30. struct kvm_vcpu *vcpu = vcpu_args->vcpu;
  31. struct kvm_run *run;
  32. int ret;
  33. run = vcpu->run;
  34. /* Let the guest access its memory until a stop signal is received */
  35. while (!READ_ONCE(memstress_args.stop_vcpus)) {
  36. ret = _vcpu_run(vcpu);
  37. TEST_ASSERT(ret == 0, "vcpu_run failed: %d", ret);
  38. if (get_ucall(vcpu, NULL) == UCALL_SYNC)
  39. continue;
  40. TEST_ASSERT(false,
  41. "Invalid guest sync status: exit_reason=%s\n",
  42. exit_reason_str(run->exit_reason));
  43. }
  44. }
  45. static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
  46. uint64_t nr_modifications)
  47. {
  48. uint64_t pages = max_t(int, vm->page_size, getpagesize()) / vm->page_size;
  49. uint64_t gpa;
  50. int i;
  51. /*
  52. * Add the dummy memslot just below the memstress memslot, which is
  53. * at the top of the guest physical address space.
  54. */
  55. gpa = memstress_args.gpa - pages * vm->page_size;
  56. for (i = 0; i < nr_modifications; i++) {
  57. usleep(delay);
  58. vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa,
  59. DUMMY_MEMSLOT_INDEX, pages, 0);
  60. vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX);
  61. }
  62. }
  63. struct test_params {
  64. useconds_t delay;
  65. uint64_t nr_iterations;
  66. bool partition_vcpu_memory_access;
  67. bool disable_slot_zap_quirk;
  68. };
  69. static void run_test(enum vm_guest_mode mode, void *arg)
  70. {
  71. struct test_params *p = arg;
  72. struct kvm_vm *vm;
  73. vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
  74. VM_MEM_SRC_ANONYMOUS,
  75. p->partition_vcpu_memory_access);
  76. #ifdef __x86_64__
  77. if (p->disable_slot_zap_quirk)
  78. vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, KVM_X86_QUIRK_SLOT_ZAP_ALL);
  79. pr_info("Memslot zap quirk %s\n", p->disable_slot_zap_quirk ?
  80. "disabled" : "enabled");
  81. #endif
  82. pr_info("Finished creating vCPUs\n");
  83. memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);
  84. pr_info("Started all vCPUs\n");
  85. add_remove_memslot(vm, p->delay, p->nr_iterations);
  86. memstress_join_vcpu_threads(nr_vcpus);
  87. pr_info("All vCPU threads joined\n");
  88. memstress_destroy_vm(vm);
  89. }
  90. static void help(char *name)
  91. {
  92. puts("");
  93. printf("usage: %s [-h] [-m mode] [-d delay_usec] [-q]\n"
  94. " [-b memory] [-v vcpus] [-o] [-i iterations]\n", name);
  95. guest_modes_help();
  96. printf(" -d: add a delay between each iteration of adding and\n"
  97. " deleting a memslot in usec.\n");
  98. printf(" -q: Disable memslot zap quirk.\n");
  99. printf(" -b: specify the size of the memory region which should be\n"
  100. " accessed by each vCPU. e.g. 10M or 3G.\n"
  101. " Default: 1G\n");
  102. printf(" -v: specify the number of vCPUs to run.\n");
  103. printf(" -o: Overlap guest memory accesses instead of partitioning\n"
  104. " them into a separate region of memory for each vCPU.\n");
  105. printf(" -i: specify the number of iterations of adding and removing\n"
  106. " a memslot.\n"
  107. " Default: %d\n", DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS);
  108. puts("");
  109. exit(0);
  110. }
  111. int main(int argc, char *argv[])
  112. {
  113. int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
  114. int opt;
  115. struct test_params p = {
  116. .delay = 0,
  117. .nr_iterations = DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS,
  118. .partition_vcpu_memory_access = true
  119. };
  120. guest_modes_append_default();
  121. while ((opt = getopt(argc, argv, "hm:d:qb:v:oi:")) != -1) {
  122. switch (opt) {
  123. case 'm':
  124. guest_modes_cmdline(optarg);
  125. break;
  126. case 'd':
  127. p.delay = atoi_non_negative("Delay", optarg);
  128. break;
  129. case 'b':
  130. guest_percpu_mem_size = parse_size(optarg);
  131. break;
  132. case 'v':
  133. nr_vcpus = atoi_positive("Number of vCPUs", optarg);
  134. TEST_ASSERT(nr_vcpus <= max_vcpus,
  135. "Invalid number of vcpus, must be between 1 and %d",
  136. max_vcpus);
  137. break;
  138. case 'o':
  139. p.partition_vcpu_memory_access = false;
  140. break;
  141. case 'i':
  142. p.nr_iterations = atoi_positive("Number of iterations", optarg);
  143. break;
  144. #ifdef __x86_64__
  145. case 'q':
  146. p.disable_slot_zap_quirk = true;
  147. TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
  148. KVM_X86_QUIRK_SLOT_ZAP_ALL);
  149. break;
  150. #endif
  151. case 'h':
  152. default:
  153. help(argv[0]);
  154. break;
  155. }
  156. }
  157. for_each_guest_mode(run_test, &p);
  158. return 0;
  159. }