nvme-multipath: serialize ana_log_buf lifecycle with ana_lock
PATCH v3 1/1 nvme-multipath: serialize ana_log_buf lifecycle with ana_lock
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.
---
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/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
drivers/nvme/host/multipath.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)
drivers/nvme/host/multipath.c +26 -5
@@ -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); }
Signature · 1 line
2.43.7
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] nvme-multipath: TOCTOU race on ana_log_buf in nvme_mpath_add_disk() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] nvme-multipath: TOCTOU race on ana_log_buf in nvme_mpath_add_disk()
--
commit f8acdff41f6798914e6883d82fb65ecaaa130b49
Author: Guixin Liu <[email protected]>
nvme-multipath: serialize ana_log_buf lifecycle with ana_lock
This commit serializes ctrl->ana_log_buf freeing and reallocation with ctrl->ana_lock and makes nvme_parse_ana_log() and nvme_read_ana_log() bail out when ctrl->ana_log_buf is NULL.
drivers/nvme/host/multipath.c static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data, · line 871
Quoted 10 lines
drivers/nvme/host/multipath.c +2
-0@@ -867,6 +867,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 ;
[Severity: Medium]
Can returning 0 here cause nvme_mpath_add_disk() to mistake a freed ana_log_buf for a missing ANA group descriptor when racing with nvme_mpath_uninit()?
drivers/nvme/host/multipath.c:nvme_mpath_add_disk() {
if (nvme_ctrl_use_ana(ns->ctrl)) {
struct nvme_ana_group_desc desc = {
.grpid = anagrpid,
.state = 0,
};
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);
mutex_unlock(&ns->ctrl->ana_lock);
if (desc.state) {
/* found the group desc: update */
nvme_update_ns_ana_state(&desc, ns);
} else {
/* group desc not found: trigger a re-read */
set_bit(NVME_NS_ANA_PENDING, &ns->flags);
queue_work(nvme_wq, &ns->ctrl->ana_work);
}
} else {
ns->ana_state = NVME_ANA_OPTIMIZED;
nvme_mpath_set_live(ns);
}
}Because nvme_mpath_add_disk() checks nvme_ctrl_use_ana() outside ana_lock, a concurrent nvme_mpath_uninit() can clear ana_log_buf before ana_lock is acquired:
CPU0 CPU1nvme_mpath_add_disk() nvme_mpath_init_identify()
nvme_ctrl_use_ana() -> true /* ana_log_size > max_transfer_size */
nvme_mpath_uninit()
cancel_work_sync(&ctrl->ana_work)
mutex_lock(&ctrl->ana_lock)
ctrl->ana_log_buf = NULL
mutex_unlock(&ctrl->ana_lock) mutex_lock(&ctrl->ana_lock)
nvme_parse_ana_log() -> 0 mutex_unlock(&ctrl->ana_lock)
/* desc.state == 0 */
set_bit(NVME_NS_ANA_PENDING, ...)
queue_work(..., &ctrl->ana_work) <-- queued after ANA teardown
When nvme_mpath_init_identify() disables ANA because ana_log_size exceeds max_transfer_size, it returns 0 and the controller proceeds without ANA. CPU0 sets NVME_NS_ANA_PENDING instead of taking the non-ANA branch to mark the namespace live, and nvme_read_ana_log() now bails out on the NULL ana_log_buf, leaving the path stuck in NVME_PATH_DISABLED_ANA_PENDING. Also, once ana_log_buf is NULL, nvme_mpath_stop() returns early because nvme_ctrl_use_ana() is false, so it will not cancel the newly queued ana_work.
Would rechecking nvme_ctrl_use_ana() under ana_lock in nvme_mpath_add_disk() (or having nvme_parse_ana_log() return an error such as -ENOENT when ana_log_buf is NULL) allow nvme_mpath_add_disk() to fall back to the non-ANA path?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 21 lines · Guixin Liu
nvme_parse_ana_log()readsctrl->ana_log_bufandctrl->ana_log_sizeunderctrl->ana_lock, but the buffer lifecycle is not fully serialized against it:-
nvme_mpath_uninit()frees the buffer withoutana_lock, so a concurrent parser innvme_mpath_add_disk()can use freed memory.
-nvme_mpath_init_identify()reallocates the buffer and updatesana_log_sizeoutsideana_lock.nvme_mpath_add_disk()testsnvme_ctrl_use_ana()before taking the lock, so it can pass the check and then dereference a NULL buffer afternvme_mpath_uninit()cleared it; and the freshly allocated buffer can be parsed beforenvme_read_ana_log()populates it, reading uninitialized memory.
- The shrink path leaves the old log in place while shrinkingana_log_size, and anagrpmax is updated outsideana_lock, so a concurrent parser validates stale descriptors against the new limits and tripsWARN_ON_ONCE().
- Afternvme_mpath_uninit()clears the buffer,nvme_mpath_add_disk()can still queueana_work;nvme_read_ana_log()then callsnvme_get_log()with a NULL buffer and size 0, andnvme_bytes_to_numd(0)underflows to0xffffffff.
Quoted 11 lines
Take
ana_lockaround the buffer release innvme_mpath_uninit()and around the reallocation innvme_mpath_init_identify(), updatingana_log_buf,ana_log_sizeand 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 beforenvme_read_ana_log()populates the log sees ngrps == 0.nvme_parse_ana_log()andnvme_read_ana_log()bail out when the buffer is NULL. Keep theana_workdrain outside the lock:ana_workitself takesana_lock, so cancelling it under the lock would deadlock.
Along with this change, can we also annotate relevant ANA fields in struct nvme_ctrl using ctrl->ana_lock? That would help document all fields protected with ana_lock as well as all references to those fields would be also analyzed by thread-safety context analyzer when compiled with clang.
Thanks,
--Nilay