[libvirt] [PATCH v5 25/36] qemu_capabilities: Introduce virQEMuCapsMigratablePropsDiff

Chris Venteicher posted 36 patches 7 years, 2 months ago
There is a newer version of this series
[libvirt] [PATCH v5 25/36] qemu_capabilities: Introduce virQEMuCapsMigratablePropsDiff
Posted by Chris Venteicher 7 years, 2 months ago
Create an augmented CPUModelInfo identifying which props are/aren't
migratable based on a diff between migratable and non-migratable
inputs.

This patch pulls existing logic out of virQEMUCapsProbeQMPHostCPU
and wraps the existing logic in a standalone function hopefully
simplifying both functions and making the inputs and outputs clearer.

The patch also sets cpuData->info = NULL to make sure bad data does not
remain in failure cases.

Q) Can the right people quickly determine if they should review this?

Signed-off-by: Chris Venteicher <cventeic@redhat.com>
---
 src/qemu/qemu_capabilities.c | 131 ++++++++++++++++++++++++-----------
 1 file changed, 92 insertions(+), 39 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index cd685298e6..bd75f82e70 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2378,14 +2378,91 @@ virQEMUCapsProbeQMPCPUDefinitions(virQEMUCapsPtr qemuCaps,
 }
 
 
+/* virQEMUCapsMigratablePropsDiff
+ * @migratable: input where all migratable props have value true
+ * and non-migratable and unsupported props have value false
+ *
+ * @nonMigratable: input where all migratable & non-migratable props
+ * have value true and unsupported props have value false
+ *
+ * @augmented: output including all props listed in @migratable but
+ * both migratable & non-migratable props have value true,
+ * unsupported props have value false,
+ * and prop->migratable is set to VIR_TRISTATE_BOOL_{YES/NO}
+ * for each supported prop
+ *
+ * Differences in expanded CPUModelInfo inputs @migratable and @nonMigratable are
+ * used to create output @augmented where individual props have prop->migratable
+ * set to indicate if prop is or isn't migratable.
+ */
+static int
+virQEMUCapsMigratablePropsDiff(qemuMonitorCPUModelInfoPtr migratable,
+                               qemuMonitorCPUModelInfoPtr nonMigratable,
+                               qemuMonitorCPUModelInfoPtr *augmented)
+{
+    int ret = -1;
+    qemuMonitorCPUModelInfoPtr tmp;
+    qemuMonitorCPUPropertyPtr prop;
+    qemuMonitorCPUPropertyPtr mProp;
+    qemuMonitorCPUPropertyPtr nmProp;
+    virHashTablePtr hash = NULL;
+    size_t i;
+
+    *augmented = NULL;
+
+    if (!(tmp = qemuMonitorCPUModelInfoCopy(migratable)))
+        goto cleanup;
+
+    if (!nonMigratable)
+        goto done;
+
+    if (!(hash = virHashCreate(0, NULL)))
+        goto cleanup;
+
+    for (i = 0; i < tmp->nprops; i++) {
+        prop = tmp->props + i;
+
+        if (virHashAddEntry(hash, prop->name, prop) < 0)
+            goto cleanup;
+    }
+
+    for (i = 0; i < nonMigratable->nprops; i++) {
+        nmProp = nonMigratable->props + i;
+
+        if (!(mProp = virHashLookup(hash, nmProp->name)) ||
+            mProp->type != QEMU_MONITOR_CPU_PROPERTY_BOOLEAN ||
+            mProp->type != nmProp->type)
+            continue;  /* In non-migratable list but not in migratable list */
+
+        if (mProp->value.boolean) {
+            mProp->migratable = VIR_TRISTATE_BOOL_YES;
+        } else if (nmProp->value.boolean) {
+            mProp->value.boolean = true;
+            mProp->migratable = VIR_TRISTATE_BOOL_NO;
+        }
+    }
+
+    tmp->migratability = true;
+
+ done:
+    VIR_STEAL_PTR(*augmented, tmp);
+    ret = 0;
+
+ cleanup:
+    qemuMonitorCPUModelInfoFree(tmp);
+    virHashFree(hash);
+    return ret;
+}
+
+
 static int
 virQEMUCapsProbeQMPHostCPU(virQEMUCapsPtr qemuCaps,
                            qemuMonitorPtr mon,
                            bool tcg)
 {
-    qemuMonitorCPUModelInfoPtr modelInfo = NULL;
+    qemuMonitorCPUModelInfoPtr migratable = NULL;
     qemuMonitorCPUModelInfoPtr nonMigratable = NULL;
-    virHashTablePtr hash = NULL;
+    qemuMonitorCPUModelInfoPtr augmented = NULL;
     const char *model;
     qemuMonitorCPUModelExpansionType type;
     virDomainVirtType virtType;
@@ -2404,6 +2481,7 @@ virQEMUCapsProbeQMPHostCPU(virQEMUCapsPtr qemuCaps,
     }
 
     cpuData = virQEMUCapsGetHostCPUData(qemuCaps, virtType);
+    cpuData->info = NULL;
 
     /* Some x86_64 features defined in cpu_map.xml use spelling which differ
      * from the one preferred by QEMU. Static expansion would give us only the
@@ -2415,54 +2493,29 @@ virQEMUCapsProbeQMPHostCPU(virQEMUCapsPtr qemuCaps,
     else
         type = QEMU_MONITOR_CPU_MODEL_EXPANSION_STATIC;
 
-    if (qemuMonitorGetCPUModelExpansion(mon, type, model, true, &modelInfo) < 0)
+    if (qemuMonitorGetCPUModelExpansion(mon, type, model, true, &migratable) < 0)
         goto cleanup;
 
+    if (!migratable) {
+        ret = 0;      /* Qemu can't expand the model name, exit without error */
+        goto cleanup;
+    }
+
     /* Try to check migratability of each feature. */
-    if (modelInfo &&
-        qemuMonitorGetCPUModelExpansion(mon, type, model, false,
+    if (qemuMonitorGetCPUModelExpansion(mon, type, model, false,
                                         &nonMigratable) < 0)
         goto cleanup;
 
-    if (nonMigratable) {
-        qemuMonitorCPUPropertyPtr prop;
-        qemuMonitorCPUPropertyPtr nmProp;
-        size_t i;
+    if (virQEMUCapsMigratablePropsDiff(migratable, nonMigratable, &augmented) < 0)
+        goto cleanup;
 
-        if (!(hash = virHashCreate(0, NULL)))
-            goto cleanup;
-
-        for (i = 0; i < modelInfo->nprops; i++) {
-            prop = modelInfo->props + i;
-            if (virHashAddEntry(hash, prop->name, prop) < 0)
-                goto cleanup;
-        }
-
-        for (i = 0; i < nonMigratable->nprops; i++) {
-            nmProp = nonMigratable->props + i;
-            if (!(prop = virHashLookup(hash, nmProp->name)) ||
-                prop->type != QEMU_MONITOR_CPU_PROPERTY_BOOLEAN ||
-                prop->type != nmProp->type)
-                continue;
-
-            if (prop->value.boolean) {
-                prop->migratable = VIR_TRISTATE_BOOL_YES;
-            } else if (nmProp->value.boolean) {
-                prop->value.boolean = true;
-                prop->migratable = VIR_TRISTATE_BOOL_NO;
-            }
-        }
-
-        modelInfo->migratability = true;
-    }
-
-    VIR_STEAL_PTR(cpuData->info, modelInfo);
+    VIR_STEAL_PTR(cpuData->info, augmented);
     ret = 0;
 
  cleanup:
-    virHashFree(hash);
+    qemuMonitorCPUModelInfoFree(migratable);
     qemuMonitorCPUModelInfoFree(nonMigratable);
-    qemuMonitorCPUModelInfoFree(modelInfo);
+    qemuMonitorCPUModelInfoFree(augmented);
 
     return ret;
 }
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v5 25/36] qemu_capabilities: Introduce virQEMuCapsMigratablePropsDiff
Posted by Jiri Denemark 7 years, 1 month ago
On Sun, Dec 02, 2018 at 23:10:19 -0600, Chris Venteicher wrote:
> Create an augmented CPUModelInfo identifying which props are/aren't
> migratable based on a diff between migratable and non-migratable
> inputs.
> 
> This patch pulls existing logic out of virQEMUCapsProbeQMPHostCPU
> and wraps the existing logic in a standalone function hopefully
> simplifying both functions and making the inputs and outputs clearer.
> 
> The patch also sets cpuData->info = NULL to make sure bad data does not
> remain in failure cases.
> 
> Q) Can the right people quickly determine if they should review this?

This does not belong to a commit message. If you have any comments or
questions, put them after the "---" line.

> 
> Signed-off-by: Chris Venteicher <cventeic@redhat.com>
> ---
>  src/qemu/qemu_capabilities.c | 131 ++++++++++++++++++++++++-----------
>  1 file changed, 92 insertions(+), 39 deletions(-)
> 
> diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
> index cd685298e6..bd75f82e70 100644
> --- a/src/qemu/qemu_capabilities.c
> +++ b/src/qemu/qemu_capabilities.c
> @@ -2378,14 +2378,91 @@ virQEMUCapsProbeQMPCPUDefinitions(virQEMUCapsPtr qemuCaps,
>  }
>  
>  
> +/* virQEMUCapsMigratablePropsDiff

The name is quite misleading. The function does not produce a diff, it
just uses two different views on CPU props to set migratable flags for
each prop.

> + * @migratable: input where all migratable props have value true
> + * and non-migratable and unsupported props have value false
> + *
> + * @nonMigratable: input where all migratable & non-migratable props
> + * have value true and unsupported props have value false
> + *
> + * @augmented: output including all props listed in @migratable but
> + * both migratable & non-migratable props have value true,
> + * unsupported props have value false,
> + * and prop->migratable is set to VIR_TRISTATE_BOOL_{YES/NO}
> + * for each supported prop

I think it would make more sense to just return the augmented ModelInfo.
The function will never set @augmented = NULL on success and thus we can
return NULL on error. That way you could get rid of the strange tmp
variable, which is quite a bit more than just a simple temporary
variable.

> + *
> + * Differences in expanded CPUModelInfo inputs @migratable and @nonMigratable are
> + * used to create output @augmented where individual props have prop->migratable
> + * set to indicate if prop is or isn't migratable.
> + */
> +static int
> +virQEMUCapsMigratablePropsDiff(qemuMonitorCPUModelInfoPtr migratable,
> +                               qemuMonitorCPUModelInfoPtr nonMigratable,
> +                               qemuMonitorCPUModelInfoPtr *augmented)
> +{
> +    int ret = -1;
> +    qemuMonitorCPUModelInfoPtr tmp;
> +    qemuMonitorCPUPropertyPtr prop;
> +    qemuMonitorCPUPropertyPtr mProp;
> +    qemuMonitorCPUPropertyPtr nmProp;
> +    virHashTablePtr hash = NULL;
> +    size_t i;
> +
> +    *augmented = NULL;
> +
> +    if (!(tmp = qemuMonitorCPUModelInfoCopy(migratable)))
> +        goto cleanup;
> +
> +    if (!nonMigratable)
> +        goto done;
> +
> +    if (!(hash = virHashCreate(0, NULL)))
> +        goto cleanup;
> +
> +    for (i = 0; i < tmp->nprops; i++) {
> +        prop = tmp->props + i;
> +
> +        if (virHashAddEntry(hash, prop->name, prop) < 0)
> +            goto cleanup;
> +    }
> +
> +    for (i = 0; i < nonMigratable->nprops; i++) {
> +        nmProp = nonMigratable->props + i;

"nmProp" and "mProp" look very similar, it took me some time to realize
they are different.

> +
> +        if (!(mProp = virHashLookup(hash, nmProp->name)) ||
> +            mProp->type != QEMU_MONITOR_CPU_PROPERTY_BOOLEAN ||
> +            mProp->type != nmProp->type)
> +            continue;  /* In non-migratable list but not in migratable list */
> +
> +        if (mProp->value.boolean) {
> +            mProp->migratable = VIR_TRISTATE_BOOL_YES;
> +        } else if (nmProp->value.boolean) {
> +            mProp->value.boolean = true;
> +            mProp->migratable = VIR_TRISTATE_BOOL_NO;
> +        }
> +    }
> +
> +    tmp->migratability = true;
> +
> + done:
> +    VIR_STEAL_PTR(*augmented, tmp);
> +    ret = 0;
> +
> + cleanup:
> +    qemuMonitorCPUModelInfoFree(tmp);
> +    virHashFree(hash);
> +    return ret;
> +}
> +
> +
>  static int
>  virQEMUCapsProbeQMPHostCPU(virQEMUCapsPtr qemuCaps,
>                             qemuMonitorPtr mon,
>                             bool tcg)
>  {
> -    qemuMonitorCPUModelInfoPtr modelInfo = NULL;
> +    qemuMonitorCPUModelInfoPtr migratable = NULL;
>      qemuMonitorCPUModelInfoPtr nonMigratable = NULL;
> -    virHashTablePtr hash = NULL;
> +    qemuMonitorCPUModelInfoPtr augmented = NULL;
>      const char *model;
>      qemuMonitorCPUModelExpansionType type;
>      virDomainVirtType virtType;
> @@ -2404,6 +2481,7 @@ virQEMUCapsProbeQMPHostCPU(virQEMUCapsPtr qemuCaps,
>      }
>  
>      cpuData = virQEMUCapsGetHostCPUData(qemuCaps, virtType);
> +    cpuData->info = NULL;

This is pretty suspicious. Either cpuData->info was already NULL and
there's no need to reset it again or we just leaked the memory
cpuData->info was pointing to.

>  
>      /* Some x86_64 features defined in cpu_map.xml use spelling which differ
>       * from the one preferred by QEMU. Static expansion would give us only the
...

Jirka

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list