All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme: requeue keep-alive work when a Keep Alive fails
@ 2026-12-02 16:37 Andrew Stellman
  2026-12-02 17:04 ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Stellman @ 2026-12-02 16:37 UTC (permalink / raw)
  To: Keith Busch, Christoph Hellwig, Sagi Grimberg
  Cc: Jens Axboe, linux-nvme, linux-kernel

From: Andrew Stellman <astellman@stellman-greene.com>

When a Keep Alive command completes with an error,
nvme_keep_alive_end_io() logs it and returns without requeueing
ctrl->ka_work, so no further Keep Alives are sent while the controller
stays LIVE. With a test-only nvmet change that failed one Keep Alive,
the target's keep-alive timer expired.

Requeue ka_work on the error path under the same LIVE/CONNECTING check
the success path uses.

Found with Quality Playbook
(https://github.com/andrewstellman/quality-playbook).

Fixes: 038bd4cb6766 ("nvme: add keep-alive support")
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
This changes behavior: after a Keep Alive error in LIVE or CONNECTING,
Keep Alives now continue instead of stopping.

Tested on mainline 7.4.0-rc5 (19cc08c1a6a6) in arm64 QEMU with KASAN,
NVMe/TCP loopback to nvmet with keep_alive_tmo=6, using a test-only
nvmet change that fails one Keep Alive with Internal Error/DNR. Without
the patch no further Keep Alive was sent before the target's keep-alive
timer expired; with it the next Keep Alive arrived about 3 seconds later
and the controller stayed LIVE. No other status, transport or the
CONNECTING case was tested.

I found this issue running Quality Playbook with Claude Opus 5.5. The
fix was drafted with OpenAI GPT-6 (Codex), and this changelog with
OpenAI GPT-6 (Codex) and Claude Sonnet 5.5; both were reviewed by me. A
reproducer is available on request.

 drivers/nvme/host/core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index f43582e5f6c4..ca04ff0f437d 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;
 	}
 
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] nvme: requeue keep-alive work when a Keep Alive fails
@ 2026-12-02 16:49 Andrew Stellman
  2026-12-02 17:13 ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Stellman @ 2026-12-02 16:49 UTC (permalink / raw)
  To: Keith Busch, Christoph Hellwig, Sagi Grimberg
  Cc: Jens Axboe, linux-nvme, linux-kernel

From: Andrew Stellman <astellman@stellman-greene.com>

When a Keep Alive command completes with an error,
nvme_keep_alive_end_io() logs it and returns without requeueing
ctrl->ka_work, so no further Keep Alives are sent while the controller
stays LIVE. With a test-only nvmet change that failed one Keep Alive,
the target's keep-alive timer expired.

Requeue ka_work on the error path under the same LIVE/CONNECTING check
the success path uses.

Found with Quality Playbook
(https://github.com/andrewstellman/quality-playbook).

Fixes: 038bd4cb6766 ("nvme: add keep-alive support")
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
This changes behavior: after a Keep Alive error in LIVE or CONNECTING,
Keep Alives now continue instead of stopping.

Tested on mainline 7.4.0-rc5 (19cc08c1a6a6) in arm64 QEMU with KASAN,
NVMe/TCP loopback to nvmet with keep_alive_tmo=6, using a test-only
nvmet change that fails one Keep Alive with Internal Error/DNR. Without
the patch no further Keep Alive was sent before the target's keep-alive
timer expired; with it the next Keep Alive arrived about 3 seconds later
and the controller stayed LIVE. No other status, transport or the
CONNECTING case was tested.

I found this issue running Quality Playbook with Claude Opus 5.5. The
fix was drafted with OpenAI GPT-6 (Codex), and this changelog with
OpenAI GPT-6 (Codex) and Claude Sonnet 5.5; both were reviewed by me. A
reproducer is available on request.

 drivers/nvme/host/core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index f43582e5f6c4..ca04ff0f437d 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;
 	}
 
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvme: requeue keep-alive work when a Keep Alive fails
  2026-12-02 16:37 Andrew Stellman
@ 2026-12-02 17:04 ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-02 17:04 UTC (permalink / raw)
  To: Andrew Stellman; +Cc: linux-nvme

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvme: requeue keep-alive work when a Keep Alive fails
  2026-12-02 16:49 [PATCH] nvme: requeue keep-alive work when a Keep Alive fails Andrew Stellman
@ 2026-12-02 17:13 ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-02 17:13 UTC (permalink / raw)
  To: Andrew Stellman; +Cc: linux-nvme

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] nvme: zero-delay ka_work loop in nvme_keep_alive_end_io() when
  deadline is 0
- [High] nvme: nvme_keep_alive_end_io() re-arms ka_work after
  nvme_stop_keep_alive()
- [Medium] nvme: uncleared comp_seen on error in
  nvme_keep_alive_end_io() skips retry
--

commit df70267c1d3880bdae24c7ceb3c48d593ea8973c
Author: Andrew Stellman <astellman@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 spin in a zero-delay workqueue loop on nvme_wq if the request
fails before nvme_start_request() sets rq->deadline?

Keep Alive requests carry REQ_FAILFAST_DRIVER, so when ->queue_rq rejects
a command on an unquiesced non-ready admin queue via
nvme_fail_nonready_command() (for example in nvme_tcp_queue_rq() after
nvme_tcp_complete_timed_out() stops queue 0 while in
NVME_CTRL_CONNECTING), the request completes immediately with
rq->deadline still 0.  nvme_keep_alive_end_io() then computes rtt as
jiffies + rq->timeout, logs an unratelimited warning, sets delay = 0, and
requeues ctrl->ka_work with a delay of 0.

[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?

In nvme_tcp_setup_ctrl() and nvme_rdma_setup_ctrl(), the destroy_admin
error path (as well as the TLS secure concatenation admin queue restart)
calls nvme_stop_keep_alive() before tearing down the admin queue:

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

If a Keep Alive request is already in flight when nvme_stop_keep_alive()
runs, cancel_delayed_work_sync(&ctrl->ka_work) returns while the request
is still outstanding.  nvme_tcp_teardown_admin_queue() then calls
nvme_cancel_admin_tagset(), completing the request with
NVME_SC_HOST_ABORTED_CMD while ctrl->state is still NVME_CTRL_CONNECTING,
which now queues ctrl->ka_work again on the torn-down admin queue.

[Severity: Medium]
Should the error path also clear ctrl->comp_seen (or update
ctrl->ka_last_check_time) before requeueing ctrl->ka_work?

When the Keep Alive request completes with an error, __nvme_complete_rq()
still sets ctrl->comp_seen = true because rq->deadline - rq->timeout >=
ctrl->ka_last_check_time:

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

Returning before ctrl->comp_seen = false leaves comp_seen set on
controllers with NVME_CTRL_ATTR_TBKAS enabled, so the next run of
nvme_keep_alive_work() treats the failed Keep Alive completion itself as
I/O traffic, skips sending a retry, and defers for another period, which
can still let the target's keep-alive timer expire.

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-02 17:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 16:49 [PATCH] nvme: requeue keep-alive work when a Keep Alive fails Andrew Stellman
2026-12-02 17:13 ` sashiko-bot
  -- strict thread matches above, loose matches on Subject: below --
2026-12-02 16:37 Andrew Stellman
2026-12-02 17:04 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.