From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Michael Roth <michael.roth@amd.com>,
Yan Zhao <yan.y.zhao@intel.com>, Ira Weiny <ira.weiny@intel.com>,
Vishal Annapurve <vannapurve@google.com>,
Rick Edgecombe <rick.p.edgecombe@intel.com>
Subject: [RFC PATCH 09/12] KVM: TDX: Fold tdx_mem_page_record_premap_cnt() into its sole caller
Date: Tue, 26 Aug 2025 17:05:19 -0700 [thread overview]
Message-ID: <20250827000522.4022426-10-seanjc@google.com> (raw)
In-Reply-To: <20250827000522.4022426-1-seanjc@google.com>
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.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/vmx/tdx.c | 49 ++++++++++++++++++------------------------
1 file changed, 21 insertions(+), 28 deletions(-)
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index b7559ea1e353..e4b70c0dbda3 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -1608,29 +1608,6 @@ static int tdx_mem_page_aug(struct kvm *kvm, gfn_t gfn,
return 0;
}
-/*
- * KVM_TDX_INIT_MEM_REGION calls kvm_gmem_populate() to map guest pages; the
- * callback tdx_gmem_post_populate() then maps pages into private memory.
- * through the a seamcall TDH.MEM.PAGE.ADD(). The SEAMCALL also requires the
- * private EPT structures for the page to have been built before, which is
- * done via kvm_tdp_map_page(). nr_premapped counts the number of pages that
- * were added to the EPT structures but not added with TDH.MEM.PAGE.ADD().
- * The counter has to be zero on KVM_TDX_FINALIZE_VM, to ensure that there
- * are no half-initialized shared EPT pages.
- */
-static int tdx_mem_page_record_premap_cnt(struct kvm *kvm, gfn_t gfn,
- enum pg_level level, kvm_pfn_t pfn)
-{
- struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
-
- if (KVM_BUG_ON(kvm->arch.pre_fault_allowed, kvm))
- return -EIO;
-
- /* nr_premapped will be decreased when tdh_mem_page_add() is called. */
- atomic64_inc(&kvm_tdx->nr_premapped);
- return 0;
-}
-
static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
enum pg_level level, kvm_pfn_t pfn)
{
@@ -1641,14 +1618,30 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
return -EIO;
/*
- * Read 'pre_fault_allowed' before 'kvm_tdx->state'; see matching
- * barrier in tdx_td_finalize().
+ * Ensure pre_fault_allowed is read by kvm_arch_vcpu_pre_fault_memory()
+ * before kvm_tdx->state. Userspace must not be allowed to pre-fault
+ * arbitrary memory until the initial memory image is finalized. Pairs
+ * with the smp_wmb() in tdx_td_finalize().
*/
smp_rmb();
- if (likely(kvm_tdx->state == TD_STATE_RUNNABLE))
- return tdx_mem_page_aug(kvm, gfn, level, pfn);
- return tdx_mem_page_record_premap_cnt(kvm, gfn, level, pfn);
+ /*
+ * If the TD isn't finalized/runnable, then userspace is initializing
+ * the VM image via KVM_TDX_INIT_MEM_REGION. Increment the number of
+ * pages that need to be initialized via TDH.MEM.PAGE.ADD (PAGE.ADD
+ * requires a pre-existing S-EPT mapping). KVM_TDX_FINALIZE_VM checks
+ * the counter to ensure all mapped pages have been added to the image,
+ * to prevent running the TD with uninitialized memory.
+ */
+ if (unlikely(kvm_tdx->state != TD_STATE_RUNNABLE)) {
+ if (KVM_BUG_ON(kvm->arch.pre_fault_allowed, kvm))
+ return -EIO;
+
+ atomic64_inc(&kvm_tdx->nr_premapped);
+ return 0;
+ }
+
+ return tdx_mem_page_aug(kvm, gfn, level, pfn);
}
static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn,
--
2.51.0.268.g9569e192d0-goog
next prev parent reply other threads:[~2025-08-27 0:05 UTC|newest]
Thread overview: 85+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-27 0:05 [RFC PATCH 00/12] KVM: x86/mmu: TDX post-populate cleanups Sean Christopherson
2025-08-27 0:05 ` [RFC PATCH 01/12] KVM: TDX: Drop PROVE_MMU=y sanity check on to-be-populated mappings Sean Christopherson
2025-08-27 8:14 ` Yan Zhao
2025-08-28 0:37 ` Ira Weiny
2025-08-28 2:13 ` Huang, Kai
2025-08-27 0:05 ` [RFC PATCH 02/12] KVM: x86/mmu: Add dedicated API to map guest_memfd pfn into TDP MMU Sean Christopherson
2025-08-27 8:25 ` Yan Zhao
2025-08-28 0:54 ` Edgecombe, Rick P
2025-08-28 1:26 ` Edgecombe, Rick P
2025-08-28 6:23 ` Yan Zhao
2025-08-28 19:40 ` Sean Christopherson
2025-08-29 1:16 ` Yan Zhao
2025-09-01 0:39 ` Yan Zhao
2025-08-28 6:55 ` Yan Zhao
2025-08-28 0:40 ` Ira Weiny
2025-08-28 1:51 ` Edgecombe, Rick P
2025-08-28 19:57 ` Sean Christopherson
2025-08-27 0:05 ` [RFC PATCH 03/12] Revert "KVM: x86/tdp_mmu: Add a helper function to walk down the TDP MMU" Sean Christopherson
2025-08-27 0:05 ` [RFC PATCH 04/12] KVM: x86/mmu: Rename kvm_tdp_map_page() to kvm_tdp_prefault_page() Sean Christopherson
2025-08-28 2:01 ` Edgecombe, Rick P
2025-08-28 18:50 ` Sean Christopherson
2025-08-28 19:04 ` Edgecombe, Rick P
2025-08-27 0:05 ` [RFC PATCH 05/12] KVM: TDX: Drop superfluous page pinning in S-EPT management Sean Christopherson
2025-08-27 8:33 ` Yan Zhao
2025-08-28 2:05 ` Edgecombe, Rick P
2025-08-28 20:16 ` Sean Christopherson
2025-08-28 0:36 ` Ira Weiny
2025-08-28 7:08 ` Yan Zhao
2025-08-28 15:54 ` Ira Weiny
2025-08-28 2:45 ` Huang, Kai
2025-08-27 0:05 ` [RFC PATCH 06/12] KVM: TDX: Return -EIO, not -EINVAL, on a KVM_BUG_ON() condition Sean Christopherson
2025-08-27 8:39 ` Yan Zhao
2025-08-27 17:26 ` Sean Christopherson
2025-08-28 2:11 ` Edgecombe, Rick P
2025-08-28 19:21 ` Sean Christopherson
2025-08-28 20:13 ` Edgecombe, Rick P
2025-08-28 21:00 ` Sean Christopherson
2025-08-28 21:19 ` Edgecombe, Rick P
2025-08-28 21:34 ` Sean Christopherson
2025-08-28 15:03 ` Ira Weiny
2025-08-27 0:05 ` [RFC PATCH 07/12] KVM: TDX: Avoid a double-KVM_BUG_ON() in tdx_sept_zap_private_spte() Sean Christopherson
2025-08-28 2:19 ` Edgecombe, Rick P
2025-08-28 14:50 ` Edgecombe, Rick P
2025-08-29 1:10 ` Yan Zhao
2025-08-28 15:02 ` Ira Weiny
2025-08-27 0:05 ` [RFC PATCH 08/12] KVM: TDX: Use atomic64_dec_return() instead of a poor equivalent Sean Christopherson
2025-08-28 2:56 ` Edgecombe, Rick P
2025-08-28 6:48 ` Yan Zhao
2025-08-28 19:14 ` Edgecombe, Rick P
2025-08-28 22:33 ` Sean Christopherson
2025-08-28 23:18 ` Edgecombe, Rick P
2025-08-28 15:03 ` Ira Weiny
2025-08-27 0:05 ` Sean Christopherson [this message]
2025-08-27 9:02 ` [RFC PATCH 09/12] KVM: TDX: Fold tdx_mem_page_record_premap_cnt() into its sole caller Yan Zhao
2025-08-27 19:08 ` Sean Christopherson
2025-08-28 3:13 ` Edgecombe, Rick P
2025-08-28 5:56 ` Yan Zhao
2025-08-28 19:08 ` Edgecombe, Rick P
2025-08-28 5:43 ` Yan Zhao
2025-08-28 17:00 ` Sean Christopherson
2025-08-28 18:52 ` Edgecombe, Rick P
2025-08-28 20:26 ` Sean Christopherson
2025-08-28 21:33 ` Edgecombe, Rick P
2025-08-28 21:57 ` Sean Christopherson
2025-08-28 23:17 ` Edgecombe, Rick P
2025-08-29 6:08 ` Yan Zhao
2025-08-28 22:06 ` Ira Weiny
2025-08-28 23:17 ` Sean Christopherson
2025-08-29 0:35 ` Ira Weiny
2025-08-29 6:06 ` Yan Zhao
2025-08-28 21:44 ` Sean Christopherson
2025-08-29 2:42 ` Binbin Wu
2025-08-29 2:31 ` Yan Zhao
2025-08-29 6:33 ` Yan Zhao
2025-08-28 15:30 ` Ira Weiny
2025-08-28 15:28 ` Ira Weiny
2025-08-27 0:05 ` [RFC PATCH 10/12] KVM: TDX: Assert that slots_lock is held when nr_premapped is accessed Sean Christopherson
2025-08-27 0:05 ` [RFC PATCH 11/12] KVM: TDX: Track nr_premapped as an "unsigned long", not an "atomic64_t" Sean Christopherson
2025-08-27 9:12 ` Yan Zhao
2025-08-27 0:05 ` [RFC PATCH 12/12] KVM: TDX: Rename nr_premapped to nr_pending_tdh_mem_page_adds Sean Christopherson
2025-08-27 9:22 ` Yan Zhao
2025-08-28 15:23 ` Ira Weiny
2025-08-27 9:48 ` [RFC PATCH 00/12] KVM: x86/mmu: TDX post-populate cleanups Yan Zhao
2025-08-28 19:01 ` Edgecombe, Rick P
2025-08-28 23:19 ` Sean Christopherson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250827000522.4022426-10-seanjc@google.com \
--to=seanjc@google.com \
--cc=ira.weiny@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=michael.roth@amd.com \
--cc=pbonzini@redhat.com \
--cc=rick.p.edgecombe@intel.com \
--cc=vannapurve@google.com \
--cc=yan.y.zhao@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).