From 0a089121f6f2be223318e8c4387c47bab6ca2198 Mon Sep 17 00:00:00 2001 From: Vitaliy Mysak Date: Thu, 3 Oct 2019 17:25:29 +0200 Subject: [PATCH] vhost: move memory registration functions to vhost_compat.c This change will allow us to move other DPDK specific functions to rte_vhost_compat.c, such as session callbacks which are the only consumers of these memory management functions. Change-Id: Ieb7b3f08ddf2e7cf04ecf18e8af4ad04124ccfea Signed-off-by: Vitaliy Mysak Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/470458 Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Shuhei Matsumoto --- lib/vhost/rte_vhost_compat.c | 57 ++++++++++++++++++++++++++++++++++++ lib/vhost/vhost.c | 56 ----------------------------------- lib/vhost/vhost_internal.h | 6 ++++ 3 files changed, 63 insertions(+), 56 deletions(-) diff --git a/lib/vhost/rte_vhost_compat.c b/lib/vhost/rte_vhost_compat.c index 60ba72979..c93357de1 100644 --- a/lib/vhost/rte_vhost_compat.c +++ b/lib/vhost/rte_vhost_compat.c @@ -47,6 +47,63 @@ #include "vhost_internal.h" #include "spdk_internal/vhost_user.h" +#include "spdk_internal/memory.h" + +void +vhost_session_mem_register(struct rte_vhost_memory *mem) +{ + struct rte_vhost_mem_region *region; + uint32_t i; + uint64_t previous_start = UINT64_MAX; + + for (i = 0; i < mem->nregions; i++) { + uint64_t start, end, len; + region = &mem->regions[i]; + start = FLOOR_2MB(region->mmap_addr); + end = CEIL_2MB(region->mmap_addr + region->mmap_size); + if (start == previous_start) { + start += (size_t) VALUE_2MB; + } + previous_start = start; + len = end - start; + SPDK_INFOLOG(SPDK_LOG_VHOST, "Registering VM memory for vtophys translation - 0x%jx len:0x%jx\n", + start, len); + + if (spdk_mem_register((void *)start, len) != 0) { + SPDK_WARNLOG("Failed to register memory region %"PRIu32". Future vtophys translation might fail.\n", + i); + continue; + } + } +} + +void +vhost_session_mem_unregister(struct rte_vhost_memory *mem) +{ + struct rte_vhost_mem_region *region; + uint32_t i; + uint64_t previous_start = UINT64_MAX; + + for (i = 0; i < mem->nregions; i++) { + uint64_t start, end, len; + region = &mem->regions[i]; + start = FLOOR_2MB(region->mmap_addr); + end = CEIL_2MB(region->mmap_addr + region->mmap_size); + if (start == previous_start) { + start += (size_t) VALUE_2MB; + } + previous_start = start; + len = end - start; + + if (spdk_vtophys((void *) start, NULL) == SPDK_VTOPHYS_ERROR) { + continue; /* region has not been registered */ + } + + if (spdk_mem_unregister((void *)start, len) != 0) { + assert(false); + } + } +} static int new_connection(int vid) diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c index 9ca7af9ac..8f8413607 100644 --- a/lib/vhost/vhost.c +++ b/lib/vhost/vhost.c @@ -524,62 +524,6 @@ vhost_session_find_by_vid(int vid) return NULL; } -static void -vhost_session_mem_register(struct rte_vhost_memory *mem) -{ - struct rte_vhost_mem_region *region; - uint32_t i; - uint64_t previous_start = UINT64_MAX; - - for (i = 0; i < mem->nregions; i++) { - uint64_t start, end, len; - region = &mem->regions[i]; - start = FLOOR_2MB(region->mmap_addr); - end = CEIL_2MB(region->mmap_addr + region->mmap_size); - if (start == previous_start) { - start += (size_t) VALUE_2MB; - } - previous_start = start; - len = end - start; - SPDK_INFOLOG(SPDK_LOG_VHOST, "Registering VM memory for vtophys translation - 0x%jx len:0x%jx\n", - start, len); - - if (spdk_mem_register((void *)start, len) != 0) { - SPDK_WARNLOG("Failed to register memory region %"PRIu32". Future vtophys translation might fail.\n", - i); - continue; - } - } -} - -static void -vhost_session_mem_unregister(struct rte_vhost_memory *mem) -{ - struct rte_vhost_mem_region *region; - uint32_t i; - uint64_t previous_start = UINT64_MAX; - - for (i = 0; i < mem->nregions; i++) { - uint64_t start, end, len; - region = &mem->regions[i]; - start = FLOOR_2MB(region->mmap_addr); - end = CEIL_2MB(region->mmap_addr + region->mmap_size); - if (start == previous_start) { - start += (size_t) VALUE_2MB; - } - previous_start = start; - len = end - start; - - if (spdk_vtophys((void *) start, NULL) == SPDK_VTOPHYS_ERROR) { - continue; /* region has not been registered */ - } - - if (spdk_mem_unregister((void *)start, len) != 0) { - assert(false); - } - } -} - struct spdk_vhost_dev * spdk_vhost_dev_next(struct spdk_vhost_dev *vdev) { diff --git a/lib/vhost/vhost_internal.h b/lib/vhost/vhost_internal.h index 7df9ab23b..dc80165b7 100644 --- a/lib/vhost/vhost_internal.h +++ b/lib/vhost/vhost_internal.h @@ -330,6 +330,12 @@ int vhost_set_config_cb(int vid, uint8_t *config, uint32_t offset, uint32_t size, uint32_t flags) #endif +/* + * Memory registration functions used in start/stop device callbacks + */ +void vhost_session_mem_register(struct rte_vhost_memory *mem); +void vhost_session_mem_unregister(struct rte_vhost_memory *mem); + /* * Call a function for each session of the provided vhost device. * The function will be called one-by-one on each session's thread.