memory: Fix leaks due to owner-shared MRs circular references
PATCH 1/1 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
--- docs/devel/memory.rst | 7 +++++-- system/memory.c | 45 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 11 deletions(-)
docs/devel/memory.rst +5 -2
@@ -158,8 +158,11 @@ ioeventfd) can be changed during the region lifecycle. They take effect as soon as the region is made visible. This can be immediately, later, or never. -Destruction of a memory region happens automatically when the owner -object dies. +Destruction of a memory region happens automatically when the owner object +dies. When there are multiple memory regions under the same owner object, +the memory API will guarantee all memory regions will be properly detached +and finalized one by one. The order which memory region will be finalized +first is not guaranteed. If however the memory region is part of a dynamically allocated data structure, you should call object_unparent() to destroy the memory region
system/memory.c +36 -9
66 lines not on this page · raw message
Signature · 1 line
2.50.1
Quoted 74 lines · Peter Xu
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'sfinalize().Delete subregions within object's
finalize()won't work either, becausefinalize()will be invoked only if the refcount goes to zero first. What is worse,object_finalize()will doobject_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'sfinalize().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 allowfinalize()to happen in any order of either the parent or child MR. Loose themr->containercheck in MR'sfinalize()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:
Signed-off-by Peter Xu <[[email protected]](mailto:[email protected])>---
docs/devel/memory.rst | 7 +++++--
system/memory.c | 45 ++++++++++++++++++++++++++++++++++---------2 files changed, 41 insertions(+), 11 deletions(-)docs/devel/memory.rst +0
-0@@ -158,8 +158,11 @@ ioeventfd) can be changed during the region lifecycle. They take effect as soon as the region is made visible. This can be immediately, later,or never.
docs/devel/memory.rst +5
-2-Destruction of a memory region happens automatically when the owner -object dies. +Destruction of a memory region happens automatically when the owner object +dies. When there are multiple memory regions under the same owner object, +the memory API will guarantee all memory regions will be properly detached +and finalized one by one. The order which memory region will be finalized +first is not guaranteed.
If however the memory region is part of a dynamically allocated data structure, you should callobject_unparent()to destroy the memory regionsystem/memory.c +0
-0@@ -1796,16 +1796,36 @@ static void memory_region_finalize(Object *obj) { MemoryRegion *mr = MEMORY_REGION(obj);
-assert(!mr->container);
Quoted 8 lines
-
- /* We know the region is not visible in any address space (it - * does not have a container and cannot be a root either because - * it has no references, so we can blindly clearmr->enabled. - *memory_region_set_enabledinstead could trigger a transaction - * and cause an infinite loop.
+ /*
+ * Each memory region (that can be dynamically freed..) must has an
"must have" or "has"
Quoted 7 lines
+ * owner, and it always has the same lifecycle of its owner. It means + * when reaching here, the memory region's owner refcount is zero. + *
+ * Here it is possible that the MR has:
+ *
+ * (1)mr->containerset, which means this MR can be a subregion of a + * container MR, in this case it must share the same owner
s/it/they ?
Quoted 39 lines
+ * (otherwise the container should have kept a refcount of this + * MR's owner), or,
+ *
+ * (2)mr->subregionsnon-empty, which means this MR can be a container + * of other MRs (share the owner or not).
+ *
+ * We know the MR, or any MR that is attached to this one as either + * container or children, is not visible in any address space, because + * otherwise the address space should have taken at least one refcount + * of this MR's owner. So we can blindly clearmr->enabled. + *
+ *memory_region_set_enabledinstead could trigger a transaction and + * cause an infinite loop.
*/
mr->enabled= false;
memory_region_transaction_begin();
+ if (mr->container) {
+ /* Must share the owner; see above comments */
+assert(mr->container->owner == mr->owner);
+memory_region_del_subregion(mr->container, mr);
+ }
while (!QTAILQ_EMPTY(&mr->subregions)) {
MemoryRegion *subregion =QTAILQ_FIRST(&mr->subregions);memory_region_del_subregion(mr, subregion);system/memory.c +0
-0@@ -2625,7 +2645,10 @@ static void memory_region_update_container_subregions(MemoryRegion *subregion)
memory_region_transaction_begin();
-memory_region_ref(subregion);
+ if (mr->owner!=subregion->owner) {
+memory_region_ref(subregion);
+ }
+
QTAILQ_FOREACH(other, &mr->subregions, subregions_link){ if (subregion->priority>=other->priority) {QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);system/memory.c +0
-0@@ -2683,7 +2706,11 @@ void memory_region_del_subregion(MemoryRegion *mr, assert(alias->mapped_via_alias >= 0); }
system/memory.c void memory_region_del_subregion(MemoryRegion *mr, · line 2716
Quoted 10 lines
system/memory.c +5
-1QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link); - memory_region_unref(subregion); + + if (mr->owner != subregion->owner) { + memory_region_unref(subregion); + } + memory_region_update_pending |= mr->enabled && subregion->enabled; memory_region_transaction_commit(); }
Quoted 36 lines · Peter Xu
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'sfinalize().Delete subregions within object's
finalize()won't work either, becausefinalize()will be invoked only if the refcount goes to zero first. What is worse,object_finalize()will doobject_property_del_all()first beforeobject_deinit(). Since embeded MRs will be properties of the owner object, it means they'll be freed _before_ the owner'sfinalize().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 allowfinalize()to happen in any order of either the parent or child MR. Loose themr->containercheck in MR'sfinalize()to allow auto-detach. Double check it shares the same owner.
Quoted 7 lines
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
Signed-off-by Peter Xu <[email protected]>
I have some wordsmithing review stuff for doc and comment text below, but otherwise
Quoted 10 lines
--- docs/devel/memory.rst | 7 +++++-- system/memory.c | 45 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 11 deletions(-)docs/devel/memory.rst +0
-0
docs/devel/memory.rst ioeventfd) can be changed during the region lifecycle. They take effect · line 164
Quoted 9 lines
docs/devel/memory.rst +4
-2@@ -158,8 +158,11 @@ ioeventfd) can be changed during the region lifecycle. They take effect as soon as the region is made visible. This can be immediately, later, or never. -Destruction of a memory region happens automatically when the owner -object dies. +Destruction of a memory region happens automatically when the owner object +dies. When there are multiple memory regions under the same owner object, +the memory API will guarantee all memory regions will be properly detached +and finalized one by one. The order which memory region will be finalized
"The order in which memory regions will be finalized is not guaranteed."
Quoted 11 lines
docs/devel/memory.rst +1
-0+first is not guaranteed. If however the memory region is part of a dynamically allocated data structure, you should call object_unparent() to destroy the memory regionsystem/memory.c +0
-0@@ -1796,16 +1796,36 @@ static void memory_region_finalize(Object *obj) {MemoryRegion *mr =MEMORY_REGION (obj);
system/memory.c static void memory_region_finalize(Object *obj) · line 1800
Quoted 10 lines
system/memory.c +2
-7-assert (!mr->container); - -/* We know the region is not visible in any address space (it -* does not have a container and cannot be a root either because -* it has no references, so we can blindly clear mr->enabled. -* memory_region_set_enabled instead could trigger a transaction -* and cause an infinite loop. +/* +* Each memory region (that can be dynamically freed..) must has an
s/..//
s/must has/must have/
system/memory.c static void memory_region_finalize(Object *obj) · line 1802
Quoted 2 lines
system/memory.c +2
-0+* owner, and it always has the same lifecycle of its owner. It means +* when reaching here, the memory region's owner refcount is zero.
"region's owner's refcount"
system/memory.c static void memory_region_finalize(Object *obj) · line 1806
Quoted 4 lines
system/memory.c +4
-0+* +* Here it is possible that the MR has: +* +* (1) mr->container set, which means this MR can be a subregion of a
"this MR is a subregion of"
system/memory.c static void memory_region_finalize(Object *obj) · line 1807
Quoted 1 line
system/memory.c +1
-0+* container MR, in this case it must share the same owner
s/, in/. In/
"same owner as that container"
system/memory.c static void memory_region_finalize(Object *obj) · line 1809
Quoted 2 lines
system/memory.c +2
-0+* (otherwise the container should have kept a refcount of this +* MR's owner), or,
s/, or,/
(it's possible for both 1 and 2 to be true for an MR here)
system/memory.c static void memory_region_finalize(Object *obj) · line 1811
Quoted 2 lines
system/memory.c +2
-0+* +* (2) mr->subregions non-empty, which means this MR can be a container
"is a container"
system/memory.c static void memory_region_finalize(Object *obj) · line 1812
Quoted 1 line
system/memory.c +1
-0+* of other MRs (share the owner or not).
"of another MR (which might have the same owner as this MR, or
a different owner)"
Quoted 45 lines
system/memory.c +22
-3+* +* We know the MR, or any MR that is attached to this one as either +* container or children, is not visible in any address space, because +* otherwise the address space should have taken at least one refcount +* of this MR's owner. So we can blindly clear mr->enabled. +* +* memory_region_set_enabled instead could trigger a transaction and +* cause an infinite loop. */ mr->enabled = false;memory_region_transaction_begin (); +if (mr->container) { +/* Must share the owner; see above comments */ +assert (mr->container->owner == mr->owner); +memory_region_del_subregion (mr->container, mr); + }while (!QTAILQ_EMPTY (&mr->subregions)) {MemoryRegion *subregion =QTAILQ_FIRST (&mr->subregions);memory_region_del_subregion (mr, subregion);@@ -2625,7 +2645,10 @@ static void memory_region_update_container_subregions(MemoryRegion *subregion)memory_region_transaction_begin (); -memory_region_ref (subregion); +if (mr->owner != subregion->owner) { +memory_region_ref (subregion); + } + QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {if (subregion->priority >= other->priority) {QTAILQ_INSERT_BEFORE (other, subregion, subregions_link);@@ -2683,7 +2706,11 @@ void memory_region_del_subregion(MemoryRegion *mr,assert (alias->mapped_via_alias >=0 ); }QTAILQ_REMOVE (&mr->subregions, subregion, subregions_link); -memory_region_unref (subregion); + +if (mr->owner != subregion->owner) { +memory_region_unref (subregion); + } + memory_region_update_pending |= mr->enabled && subregion->enabled;memory_region_transaction_commit (); } --
Quoted 1 line
2.50.1
thanks
-- PMM
Quoted 120 lines · Peter Maydell
Quoted 36 lines · Peter Xu
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'sfinalize().Delete subregions within object's
finalize()won't work either, becausefinalize()will be invoked only if the refcount goes to zero first. What is worse,object_finalize()will doobject_property_del_all()first beforeobject_deinit(). Since embeded MRs will be properties of the owner object, it means they'll be freed _before_ the owner'sfinalize().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 allowfinalize()to happen in any order of either the parent or child MR. Loose themr->containercheck in MR'sfinalize()to allow auto-detach. Double check it shares the same owner.Quoted 7 lines
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
Signed-off-by Peter Xu <[email protected]>I have some wordsmithing review stuff for doc and comment text below, but otherwise
Reviewed-by Peter Maydell <[email protected]>Tested-by Peter Maydell <[email protected]>Quoted 10 lines
--- docs/devel/memory.rst | 7 +++++-- system/memory.c | 45 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 11 deletions(-)docs/devel/memory.rst +0
-0docs/devel/memory.rst ioeventfd) can be changed during the region lifecycle. They take effect · line 164
Quoted 9 lines
docs/devel/memory.rst +4
-2@@ -158,8 +158,11 @@ ioeventfd) can be changed during the region lifecycle. They take effect as soon as the region is made visible. This can be immediately, later, or never. -Destruction of a memory region happens automatically when the owner -object dies. +Destruction of a memory region happens automatically when the owner object +dies. When there are multiple memory regions under the same owner object, +the memory API will guarantee all memory regions will be properly detached +and finalized one by one. The order which memory region will be finalized"The order in which memory regions will be finalized is not guaranteed."
Quoted 11 lines
docs/devel/memory.rst +1
-0+first is not guaranteed. If however the memory region is part of a dynamically allocated data structure, you should call object_unparent() to destroy the memory regionsystem/memory.c +0
-0@@ -1796,16 +1796,36 @@ static void memory_region_finalize(Object *obj) {MemoryRegion *mr =MEMORY_REGION (obj);system/memory.c static void memory_region_finalize(Object *obj) · line 1800
Quoted 10 lines
system/memory.c +2
-7-assert (!mr->container); - -/* We know the region is not visible in any address space (it -* does not have a container and cannot be a root either because -* it has no references, so we can blindly clear mr->enabled. -* memory_region_set_enabled instead could trigger a transaction -* and cause an infinite loop. +/* +* Each memory region (that can be dynamically freed..) must has an s/..//
s/must has/must have/system/memory.c static void memory_region_finalize(Object *obj) · line 1802
Quoted 2 lines
system/memory.c +2
-0+* owner, and it always has the same lifecycle of its owner. It means +* when reaching here, the memory region's owner refcount is zero. "region's owner's refcount"
system/memory.c static void memory_region_finalize(Object *obj) · line 1806
Quoted 4 lines
system/memory.c +4
-0+* +* Here it is possible that the MR has: +* +* (1) mr->container set, which means this MR can be a subregion of a "this MR is a subregion of"
system/memory.c static void memory_region_finalize(Object *obj) · line 1807
Quoted 1 line
system/memory.c +1
-0+* container MR, in this case it must share the same owner s/, in/. In/
"same owner as that container"
system/memory.c static void memory_region_finalize(Object *obj) · line 1809
Quoted 2 lines
system/memory.c +2
-0+* (otherwise the container should have kept a refcount of this +* MR's owner), or,
Quoted 8 lines
s/, or,/
(it's possible for both 1 and 2 to be true for an MR here)
system/memory.c static void memory_region_finalize(Object *obj) · line 1811
Quoted 2 lines
system/memory.c +2
-0+* +* (2) mr->subregions non-empty, which means this MR can be a container "is a container"
I'll fix all things until here (including Clement's suggestions in the other email).
Quoted 5 lines
system/memory.c static void memory_region_finalize(Object *obj) · line 1812
Quoted 2 lines
system/memory.c +1
-0+* of other MRs (share the owner or not). "of another MR (which might have the same owner as this MR, or
a different owner)"
IIUC there can be one, or more than one MRs as children. The finalize() of this MR should release the rest refcounts for all of them if applicable.
Latest version of this paragraph looks like this now:
* (2) mr->subregions non-empty, which means this MR is a container of
* one or more other MRs (which might have the the owner as this
* MR, or a different owner).
If above looks good I'll go ahead and merge this patch with the fixups.
Thanks a lot to both for the careful reviews (and tests).
Signature · 1 line
Peter Xu