mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yan Zhao <yan.y.zhao@intel.com>
To: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: <pbonzini@redhat.com>, <seanjc@google.com>,
	<rick.p.edgecombe@intel.com>, <isaku.yamahata@intel.com>,
	<kai.huang@intel.com>, <tglx@linutronix.de>, <mingo@redhat.com>,
	<bp@alien8.de>, <dave.hansen@linux.intel.com>,
	<kvm@vger.kernel.org>, <x86@kernel.org>,
	<linux-coco@lists.linux.dev>, <linux-kernel@vger.kernel.org>
Subject: Re: [RFC, PATCH 08/12] KVM: x86/tdp_mmu: Add phys_prepare() and phys_cleanup() to kvm_x86_ops
Date: Tue, 6 May 2025 19:55:17 +0800	[thread overview]
Message-ID: <aBn4pfn4aMXcFHd7@yzhao56-desk.sh.intel.com> (raw)
In-Reply-To: <20250502130828.4071412-9-kirill.shutemov@linux.intel.com>

On Fri, May 02, 2025 at 04:08:24PM +0300, Kirill A. Shutemov wrote:
> The functions kvm_x86_ops::link_external_spt() and
> kvm_x86_ops::set_external_spte() are used to assign new memory to a VM.
> When using TDX with Dynamic PAMT enabled, the assigned memory must be
> covered by PAMT.
> 
> The new function kvm_x86_ops::phys_prepare() is called before
> link_external_spt() and set_external_spte() to ensure that the memory is
> ready to be assigned to the virtual machine. In the case of TDX, it
> makes sure that the memory is covered by PAMT.
> 
> kvm_x86_ops::phys_prepare() is called in a context where struct kvm_vcpu
> is available, allowing the implementation to allocate memory from a
> per-VCPU pool.
> 
Why not invoke phys_prepare() and phys_cleanup() in set_external_spte_present()?
Or in tdx_sept_set_private_spte()/tdx_sept_link_private_spt()?

> The function kvm_x86_ops::phys_cleanup() frees PAMT memory in case of
> failure.
> 
> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> ---
>  arch/x86/include/asm/kvm-x86-ops.h |  2 ++
>  arch/x86/include/asm/kvm_host.h    |  3 ++
>  arch/x86/kvm/mmu/tdp_mmu.c         | 47 +++++++++++++++++++++++++++---
>  3 files changed, 48 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h
> index 79406bf07a1c..37081d04e82f 100644
> --- a/arch/x86/include/asm/kvm-x86-ops.h
> +++ b/arch/x86/include/asm/kvm-x86-ops.h
> @@ -99,6 +99,8 @@ KVM_X86_OP_OPTIONAL(link_external_spt)
>  KVM_X86_OP_OPTIONAL(set_external_spte)
>  KVM_X86_OP_OPTIONAL(free_external_spt)
>  KVM_X86_OP_OPTIONAL(remove_external_spte)
> +KVM_X86_OP_OPTIONAL(phys_prepare)
> +KVM_X86_OP_OPTIONAL(phys_cleanup)
>  KVM_X86_OP(has_wbinvd_exit)
>  KVM_X86_OP(get_l2_tsc_offset)
>  KVM_X86_OP(get_l2_tsc_multiplier)
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index 6c06f3d6e081..91958c55f918 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -1813,6 +1813,9 @@ struct kvm_x86_ops {
>  	int (*remove_external_spte)(struct kvm *kvm, gfn_t gfn, enum pg_level level,
>  				    kvm_pfn_t pfn_for_gfn);
>  
> +	int (*phys_prepare)(struct kvm_vcpu *vcpu, kvm_pfn_t pfn);
> +	void (*phys_cleanup)(kvm_pfn_t pfn);
> +
>  	bool (*has_wbinvd_exit)(void);
>  
>  	u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);
> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> index 405874f4d088..f6c836b2e6fc 100644
> --- a/arch/x86/kvm/mmu/tdp_mmu.c
> +++ b/arch/x86/kvm/mmu/tdp_mmu.c
> @@ -1137,6 +1137,26 @@ void kvm_tdp_mmu_invalidate_roots(struct kvm *kvm,
>  	}
>  }
>  
> +static int tdp_mmu_install_spte(struct kvm_vcpu *vcpu,
> +				struct tdp_iter *iter,
> +				u64 spte)
> +{
> +	kvm_pfn_t pfn = 0;
> +	int ret = 0;
> +
> +	if (is_mirror_sptep(iter->sptep) && !is_frozen_spte(spte)) {
> +		pfn = spte_to_pfn(spte);
> +		ret = static_call(kvm_x86_phys_prepare)(vcpu, pfn);
> +	}
> +	if (ret)
> +		return ret;
> +	ret = tdp_mmu_set_spte_atomic(vcpu->kvm, iter, spte);
> +	if (pfn && ret)
> +		static_call(kvm_x86_phys_cleanup)(pfn);
> +
> +	return ret;
> +}
> +
>  /*
>   * Installs a last-level SPTE to handle a TDP page fault.
>   * (NPT/EPT violation/misconfiguration)
> @@ -1170,7 +1190,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,
>  
>  	if (new_spte == iter->old_spte)
>  		ret = RET_PF_SPURIOUS;
> -	else if (tdp_mmu_set_spte_atomic(vcpu->kvm, iter, new_spte))
> +	else if (tdp_mmu_install_spte(vcpu, iter, new_spte))
>  		return RET_PF_RETRY;
>  	else if (is_shadow_present_pte(iter->old_spte) &&
>  		 (!is_last_spte(iter->old_spte, iter->level) ||
> @@ -1211,7 +1231,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,
>   * Returns: 0 if the new page table was installed. Non-0 if the page table
>   *          could not be installed (e.g. the atomic compare-exchange failed).
>   */
> -static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,
> +static int __tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,
>  			   struct kvm_mmu_page *sp, bool shared)
>  {
>  	u64 spte = make_nonleaf_spte(sp->spt, !kvm_ad_enabled);
> @@ -1230,6 +1250,25 @@ static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,
>  	return 0;
>  }
>  
> +static int tdp_mmu_link_sp(struct kvm_vcpu *vcpu, struct tdp_iter *iter,
> +			   struct kvm_mmu_page *sp, bool shared)
> +{
> +	kvm_pfn_t pfn = 0;
> +	int ret = 0;
> +
> +	if (sp->external_spt) {
> +		pfn = __pa(sp->external_spt) >> PAGE_SHIFT;
> +		ret = static_call(kvm_x86_phys_prepare)(vcpu, pfn);
> +		if (ret)
> +			return ret;
> +	}
> +	ret = __tdp_mmu_link_sp(vcpu->kvm, iter, sp, shared);
> +	if (pfn && ret)
> +		static_call(kvm_x86_phys_cleanup)(pfn);
> +
> +	return ret;
> +}
> +
>  static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter,
>  				   struct kvm_mmu_page *sp, bool shared);
>  
> @@ -1288,7 +1327,7 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
>  			KVM_BUG_ON(is_mirror_sptep(iter.sptep), vcpu->kvm);
>  			r = tdp_mmu_split_huge_page(kvm, &iter, sp, true);
>  		} else {
> -			r = tdp_mmu_link_sp(kvm, &iter, sp, true);
> +			r = tdp_mmu_link_sp(vcpu, &iter, sp, true);
>  		}
>  
>  		/*
> @@ -1514,7 +1553,7 @@ static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter,
>  	 * correctness standpoint since the translation will be the same either
>  	 * way.
>  	 */
> -	ret = tdp_mmu_link_sp(kvm, iter, sp, shared);
> +	ret = __tdp_mmu_link_sp(kvm, iter, sp, shared);
>  	if (ret)
>  		goto out;
>  
> -- 
> 2.47.2
> 

  reply	other threads:[~2025-05-06 11:58 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-02 13:08 [RFC, PATCH 00/12] TDX: Enable Dynamic PAMT Kirill A. Shutemov
2025-05-02 13:08 ` [RFC, PATCH 01/12] x86/virt/tdx: Allocate page bitmap for " Kirill A. Shutemov
2025-05-05 10:08   ` Huang, Kai
2025-05-02 13:08 ` [RFC, PATCH 02/12] x86/virt/tdx: Allocate reference counters for PAMT memory Kirill A. Shutemov
2025-05-05 11:05   ` Huang, Kai
2025-05-08 13:03     ` kirill.shutemov
2025-05-09  1:06       ` Huang, Kai
2025-05-12  9:53         ` kirill.shutemov
2025-05-13 23:24           ` Huang, Kai
2025-05-09  9:52   ` Chao Gao
2025-05-12  9:51     ` Kirill A. Shutemov
2025-05-02 13:08 ` [RFC, PATCH 03/12] x86/virt/tdx: Add wrappers for TDH.PHYMEM.PAMT.ADD/REMOVE Kirill A. Shutemov
2025-05-09 10:18   ` Chao Gao
2025-05-12  9:55     ` Kirill A. Shutemov
2025-05-02 13:08 ` [RFC, PATCH 04/12] x86/virt/tdx: Account PAMT memory and print if in /proc/meminfo Kirill A. Shutemov
2025-05-02 13:08 ` [RFC, PATCH 05/12] KVM: TDX: Add tdx_pamt_get()/put() helpers Kirill A. Shutemov
2025-05-05 12:44   ` Huang, Kai
2025-05-07  1:01     ` Yan Zhao
2025-05-07  1:15       ` Vishal Annapurve
2025-05-07  2:42         ` Yan Zhao
2025-05-08 13:19           ` kirill.shutemov
2025-05-07 16:31     ` Dave Hansen
2025-05-08  2:08       ` Yan Zhao
2025-05-08 13:21         ` kirill.shutemov
2025-05-08 13:16       ` kirill.shutemov
2025-05-23  9:42     ` kirill.shutemov
2025-05-14  5:25   ` Chao Gao
2025-05-23 10:46     ` Kirill A. Shutemov
2025-05-14  5:33   ` Chao Gao
2025-05-14  6:25     ` Kirill A. Shutemov
2025-05-02 13:08 ` [RFC, PATCH 06/12] KVM: TDX: Allocate PAMT memory in __tdx_td_init() Kirill A. Shutemov
2025-05-05 12:46   ` Huang, Kai
2025-05-02 13:08 ` [RFC, PATCH 07/12] KVM: TDX: Allocate PAMT memory in tdx_td_vcpu_init() Kirill A. Shutemov
2025-05-02 13:08 ` [RFC, PATCH 08/12] KVM: x86/tdp_mmu: Add phys_prepare() and phys_cleanup() to kvm_x86_ops Kirill A. Shutemov
2025-05-06 11:55   ` Yan Zhao [this message]
2025-05-08 13:23     ` Kirill A. Shutemov
2025-05-09  1:25       ` Yan Zhao
2025-05-12  9:55         ` Kirill A. Shutemov
2025-05-14  0:00           ` Huang, Kai
2025-05-14  6:43             ` kirill.shutemov
2025-05-19  5:00               ` Huang, Kai
2025-05-23 12:00             ` kirill.shutemov
2025-06-05 13:01               ` kirill.shutemov
2025-06-05 22:21                 ` Huang, Kai
2025-06-06 10:20                   ` kirill.shutemov
2025-05-14  6:15   ` Chao Gao
2025-05-02 13:08 ` [RFC, PATCH 09/12] KVM: TDX: Preallocate PAMT pages to be used in page fault path Kirill A. Shutemov
2025-05-14  0:07   ` Huang, Kai
2025-05-14  6:30   ` Chao Gao
2025-05-30 10:28     ` Kirill A. Shutemov
2025-05-02 13:08 ` [RFC, PATCH 10/12] KVM: TDX: Hookup phys_prepare() and phys_cleanup() kvm_x86_ops Kirill A. Shutemov
2025-05-02 13:08 ` [RFC, PATCH 11/12] KVM: TDX: Reclaim PAMT memory Kirill A. Shutemov
2025-05-14  1:11   ` Huang, Kai
2025-05-14 15:21     ` Vishal Annapurve
2025-05-19  5:06       ` Huang, Kai
2025-05-02 13:08 ` [RFC, PATCH 12/12] x86/virt/tdx: Enable Dynamic PAMT Kirill A. Shutemov
2025-05-14 13:41 ` [RFC, PATCH 00/12] TDX: " Sean Christopherson
2025-05-15 14:22   ` Kirill A. Shutemov
2025-05-15 15:03     ` Dave Hansen
2025-05-15 16:02       ` Kirill A. Shutemov
2025-05-14 20:33 ` Zhi Wang
2025-05-15  9:17   ` Kirill A. Shutemov
2025-05-15 14:03     ` Dave Hansen

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=aBn4pfn4aMXcFHd7@yzhao56-desk.sh.intel.com \
    --to=yan.y.zhao@intel.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=isaku.yamahata@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kirill.shutemov@linux.intel.com \
    --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=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --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

all inboxes | Powered by JetHome®