Skip to content

Commit b4b2b6e

Browse files
committed
KVM: TDX: Fold tdx_mem_page_record_premap_cnt() into its sole caller
Fold tdx_mem_page_record_premap_cnt() into tdx_sept_set_private_spte() as providing a one-off helper for effectively three lines of code is at best a wash, and splitting the code makes the comment for smp_rmb() _extremely_ confusing as the comment talks about reading kvm->arch.pre_fault_allowed before kvm_tdx->state, but the immediately visible code does the exact opposite. Opportunistically rewrite the comments to more explicitly explain who is checking what, as well as _why_ the ordering matters. No functional change intended. Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Kai Huang <kai.huang@intel.com> Reviewed-by: Yan Zhao <yan.y.zhao@intel.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Tested-by: Kai Huang <kai.huang@intel.com> Link: https://patch.msgid.link/20251030200951.3402865-16-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent af96d54 commit b4b2b6e

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

arch/x86/kvm/vmx/tdx.c

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,29 +1605,6 @@ static int tdx_mem_page_aug(struct kvm *kvm, gfn_t gfn,
16051605
return 0;
16061606
}
16071607

1608-
/*
1609-
* KVM_TDX_INIT_MEM_REGION calls kvm_gmem_populate() to map guest pages; the
1610-
* callback tdx_gmem_post_populate() then maps pages into private memory.
1611-
* through the a seamcall TDH.MEM.PAGE.ADD(). The SEAMCALL also requires the
1612-
* private EPT structures for the page to have been built before, which is
1613-
* done via kvm_tdp_map_page(). nr_premapped counts the number of pages that
1614-
* were added to the EPT structures but not added with TDH.MEM.PAGE.ADD().
1615-
* The counter has to be zero on KVM_TDX_FINALIZE_VM, to ensure that there
1616-
* are no half-initialized shared EPT pages.
1617-
*/
1618-
static int tdx_mem_page_record_premap_cnt(struct kvm *kvm, gfn_t gfn,
1619-
enum pg_level level, kvm_pfn_t pfn)
1620-
{
1621-
struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
1622-
1623-
if (KVM_BUG_ON(kvm->arch.pre_fault_allowed, kvm))
1624-
return -EIO;
1625-
1626-
/* nr_premapped will be decreased when tdh_mem_page_add() is called. */
1627-
atomic64_inc(&kvm_tdx->nr_premapped);
1628-
return 0;
1629-
}
1630-
16311608
static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
16321609
enum pg_level level, u64 mirror_spte)
16331610
{
@@ -1642,14 +1619,30 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
16421619
(mirror_spte & VMX_EPT_RWX_MASK) != VMX_EPT_RWX_MASK);
16431620

16441621
/*
1645-
* Read 'pre_fault_allowed' before 'kvm_tdx->state'; see matching
1646-
* barrier in tdx_td_finalize().
1622+
* Ensure pre_fault_allowed is read by kvm_arch_vcpu_pre_fault_memory()
1623+
* before kvm_tdx->state. Userspace must not be allowed to pre-fault
1624+
* arbitrary memory until the initial memory image is finalized. Pairs
1625+
* with the smp_wmb() in tdx_td_finalize().
16471626
*/
16481627
smp_rmb();
1649-
if (likely(kvm_tdx->state == TD_STATE_RUNNABLE))
1650-
return tdx_mem_page_aug(kvm, gfn, level, pfn);
16511628

1652-
return tdx_mem_page_record_premap_cnt(kvm, gfn, level, pfn);
1629+
/*
1630+
* If the TD isn't finalized/runnable, then userspace is initializing
1631+
* the VM image via KVM_TDX_INIT_MEM_REGION. Increment the number of
1632+
* pages that need to be mapped and initialized via TDH.MEM.PAGE.ADD.
1633+
* KVM_TDX_FINALIZE_VM checks the counter to ensure all pre-mapped
1634+
* pages have been added to the image, to prevent running the TD with a
1635+
* valid mapping in the mirror EPT, but not in the S-EPT.
1636+
*/
1637+
if (unlikely(kvm_tdx->state != TD_STATE_RUNNABLE)) {
1638+
if (KVM_BUG_ON(kvm->arch.pre_fault_allowed, kvm))
1639+
return -EIO;
1640+
1641+
atomic64_inc(&kvm_tdx->nr_premapped);
1642+
return 0;
1643+
}
1644+
1645+
return tdx_mem_page_aug(kvm, gfn, level, pfn);
16531646
}
16541647

16551648
static int tdx_sept_link_private_spt(struct kvm *kvm, gfn_t gfn,

0 commit comments

Comments
 (0)