public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] KVM: x86: Add a helper to dedup loading guest/host XCR0 and XSS
@ 2025-11-10  5:05 Binbin Wu
  2025-11-10 12:54 ` Xiaoyao Li
  2025-11-21 18:55 ` Sean Christopherson
  0 siblings, 2 replies; 3+ messages in thread
From: Binbin Wu @ 2025-11-10  5:05 UTC (permalink / raw)
  To: seanjc, pbonzini; +Cc: kvm, linux-kernel, chao.gao, xiaoyao.li, binbin.wu

Add and use a helper, kvm_load_xfeatures(), to dedup the code that loads
guest/host xfeatures.

Opportunistically return early if X86_CR4_OSXSAVE is not set to reduce
indentations.

No functional change intended.

Suggested-by: Chao Gao <chao.gao@intel.com>
Reviewed-by: Chao Gao <chao.gao@intel.com>
Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com>
---
v2:
- Pass a bool to distinguish guest/host. [Chao, Xiaoyao]
- Fix a typo in the short log. [Chao]
- Opportunistically return early if X86_CR4_OSXSAVE is not set to reduce
  indentations.

v1:
- https://lore.kernel.org/kvm/20251106101138.2756175-1-binbin.wu@linux.intel.com
---
 arch/x86/kvm/x86.c | 33 ++++++++++-----------------------
 1 file changed, 10 insertions(+), 23 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 9c2e28028c2b..2c521902e2c6 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1219,34 +1219,21 @@ void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_lmsw);
 
-static void kvm_load_guest_xfeatures(struct kvm_vcpu *vcpu)
+static void kvm_load_xfeatures(struct kvm_vcpu *vcpu, bool load_guest)
 {
 	if (vcpu->arch.guest_state_protected)
 		return;
 
-	if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
-		if (vcpu->arch.xcr0 != kvm_host.xcr0)
-			xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
-
-		if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
-		    vcpu->arch.ia32_xss != kvm_host.xss)
-			wrmsrq(MSR_IA32_XSS, vcpu->arch.ia32_xss);
-	}
-}
-
-static void kvm_load_host_xfeatures(struct kvm_vcpu *vcpu)
-{
-	if (vcpu->arch.guest_state_protected)
+	if (!kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE))
 		return;
 
-	if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
-		if (vcpu->arch.xcr0 != kvm_host.xcr0)
-			xsetbv(XCR_XFEATURE_ENABLED_MASK, kvm_host.xcr0);
+	if (vcpu->arch.xcr0 != kvm_host.xcr0)
+		xsetbv(XCR_XFEATURE_ENABLED_MASK,
+		       load_guest ? vcpu->arch.xcr0 : kvm_host.xcr0);
 
-		if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
-		    vcpu->arch.ia32_xss != kvm_host.xss)
-			wrmsrq(MSR_IA32_XSS, kvm_host.xss);
-	}
+	if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
+	    vcpu->arch.ia32_xss != kvm_host.xss)
+		wrmsrq(MSR_IA32_XSS, load_guest ? vcpu->arch.ia32_xss : kvm_host.xss);
 }
 
 static void kvm_load_guest_pkru(struct kvm_vcpu *vcpu)
@@ -11333,7 +11320,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	if (vcpu->arch.guest_fpu.xfd_err)
 		wrmsrq(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err);
 
-	kvm_load_guest_xfeatures(vcpu);
+	kvm_load_xfeatures(vcpu, true);
 
 	if (unlikely(vcpu->arch.switch_db_regs &&
 		     !(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH))) {
@@ -11429,7 +11416,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	vcpu->mode = OUTSIDE_GUEST_MODE;
 	smp_wmb();
 
-	kvm_load_host_xfeatures(vcpu);
+	kvm_load_xfeatures(vcpu, false);
 
 	/*
 	 * Sync xfd before calling handle_exit_irqoff() which may

base-commit: 9052f4f6c539ea1fb7b282a34e6bb33154ce0b63
-- 
2.46.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] KVM: x86: Add a helper to dedup loading guest/host XCR0 and XSS
  2025-11-10  5:05 [PATCH v2] KVM: x86: Add a helper to dedup loading guest/host XCR0 and XSS Binbin Wu
@ 2025-11-10 12:54 ` Xiaoyao Li
  2025-11-21 18:55 ` Sean Christopherson
  1 sibling, 0 replies; 3+ messages in thread
From: Xiaoyao Li @ 2025-11-10 12:54 UTC (permalink / raw)
  To: Binbin Wu, seanjc, pbonzini; +Cc: kvm, linux-kernel, chao.gao

On 11/10/2025 1:05 PM, Binbin Wu wrote:
> Add and use a helper, kvm_load_xfeatures(), to dedup the code that loads
> guest/host xfeatures.
> 
> Opportunistically return early if X86_CR4_OSXSAVE is not set to reduce
> indentations.
> 
> No functional change intended.
> 
> Suggested-by: Chao Gao <chao.gao@intel.com>
> Reviewed-by: Chao Gao <chao.gao@intel.com>
> Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com>

Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com>

> ---
> v2:
> - Pass a bool to distinguish guest/host. [Chao, Xiaoyao]
> - Fix a typo in the short log. [Chao]
> - Opportunistically return early if X86_CR4_OSXSAVE is not set to reduce
>    indentations.
> 
> v1:
> - https://lore.kernel.org/kvm/20251106101138.2756175-1-binbin.wu@linux.intel.com
> ---
>   arch/x86/kvm/x86.c | 33 ++++++++++-----------------------
>   1 file changed, 10 insertions(+), 23 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 9c2e28028c2b..2c521902e2c6 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1219,34 +1219,21 @@ void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
>   }
>   EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_lmsw);
>   
> -static void kvm_load_guest_xfeatures(struct kvm_vcpu *vcpu)
> +static void kvm_load_xfeatures(struct kvm_vcpu *vcpu, bool load_guest)
>   {
>   	if (vcpu->arch.guest_state_protected)
>   		return;
>   
> -	if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
> -		if (vcpu->arch.xcr0 != kvm_host.xcr0)
> -			xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
> -
> -		if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
> -		    vcpu->arch.ia32_xss != kvm_host.xss)
> -			wrmsrq(MSR_IA32_XSS, vcpu->arch.ia32_xss);
> -	}
> -}
> -
> -static void kvm_load_host_xfeatures(struct kvm_vcpu *vcpu)
> -{
> -	if (vcpu->arch.guest_state_protected)
> +	if (!kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE))
>   		return;
>   
> -	if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
> -		if (vcpu->arch.xcr0 != kvm_host.xcr0)
> -			xsetbv(XCR_XFEATURE_ENABLED_MASK, kvm_host.xcr0);
> +	if (vcpu->arch.xcr0 != kvm_host.xcr0)
> +		xsetbv(XCR_XFEATURE_ENABLED_MASK,
> +		       load_guest ? vcpu->arch.xcr0 : kvm_host.xcr0);
>   
> -		if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
> -		    vcpu->arch.ia32_xss != kvm_host.xss)
> -			wrmsrq(MSR_IA32_XSS, kvm_host.xss);
> -	}
> +	if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
> +	    vcpu->arch.ia32_xss != kvm_host.xss)
> +		wrmsrq(MSR_IA32_XSS, load_guest ? vcpu->arch.ia32_xss : kvm_host.xss);
>   }
>   
>   static void kvm_load_guest_pkru(struct kvm_vcpu *vcpu)
> @@ -11333,7 +11320,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
>   	if (vcpu->arch.guest_fpu.xfd_err)
>   		wrmsrq(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err);
>   
> -	kvm_load_guest_xfeatures(vcpu);
> +	kvm_load_xfeatures(vcpu, true);
>   
>   	if (unlikely(vcpu->arch.switch_db_regs &&
>   		     !(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH))) {
> @@ -11429,7 +11416,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
>   	vcpu->mode = OUTSIDE_GUEST_MODE;
>   	smp_wmb();
>   
> -	kvm_load_host_xfeatures(vcpu);
> +	kvm_load_xfeatures(vcpu, false);
>   
>   	/*
>   	 * Sync xfd before calling handle_exit_irqoff() which may
> 
> base-commit: 9052f4f6c539ea1fb7b282a34e6bb33154ce0b63


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] KVM: x86: Add a helper to dedup loading guest/host XCR0 and XSS
  2025-11-10  5:05 [PATCH v2] KVM: x86: Add a helper to dedup loading guest/host XCR0 and XSS Binbin Wu
  2025-11-10 12:54 ` Xiaoyao Li
@ 2025-11-21 18:55 ` Sean Christopherson
  1 sibling, 0 replies; 3+ messages in thread
From: Sean Christopherson @ 2025-11-21 18:55 UTC (permalink / raw)
  To: Sean Christopherson, pbonzini, Binbin Wu
  Cc: kvm, linux-kernel, chao.gao, xiaoyao.li

On Mon, 10 Nov 2025 13:05:39 +0800, Binbin Wu wrote:
> Add and use a helper, kvm_load_xfeatures(), to dedup the code that loads
> guest/host xfeatures.
> 
> Opportunistically return early if X86_CR4_OSXSAVE is not set to reduce
> indentations.
> 
> No functional change intended.
> 
> [...]

Applied to kvm-x86 misc, thanks!

[1/1] KVM: x86: Add a helper to dedup loading guest/host XCR0 and XSS
      https://github.com/kvm-x86/linux/commit/0b28f21ad462

--
https://github.com/kvm-x86/linux/tree/next

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-11-21 18:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-10  5:05 [PATCH v2] KVM: x86: Add a helper to dedup loading guest/host XCR0 and XSS Binbin Wu
2025-11-10 12:54 ` Xiaoyao Li
2025-11-21 18:55 ` Sean Christopherson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox