dirty_log_test.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KVM dirty page logging test
  4. *
  5. * Copyright (C) 2018, Red Hat, Inc.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <time.h>
  11. #include <pthread.h>
  12. #include <linux/bitmap.h>
  13. #include <linux/bitops.h>
  14. #include "test_util.h"
  15. #include "kvm_util.h"
  16. #define DEBUG printf
  17. #define VCPU_ID 1
  18. /* The memory slot index to track dirty pages */
  19. #define TEST_MEM_SLOT_INDEX 1
  20. /*
  21. * GPA offset of the testing memory slot. Must be bigger than the
  22. * default vm mem slot, which is DEFAULT_GUEST_PHY_PAGES.
  23. */
  24. #define TEST_MEM_OFFSET (1ULL << 30) /* 1G */
  25. /* Size of the testing memory slot */
  26. #define TEST_MEM_PAGES (1ULL << 18) /* 1G for 4K pages */
  27. /* How many pages to dirty for each guest loop */
  28. #define TEST_PAGES_PER_LOOP 1024
  29. /* How many host loops to run (one KVM_GET_DIRTY_LOG for each loop) */
  30. #define TEST_HOST_LOOP_N 32UL
  31. /* Interval for each host loop (ms) */
  32. #define TEST_HOST_LOOP_INTERVAL 10UL
  33. /*
  34. * Guest variables. We use these variables to share data between host
  35. * and guest. There are two copies of the variables, one in host memory
  36. * (which is unused) and one in guest memory. When the host wants to
  37. * access these variables, it needs to call addr_gva2hva() to access the
  38. * guest copy.
  39. */
  40. uint64_t guest_random_array[TEST_PAGES_PER_LOOP];
  41. uint64_t guest_iteration;
  42. uint64_t guest_page_size;
  43. /*
  44. * Writes to the first byte of a random page within the testing memory
  45. * region continuously.
  46. */
  47. void guest_code(void)
  48. {
  49. int i = 0;
  50. uint64_t volatile *array = guest_random_array;
  51. uint64_t volatile *guest_addr;
  52. while (true) {
  53. for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
  54. /*
  55. * Write to the first 8 bytes of a random page
  56. * on the testing memory region.
  57. */
  58. guest_addr = (uint64_t *)
  59. (TEST_MEM_OFFSET +
  60. (array[i] % TEST_MEM_PAGES) * guest_page_size);
  61. *guest_addr = guest_iteration;
  62. }
  63. /* Tell the host that we need more random numbers */
  64. GUEST_SYNC(1);
  65. }
  66. }
  67. /*
  68. * Host variables. These variables should only be used by the host
  69. * rather than the guest.
  70. */
  71. bool host_quit;
  72. /* Points to the test VM memory region on which we track dirty logs */
  73. void *host_test_mem;
  74. /* For statistics only */
  75. uint64_t host_dirty_count;
  76. uint64_t host_clear_count;
  77. uint64_t host_track_next_count;
  78. /*
  79. * We use this bitmap to track some pages that should have its dirty
  80. * bit set in the _next_ iteration. For example, if we detected the
  81. * page value changed to current iteration but at the same time the
  82. * page bit is cleared in the latest bitmap, then the system must
  83. * report that write in the next get dirty log call.
  84. */
  85. unsigned long *host_bmap_track;
  86. void generate_random_array(uint64_t *guest_array, uint64_t size)
  87. {
  88. uint64_t i;
  89. for (i = 0; i < size; i++) {
  90. guest_array[i] = random();
  91. }
  92. }
  93. void *vcpu_worker(void *data)
  94. {
  95. int ret;
  96. uint64_t loops, *guest_array, pages_count = 0;
  97. struct kvm_vm *vm = data;
  98. struct kvm_run *run;
  99. struct guest_args args;
  100. run = vcpu_state(vm, VCPU_ID);
  101. /* Retrieve the guest random array pointer and cache it */
  102. guest_array = addr_gva2hva(vm, (vm_vaddr_t)guest_random_array);
  103. DEBUG("VCPU starts\n");
  104. generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
  105. while (!READ_ONCE(host_quit)) {
  106. /* Let the guest to dirty these random pages */
  107. ret = _vcpu_run(vm, VCPU_ID);
  108. guest_args_read(vm, VCPU_ID, &args);
  109. if (run->exit_reason == KVM_EXIT_IO &&
  110. args.port == GUEST_PORT_SYNC) {
  111. pages_count += TEST_PAGES_PER_LOOP;
  112. generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
  113. } else {
  114. TEST_ASSERT(false,
  115. "Invalid guest sync status: "
  116. "exit_reason=%s\n",
  117. exit_reason_str(run->exit_reason));
  118. }
  119. }
  120. DEBUG("VCPU exits, dirtied %"PRIu64" pages\n", pages_count);
  121. return NULL;
  122. }
  123. void vm_dirty_log_verify(unsigned long *bmap, uint64_t iteration)
  124. {
  125. uint64_t page;
  126. uint64_t volatile *value_ptr;
  127. for (page = 0; page < TEST_MEM_PAGES; page++) {
  128. value_ptr = host_test_mem + page * getpagesize();
  129. /* If this is a special page that we were tracking... */
  130. if (test_and_clear_bit(page, host_bmap_track)) {
  131. host_track_next_count++;
  132. TEST_ASSERT(test_bit(page, bmap),
  133. "Page %"PRIu64" should have its dirty bit "
  134. "set in this iteration but it is missing",
  135. page);
  136. }
  137. if (test_bit(page, bmap)) {
  138. host_dirty_count++;
  139. /*
  140. * If the bit is set, the value written onto
  141. * the corresponding page should be either the
  142. * previous iteration number or the current one.
  143. */
  144. TEST_ASSERT(*value_ptr == iteration ||
  145. *value_ptr == iteration - 1,
  146. "Set page %"PRIu64" value %"PRIu64
  147. " incorrect (iteration=%"PRIu64")",
  148. page, *value_ptr, iteration);
  149. } else {
  150. host_clear_count++;
  151. /*
  152. * If cleared, the value written can be any
  153. * value smaller or equals to the iteration
  154. * number. Note that the value can be exactly
  155. * (iteration-1) if that write can happen
  156. * like this:
  157. *
  158. * (1) increase loop count to "iteration-1"
  159. * (2) write to page P happens (with value
  160. * "iteration-1")
  161. * (3) get dirty log for "iteration-1"; we'll
  162. * see that page P bit is set (dirtied),
  163. * and not set the bit in host_bmap_track
  164. * (4) increase loop count to "iteration"
  165. * (which is current iteration)
  166. * (5) get dirty log for current iteration,
  167. * we'll see that page P is cleared, with
  168. * value "iteration-1".
  169. */
  170. TEST_ASSERT(*value_ptr <= iteration,
  171. "Clear page %"PRIu64" value %"PRIu64
  172. " incorrect (iteration=%"PRIu64")",
  173. page, *value_ptr, iteration);
  174. if (*value_ptr == iteration) {
  175. /*
  176. * This page is _just_ modified; it
  177. * should report its dirtyness in the
  178. * next run
  179. */
  180. set_bit(page, host_bmap_track);
  181. }
  182. }
  183. }
  184. }
  185. void help(char *name)
  186. {
  187. puts("");
  188. printf("usage: %s [-i iterations] [-I interval] [-h]\n", name);
  189. puts("");
  190. printf(" -i: specify iteration counts (default: %"PRIu64")\n",
  191. TEST_HOST_LOOP_N);
  192. printf(" -I: specify interval in ms (default: %"PRIu64" ms)\n",
  193. TEST_HOST_LOOP_INTERVAL);
  194. puts("");
  195. exit(0);
  196. }
  197. int main(int argc, char *argv[])
  198. {
  199. pthread_t vcpu_thread;
  200. struct kvm_vm *vm;
  201. uint64_t volatile *psize, *iteration;
  202. unsigned long *bmap, iterations = TEST_HOST_LOOP_N,
  203. interval = TEST_HOST_LOOP_INTERVAL;
  204. int opt;
  205. while ((opt = getopt(argc, argv, "hi:I:")) != -1) {
  206. switch (opt) {
  207. case 'i':
  208. iterations = strtol(optarg, NULL, 10);
  209. break;
  210. case 'I':
  211. interval = strtol(optarg, NULL, 10);
  212. break;
  213. case 'h':
  214. default:
  215. help(argv[0]);
  216. break;
  217. }
  218. }
  219. TEST_ASSERT(iterations > 2, "Iteration must be bigger than zero\n");
  220. TEST_ASSERT(interval > 0, "Interval must be bigger than zero");
  221. DEBUG("Test iterations: %"PRIu64", interval: %"PRIu64" (ms)\n",
  222. iterations, interval);
  223. srandom(time(0));
  224. bmap = bitmap_alloc(TEST_MEM_PAGES);
  225. host_bmap_track = bitmap_alloc(TEST_MEM_PAGES);
  226. vm = vm_create_default(VCPU_ID, TEST_MEM_PAGES, guest_code);
  227. /* Add an extra memory slot for testing dirty logging */
  228. vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
  229. TEST_MEM_OFFSET,
  230. TEST_MEM_SLOT_INDEX,
  231. TEST_MEM_PAGES,
  232. KVM_MEM_LOG_DIRTY_PAGES);
  233. /* Cache the HVA pointer of the region */
  234. host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)TEST_MEM_OFFSET);
  235. /* Do 1:1 mapping for the dirty track memory slot */
  236. virt_map(vm, TEST_MEM_OFFSET, TEST_MEM_OFFSET,
  237. TEST_MEM_PAGES * getpagesize(), 0);
  238. vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
  239. /* Tell the guest about the page size on the system */
  240. psize = addr_gva2hva(vm, (vm_vaddr_t)&guest_page_size);
  241. *psize = getpagesize();
  242. /* Start the iterations */
  243. iteration = addr_gva2hva(vm, (vm_vaddr_t)&guest_iteration);
  244. *iteration = 1;
  245. /* Start dirtying pages */
  246. pthread_create(&vcpu_thread, NULL, vcpu_worker, vm);
  247. while (*iteration < iterations) {
  248. /* Give the vcpu thread some time to dirty some pages */
  249. usleep(interval * 1000);
  250. kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
  251. vm_dirty_log_verify(bmap, *iteration);
  252. (*iteration)++;
  253. }
  254. /* Tell the vcpu thread to quit */
  255. host_quit = true;
  256. pthread_join(vcpu_thread, NULL);
  257. DEBUG("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), "
  258. "track_next (%"PRIu64")\n", host_dirty_count, host_clear_count,
  259. host_track_next_count);
  260. free(bmap);
  261. free(host_bmap_track);
  262. kvm_vm_free(vm);
  263. return 0;
  264. }