* [PATCH] nvme-tcp: drop the queue's reference to a timed-out request
@ 2026-11-29 3:15 Yehyeong Lee
2026-11-29 3:46 ` sashiko-bot
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Yehyeong Lee @ 2026-11-29 3:15 UTC (permalink / raw)
To: hch, kbusch, sagi; +Cc: linux-nvme, linux-kernel
When a command times out while the controller is not LIVE, error
recovery is not an option: nvme_tcp_error_recovery() can only move the
controller to RESETTING from NEW or LIVE, so from CONNECTING it returns
without doing anything and no teardown runs. nvme_tcp_timeout()
therefore completes the command itself. nvme_tcp_complete_timed_out()
stops the one queue the command was on and ends the request, which
frees its bio.
Stopping the queue cancels io_work, but it does not drop the queue's
own references to that request. The queue refers to a request as
queue->request while it is being sent, and through its send_list or
req_list while the request waits its turn. All three are dropped only
by the send path itself, in nvme_tcp_fetch_request() and
nvme_tcp_done_send_req(), and stopping the queue is what shuts that
path down, so the references outlive the completion.
The same completion releases the reconnect's freeze wait, so the
controller is switched to LIVE while that queue is still stopped:
NVME_TCP_Q_LIVE is set only by nvme_tcp_start_queue(), which needs a
new connect. nvme_check_ready() lets a command through as soon as the
controller state is LIVE without looking at the queue, so blk-mq
dispatches to the stopped queue, nvme_tcp_queue_request() arms io_work
again, and the send path walks the bio that was freed:
[ 74.380132] BUG: KASAN: slab-use-after-free in nvme_tcp_try_send_data+0xb12/0xd10
[ 74.380204] Read of size 8 at addr ffff888009432b70 by task kworker/0:1H/105
[ 74.380213]
[ 74.380222] CPU: 0 UID: 0 PID: 105 Comm: kworker/0:1H Not tainted 7.4.0-rc4-N1SORC-00743-gb6be37eaaa07 #9 PREEMPT(lazy)
[ 74.380233] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[ 74.380240] Workqueue: nvme_tcp_wq nvme_tcp_io_work
[ 74.380256] Call Trace:
[ 74.380261] <TASK>
[ 74.380266] dump_stack_lvl+0x53/0x70
[ 74.380305] print_report+0xd0/0x640
[ 74.380384] kasan_report+0xce/0x100
[ 74.380405] nvme_tcp_try_send_data+0xb12/0xd10
[ 74.380459] nvme_tcp_try_send+0x856/0x1140
[ 74.380491] nvme_tcp_io_work+0x125/0x720
[ 74.380520] process_one_work+0x6bb/0x1090
[ 74.380577] worker_thread+0x45b/0xd10
[ 74.380605] kthread+0x2c8/0x3b0
[ 74.380635] ret_from_fork+0x36e/0x5a0
[ 74.380683] ret_from_fork_asm+0x1a/0x30
[ 74.380695] </TASK>
[ 74.380698]
[ 74.380700] Allocated by task 135:
[ 74.380706] kasan_save_stack+0x33/0x60
[ 74.380715] kasan_save_track+0x14/0x40
[ 74.380724] __kasan_slab_alloc+0x6e/0x70
[ 74.380732] kmem_cache_alloc_noprof+0x10e/0x340
[ 74.380740] bio_alloc_bioset+0x214/0x1130
[ 74.380759] blkdev_direct_IO+0xd6c/0x1900
[ 74.380769] blkdev_write_iter+0x756/0xdc0
[ 74.380778] vfs_write+0x5d2/0xcd0
[ 74.380787] ksys_write+0xf7/0x1c0
[ 74.380796] do_syscall_64+0xdf/0x4c0
[ 74.380805] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 74.380812]
[ 74.380814] Freed by task 105:
[ 74.380818] kasan_save_stack+0x33/0x60
[ 74.380826] kasan_save_track+0x14/0x40
[ 74.380835] kasan_save_free_info+0x3b/0x60
[ 74.380842] __kasan_slab_free+0x57/0x80
[ 74.380850] kmem_cache_free+0x1b8/0x380
[ 74.380858] mempool_free+0xbd/0x110
[ 74.380871] bio_free+0x1ab/0x250
[ 74.380878] blk_update_request+0x229/0x10e0
[ 74.380886] blk_mq_end_request+0x48/0x70
[ 74.380894] nvme_tcp_timeout+0x7d3/0xc20
[ 74.380902] blk_mq_handle_expired+0x256/0x420
[ 74.380909] bt_iter+0x24b/0x340
[ 74.380918] blk_mq_queue_tag_busy_iter+0x587/0x12a0
[ 74.380928] blk_mq_timeout_work+0x39e/0x4e0
[ 74.380935] process_one_work+0x6bb/0x1090
[ 74.380943] worker_thread+0x45b/0xd10
[ 74.380952] kthread+0x2c8/0x3b0
[ 74.380958] ret_from_fork+0x36e/0x5a0
[ 74.380964] ret_from_fork_asm+0x1a/0x30
The queue still refers to the completed request after io_work fails,
so nvme_tcp_fail_request() ends it a second time:
[ 74.383642] Oops: general protection fault, probably for non-canonical address 0xdffffc000000001f: 0000 [#1] SMP KASAN NOPTI
[ 74.693982] KASAN: null-ptr-deref in range [0x00000000000000f8-0x00000000000000ff]
[ 74.703238] CPU: 0 UID: 0 PID: 105 Comm: kworker/0:1H Tainted: G B 7.4.0-rc4-N1SORC-00743-gb6be37eaaa07 #9 PREEMPT(lazy)
[ 74.705263] Tainted: [B]=BAD_PAGE
[ 74.705896] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[ 74.707920] Workqueue: nvme_tcp_wq nvme_tcp_io_work
[ 74.708785] RIP: 0010:blk_mq_complete_request_remote+0xce/0x7d0
[ 74.712738] RSP: 0018:ffffc90000697a78 EFLAGS: 00010203
[ 74.713688] RAX: dffffc0000000000 RBX: ffff888009226740 RCX: 1ffffffff0c15b94
[ 74.715088] RDX: 000000000000001f RSI: 1ffff11001244d08 RDI: 00000000000000fe
[ 74.716498] RBP: 1ffff920000d2f53 R08: 0000000004208060 R09: 1ffff110012064f6
[ 74.717769] R10: ffffc900006975a7 R11: 0000000000000028 R12: 0000000000000000
[ 74.718971] R13: 00000000ffffffe0 R14: ffff8880090e002c R15: 1ffff920000d2f6c
[ 74.720109] FS: 0000000000000000(0000) GS:ffff88811dff1000(0000) knlGS:0000000000000000
[ 74.721514] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 74.722599] CR2: 00000000059e5021 CR3: 0000000009354003 CR4: 0000000000770ef0
[ 74.723869] PKRU: 55555554
[ 74.724361] Call Trace:
[ 74.724801] <TASK>
[ 74.726775] nvme_tcp_try_send+0x618/0x1140
[ 74.728929] nvme_tcp_io_work+0x125/0x720
[ 74.731636] process_one_work+0x6bb/0x1090
[ 74.736361] worker_thread+0x45b/0xd10
[ 74.738504] kthread+0x2c8/0x3b0
[ 74.740811] ret_from_fork+0x36e/0x5a0
[ 74.743691] ret_from_fork_asm+0x1a/0x30
[ 74.744374] </TASK>
[ 74.744840] ---[ end trace 0000000000000000 ]---
The controller is a remote peer. A target that stops answering for
longer than the command timeout while the association is being
re-established is enough to reach the state above - a LIVE controller
dispatching to a stopped queue - with no help from the host; that much
was measured with a namespace whose completions were delayed past the
timeout. The queue is only left referring to the completed request if
the association also dies while that request is being sent, which had
to be forced to reproduce it, so the window is narrow. Both halves
are on the wire side of the host.
Drop the queue's references to the request before completing it. The
nvme_tcp_stop_queue() call right above has already shut the socket down
and cancel_work_sync()ed io_work under the queue lock, so the only
thing left that can be in the send path is a direct send from
.queue_rq; take send_mutex against that. send_mutex is initialized in
nvme_tcp_init_queue() and destroyed in nvme_tcp_exit_queue(), so it is
valid for as long as the controller is. Closing the reference closes
the second completion too, because that can only reach the request
through the same two references.
Fixes: 236187c4ed19 ("nvme-tcp: fix timeout handler")
Cc: stable@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---
drivers/nvme/host/tcp.c | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 62a594b8ab9bfa..ca3a63d260ed87 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -3031,12 +3031,46 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
nvme_tcp_queue_request(&ctrl->async_req, true);
}
+/*
+ * The timeout handler below completes the request itself, which ends it and
+ * frees its bio. The queue the request was being sent on can still hold a
+ * reference to it: as queue->request if a send was in progress, or linked on
+ * the queue's send_list or req_list if it was waiting its turn. Stopping the
+ * queue cancels io_work but drops none of those references, and nothing else
+ * drops them either - only the send path does, and that is what was just shut
+ * down. If the queue is handed work again the send path picks the request up
+ * and walks the bio this completion is about to free.
+ *
+ * Drop the queue's references to the request before completing it.
+ */
+static void nvme_tcp_drop_queue_ref(struct nvme_tcp_request *req)
+{
+ struct nvme_tcp_queue *queue = req->queue;
+
+ mutex_lock(&queue->send_mutex);
+
+ if (queue->request == req)
+ queue->request = NULL;
+
+ /*
+ * req_list is a lockless list, so a single entry cannot be removed
+ * from it; splice it onto send_list first, as the send path does.
+ */
+ nvme_tcp_process_req_list(queue);
+ if (!list_empty(&req->entry))
+ list_del_init(&req->entry);
+ init_llist_node(&req->lentry);
+
+ mutex_unlock(&queue->send_mutex);
+}
+
static void nvme_tcp_complete_timed_out(struct request *rq)
{
struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
+ nvme_tcp_drop_queue_ref(req);
nvmf_complete_timed_out_request(rq);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] nvme-tcp: drop the queue's reference to a timed-out request
2026-11-29 3:15 [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
@ 2026-11-29 3:46 ` sashiko-bot
2026-11-30 10:44 ` [PATCH v2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
2026-11-30 10:49 ` [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-29 3:46 UTC (permalink / raw)
To: Yehyeong Lee; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] nvme-tcp: residual recv state and genctr in
nvme_tcp_complete_timed_out()
- [High] nvme-tcp: race in nvme_tcp_complete_timed_out() before dropping
queue ref
--
commit 3e4a03d71fdd623bf2fb0e79370c509e4168fd7a
Author: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
nvme-tcp: drop the queue's reference to a timed-out request
This commit drops the queue's send-path references (queue->request,
send_list, and req_list) to a timed-out request in
nvme_tcp_complete_timed_out() before completing it.
> diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> index 62a594b8ab9bf..ca3a63d260ed8 100644
> --- a/drivers/nvme/host/tcp.c
> +++ b/drivers/nvme/host/tcp.c
> @@ -3031,12 +3031,46 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
> nvme_tcp_queue_request(&ctrl->async_req, true);
> }
>
> +/*
> + * The timeout handler below completes the request itself, which ends it and
> + * frees its bio. The queue the request was being sent on can still hold a
> + * reference to it: as queue->request if a send was in progress, or linked on
> + * the queue's send_list or req_list if it was waiting its turn. Stopping the
> + * queue cancels io_work but drops none of those references, and nothing else
> + * drops them either - only the send path does, and that is what was just shut
> + * down. If the queue is handed work again the send path picks the request up
> + * and walks the bio this completion is about to free.
> + *
> + * Drop the queue's references to the request before completing it.
> + */
> +static void nvme_tcp_drop_queue_ref(struct nvme_tcp_request *req)
> +{
> + struct nvme_tcp_queue *queue = req->queue;
> +
> + mutex_lock(&queue->send_mutex);
> +
> + if (queue->request == req)
> + queue->request = NULL;
> +
> + /*
> + * req_list is a lockless list, so a single entry cannot be removed
> + * from it; splice it onto send_list first, as the send path does.
> + */
> + nvme_tcp_process_req_list(queue);
> + if (!list_empty(&req->entry))
> + list_del_init(&req->entry);
> + init_llist_node(&req->lentry);
> +
> + mutex_unlock(&queue->send_mutex);
> +}
[Severity: High]
Can the receive path still reference the completed request when io_work
is re-armed on the stopped queue?
Neither __nvme_tcp_stop_queue() nor nvme_tcp_drop_queue_ref() clears
queue->rd_enabled or resets the receive context with
nvme_tcp_init_recv_ctx(), and kernel_sock_shutdown(queue->sock,
SHUT_RDWR) leaves already-queued packets in sk->sk_receive_queue. When a
new request is later dispatched to the stopped queue and re-arms io_work,
nvme_tcp_io_work() reaches nvme_tcp_try_recv():
- If the timed-out command was a read in NVME_TCP_RECV_DATA or
NVME_TCP_RECV_DDGST, nvme_tcp_recv_data() looks up rq directly via
nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id), copies
payload into req->iter / req->curr_bio (writing to the freed bio's
pages), and completes rq a second time on NVME_TCP_F_DATA_SUCCESS.
- If the queue is in NVME_TCP_RECV_PDU, nvmf_complete_timed_out_request()
does not increment nvme_req(rq)->genctr, so nvme_find_rq() still
matches a buffered nvme_tcp_rsp or nvme_tcp_r2t for rq's command_id.
nvme_tcp_process_nvme_cqe() then completes rq a second time, or
nvme_tcp_handle_r2t() sees the reinitialized req->lentry and
req->entry and queues the completed req back onto queue->req_list.
> +
> static void nvme_tcp_complete_timed_out(struct request *rq)
> {
> struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
> struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
>
> nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
> + nvme_tcp_drop_queue_ref(req);
> nvmf_complete_timed_out_request(rq);
> }
[Severity: High]
Can nvme_tcp_try_send() fail and requeue rq while nvme_tcp_stop_queue()
is shutting down the socket, allowing rq to be re-dispatched onto
queue->req_list before nvmf_complete_timed_out_request() completes it?
timeout work kworker
nvme_tcp_complete_timed_out(rq)
nvme_tcp_stop_queue()
__nvme_tcp_stop_queue()
kernel_sock_shutdown()
nvme_tcp_io_work()
nvme_tcp_try_send()
sock_sendmsg() -> -EPIPE
nvme_tcp_fail_request(req)
...
nvme_retry_req(rq)
blk_mq_requeue_request()
cancel_work_sync()
nvme_tcp_wait_queue()
nvme_tcp_drop_queue_ref(req)
blk_mq_requeue_work()
...
nvme_tcp_queue_rq(rq)
nvme_start_request(rq)
nvme_tcp_queue_request(req)
nvmf_complete_timed_out_request(rq)
blk_mq_complete_request(rq) <-- req still on queue->req_list
When sock_sendmsg() fails on the shut-down socket, nvme_tcp_fail_request()
completes rq with NVME_SC_HOST_PATH_ERROR. For retryable commands that
do not use multipath (such as fabrics auth commands during
NVME_CTRL_CONNECTING, or I/O when multipath is disabled),
__nvme_complete_rq() calls nvme_retry_req(), which resets rq->state to
MQ_RQ_IDLE and clears RQF_TIMED_OUT. Once nvme_tcp_queue_rq()
re-dispatches rq, rq->state is MQ_RQ_IN_FLIGHT again, so
nvmf_complete_timed_out_request() completes rq and frees its bio while
req is back on queue->req_list with io_work armed, causing the same
use-after-free in nvme_tcp_try_send_data() and double completion.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261129031537.1993449-1-yhlee@isslab.korea.ac.kr?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2] nvme-tcp: do not refer to a timed-out request after completing it
2026-11-29 3:15 [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
2026-11-29 3:46 ` sashiko-bot
@ 2026-11-30 10:44 ` Yehyeong Lee
2026-11-30 11:17 ` sashiko-bot
2026-11-30 10:49 ` [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
2 siblings, 1 reply; 5+ messages in thread
From: Yehyeong Lee @ 2026-11-30 10:44 UTC (permalink / raw)
To: hch, kbusch, sagi; +Cc: linux-nvme, linux-kernel
When a command times out while the controller is not LIVE, error
recovery is not an option: nvme_tcp_error_recovery() can only move the
controller to RESETTING from NEW or LIVE, so from CONNECTING it returns
without doing anything and no teardown runs. nvme_tcp_timeout()
therefore completes the command itself. nvme_tcp_complete_timed_out()
stops the one queue the command was on and ends the request, which
frees its bio.
Stopping the queue cancels io_work, but it does not drop the queue's
own references to that request. The queue refers to a request as
queue->request while it is being sent, and through its send_list or
req_list while the request waits its turn. All three are dropped only
by the send path itself, in nvme_tcp_fetch_request() and
nvme_tcp_done_send_req(), and stopping the queue is what shuts that
path down, so the references outlive the completion.
The same completion releases the reconnect's freeze wait, so the
controller is switched to LIVE while that queue is still stopped:
NVME_TCP_Q_LIVE is set only by nvme_tcp_start_queue(), which needs a
new connect. nvme_check_ready() lets a command through as soon as the
controller state is LIVE without looking at the queue, so blk-mq
dispatches to the stopped queue, nvme_tcp_queue_request() arms io_work
again, and the send path walks the bio that was freed:
[ 74.380132] BUG: KASAN: slab-use-after-free in nvme_tcp_try_send_data+0xb12/0xd10
[ 74.380204] Read of size 8 at addr ffff888009432b70 by task kworker/0:1H/105
[ 74.380213]
[ 74.380222] CPU: 0 UID: 0 PID: 105 Comm: kworker/0:1H Not tainted 7.4.0-rc4-N1SORC-00743-gb6be37eaaa07 #9 PREEMPT(lazy)
[ 74.380233] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[ 74.380240] Workqueue: nvme_tcp_wq nvme_tcp_io_work
[ 74.380256] Call Trace:
[ 74.380261] <TASK>
[ 74.380266] dump_stack_lvl+0x53/0x70
[ 74.380305] print_report+0xd0/0x640
[ 74.380384] kasan_report+0xce/0x100
[ 74.380405] nvme_tcp_try_send_data+0xb12/0xd10
[ 74.380459] nvme_tcp_try_send+0x856/0x1140
[ 74.380491] nvme_tcp_io_work+0x125/0x720
[ 74.380520] process_one_work+0x6bb/0x1090
[ 74.380577] worker_thread+0x45b/0xd10
[ 74.380605] kthread+0x2c8/0x3b0
[ 74.380635] ret_from_fork+0x36e/0x5a0
[ 74.380683] ret_from_fork_asm+0x1a/0x30
[ 74.380695] </TASK>
[ 74.380698]
[ 74.380700] Allocated by task 135:
[ 74.380706] kasan_save_stack+0x33/0x60
[ 74.380715] kasan_save_track+0x14/0x40
[ 74.380724] __kasan_slab_alloc+0x6e/0x70
[ 74.380732] kmem_cache_alloc_noprof+0x10e/0x340
[ 74.380740] bio_alloc_bioset+0x214/0x1130
[ 74.380759] blkdev_direct_IO+0xd6c/0x1900
[ 74.380769] blkdev_write_iter+0x756/0xdc0
[ 74.380778] vfs_write+0x5d2/0xcd0
[ 74.380787] ksys_write+0xf7/0x1c0
[ 74.380796] do_syscall_64+0xdf/0x4c0
[ 74.380805] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 74.380812]
[ 74.380814] Freed by task 105:
[ 74.380818] kasan_save_stack+0x33/0x60
[ 74.380826] kasan_save_track+0x14/0x40
[ 74.380835] kasan_save_free_info+0x3b/0x60
[ 74.380842] __kasan_slab_free+0x57/0x80
[ 74.380850] kmem_cache_free+0x1b8/0x380
[ 74.380858] mempool_free+0xbd/0x110
[ 74.380871] bio_free+0x1ab/0x250
[ 74.380878] blk_update_request+0x229/0x10e0
[ 74.380886] blk_mq_end_request+0x48/0x70
[ 74.380894] nvme_tcp_timeout+0x7d3/0xc20
[ 74.380902] blk_mq_handle_expired+0x256/0x420
[ 74.380909] bt_iter+0x24b/0x340
[ 74.380918] blk_mq_queue_tag_busy_iter+0x587/0x12a0
[ 74.380928] blk_mq_timeout_work+0x39e/0x4e0
[ 74.380935] process_one_work+0x6bb/0x1090
[ 74.380943] worker_thread+0x45b/0xd10
[ 74.380952] kthread+0x2c8/0x3b0
[ 74.380958] ret_from_fork+0x36e/0x5a0
[ 74.380964] ret_from_fork_asm+0x1a/0x30
The queue still refers to the completed request after io_work fails,
so nvme_tcp_fail_request() ends it a second time:
[ 74.383642] Oops: general protection fault, probably for non-canonical address 0xdffffc000000001f: 0000 [#1] SMP KASAN NOPTI
[ 74.693982] KASAN: null-ptr-deref in range [0x00000000000000f8-0x00000000000000ff]
[ 74.703238] CPU: 0 UID: 0 PID: 105 Comm: kworker/0:1H Tainted: G B 7.4.0-rc4-N1SORC-00743-gb6be37eaaa07 #9 PREEMPT(lazy)
[ 74.705263] Tainted: [B]=BAD_PAGE
[ 74.705896] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[ 74.707920] Workqueue: nvme_tcp_wq nvme_tcp_io_work
[ 74.708785] RIP: 0010:blk_mq_complete_request_remote+0xce/0x7d0
[ 74.712738] RSP: 0018:ffffc90000697a78 EFLAGS: 00010203
[ 74.713688] RAX: dffffc0000000000 RBX: ffff888009226740 RCX: 1ffffffff0c15b94
[ 74.715088] RDX: 000000000000001f RSI: 1ffff11001244d08 RDI: 00000000000000fe
[ 74.716498] RBP: 1ffff920000d2f53 R08: 0000000004208060 R09: 1ffff110012064f6
[ 74.717769] R10: ffffc900006975a7 R11: 0000000000000028 R12: 0000000000000000
[ 74.718971] R13: 00000000ffffffe0 R14: ffff8880090e002c R15: 1ffff920000d2f6c
[ 74.720109] FS: 0000000000000000(0000) GS:ffff88811dff1000(0000) knlGS:0000000000000000
[ 74.721514] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 74.722599] CR2: 00000000059e5021 CR3: 0000000009354003 CR4: 0000000000770ef0
[ 74.723869] PKRU: 55555554
[ 74.724361] Call Trace:
[ 74.724801] <TASK>
[ 74.726775] nvme_tcp_try_send+0x618/0x1140
[ 74.728929] nvme_tcp_io_work+0x125/0x720
[ 74.731636] process_one_work+0x6bb/0x1090
[ 74.736361] worker_thread+0x45b/0xd10
[ 74.738504] kthread+0x2c8/0x3b0
[ 74.740811] ret_from_fork+0x36e/0x5a0
[ 74.743691] ret_from_fork_asm+0x1a/0x30
[ 74.744374] </TASK>
[ 74.744840] ---[ end trace 0000000000000000 ]---
The controller is a remote peer. A target that stops answering for
longer than the command timeout while the association is being
re-established is enough to reach the state above - a LIVE controller
dispatching to a stopped queue - with no help from the host; that much
was measured with a namespace whose completions were delayed past the
timeout. The queue is only left referring to the completed request if
the association also dies while that request is being sent, which had
to be forced to reproduce it, so the window is narrow. Both halves
are on the wire side of the host.
The queue's send references are not the only way back to the request.
nvmf_complete_timed_out_request() ends it with
blk_mq_complete_request(), which does not go through
nvme_try_complete_req() - the only place the host advances
nvme_req(rq)->genctr - so the command id of the completed request is
still live in the tagset and still resolves. nvme_find_rq() accepts
it on the generation alone and nvme_cid_to_rq() checks nothing at all,
and the receive side is still open: SHUT_RD does not discard what the
socket has already received, the read_sock() path does not look at the
shutdown state, and nvme_tcp_io_work() calls nvme_tcp_try_recv()
without testing whether the queue is live, unlike nvme_tcp_poll(). The
dispatch above arms can therefore hand bytes that arrived before the
stop to nvme_tcp_recv_skb(), which resolves them back onto the request
that was just completed - a second completion from
nvme_tcp_process_nvme_cqe(), or a write into the freed bio from
nvme_tcp_recv_data(). That route has not been reproduced as a crash:
holding bytes unread past the stop also makes the reconnect's freeze
wait expire, so the controller never reaches LIVE and nothing is
dispatched to the stopped queue. That the command id still resolves to
the completed request was measured directly, through both lookups.
Drop the queue's send references to the request before completing it.
The nvme_tcp_stop_queue() call right above has already shut the socket
down and cancel_work_sync()ed io_work under the queue lock, so the only
thing left that can be in the send path is a direct send from
.queue_rq; take send_mutex against that. send_mutex is initialized in
nvme_tcp_init_queue() and destroyed in nvme_tcp_exit_queue(), so it is
valid for as long as the controller is.
And stop receiving on a queue that is being stopped, by clearing
rd_enabled in nvme_tcp_stop_queue_nowait() - the flag
nvme_tcp_recv_skb() already tests - so that no command id is resolved
on a queue the host has given up on. It is cleared whether or not the
queue ever reached NVME_TCP_Q_LIVE, because a Connect command can time
out on a queue that never got there; __nvme_tcp_stop_queue() is then
not called at all, and that queue's socket is the one holding the
response. nvme_tcp_start_queue() sets the flag again, next to
nvme_tcp_init_recv_ctx(), on the next connect, so the two are paired
and a live queue is untouched.
Fixes: 236187c4ed19 ("nvme-tcp: fix timeout handler")
Cc: stable@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---
Changes since v1:
- v1 claimed the second completion could only reach the request
through the same two send references. That is wrong, and the
sentence is gone: nvmf_complete_timed_out_request() does not
advance genctr, so the command id is still live in the tagset and
the receive side still resolves it onto the completed request. The
changelog now describes that route, and says plainly that it was
confirmed by reaching the state and not by a crash.
- Also stop receiving on a queue that is being stopped, by clearing
rd_enabled in nvme_tcp_stop_queue_nowait(). Gating io_work's
receive on NVME_TCP_Q_LIVE, the way nvme_tcp_poll() does, was tried
first and is wrong: nvme_tcp_start_queue() sets that bit only after
nvmf_connect_{admin,io}_queue() returns, so the Connect response is
dropped and no controller connects at all.
- The send-reference drop itself is unchanged from v1, including the
blocking send_mutex acquisition in the timeout path.
drivers/nvme/host/tcp.c | 49 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 62a594b8ab9bfa..e0a2fa1d8d3553 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -2303,6 +2303,21 @@ static void nvme_tcp_stop_queue_nowait(struct nvme_ctrl *nctrl, int qid)
}
mutex_lock(&queue->queue_lock);
+
+ /*
+ * Stop receiving, whether or not this queue got as far as LIVE.
+ * SHUT_RD below does not discard what the socket has already
+ * received, and the receive path nvme-tcp uses - read_sock() -
+ * does not look at the shutdown state, so residual bytes stay
+ * readable; rd_enabled is what keeps nvme_tcp_recv_skb() from
+ * consuming them. Clearing it here, before anything else, means
+ * neither an io_work that is running now nor one armed after the
+ * cancel_work_sync() below can resolve a command id on a queue
+ * that is being stopped. nvme_tcp_start_queue() sets it again,
+ * next to the receive context, on the next connect.
+ */
+ queue->rd_enabled = false;
+
if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
__nvme_tcp_stop_queue(queue);
/* Stopping the queue will disable TLS */
@@ -3031,12 +3046,46 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
nvme_tcp_queue_request(&ctrl->async_req, true);
}
+/*
+ * The timeout handler below completes the request itself, which ends it and
+ * frees its bio. The queue the request was being sent on can still hold a
+ * reference to it: as queue->request if a send was in progress, or linked on
+ * the queue's send_list or req_list if it was waiting its turn. Stopping the
+ * queue cancels io_work but drops none of those references, and nothing else
+ * drops them either - only the send path does, and that is what was just shut
+ * down. If the queue is handed work again the send path picks the request up
+ * and walks the bio this completion is about to free.
+ *
+ * Drop the queue's references to the request before completing it.
+ */
+static void nvme_tcp_drop_queue_ref(struct nvme_tcp_request *req)
+{
+ struct nvme_tcp_queue *queue = req->queue;
+
+ mutex_lock(&queue->send_mutex);
+
+ if (queue->request == req)
+ queue->request = NULL;
+
+ /*
+ * req_list is a lockless list, so a single entry cannot be removed
+ * from it; splice it onto send_list first, as the send path does.
+ */
+ nvme_tcp_process_req_list(queue);
+ if (!list_empty(&req->entry))
+ list_del_init(&req->entry);
+ init_llist_node(&req->lentry);
+
+ mutex_unlock(&queue->send_mutex);
+}
+
static void nvme_tcp_complete_timed_out(struct request *rq)
{
struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
+ nvme_tcp_drop_queue_ref(req);
nvmf_complete_timed_out_request(rq);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] nvme-tcp: drop the queue's reference to a timed-out request
2026-11-29 3:15 [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
2026-11-29 3:46 ` sashiko-bot
2026-11-30 10:44 ` [PATCH v2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
@ 2026-11-30 10:49 ` Yehyeong Lee
2 siblings, 0 replies; 5+ messages in thread
From: Yehyeong Lee @ 2026-11-30 10:49 UTC (permalink / raw)
To: hch, kbusch, sagi; +Cc: linux-nvme, linux-kernel
On Sun, 29 Nov 2026 12:15:37 +0900, I wrote:
> Closing the reference closes the second completion too, because that
> can only reach the request through the same two references.
That sentence is wrong, and I should not have written it. Going back
over the paths that still lead to the completed request turned up a
third one. nvmf_complete_timed_out_request() ends the request with
blk_mq_complete_request(), which never passes through
nvme_try_complete_req() - the only place the host advances
nvme_req(rq)->genctr. The command id is therefore still live in the
tagset: nvme_find_rq() accepts it on the generation alone and
nvme_cid_to_rq() checks nothing at all. The receive side of the
stopped queue is still open too, because SHUT_RD does not discard what
the socket has already received and the read_sock() path does not look
at the shutdown state, so a residual PDU can be resolved back onto the
request that was just completed.
I have not turned that into a crash - holding bytes unread past the
stop also makes the reconnect's freeze wait expire, so the controller
never reaches LIVE and nothing is dispatched to the stopped queue - but
that the command id still resolves to the completed request is
straightforward to observe through both lookups.
v2 drops the false sentence, describes that route, and adds the second
half of the fix: clearing rd_enabled in nvme_tcp_stop_queue_nowait(),
so a queue the host has given up on resolves no further command ids.
Please look at that one instead of this patch.
Superseded by v2: https://lore.kernel.org/linux-nvme/20261130104426.2140231-1-yhlee@isslab.korea.ac.kr/
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] nvme-tcp: do not refer to a timed-out request after completing it
2026-11-30 10:44 ` [PATCH v2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
@ 2026-11-30 11:17 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-30 11:17 UTC (permalink / raw)
To: Yehyeong Lee; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] nvme-tcp: missing queue stop in nvme_tcp_stop_queue_nowait()
before LIVE
- [High] nvme-tcp: race in nvme_tcp_complete_timed_out() before dropping
queue ref
--
commit 525e6d2baaccfcee477c6959e45a7456d4788b99
Author: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
nvme-tcp: do not refer to a timed-out request after completing it
This commit clears queue->rd_enabled when stopping a queue and drops the
queue's send references to a timed-out request in
nvme_tcp_complete_timed_out() before completing it.
> diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> index 62a594b8ab9bf..e0a2fa1d8d355 100644
> --- a/drivers/nvme/host/tcp.c
> +++ b/drivers/nvme/host/tcp.c
> @@ -2303,6 +2303,21 @@ static void nvme_tcp_stop_queue_nowait(struct nvme_ctrl *nctrl, int qid)
> }
>
> mutex_lock(&queue->queue_lock);
> +
> + /*
> + * Stop receiving, whether or not this queue got as far as LIVE.
> + * SHUT_RD below does not discard what the socket has already
> + * received, and the receive path nvme-tcp uses - read_sock() -
> + * does not look at the shutdown state, so residual bytes stay
> + * readable; rd_enabled is what keeps nvme_tcp_recv_skb() from
> + * consuming them. Clearing it here, before anything else, means
> + * neither an io_work that is running now nor one armed after the
> + * cancel_work_sync() below can resolve a command id on a queue
> + * that is being stopped. nvme_tcp_start_queue() sets it again,
> + * next to the receive context, on the next connect.
> + */
> + queue->rd_enabled = false;
> +
> if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
> __nvme_tcp_stop_queue(queue);
> /* Stopping the queue will disable TLS */
[Severity: High]
Does clearing queue->rd_enabled without calling __nvme_tcp_stop_queue()
leave io_work running and queue->sock open when a command times out before
NVME_TCP_Q_LIVE is set?
nvme_tcp_start_queue() enables rd_enabled, installs socket callbacks, and
runs nvmf_connect_admin_queue() or nvmf_connect_io_queue() (including
in-band authentication) before setting NVME_TCP_Q_LIVE. If a Connect or
auth command times out there, test_and_clear_bit(NVME_TCP_Q_LIVE, ...) is
false, so __nvme_tcp_stop_queue() is skipped until connect returns.
Because nvme_tcp_recv_skb() checks queue->rd_enabled only once at entry
before its while (len) loop, an already running receive or zero-copy send
in io_work is not fenced by cancel_work_sync():
CPU0 (timeout) CPU1 (io_work)
nvme_tcp_Start_queue()
nvmf_connect_admin_queue()
...
nvme_tcp_complete_timed_out(rq)
nvme_tcp_recv_skb()
if (!queue->rd_enabled)
while (len) {
nvme_tcp_stop_queue()
queue->rd_enabled = false;
/* NVME_TCP_Q_LIVE not set,
* __nvme_tcp_stop_queue()
* skipped
*/
nvme_tcp_drop_queue_ref(req)
nvmf_complete_timed_out_request(rq)
/* rq completed, buffer freed */
nvme_tcp_recv_pdu() /
nvme_tcp_recv_data()
<-- double completion or
use-after-free
In addition, nvme_auth_submit() submits auth commands with
NVME_SUBMIT_RETRY, so completing a timed-out auth request with
NVME_SC_HOST_ABORTED_CMD retries it onto the still-open socket after
nvme_tcp_stop_queue_nowait() has cleared queue->rd_enabled and
queue->tls_enabled. That bypasses the !nvme_tcp_queue_tls(queue) guard in
nvme_tcp_queue_request() (risking inline kTLS send under blk-mq set->srcu)
and drops the reply because rd_enabled is false, hanging each retry for
the full command timeout.
> @@ -3031,12 +3046,46 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
> nvme_tcp_queue_request(&ctrl->async_req, true);
> }
>
> +/*
> + * The timeout handler below completes the request itself, which ends it and
> + * frees its bio. The queue the request was being sent on can still hold a
> + * reference to it: as queue->request if a send was in progress, or linked on
> + * the queue's send_list or req_list if it was waiting its turn. Stopping the
> + * queue cancels io_work but drops none of those references, and nothing else
> + * drops them either - only the send path does, and that is what was just shut
> + * down. If the queue is handed work again the send path picks the request up
> + * and walks the bio this completion is about to free.
> + *
> + * Drop the queue's references to the request before completing it.
> + */
> +static void nvme_tcp_drop_queue_ref(struct nvme_tcp_request *req)
> +{
> + struct nvme_tcp_queue *queue = req->queue;
> +
> + mutex_lock(&queue->send_mutex);
> +
> + if (queue->request == req)
> + queue->request = NULL;
> +
> + /*
> + * req_list is a lockless list, so a single entry cannot be removed
> + * from it; splice it onto send_list first, as the send path does.
> + */
> + nvme_tcp_process_req_list(queue);
> + if (!list_empty(&req->entry))
> + list_del_init(&req->entry);
> + init_llist_node(&req->lentry);
> +
> + mutex_unlock(&queue->send_mutex);
> +}
> +
> static void nvme_tcp_complete_timed_out(struct request *rq)
> {
> struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
> struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
>
> nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
> + nvme_tcp_drop_queue_ref(req);
> nvmf_complete_timed_out_request(rq);
> }
[Severity: High]
Can nvme_tcp_try_send() fail and complete req between
kernel_sock_shutdown() in nvme_tcp_stop_queue() and
nvme_tcp_drop_queue_ref(req)?
__nvme_tcp_stop_queue() calls kernel_sock_shutdown(queue->sock, SHUT_RDWR)
before cancel_work_sync(&queue->io_work) and without holding
queue->send_mutex, and nvme_tcp_wait_queue() then sleeps for up to 100ms
before nvme_tcp_drop_queue_ref() is reached. If io_work or a direct send
from nvme_tcp_queue_request() runs during that window while req is still on
the queue, sock_sendmsg() returns -EPIPE and nvme_tcp_fail_request()
completes rq with NVME_SC_HOST_PATH_ERROR:
CPU0 (timeout) CPU1 (io_work / direct send)
nvme_tcp_complete_timed_out(rq)
nvme_tcp_stop_queue()
kernel_sock_shutdown()
nvme_tcp_try_send()
sock_sendmsg() -> -EPIPE
nvme_tcp_fail_request(req)
nvme_try_complete_req(rq)
cancel_work_sync()
nvme_tcp_wait_queue()
nvme_tcp_drop_queue_ref(req)
/* softirq or sync waiter frees
* or requeues rq -> MQ_RQ_IDLE
*/
nvmf_complete_timed_out_request(rq)
blk_mq_request_started(rq)
/* sees MQ_RQ_COMPLETE */
!blk_mq_request_completed(rq)
/* sees MQ_RQ_IDLE */
blk_mq_complete_request(rq) <-- double completion
If the request is retryable, nvme_complete_rq() can also requeue and
re-dispatch rq during nvme_tcp_wait_queue() (setting rq->state back to
MQ_RQ_IN_FLIGHT), causing nvmf_complete_timed_out_request(rq) to abort the
newly restarted request. Dropping the queue reference under send_mutex
before shutting down the socket, or preventing nvme_tcp_try_send() from
failing requests once the queue is stopping, would close this window.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130104426.2140231-1-yhlee@isslab.korea.ac.kr?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-30 11:17 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 3:15 [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
2026-11-29 3:46 ` sashiko-bot
2026-11-30 10:44 ` [PATCH v2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
2026-11-30 11:17 ` sashiko-bot
2026-11-30 10:49 ` [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox