Skip to contentSkip to conversation

KVM: selftests: Hugepage fixes and cleanups

PATCH Applied David Matlack 4 participants 19 messages Nov 11, 2021 – Nov 16, 2021

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

v2 cover KVM: selftests: Hugepage fixes and cleanups

Fix hugepage bugs in the KVM selftests that specifically affect dirty logging and demand paging tests. Found while attempting to verify KVM changes/fixes related to hugepages and dirty logging (patches incoming in a separate series).

Clean up the perf_test_args util on top of the hugepage fixes to clarify what "page size" means, and to improve confidence in the code doing what it thinks it's doing. In a few cases, users of perf_test_args were duplicating (approximating?) calculations made by perf_test_args, and it wasn't obvious that both pieces of code were guaranteed to end up with the same result.

v2:
- Add separate align up/down helpers and use the throughout the series rather than openly coding the bitwise math [Ben, Paolo]
- Do no pad HugeTLB mmaps [Yanan]
- Drop "[PATCH 04/15] KVM: selftests: Force stronger HVA alignment (1gb) for hugepages" since HugeTLB does not require manual HVA alignment [David]
- Drop "[PATCH 15/15] KVM: selftests: Get rid of gorilla math in memslots modification test" since the gorilla math no longer exists [David]
- Drop "[PATCH 14/15] KVM: selftests: Track size of per-VM memslot in perf_test_args" since it was just a prep patch for [PATCH 15/15] [David]
- Update the series to kvm/next [David]

v1: https://lore.kernel.org/kvm/[email protected]/.

Sean Christopherson (12):
KVM: selftests: Explicitly state indicies for vm_guest_mode_params
array
KVM: selftests: Expose align() helpers to tests
KVM: selftests: Assert mmap HVA is aligned when using HugeTLB
KVM: selftests: Require GPA to be aligned when backed by hugepages
KVM: selftests: Use shorthand local var to access struct
perf_tests_args
KVM: selftests: Capture per-vCPU GPA in perf_test_vcpu_args
KVM: selftests: Use perf util's per-vCPU GPA/pages in demand paging
test
KVM: selftests: Move per-VM GPA into perf_test_args
KVM: selftests: Remove perf_test_args.host_page_size
KVM: selftests: Create VM with adjusted number of guest pages for perf
tests
KVM: selftests: Fill per-vCPU struct during "perf_test" VM creation
KVM: selftests: Sync perf_test_args to guest during VM creation

 .../selftests/kvm/access_tracking_perf_test.c |   8 +-
 .../selftests/kvm/demand_paging_test.c        |  31 +----
 .../selftests/kvm/dirty_log_perf_test.c       |  10 +-
 tools/testing/selftests/kvm/dirty_log_test.c  |   6 +-
 .../selftests/kvm/include/perf_test_util.h    |  18 +--
 .../testing/selftests/kvm/include/test_util.h |  26 ++++
 .../selftests/kvm/kvm_page_table_test.c       |   2 +-
 tools/testing/selftests/kvm/lib/elf.c         |   3 +-
 tools/testing/selftests/kvm/lib/kvm_util.c    |  44 +++---
 .../selftests/kvm/lib/perf_test_util.c        | 126 ++++++++++--------
 tools/testing/selftests/kvm/lib/test_util.c   |   5 +
 .../kvm/memslot_modification_stress_test.c    |  13 +-
 12 files changed, 153 insertions(+), 139 deletions(-)
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#1 lore raw

PATCH v2 1/12 KVM: selftests: Explicitly state indicies for vm_guest_mode_params array

From: Sean Christopherson <[email protected]>

Explicitly state the indices when populating vm_guest_mode_params to make it marginally easier to visualize what's going on.

No functional change intended.

[Added indices for new guest modes.]
---
 tools/testing/selftests/kvm/lib/kvm_util.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +9 -9
index 041004c0fda7..b624c24290dd 100644
@@ -187,15 +187,15 @@ const char *vm_guest_mode_string(uint32_t i)
 }
 
 const struct vm_guest_mode_params vm_guest_mode_params[] = {
-	{ 52, 48,  0x1000, 12 },
-	{ 52, 48, 0x10000, 16 },
-	{ 48, 48,  0x1000, 12 },
-	{ 48, 48, 0x10000, 16 },
-	{ 40, 48,  0x1000, 12 },
-	{ 40, 48, 0x10000, 16 },
-	{  0,  0,  0x1000, 12 },
-	{ 47, 64,  0x1000, 12 },
-	{ 44, 64,  0x1000, 12 },
+	[VM_MODE_P52V48_4K]	= { 52, 48,  0x1000, 12 },
+	[VM_MODE_P52V48_64K]	= { 52, 48, 0x10000, 16 },
+	[VM_MODE_P48V48_4K]	= { 48, 48,  0x1000, 12 },
+	[VM_MODE_P48V48_64K]	= { 48, 48, 0x10000, 16 },
+	[VM_MODE_P40V48_4K]	= { 40, 48,  0x1000, 12 },
+	[VM_MODE_P40V48_64K]	= { 40, 48, 0x10000, 16 },
+	[VM_MODE_PXXV48_4K]	= {  0,  0,  0x1000, 12 },
+	[VM_MODE_P47V64_4K]	= { 47, 64,  0x1000, 12 },
+	[VM_MODE_P44V64_4K]	= { 44, 64,  0x1000, 12 },
 };
 _Static_assert(sizeof(vm_guest_mode_params)/sizeof(struct vm_guest_mode_params) == NUM_VM_MODES,
 	       "Missing new mode params?");
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#2 lore raw

PATCH v2 2/12 KVM: selftests: Expose align() helpers to tests

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Refactor align() to work with non-pointers and split into separate helpers for aligning up vs. down. Add align_ptr_up() for use with pointers. Expose all helpers so that they can be used by tests and/or other utilities. The align_down() helper in particular will be used to ensure gpa alignment for hugepages.

No functional change intended.

[Added sepearate up/down helpers and replaced open-coded alignment
bit math throughout the KVM selftests.]

---
 tools/testing/selftests/kvm/dirty_log_test.c  |  6 ++---
 .../testing/selftests/kvm/include/test_util.h | 25 +++++++++++++++++++
 .../selftests/kvm/kvm_page_table_test.c       |  2 +-
 tools/testing/selftests/kvm/lib/elf.c         |  3 +--
 tools/testing/selftests/kvm/lib/kvm_util.c    | 13 ++--------
 .../selftests/kvm/lib/perf_test_util.c        |  4 +--
 6 files changed, 34 insertions(+), 19 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +3 -3
index 792c60e1b17d..3fcd89e195c7 100644
@@ -115,7 +115,7 @@ static void guest_code(void)
 			addr = guest_test_virt_mem;
 			addr += (READ_ONCE(random_array[i]) % guest_num_pages)
 				* guest_page_size;
-			addr &= ~(host_page_size - 1);
+			addr = align_down(addr, host_page_size);
 			*(uint64_t *)addr = READ_ONCE(iteration);
 		}
 
@@ -737,14 +737,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	if (!p->phys_offset) {
 		guest_test_phys_mem = (vm_get_max_gfn(vm) -
 				       guest_num_pages) * guest_page_size;
-		guest_test_phys_mem &= ~(host_page_size - 1);
+		guest_test_phys_mem = align_down(guest_test_phys_mem, host_page_size);
 	} else {
 		guest_test_phys_mem = p->phys_offset;
 	}
 
 #ifdef __s390x__
 	/* Align to 1M (segment size) */
-	guest_test_phys_mem &= ~((1 << 20) - 1);
+	guest_test_phys_mem = align_down(guest_test_phys_mem, 1 << 20);
 #endif
 
 	pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem);
tools/testing/selftests/kvm/include/test_util.h +25 -0
index f8fddc84c0d3..78c06310cc0e 100644
@@ -117,4 +117,29 @@ static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)
 	return vm_mem_backing_src_alias(t)->flag & MAP_SHARED;
 }
 
+/* Aligns x up to the next multiple of size. Size must be a power of 2. */
+static inline uint64_t align_up(uint64_t x, uint64_t size)
+{
+	uint64_t mask = size - 1;
+
+	TEST_ASSERT(size != 0 && !(size & (size - 1)),
+		    "size not a power of 2: %lu", size);
+	return ((x + mask) & ~mask);
+}
+
+static inline uint64_t align_down(uint64_t x, uint64_t size)
+{
+	uint64_t x_aligned_up = align_up(x, size);
+
+	if (x == x_aligned_up)
+		return x;
+	else
+		return x_aligned_up - size;
+}
+
+static inline void *align_ptr_up(void *x, size_t size)
+{
+	return (void *)align_up((unsigned long)x, size);
+}
+
 #endif /* SELFTEST_KVM_TEST_UTIL_H */
tools/testing/selftests/kvm/kvm_page_table_test.c +1 -1
index 36407cb0ec85..3836322add00 100644
@@ -280,7 +280,7 @@ static struct kvm_vm *pre_init_before_test(enum vm_guest_mode mode, void *arg)
 #ifdef __s390x__
 	alignment = max(0x100000, alignment);
 #endif
-	guest_test_phys_mem &= ~(alignment - 1);
+	guest_test_phys_mem = align_down(guest_test_virt_mem, alignment);
 
 	/* Set up the shared data structure test_args */
 	test_args.vm = vm;
tools/testing/selftests/kvm/lib/elf.c +1 -2
index eac44f5d0db0..13e8e3dcf984 100644
@@ -157,8 +157,7 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)
 			"memsize of 0,\n"
 			"  phdr index: %u p_memsz: 0x%" PRIx64,
 			n1, (uint64_t) phdr.p_memsz);
-		vm_vaddr_t seg_vstart = phdr.p_vaddr;
-		seg_vstart &= ~(vm_vaddr_t)(vm->page_size - 1);
+		vm_vaddr_t seg_vstart = align_down(phdr.p_vaddr, vm->page_size);
 		vm_vaddr_t seg_vend = phdr.p_vaddr + phdr.p_memsz - 1;
 		seg_vend |= vm->page_size - 1;
 		size_t seg_size = seg_vend - seg_vstart + 1;
tools/testing/selftests/kvm/lib/kvm_util.c +2 -11
index b624c24290dd..63375118d48f 100644
@@ -22,15 +22,6 @@
 
 static int vcpu_mmap_sz(void);
 
-/* Aligns x up to the next multiple of size. Size must be a power of 2. */
-static void *align(void *x, size_t size)
-{
-	size_t mask = size - 1;
-	TEST_ASSERT(size != 0 && !(size & (size - 1)),
-		    "size not a power of 2: %lu", size);
-	return (void *) (((size_t) x + mask) & ~mask);
-}
-
 /*
  * Open KVM_DEV_PATH if available, otherwise exit the entire program.
  *
@@ -911,7 +902,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
 		    region->mmap_start, errno);
 
 	/* Align host address */
-	region->host_mem = align(region->mmap_start, alignment);
+	region->host_mem = align_ptr_up(region->mmap_start, alignment);
 
 	/* As needed perform madvise */
 	if ((src_type == VM_MEM_SRC_ANONYMOUS ||
@@ -954,7 +945,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
 			    "mmap of alias failed, errno: %i", errno);
 
 		/* Align host alias address */
-		region->host_alias = align(region->mmap_alias, alignment);
+		region->host_alias = align_ptr_up(region->mmap_alias, alignment);
 	}
 }
 
tools/testing/selftests/kvm/lib/perf_test_util.c +2 -2
index 0ef80dbdc116..6b8d5020dc54 100644
@@ -92,10 +92,10 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
 	guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
 			      perf_test_args.guest_page_size;
-	guest_test_phys_mem &= ~(perf_test_args.host_page_size - 1);
+	guest_test_phys_mem = align_down(guest_test_phys_mem, perf_test_args.host_page_size);
 #ifdef __s390x__
 	/* Align to 1M (segment size) */
-	guest_test_phys_mem &= ~((1 << 20) - 1);
+	guest_test_phys_mem = align_down(guest_test_phys_mem, 1 << 20);
 #endif
 	pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem);
 
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
Reviewed-by on v2 2/12
#3 lore raw

Re: [PATCH v2 02/12] KVM: selftests: Expose align() helpers to tests

Quoted 9 lines · David Matlack

From: Sean Christopherson <[email protected]>

Refactor align() to work with non-pointers and split into separate helpers for aligning up vs. down. Add align_ptr_up() for use with pointers. Expose all helpers so that they can be used by tests and/or other utilities. The align_down() helper in particular will be used to ensure gpa alignment for hugepages.

Quoted 4 lines

No functional change intended.

[Added sepearate up/down helpers and replaced open-coded alignment

Nit: separate

Quoted 2 lines

bit math throughout the KVM selftests.]

This is wonderful.

Quoted 158 lines
---
 tools/testing/selftests/kvm/dirty_log_test.c  |  6 ++---
 .../testing/selftests/kvm/include/test_util.h | 25 +++++++++++++++++++
 .../selftests/kvm/kvm_page_table_test.c       |  2 +-
 tools/testing/selftests/kvm/lib/elf.c         |  3 +--
 tools/testing/selftests/kvm/lib/kvm_util.c    | 13 ++--------
 .../selftests/kvm/lib/perf_test_util.c        |  4 +--
 6 files changed, 34 insertions(+), 19 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +3 -3
index 792c60e1b17d..3fcd89e195c7 100644
@@ -115,7 +115,7 @@ static void guest_code(void)
                        addr = guest_test_virt_mem;
                        addr += (READ_ONCE(random_array[i]) % guest_num_pages)
                                * guest_page_size;
-                       addr &= ~(host_page_size - 1);
+                       addr = align_down(addr, host_page_size);
                        *(uint64_t *)addr = READ_ONCE(iteration);
                }
 
@@ -737,14 +737,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        if (!p->phys_offset) {
                guest_test_phys_mem = (vm_get_max_gfn(vm) -
                                       guest_num_pages) * guest_page_size;
-               guest_test_phys_mem &= ~(host_page_size - 1);
+               guest_test_phys_mem = align_down(guest_test_phys_mem, host_page_size);
        } else {
                guest_test_phys_mem = p->phys_offset;
        }
 
 #ifdef __s390x__
        /* Align to 1M (segment size) */
-       guest_test_phys_mem &= ~((1 << 20) - 1);
+       guest_test_phys_mem = align_down(guest_test_phys_mem, 1 << 20);
 #endif
 
        pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem);
tools/testing/selftests/kvm/include/test_util.h +25 -0
index f8fddc84c0d3..78c06310cc0e 100644
@@ -117,4 +117,29 @@ static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)
        return vm_mem_backing_src_alias(t)->flag & MAP_SHARED;
 }
 
+/* Aligns x up to the next multiple of size. Size must be a power of 2. */
+static inline uint64_t align_up(uint64_t x, uint64_t size)
+{
+       uint64_t mask = size - 1;
+
+       TEST_ASSERT(size != 0 && !(size & (size - 1)),
+                   "size not a power of 2: %lu", size);
+       return ((x + mask) & ~mask);
+}
+
+static inline uint64_t align_down(uint64_t x, uint64_t size)
+{
+       uint64_t x_aligned_up = align_up(x, size);
+
+       if (x == x_aligned_up)
+               return x;
+       else
+               return x_aligned_up - size;
+}
+
+static inline void *align_ptr_up(void *x, size_t size)
+{
+       return (void *)align_up((unsigned long)x, size);
+}
+
 #endif /* SELFTEST_KVM_TEST_UTIL_H */
tools/testing/selftests/kvm/kvm_page_table_test.c +1 -1
index 36407cb0ec85..3836322add00 100644
@@ -280,7 +280,7 @@ static struct kvm_vm *pre_init_before_test(enum vm_guest_mode mode, void *arg)
 #ifdef __s390x__
        alignment = max(0x100000, alignment);
 #endif
-       guest_test_phys_mem &= ~(alignment - 1);
+       guest_test_phys_mem = align_down(guest_test_virt_mem, alignment);
 
        /* Set up the shared data structure test_args */
        test_args.vm = vm;
tools/testing/selftests/kvm/lib/elf.c +1 -2
index eac44f5d0db0..13e8e3dcf984 100644
@@ -157,8 +157,7 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)
                        "memsize of 0,\n"
                        "  phdr index: %u p_memsz: 0x%" PRIx64,
                        n1, (uint64_t) phdr.p_memsz);
-               vm_vaddr_t seg_vstart = phdr.p_vaddr;
-               seg_vstart &= ~(vm_vaddr_t)(vm->page_size - 1);
+               vm_vaddr_t seg_vstart = align_down(phdr.p_vaddr, vm->page_size);
                vm_vaddr_t seg_vend = phdr.p_vaddr + phdr.p_memsz - 1;
                seg_vend |= vm->page_size - 1;
                size_t seg_size = seg_vend - seg_vstart + 1;
tools/testing/selftests/kvm/lib/kvm_util.c +2 -11
index b624c24290dd..63375118d48f 100644
@@ -22,15 +22,6 @@
 
 static int vcpu_mmap_sz(void);
 
-/* Aligns x up to the next multiple of size. Size must be a power of 2. */
-static void *align(void *x, size_t size)
-{
-       size_t mask = size - 1;
-       TEST_ASSERT(size != 0 && !(size & (size - 1)),
-                   "size not a power of 2: %lu", size);
-       return (void *) (((size_t) x + mask) & ~mask);
-}
-
 /*
  * Open KVM_DEV_PATH if available, otherwise exit the entire program.
  *
@@ -911,7 +902,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
                    region->mmap_start, errno);
 
        /* Align host address */
-       region->host_mem = align(region->mmap_start, alignment);
+       region->host_mem = align_ptr_up(region->mmap_start, alignment);
 
        /* As needed perform madvise */
        if ((src_type == VM_MEM_SRC_ANONYMOUS ||
@@ -954,7 +945,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
                            "mmap of alias failed, errno: %i", errno);
 
                /* Align host alias address */
-               region->host_alias = align(region->mmap_alias, alignment);
+               region->host_alias = align_ptr_up(region->mmap_alias, alignment);
        }
 }
 
tools/testing/selftests/kvm/lib/perf_test_util.c +2 -3
index 0ef80dbdc116..6b8d5020dc54 100644
@@ -92,10 +92,10 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
        guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
                              perf_test_args.guest_page_size;
-       guest_test_phys_mem &= ~(perf_test_args.host_page_size - 1);
+       guest_test_phys_mem = align_down(guest_test_phys_mem, perf_test_args.host_page_size);
 #ifdef __s390x__
        /* Align to 1M (segment size) */
-       guest_test_phys_mem &= ~((1 << 20) - 1);
+       guest_test_phys_mem = align_down(guest_test_phys_mem, 1 << 20);
 #endif
        pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem);
 
--

2.34.0.rc1.387.gb447b232ab-goog

#4 lore raw

PATCH v2 3/12 KVM: selftests: Assert mmap HVA is aligned when using HugeTLB

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Manually padding and aligning the mmap region is only needed when using THP. When using HugeTLB, mmap will always return an address aligned to the HugeTLB page size. Add a comment to clarify this and assert the mmap behavior for HugeTLB.

Cc Ben Gardon <[email protected]>
Cc Yanan Wang <[email protected]>
Cc Andrew Jones <[email protected]>
Cc Peter Xu <[email protected]>
Cc Aaron Lewis <[email protected]>

[Removed requirement that HugeTLB mmaps must be padded per Yanan's
feedback and added assertion that mmap returns aligned addresses when using HugeTLB.]

---
 tools/testing/selftests/kvm/include/test_util.h |  1 +
 tools/testing/selftests/kvm/lib/kvm_util.c      | 11 +++++++++++
 tools/testing/selftests/kvm/lib/test_util.c     |  5 +++++
 3 files changed, 17 insertions(+)
tools/testing/selftests/kvm/include/test_util.h +1 -0
index 78c06310cc0e..99e0dcdc923f 100644
@@ -104,6 +104,7 @@ size_t get_trans_hugepagesz(void);
 size_t get_def_hugetlb_pagesz(void);
 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i);
 size_t get_backing_src_pagesz(uint32_t i);
+bool is_backing_src_hugetlb(uint32_t i);
 void backing_src_help(const char *flag);
 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
 long get_run_delay(void);
tools/testing/selftests/kvm/lib/kvm_util.c +11 -0
index 63375118d48f..07f37456bba0 100644
@@ -866,6 +866,12 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
 	alignment = 1;
 #endif
 
+	/*
+	 * When using THP mmap is not guaranteed to returned a hugepage aligned
+	 * address so we have to pad the mmap. Padding is not needed for HugeTLB
+	 * because mmap will always return an address aligned to the HugeTLB
+	 * page size.
+	 */
 	if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
 		alignment = max(backing_src_pagesz, alignment);
 
@@ -901,6 +907,11 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
 		    "test_malloc failed, mmap_start: %p errno: %i",
 		    region->mmap_start, errno);
 
+	TEST_ASSERT(!is_backing_src_hugetlb(src_type) ||
+		    region->mmap_start == align_ptr_up(region->mmap_start, backing_src_pagesz),
+		    "mmap_start %p is not aligned to HugeTLB page size 0x%lx",
+		    region->mmap_start, backing_src_pagesz);
+
 	/* Align host address */
 	region->host_mem = align_ptr_up(region->mmap_start, alignment);
 
tools/testing/selftests/kvm/lib/test_util.c +5 -0
index b72429108993..6d23878bbfe1 100644
@@ -283,6 +283,11 @@ size_t get_backing_src_pagesz(uint32_t i)
 	}
 }
 
+bool is_backing_src_hugetlb(uint32_t i)
+{
+	return !!(vm_mem_backing_src_alias(i)->flag & MAP_HUGETLB);
+}
+
 static void print_available_backing_src_types(const char *prefix)
 {
 	int i;
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#5 lore raw

PATCH v2 4/12 KVM: selftests: Require GPA to be aligned when backed by hugepages

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Assert that the GPA for a memslot backed by a hugepage is aligned to the hugepage size and fix perf_test_util accordingly. Lack of GPA alignment prevents KVM from backing the guest with hugepages, e.g. x86's write-protection of hugepages when dirty logging is activated is otherwise not exercised.

Add a comment explaining that guest_page_size is for non-huge pages to try and avoid confusion about what it actually tracks.

Cc Ben Gardon <[email protected]>
Cc Yanan Wang <[email protected]>
Cc Andrew Jones <[email protected]>
Cc Peter Xu <[email protected]>
Cc Aaron Lewis <[email protected]>
[Used get_backing_src_pagesz() to determine alignment dynamically.]
---
 tools/testing/selftests/kvm/lib/kvm_util.c       | 2 ++
 tools/testing/selftests/kvm/lib/perf_test_util.c | 7 ++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/kvm_util.c +2 -0
index 07f37456bba0..1f6a01c33dce 100644
@@ -875,6 +875,8 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
 	if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
 		alignment = max(backing_src_pagesz, alignment);
 
+	ASSERT_EQ(guest_paddr, align_up(guest_paddr, backing_src_pagesz));
+
 	/* Add enough memory to align up if necessary */
 	if (alignment > 1)
 		region->mmap_size += alignment;
tools/testing/selftests/kvm/lib/perf_test_util.c +6 -1
index 6b8d5020dc54..a015f267d945 100644
@@ -55,11 +55,16 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 {
 	struct kvm_vm *vm;
 	uint64_t guest_num_pages;
+	uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);
 	int i;
 
 	pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
 
 	perf_test_args.host_page_size = getpagesize();
+	/*
+	 * Snapshot the non-huge page size.  This is used by the guest code to
+	 * access/dirty pages at the logging granularity.
+	 */
 	perf_test_args.guest_page_size = vm_guest_mode_params[mode].page_size;
 
 	guest_num_pages = vm_adjust_num_guest_pages(mode,
@@ -92,7 +97,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
 	guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
 			      perf_test_args.guest_page_size;
-	guest_test_phys_mem = align_down(guest_test_phys_mem, perf_test_args.host_page_size);
+	guest_test_phys_mem = align_down(guest_test_phys_mem, backing_src_pagesz);
 #ifdef __s390x__
 	/* Align to 1M (segment size) */
 	guest_test_phys_mem = align_down(guest_test_phys_mem, 1 << 20);
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#6 lore raw

Re: [PATCH v2 04/12] KVM: selftests: Require GPA to be aligned when backed by hugepages

Quoted 47 lines · David Matlack

From: Sean Christopherson <[email protected]>

Assert that the GPA for a memslot backed by a hugepage is aligned to the hugepage size and fix perf_test_util accordingly. Lack of GPA alignment prevents KVM from backing the guest with hugepages, e.g. x86's write-protection of hugepages when dirty logging is activated is otherwise not exercised.

Add a comment explaining that guest_page_size is for non-huge pages to try and avoid confusion about what it actually tracks.

Cc Ben Gardon <[email protected]>
Cc Yanan Wang <[email protected]>
Cc Andrew Jones <[email protected]>
Cc Peter Xu <[email protected]>
Cc Aaron Lewis <[email protected]>
[Used get_backing_src_pagesz() to determine alignment dynamically.]
---
 tools/testing/selftests/kvm/lib/kvm_util.c       | 2 ++
 tools/testing/selftests/kvm/lib/perf_test_util.c | 7 ++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/kvm_util.c +2 -0
index 07f37456bba0..1f6a01c33dce 100644
@@ -875,6 +875,8 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
        if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
                alignment = max(backing_src_pagesz, alignment);
 
+       ASSERT_EQ(guest_paddr, align_up(guest_paddr, backing_src_pagesz));
+
        /* Add enough memory to align up if necessary */
        if (alignment > 1)
                region->mmap_size += alignment;
tools/testing/selftests/kvm/lib/perf_test_util.c +1 -0
index 6b8d5020dc54..a015f267d945 100644
@@ -55,11 +55,16 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 {
        struct kvm_vm *vm;
        uint64_t guest_num_pages;
+       uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);

tools/testing/selftests/kvm/lib/perf_test_util.c struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus, · line 68

Quoted 10 lines
tools/testing/selftests/kvm/lib/perf_test_util.c +4 -0
        int i;
 
        pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
 
        perf_test_args.host_page_size = getpagesize();
+       /*
+        * Snapshot the non-huge page size.  This is used by the guest code to
+        * access/dirty pages at the logging granularity.
+        */
        perf_test_args.guest_page_size = vm_guest_mode_params[mode].page_size;

Is this comment correct? I wouldn't expect the guest page size to determine the host dirty logging granularity.

Quoted 14 lines
tools/testing/selftests/kvm/lib/perf_test_util.c +1 -2
        guest_num_pages = vm_adjust_num_guest_pages(mode,
@@ -92,7 +97,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
        guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
                              perf_test_args.guest_page_size;
-       guest_test_phys_mem = align_down(guest_test_phys_mem, perf_test_args.host_page_size);
+       guest_test_phys_mem = align_down(guest_test_phys_mem, backing_src_pagesz);
 #ifdef __s390x__
        /* Align to 1M (segment size) */
        guest_test_phys_mem = align_down(guest_test_phys_mem, 1 << 20);
--

2.34.0.rc1.387.gb447b232ab-goog

#7 lore raw

Re: [PATCH v2 04/12] KVM: selftests: Require GPA to be aligned when backed by hugepages

Quoted 52 lines · Ben Gardon
Quoted 51 lines · David Matlack

From: Sean Christopherson <[email protected]>

Assert that the GPA for a memslot backed by a hugepage is aligned to the hugepage size and fix perf_test_util accordingly. Lack of GPA alignment prevents KVM from backing the guest with hugepages, e.g. x86's write-protection of hugepages when dirty logging is activated is otherwise not exercised.

Add a comment explaining that guest_page_size is for non-huge pages to try and avoid confusion about what it actually tracks.

Cc Ben Gardon <[email protected]>
Cc Yanan Wang <[email protected]>
Cc Andrew Jones <[email protected]>
Cc Peter Xu <[email protected]>
Cc Aaron Lewis <[email protected]>
[Used get_backing_src_pagesz() to determine alignment dynamically.]
---
 tools/testing/selftests/kvm/lib/kvm_util.c       | 2 ++
 tools/testing/selftests/kvm/lib/perf_test_util.c | 7 ++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/kvm_util.c +2 -0
index 07f37456bba0..1f6a01c33dce 100644
@@ -875,6 +875,8 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
        if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
                alignment = max(backing_src_pagesz, alignment);
 
+       ASSERT_EQ(guest_paddr, align_up(guest_paddr, backing_src_pagesz));
+
        /* Add enough memory to align up if necessary */
        if (alignment > 1)
                region->mmap_size += alignment;
tools/testing/selftests/kvm/lib/perf_test_util.c +1 -0
index 6b8d5020dc54..a015f267d945 100644
@@ -55,11 +55,16 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 {
        struct kvm_vm *vm;
        uint64_t guest_num_pages;
+       uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);
        int i;
 
        pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
 
Quoted 9 lines

tools/testing/selftests/kvm/lib/perf_test_util.c struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus, · line 68

Quoted 6 lines
tools/testing/selftests/kvm/lib/perf_test_util.c +4 -0
        perf_test_args.host_page_size = getpagesize();
+       /*
+        * Snapshot the non-huge page size.  This is used by the guest code to
+        * access/dirty pages at the logging granularity.
+        */
        perf_test_args.guest_page_size = vm_guest_mode_params[mode].page_size;

Is this comment correct? I wouldn't expect the guest page size to determine the host dirty logging granularity.

"guest page size" is a bit of a misnomer. It's not the page size of the guest's page tables, rather it's the non-huge page size of the PTEs that KVM uses to map guest memory. That info is exposed to the guest so that the guest and host agree on the stride.

Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/kvm/lib/perf_test_util.c +1 -2
        guest_num_pages = vm_adjust_num_guest_pages(mode,
@@ -92,7 +97,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
        guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
                              perf_test_args.guest_page_size;
-       guest_test_phys_mem = align_down(guest_test_phys_mem, perf_test_args.host_page_size);
+       guest_test_phys_mem = align_down(guest_test_phys_mem, backing_src_pagesz);
 #ifdef __s390x__
        /* Align to 1M (segment size) */
        guest_test_phys_mem = align_down(guest_test_phys_mem, 1 << 20);
--

2.34.0.rc1.387.gb447b232ab-goog

#8 lore raw

PATCH v2 5/12 KVM: selftests: Use shorthand local var to access struct perf_tests_args

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Use 'pta' as a local pointer to the global perf_tests_args in order to shorten line lengths and make the code borderline readable.

No functional change intended.

---
 .../selftests/kvm/lib/perf_test_util.c        | 35 ++++++++++---------
 1 file changed, 19 insertions(+), 16 deletions(-)
tools/testing/selftests/kvm/lib/perf_test_util.c +19 -16
index a015f267d945..ccdc950c829e 100644
@@ -24,7 +24,8 @@ static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
  */
 static void guest_code(uint32_t vcpu_id)
 {
-	struct perf_test_vcpu_args *vcpu_args = &perf_test_args.vcpu_args[vcpu_id];
+	struct perf_test_args *pta = &perf_test_args;
+	struct perf_test_vcpu_args *vcpu_args = &pta->vcpu_args[vcpu_id];
 	uint64_t gva;
 	uint64_t pages;
 	int i;
@@ -37,9 +38,9 @@ static void guest_code(uint32_t vcpu_id)
 
 	while (true) {
 		for (i = 0; i < pages; i++) {
-			uint64_t addr = gva + (i * perf_test_args.guest_page_size);
+			uint64_t addr = gva + (i * pta->guest_page_size);
 
-			if (i % perf_test_args.wr_fract == 0)
+			if (i % pta->wr_fract == 0)
 				*(uint64_t *)addr = 0x0123456789ABCDEF;
 			else
 				READ_ONCE(*(uint64_t *)addr);
@@ -53,6 +54,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 				   uint64_t vcpu_memory_bytes, int slots,
 				   enum vm_mem_backing_src_type backing_src)
 {
+	struct perf_test_args *pta = &perf_test_args;
 	struct kvm_vm *vm;
 	uint64_t guest_num_pages;
 	uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);
@@ -60,29 +62,29 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
 	pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
 
-	perf_test_args.host_page_size = getpagesize();
+	pta->host_page_size = getpagesize();
 	/*
 	 * Snapshot the non-huge page size.  This is used by the guest code to
 	 * access/dirty pages at the logging granularity.
 	 */
-	perf_test_args.guest_page_size = vm_guest_mode_params[mode].page_size;
+	pta->guest_page_size = vm_guest_mode_params[mode].page_size;
 
 	guest_num_pages = vm_adjust_num_guest_pages(mode,
-				(vcpus * vcpu_memory_bytes) / perf_test_args.guest_page_size);
+				(vcpus * vcpu_memory_bytes) / pta->guest_page_size);
 
-	TEST_ASSERT(vcpu_memory_bytes % perf_test_args.host_page_size == 0,
+	TEST_ASSERT(vcpu_memory_bytes % pta->host_page_size == 0,
 		    "Guest memory size is not host page size aligned.");
-	TEST_ASSERT(vcpu_memory_bytes % perf_test_args.guest_page_size == 0,
+	TEST_ASSERT(vcpu_memory_bytes % pta->guest_page_size == 0,
 		    "Guest memory size is not guest page size aligned.");
 	TEST_ASSERT(guest_num_pages % slots == 0,
 		    "Guest memory cannot be evenly divided into %d slots.",
 		    slots);
 
 	vm = vm_create_with_vcpus(mode, vcpus, DEFAULT_GUEST_PHY_PAGES,
-				  (vcpus * vcpu_memory_bytes) / perf_test_args.guest_page_size,
+				  (vcpus * vcpu_memory_bytes) / pta->guest_page_size,
 				  0, guest_code, NULL);
 
-	perf_test_args.vm = vm;
+	pta->vm = vm;
 
 	/*
 	 * If there should be more memory in the guest test region than there
@@ -96,7 +98,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 		    vcpu_memory_bytes);
 
 	guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
-			      perf_test_args.guest_page_size;
+			      pta->guest_page_size;
 	guest_test_phys_mem = align_down(guest_test_phys_mem, backing_src_pagesz);
 #ifdef __s390x__
 	/* Align to 1M (segment size) */
@@ -108,7 +110,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 	for (i = 0; i < slots; i++) {
 		uint64_t region_pages = guest_num_pages / slots;
 		vm_paddr_t region_start = guest_test_phys_mem +
-			region_pages * perf_test_args.guest_page_size * i;
+			region_pages * pta->guest_page_size * i;
 
 		vm_userspace_mem_region_add(vm, backing_src, region_start,
 					    PERF_TEST_MEM_SLOT_INDEX + i,
@@ -133,25 +135,26 @@ void perf_test_setup_vcpus(struct kvm_vm *vm, int vcpus,
 			   uint64_t vcpu_memory_bytes,
 			   bool partition_vcpu_memory_access)
 {
+	struct perf_test_args *pta = &perf_test_args;
 	vm_paddr_t vcpu_gpa;
 	struct perf_test_vcpu_args *vcpu_args;
 	int vcpu_id;
 
 	for (vcpu_id = 0; vcpu_id < vcpus; vcpu_id++) {
-		vcpu_args = &perf_test_args.vcpu_args[vcpu_id];
+		vcpu_args = &pta->vcpu_args[vcpu_id];
 
 		vcpu_args->vcpu_id = vcpu_id;
 		if (partition_vcpu_memory_access) {
 			vcpu_args->gva = guest_test_virt_mem +
 					 (vcpu_id * vcpu_memory_bytes);
 			vcpu_args->pages = vcpu_memory_bytes /
-					   perf_test_args.guest_page_size;
+					   pta->guest_page_size;
 			vcpu_gpa = guest_test_phys_mem +
 				   (vcpu_id * vcpu_memory_bytes);
 		} else {
 			vcpu_args->gva = guest_test_virt_mem;
 			vcpu_args->pages = (vcpus * vcpu_memory_bytes) /
-					   perf_test_args.guest_page_size;
+					   pta->guest_page_size;
 			vcpu_gpa = guest_test_phys_mem;
 		}
 
@@ -159,6 +162,6 @@ void perf_test_setup_vcpus(struct kvm_vm *vm, int vcpus,
 
 		pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
 			 vcpu_id, vcpu_gpa, vcpu_gpa +
-			 (vcpu_args->pages * perf_test_args.guest_page_size));
+			 (vcpu_args->pages * pta->guest_page_size));
 	}
 }
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#9 lore raw

PATCH v2 6/12 KVM: selftests: Capture per-vCPU GPA in perf_test_vcpu_args

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Capture the per-vCPU GPA in perf_test_vcpu_args so that tests can get the GPA without having to calculate the GPA on their own.

No functional change intended.

---
 tools/testing/selftests/kvm/include/perf_test_util.h | 1 +
 tools/testing/selftests/kvm/lib/perf_test_util.c     | 9 ++++-----
 2 files changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/perf_test_util.h +1 -0
index df9f1a3a3ffb..20aec72fe7b8 100644
@@ -18,6 +18,7 @@
 #define PERF_TEST_MEM_SLOT_INDEX	1
 
 struct perf_test_vcpu_args {
+	uint64_t gpa;
 	uint64_t gva;
 	uint64_t pages;
 
tools/testing/selftests/kvm/lib/perf_test_util.c +4 -5
index ccdc950c829e..d9c6bcb7964d 100644
@@ -136,7 +136,6 @@ void perf_test_setup_vcpus(struct kvm_vm *vm, int vcpus,
 			   bool partition_vcpu_memory_access)
 {
 	struct perf_test_args *pta = &perf_test_args;
-	vm_paddr_t vcpu_gpa;
 	struct perf_test_vcpu_args *vcpu_args;
 	int vcpu_id;
 
@@ -149,19 +148,19 @@ void perf_test_setup_vcpus(struct kvm_vm *vm, int vcpus,
 					 (vcpu_id * vcpu_memory_bytes);
 			vcpu_args->pages = vcpu_memory_bytes /
 					   pta->guest_page_size;
-			vcpu_gpa = guest_test_phys_mem +
-				   (vcpu_id * vcpu_memory_bytes);
+			vcpu_args->gpa = guest_test_phys_mem +
+					 (vcpu_id * vcpu_memory_bytes);
 		} else {
 			vcpu_args->gva = guest_test_virt_mem;
 			vcpu_args->pages = (vcpus * vcpu_memory_bytes) /
 					   pta->guest_page_size;
-			vcpu_gpa = guest_test_phys_mem;
+			vcpu_args->gpa = guest_test_phys_mem;
 		}
 
 		vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
 
 		pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
-			 vcpu_id, vcpu_gpa, vcpu_gpa +
+			 vcpu_id, vcpu_args->gpa, vcpu_args->gpa +
 			 (vcpu_args->pages * pta->guest_page_size));
 	}
 }
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#10 lore raw

PATCH v2 7/12 KVM: selftests: Use perf util's per-vCPU GPA/pages in demand paging test

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Grab the per-vCPU GPA and number of pages from perf_util in the demand paging test instead of duplicating perf_util's calculations.

Note, this may or may not result in a functional change. It's not clear that the test's calculations are guaranteed to yield the same value as perf_util, e.g. if guest_percpu_mem_size != vcpu_args->pages.

---
 .../selftests/kvm/demand_paging_test.c        | 21 +++++--------------
 1 file changed, 5 insertions(+), 16 deletions(-)
tools/testing/selftests/kvm/demand_paging_test.c +5 -16
index 1510b21e6306..3c729a0a1ab1 100644
@@ -322,26 +322,15 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 		TEST_ASSERT(pipefds, "Unable to allocate memory for pipefd");
 
 		for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
-			vm_paddr_t vcpu_gpa;
+			struct perf_test_vcpu_args *vcpu_args;
 			void *vcpu_hva;
 			void *vcpu_alias;
-			uint64_t vcpu_mem_size;
 
-
-			if (p->partition_vcpu_memory_access) {
-				vcpu_gpa = guest_test_phys_mem +
-					   (vcpu_id * guest_percpu_mem_size);
-				vcpu_mem_size = guest_percpu_mem_size;
-			} else {
-				vcpu_gpa = guest_test_phys_mem;
-				vcpu_mem_size = guest_percpu_mem_size * nr_vcpus;
-			}
-			PER_VCPU_DEBUG("Added VCPU %d with test mem gpa [%lx, %lx)\n",
-				       vcpu_id, vcpu_gpa, vcpu_gpa + vcpu_mem_size);
+			vcpu_args = &perf_test_args.vcpu_args[vcpu_id];
 
 			/* Cache the host addresses of the region */
-			vcpu_hva = addr_gpa2hva(vm, vcpu_gpa);
-			vcpu_alias = addr_gpa2alias(vm, vcpu_gpa);
+			vcpu_hva = addr_gpa2hva(vm, vcpu_args->gpa);
+			vcpu_alias = addr_gpa2alias(vm, vcpu_args->gpa);
 
 			/*
 			 * Set up user fault fd to handle demand paging
@@ -355,7 +344,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 					    pipefds[vcpu_id * 2], p->uffd_mode,
 					    p->uffd_delay, &uffd_args[vcpu_id],
 					    vcpu_hva, vcpu_alias,
-					    vcpu_mem_size);
+					    vcpu_args->pages * perf_test_args.guest_page_size);
 		}
 	}
 
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#11 lore raw

PATCH v2 8/12 KVM: selftests: Move per-VM GPA into perf_test_args

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Move the per-VM GPA into perf_test_args instead of storing it as a separate global variable. It's not obvious that guest_test_phys_mem holds a GPA, nor that it's connected/coupled with per_vcpu->gpa.

No functional change intended.

---
 .../selftests/kvm/include/perf_test_util.h    |  8 +------
 .../selftests/kvm/lib/perf_test_util.c        | 21 +++++++------------
 .../kvm/memslot_modification_stress_test.c    |  2 +-
 3 files changed, 10 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/include/perf_test_util.h +1 -7
index 20aec72fe7b8..d7cde1ab2a85 100644
@@ -29,6 +29,7 @@ struct perf_test_vcpu_args {
 struct perf_test_args {
 	struct kvm_vm *vm;
 	uint64_t host_page_size;
+	uint64_t gpa;
 	uint64_t guest_page_size;
 	int wr_fract;
 
@@ -37,13 +38,6 @@ struct perf_test_args {
 
 extern struct perf_test_args perf_test_args;
 
-/*
- * Guest physical memory offset of the testing memory slot.
- * This will be set to the topmost valid physical address minus
- * the test memory size.
- */
-extern uint64_t guest_test_phys_mem;
-
 struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 				   uint64_t vcpu_memory_bytes, int slots,
 				   enum vm_mem_backing_src_type backing_src);
tools/testing/selftests/kvm/lib/perf_test_util.c +8 -13
index d9c6bcb7964d..0fc2d834c1c7 100644
@@ -10,8 +10,6 @@
 
 struct perf_test_args perf_test_args;
 
-uint64_t guest_test_phys_mem;
-
 /*
  * Guest virtual memory offset of the testing memory slot.
  * Must not conflict with identity mapped test code.
@@ -97,20 +95,18 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 		    guest_num_pages, vm_get_max_gfn(vm), vcpus,
 		    vcpu_memory_bytes);
 
-	guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
-			      pta->guest_page_size;
-	guest_test_phys_mem = align_down(guest_test_phys_mem, backing_src_pagesz);
+	pta->gpa = (vm_get_max_gfn(vm) - guest_num_pages) * pta->guest_page_size;
+	pta->gpa = align_down(pta->gpa, backing_src_pagesz);
 #ifdef __s390x__
 	/* Align to 1M (segment size) */
-	guest_test_phys_mem = align_down(guest_test_phys_mem, 1 << 20);
+	pta->gpa = align_down(pta->gpa, 1 << 20);
 #endif
-	pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem);
+	pr_info("guest physical test memory offset: 0x%lx\n", pta->gpa);
 
 	/* Add extra memory slots for testing */
 	for (i = 0; i < slots; i++) {
 		uint64_t region_pages = guest_num_pages / slots;
-		vm_paddr_t region_start = guest_test_phys_mem +
-			region_pages * pta->guest_page_size * i;
+		vm_paddr_t region_start = pta->gpa + region_pages * pta->guest_page_size * i;
 
 		vm_userspace_mem_region_add(vm, backing_src, region_start,
 					    PERF_TEST_MEM_SLOT_INDEX + i,
@@ -118,7 +114,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 	}
 
 	/* Do mapping for the demand paging memory slot */
-	virt_map(vm, guest_test_virt_mem, guest_test_phys_mem, guest_num_pages);
+	virt_map(vm, guest_test_virt_mem, pta->gpa, guest_num_pages);
 
 	ucall_init(vm, NULL);
 
@@ -148,13 +144,12 @@ void perf_test_setup_vcpus(struct kvm_vm *vm, int vcpus,
 					 (vcpu_id * vcpu_memory_bytes);
 			vcpu_args->pages = vcpu_memory_bytes /
 					   pta->guest_page_size;
-			vcpu_args->gpa = guest_test_phys_mem +
-					 (vcpu_id * vcpu_memory_bytes);
+			vcpu_args->gpa = pta->gpa + (vcpu_id * vcpu_memory_bytes);
 		} else {
 			vcpu_args->gva = guest_test_virt_mem;
 			vcpu_args->pages = (vcpus * vcpu_memory_bytes) /
 					   pta->guest_page_size;
-			vcpu_args->gpa = guest_test_phys_mem;
+			vcpu_args->gpa = pta->gpa;
 		}
 
 		vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
tools/testing/selftests/kvm/memslot_modification_stress_test.c +1 -1
index 4cfcafea9f5a..d105180d5e8c 100644
@@ -80,7 +80,7 @@ static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay,
 	 * Add the dummy memslot just below the perf_test_util memslot, which is
 	 * at the top of the guest physical address space.
 	 */
-	gpa = guest_test_phys_mem - pages * vm_get_page_size(vm);
+	gpa = perf_test_args.gpa - pages * vm_get_page_size(vm);
 
 	for (i = 0; i < nr_modifications; i++) {
 		usleep(delay);
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#12 lore raw

PATCH v2 9/12 KVM: selftests: Remove perf_test_args.host_page_size

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Remove perf_test_args.host_page_size and instead use getpagesize() so that it's somewhat obvious that, for tests that care about the host page size, they care about the system page size, not the hardware page size, e.g. that the logic is unchanged if hugepages are in play.

No functional change intended.

---
 tools/testing/selftests/kvm/include/perf_test_util.h | 1 -
 tools/testing/selftests/kvm/lib/perf_test_util.c     | 3 +--
 2 files changed, 1 insertion(+), 3 deletions(-)
tools/testing/selftests/kvm/include/perf_test_util.h +0 -1
index d7cde1ab2a85..9348580dc5be 100644
@@ -28,7 +28,6 @@ struct perf_test_vcpu_args {
 
 struct perf_test_args {
 	struct kvm_vm *vm;
-	uint64_t host_page_size;
 	uint64_t gpa;
 	uint64_t guest_page_size;
 	int wr_fract;
tools/testing/selftests/kvm/lib/perf_test_util.c +1 -2
index 0fc2d834c1c7..a0aded8cfce3 100644
@@ -60,7 +60,6 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
 	pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
 
-	pta->host_page_size = getpagesize();
 	/*
 	 * Snapshot the non-huge page size.  This is used by the guest code to
 	 * access/dirty pages at the logging granularity.
@@ -70,7 +69,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 	guest_num_pages = vm_adjust_num_guest_pages(mode,
 				(vcpus * vcpu_memory_bytes) / pta->guest_page_size);
 
-	TEST_ASSERT(vcpu_memory_bytes % pta->host_page_size == 0,
+	TEST_ASSERT(vcpu_memory_bytes % getpagesize() == 0,
 		    "Guest memory size is not host page size aligned.");
 	TEST_ASSERT(vcpu_memory_bytes % pta->guest_page_size == 0,
 		    "Guest memory size is not guest page size aligned.");
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#13 lore raw

PATCH v2 10/12 KVM: selftests: Create VM with adjusted number of guest pages for perf tests

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Use the already computed guest_num_pages when creating the so called extra VM pages for a perf test, and add a comment explaining why the pages are allocated as extra pages.

---
 tools/testing/selftests/kvm/lib/perf_test_util.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/perf_test_util.c +6 -2
index a0aded8cfce3..b3154b5b0cfd 100644
@@ -77,9 +77,13 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 		    "Guest memory cannot be evenly divided into %d slots.",
 		    slots);
 
+	/*
+	 * 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(mode, vcpus, DEFAULT_GUEST_PHY_PAGES,
-				  (vcpus * vcpu_memory_bytes) / pta->guest_page_size,
-				  0, guest_code, NULL);
+				  guest_num_pages, 0, guest_code, NULL);
 
 	pta->vm = vm;
 
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#14 lore raw

PATCH v2 11/12 KVM: selftests: Fill per-vCPU struct during "perf_test" VM creation

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Fill the per-vCPU args when creating the perf_test VM instead of having the caller do so. This helps ensure that any adjustments to the number of pages (and thus vcpu_memory_bytes) are reflected in the per-VM args. Automatically filling the per-vCPU args will also allow a future patch to do the sync to the guest during creation.

[Updated access_tracking_perf_test as well.]
---
 .../selftests/kvm/access_tracking_perf_test.c |  5 +-
 .../selftests/kvm/demand_paging_test.c        |  5 +-
 .../selftests/kvm/dirty_log_perf_test.c       |  6 +-
 .../selftests/kvm/include/perf_test_util.h    |  6 +-
 .../selftests/kvm/lib/perf_test_util.c        | 71 ++++++++++---------
 .../kvm/memslot_modification_stress_test.c    |  6 +-
 6 files changed, 45 insertions(+), 54 deletions(-)
tools/testing/selftests/kvm/access_tracking_perf_test.c +1 -4
index 5d95113c7b7c..fdef6c906388 100644
@@ -332,10 +332,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	int vcpus = params->vcpus;
 
 	vm = perf_test_create_vm(mode, vcpus, params->vcpu_memory_bytes, 1,
-				 params->backing_src);
-
-	perf_test_setup_vcpus(vm, vcpus, params->vcpu_memory_bytes,
-			      !overlap_memory_access);
+				 params->backing_src, !overlap_memory_access);
 
 	vcpu_threads = create_vcpu_threads(vcpus);
 
tools/testing/selftests/kvm/demand_paging_test.c +1 -4
index 3c729a0a1ab1..0fee44f5e5ae 100644
@@ -293,7 +293,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	int r;
 
 	vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
-				 p->src_type);
+				 p->src_type, p->partition_vcpu_memory_access);
 
 	perf_test_args.wr_fract = 1;
 
@@ -307,9 +307,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
 	TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 
-	perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
-			      p->partition_vcpu_memory_access);
-
 	if (p->uffd_mode) {
 		uffd_handler_threads =
 			malloc(nr_vcpus * sizeof(*uffd_handler_threads));
tools/testing/selftests/kvm/dirty_log_perf_test.c +2 -4
index 7ffab5bd5ce5..62f9cc2a3146 100644
@@ -186,7 +186,8 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	struct timespec clear_dirty_log_total = (struct timespec){0};
 
 	vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size,
-				 p->slots, p->backing_src);
+				 p->slots, p->backing_src,
+				 p->partition_vcpu_memory_access);
 
 	perf_test_args.wr_fract = p->wr_fract;
 
@@ -206,9 +207,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
 	TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 
-	perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
-			      p->partition_vcpu_memory_access);
-
 	sync_global_to_guest(vm, perf_test_args);
 
 	/* Start the iterations */
tools/testing/selftests/kvm/include/perf_test_util.h +2 -4
index 9348580dc5be..91804be1cf53 100644
@@ -39,10 +39,8 @@ extern struct perf_test_args perf_test_args;
 
 struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 				   uint64_t vcpu_memory_bytes, int slots,
-				   enum vm_mem_backing_src_type backing_src);
+				   enum vm_mem_backing_src_type backing_src,
+				   bool partition_vcpu_memory_access);
 void perf_test_destroy_vm(struct kvm_vm *vm);
-void perf_test_setup_vcpus(struct kvm_vm *vm, int vcpus,
-			   uint64_t vcpu_memory_bytes,
-			   bool partition_vcpu_memory_access);
 
 #endif /* SELFTEST_KVM_PERF_TEST_UTIL_H */
tools/testing/selftests/kvm/lib/perf_test_util.c +37 -34

88 lines not on this page · raw message

tools/testing/selftests/kvm/memslot_modification_stress_test.c +2 -4
index d105180d5e8c..27af0bb8deb7 100644
@@ -105,16 +105,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	int vcpu_id;
 
 	vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
-				 VM_MEM_SRC_ANONYMOUS);
+				 VM_MEM_SRC_ANONYMOUS,
+				 p->partition_vcpu_memory_access);
 
 	perf_test_args.wr_fract = 1;
 
 	vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
 	TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 
-	perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
-			      p->partition_vcpu_memory_access);
-
 	/* Export the shared variables to the guest */
 	sync_global_to_guest(vm, perf_test_args);
 
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
Reviewed-by on v2 11/12
#15 lore raw

Re: [PATCH v2 11/12] KVM: selftests: Fill per-vCPU struct during "perf_test" VM creation

Quoted 9 lines · David Matlack

From: Sean Christopherson <[email protected]>

Fill the per-vCPU args when creating the perf_test VM instead of having the caller do so. This helps ensure that any adjustments to the number of pages (and thus vcpu_memory_bytes) are reflected in the per-VM args. Automatically filling the per-vCPU args will also allow a future patch to do the sync to the guest during creation.

Quoted 3 lines
[Updated access_tracking_perf_test as well.]
Quoted 210 lines
---
 .../selftests/kvm/access_tracking_perf_test.c |  5 +-
 .../selftests/kvm/demand_paging_test.c        |  5 +-
 .../selftests/kvm/dirty_log_perf_test.c       |  6 +-
 .../selftests/kvm/include/perf_test_util.h    |  6 +-
 .../selftests/kvm/lib/perf_test_util.c        | 71 ++++++++++---------
 .../kvm/memslot_modification_stress_test.c    |  6 +-
 6 files changed, 45 insertions(+), 54 deletions(-)
tools/testing/selftests/kvm/access_tracking_perf_test.c +1 -4
index 5d95113c7b7c..fdef6c906388 100644
@@ -332,10 +332,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        int vcpus = params->vcpus;
 
        vm = perf_test_create_vm(mode, vcpus, params->vcpu_memory_bytes, 1,
-                                params->backing_src);
-
-       perf_test_setup_vcpus(vm, vcpus, params->vcpu_memory_bytes,
-                             !overlap_memory_access);
+                                params->backing_src, !overlap_memory_access);
 
        vcpu_threads = create_vcpu_threads(vcpus);
 
tools/testing/selftests/kvm/demand_paging_test.c +1 -4
index 3c729a0a1ab1..0fee44f5e5ae 100644
@@ -293,7 +293,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        int r;
 
        vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
-                                p->src_type);
+                                p->src_type, p->partition_vcpu_memory_access);
 
        perf_test_args.wr_fract = 1;
 
@@ -307,9 +307,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
        TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 
-       perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
-                             p->partition_vcpu_memory_access);
-
        if (p->uffd_mode) {
                uffd_handler_threads =
                        malloc(nr_vcpus * sizeof(*uffd_handler_threads));
tools/testing/selftests/kvm/dirty_log_perf_test.c +2 -4
index 7ffab5bd5ce5..62f9cc2a3146 100644
@@ -186,7 +186,8 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        struct timespec clear_dirty_log_total = (struct timespec){0};
 
        vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size,
-                                p->slots, p->backing_src);
+                                p->slots, p->backing_src,
+                                p->partition_vcpu_memory_access);
 
        perf_test_args.wr_fract = p->wr_fract;
 
@@ -206,9 +207,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
        TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 
-       perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
-                             p->partition_vcpu_memory_access);
-
        sync_global_to_guest(vm, perf_test_args);
 
        /* Start the iterations */
tools/testing/selftests/kvm/include/perf_test_util.h +2 -4
index 9348580dc5be..91804be1cf53 100644
@@ -39,10 +39,8 @@ extern struct perf_test_args perf_test_args;
 
 struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
                                   uint64_t vcpu_memory_bytes, int slots,
-                                  enum vm_mem_backing_src_type backing_src);
+                                  enum vm_mem_backing_src_type backing_src,
+                                  bool partition_vcpu_memory_access);
 void perf_test_destroy_vm(struct kvm_vm *vm);
-void perf_test_setup_vcpus(struct kvm_vm *vm, int vcpus,
-                          uint64_t vcpu_memory_bytes,
-                          bool partition_vcpu_memory_access);
 
 #endif /* SELFTEST_KVM_PERF_TEST_UTIL_H */
tools/testing/selftests/kvm/lib/perf_test_util.c +37 -34
index b3154b5b0cfd..13c8bc22f4e1 100644
@@ -48,9 +48,43 @@ static void guest_code(uint32_t vcpu_id)
        }
 }
 
+void perf_test_setup_vcpus(struct kvm_vm *vm, int vcpus,
+                          uint64_t vcpu_memory_bytes,
+                          bool partition_vcpu_memory_access)
+{
+       struct perf_test_args *pta = &perf_test_args;
+       struct perf_test_vcpu_args *vcpu_args;
+       int vcpu_id;
+
+       for (vcpu_id = 0; vcpu_id < vcpus; vcpu_id++) {
+               vcpu_args = &pta->vcpu_args[vcpu_id];
+
+               vcpu_args->vcpu_id = vcpu_id;
+               if (partition_vcpu_memory_access) {
+                       vcpu_args->gva = guest_test_virt_mem +
+                                        (vcpu_id * vcpu_memory_bytes);
+                       vcpu_args->pages = vcpu_memory_bytes /
+                                          pta->guest_page_size;
+                       vcpu_args->gpa = pta->gpa + (vcpu_id * vcpu_memory_bytes);
+               } else {
+                       vcpu_args->gva = guest_test_virt_mem;
+                       vcpu_args->pages = (vcpus * vcpu_memory_bytes) /
+                                          pta->guest_page_size;
+                       vcpu_args->gpa = pta->gpa;
+               }
+
+               vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
+
+               pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
+                        vcpu_id, vcpu_args->gpa, vcpu_args->gpa +
+                        (vcpu_args->pages * pta->guest_page_size));
+       }
+}
+
 struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
                                   uint64_t vcpu_memory_bytes, int slots,
-                                  enum vm_mem_backing_src_type backing_src)
+                                  enum vm_mem_backing_src_type backing_src,
+                                  bool partition_vcpu_memory_access)
 {
        struct perf_test_args *pta = &perf_test_args;
        struct kvm_vm *vm;
@@ -119,6 +153,8 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
        /* Do mapping for the demand paging memory slot */
        virt_map(vm, guest_test_virt_mem, pta->gpa, guest_num_pages);
 
+       perf_test_setup_vcpus(vm, vcpus, vcpu_memory_bytes, partition_vcpu_memory_access);
+
        ucall_init(vm, NULL);
 
        return vm;
@@ -129,36 +165,3 @@ void perf_test_destroy_vm(struct kvm_vm *vm)
        ucall_uninit(vm);
        kvm_vm_free(vm);
 }
-
-void perf_test_setup_vcpus(struct kvm_vm *vm, int vcpus,
-                          uint64_t vcpu_memory_bytes,
-                          bool partition_vcpu_memory_access)
-{
-       struct perf_test_args *pta = &perf_test_args;
-       struct perf_test_vcpu_args *vcpu_args;
-       int vcpu_id;
-
-       for (vcpu_id = 0; vcpu_id < vcpus; vcpu_id++) {
-               vcpu_args = &pta->vcpu_args[vcpu_id];
-
-               vcpu_args->vcpu_id = vcpu_id;
-               if (partition_vcpu_memory_access) {
-                       vcpu_args->gva = guest_test_virt_mem +
-                                        (vcpu_id * vcpu_memory_bytes);
-                       vcpu_args->pages = vcpu_memory_bytes /
-                                          pta->guest_page_size;
-                       vcpu_args->gpa = pta->gpa + (vcpu_id * vcpu_memory_bytes);
-               } else {
-                       vcpu_args->gva = guest_test_virt_mem;
-                       vcpu_args->pages = (vcpus * vcpu_memory_bytes) /
-                                          pta->guest_page_size;
-                       vcpu_args->gpa = pta->gpa;
-               }
-
-               vcpu_args_set(vm, vcpu_id, 1, vcpu_id);
-
-               pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
-                        vcpu_id, vcpu_args->gpa, vcpu_args->gpa +
-                        (vcpu_args->pages * pta->guest_page_size));
-       }
-}
tools/testing/selftests/kvm/memslot_modification_stress_test.c +2 -5
index d105180d5e8c..27af0bb8deb7 100644
@@ -105,16 +105,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        int vcpu_id;
 
        vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
-                                VM_MEM_SRC_ANONYMOUS);
+                                VM_MEM_SRC_ANONYMOUS,
+                                p->partition_vcpu_memory_access);
 
        perf_test_args.wr_fract = 1;
 
        vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
        TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 
-       perf_test_setup_vcpus(vm, nr_vcpus, guest_percpu_mem_size,
-                             p->partition_vcpu_memory_access);
-
        /* Export the shared variables to the guest */
        sync_global_to_guest(vm, perf_test_args);
 
--

2.34.0.rc1.387.gb447b232ab-goog

#16 lore raw

PATCH v2 12/12 KVM: selftests: Sync perf_test_args to guest during VM creation

In reply to David Matlack

From: Sean Christopherson <[email protected]>

Copy perf_test_args to the guest during VM creation instead of relying on the caller to do so at their leisure. Ideally, tests wouldn't even be able to modify perf_test_args, i.e. they would have no motivation to do the sync, but enforcing that is arguably a net negative for readability.

No functional change intended.

[Set wr_fract=1 by default and add helper to override it since the new
access_tracking_perf_test needs to set it dynamically.]

---
 .../selftests/kvm/access_tracking_perf_test.c        |  3 +--
 tools/testing/selftests/kvm/demand_paging_test.c     |  5 -----
 tools/testing/selftests/kvm/dirty_log_perf_test.c    |  4 +---
 tools/testing/selftests/kvm/include/perf_test_util.h |  2 ++
 tools/testing/selftests/kvm/lib/perf_test_util.c     | 12 ++++++++++++
 .../selftests/kvm/memslot_modification_stress_test.c |  5 -----
 6 files changed, 16 insertions(+), 15 deletions(-)
tools/testing/selftests/kvm/access_tracking_perf_test.c +1 -2
index fdef6c906388..5364a2ed7c68 100644
@@ -277,8 +277,7 @@ static void run_iteration(struct kvm_vm *vm, int vcpus, const char *description)
 static void access_memory(struct kvm_vm *vm, int vcpus, enum access_type access,
 			  const char *description)
 {
-	perf_test_args.wr_fract = (access == ACCESS_READ) ? INT_MAX : 1;
-	sync_global_to_guest(vm, perf_test_args);
+	perf_test_set_wr_fract(vm, (access == ACCESS_READ) ? INT_MAX : 1);
 	iteration_work = ITERATION_ACCESS_MEMORY;
 	run_iteration(vm, vcpus, description);
 }
tools/testing/selftests/kvm/demand_paging_test.c +0 -5
index 0fee44f5e5ae..26f8fd8a57ec 100644
@@ -295,8 +295,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
 				 p->src_type, p->partition_vcpu_memory_access);
 
-	perf_test_args.wr_fract = 1;
-
 	demand_paging_size = get_backing_src_pagesz(p->src_type);
 
 	guest_data_prototype = malloc(demand_paging_size);
@@ -345,9 +343,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 		}
 	}
 
-	/* Export the shared variables to the guest */
-	sync_global_to_guest(vm, perf_test_args);
-
 	pr_info("Finished creating vCPUs and starting uffd threads\n");
 
 	clock_gettime(CLOCK_MONOTONIC, &start);
tools/testing/selftests/kvm/dirty_log_perf_test.c +1 -3
index 62f9cc2a3146..583b4d95aa98 100644
@@ -189,7 +189,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 				 p->slots, p->backing_src,
 				 p->partition_vcpu_memory_access);
 
-	perf_test_args.wr_fract = p->wr_fract;
+	perf_test_set_wr_fract(vm, p->wr_fract);
 
 	guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm);
 	guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages);
@@ -207,8 +207,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
 	TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 
-	sync_global_to_guest(vm, perf_test_args);
-
 	/* Start the iterations */
 	iteration = 0;
 	host_quit = false;
tools/testing/selftests/kvm/include/perf_test_util.h +2 -0
index 91804be1cf53..74e3622b3a6e 100644
@@ -43,4 +43,6 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 				   bool partition_vcpu_memory_access);
 void perf_test_destroy_vm(struct kvm_vm *vm);
 
+void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract);
+
 #endif /* SELFTEST_KVM_PERF_TEST_UTIL_H */
tools/testing/selftests/kvm/lib/perf_test_util.c +12 -0
index 13c8bc22f4e1..77f9eb5667c9 100644
@@ -94,6 +94,9 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
 	pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
 
+	/* By default vCPUs will write to memory. */
+	pta->wr_fract = 1;
+
 	/*
 	 * Snapshot the non-huge page size.  This is used by the guest code to
 	 * access/dirty pages at the logging granularity.
@@ -157,6 +160,9 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
 	ucall_init(vm, NULL);
 
+	/* Export the shared variables to the guest. */
+	sync_global_to_guest(vm, perf_test_args);
+
 	return vm;
 }
 
@@ -165,3 +171,9 @@ void perf_test_destroy_vm(struct kvm_vm *vm)
 	ucall_uninit(vm);
 	kvm_vm_free(vm);
 }
+
+void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract)
+{
+	perf_test_args.wr_fract = wr_fract;
+	sync_global_to_guest(vm, perf_test_args);
+}
tools/testing/selftests/kvm/memslot_modification_stress_test.c +0 -5
index 27af0bb8deb7..df431d0da1ee 100644
@@ -108,14 +108,9 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 				 VM_MEM_SRC_ANONYMOUS,
 				 p->partition_vcpu_memory_access);
 
-	perf_test_args.wr_fract = 1;
-
 	vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
 	TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 
-	/* Export the shared variables to the guest */
-	sync_global_to_guest(vm, perf_test_args);
-
 	pr_info("Finished creating vCPUs\n");
 
 	for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
Reviewed-by on v2 12/12
#17 lore raw

Re: [PATCH v2 12/12] KVM: selftests: Sync perf_test_args to guest during VM creation

Quoted 8 lines · David Matlack

From: Sean Christopherson <[email protected]>

Copy perf_test_args to the guest during VM creation instead of relying on the caller to do so at their leisure. Ideally, tests wouldn't even be able to modify perf_test_args, i.e. they would have no motivation to do the sync, but enforcing that is arguably a net negative for readability.

Quoted 6 lines

No functional change intended.

[Set wr_fract=1 by default and add helper to override it since the new
access_tracking_perf_test needs to set it dynamically.]

Quoted 135 lines
---
 .../selftests/kvm/access_tracking_perf_test.c        |  3 +--
 tools/testing/selftests/kvm/demand_paging_test.c     |  5 -----
 tools/testing/selftests/kvm/dirty_log_perf_test.c    |  4 +---
 tools/testing/selftests/kvm/include/perf_test_util.h |  2 ++
 tools/testing/selftests/kvm/lib/perf_test_util.c     | 12 ++++++++++++
 .../selftests/kvm/memslot_modification_stress_test.c |  5 -----
 6 files changed, 16 insertions(+), 15 deletions(-)
tools/testing/selftests/kvm/access_tracking_perf_test.c +1 -2
index fdef6c906388..5364a2ed7c68 100644
@@ -277,8 +277,7 @@ static void run_iteration(struct kvm_vm *vm, int vcpus, const char *description)
 static void access_memory(struct kvm_vm *vm, int vcpus, enum access_type access,
                          const char *description)
 {
-       perf_test_args.wr_fract = (access == ACCESS_READ) ? INT_MAX : 1;
-       sync_global_to_guest(vm, perf_test_args);
+       perf_test_set_wr_fract(vm, (access == ACCESS_READ) ? INT_MAX : 1);
        iteration_work = ITERATION_ACCESS_MEMORY;
        run_iteration(vm, vcpus, description);
 }
tools/testing/selftests/kvm/demand_paging_test.c +0 -5
index 0fee44f5e5ae..26f8fd8a57ec 100644
@@ -295,8 +295,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
                                 p->src_type, p->partition_vcpu_memory_access);
 
-       perf_test_args.wr_fract = 1;
-
        demand_paging_size = get_backing_src_pagesz(p->src_type);
 
        guest_data_prototype = malloc(demand_paging_size);
@@ -345,9 +343,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                }
        }
 
-       /* Export the shared variables to the guest */
-       sync_global_to_guest(vm, perf_test_args);
-
        pr_info("Finished creating vCPUs and starting uffd threads\n");
 
        clock_gettime(CLOCK_MONOTONIC, &start);
tools/testing/selftests/kvm/dirty_log_perf_test.c +1 -3
index 62f9cc2a3146..583b4d95aa98 100644
@@ -189,7 +189,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                 p->slots, p->backing_src,
                                 p->partition_vcpu_memory_access);
 
-       perf_test_args.wr_fract = p->wr_fract;
+       perf_test_set_wr_fract(vm, p->wr_fract);
 
        guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm);
        guest_num_pages = vm_adjust_num_guest_pages(mode, guest_num_pages);
@@ -207,8 +207,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
        TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 
-       sync_global_to_guest(vm, perf_test_args);
-
        /* Start the iterations */
        iteration = 0;
        host_quit = false;
tools/testing/selftests/kvm/include/perf_test_util.h +2 -0
index 91804be1cf53..74e3622b3a6e 100644
@@ -43,4 +43,6 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
                                   bool partition_vcpu_memory_access);
 void perf_test_destroy_vm(struct kvm_vm *vm);
 
+void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract);
+
 #endif /* SELFTEST_KVM_PERF_TEST_UTIL_H */
tools/testing/selftests/kvm/lib/perf_test_util.c +12 -0
index 13c8bc22f4e1..77f9eb5667c9 100644
@@ -94,6 +94,9 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
        pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
 
+       /* By default vCPUs will write to memory. */
+       pta->wr_fract = 1;
+
        /*
         * Snapshot the non-huge page size.  This is used by the guest code to
         * access/dirty pages at the logging granularity.
@@ -157,6 +160,9 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus,
 
        ucall_init(vm, NULL);
 
+       /* Export the shared variables to the guest. */
+       sync_global_to_guest(vm, perf_test_args);
+
        return vm;
 }
 
@@ -165,3 +171,9 @@ void perf_test_destroy_vm(struct kvm_vm *vm)
        ucall_uninit(vm);
        kvm_vm_free(vm);
 }
+
+void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract)
+{
+       perf_test_args.wr_fract = wr_fract;
+       sync_global_to_guest(vm, perf_test_args);
+}
tools/testing/selftests/kvm/memslot_modification_stress_test.c +0 -6
index 27af0bb8deb7..df431d0da1ee 100644
@@ -108,14 +108,9 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                 VM_MEM_SRC_ANONYMOUS,
                                 p->partition_vcpu_memory_access);
 
-       perf_test_args.wr_fract = 1;
-
        vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads));
        TEST_ASSERT(vcpu_threads, "Memory allocation failed");
 
-       /* Export the shared variables to the guest */
-       sync_global_to_guest(vm, perf_test_args);
-
        pr_info("Finished creating vCPUs\n");
 
        for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++)
--

2.34.0.rc1.387.gb447b232ab-goog

maintainer Applied
#18 lore raw
In reply to David Matlack
Quoted 11 lines · David Matlack

Fix hugepage bugs in the KVM selftests that specifically affect dirty logging and demand paging tests. Found while attempting to verify KVM changes/fixes related to hugepages and dirty logging (patches incoming in a separate series).

Clean up the perf_test_args util on top of the hugepage fixes to clarify what "page size" means, and to improve confidence in the code doing what it thinks it's doing. In a few cases, users of perf_test_args were duplicating (approximating?) calculations made by perf_test_args, and it wasn't obvious that both pieces of code were guaranteed to end up with the same result.

Queued, thanks.

Paolo

Quoted 48 lines

v2:
- Add separate align up/down helpers and use the throughout the series rather than openly coding the bitwise math [Ben, Paolo]
- Do no pad HugeTLB mmaps [Yanan]
- Drop "[PATCH 04/15] KVM: selftests: Force stronger HVA alignment (1gb) for hugepages" since HugeTLB does not require manual HVA alignment [David]
- Drop "[PATCH 15/15] KVM: selftests: Get rid of gorilla math in memslots modification test" since the gorilla math no longer exists [David]
- Drop "[PATCH 14/15] KVM: selftests: Track size of per-VM memslot in perf_test_args" since it was just a prep patch for [PATCH 15/15] [David]
- Update the series to kvm/next [David]

v1: https://lore.kernel.org/kvm/[email protected]/.

Sean Christopherson (12):
KVM: selftests: Explicitly state indicies for vm_guest_mode_params
array
KVM: selftests: Expose align() helpers to tests
KVM: selftests: Assert mmap HVA is aligned when using HugeTLB
KVM: selftests: Require GPA to be aligned when backed by hugepages
KVM: selftests: Use shorthand local var to access struct
perf_tests_args
KVM: selftests: Capture per-vCPU GPA in perf_test_vcpu_args
KVM: selftests: Use perf util's per-vCPU GPA/pages in demand paging
test
KVM: selftests: Move per-VM GPA into perf_test_args
KVM: selftests: Remove perf_test_args.host_page_size
KVM: selftests: Create VM with adjusted number of guest pages for perf
tests
KVM: selftests: Fill per-vCPU struct during "perf_test" VM creation
KVM: selftests: Sync perf_test_args to guest during VM creation

  .../selftests/kvm/access_tracking_perf_test.c |   8 +-
  .../selftests/kvm/demand_paging_test.c        |  31 +----
  .../selftests/kvm/dirty_log_perf_test.c       |  10 +-
  tools/testing/selftests/kvm/dirty_log_test.c  |   6 +-
  .../selftests/kvm/include/perf_test_util.h    |  18 +--
  .../testing/selftests/kvm/include/test_util.h |  26 ++++
  .../selftests/kvm/kvm_page_table_test.c       |   2 +-
  tools/testing/selftests/kvm/lib/elf.c         |   3 +-
  tools/testing/selftests/kvm/lib/kvm_util.c    |  44 +++---
  .../selftests/kvm/lib/perf_test_util.c        | 126 ++++++++++--------
  tools/testing/selftests/kvm/lib/test_util.c   |   5 +
  .../kvm/memslot_modification_stress_test.c    |  13 +-
  12 files changed, 153 insertions(+), 139 deletions(-)