Re: [RFC PATCH v2 09/23] KVM: x86/tdp_mmu: Add split_external_spt hook called during write mmu_lock
From: Yan Zhao
Date: Mon Nov 17 2025 - 04:18:19 EST
On Mon, Nov 17, 2025 at 04:53:17PM +0800, Binbin Wu wrote:
>
>
> On 8/7/2025 5:43 PM, Yan Zhao wrote:
> [...]
> > /**
> > * handle_removed_pt() - handle a page table removed from the TDP structure
> > *
> > @@ -765,12 +778,20 @@ static u64 tdp_mmu_set_spte(struct kvm *kvm, int as_id, tdp_ptep_t sptep,
> > handle_changed_spte(kvm, as_id, gfn, old_spte, new_spte, level, false);
> > /*
> > - * Users that do non-atomic setting of PTEs don't operate on mirror
> > - * roots, so don't handle it and bug the VM if it's seen.
> > + * Propagate changes of SPTE to the external page table under write
> > + * mmu_lock.
> > + * Current valid transitions:
> > + * - present leaf to !present.
> > + * - present non-leaf to !present.
>
> Nit:
> Maybe add a small note to limit the scenario, such as "after releasing the HKID"
> or "during the TD teardown"?
I'm not sure if we need that level of detail.
e.g., for case "present leaf to !present", it's before releasing the HKID w/o
patch [1], but can be after the HKID w/ patch [1].
[1] https://lore.kernel.org/all/20250729193341.621487-6-seanjc@xxxxxxxxxx/
> > + * - present leaf to present non-leaf (splitting)
> > */
> > if (is_mirror_sptep(sptep)) {
> > - KVM_BUG_ON(is_shadow_present_pte(new_spte), kvm);
> > - remove_external_spte(kvm, gfn, old_spte, level);
> > + if (!is_shadow_present_pte(new_spte))
> > + remove_external_spte(kvm, gfn, old_spte, level);
> > + else if (is_last_spte(old_spte, level) && !is_last_spte(new_spte, level))
> > + split_external_spt(kvm, gfn, old_spte, new_spte, level);
> > + else
> > + KVM_BUG_ON(1, kvm);
> > }
> > return old_spte;
>