Skip to contentSkip to conversation

hw/core: fix error checkig in smp_parse

PATCH Applied Philippe Mathieu-Daudé 2 participants 6 messages Aug 13, 2021

lore
Share & tools
lore
#0 lore raw

v2 cover hw/core: fix error checkig in smp_parse

Respin of Daniel's series checking MachineClass::smp_parse() return value instead of *errp.

Daniel P. Berrangé (1):
hw/core: fix error checkig in smp_parse

Philippe Mathieu-Daudé (2):
hw/core: Add missing return on error
hw/core: Have MachineClass::smp_parse() return boolean on error

 include/hw/boards.h |  2 +-
 hw/core/machine.c   | 14 ++++++++------
 hw/i386/pc.c        | 10 ++++++----
 3 files changed, 15 insertions(+), 11 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/3 hw/core: Add missing return on error

If dies is not supported by this machine's CPU topology, don't keep processing options and return directly.

Fixes 0aebebb561c ("machine: reject -smp dies!=1 for non-PC machines")
---
 hw/core/machine.c | 1 +
 1 file changed, 1 insertion(+)
hw/core/machine.c +1 -0
index 943974d411c..abaeda589b7 100644
@@ -752,6 +752,7 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
 
     if (config->has_dies && config->dies != 0 && config->dies != 1) {
         error_setg(errp, "dies not supported by this machine's CPU topology");
+        return;
     }
 
     /* compute missing values, prefer sockets over cores over threads */
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH-for-6.1? v2 1/3] hw/core: Add missing return on error

Quoted 15 lines · Philippe Mathieu-Daudé

If dies is not supported by this machine's CPU topology, don't keep processing options and return directly.

Fixes 0aebebb561c ("machine: reject -smp dies!=1 for non-PC machines")
---
  hw/core/machine.c | 1 +
  1 file changed, 1 insertion(+)
hw/core/machine.c +0 -0
index 943974d411c..abaeda589b7 100644

hw/core/machine.c static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp) · line 758

Quoted 7 lines
hw/core/machine.c +1 -0
@@ -752,6 +752,7 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
 
      if (config->has_dies && config->dies != 0 && config->dies != 1) {
          error_setg(errp, "dies not supported by this machine's CPU topology");
+        return;
      }
 
      /* compute missing values, prefer sockets over cores over threads */

Queued, thanks.

Paolo

#3 lore raw

PATCH v2 2/3 hw/core: Have MachineClass::smp_parse() return boolean on error

In reply to Philippe Mathieu-Daudé

Just for consistency, following the example documented since commit e3fe3988d7 ("error: Document Error API usage rules"), return a boolean value indicating an error is set or not. Directly pass errp as the local_err is not requested in our case.

---
 include/hw/boards.h |  2 +-
 hw/core/machine.c   | 12 +++++++-----
 hw/i386/pc.c        | 10 ++++++----
 3 files changed, 14 insertions(+), 10 deletions(-)
include/hw/boards.h +1 -1
index accd6eff35a..d5b7058c2e2 100644
@@ -209,7 +209,7 @@ struct MachineClass {
     void (*reset)(MachineState *state);
     void (*wakeup)(MachineState *state);
     int (*kvm_type)(MachineState *machine, const char *arg);
-    void (*smp_parse)(MachineState *ms, SMPConfiguration *config, Error **errp);
+    bool (*smp_parse)(MachineState *ms, SMPConfiguration *config, Error **errp);
 
     BlockInterfaceType block_default_type;
     int units_per_default_bus;
hw/core/machine.c +7 -5
index abaeda589b7..159c6b098e2 100644
@@ -743,7 +743,7 @@ void machine_set_cpu_numa_node(MachineState *machine,
     }
 }
 
-static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
+static bool smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
 {
     unsigned cpus    = config->has_cpus ? config->cpus : 0;
     unsigned sockets = config->has_sockets ? config->sockets : 0;
@@ -752,7 +752,7 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
 
     if (config->has_dies && config->dies != 0 && config->dies != 1) {
         error_setg(errp, "dies not supported by this machine's CPU topology");
-        return;
+        return true;
     }
 
     /* compute missing values, prefer sockets over cores over threads */
@@ -778,14 +778,14 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
                    "sockets (%u) * cores (%u) * threads (%u) < "
                    "smp_cpus (%u)",
                    sockets, cores, threads, cpus);
-        return;
+        return true;
     }
 
     ms->smp.max_cpus = config->has_maxcpus ? config->maxcpus : cpus;
 
     if (ms->smp.max_cpus < cpus) {
         error_setg(errp, "maxcpus must be equal to or greater than smp");
-        return;
+        return true;
     }
 
     if (sockets * cores * threads != ms->smp.max_cpus) {
@@ -794,13 +794,15 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
                    "!= maxcpus (%u)",
                    sockets, cores, threads,
                    ms->smp.max_cpus);
-        return;
+        return true;
     }
 
     ms->smp.cpus = cpus;
     ms->smp.cores = cores;
     ms->smp.threads = threads;
     ms->smp.sockets = sockets;
+
+    return false;
 }
 
 static void machine_get_smp(Object *obj, Visitor *v, const char *name,
hw/i386/pc.c +6 -4
index c2b9d62a358..84138a8bfd2 100644
@@ -712,7 +712,7 @@ void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
  * This function is very similar to smp_parse()
  * in hw/core/machine.c but includes CPU die support.
  */
-static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
+static bool pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
 {
     unsigned cpus    = config->has_cpus ? config->cpus : 0;
     unsigned sockets = config->has_sockets ? config->sockets : 0;
@@ -743,14 +743,14 @@ static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **err
                    "sockets (%u) * dies (%u) * cores (%u) * threads (%u) < "
                    "smp_cpus (%u)",
                    sockets, dies, cores, threads, cpus);
-        return;
+        return true;
     }
 
     ms->smp.max_cpus = config->has_maxcpus ? config->maxcpus : cpus;
 
     if (ms->smp.max_cpus < cpus) {
         error_setg(errp, "maxcpus must be equal to or greater than smp");
-        return;
+        return true;
     }
 
     if (sockets * dies * cores * threads != ms->smp.max_cpus) {
@@ -759,7 +759,7 @@ static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **err
                    "!= maxcpus (%u)",
                    sockets, dies, cores, threads,
                    ms->smp.max_cpus);
-        return;
+        return true;
     }
 
     ms->smp.cpus = cpus;
@@ -767,6 +767,8 @@ static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **err
     ms->smp.threads = threads;
     ms->smp.sockets = sockets;
     ms->smp.dies = dies;
+
+    return false;
 }
 
 static
Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH-for-6.1? v2 2/3] hw/core: Have MachineClass::smp_parse() return boolean on error

Quoted 7 lines · Philippe Mathieu-Daudé

Just for consistency, following the example documented since commit e3fe3988d7 ("error: Document Error API usage rules"), return a boolean value indicating an error is set or not. Directly pass errp as the local_err is not requested in our case.

This was intentional; no one should invoke mc->smp_parse directly except for machine_set_smp, therefore if mc->smp_parse returns bool there are more places where things can go wrong (instead of just the one that is fixed by patch 3).

Paolo

Quoted 124 lines
---
  include/hw/boards.h |  2 +-
  hw/core/machine.c   | 12 +++++++-----
  hw/i386/pc.c        | 10 ++++++----
  3 files changed, 14 insertions(+), 10 deletions(-)
include/hw/boards.h +1 -1
index accd6eff35a..d5b7058c2e2 100644
@@ -209,7 +209,7 @@ struct MachineClass {
      void (*reset)(MachineState *state);
      void (*wakeup)(MachineState *state);
      int (*kvm_type)(MachineState *machine, const char *arg);
-    void (*smp_parse)(MachineState *ms, SMPConfiguration *config, Error **errp);
+    bool (*smp_parse)(MachineState *ms, SMPConfiguration *config, Error **errp);
 
      BlockInterfaceType block_default_type;
      int units_per_default_bus;
hw/core/machine.c +7 -5
index abaeda589b7..159c6b098e2 100644
@@ -743,7 +743,7 @@ void machine_set_cpu_numa_node(MachineState *machine,
      }
  }
 
-static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
+static bool smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
  {
      unsigned cpus    = config->has_cpus ? config->cpus : 0;
      unsigned sockets = config->has_sockets ? config->sockets : 0;
@@ -752,7 +752,7 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
 
      if (config->has_dies && config->dies != 0 && config->dies != 1) {
          error_setg(errp, "dies not supported by this machine's CPU topology");
-        return;
+        return true;
      }
 
      /* compute missing values, prefer sockets over cores over threads */
@@ -778,14 +778,14 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
                     "sockets (%u) * cores (%u) * threads (%u) < "
                     "smp_cpus (%u)",
                     sockets, cores, threads, cpus);
-        return;
+        return true;
      }
 
      ms->smp.max_cpus = config->has_maxcpus ? config->maxcpus : cpus;
 
      if (ms->smp.max_cpus < cpus) {
          error_setg(errp, "maxcpus must be equal to or greater than smp");
-        return;
+        return true;
      }
 
      if (sockets * cores * threads != ms->smp.max_cpus) {
@@ -794,13 +794,15 @@ static void smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
                     "!= maxcpus (%u)",
                     sockets, cores, threads,
                     ms->smp.max_cpus);
-        return;
+        return true;
      }
 
      ms->smp.cpus = cpus;
      ms->smp.cores = cores;
      ms->smp.threads = threads;
      ms->smp.sockets = sockets;
+
+    return false;
  }
 
  static void machine_get_smp(Object *obj, Visitor *v, const char *name,
hw/i386/pc.c +6 -4
index c2b9d62a358..84138a8bfd2 100644
@@ -712,7 +712,7 @@ void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
   * This function is very similar to smp_parse()
   * in hw/core/machine.c but includes CPU die support.
   */
-static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
+static bool pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **errp)
  {
      unsigned cpus    = config->has_cpus ? config->cpus : 0;
      unsigned sockets = config->has_sockets ? config->sockets : 0;
@@ -743,14 +743,14 @@ static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **err
                     "sockets (%u) * dies (%u) * cores (%u) * threads (%u) < "
                     "smp_cpus (%u)",
                     sockets, dies, cores, threads, cpus);
-        return;
+        return true;
      }
 
      ms->smp.max_cpus = config->has_maxcpus ? config->maxcpus : cpus;
 
      if (ms->smp.max_cpus < cpus) {
          error_setg(errp, "maxcpus must be equal to or greater than smp");
-        return;
+        return true;
      }
 
      if (sockets * dies * cores * threads != ms->smp.max_cpus) {
@@ -759,7 +759,7 @@ static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **err
                     "!= maxcpus (%u)",
                     sockets, dies, cores, threads,
                     ms->smp.max_cpus);
-        return;
+        return true;
      }
 
      ms->smp.cpus = cpus;
@@ -767,6 +767,8 @@ static void pc_smp_parse(MachineState *ms, SMPConfiguration *config, Error **err
      ms->smp.threads = threads;
      ms->smp.sockets = sockets;
      ms->smp.dies = dies;
+
+    return false;
  }
 
  static
#5 lore raw

PATCH v2 3/3 hw/core: fix error checkig in smp_parse

In reply to Philippe Mathieu-Daudé

From: Daniel P. Berrangé <[email protected]>

The machine_set_smp() mistakenly checks 'errp' not '*errp', and so thinks there is an error every single time it runs. This causes it to jump to the end of the method, skipping the max CPUs checks. The caller meanwhile sees no error and so carries on execution. The result of all this is:

 $ qemu-system-x86_64 -smp -1
 qemu-system-x86_64: GLib: ../glib/gmem.c:142: failed to allocate 481036337048 bytes

instead of

 $ qemu-system-x86_64 -smp -1
 qemu-system-x86_64: Invalid SMP CPUs -1. The max CPUs supported by machine 'pc-i440fx-6.1' is 255

This is a regression from

commit fe68090e8fbd6e831aaf3fc3bb0459c5cccf14cf
Author: Paolo Bonzini <[email protected]>
Date: Thu May 13 09:03:48 2021 -0400

machine: add smp compound property

Message-Id: <[email protected]>

[PMD: Check MachineClass::smp_parse() returned value]
---
 hw/core/machine.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/core/machine.c +1 -2
index 159c6b098e2..29edf655140 100644
@@ -834,8 +834,7 @@ static void machine_set_smp(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    mc->smp_parse(ms, config, errp);
-    if (errp) {
+    if (mc->smp_parse(ms, config, errp)) {
         goto out_free;
     }
 
Signature · 1 line
2.31.1