* [PATCH] vfio: fix memory leaks during realize failures
@ 2026-11-12 9:46 Mark Cave-Ayland
2026-11-30 18:12 ` Cédric Le Goater
0 siblings, 1 reply; 2+ messages in thread
From: Mark Cave-Ayland @ 2026-11-12 9:46 UTC (permalink / raw)
To: alex, clg, qemu-devel
Both VFIOBAR and VFIORegion allocate a new MemoryRegion for use with the owner
being the vfio-pci device itself. In the normal case where the vfio-pci device
is finalized, the refcount on these MemoryRegions is decremented as normal
causing them to be removed.
Unfortunately this doesn't work if the vfio-pci device realize() function
itself fails: in this case the vfio-pci device is never finalized leaving
these MemoryRegions still allocated when the QEMU process terminates.
Ensure that these MemoryRegions are always removed by manually unparenting
them in the corresponding vfio_bars_finalize() and vfio_region_finalize()
functions.
Signed-off-by: Mark Cave-Ayland <mark.caveayland@nutanix.com>
---
hw/vfio/pci.c | 1 +
hw/vfio/region.c | 1 +
2 files changed, 2 insertions(+)
[MCA: I found this whilst making changes to vfio-user that tested failure
paths in vfio_user_pci_realize(). In this case there were a number of
leaked MemoryRegions appearing in the output of a '--cc=clang
--cxx=clang++ --enable-ubsan --enable-asan' build.]
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 0da2e143b8..05cce1e252 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -2062,6 +2062,7 @@ 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;
}
diff --git a/hw/vfio/region.c b/hw/vfio/region.c
index 8de26f3d9b..15c8850072 100644
--- a/hw/vfio/region.c
+++ b/hw/vfio/region.c
@@ -523,6 +523,7 @@ void vfio_region_finalize(VFIORegion *region)
}
}
+ object_unparent(OBJECT(region->mem));
g_free(region->mem);
g_free(region->mmaps);
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] vfio: fix memory leaks during realize failures
2026-11-12 9:46 [PATCH] vfio: fix memory leaks during realize failures Mark Cave-Ayland
@ 2026-11-30 18:12 ` Cédric Le Goater
0 siblings, 0 replies; 2+ messages in thread
From: Cédric Le Goater @ 2026-11-30 18:12 UTC (permalink / raw)
To: Mark Cave-Ayland, alex, qemu-devel
On 11/12/26 10:46, Mark Cave-Ayland wrote:
> Both VFIOBAR and VFIORegion allocate a new MemoryRegion for use with the owner
> being the vfio-pci device itself. In the normal case where the vfio-pci device
> is finalized, the refcount on these MemoryRegions is decremented as normal
> causing them to be removed.
>
> Unfortunately this doesn't work if the vfio-pci device realize() function
> itself fails: in this case the vfio-pci device is never finalized leaving
> these MemoryRegions still allocated when the QEMU process terminates.
>
> Ensure that these MemoryRegions are always removed by manually unparenting
> them in the corresponding vfio_bars_finalize() and vfio_region_finalize()
> functions.
>
> Signed-off-by: Mark Cave-Ayland <mark.caveayland@nutanix.com>
> ---
> hw/vfio/pci.c | 1 +
> hw/vfio/region.c | 1 +
> 2 files changed, 2 insertions(+)
>
> [MCA: I found this whilst making changes to vfio-user that tested failure
> paths in vfio_user_pci_realize(). In this case there were a number of
> leaked MemoryRegions appearing in the output of a '--cc=clang
> --cxx=clang++ --enable-ubsan --enable-asan' build.]
>
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index 0da2e143b8..05cce1e252 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -2062,6 +2062,7 @@ 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;
> }
> diff --git a/hw/vfio/region.c b/hw/vfio/region.c
> index 8de26f3d9b..15c8850072 100644
> --- a/hw/vfio/region.c
> +++ b/hw/vfio/region.c
> @@ -523,6 +523,7 @@ void vfio_region_finalize(VFIORegion *region)
> }
> }
What about the ®ion->mmaps[i].mem ? Shouldn't it be unparented too ?
>
> + object_unparent(OBJECT(region->mem));
> g_free(region->mem);
> g_free(region->mmaps);
>
I see that vfio_user_pci_realize() and vfio_user_pci_finalize() both
call vfio_pci_put_device(). Can't it be called twice ?
we should harden vfio_pci_put_device() to avoid double frees.
Thanks,
C.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 18:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 9:46 [PATCH] vfio: fix memory leaks during realize failures Mark Cave-Ayland
2026-11-30 18:12 ` Cédric Le Goater
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.