demand_paging_test.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KVM demand paging test
  4. * Adapted from dirty_log_test.c
  5. *
  6. * Copyright (C) 2018, Red Hat, Inc.
  7. * Copyright (C) 2019, Google, Inc.
  8. */
  9. #include <inttypes.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <pthread.h>
  14. #include <linux/userfaultfd.h>
  15. #include <sys/syscall.h>
  16. #include "kvm_util.h"
  17. #include "test_util.h"
  18. #include "memstress.h"
  19. #include "guest_modes.h"
  20. #include "ucall_common.h"
  21. #include "userfaultfd_util.h"
  22. #ifdef __NR_userfaultfd
  23. static int nr_vcpus = 1;
  24. static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
  25. static size_t demand_paging_size;
  26. static char *guest_data_prototype;
  27. static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)
  28. {
  29. struct kvm_vcpu *vcpu = vcpu_args->vcpu;
  30. int vcpu_idx = vcpu_args->vcpu_idx;
  31. struct kvm_run *run = vcpu->run;
  32. struct timespec start;
  33. struct timespec ts_diff;
  34. int ret;
  35. clock_gettime(CLOCK_MONOTONIC, &start);
  36. /* Let the guest access its memory */
  37. ret = _vcpu_run(vcpu);
  38. TEST_ASSERT(ret == 0, "vcpu_run failed: %d", ret);
  39. if (get_ucall(vcpu, NULL) != UCALL_SYNC) {
  40. TEST_ASSERT(false,
  41. "Invalid guest sync status: exit_reason=%s",
  42. exit_reason_str(run->exit_reason));
  43. }
  44. ts_diff = timespec_elapsed(start);
  45. PER_VCPU_DEBUG("vCPU %d execution time: %ld.%.9lds\n", vcpu_idx,
  46. ts_diff.tv_sec, ts_diff.tv_nsec);
  47. }
  48. static int handle_uffd_page_request(int uffd_mode, int uffd,
  49. struct uffd_msg *msg)
  50. {
  51. pid_t tid = syscall(__NR_gettid);
  52. uint64_t addr = msg->arg.pagefault.address;
  53. struct timespec start;
  54. struct timespec ts_diff;
  55. int r;
  56. clock_gettime(CLOCK_MONOTONIC, &start);
  57. if (uffd_mode == UFFDIO_REGISTER_MODE_MISSING) {
  58. struct uffdio_copy copy;
  59. copy.src = (uint64_t)guest_data_prototype;
  60. copy.dst = addr;
  61. copy.len = demand_paging_size;
  62. copy.mode = 0;
  63. r = ioctl(uffd, UFFDIO_COPY, &copy);
  64. /*
  65. * With multiple vCPU threads fault on a single page and there are
  66. * multiple readers for the UFFD, at least one of the UFFDIO_COPYs
  67. * will fail with EEXIST: handle that case without signaling an
  68. * error.
  69. *
  70. * Note that this also suppress any EEXISTs occurring from,
  71. * e.g., the first UFFDIO_COPY/CONTINUEs on a page. That never
  72. * happens here, but a realistic VMM might potentially maintain
  73. * some external state to correctly surface EEXISTs to userspace
  74. * (or prevent duplicate COPY/CONTINUEs in the first place).
  75. */
  76. if (r == -1 && errno != EEXIST) {
  77. pr_info("Failed UFFDIO_COPY in 0x%lx from thread %d, errno = %d\n",
  78. addr, tid, errno);
  79. return r;
  80. }
  81. } else if (uffd_mode == UFFDIO_REGISTER_MODE_MINOR) {
  82. struct uffdio_continue cont = {0};
  83. cont.range.start = addr;
  84. cont.range.len = demand_paging_size;
  85. r = ioctl(uffd, UFFDIO_CONTINUE, &cont);
  86. /*
  87. * With multiple vCPU threads fault on a single page and there are
  88. * multiple readers for the UFFD, at least one of the UFFDIO_COPYs
  89. * will fail with EEXIST: handle that case without signaling an
  90. * error.
  91. *
  92. * Note that this also suppress any EEXISTs occurring from,
  93. * e.g., the first UFFDIO_COPY/CONTINUEs on a page. That never
  94. * happens here, but a realistic VMM might potentially maintain
  95. * some external state to correctly surface EEXISTs to userspace
  96. * (or prevent duplicate COPY/CONTINUEs in the first place).
  97. */
  98. if (r == -1 && errno != EEXIST) {
  99. pr_info("Failed UFFDIO_CONTINUE in 0x%lx, thread %d, errno = %d\n",
  100. addr, tid, errno);
  101. return r;
  102. }
  103. } else {
  104. TEST_FAIL("Invalid uffd mode %d", uffd_mode);
  105. }
  106. ts_diff = timespec_elapsed(start);
  107. PER_PAGE_DEBUG("UFFD page-in %d \t%ld ns\n", tid,
  108. timespec_to_ns(ts_diff));
  109. PER_PAGE_DEBUG("Paged in %ld bytes at 0x%lx from thread %d\n",
  110. demand_paging_size, addr, tid);
  111. return 0;
  112. }
  113. struct test_params {
  114. int uffd_mode;
  115. bool single_uffd;
  116. useconds_t uffd_delay;
  117. int readers_per_uffd;
  118. enum vm_mem_backing_src_type src_type;
  119. bool partition_vcpu_memory_access;
  120. };
  121. static void prefault_mem(void *alias, uint64_t len)
  122. {
  123. size_t p;
  124. TEST_ASSERT(alias != NULL, "Alias required for minor faults");
  125. for (p = 0; p < (len / demand_paging_size); ++p) {
  126. memcpy(alias + (p * demand_paging_size),
  127. guest_data_prototype, demand_paging_size);
  128. }
  129. }
  130. static void run_test(enum vm_guest_mode mode, void *arg)
  131. {
  132. struct memstress_vcpu_args *vcpu_args;
  133. struct test_params *p = arg;
  134. struct uffd_desc **uffd_descs = NULL;
  135. uint64_t uffd_region_size;
  136. struct timespec start;
  137. struct timespec ts_diff;
  138. double vcpu_paging_rate;
  139. struct kvm_vm *vm;
  140. int i, num_uffds = 0;
  141. vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
  142. p->src_type, p->partition_vcpu_memory_access);
  143. demand_paging_size = get_backing_src_pagesz(p->src_type);
  144. guest_data_prototype = malloc(demand_paging_size);
  145. TEST_ASSERT(guest_data_prototype,
  146. "Failed to allocate buffer for guest data pattern");
  147. memset(guest_data_prototype, 0xAB, demand_paging_size);
  148. if (p->uffd_mode == UFFDIO_REGISTER_MODE_MINOR) {
  149. num_uffds = p->single_uffd ? 1 : nr_vcpus;
  150. for (i = 0; i < num_uffds; i++) {
  151. vcpu_args = &memstress_args.vcpu_args[i];
  152. prefault_mem(addr_gpa2alias(vm, vcpu_args->gpa),
  153. vcpu_args->pages * memstress_args.guest_page_size);
  154. }
  155. }
  156. if (p->uffd_mode) {
  157. num_uffds = p->single_uffd ? 1 : nr_vcpus;
  158. uffd_region_size = nr_vcpus * guest_percpu_mem_size / num_uffds;
  159. uffd_descs = malloc(num_uffds * sizeof(struct uffd_desc *));
  160. TEST_ASSERT(uffd_descs, "Memory allocation failed");
  161. for (i = 0; i < num_uffds; i++) {
  162. struct memstress_vcpu_args *vcpu_args;
  163. void *vcpu_hva;
  164. vcpu_args = &memstress_args.vcpu_args[i];
  165. /* Cache the host addresses of the region */
  166. vcpu_hva = addr_gpa2hva(vm, vcpu_args->gpa);
  167. /*
  168. * Set up user fault fd to handle demand paging
  169. * requests.
  170. */
  171. uffd_descs[i] = uffd_setup_demand_paging(
  172. p->uffd_mode, p->uffd_delay, vcpu_hva,
  173. uffd_region_size,
  174. p->readers_per_uffd,
  175. &handle_uffd_page_request);
  176. }
  177. }
  178. pr_info("Finished creating vCPUs and starting uffd threads\n");
  179. clock_gettime(CLOCK_MONOTONIC, &start);
  180. memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);
  181. pr_info("Started all vCPUs\n");
  182. memstress_join_vcpu_threads(nr_vcpus);
  183. ts_diff = timespec_elapsed(start);
  184. pr_info("All vCPU threads joined\n");
  185. if (p->uffd_mode) {
  186. /* Tell the user fault fd handler threads to quit */
  187. for (i = 0; i < num_uffds; i++)
  188. uffd_stop_demand_paging(uffd_descs[i]);
  189. }
  190. pr_info("Total guest execution time:\t%ld.%.9lds\n",
  191. ts_diff.tv_sec, ts_diff.tv_nsec);
  192. vcpu_paging_rate = memstress_args.vcpu_args[0].pages /
  193. ((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / NSEC_PER_SEC);
  194. pr_info("Per-vcpu demand paging rate:\t%f pgs/sec/vcpu\n",
  195. vcpu_paging_rate);
  196. pr_info("Overall demand paging rate:\t%f pgs/sec\n",
  197. vcpu_paging_rate * nr_vcpus);
  198. memstress_destroy_vm(vm);
  199. free(guest_data_prototype);
  200. if (p->uffd_mode)
  201. free(uffd_descs);
  202. }
  203. static void help(char *name)
  204. {
  205. puts("");
  206. printf("usage: %s [-h] [-m vm_mode] [-u uffd_mode] [-a]\n"
  207. " [-d uffd_delay_usec] [-r readers_per_uffd] [-b memory]\n"
  208. " [-s type] [-v vcpus] [-c cpu_list] [-o]\n", name);
  209. guest_modes_help();
  210. printf(" -u: use userfaultfd to handle vCPU page faults. Mode is a\n"
  211. " UFFD registration mode: 'MISSING' or 'MINOR'.\n");
  212. kvm_print_vcpu_pinning_help();
  213. printf(" -a: Use a single userfaultfd for all of guest memory, instead of\n"
  214. " creating one for each region paged by a unique vCPU\n"
  215. " Set implicitly with -o, and no effect without -u.\n");
  216. printf(" -d: add a delay in usec to the User Fault\n"
  217. " FD handler to simulate demand paging\n"
  218. " overheads. Ignored without -u.\n");
  219. printf(" -r: Set the number of reader threads per uffd.\n");
  220. printf(" -b: specify the size of the memory region which should be\n"
  221. " demand paged by each vCPU. e.g. 10M or 3G.\n"
  222. " Default: 1G\n");
  223. backing_src_help("-s");
  224. printf(" -v: specify the number of vCPUs to run.\n");
  225. printf(" -o: Overlap guest memory accesses instead of partitioning\n"
  226. " them into a separate region of memory for each vCPU.\n");
  227. puts("");
  228. exit(0);
  229. }
  230. int main(int argc, char *argv[])
  231. {
  232. int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
  233. const char *cpulist = NULL;
  234. struct test_params p = {
  235. .src_type = DEFAULT_VM_MEM_SRC,
  236. .partition_vcpu_memory_access = true,
  237. .readers_per_uffd = 1,
  238. .single_uffd = false,
  239. };
  240. int opt;
  241. guest_modes_append_default();
  242. while ((opt = getopt(argc, argv, "ahom:u:d:b:s:v:c:r:")) != -1) {
  243. switch (opt) {
  244. case 'm':
  245. guest_modes_cmdline(optarg);
  246. break;
  247. case 'u':
  248. if (!strcmp("MISSING", optarg))
  249. p.uffd_mode = UFFDIO_REGISTER_MODE_MISSING;
  250. else if (!strcmp("MINOR", optarg))
  251. p.uffd_mode = UFFDIO_REGISTER_MODE_MINOR;
  252. TEST_ASSERT(p.uffd_mode, "UFFD mode must be 'MISSING' or 'MINOR'.");
  253. break;
  254. case 'a':
  255. p.single_uffd = true;
  256. break;
  257. case 'd':
  258. p.uffd_delay = strtoul(optarg, NULL, 0);
  259. TEST_ASSERT(p.uffd_delay >= 0, "A negative UFFD delay is not supported.");
  260. break;
  261. case 'b':
  262. guest_percpu_mem_size = parse_size(optarg);
  263. break;
  264. case 's':
  265. p.src_type = parse_backing_src_type(optarg);
  266. break;
  267. case 'v':
  268. nr_vcpus = atoi_positive("Number of vCPUs", optarg);
  269. TEST_ASSERT(nr_vcpus <= max_vcpus,
  270. "Invalid number of vcpus, must be between 1 and %d", max_vcpus);
  271. break;
  272. case 'c':
  273. cpulist = optarg;
  274. break;
  275. case 'o':
  276. p.partition_vcpu_memory_access = false;
  277. p.single_uffd = true;
  278. break;
  279. case 'r':
  280. p.readers_per_uffd = atoi(optarg);
  281. TEST_ASSERT(p.readers_per_uffd >= 1,
  282. "Invalid number of readers per uffd %d: must be >=1",
  283. p.readers_per_uffd);
  284. break;
  285. case 'h':
  286. default:
  287. help(argv[0]);
  288. break;
  289. }
  290. }
  291. if (p.uffd_mode == UFFDIO_REGISTER_MODE_MINOR &&
  292. !backing_src_is_shared(p.src_type)) {
  293. TEST_FAIL("userfaultfd MINOR mode requires shared memory; pick a different -s");
  294. }
  295. if (cpulist) {
  296. kvm_parse_vcpu_pinning(cpulist, memstress_args.vcpu_to_pcpu,
  297. nr_vcpus);
  298. memstress_args.pin_vcpus = true;
  299. }
  300. for_each_guest_mode(run_test, &p);
  301. return 0;
  302. }
  303. #else /* __NR_userfaultfd */
  304. #warning "missing __NR_userfaultfd definition"
  305. int main(void)
  306. {
  307. print_skip("__NR_userfaultfd must be present for userfaultfd test");
  308. return KSFT_SKIP;
  309. }
  310. #endif /* __NR_userfaultfd */