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 <[email protected]> --- 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
