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

sashiko-bot at kernel.org sashiko-bot at kernel.org
Wed Dec 2 09:04:14 PST 2026


Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] nvme: nvme_keep_alive_end_io() re-arms ka_work after
  nvme_stop_keep_alive()
- [High] nvme: 0-delay ka_work loop in nvme_keep_alive_end_io() on
  unstarted rq
- [High] nvme: TOCTOU race and early blk_mq_free_request() in
  nvme_keep_alive_end_io()
- [Medium] nvme: nvme_keep_alive_end_io() leaves comp_seen set on Keep
  Alive error
--

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

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

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

> diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
> index f43582e5f6c4a..ca04ff0f437d6 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 || state == NVME_CTRL_CONNECTING)
> +			queue_delayed_work(nvme_wq, &ctrl->ka_work, delay);
>  		return RQ_END_IO_NONE;
>  	}
>  

[Severity: High]
Can this re-arm ctrl->ka_work after nvme_stop_keep_alive() has already
returned during admin queue teardown in NVME_CTRL_CONNECTING?

nvme_init_ctrl_finish() starts keep-alive while ctrl->state is still
NVME_CTRL_CONNECTING.  When nvme_keep_alive_work() runs, it submits the
Keep Alive request on ctrl->admin_q via blk_execute_rq_nowait(), so
ctrl->ka_work is not pending while the request is in flight.

If controller setup subsequently tears down or restarts the admin queue in
NVME_CTRL_CONNECTING (such as the destroy_admin and secure-concatenation
paths in nvme_tcp_setup_ctrl()), it calls nvme_stop_keep_alive() and then
nvme_tcp_teardown_admin_queue() -> nvme_cancel_admin_tagset():

drivers/nvme/host/tcp.c:nvme_tcp_setup_ctrl() {
    ...
destroy_admin:
	nvme_stop_keep_alive(ctrl);
	nvme_tcp_teardown_admin_queue(ctrl, new);
    ...
}

Because nvme_stop_keep_alive() only calls
cancel_delayed_work_sync(&ctrl->ka_work), it does not wait for the
in-flight Keep Alive request.  nvme_cancel_admin_tagset() then cancels that
request via nvme_cancel_request() with NVME_SC_HOST_ABORTED_CMD and
NVME_REQ_CANCELLED while ctrl->state is still NVME_CTRL_CONNECTING, and
nvme_keep_alive_end_io() now queues ctrl->ka_work again after keep-alive
was stopped and while the transport admin queue is being torn down.

[Severity: High]
Does this enter a zero-delay workqueue loop on nvme_wq when a Keep Alive
request fails before blk_mq_start_request() sets rq->deadline?

blk_mq_rq_ctx_init() initializes rq->deadline to 0, and rq->deadline is
only populated when the transport ->queue_rq() callback calls
nvme_start_request().  If the transport admin queue is not live while
ctrl->admin_q is unquiesced in NVME_CTRL_CONNECTING (for example, after
nvme_tcp_complete_timed_out() stops queue 0 on a Keep Alive timeout in
NVME_CTRL_CONNECTING, or after nvme_fc_delete_association() unquiesces
ctrl->admin_q), ->queue_rq() calls nvme_fail_nonready_command() before
starting the request.

Because nvme_init_request() sets REQ_FAILFAST_DRIVER on Keep Alive
requests, nvme_fail_nonready_command() immediately completes rq via
nvme_host_path_error() with rq->deadline still 0.  At the top of
nvme_keep_alive_end_io(), rtt = jiffies - (rq->deadline - rq->timeout)
evaluates to jiffies + rq->timeout > delay, logs a spurious long keepalive
RTT warning, sets delay = 0, and now requeues ctrl->ka_work with a delay of
0 in a tight loop.

[Severity: High]
Can a concurrent controller reset or teardown finish stopping keep-alive
between blk_mq_free_request(rq) and queue_delayed_work()?

CPU0                                    CPU1
nvme_keep_alive_end_io()
  state = nvme_ctrl_state(ctrl)
  /* state == NVME_CTRL_LIVE */
                                        nvme_change_ctrl_state(...,
                                          NVME_CTRL_RESETTING)
                                        nvme_stop_keep_alive(ctrl)
                                        nvme_cancel_admin_tagset(ctrl)
  blk_mq_free_request(rq)
                                        /* tagset wait / queue destroy
                                           completes once rq is freed */
  dev_err(...)
  if (state == NVME_CTRL_LIVE ...)
    queue_delayed_work(..., &ctrl->ka_work, ...)  <-- re-armed after stop

nvme_keep_alive_end_io() snapshots ctrl->state at entry and calls
blk_mq_free_request(rq) before dev_err() and queue_delayed_work().  Freeing
rq drops the admin_tagset tag and admin_q usage reference that
nvme_cancel_admin_tagset() and nvme_remove_admin_tag_set() wait on, so a
concurrent reset or deletion can complete teardown while CPU0 is in
dev_err(), after which CPU0 tests the stale state snapshot and re-queues
ctrl->ka_work on a stopped or uninitialized controller.

[Severity: Medium]
Does returning before clearing ctrl->comp_seen cause the next
nvme_keep_alive_work() run to skip sending a Keep Alive on controllers with
Traffic-Based Keep Alive (NVME_CTRL_ATTR_TBKAS)?

When the failed Keep Alive request completes through __nvme_complete_rq(),
it sets ctrl->comp_seen = true:

drivers/nvme/host/core.c:__nvme_complete_rq() {
    ...
	if (ctrl->kas &&
	    req->deadline - req->timeout >= ctrl->ka_last_check_time)
		ctrl->comp_seen = true;
    ...
}

The success path in nvme_keep_alive_end_io() resets ctrl->comp_seen = false
and updates ctrl->ka_last_check_time = jiffies, but the error path returns
early with comp_seen still set.  On the next run of nvme_keep_alive_work(),
(ctrl->ctratt & NVME_CTRL_ATTR_TBKAS) && comp_seen is true, so it treats
the failed Keep Alive itself as I/O traffic and defers the retry by another
keep-alive work period, which can still let the target's KATO timer expire.

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



More information about the Linux-nvme mailing list