[PATCH v2 02/22] nvmet: fix nvmet_ns::bdev access using nvmet_subsys::lock
Nilay Shroff
nilay at linux.ibm.com
Tue Nov 17 23:34:43 PST 2026
nvmet_get_smart_log_all() iterates over enabled namespaces and accesses
ns->bdev to collect block device statistics without holding
nvmet_subsys::lock.
Since nvmet_ns::bdev is annotated with __guarded_by(&subsys->lock), the
clang context analyser reports this access as a locking violation:
drivers/nvme/target/admin-cmd.c:274:12: error: reading variable 'bdev' requires holding mutex 'ns->subsys->lock' [-Werror,-Wthread-safety-precise]
274 | if (!ns->bdev)
| ^
Fix this by acquiring ctrl->subsys->lock while iterating over the
enabled namespaces and accessing their block device statistics.
Although ctrl->subsys->lock and ns->subsys->lock refer to the same
mutex, the Clang context analyzer cannot infer this relationship
because the lock is accessed through different expressions. Explicitly
assert the ns->subsys->lock capability using __assume_ctx_lock()
before accessing ns->bdev, and release the assumed capability using
__release_ctx_lock() afterward.
Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
---
drivers/nvme/target/admin-cmd.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index d45c05f5d746..70b52a43156b 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -289,17 +289,31 @@ static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
unsigned long idx;
ctrl = req->sq->ctrl;
+ mutex_lock(&ctrl->subsys->lock);
nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
+ /*
+ * Both ctrl->subsys->lock and ns->subsys->lock refer to the
+ * same lock. However there's no way for us to annotate such
+ * locking semantics using clang context annotation. So
+ * explicitly assert the capability.
+ */
+ __assume_ctx_lock(&ns->subsys->lock);
+
/* we don't have the right data for file backed ns */
- if (!ns->bdev)
+ if (!ns->bdev) {
+ __release_ctx_lock(&ns->subsys->lock);
continue;
+ }
host_reads += part_stat_read(ns->bdev, ios[READ]);
data_units_read += DIV_ROUND_UP(
part_stat_read(ns->bdev, sectors[READ]), 1000);
host_writes += part_stat_read(ns->bdev, ios[WRITE]);
data_units_written += DIV_ROUND_UP(
part_stat_read(ns->bdev, sectors[WRITE]), 1000);
+
+ __release_ctx_lock(&ns->subsys->lock);
}
+ mutex_unlock(&ctrl->subsys->lock);
put_unaligned_le64(host_reads, &slog->host_reads[0]);
put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
--
2.53.0
More information about the Linux-nvme
mailing list