@@ -1855,6 +1855,24 @@ void memory_region_unref(MemoryRegion *mr)
}
}
+static void memory_region_ref_subregion(MemoryRegion *subregion)
+{
+ if (subregion->container->owner == subregion->owner) {
+ object_ref(OBJECT(subregion));
+ } else {
+ memory_region_ref(subregion);
+ }
+}
+
+static void memory_region_unref_subregion(MemoryRegion *subregion)
+{
+ if (subregion->container->owner == subregion->owner) {
+ object_unref(OBJECT(subregion));
+ } else {
+ memory_region_unref(subregion);
+ }
+}
+
uint64_t memory_region_size(MemoryRegion *mr)
{
if (int128_eq(mr->size, int128_2_64())) {
@@ -2644,7 +2662,8 @@ static void memory_region_update_container_subregions(MemoryRegion *subregion)
memory_region_transaction_begin();
- memory_region_ref(subregion);
+ memory_region_ref_subregion(subregion);
+
QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
if (subregion->priority >= other->priority) {
QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
@@ -2702,7 +2721,8 @@ void memory_region_del_subregion(MemoryRegion *mr,
assert(alias->mapped_via_alias >= 0);
}
QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
- memory_region_unref(subregion);
+ memory_region_unref_subregion(subregion);
+
memory_region_update_pending |= mr->enabled && subregion->enabled;
memory_region_transaction_commit();
}