From patchwork Tue Feb 15 15:02:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ding, Xuan" X-Patchwork-Id: 107565 X-Patchwork-Delegate: maxime.coquelin@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 7550AA00C5; Tue, 15 Feb 2022 07:26:05 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 625DA41151; Tue, 15 Feb 2022 07:26:05 +0100 (CET) Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by mails.dpdk.org (Postfix) with ESMTP id 878414113F; Tue, 15 Feb 2022 07:26:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644906363; x=1676442363; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=SYKHfz6jh251VX1zhOMan6V6K0TudmIz2bANYliINnA=; b=jozYqZng9jzznhiZDvKv9lR2M6yE9dBQ2EdB1SFIhc54X9uUEI5SMC5F XaWfg211Bv2B9Uz/glGBvlMiwIr6o4uiBFib+tWW/9fhqYlHNWpqXha+7 OIetUQ6D3diTm10RN6GixTb6Q6nkbk8i8WRew64sW26cWl/Bn5ZS1aacx lPOWo+HUFsKQ5OdvY8/+OlQ1dAByC7UcqIb5GvGezPXwk16K3V2GL6iHi FQaTm8Qvo69lv3+JPwrV/HoDkZK8rpFEv+pT1uX+AudrVQBoWiSsp5Tm9 BXrF9oBMLYKPXVd16eWJjrhNcpe48GhsXi48/GOWLUdOkk8C0unMY1tjs Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10258"; a="233806860" X-IronPort-AV: E=Sophos;i="5.88,370,1635231600"; d="scan'208";a="233806860" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Feb 2022 22:25:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,370,1635231600"; d="scan'208";a="570656207" Received: from npg-dpdk-xuan-cbdma.sh.intel.com ([10.67.110.228]) by orsmga001.jf.intel.com with ESMTP; 14 Feb 2022 22:25:57 -0800 From: xuan.ding@intel.com To: maxime.coquelin@redhat.com, chenbo.xia@intel.com Cc: dev@dpdk.org, ktraynor@redhat.com, jiayu.hu@intel.com, yuanx.wang@intel.com, Xuan Ding , stable@dpdk.org Subject: [PATCH v3 1/2] vhost: fix field naming in guest page struct Date: Tue, 15 Feb 2022 15:02:25 +0000 Message-Id: <20220215150226.98292-2-xuan.ding@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220215150226.98292-1-xuan.ding@intel.com> References: <20220215150226.98292-1-xuan.ding@intel.com> X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Xuan Ding 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. Fixes: e246896178e6 ("vhost: get guest/host physical address mappings") Cc: stable@dpdk.org Signed-off-by: Xuan Ding Reviewed-by: Maxime Coquelin --- lib/vhost/vhost.h | 10 +++++----- lib/vhost/vhost_user.c | 20 ++++++++++---------- lib/vhost/virtio_net.c | 11 ++++++----- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/vhost/vhost.h b/lib/vhost/vhost.h index b3f0c1d07c..c5d8b84320 100644 --- a/lib/vhost/vhost.h +++ b/lib/vhost/vhost.h @@ -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; }; @@ -618,13 +618,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 { @@ -635,13 +635,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; } } } diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c index 3b810c112c..cd066d8af0 100644 --- a/lib/vhost/vhost_user.c +++ b/lib/vhost/vhost_user.c @@ -985,7 +985,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; @@ -1006,7 +1006,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; @@ -1015,7 +1015,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; @@ -1028,14 +1028,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; @@ -1044,9 +1044,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; @@ -1079,8 +1079,8 @@ dump_guest_pages(struct virtio_net *dev) dev->ifname, i); VHOST_LOG_CONFIG(INFO, "(%s)\tguest_phys_addr: %" PRIx64 "\n", dev->ifname, page->guest_phys_addr); - VHOST_LOG_CONFIG(INFO, "(%s)\thost_phys_addr : %" PRIx64 "\n", - dev->ifname, page->host_phys_addr); + VHOST_LOG_CONFIG(INFO, "(%s)\thost_iova : %" PRIx64 "\n", + dev->ifname, page->host_iova); VHOST_LOG_CONFIG(INFO, "(%s)\tsize : %" PRIx64 "\n", dev->ifname, page->size); } diff --git a/lib/vhost/virtio_net.c b/lib/vhost/virtio_net.c index f19713137c..00c6c41292 100644 --- a/lib/vhost/virtio_net.c +++ b/lib/vhost/virtio_net.c @@ -871,20 +871,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, "(%s) %s: failed to get hpa.\n", dev->ifname, __func__); + if (unlikely(!host_iova)) { + VHOST_LOG_DATA(ERR, "(%s) %s: failed to get host iova.\n", + dev->ifname, __func__); return -1; } if (unlikely(async_iter_add_iovec(dev, 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; From patchwork Tue Feb 15 15:02:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ding, Xuan" X-Patchwork-Id: 107566 X-Patchwork-Delegate: maxime.coquelin@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 62E4AA00C5; Tue, 15 Feb 2022 07:26:10 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3E7E441156; Tue, 15 Feb 2022 07:26:06 +0100 (CET) Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by mails.dpdk.org (Postfix) with ESMTP id 5557341144; Tue, 15 Feb 2022 07:26:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644906364; x=1676442364; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=YoeFrmCXHV4Yc9I+eWXRAKMTrBAA5RuZB/oo7WWeSZ0=; b=V0z9+15lc+m95APEofaSvC3okhulgv6BzSwEOG/+wmlqE8JtT6GlUrsb dZ8z+QV0I1Yv3Rv/VLEtXiAitwcM9ABSIGi/7Osoa70D1THZ8t4seSPJu AY0hyizqIU1LOqQOm53K9O7HPGRhF+4TsVJhJE6REaIhy4BJj5pKizFp4 nVZ0ZrThIEnlWTrHELzOrHF94NFmi5Ldy1nNBenYuk7twaIUBw5uLR3Kr 9aFvtel4GLtEzZp2wxP70R468K7sp3WV/wFkUX/mjDdQV7LUqpcpWH8As Gu/LVz6uiFdGH/SH0Qk3L30U+MzpdcelrbnfMMiIn4iIBfebvRHZYbS1j g==; X-IronPort-AV: E=McAfee;i="6200,9189,10258"; a="233806865" X-IronPort-AV: E=Sophos;i="5.88,370,1635231600"; d="scan'208";a="233806865" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Feb 2022 22:26:03 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,370,1635231600"; d="scan'208";a="570656231" Received: from npg-dpdk-xuan-cbdma.sh.intel.com ([10.67.110.228]) by orsmga001.jf.intel.com with ESMTP; 14 Feb 2022 22:26:00 -0800 From: xuan.ding@intel.com To: maxime.coquelin@redhat.com, chenbo.xia@intel.com Cc: dev@dpdk.org, ktraynor@redhat.com, jiayu.hu@intel.com, yuanx.wang@intel.com, Xuan Ding , stable@dpdk.org Subject: [PATCH v3 2/2] vhost: fix physical address mapping Date: Tue, 15 Feb 2022 15:02:26 +0000 Message-Id: <20220215150226.98292-3-xuan.ding@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220215150226.98292-1-xuan.ding@intel.com> References: <20220215150226.98292-1-xuan.ding@intel.com> X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Xuan Ding 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") Cc: stable@dpdk.org Signed-off-by: Xuan Ding Signed-off-by: Yuan Wang Reviewed-by: Maxime Coquelin --- lib/vhost/vhost.h | 1 + lib/vhost/vhost_user.c | 119 ++++++++++++++++++++--------------------- 2 files changed, 58 insertions(+), 62 deletions(-) diff --git a/lib/vhost/vhost.h b/lib/vhost/vhost.h index c5d8b84320..d4586f3341 100644 --- a/lib/vhost/vhost.h +++ b/lib/vhost/vhost.h @@ -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; }; diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c index cd066d8af0..981790ea53 100644 --- a/lib/vhost/vhost_user.c +++ b/lib/vhost/vhost_user.c @@ -142,57 +142,57 @@ get_blk_size(int fd) return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize; } -static int -async_dma_map(struct virtio_net *dev, 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; + uint32_t i; + struct guest_page *page; - host_iova = rte_mem_virt2iova((void *)(uintptr_t)region->host_user_addr); if (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, "(%s) DMA engine map failed\n", dev->ifname); - /* 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, "(%s) DMA engine unmap failed\n", dev->ifname); - 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 @@ -204,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(dev, reg, false); - munmap(reg->mmap_addr, reg->mmap_size); close(reg->fd); } @@ -985,7 +985,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; @@ -997,7 +997,7 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr, dev->max_guest_pages * sizeof(*page), RTE_CACHE_LINE_SIZE); if (dev->guest_pages == NULL) { - VHOST_LOG_CONFIG(ERR, "(%s) cannot realloc guest_pages\n", dev->ifname); + VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n"); rte_free(old_pages); return -1; } @@ -1006,8 +1006,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) { last_page->size += size; return 0; } @@ -1016,6 +1017,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; @@ -1035,7 +1037,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; @@ -1047,7 +1050,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; @@ -1223,7 +1226,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) { @@ -1280,16 +1282,6 @@ vhost_user_mmap_region(struct virtio_net *dev, dev->ifname); return -1; } - - if (rte_vfio_is_enabled("vfio")) { - ret = async_dma_map(dev, region, true); - if (ret) { - VHOST_LOG_CONFIG(ERR, - "(%s) configure IOMMU for DMA engine failed\n", - dev->ifname); - return -1; - } - } } VHOST_LOG_CONFIG(INFO, "(%s) guest memory region size: 0x%" PRIx64 "\n", @@ -1426,6 +1418,9 @@ vhost_user_set_mem_table(struct virtio_net **pdev, 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, ctx) < 0) goto free_mem_table;