Skip to contentSkip to conversation

KVM: x86: Revise guest_fpu xcomp_bv field

PATCH In review Jing Liu 2 participants 3 messages Feb 25, 2021 – Mar 9, 2021

Subsystems (3)
lore
Share & tools
lore

Jing Liu

author
#0 lore raw

PATCH v2 1/1 KVM: x86: Revise guest_fpu xcomp_bv field

XCOMP_BV[63] field indicates that the save area is in the compacted format and XCOMP_BV[62:0] indicates the states that have space allocated in the save area, including both XCR0 and XSS bits enabled by the host kernel. Use xfeatures_mask_all for calculating xcomp_bv and reuse XCOMP_BV_COMPACTED_FORMAT defined by kernel.

---
 arch/x86/kvm/x86.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
arch/x86/kvm/x86.c +2 -6
index 1b404e4d7dd8..f115493f577d 100644
@@ -4435,8 +4435,6 @@ static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
 	return 0;
 }
 
-#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
-
 static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
 {
 	struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
@@ -4494,7 +4492,8 @@ static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
 	/* Set XSTATE_BV and possibly XCOMP_BV.  */
 	xsave->header.xfeatures = xstate_bv;
 	if (boot_cpu_has(X86_FEATURE_XSAVES))
-		xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
+		xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
+					 xfeatures_mask_all;
 
 	/*
 	 * Copy each region from the non-compacted offset to the
@@ -9912,9 +9911,6 @@ static void fx_init(struct kvm_vcpu *vcpu)
 		return;
 
 	fpstate_init(&vcpu->arch.guest_fpu->state);
-	if (boot_cpu_has(X86_FEATURE_XSAVES))
-		vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
-			host_xcr0 | XSTATE_COMPACTION_ENABLED;
 
 	/*
 	 * Ensure guest xcr0 is valid for loading
Signature · 1 line
2.18.4
#1 lore raw
Quoted 25 lines · Jing Liu

XCOMP_BV[63] field indicates that the save area is in the compacted format and XCOMP_BV[62:0] indicates the states that have space allocated in the save area, including both XCR0 and XSS bits enabled by the host kernel. Use xfeatures_mask_all for calculating xcomp_bv and reuse XCOMP_BV_COMPACTED_FORMAT defined by kernel.

---
 arch/x86/kvm/x86.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
arch/x86/kvm/x86.c +0 -2
index 1b404e4d7dd8..f115493f577d 100644
@@ -4435,8 +4435,6 @@ static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
 	return 0;
 }
 
-#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
-
 static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
 {
 	struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;

arch/x86/kvm/x86.c static void load_xsave(struct kvm_vcpu *vcpu, u8 *src) · line 4496

Quoted 6 lines
arch/x86/kvm/x86.c +2 -1
@@ -4494,7 +4492,8 @@ static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
 	/* Set XSTATE_BV and possibly XCOMP_BV.  */
 	xsave->header.xfeatures = xstate_bv;
 	if (boot_cpu_has(X86_FEATURE_XSAVES))
-		xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
+		xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
+					 xfeatures_mask_all;

Doesn't fill_xsave also need to be updated? Not with xfeatures_mask_all, but to account for arch.ia32_xss? I believe it's a nop with the current code, since supported_xss is zero, but it should be fixed, no?

arch/x86/kvm/x86.c static void fx_init(struct kvm_vcpu *vcpu) · line 9917

Quoted 10 lines
arch/x86/kvm/x86.c +0 -3
 	/*
 	 * Copy each region from the non-compacted offset to the
@@ -9912,9 +9911,6 @@ static void fx_init(struct kvm_vcpu *vcpu)
 		return;
 
 	fpstate_init(&vcpu->arch.guest_fpu->state);
-	if (boot_cpu_has(X86_FEATURE_XSAVES))
-		vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
-			host_xcr0 | XSTATE_COMPACTION_ENABLED;

Ugh, this _really_ needs a comment in the changelog. It took me a while to realize fpstate_init() does exactly what the new fill_xave() is doing.

And isn't the code in load_xsave() redundant and can be removed? Any code that uses get_xsave_addr() would be have a dependency on load_xsave() if it's not redundant, and I can't see how that would work.

Quoted 6 lines
arch/x86/kvm/x86.c +0 -0
 	/*
 	 * Ensure guest xcr0 is valid for loading
Signature · 1 line
2.18.4
author
#2 lore raw
Quoted 20 lines · Sean Christopherson
Quoted 19 lines · Jing Liu

XCOMP_BV[63] field indicates that the save area is in the compacted format and XCOMP_BV[62:0] indicates the states that have space allocated in the save area, including both XCR0 and XSS bits enabled by the host kernel. Use xfeatures_mask_all for calculating xcomp_bv and reuse XCOMP_BV_COMPACTED_FORMAT defined by kernel.

---
  arch/x86/kvm/x86.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 1b404e4d7dd8..f115493f577d 100644
@@ -4435,8 +4435,6 @@ static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
  	return 0;
  }
 
Quoted 15 lines

arch/x86/kvm/x86.c static void load_xsave(struct kvm_vcpu *vcpu, u8 *src) · line 4496

Quoted 12 lines
arch/x86/kvm/x86.c +2 -3
-#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
-
  static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
  {
  	struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
@@ -4494,7 +4492,8 @@ static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
  	/* Set XSTATE_BV and possibly XCOMP_BV.  */
  	xsave->header.xfeatures = xstate_bv;
  	if (boot_cpu_has(X86_FEATURE_XSAVES))
-		xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
+		xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
+					 xfeatures_mask_all;

Doesn't fill_xsave also need to be updated? Not with xfeatures_mask_all, but to account for arch.ia32_xss? I believe it's a nop with the current code, since supported_xss is zero, but it should be fixed, no?

Yes. For the arch.ia32_xss, I noticed CET (https://lkml.org/lkml/2020/7/15/1412)
has posted related change so I didn't touch xstate_bv for fill_xsave for now. Finally, fill_xsave() need e.g. arch.guest_supported_xss for xstate_bv, for xcomp_bv, xfeatures_mask_all is ok.

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/x86.c +0 -0
  	/*
  	 * Copy each region from the non-compacted offset to the
@@ -9912,9 +9911,6 @@ static void fx_init(struct kvm_vcpu *vcpu)
  		return;
 
Quoted 6 lines

arch/x86/kvm/x86.c static void fx_init(struct kvm_vcpu *vcpu) · line 9917

Quoted 4 lines
arch/x86/kvm/x86.c +0 -3
  	fpstate_init(&vcpu->arch.guest_fpu->state);
-	if (boot_cpu_has(X86_FEATURE_XSAVES))
-		vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
-			host_xcr0 | XSTATE_COMPACTION_ENABLED;

Ugh, this _really_ needs a comment in the changelog. It took me a while to realize fpstate_init() does exactly what the new fill_xave() is doing.

How about introducing that "fx_init()->fpstate_init() initializes xcomp_bv of guest_fpu so no need to set again in later fill_xsave() and load_xsave()" in commit message?

Quoted 2 lines

And isn't the code in load_xsave() redundant and can be removed?

Oh, yes. Keep fx_init() initializing xcomp_bv for guest_fpu is enough. Let me remove it in load_xsave later.
And for fill_xsave(), I think no need to set xcomp_bv there.

Quoted 3 lines

Any code that
uses get_xsave_addr() would be have a dependency on load_xsave() if it's not redundant, and I can't see how that would work.

Sorry I didn't quite understand why get_xsave_addr() has dependency on load_xsave(), do you mean the xstate_bv instead of xcomp_bv, that load_xsave() uses it to get the addr?

Thanks,
Jing

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/x86.c +0 -0
  	/*
  	 * Ensure guest xcr0 is valid for loading
Signature · 1 line
2.18.4