Do not unparent in instance_finalize()
v4 cover Do not unparent in instance_finalize()
Supersedes: <[email protected]> ("[PATCH] docs/devel: Prohibit calling object_unparent() for memory region")
Children are automatically unparented so manually unparenting is unnecessary.
Worse, automatic unparenting happens before the instance_finalize() callback of the parent gets called, so object_unparent() calls in the callback will refer to objects that are already unparented, which is semantically incorrect.
---
Changes in v4:
- Removed Based-on:.
- Restored the examples of VFIOMSIXInfo and VFIOQuirk in
docs/devel/memory.rst.
- Ensured that the timing to call object_unparent() and free memory
regions is mentioned once for each.
- Link to v3: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v3:
- Added patches to remove other object_unparent() calls in
instance_finalize().
- Dropped patch "qdev: Automatically delete memory subregions" and the
succeeding patches to avoid Ccing many.
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v2:
- Added a reference to "[PATCH] docs/devel: Prohibit calling
object_unparent() for memory region", which does something similar to
patch "docs/devel: Do not unparent in instance_finalize()" but I
forgot I sent it in the past.
- Fixed a typo in patch
"docs/devel: Do not unparent in instance_finalize()" and
"[PATCH 02/22] vfio/pci: Do not unparent in instance_finalize()".
- Dropped patches to move address_space_init() calls; I intend to
QOM-ify to fix memory leaks automatically as discussed in the
following thread:
https://lore.kernel.org/qemu-devel/[email protected]/
But the QOM-ification will be big so I'll send it as a separate
series.
- Rebased on top of "[PATCH v2 00/14] hw/pci-host/raven clean ups".
https://lore.kernel.org/qemu-devel/[email protected]/
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
---
Akihiko Odaki (7):
docs/devel: Do not unparent in instance_finalize() vfio/pci: Do not unparent in instance_finalize() hw/core/register: Do not unparent in instance_finalize()
hv-balloon: hw/core/register: Do not unparent in instance_finalize() hw/sd/sdhci: Do not unparent in instance_finalize()
vfio: Do not unparent in instance_finalize()
hw/xen: Do not unparent in instance_finalize()
docs/devel/memory.rst | 17 ++++++----------- hw/core/register.c | 1 - hw/hyperv/hv-balloon.c | 12 +----------- hw/sd/sdhci.c | 4 ---- hw/vfio/pci-quirks.c | 9 +-------- hw/vfio/pci.c | 4 ---- hw/vfio/region.c | 3 --- hw/xen/xen_pt_msi.c | 11 +---------- 8 files changed, 9 insertions(+), 52 deletions(-)
---
base-commit: ab8008b231e758e03c87c1c483c03afdd9c02e19
change-id: 20250906-use-37ecc903a9e0
Best regards,
Signature · 1 line
Akihiko Odaki <[email protected]>
PATCH v4 1/7 docs/devel: Do not unparent in instance_finalize()
Children are automatically unparented so manually unparenting is unnecessary.
Worse, automatic unparenting happens before the instance_finalize() callback of the parent gets called, so object_unparent() calls in the callback will refer to objects that are already unparented, which is semantically incorrect.
Remove the instruction to call object_unparent(), and the exception of the "do not call object_unparent()" rule for instance_finalize().
--- docs/devel/memory.rst | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
docs/devel/memory.rst +6 -11
@@ -165,17 +165,14 @@ and finalized one by one. The order in which memory regions will be finalized is not guaranteed. If however the memory region is part of a dynamically allocated data -structure, you should call object_unparent() to destroy the memory region -before the data structure is freed. For an example see VFIOMSIXInfo -and VFIOQuirk in hw/vfio/pci.c. +structure, you should free the memory region in the instance_finalize +callback. For an example see VFIOMSIXInfo and VFIOQuirk in +hw/vfio/pci.c. You must not destroy a memory region as long as it may be in use by a device or CPU. In order to do this, as a general rule do not create or -destroy memory regions dynamically during a device's lifetime, and only -call object_unparent() in the memory region owner's instance_finalize -callback. The dynamically allocated data structure that contains the -memory region then should obviously be freed in the instance_finalize -callback as well. +destroy memory regions dynamically during a device's lifetime, and must +never call object_unparent(). If you break this rule, the following situation can happen:
@@ -201,9 +198,7 @@ this exception is rarely necessary, and therefore it is discouraged, but nevertheless it is used in a few places. For regions that "have no owner" (NULL is passed at creation time), the -machine object is actually used as the owner. Since instance_finalize is -never called for the machine object, you must never call object_unparent -on regions that have no owner, unless they are aliases or containers. +machine object is actually used as the owner. Overlapping regions and priority
Signature · 1 line
2.51.0
PATCH v4 2/7 vfio/pci: Do not unparent in instance_finalize()
In reply to Akihiko OdakiChildren are automatically unparented so manually unparenting is unnecessary.
Worse, automatic unparenting happens before the insntance_finalize() callback of the parent gets called, so object_unparent() calls in the callback will refer to objects that are already unparented, which is semantically incorrect.
--- hw/vfio/pci.c | 4 ---- 1 file changed, 4 deletions(-)
hw/vfio/pci.c +0 -4
@@ -2025,7 +2025,6 @@ static void vfio_bars_finalize(VFIOPCIDevice *vdev)vfio_region_finalize (&bar->region);if (bar->mr) {assert (bar->size); -object_unparent (OBJECT (bar->mr));g_free (bar->mr); bar->mr =NULL ; }
@@ -2033,9 +2032,6 @@ static void vfio_bars_finalize(VFIOPCIDevice *vdev)if (vdev->vga) {vfio_vga_quirk_finalize (vdev); -for (i =0 ; i <ARRAY_SIZE (vdev->vga->region); i++) { -object_unparent (OBJECT (&vdev->vga->region[i].mem)); - }g_free (vdev->vga); } }
Signature · 1 line
2.51.0
Re: [PATCH v4 2/7] vfio/pci: Do not unparent in instance_finalize()
Quoted 28 lines · Akihiko Odaki
Children are automatically unparented so manually unparenting is unnecessary.
Worse, automatic unparenting happens before the
insntance_finalize()callback of the parent gets called, soobject_unparent()calls in the callback will refer to objects that are already unparented, which is semantically incorrect.Signed-off-by Akihiko Odaki <[email protected]>Reviewed-by Daniel P. Berrangé <[email protected]>--- hw/vfio/pci.c | 4 ---- 1 file changed, 4 deletions(-)hw/vfio/pci.c +0
-1@@ -2025,7 +2025,6 @@ static void vfio_bars_finalize(VFIOPCIDevice *vdev)vfio_region_finalize (&bar->region);if (bar->mr) {assert (bar->size); -object_unparent (OBJECT (bar->mr));g_free (bar->mr); bar->mr =NULL ; }
hw/vfio/pci.c static void vfio_bars_finalize(VFIOPCIDevice *vdev) · line 2037
Quoted 9 lines
hw/vfio/pci.c +0
-3@@ -2033,9 +2032,6 @@ static void vfio_bars_finalize(VFIOPCIDevice *vdev)if (vdev->vga) {vfio_vga_quirk_finalize (vdev); -for (i =0 ; i <ARRAY_SIZE (vdev->vga->region); i++) { -object_unparent (OBJECT (&vdev->vga->region[i].mem)); - }g_free (vdev->vga); } }
Thanks,
C.
PATCH v4 3/7 hw/core/register: Do not unparent in instance_finalize()
In reply to Akihiko OdakiChildren are automatically unparented so manually unparenting is unnecessary.
Worse, automatic unparenting happens before the instance_finalize() callback of the parent gets called, so object_unparent() calls in the callback will refer to objects that are already unparented, which is semantically incorrect.
--- hw/core/register.c | 1 - 1 file changed, 1 deletion(-)
hw/core/register.c +0 -1
@@ -314,7 +314,6 @@ RegisterInfoArray *register_init_block64(DeviceState *owner,void register_finalize_block (RegisterInfoArray *r_array) { -object_unparent (OBJECT (&r_array->mem));g_free (r_array->r);g_free (r_array); }
Signature · 1 line
2.51.0
PATCH v4 4/7 hv-balloon: hw/core/register: Do not unparent in instance_finalize()
In reply to Akihiko OdakiChildren are automatically unparented so manually unparenting is unnecessary.
Worse, automatic unparenting happens before the instance_finalize() callback of the parent gets called, so object_unparent() calls in the callback will refer to objects that are already unparented, which is semantically incorrect.
--- hw/hyperv/hv-balloon.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
hw/hyperv/hv-balloon.c +1 -11
@@ -1475,16 +1475,6 @@ static void hv_balloon_ensure_mr(HvBalloon *balloon) balloon->mr->align =memory_region_get_alignment (hostmem_mr); } -static void hv_balloon_free_mr (HvBalloon *balloon) -{ -if (!balloon->mr) { -return ; - } - -object_unparent (OBJECT (balloon->mr)); -g_clear_pointer (&balloon->mr, g_free); -} -static void hv_balloon_vmdev_realize (VMBusDevice *vdev,Error **errp) {ERRP_GUARD ();
@@ -1580,7 +1570,7 @@ static void hv_balloon_vmdev_reset(VMBusDevice *vdev)*/ static void hv_balloon_unrealize_finalize_common (HvBalloon *balloon) { -hv_balloon_free_mr (balloon); +g_clear_pointer (&balloon->mr, g_free); balloon->addr =0 ; balloon->memslot_count =0 ;
Signature · 1 line
2.51.0
PATCH v4 5/7 hw/sd/sdhci: Do not unparent in instance_finalize()
In reply to Akihiko OdakiChildren are automatically unparented so manually unparenting is unnecessary.
Worse, automatic unparenting happens before the instance_finalize() callback of the parent gets called, so object_unparent() calls in the callback will refer to objects that are already unparented, which is semantically incorrect.
--- hw/sd/sdhci.c | 4 ---- 1 file changed, 4 deletions(-)
hw/sd/sdhci.c +0 -4
@@ -1578,10 +1578,6 @@ static void sdhci_sysbus_finalize(Object *obj) {SDHCIState *s =SYSBUS_SDHCI (obj); -if (s->dma_mr) { -object_unparent (OBJECT (s->dma_mr)); - } -sdhci_uninitfn (s); }
Signature · 1 line
2.51.0
PATCH v4 6/7 vfio: Do not unparent in instance_finalize()
In reply to Akihiko OdakiChildren are automatically unparented so manually unparenting is unnecessary.
Worse, automatic unparenting happens before the instance_finalize() callback of the parent gets called, so object_unparent() calls in the callback will refer to objects that are already unparented, which is semantically incorrect.
--- hw/vfio/pci-quirks.c | 9 +-------- hw/vfio/region.c | 3 --- 2 files changed, 1 insertion(+), 11 deletions(-)
hw/vfio/pci-quirks.c +1 -8
@@ -1159,15 +1159,12 @@ void vfio_vga_quirk_exit(VFIOPCIDevice *vdev)void vfio_vga_quirk_finalize (VFIOPCIDevice *vdev) { -int i, j; +int i;for (i =0 ; i <ARRAY_SIZE (vdev->vga->region); i++) {while (!QLIST_EMPTY (&vdev->vga->region[i].quirks)) {VFIOQuirk *quirk =QLIST_FIRST (&vdev->vga->region[i].quirks);QLIST_REMOVE (quirk, next); -for (j =0 ; j < quirk->nr_mem; j++) { -object_unparent (OBJECT (&quirk->mem[j])); - }g_free (quirk->mem);g_free (quirk->data);g_free (quirk);
@@ -1207,14 +1204,10 @@ void vfio_bar_quirk_exit(VFIOPCIDevice *vdev, int nr)void vfio_bar_quirk_finalize (VFIOPCIDevice *vdev,int nr) {VFIOBAR *bar = &vdev->bars[nr]; -int i;while (!QLIST_EMPTY (&bar->quirks)) {VFIOQuirk *quirk =QLIST_FIRST (&bar->quirks);QLIST_REMOVE (quirk, next); -for (i =0 ; i < quirk->nr_mem; i++) { -object_unparent (OBJECT (&quirk->mem[i])); - }g_free (quirk->mem);g_free (quirk->data);g_free (quirk);
hw/vfio/region.c +0 -3
@@ -365,12 +365,9 @@ void vfio_region_finalize(VFIORegion *region)for (i =0 ; i < region->nr_mmaps; i++) {if (region->mmaps[i].mmap) {munmap (region->mmaps[i].mmap, region->mmaps[i].size); -object_unparent (OBJECT (®ion->mmaps[i].mem)); } } -object_unparent (OBJECT (region->mem)); -g_free (region->mem);g_free (region->mmaps);
Signature · 1 line
2.51.0
Re: [PATCH v4 6/7] vfio: Do not unparent in instance_finalize()
Quoted 59 lines · Akihiko Odaki
Children are automatically unparented so manually unparenting is unnecessary.
Worse, automatic unparenting happens before the
instance_finalize()callback of the parent gets called, soobject_unparent()calls in the callback will refer to objects that are already unparented, which is semantically incorrect.Signed-off-by Akihiko Odaki <[email protected]>Reviewed-by Daniel P. Berrangé <[email protected]>--- hw/vfio/pci-quirks.c | 9 +-------- hw/vfio/region.c | 3 --- 2 files changed, 1 insertion(+), 11 deletions(-)hw/vfio/pci-quirks.c +1
-8@@ -1159,15 +1159,12 @@ void vfio_vga_quirk_exit(VFIOPCIDevice *vdev)void vfio_vga_quirk_finalize (VFIOPCIDevice *vdev) { -int i, j; +int i;for (i =0 ; i <ARRAY_SIZE (vdev->vga->region); i++) {while (!QLIST_EMPTY (&vdev->vga->region[i].quirks)) {VFIOQuirk *quirk =QLIST_FIRST (&vdev->vga->region[i].quirks);QLIST_REMOVE (quirk, next); -for (j =0 ; j < quirk->nr_mem; j++) { -object_unparent (OBJECT (&quirk->mem[j])); - }g_free (quirk->mem);g_free (quirk->data);g_free (quirk);@@ -1207,14 +1204,10 @@ void vfio_bar_quirk_exit(VFIOPCIDevice *vdev, int nr)void vfio_bar_quirk_finalize (VFIOPCIDevice *vdev,int nr) {VFIOBAR *bar = &vdev->bars[nr]; -int i;while (!QLIST_EMPTY (&bar->quirks)) {VFIOQuirk *quirk =QLIST_FIRST (&bar->quirks);QLIST_REMOVE (quirk, next); -for (i =0 ; i < quirk->nr_mem; i++) { -object_unparent (OBJECT (&quirk->mem[i])); - }g_free (quirk->mem);g_free (quirk->data);g_free (quirk);hw/vfio/region.c +0
-0@@ -365,12 +365,9 @@ void vfio_region_finalize(VFIORegion *region)for (i =0 ; i < region->nr_mmaps; i++) {if (region->mmaps[i].mmap) {
hw/vfio/region.c void vfio_region_finalize(VFIORegion *region) · line 373
Quoted 10 lines
hw/vfio/region.c +0
-3munmap (region->mmaps[i].mmap, region->mmaps[i].size); -object_unparent (OBJECT (®ion->mmaps[i].mem)); } } -object_unparent (OBJECT (region->mem)); -g_free (region->mem);g_free (region->mmaps);
What about vfio_subregion_unmap() calling object_unparent() too ?
C.
PATCH v4 7/7 hw/xen: Do not unparent in instance_finalize()
In reply to Akihiko OdakiChildren are automatically unparented so manually unparenting is unnecessary.
Worse, automatic unparenting happens before the instance_finalize() callback of the parent gets called, so object_unparent() calls in the callback will refer to objects that are already unparented, which is semantically incorrect.
--- hw/xen/xen_pt_msi.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
hw/xen/xen_pt_msi.c +1 -10
@@ -637,14 +637,5 @@ void xen_pt_msix_unmap(XenPCIPassthroughState *s)void xen_pt_msix_delete (XenPCIPassthroughState *s) { -XenPTMSIX *msix = s->msix; - -if (!msix) { -return ; - } - -object_unparent (OBJECT (&msix->mmio)); - -g_free (s->msix); - s->msix =NULL ; +g_clear_pointer (&s->msix, g_free); }
Signature · 1 line
2.51.0