Skip to contentSkip to conversation

KVM: X86: Call kvm_x86_ops.cpuid_update() after CPUIDs fully updated

PATCH In review Xiaoyao Li 2 participants 5 messages May 28, 2020

Subsystems (3)
lore
Share & tools
lore

Xiaoyao Li

author
#0 lore raw

PATCH 1/1 KVM: X86: Call kvm_x86_ops.cpuid_update() after CPUIDs fully updated

kvm_x86_ops.cpuid_update() is used to update vmx/svm settings based on updated CPUID settings. So it's supposed to be called after CPUIDs are fully updated, not in the middle stage.

---
 arch/x86/kvm/cpuid.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/x86/kvm/cpuid.c +8 -2
index cd708b0b460a..753739bc1bf0 100644
@@ -208,8 +208,11 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 	vcpu->arch.cpuid_nent = cpuid->nent;
 	cpuid_fix_nx_cap(vcpu);
 	kvm_apic_set_version(vcpu);
-	kvm_x86_ops.cpuid_update(vcpu);
 	r = kvm_update_cpuid(vcpu);
+	if (r)
+		goto out;
+
+	kvm_x86_ops.cpuid_update(vcpu);
 
 out:
 	vfree(cpuid_entries);
@@ -231,8 +234,11 @@ int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
 		goto out;
 	vcpu->arch.cpuid_nent = cpuid->nent;
 	kvm_apic_set_version(vcpu);
-	kvm_x86_ops.cpuid_update(vcpu);
 	r = kvm_update_cpuid(vcpu);
+	if (r)
+		goto out;
+
+	kvm_x86_ops.cpuid_update(vcpu);
 out:
 	return r;
 }
Signature · 1 line
2.18.2
maintainer
#1 lore raw
Quoted 5 lines · Xiaoyao Li

kvm_x86_ops.cpuid_update() is used to update vmx/svm settings based on updated CPUID settings. So it's supposed to be called after CPUIDs are fully updated, not in the middle stage.

Are you seeing anything bad happening from this?

Paolo

Quoted 35 lines
---
 arch/x86/kvm/cpuid.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/x86/kvm/cpuid.c +8 -2
index cd708b0b460a..753739bc1bf0 100644
@@ -208,8 +208,11 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 	vcpu->arch.cpuid_nent = cpuid->nent;
 	cpuid_fix_nx_cap(vcpu);
 	kvm_apic_set_version(vcpu);
-	kvm_x86_ops.cpuid_update(vcpu);
 	r = kvm_update_cpuid(vcpu);
+	if (r)
+		goto out;
+
+	kvm_x86_ops.cpuid_update(vcpu);
 
 out:
 	vfree(cpuid_entries);
@@ -231,8 +234,11 @@ int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
 		goto out;
 	vcpu->arch.cpuid_nent = cpuid->nent;
 	kvm_apic_set_version(vcpu);
-	kvm_x86_ops.cpuid_update(vcpu);
 	r = kvm_update_cpuid(vcpu);
+	if (r)
+		goto out;
+
+	kvm_x86_ops.cpuid_update(vcpu);
 out:
 	return r;
 }
author
#2 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 5 lines · Xiaoyao Li

kvm_x86_ops.cpuid_update() is used to update vmx/svm settings based on updated CPUID settings. So it's supposed to be called after CPUIDs are fully updated, not in the middle stage.

Are you seeing anything bad happening from this?

Not yet.

IMO changing the order is more reasonable and less confusing.

Quoted 38 lines

Paolo

Quoted 35 lines
---
  arch/x86/kvm/cpuid.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
arch/x86/kvm/cpuid.c +8 -2
index cd708b0b460a..753739bc1bf0 100644
@@ -208,8 +208,11 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  	vcpu->arch.cpuid_nent = cpuid->nent;
  	cpuid_fix_nx_cap(vcpu);
  	kvm_apic_set_version(vcpu);
-	kvm_x86_ops.cpuid_update(vcpu);
  	r = kvm_update_cpuid(vcpu);
+	if (r)
+		goto out;
+
+	kvm_x86_ops.cpuid_update(vcpu);
 
  out:
  	vfree(cpuid_entries);
@@ -231,8 +234,11 @@ int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  		goto out;
  	vcpu->arch.cpuid_nent = cpuid->nent;
  	kvm_apic_set_version(vcpu);
-	kvm_x86_ops.cpuid_update(vcpu);
  	r = kvm_update_cpuid(vcpu);
+	if (r)
+		goto out;
+
+	kvm_x86_ops.cpuid_update(vcpu);
  out:
  	return r;
  }
maintainer
#3 lore raw
Quoted 5 lines · Xiaoyao Li
Quoted 5 lines
Quoted 5 lines

kvm_x86_ops.cpuid_update() is used to update vmx/svm settings based on updated CPUID settings. So it's supposed to be called after CPUIDs are fully updated, not in the middle stage.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Are you seeing anything bad happening from this?

Not yet.

IMO changing the order is more reasonable and less confusing.

Indeed, I just could not decide whether to include it in 5.7 or not.

Paolo

author
#4 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Xiaoyao Li
Quoted 7 lines
Quoted 6 lines

kvm_x86_ops.cpuid_update() is used to update vmx/svm settings based on updated CPUID settings. So it's supposed to be called after CPUIDs are fully updated, not in the middle stage.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Are you seeing anything bad happening from this?

Not yet.

IMO changing the order is more reasonable and less confusing.

Indeed, I just could not decide whether to include it in 5.7 or not.

Maybe for 5.8

I have a new idea to refactor a bit more that I find it does three things in kvm_update_cpuid():
- update cpuid;
- update vcpu states, e.g., apic->lapic_timer.timer_mode_mask, guest_supported_xcr0, maxphyaddr, ... etc,
- cpuid check, for vaddr_bits

I'm going to split it, and make the order as:
1. kvm_check_cpuid(), if invalid value return error;
2. kvm_update_cpuid();
3. kvm_update_state_based_on_cpuid();
and kvm_x86_ops.kvm_x86_ops.cpuid_update() can be called inside it.

If you feel OK, I'll do it tomorrow.

-Xiaoyao