[PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock
Guixin Liu
kanie at linux.alibaba.com
Tue Nov 24 22:01:53 PST 2026
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 at vger.kernel.org
Signed-off-by: Guixin Liu <kanie at 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
More information about the Linux-nvme
mailing list