s390x/pci: Free FMB timer on zPCI device unplug
PATCH v3 1/2 s390x/pci: Free FMB timer on zPCI device unplug
On an SCLP deconfigure we don't clean up the FMB timer. If there is a pending unplug request then we can destroy the S390PCIBusDevice object without correctly cleaning up the timer. So fmb_update() will then run on freed memory. Clean up the FMB timer before we destroy the S390PCIBusDevice object to prevent this use after free.
When FMB was introduced with commit 6e92c70c37 ("s390x/pci: add common function measurement block") we originally cleaned up the FMB timer in s390_pcihost_unplug(). This behavior was changed in the re-work done with commit e0998fe89104 ("s390x/pci: Introduce unplug requests and split unplug handler").
--- hw/s390x/s390-pci-bus.c | 1 + 1 file changed, 1 insertion(+)
hw/s390x/s390-pci-bus.c +1 -0
@@ -1400,6 +1400,7 @@ static void s390_pcihost_unplug(const HotplugHandler *hotplug_dev, DeviceState * }else if (object_dynamic_cast (OBJECT (dev),TYPE_S390_PCI_DEVICE )) { pbdev =S390_PCI_DEVICE (dev); pbdev->fid =0 ; +fmb_timer_free (pbdev);QTAILQ_REMOVE (&s->zpci_devs, pbdev, link);g_hash_table_remove (s->zpci_table, &pbdev->idx);if (pbdev->dma_limit) {
Signature · 1 line
2.43.0
PATCH v3 2/2 s390x/pci: Change reset method from legacy to resettable exit
On subsystem reset of the s390 PCI host bridge (TYPE_S390_PCI_HOST_BRIDGE), child buses S390PCIBus and PCIBus are reset in order. Because S390PCIBus devices are reset first via the legacy reset hook, IOMMU mappings are torn down before the PCI device itself is reset. For a PCI passthrough device this opens a window where the device may issue DMA to addresses that no longer have valid IOMMU mappings, causing an error (PEC 0x7) on the host.
The QEMU reset documentation (docs/devel/reset.rst) specifies that IOMMUs should perform teardown in the resettable 'exit' phase, after all devices have completed their 'hold' phase. Move the S390PCIBusDevice reset logic from the legacy reset hook to the resettable exit phase to close this window.
--- hw/s390x/s390-pci-bus.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
hw/s390x/s390-pci-bus.c +4 -3
@@ -1786,9 +1786,9 @@ static void s390_pci_device_realize(DeviceState *dev, Error **errp) zpci->fmb.format =ZPCI_FMB_FORMAT ; } -static void s390_pci_device_reset (DeviceState *dev) +static void s390_pci_device_reset_exit (Object *obj,ResetType type) { -S390PCIBusDevice *pbdev =S390_PCI_DEVICE (dev); +S390PCIBusDevice *pbdev =S390_PCI_DEVICE (obj);switch (pbdev->state) {case ZPCI_FS_RESERVED :
@@ -2019,10 +2019,11 @@ static const VMStateDescription s390_pci_device_vmstate = {static void s390_pci_device_class_init (ObjectClass *klass,const void *data) {DeviceClass *dc =DEVICE_CLASS (klass); +ResettableClass *rc =RESETTABLE_CLASS (klass); dc->desc ="zpci device" ;set_bit (DEVICE_CATEGORY_MISC , dc->categories); -device_class_set_legacy_reset (dc, s390_pci_device_reset); + rc->phases.exit = s390_pci_device_reset_exit; dc->bus_type =TYPE_S390_PCI_BUS ; dc->realize = s390_pci_device_realize;device_class_set_props (dc, s390_pci_device_properties);
Signature · 1 line
2.43.0
Re: [PATCH v3 2/2] s390x/pci: Change reset method from legacy to resettable exit
Quoted 13 lines · Farhan Ali
On subsystem reset of the s390 PCI host bridge (
TYPE_S390_PCI_HOST_BRIDGE), child buses S390PCIBus and PCIBus are reset in order. Because S390PCIBus devices are reset first via the legacy reset hook, IOMMU mappings are torn down before the PCI device itself is reset. For a PCI passthrough device this opens a window where the device may issue DMA to addresses that no longer have valid IOMMU mappings, causing an error (PEC0x7) on the host.The QEMU reset documentation (
docs/devel/reset.rst) specifies that IOMMUs should perform teardown in the resettable 'exit' phase, after all devices have completed their 'hold' phase. Move the S390PCIBusDevice reset logic from the legacy reset hook to the resettable exit phase to close this window.
Quoted 5 lines
Fixes 3e5cfba3ca0a ("s390x/pci: introduce S390PCIBusDevice qdev")Reviewed-by Philippe Mathieu-Daudé <[email protected]>Tested-by Omar Elghoul <[email protected]>Signed-off-by Farhan Ali <[email protected]>
Eric Farman
AppliedRe: [PATCH v3 2/2] s390x/pci: Change reset method from legacy to resettable exit
Quoted 14 lines · Matthew Rosato
Quoted 13 lines · Farhan Ali
On subsystem reset of the s390 PCI host bridge (
TYPE_S390_PCI_HOST_BRIDGE), child buses S390PCIBus and PCIBus are reset in order. Because S390PCIBus devices are reset first via the legacy reset hook, IOMMU mappings are torn down before the PCI device itself is reset. For a PCI passthrough device this opens a window where the device may issue DMA to addresses that no longer have valid IOMMU mappings, causing an error (PEC0x7) on the host.The QEMU reset documentation (
docs/devel/reset.rst) specifies that IOMMUs should perform teardown in the resettable 'exit' phase, after all devices have completed their 'hold' phase. Move the S390PCIBusDevice reset logic from the legacy reset hook to the resettable exit phase to close this window.
Quoted 7 lines
Quoted 5 lines
Fixes 3e5cfba3ca0a ("s390x/pci: introduce S390PCIBusDevice qdev")Reviewed-by Philippe Mathieu-Daudé <[email protected]>Tested-by Omar Elghoul <[email protected]>Signed-off-by Farhan Ali <[email protected]>Reviewed-by Matthew Rosato <[email protected]>
Thanks! Applied both of these to the queue for 11.2.
Quoted 12 lines · Farhan Ali
On an SCLP deconfigure we don't clean up the FMB timer. If there is a pending unplug request then we can destroy the S390PCIBusDevice object without correctly cleaning up the timer. So
fmb_update()will then run on freed memory. Clean up the FMB timer before we destroy the S390PCIBusDevice object to prevent this use after free.When FMB was introduced with commit
6e92c70c37("s390x/pci: add common function measurement block") we originally cleaned up the FMB timer ins390_pcihost_unplug(). This behavior was changed in the re-work done with commite0998fe89104("s390x/pci: Introduce unplug requests and split unplug handler").
Quoted 3 lines
Fixes e0998fe89104 ("s390x/pci: Introduce unplug requests and split unplug handler")Signed-off-by Farhan Ali <[email protected]>