[libvirt] [PATCH v2 05/31] qemu: Copy CPU models in virQEMUCapsGetCPUDefinitions

Jiri Denemark posted 31 patches 6 years, 2 months ago
There is a newer version of this series
[libvirt] [PATCH v2 05/31] qemu: Copy CPU models in virQEMUCapsGetCPUDefinitions
Posted by Jiri Denemark 6 years, 2 months ago
Rather than returning a direct pointer the list stored in qemuCaps the
function now creates a new copy of the CPU models list.

The main purpose of this seemingly useless change is to update callers
to free the result returned by virQEMUCapsGetCPUDefinitions because the
internals of this function will change significantly in the following
patches.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---

Notes:
    Version 2:
    - no change

 src/qemu/qemu_capabilities.c | 23 ++++++++++++++++++-----
 src/qemu/qemu_driver.c       |  2 +-
 src/qemu/qemu_process.c      |  7 +++++--
 tests/cputest.c              |  1 -
 4 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 14939fc91d..a40bf99aa3 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -1891,10 +1891,17 @@ virDomainCapsCPUModelsPtr
 virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
                              virDomainVirtType type)
 {
+    virDomainCapsCPUModelsPtr cpuModels;
+
     if (type == VIR_DOMAIN_VIRT_KVM)
-        return qemuCaps->kvmCPUModels;
+        cpuModels = qemuCaps->kvmCPUModels;
     else
-        return qemuCaps->tcgCPUModels;
+        cpuModels = qemuCaps->tcgCPUModels;
+
+    if (!cpuModels)
+        return NULL;
+
+    return virDomainCapsCPUModelsCopy(cpuModels);
 }
 
 
@@ -3125,6 +3132,7 @@ virQEMUCapsInitCPUModelX86(virQEMUCapsPtr qemuCaps,
                            virCPUDefPtr cpu,
                            bool migratable)
 {
+    VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
     virCPUDataPtr data = NULL;
     int ret = -1;
 
@@ -3134,7 +3142,9 @@ virQEMUCapsInitCPUModelX86(virQEMUCapsPtr qemuCaps,
     if (!(data = virQEMUCapsGetCPUModelX86Data(qemuCaps, model, migratable)))
         goto cleanup;
 
-    if (cpuDecode(cpu, data, virQEMUCapsGetCPUDefinitions(qemuCaps, type)) < 0)
+    cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, type);
+
+    if (cpuDecode(cpu, data, cpuModels) < 0)
         goto cleanup;
 
     ret = 0;
@@ -3217,10 +3227,13 @@ virQEMUCapsInitHostCPUModel(virQEMUCapsPtr qemuCaps,
     if ((rc = virQEMUCapsInitCPUModel(qemuCaps, type, cpu, false)) < 0) {
         goto error;
     } else if (rc == 1) {
+        VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
+
         VIR_DEBUG("No host CPU model info from QEMU; probing host CPU directly");
 
-        hostCPU = virQEMUCapsProbeHostCPU(hostArch,
-                                          virQEMUCapsGetCPUDefinitions(qemuCaps, type));
+        cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, type);
+        hostCPU = virQEMUCapsProbeHostCPU(hostArch, cpuModels);
+
         if (!hostCPU ||
             virCPUDefCopyModelFilter(cpu, hostCPU, true,
                                      virQEMUCapsCPUFilterFeatures,
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 6ce6348593..15c69667a5 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -13913,7 +13913,7 @@ qemuConnectBaselineHypervisorCPU(virConnectPtr conn,
     virQEMUCapsPtr qemuCaps = NULL;
     virArch arch;
     virDomainVirtType virttype;
-    virDomainCapsCPUModelsPtr cpuModels;
+    VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
     bool migratable;
     virCPUDefPtr cpu = NULL;
     char *cpustr = NULL;
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 9c50c4a1d8..58192612b0 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -6122,6 +6122,8 @@ qemuProcessUpdateGuestCPU(virDomainDefPtr def,
 
     /* nothing to update for host-passthrough */
     if (def->cpu->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {
+        VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
+
         if (def->cpu->check == VIR_CPU_CHECK_PARTIAL &&
             virCPUCompare(caps->host.arch,
                           virQEMUCapsGetHostModel(qemuCaps, def->virtType,
@@ -6134,8 +6136,9 @@ qemuProcessUpdateGuestCPU(virDomainDefPtr def,
                                                  VIR_QEMU_CAPS_HOST_CPU_MIGRATABLE)) < 0)
             return -1;
 
-        if (virCPUTranslate(def->os.arch, def->cpu,
-                            virQEMUCapsGetCPUDefinitions(qemuCaps, def->virtType)) < 0)
+        cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, def->virtType);
+
+        if (virCPUTranslate(def->os.arch, def->cpu, cpuModels) < 0)
             return -1;
 
         def->cpu->fallback = VIR_CPU_FALLBACK_FORBID;
diff --git a/tests/cputest.c b/tests/cputest.c
index 7f47e3b601..3aa8fd7bf3 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -551,7 +551,6 @@ cpuTestGetCPUModels(const struct data *data,
         return -1;
 
     *models = virQEMUCapsGetCPUDefinitions(qemuCaps, VIR_DOMAIN_VIRT_KVM);
-    virObjectRef(*models);
 
     virObjectUnref(qemuCaps);
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 05/31] qemu: Copy CPU models in virQEMUCapsGetCPUDefinitions
Posted by Ján Tomko 6 years, 2 months ago
On Tue, Oct 15, 2019 at 05:34:41PM +0200, Jiri Denemark wrote:
>Rather than returning a direct pointer the list stored in qemuCaps the
>function now creates a new copy of the CPU models list.
>
>The main purpose of this seemingly useless change is to update callers
>to free the result returned by virQEMUCapsGetCPUDefinitions because the
>internals of this function will change significantly in the following
>patches.
>
>Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
>Reviewed-by: Ján Tomko <jtomko@redhat.com>
>---
>
>Notes:
>    Version 2:
>    - no change
>
> src/qemu/qemu_capabilities.c | 23 ++++++++++++++++++-----
> src/qemu/qemu_driver.c       |  2 +-
> src/qemu/qemu_process.c      |  7 +++++--
> tests/cputest.c              |  1 -
> 4 files changed, 24 insertions(+), 9 deletions(-)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>
with the following squashed in:

diff --git a/src/conf/domain_capabilities.h b/src/conf/domain_capabilities.h
index 264c7fc429..36500435fd 100644
--- a/src/conf/domain_capabilities.h
+++ b/src/conf/domain_capabilities.h
@@ -139,6 +139,8 @@ struct _virDomainCapsCPUModels {
     virDomainCapsCPUModelPtr models;
 };
 
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainCapsCPUModels, virObjectUnref);
+
 typedef struct _virDomainCapsCPU virDomainCapsCPU;
 typedef virDomainCapsCPU *virDomainCapsCPUPtr;
 struct _virDomainCapsCPU {
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index a875896a73..e1750751b4 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3132,7 +3132,7 @@ virQEMUCapsInitCPUModelX86(virQEMUCapsPtr qemuCaps,
                            virCPUDefPtr cpu,
                            bool migratable)
 {
-    VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
+    g_autoptr(virDomainCapsCPUModels) cpuModels = NULL;
     virCPUDataPtr data = NULL;
     int ret = -1;
 
@@ -3227,7 +3227,7 @@ virQEMUCapsInitHostCPUModel(virQEMUCapsPtr qemuCaps,
     if ((rc = virQEMUCapsInitCPUModel(qemuCaps, type, cpu, false)) < 0) {
         goto error;
     } else if (rc == 1) {
-        VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
+        g_autoptr(virDomainCapsCPUModels) cpuModels = NULL;
 
         VIR_DEBUG("No host CPU model info from QEMU; probing host CPU directly");
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 78ba1f6f04..d95bf92cfd 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -13913,7 +13913,7 @@ qemuConnectBaselineHypervisorCPU(virConnectPtr conn,
     virQEMUCapsPtr qemuCaps = NULL;
     virArch arch;
     virDomainVirtType virttype;
-    VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
+    g_autoptr(virDomainCapsCPUModels) cpuModels = NULL;
     bool migratable;
     virCPUDefPtr cpu = NULL;
     char *cpustr = NULL;
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 539ab7ae33..9135a90a0d 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -6122,7 +6122,7 @@ qemuProcessUpdateGuestCPU(virDomainDefPtr def,
 
     /* nothing to update for host-passthrough */
     if (def->cpu->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {
-        VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
+        g_autoptr(virDomainCapsCPUModels) cpuModels = NULL;
 
         if (def->cpu->check == VIR_CPU_CHECK_PARTIAL &&
             virCPUCompare(caps->host.arch,

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