qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
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 10/22] qdev: Automatically delete memory subregions
Date: Wed, 10 Sep 2025 17:10:25 -0400	[thread overview]
Message-ID: <aMHpQbx1z_p6bC3E@x1.local> (raw)
In-Reply-To: <20250906-use-v1-10-c51caafd1eb7@rsg.ci.i.u-tokyo.ac.jp>

On Sat, Sep 06, 2025 at 04:11:19AM +0200, Akihiko Odaki wrote:
> A common pattern is that to delete memory subregions during realization
> error handling and unrealization. pci automatically automatically
> deletes the IO subregions, but the pattern is manually implemented
> in other places, which is tedious and error-prone.

I don't think they're the same?  What is the ultimate goal of this change?

PCI core only detachs all the BARs from the address space registered from
pci_register_bar() explicitly.  It's not an object_dynamic_cast() detaching
every MR not matter what it is..

The other thing it does is detaching the DMA root memory region.

I'm not fluent with pci, but IMHO it's good to keep those explicit.

> 
> Implement the logic to delete subregions in qdev to cover all devices.
> 
> Signed-off-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
> ---
>  MAINTAINERS            |  1 +
>  include/hw/qdev-core.h |  1 +
>  hw/core/qdev.c         | 14 ++++++++++++++
>  stubs/memory.c         |  9 +++++++++
>  stubs/meson.build      |  1 +
>  5 files changed, 26 insertions(+)
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 8147fff3523eaa45c4a0d2c21d40b4ade3f419ff..4665f0a4b7a513c5863f6d5227a0173c836505e6 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -3212,6 +3212,7 @@ F: include/system/memory.h
>  F: include/system/ram_addr.h
>  F: include/system/ramblock.h
>  F: include/system/memory_mapping.h
> +F: stubs/memory.c
>  F: system/dma-helpers.c
>  F: system/ioport.c
>  F: system/memory.c
> diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
> index 530f3da70218df59da72dc7a975dca8265600e00..8f443d5f8ea5f31d69181cc1ec53a0b022eb71cc 100644
> --- a/include/hw/qdev-core.h
> +++ b/include/hw/qdev-core.h
> @@ -526,6 +526,7 @@ bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
>   *  - unrealize any child buses by calling qbus_unrealize()
>   *    (this will recursively unrealize any devices on those buses)
>   *  - call the unrealize method of @dev
> + *  - remove @dev from memory
>   *
>   * The device can then be freed by causing its reference count to go
>   * to zero.
> diff --git a/hw/core/qdev.c b/hw/core/qdev.c
> index f600226176871361d7ff3875f5d06bd4e614be6e..8fdf6774f87ec8424348e8c9652dc4c99a2faeb5 100644
> --- a/hw/core/qdev.c
> +++ b/hw/core/qdev.c
> @@ -451,6 +451,17 @@ static bool check_only_migratable(Object *obj, Error **errp)
>      return true;
>  }
>  
> +static int del_memory_region(Object *child, void *opaque)
> +{
> +    MemoryRegion *mr = (MemoryRegion *)object_dynamic_cast(child, TYPE_MEMORY_REGION);
> +
> +    if (mr && mr->container) {
> +        memory_region_del_subregion(mr->container, mr);
> +    }
> +
> +    return 0;
> +}
> +
>  static void device_set_realized(Object *obj, bool value, Error **errp)
>  {
>      DeviceState *dev = DEVICE(obj);
> @@ -582,6 +593,7 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
>          if (dc->unrealize) {
>              dc->unrealize(dev);
>          }
> +        object_child_foreach(OBJECT(dev), del_memory_region, NULL);
>          dev->pending_deleted_event = true;
>          DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
>      }
> @@ -606,6 +618,8 @@ post_realize_fail:
>      }
>  
>  fail:
> +    object_child_foreach(OBJECT(dev), del_memory_region, NULL);
> +
>      error_propagate(errp, local_err);
>      if (unattached_parent) {
>          /*
> diff --git a/stubs/memory.c b/stubs/memory.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..9c36531ae542d804dc19ed2a3c657005881a2bca
> --- /dev/null
> +++ b/stubs/memory.c
> @@ -0,0 +1,9 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +
> +#include "qemu/osdep.h"
> +#include "system/memory.h"
> +
> +void memory_region_del_subregion(MemoryRegion *mr,
> +                                 MemoryRegion *subregion)
> +{
> +}
> diff --git a/stubs/meson.build b/stubs/meson.build
> index cef046e6854ddaa9f12714c317a541ea75b8d412..b4df4e60a1af89c9354d5b92449ce5409095b9f1 100644
> --- a/stubs/meson.build
> +++ b/stubs/meson.build
> @@ -95,5 +95,6 @@ if have_system or have_user
>  
>    # Also included in have_system for tests/unit/test-qdev-global-props
>    stub_ss.add(files('hotplug-stubs.c'))
> +  stub_ss.add(files('memory.c'))
>    stub_ss.add(files('sysbus.c'))
>  endif
> 
> -- 
> 2.51.0
> 

-- 
Peter Xu



  reply	other threads:[~2025-09-10 21:11 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
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 [this message]
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=aMHpQbx1z_p6bC3E@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).