All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] nvme-multipath: serialize ana_log_buf lifecycle with ana_lock
@ 2026-12-01  7:49 Guixin Liu
  2026-12-01  8:08 ` sashiko-bot
  2026-12-01 12:54 ` Nilay Shroff
  0 siblings, 2 replies; 3+ messages in thread
From: Guixin Liu @ 2026-12-01  7:49 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Nilay Shroff, Daniel Wagner, Hannes Reinecke
  Cc: linux-nvme

nvme_parse_ana_log() reads ctrl->ana_log_buf and ctrl->ana_log_size
under ctrl->ana_lock, but the buffer lifecycle is not fully serialized
against it:

 - nvme_mpath_uninit() frees the buffer without ana_lock, so a
   concurrent parser in nvme_mpath_add_disk() can use freed memory.
 - nvme_mpath_init_identify() reallocates the buffer and updates
   ana_log_size outside ana_lock. nvme_mpath_add_disk() tests
   nvme_ctrl_use_ana() before taking the lock, so it can pass the check
   and then dereference a NULL buffer after nvme_mpath_uninit() cleared
   it; and the freshly allocated buffer can be parsed before
   nvme_read_ana_log() populates it, reading uninitialized memory.
 - The shrink path leaves the old log in place while shrinking
   ana_log_size, and anagrpmax is updated outside ana_lock, so a
   concurrent parser validates stale descriptors against the new
   limits and trips WARN_ON_ONCE().
 - After nvme_mpath_uninit() clears the buffer, nvme_mpath_add_disk()
   can still queue ana_work; nvme_read_ana_log() then calls
   nvme_get_log() with a NULL buffer and size 0, and
   nvme_bytes_to_numd(0) underflows to 0xffffffff.

Take ana_lock around the buffer release in nvme_mpath_uninit() and
around the reallocation in nvme_mpath_init_identify(), updating
ana_log_buf, ana_log_size and anagrpmax together so a parser never
observes a freed or NULL buffer or stale limits. Allocate the new
buffer outside the lock and zero it, and clear ngrps on the shrink
path, so a parser running before nvme_read_ana_log() populates the log
sees ngrps == 0. nvme_parse_ana_log() and nvme_read_ana_log() bail out
when the buffer is NULL. Keep the ana_work drain outside the lock:
ana_work itself takes ana_lock, so cancelling it under the lock would
deadlock.

Fixes: 0d0b660f214d ("nvme: add ANA support")
Cc: stable@vger.kernel.org
Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
---
Reported by Sashiko AI review as a pre-existing issue. Confirmed by
source analysis and reproduced in a KASAN guest with nvmet-tcp loopback
and ANA enabled (test-only delays + forced ANA-log size changes).

The buffer lifecycle is not serialized against nvme_parse_ana_log(),
which reads ana_log_buf/ana_log_size under ana_lock: the free in
nvme_mpath_uninit() and the reallocation in nvme_mpath_init_identify()
run outside the lock, so a concurrent nvme_mpath_add_disk() parser can
use freed memory, dereference a NULL buffer, or read uninitialized or
stale descriptors. The FROZEN guard does not close the window: a tcp
reset runs nvme_init_identify() before nvme_start_freeze() and does not
flush scan_work.

With the fix the buffer pointer, ana_log_size and anagrpmax are updated
atomically under ana_lock, the new buffer is zeroed (and ngrps cleared
on the shrink path) so a parser before nvme_read_ana_log() populates the
log sees ngrps == 0, and both nvme_parse_ana_log() and
nvme_read_ana_log() bail out on a NULL buffer. ANA change/optimized
transitions and multipath I/O remain unaffected.

Changes since v2:
 - bail out of nvme_read_ana_log() when ana_log_buf is NULL: after
   nvme_mpath_uninit() clears the buffer, nvme_mpath_add_disk() can
   still queue ana_work, and nvme_read_ana_log() otherwise calls
   nvme_get_log() with a NULL buffer and size 0, where
   nvme_bytes_to_numd(0) underflows to 0xffffffff (Sashiko AI review)
 - update anagrpmax under ana_lock and clear ngrps on the shrink path,
   so a concurrent parser does not validate stale pre-reset descriptors
   against the shrunk ana_log_size/anagrpmax and trip WARN_ON_ONCE()
   (Sashiko AI review)

Changes since v1:
 - serialize the reallocation swap (buffer pointer and ana_log_size)
   under ana_lock, allocating the new buffer outside the lock; bail out
   of nvme_parse_ana_log() when ana_log_buf is NULL and zero the
   reallocated buffer. v1 only serialized the free, leaving a NULL
   dereference window and an uninitialized-memory read (Sashiko AI
   review)

v2: https://lore.kernel.org/all/20261125122446.1122253-1-kanie@linux.alibaba.com/
v1: https://lore.kernel.org/all/20261125060153.206699-1-kanie@linux.alibaba.com/

 drivers/nvme/host/multipath.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index 3685c792181b..899be8305792 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -889,6 +889,9 @@ static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
 
 	lockdep_assert_held(&ctrl->ana_lock);
 
+	if (!ctrl->ana_log_buf)
+		return 0;
+
 	for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
 		struct nvme_ana_group_desc *desc = base + offset;
 		u32 nr_nsids;
@@ -1008,6 +1011,10 @@ static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
 	int error;
 
 	mutex_lock(&ctrl->ana_lock);
+	if (!ctrl->ana_log_buf) {
+		error = 0;
+		goto out_unlock;
+	}
 	error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
 			ctrl->ana_log_buf, ctrl->ana_log_size, 0);
 	if (error) {
@@ -1609,7 +1616,6 @@ int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 	ctrl->anacap = id->anacap;
 	ctrl->anatt = id->anatt;
 	ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
-	ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
 
 	ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
 		ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
@@ -1622,13 +1628,26 @@ int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 		goto out_uninit;
 	}
 	if (ana_log_size > ctrl->ana_log_size) {
+		void *buf;
+
 		nvme_mpath_stop(ctrl);
-		nvme_mpath_uninit(ctrl);
-		ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL);
-		if (!ctrl->ana_log_buf)
+		buf = kvzalloc(ana_log_size, GFP_KERNEL);
+		if (!buf)
 			return -ENOMEM;
+		mutex_lock(&ctrl->ana_lock);
+		kvfree(ctrl->ana_log_buf);
+		ctrl->ana_log_buf = buf;
+		ctrl->ana_log_size = ana_log_size;
+		ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
+		mutex_unlock(&ctrl->ana_lock);
+	} else {
+		mutex_lock(&ctrl->ana_lock);
+		ctrl->ana_log_size = ana_log_size;
+		ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
+		if (ctrl->ana_log_buf)
+			ctrl->ana_log_buf->ngrps = 0;
+		mutex_unlock(&ctrl->ana_lock);
 	}
-	ctrl->ana_log_size = ana_log_size;
 	error = nvme_read_ana_log(ctrl);
 	if (error)
 		goto out_uninit;
@@ -1648,7 +1667,9 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
 	 */
 	cancel_work_sync(&ctrl->ana_work);
 
+	mutex_lock(&ctrl->ana_lock);
 	kvfree(ctrl->ana_log_buf);
 	ctrl->ana_log_buf = NULL;
 	ctrl->ana_log_size = 0;
+	mutex_unlock(&ctrl->ana_lock);
 }
-- 
2.43.7



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-01 12:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  7:49 [PATCH v3] nvme-multipath: serialize ana_log_buf lifecycle with ana_lock Guixin Liu
2026-12-01  8:08 ` sashiko-bot
2026-12-01 12:54 ` Nilay Shroff

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.