KVM: x86: Add a help to dedup loading guest/host XCR0 and XSS
PATCH 1/1 KVM: x86: Add a help to dedup loading guest/host XCR0 and XSS
Add and use a helper, kvm_load_xfeatures(), to dedup the code that loads guest/host xfeatures by passing XCR0 and XSS values accordingly.
No functional change intended.
---
The patch is based on the patch series
"KVM: x86: Cleanup #MC and XCR0/XSS/PKRU handling" [1], which is applied on top of kvm-x86 next branch(commit a996dd2a5e1ec54dcf7d7b93915ea3f97e14e68a).
[1] https://lore.kernel.org/all/[email protected]
--- arch/x86/kvm/x86.c | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-)
arch/x86/kvm/x86.c +5 -20
@@ -1203,33 +1203,18 @@ 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,u64 xcr0,u64 xss) {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); +xsetbv (XCR_XFEATURE_ENABLED_MASK , 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) -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 (guest_cpu_cap_has (vcpu,X86_FEATURE_XSAVES ) && - vcpu->arch.ia32_xss != kvm_host.xss) -wrmsrq (MSR_IA32_XSS , kvm_host.xss); +wrmsrq (MSR_IA32_XSS , xss); } }
@@ -11310,7 +11295,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, vcpu->arch.xcr0, vcpu->arch.ia32_xss);if (unlikely (vcpu->arch.switch_db_regs && !(vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH ))) {
@@ -11406,7 +11391,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, kvm_host.xcr0, kvm_host.xss);/* * Sync xfd before calling handle_exit_irqoff() which may
base-commit: a996dd2a5e1ec54dcf7d7b93915ea3f97e14e68a
prerequisite-patch-id: 9aafd634f0ab2033d7b032e227d356777469e046
prerequisite-patch-id: 656ce1f5aa97c77a9cf6125713707a5007b2c7ba
prerequisite-patch-id: d6328b8c0fdb8593bb534ab7378821edcf9f639d
prerequisite-patch-id: c7f36d1cedc4ae6416223d2225460944629b3d4f
Signature · 1 line
2.46.0
s/help/helper in the subject.
Quoted 6 lines · Binbin Wu
Add and use a helper,
kvm_load_xfeatures(), to dedup the code that loads guest/host xfeatures by passing XCR0 and XSS values accordingly.No functional change intended.
Signed-off-by Binbin Wu <[email protected]>
<snip>
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 11394
Quoted 6 lines
arch/x86/kvm/x86.c +1
-1@@ -11406,7 +11391,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, kvm_host.xcr0, kvm_host.xss);
Nit: given that xcr0/xss are either guest or host values, would it be slightly better for this helper to accept a boolean (e.g., bool load_guest) to convey that the API loads guest (or host) values rather than arbitrary xcr0/xss values? like fpu_swap_kvm_fpstate().
static void kvm_load_xfeatures(struct kvm_vcpu *vcpu, bool load_guest)
{
u64 xcr0 = load_guest ? vcpu->arch.xcr0 : kvm_host.xcr0;
u64 xss = load_guest ? vcpu->arch.ia32_xss : kvm_host.xss;
if (vcpu->arch.guest_state_protected)
return;Quoted 12 lines
arch/x86/kvm/x86.c +0
-0/* * Sync xfd before calling handle_exit_irqoff() which may base-commit:
a996dd2a5e1ec54dcf7d7b93915ea3f97e14e68a
prerequisite-patch-id:9aafd634f0ab2033d7b032e227d356777469e046
prerequisite-patch-id:656ce1f5aa97c77a9cf6125713707a5007b2c7ba
prerequisite-patch-id:d6328b8c0fdb8593bb534ab7378821edcf9f639d
prerequisite-patch-id:c7f36d1cedc4ae6416223d2225460944629b3d4fSignature · 1 line
2.46.0
Quoted 17 lines · Chao Gao
s/help/helper in the subject.
Quoted 6 lines · Binbin Wu
Add and use a helper,
kvm_load_xfeatures(), to dedup the code that loads guest/host xfeatures by passing XCR0 and XSS values accordingly.No functional change intended.
Signed-off-by Binbin Wu <[email protected]>Reviewed-by Chao Gao <[email protected]><snip>
Quoted 4 lines
arch/x86/kvm/x86.c +0
-0@@ -11406,7 +11391,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) vcpu->mode =OUTSIDE_GUEST_MODE ;smp_wmb ();
Quoted 6 lines
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 11394
Quoted 2 lines
arch/x86/kvm/x86.c +1
-1-kvm_load_host_xfeatures (vcpu); +kvm_load_xfeatures (vcpu, kvm_host.xcr0, kvm_host.xss);Nit: given that xcr0/xss are either guest or host values, would it be slightly better for this helper to accept a boolean (e.g., bool
load_guest) to convey that the API loads guest (or host) values rather than arbitrary xcr0/xss values? likefpu_swap_kvm_fpstate().
Make sense.
Quoted 5 lines
static void
kvm_load_xfeatures(struct kvm_vcpu *vcpu, bool load_guest){ u64 xcr0 = load_guest ? vcpu->arch.xcr0 : kvm_host.xcr0; u64 xss = load_guest ? vcpu->arch.ia32_xss : kvm_host.xss;
Since they are only used once, I even want to open code them as:
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, 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,
load_guest ? vcpu->arch.ia32_xss : kvm_host.xss); }
}
Quoted 16 lines
if (vcpu->arch.guest_state_protected) return;Quoted 12 lines
arch/x86/kvm/x86.c +0
-0/* * Sync xfd before calling handle_exit_irqoff() which may base-commit:
a996dd2a5e1ec54dcf7d7b93915ea3f97e14e68a
prerequisite-patch-id:9aafd634f0ab2033d7b032e227d356777469e046
prerequisite-patch-id:656ce1f5aa97c77a9cf6125713707a5007b2c7ba
prerequisite-patch-id:d6328b8c0fdb8593bb534ab7378821edcf9f639d
prerequisite-patch-id:c7f36d1cedc4ae6416223d2225460944629b3d4fSignature · 1 line
2.46.0
Sean Christopherson
maintainerQuoted 20 lines · Binbin Wu
Quoted 17 lines · Chao Gao
s/help/helper in the subject.
Quoted 6 lines · Binbin Wu
Add and use a helper,
kvm_load_xfeatures(), to dedup the code that loads guest/host xfeatures by passing XCR0 and XSS values accordingly.No functional change intended.
Signed-off-by Binbin Wu <[email protected]>Reviewed-by Chao Gao <[email protected]><snip>
Quoted 4 lines
arch/x86/kvm/x86.c +0
-0@@ -11406,7 +11391,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) vcpu->mode =OUTSIDE_GUEST_MODE ;smp_wmb ();
Quoted 8 lines
Quoted 6 lines
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 11394
Quoted 2 lines
arch/x86/kvm/x86.c +1
-1-kvm_load_host_xfeatures (vcpu); +kvm_load_xfeatures (vcpu, kvm_host.xcr0, kvm_host.xss);Nit: given that xcr0/xss are either guest or host values, would it be slightly better for this helper to accept a boolean (e.g., bool
load_guest) to convey that the API loads guest (or host) values rather than arbitrary xcr0/xss values? likefpu_swap_kvm_fpstate().Make sense.
I don't love passing true/false, but I Xiaoyao does make a good a point that subtly requiring the caller to pass vcpu->arch.xcr0 vs. kvm_host.xcr0 is weird and confusing.
Quoted 6 lines
Quoted 4 lines
static void
kvm_load_xfeatures(struct kvm_vcpu *vcpu, bool load_guest){ u64 xcr0 = load_guest ? vcpu->arch.xcr0 : kvm_host.xcr0; u64 xss = load_guest ? vcpu->arch.ia32_xss : kvm_host.xss;Since they are only used once, I even want to open code them as:
+100, I find your version much more intuitive.
Quoted 34 lines
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, 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,
load_guest ?vcpu->arch.ia32_xss: kvm_host.xss); }
}Quoted 16 lines
if (vcpu->arch.guest_state_protected) return;Quoted 12 lines
arch/x86/kvm/x86.c +0
-0/* * Sync xfd before calling handle_exit_irqoff() which may base-commit:
a996dd2a5e1ec54dcf7d7b93915ea3f97e14e68a
prerequisite-patch-id:9aafd634f0ab2033d7b032e227d356777469e046
prerequisite-patch-id:656ce1f5aa97c77a9cf6125713707a5007b2c7ba
prerequisite-patch-id:d6328b8c0fdb8593bb534ab7378821edcf9f639d
prerequisite-patch-id:c7f36d1cedc4ae6416223d2225460944629b3d4fSignature · 1 line
2.46.0
Quoted 18 lines · Chao Gao
s/help/helper in the subject.
Quoted 6 lines · Binbin Wu
Add and use a helper,
kvm_load_xfeatures(), to dedup the code that loads guest/host xfeatures by passing XCR0 and XSS values accordingly.No functional change intended.
Signed-off-by Binbin Wu <[email protected]>Reviewed-by Chao Gao <[email protected]><snip>
Quoted 4 lines
arch/x86/kvm/x86.c +0
-0@@ -11406,7 +11391,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) vcpu->mode =OUTSIDE_GUEST_MODE ;smp_wmb ();
Quoted 7 lines
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 11394
Quoted 2 lines
arch/x86/kvm/x86.c +1
-1-kvm_load_host_xfeatures (vcpu); +kvm_load_xfeatures (vcpu, kvm_host.xcr0, kvm_host.xss);Nit: given that xcr0/xss are either guest or host values, would it be slightly better for this helper to accept a boolean (e.g., bool
load_guest) to convey that the API loads guest (or host) values rather than arbitrary xcr0/xss values? likefpu_swap_kvm_fpstate().
This really is a good idea!
Allow arbitrary input but skip the write when vcpu->arch.{xcr0,ia32_xss} == kvm_host.{xcr0,ia32_xss} does introduce confusion.
Quoted 21 lines
static void
kvm_load_xfeatures(struct kvm_vcpu *vcpu, bool load_guest){ u64 xcr0 = load_guest ? vcpu->arch.xcr0 : kvm_host.xcr0; u64 xss = load_guest ? vcpu->arch.ia32_xss : kvm_host.xss; if (vcpu->arch.guest_state_protected) return;Quoted 12 lines
arch/x86/kvm/x86.c +0
-0/* * Sync xfd before calling handle_exit_irqoff() which may base-commit:
a996dd2a5e1ec54dcf7d7b93915ea3f97e14e68a
prerequisite-patch-id:9aafd634f0ab2033d7b032e227d356777469e046
prerequisite-patch-id:656ce1f5aa97c77a9cf6125713707a5007b2c7ba
prerequisite-patch-id:d6328b8c0fdb8593bb534ab7378821edcf9f639d
prerequisite-patch-id:c7f36d1cedc4ae6416223d2225460944629b3d4fSignature · 1 line
2.46.0