Skip to contentSkip to conversation

KVM: x86: Assign correct value to array.maxnent

PATCH In review Xiaoyao Li 3 participants 3 messages Jun 4, 2020

Subsystems (3)
lore
Share & tools
lore

Xiaoyao Li

author
#0 lore raw

PATCH v2 1/1 KVM: x86: Assign correct value to array.maxnent

Delay the assignment of array.maxnent to use correct value for the case cpuid->nent > KVM_MAX_CPUID_ENTRIES.

Fixes e53c95e8d41e ("KVM: x86: Encapsulate CPUID entries and metadata in struct")

---
v2:
- remove "const" of maxnent to fix build error.

---
 arch/x86/kvm/cpuid.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/cpuid.c +3 -2
index 253b8e875ccd..3d88ddf781d0 100644
@@ -426,7 +426,7 @@ EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
 
 struct kvm_cpuid_array {
 	struct kvm_cpuid_entry2 *entries;
-	const int maxnent;
+	int maxnent;
 	int nent;
 };
 
@@ -870,7 +870,6 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
 
 	struct kvm_cpuid_array array = {
 		.nent = 0,
-		.maxnent = cpuid->nent,
 	};
 	int r, i;
 
@@ -887,6 +886,8 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
 	if (!array.entries)
 		return -ENOMEM;
 
+	array.maxnent = cpuid->nent;
+
 	for (i = 0; i < ARRAY_SIZE(funcs); i++) {
 		r = get_cpuid_func(&array, funcs[i], type);
 		if (r)
Signature · 1 line
2.18.2
#1 lore raw
Quoted 34 lines · Xiaoyao Li

Delay the assignment of array.maxnent to use correct value for the case cpuid->nent > KVM_MAX_CPUID_ENTRIES.

Fixes e53c95e8d41e ("KVM: x86: Encapsulate CPUID entries and metadata in struct")

---
v2:
- remove "const" of maxnent to fix build error.

---
 arch/x86/kvm/cpuid.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/cpuid.c +1 -2
index 253b8e875ccd..3d88ddf781d0 100644
@@ -426,7 +426,7 @@ EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
 
 struct kvm_cpuid_array {
 	struct kvm_cpuid_entry2 *entries;
-	const int maxnent;
+	int maxnent;
 	int nent;
 };
 
@@ -870,7 +870,6 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
 
 	struct kvm_cpuid_array array = {
 		.nent = 0,
-		.maxnent = cpuid->nent,
 	};
 	int r, i;
 

arch/x86/kvm/cpuid.c int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, · line 889

Quoted 4 lines
arch/x86/kvm/cpuid.c +1 -0
@@ -887,6 +886,8 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
 	if (!array.entries)
 		return -ENOMEM;
 
+	array.maxnent = cpuid->nent;

Eh, I'd vote to just do:

arch/x86/kvm/cpuid.c +1 -1
index 253b8e875ccd..1e5b1ee75a76 100644
@@ -870,7 +870,7 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
 
        struct kvm_cpuid_array array = {
                .nent = 0,
-               .maxnent = cpuid->nent,
+               .maxnent = min(cpuid->nent, (u32)KVM_MAX_CPUID_ENTRIES),
        };
        int r, i;
 
Quoted 7 lines
arch/x86/kvm/cpuid.c +1 -0
+
 	for (i = 0; i < ARRAY_SIZE(funcs); i++) {
 		r = get_cpuid_func(&array, funcs[i], type);
 		if (r)
Signature · 1 line
2.18.2
maintainer
#2 lore raw
Quoted 47 lines · Sean Christopherson
Quoted 34 lines · Xiaoyao Li

Delay the assignment of array.maxnent to use correct value for the case cpuid->nent > KVM_MAX_CPUID_ENTRIES.

Fixes e53c95e8d41e ("KVM: x86: Encapsulate CPUID entries and metadata in struct")

---
v2:
- remove "const" of maxnent to fix build error.

---
 arch/x86/kvm/cpuid.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/cpuid.c +1 -2
index 253b8e875ccd..3d88ddf781d0 100644
@@ -426,7 +426,7 @@ EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
 
 struct kvm_cpuid_array {
 	struct kvm_cpuid_entry2 *entries;
-	const int maxnent;
+	int maxnent;
 	int nent;
 };
 
@@ -870,7 +870,6 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
 
 	struct kvm_cpuid_array array = {
 		.nent = 0,
-		.maxnent = cpuid->nent,
 	};
 	int r, i;
 

arch/x86/kvm/cpuid.c int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, · line 889

Quoted 4 lines
arch/x86/kvm/cpuid.c +1 -0
@@ -887,6 +886,8 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
 	if (!array.entries)
 		return -ENOMEM;
 
+	array.maxnent = cpuid->nent;

Eh, I'd vote to just do:

arch/x86/kvm/cpuid.c +0 -0
index 253b8e875ccd..1e5b1ee75a76 100644

arch/x86/kvm/cpuid.c int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, · line 876

Quoted 8 lines
arch/x86/kvm/cpuid.c +1 -1
@@ -870,7 +870,7 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
 
        struct kvm_cpuid_array array = {
                .nent = 0,
-               .maxnent = cpuid->nent,
+               .maxnent = min(cpuid->nent, (u32)KVM_MAX_CPUID_ENTRIES),
        };
        int r, i;
 

Both are fine, I've queued Xiaoyao's patch.

Paolo