[PATCH v3] nvme-multipath: serialize ana_log_buf lifecycle with ana_lock

sashiko-bot at kernel.org sashiko-bot at kernel.org
Tue Dec 1 00:08:13 PST 2026


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 <kanie at linux.alibaba.com>

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.

> diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
> index 27facdbb3d4c0..e0476af20f3e8 100644
> --- a/drivers/nvme/host/multipath.c
> +++ b/drivers/nvme/host/multipath.c
> @@ -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                                  CPU1
nvme_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?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201074919.1051448-1-kanie@linux.alibaba.com?part=1



More information about the Linux-nvme mailing list