Skip to content

Commit 958ec33

Browse files
xzpetermstsirkin
authored andcommitted
vhost: Unbreak SMMU and virtio-iommu on dev-iotlb support
Previous work on dev-iotlb message broke vhost on either SMMU or virtio-iommu since dev-iotlb (or PCIe ATS) is not yet supported for those archs. An initial idea is that we can let IOMMU to export this information to vhost so that vhost would know whether the vIOMMU would support dev-iotlb, then vhost can conditionally register to dev-iotlb or the old iotlb way. We can work based on some previous patch to introduce PCIIOMMUOps as Yi Liu proposed [1]. However it's not as easy as I thought since vhost_iommu_region_add() does not have a PCIDevice context at all since it's completely a backend. It seems non-trivial to pass over a PCI device to the backend during init. E.g. when the IOMMU notifier registered hdev->vdev is still NULL. To make the fix smaller and easier, this patch goes the other way to leverage the flag_changed() hook of vIOMMUs so that SMMU and virtio-iommu can trap the dev-iotlb registration and fail it. Then vhost could try the fallback solution as using UNMAP invalidation for it's translations. [1] https://lore.kernel.org/qemu-devel/[email protected]/ Reported-by: Eric Auger <[email protected]> Fixes: b68ba1c Reviewed-by: Eric Auger <[email protected]> Tested-by: Eric Auger <[email protected]> Signed-off-by: Peter Xu <[email protected]> Message-Id: <[email protected]> Reviewed-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 73b1230 commit 958ec33

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

hw/arm/smmuv3.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,11 @@ static int smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu,
14971497
SMMUv3State *s3 = sdev->smmu;
14981498
SMMUState *s = &(s3->smmu_state);
14991499

1500+
if (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP) {
1501+
error_setg(errp, "SMMUv3 does not support dev-iotlb yet");
1502+
return -EINVAL;
1503+
}
1504+
15001505
if (new & IOMMU_NOTIFIER_MAP) {
15011506
error_setg(errp,
15021507
"device %02x.%02x.%x requires iommu MAP notifier which is "

hw/virtio/vhost.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ static void vhost_iommu_region_add(MemoryListener *listener,
704704
Int128 end;
705705
int iommu_idx;
706706
IOMMUMemoryRegion *iommu_mr;
707+
int ret;
707708

708709
if (!memory_region_is_iommu(section->mr)) {
709710
return;
@@ -726,8 +727,16 @@ static void vhost_iommu_region_add(MemoryListener *listener,
726727
iommu->iommu_offset = section->offset_within_address_space -
727728
section->offset_within_region;
728729
iommu->hdev = dev;
729-
memory_region_register_iommu_notifier(section->mr, &iommu->n,
730-
&error_fatal);
730+
ret = memory_region_register_iommu_notifier(section->mr, &iommu->n, NULL);
731+
if (ret) {
732+
/*
733+
* Some vIOMMUs do not support dev-iotlb yet. If so, try to use the
734+
* UNMAP legacy message
735+
*/
736+
iommu->n.notifier_flags = IOMMU_NOTIFIER_UNMAP;
737+
memory_region_register_iommu_notifier(section->mr, &iommu->n,
738+
&error_fatal);
739+
}
731740
QLIST_INSERT_HEAD(&dev->iommu_list, iommu, iommu_next);
732741
/* TODO: can replay help performance here? */
733742
}

hw/virtio/virtio-iommu.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,11 @@ static int virtio_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu_mr,
893893
IOMMUNotifierFlag new,
894894
Error **errp)
895895
{
896+
if (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP) {
897+
error_setg(errp, "Virtio-iommu does not support dev-iotlb yet");
898+
return -EINVAL;
899+
}
900+
896901
if (old == IOMMU_NOTIFIER_NONE) {
897902
trace_virtio_iommu_notify_flag_add(iommu_mr->parent_obj.name);
898903
} else if (new == IOMMU_NOTIFIER_NONE) {

0 commit comments

Comments
 (0)