[PATCH v2] nvme: requeue keep-alive work when a Keep Alive fails

sashiko-bot at kernel.org sashiko-bot at kernel.org
Wed Dec 2 11:46:58 PST 2026


Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] nvme: missing comp_seen reset on error in
  nvme_keep_alive_end_io()
- [Medium] nvme: TOCTOU race and early blk_mq_free_request() in
  nvme_keep_alive_end_io()
--

commit 236de8328119255613d8447e380cbb58332dc209
Author: Andrew Stellman <astellman at stellman-greene.com>

nvme: requeue keep-alive work when a Keep Alive fails

This commit requeues ctrl->ka_work via nvme_queue_keep_alive_work() when a
Keep Alive command completes with an error while the controller is in
NVME_CTRL_LIVE.

> diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
> index f43582e5f6c4a..58bae0e85a012 100644
> --- a/drivers/nvme/host/core.c
> +++ b/drivers/nvme/host/core.c
> @@ -1387,6 +1387,8 @@ static enum rq_end_io_ret nvme_keep_alive_end_io(struct request *rq,
>  		dev_err(ctrl->device,
>  			"failed nvme_keep_alive_end_io error=%d\n",
>  				status);
> +		if (state == NVME_CTRL_LIVE)
> +			nvme_queue_keep_alive_work(ctrl);
>  		return RQ_END_IO_NONE;
>  	}

[Severity: Medium]
Should the error path also reset ctrl->comp_seen = false before calling
nvme_queue_keep_alive_work(ctrl), as the success path does?

__nvme_complete_rq() sets ctrl->comp_seen = true for any completing
request started at or after ctrl->ka_last_check_time, including a failed
Keep Alive request.  When NVME_CTRL_ATTR_TBKAS is enabled, the next run
of nvme_keep_alive_work() sees ctrl->comp_seen == true, treats the failed
Keep Alive completion as I/O traffic, and defers sending the retry Keep
Alive for another work period, which can still let the target's keep-alive
timer expire.

[Severity: Medium]
Can checking the entry snapshot of state after blk_mq_free_request(rq)
requeue ctrl->ka_work on a controller that is already resetting or
deleting?

nvme_keep_alive_end_io() snapshots state at function entry and frees rq
before dev_err() and this check.  Once blk_mq_free_request(rq) releases
the admin tag and queue reference, a concurrent reset or teardown is no
longer blocked in nvme_cancel_admin_tagset() or blk_mq_destroy_queue():

CPU0                                    CPU1
nvme_keep_alive_end_io()
  state = nvme_ctrl_state(ctrl)
    // NVME_CTRL_LIVE
  blk_mq_free_request(rq)
                                        nvme_remove_admin_tag_set()
                                          nvme_stop_keep_alive(ctrl)
                                          blk_mq_destroy_queue(ctrl->admin_q)
  dev_err(...)
  if (state == NVME_CTRL_LIVE)
    nvme_queue_keep_alive_work(ctrl)
                                        nvme_keep_alive_work()
                                          ...  <-- use-after-free on admin_q

Re-reading nvme_ctrl_state(ctrl) before requeueing and freeing rq after
handling the completion would narrow this window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202192236.88068-1-bugs@stellman-greene.com?part=1



More information about the Linux-nvme mailing list