[PATCH 14/23] nvmet: fix nvmet_ns::buffered_io access using nvmet_subsys::lock
Nilay Shroff
nilay at linux.ibm.com
Sun Nov 8 19:28:19 PST 2026
The nvmet_ns::buffered_io field is protected by nvmet_subsys::lock.
However, nvmet_ns_alloc() initializes this field to false without
holding the corresponding lock, triggering a Clang context analyzer
warning:
drivers/nvme/target/core.c:782:6: error: writing variable 'buffered_io' requires holding mutex '_res->subsys->lock' exclusively
[-Werror,-Wthread-safety-analysis]
782 | ns->buffered_io = false;
| ^
Fix this by removing the initialization of ns->buffered_io as we zero
alloc namespace struct and so ideally we do not need to initialize
ns->buffered_io to false in nvmet_ns_alloc().
Additionally, nvmet_ns_buffered_io_show() accesses ns->buffered_io
without holding subsys->lock, triggering another context analyzer
warning:
drivers/nvme/target/configfs.c:774:50: error: reading variable 'buffered_io' requires holding mutex 'to_nvmet_ns(item).subsys->lock'
[-Werror,-Wthread-safety-analysis]
774 | return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
Fix this by acquiring ns->subsys->lock before reading the field.
Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
---
drivers/nvme/target/configfs.c | 9 ++++++++-
drivers/nvme/target/core.c | 1 -
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index f8a4d0d88946..34d79fadbf15 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -770,7 +770,14 @@ CONFIGFS_ATTR(nvmet_ns_, enable);
static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
{
- return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
+ struct nvmet_ns *ns = to_nvmet_ns(item);
+ ssize_t ret;
+
+ mutex_lock(&ns->subsys->lock);
+ ret = sprintf(page, "%d\n", ns->buffered_io);
+ mutex_unlock(&ns->subsys->lock);
+
+ return ret;
}
static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
index 08a70439fd55..805b5f70eb06 100644
--- a/drivers/nvme/target/core.c
+++ b/drivers/nvme/target/core.c
@@ -781,7 +781,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->buffered_io = false;
ns->csi = NVME_CSI_NVM;
return ns;
--
2.53.0
More information about the Linux-nvme
mailing list