Skip to contentSkip to conversation

Introduce support for guest AMX feature

RFC PATCH In review Jing Liu 7 participants 39 messages Feb 7, 2021 – Jul 6, 2021

Subsystems (3)
lore
Share & tools
lore

Jing Liu

author
#0 lore raw

cover Introduce support for guest AMX feature

Intel introduces Advanced Matrix Extensions (AMX) [1] feature that will be shipping soon. AMX consists of configurable two-dimensional "TILE" registers and new accelerator instructions that operate on them. TMUL (Tile matrix MULtiply) is the first accelerator instruction set to use the new registers.

Intel AMX is XSAVE supported and XSAVE enabled. It is associated with two state components, XTILECFG and XTILEDATA. The XTILEDATA state component is very large so an XSAVE extension called extended feature disable (XFD) is introduced to support dynamic usage. When XFD is enabled for a state component, any instruction that would access that state component does not execute and instead generates an #NM. So Linux kernel arms XFD to monitor the first usage of AMX.

This patchset adds AMX and XFD support for guest: providing related CPUID and MSRs to guest, adding extended XSAVE state context switch and XFD MSRs switch during vmenter/vmexit.

This RFC series is based on kernel AMX series v3 [2] in LKML though not latest upstream commit and we'd looking forward for your comments.

[1]: Intel Architecture Instruction Set Extension Programming Reference
https://software.intel.com/content/dam/develop/external/us/en/documents/architecture-instruction-set-extensions-programming-reference.pdf

[2]: AMX kernel series v3 https://lkml.org/lkml/2020/12/23/464

Jing Liu (7):
kvm: x86: Expose XFD CPUID to guest
kvm: x86: Introduce XFD MSRs as passthrough to guest
kvm: x86: Dynamic XSAVE and XFD MSRs context switch
kvm: x86: Add new ioctls for XSAVE extension
kvm: x86: Revise CPUID.D.1.EBX for alignment rule
kvm: x86: Add AMX_TILE, AMX_INT8 and AMX_BF16 support
kvm: x86: AMX XCR0 support for guest

 arch/x86/include/asm/kvm_host.h |   3 +
 arch/x86/include/uapi/asm/kvm.h |   5 ++
 arch/x86/kernel/fpu/init.c      |   1 +
 arch/x86/kernel/fpu/xstate.c    |   2 +
 arch/x86/kvm/cpuid.c            |  19 +++-
 arch/x86/kvm/vmx/vmx.c          | 114 ++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.h          |   7 +-
 arch/x86/kvm/x86.c              | 153 ++++++++++++++++++++++++++------
 include/uapi/linux/kvm.h        |   8 ++
 9 files changed, 279 insertions(+), 33 deletions(-)
Signature · 1 line
2.18.4
author
#1 lore raw

RFC PATCH 1/7 kvm: x86: Expose XFD CPUID to guest

Intel's Extended Feature Disable (XFD) feature is an extension to the XSAVE feature that allows an operating system to enable a feature while preventing specific user threads from using the feature. A processor that supports XFD enumerates CPUID.(EAX=0DH,ECX=1):EAX[4] as 1.

---
 arch/x86/kvm/cpuid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
index 83637a2ff605..04a73c395c71 100644
@@ -437,7 +437,7 @@ void kvm_set_cpu_caps(void)
 	);
 
 	kvm_cpu_cap_mask(CPUID_D_1_EAX,
-		F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES)
+		F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | F(XFD)
 	);
 
 	kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
Signature · 1 line
2.18.4
#2 lore raw

Re: [PATCH RFC 1/7] kvm: x86: Expose XFD CPUID to guest

I need a formletter for these...

GET_SUPPORTED_CPUID advertises support to userspace, it does not expose anything to the guest.

Quoted 16 lines · Jing Liu

Intel's Extended Feature Disable (XFD) feature is an extension to the XSAVE feature that allows an operating system to enable a feature while preventing specific user threads from using the feature. A processor that supports XFD enumerates CPUID.(EAX=0DH,ECX=1):EAX[4] as 1.

---
 arch/x86/kvm/cpuid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +0 -0
index 83637a2ff605..04a73c395c71 100644

arch/x86/kvm/cpuid.c void kvm_set_cpu_caps(void) · line 440

Quoted 5 lines
arch/x86/kvm/cpuid.c +1 -1
@@ -437,7 +437,7 @@ void kvm_set_cpu_caps(void)
 	);
 
 	kvm_cpu_cap_mask(CPUID_D_1_EAX,
-		F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES)
+		F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | F(XFD)

KVM must not advertise support until it actually has said support, i.e. this patch needs to go at the end of the series.

Also, adding the kvm_cpu_cap flag in a separate patch isn't strictly required. In most cases, I would go so far as to say that if there is additional enabling to be done, advertising the feature should be done in the same patch that adds the last bits of enabling. Putting the CPUID stuff in its own patch doesn't usually add values, e.g. if there's a bug in the actual support code bisecting will point at the wrong patch if userspace conditions its vCPU model on GET_SUPPORTED_CPUID.

Quoted 6 lines
arch/x86/kvm/cpuid.c +0 -0
 	);
 
 	kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
Signature · 1 line
2.18.4
author
#3 lore raw

Re: [PATCH RFC 1/7] kvm: x86: Expose XFD CPUID to guest

Quoted 4 lines · Sean Christopherson

I need a formletter for these...

GET_SUPPORTED_CPUID advertises support to userspace, it does not expose anything to the guest.

Oh, yes. This is only part of cpuid exposing process. Let me change the commit log.

Quoted 26 lines · Jing Liu
Quoted 16 lines

Intel's Extended Feature Disable (XFD) feature is an extension to the XSAVE feature that allows an operating system to enable a feature while preventing specific user threads from using the feature. A processor that supports XFD enumerates CPUID.(EAX=0DH,ECX=1):EAX[4] as 1.

---
  arch/x86/kvm/cpuid.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +0 -0
index 83637a2ff605..04a73c395c71 100644

arch/x86/kvm/cpuid.c void kvm_set_cpu_caps(void) · line 440

Quoted 5 lines
arch/x86/kvm/cpuid.c +1 -1
@@ -437,7 +437,7 @@ void kvm_set_cpu_caps(void)
  	);
 
  	kvm_cpu_cap_mask(CPUID_D_1_EAX,
-		F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES)
+		F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | F(XFD)

KVM must not advertise support until it actually has said support, i.e. this patch needs to go at the end of the series.

Quoted 7 lines

Also, adding the kvm_cpu_cap flag in a separate patch isn't strictly required. In most cases, I would go so far as to say that if there is additional enabling to be done, advertising the feature should be done in the same patch that adds the last bits of enabling. Putting the CPUID stuff in its own patch doesn't usually add values, e.g. if there's a bug in the actual support code bisecting will point at the wrong patch if userspace conditions its vCPU model on GET_SUPPORTED_CPUID.

Got it. Since XFD are separate feature from AMX, when trying to think about putting CPUID stuff to MSR stuff, current MSR stuff are mainly two parts, one is two MSRs support, another is MSR switches. So it seems not suitable to put CPUID into MSR switch patch?

Thanks,
Jing

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/cpuid.c +0 -0
  	);
 
  	kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
Signature · 1 line
2.18.4
author
#4 lore raw

RFC PATCH 2/7 kvm: x86: Introduce XFD MSRs as passthrough to guest

In reply to Jing Liu

XFD feature introduces two new MSRs: IA32_XFD and IA32_XFD_ERR. Each of the MSRs contains a state-component bitmap. XFD is enabled for state component i if XCR0[i] = IA32_XFD[i] = 1. When XFD is enabled for a state component, any instruction that would access that state component does not execute and instead generates an device-not-available exception (#NM). IA32_XFD_ERR is for indicating which state causes the #NM event.

The MSRs are per task and need be context switched between host and guest, and also between tasks inside guest just as native. Passthrough both MSRs to let guest access and write without vmexit. Add two slots for XFD MSRs as desired passthrough MSRs.

---
 arch/x86/kvm/vmx/vmx.c | 38 ++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.h |  6 +++++-
 arch/x86/kvm/x86.c     |  6 ++++++
 3 files changed, 49 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +38 -0
index 47b8357b9751..7fa54e78c45c 100644
@@ -162,6 +162,8 @@ static u32 vmx_possible_passthrough_msrs[MAX_POSSIBLE_PASSTHROUGH_MSRS] = {
 	MSR_IA32_SYSENTER_CS,
 	MSR_IA32_SYSENTER_ESP,
 	MSR_IA32_SYSENTER_EIP,
+	MSR_IA32_XFD,
+	MSR_IA32_XFD_ERR,
 	MSR_CORE_C1_RES,
 	MSR_CORE_C3_RESIDENCY,
 	MSR_CORE_C6_RESIDENCY,
@@ -1824,6 +1826,18 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 		msr_info->data = vmx->msr_ia32_umwait_control;
 		break;
+	case MSR_IA32_XFD:
+		if (!msr_info->host_initiated)
+			return 1;
+
+		msr_info->data = vmx->msr_ia32_xfd;
+		break;
+	case MSR_IA32_XFD_ERR:
+		if (!msr_info->host_initiated)
+			return 1;
+
+		msr_info->data = vmx->msr_ia32_xfd_err;
+		break;
 	case MSR_IA32_SPEC_CTRL:
 		if (!msr_info->host_initiated &&
 		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
@@ -2026,6 +2040,20 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 		vmx->msr_ia32_umwait_control = data;
 		break;
+	case MSR_IA32_XFD:
+		if (!msr_info->host_initiated)
+			return 1;
+
+		vmx->msr_ia32_xfd = data;
+		break;
+	case MSR_IA32_XFD_ERR:
+		if (!msr_info->host_initiated)
+			return 1;
+		if (data)
+			break;
+
+		vmx->msr_ia32_xfd_err = data;
+		break;
 	case MSR_IA32_SPEC_CTRL:
 		if (!msr_info->host_initiated &&
 		    !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
@@ -7219,6 +7247,12 @@ static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
 		vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
 }
 
+static void vmx_update_intercept_xfd(struct kvm_vcpu *vcpu)
+{
+	vmx_set_intercept_for_msr(vcpu, MSR_IA32_XFD, MSR_TYPE_RW, false);
+	vmx_set_intercept_for_msr(vcpu, MSR_IA32_XFD_ERR, MSR_TYPE_RW, false);
+}
+
 static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -7249,6 +7283,10 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
 			guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
 		update_intel_pt_cfg(vcpu);
 
+	if (boot_cpu_has(X86_FEATURE_XFD) &&
+			guest_cpuid_has(vcpu, X86_FEATURE_XFD))
+		vmx_update_intercept_xfd(vcpu);
+
 	if (boot_cpu_has(X86_FEATURE_RTM)) {
 		struct vmx_uret_msr *msr;
 		msr = vmx_find_uret_msr(vmx, MSR_IA32_TSX_CTRL);
arch/x86/kvm/vmx/vmx.h +5 -1
index f6f66e5c6510..d487f5a53a08 100644
@@ -281,11 +281,15 @@ struct vcpu_vmx {
 	struct pt_desc pt_desc;
 
 	/* Save desired MSR intercept (read: pass-through) state */
-#define MAX_POSSIBLE_PASSTHROUGH_MSRS	13
+#define MAX_POSSIBLE_PASSTHROUGH_MSRS	15
 	struct {
 		DECLARE_BITMAP(read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
 		DECLARE_BITMAP(write, MAX_POSSIBLE_PASSTHROUGH_MSRS);
 	} shadow_msr_intercept;
+
+	/* eXtended Feature Disabling (XFD) MSRs */
+	u64 msr_ia32_xfd;
+	u64 msr_ia32_xfd_err;
 };
 
 enum ept_pointers_status {
arch/x86/kvm/x86.c +6 -0
index 93b5bacad67a..9ca8b1e58afa 100644
@@ -1213,6 +1213,7 @@ static const u32 msrs_to_save_all[] = {
 	MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
 	MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
 	MSR_IA32_UMWAIT_CONTROL,
+	MSR_IA32_XFD, MSR_IA32_XFD_ERR,
 
 	MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
 	MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
@@ -5744,6 +5745,11 @@ static void kvm_init_msr_list(void)
 			if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG))
 				continue;
 			break;
+		case MSR_IA32_XFD:
+		case MSR_IA32_XFD_ERR:
+			if (!kvm_cpu_cap_has(X86_FEATURE_XFD))
+				continue;
+			break;
 		case MSR_IA32_RTIT_CTL:
 		case MSR_IA32_RTIT_STATUS:
 			if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT))
Signature · 1 line
2.18.4
#5 lore raw

Re: [PATCH RFC 2/7] kvm: x86: Introduce XFD MSRs as passthrough to guest

Quoted 1 line · Jing Liu

Passthrough both MSRs to let guest access and write without vmexit.

Why? Except for read-only MSRs, e.g. MSR_CORE_C1_RES, passthrough MSRs are costly to support because KVM must context switch the MSR (which, by the by, is completely missing from the patch).

In other words, if these MSRs are full RW passthrough, guests with XFD enabled will need to load the guest value on entry, save the guest value on exit, and load the host value on exit. That's in the neighborhood of a 40% increase in latency for a single VM-Enter/VM-Exit roundtrip (~1500 cycles => >2000 cycles).

I'm not saying these can't be passhthrough, but there needs to be strong justification for letting the guest read/write them directly.

#6 lore raw

Re: [PATCH RFC 2/7] kvm: x86: Introduce XFD MSRs as passthrough to guest

Quoted 8 lines · Sean Christopherson
Quoted 1 line · Jing Liu

Passthrough both MSRs to let guest access and write without vmexit.

Why? Except for read-only MSRs, e.g. MSR_CORE_C1_RES, passthrough MSRs are costly to support because KVM must context switch the MSR (which, by the by, is completely missing from the patch).

Quoted 7 lines

In other words, if these MSRs are full RW passthrough, guests with XFD enabled will need to load the guest value on entry, save the guest value on exit, and load the host value on exit. That's in the neighborhood of a 40% increase in latency for a single VM-Enter/VM-Exit roundtrip (~1500 cycles => >2000 cycles).

I'm not saying these can't be passhthrough, but there needs to be strong justification for letting the guest read/write them directly.

If we virtualize XFD, we have to context switch the guest/host values on VM-entry/VM-exit, don't we? If we don't, we're forced to synthesize the #NM on any instruction that would access a disabled state component, and I don't think we have any way of doing that. We could intercept a guest WRMSR to these MSRs, but it sounds like the guest can still implicitly write to IA32_XFD_ERR, if we allow it to have a non-zero IA32_XFD.

Perhaps the answer is "don't virtualize XFD."

author
#7 lore raw

Re: [PATCH RFC 2/7] kvm: x86: Introduce XFD MSRs as passthrough to guest

In reply to Sean Christopherson
Quoted 6 lines · Sean Christopherson
Quoted 1 line · Jing Liu

Passthrough both MSRs to let guest access and write without vmexit.

Why? Except for read-only MSRs, e.g. MSR_CORE_C1_RES, passthrough MSRs are costly to support because KVM must context switch the MSR (which, by the by, is completely missing from the patch).

Quoted 7 lines

In other words, if these MSRs are full RW passthrough, guests with XFD enabled will need to load the guest value on entry, save the guest value on exit, and load the host value on exit. That's in the neighborhood of a 40% increase in latency for a single VM-Enter/VM-Exit roundtrip (~1500 cycles => >2000 cycles).

I'm not saying these can't be passhthrough, but there needs to be strong justification for letting the guest read/write them directly.

For IA32_XFD, it's per task and switched during task switch(if different). Meanwhile, hardware uses IA32_XFD to prevent any instruction per task touching AMX state at the first time and generate #NM. This means if vcpu running with AMX enabled, hardware IA32_XFD should keep a guest value, and once guest #NM handler finished, the bit should not be set anymore for this task.
No matter passthrough or not, IA32_XFD need be restored to guest value when vm-enter, and load host value on exit (or load zero to prevent losing guest AMX in use state). And passthrough makes guest IA32_XFD switch during task switch without trapped.

For IA32_XFD_ERR, hardware automatically set the bit once #NM fault. #NM handler detect and clear the bit.
No matter passthrough or not, KVM doesn't know if guest has #NM, it need read and save IA32_XFD_ERR when vmexit to prevent preemption task from clearing bit. Emulating need not restore guest non-zero IA32_XFD_ERR when vmenter and effort is guest #NM handler takes extra trap(s). But restoring a non-zero IA32_XFD_ERR only occurs for the vmexit case: between task's first AMX instruction(causing #NM) and end of clearing in guest #NM handler.

Thanks,
Jing

#8 lore raw

Re: [PATCH RFC 2/7] kvm: x86: Introduce XFD MSRs as passthrough to guest

In reply to Sean Christopherson
Quoted 10 lines · Sean Christopherson
Quoted 1 line · Jing Liu

Passthrough both MSRs to let guest access and write without vmexit.

Why? Except for read-only MSRs, e.g. MSR_CORE_C1_RES, passthrough MSRs are costly to support because KVM must context switch the MSR (which, by the by, is completely missing from the patch).

In other words, if these MSRs are full RW passthrough, guests with XFD enabled will need to load the guest value on entry, save the guest value on exit, and load the host value on exit. That's in the neighborhood of a 40% increase in latency for a single VM-Enter/VM-Exit roundtrip (~1500 cycles => >2000 cycles).

I'm not taking a position as to whether these _should_ be passthrough or not. But, if they are, I don't think you strictly need to do the RDMSR/WRMSR at VM-Exit time.

Just like the "FPU", XFD isn't be used in normal kernel code. This is why we can be lazy about FPU state with TIF_NEED_FPU_LOAD. I _suspect_ that some XFD manipulation can be at least deferred to the same place where the FPU state is manipulated: places like switch_fpu_return() or kernel_fpu_begin().

Doing that would at least help the fast VM-Exit/VM-Enter paths that really like TIF_NEED_FPU_LOAD today.

I guess the nasty part is that you actually need to stash the old XFD MSR value in the vcpu structure and that's not available at context-switch time. So, maybe this would only allow deferring the WRMSR. That's better than nothing I guess.

author
#9 lore raw

Re: [PATCH RFC 2/7] kvm: x86: Introduce XFD MSRs as passthrough to guest

Quoted 7 lines · Dave Hansen
Quoted 6 lines · Sean Christopherson
Quoted 1 line · Jing Liu

Passthrough both MSRs to let guest access and write without vmexit.

Why? Except for read-only MSRs, e.g. MSR_CORE_C1_RES, passthrough MSRs are costly to support because KVM must context switch the MSR (which, by the by, is completely missing from the patch).

Quoted 7 lines
Quoted 4 lines

In other words, if these MSRs are full RW passthrough, guests with XFD enabled will need to load the guest value on entry, save the guest value on exit, and load the host value on exit. That's in the neighborhood of a 40% increase in latency for a single VM-Enter/VM-Exit roundtrip (~1500 cycles => >2000 cycles).

I'm not taking a position as to whether these _should_ be passthrough or not. But, if they are, I don't think you strictly need to do the RDMSR/WRMSR at VM-Exit time.

Hi Dave,

Thanks for reviewing the patches.

When vmexit, clearing XFD (because KVM thinks guest has requested AMX) can be deferred to the time when host does XSAVES, but this means need a new flag in common "fpu" structure or a common macro per thread which works only dedicated for KVM case, and check the flag in 1) switch_fpu_prepare()
2) kernel_fpu_begin() . This is the concern to me.

Thanks,
Jing

Quoted 13 lines

Just like the "FPU", XFD isn't be used in normal kernel code. This is why we can be lazy about FPU state with TIF_NEED_FPU_LOAD. I _suspect_ that some XFD manipulation can be at least deferred to the same place where the FPU state is manipulated: places like switch_fpu_return() or kernel_fpu_begin().

Doing that would at least help the fast VM-Exit/VM-Enter paths that really like TIF_NEED_FPU_LOAD today.

I guess the nasty part is that you actually need to stash the old XFD MSR value in the vcpu structure and that's not available at context-switch time. So, maybe this would only allow deferring the WRMSR. That's better than nothing I guess.

#10 lore raw

Re: [PATCH RFC 2/7] kvm: x86: Introduce XFD MSRs as passthrough to guest

Quoted 20 lines · On 6/27/21 7:00 PM, Liu, Jing2 wrote:
Quoted 13 lines · Dave Hansen
Quoted 12 lines · Sean Christopherson
Quoted 1 line · Jing Liu

Passthrough both MSRs to let guest access and write without vmexit.

Why? Except for read-only MSRs, e.g. MSR_CORE_C1_RES, passthrough MSRs are costly to support because KVM must context switch the MSR (which, by the by, is completely missing from the patch).

In other words, if these MSRs are full RW passthrough, guests with XFD enabled will need to load the guest value on entry, save the guest value on exit, and load the host value on exit. That's in the neighborhood of a 40% increase in latency for a single VM-Enter/VM-Exit roundtrip (~1500 cycles =>

Quoted 4 lines
Quoted 1 line
Quoted 1 line

2000 cycles).

I'm not taking a position as to whether these _should_ be passthrough or not.  But, if they are, I don't think you strictly need to do the RDMSR/WRMSR at VM-Exit time.

Hi Dave,

Quoted 7 lines

Thanks for reviewing the patches.

When vmexit, clearing XFD (because KVM thinks guest has requested AMX) can be deferred to the time when host does XSAVES, but this means need a new flag in common "fpu" structure or a common macro per thread which works only dedicated for KVM case, and check the flag in 1) switch_fpu_prepare()
2) kernel_fpu_begin() . This is the concern to me.

Why is this a concern? You're worried about finding a single bit worth of space somewhere?

author
#11 lore raw

Re: [PATCH RFC 2/7] kvm: x86: Introduce XFD MSRs as passthrough to guest

Quoted 23 lines · Dave Hansen
Quoted 22 lines · On 6/27/21 7:00 PM, Liu, Jing2 wrote:
Quoted 13 lines · Dave Hansen
Quoted 12 lines · Sean Christopherson
Quoted 1 line · Jing Liu

Passthrough both MSRs to let guest access and write without vmexit.

Why? Except for read-only MSRs, e.g. MSR_CORE_C1_RES, passthrough MSRs are costly to support because KVM must context switch the MSR (which, by the by, is completely missing from the patch).

In other words, if these MSRs are full RW passthrough, guests with XFD enabled will need to load the guest value on entry, save the guest value on exit, and load the host value on exit. That's in the neighborhood of a 40% increase in latency for a single VM-Enter/VM-Exit roundtrip (~1500 cycles =>

Quoted 4 lines
Quoted 1 line
Quoted 1 line

2000 cycles).

I'm not taking a position as to whether these _should_ be passthrough or not.  But, if they are, I don't think you strictly need to do the RDMSR/WRMSR at VM-Exit time.

Hi Dave,

Thanks for reviewing the patches.

Quoted 7 lines
Quoted 5 lines

When vmexit, clearing XFD (because KVM thinks guest has requested AMX) can be deferred to the time when host does XSAVES, but this means need a new flag in common "fpu" structure or a common macro per thread which works only dedicated for KVM case, and check the flag in 1) switch_fpu_prepare()
2) kernel_fpu_begin() . This is the concern to me.

Why is this a concern? You're worried about finding a single bit worth of space somewhere?

A bit of flag can be found so far though the space is somehow nervous. What I am worrying about is, we introduce a flag per thread and add the check in core place like softirq path and context switch path, to handle a case only for KVM thread + XFD=1 + AMX usage in guest. This is not a quite frequent case but we need check every time for every thread.

I am considering using XGETBV(1) (~24 cycles) to detect if KVM really need wrmsr(0) to clear XFD for guest AMX state when vmexit. And this is not a quite frequent case I think. Only one concern is, does/will kernel check somewhere that thread's memory fpu buffer is already large but thread's XFD=1? (I believe not)

Thanks,
Jing

Quoted 1 line
author
#12 lore raw

RFC PATCH 3/7 kvm: x86: XSAVE state and XFD MSRs context switch

In reply to Jing Liu

XFD allows the kernel to enable a feature state in XCR0 and to receive a #NM trap when a task uses instructions accessing that state. Kernel defines "struct fpu.state_mask" to indicate the saved xstate and interact with the XFD hardware when needed via a simple conversion. Once a dynamic feature is detected, "state_mask" is expanded and "state_ptr" is dynamically allocated to hold the whole state. Meanwhile once the state is not in INIT state, the corresponding XFD bit should not be armed anymore.

In KVM, "guest_fpu" serves for any guest task working on this vcpu during vmexit and vmenter. We provide a pre-allocated guest_fpu space and entire "guest_fpu.state_mask" to avoid each dynamic features detection on each vcpu task. Meanwhile, to ensure correctly xsaves/xrstors guest state, set IA32_XFD as zero during vmexit and vmenter.

For "current->thread.fpu", since host and guest probably have different state and mask, it also need be switched to the right context when fpu load and put.

---
 arch/x86/include/asm/kvm_host.h |  3 ++
 arch/x86/kernel/fpu/init.c      |  1 +
 arch/x86/kernel/fpu/xstate.c    |  2 +
 arch/x86/kvm/vmx/vmx.c          | 76 +++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.h          |  1 +
 arch/x86/kvm/x86.c              | 69 +++++++++++++++++++++++++-----
 6 files changed, 141 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
index 7e5f33a0d0e2..6dedf3d22659 100644
@@ -1203,6 +1203,9 @@ struct kvm_x86_ops {
 			       struct x86_exception *exception);
 	void (*handle_exit_irqoff)(struct kvm_vcpu *vcpu);
 
+	void (*xfd_load)(struct kvm_vcpu *vcpu);
+	void (*xfd_put)(struct kvm_vcpu *vcpu);
+
 	void (*request_immediate_exit)(struct kvm_vcpu *vcpu);
 
 	void (*sched_in)(struct kvm_vcpu *kvm, int cpu);
arch/x86/kernel/fpu/init.c +1 -0
index 7e0c68043ce3..fbb761fc13ec 100644
@@ -145,6 +145,7 @@ EXPORT_SYMBOL_GPL(fpu_kernel_xstate_min_size);
  * can be dynamically expanded to include some states up to this size.
  */
 unsigned int fpu_kernel_xstate_max_size;
+EXPORT_SYMBOL_GPL(fpu_kernel_xstate_max_size);
 
 /* Get alignment of the TYPE. */
 #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)
arch/x86/kernel/fpu/xstate.c +2 -0
index 080f3be9a5e6..9c471a0364e2 100644
@@ -77,12 +77,14 @@ static struct xfeature_capflag_info xfeature_capflags[] __initdata = {
  * XSAVE buffer, both supervisor and user xstates.
  */
 u64 xfeatures_mask_all __read_mostly;
+EXPORT_SYMBOL_GPL(xfeatures_mask_all);
 
 /*
  * This represents user xstates, a subset of xfeatures_mask_all, saved in a
  * dynamic kernel XSAVE buffer.
  */
 u64 xfeatures_mask_user_dynamic __read_mostly;
+EXPORT_SYMBOL_GPL(xfeatures_mask_user_dynamic);
 
 static unsigned int xstate_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
 static unsigned int xstate_sizes[XFEATURE_MAX]   = { [ 0 ... XFEATURE_MAX - 1] = -1};
arch/x86/kvm/vmx/vmx.c +76 -0

100 lines not on this page · raw message

arch/x86/kvm/vmx/vmx.h +1 -0
index d487f5a53a08..9a9ea37a29b1 100644
@@ -288,6 +288,7 @@ struct vcpu_vmx {
 	} shadow_msr_intercept;
 
 	/* eXtended Feature Disabling (XFD) MSRs */
+	u64 host_ia32_xfd;
 	u64 msr_ia32_xfd;
 	u64 msr_ia32_xfd_err;
 };
arch/x86/kvm/x86.c +58 -11

132 lines not on this page · raw message

Signature · 1 line
2.18.4
maintainer
#13 lore raw

Re: [PATCH RFC 3/7] kvm: x86: XSAVE state and XFD MSRs context switch

Quoted 21 lines · Jing Liu
arch/x86/kernel/fpu/init.c +1 -0
index 7e0c68043ce3..fbb761fc13ec 100644
@@ -145,6 +145,7 @@ EXPORT_SYMBOL_GPL(fpu_kernel_xstate_min_size);
  * can be dynamically expanded to include some states up to this size.
  */
 unsigned int fpu_kernel_xstate_max_size;
+EXPORT_SYMBOL_GPL(fpu_kernel_xstate_max_size);
 
 /* Get alignment of the TYPE. */
 #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)
arch/x86/kernel/fpu/xstate.c +1 -0
index 080f3be9a5e6..9c471a0364e2 100644
@@ -77,12 +77,14 @@ static struct xfeature_capflag_info xfeature_capflags[] __initdata = {
  * XSAVE buffer, both supervisor and user xstates.
  */
 u64 xfeatures_mask_all __read_mostly;
+EXPORT_SYMBOL_GPL(xfeatures_mask_all);

arch/x86/kernel/fpu/xstate.c static struct xfeature_capflag_info xfeature_capflags[] __initdata = { · line 90

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +1 -0
 
 /*
  * This represents user xstates, a subset of xfeatures_mask_all, saved in a
  * dynamic kernel XSAVE buffer.
  */
 u64 xfeatures_mask_user_dynamic __read_mostly;
+EXPORT_SYMBOL_GPL(xfeatures_mask_user_dynamic);
 
 static unsigned int xstate_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
 static unsigned int xstate_sizes[XFEATURE_MAX]   = { [ 0 ... XFEATURE_MAX - 1] = -1};

Make sure you Cc [email protected] when touching code outside of kvm.

There's this script called scripts/get_maintainer.pl which will tell you who to Cc. Use it before you send next time please.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#14 lore raw

Re: [PATCH RFC 3/7] kvm: x86: XSAVE state and XFD MSRs context switch

Quoted 30 lines · Borislav Petkov
Quoted 29 lines · Jing Liu
arch/x86/kernel/fpu/init.c +1 -0
index 7e0c68043ce3..fbb761fc13ec 100644
@@ -145,6 +145,7 @@ EXPORT_SYMBOL_GPL(fpu_kernel_xstate_min_size);
   * can be dynamically expanded to include some states up to this size.
   */
  unsigned int fpu_kernel_xstate_max_size;
+EXPORT_SYMBOL_GPL(fpu_kernel_xstate_max_size);
 
  /* Get alignment of the TYPE. */
  #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)
arch/x86/kernel/fpu/xstate.c +2 -0
index 080f3be9a5e6..9c471a0364e2 100644
@@ -77,12 +77,14 @@ static struct xfeature_capflag_info xfeature_capflags[] __initdata = {
   * XSAVE buffer, both supervisor and user xstates.
   */
  u64 xfeatures_mask_all __read_mostly;
+EXPORT_SYMBOL_GPL(xfeatures_mask_all);
 
  /*
   * This represents user xstates, a subset of xfeatures_mask_all, saved in a
   * dynamic kernel XSAVE buffer.
   */
  u64 xfeatures_mask_user_dynamic __read_mostly;
+EXPORT_SYMBOL_GPL(xfeatures_mask_user_dynamic);
 
Quoted 8 lines

arch/x86/kernel/fpu/xstate.c static struct xfeature_capflag_info xfeature_capflags[] __initdata = { · line 90

Quoted 2 lines
arch/x86/kernel/fpu/xstate.c +0 -0
  static unsigned int xstate_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
  static unsigned int xstate_sizes[XFEATURE_MAX]   = { [ 0 ... XFEATURE_MAX - 1] = -1};

Make sure you Cc [email protected] when touching code outside of kvm.

There's this script called scripts/get_maintainer.pl which will tell you who to Cc. Use it before you send next time please.

Thx.

Thank you for the reminder. I'll cc that next time.

BRs,
Jing

maintainer
#15 lore raw

Re: [PATCH RFC 3/7] kvm: x86: XSAVE state and XFD MSRs context switch

In reply to Jing Liu
Quoted 1 line · Jing Liu

|In KVM, "guest_fpu" serves for any guest task working on this vcpu during vmexit and vmenter. We provide a pre-allocated guest_fpu space and entire "guest_fpu.state_mask" to avoid each dynamic features detection on each vcpu task. Meanwhile, to ensure correctly xsaves/xrstors guest state, set IA32_XFD as zero during vmexit and vmenter.|

Most guests will not need the whole xstate feature set. So perhaps you
could set XFD to the host value | the guest value, trap #NM if the
host XFD is zero, and possibly reflect the exception to the guest's XFD and XFD_ERR.

In addition, loading the guest XFD MSRs should use the MSR autoload feature (add_atomic_switch_msr).

Paolo

#16 lore raw

Re: [PATCH RFC 3/7] kvm: x86: XSAVE state and XFD MSRs context switch

Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Jing Liu

|In KVM, "guest_fpu" serves for any guest task working on this vcpu during vmexit and vmenter. We provide a pre-allocated guest_fpu space and entire "guest_fpu.state_mask" to avoid each dynamic features detection on each vcpu task. Meanwhile, to ensure correctly xsaves/xrstors guest state, set IA32_XFD as zero during vmexit and vmenter.|

Quoted 6 lines

Most guests will not need the whole xstate feature set. So perhaps you could set XFD to the host value | the guest value, trap #NM if the host XFD is zero, and possibly reflect the exception to the guest's XFD and XFD_ERR.

In addition, loading the guest XFD MSRs should use the MSR autoload feature (add_atomic_switch_msr).

Why do you say that? I would strongly prefer to use the load lists only if they are absolutely necessary. I don't think that's the case here, as I can't imagine accessing FPU state in NMI context is allowed, at least not without a big pile of save/restore code.

maintainer
#17 lore raw

Re: [PATCH RFC 3/7] kvm: x86: XSAVE state and XFD MSRs context switch

Quoted 13 lines · Sean Christopherson
Quoted 12 lines · Paolo Bonzini
Quoted 6 lines · Jing Liu

In KVM, "guest_fpu" serves for any guest task working on this vcpu during vmexit and vmenter. We provide a pre-allocated guest_fpu space and entire "guest_fpu.state_mask" to avoid each dynamic features detection on each vcpu task. Meanwhile, to ensure correctly xsaves/xrstors guest state, set IA32_XFD as zero during vmexit and vmenter.

Most guests will not need the whole xstate feature set. So perhaps you could set XFD to the host value | the guest value, trap #NM if the host XFD is zero, and possibly reflect the exception to the guest's XFD and XFD_ERR.

Quoted 7 lines
Quoted 2 lines

In addition, loading the guest XFD MSRs should use the MSR autoload feature (add_atomic_switch_msr).

Why do you say that? I would strongly prefer to use the load lists only if they are absolutely necessary. I don't think that's the case here, as I can't imagine accessing FPU state in NMI context is allowed, at least not without a big pile of save/restore code.

I was thinking more of the added vmentry/vmexit overhead due to xfd_guest_enter xfd_guest_exit.

That said, the case where we saw MSR autoload as faster involved EFER, and we decided that it was due to TLB flushes (commit f6577a5fa15d, "x86, kvm, vmx: Always use LOAD_IA32_EFER if available", 2014-11-12). Do you know if RDMSR/WRMSR is always slower than MSR autoload?

Paolo

#18 lore raw

Re: [PATCH RFC 3/7] kvm: x86: XSAVE state and XFD MSRs context switch

Quoted 22 lines · Paolo Bonzini
Quoted 13 lines · Sean Christopherson
Quoted 12 lines · Paolo Bonzini
Quoted 6 lines · Jing Liu

In KVM, "guest_fpu" serves for any guest task working on this vcpu during vmexit and vmenter. We provide a pre-allocated guest_fpu space and entire "guest_fpu.state_mask" to avoid each dynamic features detection on each vcpu task. Meanwhile, to ensure correctly xsaves/xrstors guest state, set IA32_XFD as zero during vmexit and vmenter.

Most guests will not need the whole xstate feature set. So perhaps you could set XFD to the host value | the guest value, trap #NM if the host XFD is zero, and possibly reflect the exception to the guest's XFD and XFD_ERR.

Quoted 7 lines
Quoted 2 lines

In addition, loading the guest XFD MSRs should use the MSR autoload feature (add_atomic_switch_msr).

Why do you say that? I would strongly prefer to use the load lists only if they are absolutely necessary. I don't think that's the case here, as I can't imagine accessing FPU state in NMI context is allowed, at least not without a big pile of save/restore code.

Quoted 7 lines

I was thinking more of the added vmentry/vmexit overhead due to xfd_guest_enter xfd_guest_exit.

That said, the case where we saw MSR autoload as faster involved EFER, and we decided that it was due to TLB flushes (commit f6577a5fa15d, "x86, kvm,
vmx: Always use LOAD_IA32_EFER if available", 2014-11-12). Do you know if RDMSR/WRMSR is always slower than MSR autoload?

RDMSR/WRMSR may be marginally slower, but only because the autoload stuff avoids serializing the pipeline after every MSR. The autoload paths are effectively just wrappers around the WRMSR ucode, plus some extra VM-Enter specific checks, as ucode needs to perform all the normal fault checks on the index and value.

On the flip side, if the load lists are dynamically constructed, I suspect the code overhead of walking the lists negates any advantages of the load lists.

TL;DR: it likely depends on the exact use case. My primary objection to using the load lists is that people tend to assume they are more performant that raw RDMSR/WRMSR, and so aren't as careful/thoughtful as they should be about adding MSRs to the save/restore paths.

Note, the dedicated VMCS fields, e.g. EFER and SYSENTER, are 1-2 orders of magnitude faster than raw RDMSR/WRMSR or the load lists, as they obviously have dedicated handling in VM-Enter ucode.

maintainer
#19 lore raw

Re: [PATCH RFC 3/7] kvm: x86: XSAVE state and XFD MSRs context switch

Quoted 6 lines · Sean Christopherson
Quoted 4 lines

That said, the case where we saw MSR autoload as faster involved EFER, and we decided that it was due to TLB flushes (commit f6577a5fa15d, "x86, kvm,
vmx: Always use LOAD_IA32_EFER if available", 2014-11-12). Do you know if RDMSR/WRMSR is always slower than MSR autoload?

RDMSR/WRMSR may be marginally slower, but only because the autoload stuff avoids serializing the pipeline after every MSR.

That's probably adding up quickly...

Quoted 5 lines

The autoload paths are effectively
just wrappers around the WRMSR ucode, plus some extra VM-Enter specific checks, as ucode needs to perform all the normal fault checks on the index and value. On the flip side, if the load lists are dynamically constructed, I suspect the code overhead of walking the lists negates any advantages of the load lists.

... but yeah this is not very encouraging.

Context switch time is a problem for XFD. In a VM that uses AMX, most threads in the guest will have nonzero XFD but the vCPU thread itself will have zero XFD. So as soon as one thread in the VM forces the vCPU thread to clear XFD, you pay a price on all vmexits and vmentries.

However, running the host with _more_ bits set than necessary in XFD should not be a problem as long as the host doesn't use the AMX instructions. So perhaps Jing can look into keeping XFD=0 for as little time as possible, and XFD=host_XFD|guest_XFD as much as possible.

Paolo

#20 lore raw

Re: [PATCH RFC 3/7] kvm: x86: XSAVE state and XFD MSRs context switch

Quoted 23 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson
Quoted 4 lines

That said, the case where we saw MSR autoload as faster involved EFER, and we decided that it was due to TLB flushes (commit f6577a5fa15d, "x86, kvm,
vmx: Always use LOAD_IA32_EFER if available", 2014-11-12). Do you know if RDMSR/WRMSR is always slower than MSR autoload?

RDMSR/WRMSR may be marginally slower, but only because the autoload stuff avoids serializing the pipeline after every MSR.

That's probably adding up quickly...

Quoted 5 lines

The autoload paths are effectively
just wrappers around the WRMSR ucode, plus some extra VM-Enter specific checks, as ucode needs to perform all the normal fault checks on the index and value. On the flip side, if the load lists are dynamically constructed, I suspect the code overhead of walking the lists negates any advantages of the load lists.

... but yeah this is not very encouraging.

Context switch time is a problem for XFD. In a VM that uses AMX, most threads in the guest will have nonzero XFD but the vCPU thread itself will have zero XFD. So as soon as one thread in the VM forces the vCPU thread to clear XFD, you pay a price on all vmexits and vmentries.

Quoted 4 lines

However, running the host with _more_ bits set than necessary in XFD should not be a problem as long as the host doesn't use the AMX instructions. So perhaps Jing can look into keeping XFD=0 for as little time as possible, and XFD=host_XFD|guest_XFD as much as possible.

This sounds like the lazy-fpu (eagerfpu?) that used to be part of the kernel? I recall that we had a CVE for that - so it may also be worth double-checking that we don't reintroduce that one.

author
#21 lore raw

Re: [PATCH RFC 3/7] kvm: x86: XSAVE state and XFD MSRs context switch

Quoted 1 line · Konrad Rzeszutek Wilk

On Mon, Feb 08, 2021 at 07:12:22PM +0100, Paolo Bonzini wrote:

[...]

Quoted 8 lines
Quoted 5 lines

However, running the host with _more_ bits set than necessary in XFD should not be a problem as long as the host doesn't use the AMX instructions. So perhaps Jing can look into keeping XFD=0 for as little time as possible, and XFD=host_XFD|guest_XFD as much as possible.

This sounds like the lazy-fpu (eagerfpu?) that used to be part of the kernel? I recall that we had a CVE for that - so it may also be worth double-checking that we don't reintroduce that one.

Not sure if this means lazy restore, but the spec mentions that "System software should not use XFD to implement a 'lazy restore' approach to management of the XTILEDATA state component." One reason is XSAVE(S) will lose the xTILEDATA when XFD[i] is nonzero.

Thanks,
Jing

author
#22 lore raw

Re: [PATCH RFC 3/7] kvm: x86: XSAVE state and XFD MSRs context switch

In reply to Paolo Bonzini
Quoted 10 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson
Quoted 4 lines

That said, the case where we saw MSR autoload as faster involved EFER, and we decided that it was due to TLB flushes (commit f6577a5fa15d, "x86, kvm,
vmx: Always use LOAD_IA32_EFER if available", 2014-11-12). Do you know if RDMSR/WRMSR is always slower than MSR autoload?

RDMSR/WRMSR may be marginally slower, but only because the autoload stuff avoids serializing the pipeline after every MSR.

That's probably adding up quickly...

Quoted 7 lines
Quoted 5 lines

The autoload paths are effectively
just wrappers around the WRMSR ucode, plus some extra VM-Enter specific checks, as ucode needs to perform all the normal fault checks on the index and value. On the flip side, if the load lists are dynamically constructed, I suspect the code overhead of walking the lists negates any advantages of the load lists.

... but yeah this is not very encouraging.

Thanks for reviewing the patches.

Quoted 2 lines

Context switch time is a problem for XFD.  In a VM that uses AMX, most threads in the guest will have nonzero XFD but the vCPU thread itself will have zero XFD.  So as soon as one thread in the VM forces the vCPU thread to clear XFD, you pay a price on all vmexits and vmentries.

Spec says,
"If XSAVE, XSAVEC, XSAVEOPT, or XSAVES is saving the state component i, the instruction does not generate #NM when XCR0[i] = IA32_XFD[i] = 1; instead, it saves bit i of XSTATE_BV field of the XSAVE header as 0 (indicating that the state component is in its initialized state). With the exception of XSAVE, no data is saved for the state component (XSAVE saves the initial value of the state component..."

Thus, the key point is not losing the non initial AMX state on vmexit and vmenter. If AMX state is in initialized state, it doesn't matter. Otherwise, XFD[i] should not be armed with a nonzero value.

If we don't want to extremely set XFD=0 every time on vmexit, it would be useful to first detect if guest AMX state is initial or not. How about using XINUSE notation here?
(Details in SDM vol1 13.6 PROCESSOR TRACKING OF XSAVE-MANAGED STATE, and vol2 XRSTOR/XRSTORS instruction operation part) The main idea is processor tracks the status of various state components by XINUSE, and it shows if the state component is in use or not. When XINUSE[i]=0, state component i is in initial configuration. Otherwise, kvm should take care of XFD on vmexit.

Quoted 1 line

However, running the host with _more_ bits set than necessary in XFD should not be a problem as long as the host doesn't use the AMX instructions.

Does "running the host" mean running in kvm? why need more bits (host_XFD|guest_XFD), I'm trying to think about the case that guest_XFD is not enough? e.g. In guest, it only need bit i when guest supports it and guest uses
the passthru XFD[i] for detecting dynamic usage;
In kvm, kvm doesn't use AMX instructions; and "system software should not use XFD to implement a 'lazy restore' approach to management of the XTILEDATA state component."
Out of kvm, kernel ensures setting correct XFD for threads when scheduling;

Thanks,
Jing

Quoted 4 lines

So perhaps Jing can look into keeping XFD=0 for as little time as possible, and XFD=host_XFD|guest_XFD as much as possible.

Paolo

author
#23 lore raw

RFC PATCH 4/7 kvm: x86: Add new ioctls for XSAVE extension

In reply to Jing Liu

The static xstate buffer kvm_xsave contains the extended register states, but it is not enough for dynamic features with large state.

Introduce a new capability called KVM_CAP_X86_XSAVE_EXTENSION to detect if hardware has XSAVE extension (XFD). Meanwhile, add two new ioctl interfaces to get/set the whole xstate using struct kvm_xsave_extension buffer containing both static and dynamic xfeatures. Reuse fill_xsave and load_xsave for both cases.

---
 arch/x86/include/uapi/asm/kvm.h |  5 +++
 arch/x86/kvm/x86.c              | 70 +++++++++++++++++++++++++--------
 include/uapi/linux/kvm.h        |  8 ++++
 3 files changed, 66 insertions(+), 17 deletions(-)
arch/x86/include/uapi/asm/kvm.h +5 -0
index 89e5f3d1bba8..bf785e89a728 100644
@@ -362,6 +362,11 @@ struct kvm_xsave {
 	__u32 region[1024];
 };
 
+/* for KVM_CAP_XSAVE_EXTENSION */
+struct kvm_xsave_extension {
+	__u32 region[3072];
+};
+
 #define KVM_MAX_XCRS	16
 
 struct kvm_xcr {
arch/x86/kvm/x86.c +53 -17

150 lines not on this page · raw message

include/uapi/linux/kvm.h +8 -0
index ca41220b40b8..42a167a29350 100644
@@ -1053,6 +1053,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_X86_USER_SPACE_MSR 188
 #define KVM_CAP_X86_MSR_FILTER 189
 #define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190
+#define KVM_CAP_X86_XSAVE_EXTENSION 191
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -1462,6 +1463,13 @@ struct kvm_s390_ucas_mapping {
 /* Available with KVM_CAP_XSAVE */
 #define KVM_GET_XSAVE		  _IOR(KVMIO,  0xa4, struct kvm_xsave)
 #define KVM_SET_XSAVE		  _IOW(KVMIO,  0xa5, struct kvm_xsave)
+/*
+ * Available with KVM_CAP_XSAVE_EXTENSION
+ * 0xa4/0xa5 are ok for the new ioctls since structure size is different.
+ */
+#define KVM_GET_XSAVE_EXTENSION   _IOW(KVMIO,  0xa4, struct kvm_xsave_extension)
+#define KVM_SET_XSAVE_EXTENSION   _IOW(KVMIO,  0xa5, struct kvm_xsave_extension)
+
 /* Available with KVM_CAP_XCRS */
 #define KVM_GET_XCRS		  _IOR(KVMIO,  0xa6, struct kvm_xcrs)
 #define KVM_SET_XCRS		  _IOW(KVMIO,  0xa7, struct kvm_xcrs)
Signature · 1 line
2.18.4
#24 lore raw

Re: [PATCH RFC 4/7] kvm: x86: Add new ioctls for XSAVE extension

Quoted 21 lines · Jing Liu

The static xstate buffer kvm_xsave contains the extended register states, but it is not enough for dynamic features with large state.

Introduce a new capability called KVM_CAP_X86_XSAVE_EXTENSION to detect if hardware has XSAVE extension (XFD). Meanwhile, add two new ioctl interfaces to get/set the whole xstate using struct kvm_xsave_extension buffer containing both static and dynamic xfeatures. Reuse fill_xsave and load_xsave for both cases.

---
 arch/x86/include/uapi/asm/kvm.h |  5 +++
 arch/x86/kvm/x86.c              | 70 +++++++++++++++++++++++++--------
 include/uapi/linux/kvm.h        |  8 ++++
 3 files changed, 66 insertions(+), 17 deletions(-)
arch/x86/include/uapi/asm/kvm.h +0 -0
index 89e5f3d1bba8..bf785e89a728 100644

arch/x86/include/uapi/asm/kvm.h struct kvm_xsave { · line 367

Quoted 6 lines
arch/x86/include/uapi/asm/kvm.h +3 -0
@@ -362,6 +362,11 @@ struct kvm_xsave {
 	__u32 region[1024];
 };
 
+/* for KVM_CAP_XSAVE_EXTENSION */
+struct kvm_xsave_extension {
+	__u32 region[3072];

Fool me once, shame on you (Intel). Fool me twice, shame on me (KVM).

As amusing as kvm_xsave_really_extended would be, the required size should be discoverable, not hardcoded. Nothing prevents a hardware vendor from inventing a newfangled feature that requires yet more space.

As an alternative to adding a dedicated capability, can we leverage GET_SUPPORTED_CPUID, leaf CPUID.0xD, to enumerate the minimum required size and state that the new ioctl() is available if the min size is greater than 1024? Or is that unnecessarily convoluted...

author
#25 lore raw

Re: [PATCH RFC 4/7] kvm: x86: Add new ioctls for XSAVE extension

Quoted 25 lines · Sean Christopherson
Quoted 24 lines · Jing Liu

The static xstate buffer kvm_xsave contains the extended register states, but it is not enough for dynamic features with large state.

Introduce a new capability called KVM_CAP_X86_XSAVE_EXTENSION to detect if hardware has XSAVE extension (XFD). Meanwhile, add two new ioctl interfaces to get/set the whole xstate using struct kvm_xsave_extension buffer containing both static and dynamic xfeatures. Reuse fill_xsave and load_xsave for both cases.

---
  arch/x86/include/uapi/asm/kvm.h |  5 +++
  arch/x86/kvm/x86.c              | 70 +++++++++++++++++++++++++--------
  include/uapi/linux/kvm.h        |  8 ++++
  3 files changed, 66 insertions(+), 17 deletions(-)
arch/x86/include/uapi/asm/kvm.h +0 -0
index 89e5f3d1bba8..bf785e89a728 100644
@@ -362,6 +362,11 @@ struct kvm_xsave {
  	__u32 region[1024];
  };
 
Quoted 7 lines

arch/x86/include/uapi/asm/kvm.h struct kvm_xsave { · line 367

Quoted 3 lines
arch/x86/include/uapi/asm/kvm.h +3 -0
+/* for KVM_CAP_XSAVE_EXTENSION */
+struct kvm_xsave_extension {
+	__u32 region[3072];

Fool me once, shame on you (Intel). Fool me twice, shame on me (KVM).

As amusing as kvm_xsave_really_extended would be, the required size should be discoverable, not hardcoded.

Thanks for reviewing the patch.
When looking at current kvm_xsave structure, I felt confusing about the static hardcoding of 1024 bytes, but failed to find clue for its final decision in 2010[1]. So we'd prefer to changing the way right? Please correct me if I misunderstood.

Quoted 4 lines

Nothing prevents a hardware vendor from inventing
a newfangled feature that requires yet more space.
As an alternative to adding a dedicated capability, can we leverage GET_SUPPORTED_CPUID, leaf CPUID.0xD,

Yes, this is a good way to avoid a dedicated capability. Thanks for the suggestion.
Use 0xD.1.EBX for size of enabled xcr0|xss if supposing kvm_xsave cares both.

Quoted 2 lines

to enumerate the minimum required size and
state

For the state, an extreme case is using an old qemu as follows, but a new kvm with more future_featureZ supported. If hardware vendor arranges one by one, it's OK to use static state like X86XSaveArea(2) and get/set between userspace and kvm because it's non-compacted. If not, the state will not correct.
So far it is OK, so I'm wondering if this would be an issue for now?

X86XSaveArea2 {
    ...
    XSaveAVX
    ...
    AMX_XTILE;
    future_featureX;
    future_featureY;
}

Quoted 2 lines

that the new ioctl() is available if the min size is greater than 1024?
Or is that unnecessarily convoluted...

To enable a dynamic size kvm_xsave2(Thanks Jim's name suggestion), if things as follows are what we might want.
/* for xstate large than 1024 */
struct kvm_xsave2 {
    int size; // size of the whole xstate
    void *ptr; // xstate pointer
}
#define KVM_GET_XSAVE2   _IOW(KVMIO,  0xa4, struct kvm_xsave2)

Take @size together, so KVM need not fetch 0xd.1.ebx each time or a dedicated variable.

For Userspace(Qemu):
struct X86XSaveArea2 {...}// new struct holding all features

if 0xd.1.ebx <= sizeof(kvm_xsave) env->xsave_buf = alloc(sizeof(kvm_xsave))
    ...
    ioctl(KVM_GET/SET_XSAVE, X86XSaveArea *) else
    env->xsave_buf = alloc(0xd.1.ebx + sizeof(int))
    ...
    xsave2 = env->xsave_buf
    xsave2->size = ...
X86XSaveArea2 *area2 = xsave2->ptr ioctl(KVM_GET/SET_XSAVE2, xsave2)
endif

[1] https://lore.kernel.org/kvm/[email protected]/

Thanks,
Jing

#26 lore raw

Re: [PATCH RFC 4/7] kvm: x86: Add new ioctls for XSAVE extension

Quoted 19 lines · On Wed, May 26, 2021, Liu, Jing2 wrote:
Quoted 17 lines · Sean Christopherson
Quoted 16 lines · Jing Liu

The static xstate buffer kvm_xsave contains the extended register states, but it is not enough for dynamic features with large state.

Introduce a new capability called KVM_CAP_X86_XSAVE_EXTENSION to detect if hardware has XSAVE extension (XFD). Meanwhile, add two new ioctl interfaces to get/set the whole xstate using struct kvm_xsave_extension buffer containing both static and dynamic xfeatures. Reuse fill_xsave and load_xsave for both cases.

---
  arch/x86/include/uapi/asm/kvm.h |  5 +++
  arch/x86/kvm/x86.c              | 70 +++++++++++++++++++++++++--------
  include/uapi/linux/kvm.h        |  8 ++++
  3 files changed, 66 insertions(+), 17 deletions(-)
Quoted 6 lines
Quoted 6 lines

arch/x86/include/uapi/asm/kvm.h struct kvm_xsave { · line 362

Quoted 6 lines
arch/x86/include/uapi/asm/kvm.h +0 -0
index 89e5f3d1bba8..bf785e89a728 100644
@@ -362,6 +362,11 @@ struct kvm_xsave {
  	__u32 region[1024];

Hold up a sec. How big is the AMX data? The existing size is 4096 bytes, not 1024 bytes. IIRC, AMX is >4k, so we still need a new ioctl(), but we should be careful to mentally adjust for the __u32 when mentioning the sizes.

Quoted 11 lines
Quoted 8 lines

arch/x86/include/uapi/asm/kvm.h struct kvm_xsave { · line 367

Quoted 4 lines
arch/x86/include/uapi/asm/kvm.h +3 -0
  };
+/* for KVM_CAP_XSAVE_EXTENSION */
+struct kvm_xsave_extension {
+	__u32 region[3072];

Fool me once, shame on you (Intel). Fool me twice, shame on me (KVM).

As amusing as kvm_xsave_really_extended would be, the required size should be discoverable, not hardcoded.

Thanks for reviewing the patch. When looking at current kvm_xsave structure, I felt confusing about the static hardcoding of 1024 bytes, but failed to find clue for its final decision in 2010[1].

Simplicitly and lack of foresight :-)

Quoted 1 line

So we'd prefer to changing the way right? Please correct me if I misunderstood.

Sadly, we can't fix the existing ioctl() without breaking userspace. But for the new ioctl(), yes, its size should not be hardcoded.

Quoted 6 lines
Quoted 3 lines

Nothing prevents a hardware vendor from inventing a newfangled feature that requires yet more space. As an alternative to adding a dedicated capability, can we leverage GET_SUPPORTED_CPUID, leaf CPUID.0xD,

Yes, this is a good way to avoid a dedicated capability. Thanks for the suggestion. Use 0xD.1.EBX for size of enabled xcr0|xss if supposing kvm_xsave cares both.

Quoted 7 lines
Quoted 1 line

to enumerate the minimum required size and state

For the state, an extreme case is using an old qemu as follows, but a new kvm with more future_featureZ supported. If hardware vendor arranges one by one, it's OK to use static state like X86XSaveArea(2) and get/set between userspace and kvm because it's non-compacted. If not, the state will not correct.
So far it is OK, so I'm wondering if this would be an issue for now?

Oh, you're saying that, because kvm_xsave is non-compacted, future features may overflow kvm_xsave simply because the architectural offset overflows 4096 bytes.

That should be a non-issue for old KVM/kernels, since the new features shouldn't be enabled. For new KVM, I think the right approach is to reject KVM_GET_XSAVE and KVM_SET_XSAVE if the required size is greater than sizeof(struct kvm_xsave). I.e. force userspace to either hide the features from the guest, or use KVM_{G,S}ET_XSAVE2.

Quoted 20 lines

X86XSaveArea2 {
    ...
    XSaveAVX
    ...
    AMX_XTILE;
    future_featureX;
    future_featureY;
}

Quoted 2 lines

that the new ioctl() is available if the min size is greater than 1024?
Or is that unnecessarily convoluted...

To enable a dynamic size kvm_xsave2(Thanks Jim's name suggestion), if things as follows are what we might want.
/* for xstate large than 1024 */
struct kvm_xsave2 {
    int size; // size of the whole xstate
    void *ptr; // xstate pointer
}
#define KVM_GET_XSAVE2   _IOW(KVMIO,  0xa4, struct kvm_xsave2)

Quoted 2 lines

Take @size together, so KVM need not fetch 0xd.1.ebx each time or a dedicated variable.

Yes, userspace needs to provide the size so that KVM doesn't unintentionally overflow the buffer provided by userspace. We might also want to hedge by adding a flags? Can't think of a use for it at the moment, though.

struct kvm_xsave2 {

  	__u32 flags;
	__u32 size;
	__u8  state[0];

};

author
#27 lore raw

Re: [PATCH RFC 4/7] kvm: x86: Add new ioctls for XSAVE extension

Quoted 19 lines · Sean Christopherson
Quoted 18 lines · On Wed, May 26, 2021, Liu, Jing2 wrote:
Quoted 17 lines · Sean Christopherson
Quoted 16 lines · Jing Liu

The static xstate buffer kvm_xsave contains the extended register states, but it is not enough for dynamic features with large state.

Introduce a new capability called KVM_CAP_X86_XSAVE_EXTENSION to detect if hardware has XSAVE extension (XFD). Meanwhile, add two new ioctl interfaces to get/set the whole xstate using struct kvm_xsave_extension buffer containing both static and dynamic xfeatures. Reuse fill_xsave and load_xsave for both cases.

---
   arch/x86/include/uapi/asm/kvm.h |  5 +++
   arch/x86/kvm/x86.c              | 70 +++++++++++++++++++++++++--------
   include/uapi/linux/kvm.h        |  8 ++++
   3 files changed, 66 insertions(+), 17 deletions(-)
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

arch/x86/include/uapi/asm/kvm.h struct kvm_xsave { · line 362

Quoted 6 lines
arch/x86/include/uapi/asm/kvm.h +0 -0
index 89e5f3d1bba8..bf785e89a728 100644
@@ -362,6 +362,11 @@ struct kvm_xsave {
   	__u32 region[1024];

Hold up a sec. How big is the AMX data?

AMX tileconfig size is 64B, but tiledata size is 8K.

Quoted 2 lines

The existing size is 4096 bytes, not
1024 bytes. IIRC, AMX is >4k, so we still need a new ioctl(),

Yep, kvm_xsave can hold 4KB state. We need a new ioctl, holding all the states, not only AMX. And once KVM supports AMX, the size will >4096 so qemu need use kvm_xsave2 instead, otherwise, cannot save/restore whole AMX state.

Quoted 33 lines

but we should be
careful to mentally adjust for the __u32 when mentioning the sizes.

Quoted 11 lines
Quoted 8 lines

arch/x86/include/uapi/asm/kvm.h struct kvm_xsave { · line 367

Quoted 4 lines
arch/x86/include/uapi/asm/kvm.h +3 -0
   };
+/* for KVM_CAP_XSAVE_EXTENSION */
+struct kvm_xsave_extension {
+	__u32 region[3072];

Fool me once, shame on you (Intel). Fool me twice, shame on me (KVM).

As amusing as kvm_xsave_really_extended would be, the required size should be discoverable, not hardcoded.

Thanks for reviewing the patch. When looking at current kvm_xsave structure, I felt confusing about the static hardcoding of 1024 bytes, but failed to find clue for its final decision in 2010[1].

Simplicitly and lack of foresight :-)

Quoted 1 line

So we'd prefer to changing the way right? Please correct me if I misunderstood.

Sadly, we can't fix the existing ioctl() without breaking userspace. But for the new ioctl(), yes, its size should not be hardcoded.

Quoted 6 lines
Quoted 3 lines

Nothing prevents a hardware vendor from inventing a newfangled feature that requires yet more space. As an alternative to adding a dedicated capability, can we leverage GET_SUPPORTED_CPUID, leaf CPUID.0xD,

Yes, this is a good way to avoid a dedicated capability. Thanks for the suggestion. Use 0xD.1.EBX for size of enabled xcr0|xss if supposing kvm_xsave cares both.

Quoted 7 lines
Quoted 1 line

to enumerate the minimum required size and state

For the state, an extreme case is using an old qemu as follows, but a new kvm with more future_featureZ supported. If hardware vendor arranges one by one, it's OK to use static state like X86XSaveArea(2) and get/set between userspace and kvm because it's non-compacted. If not, the state will not correct.
So far it is OK, so I'm wondering if this would be an issue for now?

Quoted 8 lines

Oh, you're saying that, because kvm_xsave is non-compacted, future features may overflow kvm_xsave simply because the architectural offset overflows 4096 bytes.

That should be a non-issue for old KVM/kernels, since the new features shouldn't be enabled. For new KVM, I think the right approach is to reject KVM_GET_XSAVE and KVM_SET_XSAVE if the required size is greater than sizeof(struct kvm_xsave). I.e. force userspace to either hide the features from the guest, or use KVM_{G,S}ET_XSAVE2.

I was considering if the order/offset of future features will impact the compatibility if it is not designed one by one. But I realized it's not an issue because there uses non-compacted format so each offset strictly refers to spec.

Quoted 26 lines
Quoted 20 lines

X86XSaveArea2 {
    ...
    XSaveAVX
    ...
    AMX_XTILE;
    future_featureX;
    future_featureY;
}

Quoted 2 lines

that the new ioctl() is available if the min size is greater than 1024?
Or is that unnecessarily convoluted...

To enable a dynamic size kvm_xsave2(Thanks Jim's name suggestion), if things as follows are what we might want.
/* for xstate large than 1024 */
struct kvm_xsave2 {
    int size; // size of the whole xstate
    void *ptr; // xstate pointer
}
#define KVM_GET_XSAVE2   _IOW(KVMIO,  0xa4, struct kvm_xsave2)

Quoted 2 lines

Take @size together, so KVM need not fetch 0xd.1.ebx each time or a dedicated variable.

Yes, userspace needs to provide the size so that KVM doesn't unintentionally overflow the buffer provided by userspace. We might also want to hedge by adding a flags? Can't think of a use for it at the moment, though.

Quoted 5 lines
   struct kvm_xsave2 {
   	__u32 flags;
	__u32 size;
	__u8  state[0];
   };

u8 makes things simple that kvm doesn't need compute size to u32.

Thanks,
Jing

author
#28 lore raw

Re: [PATCH RFC 4/7] kvm: x86: Add new ioctls for XSAVE extension

Quoted 30 lines · On 6/1/2021 6:24 PM, Liu, Jing2 wrote:
Quoted 20 lines · Sean Christopherson
Quoted 19 lines · On Wed, May 26, 2021, Liu, Jing2 wrote:
Quoted 18 lines · Sean Christopherson
Quoted 17 lines · Jing Liu

The static xstate buffer kvm_xsave contains the extended register states, but it is not enough for dynamic features with large state.

Introduce a new capability called KVM_CAP_X86_XSAVE_EXTENSION to detect if hardware has XSAVE extension (XFD). Meanwhile, add two new ioctl interfaces to get/set the whole xstate using struct kvm_xsave_extension buffer containing both static and dynamic xfeatures. Reuse fill_xsave and load_xsave for both cases.

---
   arch/x86/include/uapi/asm/kvm.h |  5 +++
   arch/x86/kvm/x86.c              | 70
+++++++++++++++++++++++++--------
   include/uapi/linux/kvm.h        |  8 ++++

   3 files changed, 66 insertions(+), 17 deletions(-)
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

arch/x86/include/uapi/asm/kvm.h struct kvm_xsave { · line 362

Quoted 6 lines
arch/x86/include/uapi/asm/kvm.h +0 -0
index 89e5f3d1bba8..bf785e89a728 100644
@@ -362,6 +362,11 @@ struct kvm_xsave {
        __u32 region[1024];

Hold up a sec.  How big is the AMX data?

Quoted 32 lines

AMX tileconfig size is 64B, but tiledata size is 8K.

Quoted 2 lines

The existing size is 4096 bytes, not
1024 bytes.  IIRC, AMX is >4k, so we still need a new ioctl(),

Yep, kvm_xsave can hold 4KB state. We need a new ioctl, holding all the states, not only AMX. And once KVM supports AMX, the size will >4096 so qemu need use kvm_xsave2 instead, otherwise, cannot save/restore whole AMX state.

Quoted 26 lines

but we should be
careful to mentally adjust for the __u32 when mentioning the sizes.

Quoted 11 lines
Quoted 8 lines

arch/x86/include/uapi/asm/kvm.h struct kvm_xsave { · line 367

Quoted 4 lines
arch/x86/include/uapi/asm/kvm.h +3 -0
    };
+/* for KVM_CAP_XSAVE_EXTENSION */
+struct kvm_xsave_extension {
+    __u32 region[3072];

Fool me once, shame on you (Intel).  Fool me twice, shame on me (KVM).

As amusing as kvm_xsave_really_extended would be, the required size should be discoverable, not hardcoded.

Thanks for reviewing the patch.  When looking at current kvm_xsave structure, I felt confusing about the static hardcoding of 1024 bytes, but failed to find clue for its final decision in 2010[1].

Simplicitly and lack of foresight :-)

Quoted 1 line

So we'd prefer to changing the way right? Please correct me if I misunderstood.

Sadly, we can't fix the existing ioctl() without breaking userspace.  But for the new ioctl(), yes, its size should not be hardcoded.

Quoted 6 lines
Quoted 3 lines

Nothing prevents a hardware vendor from inventing a newfangled feature that requires yet more space.  As an alternative to adding a dedicated capability, can we leverage GET_SUPPORTED_CPUID, leaf CPUID.0xD,

Yes, this is a good way to avoid a dedicated capability. Thanks for the suggestion.  Use 0xD.1.EBX for size of enabled xcr0|xss if supposing kvm_xsave cares both.

I think kvm_xsave ioctl only cares user states because supervisor states should always use compacted format. When trying to think about how to get/set supervisor states, I think it can not reuse current design (qemu talks with kvm via a buffer and set/get to/from qemu's env->some_feature one by one according to the offset). So do we need handle supervisor states and change the way?

For kvm_xsave2 which expands to >4096B, if reuse and expand current way, it only detects xcr0 from 0xD.0.EBX.

[...]

Thanks,
Jing

#29 lore raw

Re: [PATCH RFC 4/7] kvm: x86: Add new ioctls for XSAVE extension

In reply to Jing Liu
Quoted 13 lines · Jing Liu

The static xstate buffer kvm_xsave contains the extended register states, but it is not enough for dynamic features with large state.

Introduce a new capability called KVM_CAP_X86_XSAVE_EXTENSION to detect if hardware has XSAVE extension (XFD). Meanwhile, add two new ioctl interfaces to get/set the whole xstate using struct kvm_xsave_extension buffer containing both static and dynamic xfeatures. Reuse fill_xsave and load_xsave for both cases.

---

include/uapi/linux/kvm.h struct kvm_s390_ucas_mapping { · line 1471

Quoted 2 lines
include/uapi/linux/kvm.h +2 -0
+#define KVM_GET_XSAVE_EXTENSION   _IOW(KVMIO,  0xa4, struct kvm_xsave_extension)
+#define KVM_SET_XSAVE_EXTENSION   _IOW(KVMIO,  0xa5, struct kvm_xsave_extension)

Isn't the convention to call these KVM_GET_XSAVE2 and KVM_SET_XSAVE2?

Do you have any documentation to add to Documentation/virt/kvm/api.rst?

author
#30 lore raw

Re: [PATCH RFC 4/7] kvm: x86: Add new ioctls for XSAVE extension

Quoted 10 lines · Jim Mattson
Quoted 9 lines · Jing Liu

The static xstate buffer kvm_xsave contains the extended register states, but it is not enough for dynamic features with large state.

Introduce a new capability called KVM_CAP_X86_XSAVE_EXTENSION to detect if hardware has XSAVE extension (XFD). Meanwhile, add two new ioctl interfaces to get/set the whole xstate using struct kvm_xsave_extension buffer containing both static and dynamic xfeatures. Reuse fill_xsave and load_xsave for both cases.

Quoted 7 lines

include/uapi/linux/kvm.h struct kvm_s390_ucas_mapping { · line 1471

Quoted 4 lines

---

include/uapi/linux/kvm.h +2 -0
+#define KVM_GET_XSAVE_EXTENSION   _IOW(KVMIO,  0xa4, struct kvm_xsave_extension)
+#define KVM_SET_XSAVE_EXTENSION   _IOW(KVMIO,  0xa5, struct kvm_xsave_extension)

Isn't the convention to call these KVM_GET_XSAVE2 and KVM_SET_XSAVE2?

Do you have any documentation to add to Documentation/virt/kvm/api.rst?

Thanks for reviewing the patch.
I'll change the name as convention and add documentation if new apis are needed.

BRs,
Jing

author
#31 lore raw

RFC PATCH 5/7 kvm: x86: Revise CPUID.D.1.EBX for alignment rule

In reply to Jing Liu

CPUID.0xD.1.EBX[1] is set if, when the compacted format of an XSAVE area is used, this extended state component located on the next 64-byte boundary following the preceding state component (otherwise, it is located immediately following the preceding state component).

AMX tileconfig and tiledata are the first to use 64B alignment. Revise the runtime cpuid modification for this rule.

---
 arch/x86/kvm/cpuid.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/kvm/cpuid.c +5 -0
index 04a73c395c71..ee1fac0a865e 100644
@@ -35,12 +35,17 @@ static u32 xstate_required_size(u64 xstate_bv, bool compacted)
 {
 	int feature_bit = 0;
 	u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
+	bool is_aligned = false;
 
 	xstate_bv &= XFEATURE_MASK_EXTEND;
 	while (xstate_bv) {
 		if (xstate_bv & 0x1) {
 		        u32 eax, ebx, ecx, edx, offset;
 		        cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
+			/* ECX[2]: 64B alignment in compacted form */
+			is_aligned = !!(ecx & 2);
+			if (is_aligned && compacted)
+				ret = ALIGN(ret, 64);
 			offset = compacted ? ret : ebx;
 			ret = max(ret, offset + eax);
 		}
Signature · 1 line
2.18.4
#32 lore raw

Re: [PATCH RFC 5/7] kvm: x86: Revise CPUID.D.1.EBX for alignment rule

Quoted 21 lines · Jing Liu

CPUID.0xD.1.EBX[1] is set if, when the compacted format of an XSAVE area is used, this extended state component located on the next 64-byte boundary following the preceding state component (otherwise, it is located immediately following the preceding state component).

AMX tileconfig and tiledata are the first to use 64B alignment. Revise the runtime cpuid modification for this rule.

---
 arch/x86/kvm/cpuid.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/kvm/cpuid.c +0 -0
index 04a73c395c71..ee1fac0a865e 100644
@@ -35,12 +35,17 @@ static u32 xstate_required_size(u64 xstate_bv, bool compacted)
 {
 	int feature_bit = 0;
 	u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;

arch/x86/kvm/cpuid.c static u32 xstate_required_size(u64 xstate_bv, bool compacted) · line 47

Quoted 10 lines
arch/x86/kvm/cpuid.c +4 -0
+	bool is_aligned = false;
 
 	xstate_bv &= XFEATURE_MASK_EXTEND;
 	while (xstate_bv) {
 		if (xstate_bv & 0x1) {
 		        u32 eax, ebx, ecx, edx, offset;
 		        cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
+			/* ECX[2]: 64B alignment in compacted form */
+			is_aligned = !!(ecx & 2);
+			if (is_aligned && compacted)

I'd forego the local is_aligned, and also check "compacted" first so that the uncompacted variant isn't required to evaluated ecx.

And the real reason I am responding... can you post this as a standalone patch? I stumbled across the "aligned" flag when reading through the SDM for a completely unrelated reason, and also discovered that the flag has been documented since 2016. While AMX may be the first to "officially" utilize the alignment flag, the flag itself is architectural and not strictly limited to AMX.

Quoted 7 lines
arch/x86/kvm/cpuid.c +1 -0
+				ret = ALIGN(ret, 64);
 			offset = compacted ? ret : ebx;
 			ret = max(ret, offset + eax);
 		}
Signature · 1 line
2.18.4
author
#33 lore raw

Re: [PATCH RFC 5/7] kvm: x86: Revise CPUID.D.1.EBX for alignment rule

Quoted 24 lines · Sean Christopherson
Quoted 23 lines · Jing Liu

CPUID.0xD.1.EBX[1] is set if, when the compacted format of an XSAVE area is used, this extended state component located on the next 64-byte boundary following the preceding state component (otherwise, it is located immediately following the preceding state component).

AMX tileconfig and tiledata are the first to use 64B alignment. Revise the runtime cpuid modification for this rule.

---
  arch/x86/kvm/cpuid.c | 5 +++++
  1 file changed, 5 insertions(+)
arch/x86/kvm/cpuid.c +1 -0
index 04a73c395c71..ee1fac0a865e 100644
@@ -35,12 +35,17 @@ static u32 xstate_required_size(u64 xstate_bv, bool compacted)
  {
  	int feature_bit = 0;
  	u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
+	bool is_aligned = false;
 
Quoted 10 lines

arch/x86/kvm/cpuid.c static u32 xstate_required_size(u64 xstate_bv, bool compacted) · line 47

Quoted 8 lines
arch/x86/kvm/cpuid.c +3 -0
  	xstate_bv &= XFEATURE_MASK_EXTEND;
  	while (xstate_bv) {
  		if (xstate_bv & 0x1) {
  		        u32 eax, ebx, ecx, edx, offset;
  		        cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
+			/* ECX[2]: 64B alignment in compacted form */
+			is_aligned = !!(ecx & 2);
+			if (is_aligned && compacted)

I'd forego the local is_aligned, and also check "compacted" first so that the uncompacted variant isn't required to evaluated ecx.

Non-compacted only works for XCR0 (user states), do we need add a check or simply do 'state_bv &= XFEATURE_MASK_USER_ENABLED'?

        xstate_bv &= XFEATURE_MASK_EXTEND;
+       /* Only user states use non-compacted format. */
+       if (!compacted)
+               xstate_bv &= XFEATURE_MASK_USER_SUPPORTED;
+
        while (xstate_bv) {
                if (xstate_bv & 0x1) {
                        u32 eax, ebx, ecx, edx, offset;
                        cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx); -                       offset = compacted ? ret : ebx;
+                       offset = compacted ? ((ecx & 0x2) ? ALIGN(ret, 64) : ret) : ebx; ret = max(ret, offset + eax);
                }

Quoted 2 lines

And the real reason I am responding... can you post this as a standalone patch?

Sure. Let me separate it.

Quoted 4 lines

I stumbled across the "aligned" flag when reading through the SDM for a completely unrelated reason, and also discovered that the flag has been documented since 2016. While AMX may be the first to "officially" utilize the alignment flag, the flag itself is architectural and not strictly limited to AMX.

Yes, this is not a new feature, but seems no one use it before.

Thanks,
Jing

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/cpuid.c +1 -0
+				ret = ALIGN(ret, 64);
  			offset = compacted ? ret : ebx;
  			ret = max(ret, offset + eax);
  		}
Signature · 1 line
2.18.4
author
#34 lore raw

RFC PATCH 6/7 kvm: x86: Add AMX_TILE, AMX_INT8 and AMX_BF16 support

In reply to Jing Liu

Intel introduces AMX architecture in SPR platform, which includes AMX_TILE, AMX_INT8 and AMX_BF16 support.

Exposes these features to KVM guest.

---
 arch/x86/kvm/cpuid.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kvm/cpuid.c +10 -2
index ee1fac0a865e..1b3ea9195a75 100644
@@ -423,7 +423,8 @@ void kvm_set_cpu_caps(void)
 		F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
 		F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
 		F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) |
-		F(SERIALIZE) | F(TSXLDTRK)
+		F(SERIALIZE) | F(TSXLDTRK) |
+		F(AMX_TILE) | F(AMX_INT8) | F(AMX_BF16)
 	);
 
 	/* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
@@ -544,6 +545,8 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
 	case 0x14:
 	case 0x17:
 	case 0x18:
+	case 0x1d:
+	case 0x1e:
 	case 0x1f:
 	case 0x8000001d:
 		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
@@ -667,6 +670,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 		break;
 	case 9:
 		break;
+	case 0x1e:
+		break;
 	case 0xa: { /* Architectural Performance Monitoring */
 		struct x86_pmu_capability cap;
 		union cpuid10_eax eax;
@@ -766,9 +771,12 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 			entry->edx = 0;
 		}
 		break;
+	/* Intel AMX TILE */
+	case 0x1d:
 	/* Intel PT */
 	case 0x14:
-		if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) {
+		if ((function == 0x14 && !kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) ||
+		    (function == 0x1d && !kvm_cpu_cap_has(X86_FEATURE_AMX_TILE))) {
 			entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
 			break;
 		}
Signature · 1 line
2.18.4
author
#35 lore raw

RFC PATCH 7/7 kvm: x86: AMX XCR0 support for guest

In reply to Jing Liu

Two XCR0 bits are defined for AMX to support XSAVE mechanism. Bit 17 is for tilecfg and bit 18 is for tiledata.

---
 arch/x86/kvm/x86.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +7 -1
index bfbde877221e..f1c5893dee18 100644
@@ -189,7 +189,7 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs;
 #define KVM_SUPPORTED_XCR0     (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
 				| XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
 				| XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
-				| XFEATURE_MASK_PKRU)
+				| XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE)
 
 u64 __read_mostly host_efer;
 EXPORT_SYMBOL_GPL(host_efer);
@@ -946,6 +946,12 @@ static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
 		if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
 			return 1;
 	}
+
+	if (xcr0 & XFEATURE_MASK_XTILE) {
+		if ((xcr0 & XFEATURE_MASK_XTILE) != XFEATURE_MASK_XTILE)
+			return 1;
+	}
+
 	vcpu->arch.xcr0 = xcr0;
 
 	if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Signature · 1 line
2.18.4
#36 lore raw

Re: [PATCH RFC 7/7] kvm: x86: AMX XCR0 support for guest

Quoted 2 lines · Jing Liu

Two XCR0 bits are defined for AMX to support XSAVE mechanism. Bit 17 is for tilecfg and bit 18 is for tiledata.

This fails to explain why they must be set in tandem. Out of curisoity, assuming they do indeed need to be set/cleared as a pair, what's the point of having two separate bits?

Quoted 34 lines
---
 arch/x86/kvm/x86.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +7 -1
index bfbde877221e..f1c5893dee18 100644
@@ -189,7 +189,7 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs;
 #define KVM_SUPPORTED_XCR0     (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
 				| XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
 				| XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
-				| XFEATURE_MASK_PKRU)
+				| XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE)
 
 u64 __read_mostly host_efer;
 EXPORT_SYMBOL_GPL(host_efer);
@@ -946,6 +946,12 @@ static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
 		if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
 			return 1;
 	}
+
+	if (xcr0 & XFEATURE_MASK_XTILE) {
+		if ((xcr0 & XFEATURE_MASK_XTILE) != XFEATURE_MASK_XTILE)
+			return 1;
+	}
+
 	vcpu->arch.xcr0 = xcr0;
 
 	if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Signature · 1 line
2.18.4
author
#37 lore raw

Re: [PATCH RFC 7/7] kvm: x86: AMX XCR0 support for guest

Quoted 4 lines · Sean Christopherson
Quoted 2 lines · Jing Liu

Two XCR0 bits are defined for AMX to support XSAVE mechanism. Bit 17 is for tilecfg and bit 18 is for tiledata.

This fails to explain why they must be set in tandem.

The spec says,
"executing the XSETBV instruction causes a general-protection fault (#GP) if ECX=0 and EAX[17] ≠ EAX[18] (XTILECFG and XTILEDATA must be enabled together). This implies that the value of XCR0[17:18] is always either 00b or 11b."

I can add more to changelog if this is reasonable.

Quoted 3 lines

Out of curisoity, assuming
they do indeed need to be set/cleared as a pair, what's the point of having two separate bits?

What I can see is to separate different states and mirror by XFD which can set bits separately.

Thanks,
Jing

Quoted 35 lines
Quoted 35 lines
---
  arch/x86/kvm/x86.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +7 -1
index bfbde877221e..f1c5893dee18 100644
@@ -189,7 +189,7 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs;
  #define KVM_SUPPORTED_XCR0     (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \
  				| XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \
  				| XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \
-				| XFEATURE_MASK_PKRU)
+				| XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE)
 
  u64 __read_mostly host_efer;
  EXPORT_SYMBOL_GPL(host_efer);
@@ -946,6 +946,12 @@ static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
  		if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
  			return 1;
  	}
+
+	if (xcr0 & XFEATURE_MASK_XTILE) {
+		if ((xcr0 & XFEATURE_MASK_XTILE) != XFEATURE_MASK_XTILE)
+			return 1;
+	}
+
  	vcpu->arch.xcr0 = xcr0;
 
  	if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
Signature · 1 line
2.18.4
#38 lore raw

Re: [PATCH RFC 7/7] kvm: x86: AMX XCR0 support for guest

Quoted 5 lines · On Wed, May 26, 2021, Liu, Jing2 wrote:
Quoted 3 lines · Sean Christopherson
Quoted 2 lines · Jing Liu

Two XCR0 bits are defined for AMX to support XSAVE mechanism. Bit 17 is for tilecfg and bit 18 is for tiledata.

Quoted 7 lines
Quoted 1 line

This fails to explain why they must be set in tandem.

The spec says, "executing the XSETBV instruction causes a general-protection fault (#GP) if ECX=0 and EAX[17] ≠ EAX[18] (XTILECFG and XTILEDATA must be enabled together). This implies that the value of XCR0[17:18] is always either 00b or 11b."

I can add more to changelog if this is reasonable.

Ya, please do. It doesn't have to be the full thing verbatim (but that's ok, too), but the requirement does need to be called out.

Quoted 5 lines
Quoted 2 lines

Out of curisoity, assuming they do indeed need to be set/cleared as a pair, what's the point of having two separate bits?

What I can see is to separate different states and mirror by XFD which can set bits separately.

Ah, that would make sense. Thanks!