KVM: X86: Reset vcpu->arch.cpuid_nent to 0 if SET_CPUID* fails
authorXiaoyao Li <xiaoyao.li@intel.com>
Wed, 8 Jul 2020 06:50:47 +0000 (14:50 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 8 Jul 2020 20:22:00 +0000 (16:22 -0400)
Current implementation keeps userspace input of CPUID configuration and
cpuid->nent even if kvm_update_cpuid() fails. Reset vcpu->arch.cpuid_nent
to 0 for the case of failure as a simple fix.

Besides, update the doc to explicitly state that if IOCTL SET_CPUID*
fail KVM gives no gurantee that previous valid CPUID configuration is
kept.

Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
Message-Id: <20200708065054.19713-2-xiaoyao.li@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Documentation/virt/kvm/api.rst
arch/x86/kvm/cpuid.c

index 1cfe79b..644e532 100644 (file)
@@ -669,6 +669,10 @@ MSRs that have been set successfully.
 Defines the vcpu responses to the cpuid instruction.  Applications
 should use the KVM_SET_CPUID2 ioctl if available.
 
+Note, when this IOCTL fails, KVM gives no guarantees that previous valid CPUID
+configuration (if there is) is not corrupted. Userspace can get a copy of the
+resulting CPUID configuration through KVM_GET_CPUID2 in case.
+
 ::
 
   struct kvm_cpuid_entry {
index 6f2b6e9..b7bbed0 100644 (file)
@@ -209,6 +209,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:
@@ -232,6 +234,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;
 }