nvme-multipath: serialize ana_log_buf lifecycle with ana_lock
PATCH v2 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.
Take ana_lock around the buffer release in nvme_mpath_uninit() and around the reallocation in nvme_mpath_init_identify(), updating ana_log_buf and ana_log_size together so a parser never observes a freed or NULL buffer or a stale size. Allocate the new buffer outside the lock and zero it, so a parser running before nvme_read_ana_log() populates it sees ngrps == 0. nvme_parse_ana_log() also bails out when the buffer is NULL, covering the teardown paths. 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.
The race is in the reallocation path: nvme_mpath_init_identify() frees the old buffer through nvme_mpath_uninit() before taking ana_lock in nvme_read_ana_log(). The FROZEN guard does not close the window: a tcp reset runs nvme_init_identify() (configure_admin_queue) before nvme_start_freeze() (configure_io_queues), and reset does not flush scan_work.
Reproduction (test-only delays + forced ANA-log growth):
- use-after-free: buffer address tracking shows nvme_mpath_uninit() freeing the old ana_log_buf while nvme_parse_ana_log() still dereferences it (the new buffer lands at a different address).
- NULL dereference: widening the window between nvme_mpath_uninit() and the reallocation makes nvme_mpath_add_disk() pass nvme_ctrl_use_ana() and then parse a NULL buffer; without the fix this faults in nvme_parse_ana_log() (buf register NULL).
- uninitialized read: the new buffer is parsed before nvme_read_ana_log() populates it.
With the fix the buffer pointer and ana_log_size are swapped atomically under ana_lock (a parser sees neither a freed nor a NULL buffer), the new buffer is zeroed (ngrps == 0 before population), and nvme_parse_ana_log() bails out on NULL; none of the three windows fault. ANA change/optimized transitions and multipath I/O remain unaffected.
Changes since v1:
- serialize the reallocation swap (buffer pointer and ana_log_size) under ana_lock, allocating the new buffer outside the lock, so a parser never observes a freed or NULL buffer; also 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 (nvme_mpath_add_disk() tests nvme_ctrl_use_ana() outside ana_lock) and an uninitialized-memory read before nvme_read_ana_log() populates the new buffer (Sashiko AI review)
v1: https://lore.kernel.org/all/[email protected]/
drivers/nvme/host/multipath.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
drivers/nvme/host/multipath.c +18 -4
@@ -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;
@@ -1622,13 +1625,22 @@ 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; +mutex_unlock (&ctrl->ana_lock); + }else { +mutex_lock (&ctrl->ana_lock); + ctrl->ana_log_size = ana_log_size; +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 +1660,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 2 potential issues to consider: - [High] nvme-multipath: ana_work re-queued with NULL buffer after nvme_mpath_uninit() - [Medium]…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] nvme-multipath: ana_work re-queued with NULL buffer after nvme_mpath_uninit()
- [Medium] nvme-multipath: stale ana_log_buf parsed against shrunk size on reset
--
commit 6951dbeaa851be5d302a81751db149aaf0a1b322
Author: Guixin Liu <[email protected]>
nvme-multipath: serialize ana_log_buf lifecycle with ana_lock
This commit serializes ctrl->ana_log_buf and ctrl->ana_log_size updates under ctrl->ana_lock across nvme_mpath_init_identify() and nvme_mpath_uninit(), and adds a NULL buffer check to nvme_parse_ana_log().
Quoted 31 lines
drivers/nvme/host/multipath.c +11
-3@@ -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 ; +for (i =0 ; i <le16_to_cpu (ctrl->ana_log_buf->ngrps); i++) {struct nvme_ana_group_desc *desc = base + offset;u32 nr_nsids;@@ -1589,13 +1592,22 @@ 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;
drivers/nvme/host/multipath.c int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) · line 1613
Quoted 10 lines
drivers/nvme/host/multipath.c +5
-1+mutex_unlock (&ctrl->ana_lock); + }else { +mutex_lock (&ctrl->ana_lock); + ctrl->ana_log_size = ana_log_size; +mutex_unlock (&ctrl->ana_lock); } - ctrl->ana_log_size = ana_log_size; error =nvme_read_ana_log (ctrl);if (error) goto out_uninit;
[Severity: Medium]
When a controller resets with a smaller ana_log_size (or a smaller ctrl->anagrpmax, which is updated above outside ctrl->ana_lock), does this else branch leave the pre-reset ANA log in ctrl->ana_log_buf while shrinking ctrl->ana_log_size?
A concurrent nvme_parse_ana_log() running before nvme_read_ana_log() overwrites the buffer will validate the old descriptors against the newly shrunk ctrl->ana_log_size and ctrl->anagrpmax, hitting WARN_ON_ONCE() and failing with -EINVAL:
CPU0 CPU1nvme_mpath_init_identify()
ctrl->anagrpmax = ...
mutex_lock(&ctrl->ana_lock)
ctrl->ana_log_size = ana_log_size
mutex_unlock(&ctrl->ana_lock)
nvme_mpath_add_disk()
mutex_lock(&ctrl->ana_lock)
nvme_parse_ana_log()
offset > ctrl->ana_log_size
<-- WARN_ON_ONCE() nvme_read_ana_log()
Zeroing ctrl->ana_log_buf->ngrps (and updating ctrl->anagrpmax) under ctrl->ana_lock would keep concurrent parsers from walking stale pre-reset descriptors before nvme_read_ana_log() populates the new log.
drivers/nvme/host/multipath.c void nvme_mpath_uninit(struct nvme_ctrl *ctrl) · line 1635
Quoted 10 lines
drivers/nvme/host/multipath.c +2
-0@@ -1615,7 +1627,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); }
[Severity: High]
Can nvme_mpath_add_disk() re-queue ctrl->ana_work after nvme_mpath_uninit() has already cancelled it and freed ctrl->ana_log_buf?
When nvme_mpath_init_identify() jumps to out_uninit during a reset (for example, when ana_log_size > max_transfer_size disables ANA with error == 0 so the controller still becomes live), nvme_mpath_add_disk() can pass its unlocked nvme_ctrl_use_ana() check before ctrl->ana_log_buf is cleared:
CPU0 CPU1nvme_mpath_add_disk() nvme_mpath_uninit()
if (nvme_ctrl_use_ana(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)
mutex_lock(&ctrl->ana_lock)
nvme_parse_ana_log()
if (!ctrl->ana_log_buf)
return 0
mutex_unlock(&ctrl->ana_lock)
if (desc.state) /* 0 */
else
set_bit(NVME_NS_ANA_PENDING, ...)
queue_work(..., &ctrl->ana_work)
nvme_ana_work()
nvme_read_ana_log()
nvme_get_log(..., NULL, 0, 0)
<-- numd underflows to 0xffffffffBecause nvme_parse_ana_log() returns 0 with desc.state == 0 when ctrl->ana_log_buf is NULL, nvme_mpath_add_disk() treats it as a missing group descriptor, leaves the namespace stuck with NVME_NS_ANA_PENDING set, and queues ctrl->ana_work. nvme_read_ana_log() then calls nvme_get_log() without checking ctrl->ana_log_buf, passing a NULL buffer and size 0; nvme_bytes_to_numd(0) underflows to 0xffffffff and submits a 16 GiB Admin Get Log Page command with no data buffer mapped.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 128 lines · 在 2026/11/25 20:39, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] nvme-multipath:ana_workre-queued with NULL buffer afternvme_mpath_uninit()
- [Medium] nvme-multipath: staleana_log_bufparsed against shrunk size on reset
--commit
6951dbeaa851be5d302a81751db149aaf0a1b322
Author: Guixin Liu <[email protected]>nvme-multipath: serialize
ana_log_buflifecycle withana_lockThis commit serializes
ctrl->ana_log_bufandctrl->ana_log_sizeupdates underctrl->ana_lockacrossnvme_mpath_init_identify()andnvme_mpath_uninit(), and adds a NULL buffer check tonvme_parse_ana_log().Quoted 31 lines
drivers/nvme/host/multipath.c +11
-3@@ -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 ; +for (i =0 ; i <le16_to_cpu (ctrl->ana_log_buf->ngrps); i++) {struct nvme_ana_group_desc *desc = base + offset;u32 nr_nsids;@@ -1589,13 +1592,22 @@ 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;drivers/nvme/host/multipath.c int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) · line 1613
Quoted 10 lines
drivers/nvme/host/multipath.c +5
-1+mutex_unlock (&ctrl->ana_lock); + }else { +mutex_lock (&ctrl->ana_lock); + ctrl->ana_log_size = ana_log_size; +mutex_unlock (&ctrl->ana_lock); } - ctrl->ana_log_size = ana_log_size; error =nvme_read_ana_log (ctrl);if (error) goto out_uninit;[Severity: Medium]
When a controller resets with a smallerana_log_size(or a smallerctrl->anagrpmax, which is updated above outsidectrl->ana_lock), does this else branch leave the pre-reset ANA log inctrl->ana_log_bufwhile shrinkingctrl->ana_log_size?A concurrent
nvme_parse_ana_log()running beforenvme_read_ana_log()overwrites the buffer will validate the old descriptors against the newly shrunkctrl->ana_log_sizeandctrl->anagrpmax, hittingWARN_ON_ONCE()and failing with-EINVAL:CPU0 CPU1
nvme_mpath_init_identify()
ctrl->anagrpmax= ...
mutex_lock(&ctrl->ana_lock)
ctrl->ana_log_size=ana_log_size
mutex_unlock(&ctrl->ana_lock)nvme_mpath_add_disk() mutex_lock(&ctrl->ana_lock) nvme_parse_ana_log() offset > ctrl->ana_log_size <-- WARN_ON_ONCE()
nvme_read_ana_log()Zeroing
ctrl->ana_log_buf->ngrps(and updatingctrl->anagrpmax) underctrl->ana_lockwould keep concurrent parsers from walking stale pre-reset descriptors beforenvme_read_ana_log()populates the new log.drivers/nvme/host/multipath.c void nvme_mpath_uninit(struct nvme_ctrl *ctrl) · line 1635
Quoted 10 lines
drivers/nvme/host/multipath.c +2
-0@@ -1615,7 +1627,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); }[Severity: High]
Cannvme_mpath_add_disk()re-queuectrl->ana_workafternvme_mpath_uninit()has already cancelled it and freedctrl->ana_log_buf?When
nvme_mpath_init_identify()jumps toout_uninitduring a reset (for example, whenana_log_size>max_transfer_sizedisables ANA with error == 0 so the controller still becomes live),nvme_mpath_add_disk()can pass its unlockednvme_ctrl_use_ana()check beforectrl->ana_log_bufis cleared:CPU0 CPU1
nvme_mpath_add_disk()nvme_mpath_uninit()
if (nvme_ctrl_use_ana(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) mutex_lock(&ctrl->ana_lock) nvme_parse_ana_log() if (!ctrl->ana_log_buf) return 0 mutex_unlock(&ctrl->ana_lock) if (desc.state) /* 0 */ else set_bit(NVME_NS_ANA_PENDING, ...) queue_work(..., &ctrl->ana_work) nvme_ana_work() nvme_read_ana_log() nvme_get_log(..., NULL, 0, 0) <-- numd underflows to 0xffffffff
Quoted 7 lines
Because
nvme_parse_ana_log()returns 0 with desc.state == 0 whenctrl->ana_log_bufis NULL,nvme_mpath_add_disk()treats it as a missing group descriptor, leaves the namespace stuck withNVME_NS_ANA_PENDINGset, and queuesctrl->ana_work.nvme_read_ana_log()then callsnvme_get_log()without checkingctrl->ana_log_buf, passing a NULL buffer and size 0;nvme_bytes_to_numd(0)underflows to0xffffffffand submits a 16 GiB Admin Get Log Page command with no data buffer mapped.
Right.. Fixed in v3.
Best Regards,
Guixin Liu