[PATCH 07/23] nvmet: fix nvmet_ns::nsid access using nvmet_subsys::lock

Nilay Shroff nilay at linux.ibm.com
Sun Nov 8 19:28:12 PST 2026


nvmet_ns::nsid is protected by nvmet_subsys::lock. However, the Clang
context analyzer reports errors in nvmet_format_ana_group(),
nvmet_execute_identify_endgrp_list() and nvmet_execute_identify_nslist()
because these functions access ns->nsid while iterating over enabled
namespaces without holding the corresponding lock:

drivers/nvme/target/admin-cmd.c:493:44: error: reading variable 'nsid' requires holding mutex 'ns->subsys->lock'
      [-Werror,-Wthread-safety-precise]
  493 |                                 desc->nsids[count++] = cpu_to_le32(ns->nsid);
      |                                                                        ^

drivers/nvme/target/admin-cmd.c:955:11: error: reading variable 'nsid' requires holding mutex 'ns->subsys->lock'
      [-Werror,-Wthread-safety-analysis]
  955 |                 if (ns->nsid <= min_endgid)
      |                         ^

drivers/nvme/target/admin-cmd.c:1002:31: error: reading variable 'nsid' requires holding mutex 'ns->subsys->lock'
      [-Werror,-Wthread-safety-analysis]
 1002 |                 list[i++] = cpu_to_le32(ns->nsid);

Fix above errors, acquiring ctrl->subsys->lock while iterating over
enabled namespaces in these functions to protect accesses to namespace
fields.

Since the context analyzer cannot infer that ctrl->subsys->lock and
ns->subsys->lock refer to the same mutex, explicitly assert the
ns->subsys->lock capability using __assume_ctx_lock() inside each
iteration.

Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
---
 drivers/nvme/target/admin-cmd.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index 590ca68588f8..761caac84255 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -512,10 +512,19 @@ static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
 	u32 count = 0;
 
 	if (!(req->cmd->get_log_page.lsp & NVME_ANA_LOG_RGO)) {
+		mutex_lock(&ctrl->subsys->lock);
 		nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
+			/*
+			 * Both ctrl->subsys->lock and ns->subsys->lock refers
+			 * to the same lock. However there's no way for us to
+			 * annotate such locking semantics using clang context
+			 * annotation. So explicitly assert the capability.
+			 */
+			__assume_ctx_lock(&ns->subsys->lock);
 			if (ns->anagrpid == grpid)
 				desc->nsids[count++] = cpu_to_le32(ns->nsid);
 		}
+		mutex_unlock(&ctrl->subsys->lock);
 	}
 
 	desc->grpid = cpu_to_le32(grpid);
@@ -966,7 +975,16 @@ static void nvmet_execute_identify_endgrp_list(struct nvmet_req *req)
 		goto out;
 	}
 
+	mutex_lock(&ctrl->subsys->lock);
 	nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
+		/*
+		 * Both ctrl->subsys->lock and ns->subsys->lock refers to the
+		 * same lock. However there's no way for us to annotate such
+		 * locking semantics using clang context annotation. So
+		 * explicitly assert the capability.
+		 */
+		__assume_ctx_lock(&ns->subsys->lock);
+
 		if (ns->nsid <= min_endgid)
 			continue;
 
@@ -974,6 +992,7 @@ static void nvmet_execute_identify_endgrp_list(struct nvmet_req *req)
 		if (i == buf_size / sizeof(__le16))
 			break;
 	}
+	mutex_unlock(&ctrl->subsys->lock);
 
 	list[0] = cpu_to_le16(i - 1);
 	status = nvmet_copy_to_sgl(req, 0, list, buf_size);
@@ -1009,7 +1028,16 @@ static void nvmet_execute_identify_nslist(struct nvmet_req *req, bool match_css)
 		goto out;
 	}
 
+	mutex_lock(&ctrl->subsys->lock);
 	nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
+		/*
+		 * Both ctrl->subsys->lock and ns->subsys->lock refers to the
+		 * same lock. However there's no way for us to annotate such
+		 * locking semantics using clang context annotation. So
+		 * explicitly assert the capability.
+		 */
+		__assume_ctx_lock(&ns->subsys->lock);
+
 		if (ns->nsid <= min_nsid)
 			continue;
 		if (match_css && ns->csi != req->cmd->identify.csi)
@@ -1018,6 +1046,7 @@ static void nvmet_execute_identify_nslist(struct nvmet_req *req, bool match_css)
 		if (i == buf_size / sizeof(__le32))
 			break;
 	}
+	mutex_unlock(&ctrl->subsys->lock);
 
 	status = nvmet_copy_to_sgl(req, 0, list, buf_size);
 
-- 
2.53.0




More information about the Linux-nvme mailing list