Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] nvme: requeue keep-alive work when a Keep Alive fails
@ 2026-12-02 19:22 Andrew Stellman
  2026-12-02 19:46 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Stellman @ 2026-12-02 19:22 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 fails, nvme_keep_alive_end_io() returns without
queueing ka_work again, so no more Keep Alives get sent and the target's
keep-alive timer expires even though the controller is still LIVE. Fix
this by requeueing ka_work in LIVE with nvme_queue_keep_alive_work(), so
a failed Keep Alive gets retried after the normal delay.

Fixes: 038bd4cb6766 ("nvme: add keep-alive support")
Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
---
v2: requeue only in LIVE, with the normal delay, after Sashiko's review
of v1. A failed Keep Alive in CONNECTING still stops keep-alive, as it
does today.
v1: https://lore.kernel.org/r/20261202163728.48705-1-bugs@stellman-greene.com

Tested on mainline 19cc08c1a6a6 in arm64 QEMU with KASAN over NVMe/TCP
loopback with a test-only nvmet error: before the fix the target's
keep-alive timer expired, and with it another Keep Alive arrived first.

Found with Quality Playbook
(https://github.com/andrewstellman/quality-playbook) running Claude
Opus 5.5; the fix was drafted with OpenAI GPT-6 (Codex).

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..58bae0e85a01 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;
 	}
 
-- 
2.53.0



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

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

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


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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 19:22 [PATCH v2] nvme: requeue keep-alive work when a Keep Alive fails Andrew Stellman
2026-12-02 19:46 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox