qemu/include/exec
Peter Maydell 9d061df678 physmem: Destroy all CPU AddressSpaces on unrealize
When we unrealize a CPU object (which happens on vCPU hot-unplug), we
should destroy all the AddressSpace objects we created via calls to
cpu_address_space_init() when the CPU was realized.

Commit 24bec42f3d added a function to do this for a specific
AddressSpace, but did not add any places where the function was
called.

Since we always want to destroy all the AddressSpaces on unrealize,
regardless of the target architecture, we don't need to try to keep
track of how many are still undestroyed, or make the target
architecture code manually call a destroy function for each AS it
created.  Instead we can adjust the function to always completely
destroy the whole cpu->ases array, and arrange for it to be called
during CPU unrealize as part of the common code.

Without this fix, AddressSanitizer will report a leak like this
from a run where we hot-plugged and then hot-unplugged an x86 KVM
vCPU:

Direct leak of 416 byte(s) in 1 object(s) allocated from:
    #0 0x5b638565053d in calloc (/data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/qemu-system-x86_64+0x1ee153d) (BuildId: c1cd6022b195142106e1bffeca23498c2b752bca)
    #1 0x7c28083f77b1 in g_malloc0 (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x637b1) (BuildId: 1eb6131419edb83b2178b682829a6913cf682d75)
    #2 0x5b6386999c7c in cpu_address_space_init /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../system/physmem.c:797:25
    #3 0x5b638727f049 in kvm_cpu_realizefn /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../target/i386/kvm/kvm-cpu.c:102:5
    #4 0x5b6385745f40 in accel_cpu_common_realize /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../accel/accel-common.c:101:13
    #5 0x5b638568fe3c in cpu_exec_realizefn /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../hw/core/cpu-common.c:232:10
    #6 0x5b63874a2cd5 in x86_cpu_realizefn /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../target/i386/cpu.c:9321:5
    #7 0x5b6387a0469a in device_set_realized /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../hw/core/qdev.c:494:13
    #8 0x5b6387a27d9e in property_set_bool /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../qom/object.c:2375:5
    #9 0x5b6387a2090b in object_property_set /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../qom/object.c:1450:5
    #10 0x5b6387a35b05 in object_property_set_qobject /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../qom/qom-qobject.c:28:10
    #11 0x5b6387a21739 in object_property_set_bool /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../qom/object.c:1520:15
    #12 0x5b63879fe510 in qdev_realize /data_nvme1n1/linaro/qemu-from-laptop/qemu/build/x86-tgts-asan/../../hw/core/qdev.c:276:12

Cc: qemu-stable@nongnu.org
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2517
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: David Hildenbrand <david@redhat.com>
Link: https://lore.kernel.org/r/20250929144228.1994037-4-peter.maydell@linaro.org
Signed-off-by: Peter Xu <peterx@redhat.com>
(cherry picked from commit 300a87c502)
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2025-10-05 09:39:24 +03:00
..
abi_ptr.h exec: Declare abi_ptr type in its own 'abi_ptr.h' header 2024-04-26 17:03:05 +02:00
address-spaces.h exec/address-spaces.h: Remove unuseful 'exec/memory.h' include 2023-08-31 19:47:43 +02:00
breakpoint.h exec: Declare CPUBreakpoint/CPUWatchpoint type in 'breakpoint.h' header 2024-04-26 17:03:05 +02:00
cpu-all.h exec/cpu-all: remove BSWAP_NEEDED 2025-03-21 12:53:36 +01:00
cpu-common.h physmem: Destroy all CPU AddressSpaces on unrealize 2025-10-05 09:39:24 +03:00
cpu-defs.h accel/tcg: Restrict CPU_TLB_DYN_*_BITS definitions to accel/tcg/ 2025-03-07 07:53:44 -08:00
cpu-interrupt.h include/exec: Split out exec/cpu-interrupt.h 2025-03-08 07:55:03 -08:00
cpu_ldst.h user: Introduce 'user/guest-host.h' header 2024-12-20 17:44:56 +01:00
cputlb.h exec: Declare tlb_flush*() in 'exec/cputlb.h' 2025-03-08 07:56:14 -08:00
exec-all.h accel/tcg: Split out getpc.h 2025-03-08 09:34:43 -08:00
gdbstub.h system: propagate Error to gdbserver_start (and other device setups) 2025-01-17 10:44:25 +00:00
helper-gen-common.h plugins: Remove plugin helpers 2024-04-30 16:12:05 -07:00
helper-gen.h tcg: Split helper-gen.h 2023-06-05 12:04:29 -07:00
helper-gen.h.inc tcg: Pass function pointer to tcg_gen_call* 2024-04-30 16:12:05 -07:00
helper-head.h.inc include/exec: Introduce fpst alias in helper-head.h.inc 2024-12-12 14:28:38 -06:00
helper-info.c.inc accel/tcg: Rename helper-head.h -> helper-head.h.inc 2024-04-26 15:31:37 +02:00
helper-proto-common.h plugins: Remove plugin helpers 2024-04-30 16:12:05 -07:00
helper-proto.h tcg: Split helper-proto.h 2023-06-05 12:04:29 -07:00
helper-proto.h.inc accel/tcg: Rename helper-head.h -> helper-head.h.inc 2024-04-26 15:31:37 +02:00
hwaddr.h bulk: Rename TARGET_FMT_plx -> HWADDR_FMT_plx 2023-01-18 11:14:34 +01:00
ioport.h exec/ioport: Add portio_list_set_enabled() 2024-02-14 06:09:32 -05:00
log.h include/exec/log: Do not reference QemuLogFile directly 2022-04-20 10:51:11 -07:00
memattrs.h physmem: teach cpu_memory_rw_debug() to write to more memory regions 2025-02-12 11:33:13 -05:00
memop.h include/exec/memop.h: Expand comment for MO_ATOM_SUBALIGN 2025-03-07 10:39:29 +00:00
memopidx.h Normalize header guard symbol definition 2022-05-11 16:50:26 +02:00
memory-internal.h exec/memory: Expose memory_region_access_valid() 2023-01-09 13:50:13 +01:00
memory.h memory: New AS helper to serialize destroy+free 2025-10-05 09:39:24 +03:00
memory_ldst.h.inc bulk: Do not declare function prototypes using 'extern' keyword 2023-08-31 19:47:43 +02:00
memory_ldst_cached.h.inc exec/memory_ldst_cached: Use correct type size 2021-05-26 08:35:51 -07:00
memory_ldst_phys.h.inc exec/memory_ldst_phys: Use correct type sizes 2021-05-26 08:35:51 -07:00
mmu-access-type.h exec: Declare MMUAccessType type in 'mmu-access-type.h' header 2024-04-26 17:03:05 +02:00
page-protection.h qemu/compiler: Absorb 'clang-tsa.h' 2025-03-06 14:21:25 +01:00
page-vary.h bulk: Do not declare function prototypes using 'extern' keyword 2023-08-31 19:47:43 +02:00
plugin-gen.h plugins: Read mem_only directly from TB cflags 2024-05-15 08:55:18 +02:00
poison.h exec/cpu-all: remove BSWAP_NEEDED 2025-03-21 12:53:36 +01:00
ram_addr.h system: Declare qemu_[min/max]rampagesize() in 'system/hostmem.h' 2025-03-11 17:01:14 +01:00
ramblock.h migration: ram block cpr blockers 2025-03-10 12:09:24 -03:00
ramlist.h softmmu/physmem: fix memory leak in dirty_memory_extend() 2024-09-09 10:55:39 -04:00
replay-core.h replay: Extract core API to 'exec/replay-core.h' 2023-02-27 22:29:01 +01:00
target_long.h tcg: Define MO_TL 2023-10-22 16:34:21 -07:00
target_page.h include/exec: Move TARGET_PAGE_{SIZE,MASK,BITS} to target_page.h 2025-03-07 13:02:54 -08:00
tb-flush.h accel/tcg: Declare tcg_flush_jmp_cache() in 'exec/tb-flush.h' 2023-11-07 12:13:27 +01:00
tlb-common.h include/exec: Use uintptr_t in CPUTLBEntry 2025-02-18 08:29:02 -08:00
translation-block.h accel/tcg: Move tcg_cflags_has/set() to 'exec/translation-block.h' 2024-12-20 17:44:57 +01:00
translator.h accel/tcg: Move gen_intermediate_code to TCGCPUOps.translate_core 2024-12-24 08:32:15 -08:00
tswap.h exec/tswap: Massage target_needs_bswap() definition 2024-10-15 11:55:09 -03:00
vaddr.h include/exec: Change vaddr to uintptr_t 2025-02-18 08:29:02 -08:00