From 253cca4fc3a89c38e79d2e940c5a0b7bb082afcc Mon Sep 17 00:00:00 2001
From: Zhanghongtao2417 <651380626@qq.com>
Date: Fri, 26 Apr 2024 22:01:25 +0800
Subject: [PATCH] nvme/cuse: Add ctrlr_lock for cuse register and unregister

conflicts:
lib/nvme/nvme_io_msg.c  nvme_io_msg_ctrlr_update

spdk_nvme_cuse_unregister and spdk_nvme_ctrlr_process_admin_completions
 running at the same time, concurrently operate external_io_msgs.
So we add locks to protect.

Fixes #3353

Change-Id: Id5176975676c29a475e8e2a0d7c93e44646c00dc
Signed-off-by: Zhanghongtao2417 <651380626@qq.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/22927
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: Jim Harris <jim.harris@samsung.com>
Reviewed-by: Konrad Sztyber <konrad.sztyber@intel.com>

---
 lib/nvme/nvme_io_msg.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/lib/nvme/nvme_io_msg.c b/lib/nvme/nvme_io_msg.c
index 94c4d07..9f5ee6d 100644
--- a/lib/nvme/nvme_io_msg.c
+++ b/lib/nvme/nvme_io_msg.c
@@ -111,13 +111,16 @@ nvme_io_msg_ctrlr_register(struct spdk_nvme_ctrlr *ctrlr,
 		return -EINVAL;
 	}
 
+	nvme_robust_mutex_lock(&ctrlr->ctrlr_lock);
 	if (nvme_io_msg_is_producer_registered(ctrlr, io_msg_producer)) {
+		nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock);
 		return -EEXIST;
 	}
 
 	if (!STAILQ_EMPTY(&ctrlr->io_producers) || ctrlr->is_resetting) {
 		/* There are registered producers - IO messaging already started */
 		STAILQ_INSERT_TAIL(&ctrlr->io_producers, io_msg_producer, link);
+		nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock);
 		return 0;
 	}
 
@@ -129,6 +132,7 @@ nvme_io_msg_ctrlr_register(struct spdk_nvme_ctrlr *ctrlr,
 	ctrlr->external_io_msgs = spdk_ring_create(SPDK_RING_TYPE_MP_SC, 65536, SPDK_ENV_SOCKET_ID_ANY);
 	if (!ctrlr->external_io_msgs) {
 		SPDK_ERRLOG("Unable to allocate memory for message ring\n");
+		nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock);
 		return -ENOMEM;
 	}
 
@@ -141,6 +145,7 @@ nvme_io_msg_ctrlr_register(struct spdk_nvme_ctrlr *ctrlr,
 	}
 
 	STAILQ_INSERT_TAIL(&ctrlr->io_producers, io_msg_producer, link);
+	nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock);
 
 	return 0;
 }
@@ -156,9 +161,11 @@ nvme_io_msg_ctrlr_update(struct spdk_nvme_ctrlr *ctrlr)
 	}
 
 	/* Update all producers */
+	nvme_robust_mutex_lock(&ctrlr->ctrlr_lock);
 	STAILQ_FOREACH(io_msg_producer, &ctrlr->io_producers, link) {
 		io_msg_producer->update(ctrlr);
 	}
+	nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock);
 }
 
 void
@@ -195,7 +202,9 @@ nvme_io_msg_ctrlr_unregister(struct spdk_nvme_ctrlr *ctrlr,
 {
 	assert(io_msg_producer != NULL);
 
+	nvme_robust_mutex_lock(&ctrlr->ctrlr_lock);
 	if (!nvme_io_msg_is_producer_registered(ctrlr, io_msg_producer)) {
+		nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock);
 		return;
 	}
 
@@ -203,4 +212,5 @@ nvme_io_msg_ctrlr_unregister(struct spdk_nvme_ctrlr *ctrlr,
 	if (STAILQ_EMPTY(&ctrlr->io_producers)) {
 		nvme_io_msg_ctrlr_detach(ctrlr);
 	}
+	nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock);	
 }
-- 
2.27.0

