[PATCH 03/23] nvmet: add context annotation for nvmet_ns::file
Nilay Shroff
nilay at linux.ibm.com
Sun Nov 8 19:28:08 PST 2026
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()
nvmet_file_flush() and nvmet_file_ns_revalidate() with
__must_hold(&ns->subsys->lock) to document and validate their locking
requirements.
Finally, nvmet_file_flush_work() invokes nvmet_file_flush() from the
I/O hotpath after acquiring a namespace reference. Explicitly assert
the ns->subsys->lock capability using __assume_ctx_lock() to satisfy
the context analyzer, since nvmet_file_flush() requires callers to
hold this lock.
Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
---
drivers/nvme/target/core.c | 2 +-
drivers/nvme/target/io-cmd-file.c | 25 +++++++++++++++++++------
drivers/nvme/target/nvmet.h | 16 +++++++++++-----
3 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index c9837263898a..ef99c45f0f37 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -1171,7 +1171,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 695963dd932e..4307232c9160 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);
@@ -241,7 +241,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);
@@ -258,6 +258,19 @@ static void nvmet_file_flush_work(struct work_struct *w)
{
struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
+ /*
+ * The flush work is scheduled from the I/O hotpath after acquiring
+ * a reference to the namespace. This ensures that the namespace
+ * cannot be modified while the I/O is in flight.
+ *
+ * nvmet_file_flush() requires callers to hold ns->subsys->lock.
+ * Since we already hold a namespace reference, explicitly assert
+ * the lock capability here to satisfy the context analyzer.
+ *
+ * Other callers of nvmet_file_flush() that do not hold a namespace
+ * reference must acquire ns->subsys->lock before invoking it.
+ */
+ __assume_ctx_lock(&req->ns->subsys->lock);
nvmet_req_complete(req, nvmet_file_flush(req));
}
@@ -293,7 +306,7 @@ static void nvmet_file_execute_discard(struct nvmet_req *req)
break;
}
- 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);
@@ -347,7 +360,7 @@ static void nvmet_file_write_zeroes_work(struct work_struct *w)
return;
}
- 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 41912877ade3..74b066ce9ab0 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)
{
@@ -757,17 +759,21 @@ 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)
+ __must_hold(&req->ns->subsys->lock);
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
More information about the Linux-nvme
mailing list