From: Sean Christopherson <seanjc@google.com>
To: Dave Hansen <dave.hansen@linux.intel.com>
Cc: linux-kernel@vger.kernel.org,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
"Kirill A. Shutemov" <kas@kernel.org>,
Rick Edgecombe <rick.p.edgecombe@intel.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Kai Huang <kai.huang@intel.com>,
Isaku Yamahata <isaku.yamahata@intel.com>,
Vishal Annapurve <vannapurve@google.com>,
Thomas Huth <thuth@redhat.com>,
Adrian Hunter <adrian.hunter@intel.com>,
linux-coco@lists.linux.dev, kvm@vger.kernel.org,
Farrah Chen <farrah.chen@intel.com>
Subject: Re: [PATCH] x86/virt/tdx: Use precalculated TDVPR page physical address
Date: Mon, 20 Oct 2025 06:57:28 -0700 [thread overview]
Message-ID: <aPY_yC45suT8sn8F@google.com> (raw)
In-Reply-To: <20250910144453.1389652-1-dave.hansen@linux.intel.com>
On Wed, Sep 10, 2025, 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>
> ---
> 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;
'0' is a perfectly legal physical address. And using '0' in the existing code to
nullify a pointer is gross.
Why do these structures track struct page everywhere? Nothing actually uses the
struct page object (except calls to __free_page(). The leaf functions all take
a physical address or a virtual address. Track one of those and then use __pa()
or __va() to get at the other.
Side topic, if you're going to bother tracking the number of pages in each struct
despite them being global values, at least reap the benefits of __counted_by().
struct tdx_td {
/* TD root structure: */
void *tdr_page;
int tdcs_nr_pages;
/* TD control structure: */
void *tdcs_pages[] __counted_by(tdcs_nr_pages);
};
struct tdx_vp {
/* TDVP root page */
void *tdvpr_page;
int tdcx_nr_pages;
/* TD vCPU control structure: */
void *tdcx_pages[] __counted_by(tdcx_nr_pages);
};
next prev parent reply other threads:[~2025-10-20 13:57 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-10 14:44 [PATCH] x86/virt/tdx: Use precalculated TDVPR page physical address Dave Hansen
2025-09-10 16:06 ` Kiryl Shutsemau
2025-09-10 16:10 ` Dave Hansen
2025-09-10 16:12 ` Kiryl Shutsemau
2025-09-10 16:57 ` Dave Hansen
2025-09-11 15:41 ` Kiryl Shutsemau
2025-10-20 13:57 ` Sean Christopherson [this message]
2025-10-20 14:14 ` Dave Hansen
2025-10-20 14:42 ` Sean Christopherson
2025-10-20 15:10 ` Dave Hansen
2025-10-20 15:25 ` Sean Christopherson
2025-10-20 15:43 ` Dave Hansen
2025-10-21 17:51 ` Sean Christopherson
2025-10-29 23:51 ` Dave Hansen
2025-10-30 15:42 ` 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=aPY_yC45suT8sn8F@google.com \
--to=seanjc@google.com \
--cc=adrian.hunter@intel.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=farrah.chen@intel.com \
--cc=hpa@zytor.com \
--cc=isaku.yamahata@intel.com \
--cc=kai.huang@intel.com \
--cc=kas@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-coco@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=rick.p.edgecombe@intel.com \
--cc=tglx@linutronix.de \
--cc=thuth@redhat.com \
--cc=vannapurve@google.com \
--cc=x86@kernel.org \
/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).