Skip to contentSkip to conversation

KVM: selftests: Test partial KVM_CLEAR_DIRTY_LOG requests

PATCH New Tharit Tangkijwanichakul 2 participants 2 messages Sep 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: selftests: Test partial KVM_CLEAR_DIRTY_LOG requests

The clear-log mode invokes KVM_CLEAR_DIRTY_LOG with first_page set to zero and num_pages covering the entire memory slot. But the clear-log allows targeted clearing. So, the test does not verify that KVM correctly applies a bitmap relative to a nonzero first_page, or that pages not selected by a partial request remain dirty.

Add a focused test that creates a memory slot with KVM_DIRTY_LOG_INITIALLY_SET, then submits a 64-page clear request starting at page 64 with only bitmap bit 1 set. Verify that memslot page 65 becomes clean while every other host-page bit remains dirty.

Write to page 65 from the guest and verify that dirty tracking was re-enabled and the page becomes dirty again.

---
Changes in v2:
Address two issues from Sashiko's review
- Address Out-of-bound guest access when host page size
exceeds the guest page size by sizing the test memslot from 192 host pages, converting to guest pages before applying the allocation adjustment.
- Skip the partial-clear test if KVM_DIRTY_LOG_INITIALLY_SET is not
supported, since the test requires the bitmap to start with all pages marked dirty.

Test Results:
- on x86_64 with 4K host and guest pages:
./dirty_log_test -M clear-log
PASS
- on arm64 Rock 5B with 4K host pages, including 4K, 16K and 64K guest page modes:
./dirty_log_test -M clear-log
PASS

---
 tools/testing/selftests/kvm/dirty_log_test.c | 112 +++++++++++++++++++
 1 file changed, 112 insertions(+)
tools/testing/selftests/kvm/dirty_log_test.c +112 -0
index af5eb0334a74..3976748f3940 100644
@@ -815,6 +815,114 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	kvm_vm_free(vm);
 }
 
+/* Use the 2nd 64-page range to exercise a non-zero offset. */
+#define TEST_CLEAR_LOG_FIRST_PAGE	64
+#define TEST_CLEAR_LOG_NUM_PAGES	64
+#define TEST_CLEAR_LOG_BIT		1
+#define TEST_CLEAR_LOG_PAGE		(TEST_CLEAR_LOG_FIRST_PAGE + \
+					 TEST_CLEAR_LOG_BIT)
+static void guest_code_clear_log(void)
+{
+	u64 addr = guest_test_virt_mem + TEST_CLEAR_LOG_PAGE * host_page_size;
+
+	while (true) {
+		vcpu_arch_put_guest(*(u64 *)addr, 1);
+		GUEST_SYNC(1);
+	}
+}
+
+/*
+ * Verify KVM_CLEAR_DIRTY_LOG only clears (and re-arms) the requested range:
+ * with KVM_DIRTY_LOG_INITIALLY_SET every page starts dirty; clearing a single
+ * page must leave its neighbours dirty and must write-protect the page so the
+ * next guest write re-dirties it.
+ */
+static void run_clear_log_partial_test(enum vm_guest_mode mode, void *arg)
+{
+	unsigned long *bmap, *clear_bmap;
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	u64 page;
+
+	/* Allocate enough memory for three 64-host-page range */
+	guest_num_pages = vm_adjust_num_guest_pages(mode,
+			vm_num_guest_pages(mode, 3 * 64));
+
+	vm = create_vm(mode, &vcpu, 2 * guest_num_pages, guest_code_clear_log);
+
+	guest_page_size = vm->page_size;
+	host_num_pages = vm_num_host_pages(mode, guest_num_pages);
+	host_page_size = getpagesize();
+
+	guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
+	guest_test_phys_mem = (vm->max_gfn - guest_num_pages) * guest_page_size;
+	guest_test_phys_mem = align_down(guest_test_phys_mem, host_page_size);
+
+	bmap = bitmap_zalloc(host_num_pages);
+	clear_bmap = bitmap_zalloc(BITS_PER_LONG);
+
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
+				    guest_test_phys_mem, TEST_MEM_SLOT_INDEX,
+				    guest_num_pages, KVM_MEM_LOG_DIRTY_PAGES);
+	virt_map(vm, guest_test_virt_mem, guest_test_phys_mem, guest_num_pages);
+
+	sync_global_to_guest(vm, host_page_size);
+	sync_global_to_guest(vm, guest_test_virt_mem);
+
+	kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
+	for (page = 0; page < host_num_pages; page++)
+		TEST_ASSERT(test_bit_le(page, bmap),
+			    "Page %lu should be dirty after INITIALLY_SET", page);
+
+	__set_bit_le(TEST_CLEAR_LOG_BIT, clear_bmap);
+	kvm_vm_clear_dirty_log(vm, TEST_MEM_SLOT_INDEX, clear_bmap,
+			       TEST_CLEAR_LOG_FIRST_PAGE,
+			       TEST_CLEAR_LOG_NUM_PAGES);
+
+	kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
+	for (page = 0; page < host_num_pages; page++) {
+		if (page == TEST_CLEAR_LOG_PAGE)
+			TEST_ASSERT(!test_bit_le(page, bmap),
+				    "Cleared page %lu should be clean", page);
+		else
+			TEST_ASSERT(test_bit_le(page, bmap),
+				    "Page %lu should still be dirty", page);
+	}
+
+	/* The cleared page is now write-protected; a guest write must re-arm it */
+	vcpu_run(vcpu);
+	TEST_ASSERT(get_ucall(vcpu, NULL) == UCALL_SYNC,
+		    "Unexpected guest exit: %s",
+		    exit_reason_str(vcpu->run->exit_reason));
+
+	kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
+	TEST_ASSERT(test_bit_le(TEST_CLEAR_LOG_PAGE, bmap),
+		    "Page %lu should be dirty again after re-arming",
+		    (u64)TEST_CLEAR_LOG_PAGE);
+
+	free(bmap);
+	free(clear_bmap);
+	kvm_vm_free(vm);
+}
+
+static void test_clear_log_partial(void)
+{
+	host_log_mode = LOG_MODE_CLEAR_LOG;
+	if (!log_mode_supported()) {
+		print_skip("Log mode '%s' not supported",
+			   log_modes[host_log_mode].name);
+		return;
+	}
+
+	if (!(kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2) &
+		KVM_DIRTY_LOG_INITIALLY_SET)) {
+		print_skip("KVM_DIRTY_LOG_INITIALLY_SET not supported");
+		return;
+	}
+
+	for_each_guest_mode(run_clear_log_partial_test, NULL);
+}
+
 static void help(char *name)
 {
 	puts("");
@@ -913,5 +1021,9 @@ int main(int argc, char *argv[])
 		for_each_guest_mode(run_test, &p);
 	}
 
+	if (host_log_mode_option == LOG_MODE_ALL ||
+	    host_log_mode_option == LOG_MODE_CLEAR_LOG)
+		test_clear_log_partial();
+
 	return 0;
 }
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] kvm: selftests: out-of-bounds memory read in dirty_log_test --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: selftests: out-of-bounds memory read in dirty_log_test
--

commit 91a9082b8baef537794ce258865b745e98facf50
Author: Tharit Tangkijwanichakul <[email protected]>

KVM: selftests: Test partial KVM_CLEAR_DIRTY_LOG requests

This commit adds a test to verify that KVM correctly applies a clear log bitmap relative to a nonzero first_page offset. It introduces run_clear_log_partial_test() to ensure that pages not selected by a partial request remain dirty.

Quoted 4 lines
tools/testing/selftests/kvm/dirty_log_test.c +0 -0
index af5eb0334a74f..3976748f39400 100644

[ ... ]

Quoted 1 line
tools/testing/selftests/kvm/dirty_log_test.c +0 -0
@@ -815,6 +815,114 @@ static void run_test(enum vm_guest_mode mode, void *arg)

[ ... ]

Quoted 6 lines
tools/testing/selftests/kvm/dirty_log_test.c +6 -0
+static void run_clear_log_partial_test(enum vm_guest_mode mode, void *arg)
+{
+	unsigned long *bmap, *clear_bmap;
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	u64 page;

[ ... ]

tools/testing/selftests/kvm/dirty_log_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 862

Quoted 2 lines
tools/testing/selftests/kvm/dirty_log_test.c +2 -0
+	bmap = bitmap_zalloc(host_num_pages);
+	clear_bmap = bitmap_zalloc(BITS_PER_LONG);

[Severity: Medium]
Does this allocate an undersized buffer on 32-bit systems?

Using BITS_PER_LONG allocates 32 bits (4 bytes) on 32-bit architectures, rather than the 64 bits mandated by the KVM API bit count for the request below.

Quoted 2 lines
tools/testing/selftests/kvm/dirty_log_test.c +2 -0
+
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,

[ ... ]

tools/testing/selftests/kvm/dirty_log_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 880

Quoted 4 lines
tools/testing/selftests/kvm/dirty_log_test.c +4 -0
+	__set_bit_le(TEST_CLEAR_LOG_BIT, clear_bmap);
+	kvm_vm_clear_dirty_log(vm, TEST_MEM_SLOT_INDEX, clear_bmap,
+			       TEST_CLEAR_LOG_FIRST_PAGE,
+			       TEST_CLEAR_LOG_NUM_PAGES);

[Severity: Medium]
Will this trigger an out-of-bounds read of userspace heap memory?

The test passes clear_bmap alongside TEST_CLEAR_LOG_NUM_PAGES (which is 64) to kvm_vm_clear_dirty_log(). This instructs the kernel to read 64 bits (8 bytes) of dirty log data.

Since clear_bmap is only 4 bytes long on 32-bit systems, this appears to cause the kernel to read 4 bytes past the end of the allocated buffer.

Quoted 2 lines
tools/testing/selftests/kvm/dirty_log_test.c +2 -0
+
+	kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1