qdev release function cleanups + unit test
v2 cover qdev release function cleanups + unit test
Hello,
This was originally a single patch to make the property array release
function free the property array instead of having the users free it in their exit functions. This fixes leaks and reduces code overhead. After review, Marc-André asked to include some work he did on this earlier
(https://patchew.org/QEMU/[email protected]/).
I have included his patches that added unit testing and made some release functions idempotent.
Chandan Somani (1):
qdev: Free property array on release
Marc-André Lureau (4):
qdev: make release_string() idempotent
qdev: make release_drive() idempotent
qdev: make release_tpm() idempotent
tests: add /qdev/free-properties test
backends/tpm/tpm_util.c | 1 + block/accounting.c | 1 - hw/core/qdev-properties-system.c | 1 + hw/core/qdev-properties.c | 24 ++++---- hw/input/stellaris_gamepad.c | 8 --- hw/intc/arm_gicv3_common.c | 8 --- hw/intc/rx_icu.c | 8 --- hw/misc/arm_sysctl.c | 2 - hw/misc/mps2-scc.c | 8 --- hw/net/rocker/rocker.c | 1 - hw/nvram/xlnx-efuse.c | 8 --- hw/nvram/xlnx-versal-efuse-ctrl.c | 8 --- hw/virtio/virtio-iommu-pci.c | 8 --- tests/unit/meson.build | 1 + tests/unit/test-qdev.c | 96 +++++++++++++++++++++++++++++++ 15 files changed, 111 insertions(+), 72 deletions(-) create mode 100644 tests/unit/test-qdev.c
Signature · 1 line
2.51.1
PATCH v2 1/5 qdev: Free property array on release
Before this patch, users of the property array would free the array themselves in their cleanup functions. This causes inconsistencies where some users leak the array and some free them.
This patch makes it so that the property array's release function frees the property array (instead of just its elements). It fixes any leaks and requires less code.
DEFINE_PROP_ARRAY leakers that are fixed in this patch:
ebpf-rss_fds in hw/net/virtio-net.c rnmi_irqvec, rnmi_excpvec in hw/riscv/riscv_hart.c common.display_modes in hw/display/apple-gfx-mmio.m common.display_modes in hw/display/apple-gfx-pci.m
--- block/accounting.c | 1 - hw/core/qdev-properties.c | 21 ++++++++++----------- hw/input/stellaris_gamepad.c | 8 -------- hw/intc/arm_gicv3_common.c | 8 -------- hw/intc/rx_icu.c | 8 -------- hw/misc/arm_sysctl.c | 2 -- hw/misc/mps2-scc.c | 8 -------- hw/net/rocker/rocker.c | 1 - hw/nvram/xlnx-efuse.c | 8 -------- hw/nvram/xlnx-versal-efuse-ctrl.c | 8 -------- hw/virtio/virtio-iommu-pci.c | 8 -------- 11 files changed, 10 insertions(+), 71 deletions(-)
block/accounting.c +0 -1
@@ -73,7 +73,6 @@ bool block_acct_setup(BlockAcctStats *stats, enum OnOffAuto account_invalid, }block_acct_add_interval (stats, stats_intervals[i]); } -g_free (stats_intervals); }return true; }
hw/core/qdev-properties.c +10 -11
@@ -673,10 +673,8 @@ static Property array_elem_prop(Object *obj, const Property *parent_prop,/* * Object property release callback for array properties: We call the -* underlying element's property release hook for each element. -* -* Note that it is the responsibility of the individual device's deinit -* to free the array proper. +* underlying element's property release hook for each element and free the +* property array. */ static void release_prop_array (Object *obj,const char *name,void *opaque) {
@@ -686,15 +684,16 @@ static void release_prop_array(Object *obj, const char *name, void *opaque)char *elem = *arrayptr;int i; -if (!prop->arrayinfo->release) { -return ; +if (prop->arrayinfo->release) { +for (i =0 ; i < *alenptr; i++) { +Property elem_prop =array_elem_prop (obj, prop, name, elem); + prop->arrayinfo->release (obj,NULL , &elem_prop); + elem += prop->arrayfieldsize; + } } -for (i =0 ; i < *alenptr; i++) { -Property elem_prop =array_elem_prop (obj, prop, name, elem); - prop->arrayinfo->release (obj,NULL , &elem_prop); - elem += prop->arrayfieldsize; - } +g_clear_pointer (arrayptr, g_free); + *alenptr =0 ; }/*
hw/input/stellaris_gamepad.c +0 -8
@@ -63,13 +63,6 @@ static void stellaris_gamepad_realize(DeviceState *dev, Error **errp)qemu_input_handler_register (dev, &stellaris_gamepad_handler); } -static void stellaris_gamepad_finalize (Object *obj) -{ -StellarisGamepad *s =STELLARIS_GAMEPAD (obj); - -g_free (s->keycodes); -} -static void stellaris_gamepad_reset_enter (Object *obj,ResetType type) {StellarisGamepad *s =STELLARIS_GAMEPAD (obj);
@@ -98,7 +91,6 @@ static const TypeInfo stellaris_gamepad_info[] = { .name = TYPE_STELLARIS_GAMEPAD, .parent = TYPE_SYS_BUS_DEVICE, .instance_size = sizeof(StellarisGamepad), - .instance_finalize = stellaris_gamepad_finalize, .class_init = stellaris_gamepad_class_init, }, };
hw/intc/arm_gicv3_common.c +0 -8
@@ -488,13 +488,6 @@ static void arm_gicv3_common_realize(DeviceState *dev, Error **errp) s->itslist =g_ptr_array_new (); } -static void arm_gicv3_finalize (Object *obj) -{ -GICv3State *s =ARM_GICV3_COMMON (obj); - -g_free (s->redist_region_count); -} -static void arm_gicv3_common_reset_hold (Object *obj,ResetType type) {GICv3State *s =ARM_GICV3_COMMON (obj);
@@ -644,7 +637,6 @@ static const TypeInfo arm_gicv3_common_type = { .instance_size = sizeof(GICv3State), .class_size = sizeof(ARMGICv3CommonClass), .class_init = arm_gicv3_common_class_init, - .instance_finalize = arm_gicv3_finalize, .abstract = true, .interfaces = (const InterfaceInfo []) { {TYPE_ARM_LINUX_BOOT_IF },
hw/intc/rx_icu.c +0 -8
@@ -334,13 +334,6 @@ static void rxicu_init(Object *obj)sysbus_init_irq (d, &icu->_swi); } -static void rxicu_fini (Object *obj) -{ -RXICUState *icu =RX_ICU (obj); -g_free (icu->map); -g_free (icu->init_sense); -} -static const VMStateDescription vmstate_rxicu = { .name ="rx-icu" , .version_id =1 ,
@@ -382,7 +375,6 @@ static const TypeInfo rxicu_info = { .parent = TYPE_SYS_BUS_DEVICE, .instance_size = sizeof(RXICUState), .instance_init = rxicu_init, - .instance_finalize = rxicu_fini, .class_init = rxicu_class_init, };
hw/misc/arm_sysctl.c +0 -2
@@ -618,9 +618,7 @@ static void arm_sysctl_finalize(Object *obj) {arm_sysctl_state *s =ARM_SYSCTL (obj); -g_free (s->db_voltage);g_free (s->db_clock); -g_free (s->db_clock_reset); }static const Property arm_sysctl_properties[] = {
hw/misc/mps2-scc.c +0 -8
@@ -405,13 +405,6 @@ static void mps2_scc_realize(DeviceState *dev, Error **errp) s->oscclk =g_new0 (uint32_t, s->num_oscclk); } -static void mps2_scc_finalize (Object *obj) -{ -MPS2SCC *s =MPS2_SCC (obj); - -g_free (s->oscclk_reset); -} -static bool cfg7_needed (void *opaque) {MPS2SCC *s = opaque;
@@ -489,7 +482,6 @@ static const TypeInfo mps2_scc_info = { .parent = TYPE_SYS_BUS_DEVICE, .instance_size = sizeof(MPS2SCC), .instance_init = mps2_scc_init, - .instance_finalize = mps2_scc_finalize, .class_init = mps2_scc_class_init, };
hw/net/rocker/rocker.c +0 -1
@@ -1429,7 +1429,6 @@ static void pci_rocker_uninit(PCIDevice *dev)world_free (r->worlds[i]); } } -g_free (r->fp_ports_peers); }static void rocker_reset (DeviceState *dev)
hw/nvram/xlnx-efuse.c +0 -8
@@ -224,13 +224,6 @@ static void efuse_realize(DeviceState *dev, Error **errp) } } -static void efuse_finalize (Object *obj) -{ -XlnxEFuse *s =XLNX_EFUSE (obj); - -g_free (s->ro_bits); -} -static void efuse_prop_set_drive (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp) {
@@ -288,7 +281,6 @@ static const TypeInfo efuse_info = { .name = TYPE_XLNX_EFUSE, .parent = TYPE_DEVICE, .instance_size = sizeof(XlnxEFuse), - .instance_finalize = efuse_finalize, .class_init = efuse_class_init, };
hw/nvram/xlnx-versal-efuse-ctrl.c +0 -8
@@ -724,13 +724,6 @@ static void efuse_ctrl_init(Object *obj)sysbus_init_irq (sbd, &s->irq_efuse_imr); } -static void efuse_ctrl_finalize (Object *obj) -{ -XlnxVersalEFuseCtrl *s =XLNX_VERSAL_EFUSE_CTRL (obj); - -g_free (s->extra_pg0_lock_spec); -} -static const VMStateDescription vmstate_efuse_ctrl = { .name =TYPE_XLNX_VERSAL_EFUSE_CTRL , .version_id =1 ,
@@ -767,7 +760,6 @@ static const TypeInfo efuse_ctrl_info = { .instance_size = sizeof(XlnxVersalEFuseCtrl), .class_init = efuse_ctrl_class_init, .instance_init = efuse_ctrl_init, - .instance_finalize = efuse_ctrl_finalize, };static void efuse_ctrl_register_types (void )
hw/virtio/virtio-iommu-pci.c +0 -8
@@ -94,18 +94,10 @@ static void virtio_iommu_pci_instance_init(Object *obj) TYPE_VIRTIO_IOMMU); } -static void virtio_iommu_pci_instance_finalize (Object *obj) -{ -VirtIOIOMMUPCI *dev =VIRTIO_IOMMU_PCI (obj); - -g_free (dev->vdev.prop_resv_regions); -} -static const VirtioPCIDeviceTypeInfo virtio_iommu_pci_info = { .generic_name =TYPE_VIRTIO_IOMMU_PCI , .instance_size =sizeof (VirtIOIOMMUPCI), .instance_init = virtio_iommu_pci_instance_init, - .instance_finalize = virtio_iommu_pci_instance_finalize, .class_init = virtio_iommu_pci_class_init, };
Signature · 1 line
2.51.1
PATCH v2 2/5 qdev: make release_string() idempotent
In reply to Chandan SomaniFrom: Marc-André Lureau <[email protected]>
So it can eventually be called multiple times safely.
Message-Id: <[email protected]>
--- hw/core/qdev-properties.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/core/qdev-properties.c +2 -1
@@ -551,7 +551,8 @@ const PropertyInfo qdev_prop_usize = {static void release_string (Object *obj,const char *name,void *opaque) {const Property *prop = opaque; -g_free (*(char **)object_field_prop_ptr (obj, prop)); + +g_clear_pointer ((char **)object_field_prop_ptr (obj, prop), g_free); } static void get_string(Object *obj, Visitor *v, const char *name,
Signature · 1 line
2.51.1
PATCH v2 3/5 qdev: make release_drive() idempotent
In reply to Chandan SomaniFrom: Marc-André Lureau <[email protected]>
So it can eventually be called multiple times safely.
Message-Id: <[email protected]>
--- hw/core/qdev-properties-system.c | 1 + 1 file changed, 1 insertion(+)
hw/core/qdev-properties-system.c +1 -0
@@ -232,6 +232,7 @@ static void release_drive(Object *obj, const char *name, void *opaque)if (*ptr) {blockdev_auto_del (*ptr);blk_detach_dev (*ptr, dev); + *ptr =NULL ; } }
Signature · 1 line
2.51.1
PATCH v2 4/5 qdev: make release_tpm() idempotent
In reply to Chandan SomaniFrom: Marc-André Lureau <[email protected]>
So it can eventually be called multiple times safely.
Message-Id: <[email protected]>
--- backends/tpm/tpm_util.c | 1 + 1 file changed, 1 insertion(+)
backends/tpm/tpm_util.c +1 -0
@@ -72,6 +72,7 @@ static void release_tpm(Object *obj, const char *name, void *opaque)if (*be) {tpm_backend_reset (*be); + *be =NULL ; } }
Signature · 1 line
2.51.1
PATCH v2 5/5 tests: add /qdev/free-properties test
In reply to Chandan SomaniFrom: Marc-André Lureau <[email protected]>
Add a simple qdev test to check that allocated properties get freed with the object. This test exhibited array leaks before the fixes.
Message-Id: <[email protected]>
--- tests/unit/meson.build | 1 + tests/unit/test-qdev.c | 96 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 tests/unit/test-qdev.c
tests/unit/meson.build +1 -0
@@ -156,6 +156,7 @@ if have_system 'test-qdev-global-props': [qom, hwcore] } endif + tests += {'test-qdev': [qom, hwcore]} endif if have_ga and host_os == 'linux'
tests/unit/test-qdev.c new file +96 -0
96 lines not on this page · raw message
Signature · 1 line
2.51.1
Quoted 28 lines · Chandan Somani
Hello,
This was originally a single patch to make the property array release
function free the property array instead of having the users free it in their exit functions. This fixes leaks and reduces code overhead. After review, Marc-André asked to include some work he did on this earlier
(https://patchew.org/QEMU/[email protected]/).
I have included his patches that added unit testing and made some release functions idempotent.Chandan Somani (1):
qdev: Free property array on releaseMarc-André Lureau (4):
qdev: makerelease_string()idempotent
qdev: makerelease_drive()idempotent
qdev: makerelease_tpm()idempotent
tests: add /qdev/free-properties testbackends/tpm/tpm_util.c | 1 + block/accounting.c | 1 - hw/core/qdev-properties-system.c | 1 + hw/core/qdev-properties.c | 24 ++++---- hw/input/stellaris_gamepad.c | 8 --- hw/intc/arm_gicv3_common.c | 8 --- hw/intc/rx_icu.c | 8 --- hw/misc/arm_sysctl.c | 2 - hw/misc/mps2-scc.c | 8 ---
Quoted 12 lines
hw/net/rocker/rocker.c | 1 - hw/nvram/xlnx-efuse.c | 8 --- hw/nvram/xlnx-versal-efuse-ctrl.c | 8 --- hw/virtio/virtio-iommu-pci.c | 8 --- tests/unit/meson.build | 1 + tests/unit/test-qdev.c | 96 +++++++++++++++++++++++++++++++ 15 files changed, 111 insertions(+), 72 deletions(-) create mode 100644 tests/unit/test-qdev.cSignature · 1 line
2.51.1
Hi Paolo,
Could you please take a look at this qdev patch and put it through your
tree if it looks good?
Thanks,
Chandan
Quoted 43 lines · Chandan Somani
Hello,
This was originally a single patch to make the property array release
function free the property array instead of having the users free it in their exit functions. This fixes leaks and reduces code overhead. After review, Marc-André asked to include some work he did on this earlier
(
https://patchew.org/QEMU/[email protected]/
).
I have included his patches that added unit testing and made some release functions idempotent.Chandan Somani (1):
qdev: Free property array on releaseMarc-André Lureau (4):
qdev: makerelease_string()idempotent
qdev: makerelease_drive()idempotent
qdev: makerelease_tpm()idempotent
tests: add /qdev/free-properties testbackends/tpm/tpm_util.c | 1 + block/accounting.c | 1 - hw/core/qdev-properties-system.c | 1 + hw/core/qdev-properties.c | 24 ++++---- hw/input/stellaris_gamepad.c | 8 --- hw/intc/arm_gicv3_common.c | 8 --- hw/intc/rx_icu.c | 8 --- hw/misc/arm_sysctl.c | 2 - hw/misc/mps2-scc.c | 8 --- hw/net/rocker/rocker.c | 1 - hw/nvram/xlnx-efuse.c | 8 --- hw/nvram/xlnx-versal-efuse-ctrl.c | 8 --- hw/virtio/virtio-iommu-pci.c | 8 --- tests/unit/meson.build | 1 + tests/unit/test-qdev.c | 96 +++++++++++++++++++++++++++++++ 15 files changed, 111 insertions(+), 72 deletions(-) create mode 100644 tests/unit/test-qdev.c--
2.51.1
Paolo Bonzini
AppliedQueued, thanks.
Paolo