[PATCH 0/7] some cleanups around CPU related code

Hi, Here are some cleanups I ended up making while investigating a test issue with ppc64 hosts recently. Daniel Henrique Barboza (7): qemu_capabilities.c: use g_autofree in virQEMUCapsLoadCache() cpu_x86.c: remove 'guest' param from x86Compute() cpu_ppc64.c: remove 'guest' param from ppc64Compute() qemu_capabilities.c: use g_autofree in virQEMUCapsLoadHostCPUModelInfo domain_conf.h: add autoptr cleanup func to virDomainXMLOptionPtr qemu_process.c: use g_autoptr() in qemuProcessQMPInitMonitor qemu_capabilities.c: use g_autoptr() in virQEMUCapsInitHostCPUModel() src/conf/domain_conf.h | 1 + src/cpu/cpu_ppc64.c | 26 +------- src/cpu/cpu_x86.c | 38 +----------- src/qemu/qemu_capabilities.c | 116 +++++++++++++++-------------------- src/qemu/qemu_process.c | 15 ++--- 5 files changed, 57 insertions(+), 139 deletions(-) -- 2.31.1

Use autofree with 'str' and 'nodes' to get rid of the 'cleanup' label. Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_capabilities.c | 66 ++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 67fae46a34..aef76ecc56 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -4125,20 +4125,19 @@ virQEMUCapsLoadCache(virArch hostArch, bool skipInvalidation) { g_autoptr(xmlDoc) doc = NULL; - int ret = -1; size_t i; int n; - xmlNodePtr *nodes = NULL; + g_autofree xmlNodePtr *nodes = NULL; g_autoptr(xmlXPathContext) ctxt = NULL; - char *str = NULL; + g_autofree char *str = NULL; long long int l; unsigned long lu; if (!(doc = virXMLParseFile(filename))) - goto cleanup; + return -1; if (!(ctxt = virXMLXPathContextNew(doc))) - goto cleanup; + return -1; ctxt->node = xmlDocGetRootElement(doc); @@ -4147,13 +4146,13 @@ virQEMUCapsLoadCache(virArch hostArch, _("unexpected root element <%s>, " "expecting <qemuCaps>"), ctxt->node->name); - goto cleanup; + return -1; } if (virXPathLongLong("string(./selfctime)", ctxt, &l) < 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing selfctime in QEMU capabilities XML")); - goto cleanup; + return -1; } qemuCaps->libvirtCtime = (time_t)l; @@ -4171,26 +4170,25 @@ virQEMUCapsLoadCache(virArch hostArch, (long long)virGetSelfLastChanged(), (unsigned long)qemuCaps->libvirtVersion, (unsigned long)LIBVIR_VERSION_NUMBER); - ret = 1; - goto cleanup; + return 1; } if (!(str = virXPathString("string(./emulator)", ctxt))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing emulator in QEMU capabilities cache")); - goto cleanup; + return -1; } if (STRNEQ(str, qemuCaps->binary)) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Expected caps for '%s' but saw '%s'"), qemuCaps->binary, str); - goto cleanup; + return -1; } VIR_FREE(str); if (virXPathLongLong("string(./qemuctime)", ctxt, &l) < 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing qemuctime in QEMU capabilities XML")); - goto cleanup; + return -1; } qemuCaps->ctime = (time_t)l; @@ -4200,7 +4198,7 @@ virQEMUCapsLoadCache(virArch hostArch, if ((n = virXPathNodeSet("./flag", ctxt, &nodes)) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to parse qemu capabilities flags")); - goto cleanup; + return -1; } VIR_DEBUG("Got flags %d", n); for (i = 0; i < n; i++) { @@ -4208,13 +4206,13 @@ virQEMUCapsLoadCache(virArch hostArch, if (!(str = virXMLPropString(nodes[i], "name"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing flag name in QEMU capabilities cache")); - goto cleanup; + return -1; } flag = virQEMUCapsTypeFromString(str); if (flag < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unknown qemu capabilities flag %s"), str); - goto cleanup; + return -1; } VIR_FREE(str); virQEMUCapsSet(qemuCaps, flag); @@ -4224,20 +4222,20 @@ virQEMUCapsLoadCache(virArch hostArch, if (virXPathUInt("string(./version)", ctxt, &qemuCaps->version) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing version in QEMU capabilities cache")); - goto cleanup; + return -1; } if (virXPathUInt("string(./kvmVersion)", ctxt, &qemuCaps->kvmVersion) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing version in QEMU capabilities cache")); - goto cleanup; + return -1; } if (virXPathUInt("string(./microcodeVersion)", ctxt, &qemuCaps->microcodeVersion) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing microcode version in QEMU capabilities cache")); - goto cleanup; + return -1; } qemuCaps->hostCPUSignature = virXPathString("string(./hostCPUSignature)", ctxt); @@ -4251,35 +4249,35 @@ virQEMUCapsLoadCache(virArch hostArch, if (virXPathBoolean("boolean(./kernelVersion)", ctxt) > 0) { qemuCaps->kernelVersion = virXPathString("string(./kernelVersion)", ctxt); if (!qemuCaps->kernelVersion) - goto cleanup; + return -1; } if (!(str = virXPathString("string(./arch)", ctxt))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing arch in QEMU capabilities cache")); - goto cleanup; + return -1; } if (!(qemuCaps->arch = virArchFromString(str))) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown arch %s in QEMU capabilities cache"), str); - goto cleanup; + return -1; } VIR_FREE(str); if (virXPathBoolean("boolean(./cpudata)", ctxt) > 0) { qemuCaps->cpuData = virCPUDataParseNode(virXPathNode("./cpudata", ctxt)); if (!qemuCaps->cpuData) - goto cleanup; + return -1; } if (virQEMUCapsLoadAccel(qemuCaps, ctxt, VIR_DOMAIN_VIRT_KVM) < 0 || virQEMUCapsLoadAccel(qemuCaps, ctxt, VIR_DOMAIN_VIRT_QEMU) < 0) - goto cleanup; + return -1; if ((n = virXPathNodeSet("./gic", ctxt, &nodes)) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to parse qemu capabilities gic")); - goto cleanup; + return -1; } if (n > 0) { unsigned int uintValue; @@ -4295,13 +4293,13 @@ virQEMUCapsLoadCache(virArch hostArch, virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing GIC version " "in QEMU capabilities cache")); - goto cleanup; + return -1; } if (virStrToLong_ui(str, NULL, 10, &uintValue) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed GIC version " "in QEMU capabilities cache")); - goto cleanup; + return -1; } cap->version = uintValue; VIR_FREE(str); @@ -4310,13 +4308,13 @@ virQEMUCapsLoadCache(virArch hostArch, virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing in-kernel GIC information " "in QEMU capabilities cache")); - goto cleanup; + return -1; } if (!(boolValue = STREQ(str, "yes")) && STRNEQ(str, "no")) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed in-kernel GIC information " "in QEMU capabilities cache")); - goto cleanup; + return -1; } if (boolValue) cap->implementation |= VIR_GIC_IMPLEMENTATION_KERNEL; @@ -4326,13 +4324,13 @@ virQEMUCapsLoadCache(virArch hostArch, virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing emulated GIC information " "in QEMU capabilities cache")); - goto cleanup; + return -1; } if (!(boolValue = STREQ(str, "yes")) && STRNEQ(str, "no")) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed emulated GIC information " "in QEMU capabilities cache")); - goto cleanup; + return -1; } if (boolValue) cap->implementation |= VIR_GIC_IMPLEMENTATION_EMULATED; @@ -4342,7 +4340,7 @@ virQEMUCapsLoadCache(virArch hostArch, VIR_FREE(nodes); if (virQEMUCapsParseSEVInfo(qemuCaps, ctxt) < 0) - goto cleanup; + return -1; virQEMUCapsInitHostCPUModel(qemuCaps, hostArch, VIR_DOMAIN_VIRT_KVM); virQEMUCapsInitHostCPUModel(qemuCaps, hostArch, VIR_DOMAIN_VIRT_QEMU); @@ -4356,11 +4354,7 @@ virQEMUCapsLoadCache(virArch hostArch, if (skipInvalidation) qemuCaps->invalidation = false; - ret = 0; - cleanup: - VIR_FREE(str); - VIR_FREE(nodes); - return ret; + return 0; } -- 2.31.1

On a Thursday in 2021, Daniel Henrique Barboza wrote:
Use autofree with 'str' and 'nodes' to get rid of the 'cleanup' label.
The reason this function is not yet completely converted to g_auto is that we discourage mixing of manual and automatic freeing, which is what happens here with both of these variables. A combination of using separate variables and/or reducing the scope of some of them by putting them into the loops where they're used or separate functions will be needed to satisfy that requirement. Jano
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_capabilities.c | 66 ++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 36 deletions(-)

x86Compute() is a static function called only by virCPUx86Compare() which passes NULL to the 'guest' parameter of x86Compute(). The last caller of x86Compute() that used it with 'guest' != NULL was an API called 'cpuGuestData'. This API was dropped by commit 03fa904c0c0cb2 a few years ago. Since then all callers of x86Compute() uses it with 'guest' = NULL. Removing the 'guest' parameter allow us to remove a good chunk of logic that isn't being used for awhile. Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/cpu/cpu_x86.c | 38 +------------------------------------- 1 file changed, 1 insertion(+), 37 deletions(-) diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c index 54cfed3fe6..e396bbb4e4 100644 --- a/src/cpu/cpu_x86.c +++ b/src/cpu/cpu_x86.c @@ -1833,7 +1833,6 @@ virCPUx86DataParse(xmlNodePtr node) static virCPUCompareResult x86Compute(virCPUDef *host, virCPUDef *cpu, - virCPUData **guest, char **message) { virCPUx86Map *map = NULL; @@ -1848,7 +1847,6 @@ x86Compute(virCPUDef *host, g_autoptr(virCPUData) guestData = NULL; virCPUCompareResult ret; virCPUx86CompareResult result; - virArch arch; size_t i; if (cpu->arch != VIR_ARCH_NONE) { @@ -1870,9 +1868,6 @@ x86Compute(virCPUDef *host, } return VIR_CPU_COMPARE_INCOMPATIBLE; } - arch = cpu->arch; - } else { - arch = host->arch; } if (cpu->vendor && @@ -1939,37 +1934,6 @@ x86Compute(virCPUDef *host, return VIR_CPU_COMPARE_INCOMPATIBLE; } - if (guest) { - guest_model = x86ModelCopy(host_model); - - if (cpu->vendor && host_model->vendor && - virCPUx86DataAddItem(&guest_model->data, - &host_model->vendor->data) < 0) - return VIR_CPU_COMPARE_ERROR; - - if (host_model->signatures && host_model->signatures->count > 0) { - virCPUx86Signature *sig = &host_model->signatures->items[0]; - if (x86DataAddSignature(&guest_model->data, - virCPUx86SignatureToCPUID(sig)) < 0) - return VIR_CPU_COMPARE_ERROR; - } - - if (cpu->type == VIR_CPU_TYPE_GUEST - && cpu->match == VIR_CPU_MATCH_EXACT) - x86DataSubtract(&guest_model->data, &diff->data); - - if (x86DataAdd(&guest_model->data, &cpu_force->data)) - return VIR_CPU_COMPARE_ERROR; - - x86DataSubtract(&guest_model->data, &cpu_disable->data); - - if (!(guestData = virCPUDataNew(arch))) - return VIR_CPU_COMPARE_ERROR; - x86DataCopy(&guestData->data.x86, &guest_model->data); - - *guest = g_steal_pointer(&guestData); - } - return ret; } #undef virX86CpuIncompatible @@ -1994,7 +1958,7 @@ virCPUx86Compare(virCPUDef *host, return VIR_CPU_COMPARE_INCOMPATIBLE; } - ret = x86Compute(host, cpu, NULL, &message); + ret = x86Compute(host, cpu, &message); if (ret == VIR_CPU_COMPARE_INCOMPATIBLE && failIncompatible) { if (message) -- 2.31.1

On a Thursday in 2021, Daniel Henrique Barboza wrote:
x86Compute() is a static function called only by virCPUx86Compare() which passes NULL to the 'guest' parameter of x86Compute().
The last caller of x86Compute() that used it with 'guest' != NULL was an API called 'cpuGuestData'. This API was dropped by commit 03fa904c0c0cb2 a few years ago. Since then all callers of x86Compute() uses it with 'guest' = NULL.
Removing the 'guest' parameter allow us to remove a good chunk of logic that isn't being used for awhile.
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/cpu/cpu_x86.c | 38 +------------------------------------- 1 file changed, 1 insertion(+), 37 deletions(-)
diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c index 54cfed3fe6..e396bbb4e4 100644 --- a/src/cpu/cpu_x86.c +++ b/src/cpu/cpu_x86.c @@ -1833,7 +1833,6 @@ virCPUx86DataParse(xmlNodePtr node) static virCPUCompareResult x86Compute(virCPUDef *host, virCPUDef *cpu, - virCPUData **guest, char **message) { virCPUx86Map *map = NULL; @@ -1848,7 +1847,6 @@ x86Compute(virCPUDef *host, g_autoptr(virCPUData) guestData = NULL; virCPUCompareResult ret; virCPUx86CompareResult result; - virArch arch; size_t i;
../../work/libvirt/src/cpu/cpu_x86.c:1846:31: error: unused variable 'guest_model' [-Werror,-Wunused-variable] g_autoptr(virCPUx86Model) guest_model = NULL; ^ ../../work/libvirt/src/cpu/cpu_x86.c:1847:27: error: unused variable 'guestData' [-Werror,-Wunused-variable] g_autoptr(virCPUData) guestData = NULL;
if (cpu->arch != VIR_ARCH_NONE) {
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

ppc64Compute() is used only once, by virCPUppc64Compare(), which doesn't use the 'guest' parameter. The reason is similar to what was mentioned in the previous patch: this parameter was being used by an API called 'cpuGuestData' that was dropped by commit 03fa904c0c0cb2. Removing the 'guest' parameter will not only remove unused code from ppc64Compute() but also remove the ppc64MakeCPUData() entirely. Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/cpu/cpu_ppc64.c | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/src/cpu/cpu_ppc64.c b/src/cpu/cpu_ppc64.c index c7caaa9608..314d2f7c86 100644 --- a/src/cpu/cpu_ppc64.c +++ b/src/cpu/cpu_ppc64.c @@ -379,32 +379,15 @@ ppc64LoadMap(void) return g_steal_pointer(&map); } -static virCPUData * -ppc64MakeCPUData(virArch arch, - virCPUppc64Data *data) -{ - g_autoptr(virCPUData) cpuData = NULL; - - cpuData = g_new0(virCPUData, 1); - cpuData->arch = arch; - - if (ppc64DataCopy(&cpuData->data.ppc64, data) < 0) - return NULL; - - return g_steal_pointer(&cpuData); -} - static virCPUCompareResult ppc64Compute(virCPUDef *host, const virCPUDef *other, - virCPUData **guestData, char **message) { g_autoptr(virCPUppc64Map) map = NULL; g_autoptr(virCPUppc64Model) host_model = NULL; g_autoptr(virCPUppc64Model) guest_model = NULL; g_autoptr(virCPUDef) cpu = NULL; - virArch arch; size_t i; /* Ensure existing configurations are handled correctly */ @@ -431,9 +414,6 @@ ppc64Compute(virCPUDef *host, return VIR_CPU_COMPARE_INCOMPATIBLE; } - arch = cpu->arch; - } else { - arch = host->arch; } if (cpu->vendor && @@ -506,10 +486,6 @@ ppc64Compute(virCPUDef *host, return VIR_CPU_COMPARE_INCOMPATIBLE; } - if (guestData) - if (!(*guestData = ppc64MakeCPUData(arch, &guest_model->data))) - return VIR_CPU_COMPARE_ERROR; - return VIR_CPU_COMPARE_IDENTICAL; } @@ -532,7 +508,7 @@ virCPUppc64Compare(virCPUDef *host, return VIR_CPU_COMPARE_INCOMPATIBLE; } - ret = ppc64Compute(host, cpu, NULL, &message); + ret = ppc64Compute(host, cpu, &message); if (failIncompatible && ret == VIR_CPU_COMPARE_INCOMPATIBLE) { ret = VIR_CPU_COMPARE_ERROR; -- 2.31.1

Use 'g_autofree' in the 'str' parameter and remove the 'cleanup' label. Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_capabilities.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index aef76ecc56..5257fe64b2 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -3723,21 +3723,18 @@ virQEMUCapsLoadHostCPUModelInfo(virQEMUCapsAccel *caps, xmlXPathContextPtr ctxt, const char *typeStr) { - char *str = NULL; + g_autofree char *str = NULL; xmlNodePtr hostCPUNode; g_autofree xmlNodePtr *nodes = NULL; VIR_XPATH_NODE_AUTORESTORE(ctxt) g_autoptr(qemuMonitorCPUModelInfo) hostCPU = NULL; g_autofree char *xpath = g_strdup_printf("./hostCPU[@type='%s']", typeStr); - int ret = -1; size_t i; int n; int val; - if (!(hostCPUNode = virXPathNode(xpath, ctxt))) { - ret = 0; - goto cleanup; - } + if (!(hostCPUNode = virXPathNode(xpath, ctxt))) + return 0; hostCPU = g_new0(qemuMonitorCPUModelInfo, 1); @@ -3745,14 +3742,14 @@ virQEMUCapsLoadHostCPUModelInfo(virQEMUCapsAccel *caps, virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing host CPU model name in QEMU " "capabilities cache")); - goto cleanup; + return -1; } if (!(str = virXMLPropString(hostCPUNode, "migratability")) || (val = virTristateBoolTypeFromString(str)) <= 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("invalid migratability value for host CPU model")); - goto cleanup; + return -1; } hostCPU->migratability = val == VIR_TRISTATE_BOOL_YES; VIR_FREE(str); @@ -3772,7 +3769,7 @@ virQEMUCapsLoadHostCPUModelInfo(virQEMUCapsAccel *caps, virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing 'name' attribute for a host CPU" " model property in QEMU capabilities cache")); - goto cleanup; + return -1; } if (!(str = virXMLPropString(ctxt->node, "type")) || @@ -3780,7 +3777,7 @@ virQEMUCapsLoadHostCPUModelInfo(virQEMUCapsAccel *caps, virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("missing or invalid CPU model property type " "in QEMU capabilities cache")); - goto cleanup; + return -1; } VIR_FREE(str); @@ -3798,7 +3795,7 @@ virQEMUCapsLoadHostCPUModelInfo(virQEMUCapsAccel *caps, _("invalid string value for '%s' host CPU " "model property in QEMU capabilities cache"), prop->name); - goto cleanup; + return -1; } break; @@ -3809,7 +3806,7 @@ virQEMUCapsLoadHostCPUModelInfo(virQEMUCapsAccel *caps, _("invalid number value for '%s' host CPU " "model property in QEMU capabilities cache"), prop->name); - goto cleanup; + return -1; } break; @@ -3823,7 +3820,7 @@ virQEMUCapsLoadHostCPUModelInfo(virQEMUCapsAccel *caps, _("unknown migratable value for '%s' host " "CPU model property"), prop->name); - goto cleanup; + return -1; } prop->migratable = val; @@ -3833,11 +3830,7 @@ virQEMUCapsLoadHostCPUModelInfo(virQEMUCapsAccel *caps, } caps->hostCPU.info = g_steal_pointer(&hostCPU); - ret = 0; - - cleanup: - VIR_FREE(str); - return ret; + return 0; } -- 2.31.1

This will enable code cleanups on code that still relies on virObjectUnref() this pointer manually. Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/conf/domain_conf.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index ab9a7d66f8..0ecd3803db 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3214,6 +3214,7 @@ struct _virDomainXMLOption { /* Snapshot postparse callbacks */ virDomainMomentPostParseCallback momentPostParse; }; +G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainXMLOption, virObjectUnref); struct virDomainDefPostParseDeviceIteratorData { virDomainXMLOption *xmlopt; -- 2.31.1

On a Thursday in 2021, Daniel Henrique Barboza wrote:
This will enable code cleanups on code that still relies on virObjectUnref() this pointer manually.
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/conf/domain_conf.h | 1 + 1 file changed, 1 insertion(+)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

The 'xmlopt' parameter can be auto-unref by using g_autoptr(). Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_process.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index b6c81dd23a..c355a39e15 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -9257,9 +9257,8 @@ qemuProcessQMPInitMonitor(qemuMonitor *mon) static int qemuProcessQMPConnectMonitor(qemuProcessQMP *proc) { - virDomainXMLOption *xmlopt = NULL; + g_autoptr(virDomainXMLOption) xmlopt = NULL; virDomainChrSourceDef monConfig; - int ret = -1; VIR_DEBUG("proc=%p, emulator=%s, proc->pid=%lld", proc, proc->binary, (long long)proc->pid); @@ -9271,25 +9270,21 @@ qemuProcessQMPConnectMonitor(qemuProcessQMP *proc) if (!(xmlopt = virDomainXMLOptionNew(NULL, NULL, NULL, NULL, NULL)) || !(proc->vm = virDomainObjNew(xmlopt)) || !(proc->vm->def = virDomainDefNew(xmlopt))) - goto cleanup; + return -1; proc->vm->pid = proc->pid; if (!(proc->mon = qemuMonitorOpen(proc->vm, &monConfig, true, 0, virEventThreadGetContext(proc->eventThread), &callbacks, NULL))) - goto cleanup; + return -1; virObjectLock(proc->mon); if (qemuProcessQMPInitMonitor(proc->mon) < 0) - goto cleanup; - - ret = 0; + return -1; - cleanup: - virObjectUnref(xmlopt); - return ret; + return 0; } -- 2.31.1

On a Thursday in 2021, Daniel Henrique Barboza wrote:
The 'xmlopt' parameter can be auto-unref by using g_autoptr().
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_process.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

All 'virCPUDef' pointers can be auto-freed and the 'cleanup' label removed. Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_capabilities.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 5257fe64b2..dbf08d681b 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -3597,11 +3597,11 @@ virQEMUCapsInitHostCPUModel(virQEMUCaps *qemuCaps, virArch hostArch, virDomainVirtType type) { - virCPUDef *cpu = NULL; - virCPUDef *cpuExpanded = NULL; - virCPUDef *migCPU = NULL; - virCPUDef *hostCPU = NULL; - virCPUDef *fullCPU = NULL; + g_autoptr(virCPUDef) cpu = NULL; + g_autoptr(virCPUDef) cpuExpanded = NULL; + g_autoptr(virCPUDef) migCPU = NULL; + g_autoptr(virCPUDef) hostCPU = NULL; + g_autoptr(virCPUDef) fullCPU = NULL; size_t i; int rc; @@ -3676,19 +3676,14 @@ virQEMUCapsInitHostCPUModel(virQEMUCaps *qemuCaps, goto error; } - virQEMUCapsSetHostModel(qemuCaps, type, cpu, migCPU, fullCPU); + virQEMUCapsSetHostModel(qemuCaps, type, g_steal_pointer(&cpu), + g_steal_pointer(&migCPU), + g_steal_pointer(&fullCPU)); - cleanup: - virCPUDefFree(cpuExpanded); - virCPUDefFree(hostCPU); return; error: - virCPUDefFree(cpu); - virCPUDefFree(migCPU); - virCPUDefFree(fullCPU); virResetLastError(); - goto cleanup; } -- 2.31.1

On a Thursday in 2021, Daniel Henrique Barboza wrote:
All 'virCPUDef' pointers can be auto-freed and the 'cleanup' label removed.
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> --- src/qemu/qemu_capabilities.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 5257fe64b2..dbf08d681b 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -3597,11 +3597,11 @@ virQEMUCapsInitHostCPUModel(virQEMUCaps *qemuCaps, virArch hostArch, virDomainVirtType type) { - virCPUDef *cpu = NULL; - virCPUDef *cpuExpanded = NULL; - virCPUDef *migCPU = NULL; - virCPUDef *hostCPU = NULL; - virCPUDef *fullCPU = NULL; + g_autoptr(virCPUDef) cpu = NULL; + g_autoptr(virCPUDef) cpuExpanded = NULL; + g_autoptr(virCPUDef) migCPU = NULL;
migCPU is also freed manually if virQEMUCapsInitCPUModel fails. However in both callers of virQEMUCapsInitCPUModel the cpu argument is allocated right above by virQEMUCapsNewHostCPUModel(). By moving the allocation inside virQEMUCapsInitCPUModel, the virCPUDefFree can be removed. Jano
+ g_autoptr(virCPUDef) hostCPU = NULL; + g_autoptr(virCPUDef) fullCPU = NULL; size_t i; int rc;
participants (2)
-
Daniel Henrique Barboza
-
Ján Tomko