Fix vIOMMU reset order
v2 cover Fix vIOMMU reset order
With current reset scheme, DMA capable devices are reset before the vIOMMU which translate them. This holds for the different IOMMUs and various DMA capable devices such as virtio devices and VFIO ones. With virtio devices spurious traces can be observed at qemu level such as "virtio: zero sized buffers are not allowed" while for VFIO devices, translation faults can be observed at host level.
Virtio devices use 3 phase reset and virtio-pci devices are reset in the 'hold' phase. VFIO device reset are registered using qemu_register_reset() and as a consequence they are also reset on 'hold' phase.
Note that the tree of QOM devices resets depth-first but it does so while enforcing the 3 phases. First the tree is traversed doing the 'enter' phase, then the 'hold' phase and eventually the 'exit' phase.
However the QOM hierarchy is not built so that vIOMMUs get reset before the DMA capable devices (IOMMUs are using either legacy reset scheme or hold phase). Changing the QOM hierarchy does not sound trivial while forcing the vIOMMUs to be reset on 'exit' phase sounds reasonable and much simpler. Obviously this relies on the assumption that all DMA capable devices quiesce their DMA before (ie. during 'enter' or hold' phase).
This was tested with qmp system_reset and virsh reset.
Best Regards
Eric
This series can be found at:
https://github.com/eauger/qemu/tree/viommu-3phase-reset-v2
History:
v1 -> v2:
- Removed hw/i386/intel_iommu: Tear down address spaces before IOMMU reset
- Also move SMMU base class reset to exit reset. This was an oversight from v1
- Add last patch documenting expectations in terms of DMA reset
- Improved commit messages and cover letter
- dared to keep Michael's A-b for the patches whose code was not altered
References:
[1] [PATCH 0/4] intel_iommu: Reset vIOMMU after all the rest of devices
https://lore.kernel.org/all/[email protected]/
Eric Auger (5):
hw/virtio/virtio-iommu: Migrate to 3-phase reset hw/i386/intel-iommu: Migrate to 3-phase reset hw/arm/smmuv3: Move reset to exit phase hw/vfio/common: Add a trace point in vfio_reset_handler docs/devel/reset: Document reset expectations for DMA and IOMMU
docs/devel/reset.rst | 5 +++++ hw/arm/smmu-common.c | 9 +++++++-- hw/arm/smmuv3.c | 14 ++++++++++---- hw/i386/intel_iommu.c | 12 +++++++++--- hw/vfio/common.c | 1 + hw/virtio/virtio-iommu.c | 14 ++++++++++---- hw/arm/trace-events | 1 + hw/i386/trace-events | 1 + hw/vfio/trace-events | 1 + hw/virtio/trace-events | 2 +- 10 files changed, 46 insertions(+), 14 deletions(-)
Signature · 1 line
2.47.1
PATCH v2 1/5 hw/virtio/virtio-iommu: Migrate to 3-phase reset
Currently the iommu may be reset before the devices it protects. For example this happens with virtio-net.
Let's use 3-phase reset mechanism and reset the IOMMU on exit phase after all DMA capable devices have been reset during the 'enter' or 'hold' phase.
---
v2 -> v3:
- add a comment emphasizing the fact that the exit reset needs to be used
--- hw/virtio/virtio-iommu.c | 14 ++++++++++---- hw/virtio/trace-events | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-)
hw/virtio/virtio-iommu.c +10 -4
@@ -1504,11 +1504,11 @@ static void virtio_iommu_device_unrealize(DeviceState *dev)virtio_cleanup (vdev); } -static void virtio_iommu_device_reset (VirtIODevice *vdev) +static void virtio_iommu_device_reset_exit (Object *obj,ResetType type) { -VirtIOIOMMU *s =VIRTIO_IOMMU (vdev); +VirtIOIOMMU *s =VIRTIO_IOMMU (obj); -trace_virtio_iommu_device_reset (); +trace_virtio_iommu_device_reset_exit ();if (s->domains) {g_tree_destroy (s->domains);
@@ -1668,6 +1668,7 @@ static void virtio_iommu_class_init(ObjectClass *klass, void *data) {DeviceClass *dc =DEVICE_CLASS (klass);VirtioDeviceClass *vdc =VIRTIO_DEVICE_CLASS (klass); +ResettableClass *rc =RESETTABLE_CLASS (klass);device_class_set_props (dc, virtio_iommu_properties); dc->vmsd = &vmstate_virtio_iommu;
@@ -1675,7 +1676,12 @@ static void virtio_iommu_class_init(ObjectClass *klass, void *data)set_bit (DEVICE_CATEGORY_MISC , dc->categories); vdc->realize = virtio_iommu_device_realize; vdc->unrealize = virtio_iommu_device_unrealize; - vdc->reset = virtio_iommu_device_reset; + +/* +* Use 'exit' reset phase to make sure all DMA requests +* have been quiesced during 'enter' or 'hold' phase +*/ + rc->phases.exit = virtio_iommu_device_reset_exit; vdc->get_config = virtio_iommu_get_config; vdc->set_config = virtio_iommu_set_config; vdc->get_features = virtio_iommu_get_features;
hw/virtio/trace-events +1 -1
@@ -108,7 +108,7 @@ virtio_pci_notify_write(uint64_t addr, uint64_t val, unsigned int size) "0x%" PR virtio_pci_notify_write_pio(uint64_t addr, uint64_t val, unsigned int size) "0x%" PRIx64" = 0x%" PRIx64 " (%d)" # hw/virtio/virtio-iommu.c -virtio_iommu_device_reset(void) "reset!" +virtio_iommu_device_reset_exit(void) "reset!" virtio_iommu_system_reset(void) "system reset!" virtio_iommu_get_features(uint64_t features) "device supports features=0x%"PRIx64 virtio_iommu_device_status(uint8_t status) "driver status = %d"
Signature · 1 line
2.47.1
Re: [PATCH v2 1/5] hw/virtio/virtio-iommu: Migrate to 3-phase reset
Quoted 11 lines · Eric Auger
Currently the iommu may be reset before the devices it protects. For example this happens with virtio-net.
Let's use 3-phase reset mechanism and reset the IOMMU on exit phase after all DMA capable devices have been reset during the 'enter' or 'hold' phase.
Signed-off-by Eric Auger <[email protected]>
Thanks
PATCH v2 2/5 hw/i386/intel-iommu: Migrate to 3-phase reset
In reply to Eric AugerCurrently the IOMMU may be reset before the devices it protects. For example this happens with virtio devices but also with VFIO devices. In this latter case this produces spurious translation faults on host.
Let's use 3-phase reset mechanism and reset the IOMMU on exit phase after all DMA capable devices have been reset on 'enter' or 'hold' phase.
---
v1 -> v2:
- add a comment emphasizing the importance of using an 'exit reset
--- hw/i386/intel_iommu.c | 12 +++++++++--- hw/i386/trace-events | 1 + 2 files changed, 10 insertions(+), 3 deletions(-)
hw/i386/intel_iommu.c +9 -3
@@ -4697,10 +4697,11 @@ static void vtd_init(IntelIOMMUState *s)/* Should not reset address_spaces when reset because devices will still use * the address space they got at first (won't ask the bus again). */ -static void vtd_reset (DeviceState *dev) +static void vtd_reset_exit (Object *obj,ResetType type) { -IntelIOMMUState *s =INTEL_IOMMU_DEVICE (dev); +IntelIOMMUState *s =INTEL_IOMMU_DEVICE (obj); +trace_vtd_reset_exit ();vtd_init (s);vtd_address_space_refresh_all (s); }
@@ -4864,8 +4865,13 @@ static void vtd_class_init(ObjectClass *klass, void *data) {DeviceClass *dc =DEVICE_CLASS (klass);X86IOMMUClass *x86_class =X86_IOMMU_DEVICE_CLASS (klass); +ResettableClass *rc =RESETTABLE_CLASS (klass); -device_class_set_legacy_reset (dc, vtd_reset); +/* +* Use 'exit' reset phase to make sure all DMA requests +* have been quiesced during 'enter' or 'hold' phase +*/ + rc->phases.exit = vtd_reset_exit; dc->vmsd = &vtd_vmstate;device_class_set_props (dc, vtd_properties); dc->hotpluggable = false;
hw/i386/trace-events +1 -0
@@ -68,6 +68,7 @@ vtd_frr_new(int index, uint64_t hi, uint64_t lo) "index %d high 0x%"PRIx64" low vtd_warn_invalid_qi_tail(uint16_t tail) "tail 0x%"PRIx16 vtd_warn_ir_vector(uint16_t sid, int index, int vec, int target) "sid 0x%"PRIx16" index %d vec %d (should be: %d)" vtd_warn_ir_trigger(uint16_t sid, int index, int trig, int target) "sid 0x%"PRIx16" index %d trigger %d (should be: %d)" +vtd_reset_exit(void) "" # amd_iommu.c amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64" + offset 0x%"PRIx32
Signature · 1 line
2.47.1
Re: [PATCH v2 2/5] hw/i386/intel-iommu: Migrate to 3-phase reset
Quoted 6 lines · Eric Auger
Currently the IOMMU may be reset before the devices it protects. For example this happens with virtio devices but also with VFIO devices. In this latter case this produces spurious translation faults on host.
Quoted 7 lines
Let's use 3-phase reset mechanism and reset the IOMMU on exit phase after all DMA capable devices have been reset on 'enter' or 'hold' phase.
Signed-off-by Eric Auger <[email protected]>
Thanks
PATCH v2 3/5 hw/arm/smmuv3: Move reset to exit phase
In reply to Eric AugerCurrently the iommu may be reset before the devices it protects. For example this happens with virtio-scsi-pci. when system_reset is issued from qmp monitor: spurious "virtio: zero sized buffers are not allowed" warnings can be observed. This happens because outstanding DMA requests are still happening while the SMMU gets reset.
This can also happen with VFIO devices. In that case spurious DMA translation faults can be observed on host.
Make sure the SMMU is reset in the 'exit' phase after all DMA capable devices have been reset during the 'enter' or 'hold' phase.
---
v1 -> v2:
- also migrate smmu_base_reset_hold to smmu_base_reset_exit which was an oversight of v1
- add a comment emphasizing the importance of doing the reset in 'exit' phase.
--- hw/arm/smmu-common.c | 9 +++++++-- hw/arm/smmuv3.c | 14 ++++++++++---- hw/arm/trace-events | 1 + 3 files changed, 18 insertions(+), 6 deletions(-)
hw/arm/smmu-common.c +7 -2
@@ -924,7 +924,12 @@ static void smmu_base_realize(DeviceState *dev, Error **errp) } } -static void smmu_base_reset_hold (Object *obj,ResetType type) +/* +* Make sure the IOMMU is reset in 'exit' phase after +* all outstanding DMA requests have been quiesced during +* the 'enter' or 'hold' reset phases +*/ +static void smmu_base_reset_exit (Object *obj,ResetType type) {SMMUState *s =ARM_SMMU (obj);
@@ -949,7 +954,7 @@ static void smmu_base_class_init(ObjectClass *klass, void *data)device_class_set_props (dc, smmu_dev_properties);device_class_set_parent_realize (dc, smmu_base_realize, &sbc->parent_realize); - rc->phases.hold = smmu_base_reset_hold; + rc->phases.exit = smmu_base_reset_exit; }static const TypeInfo smmu_base_info = {
hw/arm/smmuv3.c +10 -4
@@ -1870,13 +1870,19 @@ static void smmu_init_irq(SMMUv3State *s, SysBusDevice *dev) } } -static void smmu_reset_hold (Object *obj,ResetType type) +/* +* Make sure the IOMMU is reset in 'exit' phase after +* all outstanding DMA requests have been quiesced during +* the 'enter' or 'hold' reset phases +*/ +static void smmu_reset_exit (Object *obj,ResetType type) {SMMUv3State *s =ARM_SMMUV3 (obj);SMMUv3Class *c =ARM_SMMUV3_GET_CLASS (s); -if (c->parent_phases.hold) { - c->parent_phases.hold (obj, type); +trace_smmu_reset_exit (); +if (c->parent_phases.exit) { + c->parent_phases.exit (obj, type); }smmuv3_init_regs (s);
@@ -1999,7 +2005,7 @@ static void smmuv3_class_init(ObjectClass *klass, void *data)SMMUv3Class *c =ARM_SMMUV3_CLASS (klass); dc->vmsd = &vmstate_smmuv3; -resettable_class_set_parent_phases (rc,NULL , smmu_reset_hold,NULL , +resettable_class_set_parent_phases (rc,NULL ,NULL , smmu_reset_exit, &c->parent_phases);device_class_set_parent_realize (dc, smmu_realize, &c->parent_realize);
hw/arm/trace-events +1 -0
@@ -56,6 +56,7 @@ smmuv3_config_cache_inv(uint32_t sid) "Config cache INV for sid=0x%x" smmuv3_notify_flag_add(const char *iommu) "ADD SMMUNotifier node for iommu mr=%s" smmuv3_notify_flag_del(const char *iommu) "DEL SMMUNotifier node for iommu mr=%s" smmuv3_inv_notifiers_iova(const char *name, int asid, int vmid, uint64_t iova, uint8_t tg, uint64_t num_pages, int stage) "iommu mr=%s asid=%d vmid=%d iova=0x%"PRIx64" tg=%d num_pages=0x%"PRIx64" stage=%d" +smmu_reset_exit(void) "" # strongarm.c strongarm_uart_update_parameters(const char *label, int speed, char parity, int data_bits, int stop_bits) "%s speed=%d parity=%c data=%d stop=%d"
Signature · 1 line
2.47.1
PATCH v2 4/5 hw/vfio/common: Add a trace point in vfio_reset_handler
In reply to Eric AugerTo ease the debug of reset sequence, let's add a trace point in vfio_reset_handler()
--- hw/vfio/common.c | 1 + hw/vfio/trace-events | 1 + 2 files changed, 2 insertions(+)
hw/vfio/common.c +1 -0
@@ -1386,6 +1386,7 @@ void vfio_reset_handler(void *opaque) {VFIODevice *vbasedev; +trace_vfio_reset_handler (); QLIST_FOREACH(vbasedev, &vfio_device_list, global_next) {if (vbasedev->dev->realized) { vbasedev->ops->vfio_compute_needs_reset (vbasedev);
hw/vfio/trace-events +1 -0
@@ -120,6 +120,7 @@ vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype vfio_legacy_dma_unmap_overflow_workaround(void) "" vfio_get_dirty_bitmap(uint64_t iova, uint64_t size, uint64_t bitmap_size, uint64_t start, uint64_t dirty_pages) "iova=0x%"PRIx64" size= 0x%"PRIx64" bitmap_size=0x%"PRIx64" start=0x%"PRIx64" dirty_pages=%"PRIu64 vfio_iommu_map_dirty_notify(uint64_t iova_start, uint64_t iova_end) "iommu dirty @ 0x%"PRIx64" - 0x%"PRIx64 +vfio_reset_handler(void) "" # platform.c vfio_platform_realize(char *name, char *compat) "vfio device %s, compat = %s"
Signature · 1 line
2.47.1
PATCH v2 5/5 docs/devel/reset: Document reset expectations for DMA and IOMMU
In reply to Eric AugerTo avoid any translation faults, the IOMMUs are expected to be reset after the devices they protect. Document that we expect DMA requests to be stopped during the 'enter' or 'hold' phase while IOMMUs should be reset during the 'exit' phase.
--- docs/devel/reset.rst | 5 +++++ 1 file changed, 5 insertions(+)
docs/devel/reset.rst +5 -0
@@ -143,6 +143,11 @@ The *exit* phase is executed only when the last reset operation ends. Therefore the object does not need to care how many of reset controllers it has and how many of them have started a reset. +DMA capable devices are expected to cancel all outstanding DMA operations +during either 'enter' or 'hold' phases. IOMMUs are expected to reset during +the 'exit' phase and this sequencing makes sure no outstanding DMA request +will fault. + Handling reset in a resettable object -------------------------------------
Signature · 1 line
2.47.1
Hi Eric,
Quoted 5 lines
-----Original Message-----
From: Eric Auger <[email protected]>
Subject: [PATCH v2 0/5] Fix vIOMMU reset orderWith current reset scheme, DMA capable devices are reset before
s/before/after
Quoted 12 lines
the vIOMMU which translate them. This holds for the different IOMMUs and various DMA capable devices such as virtio devices and VFIO ones. With virtio devices spurious traces can be observed at qemu level such as "virtio: zero sized buffers are not allowed" while for VFIO devices, translation faults can be observed at host level.
Virtio devices use 3 phase reset and virtio-pci devices are reset in the 'hold' phase. VFIO device reset are registered using
qemu_register_reset()and as a consequence they are also reset on 'hold' phase.
Quoted 7 lines
Note that the tree of QOM devices resets depth-first but it does so while enforcing the 3 phases. First the tree is traversed doing the 'enter' phase, then the 'hold' phase and eventually the 'exit' phase.
However the QOM hierarchy is not built so that vIOMMUs get reset before the DMA capable devices (IOMMUs are using either legacy reset
s/before/after
Quoted 7 lines
scheme or hold phase). Changing the QOM hierarchy does not sound trivial while forcing the vIOMMUs to be reset on 'exit' phase sounds reasonable and much simpler. Obviously this relies on the assumption that all DMA capable devices quiesce their DMA before (ie. during 'enter' or hold' phase).
This was tested with qmp
system_resetand virsh reset.
For the whole series,
Thanks
Zhenzhong
Hi Zhenzhong,
Quoted 8 lines · Zhenzhong
Hi Eric,
Quoted 5 lines
-----Original Message-----
From: Eric Auger <[email protected]>
Subject: [PATCH v2 0/5] Fix vIOMMU reset orderWith current reset scheme, DMA capable devices are reset before
s/before/after
ugh definitively! :-)
Quoted 30 lines
Quoted 13 lines
the vIOMMU which translate them. This holds for the different IOMMUs and various DMA capable devices such as virtio devices and VFIO ones. With virtio devices spurious traces can be observed at qemu level such as "virtio: zero sized buffers are not allowed" while for VFIO devices, translation faults can be observed at host level.
Virtio devices use 3 phase reset and virtio-pci devices are reset in the 'hold' phase. VFIO device reset are registered using
qemu_register_reset()and as a consequence they are also reset on 'hold' phase.Quoted 7 lines
Note that the tree of QOM devices resets depth-first but it does so while enforcing the 3 phases. First the tree is traversed doing the 'enter' phase, then the 'hold' phase and eventually the 'exit' phase.
However the QOM hierarchy is not built so that vIOMMUs get reset before the DMA capable devices (IOMMUs are using either legacy reset
s/before/after
Quoted 6 lines
scheme or hold phase). Changing the QOM hierarchy does not sound trivial while forcing the vIOMMUs to be reset on 'exit' phase sounds reasonable and much simpler. Obviously this relies on the assumption that all DMA capable devices quiesce their DMA before (ie. during 'enter' or hold' phase).
Quoted 4 lines
Quoted 1 line
This was tested with qmp
system_resetand virsh reset.For the whole series,
Reviewed-by Zhenzhong Duan <[email protected]>
Thanks!
Eric
Quoted 3 lines
Thanks
Zhenzhong