vhost: register and change IOMMU flag depending on ATS state
v4 cover vhost: register and change IOMMU flag depending on ATS state
When IOMMU and vhost are enabled together, QEMU tracks IOTLB or Device-TLB unmap events depending on whether Device-TLB is enabled. But even if Device-TLB and PCI ATS is enabled, the guest can reject to use it. For example, this situation appears when Windows Server 2022 is running with intel-iommu with device-iotlb=on and virtio-net-pci with vhost=on. The guest implies that no address translation info cached in device IOTLB and doesn't send device IOTLB invalidation commands. So, it leads to irrelevant address translations in vhost-net in the host kernel. Therefore network frames from the guest in host tap interface contains wrong payload data.
This series adds checking of ATS state for proper unmap flag register (IOMMU_NOTIFIER_UNMAP or IOMMU_NOTIFIER_DEVIOTLB_UNMAP).
Tested on Windows Server 2022, Windows 11 and Fedora guests with
-device virtio-net-pci,bus=pci.3,netdev=nd0,iommu_platform=on,ats=on -netdev tap,id=nd0,ifname=tap1,script=no,downscript=no,vhost=on -device intel-iommu,intremap=on,eim=on,device-iotlb=on/off
Tested on Fedora guest with
-device virtio-iommu
v4: call vhost_toggle_device_iotlb regardless of vhost backend,
move vhost_started check to generic part
v3: call virtio_pci_ats_ctrl_trigger directly, remove
IOMMU_NOTIFIER_UNMAP fallbacks
v2: remove memory_region_iommu_notify_flags_changed, move trigger to
VirtioDeviceClass, use vhost_ops, use device_iotlb name
Viktor Prutyanov (2):
vhost: register and change IOMMU flag depending on Device-TLB state
virtio-net: pass Device-TLB enable/disable events to vhost
hw/net/virtio-net.c | 1 + hw/virtio/vhost.c | 38 ++++++++++++++++++++++++++------------ include/hw/virtio/vhost.h | 1 + 3 files changed, 28 insertions(+), 12 deletions(-)
Signature · 1 line
2.21.0
PATCH v4 1/2 vhost: register and change IOMMU flag depending on Device-TLB state
The guest can disable or never enable Device-TLB. In these cases, it can't be used even if enabled in QEMU. So, check Device-TLB state before registering IOMMU notifier and select unmap flag depending on that. Also, implement a way to change IOMMU notifier flag if Device-TLB state is changed.
--- hw/virtio/vhost.c | 38 ++++++++++++++++++++++++++------------ include/hw/virtio/vhost.h | 1 + 2 files changed, 27 insertions(+), 12 deletions(-)
hw/virtio/vhost.c +26 -12
@@ -781,7 +781,6 @@ static void vhost_iommu_region_add(MemoryListener *listener,Int128 end;int iommu_idx;IOMMUMemoryRegion *iommu_mr; -int ret;if (!memory_region_is_iommu (section->mr)) {return ;
@@ -796,7 +795,9 @@ static void vhost_iommu_region_add(MemoryListener *listener, iommu_idx =memory_region_iommu_attrs_to_index (iommu_mr,MEMTXATTRS_UNSPECIFIED );iommu_notifier_init (&iommu->n, vhost_iommu_unmap_notify, -IOMMU_NOTIFIER_DEVIOTLB_UNMAP , + dev->vdev->device_iotlb_enabled ? +IOMMU_NOTIFIER_DEVIOTLB_UNMAP : +IOMMU_NOTIFIER_UNMAP , section->offset_within_region,int128_get64 (end), iommu_idx);
@@ -804,16 +805,8 @@ static void vhost_iommu_region_add(MemoryListener *listener, iommu->iommu_offset = section->offset_within_address_space - section->offset_within_region; iommu->hdev = dev; - ret =memory_region_register_iommu_notifier (section->mr, &iommu->n,NULL ); -if (ret) { -/* -* Some vIOMMUs do not support dev-iotlb yet. If so, try to use the -* UNMAP legacy message -*/ - iommu->n.notifier_flags =IOMMU_NOTIFIER_UNMAP ; -memory_region_register_iommu_notifier (section->mr, &iommu->n, - &error_fatal); - } +memory_region_register_iommu_notifier (section->mr, &iommu->n, + &error_fatal);QLIST_INSERT_HEAD (&dev->iommu_list, iommu, iommu_next);/* TODO: can replay help performance here? */ }
@@ -841,6 +834,27 @@ static void vhost_iommu_region_del(MemoryListener *listener, } } +void vhost_toggle_device_iotlb (VirtIODevice *vdev) +{ +VirtioDeviceClass *vdc =VIRTIO_DEVICE_GET_CLASS (vdev); +struct vhost_dev *dev; +struct vhost_iommu *iommu; + +if (vdev->vhost_started) { + dev = vdc->get_vhost (vdev); + }else { +return ; + } + + QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) { +memory_region_unregister_iommu_notifier (iommu->mr, &iommu->n); + iommu->n.notifier_flags = vdev->device_iotlb_enabled ? +IOMMU_NOTIFIER_DEVIOTLB_UNMAP :IOMMU_NOTIFIER_UNMAP ; +memory_region_register_iommu_notifier (iommu->mr, &iommu->n, + &error_fatal); + } +} +static int vhost_virtqueue_set_addr (struct vhost_dev *dev,struct vhost_virtqueue *vq,unsigned idx,bool enable_log)
include/hw/virtio/vhost.h +1 -0
@@ -320,6 +320,7 @@ bool vhost_has_free_slot(void);int vhost_net_set_backend (struct vhost_dev *hdev,struct vhost_vring_file *file); +void vhost_toggle_device_iotlb (VirtIODevice *vdev);int vhost_device_iotlb_miss (struct vhost_dev *dev,uint64_t iova,int write); int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev,
Signature · 1 line
2.21.0
Re: [PATCH v4 1/2] vhost: register and change IOMMU flag depending on Device-TLB state
Quoted 9 lines · Viktor Prutyanov
The guest can disable or never enable Device-TLB. In these cases, it can't be used even if enabled in QEMU. So, check Device-TLB state before registering IOMMU notifier and select unmap flag depending on that. Also, implement a way to change IOMMU notifier flag if Device-TLB state is changed.
Signed-off-by Viktor Prutyanov <[email protected]>
Thanks
Quoted 90 lines
--- hw/virtio/vhost.c | 38 ++++++++++++++++++++++++++------------ include/hw/virtio/vhost.h | 1 + 2 files changed, 27 insertions(+), 12 deletions(-)hw/virtio/vhost.c +26
-12@@ -781,7 +781,6 @@ static void vhost_iommu_region_add(MemoryListener *listener,Int128 end;int iommu_idx;IOMMUMemoryRegion *iommu_mr; -int ret;if (!memory_region_is_iommu (section->mr)) {return ;@@ -796,7 +795,9 @@ static void vhost_iommu_region_add(MemoryListener *listener, iommu_idx =memory_region_iommu_attrs_to_index (iommu_mr,MEMTXATTRS_UNSPECIFIED );iommu_notifier_init (&iommu->n, vhost_iommu_unmap_notify, -IOMMU_NOTIFIER_DEVIOTLB_UNMAP , + dev->vdev->device_iotlb_enabled ? +IOMMU_NOTIFIER_DEVIOTLB_UNMAP : +IOMMU_NOTIFIER_UNMAP , section->offset_within_region,int128_get64 (end), iommu_idx);@@ -804,16 +805,8 @@ static void vhost_iommu_region_add(MemoryListener *listener, iommu->iommu_offset = section->offset_within_address_space - section->offset_within_region; iommu->hdev = dev; - ret =memory_region_register_iommu_notifier (section->mr, &iommu->n,NULL ); -if (ret) { -/* -* Some vIOMMUs do not support dev-iotlb yet. If so, try to use the -* UNMAP legacy message -*/ - iommu->n.notifier_flags =IOMMU_NOTIFIER_UNMAP ; -memory_region_register_iommu_notifier (section->mr, &iommu->n, - &error_fatal); - } +memory_region_register_iommu_notifier (section->mr, &iommu->n, + &error_fatal);QLIST_INSERT_HEAD (&dev->iommu_list, iommu, iommu_next);/* TODO: can replay help performance here? */ }@@ -841,6 +834,27 @@ static void vhost_iommu_region_del(MemoryListener *listener, } } +void vhost_toggle_device_iotlb (VirtIODevice *vdev) +{ +VirtioDeviceClass *vdc =VIRTIO_DEVICE_GET_CLASS (vdev); +struct vhost_dev *dev; +struct vhost_iommu *iommu; + +if (vdev->vhost_started) { + dev = vdc->get_vhost (vdev); + }else { +return ; + } + + QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) { +memory_region_unregister_iommu_notifier (iommu->mr, &iommu->n); + iommu->n.notifier_flags = vdev->device_iotlb_enabled ? +IOMMU_NOTIFIER_DEVIOTLB_UNMAP :IOMMU_NOTIFIER_UNMAP ; +memory_region_register_iommu_notifier (iommu->mr, &iommu->n, + &error_fatal); + } +} +static int vhost_virtqueue_set_addr (struct vhost_dev *dev,struct vhost_virtqueue *vq,unsigned idx,bool enable_log)include/hw/virtio/vhost.h +1
-1@@ -320,6 +320,7 @@ bool vhost_has_free_slot(void);int vhost_net_set_backend (struct vhost_dev *hdev,struct vhost_vring_file *file); +void vhost_toggle_device_iotlb (VirtIODevice *vdev);int vhost_device_iotlb_miss (struct vhost_dev *dev,uint64_t iova,int write); int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev, --2.21.0
PATCH v4 2/2 virtio-net: pass Device-TLB enable/disable events to vhost
In reply to Viktor PrutyanovIf vhost is enabled for virtio-net, Device-TLB enable/disable events must be passed to vhost for proper IOMMU unmap flag selection.
--- hw/net/virtio-net.c | 1 + 1 file changed, 1 insertion(+)
hw/net/virtio-net.c +1 -0
@@ -3951,6 +3951,7 @@ static void virtio_net_class_init(ObjectClass *klass, void *data) vdc->vmsd = &vmstate_virtio_net_device; vdc->primary_unplug_pending = primary_unplug_pending; vdc->get_vhost = virtio_net_get_vhost; + vdc->toggle_device_iotlb = vhost_toggle_device_iotlb; }static const TypeInfo virtio_net_info = {
Signature · 1 line
2.21.0
Re: [PATCH v4 2/2] virtio-net: pass Device-TLB enable/disable events to vhost
Quoted 5 lines · Viktor Prutyanov
If vhost is enabled for virtio-net, Device-TLB enable/disable events must be passed to vhost for proper IOMMU unmap flag selection.
Signed-off-by Viktor Prutyanov <[email protected]>
Thanks
Quoted 19 lines
--- hw/net/virtio-net.c | 1 + 1 file changed, 1 insertion(+)hw/net/virtio-net.c +1
-1@@ -3951,6 +3951,7 @@ static void virtio_net_class_init(ObjectClass *klass, void *data) vdc->vmsd = &vmstate_virtio_net_device; vdc->primary_unplug_pending = primary_unplug_pending; vdc->get_vhost = virtio_net_get_vhost; + vdc->toggle_device_iotlb = vhost_toggle_device_iotlb; }static const TypeInfo virtio_net_info = { --2.21.0
Quoted 14 lines · Viktor Prutyanov
When IOMMU and vhost are enabled together, QEMU tracks IOTLB or Device-TLB unmap events depending on whether Device-TLB is enabled. But even if Device-TLB and PCI ATS is enabled, the guest can reject to use it. For example, this situation appears when Windows Server 2022 is running with intel-iommu with device-iotlb=on and virtio-net-pci with vhost=on. The guest implies that no address translation info cached in device IOTLB and doesn't send device IOTLB invalidation commands. So, it leads to irrelevant address translations in vhost-net in the host kernel. Therefore network frames from the guest in host tap interface contains wrong payload data.
This series adds checking of ATS state for proper unmap flag register (
IOMMU_NOTIFIER_UNMAPorIOMMU_NOTIFIER_DEVIOTLB_UNMAP).
Quoted 6 lines
Tested on Windows Server 2022, Windows 11 and Fedora guests with
-device virtio-net-pci,bus=pci.3,netdev=nd0,iommu_platform=on,ats=on -netdev tap,id=nd0,ifname=tap1,script=no,downscript=no,vhost=on -device intel-iommu,intremap=on,eim=on,device-iotlb=on/off
Tested on Fedora guest with
-device virtio-iommu
Fails build on windows hosts:
https://gitlab.com/mstredhat/qemu/-/pipelines/909063579/failures
C:\GitLab-Runner\builds\mstredhat\qemu\output/../hw/net/virtio-net.c:3954: undefined reference to `vhost_toggle_device_iotlb'
Quoted 20 lines
v4: call
vhost_toggle_device_iotlbregardless of vhost backend,
movevhost_startedcheck to generic part
v3: callvirtio_pci_ats_ctrl_triggerdirectly, remove
IOMMU_NOTIFIER_UNMAPfallbacks
v2: removememory_region_iommu_notify_flags_changed, move trigger to
VirtioDeviceClass, usevhost_ops, usedevice_iotlbnameViktor Prutyanov (2):
vhost: register and change IOMMU flag depending on Device-TLB state
virtio-net: pass Device-TLB enable/disable events to vhosthw/net/virtio-net.c | 1 + hw/virtio/vhost.c | 38 ++++++++++++++++++++++++++------------ include/hw/virtio/vhost.h | 1 + 3 files changed, 28 insertions(+), 12 deletions(-)Signature · 1 line
2.21.0