[PATCH v18 05/29] vhost: check DMA capability only once

Anatoly Burakov anatoly.burakov at intel.com
Thu Sep 24 18:13:12 CEST 2026


Currently, vhost async copy feature will check if VFIO supports DMA mapping
on every attempt to add/remove memory region or set memory table. This is
not necessary, so instead check this once at new vhost connection, and
store that value for future use.

Signed-off-by: Anatoly Burakov <anatoly.burakov at intel.com>
---
 lib/vhost/socket.c     |  5 ++++-
 lib/vhost/vhost.h      |  1 +
 lib/vhost/vhost_user.c | 10 +++++-----
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c
index 0943b3e9bb6..a8f58c95e83 100644
--- a/lib/vhost/socket.c
+++ b/lib/vhost/socket.c
@@ -17,6 +17,7 @@
 #include <eal_export.h>
 #include <rte_thread.h>
 #include <rte_log.h>
+#include <rte_vfio.h>
 
 #include "fd_man.h"
 #include "vduse.h"
@@ -242,8 +243,10 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
 	if (vsocket->async_copy) {
 		dev = get_device(vid);
 
-		if (dev)
+		if (dev != NULL) {
 			dev->async_copy = 1;
+			dev->dma_map_available = rte_vfio_is_enabled("vfio");
+		}
 	}
 
 	VHOST_CONFIG_LOG(vsocket->path, INFO, "new device, handle is %d", vid);
diff --git a/lib/vhost/vhost.h b/lib/vhost/vhost.h
index bb4708aed5e..75736eed3b5 100644
--- a/lib/vhost/vhost.h
+++ b/lib/vhost/vhost.h
@@ -499,6 +499,7 @@ struct __rte_cache_aligned virtio_net {
 	RTE_ATOMIC(int16_t)	broadcast_rarp;
 	uint32_t		nr_vring;
 	int			async_copy;
+	bool			dma_map_available;
 
 	int			extbuf;
 	int			linearbuf;
diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
index 020c993b299..2d91722fcc0 100644
--- a/lib/vhost/vhost_user.c
+++ b/lib/vhost/vhost_user.c
@@ -267,7 +267,7 @@ free_all_mem_regions(struct virtio_net *dev)
 	if (!dev || !dev->mem)
 		return;
 
-	if (dev->async_copy && rte_vfio_is_enabled("vfio"))
+	if (dev->async_copy && dev->dma_map_available)
 		async_dma_map(dev, false);
 
 	for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++) {
@@ -1564,7 +1564,7 @@ vhost_user_set_mem_table(struct virtio_net **pdev,
 		dev->mem->nregions++;
 	}
 
-	if (dev->async_copy && rte_vfio_is_enabled("vfio"))
+	if (dev->async_copy && dev->dma_map_available)
 		async_dma_map(dev, true);
 
 	if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0)
@@ -1750,7 +1750,7 @@ vhost_user_add_mem_reg(struct virtio_net **pdev,
 
 	dev->mem->nregions++;
 
-	if (dev->async_copy && rte_vfio_is_enabled("vfio")) {
+	if (dev->async_copy && dev->dma_map_available) {
 		if (async_dma_map_region(dev, reg, true) < 0)
 			goto free_new_region_no_dma;
 	}
@@ -1791,7 +1791,7 @@ vhost_user_add_mem_reg(struct virtio_net **pdev,
 	return RTE_VHOST_MSG_RESULT_OK;
 
 free_new_region:
-	if (dev->async_copy && rte_vfio_is_enabled("vfio"))
+	if (dev->async_copy && dev->dma_map_available)
 		async_dma_map_region(dev, reg, false);
 free_new_region_no_dma:
 	remove_guest_pages(dev, reg);
@@ -1827,7 +1827,7 @@ vhost_user_rem_mem_reg(struct virtio_net **pdev,
 		if (region->userspace_addr == current_region->guest_user_addr
 			&& region->guest_phys_addr == current_region->guest_phys_addr
 			&& region->memory_size == current_region->size) {
-			if (dev->async_copy && rte_vfio_is_enabled("vfio"))
+			if (dev->async_copy && dev->dma_map_available)
 				async_dma_map_region(dev, current_region, false);
 			if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
 				vhost_user_iotlb_cache_remove(dev,
-- 
2.52.0



More information about the dev mailing list