From: Sean Christopherson <seanjc@google.com>
To: Yosry Ahmed <yosry.ahmed@linux.dev>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 10/16] KVM: selftests: Reuse virt mapping functions for nested EPTs
Date: Tue, 23 Dec 2025 15:12:09 -0800 [thread overview]
Message-ID: <aUshyQad7LjdhYAY@google.com> (raw)
In-Reply-To: <20251127013440.3324671-11-yosry.ahmed@linux.dev>
On Thu, Nov 27, 2025, Yosry Ahmed wrote:
> __tdp_pg_map() bears a lot of resemblence to __virt_pg_map(). The
> main differences are:
> - It uses the EPT struct overlay instead of the PTE masks.
> - It always assumes 4-level EPTs.
>
> To reuse __virt_pg_map(), initialize the PTE masks in nested MMU with
> EPT PTE masks. EPTs have no 'present' or 'user' bits, so use the
> 'readable' bit instead like shadow_{present/user}_mask, ignoring the
> fact that entries can be present and not readable if the CPU has
> VMX_EPT_EXECUTE_ONLY_BIT. This is simple and sufficient for testing.
Ugh, no. I am strongly against playing the same insane games KVM itself plays
with overloading protectin/access bits. There's no reason for selftests to do
the same, e.g. selftests aren't shadowing guest PTEs and doing permission checks
in hot paths and so don't need to multiplex a bunch of things into an inscrutable
(but performant!) system.
> Add an executable bitmask and update __virt_pg_map() and friends to set
> the bit on newly created entries to match the EPT behavior. It's a nop
> for x86 page tables.
>
> Another benefit of reusing the code is having separate handling for
> upper-level PTEs vs 4K PTEs, which avoids some quirks like setting the
> large bit on a 4K PTE in the EPTs.
>
> No functional change intended.
>
> Suggested-by: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev>
> ---
> .../selftests/kvm/include/x86/processor.h | 3 +
> .../testing/selftests/kvm/lib/x86/processor.c | 12 +-
> tools/testing/selftests/kvm/lib/x86/vmx.c | 115 ++++--------------
> 3 files changed, 33 insertions(+), 97 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
> index fb2b2e53d453..62e10b296719 100644
> --- a/tools/testing/selftests/kvm/include/x86/processor.h
> +++ b/tools/testing/selftests/kvm/include/x86/processor.h
> @@ -1447,6 +1447,7 @@ struct pte_masks {
> uint64_t dirty;
> uint64_t huge;
> uint64_t nx;
> + uint64_t x;
To be consistent with e.g. writable, call this executable.
> uint64_t c;
> uint64_t s;
> };
> @@ -1464,6 +1465,7 @@ struct kvm_mmu {
> #define PTE_DIRTY_MASK(mmu) ((mmu)->pte_masks.dirty)
> #define PTE_HUGE_MASK(mmu) ((mmu)->pte_masks.huge)
> #define PTE_NX_MASK(mmu) ((mmu)->pte_masks.nx)
> +#define PTE_X_MASK(mmu) ((mmu)->pte_masks.x)
> #define PTE_C_MASK(mmu) ((mmu)->pte_masks.c)
> #define PTE_S_MASK(mmu) ((mmu)->pte_masks.s)
>
> @@ -1474,6 +1476,7 @@ struct kvm_mmu {
> #define pte_dirty(mmu, pte) (!!(*(pte) & PTE_DIRTY_MASK(mmu)))
> #define pte_huge(mmu, pte) (!!(*(pte) & PTE_HUGE_MASK(mmu)))
> #define pte_nx(mmu, pte) (!!(*(pte) & PTE_NX_MASK(mmu)))
> +#define pte_x(mmu, pte) (!!(*(pte) & PTE_X_MASK(mmu)))
And then here to not assume PRESENT == READABLE, just check if the MMU even has
a PRESENT bit. We may still need changes, e.g. the page table builders actually
need to verify a PTE is _writable_, not just present, but that's largely an
orthogonal issue.
#define is_present_pte(mmu, pte) \
(PTE_PRESENT_MASK(mmu) ? \
!!(*(pte) & PTE_PRESENT_MASK(mmu)) : \
!!(*(pte) & (PTE_READABLE_MASK(mmu) | PTE_EXECUTABLE_MASK(mmu))))
And to properly capture the relationship between NX and EXECUTABLE:
#define is_executable_pte(mmu, pte) \
((*(pte) & (PTE_EXECUTABLE_MASK(mmu) | PTE_NX_MASK(mmu))) == PTE_EXECUTABLE_MASK(mmu))
#define is_nx_pte(mmu, pte) (!is_executable_pte(mmu, pte))
> #define pte_c(mmu, pte) (!!(*(pte) & PTE_C_MASK(mmu)))
> #define pte_s(mmu, pte) (!!(*(pte) & PTE_S_MASK(mmu)))
>
> diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
> index bff75ff05364..8b0e17f8ca37 100644
> --- a/tools/testing/selftests/kvm/lib/x86/processor.c
> +++ b/tools/testing/selftests/kvm/lib/x86/processor.c
> @@ -162,8 +162,7 @@ struct kvm_mmu *mmu_create(struct kvm_vm *vm, int pgtable_levels,
> struct kvm_mmu *mmu = calloc(1, sizeof(*mmu));
>
> TEST_ASSERT(mmu, "-ENOMEM when allocating MMU");
> - if (pte_masks)
> - mmu->pte_masks = *pte_masks;
> + mmu->pte_masks = *pte_masks;
Rather than pass NULL (and allow NULL here) in the previous patch, pass an
empty pte_masks. That avoids churning the MMU initialization code, and allows
for a better TODO in the previous patch.
> + /*
> + * EPTs do not have 'present' or 'user' bits, instead bit 0 is the
> + * 'readable' bit. In some cases, EPTs can be execute-only and an entry
> + * is present but not readable. However, for the purposes of testing we
> + * assume 'present' == 'user' == 'readable' for simplicity.
> + */
> + pte_masks = (struct pte_masks){
> + .present = BIT_ULL(0),
> + .user = BIT_ULL(0),
> + .writable = BIT_ULL(1),
> + .x = BIT_ULL(2),
> + .accessed = BIT_ULL(5),
> + .dirty = BIT_ULL(6),
> + .huge = BIT_ULL(7),
> + .nx = 0,
> + };
> +
> /* EPTP_PWL_4 is always used */
Make this a TODO, e.g.
/* TODO: Add support for 5-level paging. */
so that it's clear this is a shortcoming, not some fundamental property of
selftests.
next prev parent reply other threads:[~2025-12-23 23:12 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-27 1:34 [PATCH v3 00/16] Add Nested NPT support in selftests Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 01/16] KVM: selftests: Make __vm_get_page_table_entry() static Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 02/16] KVM: selftests: Stop passing a memslot to nested_map_memslot() Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 03/16] KVM: selftests: Rename nested TDP mapping functions Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 04/16] KVM: selftests: Kill eptPageTablePointer Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 05/16] KVM: selftests: Stop setting AD bits on nested EPTs on creation Yosry Ahmed
2025-12-23 22:26 ` Sean Christopherson
2025-12-23 23:35 ` Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 06/16] KVM: selftests: Introduce struct kvm_mmu Yosry Ahmed
2025-12-23 22:29 ` Sean Christopherson
2025-12-23 23:38 ` Yosry Ahmed
2025-12-29 15:24 ` Sean Christopherson
2025-11-27 1:34 ` [PATCH v3 07/16] KVM: selftests: Move PTE bitmasks to kvm_mmu Yosry Ahmed
2025-12-23 22:31 ` Sean Christopherson
2025-12-23 23:40 ` Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 08/16] KVM: selftests: Use a nested MMU to share nested EPTs between vCPUs Yosry Ahmed
2025-12-23 23:16 ` Sean Christopherson
2025-11-27 1:34 ` [PATCH v3 09/16] KVM: selftests: Stop passing VMX metadata to TDP mapping functions Yosry Ahmed
2025-12-15 18:38 ` Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 10/16] KVM: selftests: Reuse virt mapping functions for nested EPTs Yosry Ahmed
2025-12-23 23:12 ` Sean Christopherson [this message]
2025-12-23 23:45 ` Yosry Ahmed
2025-12-30 0:08 ` Sean Christopherson
2025-12-30 4:03 ` Yosry Ahmed
2025-12-30 15:43 ` Sean Christopherson
2025-12-23 23:14 ` Sean Christopherson
2025-12-23 23:47 ` Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 11/16] KVM: selftests: Move TDP mapping functions outside of vmx.c Yosry Ahmed
2025-12-23 23:13 ` Sean Christopherson
2025-11-27 1:34 ` [PATCH v3 12/16] KVM: selftests: Allow kvm_cpu_has_ept() to be called on AMD CPUs Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 13/16] KVM: selftests: Add support for nested NPTs Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 14/16] KVM: selftests: Set the user bit on nested NPT PTEs Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 15/16] KVM: selftests: Extend vmx_dirty_log_test to cover SVM Yosry Ahmed
2025-11-27 1:34 ` [PATCH v3 16/16] KVM: selftests: Extend memstress to run on nested SVM Yosry Ahmed
2025-12-23 22:01 ` [PATCH v3 00/16] Add Nested NPT support in selftests Sean Christopherson
2025-12-23 23:48 ` 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=aUshyQad7LjdhYAY@google.com \
--to=seanjc@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.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