arch/x86/include/asm/tdx.h | 2 ++ arch/x86/kvm/vmx/tdx.c | 9 +++++++++ arch/x86/virt/vmx/tdx/tdx.c | 21 ++++++++------------- 3 files changed, 19 insertions(+), 13 deletions(-)
From: Kai Huang <kai.huang@intel.com>
All of the x86 KVM guest types (VMX, SEV and TDX) do some special context
tracking when entering guests. This means that the actual guest entry
sequence must be noinstr.
Part of entering a TDX guest is passing a physical address to the TDX
module. Right now, that physical address is stored as a 'struct page'
and converted to a physical address at guest entry. That page=>phys
conversion can be complicated, can vary greatly based on kernel
config, and it is definitely _not_ a noinstr path today.
There have been a number of tinkering approaches to try and fix this
up, but they all fall down due to some part of the page=>phys
conversion infrastructure not being noinstr friendly.
Precalculate the page=>phys conversion and store it in the existing
'tdx_vp' structure. Use the new field at every site that needs a
tdvpr physical address. Remove the now redundant tdx_tdvpr_pa().
Remove the __flatten remnant from the tinkering.
Note that only one user of the new field is actually noinstr. All
others can use page_to_phys(). But, they might as well save the effort
since there is a pre-calculated value sitting there for them.
[ dhansen: rewrite all the text ]
Signed-off-by: Kai Huang <kai.huang@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Tested-by: Farrah Chen <farrah.chen@intel.com>
---
arch/x86/include/asm/tdx.h | 2 ++
arch/x86/kvm/vmx/tdx.c | 9 +++++++++
arch/x86/virt/vmx/tdx/tdx.c | 21 ++++++++-------------
3 files changed, 19 insertions(+), 13 deletions(-)
diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index 6120461bd5ff3..6b338d7f01b7d 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -171,6 +171,8 @@ struct tdx_td {
struct tdx_vp {
/* TDVP root page */
struct page *tdvpr_page;
+ /* precalculated page_to_phys(tdvpr_page) for use in noinstr code */
+ phys_addr_t tdvpr_pa;
/* TD vCPU control structure: */
struct page **tdcx_pages;
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 04b6d332c1afa..75326a7449cc3 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -852,6 +852,7 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu)
if (tdx->vp.tdvpr_page) {
tdx_reclaim_control_page(tdx->vp.tdvpr_page);
tdx->vp.tdvpr_page = 0;
+ tdx->vp.tdvpr_pa = 0;
}
tdx->state = VCPU_TD_STATE_UNINITIALIZED;
@@ -2931,6 +2932,13 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
return -ENOMEM;
tdx->vp.tdvpr_page = page;
+ /*
+ * page_to_phys() does not work in 'noinstr' code, like guest
+ * entry via tdh_vp_enter(). Precalculate and store it instead
+ * of doing it at runtime later.
+ */
+ tdx->vp.tdvpr_pa = page_to_phys(tdx->vp.tdvpr_page);
+
tdx->vp.tdcx_pages = kcalloc(kvm_tdx->td.tdcx_nr_pages, sizeof(*tdx->vp.tdcx_pages),
GFP_KERNEL);
if (!tdx->vp.tdcx_pages) {
@@ -2993,6 +3001,7 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
if (tdx->vp.tdvpr_page)
__free_page(tdx->vp.tdvpr_page);
tdx->vp.tdvpr_page = 0;
+ tdx->vp.tdvpr_pa = 0;
return ret;
}
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index 330b560313afe..eac4032484626 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -1504,11 +1504,6 @@ static inline u64 tdx_tdr_pa(struct tdx_td *td)
return page_to_phys(td->tdr_page);
}
-static inline u64 tdx_tdvpr_pa(struct tdx_vp *td)
-{
- return page_to_phys(td->tdvpr_page);
-}
-
/*
* The TDX module exposes a CLFLUSH_BEFORE_ALLOC bit to specify whether
* a CLFLUSH of pages is required before handing them to the TDX module.
@@ -1520,9 +1515,9 @@ static void tdx_clflush_page(struct page *page)
clflush_cache_range(page_to_virt(page), PAGE_SIZE);
}
-noinstr __flatten u64 tdh_vp_enter(struct tdx_vp *td, struct tdx_module_args *args)
+noinstr u64 tdh_vp_enter(struct tdx_vp *td, struct tdx_module_args *args)
{
- args->rcx = tdx_tdvpr_pa(td);
+ args->rcx = td->tdvpr_pa;
return __seamcall_dirty_cache(__seamcall_saved_ret, TDH_VP_ENTER, args);
}
@@ -1583,7 +1578,7 @@ u64 tdh_vp_addcx(struct tdx_vp *vp, struct page *tdcx_page)
{
struct tdx_module_args args = {
.rcx = page_to_phys(tdcx_page),
- .rdx = tdx_tdvpr_pa(vp),
+ .rdx = vp->tdvpr_pa,
};
tdx_clflush_page(tdcx_page);
@@ -1652,7 +1647,7 @@ EXPORT_SYMBOL_GPL(tdh_mng_create);
u64 tdh_vp_create(struct tdx_td *td, struct tdx_vp *vp)
{
struct tdx_module_args args = {
- .rcx = tdx_tdvpr_pa(vp),
+ .rcx = vp->tdvpr_pa,
.rdx = tdx_tdr_pa(td),
};
@@ -1708,7 +1703,7 @@ EXPORT_SYMBOL_GPL(tdh_mr_finalize);
u64 tdh_vp_flush(struct tdx_vp *vp)
{
struct tdx_module_args args = {
- .rcx = tdx_tdvpr_pa(vp),
+ .rcx = vp->tdvpr_pa,
};
return seamcall(TDH_VP_FLUSH, &args);
@@ -1754,7 +1749,7 @@ EXPORT_SYMBOL_GPL(tdh_mng_init);
u64 tdh_vp_rd(struct tdx_vp *vp, u64 field, u64 *data)
{
struct tdx_module_args args = {
- .rcx = tdx_tdvpr_pa(vp),
+ .rcx = vp->tdvpr_pa,
.rdx = field,
};
u64 ret;
@@ -1771,7 +1766,7 @@ EXPORT_SYMBOL_GPL(tdh_vp_rd);
u64 tdh_vp_wr(struct tdx_vp *vp, u64 field, u64 data, u64 mask)
{
struct tdx_module_args args = {
- .rcx = tdx_tdvpr_pa(vp),
+ .rcx = vp->tdvpr_pa,
.rdx = field,
.r8 = data,
.r9 = mask,
@@ -1784,7 +1779,7 @@ EXPORT_SYMBOL_GPL(tdh_vp_wr);
u64 tdh_vp_init(struct tdx_vp *vp, u64 initial_rcx, u32 x2apicid)
{
struct tdx_module_args args = {
- .rcx = tdx_tdvpr_pa(vp),
+ .rcx = vp->tdvpr_pa,
.rdx = initial_rcx,
.r8 = x2apicid,
};
--
2.34.1
On Wed, Sep 10, 2025 at 07:44:53AM -0700, Dave Hansen wrote: > From: Kai Huang <kai.huang@intel.com> > > All of the x86 KVM guest types (VMX, SEV and TDX) do some special context > tracking when entering guests. This means that the actual guest entry > sequence must be noinstr. > > Part of entering a TDX guest is passing a physical address to the TDX > module. Right now, that physical address is stored as a 'struct page' > and converted to a physical address at guest entry. That page=>phys > conversion can be complicated, can vary greatly based on kernel > config, and it is definitely _not_ a noinstr path today. > > There have been a number of tinkering approaches to try and fix this > up, but they all fall down due to some part of the page=>phys > conversion infrastructure not being noinstr friendly. > > Precalculate the page=>phys conversion and store it in the existing > 'tdx_vp' structure. Use the new field at every site that needs a > tdvpr physical address. Remove the now redundant tdx_tdvpr_pa(). > Remove the __flatten remnant from the tinkering. > > Note that only one user of the new field is actually noinstr. All > others can use page_to_phys(). But, they might as well save the effort > since there is a pre-calculated value sitting there for them. > > [ dhansen: rewrite all the text ] > > Signed-off-by: Kai Huang <kai.huang@intel.com> > Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> > Tested-by: Farrah Chen <farrah.chen@intel.com> Reviewed-by: Kiryl Shutsemau <kas@kernel.org> One nitpick is below. > --- > arch/x86/include/asm/tdx.h | 2 ++ > arch/x86/kvm/vmx/tdx.c | 9 +++++++++ > arch/x86/virt/vmx/tdx/tdx.c | 21 ++++++++------------- > 3 files changed, 19 insertions(+), 13 deletions(-) > > diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h > index 6120461bd5ff3..6b338d7f01b7d 100644 > --- a/arch/x86/include/asm/tdx.h > +++ b/arch/x86/include/asm/tdx.h > @@ -171,6 +171,8 @@ struct tdx_td { > struct tdx_vp { > /* TDVP root page */ > struct page *tdvpr_page; > + /* precalculated page_to_phys(tdvpr_page) for use in noinstr code */ > + phys_addr_t tdvpr_pa; Missing newline above the new field? -- Kiryl Shutsemau / Kirill A. Shutemov
On 9/10/25 09:06, Kiryl Shutsemau wrote: >> struct tdx_vp { >> /* TDVP root page */ >> struct page *tdvpr_page; >> + /* precalculated page_to_phys(tdvpr_page) for use in noinstr code */ >> + phys_addr_t tdvpr_pa; > Missing newline above the new field? I was actually trying to group the two fields together that are aliases for the same logical thing. Is that problematic?
On Wed, Sep 10, 2025 at 09:10:06AM -0700, Dave Hansen wrote: > On 9/10/25 09:06, Kiryl Shutsemau wrote: > >> struct tdx_vp { > >> /* TDVP root page */ > >> struct page *tdvpr_page; > >> + /* precalculated page_to_phys(tdvpr_page) for use in noinstr code */ > >> + phys_addr_t tdvpr_pa; > > Missing newline above the new field? > > I was actually trying to group the two fields together that are aliases > for the same logical thing. > > Is that problematic? No. Just looks odd to me. But I see 'struct tdx_td' also uses similar style. -- Kiryl Shutsemau / Kirill A. Shutemov
On 9/10/25 09:12, Kiryl Shutsemau wrote: > On Wed, Sep 10, 2025 at 09:10:06AM -0700, Dave Hansen wrote: >> On 9/10/25 09:06, Kiryl Shutsemau wrote: >>>> struct tdx_vp { >>>> /* TDVP root page */ >>>> struct page *tdvpr_page; >>>> + /* precalculated page_to_phys(tdvpr_page) for use in noinstr code */ >>>> + phys_addr_t tdvpr_pa; >>> Missing newline above the new field? >> I was actually trying to group the two fields together that are aliases >> for the same logical thing. >> >> Is that problematic? > No. Just looks odd to me. But I see 'struct tdx_td' also uses similar > style. Your review or ack tag there seems to have been mangled by your email client. Could you try to resend it, please? ;)
© 2016 - 2025 Red Hat, Inc.