[PATCH v2 21/22] nvmet: fix nvmet_ns::csi access using nvmet_subsys::lock
Nilay Shroff
nilay at linux.ibm.com
Tue Nov 17 23:35:02 PST 2026
The nvmet_ns::csi field is protected by nvmet_subsys::lock.
However, nvmet_execute_identify_nslist() accesses ns->csi without
holding the corresponding lock, triggering a Clang context analyzer
error:
drivers/nvme/target/admin-cmd.c:1016:24: error: reading variable 'csi' requires holding mutex 'ns->subsys->lock'
[-Werror,-Wthread-safety-analysis]
1016 | if (match_css && ns->csi != req->cmd->identify.csi)
| ^
Fix this by acquiring ctrl->subsys->lock before iterating over the
enabled namespaces.
Although ctrl->subsys->lock and ns->subsys->lock refer to the same
mutex, the Clang context analyzer cannot recognize that they represent
the same lock because they are accessed through different expressions.
Explicitly assert the ns->subsys->lock capability using
__assume_ctx_lock() before accessing ns->csi, and release the assumed
capability using __release_ctx_lock() afterward.
Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index b0ec1ed7ddfa..de13614b3cb0 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -1015,15 +1015,26 @@ 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) {
- if (ns->nsid <= min_nsid)
- continue;
- if (match_css && ns->csi != req->cmd->identify.csi)
- continue;
- list[i++] = cpu_to_le32(ns->nsid);
+ /*
+ * Both ctrl->subsys->lock and ns->subsys->lock refer to the
+ * same lock. However, there's no way 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 &&
+ (!match_css || ns->csi == req->cmd->identify.csi))
+ list[i++] = cpu_to_le32(ns->nsid);
+
+ __release_ctx_lock(&ns->subsys->lock);
+
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