All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nilay Shroff <nilay@linux.ibm.com>
To: kbusch@kernel.org, hch@lst.de, bvanassche@acm.org,
	sagi@grimberg.me, axboe@kernel.dk, chaitanyak@nvidia.com
Cc: gjoyce@linux.ibm.com, elver@google.com,
	linux-nvme@lists.infradead.org,
	Nilay Shroff <nilay@linux.ibm.com>
Subject: [PATCH v2 10/22] nvmet: fix nvmet_ns::uuid access using nvmet_subsys::lock
Date: Wed, 18 Nov 2026 13:04:51 +0530	[thread overview]
Message-ID: <20261118073505.288910-11-nilay@linux.ibm.com> (raw)
In-Reply-To: <20261118073505.288910-1-nilay@linux.ibm.com>

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@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



  parent reply	other threads:[~2026-11-18  7:36 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 02/22] nvmet: fix nvmet_ns::bdev access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 03/22] nvmet: add context annotation for nvmet_ns::file Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 04/22] nvmet: add context annotation for nvmet_ns::bdev_file Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 05/22] nvmet: add context annotation for nvmet_ns::readonly Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 06/22] nvmet: add context annotation for nvmet_ns::blksize_shift Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 07/22] nvmet: add context annotation for nvmet_ns::size Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 09/22] nvmet: add context annotation for nvmet_ns::uuid Nilay Shroff
2026-11-18  7:34 ` Nilay Shroff [this message]
2026-11-18  7:34 ` [PATCH v2 11/22] nvmet: add context annotation for nvmet_ns::buffered_io Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 12/22] nvmet: fix nvmet_ns::buffered_io access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 13/22] nvmet: add context annotation for nvmet_ns::enabled Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 14/22] nvmet: fix nvmet_ns::enable access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 15/22] nvmet: add context annotation for nvmet_ns::device_path Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 16/22] nvmet: fix nvmet_ns::device_path access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 17/22] nvmet: add context annotation for PCI P2P parameters in struct nvmet_ns Nilay Shroff
2026-11-18  7:35 ` [PATCH v2 19/22] nvmet: add context annotation for integrity params " Nilay Shroff
2026-11-18  7:35 ` [PATCH v2 20/22] nvmet: add context annotation for nvmet_ns::csi Nilay Shroff
2026-11-18  7:35 ` [PATCH v2 21/22] nvmet: fix nvmet_ns::csi access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:35 ` [PATCH v2 22/22] nvmet: add context annotation for nvmet_ns::debugfs_dir Nilay Shroff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118073505.288910-11-nilay@linux.ibm.com \
    --to=nilay@linux.ibm.com \
    --cc=axboe@kernel.dk \
    --cc=bvanassche@acm.org \
    --cc=chaitanyak@nvidia.com \
    --cc=elver@google.com \
    --cc=gjoyce@linux.ibm.com \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.