Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Nilay Shroff <nilay@linux.ibm.com>
To: kbusch@kernel.org, hch@lst.de, bvanassche@acm.org,
	sagi@grimberg.me, axboe@kernel.dk, chaitanyak@nvidia.com
Cc: gjoyce@linux.ibm.com, elver@google.com,
	linux-nvme@lists.infradead.org,
	Nilay Shroff <nilay@linux.ibm.com>
Subject: [PATCH v2 02/22] nvmet: fix nvmet_ns::bdev access using nvmet_subsys::lock
Date: Wed, 18 Nov 2026 13:04:43 +0530	[thread overview]
Message-ID: <20261118073505.288910-3-nilay@linux.ibm.com> (raw)
In-Reply-To: <20261118073505.288910-1-nilay@linux.ibm.com>

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@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



  parent reply	other threads:[~2026-11-18  7:36 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  7:34 [PATCH v2 00/22] nvmet: add context annotation for params in struct nvmet_ns Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 01/22] nvmet: add context annotation for nvmet_ns::bdev Nilay Shroff
2026-11-18  7:34 ` Nilay Shroff [this message]
2026-11-18  7:34 ` [PATCH v2 03/22] nvmet: add context annotation for nvmet_ns::file Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 04/22] nvmet: add context annotation for nvmet_ns::bdev_file Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 05/22] nvmet: add context annotation for nvmet_ns::readonly Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 06/22] nvmet: add context annotation for nvmet_ns::blksize_shift Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 07/22] nvmet: add context annotation for nvmet_ns::size Nilay Shroff
2026-11-18  7:50   ` sashiko-bot
2026-11-18  7:34 ` [PATCH v2 08/22] nvmet: add context annotation for nvmet_ns::nguid Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 09/22] nvmet: add context annotation for nvmet_ns::uuid Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 10/22] nvmet: fix nvmet_ns::uuid access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 11/22] nvmet: add context annotation for nvmet_ns::buffered_io Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 12/22] nvmet: fix nvmet_ns::buffered_io access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 13/22] nvmet: add context annotation for nvmet_ns::enabled Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 14/22] nvmet: fix nvmet_ns::enable access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 15/22] nvmet: add context annotation for nvmet_ns::device_path Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 16/22] nvmet: fix nvmet_ns::device_path access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 17/22] nvmet: add context annotation for PCI P2P parameters in struct nvmet_ns Nilay Shroff
2026-11-18  7:34 ` [PATCH v2 18/22] nvmet: fix PCI P2P parameters access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:55   ` sashiko-bot
2026-11-18  7:35 ` [PATCH v2 19/22] nvmet: add context annotation for integrity params in struct nvmet_ns Nilay Shroff
2026-11-18  7:35 ` [PATCH v2 20/22] nvmet: add context annotation for nvmet_ns::csi Nilay Shroff
2026-11-18  7:35 ` [PATCH v2 21/22] nvmet: fix nvmet_ns::csi access using nvmet_subsys::lock Nilay Shroff
2026-11-18  7:35 ` [PATCH v2 22/22] nvmet: add context annotation for nvmet_ns::debugfs_dir Nilay Shroff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118073505.288910-3-nilay@linux.ibm.com \
    --to=nilay@linux.ibm.com \
    --cc=axboe@kernel.dk \
    --cc=bvanassche@acm.org \
    --cc=chaitanyak@nvidia.com \
    --cc=elver@google.com \
    --cc=gjoyce@linux.ibm.com \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox