From 89e47f60148b16b1021030f6d25aab142b6392b5 Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Thu, 28 May 2020 13:10:30 -0700 Subject: [PATCH] nvme: create netlink socket during nvme_driver_init This helps ensure thread safety on creation of the netlink socket, when probe is called from multiple threads at once. It is also a lot more clean - we just create it once, rather than checking every time probe is called to see if it has to be created. Signed-off-by: Jim Harris Change-Id: I528cedc3ff44de6ea8ecaf6d2389226502ba408e Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/2681 Community-CI: Broadcom CI Reviewed-by: Changpeng Liu Reviewed-by: Tomasz Zawadzki Reviewed-by: Aleksey Marchuk Tested-by: SPDK CI Jenkins --- lib/nvme/nvme.c | 5 +++++ lib/nvme/nvme_internal.h | 3 +++ lib/nvme/nvme_pcie.c | 16 ++++++---------- test/unit/lib/nvme/nvme.c/nvme_ut.c | 1 + 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/nvme/nvme.c b/lib/nvme/nvme.c index bf3611990..35cef1ac7 100644 --- a/lib/nvme/nvme.c +++ b/lib/nvme/nvme.c @@ -35,6 +35,7 @@ #include "spdk/string.h" #include "nvme_internal.h" #include "nvme_io_msg.h" +#include "nvme_uevent.h" #define SPDK_NVME_DRIVER_NAME "spdk_nvme_driver" @@ -438,6 +439,10 @@ nvme_driver_init(void) nvme_robust_mutex_lock(&g_spdk_nvme_driver->lock); g_spdk_nvme_driver->initialized = false; + g_spdk_nvme_driver->hotplug_fd = spdk_uevent_connect(); + if (g_spdk_nvme_driver->hotplug_fd < 0) { + SPDK_DEBUGLOG(SPDK_LOG_NVME, "Failed to open uevent netlink socket\n"); + } TAILQ_INIT(&g_spdk_nvme_driver->shared_attached_ctrlrs); diff --git a/lib/nvme/nvme_internal.h b/lib/nvme/nvme_internal.h index d259e362c..b14f4d173 100644 --- a/lib/nvme/nvme_internal.h +++ b/lib/nvme/nvme_internal.h @@ -798,6 +798,9 @@ struct nvme_driver { bool initialized; struct spdk_uuid default_extended_host_id; + + /** netlink socket fd for hotplug messages */ + int hotplug_fd; }; extern struct nvme_driver *g_spdk_nvme_driver; diff --git a/lib/nvme/nvme_pcie.c b/lib/nvme/nvme_pcie.c index a1484a0fa..1e69281d5 100644 --- a/lib/nvme/nvme_pcie.c +++ b/lib/nvme/nvme_pcie.c @@ -214,7 +214,6 @@ static int nvme_pcie_qpair_destroy(struct spdk_nvme_qpair *qpair); __thread struct nvme_pcie_ctrlr *g_thread_mmio_ctrlr = NULL; static uint16_t g_signal_lock; static bool g_sigset = false; -static int g_hotplug_fd = -1; static void nvme_sigbus_fault_sighandler(int signum, siginfo_t *info, void *ctx) @@ -273,7 +272,11 @@ _nvme_pcie_hotplug_monitor(struct spdk_nvme_probe_ctx *probe_ctx) union spdk_nvme_csts_register csts; struct spdk_nvme_ctrlr_process *proc; - while (spdk_get_uevent(g_hotplug_fd, &event) > 0) { + if (g_spdk_nvme_driver->hotplug_fd < 0) { + return 0; + } + + while (spdk_get_uevent(g_spdk_nvme_driver->hotplug_fd, &event) > 0) { if (event.subsystem == SPDK_NVME_UEVENT_SUBSYSTEM_UIO || event.subsystem == SPDK_NVME_UEVENT_SUBSYSTEM_VFIO) { if (event.action == SPDK_NVME_UEVENT_ADD) { @@ -778,14 +781,7 @@ nvme_pcie_ctrlr_scan(struct spdk_nvme_probe_ctx *probe_ctx, /* Only the primary process can monitor hotplug. */ if (spdk_process_is_primary()) { - if (g_hotplug_fd < 0) { - g_hotplug_fd = spdk_uevent_connect(); - if (g_hotplug_fd < 0) { - SPDK_DEBUGLOG(SPDK_LOG_NVME, "Failed to open uevent netlink socket\n"); - } - } else { - _nvme_pcie_hotplug_monitor(probe_ctx); - } + _nvme_pcie_hotplug_monitor(probe_ctx); } if (enum_ctx.has_pci_addr == false) { diff --git a/test/unit/lib/nvme/nvme.c/nvme_ut.c b/test/unit/lib/nvme/nvme.c/nvme_ut.c index 6d6cbda86..f6a74ee62 100644 --- a/test/unit/lib/nvme/nvme.c/nvme_ut.c +++ b/test/unit/lib/nvme/nvme.c/nvme_ut.c @@ -63,6 +63,7 @@ DEFINE_STUB(nvme_transport_ctrlr_construct, struct spdk_nvme_ctrlr *, DEFINE_STUB_V(nvme_io_msg_ctrlr_detach, (struct spdk_nvme_ctrlr *ctrlr)); DEFINE_STUB(spdk_nvme_transport_available, bool, (enum spdk_nvme_transport_type trtype), true); +DEFINE_STUB(spdk_uevent_connect, int, (void), 1); static bool ut_destruct_called = false;