Subject: Re: [PATCH v2 1/4] KVM: selftests: Move pgd_created check into virt_pgd_alloc

From: Sean Christopherson <[email-masked]>
Date: Wed, 28 Jun 2023 11:30:52 -0700

On Sat, Apr 08, 2023, Nicholas Piggin wrote:
> virt_arch_pgd_alloc all do the same test and set of pgd_created. Move
> this into common code.

Can you give virt_arch_dump() and addr_gva2gpa() the same treatment?  E.g. either
in one patch or in separate patches:

---
 tools/testing/selftests/kvm/include/kvm_util_base.h | 4 +++-
 tools/testing/selftests/kvm/lib/aarch64/processor.c | 6 ------
 tools/testing/selftests/kvm/lib/powerpc/processor.c | 3 ---
 tools/testing/selftests/kvm/lib/riscv/processor.c   | 6 ------
 tools/testing/selftests/kvm/lib/s390x/processor.c   | 3 ---
 tools/testing/selftests/kvm/lib/x86_64/processor.c  | 3 ---
 6 files changed, 3 insertions(+), 22 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index e52405c9fa8b..2545d65483f3 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -908,6 +908,7 @@ vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva);
 
 static inline vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)
 {
+	TEST_ASSERT(vm->pgd_created, "No guest mappings have been estalished");
 	return addr_arch_gva2gpa(vm, gva);
 }
 
@@ -930,7 +931,8 @@ void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent);
 
 static inline void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
 {
-	virt_arch_dump(stream, vm, indent);
+	if (vm->pgd_created)
+		virt_arch_dump(stream, vm, indent);
 }

diff --git a/tools/testing/selftests/kvm/lib/aarch64/processor.c b/tools/testing/selftests/kvm/lib/aarch64/processor.c
index 3da3ec7c5b23..932fa9d350af 100644
--- a/tools/testing/selftests/kvm/lib/aarch64/processor.c
+++ b/tools/testing/selftests/kvm/lib/aarch64/processor.c
@@ -157,9 +157,6 @@ uint64_t *virt_get_pte_hva(struct kvm_vm *vm, vm_vaddr_t gva)
 {
 	uint64_t *ptep;
 
-	if (!vm->pgd_created)
-		goto unmapped_gva;
-
 	ptep = addr_gpa2hva(vm, vm->pgd) + pgd_index(vm, gva) * 8;
 	if (!ptep)
 		goto unmapped_gva;
@@ -222,9 +219,6 @@ void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
 	int level = 4 - (vm->pgtable_levels - 1);
 	uint64_t pgd, *ptep;
 
-	if (!vm->pgd_created)
-		return;
-
 	for (pgd = vm->pgd; pgd < vm->pgd + ptrs_per_pgd(vm) * 8; pgd += 8) {
 		ptep = addr_gpa2hva(vm, pgd);
 		if (!*ptep)
diff --git a/tools/testing/selftests/kvm/lib/powerpc/processor.c b/tools/testing/selftests/kvm/lib/powerpc/processor.c
index 57d64d281467..589c5eb15abf 100644
--- a/tools/testing/selftests/kvm/lib/powerpc/processor.c
+++ b/tools/testing/selftests/kvm/lib/powerpc/processor.c
@@ -246,9 +246,6 @@ void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
 {
 	vm_paddr_t pt = vm->pgd;
 
-	if (!vm->pgd_created)
-		return;
-
 	virt_dump_pt(stream, vm, pt, 0, 1, indent);
 }
 
diff --git a/tools/testing/selftests/kvm/lib/riscv/processor.c b/tools/testing/selftests/kvm/lib/riscv/processor.c
index 7695ba2cd369..251581364643 100644
--- a/tools/testing/selftests/kvm/lib/riscv/processor.c
+++ b/tools/testing/selftests/kvm/lib/riscv/processor.c
@@ -111,9 +111,6 @@ vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)
 	uint64_t *ptep;
 	int level = vm->pgtable_levels - 1;
 
-	if (!vm->pgd_created)
-		goto unmapped_gva;
-
 	ptep = addr_gpa2hva(vm, vm->pgd) + pte_index(vm, gva, level) * 8;
 	if (!ptep)
 		goto unmapped_gva;
@@ -162,9 +159,6 @@ void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
 	int level = vm->pgtable_levels - 1;
 	uint64_t pgd, *ptep;
 
-	if (!vm->pgd_created)
-		return;
-
 	for (pgd = vm->pgd; pgd < vm->pgd + ptrs_per_pte(vm) * 8; pgd += 8) {
 		ptep = addr_gpa2hva(vm, pgd);
 		if (!*ptep)
diff --git a/tools/testing/selftests/kvm/lib/s390x/processor.c b/tools/testing/selftests/kvm/lib/s390x/processor.c
index 358e03f09c7a..39f4b87b9b15 100644
--- a/tools/testing/selftests/kvm/lib/s390x/processor.c
+++ b/tools/testing/selftests/kvm/lib/s390x/processor.c
@@ -145,9 +145,6 @@ static void virt_dump_region(FILE *stream, struct kvm_vm *vm, uint8_t indent,
 
 void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
 {
-	if (!vm->pgd_created)
-		return;
-
 	virt_dump_region(stream, vm, indent, vm->pgd);
 }
 
diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index d4deb2718e86..32ed0eba3fbd 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -307,9 +307,6 @@ void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
 	uint64_t *pde, *pde_start;
 	uint64_t *pte, *pte_start;
 
-	if (!vm->pgd_created)
-		return;
-
 	fprintf(stream, "%*s                                          "
 		"                no\n", indent, "");
 	fprintf(stream, "%*s      index hvaddr         gpaddr         "

base-commit: 84e95f5585f25b5199e23c8547867616337103bd
--