From: Jim Mattson <jmattson@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
Shuah Khan <shuah@kernel.org>,
Sean Christopherson <seanjc@google.com>,
Bibo Mao <maobibo@loongson.cn>,
Huacai Chen <chenhuacai@kernel.org>,
Andrew Jones <ajones@ventanamicro.com>,
Claudio Imbrenda <imbrenda@linux.ibm.com>,
"Pratik R. Sampat" <prsampat@amd.com>,
Kai Huang <kai.huang@intel.com>,
Eric Auger <eric.auger@redhat.com>,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
linux-kselftest@vger.kernel.org
Cc: Jim Mattson <jmattson@google.com>
Subject: [PATCH 2/4] KVM: selftests: Use a loop to walk guest page tables
Date: Wed, 17 Sep 2025 14:48:38 -0700 [thread overview]
Message-ID: <20250917215031.2567566-3-jmattson@google.com> (raw)
In-Reply-To: <20250917215031.2567566-1-jmattson@google.com>
Walk the guest page tables via a loop when searching for a PTE,
instead of using unique variables for each level of the page tables.
This simplifies the code and makes it easier to support 5-level paging
in the future.
Signed-off-by: Jim Mattson <jmattson@google.com>
---
.../testing/selftests/kvm/lib/x86/processor.c | 21 +++++++------------
1 file changed, 8 insertions(+), 13 deletions(-)
diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
index 0238e674709d..433365c8196d 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -270,7 +270,8 @@ static bool vm_is_target_pte(uint64_t *pte, int *level, int current_level)
uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
int *level)
{
- uint64_t *pml4e, *pdpe, *pde;
+ uint64_t *pte = &vm->pgd;
+ int current_level;
TEST_ASSERT(!vm->arch.is_pt_protected,
"Walking page tables of protected guests is impossible");
@@ -291,19 +292,13 @@ uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
TEST_ASSERT(vaddr == (((int64_t)vaddr << 16) >> 16),
"Canonical check failed. The virtual address is invalid.");
- pml4e = virt_get_pte(vm, &vm->pgd, vaddr, PG_LEVEL_512G);
- if (vm_is_target_pte(pml4e, level, PG_LEVEL_512G))
- return pml4e;
-
- pdpe = virt_get_pte(vm, pml4e, vaddr, PG_LEVEL_1G);
- if (vm_is_target_pte(pdpe, level, PG_LEVEL_1G))
- return pdpe;
-
- pde = virt_get_pte(vm, pdpe, vaddr, PG_LEVEL_2M);
- if (vm_is_target_pte(pde, level, PG_LEVEL_2M))
- return pde;
+ for (current_level = vm->pgtable_levels; current_level > 0; current_level--) {
+ pte = virt_get_pte(vm, pte, vaddr, current_level);
+ if (vm_is_target_pte(pte, level, current_level))
+ return pte;
+ }
- return virt_get_pte(vm, pde, vaddr, PG_LEVEL_4K);
+ return pte;
}
uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr)
--
2.51.0.470.ga7dc726c21-goog
next prev parent reply other threads:[~2025-09-17 21:51 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-17 21:48 [PATCH 0/4] KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1 Jim Mattson
2025-09-17 21:48 ` [PATCH 1/4] KVM: selftests: Use a loop to create guest page tables Jim Mattson
2025-10-20 17:10 ` Yosry Ahmed
2025-09-17 21:48 ` Jim Mattson [this message]
2025-10-20 17:21 ` [PATCH 2/4] KVM: selftests: Use a loop to walk " Yosry Ahmed
2025-10-21 22:11 ` Jim Mattson
2025-10-21 22:24 ` Yosry Ahmed
2025-09-17 21:48 ` [PATCH 3/4] KVM: selftests: Add VM_MODE_PXXV57_4K VM mode Jim Mattson
2025-10-15 21:23 ` Yosry Ahmed
2025-10-21 22:34 ` Jim Mattson
2025-10-21 22:56 ` Yosry Ahmed
2025-10-15 21:25 ` Yosry Ahmed
2025-10-15 22:58 ` Sean Christopherson
2025-10-15 22:59 ` Sean Christopherson
2025-10-16 0:40 ` Sean Christopherson
2025-10-21 22:37 ` Jim Mattson
2025-09-17 21:48 ` [PATCH 4/4] KVM: selftests: Add a VMX test for LA57 nested state Jim Mattson
2025-10-20 17:26 ` Yosry Ahmed
2025-10-21 23:40 ` Jim Mattson
2025-10-22 18:07 ` Yosry Ahmed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250917215031.2567566-3-jmattson@google.com \
--to=jmattson@google.com \
--cc=ajones@ventanamicro.com \
--cc=chenhuacai@kernel.org \
--cc=eric.auger@redhat.com \
--cc=imbrenda@linux.ibm.com \
--cc=kai.huang@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=maobibo@loongson.cn \
--cc=pbonzini@redhat.com \
--cc=prsampat@amd.com \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox