Skip to contentSkip to conversation

KVM: x86: Update KVM_GET_CPUID2 to return valid entry count

PATCH Superseded Takahiro Itazuri 2 participants 2 messages Apr 13, 2023 – May 26, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: x86: Update KVM_GET_CPUID2 to return valid entry count

Modify the KVM_GET_CPUID2 API to return the number of valid entries in nent field of kvm_cpuid2 even on success.

Previously, the KVM_GET_CPUID2 API only updated the nent field when an error was returned. If the API was called with an entry count larger than necessary (e.g., KVM_MAX_CPUID_ENTRIES), it would succeed, but the nent field would continue to show a value larger than the actual number of entries filled by the KVM_GET_CPUID2 API. With this change, users can rely on the updated nent field and there is no need to traverse unnecessary entries and check whether an entry is valid or not.

---
Changes
v1 -> v2
* Capitalize "kvm" in the commit title.
* Remove a scratch "nent" variable.
* Link to v1: https://lore.kernel.org/all/[email protected]/

 arch/x86/kvm/cpuid.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
arch/x86/kvm/cpuid.c +8 -8
index 599aebec2d52..20d28ebdc672 100644
@@ -523,18 +523,18 @@ int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
 			      struct kvm_cpuid2 *cpuid,
 			      struct kvm_cpuid_entry2 __user *entries)
 {
-	int r;
+	int r = 0;
 
-	r = -E2BIG;
 	if (cpuid->nent < vcpu->arch.cpuid_nent)
-		goto out;
-	r = -EFAULT;
-	if (copy_to_user(entries, vcpu->arch.cpuid_entries,
+		r = -E2BIG;
+	else if (copy_to_user(entries, vcpu->arch.cpuid_entries,
 			 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
-		goto out;
-	return 0;
+		r = -EFAULT;
 
-out:
+	/*
+	 * Update "nent" even on failure, e.g. so that userspace can fix an
+	 * -E2BIG issue by allocating a larger array.
+	 */
 	cpuid->nent = vcpu->arch.cpuid_nent;
 	return r;
 }
Signature · 1 line
2.39.2
#1 lore raw
Quoted 41 lines · Takahiro Itazuri

Modify the KVM_GET_CPUID2 API to return the number of valid entries in nent field of kvm_cpuid2 even on success.

Previously, the KVM_GET_CPUID2 API only updated the nent field when an error was returned. If the API was called with an entry count larger than necessary (e.g., KVM_MAX_CPUID_ENTRIES), it would succeed, but the nent field would continue to show a value larger than the actual number of entries filled by the KVM_GET_CPUID2 API. With this change, users can rely on the updated nent field and there is no need to traverse unnecessary entries and check whether an entry is valid or not.

---
Changes
v1 -> v2
* Capitalize "kvm" in the commit title.
* Remove a scratch "nent" variable.
* Link to v1: https://lore.kernel.org/all/[email protected]/

 arch/x86/kvm/cpuid.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
arch/x86/kvm/cpuid.c +3 -5
index 599aebec2d52..20d28ebdc672 100644
@@ -523,18 +523,18 @@ int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
 			      struct kvm_cpuid2 *cpuid,
 			      struct kvm_cpuid_entry2 __user *entries)
 {
-	int r;
+	int r = 0;
 
-	r = -E2BIG;
 	if (cpuid->nent < vcpu->arch.cpuid_nent)
-		goto out;
-	r = -EFAULT;
-	if (copy_to_user(entries, vcpu->arch.cpuid_entries,
+		r = -E2BIG;
+	else if (copy_to_user(entries, vcpu->arch.cpuid_entries,

arch/x86/kvm/cpuid.c int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, · line 537

Quoted 10 lines
arch/x86/kvm/cpuid.c +5 -3
 			 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
-		goto out;
-	return 0;
+		r = -EFAULT;
 
-out:
+	/*
+	 * Update "nent" even on failure, e.g. so that userspace can fix an
+	 * -E2BIG issue by allocating a larger array.
+	 */

Gah, this is wrong. The caller only copies @cpuid to userspace on success. My fault for not seeing this in v1, but this is exactly why we write tests.

I've written a test and a fix, I'll post 'em shortly.