mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Mi, Dapeng" <dapeng1.mi@linux.intel.com>
To: Chao Gao <chao.gao@intel.com>,
	linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: seanjc@google.com, pbonzini@redhat.com
Subject: Re: [PATCH 1/2] KVM: x86: Deduplicate MSR interception enabling and disabling
Date: Fri, 13 Jun 2025 09:37:44 +0800	[thread overview]
Message-ID: <0d1e9a86-41aa-46dd-812b-308db5861b16@linux.intel.com> (raw)
In-Reply-To: <20250612081947.94081-2-chao.gao@intel.com>


On 6/12/2025 4:19 PM, Chao Gao wrote:
> Extract a common function from MSR interception disabling logic and create
> disabling and enabling functions based on it. This removes most of the
> duplicated code for MSR interception disabling/enabling.
>
> No functional change intended.
>
> Signed-off-by: Chao Gao <chao.gao@intel.com>
> ---
>  arch/x86/kvm/svm/svm.c | 23 +++++++++--------------
>  arch/x86/kvm/svm/svm.h | 10 +---------
>  arch/x86/kvm/vmx/vmx.c | 25 +++++++++----------------
>  arch/x86/kvm/vmx/vmx.h | 10 +---------
>  4 files changed, 20 insertions(+), 48 deletions(-)
>
> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> index 5453478d1ca3..cc5f81afd8af 100644
> --- a/arch/x86/kvm/svm/svm.c
> +++ b/arch/x86/kvm/svm/svm.c
> @@ -685,21 +685,21 @@ static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
>  	return svm_test_msr_bitmap_write(msrpm, msr);
>  }
>  
> -void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
> +void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool enable)
>  {
>  	struct vcpu_svm *svm = to_svm(vcpu);
>  	void *msrpm = svm->msrpm;
>  
>  	/* Don't disable interception for MSRs userspace wants to handle. */
>  	if (type & MSR_TYPE_R) {
> -		if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
> +		if (!enable && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
>  			svm_clear_msr_bitmap_read(msrpm, msr);
>  		else
>  			svm_set_msr_bitmap_read(msrpm, msr);
>  	}
>  
>  	if (type & MSR_TYPE_W) {
> -		if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
> +		if (!enable && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
>  			svm_clear_msr_bitmap_write(msrpm, msr);
>  		else
>  			svm_set_msr_bitmap_write(msrpm, msr);
> @@ -709,19 +709,14 @@ void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
>  	svm->nested.force_msr_bitmap_recalc = true;
>  }
>  
> -void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
> +void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
>  {
> -	struct vcpu_svm *svm = to_svm(vcpu);
> -	void *msrpm = svm->msrpm;
> -
> -	if (type & MSR_TYPE_R)
> -		svm_set_msr_bitmap_read(msrpm, msr);
> -
> -	if (type & MSR_TYPE_W)
> -		svm_set_msr_bitmap_write(msrpm, msr);
> +	svm_set_intercept_for_msr(vcpu, msr, type, false);
> +}
>  
> -	svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
> -	svm->nested.force_msr_bitmap_recalc = true;
> +void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
> +{
> +	svm_set_intercept_for_msr(vcpu, msr, type, true);
>  }
>  
>  void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask)
> diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
> index 8d3279563261..faa478d9fc62 100644
> --- a/arch/x86/kvm/svm/svm.h
> +++ b/arch/x86/kvm/svm/svm.h
> @@ -696,15 +696,7 @@ void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
>  
>  void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
>  void svm_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
> -
> -static inline void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
> -					     int type, bool enable_intercept)
> -{
> -	if (enable_intercept)
> -		svm_enable_intercept_for_msr(vcpu, msr, type);
> -	else
> -		svm_disable_intercept_for_msr(vcpu, msr, type);
> -}
> +void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool enable);
>  
>  /* nested.c */
>  
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index 277c6b5b5d5f..559261b18512 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -3952,7 +3952,7 @@ static void vmx_msr_bitmap_l01_changed(struct vcpu_vmx *vmx)
>  	vmx->nested.force_msr_bitmap_recalc = true;
>  }
>  
> -void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
> +void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool enable)
>  {
>  	struct vcpu_vmx *vmx = to_vmx(vcpu);
>  	unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
> @@ -3963,35 +3963,28 @@ void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
>  	vmx_msr_bitmap_l01_changed(vmx);
>  
>  	if (type & MSR_TYPE_R) {
> -		if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
> +		if (!enable && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
>  			vmx_clear_msr_bitmap_read(msr_bitmap, msr);
>  		else
>  			vmx_set_msr_bitmap_read(msr_bitmap, msr);
>  	}
>  
>  	if (type & MSR_TYPE_W) {
> -		if (kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
> +		if (!enable && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
>  			vmx_clear_msr_bitmap_write(msr_bitmap, msr);
>  		else
>  			vmx_set_msr_bitmap_write(msr_bitmap, msr);
>  	}
>  }
>  
> -void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
> +void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
>  {
> -	struct vcpu_vmx *vmx = to_vmx(vcpu);
> -	unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
> -
> -	if (!cpu_has_vmx_msr_bitmap())
> -		return;
> -
> -	vmx_msr_bitmap_l01_changed(vmx);
> -
> -	if (type & MSR_TYPE_R)
> -		vmx_set_msr_bitmap_read(msr_bitmap, msr);
> +	vmx_set_intercept_for_msr(vcpu, msr, type, false);
> +}
>  
> -	if (type & MSR_TYPE_W)
> -		vmx_set_msr_bitmap_write(msr_bitmap, msr);
> +void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
> +{
> +	vmx_set_intercept_for_msr(vcpu, msr, type, true);
>  }
>  
>  static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu)
> diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
> index a26fe3d9e1d2..31acd8c726e3 100644
> --- a/arch/x86/kvm/vmx/vmx.h
> +++ b/arch/x86/kvm/vmx/vmx.h
> @@ -388,21 +388,13 @@ void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu);
>  
>  void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
>  void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
> +void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool enable);
>  
>  u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
>  u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
>  
>  gva_t vmx_get_untagged_addr(struct kvm_vcpu *vcpu, gva_t gva, unsigned int flags);
>  
> -static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
> -					     int type, bool value)
> -{
> -	if (value)
> -		vmx_enable_intercept_for_msr(vcpu, msr, type);
> -	else
> -		vmx_disable_intercept_for_msr(vcpu, msr, type);
> -}
> -
>  void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu);
>  
>  /*

The change looks good to me. 

Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com>

Just curious, is there a preference on using these 3 interfaces? When
should we use the disable/enable interfaces? When should be we use the set
interface?  or no preference?



  parent reply	other threads:[~2025-06-13  1:37 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-12  8:19 [PATCH 0/2] More cleanups to MSR interception code Chao Gao
2025-06-12  8:19 ` [PATCH 1/2] KVM: x86: Deduplicate MSR interception enabling and disabling Chao Gao
2025-06-12 11:25   ` Nikolay Borisov
2025-06-12 12:04     ` Chao Gao
2025-06-13  1:37   ` Mi, Dapeng [this message]
2025-06-16  2:20     ` Chao Gao
2025-06-24 22:44       ` Sean Christopherson
2025-06-24 22:47   ` Sean Christopherson
2025-06-25 10:57     ` Chao Gao
2025-06-12  8:19 ` [PATCH 2/2] KVM: SVM: Simplify MSR interception logic for IA32_XSS MSR Chao Gao
2025-06-13  1:39   ` Mi, Dapeng
2025-06-24 22:48   ` Sean Christopherson
2025-06-25  7:20   ` Binbin Wu
2025-06-25 22:25 ` [PATCH 0/2] More cleanups to MSR interception code 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=0d1e9a86-41aa-46dd-812b-308db5861b16@linux.intel.com \
    --to=dapeng1.mi@linux.intel.com \
    --cc=chao.gao@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.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

all inboxes | Powered by JetHome®