qemu-cr16/include/exec
Peter Maydell 300a87c502 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>
2025-10-03 09:48:02 -04:00
..
abi_ptr.h exec: Declare abi_ptr type in its own 'abi_ptr.h' header 2024-04-26 17:03:05 +02:00
breakpoint.h exec: Declare CPUBreakpoint/CPUWatchpoint type in 'breakpoint.h' header 2024-04-26 17:03:05 +02:00
cpu-common.h physmem: Destroy all CPU AddressSpaces on unrealize 2025-10-03 09:48:02 -04:00
cpu-defs.h page-vary: Restrict scope of TARGET_PAGE_BITS_MIN 2025-04-23 15:04:57 -07:00
cpu-interrupt.h include/exec: Split out exec/cpu-interrupt.h 2025-03-08 07:55:03 -08:00
cputlb.h include/hw/core/cpu: Introduce MMUIdxMap 2025-09-23 16:55:23 -07:00
gdbstub.h monitor: Clean up HMP gdbserver error reporting 2025-09-30 14:43:52 +02: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 tcg: add vaddr type for helpers 2025-05-14 15:12:40 +01: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 accel/tcg: Include 'accel/tcg/getpc.h' in 'exec/helper-proto' 2025-04-30 12:45: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
icount.h include/exec: Protect icount_enabled from poisoned symbols 2025-04-23 14:08:44 -07:00
log.h include/exec/log: Do not reference QemuLogFile directly 2022-04-20 10:51:11 -07:00
memattrs.h pci: Add a memory attribute for pre-translated DMA operations 2025-07-15 02:56:39 -04:00
memop.h include/exec: fix assert in size_memop 2025-06-07 16:40:44 +01:00
memopidx.h Normalize header guard symbol definition 2022-05-11 16:50:26 +02:00
memory_ldst.h.inc exec/memory_ldst: extract memory_ldst declarations from cpu-all.h 2025-04-23 13:52:20 -07: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: extract memory_ldst_phys declarations from cpu-all.h 2025-04-23 13:52:20 -07:00
mmap-lock.h include/exec: Split out mmap-lock.h 2025-04-23 14:08:17 -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 include/exec: Split out mmap-lock.h 2025-04-23 14:08:17 -07:00
page-vary.h page-vary: Move and rename qemu_target_page_bits_min 2025-04-23 15:04:57 -07:00
plugin-gen.h plugins: Read mem_only directly from TB cflags 2024-05-15 08:55:18 +02:00
poison.h accel/tcg: Convert TARGET_HAS_PRECISE_SMC to TCGCPUOps.precise_smc 2025-04-30 12:45:05 -07: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/target_page.h: move page-target.c to header 2025-09-02 17:57:05 +02:00
tb-flush.h accel/tcg: Create queue_tb_flush from tb_flush 2025-09-24 10:29:43 -07:00
tlb-common.h include/exec: Use uintptr_t in CPUTLBEntry 2025-02-18 08:29:02 -08:00
tlb-flags.h include/exec: Redefine tlb-flags with absolute values 2025-04-23 15:04:57 -07:00
translation-block.h include/exec: Move tb_invalidate_phys_range to translation-block.h 2025-04-30 12:45:05 -07:00
translator.h accel/tcg: Implement translator_ld*_end 2025-04-23 14:08:17 -07:00
tswap.h qemu: Declare all load/store helper in 'qemu/bswap.h' 2025-07-15 02:56:39 -04:00
vaddr.h include/exec: Change vaddr to uintptr_t 2025-02-18 08:29:02 -08:00
watchpoint.h include/exec: Split out watchpoint.h 2025-04-23 14:08:36 -07:00