[PATCH 1/2] KVM: selftests: Extract memslot setup code from spawn_vm()
From: Yohei Kojima
Date: Thu Feb 26 2026 - 02:35:37 EST
Add vm_userspace_mem_region_add_map() function to set up memslot, and
update spawn_vm() to use it. This helps the test cases to create
additional memslots easily.
Signed-off-by: Yohei Kojima <yohei.kojima@xxxxxxxx>
---
.../selftests/kvm/set_memory_region_test.c | 47 ++++++++++++-------
1 file changed, 29 insertions(+), 18 deletions(-)
diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c
index 7fe427ff9b38..8d4fd713347c 100644
--- a/tools/testing/selftests/kvm/set_memory_region_test.c
+++ b/tools/testing/selftests/kvm/set_memory_region_test.c
@@ -107,31 +107,42 @@ static void wait_for_vcpu(void)
usleep(100000);
}
+static void vm_userspace_mem_region_add_map(struct kvm_vm *vm,
+ uint64_t addr, uint32_t slot, size_t size, uint32_t flags)
+{
+ uint64_t *hva;
+ uint64_t gpa;
+
+ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_THP,
+ addr, slot,
+ size / getpagesize(), flags);
+
+ /*
+ * Allocate and map two pages so that the GPA accessed by guest_code()
+ * stays valid across the memslot move.
+ */
+ gpa = vm_phy_pages_alloc(vm, 2, addr, slot);
+ TEST_ASSERT(gpa == addr, "Failed vm_phy_pages_alloc\n");
+
+ virt_map(vm, addr, addr, 2);
+
+ /* Ditto for the host mapping so that both pages can be zeroed. */
+ hva = addr_gpa2hva(vm, MEM_REGION_GPA);
+ memset(hva, 0, 2 * 4096);
+}
+
static struct kvm_vm *spawn_vm(struct kvm_vcpu **vcpu, pthread_t *vcpu_thread,
void *guest_code)
{
struct kvm_vm *vm;
- uint64_t *hva;
- uint64_t gpa;
vm = vm_create_with_one_vcpu(vcpu, guest_code);
- vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_THP,
- MEM_REGION_GPA, MEM_REGION_SLOT,
- MEM_REGION_SIZE / getpagesize(), 0);
-
- /*
- * Allocate and map two pages so that the GPA accessed by guest_code()
- * stays valid across the memslot move.
- */
- gpa = vm_phy_pages_alloc(vm, 2, MEM_REGION_GPA, MEM_REGION_SLOT);
- TEST_ASSERT(gpa == MEM_REGION_GPA, "Failed vm_phy_pages_alloc\n");
-
- virt_map(vm, MEM_REGION_GPA, MEM_REGION_GPA, 2);
-
- /* Ditto for the host mapping so that both pages can be zeroed. */
- hva = addr_gpa2hva(vm, MEM_REGION_GPA);
- memset(hva, 0, 2 * 4096);
+ vm_userspace_mem_region_add_map(vm,
+ MEM_REGION_GPA,
+ MEM_REGION_SLOT,
+ MEM_REGION_SIZE,
+ 0);
pthread_create(vcpu_thread, NULL, vcpu_worker, *vcpu);
--
2.43.0