Skip to contentSkip to conversation

machine: Disallow specifying topology parameters as zero

PATCH Superseded Yanan Wang 4 participants 10 messages Jul 22, 2021

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Yanan Wang

author
#0 lore raw

cover machine: Disallow specifying topology parameters as zero

In the SMP configuration, we should either specify a topology parameter with a reasonable value (equal to or greater than 1) or just leave it omitted and QEMU will calculate its value. Configurations which explicitly specify the topology parameters as zero like "sockets=0" are meaningless, so disallow them.

However; the commit 1e63fe685804d
(machine: pass QAPI struct to mc->smp_parse) has documented that '0' has the same semantics as omitting a parameter in the qapi comment for SMPConfiguration. So this patch fixes the doc and also adds the corresponding sanity check in the smp parsers.

This patch originly comes form [1], and it was suggested that this patch fixing the doc should be sent for 6.1 to avoid a deprecation process in the future.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Yanan Wang (1):
machine: Disallow specifying topology parameters as zero

 hw/core/machine.c | 30 ++++++++++++++++++++++--------
 hw/i386/pc.c      | 33 ++++++++++++++++++++++++---------
 qapi/machine.json |  6 +++---
 3 files changed; 49 insertions(+); 20 deletions(-)

--
2.19.1

author
#1 lore raw

PATCH 1/1 machine: Disallow specifying topology parameters as zero

In the SMP configuration, we should either specify a topology parameter with a reasonable value (equal to or greater than 1) or just leave it omitted and QEMU will calculate its value. Configurations which explicitly specify the topology parameters as zero like "sockets=0" are meaningless, so disallow them.

However, the commit 1e63fe685804d
(machine: pass QAPI struct to mc->smp_parse) has documented that '0' has the same semantics as omitting a parameter in the qapi comment for SMPConfiguration. So this patch fixes the doc and also adds the corresponding sanity check in the smp parsers.

---
 hw/core/machine.c | 30 ++++++++++++++++++++++--------
 hw/i386/pc.c      | 33 ++++++++++++++++++++++++---------
 qapi/machine.json |  6 +++---
 3 files changed, 49 insertions(+), 20 deletions(-)
hw/core/machine.c +22 -8
index 775add0795..ada300542b 100644
@@ -745,11 +745,24 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
 {
     unsigned cpus    = config->has_cpus ? config->cpus : 0;
     unsigned sockets = config->has_sockets ? config->sockets : 0;
+    unsigned dies    = config->has_dies ? config->dies : 0;
     unsigned cores   = config->has_cores ? config->cores : 0;
     unsigned threads = config->has_threads ? config->threads : 0;
+    unsigned maxcpus = config->has_maxcpus ? config->maxcpus : 0;
+
+    if ((config->has_cpus && config->cpus == 0) ||
+        (config->has_sockets && config->sockets == 0) ||
+        (config->has_dies && config->dies == 0) ||
+        (config->has_cores && config->cores == 0) ||
+        (config->has_threads && config->threads == 0) ||
+        (config->has_maxcpus && config->maxcpus == 0)) {
+        error_setg(errp, "parameters must be equal to or greater than one if provided");
+        return;
+    }
 
-    if (config->has_dies && config->dies != 0 && config->dies != 1) {
+    if (dies > 1) {
         error_setg(errp, "dies not supported by this machine's CPU topology");
+        return;
     }
 
     /* compute missing values, prefer sockets over cores over threads */
@@ -760,8 +773,8 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
             sockets = sockets > 0 ? sockets : 1;
             cpus = cores * threads * sockets;
         } else {
-            ms->smp.max_cpus = config->has_maxcpus ? config->maxcpus : cpus;
-            sockets = ms->smp.max_cpus / (cores * threads);
+            maxcpus = maxcpus > 0 ? maxcpus : cpus;
+            sockets = maxcpus / (cores * threads);
         }
     } else if (cores == 0) {
         threads = threads > 0 ? threads : 1;
@@ -778,26 +791,27 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
         return;
     }
 
-    ms->smp.max_cpus = config->has_maxcpus ? config->maxcpus : cpus;
+    maxcpus = maxcpus > 0 ? maxcpus : cpus;
 
-    if (ms->smp.max_cpus < cpus) {
+    if (maxcpus < cpus) {
         error_setg(errp, "maxcpus must be equal to or greater than smp");
         return;
     }
 
-    if (sockets * cores * threads != ms->smp.max_cpus) {
+    if (sockets * cores * threads != maxcpus) {
         error_setg(errp, "Invalid CPU topology: "
                    "sockets (%u) * cores (%u) * threads (%u) "
                    "!= maxcpus (%u)",
                    sockets, cores, threads,
-                   ms->smp.max_cpus);
+                   maxcpus);
         return;
     }
 
     ms->smp.cpus = cpus;
+    ms->smp.sockets = sockets;
     ms->smp.cores = cores;
     ms->smp.threads = threads;
-    ms->smp.sockets = sockets;
+    ms->smp.max_cpus = maxcpus;
 }
 
 static void machine_get_smp(Object *obj, Visitor *v, const char *name,
hw/i386/pc.c +24 -9
index c2b9d62a35..db93841097 100644
@@ -716,9 +716,23 @@ static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **err
 {
     unsigned cpus    = config->has_cpus ? config->cpus : 0;
     unsigned sockets = config->has_sockets ? config->sockets : 0;
-    unsigned dies    = config->has_dies ? config->dies : 1;
+    unsigned dies    = config->has_dies ? config->dies : 0;
     unsigned cores   = config->has_cores ? config->cores : 0;
     unsigned threads = config->has_threads ? config->threads : 0;
+    unsigned maxcpus = config->has_maxcpus ? config->maxcpus : 0;
+
+    if ((config->has_cpus && config->cpus == 0) ||
+        (config->has_sockets && config->sockets == 0) ||
+        (config->has_dies && config->dies == 0) ||
+        (config->has_cores && config->cores == 0) ||
+        (config->has_threads && config->threads == 0) ||
+        (config->has_maxcpus && config->maxcpus == 0)) {
+        error_setg(errp, "parameters must be equal to or greater than one if provided");
+        return;
+    }
+
+    /* directly default dies to 1 if it's omitted */
+    dies = dies > 0 ? dies : 1;
 
     /* compute missing values, prefer sockets over cores over threads */
     if (cpus == 0 || sockets == 0) {
@@ -728,8 +742,8 @@ static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **err
             sockets = sockets > 0 ? sockets : 1;
             cpus = cores * threads * dies * sockets;
         } else {
-            ms->smp.max_cpus = config->has_maxcpus ? config->maxcpus : cpus;
-            sockets = ms->smp.max_cpus / (cores * threads * dies);
+            maxcpus = maxcpus > 0 ? maxcpus : cpus;
+            sockets = maxcpus / (cores * threads * dies);
         }
     } else if (cores == 0) {
         threads = threads > 0 ? threads : 1;
@@ -746,27 +760,28 @@ static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **err
         return;
     }
 
-    ms->smp.max_cpus = config->has_maxcpus ? config->maxcpus : cpus;
+    maxcpus = maxcpus > 0 ? maxcpus : cpus;
 
-    if (ms->smp.max_cpus < cpus) {
+    if (maxcpus < cpus) {
         error_setg(errp, "maxcpus must be equal to or greater than smp");
         return;
     }
 
-    if (sockets * dies * cores * threads != ms->smp.max_cpus) {
+    if (sockets * dies * cores * threads != maxcpus) {
         error_setg(errp, "Invalid CPU topology deprecated: "
                    "sockets (%u) * dies (%u) * cores (%u) * threads (%u) "
                    "!= maxcpus (%u)",
                    sockets, dies, cores, threads,
-                   ms->smp.max_cpus);
+                   maxcpus);
         return;
     }
 
     ms->smp.cpus = cpus;
-    ms->smp.cores = cores;
-    ms->smp.threads = threads;
     ms->smp.sockets = sockets;
     ms->smp.dies = dies;
+    ms->smp.cores = cores;
+    ms->smp.threads = threads;
+    ms->smp.max_cpus = maxcpus;
 }
 
 static
qapi/machine.json +3 -3
index c3210ee1fb..9272cb3cf8 100644
@@ -1288,8 +1288,8 @@
 ##
 # @SMPConfiguration:
 #
-# Schema for CPU topology configuration.  "0" or a missing value lets
-# QEMU figure out a suitable value based on the ones that are provided.
+# Schema for CPU topology configuration. A missing value lets QEMU
+# figure out a suitable value based on the ones that are provided.
 #
 # @cpus: number of virtual CPUs in the virtual machine
 #
@@ -1297,7 +1297,7 @@
 #
 # @dies: number of dies per socket in the CPU topology
 #
-# @cores: number of cores per thread in the CPU topology
+# @cores: number of cores per die in the CPU topology
 #
 # @threads: number of threads per core in the CPU topology
 #
Signature · 1 line
2.19.1
#2 lore raw
In reply to Yanan Wang
Quoted 11 lines · Yanan Wang

In the SMP configuration, we should either specify a topology parameter with a reasonable value (equal to or greater than 1) or just leave it omitted and QEMU will calculate its value. Configurations which explicitly specify the topology parameters as zero like "sockets=0" are meaningless, so disallow them.

However; the commit 1e63fe685804d
(machine: pass QAPI struct to mc->smp_parse) has documented that '0' has the same semantics as omitting a parameter in the qapi comment for SMPConfiguration. So this patch fixes the doc and also adds the corresponding sanity check in the smp parsers.

Are we expecting any real users to have used that 'parameter=0' behaviour? I expect that libvirt and other management software already did the right thing; unfortunately, sometimes weird configuration lines tend to persist in search results.

Quoted 14 lines

This patch originly comes form [1], and it was suggested that this patch fixing the doc should be sent for 6.1 to avoid a deprecation process in the future.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Yanan Wang (1):
machine: Disallow specifying topology parameters as zero

 hw/core/machine.c | 30 ++++++++++++++++++++++--------
 hw/i386/pc.c      | 33 ++++++++++++++++++++++++---------
 qapi/machine.json |  6 +++---
 3 files changed; 49 insertions(+); 20 deletions(-)
#3 lore raw
Quoted 13 lines · Cornelia Huck
Quoted 11 lines · Yanan Wang

In the SMP configuration, we should either specify a topology parameter with a reasonable value (equal to or greater than 1) or just leave it omitted and QEMU will calculate its value. Configurations which explicitly specify the topology parameters as zero like "sockets=0" are meaningless, so disallow them.

However; the commit 1e63fe685804d
(machine: pass QAPI struct to mc->smp_parse) has documented that '0' has the same semantics as omitting a parameter in the qapi comment for SMPConfiguration. So this patch fixes the doc and also adds the corresponding sanity check in the smp parsers.

Quoted 4 lines

Are we expecting any real users to have used that 'parameter=0' behaviour? I expect that libvirt and other management software already did the right thing; unfortunately, sometimes weird configuration lines tend to persist in search results.

I think there may not any users who have already used a configuration with explicit "parameter=0", instead it should have been just omitted. Yes, libvirt now rejects "parameter=0" when parsing XML, but we now still allows "parameter=0" in the direct QEMU cmdlines. If we hope to disallow this kind of config thoroughly, we'd better also have a sanity check in QEMU.

Thanks,
Yanan

Quoted 14 lines
Quoted 6 lines

This patch originly comes form [1], and it was suggested that this patch fixing the doc should be sent for 6.1 to avoid a deprecation process in the future.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 7 lines

Yanan Wang (1):
machine: Disallow specifying topology parameters as zero

  hw/core/machine.c | 30 ++++++++++++++++++++++--------
  hw/i386/pc.c      | 33 ++++++++++++++++++++++++---------
  qapi/machine.json |  6 +++---
  3 files changed; 49 insertions(+); 20 deletions(-)

.

#4 lore raw
In reply to Cornelia Huck
Quoted 14 lines · Cornelia Huck
Quoted 11 lines · Yanan Wang

In the SMP configuration, we should either specify a topology parameter with a reasonable value (equal to or greater than 1) or just leave it omitted and QEMU will calculate its value. Configurations which explicitly specify the topology parameters as zero like "sockets=0" are meaningless, so disallow them.

However; the commit 1e63fe685804d
(machine: pass QAPI struct to mc->smp_parse) has documented that '0' has the same semantics as omitting a parameter in the qapi comment for SMPConfiguration. So this patch fixes the doc and also adds the corresponding sanity check in the smp parsers.

Quoted 4 lines

Are we expecting any real users to have used that 'parameter=0' behaviour? I expect that libvirt and other management software already did the right thing; unfortunately, sometimes weird configuration lines tend to persist in search results.

I understand this concern. I think the only documentation we had prior to commit 1e63fe685804 was

DEF("smp", HAS_ARG, QEMU_OPTION_smp,
"-smp [cpus=]n[,maxcpus=cpus][,cores=cores][,threads=threads][,dies=dies][,sockets=sockets]\n"
" set the number of CPUs to 'n' [default=1]\n"
" maxcpus= maximum number of total cpus, including\n"
" offline CPUs for hotplug, etc\n"
" cores= number of CPU cores on one socket (for PC, it's on one die)\n"
" threads= number of threads on one CPU core\n"
" dies= number of CPU dies on one socket (for PC only)\n"
" sockets= number of discrete sockets in the system\n",
QEMU_ARCH_ALL)
SRST
``-smp [cpus=]n[,cores=cores][,threads=threads][,dies=dies][,sockets=sockets][,maxcpus=maxcpus]``
Simulate an SMP system with n CPUs. On the PC target, up to 255 CPUs
are supported. On Sparc32 target, Linux limits the number of usable
CPUs to 4. For the PC target, the number of cores per die, the
number of threads per cores, the number of dies per packages and the
total number of sockets can be specified. Missing values will be
computed. If any on the three values is given, the total number of
CPUs n can be omitted. maxcpus specifies the maximum number of
hotpluggable CPUs.
ERST

This doesn't mention zero inputs and even implies non-zero inputs.

I'm not sure if we need to worry about the odd command line that used zero for some parameters. What do you think?

Thanks,
drew

Quoted 16 lines
Quoted 15 lines

This patch originly comes form [1], and it was suggested that this patch fixing the doc should be sent for 6.1 to avoid a deprecation process in the future.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Yanan Wang (1):
machine: Disallow specifying topology parameters as zero

 hw/core/machine.c | 30 ++++++++++++++++++++++--------
 hw/i386/pc.c      | 33 ++++++++++++++++++++++++---------
 qapi/machine.json |  6 +++---
 3 files changed; 49 insertions(+); 20 deletions(-)
#5 lore raw
Quoted 45 lines · Andrew Jones
Quoted 14 lines · Cornelia Huck
Quoted 11 lines · Yanan Wang

In the SMP configuration, we should either specify a topology parameter with a reasonable value (equal to or greater than 1) or just leave it omitted and QEMU will calculate its value. Configurations which explicitly specify the topology parameters as zero like "sockets=0" are meaningless, so disallow them.

However; the commit 1e63fe685804d
(machine: pass QAPI struct to mc->smp_parse) has documented that '0' has the same semantics as omitting a parameter in the qapi comment for SMPConfiguration. So this patch fixes the doc and also adds the corresponding sanity check in the smp parsers.

Quoted 4 lines

Are we expecting any real users to have used that 'parameter=0' behaviour? I expect that libvirt and other management software already did the right thing; unfortunately, sometimes weird configuration lines tend to persist in search results.

I understand this concern. I think the only documentation we had prior to commit 1e63fe685804 was

DEF("smp", HAS_ARG, QEMU_OPTION_smp,
"-smp [cpus=]n[,maxcpus=cpus][,cores=cores][,threads=threads][,dies=dies][,sockets=sockets]\n"
" set the number of CPUs to 'n' [default=1]\n"
" maxcpus= maximum number of total cpus, including\n"
" offline CPUs for hotplug, etc\n"
" cores= number of CPU cores on one socket (for PC, it's on one die)\n"
" threads= number of threads on one CPU core\n"
" dies= number of CPU dies on one socket (for PC only)\n"
" sockets= number of discrete sockets in the system\n",
QEMU_ARCH_ALL)
SRST
``-smp [cpus=]n[,cores=cores][,threads=threads][,dies=dies][,sockets=sockets][,maxcpus=maxcpus]``
Simulate an SMP system with n CPUs. On the PC target, up to 255 CPUs
are supported. On Sparc32 target, Linux limits the number of usable
CPUs to 4. For the PC target, the number of cores per die, the
number of threads per cores, the number of dies per packages and the
total number of sockets can be specified. Missing values will be
computed. If any on the three values is given, the total number of
CPUs n can be omitted. maxcpus specifies the maximum number of
hotpluggable CPUs.
ERST

Quoted 1 line

This doesn't mention zero inputs and even implies non-zero inputs.

Yes, hopefully that kept people away from using 0 magic, unless they read the code.

Quoted 3 lines

I'm not sure if we need to worry about the odd command line that used zero for some parameters. What do you think?

I did a cursory search for bad examples, and nothing popped up. So this should be reasonably painless.

#6 lore raw
In reply to Andrew Jones
Quoted 4 lines · Andrew Jones

This doesn't mention zero inputs and even implies non-zero inputs.

I'm not sure if we need to worry about the odd command line that used zero for some parameters. What do you think?

I think I agree as well, however the patch that Yanan sent has unnecessary duplication between smp_parse and pc_smp_parse. machine_set_smp is a better place to implement this kind of check.

Paolo

#7 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 4 lines · Andrew Jones

This doesn't mention zero inputs and even implies non-zero inputs.

I'm not sure if we need to worry about the odd command line that used zero for some parameters. What do you think?

I think I agree as well, however the patch that Yanan sent has unnecessary duplication between smp_parse and pc_smp_parse. machine_set_smp is a better place to implement this kind of check.

The smp_parse and pc_smp_parse are going to be converted into a generic parser, and the added sanity-check in this patch will also be tested in an unit test. So is it probably better to keep the check in the parser instead of the caller? The duplication will be eliminated anyway when there is one single parser.

But I can also implement the check in machine_set_smp as you mentioned if it's more reasonable and preferred. :)

Thanks,
Yanan
.

#8 lore raw
Quoted 8 lines · wangyanan (Y)

The smp_parse and pc_smp_parse are going to be converted into a generic parser, and the added sanity-check in this patch will also be tested in an unit test. So is it probably better to keep the check in the parser instead of the caller? The duplication will be eliminated anyway when there is one single parser.

But I can also implement the check in machine_set_smp as you mentioned if it's more reasonable and preferred. :)

Yes, I would prefer to avoid having duplicate code. There are some common checks already in machine_set_smp, e.g. comparing ms->smp.cpus against mc->min_cpus and mc->max_cpus.

Paolo

#9 lore raw
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · wangyanan (Y)

The smp_parse and pc_smp_parse are going to be converted into a generic parser, and the added sanity-check in this patch will also be tested in an unit test. So is it probably better to keep the check in the parser instead of the caller? The duplication will be eliminated anyway when there is one single parser.

Quoted 6 lines
Quoted 2 lines

But I can also implement the check in machine_set_smp as you mentioned if it's more reasonable and preferred. :)

Yes, I would prefer to avoid having duplicate code.  There are some common checks already in machine_set_smp, e.g. comparing ms->smp.cpus against mc->min_cpus and mc->max_cpus.

Ok, I will send a v2.

Thanks,
Yanan
.