Skip to contentSkip to conversation

vhost: Ignore vrings in dirty log when using a vIOMMU

PATCH In review Greg Kurz 3 participants 8 messages Sep 25, 2020 – Oct 6, 2020

lore
Share & tools
lore

Greg Kurz

author
#0 lore raw

PATCH 1/1 vhost: Ignore vrings in dirty log when using a vIOMMU

When a vIOMMU is present, any address comming from the guest is an IO virtual address, including those of the vrings. The backend's accesses to the vrings happen through vIOMMU translation : the backend hence only logs the final guest physical address, not the IO virtual one. It thus doesn't make sense to make room for the vring addresses in the dirty log in this case.

This fixes a crash of the source when migrating a guest using in-kernel vhost-net and iommu_platform=on on POWER, because DMA regions are put at very high addresses and the resulting log size is likely to cause g_malloc0() to abort.

---
 hw/virtio/vhost.c |   38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
hw/virtio/vhost.c +24 -14
index 1a1384e7a642..0b83d6b8e65e 100644
@@ -106,6 +106,20 @@ static void vhost_dev_sync_region(struct vhost_dev *dev,
     }
 }
 
+static int vhost_dev_has_iommu(struct vhost_dev *dev)
+{
+    VirtIODevice *vdev = dev->vdev;
+
+    /*
+     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+}
+
 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                                    MemoryRegionSection *section,
                                    hwaddr first,
@@ -130,6 +144,11 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                               range_get_last(reg->guest_phys_addr,
                                              reg->memory_size));
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return 0;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -172,6 +191,11 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)
                                        reg->memory_size);
         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return log_size;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -287,20 +311,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
     dev->log_size = size;
 }
 
-static int vhost_dev_has_iommu(struct vhost_dev *dev)
-{
-    VirtIODevice *vdev = dev->vdev;
-
-    /*
-     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
-}
-
 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
                               hwaddr *plen, bool is_write)
 {
author
#1 lore raw

Cc'ing Jason since this was detected using vhost-net.

Quoted 91 lines · On Fri, 25 Sep 2020 19:29:43 +0200 Greg Kurz <[email protected]> wrote:

When a vIOMMU is present, any address comming from the guest is an IO virtual address, including those of the vrings. The backend's accesses to the vrings happen through vIOMMU translation : the backend hence only logs the final guest physical address, not the IO virtual one. It thus doesn't make sense to make room for the vring addresses in the dirty log in this case.

This fixes a crash of the source when migrating a guest using in-kernel vhost-net and iommu_platform=on on POWER, because DMA regions are put at very high addresses and the resulting log size is likely to cause g_malloc0() to abort.

---
 hw/virtio/vhost.c |   38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
hw/virtio/vhost.c +24 -14
index 1a1384e7a642..0b83d6b8e65e 100644
@@ -106,6 +106,20 @@ static void vhost_dev_sync_region(struct vhost_dev *dev,
     }
 }
 
+static int vhost_dev_has_iommu(struct vhost_dev *dev)
+{
+    VirtIODevice *vdev = dev->vdev;
+
+    /*
+     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+}
+
 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                                    MemoryRegionSection *section,
                                    hwaddr first,
@@ -130,6 +144,11 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                               range_get_last(reg->guest_phys_addr,
                                              reg->memory_size));
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return 0;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -172,6 +191,11 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)
                                        reg->memory_size);
         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return log_size;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -287,20 +311,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
     dev->log_size = size;
 }
 
-static int vhost_dev_has_iommu(struct vhost_dev *dev)
-{
-    VirtIODevice *vdev = dev->vdev;
-
-    /*
-     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
-}
-
 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
                               hwaddr *plen, bool is_write)
 {
#2 lore raw
In reply to Greg Kurz
Quoted 7 lines · Greg Kurz

When a vIOMMU is present, any address comming from the guest is an IO virtual address, including those of the vrings. The backend's accesses to the vrings happen through vIOMMU translation : the backend hence only logs the final guest physical address, not the IO virtual one. It thus doesn't make sense to make room for the vring addresses in the dirty log in this case.

Quoted 7 lines

This fixes a crash of the source when migrating a guest using in-kernel vhost-net and iommu_platform=on on POWER, because DMA regions are put at very high addresses and the resulting log size is likely to cause g_malloc0() to abort.

I'm a little confused as to what's going on here. Obviously allocating dirty bitmaps in IOVA space doesn't make much sense. But.. in all cases isn't the ring ending up in guest memory, whether translated or not. So why do specific addresses of the ring make a difference in *any* case.

Quoted 76 lines
---
 hw/virtio/vhost.c |   38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
hw/virtio/vhost.c +24 -14
index 1a1384e7a642..0b83d6b8e65e 100644
@@ -106,6 +106,20 @@ static void vhost_dev_sync_region(struct vhost_dev *dev,
     }
 }
 
+static int vhost_dev_has_iommu(struct vhost_dev *dev)
+{
+    VirtIODevice *vdev = dev->vdev;
+
+    /*
+     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+}
+
 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                                    MemoryRegionSection *section,
                                    hwaddr first,
@@ -130,6 +144,11 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                               range_get_last(reg->guest_phys_addr,
                                              reg->memory_size));
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return 0;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -172,6 +191,11 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)
                                        reg->memory_size);
         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return log_size;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -287,20 +311,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
     dev->log_size = size;
 }
 
-static int vhost_dev_has_iommu(struct vhost_dev *dev)
-{
-    VirtIODevice *vdev = dev->vdev;
-
-    /*
-     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
-}
-
 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
                               hwaddr *plen, bool is_write)
 {
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
author
#3 lore raw
Quoted 13 lines · On Mon, 28 Sep 2020 16:23:43 +1000 David Gibson <[email protected]>…
Quoted 12 lines · Greg Kurz

When a vIOMMU is present, any address comming from the guest is an IO virtual address, including those of the vrings. The backend's accesses to the vrings happen through vIOMMU translation : the backend hence only logs the final guest physical address, not the IO virtual one. It thus doesn't make sense to make room for the vring addresses in the dirty log in this case.

This fixes a crash of the source when migrating a guest using in-kernel vhost-net and iommu_platform=on on POWER, because DMA regions are put at very high addresses and the resulting log size is likely to cause g_malloc0() to abort.

Quoted 9 lines
Quoted 2 lines

I'm a little confused as to what's going on here. Obviously allocating dirty bitmaps in IOVA space doesn't make much sense. But.. in all cases isn't the ring ending up in guest memory, whether translated or not. So why do specific addresses of the ring make a difference in *any* case.

I admit I'm a bit surprised as well... I can't think of a scenario where the address of the used ring would be higher than the guest memory... Maybe MST can shed some light here ?

Quoted 77 lines
Quoted 76 lines
---
 hw/virtio/vhost.c |   38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
hw/virtio/vhost.c +24 -14
index 1a1384e7a642..0b83d6b8e65e 100644
@@ -106,6 +106,20 @@ static void vhost_dev_sync_region(struct vhost_dev *dev,
     }
 }
 
+static int vhost_dev_has_iommu(struct vhost_dev *dev)
+{
+    VirtIODevice *vdev = dev->vdev;
+
+    /*
+     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+}
+
 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                                    MemoryRegionSection *section,
                                    hwaddr first,
@@ -130,6 +144,11 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                               range_get_last(reg->guest_phys_addr,
                                              reg->memory_size));
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return 0;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -172,6 +191,11 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)
                                        reg->memory_size);
         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return log_size;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -287,20 +311,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
     dev->log_size = size;
 }
 
-static int vhost_dev_has_iommu(struct vhost_dev *dev)
-{
-    VirtIODevice *vdev = dev->vdev;
-
-    /*
-     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
-}
-
 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
                               hwaddr *plen, bool is_write)
 {
#4 lore raw
Quoted 26 lines · Greg Kurz
Quoted 13 lines · On Mon, 28 Sep 2020 16:23:43 +1000 David Gibson <[email protected]>…
Quoted 12 lines · Greg Kurz

When a vIOMMU is present, any address comming from the guest is an IO virtual address, including those of the vrings. The backend's accesses to the vrings happen through vIOMMU translation : the backend hence only logs the final guest physical address, not the IO virtual one. It thus doesn't make sense to make room for the vring addresses in the dirty log in this case.

This fixes a crash of the source when migrating a guest using in-kernel vhost-net and iommu_platform=on on POWER, because DMA regions are put at very high addresses and the resulting log size is likely to cause g_malloc0() to abort.

Quoted 9 lines
Quoted 2 lines

I'm a little confused as to what's going on here. Obviously allocating dirty bitmaps in IOVA space doesn't make much sense. But.. in all cases isn't the ring ending up in guest memory, whether translated or not. So why do specific addresses of the ring make a difference in *any* case.

Quoted 3 lines

I admit I'm a bit surprised as well... I can't think of a scenario where the address of the used ring would be higher than the guest memory... Maybe MST can shed some light here ?

So the original idea was that vring itself is specified in terms of HVA as opposed to rest of stuff which is specified in terms of GPA.
This way we wanted to support e.g. migration by vhost writing into qemu address space, qemu copying data to guest memory.

Quoted 78 lines
Quoted 77 lines
Quoted 76 lines
---
 hw/virtio/vhost.c |   38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
hw/virtio/vhost.c +24 -14
index 1a1384e7a642..0b83d6b8e65e 100644
@@ -106,6 +106,20 @@ static void vhost_dev_sync_region(struct vhost_dev *dev,
     }
 }
 
+static int vhost_dev_has_iommu(struct vhost_dev *dev)
+{
+    VirtIODevice *vdev = dev->vdev;
+
+    /*
+     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+}
+
 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                                    MemoryRegionSection *section,
                                    hwaddr first,
@@ -130,6 +144,11 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                               range_get_last(reg->guest_phys_addr,
                                              reg->memory_size));
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return 0;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -172,6 +191,11 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)
                                        reg->memory_size);
         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return log_size;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -287,20 +311,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
     dev->log_size = size;
 }
 
-static int vhost_dev_has_iommu(struct vhost_dev *dev)
-{
-    VirtIODevice *vdev = dev->vdev;
-
-    /*
-     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
-}
-
 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
                               hwaddr *plen, bool is_write)
 {
author
#5 lore raw
Quoted 27 lines · On Mon, 5 Oct 2020 10:18:03 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 26 lines · Greg Kurz
Quoted 22 lines · On Mon, 28 Sep 2020 16:23:43 +1000 David Gibson <[email protected]>…
Quoted 7 lines · Greg Kurz

When a vIOMMU is present, any address comming from the guest is an IO virtual address, including those of the vrings. The backend's accesses to the vrings happen through vIOMMU translation : the backend hence only logs the final guest physical address, not the IO virtual one. It thus doesn't make sense to make room for the vring addresses in the dirty log in this case.

Quoted 7 lines

This fixes a crash of the source when migrating a guest using in-kernel vhost-net and iommu_platform=on on POWER, because DMA regions are put at very high addresses and the resulting log size is likely to cause g_malloc0() to abort.

I'm a little confused as to what's going on here. Obviously allocating dirty bitmaps in IOVA space doesn't make much sense. But.. in all cases isn't the ring ending up in guest memory, whether translated or not. So why do specific addresses of the ring make a difference in *any* case.

Quoted 7 lines
Quoted 3 lines

I admit I'm a bit surprised as well... I can't think of a scenario where the address of the used ring would be higher than the guest memory... Maybe MST can shed some light here ?

So the original idea was that vring itself is specified in terms of HVA as opposed to rest of stuff which is specified in terms of GPA.

The vring itself is indeed described in term of HVA (vq->used) but when it comes to the dirty log, QEMU passes the GPA of the used structure to the vhost backend:

From vhost_virtqueue_set_addr():

    addr.log_guest_addr = vq->used_phys;
                              ^^ GPA ^^
    addr.flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0;
    r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr);

And the sizing of the bitmap computed in vhost_get_log_size() is also based on this GPA:

    for (i = 0; i < dev->nvqs; ++i) {
        struct vhost_virtqueue *vq = dev->vqs + i;

        if (!vq->used_phys && !vq->used_size) {
            continue;
        }

        uint64_t last = vq->used_phys + vq->used_size - 1;
                            ^^ GPA ^^
        log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
    }

With platform_iommu=off, I couldn't find a case where this second loop in vhost_get_log_size() increases the log size actually... and when platform_iommu=on, vq->used_phys is a GIOVA that my other patches you've already merged in the kernel explicitly ignore when it comes to the dirty log... So it really seems that this loop is useless for the general case. If it is there to address some corner case, I guess it deserves a comment at the very least.

Cheers,

--
Greg

Quoted 86 lines

This way we wanted to support e.g. migration by vhost writing into qemu address space, qemu copying data to guest memory.

Quoted 78 lines
Quoted 77 lines
Quoted 76 lines
---
 hw/virtio/vhost.c |   38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
hw/virtio/vhost.c +24 -14
index 1a1384e7a642..0b83d6b8e65e 100644
@@ -106,6 +106,20 @@ static void vhost_dev_sync_region(struct vhost_dev *dev,
     }
 }
 
+static int vhost_dev_has_iommu(struct vhost_dev *dev)
+{
+    VirtIODevice *vdev = dev->vdev;
+
+    /*
+     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+}
+
 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                                    MemoryRegionSection *section,
                                    hwaddr first,
@@ -130,6 +144,11 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                               range_get_last(reg->guest_phys_addr,
                                              reg->memory_size));
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return 0;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -172,6 +191,11 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)
                                        reg->memory_size);
         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return log_size;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -287,20 +311,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
     dev->log_size = size;
 }
 
-static int vhost_dev_has_iommu(struct vhost_dev *dev)
-{
-    VirtIODevice *vdev = dev->vdev;
-
-    /*
-     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
-}
-
 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
                               hwaddr *plen, bool is_write)
 {
#6 lore raw
Quoted 42 lines · Greg Kurz
Quoted 27 lines · On Mon, 5 Oct 2020 10:18:03 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 26 lines · Greg Kurz
Quoted 22 lines · On Mon, 28 Sep 2020 16:23:43 +1000 David Gibson <[email protected]>…
Quoted 7 lines · Greg Kurz

When a vIOMMU is present, any address comming from the guest is an IO virtual address, including those of the vrings. The backend's accesses to the vrings happen through vIOMMU translation : the backend hence only logs the final guest physical address, not the IO virtual one. It thus doesn't make sense to make room for the vring addresses in the dirty log in this case.

Quoted 7 lines

This fixes a crash of the source when migrating a guest using in-kernel vhost-net and iommu_platform=on on POWER, because DMA regions are put at very high addresses and the resulting log size is likely to cause g_malloc0() to abort.

I'm a little confused as to what's going on here. Obviously allocating dirty bitmaps in IOVA space doesn't make much sense. But.. in all cases isn't the ring ending up in guest memory, whether translated or not. So why do specific addresses of the ring make a difference in *any* case.

Quoted 7 lines
Quoted 3 lines

I admit I'm a bit surprised as well... I can't think of a scenario where the address of the used ring would be higher than the guest memory... Maybe MST can shed some light here ?

So the original idea was that vring itself is specified in terms of HVA as opposed to rest of stuff which is specified in terms of GPA.

The vring itself is indeed described in term of HVA (vq->used) but when it comes to the dirty log, QEMU passes the GPA of the used structure to the vhost backend:

Quoted 6 lines
Quoted 1 line

From vhost_virtqueue_set_addr():

    addr.log_guest_addr = vq->used_phys;
                              ^^ GPA ^^
    addr.flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0;
    r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr);

Right. The point being we either use the log bitmap or the ring aliasing trick to track memory changes, not both. If we used the ring aliasing trick then presumably VHOST_VRING_F_LOG would be clear and then log_guest_addr is unused.

Quoted 114 lines

And the sizing of the bitmap computed in vhost_get_log_size() is also based on this GPA:

    for (i = 0; i < dev->nvqs; ++i) {
        struct vhost_virtqueue *vq = dev->vqs + i;

        if (!vq->used_phys && !vq->used_size) {
            continue;
        }

        uint64_t last = vq->used_phys + vq->used_size - 1;
                            ^^ GPA ^^
        log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
    }

With platform_iommu=off, I couldn't find a case where this second loop in vhost_get_log_size() increases the log size actually... and when platform_iommu=on, vq->used_phys is a GIOVA that my other patches you've already merged in the kernel explicitly ignore when it comes to the dirty log... So it really seems that this loop is useless for the general case. If it is there to address some corner case, I guess it deserves a comment at the very least.

Cheers,

--
Greg

Quoted 86 lines

This way we wanted to support e.g. migration by vhost writing into qemu address space, qemu copying data to guest memory.

Quoted 78 lines
Quoted 77 lines
Quoted 76 lines
---
 hw/virtio/vhost.c |   38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
hw/virtio/vhost.c +24 -14
index 1a1384e7a642..0b83d6b8e65e 100644
@@ -106,6 +106,20 @@ static void vhost_dev_sync_region(struct vhost_dev *dev,
     }
 }
 
+static int vhost_dev_has_iommu(struct vhost_dev *dev)
+{
+    VirtIODevice *vdev = dev->vdev;
+
+    /*
+     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+}
+
 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                                    MemoryRegionSection *section,
                                    hwaddr first,
@@ -130,6 +144,11 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                               range_get_last(reg->guest_phys_addr,
                                              reg->memory_size));
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return 0;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -172,6 +191,11 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)
                                        reg->memory_size);
         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return log_size;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -287,20 +311,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
     dev->log_size = size;
 }
 
-static int vhost_dev_has_iommu(struct vhost_dev *dev)
-{
-    VirtIODevice *vdev = dev->vdev;
-
-    /*
-     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
-}
-
 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
                               hwaddr *plen, bool is_write)
 {
author
#7 lore raw
Quoted 43 lines · On Tue, 6 Oct 2020 06:49:28 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 42 lines · Greg Kurz
Quoted 27 lines · On Mon, 5 Oct 2020 10:18:03 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 26 lines · Greg Kurz
Quoted 22 lines · On Mon, 28 Sep 2020 16:23:43 +1000 David Gibson <[email protected]>…
Quoted 7 lines · Greg Kurz

When a vIOMMU is present, any address comming from the guest is an IO virtual address, including those of the vrings. The backend's accesses to the vrings happen through vIOMMU translation : the backend hence only logs the final guest physical address, not the IO virtual one. It thus doesn't make sense to make room for the vring addresses in the dirty log in this case.

Quoted 7 lines

This fixes a crash of the source when migrating a guest using in-kernel vhost-net and iommu_platform=on on POWER, because DMA regions are put at very high addresses and the resulting log size is likely to cause g_malloc0() to abort.

I'm a little confused as to what's going on here. Obviously allocating dirty bitmaps in IOVA space doesn't make much sense. But.. in all cases isn't the ring ending up in guest memory, whether translated or not. So why do specific addresses of the ring make a difference in *any* case.

Quoted 7 lines
Quoted 3 lines

I admit I'm a bit surprised as well... I can't think of a scenario where the address of the used ring would be higher than the guest memory... Maybe MST can shed some light here ?

So the original idea was that vring itself is specified in terms of HVA as opposed to rest of stuff which is specified in terms of GPA.

The vring itself is indeed described in term of HVA (vq->used) but when it comes to the dirty log, QEMU passes the GPA of the used structure to the vhost backend:

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

From vhost_virtqueue_set_addr():

    addr.log_guest_addr = vq->used_phys;
                              ^^ GPA ^^
    addr.flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0;
    r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr);

Right. The point being we either use the log bitmap or the ring aliasing

Not sure what the "ring aliasing trick" is referring too...

Quoted 4 lines

trick to track memory changes, not both. If we used the ring aliasing trick then presumably VHOST_VRING_F_LOG would be clear and then log_guest_addr is unused.

Ok, so IIUC this means that vhost_get_log_size() never needs to special case vq->used_phys (ie. log_guest_addr) since it is a GPA (ie. <= guest RAM size and thus already covered by the first loop), right ?

Quoted 115 lines
Quoted 114 lines

And the sizing of the bitmap computed in vhost_get_log_size() is also based on this GPA:

    for (i = 0; i < dev->nvqs; ++i) {
        struct vhost_virtqueue *vq = dev->vqs + i;

        if (!vq->used_phys && !vq->used_size) {
            continue;
        }

        uint64_t last = vq->used_phys + vq->used_size - 1;
                            ^^ GPA ^^
        log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
    }

With platform_iommu=off, I couldn't find a case where this second loop in vhost_get_log_size() increases the log size actually... and when platform_iommu=on, vq->used_phys is a GIOVA that my other patches you've already merged in the kernel explicitly ignore when it comes to the dirty log... So it really seems that this loop is useless for the general case. If it is there to address some corner case, I guess it deserves a comment at the very least.

Cheers,

--
Greg

Quoted 86 lines

This way we wanted to support e.g. migration by vhost writing into qemu address space, qemu copying data to guest memory.

Quoted 78 lines
Quoted 77 lines
Quoted 76 lines
---
 hw/virtio/vhost.c |   38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
hw/virtio/vhost.c +24 -14
index 1a1384e7a642..0b83d6b8e65e 100644
@@ -106,6 +106,20 @@ static void vhost_dev_sync_region(struct vhost_dev *dev,
     }
 }
 
+static int vhost_dev_has_iommu(struct vhost_dev *dev)
+{
+    VirtIODevice *vdev = dev->vdev;
+
+    /*
+     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+}
+
 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                                    MemoryRegionSection *section,
                                    hwaddr first,
@@ -130,6 +144,11 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
                               range_get_last(reg->guest_phys_addr,
                                              reg->memory_size));
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return 0;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -172,6 +191,11 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)
                                        reg->memory_size);
         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
     }
+
+    if (vhost_dev_has_iommu(dev)) {
+        return log_size;
+    }
+
     for (i = 0; i < dev->nvqs; ++i) {
         struct vhost_virtqueue *vq = dev->vqs + i;
 
@@ -287,20 +311,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
     dev->log_size = size;
 }
 
-static int vhost_dev_has_iommu(struct vhost_dev *dev)
-{
-    VirtIODevice *vdev = dev->vdev;
-
-    /*
-     * 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_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
-}
-
 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
                               hwaddr *plen, bool is_write)
 {