[PATCH 12/17] KVM: x86/mmu: Plumb the old_spte into kvm_x86_ops.set_external_spte()

Rick Edgecombe
2026-03-27T20:14:35Z

From: Sean Christopherson <seanjc@google.com>

Plumb the old SPTE into .set_external_spte() so that the callback can be
used to handle removal and splitting of leaf SPTEs. Rename mirror_spte to
new_spte to follow the TDP MMU's naming, and to make it more obvious what
value the parameter holds.

Opportunistically tweak the ordering of parameters to match the pattern of
most TDP MMU functions, which do "old, new, level".

Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
---
arch/x86/include/asm/kvm_host.h | 4 ++--
arch/x86/kvm/mmu/tdp_mmu.c | 3 ++-
arch/x86/kvm/vmx/tdx.c | 18 +++++++++---------
3 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 1139bd89f0cf..808d2c7ea546 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1877,8 +1877,8 @@ struct kvm_x86_ops {
int root_level);

/* Update the external page table from spte getting set. */
- int (*set_external_spte)(struct kvm *kvm, gfn_t gfn, enum pg_level level,
- u64 mirror_spte);
+ int (*set_external_spte)(struct kvm *kvm, gfn_t gfn, u64 old_spte,
+ u64 new_spte, enum pg_level level);

/* Update external page tables for page table about to be freed. */
int (*free_external_spt)(struct kvm *kvm, gfn_t gfn, enum pg_level level,
diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
index 85c92aec868f..991870789863 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.c
+++ b/arch/x86/kvm/mmu/tdp_mmu.c
@@ -593,7 +593,8 @@ static int __handle_changed_spte(struct kvm *kvm, struct kvm_mmu_page *sp,
(is_leaf || !is_present || WARN_ON_ONCE(pfn_changed))) {
handle_removed_pt(kvm, spte_to_child_pt(old_spte, level), shared);
} else if (is_mirror_sp(sp) && is_present) {
- r = kvm_x86_call(set_external_spte)(kvm, gfn, level, new_spte);
+ r = kvm_x86_call(set_external_spte)(kvm, gfn, old_spte,
+ new_spte, level);
if (r)
return r;
}
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index b44a9c96c89e..569a0576e7c9 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -1690,16 +1690,16 @@ static int tdx_sept_link_private_spt(struct kvm *kvm, gfn_t gfn,
}

static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level level,
- u64 mirror_spte)
+ u64 new_spte)
{
struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
- kvm_pfn_t pfn = spte_to_pfn(mirror_spte);
+ kvm_pfn_t pfn = spte_to_pfn(new_spte);

/* TODO: handle large pages. */
if (KVM_BUG_ON(level != PG_LEVEL_4K, kvm))
return -EIO;

- WARN_ON_ONCE((mirror_spte & VMX_EPT_RWX_MASK) != VMX_EPT_RWX_MASK);
+ WARN_ON_ONCE((new_spte & VMX_EPT_RWX_MASK) != VMX_EPT_RWX_MASK);

/*
* Ensure pre_fault_allowed is read by kvm_arch_vcpu_pre_fault_memory()
@@ -1719,18 +1719,18 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leve
return tdx_mem_page_aug(kvm, gfn, level, pfn);
}

-static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
- enum pg_level level, u64 mirror_spte)
+static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, u64 old_spte,
+ u64 new_spte, enum pg_level level)
{
- if (KVM_BUG_ON(!is_shadow_present_pte(mirror_spte), kvm))
+ if (KVM_BUG_ON(!is_shadow_present_pte(new_spte), kvm))
return -EIO;

lockdep_assert_held(&kvm->mmu_lock);

- if (!is_last_spte(mirror_spte, level))
- return tdx_sept_link_private_spt(kvm, gfn, level, mirror_spte);
+ if (!is_last_spte(new_spte, level))
+ return tdx_sept_link_private_spt(kvm, gfn, level, new_spte);

- return tdx_sept_map_leaf_spte(kvm, gfn, level, mirror_spte);
+ return tdx_sept_map_leaf_spte(kvm, gfn, level, new_spte);
}

/*
--
2.53.0