qemu-cr16/docs
Peter Xu ac7a892fd3 memory: Fix leaks due to owner-shared MRs circular references
Currently, QEMU refcounts the MR by always taking it from the owner.

It's common that one object will have multiple MR objects embeded in the
object itself.  All the MRs in this case share the same lifespan of the
owner object.

It's also common that in the instance_init() of an object, MR A can be a
container of MR B, C, D, by using memory_region_add_subregion*() set of
memory region APIs.

Now we have a circular reference issue, as when adding subregions for MR A,
we essentially incremented the owner's refcount within the instance_init(),
meaning the object will be self-boosted and its refcount can never go down
to zero if the MRs won't get detached properly before object's finalize().

Delete subregions within object's finalize() won't work either, because
finalize() will be invoked only if the refcount goes to zero first.  What
is worse, object_finalize() will do object_property_del_all() first before
object_deinit().  Since embeded MRs will be properties of the owner object,
it means they'll be freed _before_ the owner's finalize().

To fix that, teach memory API to stop refcount on MRs that share the same
owner.  Because if they share the lifecycle of the owner, then they share
the same lifecycle between themselves, hence the refcount doesn't help but
only introduce troubles.

Meanwhile, allow auto-detachments of MRs during finalize() of MRs even
against its container, as long as they belong to the same owner.

The latter is needed because now it's possible to have MRs' finalize()
happen in any order when they share the same lifespan with a same owner.
In this case, we should allow finalize() to happen in any order of either
the parent or child MR.  Loose the mr->container check in MR's finalize()
to allow auto-detach.  Double check it shares the same owner.

Proper document this behavior in code.

This patch is heavily based on the work done by Akihiko Odaki:

https://lore.kernel.org/r/CAFEAcA8DV40fGsci76r4yeP1P-SP_QjNRDD2OzPxjx5wRs0GEg@mail.gmail.com

Cc: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Clément Mathieu--Drif <clement.mathieu--drif@eviden.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Tested-by: Peter Maydell <peter.maydell@linaro.org>
Link: https://lore.kernel.org/r/20250826221750.285242-1-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
2025-09-15 12:00:12 -04:00
..
_templates
about vfio: Remove 'vfio-platform' 2025-09-08 16:46:31 +02:00
config vl: recognize audiodev groups in configuration files 2023-09-22 17:35:11 +02:00
devel memory: Fix leaks due to owner-shared MRs circular references 2025-09-15 12:00:12 -04:00
interop QAPI patches patches for 2025-07-14 2025-07-16 07:07:30 -04:00
specs docs: fix typo in xive doc 2025-09-03 10:57:50 +03:00
sphinx scripts/kerneldoc: Switch to the Python kernel-doc script 2025-08-30 16:37:23 +01:00
sphinx-static docs/qapi-domain: add CSS styling 2025-03-11 10:10:57 +01:00
spin treewide: fix paths for relocated files in comments 2025-07-02 18:26:27 +02:00
system hw/mips: Remove mipssim machine 2025-09-02 17:57:05 +02:00
tools qemu-img: rebase: refresh options/--help (short option change) 2025-07-15 20:49:01 +02:00
user linux-user: Drop deprecated -p option 2025-08-30 07:00:20 +10:00
block-replication.txt docs/block-replication.txt: Fix replication top-id command demo 2021-12-17 11:35:00 +01:00
bypass-iommu.txt
COLO-FT.txt chardev: finalize 'reconnect' deprecation 2024-10-28 14:37:25 +08:00
colo-proxy.txt colo: examples: remove mentions of script= and (wrong) downscript= 2024-01-30 21:20:20 +03:00
conf.py scripts/kerneldoc: Switch to the Python kernel-doc script 2025-08-30 16:37:23 +01:00
defs.rst.inc
glossary.rst docs: disambiguate cross-references 2025-03-11 10:26:52 +01:00
igd-assign.txt vfio/igd: Require host VGA decode for legacy mode 2025-07-28 17:52:34 +02:00
image-fuzzer.txt docs: Render binary names as monospaced text 2021-11-22 15:02:38 +01:00
index.rst docs: add a glossary 2025-01-17 10:45:54 +00:00
memory-hotplug.txt
meson.build 9p: remove 'proxy' filesystem backend driver 2024-10-03 19:33:25 +02:00
multi-thread-compression.txt docs tests: Fix use of migrate_set_parameter 2023-09-08 13:08:52 +03:00
multiseat.txt docs: Spell QEMU all caps 2021-11-19 10:16:58 +01:00
nvdimm.txt
pci_expander_bridge.txt docs, tests: do not specify scsi=off 2024-06-05 11:00:56 +02:00
pcie.txt docs/pcie.txt: Replace ioh3420 with pcie-root-port 2023-01-28 06:21:30 -05:00
pcie_pci_bridge.txt
pcie_sriov.txt pcie_sriov: Ensure VF addr does not overflow 2025-02-20 18:23:19 -05:00
qcow2-cache.txt docs/interop: convert text files to restructuredText 2025-05-29 17:45:10 +01:00
qdev-device-use.txt util: remove support -chardev tty and -chardev parport 2023-01-06 00:51:02 +01:00
qemu-option-trace.rst.inc
qemupciserial.inf
rdma.txt docs tests: Fix use of migrate_set_parameter 2023-09-08 13:08:52 +03:00
requirements.txt docs: Bump sphinx to 6.2.1 2025-07-16 17:02:46 +02:00
spice-port-fqdn.txt
throttle.txt docs: Drop deprecated 'props' from object-add 2021-11-22 15:02:38 +01:00
xbzrle.txt
xen-save-devices-state.txt