[PATCH v3] KVM: x86: Sync the states size with the XCR0/IA32_XSS at, any time

Like Xu posted 1 patch 4 years, 5 months ago
arch/x86/kvm/x86.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
[PATCH v3] KVM: x86: Sync the states size with the XCR0/IA32_XSS at, any time
Posted by Like Xu 4 years, 5 months ago
From: Like Xu <likexu@tencent.com>

XCR0 is reset to 1 by RESET but not INIT and IA32_XSS is zeroed by
both RESET and INIT. The kvm_set_msr_common()'s handling of MSR_IA32_XSS
also needs to update kvm_update_cpuid_runtime(). In the above cases, the
size in bytes of the XSAVE area containing all states enabled by XCR0 or
(XCRO | IA32_XSS) needs to be updated.

For simplicity and consistency, existing helpers are used to write values
and call kvm_update_cpuid_runtime(), and it's not exactly a fast path.

Fixes: a554d207dc46 ("KVM: X86: Processor States following Reset or INIT")
Signed-off-by: Like Xu <likexu@tencent.com>
---
v2 -> v3 Changelog:
- Apply s/legacy/existing in the commit message; (Sean)
- Invoke kvm_update_cpuid_runtime() for MSR_IA32_XSS; (Sean)

  arch/x86/kvm/x86.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 55518b7d3b96..4b509b26d9ab 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3535,6 +3535,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct 
msr_data *msr_info)
  		if (data & ~supported_xss)
  			return 1;
  		vcpu->arch.ia32_xss = data;
+		kvm_update_cpuid_runtime(vcpu);
  		break;
  	case MSR_SMI_COUNT:
  		if (!msr_info->host_initiated)
@@ -11256,7 +11257,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)

  		vcpu->arch.msr_misc_features_enables = 0;

-		vcpu->arch.xcr0 = XFEATURE_MASK_FP;
+		__kvm_set_xcr(vcpu, 0, XFEATURE_MASK_FP);
  	}

  	/* All GPRs except RDX (handled below) are zeroed on RESET/INIT. */
@@ -11273,7 +11274,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  	cpuid_0x1 = kvm_find_cpuid_entry(vcpu, 1, 0);
  	kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600);

-	vcpu->arch.ia32_xss = 0;
+	__kvm_set_msr(vcpu, MSR_IA32_XSS, 0, true);

  	static_call(kvm_x86_vcpu_reset)(vcpu, init_event);

-- 
2.33.1


Re: [PATCH v3] KVM: x86: Sync the states size with the XCR0/IA32_XSS at, any time
Posted by Sean Christopherson 4 years, 5 months ago
On Sun, Jan 23, 2022, Like Xu wrote:
> From: Like Xu <likexu@tencent.com>
> 
> XCR0 is reset to 1 by RESET but not INIT and IA32_XSS is zeroed by
> both RESET and INIT. The kvm_set_msr_common()'s handling of MSR_IA32_XSS
> also needs to update kvm_update_cpuid_runtime(). In the above cases, the
> size in bytes of the XSAVE area containing all states enabled by XCR0 or
> (XCRO | IA32_XSS) needs to be updated.
> 
> For simplicity and consistency, existing helpers are used to write values
> and call kvm_update_cpuid_runtime(), and it's not exactly a fast path.
> 
> Fixes: a554d207dc46 ("KVM: X86: Processor States following Reset or INIT")
> Signed-off-by: Like Xu <likexu@tencent.com>
> ---
> v2 -> v3 Changelog:
> - Apply s/legacy/existing in the commit message; (Sean)
> - Invoke kvm_update_cpuid_runtime() for MSR_IA32_XSS; (Sean)
> 
>  arch/x86/kvm/x86.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 55518b7d3b96..4b509b26d9ab 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -3535,6 +3535,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct
> msr_data *msr_info)
>  		if (data & ~supported_xss)
>  			return 1;
>  		vcpu->arch.ia32_xss = data;
> +		kvm_update_cpuid_runtime(vcpu);
>  		break;
>  	case MSR_SMI_COUNT:
>  		if (!msr_info->host_initiated)
> @@ -11256,7 +11257,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
> 
>  		vcpu->arch.msr_misc_features_enables = 0;
> 
> -		vcpu->arch.xcr0 = XFEATURE_MASK_FP;
> +		__kvm_set_xcr(vcpu, 0, XFEATURE_MASK_FP);
>  	}
> 
>  	/* All GPRs except RDX (handled below) are zeroed on RESET/INIT. */
> @@ -11273,7 +11274,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
>  	cpuid_0x1 = kvm_find_cpuid_entry(vcpu, 1, 0);
>  	kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600);
> 
> -	vcpu->arch.ia32_xss = 0;
> +	__kvm_set_msr(vcpu, MSR_IA32_XSS, 0, true);

Heh, this now conflicts with a patch Xiaoyao just posted, turns out the SDM was
wrong.  I think there's also some whitespace change or something that prevents
this from applying cleanly.  For convenience, I'll post a miniseries with this
and Xiaoyao's patch.

[*] https://lore.kernel.org/all/20220126034750.2495371-1-xiaoyao.li@intel.com

> 
>  	static_call(kvm_x86_vcpu_reset)(vcpu, init_event);
> 
> -- 
> 2.33.1
> 
> 
Re: [PATCH v3] KVM: x86: Sync the states size with the XCR0/IA32_XSS at, any time
Posted by Paolo Bonzini 4 years, 5 months ago
On 1/26/22 17:37, Sean Christopherson wrote:
> On Sun, Jan 23, 2022, Like Xu wrote:
>> From: Like Xu <likexu@tencent.com>
>>
>> XCR0 is reset to 1 by RESET but not INIT and IA32_XSS is zeroed by
>> both RESET and INIT. The kvm_set_msr_common()'s handling of MSR_IA32_XSS
>> also needs to update kvm_update_cpuid_runtime(). In the above cases, the
>> size in bytes of the XSAVE area containing all states enabled by XCR0 or
>> (XCRO | IA32_XSS) needs to be updated.
>>
>> For simplicity and consistency, existing helpers are used to write values
>> and call kvm_update_cpuid_runtime(), and it's not exactly a fast path.
>>
>> Fixes: a554d207dc46 ("KVM: X86: Processor States following Reset or INIT")
>> Signed-off-by: Like Xu <likexu@tencent.com>
>> ---
>> v2 -> v3 Changelog:
>> - Apply s/legacy/existing in the commit message; (Sean)
>> - Invoke kvm_update_cpuid_runtime() for MSR_IA32_XSS; (Sean)
>>
>>   arch/x86/kvm/x86.c | 5 +++--
>>   1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>> index 55518b7d3b96..4b509b26d9ab 100644
>> --- a/arch/x86/kvm/x86.c
>> +++ b/arch/x86/kvm/x86.c
>> @@ -3535,6 +3535,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct
>> msr_data *msr_info)
>>   		if (data & ~supported_xss)
>>   			return 1;
>>   		vcpu->arch.ia32_xss = data;
>> +		kvm_update_cpuid_runtime(vcpu);
>>   		break;
>>   	case MSR_SMI_COUNT:
>>   		if (!msr_info->host_initiated)
>> @@ -11256,7 +11257,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
>>
>>   		vcpu->arch.msr_misc_features_enables = 0;
>>
>> -		vcpu->arch.xcr0 = XFEATURE_MASK_FP;
>> +		__kvm_set_xcr(vcpu, 0, XFEATURE_MASK_FP);
>>   	}
>>
>>   	/* All GPRs except RDX (handled below) are zeroed on RESET/INIT. */
>> @@ -11273,7 +11274,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
>>   	cpuid_0x1 = kvm_find_cpuid_entry(vcpu, 1, 0);
>>   	kvm_rdx_write(vcpu, cpuid_0x1 ? cpuid_0x1->eax : 0x600);
>>
>> -	vcpu->arch.ia32_xss = 0;
>> +	__kvm_set_msr(vcpu, MSR_IA32_XSS, 0, true);
> 
> Heh, this now conflicts with a patch Xiaoyao just posted, turns out the SDM was
> wrong.  I think there's also some whitespace change or something that prevents
> this from applying cleanly.  For convenience, I'll post a miniseries with this
> and Xiaoyao's patch.
> 
> [*] https://lore.kernel.org/all/20220126034750.2495371-1-xiaoyao.li@intel.com

It's okay, fixing the conflict in Xiaoyao's patch is trivial 
and---anyway---KVM does not support XSS != 0 for now so his patch 
doesn't really have any practical effect.

Like's patch queued, thanks.

Paolo