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
30 * GPA offset of the testing memory slot. Must be bigger than the
31 * default vm mem slot, which is DEFAULT_GUEST_PHY_PAGES.
33 #define TEST_MEM_OFFSET (1ul << 30) /* 1G */
35 /* How many pages to dirty for each guest loop */
36 #define TEST_PAGES_PER_LOOP 1024
38 /* How many host loops to run (one KVM_GET_DIRTY_LOG for each loop) */
39 #define TEST_HOST_LOOP_N 32
41 /* Interval for each host loop (ms) */
42 #define TEST_HOST_LOOP_INTERVAL 10
45 * Guest/Host shared variables. Ensure addr_gva2hva() and/or
46 * sync_global_to/from_guest() are used when accessing from
47 * the host. READ/WRITE_ONCE() should also be used with anything
50 static uint64_t host_page_size;
51 static uint64_t guest_page_size;
52 static uint64_t guest_num_pages;
53 static uint64_t random_array[TEST_PAGES_PER_LOOP];
54 static uint64_t iteration;
57 * Continuously write to the first 8 bytes of a random pages within
58 * the testing memory region.
60 static void guest_code(void)
65 for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
66 uint64_t addr = TEST_MEM_OFFSET;
67 addr += (READ_ONCE(random_array[i]) % guest_num_pages)
69 addr &= ~(host_page_size - 1);
70 *(uint64_t *)addr = READ_ONCE(iteration);
73 /* Tell the host that we need more random numbers */
79 static bool host_quit;
81 /* Points to the test VM memory region on which we track dirty logs */
82 static void *host_test_mem;
83 static uint64_t host_num_pages;
85 /* For statistics only */
86 static uint64_t host_dirty_count;
87 static uint64_t host_clear_count;
88 static uint64_t host_track_next_count;
91 * We use this bitmap to track some pages that should have its dirty
92 * bit set in the _next_ iteration. For example, if we detected the
93 * page value changed to current iteration but at the same time the
94 * page bit is cleared in the latest bitmap, then the system must
95 * report that write in the next get dirty log call.
97 static unsigned long *host_bmap_track;
99 static void generate_random_array(uint64_t *guest_array, uint64_t size)
103 for (i = 0; i < size; i++)
104 guest_array[i] = random();
107 static void *vcpu_worker(void *data)
110 struct kvm_vm *vm = data;
111 uint64_t *guest_array;
112 uint64_t pages_count = 0;
116 run = vcpu_state(vm, VCPU_ID);
118 guest_array = addr_gva2hva(vm, (vm_vaddr_t)random_array);
119 generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
121 while (!READ_ONCE(host_quit)) {
122 /* Let the guest dirty the random pages */
123 ret = _vcpu_run(vm, VCPU_ID);
124 if (get_ucall(vm, VCPU_ID, &uc) == UCALL_SYNC) {
125 pages_count += TEST_PAGES_PER_LOOP;
126 generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
129 "Invalid guest sync status: "
131 exit_reason_str(run->exit_reason));
135 DEBUG("Dirtied %"PRIu64" pages\n", pages_count);
140 static void vm_dirty_log_verify(unsigned long *bmap)
144 uint64_t step = host_page_size >= guest_page_size ? 1 :
145 guest_page_size / host_page_size;
147 for (page = 0; page < host_num_pages; page += step) {
148 value_ptr = host_test_mem + page * host_page_size;
150 /* If this is a special page that we were tracking... */
151 if (test_and_clear_bit(page, host_bmap_track)) {
152 host_track_next_count++;
153 TEST_ASSERT(test_bit(page, bmap),
154 "Page %"PRIu64" should have its dirty bit "
155 "set in this iteration but it is missing",
159 if (test_bit(page, bmap)) {
162 * If the bit is set, the value written onto
163 * the corresponding page should be either the
164 * previous iteration number or the current one.
166 TEST_ASSERT(*value_ptr == iteration ||
167 *value_ptr == iteration - 1,
168 "Set page %"PRIu64" value %"PRIu64
169 " incorrect (iteration=%"PRIu64")",
170 page, *value_ptr, iteration);
174 * If cleared, the value written can be any
175 * value smaller or equals to the iteration
176 * number. Note that the value can be exactly
177 * (iteration-1) if that write can happen
180 * (1) increase loop count to "iteration-1"
181 * (2) write to page P happens (with value
183 * (3) get dirty log for "iteration-1"; we'll
184 * see that page P bit is set (dirtied),
185 * and not set the bit in host_bmap_track
186 * (4) increase loop count to "iteration"
187 * (which is current iteration)
188 * (5) get dirty log for current iteration,
189 * we'll see that page P is cleared, with
190 * value "iteration-1".
192 TEST_ASSERT(*value_ptr <= iteration,
193 "Clear page %"PRIu64" value %"PRIu64
194 " incorrect (iteration=%"PRIu64")",
195 page, *value_ptr, iteration);
196 if (*value_ptr == iteration) {
198 * This page is _just_ modified; it
199 * should report its dirtyness in the
202 set_bit(page, host_bmap_track);
208 static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,
209 uint64_t extra_mem_pages, void *guest_code)
212 uint64_t extra_pg_pages = extra_mem_pages / 512 * 2;
214 vm = vm_create(mode, DEFAULT_GUEST_PHY_PAGES + extra_pg_pages, O_RDWR);
215 kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
217 vm_create_irqchip(vm);
219 vm_vcpu_add_default(vm, vcpuid, guest_code);
223 static void run_test(enum vm_guest_mode mode, unsigned long iterations,
224 unsigned long interval)
226 unsigned int guest_page_shift;
227 pthread_t vcpu_thread;
232 case VM_MODE_P52V48_4K:
233 guest_page_shift = 12;
235 case VM_MODE_P52V48_64K:
236 guest_page_shift = 16;
239 TEST_ASSERT(false, "Unknown guest mode, mode: 0x%x", mode);
242 DEBUG("Testing guest mode: %s\n", vm_guest_mode_string(mode));
244 guest_page_size = (1ul << guest_page_shift);
245 /* 1G of guest page sized pages */
246 guest_num_pages = (1ul << (30 - guest_page_shift));
247 host_page_size = getpagesize();
248 host_num_pages = (guest_num_pages * guest_page_size) / host_page_size +
249 !!((guest_num_pages * guest_page_size) % host_page_size);
251 bmap = bitmap_alloc(host_num_pages);
252 host_bmap_track = bitmap_alloc(host_num_pages);
254 vm = create_vm(mode, VCPU_ID, guest_num_pages, guest_code);
256 /* Add an extra memory slot for testing dirty logging */
257 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
261 KVM_MEM_LOG_DIRTY_PAGES);
263 /* Do 1:1 mapping for the dirty track memory slot */
264 virt_map(vm, TEST_MEM_OFFSET, TEST_MEM_OFFSET,
265 guest_num_pages * guest_page_size, 0);
267 /* Cache the HVA pointer of the region */
268 host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)TEST_MEM_OFFSET);
271 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
274 ucall_init(vm, UCALL_MMIO, NULL);
277 /* Export the shared variables to the guest */
278 sync_global_to_guest(vm, host_page_size);
279 sync_global_to_guest(vm, guest_page_size);
280 sync_global_to_guest(vm, guest_num_pages);
282 /* Start the iterations */
284 sync_global_to_guest(vm, iteration);
286 host_dirty_count = 0;
287 host_clear_count = 0;
288 host_track_next_count = 0;
290 pthread_create(&vcpu_thread, NULL, vcpu_worker, vm);
292 while (iteration < iterations) {
293 /* Give the vcpu thread some time to dirty some pages */
294 usleep(interval * 1000);
295 kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
296 vm_dirty_log_verify(bmap);
298 sync_global_to_guest(vm, iteration);
301 /* Tell the vcpu thread to quit */
303 pthread_join(vcpu_thread, NULL);
305 DEBUG("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), "
306 "track_next (%"PRIu64")\n", host_dirty_count, host_clear_count,
307 host_track_next_count);
310 free(host_bmap_track);
315 static struct vm_guest_modes {
316 enum vm_guest_mode mode;
319 } vm_guest_modes[NUM_VM_MODES] = {
320 { VM_MODE_P52V48_4K, 1, 1, },
322 { VM_MODE_P52V48_64K, 1, 1, },
324 { VM_MODE_P52V48_64K, 0, 0, },
328 static void help(char *name)
333 printf("usage: %s [-h] [-i iterations] [-I interval] [-m mode]\n", name);
335 printf(" -i: specify iteration counts (default: %"PRIu64")\n",
337 printf(" -I: specify interval in ms (default: %"PRIu64" ms)\n",
338 TEST_HOST_LOOP_INTERVAL);
339 printf(" -m: specify the guest mode ID to test "
340 "(default: test all supported modes)\n"
341 " This option may be used multiple times.\n"
342 " Guest mode IDs:\n");
343 for (i = 0; i < NUM_VM_MODES; ++i) {
344 printf(" %d: %s%s\n",
345 vm_guest_modes[i].mode,
346 vm_guest_mode_string(vm_guest_modes[i].mode),
347 vm_guest_modes[i].supported ? " (supported)" : "");
353 int main(int argc, char *argv[])
355 unsigned long iterations = TEST_HOST_LOOP_N;
356 unsigned long interval = TEST_HOST_LOOP_INTERVAL;
357 bool mode_selected = false;
361 while ((opt = getopt(argc, argv, "hi:I:m:")) != -1) {
364 iterations = strtol(optarg, NULL, 10);
367 interval = strtol(optarg, NULL, 10);
370 if (!mode_selected) {
371 for (i = 0; i < NUM_VM_MODES; ++i)
372 vm_guest_modes[i].enabled = 0;
373 mode_selected = true;
375 mode = strtoul(optarg, NULL, 10);
376 TEST_ASSERT(mode < NUM_VM_MODES,
377 "Guest mode ID %d too big", mode);
378 vm_guest_modes[mode].enabled = 1;
387 TEST_ASSERT(iterations > 2, "Iterations must be greater than two");
388 TEST_ASSERT(interval > 0, "Interval must be greater than zero");
390 DEBUG("Test iterations: %"PRIu64", interval: %"PRIu64" (ms)\n",
391 iterations, interval);
395 for (i = 0; i < NUM_VM_MODES; ++i) {
396 if (!vm_guest_modes[i].enabled)
398 TEST_ASSERT(vm_guest_modes[i].supported,
399 "Guest mode ID %d (%s) not supported.",
400 vm_guest_modes[i].mode,
401 vm_guest_mode_string(vm_guest_modes[i].mode));
402 run_test(vm_guest_modes[i].mode, iterations, interval);