diff --git a/module/bdev/nvme/bdev_nvme.c b/module/bdev/nvme/bdev_nvme.c index 1f5439404c..746d097ba5 100644 --- a/module/bdev/nvme/bdev_nvme.c +++ b/module/bdev/nvme/bdev_nvme.c @@ -1897,7 +1897,9 @@ nvme_ctrlr_create(struct spdk_nvme_ctrlr *ctrlr, nvme_ctrlr->adminq_timer_poller = SPDK_POLLER_REGISTER(bdev_nvme_poll_adminq, nvme_ctrlr, g_opts.nvme_adminq_poll_period_us); + pthread_mutex_lock(&g_bdev_nvme_mutex); TAILQ_INSERT_TAIL(&g_nvme_ctrlrs, nvme_ctrlr, tailq); + pthread_mutex_unlock(&g_bdev_nvme_mutex); if (g_opts.timeout_us > 0) { spdk_nvme_ctrlr_register_timeout_callback(ctrlr, g_opts.timeout_us, diff --git a/module/bdev/nvme/common.c b/module/bdev/nvme/common.c index 634bc7e026..fc7db2d519 100644 --- a/module/bdev/nvme/common.c +++ b/module/bdev/nvme/common.c @@ -44,13 +44,15 @@ nvme_ctrlr_get(const struct spdk_nvme_transport_id *trid) { struct nvme_ctrlr *nvme_ctrlr; + pthread_mutex_lock(&g_bdev_nvme_mutex); TAILQ_FOREACH(nvme_ctrlr, &g_nvme_ctrlrs, tailq) { if (spdk_nvme_transport_id_compare(trid, nvme_ctrlr->connected_trid) == 0) { - return nvme_ctrlr; + break; } } + pthread_mutex_unlock(&g_bdev_nvme_mutex); - return NULL; + return nvme_ctrlr; } struct nvme_ctrlr * @@ -62,13 +64,15 @@ nvme_ctrlr_get_by_name(const char *name) return NULL; } + pthread_mutex_lock(&g_bdev_nvme_mutex); TAILQ_FOREACH(nvme_ctrlr, &g_nvme_ctrlrs, tailq) { if (strcmp(name, nvme_ctrlr->name) == 0) { - return nvme_ctrlr; + break; } } + pthread_mutex_unlock(&g_bdev_nvme_mutex); - return NULL; + return nvme_ctrlr; } void @@ -76,9 +80,11 @@ nvme_ctrlr_for_each(nvme_ctrlr_for_each_fn fn, void *ctx) { struct nvme_ctrlr *nvme_ctrlr; + pthread_mutex_lock(&g_bdev_nvme_mutex); TAILQ_FOREACH(nvme_ctrlr, &g_nvme_ctrlrs, tailq) { fn(nvme_ctrlr, ctx); } + pthread_mutex_unlock(&g_bdev_nvme_mutex); } void