Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Guixin Liu" <kanie@linux.alibaba.com>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH v2] nvme-multipath: serialize ana_log_buf lifecycle with ana_lock
Date: Wed, 25 Nov 2026 12:39:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-233382@kernel.org> (raw)
In-Reply-To: <20261125122446.1122253-1-kanie@linux.alibaba.com>

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

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().

> diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
> index 27facdbb3d4c0..91ba71ab1c614 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;
> +
>  	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;
> +		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                                  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 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.

> @@ -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                                  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

Because 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.

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


  reply	other threads:[~2026-11-25 12:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 12:24 [PATCH v2] nvme-multipath: serialize ana_log_buf lifecycle with ana_lock Guixin Liu
2026-11-25 12:39 ` sashiko-bot [this message]
2026-11-26  1:43   ` Guixin Liu

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=sashiko-outbox-233382@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kanie@linux.alibaba.com \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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