Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock
@ 2026-11-25  6:01 Guixin Liu
  2026-11-25  6:10 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Guixin Liu @ 2026-11-25  6:01 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 under ctrl->ana_lock, but
nvme_mpath_uninit() frees the buffer without holding the lock. When
nvme_mpath_init_identify() reallocates a larger ANA log it frees the
old buffer through nvme_mpath_uninit(), while a concurrent namespace
scan in nvme_mpath_add_disk() may still be parsing that buffer under
ana_lock, leading to a use-after-free.

Take ana_lock around the buffer release in nvme_mpath_uninit() so it
serializes with concurrent parsers. 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 (not introduced by
the ANATT timer fix). Confirmed by source analysis and reproduced in a
KASAN guest with nvmet-tcp loopback and ANA enabled.

The race is in the reallocation path only: nvme_mpath_init_identify()
frees the old buffer through nvme_mpath_uninit() before taking ana_lock
in nvme_read_ana_log(). The out_uninit path (failed ANA log read) takes
ana_lock first and is already serialized against parsers. The FROZEN
guard does not close the window: a tcp reset runs nvme_init_identify()
(in configure_admin_queue) before nvme_start_freeze() (in
configure_io_queues), and reset does not flush scan_work.

Reproduction: with a test-only delay in the add_disk parse path and a
forced ANA-log growth, buffer address tracking shows nvme_mpath_uninit()
freeing the old ana_log_buf while nvme_parse_ana_log() still
dereferences it; the reallocated buffer lands at a different address,
so the parser reads freed memory. With this fix the free waits for
ana_lock and happens only after the parser completes (free timestamp
moves after the parse dereference), and the use-after-free disappears.
ANA change/optimized transitions and multipath I/O remain unaffected.

 drivers/nvme/host/multipath.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index 3685c792181b..28ed3a677c10 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -1648,7 +1648,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] 2+ messages in thread

end of thread, other threads:[~2026-11-25  6:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  6:01 [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock Guixin Liu
2026-11-25  6:10 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox