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 v5] nvme-multipath: serialize ana_log_buf lifecycle with ana_lock
Date: Fri, 4 Dec 2026 15:00:41 +0800 [thread overview]
Message-ID: <20261204070041.3602839-1-kanie@linux.alibaba.com> (raw)
nvme_parse_ana_log() reads ana_log_buf and ana_log_size under ana_lock,
but the buffer is freed in nvme_mpath_uninit() and reallocated in
nvme_mpath_init_identify() outside the lock. A concurrent parser in
nvme_mpath_add_disk() can then use freed memory, dereference a NULL
buffer, read a freshly allocated buffer before nvme_read_ana_log()
populates it, or validate stale descriptors against shrunk limits
(WARN_ON_ONCE). Once the buffer is NULL, nvme_read_ana_log() calls
nvme_get_log() with size 0, where nvme_bytes_to_numd(0) underflows to
0xffffffff, and the NULL-buffer bail in nvme_parse_ana_log() leaves
desc.state == 0 so the namespace is stuck NVME_NS_ANA_PENDING and never
goes live.
Take ana_lock around the buffer release and reallocation, updating
ana_log_buf, ana_log_size and anagrpmax together. Allocate the new
buffer outside the lock and zero it, and clear ngrps on the shrink path,
so an early parser sees ngrps == 0. nvme_read_ana_log() bails on a NULL
buffer and nvme_parse_ana_log() returns -ENOENT in that case.
nvme_mpath_add_disk() keeps the lockless nvme_ctrl_use_ana() feature test
and, on -ENOENT, clears any pending bit and falls back to the non-ANA
path so the namespace goes live even if ANA was disabled while it
parsed. The ana_work drain stays outside the lock since ana_work itself
takes ana_lock.
Fixes: 0d0b660f214d ("nvme: add ANA support")
Cc: stable@vger.kernel.org
Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
---
Originally reported by Sashiko AI review as pre-existing issues,
confirmed by source analysis and reproduced in a KASAN guest.
Tested on 7.4.0-rc1 (KASAN, nvmet-tcp loopback, ANA enabled). A test-only
parameter clears ana_log_buf before nvme_mpath_add_disk() takes ana_lock:
without the fix the namespace stays NVME_NS_ANA_PENDING and /dev/nvme0n2
never appears; with the fix it goes optimized and live and 4K reads pass.
Normal ANA path, multipath fio (err=0) and controller reset are clean.
The ns->ana_grpid/ana_state publication stays outside ana_lock on
purpose: nvme_mpath_set_live() runs synchronous report-zones I/O for
zoned namespaces, and holding ana_lock across it deadlocks scan_work
against reset_work (reset takes ana_lock in nvme_mpath_init_identify()
without flushing scan_work). The stale-snapshot race this leaves is
pre-existing and recoverable via failover. An already-pending namespace
stranded when ANA is disabled (nvme_mpath_update() returns early on a
NULL buffer) is a separate pre-existing gap left for a follow-up.
Changes since v4:
- revert the under-lock nvme_ctrl_use_ana() test, keep it lockless;
nvme_parse_ana_log() returns -ENOENT for a NULL buffer and add_disk
falls back to the non-ANA path (clearing pending) on that error
(Nilay Shroff)
Changes since v3:
- move the nvme_ctrl_use_ana() test under ana_lock so add_disk takes
the non-ANA path if ANA was disabled while it waited (Sashiko)
Changes since v2:
- bail out of nvme_read_ana_log() on a NULL buffer (numd underflow);
update anagrpmax under ana_lock and clear ngrps on shrink (Sashiko)
Changes since v1:
- serialize the realloc swap under ana_lock, allocate/zero outside the
lock, bail out of nvme_parse_ana_log() on a NULL buffer (Sashiko)
v4: https://lore.kernel.org/all/20261202085124.1252825-1-kanie@linux.alibaba.com/
v3: https://lore.kernel.org/all/20261201074919.1051448-1-kanie@linux.alibaba.com/
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 | 43 ++++++++++++++++++++++++++++-------
1 file changed, 35 insertions(+), 8 deletions(-)
diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index bd27a5355bab..e2ded689323e 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 -ENOENT;
+
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) {
@@ -1510,12 +1517,19 @@ void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid)
.grpid = anagrpid,
.state = 0,
};
+ int ret;
mutex_lock(&ns->ctrl->ana_lock);
ns->ana_grpid = le32_to_cpu(anagrpid);
- nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
+ ret = nvme_parse_ana_log(ns->ctrl, &desc,
+ nvme_lookup_ana_group_desc);
mutex_unlock(&ns->ctrl->ana_lock);
- if (desc.state) {
+ if (ret == -ENOENT) {
+ /* ANA log buffer gone: fall back to non-ANA */
+ clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
+ ns->ana_state = NVME_ANA_OPTIMIZED;
+ nvme_mpath_set_live(ns);
+ } else if (desc.state) {
/* found the group desc: update */
nvme_update_ns_ana_state(&desc, ns);
} else {
@@ -1527,7 +1541,6 @@ void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid)
ns->ana_state = NVME_ANA_OPTIMIZED;
nvme_mpath_set_live(ns);
}
-
}
void nvme_mpath_remove_disk(struct nvme_ns_head *head)
@@ -1609,7 +1622,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 +1634,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;
@@ -1649,7 +1674,9 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
cancel_work_sync(&ctrl->ana_work);
timer_delete_sync(&ctrl->anatt_timer);
+ 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-12-04 7:01 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 7:00 Guixin Liu [this message]
2026-12-04 8:23 ` [PATCH v5] nvme-multipath: serialize ana_log_buf lifecycle with ana_lock Christoph Hellwig
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=20261204070041.3602839-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