tpm_tis_sysbus: defer resource allocation to realizefn
PATCH v2 1/1 tpm_tis_sysbus: defer resource allocation to realizefn
Calling memory_region_init_ram_device_ptr() and memory_region_init_io() from tpm_tis_sysbus_initfn() crashes when the device is introspected without being realized, because the memory subsystem has not been initialized at that point.
So running:
$ qemu-system-aarch64 -device tpm-tis-device,help
triggers qdev_device_help() which creates the device object to list its properties, calling instance_init, but never realizefn. The memory region calls in instance_init then hit uninitialized subsystems:
With CONFIG_DEBUG_TCG:
Assertion 'target_page.decided' failed. (physmem.c:2524)
Without CONFIG_DEBUG_TCG:
Assertion 'mutex->initialized' failed. (qemu-thread-posix.c:107)
Since realizefn is only called when the device is actually used in a running VM, moving resource allocation there avoids the crash without breaking introspection.
---
Changes in v2:
- Also move memory_region_init_io() to realizefn (Philippe)
- Make host_page_size const (Philippe)
- Reword commit message to cover both crash variants
hw/tpm/tpm_tis_sysbus.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
hw/tpm/tpm_tis_sysbus.c +8 -10
@@ -100,19 +100,9 @@ static void tpm_tis_sysbus_initfn(Object *obj) {TPMStateSysBus *sbdev =TPM_TIS_SYSBUS (obj);TPMState *s = &sbdev->state; -size_t host_page_size =qemu_real_host_page_size (); - -memory_region_init_io (&s->mmio, obj, &tpm_tis_memory_ops, - s,"tpm-tis-mmio" , -TPM_TIS_NUM_LOCALITIES <<TPM_TIS_LOCALITY_SHIFT );sysbus_init_mmio (SYS_BUS_DEVICE (obj), &s->mmio);sysbus_init_irq (SYS_BUS_DEVICE (obj), &s->irq); - - s->ppi.buf =qemu_memalign (host_page_size, -ROUND_UP (TPM_PPI_ADDR_SIZE , host_page_size)); -memory_region_init_ram_device_ptr (&s->ppi.ram, obj,"tpm-ppi" , -TPM_PPI_ADDR_SIZE , s->ppi.buf);sysbus_init_mmio (SYS_BUS_DEVICE (obj), &s->ppi.ram); }
@@ -120,6 +110,7 @@ static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp) {TPMStateSysBus *sbdev =TPM_TIS_SYSBUS (dev);TPMState *s = &sbdev->state; +const size_t host_page_size =qemu_real_host_page_size ();if (!tpm_find ()) {error_setg (errp,"at most one TPM device is permitted" );
@@ -131,6 +122,13 @@ static void tpm_tis_sysbus_realizefn(DeviceState *dev, Error **errp)return ; } + s->ppi.buf =qemu_memalign (host_page_size, +ROUND_UP (TPM_PPI_ADDR_SIZE , host_page_size)); +memory_region_init_io (&s->mmio,OBJECT (dev), &tpm_tis_memory_ops, + s,"tpm-tis-mmio" , +TPM_TIS_NUM_LOCALITIES <<TPM_TIS_LOCALITY_SHIFT ); +memory_region_init_ram_device_ptr (&s->ppi.ram,OBJECT (dev),"tpm-ppi" , +TPM_PPI_ADDR_SIZE , s->ppi.buf);vmstate_register_ram (&s->ppi.ram, dev); }
Signature · 1 line
2.53.0
Quoted 25 lines · Mohammadfaiz Bawa
Calling
memory_region_init_ram_device_ptr()andmemory_region_init_io()fromtpm_tis_sysbus_initfn()crashes when the device is introspected without being realized, because the memory subsystem has not been initialized at that point.So running:
$ qemu-system-aarch64 -device tpm-tis-device,helptriggers
qdev_device_help()which creates the device object to list its properties, callinginstance_init, but never realizefn. The memory region calls ininstance_initthen hit uninitialized subsystems:With
CONFIG_DEBUG_TCG:
Assertion 'target_page.decided' failed. (physmem.c:2524)Without
CONFIG_DEBUG_TCG:Assertion 'mutex->initialized' failed. (qemu-thread-posix.c:107)Since realizefn is only called when the device is actually used in a running VM, moving resource allocation there avoids the crash without breaking introspection.
Signed-off-by Mohammadfaiz Bawa <[email protected]>
Quoted 8 lines
---
Changes in v2:
- Also movememory_region_init_io()to realizefn (Philippe)
- Makehost_page_sizeconst (Philippe)
- Reword commit message to cover both crash variantshw/tpm/tpm_tis_sysbus.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
Quoted 20 lines · Mohammadfaiz Bawa
Calling
memory_region_init_ram_device_ptr()andmemory_region_init_io()fromtpm_tis_sysbus_initfn()crashes when the device is introspected without being realized, because the memory subsystem has not been initialized at that point.So running:
$ qemu-system-aarch64 -device tpm-tis-device,helptriggers
qdev_device_help()which creates the device object to list its properties, callinginstance_init, but never realizefn. The memory region calls ininstance_initthen hit uninitialized subsystems:With
CONFIG_DEBUG_TCG:
Assertion 'target_page.decided' failed. (physmem.c:2524)Without
CONFIG_DEBUG_TCG:
Assertion 'mutex->initialized' failed. (qemu-thread-posix.c:107)
Quoted 6 lines
Since realizefn is only called when the device is actually used in a running VM, moving resource allocation there avoids the crash without breaking introspection.
Signed-off-by Mohammadfaiz Bawa <[email protected]>
This also fixes a memory leak that is reported by the address sanitizer during 'make check', because we currently allocate ppi.buf during instance_init and never free it. "Allocate in realize and never free" is less bad, because we don't currently support "unrealize and destroy a sysbus device".
thanks
-- PMM
Quoted 28 lines · Peter Maydell
Quoted 18 lines · Mohammadfaiz Bawa
Calling
memory_region_init_ram_device_ptr()andmemory_region_init_io()fromtpm_tis_sysbus_initfn()crashes when the device is introspected without being realized, because the memory subsystem has not been initialized at that point.So running:
$ qemu-system-aarch64 -device tpm-tis-device,helptriggers
qdev_device_help()which creates the device object to list its properties, callinginstance_init, but never realizefn. The memory region calls ininstance_initthen hit uninitialized subsystems:With
CONFIG_DEBUG_TCG:
Assertion 'target_page.decided' failed. (physmem.c:2524)Without
CONFIG_DEBUG_TCG:Quoted 8 lines
Assertion 'mutex->initialized' failed. (qemu-thread-posix.c:107)Since realizefn is only called when the device is actually used in a running VM, moving resource allocation there avoids the crash without breaking introspection.
Signed-off-by Mohammadfaiz Bawa <[email protected]>
Quoted 7 lines
This also fixes a memory leak that is reported by the address sanitizer during 'make check', because we currently allocate ppi.buf during
instance_initand never free it. "Allocate in realize and never free" is less bad, because we don't currently support "unrealize and destroy a sysbus device".Fixes 46cd2c1050f ("hw/tpm: add PPI support to tpm-tis-device for ARM64 virt")
Commit updated, thanks!