Skip to contentSkip to conversation

kvm: Check if smp_cpus exceeds max cpus supported by kvm

PATCH Superseded [email protected] 3 participants 3 messages Jul 30, 2012 – Jul 31, 2012 KVM

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm: Check if smp_cpus exceeds max cpus supported by kvm

From: Dunrong Huang <[email protected]>

Add a helper function for fetching max cpus supported by kvm.

Make QEMU exit with an error message if smp_cpus exceeds limit of VCPU count retrieved by invoking this helper function.

---
 kvm-all.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
kvm-all.c +25 -0
index 2148b20..8cb4b92 100644
@@ -1207,6 +1207,23 @@ static int kvm_irqchip_create(KVMState *s)
     return 0;
 }
 
+static int kvm_max_vcpus(KVMState *s)
+{
+    int max_vcpus = 4;
+    int ret;
+    ret = kvm_check_extension(s, KVM_CAP_MAX_VCPUS);
+    if (ret) {
+        max_vcpus = ret;
+    } else {
+       ret = kvm_check_extension(s, KVM_CAP_NR_VCPUS);
+        if (ret) {
+            max_vcpus = ret;
+        }
+   }
+
+    return max_vcpus;
+}
+
 int kvm_init(void)
 {
     static const char upgrade_note[] =
@@ -1216,6 +1233,7 @@ int kvm_init(void)
     const KVMCapabilityInfo *missing_cap;
     int ret;
     int i;
+    int max_vcpus;
 
     s = g_malloc0(sizeof(KVMState));
 
@@ -1256,6 +1274,13 @@ int kvm_init(void)
         goto err;
     }
 
+    max_vcpus = kvm_max_vcpus(s);
+    if (smp_cpus > max_vcpus) {
+        fprintf(stderr, "Number of SMP cpus requested (%d) exceeds max cpus "
+                "supported by KVM (%d)\n", smp_cpus, max_vcpus);
+        exit(1);
+    }
+
     s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
     if (s->vmfd < 0) {
 #ifdef TARGET_S390X
Signature · 1 line
1.7.8.6
#1 lore raw
Quoted 23 lines · On Mon, Jul 30, 2012 at 7:22 PM, <[email protected]> wrote:

From: Dunrong Huang <[email protected]>

Add a helper function for fetching max cpus supported by kvm.

Make QEMU exit with an error message if smp_cpus exceeds limit of VCPU count retrieved by invoking this helper function.

---
 kvm-all.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
kvm-all.c +3 -0
index 2148b20..8cb4b92 100644
@@ -1207,6 +1207,23 @@ static int kvm_irqchip_create(KVMState *s)
     return 0;
 }
 
+static int kvm_max_vcpus(KVMState *s)
+{
+    int max_vcpus = 4;

kvm-all.c static int kvm_irqchip_create(KVMState *s) · line 1222

Quoted 10 lines
kvm-all.c +10 -0
+    int ret;
+    ret = kvm_check_extension(s, KVM_CAP_MAX_VCPUS);
+    if (ret) {
+        max_vcpus = ret;
+    } else {
+       ret = kvm_check_extension(s, KVM_CAP_NR_VCPUS);
+        if (ret) {
+            max_vcpus = ret;
+        }
+   }

The indentation is off here. It should be 4 spaces.

Otherwise looks fine.

Stefan

#2 lore raw
In reply to [email protected]
Quoted 6 lines · On 30 July 2012 19:22, <[email protected]> wrote:
kvm-all.c +6 -0
+static int kvm_max_vcpus(KVMState *s)
+{
+    int max_vcpus = 4;
+    int ret;
+    ret = kvm_check_extension(s, KVM_CAP_MAX_VCPUS);
+    if (ret) {

kvm-all.c static int kvm_irqchip_create(KVMState *s) · line 1225

Quoted 10 lines
kvm-all.c +10 -0
+        max_vcpus = ret;
+    } else {
+       ret = kvm_check_extension(s, KVM_CAP_NR_VCPUS);
+        if (ret) {
+            max_vcpus = ret;
+        }
+   }
+
+    return max_vcpus;
+}

A small thing, but I think having code flow like:

    /* Find number of supported CPUs using the recommended
     * procedure from the kernel API documentation to cope with
     * older kernels that may be missing capabilities.
     */
    ret = kvm_check_extension(s, KVM_CAP_MAX_VCPUS);
    if (ret) {
        return ret;
    }
    ret = kvm_check_extension(s, KVM_CAP_NR_VCPUS);
    if (ret) {
        return ret;
    }
    return 4;

would be clearer.

(also I think a comment helps suggest that 4 isn't a magic number we made up ourselves :-))

-- PMM