Skip to contentSkip to conversation

vl: smp_parse sanity checks

PATCH Superseded Eduardo Habkost 2 participants 6 messages Nov 11, 2014 – Nov 12, 2014

lore
Share & tools
lore
#0 lore raw

v2 cover vl: smp_parse sanity checks

Quoted 1 line

From the original series sent by Andrew, only patch 1/3 was kept. Patch 2/3

from v1 was replaced by a safer fix. Patch 3/3 was removed because it generates a warning even on the most common use-case ("-smp <n>" without any extra options).

Andrew Jones (1):
vl: fix max_cpus check

Eduardo Habkost (2):
vl: Avoid unnecessary 'if' nesting
vl: Don't silently change topology when all -smp options were set

 vl.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH v2 1/3 vl: Avoid unnecessary 'if' nesting

Just a coding style change, to make other changes easier to review.

---
 vl.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
vl.c +4 -6
index f4a6e5e..c1bfc9b 100644
@@ -1284,13 +1284,11 @@ static void smp_parse(QemuOpts *opts)
             if (cpus == 0) {
                 cpus = cores * threads * sockets;
             }
+        } else if (cores == 0) {
+            threads = threads > 0 ? threads : 1;
+            cores = cpus / (sockets * threads);
         } else {
-            if (cores == 0) {
-                threads = threads > 0 ? threads : 1;
-                cores = cpus / (sockets * threads);
-            } else {
-                threads = cpus / (cores * sockets);
-            }
+            threads = cpus / (cores * sockets);
         }
 
         max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
Signature · 1 line
1.9.3
Reviewed-by on v2 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] vl: Avoid unnecessary 'if' nesting

Quoted 32 lines · Eduardo Habkost

Just a coding style change, to make other changes easier to review.

---
 vl.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
vl.c +4 -6
index f4a6e5e..c1bfc9b 100644
@@ -1284,13 +1284,11 @@ static void smp_parse(QemuOpts *opts)
             if (cpus == 0) {
                 cpus = cores * threads * sockets;
             }
+        } else if (cores == 0) {
+            threads = threads > 0 ? threads : 1;
+            cores = cpus / (sockets * threads);
         } else {
-            if (cores == 0) {
-                threads = threads > 0 ? threads : 1;
-                cores = cpus / (sockets * threads);
-            } else {
-                threads = cpus / (cores * sockets);
-            }
+            threads = cpus / (cores * sockets);
         }
 
         max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
Signature · 1 line
1.9.3
#3 lore raw

PATCH v2 2/3 vl: fix max_cpus check

In reply to Eduardo Habkost

From: Andrew Jones <[email protected]>

We should confirm max_cpus, which is >= smp_cpus, is <= the machine's true max_cpus, not just smp_cpus.

---
 vl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
vl.c +2 -2
index c1bfc9b..2ed8b07 100644
@@ -3901,9 +3901,9 @@ int main(int argc, char **argv, char **envp)
     smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
 
     machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
-    if (smp_cpus > machine_class->max_cpus) {
+    if (max_cpus > machine_class->max_cpus) {
         fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
-                "supported by machine `%s' (%d)\n", smp_cpus,
+                "supported by machine `%s' (%d)\n", max_cpus,
                 machine_class->name, machine_class->max_cpus);
         exit(1);
     }
Signature · 1 line
1.9.3
#4 lore raw

PATCH v2 3/3 vl: Don't silently change topology when all -smp options were set

In reply to Eduardo Habkost

QEMU tries to change the "threads" option even if it was explicitly set in the command-line, and it shouldn't do that.

The right thing to do when all options (cpus, sockets, cores, threds) are explicitly set is to sanity check them and abort in case they don't make sense (i.e. when sockets*cores*threads < cpus).

---
 vl.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
vl.c +7 -1
index 2ed8b07..8880a4e 100644
@@ -1287,8 +1287,14 @@ static void smp_parse(QemuOpts *opts)
         } else if (cores == 0) {
             threads = threads > 0 ? threads : 1;
             cores = cpus / (sockets * threads);
-        } else {
+        } else if (threads == 0) {
             threads = cpus / (cores * sockets);
+        } else if (sockets * cores * threads < cpus) {
+            fprintf(stderr, "cpu topology: error: "
+                    "sockets (%u) * cores (%u) * threads (%u) < "
+                    "smp_cpus (%u)\n",
+                    sockets, cores, threads, cpus);
+            exit(1);
         }
 
         max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
Signature · 1 line
1.9.3
Reviewed-by on v2 3/3
#5 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] vl: Don't silently change topology when all -smp options were set

Quoted 35 lines · Eduardo Habkost

QEMU tries to change the "threads" option even if it was explicitly set in the command-line, and it shouldn't do that.

The right thing to do when all options (cpus, sockets, cores, threds) are explicitly set is to sanity check them and abort in case they don't make sense (i.e. when sockets*cores*threads < cpus).

---
 vl.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
vl.c +7 -1
index 2ed8b07..8880a4e 100644
@@ -1287,8 +1287,14 @@ static void smp_parse(QemuOpts *opts)
         } else if (cores == 0) {
             threads = threads > 0 ? threads : 1;
             cores = cpus / (sockets * threads);
-        } else {
+        } else if (threads == 0) {
             threads = cpus / (cores * sockets);
+        } else if (sockets * cores * threads < cpus) {
+            fprintf(stderr, "cpu topology: error: "
+                    "sockets (%u) * cores (%u) * threads (%u) < "
+                    "smp_cpus (%u)\n",
+                    sockets, cores, threads, cpus);
+            exit(1);
         }
 
         max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
Signature · 1 line
1.9.3