From: Jack Thomson <[email protected]>

Add a -m option to specify different memory backing types for the
pre-fault tests (e.g., anonymous, hugetlb), allowing testing of the
pre-fault functionality across different memory configurations.

Signed-off-by: Jack Thomson <[email protected]>
---
 .../selftests/kvm/pre_fault_memory_test.c     | 42 +++++++++++++++----
 1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/tools/testing/selftests/kvm/pre_fault_memory_test.c 
b/tools/testing/selftests/kvm/pre_fault_memory_test.c
index 674931e7bb3a..e1111c4df748 100644
--- a/tools/testing/selftests/kvm/pre_fault_memory_test.c
+++ b/tools/testing/selftests/kvm/pre_fault_memory_test.c
@@ -172,6 +172,7 @@ static void pre_fault_memory(struct kvm_vcpu *vcpu, u64 
base_gpa, u64 offset,
 struct test_params {
        unsigned long vm_type;
        bool private;
+       enum vm_mem_backing_src_type mem_backing_src;
 };
 
 static void __test_pre_fault_memory(enum vm_guest_mode guest_mode, void *arg)
@@ -190,14 +191,19 @@ static void __test_pre_fault_memory(enum vm_guest_mode 
guest_mode, void *arg)
        uint64_t guest_test_virt_mem;
        uint64_t alignment, guest_page_size;
 
+       size_t backing_src_pagesz = get_backing_src_pagesz(p->mem_backing_src);
+
        pr_info("Testing guest mode: %s\n", vm_guest_mode_string(guest_mode));
+       pr_info("Testing memory backing src type: %s\n",
+               vm_mem_backing_src_alias(p->mem_backing_src)->name);
 
        vm = vm_create_shape_with_one_vcpu(shape, &vcpu, guest_code);
 
        guest_page_size = vm_guest_mode_params[guest_mode].page_size;
 
        test_config.page_size = guest_page_size;
-       test_config.test_size = TEST_BASE_SIZE + test_config.page_size;
+       test_config.test_size = align_up(TEST_BASE_SIZE + test_config.page_size,
+                                        backing_src_pagesz);
        test_config.test_num_pages = vm_calc_num_guest_pages(vm->mode, 
test_config.test_size);
 
        guest_test_phys_mem = (vm->max_gfn - test_config.test_num_pages) * 
test_config.page_size;
@@ -206,20 +212,23 @@ static void __test_pre_fault_memory(enum vm_guest_mode 
guest_mode, void *arg)
 #else
        alignment = SZ_2M;
 #endif
+       alignment = max(alignment, backing_src_pagesz);
        guest_test_phys_mem = align_down(guest_test_phys_mem, alignment);
        guest_test_virt_mem = guest_test_phys_mem & ((1ULL << (vm->va_bits - 
1)) - 1);
 
-       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
+       vm_userspace_mem_region_add(vm, p->mem_backing_src,
                                    guest_test_phys_mem, TEST_SLOT, 
test_config.test_num_pages,
                                    p->private ? KVM_MEM_GUEST_MEMFD : 0);
        virt_map(vm, guest_test_virt_mem, guest_test_phys_mem, 
test_config.test_num_pages);
 
        if (p->private)
                vm_mem_set_private(vm, guest_test_phys_mem, 
test_config.test_size);
-       pre_fault_memory(vcpu, guest_test_phys_mem, TEST_BASE_SIZE, 0, 
p->private);
+
+       pre_fault_memory(vcpu, guest_test_phys_mem, test_config.test_size, 0, 
p->private);
        /* Test pre-faulting over an already faulted range */
-       pre_fault_memory(vcpu, guest_test_phys_mem, TEST_BASE_SIZE, 0, 
p->private);
-       pre_fault_memory(vcpu, guest_test_phys_mem + TEST_BASE_SIZE,
+       pre_fault_memory(vcpu, guest_test_phys_mem, test_config.test_size, 0, 
p->private);
+       pre_fault_memory(vcpu, guest_test_phys_mem +
+                        test_config.test_size - test_config.page_size,
                         test_config.page_size * 2, test_config.page_size, 
p->private);
        pre_fault_memory(vcpu, guest_test_phys_mem + test_config.test_size,
                         test_config.page_size, test_config.page_size, 
p->private);
@@ -251,7 +260,8 @@ static void __test_pre_fault_memory(enum vm_guest_mode 
guest_mode, void *arg)
        kvm_vm_free(vm);
 }
 
-static void test_pre_fault_memory(unsigned long vm_type, bool private)
+static void test_pre_fault_memory(unsigned long vm_type, enum 
vm_mem_backing_src_type backing_src,
+                                 bool private)
 {
        if (vm_type && !(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type))) {
                pr_info("Skipping tests for vm_type 0x%lx\n", vm_type);
@@ -261,6 +271,7 @@ static void test_pre_fault_memory(unsigned long vm_type, 
bool private)
        struct test_params p = {
                .vm_type = vm_type,
                .private = private,
+               .mem_backing_src = backing_src,
        };
 
        for_each_guest_mode(__test_pre_fault_memory, &p);
@@ -270,10 +281,23 @@ int main(int argc, char *argv[])
 {
        TEST_REQUIRE(kvm_check_cap(KVM_CAP_PRE_FAULT_MEMORY));
 
-       test_pre_fault_memory(0, false);
+       int opt;
+       enum vm_mem_backing_src_type backing = VM_MEM_SRC_ANONYMOUS;
+
+       while ((opt = getopt(argc, argv, "m:")) != -1) {
+               switch (opt) {
+               case 'm':
+                       backing = parse_backing_src_type(optarg);
+                       break;
+               default:
+                       break;
+               }
+       }
+
+       test_pre_fault_memory(0, backing, false);
 #ifdef __x86_64__
-       test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, false);
-       test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, true);
+       test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, backing, false);
+       test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, backing, true);
 #endif
        return 0;
 }
-- 
2.43.0


Reply via email to