memstress.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020, Google LLC.
  4. */
  5. #include <inttypes.h>
  6. #include <linux/bitmap.h>
  7. #include "kvm_util.h"
  8. #include "memstress.h"
  9. #include "processor.h"
  10. #include "ucall_common.h"
  11. struct memstress_args memstress_args;
  12. /*
  13. * Guest virtual memory offset of the testing memory slot.
  14. * Must not conflict with identity mapped test code.
  15. */
  16. static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
  17. struct vcpu_thread {
  18. /* The index of the vCPU. */
  19. int vcpu_idx;
  20. /* The pthread backing the vCPU. */
  21. pthread_t thread;
  22. /* Set to true once the vCPU thread is up and running. */
  23. bool running;
  24. };
  25. /* The vCPU threads involved in this test. */
  26. static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS];
  27. /* The function run by each vCPU thread, as provided by the test. */
  28. static void (*vcpu_thread_fn)(struct memstress_vcpu_args *);
  29. /* Set to true once all vCPU threads are up and running. */
  30. static bool all_vcpu_threads_running;
  31. static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
  32. /*
  33. * Continuously write to the first 8 bytes of each page in the
  34. * specified region.
  35. */
  36. void memstress_guest_code(uint32_t vcpu_idx)
  37. {
  38. struct memstress_args *args = &memstress_args;
  39. struct memstress_vcpu_args *vcpu_args = &args->vcpu_args[vcpu_idx];
  40. struct guest_random_state rand_state;
  41. uint64_t gva;
  42. uint64_t pages;
  43. uint64_t addr;
  44. uint64_t page;
  45. int i;
  46. rand_state = new_guest_random_state(guest_random_seed + vcpu_idx);
  47. gva = vcpu_args->gva;
  48. pages = vcpu_args->pages;
  49. /* Make sure vCPU args data structure is not corrupt. */
  50. GUEST_ASSERT(vcpu_args->vcpu_idx == vcpu_idx);
  51. while (true) {
  52. for (i = 0; i < sizeof(memstress_args); i += args->guest_page_size)
  53. (void) *((volatile char *)args + i);
  54. for (i = 0; i < pages; i++) {
  55. if (args->random_access)
  56. page = guest_random_u32(&rand_state) % pages;
  57. else
  58. page = i;
  59. addr = gva + (page * args->guest_page_size);
  60. if (__guest_random_bool(&rand_state, args->write_percent))
  61. *(uint64_t *)addr = 0x0123456789ABCDEF;
  62. else
  63. READ_ONCE(*(uint64_t *)addr);
  64. }
  65. GUEST_SYNC(1);
  66. }
  67. }
  68. void memstress_setup_vcpus(struct kvm_vm *vm, int nr_vcpus,
  69. struct kvm_vcpu *vcpus[],
  70. uint64_t vcpu_memory_bytes,
  71. bool partition_vcpu_memory_access)
  72. {
  73. struct memstress_args *args = &memstress_args;
  74. struct memstress_vcpu_args *vcpu_args;
  75. int i;
  76. for (i = 0; i < nr_vcpus; i++) {
  77. vcpu_args = &args->vcpu_args[i];
  78. vcpu_args->vcpu = vcpus[i];
  79. vcpu_args->vcpu_idx = i;
  80. if (partition_vcpu_memory_access) {
  81. vcpu_args->gva = guest_test_virt_mem +
  82. (i * vcpu_memory_bytes);
  83. vcpu_args->pages = vcpu_memory_bytes /
  84. args->guest_page_size;
  85. vcpu_args->gpa = args->gpa + (i * vcpu_memory_bytes);
  86. } else {
  87. vcpu_args->gva = guest_test_virt_mem;
  88. vcpu_args->pages = (nr_vcpus * vcpu_memory_bytes) /
  89. args->guest_page_size;
  90. vcpu_args->gpa = args->gpa;
  91. }
  92. vcpu_args_set(vcpus[i], 1, i);
  93. pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
  94. i, vcpu_args->gpa, vcpu_args->gpa +
  95. (vcpu_args->pages * args->guest_page_size));
  96. }
  97. }
  98. struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus,
  99. uint64_t vcpu_memory_bytes, int slots,
  100. enum vm_mem_backing_src_type backing_src,
  101. bool partition_vcpu_memory_access)
  102. {
  103. struct memstress_args *args = &memstress_args;
  104. struct kvm_vm *vm;
  105. uint64_t guest_num_pages, slot0_pages = 0;
  106. uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);
  107. uint64_t region_end_gfn;
  108. int i;
  109. pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
  110. /* By default vCPUs will write to memory. */
  111. args->write_percent = 100;
  112. /*
  113. * Snapshot the non-huge page size. This is used by the guest code to
  114. * access/dirty pages at the logging granularity.
  115. */
  116. args->guest_page_size = vm_guest_mode_params[mode].page_size;
  117. guest_num_pages = vm_adjust_num_guest_pages(mode,
  118. (nr_vcpus * vcpu_memory_bytes) / args->guest_page_size);
  119. TEST_ASSERT(vcpu_memory_bytes % getpagesize() == 0,
  120. "Guest memory size is not host page size aligned.");
  121. TEST_ASSERT(vcpu_memory_bytes % args->guest_page_size == 0,
  122. "Guest memory size is not guest page size aligned.");
  123. TEST_ASSERT(guest_num_pages % slots == 0,
  124. "Guest memory cannot be evenly divided into %d slots.",
  125. slots);
  126. /*
  127. * If using nested, allocate extra pages for the nested page tables and
  128. * in-memory data structures.
  129. */
  130. if (args->nested)
  131. slot0_pages += memstress_nested_pages(nr_vcpus);
  132. /*
  133. * Pass guest_num_pages to populate the page tables for test memory.
  134. * The memory is also added to memslot 0, but that's a benign side
  135. * effect as KVM allows aliasing HVAs in meslots.
  136. */
  137. vm = __vm_create_with_vcpus(VM_SHAPE(mode), nr_vcpus,
  138. slot0_pages + guest_num_pages,
  139. memstress_guest_code, vcpus);
  140. args->vm = vm;
  141. /* Put the test region at the top guest physical memory. */
  142. region_end_gfn = vm->max_gfn + 1;
  143. #ifdef __x86_64__
  144. /*
  145. * When running vCPUs in L2, restrict the test region to 48 bits to
  146. * avoid needing 5-level page tables to identity map L2.
  147. */
  148. if (args->nested)
  149. region_end_gfn = min(region_end_gfn, (1UL << 48) / args->guest_page_size);
  150. #endif
  151. /*
  152. * If there should be more memory in the guest test region than there
  153. * can be pages in the guest, it will definitely cause problems.
  154. */
  155. TEST_ASSERT(guest_num_pages < region_end_gfn,
  156. "Requested more guest memory than address space allows.\n"
  157. " guest pages: %" PRIx64 " max gfn: %" PRIx64
  158. " nr_vcpus: %d wss: %" PRIx64 "]",
  159. guest_num_pages, region_end_gfn - 1, nr_vcpus, vcpu_memory_bytes);
  160. args->gpa = (region_end_gfn - guest_num_pages - 1) * args->guest_page_size;
  161. args->gpa = align_down(args->gpa, backing_src_pagesz);
  162. #ifdef __s390x__
  163. /* Align to 1M (segment size) */
  164. args->gpa = align_down(args->gpa, 1 << 20);
  165. #endif
  166. args->size = guest_num_pages * args->guest_page_size;
  167. pr_info("guest physical test memory: [0x%lx, 0x%lx)\n",
  168. args->gpa, args->gpa + args->size);
  169. /* Add extra memory slots for testing */
  170. for (i = 0; i < slots; i++) {
  171. uint64_t region_pages = guest_num_pages / slots;
  172. vm_paddr_t region_start = args->gpa + region_pages * args->guest_page_size * i;
  173. vm_userspace_mem_region_add(vm, backing_src, region_start,
  174. MEMSTRESS_MEM_SLOT_INDEX + i,
  175. region_pages, 0);
  176. }
  177. /* Do mapping for the demand paging memory slot */
  178. virt_map(vm, guest_test_virt_mem, args->gpa, guest_num_pages);
  179. memstress_setup_vcpus(vm, nr_vcpus, vcpus, vcpu_memory_bytes,
  180. partition_vcpu_memory_access);
  181. if (args->nested) {
  182. pr_info("Configuring vCPUs to run in L2 (nested).\n");
  183. memstress_setup_nested(vm, nr_vcpus, vcpus);
  184. }
  185. /* Export the shared variables to the guest. */
  186. sync_global_to_guest(vm, memstress_args);
  187. return vm;
  188. }
  189. void memstress_destroy_vm(struct kvm_vm *vm)
  190. {
  191. kvm_vm_free(vm);
  192. }
  193. void memstress_set_write_percent(struct kvm_vm *vm, uint32_t write_percent)
  194. {
  195. memstress_args.write_percent = write_percent;
  196. sync_global_to_guest(vm, memstress_args.write_percent);
  197. }
  198. void memstress_set_random_access(struct kvm_vm *vm, bool random_access)
  199. {
  200. memstress_args.random_access = random_access;
  201. sync_global_to_guest(vm, memstress_args.random_access);
  202. }
  203. uint64_t __weak memstress_nested_pages(int nr_vcpus)
  204. {
  205. return 0;
  206. }
  207. void __weak memstress_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu **vcpus)
  208. {
  209. pr_info("%s() not support on this architecture, skipping.\n", __func__);
  210. exit(KSFT_SKIP);
  211. }
  212. static void *vcpu_thread_main(void *data)
  213. {
  214. struct vcpu_thread *vcpu = data;
  215. int vcpu_idx = vcpu->vcpu_idx;
  216. if (memstress_args.pin_vcpus)
  217. pin_self_to_cpu(memstress_args.vcpu_to_pcpu[vcpu_idx]);
  218. WRITE_ONCE(vcpu->running, true);
  219. /*
  220. * Wait for all vCPU threads to be up and running before calling the test-
  221. * provided vCPU thread function. This prevents thread creation (which
  222. * requires taking the mmap_sem in write mode) from interfering with the
  223. * guest faulting in its memory.
  224. */
  225. while (!READ_ONCE(all_vcpu_threads_running))
  226. ;
  227. vcpu_thread_fn(&memstress_args.vcpu_args[vcpu_idx]);
  228. return NULL;
  229. }
  230. void memstress_start_vcpu_threads(int nr_vcpus,
  231. void (*vcpu_fn)(struct memstress_vcpu_args *))
  232. {
  233. int i;
  234. vcpu_thread_fn = vcpu_fn;
  235. WRITE_ONCE(all_vcpu_threads_running, false);
  236. WRITE_ONCE(memstress_args.stop_vcpus, false);
  237. for (i = 0; i < nr_vcpus; i++) {
  238. struct vcpu_thread *vcpu = &vcpu_threads[i];
  239. vcpu->vcpu_idx = i;
  240. WRITE_ONCE(vcpu->running, false);
  241. pthread_create(&vcpu->thread, NULL, vcpu_thread_main, vcpu);
  242. }
  243. for (i = 0; i < nr_vcpus; i++) {
  244. while (!READ_ONCE(vcpu_threads[i].running))
  245. ;
  246. }
  247. WRITE_ONCE(all_vcpu_threads_running, true);
  248. }
  249. void memstress_join_vcpu_threads(int nr_vcpus)
  250. {
  251. int i;
  252. WRITE_ONCE(memstress_args.stop_vcpus, true);
  253. for (i = 0; i < nr_vcpus; i++)
  254. pthread_join(vcpu_threads[i].thread, NULL);
  255. }
  256. static void toggle_dirty_logging(struct kvm_vm *vm, int slots, bool enable)
  257. {
  258. int i;
  259. for (i = 0; i < slots; i++) {
  260. int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
  261. int flags = enable ? KVM_MEM_LOG_DIRTY_PAGES : 0;
  262. vm_mem_region_set_flags(vm, slot, flags);
  263. }
  264. }
  265. void memstress_enable_dirty_logging(struct kvm_vm *vm, int slots)
  266. {
  267. toggle_dirty_logging(vm, slots, true);
  268. }
  269. void memstress_disable_dirty_logging(struct kvm_vm *vm, int slots)
  270. {
  271. toggle_dirty_logging(vm, slots, false);
  272. }
  273. void memstress_get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots)
  274. {
  275. int i;
  276. for (i = 0; i < slots; i++) {
  277. int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
  278. kvm_vm_get_dirty_log(vm, slot, bitmaps[i]);
  279. }
  280. }
  281. void memstress_clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[],
  282. int slots, uint64_t pages_per_slot)
  283. {
  284. int i;
  285. for (i = 0; i < slots; i++) {
  286. int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
  287. kvm_vm_clear_dirty_log(vm, slot, bitmaps[i], 0, pages_per_slot);
  288. }
  289. }
  290. unsigned long **memstress_alloc_bitmaps(int slots, uint64_t pages_per_slot)
  291. {
  292. unsigned long **bitmaps;
  293. int i;
  294. bitmaps = malloc(slots * sizeof(bitmaps[0]));
  295. TEST_ASSERT(bitmaps, "Failed to allocate bitmaps array.");
  296. for (i = 0; i < slots; i++) {
  297. bitmaps[i] = bitmap_zalloc(pages_per_slot);
  298. TEST_ASSERT(bitmaps[i], "Failed to allocate slot bitmap.");
  299. }
  300. return bitmaps;
  301. }
  302. void memstress_free_bitmaps(unsigned long *bitmaps[], int slots)
  303. {
  304. int i;
  305. for (i = 0; i < slots; i++)
  306. free(bitmaps[i]);
  307. free(bitmaps);
  308. }