From b4692083f1b492185cf9f69833a006f6e3344906 Mon Sep 17 00:00:00 2001 From: Ziye Yang Date: Tue, 4 Dec 2018 10:09:11 +0800 Subject: [PATCH] nvme: Fix the race condition in nvme_ctrlr_get_cc When the applications call spdk_nvme_ctrlr_alloc_io_qpair, there will be cmd to the admin qpairs in nvme_ctrlr_get_cc, so there is contention. We should use the lock to protect nvme_ctrl_get_cc. Otherwise, the multiple threads will have contention on the admin qpair, thus there will be coredump issue. We get the bug when testing NVMe-oF TCP transport, and this patch can address this issue. Change-Id: I7247f98cdf890c2eafaf8fb94580ecd714010bd5 Signed-off-by: Ziye Yang Reviewed-on: https://review.gerrithub.io/435577 Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Seth Howell Reviewed-by: Shuhei Matsumoto Reviewed-by: Ben Walker --- lib/nvme/nvme_ctrlr.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/nvme/nvme_ctrlr.c b/lib/nvme/nvme_ctrlr.c index ddef7399b..ec60ce438 100644 --- a/lib/nvme/nvme_ctrlr.c +++ b/lib/nvme/nvme_ctrlr.c @@ -248,13 +248,16 @@ spdk_nvme_ctrlr_alloc_io_qpair(struct spdk_nvme_ctrlr *ctrlr, memcpy(&opts, user_opts, spdk_min(sizeof(opts), opts_size)); } + nvme_robust_mutex_lock(&ctrlr->ctrlr_lock); if (nvme_ctrlr_get_cc(ctrlr, &cc)) { SPDK_ERRLOG("get_cc failed\n"); + nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock); return NULL; } /* Only the low 2 bits (values 0, 1, 2, 3) of QPRIO are valid. */ if ((opts.qprio & 3) != opts.qprio) { + nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock); return NULL; } @@ -264,11 +267,10 @@ spdk_nvme_ctrlr_alloc_io_qpair(struct spdk_nvme_ctrlr *ctrlr, */ if ((cc.bits.ams == SPDK_NVME_CC_AMS_RR) && (opts.qprio != SPDK_NVME_QPRIO_URGENT)) { SPDK_ERRLOG("invalid queue priority for default round robin arbitration method\n"); + nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock); return NULL; } - nvme_robust_mutex_lock(&ctrlr->ctrlr_lock); - /* * Get the first available I/O queue ID. */