Dirty-ring should be enabled after the VM is created, but before the
creation of any cpu. To to so, add an option on memstress_create_vm() that
takes dirty-ring size, and enables it at the correct moment.

This required a new function memstress_vm_create_with_vcpus() to be
created based on the previously used __vm_create_with_vcpus();

Signed-off-by: Leonardo Bras <[email protected]>
---
 .../testing/selftests/kvm/include/memstress.h |  1 +
 tools/testing/selftests/kvm/lib/memstress.c   | 27 +++++++++++++++----
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/memstress.h 
b/tools/testing/selftests/kvm/include/memstress.h
index cea2062e1dcb..465edf0da8f3 100644
--- a/tools/testing/selftests/kvm/include/memstress.h
+++ b/tools/testing/selftests/kvm/include/memstress.h
@@ -53,20 +53,21 @@ struct memstress_args {
        struct memstress_vcpu_args vcpu_args[KVM_MAX_VCPUS];
 };
 
 struct memstress_params {
        enum vm_guest_mode mode;
        int nr_vcpus;
        u64 vcpu_memory_bytes;
        int slots;
        enum vm_mem_backing_src_type backing_src;
        bool partition_vcpu_memory_access;
+       u32 dirty_ring_size;
 };
 
 extern struct memstress_args memstress_args;
 
 struct kvm_vm *memstress_create_vm(struct memstress_params *params);
 void memstress_destroy_vm(struct kvm_vm *vm);
 
 void memstress_set_write_percent(struct kvm_vm *vm, u32 write_percent);
 void memstress_set_random_access(struct kvm_vm *vm, bool random_access);
 
diff --git a/tools/testing/selftests/kvm/lib/memstress.c 
b/tools/testing/selftests/kvm/lib/memstress.c
index 880657df65e9..b1f13499e85f 100644
--- a/tools/testing/selftests/kvm/lib/memstress.c
+++ b/tools/testing/selftests/kvm/lib/memstress.c
@@ -114,20 +114,41 @@ void memstress_setup_vcpus(struct kvm_vm *vm, int 
nr_vcpus,
                }
 
                vcpu_args_set(vcpus[i], 1, i);
 
                pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
                         i, vcpu_args->gpa, vcpu_args->gpa +
                         (vcpu_args->pages * args->guest_page_size));
        }
 }
 
+static struct kvm_vm *memstress_vm_create_with_vcpus(struct memstress_params 
*params,
+                                                    u64 extra_mem_pages)
+{
+       struct kvm_vm *vm;
+       int i;
+
+       TEST_ASSERT(params->nr_vcpus > 0, "Invalid number of vCPUs (%d)",
+                   params->nr_vcpus);
+
+       vm = __vm_create(VM_SHAPE(params->mode), params->nr_vcpus, 
extra_mem_pages);
+
+       if (params->dirty_ring_size)
+               vm_enable_dirty_ring(vm, params->dirty_ring_size);
+
+       for (i = 0; i < params->nr_vcpus; ++i)
+               vcpus[i] = vm_vcpu_add(vm, i, memstress_guest_code);
+
+       kvm_arch_vm_finalize_vcpus(vm);
+       return vm;
+}
+
 struct kvm_vm *memstress_create_vm(struct memstress_params *params)
 {
        struct memstress_args *args = &memstress_args;
        struct kvm_vm *vm;
        u64 guest_num_pages, slot0_pages = 0;
        u64 backing_src_pagesz = get_backing_src_pagesz(params->backing_src);
        u64 region_end_gfn;
        int i;
 
        pr_info("Testing guest mode: %s\n", vm_guest_mode_string(params->mode));
@@ -158,25 +179,21 @@ struct kvm_vm *memstress_create_vm(struct 
memstress_params *params)
         * in-memory data structures.
         */
        if (args->nested)
                slot0_pages += memstress_nested_pages(params->nr_vcpus);
 
        /*
         * Pass guest_num_pages to populate the page tables for test memory.
         * The memory is also added to memslot 0, but that's a benign side
         * effect as KVM allows aliasing HVAs in meslots.
         */
-       vm = __vm_create_with_vcpus(VM_SHAPE(params->mode),
-                                   params->nr_vcpus,
-                                   slot0_pages + guest_num_pages,
-                                   memstress_guest_code, vcpus);
-
+       vm = memstress_vm_create_with_vcpus(params, slot0_pages + 
guest_num_pages);
        args->vm = vm;
 
        /* Put the test region at the top guest physical memory. */
        region_end_gfn = vm->max_gfn + 1;
 
 #ifdef __x86_64__
        /*
         * When running vCPUs in L2, restrict the test region to 48 bits to
         * avoid needing 5-level page tables to identity map L2.
         */
-- 
2.55.0


Reply via email to