Support add/remove memory region and get-max-slots
v14 cover Support add/remove memory region and get-max-slots
From: Pravin M Bathija <[email protected]>
This is version v14 of the patchset and it incorporates the recomendations made by Stephen Hemminger and Fengcheng Wen.
Changes from Fengcheng Wen review:
Patch 3/5
- Moved free_all_mem_regions() call sites in vhost_user_set_mem_table() from patch 4/5 to patch 3/5 so each commit compiles independently
Patch 4/5
- Renamed _dev_invalidate_vrings() to vhost_user_invalidate_vrings() to follow vhost naming convention
- Added comment explaining *pdev propagation through translate_ring_addresses / numa_realloc()
- Reordered local variables in vhost_user_add_mem_reg() and vhost_user_rem_mem_reg() by descending line length
- Shortened overlap check variable names (current_region_guest_start/end --> cur_start/end, proposed_region_guest_start/end -> new_start/end)
- Fixed DMA error path in vhost_user_add_mem_reg(): added free_new_region_no_dma label so async_dma_map_region(false) is not called when the map itself failed.
Changes from Stephen Hemminger review:
Patch 4/5
- vhost_user_add_mem_reg() now constructs a reply with the back-end's host mapping address in userspace_addr and returns RTE_VHOST_MSG_RESULT_REPLY per the vhost-user spec
- Added validate_msg_fds(dev, ctx, 0) in vhost_user_rem_mem_reg() to reject malformed messages with unexpected file descriptors
- Dropped unnecessary (uint64_t) cast in vhost_user_get_max_mem_slots()
This implementation has been extensively tested by doing Read/Write I/O from multiple instances of fio + libblkio (front-end) talking to spdk/dpdk (back-end) based drives. Tested with qemu front-end talking to dpdk testpmd (back-end) performing add/removal of memory regions. Also tested post-copy live migration after doing add_memory_region.
Version Log:
Version v14 (Current version): Incorporate code review suggestions from Stephen Hemminger and Fengcheng Wen as described above.
Version v13: Incorporate code review suggestions from Fengcheng Wen Patch 2/5
Renamed VhostUserSingleMemReg to VhostUserMemRegMsg and memory_single to memreg
Patches 3/5 and 4/5
Relocated function remove_guest_pages from patch 3/5 to 4/5
Version v12: Incorporate code review suggestions from Maxime Coquelin and ai-code-review.
Patch 3/5
Refactored async_dma_map() to delegate to async_dma_map_region(), eliminating code duplication between the two functions. Restored original comments in async_dma_map_region() explaining why ENODEV and EINVAL errors are ignored (these were stripped in v10) Reverted unnecessary changes to vhost_user_postcopy_register() -- removed the host_user_addr == 0 checks and reg_msg_index indirection that were added in v10, since this function is only called from vhost_user_set_mem_table() where regions are always contiguous.
Version v11: Incorporate code review suggestions from Stephen Hemminger. Patch 4/5
Fix incomplete cleanup in vhost_user_add_mem_reg() when vhost_user_mmap_region() fails after the mmap succeeds (e.g. add_guest_pages() realloc failure) realloc failure). The error path now calls remove_guest_pages() and free_mem_region() to undo the mapping and stale guest-page entries, preventing a leaked mmap and slot reuse corruption. The plain close(fd) path is kept for pre-mmap failures.
Version v10: Incorporate code review suggestions from Stephen Hemminger. Patch 4/5
Moved dev_invalidate_vrings after free_mem_region, array compaction, and nregions decrement. This ensures translate_ring_addresses only sees surviving memory regions, preventing vring pointers from resolving into a region that is about to be unmapped.
Version v9: Incorporate code review suggestions from Stephen Hemminger. Patch 3/5
Restored max_guest_pages initial value to hardcoded 8 instead of VHOST_MEMORY_MAX_NREGIONS, matching upstream semantics. Patch 4/5
Added close(reg->fd) and reg->fd = -1 before goto close_msg_fds in the mmap failure path to fix fd leak after fd was moved from ctx->fds[0]. Converted dev_invalidate_vrings from a plain function to a macro + implementation function pair, accepting message ID as a parameter so the static_assert reports the correct handler at each call site. Updated dev_invalidate_vrings call in add_mem_reg to pass VHOST_USER_ADD_MEM_REG as message ID.
Updated dev_invalidate_vrings call in rem_mem_reg to pass VHOST_USER_REM_MEM_REG as message ID.
Version v8: Incorporate code review suggestions from Stephen Hemminger. rewrite async_dma_map_region function to iterate guest pages by host address range matching
change function dev_invalidate_vrings to accept a double pointer to propagate pointer updates
new function remove_guest_pages was addedadd_mem_reg error path was narrowed to only clean up the single failed region instead of destroting all existing regions
Version v7: Incorporate code review suggestions from Maxime Coquelin. Add debug messages to vhost_postcopy_register function.
Version v6: Added the enablement of this feature as a final patch in this patch-set and other code optimizations as suggested by Maxime Coquelin.
Version v5: removed the patch that increased the number of memory regions from 8 to 128. This will be submitted as a separate feature at a later point after incorporating additional optimizations. Also includes code optimizations as suggested by Feng Cheng Wen.
Version v4: code optimizations as suggested by Feng Cheng Wen.
Version v3: code optimizations as suggested by Maxime Coquelin and Thomas Monjalon.
Version v2: code optimizations as suggested by Maxime Coquelin.
Version v1: Initial patch set.
Pravin M Bathija (5):
vhost: add user to mailmap and define to vhost hdr
vhost: header defines for add/rem mem region
vhost: refactor memory helper functions
vhost: add mem region add/remove handlers
vhost: enable configure memory slots
.mailmap | 1 + lib/vhost/rte_vhost.h | 4 + lib/vhost/vhost_user.c | 427 +++++++++++++++++++++++++++++++++++------ lib/vhost/vhost_user.h | 10 + 4 files changed, 380 insertions(+), 62 deletions(-)
Signature · 1 line
2.43.0
PATCH v14 1/5 vhost: add user to mailmap and define to vhost hdr
From: Pravin M Bathija <[email protected]>
- add user to mailmap file.
- define a bit-field called VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS that depicts if the feature/capability to add/remove memory regions is supported. This is a part of the overall support for add/remove memory region feature in this patchset.
--- .mailmap | 1 + lib/vhost/rte_vhost.h | 4 ++++ 2 files changed, 5 insertions(+)
.mailmap +1 -0
@@ -1295,6 +1295,7 @@ Prateek Agarwal <[email protected]> Prathisna Padmasanan <[email protected]> Praveen Kaligineedi <[email protected]> Praveen Shetty <[email protected]> +Pravin M Bathija <[email protected]> Pravin Pathak <[email protected]> <[email protected]> Prince Takkar <[email protected]> Priyalee Kushwaha <[email protected]>
lib/vhost/rte_vhost.h +4 -0
@@ -109,6 +109,10 @@ extern "C" {#define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12#endif +#ifndef VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS +#define VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS 15 +#endif +#ifndef VHOST_USER_PROTOCOL_F_STATUS #define VHOST_USER_PROTOCOL_F_STATUS 16#endif
Signature · 1 line
2.43.0
Re: [PATCH v14 1/5] vhost: add user to mailmap and define to vhost hdr
Quoted 8 lines · On Wed, May 20, 2026 at 4:20 AM <[email protected]> wrote:
From: Pravin M Bathija <[email protected]>
- add user to mailmap file.
- define a bit-field calledVHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTSthat depicts if the feature/capability to add/remove memory regions is supported. This is a part of the overall support for add/remove memory region feature in this patchset.
Quoted 5 lines
Signed-off-by Pravin M Bathija <[email protected]>Acked-by Fengchengwen <[email protected]>Reviewed-by Stephen Hemminger <[email protected]>Acked-by Maxime Coquelin <[email protected]>
It was a:
Not a ack.
Quoted 36 lines
--- .mailmap | 1 + lib/vhost/rte_vhost.h | 4 ++++ 2 files changed, 5 insertions(+).mailmap +1
-0@@ -1295,6 +1295,7 @@ Prateek Agarwal <[email protected]> Prathisna Padmasanan <[email protected]> Praveen Kaligineedi <[email protected]> Praveen Shetty <[email protected]> +Pravin M Bathija <[email protected]> Pravin Pathak <[email protected]> <[email protected]> Prince Takkar <[email protected]> Priyalee Kushwaha <[email protected]>lib/vhost/rte_vhost.h +4
-1@@ -109,6 +109,10 @@ extern "C" {#define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12#endif +#ifndef VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS +#define VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS 15 +#endif +#ifndef VHOST_USER_PROTOCOL_F_STATUS #define VHOST_USER_PROTOCOL_F_STATUS 16#endif --2.43.0
PATCH v14 2/5 vhost: header defines for add/rem mem region
In reply to [email protected]From: Pravin M Bathija <[email protected]>
The changes in this file cover the enum message requests for supporting add/remove memory regions. The front-end vhost-user client sends messages like get max memory slots, add memory region and remove memory region which are covered in these changes which are on the vhost-user back-end. The changes also include data structure definition of memory region to be added/removed. The data structure VhostUserMsg has been changed to include the memory region.
--- lib/vhost/vhost_user.h | 9 +++++++++ 1 file changed, 9 insertions(+)
lib/vhost/vhost_user.h +9 -0
@@ -67,6 +67,9 @@ typedef enum VhostUserRequest {VHOST_USER_POSTCOPY_END =30 ,VHOST_USER_GET_INFLIGHT_FD =31 ,VHOST_USER_SET_INFLIGHT_FD =32 , +VHOST_USER_GET_MAX_MEM_SLOTS =36 , +VHOST_USER_ADD_MEM_REG =37 , +VHOST_USER_REM_MEM_REG =38 ,VHOST_USER_SET_STATUS =39 ,VHOST_USER_GET_STATUS =40 , } VhostUserRequest;
@@ -91,6 +94,11 @@ typedef struct VhostUserMemory {VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS ]; } VhostUserMemory; +typedef struct VhostUserMemRegMsg { +uint64_t padding; +VhostUserMemoryRegion region; +}VhostUserMemRegMsg ; + typedef struct VhostUserLog { uint64_t mmap_size; uint64_t mmap_offset;
@@ -186,6 +194,7 @@ typedef struct __rte_packed_begin VhostUserMsg {struct vhost_vring_state state;struct vhost_vring_addr addr;VhostUserMemory memory; +VhostUserMemRegMsg memreg;VhostUserLog log;struct vhost_iotlb_msg iotlb;VhostUserCryptoSessionParam crypto_session;
Signature · 1 line
2.43.0
Re: [PATCH v14 2/5] vhost: header defines for add/rem mem region
Quoted 10 lines · On Wed, May 20, 2026 at 4:20 AM <[email protected]> wrote:
From: Pravin M Bathija <[email protected]>
The changes in this file cover the enum message requests for supporting add/remove memory regions. The front-end vhost-user client sends messages like get max memory slots, add memory region and remove memory region which are covered in these changes which are on the vhost-user back-end. The changes also include data structure definition of memory region to be added/removed. The data structure VhostUserMsg has been changed to include the memory region.
Quoted 5 lines
Signed-off-by Pravin M Bathija <[email protected]>Acked-by Fengchengwen <[email protected]>Reviewed-by Stephen Hemminger <[email protected]>Acked-by Maxime Coquelin <[email protected]>
Same here:
Quoted 42 lines
--- lib/vhost/vhost_user.h | 9 +++++++++ 1 file changed, 9 insertions(+)lib/vhost/vhost_user.h +9
-1@@ -67,6 +67,9 @@ typedef enum VhostUserRequest {VHOST_USER_POSTCOPY_END =30 ,VHOST_USER_GET_INFLIGHT_FD =31 ,VHOST_USER_SET_INFLIGHT_FD =32 , +VHOST_USER_GET_MAX_MEM_SLOTS =36 , +VHOST_USER_ADD_MEM_REG =37 , +VHOST_USER_REM_MEM_REG =38 ,VHOST_USER_SET_STATUS =39 ,VHOST_USER_GET_STATUS =40 , } VhostUserRequest;@@ -91,6 +94,11 @@ typedef struct VhostUserMemory {VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS ]; } VhostUserMemory; +typedef struct VhostUserMemRegMsg { +uint64_t padding; +VhostUserMemoryRegion region; +}VhostUserMemRegMsg ; + typedef struct VhostUserLog { uint64_t mmap_size; uint64_t mmap_offset;@@ -186,6 +194,7 @@ typedef struct __rte_packed_begin VhostUserMsg {struct vhost_vring_state state;struct vhost_vring_addr addr;VhostUserMemory memory; +VhostUserMemRegMsg memreg;VhostUserLog log;struct vhost_iotlb_msg iotlb;VhostUserCryptoSessionParam crypto_session; --2.43.0
PATCH v14 3/5 vhost: refactor memory helper functions
In reply to [email protected]From: Pravin M Bathija <[email protected]>
- Extract reusable helper routines for vhost-user backend memory operations.
- Split DMA map/unmap into per-region logic.
- Decouple and rework memory region free routines.
- Iterate over VHOST_MEMORY_MAX_NREGIONS uniformly across related functions to simplify code reuse
--- lib/vhost/vhost_user.c | 172 ++++++++++++++++++++++++++--------------- 1 file changed, 110 insertions(+), 62 deletions(-)
lib/vhost/vhost_user.c +110 -62
@@ -171,20 +171,27 @@ get_blk_size(int fd)return ret ==-1 ? (uint64_t )-1 : (uint64_t )stat.st_blksize; } -static void -async_dma_map (struct virtio_net *dev,bool do_map) +static int +async_dma_map_region (struct virtio_net *dev,struct rte_vhost_mem_region *reg,bool do_map) { -int ret =0 ;uint32_t i; -struct guest_page *page; +int ret; +uint64_t reg_start = reg->host_user_addr; +uint64_t reg_end = reg_start + reg->size; -if (do_map) { -for (i =0 ; i < dev->nr_guest_pages; i++) { - page = &dev->guest_pages[i]; +for (i =0 ; i < dev->nr_guest_pages; i++) { +struct guest_page *page = &dev->guest_pages[i]; + +/* Only process pages belonging to this region */ +if (page->host_user_addr < reg_start || + page->host_user_addr >= reg_end) +continue ; + +if (do_map) { ret =rte_vfio_container_dma_map (RTE_VFIO_DEFAULT_CONTAINER_FD , - page->host_user_addr, - page->host_iova, - page->size); + page->host_user_addr, + page->host_iova, + page->size);if (ret) {/* * DMA device may bind with kernel driver, in this case,
@@ -199,33 +206,57 @@ async_dma_map(struct virtio_net *dev, bool do_map)* normal case in async path. This is a workaround. */ if (rte_errno ==ENODEV ) -return ; +return 0 ;/* DMA mapping errors won't stop VHOST_USER_SET_MEM_TABLE. */ VHOST_CONFIG_LOG (dev->ifname,ERR ,"DMA engine map failed" ); +return -1 ; } - } - - }else { -for (i =0 ; i < dev->nr_guest_pages; i++) { - page = &dev->guest_pages[i]; + }else { ret =rte_vfio_container_dma_unmap (RTE_VFIO_DEFAULT_CONTAINER_FD , - page->host_user_addr, - page->host_iova, - page->size); + 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 ; +return 0 ;VHOST_CONFIG_LOG (dev->ifname,ERR ,"DMA engine unmap failed" ); +return -1 ; } } } + +return 0 ; +} + +static void +async_dma_map (struct virtio_net *dev,bool do_map) +{ +uint32_t i; +struct rte_vhost_mem_region *reg; + +for (i =0 ; i <VHOST_MEMORY_MAX_NREGIONS ; i++) { + reg = &dev->mem->regions[i]; +if (reg->host_user_addr ==0 ) +continue ; +async_dma_map_region (dev, reg, do_map); + } }static void -free_mem_region (struct virtio_net *dev) +free_mem_region (struct rte_vhost_mem_region *reg) +{ +if (reg !=NULL && reg->mmap_addr) { +munmap (reg->mmap_addr, reg->mmap_size); +close (reg->fd); +memset (reg,0 ,sizeof (struct rte_vhost_mem_region )); + } +} + +static void +free_all_mem_regions (struct virtio_net *dev) {uint32_t i;struct rte_vhost_mem_region *reg;
@@ -236,12 +267,10 @@ free_mem_region(struct virtio_net *dev)if (dev->async_copy &&rte_vfio_is_enabled ("vfio" ))async_dma_map (dev, false); -for (i =0 ; i < dev->mem->nregions; i++) { +for (i =0 ; i <VHOST_MEMORY_MAX_NREGIONS ; i++) { reg = &dev->mem->regions[i]; -if (reg->host_user_addr) { -munmap (reg->mmap_addr, reg->mmap_size); -close (reg->fd); - } +if (reg->mmap_addr) +free_mem_region (reg); } }
@@ -255,7 +284,7 @@ vhost_backend_cleanup(struct virtio_net *dev) vdpa_dev->ops->dev_cleanup (dev->vid);if (dev->mem) { -free_mem_region (dev); +free_all_mem_regions (dev);rte_free (dev->mem); dev->mem =NULL ; }
@@ -704,7 +733,7 @@ numa_realloc(struct virtio_net **pdev, struct vhost_virtqueue **pvq) vhost_devices[dev->vid] = dev; mem_size =sizeof (struct rte_vhost_memory ) + -sizeof (struct rte_vhost_mem_region ) * dev->mem->nregions; +sizeof (struct rte_vhost_mem_region ) *VHOST_MEMORY_MAX_NREGIONS ; mem =rte_realloc_socket (dev->mem, mem_size,0 , node);if (!mem) { VHOST_CONFIG_LOG(dev->ifname, ERR,
@@ -808,8 +837,10 @@ hua_to_alignment(struct rte_vhost_memory *mem, void *ptr)uint32_t i;uintptr_t hua = (uintptr_t )ptr; -for (i =0 ; i < mem->nregions; i++) { +for (i =0 ; i <VHOST_MEMORY_MAX_NREGIONS ; i++) { r = &mem->regions[i]; +if (r->host_user_addr ==0 ) +continue ;if (hua >= r->host_user_addr && hua < r->host_user_addr + r->size) {return get_blk_size (r->fd);
@@ -1382,6 +1413,52 @@ vhost_user_mmap_region(struct virtio_net *dev,return 0 ; } +static int +vhost_user_initialize_memory (struct virtio_net **pdev) +{ +struct virtio_net *dev = *pdev; +int numa_node =SOCKET_ID_ANY ; + +if (dev->mem !=NULL ) { +VHOST_CONFIG_LOG (dev->ifname,ERR , +"memory already initialized, free it first" ); +return -1 ; + } + +/* +* If VQ 0 has already been allocated, try to allocate on the same +* NUMA node. It can be reallocated later in numa_realloc(). +*/ +if (dev->nr_vring >0 ) + numa_node = dev->virtqueue[0 ]->numa_node; + + dev->nr_guest_pages =0 ; +if (dev->guest_pages ==NULL ) { + dev->max_guest_pages =8 ; + dev->guest_pages =rte_zmalloc_socket (NULL , + dev->max_guest_pages * +sizeof (struct guest_page ), +RTE_CACHE_LINE_SIZE , + numa_node); +if (dev->guest_pages ==NULL ) { +VHOST_CONFIG_LOG (dev->ifname,ERR , +"failed to allocate memory for dev->guest_pages" ); +return -1 ; + } + } + + dev->mem =rte_zmalloc_socket ("vhost-mem-table" ,sizeof (struct rte_vhost_memory ) + +sizeof (struct rte_vhost_mem_region ) *VHOST_MEMORY_MAX_NREGIONS ,0 , numa_node); +if (dev->mem ==NULL ) { +VHOST_CONFIG_LOG (dev->ifname,ERR ,"failed to allocate memory for dev->mem" ); +rte_free (dev->guest_pages); + dev->guest_pages =NULL ; +return -1 ; + } + +return 0 ; +} + static int vhost_user_set_mem_table(struct virtio_net **pdev, struct vhu_msg_context *ctx,
@@ -1390,7 +1467,6 @@ vhost_user_set_mem_table(struct virtio_net **pdev,struct virtio_net *dev = *pdev;struct VhostUserMemory *memory = &ctx->msg.payload.memory;struct rte_vhost_mem_region *reg; -int numa_node =SOCKET_ID_ANY ;uint64_t mmap_offset;uint32_t i;bool async_notify = false;
@@ -1435,39 +1511,13 @@ vhost_user_set_mem_table(struct virtio_net **pdev,if (dev->features & (1ULL <<VIRTIO_F_IOMMU_PLATFORM ))vhost_user_iotlb_flush_all (dev); -free_mem_region (dev); +free_all_mem_regions (dev);rte_free (dev->mem); dev->mem =NULL ; } -/* -* If VQ 0 has already been allocated, try to allocate on the same -* NUMA node. It can be reallocated later in numa_realloc(). -*/ -if (dev->nr_vring >0 ) - numa_node = dev->virtqueue[0 ]->numa_node; - - dev->nr_guest_pages =0 ; -if (dev->guest_pages ==NULL ) { - dev->max_guest_pages =8 ; - dev->guest_pages =rte_zmalloc_socket (NULL , - dev->max_guest_pages * -sizeof (struct guest_page ), -RTE_CACHE_LINE_SIZE , - numa_node); -if (dev->guest_pages ==NULL ) { -VHOST_CONFIG_LOG (dev->ifname,ERR , -"failed to allocate memory for dev->guest_pages" ); - goto close_msg_fds; - } - } - - dev->mem =rte_zmalloc_socket ("vhost-mem-table" ,sizeof (struct rte_vhost_memory ) + -sizeof (struct rte_vhost_mem_region ) * memory->nregions,0 , numa_node); -if (dev->mem ==NULL ) { -VHOST_CONFIG_LOG (dev->ifname,ERR ,"failed to allocate memory for dev->mem" ); - goto free_guest_pages; - } +if (vhost_user_initialize_memory (pdev) <0 ) + goto close_msg_fds;for (i =0 ; i < memory->nregions; i++) { reg = &dev->mem->regions[i];
@@ -1531,11 +1581,9 @@ vhost_user_set_mem_table(struct virtio_net **pdev,return RTE_VHOST_MSG_RESULT_OK ; free_mem_table: -free_mem_region (dev); +free_all_mem_regions (dev);rte_free (dev->mem); dev->mem =NULL ; - -free_guest_pages:rte_free (dev->guest_pages); dev->guest_pages =NULL ; close_msg_fds:
Signature · 1 line
2.43.0
Re: [PATCH v14 3/5] vhost: refactor memory helper functions
Quoted 9 lines · On Wed, May 20, 2026 at 4:20 AM <[email protected]> wrote:
From: Pravin M Bathija <[email protected]>
- Extract reusable helper routines for vhost-user backend memory operations.
- Split DMA map/unmap into per-region logic.
- Decouple and rework memory region free routines.
- Iterate overVHOST_MEMORY_MAX_NREGIONSuniformly across related functions to simplify code reuse
Quoted 5 lines
Signed-off-by Pravin M Bathija <[email protected]>Acked-by Fengchengwen <[email protected]>Reviewed-by Stephen Hemminger <[email protected]>Acked-by Maxime Coquelin <[email protected]>
I did not Ack this one AFAICT.
Quoted 6 lines
--- lib/vhost/vhost_user.c | 172 ++++++++++++++++++++++++++--------------- 1 file changed, 110 insertions(+), 62 deletions(-)
The change looks good though:
PATCH v14 4/5 vhost: add mem region add/remove handlers
In reply to [email protected]From: Pravin M Bathija <[email protected]>
Add support for VHOST_USER_ADD_MEM_REG, VHOST_USER_REM_MEM_REG and VHOST_USER_GET_MAX_MEM_SLOTS. Refactor memory initialization into common helper and add supporting functions for dynamic memory management.
--- lib/vhost/vhost_user.c | 255 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+)
lib/vhost/vhost_user.c +255 -0
@@ -71,6 +71,9 @@ VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES, vhost_user_set_features, false, t VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER, vhost_user_set_owner, false, true) \ VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER, vhost_user_reset_owner, false, false) \ VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE, vhost_user_set_mem_table, true, true) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_GET_MAX_MEM_SLOTS, vhost_user_get_max_mem_slots, false, false) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_ADD_MEM_REG, vhost_user_add_mem_reg, true, true) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_REM_MEM_REG, vhost_user_rem_mem_reg, true, true) \ VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE, vhost_user_set_log_base, true, true) \ VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD, vhost_user_set_log_fd, true, true) \VHOST_MESSAGE_HANDLER (VHOST_USER_SET_VRING_NUM , vhost_user_set_vring_num, false, true) \
@@ -1167,6 +1170,24 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,return 0 ; } +static void +remove_guest_pages (struct virtio_net *dev,struct rte_vhost_mem_region *reg) +{ +uint64_t reg_start = reg->host_user_addr; +uint64_t reg_end = reg_start + reg->size; +uint32_t i, j =0 ; + +for (i =0 ; i < dev->nr_guest_pages; i++) { +if (dev->guest_pages[i].host_user_addr >= reg_start && + dev->guest_pages[i].host_user_addr < reg_end) +continue ; +if (j != i) + dev->guest_pages[j] = dev->guest_pages[i]; + j++; + } + dev->nr_guest_pages = j; +} +#ifdef RTE_LIBRTE_VHOST_DEBUG /* TODO: enable it only in debug mode? */ static void
@@ -1591,6 +1612,240 @@ vhost_user_set_mem_table(struct virtio_net **pdev,return RTE_VHOST_MSG_RESULT_ERR ; } + +static int +vhost_user_get_max_mem_slots(struct virtio_net **pdev __rte_unused, +struct vhu_msg_context *ctx, + int main_fd __rte_unused) +{ +uint32_t max_mem_slots =VHOST_MEMORY_MAX_NREGIONS ; + + ctx->msg.payload.u64 = max_mem_slots; + ctx->msg.size =sizeof (ctx->msg.payload.u64); + ctx->fd_num =0 ; + +return RTE_VHOST_MSG_RESULT_REPLY ; +} + +/* +* Invalidate and re-translate all vring addresses after the memory table +* has been modified (add/remove region). +* +* translate_ring_addresses() may call numa_realloc(), which can reallocate +* the device structure. The updated pointer is written back through *pdev +* so callers must refresh their local "dev" afterwards: dev = *pdev. +*/ +static void +vhost_user_invalidate_vrings (struct virtio_net **pdev) +{ +struct virtio_net *dev = *pdev; +uint32_t i; + +for (i =0 ; i < dev->nr_vring; i++) { +struct vhost_virtqueue *vq = dev->virtqueue[i]; + +if (!vq) +continue ; + +if (vq->desc || vq->avail || vq->used) { +vq_assert_lock (dev, vq); + +vring_invalidate (dev, vq); + +translate_ring_addresses (&dev, &vq); + } + } + + *pdev = dev; +} + +/* +* Macro wrapper that performs the compile-time lock assertion with the +* correct message ID at the call site, then calls the implementation. +*/ +#define dev_invalidate_vrings (pdev, id) do { \ + static_assert(id ## _LOCK_ALL_QPS, \ + #id " handler is not declared as locking all queue pairs"); \ + vhost_user_invalidate_vrings(pdev); \ +} while (0) + +static int +vhost_user_add_mem_reg (struct virtio_net **pdev, +struct vhu_msg_context *ctx, + int main_fd __rte_unused) +{ +struct VhostUserMemoryRegion *region = &ctx->msg.payload.memreg.region; +struct virtio_net *dev = *pdev; +uint32_t i; + +/* convert first region add to normal memory table set */ +if (dev->mem ==NULL ) { +if (vhost_user_initialize_memory (pdev) <0 ) + goto close_msg_fds; + } + +/* make sure new region will fit */ +if (dev->mem->nregions >=VHOST_MEMORY_MAX_NREGIONS ) { +VHOST_CONFIG_LOG (dev->ifname,ERR ,"too many memory regions already (%u)" , + dev->mem->nregions); + goto close_msg_fds; + } + +/* make sure supplied memory fd present */ +if (ctx->fd_num !=1 ) { +VHOST_CONFIG_LOG (dev->ifname,ERR ,"fd count makes no sense (%u)" , ctx->fd_num); + goto close_msg_fds; + } + +/* Make sure no overlap in guest virtual address space */ +for (i =0 ; i < dev->mem->nregions; i++) { +struct rte_vhost_mem_region *cur = &dev->mem->regions[i]; +uint64_t cur_start = cur->guest_user_addr; +uint64_t cur_end = cur_start + cur->size -1 ; +uint64_t new_start = region->userspace_addr; +uint64_t new_end = new_start + region->memory_size -1 ; + +if (new_end >= cur_start && new_start <= cur_end) { +VHOST_CONFIG_LOG (dev->ifname,ERR , +"requested memory region overlaps with another region" ); +VHOST_CONFIG_LOG (dev->ifname,ERR , +"\tRequested region address:0x%" PRIx64, + region->userspace_addr); +VHOST_CONFIG_LOG (dev->ifname,ERR , +"\tRequested region size:0x%" PRIx64, + region->memory_size); +VHOST_CONFIG_LOG (dev->ifname,ERR , +"\tOverlapping region address:0x%" PRIx64, + cur->guest_user_addr); +VHOST_CONFIG_LOG (dev->ifname,ERR , +"\tOverlapping region size:0x%" PRIx64, + cur->size); + goto close_msg_fds; + } + } + +/* New region goes at the end of the contiguous array */ +struct rte_vhost_mem_region *reg = &dev->mem->regions[dev->mem->nregions]; + + reg->guest_phys_addr = region->guest_phys_addr; + reg->guest_user_addr = region->userspace_addr; + reg->size = region->memory_size; + reg->fd = ctx->fds[0 ]; + ctx->fds[0 ] =-1 ; + +if (vhost_user_mmap_region (dev, reg, region->mmap_offset) <0 ) { +VHOST_CONFIG_LOG (dev->ifname,ERR ,"failed to mmap region" ); +if (reg->mmap_addr) { +/* mmap succeeded but a later step (e.g. add_guest_pages) +* failed; undo the mapping and any guest-page entries. +*/ +remove_guest_pages (dev, reg); +free_mem_region (reg); + }else { +close (reg->fd); + reg->fd =-1 ; + } + goto close_msg_fds; + } + + dev->mem->nregions++; + +if (dev->async_copy &&rte_vfio_is_enabled ("vfio" )) { +if (async_dma_map_region (dev, reg, true) <0 ) + goto free_new_region_no_dma; + } + +if (dev->postcopy_listening) { +/* +* Cannot use vhost_user_postcopy_register() here because it +* reads ctx->msg.payload.memory (SET_MEM_TABLE layout), but +* ADD_MEM_REG uses the memreg payload. Register the +* single new region directly instead. +*/ +if (vhost_user_postcopy_region_register (dev, reg) <0 ) + goto free_new_region; + } + +dev_invalidate_vrings (pdev,VHOST_USER_ADD_MEM_REG ); + dev = *pdev; +dump_guest_pages (dev); + +/* Reply with the back-end's mapping address per vhost-user spec */ + ctx->msg.payload.memreg.region.userspace_addr = reg->host_user_addr; + ctx->msg.size =sizeof (ctx->msg.payload.memreg); + ctx->fd_num =0 ; + +return RTE_VHOST_MSG_RESULT_REPLY ; + +free_new_region: +if (dev->async_copy &&rte_vfio_is_enabled ("vfio" )) +async_dma_map_region (dev, reg, false); +free_new_region_no_dma: +remove_guest_pages (dev, reg); +free_mem_region (reg); + dev->mem->nregions--; +close_msg_fds: +close_msg_fds (ctx); +return RTE_VHOST_MSG_RESULT_ERR ; +} + +static int +vhost_user_rem_mem_reg (struct virtio_net **pdev, +struct vhu_msg_context *ctx, + int main_fd __rte_unused) +{ +struct VhostUserMemoryRegion *region = &ctx->msg.payload.memreg.region; +struct virtio_net *dev = *pdev; +uint32_t i; + +if (dev->mem ==NULL || dev->mem->nregions ==0 ) { +VHOST_CONFIG_LOG (dev->ifname,ERR ,"no memory regions to remove" ); +close_msg_fds (ctx); +return RTE_VHOST_MSG_RESULT_ERR ; + } + +if (validate_msg_fds (dev, ctx,0 ) !=0 ) +return RTE_VHOST_MSG_RESULT_ERR ; + +for (i =0 ; i < dev->mem->nregions; i++) { +struct rte_vhost_mem_region *current_region = &dev->mem->regions[i]; + +/* +* According to the vhost-user specification: +* The memory region to be removed is identified by its GPA, +* user address and size. The mmap offset is ignored. +*/ +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" )) +async_dma_map_region (dev, current_region, false); +remove_guest_pages (dev, current_region); +free_mem_region (current_region); + +/* Compact the regions array to keep it contiguous */ +if (i < dev->mem->nregions -1 ) { +memmove (&dev->mem->regions[i], + &dev->mem->regions[i +1 ], + (dev->mem->nregions -1 - i) * +sizeof (struct rte_vhost_mem_region )); +memset (&dev->mem->regions[dev->mem->nregions -1 ], +0 ,sizeof (struct rte_vhost_mem_region )); + } + + dev->mem->nregions--; +dev_invalidate_vrings (pdev,VHOST_USER_REM_MEM_REG ); + dev = *pdev; +close_msg_fds (ctx); +return RTE_VHOST_MSG_RESULT_OK ; + } + } + +VHOST_CONFIG_LOG (dev->ifname,ERR ,"failed to find region" ); +close_msg_fds (ctx); +return RTE_VHOST_MSG_RESULT_ERR ; +} +static bool vq_is_ready (struct virtio_net *dev,struct vhost_virtqueue *vq) {
Signature · 1 line
2.43.0
Re: [PATCH v14 4/5] vhost: add mem region add/remove handlers
Quoted 24 lines · On Wed, May 20, 2026 at 4:20 AM <[email protected]> wrote:
From: Pravin M Bathija <[email protected]>
Add support for
VHOST_USER_ADD_MEM_REG,VHOST_USER_REM_MEM_REGandVHOST_USER_GET_MAX_MEM_SLOTS. Refactor memory initialization into common helper and add supporting functions for dynamic memory management.Signed-off-by Pravin M Bathija <[email protected]>Acked-by Fengchengwen <[email protected]>Reviewed-by Stephen Hemminger <[email protected]>Acked-by Maxime Coquelin <[email protected]>--- lib/vhost/vhost_user.c | 255 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+)lib/vhost/vhost_user.c +0
-0@@ -71,6 +71,9 @@ VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES,
vhost_user_set_features, false, t
VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER,vhost_user_set_owner, false,
true) \
VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER,vhost_user_reset_owner,
false, false) \
Quoted 9 lines
VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE,
vhost_user_set_mem_table,
true, true) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_GET_MAX_MEM_SLOTS,vhost_user_get_max_mem_slots, false, false) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_ADD_MEM_REG,vhost_user_add_mem_reg, true, true) \
+VHOST_MESSAGE_HANDLER(VHOST_USER_REM_MEM_REG,vhost_user_rem_mem_reg, true, true) \
The removal request does not expect FDs in ancillary data. It should be:VHOST_MESSAGE_HANDLER(VHOST_USER_REM_MEM_REG, vhost_user_rem_mem_reg, false, true)
Quoted 241 lines
VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE,
vhost_user_set_log_base,
true, true) \
VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD,vhost_user_set_log_fd, true,
true) \
VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_NUM,vhost_user_set_vring_num,
false, true) \lib/vhost/vhost_user.c +18
-0@@ -1167,6 +1170,24 @@ add_guest_pages(struct virtio_net *dev, structrte_vhost_mem_region *reg, return 0; } +static void +remove_guest_pages (struct virtio_net *dev,struct rte_vhost_mem_region *reg) +{ +uint64_t reg_start = reg->host_user_addr; +uint64_t reg_end = reg_start + reg->size; +uint32_t i, j =0 ; + +for (i =0 ; i < dev->nr_guest_pages; i++) { +if (dev->guest_pages[i].host_user_addr >= reg_start && + dev->guest_pages[i].host_user_addr < reg_end) +continue ; +if (j != i) + dev->guest_pages[j] = dev->guest_pages[i]; + j++; + } + dev->nr_guest_pages = j; +} +#ifdef RTE_LIBRTE_VHOST_DEBUG /* TODO: enable it only in debug mode? */
static voidlib/vhost/vhost_user.c +20
-0@@ -1591,6 +1612,240 @@ vhost_user_set_mem_table(struct virtio_net **pdev,return RTE_VHOST_MSG_RESULT_ERR ; } + +static int +vhost_user_get_max_mem_slots(struct virtio_net **pdev __rte_unused, +struct vhu_msg_context *ctx, + int main_fd __rte_unused) +{ +uint32_t max_mem_slots =VHOST_MEMORY_MAX_NREGIONS ; + + ctx->msg.payload.u64 = max_mem_slots; + ctx->msg.size =sizeof (ctx->msg.payload.u64); + ctx->fd_num =0 ; + +return RTE_VHOST_MSG_RESULT_REPLY ; +} + +/* +* Invalidate and re-translate all vring addresses after the memory table +* has been modified (add/remove region). +* +* translate_ring_addresses() may call numa_realloc(), which can reallocate
+ * the device structure. The updated pointer is written back through *pdev
+ * so callers must refresh their local "dev" afterwards: dev = *pdev.
+ */lib/vhost/vhost_user.c +31
-0+static void +vhost_user_invalidate_vrings (struct virtio_net **pdev) +{ +struct virtio_net *dev = *pdev; +uint32_t i; + +for (i =0 ; i < dev->nr_vring; i++) { +struct vhost_virtqueue *vq = dev->virtqueue[i]; + +if (!vq) +continue ; + +if (vq->desc || vq->avail || vq->used) { +vq_assert_lock (dev, vq); + +vring_invalidate (dev, vq); + +translate_ring_addresses (&dev, &vq); + } + } + + *pdev = dev; +} + +/* +* Macro wrapper that performs the compile-time lock assertion with the +* correct message ID at the call site, then calls the implementation. +*/ +#define dev_invalidate_vrings (pdev, id) do { \ + static_assert(id ## _LOCK_ALL_QPS, \ + #id " handler is not declared as locking all queuepairs"); \
lib/vhost/vhost_user.c +9
-0+vhost_user_invalidate_vrings (pdev); \ +}while (0 ) + +static int +vhost_user_add_mem_reg (struct virtio_net **pdev, +struct vhu_msg_context *ctx, + int main_fd __rte_unused) +{ + struct VhostUserMemoryRegion *region =
&ctx->msg.payload.memreg.region;lib/vhost/vhost_user.c +12
-0+struct virtio_net *dev = *pdev; +uint32_t i; + +/* convert first region add to normal memory table set */ +if (dev->mem ==NULL ) { +if (vhost_user_initialize_memory (pdev) <0 ) + goto close_msg_fds; + } + +/* make sure new region will fit */ +if (dev->mem->nregions >=VHOST_MEMORY_MAX_NREGIONS ) { + VHOST_CONFIG_LOG(dev->ifname, ERR, "too many memoryregions already (%u)",
lib/vhost/vhost_user.c +7
-0+ dev->mem->nregions); + goto close_msg_fds; + } + +/* make sure supplied memory fd present */ +if (ctx->fd_num !=1 ) { + VHOST_CONFIG_LOG(dev->ifname, ERR, "fd count makes nosense (%u)",
ctx->fd_num);lib/vhost/vhost_user.c +14
-0+ goto close_msg_fds; + } + +/* Make sure no overlap in guest virtual address space */ +for (i =0 ; i < dev->mem->nregions; i++) { +struct rte_vhost_mem_region *cur = &dev->mem->regions[i]; +uint64_t cur_start = cur->guest_user_addr; +uint64_t cur_end = cur_start + cur->size -1 ; +uint64_t new_start = region->userspace_addr; +uint64_t new_end = new_start + region->memory_size -1 ; + +if (new_end >= cur_start && new_start <= cur_end) { + VHOST_CONFIG_LOG(dev->ifname, ERR, + "requested memory region overlaps withanother region");
lib/vhost/vhost_user.c +18
-0+VHOST_CONFIG_LOG (dev->ifname,ERR , +"\tRequested region address:0x%" PRIx64, + region->userspace_addr); +VHOST_CONFIG_LOG (dev->ifname,ERR , +"\tRequested region size:0x%" PRIx64, + region->memory_size); +VHOST_CONFIG_LOG (dev->ifname,ERR , +"\tOverlapping region address:0x%" PRIx64, + cur->guest_user_addr); +VHOST_CONFIG_LOG (dev->ifname,ERR , +"\tOverlapping region size:0x%" PRIx64, + cur->size); + goto close_msg_fds; + } + } + +/* New region goes at the end of the contiguous array */ + struct rte_vhost_mem_region *reg =&dev->mem->regions[dev->mem->nregions];
lib/vhost/vhost_user.c +9
-0+ + reg->guest_phys_addr = region->guest_phys_addr; + reg->guest_user_addr = region->userspace_addr; + reg->size = region->memory_size; + reg->fd = ctx->fds[0 ]; + ctx->fds[0 ] =-1 ; + +if (vhost_user_mmap_region (dev, reg, region->mmap_offset) <0 ) { + VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmapregion");
lib/vhost/vhost_user.c +2
-0+if (reg->mmap_addr) { +/* mmap succeeded but a later step (e.g.
add_guest_pages)
+ * failed; undo the mapping and any guest-page
entries.lib/vhost/vhost_user.c +20
-0+*/ +remove_guest_pages (dev, reg); +free_mem_region (reg); + }else { +close (reg->fd); + reg->fd =-1 ; + } + goto close_msg_fds; + } + + dev->mem->nregions++; + +if (dev->async_copy &&rte_vfio_is_enabled ("vfio" )) { +if (async_dma_map_region (dev, reg, true) <0 ) + goto free_new_region_no_dma; + } + +if (dev->postcopy_listening) { +/* +* Cannot use vhost_user_postcopy_register() here because it
+ * readsctx->msg.payload.memory(SET_MEM_TABLElayout),
butlib/vhost/vhost_user.c +13
-0+* ADD_MEM_REG uses the memreg payload. Register the +* single new region directly instead. +*/ +if (vhost_user_postcopy_region_register (dev, reg) <0 ) + goto free_new_region; + } + +dev_invalidate_vrings (pdev,VHOST_USER_ADD_MEM_REG ); + dev = *pdev; +dump_guest_pages (dev); + +/* Reply with the back-end's mapping address per vhost-user spec */ + ctx->msg.payload.memreg.region.userspace_addr =
reg->host_user_addr;lib/vhost/vhost_user.c +23
-0+ ctx->msg.size =sizeof (ctx->msg.payload.memreg); + ctx->fd_num =0 ; + +return RTE_VHOST_MSG_RESULT_REPLY ; + +free_new_region: +if (dev->async_copy &&rte_vfio_is_enabled ("vfio" )) +async_dma_map_region (dev, reg, false); +free_new_region_no_dma: +remove_guest_pages (dev, reg); +free_mem_region (reg); + dev->mem->nregions--; +close_msg_fds: +close_msg_fds (ctx); +return RTE_VHOST_MSG_RESULT_ERR ; +} + +static int +vhost_user_rem_mem_reg (struct virtio_net **pdev, +struct vhu_msg_context *ctx, + int main_fd __rte_unused) +{ + struct VhostUserMemoryRegion *region =
&ctx->msg.payload.memreg.region;lib/vhost/vhost_user.c +5
-0+struct virtio_net *dev = *pdev; +uint32_t i; + +if (dev->mem ==NULL || dev->mem->nregions ==0 ) { + VHOST_CONFIG_LOG(dev->ifname, ERR, "no memory regions to
Quoted 3 lines
remove");
+close_msg_fds(ctx);
Not needed if properly declared.
lib/vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net **pdev, · line 1808
Quoted 6 lines
lib/vhost/vhost_user.c +5
-0+return RTE_VHOST_MSG_RESULT_ERR ; + } + +if (validate_msg_fds (dev, ctx,0 ) !=0 ) +return RTE_VHOST_MSG_RESULT_ERR ;
With proper declaration, we can remove this check, as it is done in a generic way.
Quoted 18 lines
lib/vhost/vhost_user.c +3
-0+ +for (i =0 ; i < dev->mem->nregions; i++) { + struct rte_vhost_mem_region *current_region =&dev->mem->regions[i];
lib/vhost/vhost_user.c +4
-0+ +/* +* According to the vhost-user specification: +* The memory region to be removed is identified by its GPA,
lib/vhost/vhost_user.c +3
-0+* user address and size. The mmap offset is ignored. +*/ + if (region->userspace_addr ==
current_region->guest_user_addr
+ &®ion->guest_phys_addr==current_region->guest_phys_addrlib/vhost/vhost_user.c +3
-0+ && region->memory_size == current_region->size) { +if (dev->async_copy &&rte_vfio_is_enabled ("vfio" )) + async_dma_map_region(dev, current_region,
Quoted 3 lines
false);
+remove_guest_pages(dev, current_region);
You are missing the step to clear the IOTLB cache entries matching with this removed region.
In vhost_user_set_mem_table(), a vhost_user_iotlb_flush_all() call is made, but that would be kind of a nuclear option for memory hotplug.
I suggest removing only the entries matching the removed area, something like this:
if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
vhost_user_iotlb_cache_remove(dev, current_region->guest_phys_addr,
current_region->size);
Quoted 20 lines
lib/vhost/vhost_user.c +3
-0+free_mem_region (current_region); + +/* Compact the regions array to keep it contiguous */
lib/vhost/vhost_user.c +5
-0+if (i < dev->mem->nregions -1 ) { + memmove(&dev->mem->regions[i], + &dev->mem->regions[i + 1], + (dev->mem->nregions - 1 - i) * + sizeof(struct
rte_vhost_mem_region));lib/vhost/vhost_user.c +2
-0+ memset(&dev->mem->regions[dev->mem->nregions - 1], + 0, sizeof(struct
rte_vhost_mem_region));lib/vhost/vhost_user.c +4
-0+ } + + dev->mem->nregions--; + dev_invalidate_vrings(pdev,
VHOST_USER_REM_MEM_REG);
lib/vhost/vhost_user.c
Quoted 2 lines
lib/vhost/vhost_user.c +2
-0+ dev = *pdev; +close_msg_fds (ctx);
And no need to close FDs, are we are now sure none were provided.
lib/vhost/vhost_user.c vhost_user_set_mem_table(struct virtio_net **pdev, · line 1845
Quoted 7 lines
lib/vhost/vhost_user.c +6
-0+return RTE_VHOST_MSG_RESULT_OK ; + } + } + +VHOST_CONFIG_LOG (dev->ifname,ERR ,"failed to find region" ); +close_msg_fds (ctx);
Same, no more needed.
Quoted 10 lines
lib/vhost/vhost_user.c +3
-1+return RTE_VHOST_MSG_RESULT_ERR ; +} +static bool vq_is_ready (struct virtio_net *dev,struct vhost_virtqueue *vq) { --2.43.0
RE: [PATCH v14 4/5] vhost: add mem region add/remove handlers
Hi Maxime,
The responses are inline. Since your message was received as html, the formatting was lost after converting to plain text. I have tagged my responses as <Pravin>. The forthcoming patch v15 has the suggested changes. There was also a mix-up on my part with regards to "reviewed-by" and "Acked-by" which I have also corrected. Thank you for pointing that out.
Regards,
Pravin
Internal Use - Confidential
From: Maxime Coquelin <[email protected]>
Sent: Thursday, June 4, 2026 3:27 AM
To: Bathija, Pravin <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Stephen Hemminger <[email protected]>
Subject: Re: [PATCH v14 4/5] vhost: add mem region add/remove handlers
[EXTERNAL EMAIL]
On Wed, May 20, 2026 at 4:20 AM <mailto:[email protected]> wrote:
From: Pravin M Bathija <mailto:[email protected]>
Add support for VHOST_USER_ADD_MEM_REG, VHOST_USER_REM_MEM_REG and VHOST_USER_GET_MAX_MEM_SLOTS. Refactor memory initialization into common helper and add supporting functions for dynamic memory management.
--- lib/vhost/vhost_user.c | 255 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+)
lib/vhost/vhost_user.c +3 -0
@@ -71,6 +71,9 @@ VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES, vhost_user_set_features, false, t VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER, vhost_user_set_owner, false, true) \ VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER, vhost_user_reset_owner, false, false) \ VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE, vhost_user_set_mem_table, true, true) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_GET_MAX_MEM_SLOTS, vhost_user_get_max_mem_slots, false, false) \ +VHOST_MESSAGE_HANDLER(VHOST_USER_ADD_MEM_REG, vhost_user_add_mem_reg, true, true) \ +VHOST_MESSAGE_HANDLER (VHOST_USER_REM_MEM_REG , vhost_user_rem_mem_reg, true, true) \
The removal request does not expect FDs in ancillary data.
It should be:VHOST_MESSAGE_HANDLER(VHOST_USER_REM_MEM_REG, vhost_user_rem_mem_reg, false, true)
<Pravin> Fixed, changed handler declaration from (true, true) to (false, true).
VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE, vhost_user_set_log_base, true, true) \
VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD, vhost_user_set_log_fd, true, true) \
VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_NUM, vhost_user_set_vring_num, false, true) \
@@ -1167,6 +1170,24 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
return 0;
}
+static void
+remove_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg) +{
+ uint64_t reg_start = reg->host_user_addr; + uint64_t reg_end = reg_start + reg->size; + uint32_t i, j = 0;
+
+ for (i = 0; i < dev->nr_guest_pages; i++) {
+ if (dev->guest_pages[i].host_user_addr >= reg_start &&
+ dev->guest_pages[i].host_user_addr < reg_end)
+ continue;
+ if (j != i)
+ dev->guest_pages[j] = dev->guest_pages[i];
+ j++;
+ }
+ dev->nr_guest_pages = j;
+}
+
#ifdef RTE_LIBRTE_VHOST_DEBUG
/* TODO: enable it only in debug mode? */
static void
@@ -1591,6 +1612,240 @@ vhost_user_set_mem_table(struct virtio_net **pdev,
return RTE_VHOST_MSG_RESULT_ERR;
}
+
+static int
+vhost_user_get_max_mem_slots(struct virtio_net **pdev __rte_unused,
+ struct vhu_msg_context *ctx,
+ int main_fd __rte_unused)
+{
+ uint32_t max_mem_slots = VHOST_MEMORY_MAX_NREGIONS;
+
+ ctx->msg.payload.u64 = max_mem_slots; + ctx->msg.size = sizeof(ctx->msg.payload.u64); + ctx->fd_num = 0;
+
+ return RTE_VHOST_MSG_RESULT_REPLY;
+}
+
+/*
+ * Invalidate and re-translate all vring addresses after the memory table
+ * has been modified (add/remove region).
+ *
+ * translate_ring_addresses() may call numa_realloc(), which can reallocate
+ * the device structure. The updated pointer is written back through *pdev
+ * so callers must refresh their local "dev" afterwards: dev = *pdev.
+ */
+static void
+vhost_user_invalidate_vrings(struct virtio_net **pdev)
+{
+ struct virtio_net *dev = *pdev;
+ uint32_t i;
+
+ for (i = 0; i < dev->nr_vring; i++) {
+ struct vhost_virtqueue *vq = dev->virtqueue[i];
+
+ if (!vq)
+ continue;
+
+ if (vq->desc || vq->avail || vq->used) {
+ vq_assert_lock(dev, vq);
+
+ vring_invalidate(dev, vq);
+
+ translate_ring_addresses(&dev, &vq);
+ }
+ }
+
+ *pdev = dev;
+}
+
+/*
+ * Macro wrapper that performs the compile-time lock assertion with the
+ * correct message ID at the call site, then calls the implementation.
+ */
+#define dev_invalidate_vrings(pdev, id) do { \
+ static_assert(id ## _LOCK_ALL_QPS, \
+ #id " handler is not declared as locking all queue pairs"); \
+ vhost_user_invalidate_vrings(pdev); \
+} while (0)
+
+static int
+vhost_user_add_mem_reg(struct virtio_net **pdev,
+ struct vhu_msg_context *ctx,
+ int main_fd __rte_unused)
+{
+ struct VhostUserMemoryRegion *region = &ctx->msg.payload.memreg.region; + struct virtio_net *dev = *pdev; + uint32_t i;
+
+ /* convert first region add to normal memory table set */
+ if (dev->mem == NULL) {
+ if (vhost_user_initialize_memory(pdev) < 0)
+ goto close_msg_fds;
+ }
+
+ /* make sure new region will fit */
+ if (dev->mem->nregions >= VHOST_MEMORY_MAX_NREGIONS) {
+ VHOST_CONFIG_LOG(dev->ifname, ERR, "too many memory regions already (%u)",
+ dev->mem->nregions);
+ goto close_msg_fds;
+ }
+
+ /* make sure supplied memory fd present */
+ if (ctx->fd_num != 1) {
+ VHOST_CONFIG_LOG(dev->ifname, ERR, "fd count makes no sense (%u)", ctx->fd_num);
+ goto close_msg_fds;
+ }
+
+ /* Make sure no overlap in guest virtual address space */
+ for (i = 0; i < dev->mem->nregions; i++) {
+ struct rte_vhost_mem_region *cur = &dev->mem->regions[i]; + uint64_t cur_start = cur->guest_user_addr; + uint64_t cur_end = cur_start + cur->size - 1; + uint64_t new_start = region->userspace_addr; + uint64_t new_end = new_start + region->memory_size - 1;
+
+ if (new_end >= cur_start && new_start <= cur_end) {
+ VHOST_CONFIG_LOG(dev->ifname, ERR,
+ "requested memory region overlaps with another region");
+ VHOST_CONFIG_LOG(dev->ifname, ERR,
+ "\tRequested region address:0x%" PRIx64,
+ region->userspace_addr);
+ VHOST_CONFIG_LOG(dev->ifname, ERR,
+ "\tRequested region size:0x%" PRIx64,
+ region->memory_size);
+ VHOST_CONFIG_LOG(dev->ifname, ERR,
+ "\tOverlapping region address:0x%" PRIx64,
+ cur->guest_user_addr);
+ VHOST_CONFIG_LOG(dev->ifname, ERR,
+ "\tOverlapping region size:0x%" PRIx64,
+ cur->size);
+ goto close_msg_fds;
+ }
+ }
+
+ /* New region goes at the end of the contiguous array */
+ struct rte_vhost_mem_region *reg = &dev->mem->regions[dev->mem->nregions];
+
+ reg->guest_phys_addr = region->guest_phys_addr; + reg->guest_user_addr = region->userspace_addr; + reg->size = region->memory_size; + reg->fd = ctx->fds[0]; + ctx->fds[0] = -1;
+
+ if (vhost_user_mmap_region(dev, reg, region->mmap_offset) < 0) {
+ VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap region");
+ if (reg->mmap_addr) {
+ /* mmap succeeded but a later step (e.g. add_guest_pages)
+ * failed; undo the mapping and any guest-page entries.
+ */
+ remove_guest_pages(dev, reg);
+ free_mem_region(reg);
+ } else {
+ close(reg->fd);
+ reg->fd = -1;
+ }
+ goto close_msg_fds;
+ }
+
+ dev->mem->nregions++;
+
+ if (dev->async_copy && rte_vfio_is_enabled("vfio")) {
+ if (async_dma_map_region(dev, reg, true) < 0)
+ goto free_new_region_no_dma;
+ }
+
+ if (dev->postcopy_listening) {
+ /*
+ * Cannot use vhost_user_postcopy_register() here because it + * reads ctx->msg.payload.memory (SET_MEM_TABLE layout), but + * ADD_MEM_REG uses the memreg payload. Register the + * single new region directly instead. + */
+ if (vhost_user_postcopy_region_register(dev, reg) < 0)
+ goto free_new_region;
+ }
+
+ dev_invalidate_vrings(pdev, VHOST_USER_ADD_MEM_REG); + dev = *pdev; + dump_guest_pages(dev); + + /* Reply with the back-end's mapping address per vhost-user spec */ + ctx->msg.payload.memreg.region.userspace_addr = reg->host_user_addr; + ctx->msg.size = sizeof(ctx->msg.payload.memreg); + ctx->fd_num = 0;
+
+ return RTE_VHOST_MSG_RESULT_REPLY;
+
+free_new_region:
+ if (dev->async_copy && rte_vfio_is_enabled("vfio"))
+ async_dma_map_region(dev, reg, false);
+free_new_region_no_dma:
+ remove_guest_pages(dev, reg); + free_mem_region(reg); + dev->mem->nregions--;
+close_msg_fds:
+ close_msg_fds(ctx);
+ return RTE_VHOST_MSG_RESULT_ERR;
+}
+
+static int
+vhost_user_rem_mem_reg(struct virtio_net **pdev,
+ struct vhu_msg_context *ctx,
+ int main_fd __rte_unused)
+{
+ struct VhostUserMemoryRegion *region = &ctx->msg.payload.memreg.region; + struct virtio_net *dev = *pdev; + uint32_t i;
+
+ if (dev->mem == NULL || dev->mem->nregions == 0) {
+ VHOST_CONFIG_LOG(dev->ifname, ERR, "no memory regions to remove");
+ close_msg_fds(ctx);
Not needed if properly declared.
<Pravin> Agreed, removed.
+ return RTE_VHOST_MSG_RESULT_ERR;
+ }
+
+ if (validate_msg_fds(dev, ctx, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
With proper declaration, we can remove this check, as it is done in a generic way.
<Pravin> Agreed, removed -- the framework now rejects unexpected FDs before the handler is called.
+
+ for (i = 0; i < dev->mem->nregions; i++) {
+ struct rte_vhost_mem_region *current_region = &dev->mem->regions[i];
+
+ /*
+ * According to the vhost-user specification:
+ * The memory region to be removed is identified by its GPA,
+ * user address and size. The mmap offset is ignored.
+ */
+ 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"))+ async_dma_map_region(dev, current_region, false);
+ remove_guest_pages(dev, current_region);
You are missing the step to clear the IOTLB cache entries matching with this removed region. In vhost_user_set_mem_table(), a vhost_user_iotlb_flush_all() call is made, but that would be kind of a nuclear option for memory hotplug.
I suggest removing only the entries matching the removed area, something like this:
if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
vhost_user_iotlb_cache_remove(dev, current_region->guest_phys_addr, current_region->size);
<Pravin> Fixed, added targeted vhost_user_iotlb_cache_remove(dev, current_region->guest_phys_addr, current_region->size) guarded by VIRTIO_F_IOMMU_PLATFORM, instead of the nuclear iotlb_flush_all().
+ free_mem_region(current_region);
+
+ /* Compact the regions array to keep it contiguous */
+ if (i < dev->mem->nregions - 1) {
+ memmove(&dev->mem->regions[i],
+ &dev->mem->regions[i + 1], + (dev->mem->nregions - 1 - i) * + sizeof(struct rte_vhost_mem_region));
+ memset(&dev->mem->regions[dev->mem->nregions - 1],
+ 0, sizeof(struct rte_vhost_mem_region));
+ }
+
+ dev->mem->nregions--; + dev_invalidate_vrings(pdev, VHOST_USER_REM_MEM_REG); + dev = *pdev; + close_msg_fds(ctx);
And no need to close FDs, are we are now sure none were provided.
<Pravin> Agreed, removed.
+ return RTE_VHOST_MSG_RESULT_OK;
+ }
+ }
+
+ VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to find region");
+ close_msg_fds(ctx);
Same, no more needed.
<Pravin> Agreed, removed.
+ return RTE_VHOST_MSG_RESULT_ERR;
+}
+
static bool
vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq) {
--
2.43.0
PATCH v14 5/5 vhost: enable configure memory slots
In reply to [email protected]From: Pravin M Bathija <[email protected]>
This patch enables configure memory slots in the header define VHOST_USER_PROTOCOL_FEATURES.
--- lib/vhost/vhost_user.h | 1 + 1 file changed, 1 insertion(+)
lib/vhost/vhost_user.h +1 -0
@@ -32,6 +32,7 @@ (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD) | \ (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \ (1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT) | \ + (1ULL << VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS) | \ (1ULL << VHOST_USER_PROTOCOL_F_STATUS)) typedef enum VhostUserRequest {
Signature · 1 line
2.43.0
Re: [PATCH v14 5/5] vhost: enable configure memory slots
Quoted 34 lines · On Wed, May 20, 2026 at 4:20 AM <[email protected]> wrote:
From: Pravin M Bathija <[email protected]>
This patch enables configure memory slots in the header define
VHOST_USER_PROTOCOL_FEATURES.Signed-off-by Pravin M Bathija <[email protected]>Acked-by Fengchengwen <[email protected]>Reviewed-by Stephen Hemminger <[email protected]>Acked-by Maxime Coquelin <[email protected]>--- lib/vhost/vhost_user.h | 1 + 1 file changed, 1 insertion(+)lib/vhost/vhost_user.h +1
-0@@ -32,6 +32,7 @@ (1ULL <<VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD ) | \ (1ULL <<VHOST_USER_PROTOCOL_F_HOST_NOTIFIER ) | \ (1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT) | \ + (1ULL <<VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS) | \ (1ULL <<
VHOST_USER_PROTOCOL_F_STATUS))typedef enum VhostUserRequest {
--
2.43.0Reviewed-by Maxime Coquelin <[email protected]>