Re: [RFC PATCH v5 05/45] KVM: TDX: Drop kvm_x86_ops.link_external_spt(), use .set_external_spte() for all
From: Yan Zhao
Date: Wed Feb 04 2026 - 01:44:32 EST
On Tue, Feb 03, 2026 at 08:05:05PM +0000, Sean Christopherson wrote:
> On Tue, Feb 03, 2026, Yan Zhao wrote:
> > On Wed, Jan 28, 2026 at 05:14:37PM -0800, Sean Christopherson wrote:
> > > static int __must_check set_external_spte_present(struct kvm *kvm, tdp_ptep_t sptep,
> > > gfn_t gfn, u64 *old_spte,
> > > u64 new_spte, int level)
> > > {
> > > - bool was_present = is_shadow_present_pte(*old_spte);
> > > - bool is_present = is_shadow_present_pte(new_spte);
> > > - bool is_leaf = is_present && is_last_spte(new_spte, level);
> > > - int ret = 0;
> > > -
> > > - KVM_BUG_ON(was_present, kvm);
> > > + int ret;
> > >
> > > lockdep_assert_held(&kvm->mmu_lock);
> > > +
> > > + if (KVM_BUG_ON(is_shadow_present_pte(*old_spte), kvm))
> > > + return -EIO;
> > Why not move this check of is_shadow_present_pte() to tdx_sept_set_private_spte()
> > as well?
>
> The series gets there eventually, but as of this commit, @old_spte isn't plumbed
> into tdx_sept_set_private_spte().
>
> > Or also check !is_shadow_present_pte(new_spte) in TDP MMU?
>
> Not sure I understand this suggestion.
Sorry. The accurate expression should be
"what about moving !is_shadow_present_pte(new_spte) to TDP MMU as well?".
>
> > > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
> > > index 5688c77616e3..30494f9ceb31 100644
> > > --- a/arch/x86/kvm/vmx/tdx.c
> > > +++ b/arch/x86/kvm/vmx/tdx.c
> > > @@ -1664,18 +1664,58 @@ static int tdx_mem_page_aug(struct kvm *kvm, gfn_t gfn,
> > > return 0;
> > > }
> > >
> > > +static struct page *tdx_spte_to_external_spt(struct kvm *kvm, gfn_t gfn,
> > > + u64 new_spte, enum pg_level level)
> > > +{
> > > + struct kvm_mmu_page *sp = spte_to_child_sp(new_spte);
> > > +
> > > + if (KVM_BUG_ON(!sp->external_spt, kvm) ||
> > > + KVM_BUG_ON(sp->role.level + 1 != level, kvm) ||
> > > + KVM_BUG_ON(sp->gfn != gfn, kvm))
> > > + return NULL;
> > Could we remove the KVM_BUG_ON()s, and ...
> >
> > > + return virt_to_page(sp->external_spt);
> > > +}
> > > +
> > > +static int tdx_sept_link_private_spt(struct kvm *kvm, gfn_t gfn,
> > > + enum pg_level level, u64 mirror_spte)
> > > +{
> > > + gpa_t gpa = gfn_to_gpa(gfn);
> > > + u64 err, entry, level_state;
> > > + struct page *external_spt;
> > > +
> > > + external_spt = tdx_spte_to_external_spt(kvm, gfn, mirror_spte, level);
> > > + if (!external_spt)
> > add a KVM_BUG_ON() here?
> > It could save KVM_BUG_ON()s and have KVM_BUG_ON() match -EIO :)
>
> We could, but I don't want to, because if we're going to bother with sanity checks,
> I want the resulting WARNs to be precise. I.e. I want the WARN to capture *why*
> tdx_spte_to_external_spt() failed, to make debug/triage easier.
Ok.
> > And as Rick also mentioned, better to remove external in external_spt, e.g.
> > something like pt_page.
>
> Yeah, maybe sept_spt?
Hmm, here sept_spt is of type struct page, while sp->spt and sp->external_spt
represents VA. Not sure if it will cause confusion.
But I don't have strong opinion :)
> > And mirror_spte --> new_spte?
>
> Hmm, ya, I made that change later, but it can probably be shifted here.
>
> > > - WARN_ON_ONCE(!is_shadow_present_pte(mirror_spte) ||
> > > - (mirror_spte & VMX_EPT_RWX_MASK) != VMX_EPT_RWX_MASK);
> > > + WARN_ON_ONCE((mirror_spte & VMX_EPT_RWX_MASK) != VMX_EPT_RWX_MASK);
> > Also check this for tdx_sept_link_private_spt()?
>
> Eh, we could, but I don't think it's necessary. make_nonleaf_spte() is hardcoded
> to set full permissions (and I don't see that changing any time soon), whereas
> leaf SPTE protections are much more dynamic.
Makes sense.