[PATCH 22/23] nvmet: add context annotation for nvmet_ns::csi
Nilay Shroff
nilay at linux.ibm.com
Sun Nov 8 19:28:27 PST 2026
The nvmet_ns::csi 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::csi 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_csi() 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, nvmet_ns_alloc() initializes ns->csi to NVME_CSI_NVM
outside the subsystem lock, triggering a context analyzer error.
Although this initialization could be omitted because both the nvmet_ns
is zero-allocated and NVME_CSI_NVM is also defined as zero, explicitly
initializing the ns->csi to NVME_CSI_NVM makes the intended default
value much clear. So move the ns->csi initialization into the existing
subsys->lock critical section instead.
Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 4 +++-
drivers/nvme/target/core.c | 5 ++---
drivers/nvme/target/nvmet.h | 4 +++-
3 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index e40fc361f5b6..74e76e12cf26 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -1083,6 +1083,7 @@ static void nvmet_execute_identify_desclist(struct nvmet_req *req)
off_t off = 0;
u16 status;
uuid_t uuid;
+ u8 csi;
status = nvmet_req_find_ns(req);
if (status)
@@ -1105,9 +1106,10 @@ static void nvmet_execute_identify_desclist(struct nvmet_req *req)
goto out;
}
+ csi = nvmet_ns_csi(req->ns);
status = nvmet_copy_ns_identifier(req, NVME_NIDT_CSI,
NVME_NIDT_CSI_LEN,
- &req->ns->csi, &off);
+ &csi, &off);
if (status)
goto out;
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index 6b11beb2e2b2..1e55ca344c96 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -772,6 +772,7 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
subsys->nr_namespaces++;
uuid_gen(&ns->uuid);
+ ns->csi = NVME_CSI_NVM;
__release_ctx_lock(&ns->subsys->lock);
mutex_unlock(&subsys->lock);
@@ -781,8 +782,6 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
nvmet_ana_group_enabled[ns->anagrpid]++;
up_write(&nvmet_ana_sem);
- ns->csi = NVME_CSI_NVM;
-
return ns;
out_exit:
subsys->max_nsid = nvmet_max_nsid(subsys);
@@ -1184,7 +1183,7 @@ static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
return ret;
}
- switch (req->ns->csi) {
+ switch (nvmet_ns_csi(req->ns)) {
case NVME_CSI_NVM:
if (nvmet_ns_file(req->ns))
ret = nvmet_file_parse_io_cmd(req);
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 082d848ea2b0..77d518739f91 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -193,7 +193,8 @@ struct nvmet_ns {
__guarded_by(&subsys->lock);
int metadata_size
__guarded_by(&subsys->lock);
- u8 csi;
+ u8 csi
+ __guarded_by(&subsys->lock);
struct nvmet_pr pr;
struct xarray pr_per_ctrl_refs;
#ifdef CONFIG_NVME_TARGET_DEBUGFS
@@ -225,6 +226,7 @@ NVMET_NS_ACCESSOR(bool, buffered_io)
NVMET_NS_ACCESSOR(const char *, device_path)
NVMET_NS_ACCESSOR(int, pi_type)
NVMET_NS_ACCESSOR(int, metadata_size)
+NVMET_NS_ACCESSOR(u8, csi)
/*
* These accessors use data_race() and READ_ONCE() to allow lockless access to
--
2.53.0
More information about the Linux-nvme
mailing list