memory: New AS helper to serialize destroy+free

If an AddressSpace has been created in its own allocated
memory, cleaning it up requires first destroying the AS
and then freeing the memory. Doing this doesn't work:

    address_space_destroy(as);
    g_free_rcu(as, rcu);

because both address_space_destroy() and g_free_rcu()
try to use the same 'rcu' node in the AddressSpace struct
and the address_space_destroy hook gets overwritten.

Provide a new address_space_destroy_free() function which
will destroy the AS and then free the memory it uses, all
in one RCU callback.

(CC to stable because the next commit needs this function.)

Cc: qemu-stable@nongnu.org
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: David Hildenbrand <david@redhat.com>
Link: https://lore.kernel.org/r/20250929144228.1994037-3-peter.maydell@linaro.org
Signed-off-by: Peter Xu <peterx@redhat.com>
This commit is contained in:
Peter Xu
2025-10-03 09:48:02 -04:00
parent 9e7bfda490
commit 041600e23f
2 changed files with 32 additions and 1 deletions
+13
View File
@@ -2735,11 +2735,24 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
* Note that destruction of the AddressSpace is done via RCU;
* it is therefore not valid to free the memory the AddressSpace
* struct is in until after that RCU callback has completed.
* If you want to g_free() the AddressSpace after destruction you
* can do that with address_space_destroy_free().
*
* @as: address space to be destroyed
*/
void address_space_destroy(AddressSpace *as);
/**
* address_space_destroy_free: destroy an address space and free it
*
* This does the same thing as address_space_destroy(), and then also
* frees (via g_free()) the AddressSpace itself once the destruction
* is complete.
*
* @as: address space to be destroyed
*/
void address_space_destroy_free(AddressSpace *as);
/**
* address_space_remove_listeners: unregister all listeners of an address space
*
+19 -1
View File
@@ -3278,7 +3278,14 @@ static void do_address_space_destroy(AddressSpace *as)
memory_region_unref(as->root);
}
void address_space_destroy(AddressSpace *as)
static void do_address_space_destroy_free(AddressSpace *as)
{
do_address_space_destroy(as);
g_free(as);
}
/* Detach address space from global view, notify all listeners */
static void address_space_detach(AddressSpace *as)
{
MemoryRegion *root = as->root;
@@ -3293,9 +3300,20 @@ void address_space_destroy(AddressSpace *as)
* values to expire before freeing the data.
*/
as->root = root;
}
void address_space_destroy(AddressSpace *as)
{
address_space_detach(as);
call_rcu(as, do_address_space_destroy, rcu);
}
void address_space_destroy_free(AddressSpace *as)
{
address_space_detach(as);
call_rcu(as, do_address_space_destroy_free, rcu);
}
static const char *memory_region_type(MemoryRegion *mr)
{
if (mr->alias) {