Skip to contentSkip to conversation

vhost: correctly turn on VIRTIO_F_IOMMU_PLATFORM

PATCH Superseded Jason Wang 2 participants 5 messages Feb 26, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 vhost: correctly turn on VIRTIO_F_IOMMU_PLATFORM

We turn on device IOTLB via VIRTIO_F_IOMMU_PLATFORM unconditionally on platform without IOMMU support. This can lead unnecessary IOTLB transactions which will damage the performance.

Fixing this by check whether the device is backed by IOMMU and disable device IOTLB.

Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")
---
 hw/virtio/vhost.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/virtio/vhost.c +11 -1
index 9edfadc81d..6e12c3d2de 100644
@@ -290,7 +290,14 @@ static int vhost_dev_has_iommu(struct vhost_dev *dev)
 {
     VirtIODevice *vdev = dev->vdev;
 
-    return virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+    /*
+     * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
+     * incremental memory mapping API via IOTLB API. For platform that
+     * does not have IOMMU, there's no need to enable this feature
+     * which may cause unnecessary IOTLB miss/update trnasactions.
+     */
+    return vdev->dma_as != &address_space_memory &&
+           virtio_has_feature(dev->acked_features, VIRTIO_F_IOMMU_PLATFORM);
 }
 
 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
@@ -765,6 +772,9 @@ static int vhost_dev_set_features(struct vhost_dev *dev,
     if (enable_log) {
         features |= 0x1ULL << VHOST_F_LOG_ALL;
     }
+    if (dev->vdev->dma_as == &address_space_memory) {
+        features &= ~(0x1ULL << VIRTIO_F_IOMMU_PLATFORM);
+    }
     r = dev->vhost_ops->vhost_set_features(dev, features);
     if (r < 0) {
         VHOST_OPS_DEBUG("vhost_set_features failed");
Signature · 1 line
2.19.1
#1 lore raw
Quoted 24 lines · Jason Wang

We turn on device IOTLB via VIRTIO_F_IOMMU_PLATFORM unconditionally on platform without IOMMU support. This can lead unnecessary IOTLB transactions which will damage the performance.

Fixing this by check whether the device is backed by IOMMU and disable device IOTLB.

Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")
---
 hw/virtio/vhost.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/virtio/vhost.c +1 -1
index 9edfadc81d..6e12c3d2de 100644
@@ -290,7 +290,14 @@ static int vhost_dev_has_iommu(struct vhost_dev *dev)
 {
     VirtIODevice *vdev = dev->vdev;
 
-    return virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+    /*

hw/virtio/vhost.c static int vhost_dev_has_iommu(struct vhost_dev *dev) · line 303

Quoted 10 lines
hw/virtio/vhost.c +7 -0
+     * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
+     * incremental memory mapping API via IOTLB API. For platform that
+     * does not have IOMMU, there's no need to enable this feature
+     * which may cause unnecessary IOTLB miss/update trnasactions.
+     */
+    return vdev->dma_as != &address_space_memory &&
+           virtio_has_feature(dev->acked_features, VIRTIO_F_IOMMU_PLATFORM);
 }
 
 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,

Why check acked_features and not host features here? I'd worry that if we do it like this, userspace driver within guest can clear the feature and make device access memory directly.

hw/virtio/vhost.c static int vhost_dev_set_features(struct vhost_dev *dev, · line 777

Quoted 7 lines
hw/virtio/vhost.c +3 -0
@@ -765,6 +772,9 @@ static int vhost_dev_set_features(struct vhost_dev *dev,
     if (enable_log) {
         features |= 0x1ULL << VHOST_F_LOG_ALL;
     }
+    if (dev->vdev->dma_as == &address_space_memory) {
+        features &= ~(0x1ULL << VIRTIO_F_IOMMU_PLATFORM);
+    }

That's a guest visible change. Which seems at best unnecessary.

Quoted 5 lines
hw/virtio/vhost.c +0 -0
     r = dev->vhost_ops->vhost_set_features(dev, features);
     if (r < 0) {
         VHOST_OPS_DEBUG("vhost_set_features failed");
Signature · 1 line
2.19.1
author
#2 lore raw

----- Original Message -----

Quoted 35 lines · Jason Wang
Quoted 34 lines

We turn on device IOTLB via VIRTIO_F_IOMMU_PLATFORM unconditionally on platform without IOMMU support. This can lead unnecessary IOTLB transactions which will damage the performance.

Fixing this by check whether the device is backed by IOMMU and disable device IOTLB.

Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")
---
 hw/virtio/vhost.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/virtio/vhost.c +8 -1
index 9edfadc81d..6e12c3d2de 100644
@@ -290,7 +290,14 @@ static int vhost_dev_has_iommu(struct vhost_dev *dev)
 {
     VirtIODevice *vdev = dev->vdev;
 
-    return virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+    /*
+     * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
+     * incremental memory mapping API via IOTLB API. For platform that
+     * does not have IOMMU, there's no need to enable this feature
+     * which may cause unnecessary IOTLB miss/update trnasactions.
+     */
+    return vdev->dma_as != &address_space_memory &&
+           virtio_has_feature(dev->acked_features,
 VIRTIO_F_IOMMU_PLATFORM);
 }
 
Quoted 6 lines
Quoted 1 line

static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,

Why check acked_features and not host features here? I'd worry that if we do it like this, userspace driver within guest can clear the feature and make device access memory directly.

Right, host_features should be more than enough.

Quoted 13 lines

hw/virtio/vhost.c static int vhost_dev_set_features(struct vhost_dev · line 778

Quoted 9 lines
hw/virtio/vhost.c +3 -0
@@ -765,6 +772,9 @@ static int vhost_dev_set_features(struct vhost_dev
 *dev,
     if (enable_log) {
         features |= 0x1ULL << VHOST_F_LOG_ALL;
     }
+    if (dev->vdev->dma_as == &address_space_memory) {
+        features &= ~(0x1ULL << VIRTIO_F_IOMMU_PLATFORM);
+    }

That's a guest visible change. Which seems at best unnecessary.

I don't get how this can be visible from guest? It works as F_LOG_ALL.

Thanks

Quoted 7 lines
Quoted 5 lines
hw/virtio/vhost.c +0 -0
     r = dev->vhost_ops->vhost_set_features(dev, features);
     if (r < 0) {
         VHOST_OPS_DEBUG("vhost_set_features failed");

--
2.19.1

#3 lore raw
Quoted 61 lines · Jason Wang

----- Original Message -----

Quoted 35 lines · Jason Wang
Quoted 34 lines

We turn on device IOTLB via VIRTIO_F_IOMMU_PLATFORM unconditionally on platform without IOMMU support. This can lead unnecessary IOTLB transactions which will damage the performance.

Fixing this by check whether the device is backed by IOMMU and disable device IOTLB.

Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")
---
 hw/virtio/vhost.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/virtio/vhost.c +8 -1
index 9edfadc81d..6e12c3d2de 100644
@@ -290,7 +290,14 @@ static int vhost_dev_has_iommu(struct vhost_dev *dev)
 {
     VirtIODevice *vdev = dev->vdev;
 
-    return virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+    /*
+     * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
+     * incremental memory mapping API via IOTLB API. For platform that
+     * does not have IOMMU, there's no need to enable this feature
+     * which may cause unnecessary IOTLB miss/update trnasactions.
+     */
+    return vdev->dma_as != &address_space_memory &&
+           virtio_has_feature(dev->acked_features,
 VIRTIO_F_IOMMU_PLATFORM);
 }
 
Quoted 6 lines
Quoted 1 line

static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,

Why check acked_features and not host features here? I'd worry that if we do it like this, userspace driver within guest can clear the feature and make device access memory directly.

Right, host_features should be more than enough.

Quoted 13 lines

hw/virtio/vhost.c static int vhost_dev_set_features(struct vhost_dev · line 778

Quoted 9 lines
hw/virtio/vhost.c +3 -0
@@ -765,6 +772,9 @@ static int vhost_dev_set_features(struct vhost_dev
 *dev,
     if (enable_log) {
         features |= 0x1ULL << VHOST_F_LOG_ALL;
     }
+    if (dev->vdev->dma_as == &address_space_memory) {
+        features &= ~(0x1ULL << VIRTIO_F_IOMMU_PLATFORM);
+    }

That's a guest visible change. Which seems at best unnecessary.

Quoted 3 lines

I don't get how this can be visible from guest? It works as F_LOG_ALL.

Thanks

Oh you are right.
So just call vhost_dev_has_iommu here too?

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
hw/virtio/vhost.c +0 -0
     r = dev->vhost_ops->vhost_set_features(dev, features);
     if (r < 0) {
         VHOST_OPS_DEBUG("vhost_set_features failed");

--
2.19.1

author
#4 lore raw
Quoted 56 lines · Michael S. Tsirkin
Quoted 55 lines · Jason Wang

----- Original Message -----

Quoted 35 lines · Jason Wang
Quoted 34 lines

We turn on device IOTLB via VIRTIO_F_IOMMU_PLATFORM unconditionally on platform without IOMMU support. This can lead unnecessary IOTLB transactions which will damage the performance.

Fixing this by check whether the device is backed by IOMMU and disable device IOTLB.

Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")
---
  hw/virtio/vhost.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
hw/virtio/vhost.c +8 -1
index 9edfadc81d..6e12c3d2de 100644
@@ -290,7 +290,14 @@ static int vhost_dev_has_iommu(struct vhost_dev *dev)
  {
      VirtIODevice *vdev = dev->vdev;
 
-    return virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+    /*
+     * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
+     * incremental memory mapping API via IOTLB API. For platform that
+     * does not have IOMMU, there's no need to enable this feature
+     * which may cause unnecessary IOTLB miss/update trnasactions.
+     */
+    return vdev->dma_as != &address_space_memory &&
+           virtio_has_feature(dev->acked_features,
 VIRTIO_F_IOMMU_PLATFORM);
  }
 
Quoted 5 lines
Quoted 1 line

static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,

Why check acked_features and not host features here? I'd worry that if we do it like this, userspace driver within guest can clear the feature and make device access memory directly.

Right, host_features should be more than enough.

Quoted 11 lines

hw/virtio/vhost.c static int vhost_dev_set_features(struct vhost_dev · line 778

Quoted 8 lines
hw/virtio/vhost.c +3 -0
@@ -765,6 +772,9 @@ static int vhost_dev_set_features(struct vhost_dev
 *dev,
      if (enable_log) {
          features |= 0x1ULL << VHOST_F_LOG_ALL;
      }
+    if (dev->vdev->dma_as == &address_space_memory) {
+        features &= ~(0x1ULL << VIRTIO_F_IOMMU_PLATFORM);
+    }

That's a guest visible change. Which seems at best unnecessary.

Quoted 5 lines
Quoted 3 lines

I don't get how this can be visible from guest? It works as F_LOG_ALL.

Thanks

Oh you are right.
So just call vhost_dev_has_iommu here too?

That should work.

Thanks

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
hw/virtio/vhost.c +0 -0
      r = dev->vhost_ops->vhost_set_features(dev, features);
      if (r < 0) {
          VHOST_OPS_DEBUG("vhost_set_features failed");

--
2.19.1