vhost: fix async address mapping
v2 cover vhost: fix async address mapping
From: Xuan Ding <[email protected]>
This patchset fixes the issue of incorrect DMA mapping in PA mode. Due to the ambiguity of host_phys_addr naming in the guest page struct, rename it to host_iova.
v2:
* Change the order of patch.
Xuan Ding (2):
vhost: rename field in guest page struct
vhost: fix physical address mapping
lib/vhost/vhost.h | 11 ++-- lib/vhost/vhost_user.c | 130 ++++++++++++++++++++--------------------- lib/vhost/virtio_net.c | 11 ++-- 3 files changed, 75 insertions(+), 77 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/2 vhost: rename field in guest page struct
From: Xuan Ding <[email protected]>
This patch renames the host_phys_addr to host_iova in guest_page struct. The host_phys_addr is iova, it depends on the DPDK IOVA mode.
--- lib/vhost/vhost.h | 10 +++++----- lib/vhost/vhost_user.c | 20 ++++++++++---------- lib/vhost/virtio_net.c | 11 ++++++----- 3 files changed, 21 insertions(+), 20 deletions(-)
lib/vhost/vhost.h +5 -5
@@ -354,7 +354,7 @@ struct vring_packed_desc_event {struct guest_page {uint64_t guest_phys_addr; -uint64_t host_phys_addr; +uint64_t host_iova;uint64_t size; };
@@ -604,13 +604,13 @@ gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa,if (gpa + gpa_size <= page->guest_phys_addr + page->size) {return gpa - page->guest_phys_addr + - page->host_phys_addr; + page->host_iova; }else if (gpa < page->guest_phys_addr + page->size) { *hpa_size = page->guest_phys_addr + page->size - gpa;return gpa - page->guest_phys_addr + - page->host_phys_addr; + page->host_iova; } } }else {
@@ -621,13 +621,13 @@ gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa,if (gpa + gpa_size <= page->guest_phys_addr + page->size) {return gpa - page->guest_phys_addr + - page->host_phys_addr; + page->host_iova; }else if (gpa < page->guest_phys_addr + page->size) { *hpa_size = page->guest_phys_addr + page->size - gpa;return gpa - page->guest_phys_addr + - page->host_phys_addr; + page->host_iova; } } }
lib/vhost/vhost_user.c +10 -10
@@ -978,7 +978,7 @@ vhost_user_set_vring_base(struct virtio_net **pdev,static int add_one_guest_page (struct virtio_net *dev,uint64_t guest_phys_addr, -uint64_t host_phys_addr,uint64_t size) +uint64_t host_iova,uint64_t size) {struct guest_page *page, *last_page;struct guest_page *old_pages;
@@ -999,7 +999,7 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,if (dev->nr_guest_pages >0 ) { last_page = &dev->guest_pages[dev->nr_guest_pages -1 ];/* merge if the two pages are continuous */ -if (host_phys_addr == last_page->host_phys_addr + +if (host_iova == last_page->host_iova + last_page->size) { last_page->size += size;return 0 ;
@@ -1008,7 +1008,7 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, page = &dev->guest_pages[dev->nr_guest_pages++]; page->guest_phys_addr = guest_phys_addr; - page->host_phys_addr = host_phys_addr; + page->host_iova = host_iova; page->size = size;return 0 ;
@@ -1021,14 +1021,14 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,uint64_t reg_size = reg->size;uint64_t host_user_addr = reg->host_user_addr;uint64_t guest_phys_addr = reg->guest_phys_addr; -uint64_t host_phys_addr; +uint64_t host_iova;uint64_t size; - host_phys_addr =rte_mem_virt2iova ((void *)(uintptr_t )host_user_addr); + host_iova =rte_mem_virt2iova ((void *)(uintptr_t )host_user_addr); size = page_size - (guest_phys_addr & (page_size -1 )); size =RTE_MIN (size, reg_size); -if (add_one_guest_page (dev, guest_phys_addr, host_phys_addr, size) <0 ) +if (add_one_guest_page (dev, guest_phys_addr, host_iova, size) <0 )return -1 ; host_user_addr += size;
@@ -1037,9 +1037,9 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,while (reg_size >0 ) { size =RTE_MIN (reg_size, page_size); - host_phys_addr =rte_mem_virt2iova ((void *)(uintptr_t ) + host_iova =rte_mem_virt2iova ((void *)(uintptr_t ) host_user_addr); -if (add_one_guest_page (dev, guest_phys_addr, host_phys_addr, +if (add_one_guest_page (dev, guest_phys_addr, host_iova, size) <0 )return -1 ;
@@ -1071,11 +1071,11 @@ dump_guest_pages(struct virtio_net *dev)VHOST_LOG_CONFIG (INFO ,"guest physical page region %u\n" "\t guest_phys_addr: %" PRIx64"\n" -"\t host_phys_addr : %" PRIx64"\n" +"\t host_iova : %" PRIx64"\n" "\t size : %" PRIx64"\n" , i, page->guest_phys_addr, - page->host_phys_addr, + page->host_iova, page->size); } }
lib/vhost/virtio_net.c +6 -5
@@ -870,20 +870,21 @@ async_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,struct vhost_async *async = vq->async;uint64_t mapped_len;uint32_t buf_offset =0 ; -void *hpa; +void *host_iova;while (cpy_len) { - hpa = (void *)(uintptr_t )gpa_to_first_hpa (dev, + host_iova = (void *)(uintptr_t )gpa_to_first_hpa (dev, buf_iova + buf_offset, cpy_len, &mapped_len); -if (unlikely (!hpa)) { -VHOST_LOG_DATA (ERR ,"(%d) %s: failed to get hpa.\n" , dev->vid, __func__); +if (unlikely (!host_iova)) { +VHOST_LOG_DATA (ERR ,"(%d) %s: failed to get host iova.\n" , + dev->vid, __func__);return -1 ; }if (unlikely (async_iter_add_iovec (async, (void *)(uintptr_t )rte_pktmbuf_iova_offset (m, mbuf_offset), - hpa, (size_t )mapped_len))) + host_iova, (size_t )mapped_len)))return -1 ; cpy_len -= (uint32_t )mapped_len;
Signature · 1 line
2.17.1
Re: [PATCH v2 1/2] vhost: rename field in guest page struct
Quoted 6 lines · On 1/19/22 16:10, [email protected] wrote:
From: Xuan Ding <[email protected]>
This patch renames the
host_phys_addrtohost_iovainguest_pagestruct. Thehost_phys_addris iova, it depends on the DPDK IOVA mode.
Quoted 7 lines
Signed-off-by Xuan Ding <[email protected]>--- lib/vhost/vhost.h | 10 +++++----- lib/vhost/vhost_user.c | 20 ++++++++++---------- lib/vhost/virtio_net.c | 11 ++++++----- 3 files changed, 21 insertions(+), 20 deletions(-)
Thanks,
Maxime
PATCH v2 2/2 vhost: fix physical address mapping
In reply to [email protected]From: Xuan Ding <[email protected]>
When choosing IOVA as PA mode, IOVA is likely to be discontinuous, which requires page by page mapping for DMA devices. To be consistent, this patch implements page by page mapping instead of mapping at the region granularity for both IOVA as VA and PA mode.
--- lib/vhost/vhost.h | 1 + lib/vhost/vhost_user.c | 116 ++++++++++++++++++++--------------------- 2 files changed, 57 insertions(+), 60 deletions(-)
lib/vhost/vhost.h +1 -0
@@ -355,6 +355,7 @@ struct vring_packed_desc_event {struct guest_page {uint64_t guest_phys_addr;uint64_t host_iova; +uint64_t host_user_addr;uint64_t size; };
lib/vhost/vhost_user.c +56 -60
187 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v2 2/2] vhost: fix physical address mapping
Quoted 37 lines · On 1/19/22 16:10, [email protected] wrote:
From: Xuan Ding <[email protected]>
When choosing IOVA as PA mode, IOVA is likely to be discontinuous, which requires page by page mapping for DMA devices. To be consistent, this patch implements page by page mapping instead of mapping at the region granularity for both IOVA as VA and PA mode.
Fixes 7c61fa08b716 ("vhost: enable IOMMU for async vhost")Signed-off-by Xuan Ding <[email protected]>Signed-off-by Yuan Wang <[email protected]>--- lib/vhost/vhost.h | 1 + lib/vhost/vhost_user.c | 116 ++++++++++++++++++++--------------------- 2 files changed, 57 insertions(+), 60 deletions(-)lib/vhost/vhost.h +1
-0@@ -355,6 +355,7 @@ struct vring_packed_desc_event {struct guest_page {uint64_t guest_phys_addr;uint64_t host_iova; +uint64_t host_user_addr;uint64_t size; };lib/vhost/vhost_user.c +0
-1@@ -143,57 +143,56 @@ get_blk_size(int fd)return ret ==-1 ? (uint64_t )-1 : (uint64_t )stat.st_blksize; } -static int
lib/vhost/vhost_user.c get_blk_size(int fd) · line 151
Quoted 10 lines
lib/vhost/vhost_user.c +4
-4-async_dma_map(struct rte_vhost_mem_region *region, bool do_map) +static void +async_dma_map (struct virtio_net *dev,bool do_map) { - uint64_t host_iova;int ret =0 ; - - host_iova =rte_mem_virt2iova ((void *)(uintptr_t )region->host_user_addr); +uint32_t i; +struct guest_page *page;
Add new line.
Quoted 111 lines
lib/vhost/vhost_user.c +42
-42if (do_map) { -/* Add mapped region into the default container of DPDK. */ - ret =rte_vfio_container_dma_map (RTE_VFIO_DEFAULT_CONTAINER_FD , - region->host_user_addr, - host_iova, - region->size); -if (ret) { -/* -* DMA device may bind with kernel driver, in this case, -* we don't need to program IOMMU manually. However, if no -* device is bound with vfio/uio in DPDK, and vfio kernel -* module is loaded, the API will still be called and return -* with ENODEV/ENOSUP. -* -* DPDK vfio only returns ENODEV/ENOSUP in very similar -* situations(vfio either unsupported, or supported -* but no devices found). Either way, no mappings could be -* performed. We treat it as normal case in async path. -*/ -if (rte_errno ==ENODEV || rte_errno ==ENOTSUP ) -return 0 ; - -VHOST_LOG_CONFIG (ERR ,"DMA engine map failed\n" ); -/* DMA mapping errors won't stop VHST_USER_SET_MEM_TABLE. */ -return 0 ; +for (i =0 ; i < dev->nr_guest_pages; i++) { + page = &dev->guest_pages[i]; + ret =rte_vfio_container_dma_map (RTE_VFIO_DEFAULT_CONTAINER_FD , + page->host_user_addr, + page->host_iova, + page->size); +if (ret) { +/* +* DMA device may bind with kernel driver, in this case, +* we don't need to program IOMMU manually. However, if no +* device is bound with vfio/uio in DPDK, and vfio kernel +* module is loaded, the API will still be called and return +* with ENODEV. +* +* DPDK vfio only returns ENODEV in very similar situations +* (vfio either unsupported, or supported but no devices found). +* Either way, no mappings could be performed. We treat it as +* normal case in async path. This is a workaround. +*/ +if (rte_errno ==ENODEV ) +return ; + +/* DMA mapping errors won't stop VHOST_USER_SET_MEM_TABLE. */ +VHOST_LOG_CONFIG (ERR ,"DMA engine map failed\n" ); + } } }else { -/* Remove mapped region from the default container of DPDK. */ - ret =rte_vfio_container_dma_unmap (RTE_VFIO_DEFAULT_CONTAINER_FD , - region->host_user_addr, - host_iova, - region->size); -if (ret) { -/* like DMA map, ignore the kernel driver case when unmap. */ -if (rte_errno ==EINVAL ) -return 0 ; - -VHOST_LOG_CONFIG (ERR ,"DMA engine unmap failed\n" ); -return ret; +for (i =0 ; i < dev->nr_guest_pages; i++) { + page = &dev->guest_pages[i]; + ret =rte_vfio_container_dma_unmap (RTE_VFIO_DEFAULT_CONTAINER_FD , + page->host_user_addr, + page->host_iova, + page->size); +if (ret) { +/* like DMA map, ignore the kernel driver case when unmap. */ +if (rte_errno ==EINVAL ) +return ; + +VHOST_LOG_CONFIG (ERR ,"DMA engine unmap failed\n" ); + } } } - -return ret; } static void@@ -205,12 +204,12 @@ free_mem_region(struct virtio_net *dev)if (!dev || !dev->mem)return ; +if (dev->async_copy &&rte_vfio_is_enabled ("vfio" )) +async_dma_map (dev, false); +for (i =0 ; i < dev->mem->nregions; i++) { reg = &dev->mem->regions[i];if (reg->host_user_addr) { -if (dev->async_copy &&rte_vfio_is_enabled ("vfio" )) -async_dma_map (reg, false); -munmap (reg->mmap_addr, reg->mmap_size);close (reg->fd); }@@ -978,7 +977,7 @@ vhost_user_set_vring_base(struct virtio_net **pdev,static int add_one_guest_page (struct virtio_net *dev,uint64_t guest_phys_addr, -uint64_t host_iova,uint64_t size) +uint64_t host_iova,uint64_t host_user_addr,uint64_t size) {struct guest_page *page, *last_page;struct guest_page *old_pages;
lib/vhost/vhost_user.c add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, · line 1003
Quoted 8 lines
lib/vhost/vhost_user.c +3
-2@@ -999,8 +998,9 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,if (dev->nr_guest_pages >0 ) { last_page = &dev->guest_pages[dev->nr_guest_pages -1 ];/* merge if the two pages are continuous */ -if (host_iova == last_page->host_iova + - last_page->size) { +if (host_iova == last_page->host_iova + last_page->size + && guest_phys_addr == last_page->guest_phys_addr + last_page->size + && host_user_addr == last_page->host_user_addr + last_page->size) {
Indentation looks wrong.
Quoted 54 lines
lib/vhost/vhost_user.c +4
-11last_page->size += size;return 0 ; }@@ -1009,6 +1009,7 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, page = &dev->guest_pages[dev->nr_guest_pages++]; page->guest_phys_addr = guest_phys_addr; page->host_iova = host_iova; + page->host_user_addr = host_user_addr; page->size = size;return 0 ;@@ -1028,7 +1029,8 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg, size = page_size - (guest_phys_addr & (page_size -1 )); size =RTE_MIN (size, reg_size); -if (add_one_guest_page (dev, guest_phys_addr, host_iova, size) <0 ) +if (add_one_guest_page (dev, guest_phys_addr, host_iova, + host_user_addr, size) <0 )return -1 ; host_user_addr += size;@@ -1040,7 +1042,7 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg, host_iova =rte_mem_virt2iova ((void *)(uintptr_t ) host_user_addr);if (add_one_guest_page (dev, guest_phys_addr, host_iova, - size) <0 ) + host_user_addr, size) <0 )return -1 ; host_user_addr += size;@@ -1215,7 +1217,6 @@ vhost_user_mmap_region(struct virtio_net *dev,uint64_t mmap_size;uint64_t alignment;int populate; -int ret;/* Check for memory_size + mmap_offset overflow */ if (mmap_offset >= -region->size) {@@ -1274,14 +1275,6 @@ vhost_user_mmap_region(struct virtio_net *dev,VHOST_LOG_CONFIG (ERR ,"adding guest pages to region failed.\n" );return -1 ; } - -if (rte_vfio_is_enabled ("vfio" )) { - ret =async_dma_map (region, true); -if (ret) { -VHOST_LOG_CONFIG (ERR ,"Configure IOMMU for DMA engine failed\n" ); -return -1 ; - } - } } VHOST_LOG_CONFIG(INFO,
lib/vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, · line 1421
Quoted 9 lines
lib/vhost/vhost_user.c +3
-0@@ -1420,6 +1413,9 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg, dev->mem->nregions++; } +if (dev->async_copy &&rte_vfio_is_enabled ("vfio" )) +async_dma_map (dev, true); +if (vhost_user_postcopy_register (dev, main_fd, msg) <0 ) goto free_mem_table;
Overall, the patch looks good.
Please fix the small nits & add Fixes tag and cc stable on patch 1.
Thanks,
Maxime
Re: [PATCH v2 2/2] vhost: fix physical address mapping
In reply to [email protected]Quoted 7 lines · On 1/19/22 16:10, [email protected] wrote:
From: Xuan Ding <[email protected]>
When choosing IOVA as PA mode, IOVA is likely to be discontinuous, which requires page by page mapping for DMA devices. To be consistent, this patch implements page by page mapping instead of mapping at the region granularity for both IOVA as VA and PA mode.
Quoted 9 lines
Fixes 7c61fa08b716 ("vhost: enable IOMMU for async vhost")Signed-off-by Xuan Ding <[email protected]>Signed-off-by Yuan Wang <[email protected]>--- lib/vhost/vhost.h | 1 + lib/vhost/vhost_user.c | 116 ++++++++++++++++++++--------------------- 2 files changed, 57 insertions(+), 60 deletions(-)
Thanks,
Maxime
Quoted 8 lines · On 1/19/22 16:10, [email protected] wrote:
From: Xuan Ding <[email protected]>
This patchset fixes the issue of incorrect DMA mapping in PA mode. Due to the ambiguity of
host_phys_addrnaming in the guest page struct, rename it tohost_iova.v2:
* Change the order of patch.
I'm not sure why you changed the order of the patches.
Now, the second one is the fix, so it will make the backport more
difficult. Either both are considered to be fixes. I think it can make sense as the renaming does not introduce risk of regression and will make backporting patches easier in the future.
Other solution is to reverse the order again, but I think tagging the renaming as a fix is OK for me here.
What do you think?
Regards,
Maxime
Quoted 10 lines
Xuan Ding (2):
vhost: rename field in guest page struct
vhost: fix physical address mappinglib/vhost/vhost.h | 11 ++-- lib/vhost/vhost_user.c | 130 ++++++++++++++++++++--------------------- lib/vhost/virtio_net.c | 11 ++-- 3 files changed, 75 insertions(+), 77 deletions(-)
Quoted 18 lines · Maxime Coquelin
Quoted 8 lines · On 1/19/22 16:10, [email protected] wrote:
From: Xuan Ding <[email protected]>
This patchset fixes the issue of incorrect DMA mapping in PA mode. Due to the ambiguity of
host_phys_addrnaming in the guest page struct, rename it tohost_iova.v2:
* Change the order of patch.I'm not sure why you changed the order of the patches.
Now, the second one is the fix, so it will make the backport more difficult. Either both are considered to be fixes. I think it can make sense as the renaming does not introduce risk of regression and will make backporting patches easier in the future.
Quoted 5 lines
Other solution is to reverse the order again, but I think tagging the renaming as a fix is OK for me here.
What do you think?
Either way sounds ok to me, but can you also add stable tag(s). There isn't a stable tag on either patch at present. Thanks.
Quoted 14 lines
Regards,
MaximeQuoted 10 lines
Xuan Ding (2):
vhost: rename field in guest page struct
vhost: fix physical address mappinglib/vhost/vhost.h | 11 ++-- lib/vhost/vhost_user.c | 130 ++++++++++++++++++++--------------------- lib/vhost/virtio_net.c | 11 ++-- 3 files changed, 75 insertions(+), 77 deletions(-)
Quoted 19 lines · Kevin Traynor
Quoted 18 lines · Maxime Coquelin
Quoted 8 lines · On 1/19/22 16:10, [email protected] wrote:
From: Xuan Ding <[email protected]>
This patchset fixes the issue of incorrect DMA mapping in PA mode. Due to the ambiguity of
host_phys_addrnaming in the guest page struct, rename it tohost_iova.v2:
* Change the order of patch.I'm not sure why you changed the order of the patches.
Now, the second one is the fix, so it will make the backport more difficult. Either both are considered to be fixes. I think it can make sense as the renaming does not introduce risk of regression and will make backporting patches easier in the future.
Quoted 7 lines
Quoted 5 lines
Other solution is to reverse the order again, but I think tagging the renaming as a fix is OK for me here.
What do you think?
Either way sounds ok to me, but can you also add stable tag(s). There isn't a stable tag on either patch at present. Thanks.
OK, will do.
Thanks Kevin,
Maxime
Quoted 16 lines
Quoted 15 lines
Regards,
MaximeQuoted 11 lines
Xuan Ding (2):
vhost: rename field in guest page struct
vhost: fix physical address mappinglib/vhost/vhost.h | 11 ++--
lib/vhost/vhost_user.c| 130
++++++++++++++++++++---------------------
lib/vhost/virtio_net.c| 11 ++--3 files changed, 75 insertions(+), 77 deletions(-)
Quoted 9 lines · On 1/19/22 16:10, [email protected] wrote:
From: Xuan Ding <[email protected]>
This patchset fixes the issue of incorrect DMA mapping in PA mode. Due to the ambiguity of
host_phys_addrnaming in the guest page struct, rename it tohost_iova.v2:
* Change the order of patch.
Quoted 9 lines
Xuan Ding (2):
vhost: rename field in guest page struct
vhost: fix physical address mappinglib/vhost/vhost.h | 11 ++-- lib/vhost/vhost_user.c | 130 ++++++++++++++++++++--------------------- lib/vhost/virtio_net.c | 11 ++-- 3 files changed, 75 insertions(+), 77 deletions(-)
I was willing to apply the series, but it does not apply. Could you please rebase with taking our comments into account?
Thanks,
Maxime
Hi Maxime & Kevin,
Quoted 10 lines
-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: 2022年2月4日 18:57
To: Ding, Xuan <[email protected]>; Xia, Chenbo <[email protected]>
Cc: [email protected]; Hu, Jiayu <[email protected]>; Wang, YuanX <[email protected]>
Subject: Re: [PATCH v2 0/2] vhost: fix async address mapping
Quoted 9 lines
Quoted 8 lines · On 1/19/22 16:10, [email protected] wrote:
From: Xuan Ding <[email protected]>
This patchset fixes the issue of incorrect DMA mapping in PA mode. Due to the ambiguity of
host_phys_addrnaming in the guest page struct, rename it tohost_iova.v2:
* Change the order of patch.
The consideration of changing the order here is to avoid the fix patch using the previous variable name(host_phys_addr), so rename the variable first.
Quoted 5 lines
Quoted 5 lines
Xuan Ding (2):
vhost: rename field in guest page struct
vhost: fix physical address mapping
Quoted 8 lines
Quoted 5 lines
lib/vhost/vhost.h | 11 ++-- lib/vhost/vhost_user.c | 130 ++++++++++++++++++++--------------------- lib/vhost/virtio_net.c | 11 ++-- 3 files changed, 75 insertions(+), 77 deletions(-)I was willing to apply the series, but it does not apply. Could you please rebase with taking our comments into account?
Thanks for your comments, I will send a new patch set applied with your comments.
Regards,
Xuan
Quoted 3 lines
Thanks,
Maxime