Re: [PATCH] KVM: VMX: Explicitly track TDX VMs' root level instead of guessing it from CPUID

From: Edgecombe, Rick P

Date: Wed Aug 19 2026 - 15:00:14 EST


On Wed, 2026-08-19 at 11:45 -0700, Sean Christopherson wrote:
> > Elsewhere we use kvm_has_mirrored_tdp(vcpu->kvm) for these kind of checks.
> > Would
> > be nice to be consistent and not add any uncertainty of whether
> > mirror_root_level can be set without kvm_has_mirrored_tdp() being true.
>
> Hmm, for defense in depth, I want to explicitly check mirror_root_level,
> because returning '0' would likely have dire consequences.  How about this?

:) Sure.

Yan and I were discussing what might be a new level of defense on MMU checking.
We were basically trying to work out your thinking on some of the defensive
patches lately. It seems there has also been a new level of activity on the bugs
front so we want to adapt to any learnings you had. I actually planned to bring
it up in PUCK, but...

Can you share any thoughts? Should we be more paranoid in general, or same as
always? Or more specifically paranoid where issues hit?

>
> if (kvm_has_mirrored_tdp(vcpu->kvm) &&
>     !WARN_ON_ONCE(!vcpu->kvm->arch.mirror_root_level))
> return vcpu->kvm->arch.mirror_root_level;
>
> > > @@ -2760,6 +2754,14 @@ DEFINE_CLASS(tdx_vm_state_guard,
> > > tdx_vm_state_guard_t,
> > >        if (!IS_ERR(_T)) tdx_release_vm_state_locks(_T),
> > >        tdx_acquire_vm_state_locks(kvm), struct kvm *kvm);
> > >  
> > > +static __always_inline void tdx_set_mirror_root_level(struct kvm *kvm,
> > > int
> > > level)
> > > +{
> > > + BUILD_BUG_ON(level != 4 && level != 5);
> > > +
> > > + kvm->arch.mirror_root_level = level;
> > > + kvm->arch.gfn_direct_bits = gpa_to_gfn(BIT_ULL(level == 4 ? 47 :
> > > 51));
> >
> > No need to remove TDX_SHARED_BIT_PWL_4/5 in this patch either anymore. Since
> > this lives in TDX code.
>
> Killing them off dedups the code, and more importantly makes it all but
> impossible for mirror_root_level and the mirror root level to get out of sync.

Eh, I can see it. I weigh it against "magic numbers" though.