Re: [RFC PATCH v5 20/45] KVM: x86/mmu: Allocate/free S-EPT pages using tdx_{alloc,free}_control_page()
From: Huang, Kai
Date: Mon Feb 09 2026 - 05:42:03 EST
>
> Option #2 would be to immediately free the page in tdx_sept_reclaim_private_sp(),
> so that pages that freed via handle_removed_pt() don't defer freeing the S-EPT
> page table (which, IIUC, is safe since the TDX-Module forces TLB flushes and exits).
>
> I really, really don't like this option (if it even works).
>
> diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
> index ae7b9beb3249..4726011ad624 100644
> --- a/arch/x86/kvm/vmx/tdx.c
> +++ b/arch/x86/kvm/vmx/tdx.c
> @@ -2014,7 +2014,15 @@ static void tdx_sept_reclaim_private_sp(struct kvm *kvm, gfn_t gfn,
> */
> if (KVM_BUG_ON(is_hkid_assigned(to_kvm_tdx(kvm)), kvm) ||
> tdx_reclaim_page(virt_to_page(sp->external_spt)))
> - sp->external_spt = NULL;
> + goto out;
> +
> + /*
> + * Immediately free the control page, as the TDX subsystem doesn't
> + * support freeing pages from RCU callbacks.
> + */
> + tdx_free_control_page((unsigned long)sp->external_spt);
> +out:
> + sp->external_spt = NULL;
> }
>
> void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,
I don't think this is so bad, given we already have a bunch of
is_mirror_sp(sp)
kvm_x86_call(xx_external_spt)(..);
in TDP MMU code?
I suppose this won't make a lot of difference, but does below make you
slightly happier?
diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
index 3181406c5e0b..3588265098a8 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.c
+++ b/arch/x86/kvm/mmu/tdp_mmu.c
@@ -55,8 +55,7 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)
static void tdp_mmu_free_sp(struct kvm_mmu_page *sp)
{
- if (sp->external_spt)
- kvm_x86_call(free_external_sp)((unsigned long)sp-
>external_spt);
+ WARN_ON_ONCE(sp->external_spt);
free_page((unsigned long)sp->spt);
kmem_cache_free(mmu_page_header_cache, sp);
}
@@ -457,8 +456,17 @@ static void handle_removed_pt(struct kvm *kvm,
tdp_ptep_t pt, bool shared)
old_spte, FROZEN_SPTE, level, shared);
}
- if (is_mirror_sp(sp))
+ if (is_mirror_sp(sp)) {
kvm_x86_call(reclaim_external_sp)(kvm, base_gfn, sp);
+ /*
+ * Immediately free the control page, as the TDX subsystem
doesn't
+ * support freeing pages from RCU callbacks.
+ */
+ if (sp->external_spt) {
+ kvm_x86_call(free_external_sp)((unsigned long)sp-
>external_spt);
+ sp->external_spt = NULL;
+ }
+ }
call_rcu(&sp->rcu_head, tdp_mmu_free_sp_rcu_callback);
}