[PATCH 11/23] nvmet: add context annotation for nvmet_ns::uuid
Nilay Shroff
nilay at linux.ibm.com
Sun Nov 8 19:28:16 PST 2026
The nvmet_ns::uuid 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::uuid 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_uuid() 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, acquire ns->subsys->lock in nvmet_ns_device_uuid_show()
before accessing ns->uuid, since this configfs control path does not
hold the lock.
Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 6 ++++--
drivers/nvme/target/configfs.c | 9 ++++++++-
drivers/nvme/target/nvmet.h | 4 +++-
3 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index d784498ca720..b8015054de46 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -1082,15 +1082,17 @@ static void nvmet_execute_identify_desclist(struct nvmet_req *req)
{
off_t off = 0;
u16 status;
+ uuid_t uuid;
status = nvmet_req_find_ns(req);
if (status)
goto out;
- if (memchr_inv(&req->ns->uuid, 0, sizeof(req->ns->uuid))) {
+ uuid = nvmet_ns_uuid(req->ns);
+ if (memchr_inv(&uuid, 0, sizeof(uuid))) {
status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
NVME_NIDT_UUID_LEN,
- &req->ns->uuid, &off);
+ &uuid, &off);
if (status)
goto out;
}
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index cd2c301bc69a..f8a4d0d88946 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -615,7 +615,14 @@ CONFIGFS_ATTR(nvmet_ns_, p2pmem);
static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
{
- return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
+ struct nvmet_ns *ns = to_nvmet_ns(item);
+ ssize_t size;
+
+ mutex_lock(&ns->subsys->lock);
+ size = sprintf(page, "%pUb\n", &ns->uuid);
+ mutex_unlock(&ns->subsys->lock);
+
+ return size;
}
static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 21ed96b39a31..3e18c36e83c8 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -165,7 +165,8 @@ struct nvmet_ns {
__guarded_by(&subsys->lock);
u8 nguid[16]
__guarded_by(&subsys->lock);
- uuid_t uuid;
+ uuid_t uuid
+ __guarded_by(&subsys->lock);
u32 anagrpid;
bool buffered_io;
@@ -212,6 +213,7 @@ NVMET_NS_ACCESSOR(struct file *, file)
NVMET_NS_ACCESSOR(u32, nsid)
NVMET_NS_ACCESSOR(u32, blksize_shift)
NVMET_NS_ACCESSOR(u8 *, nguid)
+NVMET_NS_ACCESSOR(uuid_t, uuid)
/*
* These accessors use data_race() and READ_ONCE() to allow lockless access to
--
2.53.0
More information about the Linux-nvme
mailing list