KVM: selftests: Introduce after_vcpu_run hook for dirty log test
[linux-2.6-microblaze.git] / tools / testing / selftests / kvm / dirty_log_test.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * KVM dirty page logging test
4  *
5  * Copyright (C) 2018, Red Hat, Inc.
6  */
7
8 #define _GNU_SOURCE /* for program_invocation_name */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <time.h>
14 #include <pthread.h>
15 #include <linux/bitmap.h>
16 #include <linux/bitops.h>
17
18 #include "test_util.h"
19 #include "kvm_util.h"
20 #include "processor.h"
21
22 #define VCPU_ID                         1
23
24 /* The memory slot index to track dirty pages */
25 #define TEST_MEM_SLOT_INDEX             1
26
27 /* Default guest test virtual memory offset */
28 #define DEFAULT_GUEST_TEST_MEM          0xc0000000
29
30 /* How many pages to dirty for each guest loop */
31 #define TEST_PAGES_PER_LOOP             1024
32
33 /* How many host loops to run (one KVM_GET_DIRTY_LOG for each loop) */
34 #define TEST_HOST_LOOP_N                32UL
35
36 /* Interval for each host loop (ms) */
37 #define TEST_HOST_LOOP_INTERVAL         10UL
38
39 /* Dirty bitmaps are always little endian, so we need to swap on big endian */
40 #if defined(__s390x__)
41 # define BITOP_LE_SWIZZLE       ((BITS_PER_LONG-1) & ~0x7)
42 # define test_bit_le(nr, addr) \
43         test_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
44 # define set_bit_le(nr, addr) \
45         set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
46 # define clear_bit_le(nr, addr) \
47         clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
48 # define test_and_set_bit_le(nr, addr) \
49         test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
50 # define test_and_clear_bit_le(nr, addr) \
51         test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
52 #else
53 # define test_bit_le            test_bit
54 # define set_bit_le             set_bit
55 # define clear_bit_le           clear_bit
56 # define test_and_set_bit_le    test_and_set_bit
57 # define test_and_clear_bit_le  test_and_clear_bit
58 #endif
59
60 /*
61  * Guest/Host shared variables. Ensure addr_gva2hva() and/or
62  * sync_global_to/from_guest() are used when accessing from
63  * the host. READ/WRITE_ONCE() should also be used with anything
64  * that may change.
65  */
66 static uint64_t host_page_size;
67 static uint64_t guest_page_size;
68 static uint64_t guest_num_pages;
69 static uint64_t random_array[TEST_PAGES_PER_LOOP];
70 static uint64_t iteration;
71
72 /*
73  * Guest physical memory offset of the testing memory slot.
74  * This will be set to the topmost valid physical address minus
75  * the test memory size.
76  */
77 static uint64_t guest_test_phys_mem;
78
79 /*
80  * Guest virtual memory offset of the testing memory slot.
81  * Must not conflict with identity mapped test code.
82  */
83 static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
84
85 /*
86  * Continuously write to the first 8 bytes of a random pages within
87  * the testing memory region.
88  */
89 static void guest_code(void)
90 {
91         uint64_t addr;
92         int i;
93
94         /*
95          * On s390x, all pages of a 1M segment are initially marked as dirty
96          * when a page of the segment is written to for the very first time.
97          * To compensate this specialty in this test, we need to touch all
98          * pages during the first iteration.
99          */
100         for (i = 0; i < guest_num_pages; i++) {
101                 addr = guest_test_virt_mem + i * guest_page_size;
102                 *(uint64_t *)addr = READ_ONCE(iteration);
103         }
104
105         while (true) {
106                 for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
107                         addr = guest_test_virt_mem;
108                         addr += (READ_ONCE(random_array[i]) % guest_num_pages)
109                                 * guest_page_size;
110                         addr &= ~(host_page_size - 1);
111                         *(uint64_t *)addr = READ_ONCE(iteration);
112                 }
113
114                 /* Tell the host that we need more random numbers */
115                 GUEST_SYNC(1);
116         }
117 }
118
119 /* Host variables */
120 static bool host_quit;
121
122 /* Points to the test VM memory region on which we track dirty logs */
123 static void *host_test_mem;
124 static uint64_t host_num_pages;
125
126 /* For statistics only */
127 static uint64_t host_dirty_count;
128 static uint64_t host_clear_count;
129 static uint64_t host_track_next_count;
130
131 enum log_mode_t {
132         /* Only use KVM_GET_DIRTY_LOG for logging */
133         LOG_MODE_DIRTY_LOG = 0,
134
135         /* Use both KVM_[GET|CLEAR]_DIRTY_LOG for logging */
136         LOG_MODE_CLEAR_LOG = 1,
137
138         LOG_MODE_NUM,
139
140         /* Run all supported modes */
141         LOG_MODE_ALL = LOG_MODE_NUM,
142 };
143
144 /* Mode of logging to test.  Default is to run all supported modes */
145 static enum log_mode_t host_log_mode_option = LOG_MODE_ALL;
146 /* Logging mode for current run */
147 static enum log_mode_t host_log_mode;
148
149 static bool clear_log_supported(void)
150 {
151         return kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
152 }
153
154 static void clear_log_create_vm_done(struct kvm_vm *vm)
155 {
156         struct kvm_enable_cap cap = {};
157         u64 manual_caps;
158
159         manual_caps = kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
160         TEST_ASSERT(manual_caps, "MANUAL_CAPS is zero!");
161         manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |
162                         KVM_DIRTY_LOG_INITIALLY_SET);
163         cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2;
164         cap.args[0] = manual_caps;
165         vm_enable_cap(vm, &cap);
166 }
167
168 static void dirty_log_collect_dirty_pages(struct kvm_vm *vm, int slot,
169                                           void *bitmap, uint32_t num_pages)
170 {
171         kvm_vm_get_dirty_log(vm, slot, bitmap);
172 }
173
174 static void clear_log_collect_dirty_pages(struct kvm_vm *vm, int slot,
175                                           void *bitmap, uint32_t num_pages)
176 {
177         kvm_vm_get_dirty_log(vm, slot, bitmap);
178         kvm_vm_clear_dirty_log(vm, slot, bitmap, 0, num_pages);
179 }
180
181 static void default_after_vcpu_run(struct kvm_vm *vm)
182 {
183         struct kvm_run *run = vcpu_state(vm, VCPU_ID);
184
185         TEST_ASSERT(get_ucall(vm, VCPU_ID, NULL) == UCALL_SYNC,
186                     "Invalid guest sync status: exit_reason=%s\n",
187                     exit_reason_str(run->exit_reason));
188 }
189
190 struct log_mode {
191         const char *name;
192         /* Return true if this mode is supported, otherwise false */
193         bool (*supported)(void);
194         /* Hook when the vm creation is done (before vcpu creation) */
195         void (*create_vm_done)(struct kvm_vm *vm);
196         /* Hook to collect the dirty pages into the bitmap provided */
197         void (*collect_dirty_pages) (struct kvm_vm *vm, int slot,
198                                      void *bitmap, uint32_t num_pages);
199         /* Hook to call when after each vcpu run */
200         void (*after_vcpu_run)(struct kvm_vm *vm);
201 } log_modes[LOG_MODE_NUM] = {
202         {
203                 .name = "dirty-log",
204                 .collect_dirty_pages = dirty_log_collect_dirty_pages,
205                 .after_vcpu_run = default_after_vcpu_run,
206         },
207         {
208                 .name = "clear-log",
209                 .supported = clear_log_supported,
210                 .create_vm_done = clear_log_create_vm_done,
211                 .collect_dirty_pages = clear_log_collect_dirty_pages,
212                 .after_vcpu_run = default_after_vcpu_run,
213         },
214 };
215
216 /*
217  * We use this bitmap to track some pages that should have its dirty
218  * bit set in the _next_ iteration.  For example, if we detected the
219  * page value changed to current iteration but at the same time the
220  * page bit is cleared in the latest bitmap, then the system must
221  * report that write in the next get dirty log call.
222  */
223 static unsigned long *host_bmap_track;
224
225 static void log_modes_dump(void)
226 {
227         int i;
228
229         printf("all");
230         for (i = 0; i < LOG_MODE_NUM; i++)
231                 printf(", %s", log_modes[i].name);
232         printf("\n");
233 }
234
235 static bool log_mode_supported(void)
236 {
237         struct log_mode *mode = &log_modes[host_log_mode];
238
239         if (mode->supported)
240                 return mode->supported();
241
242         return true;
243 }
244
245 static void log_mode_create_vm_done(struct kvm_vm *vm)
246 {
247         struct log_mode *mode = &log_modes[host_log_mode];
248
249         if (mode->create_vm_done)
250                 mode->create_vm_done(vm);
251 }
252
253 static void log_mode_collect_dirty_pages(struct kvm_vm *vm, int slot,
254                                          void *bitmap, uint32_t num_pages)
255 {
256         struct log_mode *mode = &log_modes[host_log_mode];
257
258         TEST_ASSERT(mode->collect_dirty_pages != NULL,
259                     "collect_dirty_pages() is required for any log mode!");
260         mode->collect_dirty_pages(vm, slot, bitmap, num_pages);
261 }
262
263 static void log_mode_after_vcpu_run(struct kvm_vm *vm)
264 {
265         struct log_mode *mode = &log_modes[host_log_mode];
266
267         if (mode->after_vcpu_run)
268                 mode->after_vcpu_run(vm);
269 }
270
271 static void generate_random_array(uint64_t *guest_array, uint64_t size)
272 {
273         uint64_t i;
274
275         for (i = 0; i < size; i++)
276                 guest_array[i] = random();
277 }
278
279 static void *vcpu_worker(void *data)
280 {
281         int ret;
282         struct kvm_vm *vm = data;
283         uint64_t *guest_array;
284         uint64_t pages_count = 0;
285
286         guest_array = addr_gva2hva(vm, (vm_vaddr_t)random_array);
287
288         while (!READ_ONCE(host_quit)) {
289                 generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
290                 pages_count += TEST_PAGES_PER_LOOP;
291                 /* Let the guest dirty the random pages */
292                 ret = _vcpu_run(vm, VCPU_ID);
293                 TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
294                 log_mode_after_vcpu_run(vm);
295         }
296
297         pr_info("Dirtied %"PRIu64" pages\n", pages_count);
298
299         return NULL;
300 }
301
302 static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap)
303 {
304         uint64_t step = vm_num_host_pages(mode, 1);
305         uint64_t page;
306         uint64_t *value_ptr;
307
308         for (page = 0; page < host_num_pages; page += step) {
309                 value_ptr = host_test_mem + page * host_page_size;
310
311                 /* If this is a special page that we were tracking... */
312                 if (test_and_clear_bit_le(page, host_bmap_track)) {
313                         host_track_next_count++;
314                         TEST_ASSERT(test_bit_le(page, bmap),
315                                     "Page %"PRIu64" should have its dirty bit "
316                                     "set in this iteration but it is missing",
317                                     page);
318                 }
319
320                 if (test_and_clear_bit_le(page, bmap)) {
321                         host_dirty_count++;
322                         /*
323                          * If the bit is set, the value written onto
324                          * the corresponding page should be either the
325                          * previous iteration number or the current one.
326                          */
327                         TEST_ASSERT(*value_ptr == iteration ||
328                                     *value_ptr == iteration - 1,
329                                     "Set page %"PRIu64" value %"PRIu64
330                                     " incorrect (iteration=%"PRIu64")",
331                                     page, *value_ptr, iteration);
332                 } else {
333                         host_clear_count++;
334                         /*
335                          * If cleared, the value written can be any
336                          * value smaller or equals to the iteration
337                          * number.  Note that the value can be exactly
338                          * (iteration-1) if that write can happen
339                          * like this:
340                          *
341                          * (1) increase loop count to "iteration-1"
342                          * (2) write to page P happens (with value
343                          *     "iteration-1")
344                          * (3) get dirty log for "iteration-1"; we'll
345                          *     see that page P bit is set (dirtied),
346                          *     and not set the bit in host_bmap_track
347                          * (4) increase loop count to "iteration"
348                          *     (which is current iteration)
349                          * (5) get dirty log for current iteration,
350                          *     we'll see that page P is cleared, with
351                          *     value "iteration-1".
352                          */
353                         TEST_ASSERT(*value_ptr <= iteration,
354                                     "Clear page %"PRIu64" value %"PRIu64
355                                     " incorrect (iteration=%"PRIu64")",
356                                     page, *value_ptr, iteration);
357                         if (*value_ptr == iteration) {
358                                 /*
359                                  * This page is _just_ modified; it
360                                  * should report its dirtyness in the
361                                  * next run
362                                  */
363                                 set_bit_le(page, host_bmap_track);
364                         }
365                 }
366         }
367 }
368
369 static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,
370                                 uint64_t extra_mem_pages, void *guest_code)
371 {
372         struct kvm_vm *vm;
373         uint64_t extra_pg_pages = extra_mem_pages / 512 * 2;
374
375         pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
376
377         vm = vm_create(mode, DEFAULT_GUEST_PHY_PAGES + extra_pg_pages, O_RDWR);
378         kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
379 #ifdef __x86_64__
380         vm_create_irqchip(vm);
381 #endif
382         log_mode_create_vm_done(vm);
383         vm_vcpu_add_default(vm, vcpuid, guest_code);
384         return vm;
385 }
386
387 #define DIRTY_MEM_BITS 30 /* 1G */
388 #define PAGE_SHIFT_4K  12
389
390 static void run_test(enum vm_guest_mode mode, unsigned long iterations,
391                      unsigned long interval, uint64_t phys_offset)
392 {
393         pthread_t vcpu_thread;
394         struct kvm_vm *vm;
395         unsigned long *bmap;
396
397         if (!log_mode_supported()) {
398                 print_skip("Log mode '%s' not supported",
399                            log_modes[host_log_mode].name);
400                 return;
401         }
402
403         /*
404          * We reserve page table for 2 times of extra dirty mem which
405          * will definitely cover the original (1G+) test range.  Here
406          * we do the calculation with 4K page size which is the
407          * smallest so the page number will be enough for all archs
408          * (e.g., 64K page size guest will need even less memory for
409          * page tables).
410          */
411         vm = create_vm(mode, VCPU_ID,
412                        2ul << (DIRTY_MEM_BITS - PAGE_SHIFT_4K),
413                        guest_code);
414
415         guest_page_size = vm_get_page_size(vm);
416         /*
417          * A little more than 1G of guest page sized pages.  Cover the
418          * case where the size is not aligned to 64 pages.
419          */
420         guest_num_pages = (1ul << (DIRTY_MEM_BITS -
421                                    vm_get_page_shift(vm))) + 3;
422         guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages);
423
424         host_page_size = getpagesize();
425         host_num_pages = vm_num_host_pages(mode, guest_num_pages);
426
427         if (!phys_offset) {
428                 guest_test_phys_mem = (vm_get_max_gfn(vm) -
429                                        guest_num_pages) * guest_page_size;
430                 guest_test_phys_mem &= ~(host_page_size - 1);
431         } else {
432                 guest_test_phys_mem = phys_offset;
433         }
434
435 #ifdef __s390x__
436         /* Align to 1M (segment size) */
437         guest_test_phys_mem &= ~((1 << 20) - 1);
438 #endif
439
440         pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem);
441
442         bmap = bitmap_alloc(host_num_pages);
443         host_bmap_track = bitmap_alloc(host_num_pages);
444
445         /* Add an extra memory slot for testing dirty logging */
446         vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
447                                     guest_test_phys_mem,
448                                     TEST_MEM_SLOT_INDEX,
449                                     guest_num_pages,
450                                     KVM_MEM_LOG_DIRTY_PAGES);
451
452         /* Do mapping for the dirty track memory slot */
453         virt_map(vm, guest_test_virt_mem, guest_test_phys_mem, guest_num_pages, 0);
454
455         /* Cache the HVA pointer of the region */
456         host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)guest_test_phys_mem);
457
458 #ifdef __x86_64__
459         vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
460 #endif
461         ucall_init(vm, NULL);
462
463         /* Export the shared variables to the guest */
464         sync_global_to_guest(vm, host_page_size);
465         sync_global_to_guest(vm, guest_page_size);
466         sync_global_to_guest(vm, guest_test_virt_mem);
467         sync_global_to_guest(vm, guest_num_pages);
468
469         /* Start the iterations */
470         iteration = 1;
471         sync_global_to_guest(vm, iteration);
472         host_quit = false;
473         host_dirty_count = 0;
474         host_clear_count = 0;
475         host_track_next_count = 0;
476
477         pthread_create(&vcpu_thread, NULL, vcpu_worker, vm);
478
479         while (iteration < iterations) {
480                 /* Give the vcpu thread some time to dirty some pages */
481                 usleep(interval * 1000);
482                 log_mode_collect_dirty_pages(vm, TEST_MEM_SLOT_INDEX,
483                                              bmap, host_num_pages);
484                 vm_dirty_log_verify(mode, bmap);
485                 iteration++;
486                 sync_global_to_guest(vm, iteration);
487         }
488
489         /* Tell the vcpu thread to quit */
490         host_quit = true;
491         pthread_join(vcpu_thread, NULL);
492
493         pr_info("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), "
494                 "track_next (%"PRIu64")\n", host_dirty_count, host_clear_count,
495                 host_track_next_count);
496
497         free(bmap);
498         free(host_bmap_track);
499         ucall_uninit(vm);
500         kvm_vm_free(vm);
501 }
502
503 struct guest_mode {
504         bool supported;
505         bool enabled;
506 };
507 static struct guest_mode guest_modes[NUM_VM_MODES];
508
509 #define guest_mode_init(mode, supported, enabled) ({ \
510         guest_modes[mode] = (struct guest_mode){ supported, enabled }; \
511 })
512
513 static void help(char *name)
514 {
515         int i;
516
517         puts("");
518         printf("usage: %s [-h] [-i iterations] [-I interval] "
519                "[-p offset] [-m mode]\n", name);
520         puts("");
521         printf(" -i: specify iteration counts (default: %"PRIu64")\n",
522                TEST_HOST_LOOP_N);
523         printf(" -I: specify interval in ms (default: %"PRIu64" ms)\n",
524                TEST_HOST_LOOP_INTERVAL);
525         printf(" -p: specify guest physical test memory offset\n"
526                "     Warning: a low offset can conflict with the loaded test code.\n");
527         printf(" -M: specify the host logging mode "
528                "(default: run all log modes).  Supported modes: \n\t");
529         log_modes_dump();
530         printf(" -m: specify the guest mode ID to test "
531                "(default: test all supported modes)\n"
532                "     This option may be used multiple times.\n"
533                "     Guest mode IDs:\n");
534         for (i = 0; i < NUM_VM_MODES; ++i) {
535                 printf("         %d:    %s%s\n", i, vm_guest_mode_string(i),
536                        guest_modes[i].supported ? " (supported)" : "");
537         }
538         puts("");
539         exit(0);
540 }
541
542 int main(int argc, char *argv[])
543 {
544         unsigned long iterations = TEST_HOST_LOOP_N;
545         unsigned long interval = TEST_HOST_LOOP_INTERVAL;
546         bool mode_selected = false;
547         uint64_t phys_offset = 0;
548         unsigned int mode;
549         int opt, i, j;
550
551 #ifdef __x86_64__
552         guest_mode_init(VM_MODE_PXXV48_4K, true, true);
553 #endif
554 #ifdef __aarch64__
555         guest_mode_init(VM_MODE_P40V48_4K, true, true);
556         guest_mode_init(VM_MODE_P40V48_64K, true, true);
557
558         {
559                 unsigned int limit = kvm_check_cap(KVM_CAP_ARM_VM_IPA_SIZE);
560
561                 if (limit >= 52)
562                         guest_mode_init(VM_MODE_P52V48_64K, true, true);
563                 if (limit >= 48) {
564                         guest_mode_init(VM_MODE_P48V48_4K, true, true);
565                         guest_mode_init(VM_MODE_P48V48_64K, true, true);
566                 }
567         }
568 #endif
569 #ifdef __s390x__
570         guest_mode_init(VM_MODE_P40V48_4K, true, true);
571 #endif
572
573         while ((opt = getopt(argc, argv, "hi:I:p:m:M:")) != -1) {
574                 switch (opt) {
575                 case 'i':
576                         iterations = strtol(optarg, NULL, 10);
577                         break;
578                 case 'I':
579                         interval = strtol(optarg, NULL, 10);
580                         break;
581                 case 'p':
582                         phys_offset = strtoull(optarg, NULL, 0);
583                         break;
584                 case 'm':
585                         if (!mode_selected) {
586                                 for (i = 0; i < NUM_VM_MODES; ++i)
587                                         guest_modes[i].enabled = false;
588                                 mode_selected = true;
589                         }
590                         mode = strtoul(optarg, NULL, 10);
591                         TEST_ASSERT(mode < NUM_VM_MODES,
592                                     "Guest mode ID %d too big", mode);
593                         guest_modes[mode].enabled = true;
594                         break;
595                 case 'M':
596                         if (!strcmp(optarg, "all")) {
597                                 host_log_mode_option = LOG_MODE_ALL;
598                                 break;
599                         }
600                         for (i = 0; i < LOG_MODE_NUM; i++) {
601                                 if (!strcmp(optarg, log_modes[i].name)) {
602                                         pr_info("Setting log mode to: '%s'\n",
603                                                 optarg);
604                                         host_log_mode_option = i;
605                                         break;
606                                 }
607                         }
608                         if (i == LOG_MODE_NUM) {
609                                 printf("Log mode '%s' invalid. Please choose "
610                                        "from: ", optarg);
611                                 log_modes_dump();
612                                 exit(1);
613                         }
614                         break;
615                 case 'h':
616                 default:
617                         help(argv[0]);
618                         break;
619                 }
620         }
621
622         TEST_ASSERT(iterations > 2, "Iterations must be greater than two");
623         TEST_ASSERT(interval > 0, "Interval must be greater than zero");
624
625         pr_info("Test iterations: %"PRIu64", interval: %"PRIu64" (ms)\n",
626                 iterations, interval);
627
628         srandom(time(0));
629
630         for (i = 0; i < NUM_VM_MODES; ++i) {
631                 if (!guest_modes[i].enabled)
632                         continue;
633                 TEST_ASSERT(guest_modes[i].supported,
634                             "Guest mode ID %d (%s) not supported.",
635                             i, vm_guest_mode_string(i));
636                 if (host_log_mode_option == LOG_MODE_ALL) {
637                         /* Run each log mode */
638                         for (j = 0; j < LOG_MODE_NUM; j++) {
639                                 pr_info("Testing Log Mode '%s'\n",
640                                         log_modes[j].name);
641                                 host_log_mode = j;
642                                 run_test(i, iterations, interval, phys_offset);
643                         }
644                 } else {
645                         host_log_mode = host_log_mode_option;
646                         run_test(i, iterations, interval, phys_offset);
647                 }
648         }
649
650         return 0;
651 }