Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mohamed Khalfella" <mkhalfella@purestorage.com>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH v7 03/17] nvmet: Implement CCR nvme command
Date: Sat, 14 Nov 2026 03:30:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-214113@kernel.org> (raw)
In-Reply-To: <20261114030454.2836543-4-mkhalfella@purestorage.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] nvmet: TOCTOU race between nvmet_ctrl_find_get_ccr() and
  nvmet_start_ctrl()
--

commit 8afa887480a9e0f0751311107895be0ad6ed159f
Author: Mohamed Khalfella <mkhalfella@purestorage.com>

nvmet: Implement CCR nvme command

This commit implements the Cross-Controller Reset admin command for the
NVMe target, allowing a source controller to look up and trigger a fatal
error on an impacted controller matching the specified controller ID, ciu,
and cirn.

> diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
> index 5710ae53eb83a..3df9db454db9c 100644
> --- a/drivers/nvme/target/admin-cmd.c
> +++ b/drivers/nvme/target/admin-cmd.c
[ ... ]
> @@ -1629,6 +1630,78 @@ void nvmet_execute_keep_alive(struct nvmet_req *req)
>  	nvmet_req_complete(req, status);
>  }
>  
> +void nvmet_execute_cross_ctrl_reset(struct nvmet_req *req)
> +{
[ ... ]
> +	/* Find and get impacted controller */
> +	ictrl = nvmet_ctrl_find_get_ccr(sctrl->subsys, sctrl->hostnqn,
> +					cmd->ccr.ciu, cntlid,
> +					le64_to_cpu(cmd->ccr.cirn));
[ ... ]
> +out_unlock:
> +	mutex_unlock(&sctrl->lock);
> +	if (status == NVME_SC_SUCCESS)
> +		nvmet_ctrl_fatal_error(ictrl);
> +	nvmet_ctrl_put(ictrl);
> +out:
> +	nvmet_req_complete(req, status);
> +}
[ ... ]
> diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
> index 9c9ad4d9b292d..86018e0fb1586 100644
> --- a/drivers/nvme/target/core.c
> +++ b/drivers/nvme/target/core.c
[ ... ]
> @@ -1432,6 +1446,7 @@ static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
>  	if (!nvmet_is_disc_subsys(ctrl->subsys)) {
>  		ctrl->ciu = ((u8)(ctrl->ciu + 1)) ? : 1;
>  		ctrl->cirn = get_random_u64() ? : 1;
> +		nvmet_ctrl_cleanup_ccrs(ctrl, false);
>  	}
>  	ctrl->csts = NVME_CSTS_RDY;
>  
> @@ -1537,6 +1552,42 @@ struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn,
>  	return ctrl;
>  }
>  
> +struct nvmet_ctrl *nvmet_ctrl_find_get_ccr(struct nvmet_subsys *subsys,
> +					   const char *hostnqn, u8 ciu,
> +					   u16 cntlid, u64 cirn)
> +{
> +	struct nvmet_ctrl *ctrl, *ictrl = NULL;
> +	bool found = false;
> +
> +	mutex_lock(&subsys->lock);
> +	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
> +		if (ctrl->cntlid != cntlid)
> +			continue;
> +		if (strncmp(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE))
> +			continue;
> +
> +		/*
> +		 * Enabling the controller regenerates ciu and cirn under
> +		 * ctrl->lock. Hold the lock so that a controller becoming
> +		 * ready concurrently is matched against either the old or
> +		 * the new instance, never a mix of both.
> +		 */
> +		mutex_lock(&ctrl->lock);
> +		if (ctrl->ciu == ciu && ctrl->cirn == cirn)
> +			found = true;
> +		mutex_unlock(&ctrl->lock);

[Severity: Medium]
Can nvmet_execute_cross_ctrl_reset() race with nvmet_start_ctrl() and
trigger a fatal error on a newly enabled controller instance?

nvmet_ctrl_find_get_ccr() drops ctrl->lock right after matching ctrl->ciu
and ctrl->cirn.  Before nvmet_execute_cross_ctrl_reset() calls
nvmet_ctrl_fatal_error(ictrl), a concurrent nvmet_update_cc() can enable
ictrl and advance ictrl->ciu and ictrl->cirn:

CPU0                              CPU1
nvmet_execute_cross_ctrl_reset()
  nvmet_ctrl_find_get_ccr()
    mutex_lock(&ictrl->lock)
    /* matches ciu and cirn */
    mutex_unlock(&ictrl->lock)
                                  nvmet_update_cc()
                                    mutex_lock(&ictrl->lock)
                                    nvmet_start_ctrl()
                                      ictrl->ciu = ...;
                                      ictrl->cirn = ...;
                                    mutex_unlock(&ictrl->lock)
  nvmet_ctrl_fatal_error(ictrl)   <-- kills the new instance

If the interleaving goes the other way and nvmet_ctrl_fatal_error(ictrl)
runs just before nvmet_start_ctrl(), nvmet_start_ctrl() overwrites
ictrl->csts with the ready state, clearing the fatal status bit while
ictrl->fatal_err_work is already queued.

In addition, when a controller is re-enabled in-place via
nvmet_start_ctrl() rather than freed in nvmet_ctrl_free(),
nvmet_start_ctrl() only cleans up entries on ctrl->ccr_list where ctrl was
the source controller, leaving pending entries on other controllers where
ctrl was the impacted controller active with a non-null ccr->ctrl pointer.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114030454.2836543-1-mkhalfella@purestorage.com?part=3


  reply	other threads:[~2026-11-14  3:30 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-14  3:04 [PATCH v7 00/17] TP8028 Rapid Path Failure Recovery Mohamed Khalfella
2026-11-14  3:04 ` [PATCH v7 01/17] nvmet: Rapid Path Failure Recovery set controller identify fields Mohamed Khalfella
2026-11-14  3:17   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 02/17] nvmet/debugfs: Export controller CIU and CIRN via debugfs Mohamed Khalfella
2026-11-14  3:12   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 03/17] nvmet: Implement CCR nvme command Mohamed Khalfella
2026-11-14  3:30   ` sashiko-bot [this message]
2026-11-14  3:04 ` [PATCH v7 04/17] nvmet: Implement CCR logpage Mohamed Khalfella
2026-11-14  3:20   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 05/17] nvmet: Send an AEN on CCR completion Mohamed Khalfella
2026-11-14  3:04 ` [PATCH v7 06/17] nvme: Rapid Path Failure Recovery read controller identify fields Mohamed Khalfella
2026-11-14  3:04 ` [PATCH v7 07/17] nvme: Introduce FENCING and FENCED controller states Mohamed Khalfella
2026-11-14  3:29   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 08/17] nvme: Implement cross-controller reset recovery Mohamed Khalfella
2026-11-14  3:26   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 09/17] nvme: Implement cross-controller reset completion Mohamed Khalfella
2026-11-14  3:29   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 10/17] nvme-tcp: Use CCR to recover controller that hits an error Mohamed Khalfella
2026-11-14  3:38   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 11/17] nvme-rdma: " Mohamed Khalfella
2026-11-14  3:04 ` [PATCH v7 12/17] nvme-fc: start error recovery instead of aborting timed out IOs Mohamed Khalfella
2026-11-14  3:46   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 13/17] nvme-fc: perform error recovery directly from ioerr_work Mohamed Khalfella
2026-11-14  3:31   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 14/17] nvme-fc: Use CCR to recover controller that hits an error Mohamed Khalfella
2026-11-14  3:38   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 15/17] nvme-fc: Hold inflight requests while in FENCING state Mohamed Khalfella
2026-11-14  4:06   ` sashiko-bot
2026-11-14  3:04 ` [PATCH v7 16/17] nvmet: Add support for CQT to nvme target Mohamed Khalfella
2026-11-14  3:04 ` [PATCH v7 17/17] nvme: Add support for CQT to nvme host Mohamed Khalfella

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-214113@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=mkhalfella@purestorage.com \
    --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