All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Cédric Le Goater" <clg@redhat.com>
To: Mark Cave-Ayland <mark.caveayland@nutanix.com>,
	alex@shazbot.org, qemu-devel@nongnu.org
Subject: Re: [PATCH] vfio: fix memory leaks during realize failures
Date: Mon, 30 Nov 2026 19:12:52 +0100	[thread overview]
Message-ID: <39ff6279-eb85-416d-affe-ef2680107aea@redhat.com> (raw)
In-Reply-To: <20261112094802.2904005-1-mark.caveayland@nutanix.com>

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 &region->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.





      reply	other threads:[~2026-11-30 18:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=39ff6279-eb85-416d-affe-ef2680107aea@redhat.com \
    --to=clg@redhat.com \
    --cc=alex@shazbot.org \
    --cc=mark.caveayland@nutanix.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.