From: Peter Xu <peterx@redhat.com>
To: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
Cc: qemu-devel@nongnu.org,
"Alex Williamson" <alex.williamson@redhat.com>,
"Cédric Le Goater" <clg@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Eduardo Habkost" <eduardo@habkost.net>,
"David Hildenbrand" <david@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Helge Deller" <deller@gmx.de>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"John Snow" <jsnow@redhat.com>,
qemu-block@nongnu.org, "Keith Busch" <kbusch@kernel.org>,
"Klaus Jensen" <its@irrelevant.dk>,
"Jesper Devantier" <foss@defmacro.it>,
"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
"Nicholas Piggin" <npiggin@gmail.com>,
qemu-ppc@nongnu.org, "John Levon" <john.levon@nutanix.com>,
"Thanos Makatos" <thanos.makatos@nutanix.com>,
"Yanan Wang" <wangyanan55@huawei.com>,
"BALATON Zoltan" <balaton@eik.bme.hu>,
"Jiaxun Yang" <jiaxun.yang@flygoat.com>,
"Daniel Henrique Barboza" <danielhb413@gmail.com>,
"David Gibson" <david@gibson.dropbear.id.au>,
"Harsh Prateek Bora" <harshpb@linux.ibm.com>,
"Alexey Kardashevskiy" <aik@ozlabs.ru>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Fabiano Rosas" <farosas@suse.de>,
"Thomas Huth" <thuth@redhat.com>,
"Laurent Vivier" <lvivier@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Aurelien Jarno" <aurelien@aurel32.net>,
"Aleksandar Rikalo" <arikalo@gmail.com>,
"Max Filippov" <jcmvbkbc@gmail.com>,
"Hervé Poussineau" <hpoussin@reactos.org>,
"Mark Cave-Ayland" <mark.cave-ayland@ilande.co.uk>,
"Artyom Tarasenko" <atar4qemu@gmail.com>
Subject: Re: [PATCH 02/22] vfio/pci: Do not unparent in instance_finalize()
Date: Wed, 10 Sep 2025 16:41:24 -0400 [thread overview]
Message-ID: <aMHidDl1tdx-2G4e@x1.local> (raw)
In-Reply-To: <20250906-use-v1-2-c51caafd1eb7@rsg.ci.i.u-tokyo.ac.jp>
On Sat, Sep 06, 2025 at 04:11:11AM +0200, Akihiko Odaki wrote:
> Children 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.
>
> Signed-off-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
> ---
> hw/vfio/pci.c | 4 ----
> 1 file changed, 4 deletions(-)
>
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index 07257d0fa049b09fc296ac2279a6fafbdf93d277..2e909c190f86a722e1022fa7c45a96d2dde8d58e 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -2000,7 +2000,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;
> }
> @@ -2008,9 +2007,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);
> }
> }
So the 2nd object_unparent() here should be no-op, seeing empty list of
properties (but shouldn't causing anything severe), is that correct?
I think it still makes some sense to theoretically allow object_unparent()
to happen, at least when it happens before owner's finalize(). IIUC that
was the intention of the doc, pairing the memory_region_init*() operation.
It might depend on two use cases:
1. MRs dynamically created, it'll share the same lifecycle of the owner
after creation (just like an embeded MemoryRegion)
I feel like most, if not all, VFIO's dynamic mrs follows this trend,
that this patch touched.
In this case, IMHO instead of object_unparent(), we could also allow the
owner / device to take ownership of the MR completely, by replacing:
mr = g_new0(MemoryRegion, 1);
with:
mr = object_new(TYPE_MEMORY_REGION);
Then after memory_region_init*(), essentially the owner will be in
charge of the memory, as it'll be g_free()ed when remove the mr from
property list (in owner's finalize() automatically).
With that, the device impl can not only avoid object_unparent(), but
avoid g_free() altogether. That would make some more sense to me,
instead of relying on memory internal to unparent, and rely on device
impl to g_free().
2. MRs dynamically created, and it may be freed even before the owner
finishes its lifecycle
This is the case that I _think_ an object_unparent() should still be
allowed, because when the mr is unparented (aka, not used), we should
still provide a way for the device impl to detach and free the mr
resources on the fly.
There're a bunch of object_unparent() users, I didn't check whether there's
any real user of case (2), though.
AFAIU for such case maybe it's always better to provide real refcounting
for the mr, since the mr can always be address_space_map()ed.. with an
elevated refcount. In that case, the owner shouldn't be the device impl,
but a temp obj that represents the mr (and do refcounts).
Thanks,
--
Peter Xu
next prev parent reply other threads:[~2025-09-10 20:42 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-06 2:11 [PATCH 00/22] Fix memory region leaks and use-after-finalization Akihiko Odaki
2025-09-06 2:11 ` [PATCH 01/22] docs/devel: Do not unparent in instance_finalize() Akihiko Odaki
2025-09-06 2:11 ` [PATCH 02/22] vfio/pci: " Akihiko Odaki
2025-09-10 20:41 ` Peter Xu [this message]
2025-09-11 3:47 ` Akihiko Odaki
2025-09-11 21:37 ` Peter Xu
2025-09-12 2:09 ` Akihiko Odaki
2025-09-12 21:26 ` Peter Xu
2025-09-14 9:06 ` Akihiko Odaki
2025-09-15 20:30 ` Peter Xu
2025-09-16 11:45 ` Akihiko Odaki
2025-09-06 2:11 ` [PATCH 03/22] hw/pci-bridge: Do not assume immediate MemoryRegion finalization Akihiko Odaki
2025-09-06 2:11 ` [PATCH 04/22] target/mips: Fix AddressSpace exposure timing Akihiko Odaki
2025-09-09 9:39 ` Thomas Huth
2025-09-06 2:11 ` [PATCH 05/22] target/xtensa: " Akihiko Odaki
2025-09-09 9:42 ` Thomas Huth
2025-09-06 2:11 ` [PATCH 06/22] auxbus: " Akihiko Odaki
2025-09-09 9:43 ` Thomas Huth
2025-09-06 2:11 ` [PATCH 07/22] hw/pci-host/raven: " Akihiko Odaki
2025-09-06 9:03 ` BALATON Zoltan
2025-09-08 15:20 ` Akihiko Odaki
2025-09-08 15:31 ` Peter Maydell
2025-09-06 2:11 ` [PATCH 08/22] sun4m: " Akihiko Odaki
2025-09-09 9:48 ` Thomas Huth
2025-09-09 20:25 ` Mark Cave-Ayland
2025-09-06 2:11 ` [PATCH 09/22] sun4u: " Akihiko Odaki
2025-09-09 9:54 ` Thomas Huth
2025-09-09 20:26 ` Mark Cave-Ayland
2025-09-06 2:11 ` [PATCH 10/22] qdev: Automatically delete memory subregions Akihiko Odaki
2025-09-10 21:10 ` Peter Xu
2025-09-11 3:55 ` Akihiko Odaki
2025-09-06 2:11 ` [PATCH 11/22] vfio-user: Do not delete the subregion Akihiko Odaki
2025-09-06 2:11 ` [PATCH 12/22] hw/char/diva-gsp: " Akihiko Odaki
2025-09-06 2:11 ` [PATCH 13/22] hw/char/serial-pci-multi: " Akihiko Odaki
2025-09-06 2:11 ` [PATCH 14/22] secondary-vga: Do not delete the subregions Akihiko Odaki
2025-09-06 2:11 ` [PATCH 15/22] cmd646: " Akihiko Odaki
2025-09-06 2:11 ` [PATCH 16/22] hw/ide/piix: " Akihiko Odaki
2025-09-06 2:11 ` [PATCH 17/22] hw/ide/via: " Akihiko Odaki
2025-09-06 2:11 ` [PATCH 18/22] hw/nvme: Do not delete the subregion Akihiko Odaki
2025-09-06 2:11 ` [PATCH 19/22] pci: Do not delete the subregions Akihiko Odaki
2025-09-06 2:11 ` [PATCH 20/22] hw/ppc/spapr_pci: " Akihiko Odaki
2025-09-06 2:11 ` [PATCH 21/22] hw/usb/hcd-ehci: " Akihiko Odaki
2025-09-06 2:11 ` [PATCH 22/22] hw/usb/hcd-xhci: " Akihiko Odaki
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=aMHidDl1tdx-2G4e@x1.local \
--to=peterx@redhat.com \
--cc=aik@ozlabs.ru \
--cc=alex.bennee@linaro.org \
--cc=alex.williamson@redhat.com \
--cc=arikalo@gmail.com \
--cc=atar4qemu@gmail.com \
--cc=aurelien@aurel32.net \
--cc=balaton@eik.bme.hu \
--cc=berrange@redhat.com \
--cc=clg@redhat.com \
--cc=danielhb413@gmail.com \
--cc=david@gibson.dropbear.id.au \
--cc=david@redhat.com \
--cc=deller@gmx.de \
--cc=eduardo@habkost.net \
--cc=farosas@suse.de \
--cc=foss@defmacro.it \
--cc=harshpb@linux.ibm.com \
--cc=hpoussin@reactos.org \
--cc=its@irrelevant.dk \
--cc=jcmvbkbc@gmail.com \
--cc=jiaxun.yang@flygoat.com \
--cc=john.levon@nutanix.com \
--cc=jsnow@redhat.com \
--cc=kbusch@kernel.org \
--cc=kraxel@redhat.com \
--cc=lvivier@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=mark.cave-ayland@ilande.co.uk \
--cc=mst@redhat.com \
--cc=npiggin@gmail.com \
--cc=odaki@rsg.ci.i.u-tokyo.ac.jp \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=thanos.makatos@nutanix.com \
--cc=thuth@redhat.com \
--cc=wangyanan55@huawei.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).