[PATCH v2 10/22] nvmet: fix nvmet_ns::uuid access using nvmet_subsys::lock
Nilay Shroff
nilay at linux.ibm.com
Tue Nov 17 23:34:51 PST 2026
The nvmet_ns::uuid field is protected by nvmet_subsys::lock. However,
nvmet_ns_alloc() calls uuid_gen() without holding this lock, triggering
a Clang context analyzer error:
drivers/nvme/target/core.c:781:16: error: passing pointer to variable 'uuid' requires holding mutex '_res->subsys->lock'
[-Werror,-Wthread-safety-pointer]
781 | uuid_gen(&ns->uuid);
| ^
Fix this by separating namespace allocation from initialization.
Rename nvmet_ns_alloc() to nvmet_ns_alloc_and_init() and introduce
nvmet_ns_init(). Move part of the namespace initialization into a new
nvmet_ns_init(), Annotate nvmet_ns_init() with
__must_hold(&subsys->lock) to document and validate this requirement.
Although subsys->lock and ns->subsys->lock refer to the same mutex, the
Clang context analyzer cannot infer this relationship. Add
lockdep_assert_held(&ns->subsys->lock) in nvmet_ns_init() so that the
context analyzer can satisfy the corresponding lock annotation.
Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
---
drivers/nvme/target/configfs.c | 2 +-
drivers/nvme/target/core.c | 48 ++++++++++++++++++++++------------
drivers/nvme/target/nvmet.h | 2 +-
3 files changed, 33 insertions(+), 19 deletions(-)
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index f8a4d0d88946..10249affcccc 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -899,7 +899,7 @@ static struct config_group *nvmet_ns_make(struct config_group *group,
}
ret = -ENOMEM;
- ns = nvmet_ns_alloc(subsys, nsid);
+ ns = nvmet_ns_alloc_and_init(subsys, nsid);
if (!ns)
goto out;
config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index b031595d8edb..6b0ec84a4c09 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -723,7 +723,36 @@ void nvmet_ns_free(struct nvmet_ns *ns)
kfree(ns);
}
-struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
+static int nvmet_ns_init(struct nvmet_subsys *subsys, struct nvmet_ns *ns,
+ u32 nsid)
+ __must_hold(&subsys->lock)
+{
+ int ret;
+
+ ns->subsys = subsys;
+
+ lockdep_assert_held(&ns->subsys->lock);
+
+ init_completion(&ns->disable_done);
+ ns->nsid = nsid;
+
+ ret = xa_insert(&subsys->namespaces, ns->nsid, ns, GFP_KERNEL);
+ if (ret)
+ return ret;
+
+ if (ns->nsid > subsys->max_nsid)
+ subsys->max_nsid = nsid;
+
+ subsys->nr_namespaces++;
+
+ uuid_gen(&ns->uuid);
+ ns->buffered_io = false;
+ ns->csi = NVME_CSI_NVM;
+
+ return ret;
+}
+
+struct nvmet_ns *nvmet_ns_alloc_and_init(struct nvmet_subsys *subsys, u32 nsid)
{
struct nvmet_ns *ns;
@@ -736,19 +765,9 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
if (!ns)
goto out_unlock;
- init_completion(&ns->disable_done);
-
- ns->nsid = nsid;
- ns->subsys = subsys;
-
- if (ns->nsid > subsys->max_nsid)
- subsys->max_nsid = nsid;
-
- if (xa_insert(&subsys->namespaces, ns->nsid, ns, GFP_KERNEL))
+ if (nvmet_ns_init(subsys, ns, nsid))
goto out_exit;
- subsys->nr_namespaces++;
-
mutex_unlock(&subsys->lock);
down_write(&nvmet_ana_sem);
@@ -756,13 +775,8 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
nvmet_ana_group_enabled[ns->anagrpid]++;
up_write(&nvmet_ana_sem);
- uuid_gen(&ns->uuid);
- ns->buffered_io = false;
- ns->csi = NVME_CSI_NVM;
-
return ns;
out_exit:
- subsys->max_nsid = nvmet_max_nsid(subsys);
kfree(ns);
out_unlock:
mutex_unlock(&subsys->lock);
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 847115759985..01132beb5904 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -702,7 +702,7 @@ u16 nvmet_req_find_ns(struct nvmet_req *req);
void nvmet_put_namespace(struct nvmet_ns *ns);
int nvmet_ns_enable(struct nvmet_ns *ns);
void nvmet_ns_disable(struct nvmet_ns *ns);
-struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
+struct nvmet_ns *nvmet_ns_alloc_and_init(struct nvmet_subsys *subsys, u32 nsid);
void nvmet_ns_free(struct nvmet_ns *ns);
void nvmet_send_ana_event(struct nvmet_subsys *subsys,
--
2.53.0
More information about the Linux-nvme
mailing list