qemu-cr16/hw
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
..
9pfs 9pfs: Stop including gstrfuncs.h 2025-09-18 21:21:29 +02:00
acpi acpi: mark PMTIMER as unlocked 2025-08-29 12:48:14 +02:00
adc qom: Have class_init() take a const data argument 2025-04-25 17:00:41 +02:00
alpha qom: Have class_init() take a const data argument 2025-04-25 17:00:41 +02:00
arm hw/arm/aspeed_ast27x0-fc: Make sub-init functions return bool with errp 2025-09-29 18:00:20 +02:00
audio hw/audio/via-ac97: skip automatic zero-init of large array 2025-06-12 13:40:15 -04:00
avr qom: Have class_init() take a const data argument 2025-04-25 17:00:41 +02:00
block hw/virtio: Build various files once 2025-07-15 02:56:39 -04:00
char rust: pl011: add tracepoints 2025-10-01 11:22:07 -04:00
core physmem: Destroy all CPU AddressSpaces on unrealize 2025-10-03 09:48:02 -04:00
cpu qom: Have class_init() take a const data argument 2025-04-25 17:00:41 +02:00
cxl hw/cxl: Convert cxl_fmws_link() to Error 2025-09-30 14:43:53 +02:00
display migration: Remove error variant of vmstate_save_state() function 2025-10-03 09:48:02 -04:00
dma hw/dma/xlnx_csu_dma: skip automatic zero-init of large array 2025-06-12 13:40:15 -04:00
fsi qom: Have class_init() take a const data argument 2025-04-25 17:00:41 +02:00
gpio hw/gpio/pca9554: Avoid leak in pca9554_set_pin() 2025-09-02 17:57:05 +02:00
hppa qom: Make InterfaceInfo[] uses const 2025-04-25 17:00:41 +02:00
hyperv hv-balloon: hw/core/register: Do not unparent in instance_finalize() 2025-09-24 09:23:20 +02:00
i2c hw/arm: Replace TABs for spaces in OMAP board and device code 2025-05-14 14:29:47 +01:00
i386 hw/i386/pc_piix.c: remove unnecessary if() from pc_init1() 2025-09-02 17:58:05 +02:00
ide hw/ide/ich.c: Use qemu_init_irq_child() to avoid memory leak 2025-09-02 17:57:05 +02:00
input treewide: use qemu_set_blocking instead of g_unix_set_fd_nonblocking 2025-09-19 12:46:07 +01:00
intc migration: ensure APIC is loaded prior to VFIO PCI devices 2025-10-03 09:48:02 -04:00
ipack qom: Make InterfaceInfo[] uses const 2025-04-25 17:00:41 +02:00
ipmi qom: Make InterfaceInfo[] uses const 2025-04-25 17:00:41 +02:00
isa hw/isa/superio: Include 'system/system.h' 2025-09-02 17:58:05 +02:00
loongarch hw/loongarch: Implement DINTC plug/unplug interfaces 2025-09-28 17:31:04 +08:00
m68k hw: add compat machines for 10.2 2025-08-27 07:07:53 +02:00
mem hw/cxl: mailbox-utils: 0x5604 - FMAPI Initiate DC Add 2025-07-15 02:56:40 -04:00
microblaze hw/microblaze: Add missing FDT dependency 2025-07-15 00:24:26 +02:00
mips hw/mips/malta: Silence warning from ubsan 2025-09-09 09:34:45 +02:00
misc Error reporting patches for 2025-09-30 2025-10-01 15:02:18 -07:00
net error: Kill @error_warn 2025-10-01 08:33:24 +02:00
nubus qom: Have class_init() take a const data argument 2025-04-25 17:00:41 +02:00
nvme hw/nvme: cap MDTS value for internal limitation 2025-08-11 00:17:38 -07:00
nvram hw/nvram/aspeed_otp: Add OTP programming semantics and tracing 2025-09-29 18:00:20 +02:00
openrisc add cpu_test_interrupt()/cpu_set_interrupt() helpers and use them tree wide 2025-08-29 12:48:14 +02:00
pci migration: Remove error variant of vmstate_save_state() function 2025-10-03 09:48:02 -04:00
pci-bridge hw/arm/smmu-common: Check SMMU has PCIe Root Complex association 2025-09-16 17:31:54 +01:00
pci-host hw/pci-host/aspeed: Disable Root Device and place Root Port at 00:00.0 to AST2700 2025-09-29 18:00:20 +02:00
ppc ppc/spapr: init lrdr-capapcity phys with ram size if maxmem not provided 2025-09-28 23:45:28 +05:30
remote hw/remote/vfio-user: Clean up error reporting 2025-09-30 14:43:53 +02:00
riscv hw/riscv/virt-acpi-build.c: Update FADT and MADT versions 2025-07-30 10:59:26 +10:00
rtc hw/rtc/mc146818rtc: Drop pre-v3 migration stream support 2025-04-30 20:44:20 +02:00
rx qom: Have class_init() take a const data argument 2025-04-25 17:00:41 +02:00
s390x migration: Remove error variant of vmstate_save_state() function 2025-10-03 09:48:02 -04:00
scsi migration: Remove error variant of vmstate_save_state() function 2025-10-03 09:48:02 -04:00
sd hw/sd/sdhci: Do not unparent in instance_finalize() 2025-09-24 09:23:20 +02:00
sensor qemu: Declare all load/store helper in 'qemu/bswap.h' 2025-07-15 02:56:39 -04:00
sh4 include: Remove 'exec/exec-all.h' 2025-04-30 12:45:05 -07:00
smbios qemu: Declare all load/store helper in 'qemu/bswap.h' 2025-07-15 02:56:39 -04:00
sparc qom: Have class_init() take a const data argument 2025-04-25 17:00:41 +02:00
sparc64 qom: Make InterfaceInfo[] uses const 2025-04-25 17:00:41 +02:00
ssi hw/ssi/aspeed_smc: Fix incorrect FMC_WDT2 register read on AST1030 2025-08-04 09:07:38 +02:00
timer hpet: guard IRQ handling with BQL 2025-09-17 19:00:58 +02:00
tpm qom: Make InterfaceInfo[] uses const 2025-04-25 17:00:41 +02:00
tricore qom: Have class_init() take a const data argument 2025-04-25 17:00:41 +02:00
uefi hw/uefi: open json file in binary mode 2025-08-12 08:03:16 +02:00
ufs hw/ufs/lu: skip automatic zero-init of large array 2025-06-12 13:40:16 -04:00
usb hw/usb/hcd-uhci: don't assert for SETUP to non-0 endpoint 2025-09-25 11:06:27 +01:00
vfio migration: Remove error variant of vmstate_save_state() function 2025-10-03 09:48:02 -04:00
vfio-user vfio-user/pci.c: rename vfio_user_pci_dev_info to vfio_user_pci_info 2025-09-25 17:55:20 +02:00
virtio migration: Remove error variant of vmstate_save_state() function 2025-10-03 09:48:02 -04:00
vmapple qemu: Declare all load/store helper in 'qemu/bswap.h' 2025-07-15 02:56:39 -04:00
watchdog qom: Make InterfaceInfo[] uses const 2025-04-25 17:00:41 +02:00
xen hw/xen: Do not unparent in instance_finalize() 2025-09-24 09:23:20 +02:00
xenpv hw/boards: Do not create unusable default if=sd drives 2025-02-16 14:25:08 +01:00
xtensa qom: Have class_init() take a const data argument 2025-04-25 17:00:41 +02:00
Kconfig vfio-user: add vfio-user class and container 2025-06-26 08:55:38 +02:00
meson.build hw/meson: enter target hw first 2025-09-02 17:57:05 +02:00