From: Guixin Liu <kanie@linux.alibaba.com>
To: Keith Busch <kbusch@kernel.org>, Jens Axboe <axboe@kernel.dk>,
Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
Nilay Shroff <nilay@linux.ibm.com>,
Daniel Wagner <dwagner@suse.de>, Hannes Reinecke <hare@suse.de>
Cc: linux-nvme@lists.infradead.org
Subject: [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock
Date: Wed, 25 Nov 2026 14:01:53 +0800 [thread overview]
Message-ID: <20261125060153.206699-1-kanie@linux.alibaba.com> (raw)
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
next reply other threads:[~2026-11-25 6:02 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 6:01 Guixin Liu [this message]
2026-11-25 6:10 ` [PATCH] nvme-multipath: protect ana_log_buf teardown with ana_lock sashiko-bot
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=20261125060153.206699-1-kanie@linux.alibaba.com \
--to=kanie@linux.alibaba.com \
--cc=axboe@kernel.dk \
--cc=dwagner@suse.de \
--cc=hare@suse.de \
--cc=hch@lst.de \
--cc=kbusch@kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=nilay@linux.ibm.com \
--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