[PATCH v2 03/22] nvmet: add context annotation for nvmet_ns::file
Nilay Shroff
nilay at linux.ibm.com
Tue Nov 17 23:34:44 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()
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 at 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
More information about the Linux-nvme
mailing list