diff mbox series

[v2,1/7] KVM: X86: Reset vcpu->arch.cpuid_nent to 0 if SET_CPUID fails

Message ID 20200623115816.24132-2-xiaoyao.li@intel.com (mailing list archive)
State New, archived
Headers show
Series Refactor handling flow of SET_CPUID* | expand

Commit Message

Xiaoyao Li June 23, 2020, 11:58 a.m. UTC
It needs to invalidate CPUID configruations if usersapce provides
illegal input.

Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
 arch/x86/kvm/cpuid.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Jim Mattson June 23, 2020, 6:20 p.m. UTC | #1
On Tue, Jun 23, 2020 at 4:58 AM Xiaoyao Li <xiaoyao.li@intel.com> wrote:
>
> It needs to invalidate CPUID configruations if usersapce provides

Nits: configurations, userspace

> illegal input.
>
> Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
> ---
>  arch/x86/kvm/cpuid.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 8a294f9747aa..1d13bad42bf9 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -207,6 +207,8 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
>         kvm_apic_set_version(vcpu);
>         kvm_x86_ops.cpuid_update(vcpu);
>         r = kvm_update_cpuid(vcpu);
> +       if (r)
> +               vcpu->arch.cpuid_nent = 0;
>
>         kvfree(cpuid_entries);
>  out:
> @@ -230,6 +232,8 @@ int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
>         kvm_apic_set_version(vcpu);
>         kvm_x86_ops.cpuid_update(vcpu);
>         r = kvm_update_cpuid(vcpu);
> +       if (r)
> +               vcpu->arch.cpuid_nent = 0;
>  out:
>         return r;
>  }
> --
> 2.18.2

What if vcpu->arch.cpuid_nent was greater than 0 before the ioctl in question?
Xiaoyao Li June 24, 2020, 12:40 a.m. UTC | #2
On 6/24/2020 2:20 AM, Jim Mattson wrote:
> On Tue, Jun 23, 2020 at 4:58 AM Xiaoyao Li <xiaoyao.li@intel.com> wrote:
>>
>> It needs to invalidate CPUID configruations if usersapce provides
> 
> Nits: configurations, userspace

oh, I'll fix it.

>> illegal input.
>>
>> Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
>> ---
>>   arch/x86/kvm/cpuid.c | 4 ++++
>>   1 file changed, 4 insertions(+)
>>
>> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
>> index 8a294f9747aa..1d13bad42bf9 100644
>> --- a/arch/x86/kvm/cpuid.c
>> +++ b/arch/x86/kvm/cpuid.c
>> @@ -207,6 +207,8 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
>>          kvm_apic_set_version(vcpu);
>>          kvm_x86_ops.cpuid_update(vcpu);
>>          r = kvm_update_cpuid(vcpu);
>> +       if (r)
>> +               vcpu->arch.cpuid_nent = 0;
>>
>>          kvfree(cpuid_entries);
>>   out:
>> @@ -230,6 +232,8 @@ int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
>>          kvm_apic_set_version(vcpu);
>>          kvm_x86_ops.cpuid_update(vcpu);
>>          r = kvm_update_cpuid(vcpu);
>> +       if (r)
>> +               vcpu->arch.cpuid_nent = 0;
>>   out:
>>          return r;
>>   }
>> --
>> 2.18.2
> 
> What if vcpu->arch.cpuid_nent was greater than 0 before the ioctl in question?
>

Nice catch!

If considering it, then we have to restore the old CPUID configuration. 
So how about making it simpler to just add one line of comment in API doc:
If KVM_SET_CPUID{2} fails, the old valid configuration is cleared as a 
side effect.
diff mbox series

Patch

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 8a294f9747aa..1d13bad42bf9 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -207,6 +207,8 @@  int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 	kvm_apic_set_version(vcpu);
 	kvm_x86_ops.cpuid_update(vcpu);
 	r = kvm_update_cpuid(vcpu);
+	if (r)
+		vcpu->arch.cpuid_nent = 0;
 
 	kvfree(cpuid_entries);
 out:
@@ -230,6 +232,8 @@  int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
 	kvm_apic_set_version(vcpu);
 	kvm_x86_ops.cpuid_update(vcpu);
 	r = kvm_update_cpuid(vcpu);
+	if (r)
+		vcpu->arch.cpuid_nent = 0;
 out:
 	return r;
 }