Linux-NVME Archive on 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 03/22] nvmet: add context annotation for nvmet_ns::file
Date: Wed, 18 Nov 2026 13:04:44 +0530	[thread overview]
Message-ID: <20261118073505.288910-4-nilay@linux.ibm.com> (raw)
In-Reply-To: <20261118073505.288910-1-nilay@linux.ibm.com>

The nvmet_ns::file field is protected by nvmet_subsys::lock.
Annotate it with __guarded_by(&subsys->lock) so that the clang
context analyzer can validate accesses to this field.

Some accesses to nvmet_ns::file in the I/O hotpath do not require
holding nvmet_subsys::lock, as the namespace reference guarantees
that the namespace remains valid and the field is not modified while
I/O is in flight. However, the clang context analyzer cannot express
this access pattern directly.

Introduce nvmet_ns_file() as an inline helper annotated with
__context_unsafe() to allow these accesses without requiring the
subsystem lock. Convert the relevant accesses in the I/O hotpath and
request-processing paths to use this helper.

Additionally, annotate nvmet_file_ns_enable(), nvmet_file_ns_disable()
and nvmet_file_ns_revalidate() with __must_hold(&ns->subsys->lock) to
document and validate their locking requirements.

Since nvmet_file_flush() accesses the namespace file, change its
interface to accept a struct file pointer as an argument. This allows
callers to obtain the file pointer using the appropriate access
mechanism before invoking the helper:
- nvmet_file_flush_work() runs in the I/O hotpath after acquiring a
  namespace reference, so it uses nvmet_ns_file() to obtain the file
  pointer without holding the subsystem lock.
- nvmet_write_protect_flush_sync() already holds the subsystem lock,
  so it can directly access ns->file and pass the pointer to
  nvmet_file_flush().

Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
 drivers/nvme/target/admin-cmd.c   |  5 +++--
 drivers/nvme/target/core.c        |  2 +-
 drivers/nvme/target/io-cmd-file.c | 19 ++++++++++---------
 drivers/nvme/target/nvmet.h       | 15 ++++++++++-----
 4 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index 70b52a43156b..756bf627c16e 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -1236,9 +1236,10 @@ static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req)
 	__must_hold(&req->ns->subsys->lock)
 {
 	u16 status;
+	struct file *file = req->ns->file;
 
-	if (req->ns->file)
-		status = nvmet_file_flush(req);
+	if (file)
+		status = nvmet_file_flush(req, file);
 	else
 		status = nvmet_bdev_flush(req);
 
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index bc7f7eef2b1c..0cf0f311ed10 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -1163,7 +1163,7 @@ static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
 
 	switch (req->ns->csi) {
 	case NVME_CSI_NVM:
-		if (req->ns->file)
+		if (nvmet_ns_file(req->ns))
 			ret = nvmet_file_parse_io_cmd(req);
 		else
 			ret = nvmet_bdev_parse_io_cmd(req);
diff --git a/drivers/nvme/target/io-cmd-file.c b/drivers/nvme/target/io-cmd-file.c
index 3098b902f519..b7c5c7ecf470 100644
--- a/drivers/nvme/target/io-cmd-file.c
+++ b/drivers/nvme/target/io-cmd-file.c
@@ -84,17 +84,17 @@ static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos,
 	if (req->cmd->rw.opcode == nvme_cmd_write) {
 		if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
 			ki_flags |= IOCB_DSYNC;
-		call_iter = req->ns->file->f_op->write_iter;
+		call_iter = nvmet_ns_file(req->ns)->f_op->write_iter;
 		rw = ITER_SOURCE;
 	} else {
-		call_iter = req->ns->file->f_op->read_iter;
+		call_iter = nvmet_ns_file(req->ns)->f_op->read_iter;
 		rw = ITER_DEST;
 	}
 
 	iov_iter_bvec(&iter, rw, req->f.bvec, nr_segs, count);
 
 	iocb->ki_pos = pos;
-	iocb->ki_filp = req->ns->file;
+	iocb->ki_filp = nvmet_ns_file(req->ns);
 	iocb->ki_flags = ki_flags | iocb->ki_filp->f_iocb_flags;
 
 	return call_iter(iocb, &iter);
@@ -258,7 +258,7 @@ static void nvmet_file_execute_rw(struct nvmet_req *req)
 
 	if (req->ns->buffered_io) {
 		if (likely(!req->f.mpool_alloc) &&
-		    (req->ns->file->f_mode & FMODE_NOWAIT) &&
+		    (nvmet_ns_file(req->ns)->f_mode & FMODE_NOWAIT) &&
 		    nvmet_file_execute_io(req, IOCB_NOWAIT))
 			return;
 		nvmet_file_submit_buffered_io(req);
@@ -266,16 +266,17 @@ static void nvmet_file_execute_rw(struct nvmet_req *req)
 		nvmet_file_execute_io(req, 0);
 }
 
-u16 nvmet_file_flush(struct nvmet_req *req)
+u16 nvmet_file_flush(struct nvmet_req *req, struct file *file)
 {
-	return errno_to_nvme_status(req, vfs_fsync(req->ns->file, 1));
+	return errno_to_nvme_status(req, vfs_fsync(file, 1));
 }
 
 static void nvmet_file_flush_work(struct work_struct *w)
 {
 	struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
+	struct file *file = nvmet_ns_file(req->ns);
 
-	nvmet_req_complete(req, nvmet_file_flush(req));
+	nvmet_req_complete(req, nvmet_file_flush(req, file));
 }
 
 static void nvmet_file_execute_flush(struct nvmet_req *req)
@@ -313,7 +314,7 @@ static void nvmet_file_execute_discard(struct nvmet_req *req)
 		}
 		len = len64;
 
-		ret = vfs_fallocate(req->ns->file, mode, offset, len);
+		ret = vfs_fallocate(nvmet_ns_file(req->ns), mode, offset, len);
 		if (ret && ret != -EOPNOTSUPP) {
 			req->error_slba = le64_to_cpu(range.slba);
 			status = errno_to_nvme_status(req, ret);
@@ -368,7 +369,7 @@ static void nvmet_file_write_zeroes_work(struct work_struct *w)
 	}
 	len = len64;
 
-	ret = vfs_fallocate(req->ns->file, mode, offset, len);
+	ret = vfs_fallocate(nvmet_ns_file(req->ns), mode, offset, len);
 	nvmet_req_complete(req, ret < 0 ? errno_to_nvme_status(req, ret) : 0);
 }
 
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index fc2ccf992d30..20b87de18054 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -152,7 +152,8 @@ struct nvmet_ns {
 	struct file		*bdev_file;
 	struct block_device	*bdev
 		__guarded_by(&subsys->lock);
-	struct file		*file;
+	struct file		*file
+		__guarded_by(&subsys->lock);
 	bool			readonly;
 	u32			nsid;
 	u32			blksize_shift;
@@ -201,6 +202,7 @@ static inline _type nvmet_ns_##_name(struct nvmet_ns *ns)		    \
 }
 
 NVMET_NS_ACCESSOR(struct block_device *, bdev)
+NVMET_NS_ACCESSOR(struct file *, file)
 
 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
 {
@@ -759,17 +761,20 @@ bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
 
 int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 	__must_hold(&ns->subsys->lock);
-int nvmet_file_ns_enable(struct nvmet_ns *ns);
+int nvmet_file_ns_enable(struct nvmet_ns *ns)
+	__must_hold(&ns->subsys->lock);
 void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
 	__must_hold(&ns->subsys->lock);
-void nvmet_file_ns_disable(struct nvmet_ns *ns);
+void nvmet_file_ns_disable(struct nvmet_ns *ns)
+	__must_hold(&ns->subsys->lock);
 u16 nvmet_bdev_flush(struct nvmet_req *req)
 	__must_hold(&req->ns->subsys->lock);
-u16 nvmet_file_flush(struct nvmet_req *req);
+u16 nvmet_file_flush(struct nvmet_req *req, struct file *file);
 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
 void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
 	__must_hold(&ns->subsys->lock);
-void nvmet_file_ns_revalidate(struct nvmet_ns *ns);
+void nvmet_file_ns_revalidate(struct nvmet_ns *ns)
+	__must_hold(&ns->subsys->lock);
 bool nvmet_ns_revalidate(struct nvmet_ns *ns)
 	__must_hold(&ns->subsys->lock);
 u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts);
-- 
2.53.0



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

Thread overview: 25+ 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 ` Nilay Shroff [this message]
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:50   ` sashiko-bot
2026-11-18  7:34 ` [PATCH v2 08/22] nvmet: add context annotation for nvmet_ns::nguid 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 ` [PATCH v2 10/22] nvmet: fix nvmet_ns::uuid access using nvmet_subsys::lock Nilay Shroff
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:34 ` [PATCH v2 18/22] nvmet: fix PCI P2P parameters access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:55   ` sashiko-bot
2026-11-18  7:35 ` [PATCH v2 19/22] nvmet: add context annotation for integrity params in struct nvmet_ns 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-4-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox