vhost: fix vq dirt bitmap syncing when vIOMMU is enabled
PATCH 1/1 vhost: fix vq dirt bitmap syncing when vIOMMU is enabled
When vIOMMU is enabled, the vq->used_phys is actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.
--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)
hw/virtio/vhost.c +45 -20
@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +} +static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;int i;hwaddr start_addr;hwaddr end_addr;
@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ; + } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size); + +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size)); + used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }
@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false; - } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) {
Signature · 1 line
2.25.1
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.
Noted how exactly? What does "using 1G memory" mean?
Quoted 32 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +14
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else {
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 10 lines
hw/virtio/vhost.c +5
-0+return false; + } +} +static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;
why don't we move this inside the scope where it's used?
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 9 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;
these two, too.
Quoted 7 lines
hw/virtio/vhost.c +0
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size));
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 170
Quoted 10 lines
hw/virtio/vhost.c +10
-0+if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 174
Quoted 4 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);
Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);
accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);
Also, it bothers me that the math here will give you 0 if addr_mask is all ones. Then MIN will give 0 too and we loop forever. I think this can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 177
Quoted 3 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));
why are you syncing used_size here? Shouldn't it be s?
Quoted 37 lines
hw/virtio/vhost.c +7
-18+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false; - } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) {Signature · 1 line
2.25.1
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64: ../hw/virtio/vhost.c:85: vhost_dev_sync_region: Assertion `end / VHOST_LOG_CHUNK < dev->log_size' failed.
On the last time vhost_get_log_size() is called it takes into account 2 regions when computing the log_size:
qemu-system-x86_64: vhost_get_log_size region 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64: vhost_get_log_size region 1 last=0x3fffffff updated log_size=0x1000
so in vhost_migration_log() vhost_get_log_size(dev) returns 0x1000
In the test case, memory_region_sync_dirty_bitmap() gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:
qemu-system-x86_64: vhost_log_sync calls sync_dirty_map on pc.bios for the full range
qemu-system-x86_64: vhost_sync_dirty_bitmap calls vhost_dev_sync_region on region 0
qemu-system-x86_64: vhost_dev_sync_region end=0x9ffff < start=0xfffc0000
qemu-system-x86_64: vhost_sync_dirty_bitmap calls vhost_dev_sync_region on region 1
qemu-system-x86_64: vhost_dev_sync_region end=0x3fffffff < start=0xfffc0000
qemu-system-x86_64: vhost_sync_dirty_bitmap calls vhost_dev_sync_region on vq 0 <-----
qemu-system-x86_64: vhost_dev_sync_region pc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64: vhost_dev_sync_region pc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64: ../hw/virtio/vhost.c:85: vhost_dev_sync_region: Assertion `end / VHOST_LOG_CHUNK < dev->log_size' failed.
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Quoted 145 lines
Quoted 33 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +14
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else {hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 10 lines
hw/virtio/vhost.c +5
-0+return false; + } +} +static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 9 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Quoted 7 lines
hw/virtio/vhost.c +0
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size));hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 170
Quoted 10 lines
hw/virtio/vhost.c +10
-0+if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 174
Quoted 4 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Also, it bothers me that the math here will give you 0 if
addr_maskis all ones. Then MIN will give 0 too and we loop forever. I think this can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 177
Quoted 3 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?Quoted 29 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;
Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-3- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) {Signature · 1 line
2.25.1
Thanks
Eric
Quoted 35 lines · Eric Auger
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64:
../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.On the last time
vhost_get_log_size()is called it takes into account 2 regions when computing thelog_size:
qemu-system-x86_64:vhost_get_log_sizeregion 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64:vhost_get_log_sizeregion 1 last=0x3fffffff updated log_size=0x1000
so invhost_migration_log()vhost_get_log_size(dev)returns0x1000In the test case,
memory_region_sync_dirty_bitmap()gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:qemu-system-x86_64:
vhost_log_synccallssync_dirty_mapon pc.bios for the full range
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 0
qemu-system-x86_64:vhost_dev_sync_regionend=0x9ffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 1
qemu-system-x86_64:vhost_dev_sync_regionend=0x3fffffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon vq 0 <-----
qemu-system-x86_64:vhost_dev_sync_regionpc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64:vhost_dev_sync_regionpc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64:../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.
Quoted 1 line
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Yes, so in the case the guest iova allocator may try to use an IOVA that is beyond 1G.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +17
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +}
Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 7 lines
hw/virtio/vhost.c +2
-0+static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
That's fine.
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 10 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Right.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;
Quoted 4 lines
Quoted 4 lines
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...
Ok.
Quoted 23 lines
Quoted 23 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 174
Quoted 6 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Also, it bothers me that the math here will give you 0 if
addr_maskis all ones. Then MIN will give 0 too and we loop forever. I think this can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
Quoted 6 lines
Quoted 6 lines
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;
Right.
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 177
Quoted 6 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?
Let me fix this.
Thanks
Quoted 45 lines
Quoted 32 lines
Quoted 32 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-4- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) { --2.25.1
Thanks
Eric
Quoted 123 lines · Jason Wang
Quoted 35 lines · Eric Auger
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64:
../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.On the last time
vhost_get_log_size()is called it takes into account 2 regions when computing thelog_size:
qemu-system-x86_64:vhost_get_log_sizeregion 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64:vhost_get_log_sizeregion 1 last=0x3fffffff updated log_size=0x1000
so invhost_migration_log()vhost_get_log_size(dev)returns0x1000In the test case,
memory_region_sync_dirty_bitmap()gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:qemu-system-x86_64:
vhost_log_synccallssync_dirty_mapon pc.bios for the full range
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 0
qemu-system-x86_64:vhost_dev_sync_regionend=0x9ffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 1
qemu-system-x86_64:vhost_dev_sync_regionend=0x3fffffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon vq 0 <-----
qemu-system-x86_64:vhost_dev_sync_regionpc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64:vhost_dev_sync_regionpc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64:../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.Quoted 1 line
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Yes, so in the case the guest iova allocator may try to use an IOVA that is beyond 1G.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +17
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +}Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 7 lines
hw/virtio/vhost.c +2
-0+static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
That's fine.
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 10 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Right.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;
Quoted 27 lines
Quoted 4 lines
Quoted 4 lines
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...Ok.
Quoted 20 lines
Quoted 20 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 174
Quoted 6 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Also, it bothers me that the math here will give you 0 if
addr_maskis all ones.
So even if addr_mask is all ones, we end up with s = MIN(1, used_size) which should be fine. The used_size has been validated before to be non-zero.
Thanks
Quoted 70 lines
Then MIN will give 0 too and we loop forever. I think this
Quoted 9 lines
Quoted 9 lines
can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;Right.
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 177
Quoted 6 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?Let me fix this.
Thanks
Quoted 45 lines
Quoted 32 lines
Quoted 32 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-4- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) { --2.25.1
Thanks
Eric
Quoted 149 lines · Jason Wang
Quoted 148 lines · Jason Wang
Quoted 35 lines · Eric Auger
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64:
../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.On the last time
vhost_get_log_size()is called it takes into account 2 regions when computing thelog_size:
qemu-system-x86_64:vhost_get_log_sizeregion 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64:vhost_get_log_sizeregion 1 last=0x3fffffff updated log_size=0x1000
so invhost_migration_log()vhost_get_log_size(dev)returns0x1000In the test case,
memory_region_sync_dirty_bitmap()gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:qemu-system-x86_64:
vhost_log_synccallssync_dirty_mapon pc.bios for the full range
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 0
qemu-system-x86_64:vhost_dev_sync_regionend=0x9ffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 1
qemu-system-x86_64:vhost_dev_sync_regionend=0x3fffffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon vq 0 <-----
qemu-system-x86_64:vhost_dev_sync_regionpc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64:vhost_dev_sync_regionpc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64:../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.Quoted 1 line
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Yes, so in the case the guest iova allocator may try to use an IOVA that is beyond 1G.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +17
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +}Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 7 lines
hw/virtio/vhost.c +2
-0+static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
That's fine.
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 10 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Right.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;Quoted 4 lines
Quoted 4 lines
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...Ok.
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 174
Quoted 6 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Also, it bothers me that the math here will give you 0 if
addr_maskis all ones.So even if
addr_maskis all ones, we end up with s =MIN(1, used_size)which should be fine.
How do you figure? addr_mask is all ones, addr_mask + 1 is 0, we get MIN(0, used_size).
Quoted 75 lines
The
used_sizehas been validated before to be
non-zero.Thanks
Quoted 70 lines
Then MIN will give 0 too and we loop forever. I think this
Quoted 9 lines
Quoted 9 lines
can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;Right.
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 177
Quoted 6 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?Let me fix this.
Thanks
Quoted 45 lines
Quoted 32 lines
Quoted 32 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-4- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) { --2.25.1
Thanks
Eric
Quoted 151 lines · Michael S. Tsirkin
Quoted 149 lines · Jason Wang
Quoted 148 lines · Jason Wang
Quoted 35 lines · Eric Auger
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64:
../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.On the last time
vhost_get_log_size()is called it takes into account 2 regions when computing thelog_size:
qemu-system-x86_64:vhost_get_log_sizeregion 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64:vhost_get_log_sizeregion 1 last=0x3fffffff updated log_size=0x1000
so invhost_migration_log()vhost_get_log_size(dev)returns0x1000In the test case,
memory_region_sync_dirty_bitmap()gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:qemu-system-x86_64:
vhost_log_synccallssync_dirty_mapon pc.bios for the full range
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 0
qemu-system-x86_64:vhost_dev_sync_regionend=0x9ffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 1
qemu-system-x86_64:vhost_dev_sync_regionend=0x3fffffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon vq 0 <-----
qemu-system-x86_64:vhost_dev_sync_regionpc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64:vhost_dev_sync_regionpc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64:../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.Quoted 1 line
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Yes, so in the case the guest iova allocator may try to use an IOVA that is beyond 1G.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +17
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +}Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 7 lines
hw/virtio/vhost.c +2
-0+static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
That's fine.
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 10 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Right.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;Quoted 4 lines
Quoted 4 lines
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...Ok.
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 174
Quoted 6 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Also, it bothers me that the math here will give you 0 if
addr_maskis all ones.So even if
addr_maskis all ones, we end up with s =MIN(1, used_size)which should be fine.How do you figure?
addr_maskis all ones,addr_mask+ 1 is 0, we getMIN(0, used_size).
But we have a substration after that. In order to get a zero result, used_phys must be zero then used_phys & iotlb.addr_mask can be zero, or anything I missed here?
The 1 is calculated via assuming used_phys are all ones.
Thanks
Quoted 77 lines
Quoted 76 lines
The
used_sizehas been validated before to be
non-zero.Thanks
Quoted 70 lines
Then MIN will give 0 too and we loop forever. I think this
Quoted 9 lines
Quoted 9 lines
can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;Right.
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 177
Quoted 6 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?Let me fix this.
Thanks
Quoted 45 lines
Quoted 32 lines
Quoted 32 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-4- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) { --2.25.1
Thanks
Eric
Quoted 155 lines · Jason Wang
Quoted 154 lines · Michael S. Tsirkin
Quoted 152 lines · Jason Wang
Quoted 150 lines · Jason Wang
Quoted 35 lines · Eric Auger
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64:
../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.On the last time
vhost_get_log_size()is called it takes into account 2 regions when computing thelog_size:
qemu-system-x86_64:vhost_get_log_sizeregion 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64:vhost_get_log_sizeregion 1 last=0x3fffffff updated log_size=0x1000
so invhost_migration_log()vhost_get_log_size(dev)returns0x1000In the test case,
memory_region_sync_dirty_bitmap()gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:qemu-system-x86_64:
vhost_log_synccallssync_dirty_mapon pc.bios for the full range
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 0
qemu-system-x86_64:vhost_dev_sync_regionend=0x9ffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 1
qemu-system-x86_64:vhost_dev_sync_regionend=0x3fffffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon vq 0 <-----
qemu-system-x86_64:vhost_dev_sync_regionpc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64:vhost_dev_sync_regionpc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64:../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.Quoted 1 line
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Yes, so in the case the guest iova allocator may try to use an IOVA that is beyond 1G.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +17
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +}Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 7 lines
hw/virtio/vhost.c +2
-0+static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
That's fine.
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 10 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Right.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;Quoted 4 lines
Quoted 4 lines
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...Ok.
Quoted 20 lines
Quoted 20 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 173
Quoted 6 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Also, it bothers me that the math here will give you 0 if
addr_maskis all ones.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So even if
addr_maskis all ones, we end up with s =MIN(1, used_size)which should be fine.How do you figure?
addr_maskis all ones,addr_mask+ 1 is 0, we getMIN(0, used_size).But we have a substration after that.
I don't get it. This is your code:
s = MIN(iotlb.addr_mask + 1, used_size);
if addr_mask is all ones s becomes 0. Then while loop will loop forever.
Quoted 7 lines
In order to get a zero result,
used_physmust be zero thenused_phys& iotlb.addr_mask can be zero, or anything I missed here?The 1 is calculated via assuming
used_physare all ones.Thanks
Not used_phys, I'm talking about addr_mask being all ones.
Quoted 78 lines
Quoted 78 lines
Quoted 77 lines
The
used_sizehas been validated before to be
non-zero.Thanks
Quoted 70 lines
Then MIN will give 0 too and we loop forever. I think this
Quoted 9 lines
Quoted 9 lines
can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;Right.
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 176
Quoted 6 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?Let me fix this.
Thanks
Quoted 45 lines
Quoted 32 lines
Quoted 32 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-4- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) { --2.25.1
Thanks
Eric
Quoted 162 lines · Michael S. Tsirkin
Quoted 160 lines · Jason Wang
Quoted 158 lines · Michael S. Tsirkin
Quoted 149 lines · Jason Wang
Quoted 148 lines · Jason Wang
Quoted 35 lines · Eric Auger
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64:
../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.On the last time
vhost_get_log_size()is called it takes into account 2 regions when computing thelog_size:
qemu-system-x86_64:vhost_get_log_sizeregion 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64:vhost_get_log_sizeregion 1 last=0x3fffffff updated log_size=0x1000
so invhost_migration_log()vhost_get_log_size(dev)returns0x1000In the test case,
memory_region_sync_dirty_bitmap()gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:qemu-system-x86_64:
vhost_log_synccallssync_dirty_mapon pc.bios for the full range
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 0
qemu-system-x86_64:vhost_dev_sync_regionend=0x9ffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 1
qemu-system-x86_64:vhost_dev_sync_regionend=0x3fffffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon vq 0 <-----
qemu-system-x86_64:vhost_dev_sync_regionpc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64:vhost_dev_sync_regionpc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64:../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.Quoted 1 line
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Yes, so in the case the guest iova allocator may try to use an IOVA that is beyond 1G.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +17
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +}Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 7 lines
hw/virtio/vhost.c +2
-0+static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
That's fine.
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 10 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Right.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;Quoted 4 lines
Quoted 4 lines
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...Ok.
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 173
Quoted 6 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Also, it bothers me that the math here will give you 0 if
addr_maskis all ones.So even if
addr_maskis all ones, we end up with s =MIN(1, used_size)which should be fine.How do you figure?
addr_maskis all ones,addr_mask+ 1 is 0, we getMIN(0, used_size).
Quoted 7 lines
Quoted 1 line
But we have a substration after that.
I don't get it. This is your code:
s = MIN(iotlb.addr_mask + 1, used_size);if
addr_maskis all ones s becomes 0. Then while loop will loop forever.
Ok, I think there's some misunderstanding here. I meant using:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);
Won't result in a zero for s. So there's no need to move the plus one outside the MIN.
Thanks
Quoted 91 lines
Quoted 8 lines
In order to get a zero result,
used_physmust be zero thenused_phys& iotlb.addr_mask can be zero, or anything I missed here?The 1 is calculated via assuming
used_physare all ones.Thanks
Not
used_phys, I'm talking aboutaddr_maskbeing all ones.Quoted 78 lines
Quoted 78 lines
Quoted 77 lines
The
used_sizehas been validated before to be
non-zero.Thanks
Quoted 70 lines
Then MIN will give 0 too and we loop forever. I think this
Quoted 9 lines
Quoted 9 lines
can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;Right.
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 176
Quoted 6 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?Let me fix this.
Thanks
Quoted 45 lines
Quoted 32 lines
Quoted 32 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-4- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) { --2.25.1
Thanks
Eric
Quoted 169 lines · Jason Wang
Quoted 168 lines · Michael S. Tsirkin
Quoted 155 lines · Jason Wang
Quoted 154 lines · Michael S. Tsirkin
Quoted 152 lines · Jason Wang
Quoted 150 lines · Jason Wang
Quoted 35 lines · Eric Auger
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64:
../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.On the last time
vhost_get_log_size()is called it takes into account 2 regions when computing thelog_size:
qemu-system-x86_64:vhost_get_log_sizeregion 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64:vhost_get_log_sizeregion 1 last=0x3fffffff updated log_size=0x1000
so invhost_migration_log()vhost_get_log_size(dev)returns0x1000In the test case,
memory_region_sync_dirty_bitmap()gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:qemu-system-x86_64:
vhost_log_synccallssync_dirty_mapon pc.bios for the full range
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 0
qemu-system-x86_64:vhost_dev_sync_regionend=0x9ffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 1
qemu-system-x86_64:vhost_dev_sync_regionend=0x3fffffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon vq 0 <-----
qemu-system-x86_64:vhost_dev_sync_regionpc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64:vhost_dev_sync_regionpc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64:../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.Quoted 1 line
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Yes, so in the case the guest iova allocator may try to use an IOVA that is beyond 1G.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +17
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +}Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 7 lines
hw/virtio/vhost.c +2
-0+static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
That's fine.
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 10 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Right.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;Quoted 4 lines
Quoted 4 lines
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...Ok.
Quoted 20 lines
Quoted 20 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 173
Quoted 6 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Also, it bothers me that the math here will give you 0 if
addr_maskis all ones.Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So even if
addr_maskis all ones, we end up with s =MIN(1, used_size)which should be fine.How do you figure?
addr_maskis all ones,addr_mask+ 1 is 0, we getMIN(0, used_size).But we have a substration after that.
I don't get it. This is your code:
s = MIN(iotlb.addr_mask + 1, used_size);
Quoted 7 lines
Quoted 1 line
if
addr_maskis all ones s becomes 0. Then while loop will loop forever.Ok, I think there's some misunderstanding here. I meant using:
s =
MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Won't result in a zero for s.
If used_phys is page aligned then I think it will.
Quoted 4 lines
So there's no need to move the plus one
outside the MIN.Thanks
I mean why don't you try?
[mst@tuck ~]$ cat > a.c
#include <stdio.h>
#define MIN(a,b) (a<b ? a : b)
int main(int argc, char **argv)
{
unsigned mask = 0xffffffff;
unsigned s = MIN(mask + 1, 100);
printf("s=0x%x\n",s);
}
[mst@tuck ~]$ gcc a.c
[mst@tuck ~]$ ./a.out
s=0x0
[mst@tuck ~]$
Quoted 91 lines
Quoted 91 lines
Quoted 8 lines
In order to get a zero result,
used_physmust be zero thenused_phys& iotlb.addr_mask can be zero, or anything I missed here?The 1 is calculated via assuming
used_physare all ones.Thanks
Not
used_phys, I'm talking aboutaddr_maskbeing all ones.Quoted 78 lines
Quoted 78 lines
Quoted 77 lines
The
used_sizehas been validated before to be
non-zero.Thanks
Quoted 70 lines
Then MIN will give 0 too and we loop forever. I think this
Quoted 9 lines
Quoted 9 lines
can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;Right.
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 176
Quoted 6 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?Let me fix this.
Thanks
Quoted 45 lines
Quoted 32 lines
Quoted 32 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-4- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) { --2.25.1
Thanks
Eric
Quoted 173 lines · Michael S. Tsirkin
Quoted 171 lines · Jason Wang
Quoted 169 lines · Michael S. Tsirkin
Quoted 155 lines · Jason Wang
Quoted 154 lines · Michael S. Tsirkin
Quoted 152 lines · Jason Wang
Quoted 150 lines · Jason Wang
Quoted 35 lines · Eric Auger
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64:
../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.On the last time
vhost_get_log_size()is called it takes into account 2 regions when computing thelog_size:
qemu-system-x86_64:vhost_get_log_sizeregion 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64:vhost_get_log_sizeregion 1 last=0x3fffffff updated log_size=0x1000
so invhost_migration_log()vhost_get_log_size(dev)returns0x1000In the test case,
memory_region_sync_dirty_bitmap()gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:qemu-system-x86_64:
vhost_log_synccallssync_dirty_mapon pc.bios for the full range
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 0
qemu-system-x86_64:vhost_dev_sync_regionend=0x9ffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 1
qemu-system-x86_64:vhost_dev_sync_regionend=0x3fffffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon vq 0 <-----
qemu-system-x86_64:vhost_dev_sync_regionpc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64:vhost_dev_sync_regionpc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64:../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.Quoted 1 line
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Yes, so in the case the guest iova allocator may try to use an IOVA that is beyond 1G.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +17
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +}Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 7 lines
hw/virtio/vhost.c +2
-0+static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
That's fine.
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 10 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Right.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;Quoted 4 lines
Quoted 4 lines
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...Ok.
Quoted 20 lines
Quoted 20 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 173
Quoted 6 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Also, it bothers me that the math here will give you 0 if
addr_maskis all ones.Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So even if
addr_maskis all ones, we end up with s =MIN(1, used_size)which should be fine.How do you figure?
addr_maskis all ones,addr_mask+ 1 is 0, we getMIN(0, used_size).But we have a substration after that.
I don't get it. This is your code:
s = MIN(iotlb.addr_mask + 1, used_size);if
addr_maskis all ones s becomes 0. Then while loop will loop forever.
Quoted 7 lines
Quoted 5 lines
Ok, I think there's some misunderstanding here. I meant using:
s =
MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Won't result in a zero for s.
If
used_physis page aligned then I think it will.
So iotlb.addr_mask + 1 is zero, used_phys & iotlb.addr_mask is used_phys:
# cat t.c #include <stdio.h>
int main(void)
{
unsigned int mask = 0xFFFFFFFF;
unsigned long long used_phys = 0x1000; unsigned long long s = mask + 1 - (used_phys & mask);
fprintf(stderr, "result is %llx\n", s);
return 0;
}
# gcc t.c # ./a.out result is fffffffffffff000
Quoted 7 lines
Quoted 5 lines
So there's no need to move the plus one
outside the MIN.Thanks
I mean why don't you try?
There should be some misunderstanding. I fully understand the MIN(mask+1, used_size) case. My comments were for
MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);
And used_phys can't be zero here.
Thanks
Quoted 113 lines
[mst@tuck ~]$ cat >
a.c
#include <stdio.h>
#defineMIN(a,b)(a<b ? a : b)int
main(int argc, char **argv){ unsigned mask = 0xffffffff; unsigned s = MIN(mask + 1, 100); printf("s=0x%x\n",s); }[mst@tuck ~]$ gcc
a.c
[mst@tuck ~]$ ./a.out
s=0x0
[mst@tuck ~]$Quoted 91 lines
Quoted 91 lines
Quoted 8 lines
In order to get a zero result,
used_physmust be zero thenused_phys& iotlb.addr_mask can be zero, or anything I missed here?The 1 is calculated via assuming
used_physare all ones.Thanks
Not
used_phys, I'm talking aboutaddr_maskbeing all ones.Quoted 78 lines
Quoted 78 lines
Quoted 77 lines
The
used_sizehas been validated before to be
non-zero.Thanks
Quoted 70 lines
Then MIN will give 0 too and we loop forever. I think this
Quoted 9 lines
Quoted 9 lines
can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;Right.
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 176
Quoted 6 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?Let me fix this.
Thanks
Quoted 45 lines
Quoted 32 lines
Quoted 32 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-4- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) { --2.25.1
Thanks
Eric
Quoted 193 lines · Jason Wang
Quoted 173 lines · Michael S. Tsirkin
Quoted 171 lines · Jason Wang
Quoted 169 lines · Michael S. Tsirkin
Quoted 155 lines · Jason Wang
Quoted 154 lines · Michael S. Tsirkin
Quoted 152 lines · Jason Wang
Quoted 150 lines · Jason Wang
Quoted 35 lines · Eric Auger
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64:
../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.On the last time
vhost_get_log_size()is called it takes into account 2 regions when computing thelog_size:
qemu-system-x86_64:vhost_get_log_sizeregion 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64:vhost_get_log_sizeregion 1 last=0x3fffffff updated log_size=0x1000
so invhost_migration_log()vhost_get_log_size(dev)returns0x1000In the test case,
memory_region_sync_dirty_bitmap()gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:qemu-system-x86_64:
vhost_log_synccallssync_dirty_mapon pc.bios for the full range
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 0
qemu-system-x86_64:vhost_dev_sync_regionend=0x9ffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 1
qemu-system-x86_64:vhost_dev_sync_regionend=0x3fffffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon vq 0 <-----
qemu-system-x86_64:vhost_dev_sync_regionpc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64:vhost_dev_sync_regionpc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64:../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.Quoted 1 line
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Yes, so in the case the guest iova allocator may try to use an IOVA that is beyond 1G.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +17
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +}Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 7 lines
hw/virtio/vhost.c +2
-0+static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
That's fine.
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 10 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Right.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;Quoted 4 lines
Quoted 4 lines
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...Ok.
Quoted 20 lines
Quoted 20 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 173
Quoted 6 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Also, it bothers me that the math here will give you 0 if
addr_maskis all ones.Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So even if
addr_maskis all ones, we end up with s =MIN(1, used_size)which should be fine.How do you figure?
addr_maskis all ones,addr_mask+ 1 is 0, we getMIN(0, used_size).But we have a substration after that.
I don't get it. This is your code:
s = MIN(iotlb.addr_mask + 1, used_size);if
addr_maskis all ones s becomes 0. Then while loop will loop forever.Quoted 7 lines
Quoted 5 lines
Ok, I think there's some misunderstanding here. I meant using:
s =
MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Won't result in a zero for s.
If
used_physis page aligned then I think it will.So iotlb.addr_mask + 1 is zero,
used_phys& iotlb.addr_mask isused_phys:# cat t.c #include <stdio.h>int main(void)
{
unsigned int mask =0xFFFFFFFF;
unsigned long longused_phys=0x1000; unsigned long long s = mask + 1 - (used_phys& mask);
Quoted 7 lines
fprintf(stderr, "result is %llx\n", s);return 0;
}# gcc t.c # ./a.out result is fffffffffffff000
Oh I see. So it will only trigger if used_phys is 0. Unlikely, but I'd prefer we don't rely on this, seems fragile.
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
So there's no need to move the plus one
outside the MIN.Thanks
I mean why don't you try?
Quoted 8 lines
There should be some misunderstanding. I fully understand the
MIN(mask+1, used_size)case. My comments were for
MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);And
used_physcan't be zero here.Thanks
Right. IMHO + 1 outside is more robust.
Quoted 113 lines
Quoted 113 lines
[mst@tuck ~]$ cat >
a.c
#include <stdio.h>
#defineMIN(a,b)(a<b ? a : b)int
main(int argc, char **argv){ unsigned mask = 0xffffffff; unsigned s = MIN(mask + 1, 100); printf("s=0x%x\n",s); }[mst@tuck ~]$ gcc
a.c
[mst@tuck ~]$ ./a.out
s=0x0
[mst@tuck ~]$Quoted 91 lines
Quoted 91 lines
Quoted 8 lines
In order to get a zero result,
used_physmust be zero thenused_phys& iotlb.addr_mask can be zero, or anything I missed here?The 1 is calculated via assuming
used_physare all ones.Thanks
Not
used_phys, I'm talking aboutaddr_maskbeing all ones.Quoted 78 lines
Quoted 78 lines
Quoted 77 lines
The
used_sizehas been validated before to be
non-zero.Thanks
Quoted 70 lines
Then MIN will give 0 too and we loop forever. I think this
Quoted 9 lines
Quoted 9 lines
can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;Right.
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 176
Quoted 6 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?Let me fix this.
Thanks
Quoted 45 lines
Quoted 32 lines
Quoted 32 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-4- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) { --2.25.1
Thanks
Eric
Quoted 197 lines · Michael S. Tsirkin
Quoted 195 lines · Jason Wang
Quoted 173 lines · Michael S. Tsirkin
Quoted 171 lines · Jason Wang
Quoted 169 lines · Michael S. Tsirkin
Quoted 155 lines · Jason Wang
Quoted 154 lines · Michael S. Tsirkin
Quoted 152 lines · Jason Wang
Quoted 150 lines · Jason Wang
Quoted 35 lines · Eric Auger
Hi,
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang
When vIOMMU is enabled, the
vq->used_physis actually the IOVA not GPA. So we need to translate it to GPA before the syncing otherwise we may hit the following crash since IOVA could be out of the scope of the GPA log size. This could be noted when using virtio-IOMMU with vhost using 1G memory.Noted how exactly? What does "using 1G memory" mean?
We hit the following assertion:
qemu-system-x86_64:
../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.On the last time
vhost_get_log_size()is called it takes into account 2 regions when computing thelog_size:
qemu-system-x86_64:vhost_get_log_sizeregion 0 last=0x9ffff updated log_size=0x3
qemu-system-x86_64:vhost_get_log_sizeregion 1 last=0x3fffffff updated log_size=0x1000
so invhost_migration_log()vhost_get_log_size(dev)returns0x1000In the test case,
memory_region_sync_dirty_bitmap()gets called for mem-machine_mem, vga.vram (several times) and eventually on pc.bios. This latter is reponsible for the assertion:qemu-system-x86_64:
vhost_log_synccallssync_dirty_mapon pc.bios for the full range
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 0
qemu-system-x86_64:vhost_dev_sync_regionend=0x9ffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon region 1
qemu-system-x86_64:vhost_dev_sync_regionend=0x3fffffff < start=0xfffc0000
qemu-system-x86_64:vhost_sync_dirty_bitmapcallsvhost_dev_sync_regionon vq 0 <-----
qemu-system-x86_64:vhost_dev_sync_regionpc.bios mfirst=0xfffc0000 mlast=0xffffffff rfirst=0xfffff240 rlast=0xfffffa45
qemu-system-x86_64:vhost_dev_sync_regionpc.bios end=0xfffffa45 VHOST_LOG_CHUNK=0x40000 end/VHOST_LOG_CHUNK=0x3fff dev->log_size=0x1000
qemu-system-x86_64:../hw/virtio/vhost.c:85:vhost_dev_sync_region: Assertion `end /VHOST_LOG_CHUNK<dev->log_size' failed.Quoted 1 line
"using 1G memory": We hit the issue with a guest started with 1GB initial RAM.
Yes, so in the case the guest iova allocator may try to use an IOVA that is beyond 1G.
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Fixes c471ad0e9bd46 ("vhost_net: device IOTLB support")Reported-by Yalan Zhang <[email protected]>Tested-by Eric Auger <[email protected]>Tested-by Lei Yang <[email protected]>Signed-off-by Jason Wang <[email protected]>--- hw/virtio/vhost.c | 65 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 20 deletions(-)hw/virtio/vhost.c +17
-0@@ -106,11 +106,30 @@ static void vhost_dev_sync_region(struct vhost_dev *dev, } } +static bool 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 transactions. +*/ +if (vdev) { +return virtio_bus_device_iommu_enabled (vdev) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); + }else { +return false; + } +}Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c static void vhost_dev_sync_region(struct vhost_dev *dev, · line 132
Quoted 7 lines
hw/virtio/vhost.c +2
-0+static int vhost_sync_dirty_bitmap (struct vhost_dev *dev,MemoryRegionSection *section,hwaddr first,hwaddr last) { +IOMMUTLBEntry iotlb;why don't we move this inside the scope where it's used?
That's fine.
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 155
Quoted 10 lines
hw/virtio/vhost.c +2
-0int i;hwaddr start_addr;hwaddr end_addr;@@ -132,13 +151,37 @@ static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, }for (i =0 ; i < dev->nvqs; ++i) {struct vhost_virtqueue *vq = dev->vqs + i; +hwaddr used_phys = vq->used_phys, used_size = vq->used_size; +hwaddr phys, s;these two, too.
Right.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10
-2if (!vq->used_phys && !vq->used_size) {continue ; } -vhost_dev_sync_region (dev, section, start_addr, end_addr, vq->used_phys, -range_get_last (vq->used_phys, vq->used_size)); +if (vhost_dev_has_iommu (dev)) { +while (used_size) { +rcu_read_lock (); + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + used_phys, + true,MEMTXATTRS_UNSPECIFIED ); +rcu_read_unlock (); + +if (iotlb.target_as ==NULL ) { +return -EINVAL ;Quoted 4 lines
Quoted 4 lines
I am not sure how this can trigger. I don't like == NULL:
!iotlb.target_as is more succint. But a bigger question is how to handle this. callers ignore the return value so maybe log guest error? iommu seems misconfigured ...Ok.
Quoted 20 lines
Quoted 20 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 173
Quoted 6 lines
hw/virtio/vhost.c +4
-0+ } + + phys = iotlb.translated_addr; + s =MIN (iotlb.addr_mask +1 , used_size);Note, that iotlb.translated_addr here is an aligned address and iotlb.addr_mask + 1 is size from there.
So I think phys that you want is actually
phys = iotlb.translated_addr + (used_phys & iotlb.addr_mask);accordingly, the size would be from there until end of mask:
s = MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Also, it bothers me that the math here will give you 0 if
addr_maskis all ones.Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So even if
addr_maskis all ones, we end up with s =MIN(1, used_size)which should be fine.How do you figure?
addr_maskis all ones,addr_mask+ 1 is 0, we getMIN(0, used_size).But we have a substration after that.
I don't get it. This is your code:
s = MIN(iotlb.addr_mask + 1, used_size);if
addr_maskis all ones s becomes 0. Then while loop will loop forever.Quoted 7 lines
Quoted 5 lines
Ok, I think there's some misunderstanding here. I meant using:
s =
MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);Won't result in a zero for s.
If
used_physis page aligned then I think it will.So iotlb.addr_mask + 1 is zero,
used_phys& iotlb.addr_mask isused_phys:# cat t.c #include <stdio.h>int main(void)
{
unsigned int mask =0xFFFFFFFF;
unsigned long longused_phys=0x1000; unsigned long long s = mask + 1 - (used_phys& mask);
fprintf(stderr, "result is %llx\n", s);
Quoted 8 lines
Quoted 5 lines
return 0;
}# gcc t.c # ./a.out result is fffffffffffff000Oh I see. So it will only trigger if
used_physis 0. Unlikely, but I'd prefer we don't rely on this, seems fragile.
Ok.
Quoted 13 lines
Quoted 13 lines
Quoted 9 lines
Quoted 7 lines
So there's no need to move the plus one
outside the MIN.Thanks
I mean why don't you try?
There should be some misunderstanding. I fully understand the
MIN(mask+1, used_size)case. My comments were for
Quoted 7 lines
Quoted 5 lines
MIN(iotlb.addr_mask + 1 - (used_phys & iotlb.addr_mask), used_size);And
used_physcan't be zero here.Thanks
Right. IMHO + 1 outside is more robust.
Ok. Will do.
Thanks
Quoted 115 lines
Quoted 114 lines
Quoted 114 lines
[mst@tuck ~]$ cat >
a.c
#include <stdio.h>
#defineMIN(a,b)(a<b ? a : b)int
main(int argc, char **argv){ unsigned mask = 0xffffffff; unsigned s = MIN(mask + 1, 100); printf("s=0x%x\n",s); }[mst@tuck ~]$ gcc
a.c
[mst@tuck ~]$ ./a.out
s=0x0
[mst@tuck ~]$Quoted 91 lines
Quoted 91 lines
Quoted 8 lines
In order to get a zero result,
used_physmust be zero thenused_phys& iotlb.addr_mask can be zero, or anything I missed here?The 1 is calculated via assuming
used_physare all ones.Thanks
Not
used_phys, I'm talking aboutaddr_maskbeing all ones.Quoted 78 lines
Quoted 78 lines
Quoted 77 lines
The
used_sizehas been validated before to be
non-zero.Thanks
Quoted 70 lines
Then MIN will give 0 too and we loop forever. I think this
Quoted 9 lines
Quoted 9 lines
can not trigger, but I'd rather we play it safe and add outside of MIN after it's capped to a reasonable value. So we end up with:
/* Distance from start of used ring until last byte of IOMMU page */ s = iotlb.addr_mask - (used_phys & iotlb.addr_mask); /* size of used ring, or of the part of it until end of IOMMU page */ s = MIN(s, used_size - 1) + 1;Right.
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost.c static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, · line 176
Quoted 6 lines
hw/virtio/vhost.c +3
-0+ +vhost_dev_sync_region (dev, section, start_addr, end_addr, phys, +range_get_last (phys, used_size));why are you syncing
used_sizehere? Shouldn't it be s?Let me fix this.
Thanks
Quoted 45 lines
Quoted 32 lines
Quoted 32 lines
hw/virtio/vhost.c +7
-15+ used_size -= s; + used_phys += s; + } + }else { +vhost_dev_sync_region (dev, section, start_addr, end_addr, used_phys, +range_get_last (used_phys, used_size)); + } }return 0 ; }@@ -306,24 +349,6 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } -static bool 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 transactions. -*/ -if (vdev) { -return virtio_bus_device_iommu_enabled (vdev) && -virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); - }else { -return false;Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost.c +0
-4- } -} -static void *vhost_memory_map (struct vhost_dev *dev,hwaddr addr,hwaddr *plen,bool is_write) { --2.25.1
Thanks
Eric