diff --git a/lib/vhost/rte_vhost_user.c b/lib/vhost/rte_vhost_user.c index 397be22576..059b5ace1f 100644 --- a/lib/vhost/rte_vhost_user.c +++ b/lib/vhost/rte_vhost_user.c @@ -47,6 +47,22 @@ #include "spdk_internal/vhost_user.h" char g_vhost_user_dev_dirname[PATH_MAX] = ""; +sem_t g_dpdk_sem; + +static void __attribute__((constructor)) +_vhost_user_sem_init(void) +{ + if (sem_init(&g_dpdk_sem, 0, 0) != 0) { + SPDK_ERRLOG("Failed to initialize semaphore for rte_vhost pthread.\n"); + abort(); + } +} + +static void __attribute__((destructor)) +_vhost_user_sem_destroy(void) +{ + sem_destroy(&g_dpdk_sem); +} static inline void vhost_session_mem_region_calc(uint64_t *previous_start, uint64_t *start, uint64_t *end, diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c index 400c268662..97eddc000d 100644 --- a/lib/vhost/vhost.c +++ b/lib/vhost/vhost.c @@ -51,13 +51,6 @@ static struct spdk_thread *g_vhost_init_thread; static spdk_vhost_fini_cb g_fini_cpl_cb; -/** - * DPDK calls our callbacks synchronously but the work those callbacks - * perform needs to be async. Luckily, all DPDK callbacks are called on - * a DPDK-internal pthread, so we'll just wait on a semaphore in there. - */ -static sem_t g_dpdk_sem; - /** Return code for the current DPDK callback */ static int g_dpdk_response; @@ -1524,8 +1517,8 @@ spdk_vhost_init(spdk_vhost_init_cb init_cb) if (g_vhost_user_dev_dirname[0] == '\0') { if (getcwd(g_vhost_user_dev_dirname, sizeof(g_vhost_user_dev_dirname) - 1) == NULL) { SPDK_ERRLOG("getcwd failed (%d): %s\n", errno, spdk_strerror(errno)); - ret = -1; - goto out; + init_cb(-1); + return; } len = strlen(g_vhost_user_dev_dirname); @@ -1535,18 +1528,10 @@ spdk_vhost_init(spdk_vhost_init_cb init_cb) } } - ret = sem_init(&g_dpdk_sem, 0, 0); - if (ret != 0) { - SPDK_ERRLOG("Failed to initialize semaphore for rte_vhost pthread.\n"); - ret = -1; - goto out; - } - spdk_cpuset_zero(&g_vhost_core_mask); SPDK_ENV_FOREACH_CORE(i) { spdk_cpuset_set_cpu(&g_vhost_core_mask, i, true); } -out: init_cb(ret); } @@ -1565,9 +1550,6 @@ vhost_fini(void *arg1) } spdk_vhost_unlock(); - /* All devices are removed now. */ - sem_destroy(&g_dpdk_sem); - g_fini_cpl_cb(); } diff --git a/lib/vhost/vhost_internal.h b/lib/vhost/vhost_internal.h index d76227bfc8..b2068f6e9f 100644 --- a/lib/vhost/vhost_internal.h +++ b/lib/vhost/vhost_internal.h @@ -47,6 +47,13 @@ extern bool g_packed_ring_recovery; +/** + * DPDK calls our callbacks synchronously but the work those callbacks + * perform needs to be async. Luckily, all DPDK callbacks are called on + * a DPDK-internal pthread, so we'll just wait on a semaphore in there. + */ +extern sem_t g_dpdk_sem; + #define SPDK_VHOST_MAX_VQUEUES 256 #define SPDK_VHOST_MAX_VQ_SIZE 1024