memory: allow destroying a non-empty MemoryRegion

This is legal; the MemoryRegion will simply unreference all the
existing subregions and possibly bring them down with it as well.
However, it requires a bit of care to avoid an infinite loop.
Finalizing a memory region cannot trigger an address space update,
but memory_region_del_subregion errs on the side of caution and
might trigger a spurious update: avoid that by resetting mr->enabled
first.

Backports commit 91232d98da2bfe042d4c5744076b488880de3040 from qemu
This commit is contained in:
Paolo Bonzini 2018-02-14 19:33:01 -05:00 committed by Lioncash
parent dba4828444
commit 208deb0387
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -1215,8 +1215,22 @@ static void memory_region_finalize(struct uc_struct *uc, Object *obj, void *opaq
{
MemoryRegion *mr = MEMORY_REGION(uc, obj);
assert(QTAILQ_EMPTY(&mr->subregions));
// assert(memory_region_transaction_depth == 0);
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.
*/
mr->enabled = false;
memory_region_transaction_begin(uc);
while (!QTAILQ_EMPTY(&mr->subregions)) {
MemoryRegion *subregion = QTAILQ_FIRST(&mr->subregions);
memory_region_del_subregion(mr, subregion);
}
memory_region_transaction_commit(uc);
mr->destructor(mr);
g_free((char *)mr->name);
}