1 // SPDX-License-Identifier: GPL-2.0
3 * KVM dirty page logging test
5 * Copyright (C) 2018, Red Hat, Inc.
8 #define _GNU_SOURCE /* for program_invocation_name */
15 #include <linux/bitmap.h>
16 #include <linux/bitops.h>
18 #include "test_util.h"
20 #include "processor.h"
26 /* The memory slot index to track dirty pages */
27 #define TEST_MEM_SLOT_INDEX 1
29 /* Default guest test memory offset, 1G */
30 #define DEFAULT_GUEST_TEST_MEM 0x40000000
32 /* How many pages to dirty for each guest loop */
33 #define TEST_PAGES_PER_LOOP 1024
35 /* How many host loops to run (one KVM_GET_DIRTY_LOG for each loop) */
36 #define TEST_HOST_LOOP_N 32
38 /* Interval for each host loop (ms) */
39 #define TEST_HOST_LOOP_INTERVAL 10
42 * Guest/Host shared variables. Ensure addr_gva2hva() and/or
43 * sync_global_to/from_guest() are used when accessing from
44 * the host. READ/WRITE_ONCE() should also be used with anything
47 static uint64_t host_page_size;
48 static uint64_t guest_page_size;
49 static uint64_t guest_num_pages;
50 static uint64_t random_array[TEST_PAGES_PER_LOOP];
51 static uint64_t iteration;
54 * GPA offset of the testing memory slot. Must be bigger than
55 * DEFAULT_GUEST_PHY_PAGES.
57 static uint64_t guest_test_mem = DEFAULT_GUEST_TEST_MEM;
60 * Continuously write to the first 8 bytes of a random pages within
61 * the testing memory region.
63 static void guest_code(void)
68 for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
69 uint64_t addr = guest_test_mem;
70 addr += (READ_ONCE(random_array[i]) % guest_num_pages)
72 addr &= ~(host_page_size - 1);
73 *(uint64_t *)addr = READ_ONCE(iteration);
76 /* Tell the host that we need more random numbers */
82 static bool host_quit;
84 /* Points to the test VM memory region on which we track dirty logs */
85 static void *host_test_mem;
86 static uint64_t host_num_pages;
88 /* For statistics only */
89 static uint64_t host_dirty_count;
90 static uint64_t host_clear_count;
91 static uint64_t host_track_next_count;
94 * We use this bitmap to track some pages that should have its dirty
95 * bit set in the _next_ iteration. For example, if we detected the
96 * page value changed to current iteration but at the same time the
97 * page bit is cleared in the latest bitmap, then the system must
98 * report that write in the next get dirty log call.
100 static unsigned long *host_bmap_track;
102 static void generate_random_array(uint64_t *guest_array, uint64_t size)
106 for (i = 0; i < size; i++)
107 guest_array[i] = random();
110 static void *vcpu_worker(void *data)
113 struct kvm_vm *vm = data;
114 uint64_t *guest_array;
115 uint64_t pages_count = 0;
119 run = vcpu_state(vm, VCPU_ID);
121 guest_array = addr_gva2hva(vm, (vm_vaddr_t)random_array);
122 generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
124 while (!READ_ONCE(host_quit)) {
125 /* Let the guest dirty the random pages */
126 ret = _vcpu_run(vm, VCPU_ID);
127 if (get_ucall(vm, VCPU_ID, &uc) == UCALL_SYNC) {
128 pages_count += TEST_PAGES_PER_LOOP;
129 generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
132 "Invalid guest sync status: "
134 exit_reason_str(run->exit_reason));
138 DEBUG("Dirtied %"PRIu64" pages\n", pages_count);
143 static void vm_dirty_log_verify(unsigned long *bmap)
147 uint64_t step = host_page_size >= guest_page_size ? 1 :
148 guest_page_size / host_page_size;
150 for (page = 0; page < host_num_pages; page += step) {
151 value_ptr = host_test_mem + page * host_page_size;
153 /* If this is a special page that we were tracking... */
154 if (test_and_clear_bit(page, host_bmap_track)) {
155 host_track_next_count++;
156 TEST_ASSERT(test_bit(page, bmap),
157 "Page %"PRIu64" should have its dirty bit "
158 "set in this iteration but it is missing",
162 if (test_bit(page, bmap)) {
165 * If the bit is set, the value written onto
166 * the corresponding page should be either the
167 * previous iteration number or the current one.
169 TEST_ASSERT(*value_ptr == iteration ||
170 *value_ptr == iteration - 1,
171 "Set page %"PRIu64" value %"PRIu64
172 " incorrect (iteration=%"PRIu64")",
173 page, *value_ptr, iteration);
177 * If cleared, the value written can be any
178 * value smaller or equals to the iteration
179 * number. Note that the value can be exactly
180 * (iteration-1) if that write can happen
183 * (1) increase loop count to "iteration-1"
184 * (2) write to page P happens (with value
186 * (3) get dirty log for "iteration-1"; we'll
187 * see that page P bit is set (dirtied),
188 * and not set the bit in host_bmap_track
189 * (4) increase loop count to "iteration"
190 * (which is current iteration)
191 * (5) get dirty log for current iteration,
192 * we'll see that page P is cleared, with
193 * value "iteration-1".
195 TEST_ASSERT(*value_ptr <= iteration,
196 "Clear page %"PRIu64" value %"PRIu64
197 " incorrect (iteration=%"PRIu64")",
198 page, *value_ptr, iteration);
199 if (*value_ptr == iteration) {
201 * This page is _just_ modified; it
202 * should report its dirtyness in the
205 set_bit(page, host_bmap_track);
211 static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,
212 uint64_t extra_mem_pages, void *guest_code)
215 uint64_t extra_pg_pages = extra_mem_pages / 512 * 2;
217 vm = vm_create(mode, DEFAULT_GUEST_PHY_PAGES + extra_pg_pages, O_RDWR);
218 kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
220 vm_create_irqchip(vm);
222 vm_vcpu_add_default(vm, vcpuid, guest_code);
226 static void run_test(enum vm_guest_mode mode, unsigned long iterations,
227 unsigned long interval, bool top_offset)
229 unsigned int guest_pa_bits, guest_page_shift;
230 pthread_t vcpu_thread;
236 case VM_MODE_P52V48_4K:
238 guest_page_shift = 12;
240 case VM_MODE_P52V48_64K:
242 guest_page_shift = 16;
244 case VM_MODE_P40V48_4K:
246 guest_page_shift = 12;
248 case VM_MODE_P40V48_64K:
250 guest_page_shift = 16;
253 TEST_ASSERT(false, "Unknown guest mode, mode: 0x%x", mode);
256 DEBUG("Testing guest mode: %s\n", vm_guest_mode_string(mode));
258 max_gfn = (1ul << (guest_pa_bits - guest_page_shift)) - 1;
259 guest_page_size = (1ul << guest_page_shift);
260 /* 1G of guest page sized pages */
261 guest_num_pages = (1ul << (30 - guest_page_shift));
262 host_page_size = getpagesize();
263 host_num_pages = (guest_num_pages * guest_page_size) / host_page_size +
264 !!((guest_num_pages * guest_page_size) % host_page_size);
267 guest_test_mem = (max_gfn - guest_num_pages) * guest_page_size;
268 guest_test_mem &= ~(host_page_size - 1);
271 DEBUG("guest test mem offset: 0x%lx\n", guest_test_mem);
273 bmap = bitmap_alloc(host_num_pages);
274 host_bmap_track = bitmap_alloc(host_num_pages);
276 vm = create_vm(mode, VCPU_ID, guest_num_pages, guest_code);
278 /* Add an extra memory slot for testing dirty logging */
279 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
283 KVM_MEM_LOG_DIRTY_PAGES);
285 /* Do 1:1 mapping for the dirty track memory slot */
286 virt_map(vm, guest_test_mem, guest_test_mem,
287 guest_num_pages * guest_page_size, 0);
289 /* Cache the HVA pointer of the region */
290 host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)guest_test_mem);
293 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
296 ucall_init(vm, UCALL_MMIO, NULL);
299 /* Export the shared variables to the guest */
300 sync_global_to_guest(vm, host_page_size);
301 sync_global_to_guest(vm, guest_page_size);
302 sync_global_to_guest(vm, guest_test_mem);
303 sync_global_to_guest(vm, guest_num_pages);
305 /* Start the iterations */
307 sync_global_to_guest(vm, iteration);
309 host_dirty_count = 0;
310 host_clear_count = 0;
311 host_track_next_count = 0;
313 pthread_create(&vcpu_thread, NULL, vcpu_worker, vm);
315 while (iteration < iterations) {
316 /* Give the vcpu thread some time to dirty some pages */
317 usleep(interval * 1000);
318 kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
319 vm_dirty_log_verify(bmap);
321 sync_global_to_guest(vm, iteration);
324 /* Tell the vcpu thread to quit */
326 pthread_join(vcpu_thread, NULL);
328 DEBUG("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), "
329 "track_next (%"PRIu64")\n", host_dirty_count, host_clear_count,
330 host_track_next_count);
333 free(host_bmap_track);
338 static struct vm_guest_modes {
339 enum vm_guest_mode mode;
342 } vm_guest_modes[NUM_VM_MODES] = {
343 #if defined(__x86_64__)
344 { VM_MODE_P52V48_4K, 1, 1, },
345 { VM_MODE_P52V48_64K, 0, 0, },
346 { VM_MODE_P40V48_4K, 0, 0, },
347 { VM_MODE_P40V48_64K, 0, 0, },
348 #elif defined(__aarch64__)
349 { VM_MODE_P52V48_4K, 0, 0, },
350 { VM_MODE_P52V48_64K, 0, 0, },
351 { VM_MODE_P40V48_4K, 1, 1, },
352 { VM_MODE_P40V48_64K, 1, 1, },
356 static void help(char *name)
361 printf("usage: %s [-h] [-i iterations] [-I interval] "
362 "[-o offset] [-t] [-m mode]\n", name);
364 printf(" -i: specify iteration counts (default: %"PRIu64")\n",
366 printf(" -I: specify interval in ms (default: %"PRIu64" ms)\n",
367 TEST_HOST_LOOP_INTERVAL);
368 printf(" -o: guest test memory offset (default: 0x%lx)\n",
369 DEFAULT_GUEST_TEST_MEM);
370 printf(" -t: map guest test memory at the top of the allowed "
371 "physical address range\n");
372 printf(" -m: specify the guest mode ID to test "
373 "(default: test all supported modes)\n"
374 " This option may be used multiple times.\n"
375 " Guest mode IDs:\n");
376 for (i = 0; i < NUM_VM_MODES; ++i) {
377 printf(" %d: %s%s\n",
378 vm_guest_modes[i].mode,
379 vm_guest_mode_string(vm_guest_modes[i].mode),
380 vm_guest_modes[i].supported ? " (supported)" : "");
386 int main(int argc, char *argv[])
388 unsigned long iterations = TEST_HOST_LOOP_N;
389 unsigned long interval = TEST_HOST_LOOP_INTERVAL;
390 bool mode_selected = false;
391 bool top_offset = false;
395 while ((opt = getopt(argc, argv, "hi:I:o:tm:")) != -1) {
398 iterations = strtol(optarg, NULL, 10);
401 interval = strtol(optarg, NULL, 10);
404 guest_test_mem = strtoull(optarg, NULL, 0);
410 if (!mode_selected) {
411 for (i = 0; i < NUM_VM_MODES; ++i)
412 vm_guest_modes[i].enabled = 0;
413 mode_selected = true;
415 mode = strtoul(optarg, NULL, 10);
416 TEST_ASSERT(mode < NUM_VM_MODES,
417 "Guest mode ID %d too big", mode);
418 vm_guest_modes[mode].enabled = 1;
427 TEST_ASSERT(iterations > 2, "Iterations must be greater than two");
428 TEST_ASSERT(interval > 0, "Interval must be greater than zero");
429 TEST_ASSERT(!top_offset || guest_test_mem == DEFAULT_GUEST_TEST_MEM,
430 "Cannot use both -o [offset] and -t at the same time");
432 DEBUG("Test iterations: %"PRIu64", interval: %"PRIu64" (ms)\n",
433 iterations, interval);
437 for (i = 0; i < NUM_VM_MODES; ++i) {
438 if (!vm_guest_modes[i].enabled)
440 TEST_ASSERT(vm_guest_modes[i].supported,
441 "Guest mode ID %d (%s) not supported.",
442 vm_guest_modes[i].mode,
443 vm_guest_mode_string(vm_guest_modes[i].mode));
444 run_test(vm_guest_modes[i].mode, iterations, interval, top_offset);