From: Kevin Cheng <chengkev@google.com>
To: seanjc@google.com, pbonzini@redhat.com
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
yosry.ahmed@linux.dev, Kevin Cheng <chengkev@google.com>
Subject: [PATCH 2/3] KVM: selftests: Add TDP unmap helpers
Date: Wed, 21 Jan 2026 00:49:05 +0000 [thread overview]
Message-ID: <20260121004906.2373989-3-chengkev@google.com> (raw)
In-Reply-To: <20260121004906.2373989-1-chengkev@google.com>
Add __virt_pg_unmap(), __tdp_unmap(), and tdp_unmap() as counterparts
to the existing __virt_pg_map(), __tdp_map(), and tdp_map() functions.
These helpers allow tests to selectively unmap pages from the TDP/NPT,
enabling testing of NPT faults for unmapped pages.
Signed-off-by: Kevin Cheng <chengkev@google.com>
---
.../selftests/kvm/include/x86/processor.h | 6 +++
.../testing/selftests/kvm/lib/x86/processor.c | 53 +++++++++++++++++++
2 files changed, 59 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index 6bfffc3b0a332..23ec5030a1d1f 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -1487,6 +1487,12 @@ void tdp_map(struct kvm_vm *vm, uint64_t nested_paddr, uint64_t paddr, uint64_t
void tdp_identity_map_default_memslots(struct kvm_vm *vm);
void tdp_identity_map_1g(struct kvm_vm *vm, uint64_t addr, uint64_t size);
+void __virt_pg_unmap(struct kvm_vm *vm, struct kvm_mmu *mmu, uint64_t vaddr,
+ int level);
+void __tdp_unmap(struct kvm_vm *vm, uint64_t nested_paddr, uint64_t size,
+ int level);
+void tdp_unmap(struct kvm_vm *vm, uint64_t nested_paddr, uint64_t size);
+
/*
* Basic CPU control in CR0
*/
diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
index ab869a98bbdce..8cb0d74aaa41e 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -338,6 +338,40 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
}
}
+void __virt_pg_unmap(struct kvm_vm *vm, struct kvm_mmu *mmu, uint64_t vaddr,
+ int level)
+{
+ uint64_t *pte = &mmu->pgd;
+ int current_level;
+
+ TEST_ASSERT(level >= PG_LEVEL_4K && level <= mmu->pgtable_levels,
+ "Invalid level %d", level);
+
+ /* Walk down to target level */
+ for (current_level = mmu->pgtable_levels;
+ current_level > level;
+ current_level--) {
+ pte = virt_get_pte(vm, mmu, pte, vaddr, current_level);
+
+ TEST_ASSERT(is_present_pte(mmu, pte),
+ "Entry not present at level %d for vaddr 0x%lx",
+ current_level, vaddr);
+ TEST_ASSERT(!is_huge_pte(mmu, pte),
+ "Unexpected huge page at level %d for vaddr 0x%lx",
+ current_level, vaddr);
+ }
+
+ /* Get the PTE at target level */
+ pte = virt_get_pte(vm, mmu, pte, vaddr, level);
+
+ TEST_ASSERT(is_present_pte(mmu, pte),
+ "Entry not present at level %d for vaddr 0x%lx",
+ level, vaddr);
+
+ /* Clear the PTE */
+ *pte = 0;
+}
+
static bool vm_is_target_pte(struct kvm_mmu *mmu, uint64_t *pte,
int *level, int current_level)
{
@@ -541,6 +575,25 @@ void tdp_identity_map_1g(struct kvm_vm *vm, uint64_t addr, uint64_t size)
__tdp_map(vm, addr, addr, size, PG_LEVEL_1G);
}
+void __tdp_unmap(struct kvm_vm *vm, uint64_t nested_paddr, uint64_t size,
+ int level)
+{
+ size_t page_size = PG_LEVEL_SIZE(level);
+ size_t npages = size / page_size;
+
+ TEST_ASSERT(nested_paddr + size > nested_paddr, "Address overflow");
+
+ while (npages--) {
+ __virt_pg_unmap(vm, &vm->stage2_mmu, nested_paddr, level);
+ nested_paddr += page_size;
+ }
+}
+
+void tdp_unmap(struct kvm_vm *vm, uint64_t nested_paddr, uint64_t size)
+{
+ __tdp_unmap(vm, nested_paddr, size, PG_LEVEL_4K);
+}
+
/*
* Set Unusable Segment
*
--
2.52.0.457.g6b5491de43-goog
next prev parent reply other threads:[~2026-01-21 0:49 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-21 0:49 [PATCH 0/3] KVM: SVM: Set PFERR_GUEST_{PAGE,FINAL}_MASK for nested NPF and add selftest Kevin Cheng
2026-01-21 0:49 ` [PATCH 1/3] KVM: SVM: Fix nested NPF injection to set PFERR_GUEST_{PAGE,FINAL}_MASK Kevin Cheng
2026-01-21 22:07 ` Sean Christopherson
2026-01-22 0:45 ` Yosry Ahmed
2026-01-28 15:48 ` Sean Christopherson
2026-02-04 16:22 ` Kevin Cheng
2026-02-06 0:21 ` Sean Christopherson
2026-01-21 0:49 ` Kevin Cheng [this message]
2026-01-21 22:21 ` [PATCH 2/3] KVM: selftests: Add TDP unmap helpers Sean Christopherson
2026-01-21 0:49 ` [PATCH 3/3] KVM: selftests: Add nested NPF injection test for SVM Kevin Cheng
2026-01-23 1:13 ` Sean Christopherson
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=20260121004906.2373989-3-chengkev@google.com \
--to=chengkev@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=yosry.ahmed@linux.dev \
/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
all inboxes | Powered by JetHome®