Re: [RFC PATCH 07/12] KVM: TDX: Avoid a double-KVM_BUG_ON() in tdx_sept_zap_private_spte()
From: Yan Zhao
Date: Thu Aug 28 2025 - 21:11:44 EST
On Thu, Aug 28, 2025 at 10:50:06PM +0800, Edgecombe, Rick P wrote:
> On Wed, 2025-08-27 at 19:19 -0700, Rick Edgecombe wrote:
> > On Tue, 2025-08-26 at 17:05 -0700, Sean Christopherson wrote:
> > > Return -EIO immediately from tdx_sept_zap_private_spte() if the number of
> > > to-be-added pages underflows, so that the following "KVM_BUG_ON(err, kvm)"
> > > isn't also triggered. Isolating the check from the "is premap error"
> > > if-statement will also allow adding a lockdep assertion that premap errors
> > > are encountered if and only if slots_lock is held.
> > >
> > > Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
> > > ---
> >
> > Reviewed-by: Rick Edgecombe <rick.p.edgecombe@xxxxxxxxx>
>
> There is actually another KVM_BUG_ON() in the path here:
>
> static void remove_external_spte(struct kvm *kvm, gfn_t gfn, u64 old_spte,
> int level)
> {
> kvm_pfn_t old_pfn = spte_to_pfn(old_spte);
> int ret;
>
> /*
> * External (TDX) SPTEs are limited to PG_LEVEL_4K, and external
> * PTs are removed in a special order, involving free_external_spt().
> * But remove_external_spte() will be called on non-leaf PTEs via
> * __tdp_mmu_zap_root(), so avoid the error the former would return
> * in this case.
> */
> if (!is_last_spte(old_spte, level))
> return;
>
> /* Zapping leaf spte is allowed only when write lock is held. */
> lockdep_assert_held_write(&kvm->mmu_lock);
> /* Because write lock is held, operation should success. */
> ret = kvm_x86_call(remove_external_spte)(kvm, gfn, level, old_pfn);
> -> KVM_BUG_ON(ret, kvm);
>
> We don't need to do it in this patch, but we could remove the return value in
> .remove_external_spte, and the KVM_BUG_ON(). Just let remove_external_spte
> handle it internally.
+1. Triggering KVM_BUG_ON() only in TDX internally is better.