* [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
0 siblings, 1 reply; 2+ 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] 2+ 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
0 siblings, 0 replies; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2026-11-29 3:46 UTC | newest]
Thread overview: 2+ 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox