DPDK patches and discussions
 help / color / mirror / Atom feed
* [dpdk-dev] [PATCH] vhost: fix async DMA map
@ 2021-10-25 20:33 Maxime Coquelin
  2021-10-25 20:47 ` Maxime Coquelin
  0 siblings, 1 reply; 9+ messages in thread
From: Maxime Coquelin @ 2021-10-25 20:33 UTC (permalink / raw)
  To: dev, david.marchand, chenbo.xia, xuan.ding; +Cc: Maxime Coquelin

This patch fixes possible NULL-pointer dereferencing
reported by Coverity and also fixes NUMA reallocation
of the async DMA map.

Fixes: 7c61fa08b716 ("vhost: enable IOMMU for async vhost")

Coverity issue: 373655

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/vhost/vhost_user.c | 45 +++++++++++++++++++-----------------------
 1 file changed, 20 insertions(+), 25 deletions(-)

diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
index 720d1c1c9d..b8fbb8b415 100644
--- a/lib/vhost/vhost_user.c
+++ b/lib/vhost/vhost_user.c
@@ -149,6 +149,9 @@ async_dma_map(struct rte_vhost_mem_region *region, bool *dma_map_success, bool d
 	uint64_t host_iova;
 	int ret = 0;
 
+	if (!rte_vfio_is_enabled("vfio"))
+		return 0;
+
 	host_iova = rte_mem_virt2iova((void *)(uintptr_t)region->host_user_addr);
 	if (do_map) {
 		/* Add mapped region into the default container of DPDK. */
@@ -176,9 +179,7 @@ async_dma_map(struct rte_vhost_mem_region *region, bool *dma_map_success, bool d
 
 			VHOST_LOG_CONFIG(ERR, "DMA engine map failed\n");
 			return ret;
-
 		}
-
 	} else {
 		/* No need to do vfio unmap if the map failed. */
 		if (!*dma_map_success)
@@ -228,11 +229,8 @@ vhost_backend_cleanup(struct virtio_net *dev)
 		free_mem_region(dev);
 		rte_free(dev->mem);
 		dev->mem = NULL;
-
-		if (dev->async_map_status) {
-			rte_free(dev->async_map_status);
-			dev->async_map_status = NULL;
-		}
+		rte_free(dev->async_map_status);
+		dev->async_map_status = NULL;
 	}
 
 	rte_free(dev->guest_pages);
@@ -689,16 +687,17 @@ numa_realloc(struct virtio_net *dev, int index)
 	dev->mem = mem;
 
 	if (dev->async_copy && rte_vfio_is_enabled("vfio")) {
-		if (dev->async_map_status == NULL) {
-			dev->async_map_status = rte_zmalloc_socket("async-dma-map-status",
-					sizeof(bool) * dev->mem->nregions, 0, node);
-			if (!dev->async_map_status) {
-				VHOST_LOG_CONFIG(ERR,
-					"(%d) failed to realloc dma mapping status on node\n",
-					dev->vid);
-				return dev;
-			}
+		bool *ams;
+
+		ams = rte_realloc_socket(dev->async_map_status, sizeof(*ams) * dev->mem->nregions,
+				0, node);
+		if (!ams) {
+			VHOST_LOG_CONFIG(ERR, "Failed to realloc dma mapping status on node %d\n",
+					node);
+			return dev;
 		}
+
+		dev->async_map_status = ams;
 	}
 
 	gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp),
@@ -1294,16 +1293,14 @@ vhost_user_mmap_region(struct virtio_net *dev,
 
 	if (dev->async_copy) {
 		if (add_guest_pages(dev, region, alignment) < 0) {
-			VHOST_LOG_CONFIG(ERR,
-					"adding guest pages to region failed.\n");
+			VHOST_LOG_CONFIG(ERR, "adding guest pages to region failed.\n");
 			return -1;
 		}
 
-		if (rte_vfio_is_enabled("vfio")) {
+		if (dev->async_map_status) {
 			ret = async_dma_map(region, &dev->async_map_status[region_index], true);
 			if (ret) {
-				VHOST_LOG_CONFIG(ERR, "Configure IOMMU for DMA "
-							"engine failed\n");
+				VHOST_LOG_CONFIG(ERR, "Configure IOMMU for DMA engine failed\n");
 				return -1;
 			}
 		}
@@ -1501,10 +1498,8 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 	free_mem_region(dev);
 	rte_free(dev->mem);
 	dev->mem = NULL;
-	if (dev->async_map_status) {
-		rte_free(dev->async_map_status);
-		dev->async_map_status = NULL;
-	}
+	rte_free(dev->async_map_status);
+	dev->async_map_status = NULL;
 free_guest_pages:
 	rte_free(dev->guest_pages);
 	dev->guest_pages = NULL;
-- 
2.31.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-10-26 15:25 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-25 20:33 [dpdk-dev] [PATCH] vhost: fix async DMA map Maxime Coquelin
2021-10-25 20:47 ` Maxime Coquelin
2021-10-26  2:07   ` Ding, Xuan
2021-10-26  6:52     ` Maxime Coquelin
2021-10-26  8:49       ` Ding, Xuan
2021-10-26  9:49         ` Maxime Coquelin
2021-10-26 10:27           ` Ding, Xuan
2021-10-26 10:58             ` Burakov, Anatoly
2021-10-26 15:24               ` Burakov, Anatoly

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).