access_tracking_perf_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * access_tracking_perf_test
  4. *
  5. * Copyright (C) 2021, Google, Inc.
  6. *
  7. * This test measures the performance effects of KVM's access tracking.
  8. * Access tracking is driven by the MMU notifiers test_young, clear_young, and
  9. * clear_flush_young. These notifiers do not have a direct userspace API,
  10. * however the clear_young notifier can be triggered by marking a pages as idle
  11. * in /sys/kernel/mm/page_idle/bitmap. This test leverages that mechanism to
  12. * enable access tracking on guest memory.
  13. *
  14. * To measure performance this test runs a VM with a configurable number of
  15. * vCPUs that each touch every page in disjoint regions of memory. Performance
  16. * is measured in the time it takes all vCPUs to finish touching their
  17. * predefined region.
  18. *
  19. * Note that a deterministic correctness test of access tracking is not possible
  20. * by using page_idle as it exists today. This is for a few reasons:
  21. *
  22. * 1. page_idle only issues clear_young notifiers, which lack a TLB flush. This
  23. * means subsequent guest accesses are not guaranteed to see page table
  24. * updates made by KVM until some time in the future.
  25. *
  26. * 2. page_idle only operates on LRU pages. Newly allocated pages are not
  27. * immediately allocated to LRU lists. Instead they are held in a "pagevec",
  28. * which is drained to LRU lists some time in the future. There is no
  29. * userspace API to force this drain to occur.
  30. *
  31. * These limitations are worked around in this test by using a large enough
  32. * region of memory for each vCPU such that the number of translations cached in
  33. * the TLB and the number of pages held in pagevecs are a small fraction of the
  34. * overall workload. And if either of those conditions are not true (for example
  35. * in nesting, where TLB size is unlimited) this test will print a warning
  36. * rather than silently passing.
  37. */
  38. #include <inttypes.h>
  39. #include <limits.h>
  40. #include <pthread.h>
  41. #include <sys/mman.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include "kvm_util.h"
  45. #include "test_util.h"
  46. #include "memstress.h"
  47. #include "guest_modes.h"
  48. #include "processor.h"
  49. /* Global variable used to synchronize all of the vCPU threads. */
  50. static int iteration;
  51. /* Defines what vCPU threads should do during a given iteration. */
  52. static enum {
  53. /* Run the vCPU to access all its memory. */
  54. ITERATION_ACCESS_MEMORY,
  55. /* Mark the vCPU's memory idle in page_idle. */
  56. ITERATION_MARK_IDLE,
  57. } iteration_work;
  58. /* The iteration that was last completed by each vCPU. */
  59. static int vcpu_last_completed_iteration[KVM_MAX_VCPUS];
  60. /* Whether to overlap the regions of memory vCPUs access. */
  61. static bool overlap_memory_access;
  62. struct test_params {
  63. /* The backing source for the region of memory. */
  64. enum vm_mem_backing_src_type backing_src;
  65. /* The amount of memory to allocate for each vCPU. */
  66. uint64_t vcpu_memory_bytes;
  67. /* The number of vCPUs to create in the VM. */
  68. int nr_vcpus;
  69. };
  70. static uint64_t pread_uint64(int fd, const char *filename, uint64_t index)
  71. {
  72. uint64_t value;
  73. off_t offset = index * sizeof(value);
  74. TEST_ASSERT(pread(fd, &value, sizeof(value), offset) == sizeof(value),
  75. "pread from %s offset 0x%" PRIx64 " failed!",
  76. filename, offset);
  77. return value;
  78. }
  79. #define PAGEMAP_PRESENT (1ULL << 63)
  80. #define PAGEMAP_PFN_MASK ((1ULL << 55) - 1)
  81. static uint64_t lookup_pfn(int pagemap_fd, struct kvm_vm *vm, uint64_t gva)
  82. {
  83. uint64_t hva = (uint64_t) addr_gva2hva(vm, gva);
  84. uint64_t entry;
  85. uint64_t pfn;
  86. entry = pread_uint64(pagemap_fd, "pagemap", hva / getpagesize());
  87. if (!(entry & PAGEMAP_PRESENT))
  88. return 0;
  89. pfn = entry & PAGEMAP_PFN_MASK;
  90. __TEST_REQUIRE(pfn, "Looking up PFNs requires CAP_SYS_ADMIN");
  91. return pfn;
  92. }
  93. static bool is_page_idle(int page_idle_fd, uint64_t pfn)
  94. {
  95. uint64_t bits = pread_uint64(page_idle_fd, "page_idle", pfn / 64);
  96. return !!((bits >> (pfn % 64)) & 1);
  97. }
  98. static void mark_page_idle(int page_idle_fd, uint64_t pfn)
  99. {
  100. uint64_t bits = 1ULL << (pfn % 64);
  101. TEST_ASSERT(pwrite(page_idle_fd, &bits, 8, 8 * (pfn / 64)) == 8,
  102. "Set page_idle bits for PFN 0x%" PRIx64, pfn);
  103. }
  104. static void mark_vcpu_memory_idle(struct kvm_vm *vm,
  105. struct memstress_vcpu_args *vcpu_args)
  106. {
  107. int vcpu_idx = vcpu_args->vcpu_idx;
  108. uint64_t base_gva = vcpu_args->gva;
  109. uint64_t pages = vcpu_args->pages;
  110. uint64_t page;
  111. uint64_t still_idle = 0;
  112. uint64_t no_pfn = 0;
  113. int page_idle_fd;
  114. int pagemap_fd;
  115. /* If vCPUs are using an overlapping region, let vCPU 0 mark it idle. */
  116. if (overlap_memory_access && vcpu_idx)
  117. return;
  118. page_idle_fd = open("/sys/kernel/mm/page_idle/bitmap", O_RDWR);
  119. TEST_ASSERT(page_idle_fd > 0, "Failed to open page_idle.");
  120. pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
  121. TEST_ASSERT(pagemap_fd > 0, "Failed to open pagemap.");
  122. for (page = 0; page < pages; page++) {
  123. uint64_t gva = base_gva + page * memstress_args.guest_page_size;
  124. uint64_t pfn = lookup_pfn(pagemap_fd, vm, gva);
  125. if (!pfn) {
  126. no_pfn++;
  127. continue;
  128. }
  129. if (is_page_idle(page_idle_fd, pfn)) {
  130. still_idle++;
  131. continue;
  132. }
  133. mark_page_idle(page_idle_fd, pfn);
  134. }
  135. /*
  136. * Assumption: Less than 1% of pages are going to be swapped out from
  137. * under us during this test.
  138. */
  139. TEST_ASSERT(no_pfn < pages / 100,
  140. "vCPU %d: No PFN for %" PRIu64 " out of %" PRIu64 " pages.",
  141. vcpu_idx, no_pfn, pages);
  142. /*
  143. * Check that at least 90% of memory has been marked idle (the rest
  144. * might not be marked idle because the pages have not yet made it to an
  145. * LRU list or the translations are still cached in the TLB). 90% is
  146. * arbitrary; high enough that we ensure most memory access went through
  147. * access tracking but low enough as to not make the test too brittle
  148. * over time and across architectures.
  149. *
  150. * When running the guest as a nested VM, "warn" instead of asserting
  151. * as the TLB size is effectively unlimited and the KVM doesn't
  152. * explicitly flush the TLB when aging SPTEs. As a result, more pages
  153. * are cached and the guest won't see the "idle" bit cleared.
  154. */
  155. if (still_idle >= pages / 10) {
  156. #ifdef __x86_64__
  157. TEST_ASSERT(this_cpu_has(X86_FEATURE_HYPERVISOR),
  158. "vCPU%d: Too many pages still idle (%lu out of %lu)",
  159. vcpu_idx, still_idle, pages);
  160. #endif
  161. printf("WARNING: vCPU%d: Too many pages still idle (%lu out of %lu), "
  162. "this will affect performance results.\n",
  163. vcpu_idx, still_idle, pages);
  164. }
  165. close(page_idle_fd);
  166. close(pagemap_fd);
  167. }
  168. static void assert_ucall(struct kvm_vcpu *vcpu, uint64_t expected_ucall)
  169. {
  170. struct ucall uc;
  171. uint64_t actual_ucall = get_ucall(vcpu, &uc);
  172. TEST_ASSERT(expected_ucall == actual_ucall,
  173. "Guest exited unexpectedly (expected ucall %" PRIu64
  174. ", got %" PRIu64 ")",
  175. expected_ucall, actual_ucall);
  176. }
  177. static bool spin_wait_for_next_iteration(int *current_iteration)
  178. {
  179. int last_iteration = *current_iteration;
  180. do {
  181. if (READ_ONCE(memstress_args.stop_vcpus))
  182. return false;
  183. *current_iteration = READ_ONCE(iteration);
  184. } while (last_iteration == *current_iteration);
  185. return true;
  186. }
  187. static void vcpu_thread_main(struct memstress_vcpu_args *vcpu_args)
  188. {
  189. struct kvm_vcpu *vcpu = vcpu_args->vcpu;
  190. struct kvm_vm *vm = memstress_args.vm;
  191. int vcpu_idx = vcpu_args->vcpu_idx;
  192. int current_iteration = 0;
  193. while (spin_wait_for_next_iteration(&current_iteration)) {
  194. switch (READ_ONCE(iteration_work)) {
  195. case ITERATION_ACCESS_MEMORY:
  196. vcpu_run(vcpu);
  197. assert_ucall(vcpu, UCALL_SYNC);
  198. break;
  199. case ITERATION_MARK_IDLE:
  200. mark_vcpu_memory_idle(vm, vcpu_args);
  201. break;
  202. };
  203. vcpu_last_completed_iteration[vcpu_idx] = current_iteration;
  204. }
  205. }
  206. static void spin_wait_for_vcpu(int vcpu_idx, int target_iteration)
  207. {
  208. while (READ_ONCE(vcpu_last_completed_iteration[vcpu_idx]) !=
  209. target_iteration) {
  210. continue;
  211. }
  212. }
  213. /* The type of memory accesses to perform in the VM. */
  214. enum access_type {
  215. ACCESS_READ,
  216. ACCESS_WRITE,
  217. };
  218. static void run_iteration(struct kvm_vm *vm, int nr_vcpus, const char *description)
  219. {
  220. struct timespec ts_start;
  221. struct timespec ts_elapsed;
  222. int next_iteration, i;
  223. /* Kick off the vCPUs by incrementing iteration. */
  224. next_iteration = ++iteration;
  225. clock_gettime(CLOCK_MONOTONIC, &ts_start);
  226. /* Wait for all vCPUs to finish the iteration. */
  227. for (i = 0; i < nr_vcpus; i++)
  228. spin_wait_for_vcpu(i, next_iteration);
  229. ts_elapsed = timespec_elapsed(ts_start);
  230. pr_info("%-30s: %ld.%09lds\n",
  231. description, ts_elapsed.tv_sec, ts_elapsed.tv_nsec);
  232. }
  233. static void access_memory(struct kvm_vm *vm, int nr_vcpus,
  234. enum access_type access, const char *description)
  235. {
  236. memstress_set_write_percent(vm, (access == ACCESS_READ) ? 0 : 100);
  237. iteration_work = ITERATION_ACCESS_MEMORY;
  238. run_iteration(vm, nr_vcpus, description);
  239. }
  240. static void mark_memory_idle(struct kvm_vm *vm, int nr_vcpus)
  241. {
  242. /*
  243. * Even though this parallelizes the work across vCPUs, this is still a
  244. * very slow operation because page_idle forces the test to mark one pfn
  245. * at a time and the clear_young notifier serializes on the KVM MMU
  246. * lock.
  247. */
  248. pr_debug("Marking VM memory idle (slow)...\n");
  249. iteration_work = ITERATION_MARK_IDLE;
  250. run_iteration(vm, nr_vcpus, "Mark memory idle");
  251. }
  252. static void run_test(enum vm_guest_mode mode, void *arg)
  253. {
  254. struct test_params *params = arg;
  255. struct kvm_vm *vm;
  256. int nr_vcpus = params->nr_vcpus;
  257. vm = memstress_create_vm(mode, nr_vcpus, params->vcpu_memory_bytes, 1,
  258. params->backing_src, !overlap_memory_access);
  259. memstress_start_vcpu_threads(nr_vcpus, vcpu_thread_main);
  260. pr_info("\n");
  261. access_memory(vm, nr_vcpus, ACCESS_WRITE, "Populating memory");
  262. /* As a control, read and write to the populated memory first. */
  263. access_memory(vm, nr_vcpus, ACCESS_WRITE, "Writing to populated memory");
  264. access_memory(vm, nr_vcpus, ACCESS_READ, "Reading from populated memory");
  265. /* Repeat on memory that has been marked as idle. */
  266. mark_memory_idle(vm, nr_vcpus);
  267. access_memory(vm, nr_vcpus, ACCESS_WRITE, "Writing to idle memory");
  268. mark_memory_idle(vm, nr_vcpus);
  269. access_memory(vm, nr_vcpus, ACCESS_READ, "Reading from idle memory");
  270. memstress_join_vcpu_threads(nr_vcpus);
  271. memstress_destroy_vm(vm);
  272. }
  273. static void help(char *name)
  274. {
  275. puts("");
  276. printf("usage: %s [-h] [-m mode] [-b vcpu_bytes] [-v vcpus] [-o] [-s mem_type]\n",
  277. name);
  278. puts("");
  279. printf(" -h: Display this help message.");
  280. guest_modes_help();
  281. printf(" -b: specify the size of the memory region which should be\n"
  282. " dirtied by each vCPU. e.g. 10M or 3G.\n"
  283. " (default: 1G)\n");
  284. printf(" -v: specify the number of vCPUs to run.\n");
  285. printf(" -o: Overlap guest memory accesses instead of partitioning\n"
  286. " them into a separate region of memory for each vCPU.\n");
  287. backing_src_help("-s");
  288. puts("");
  289. exit(0);
  290. }
  291. int main(int argc, char *argv[])
  292. {
  293. struct test_params params = {
  294. .backing_src = DEFAULT_VM_MEM_SRC,
  295. .vcpu_memory_bytes = DEFAULT_PER_VCPU_MEM_SIZE,
  296. .nr_vcpus = 1,
  297. };
  298. int page_idle_fd;
  299. int opt;
  300. guest_modes_append_default();
  301. while ((opt = getopt(argc, argv, "hm:b:v:os:")) != -1) {
  302. switch (opt) {
  303. case 'm':
  304. guest_modes_cmdline(optarg);
  305. break;
  306. case 'b':
  307. params.vcpu_memory_bytes = parse_size(optarg);
  308. break;
  309. case 'v':
  310. params.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
  311. break;
  312. case 'o':
  313. overlap_memory_access = true;
  314. break;
  315. case 's':
  316. params.backing_src = parse_backing_src_type(optarg);
  317. break;
  318. case 'h':
  319. default:
  320. help(argv[0]);
  321. break;
  322. }
  323. }
  324. page_idle_fd = open("/sys/kernel/mm/page_idle/bitmap", O_RDWR);
  325. __TEST_REQUIRE(page_idle_fd >= 0,
  326. "CONFIG_IDLE_PAGE_TRACKING is not enabled");
  327. close(page_idle_fd);
  328. for_each_guest_mode(run_test, &params);
  329. return 0;
  330. }