[PATCH v7] nvme-tcp: do not refer to a timed-out request after completing it

From: Yehyeong Lee

Date: Sat Dec 05 2026 - 18:05:14 EST


nvme_tcp_error_recovery() reaches RESETTING only from NEW or LIVE, so a
command that times out while the controller is CONNECTING is completed in
place: nvme_tcp_complete_timed_out() ends the request and frees its bio
but leaves the queue referring to it. The send references outlive the
completion, a C2HData header leaves the receive context aimed at the
request, and nvme_find_rq() still resolves its command id, as nothing
here advances the generation. The send references alone suffice - the
completion unblocks the reconnect, the controller goes LIVE with this
queue stopped, and the send path walks the freed bio:

BUG: KASAN: slab-use-after-free in nvme_tcp_try_send_data+0xb12/0xd10
Read of size 8 at addr ffff888009432b70 by task kworker/0:1H/105

Workqueue: nvme_tcp_wq nvme_tcp_io_work
Call Trace:
<TASK>
dump_stack_lvl+0x53/0x70
print_report+0xd0/0x640
kasan_report+0xce/0x100
nvme_tcp_try_send_data+0xb12/0xd10
nvme_tcp_try_send+0x856/0x1140
nvme_tcp_io_work+0x125/0x720
process_one_work+0x6bb/0x1090
worker_thread+0x45b/0xd10
kthread+0x2c8/0x3b0
ret_from_fork+0x36e/0x5a0
ret_from_fork_asm+0x1a/0x30
</TASK>

Allocated by task 135:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x40
__kasan_slab_alloc+0x6e/0x70
kmem_cache_alloc_noprof+0x10e/0x340
bio_alloc_bioset+0x214/0x1130
blkdev_direct_IO+0xd6c/0x1900
blkdev_write_iter+0x756/0xdc0
vfs_write+0x5d2/0xcd0
ksys_write+0xf7/0x1c0
do_syscall_64+0xdf/0x4c0
entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 105:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x40
kasan_save_free_info+0x3b/0x60
__kasan_slab_free+0x57/0x80
kmem_cache_free+0x1b8/0x380
mempool_free+0xbd/0x110
bio_free+0x1ab/0x250
blk_update_request+0x229/0x10e0
blk_mq_end_request+0x48/0x70
nvme_tcp_timeout+0x7d3/0xc20
blk_mq_handle_expired+0x256/0x420
bt_iter+0x24b/0x340
blk_mq_queue_tag_busy_iter+0x587/0x12a0
blk_mq_timeout_work+0x39e/0x4e0
process_one_work+0x6bb/0x1090
worker_thread+0x45b/0xd10
kthread+0x2c8/0x3b0
ret_from_fork+0x36e/0x5a0
ret_from_fork_asm+0x1a/0x30

Stopping the queue does not help: nvme_tcp_stop_queue_nowait() reaches
__nvme_tcp_stop_queue() only inside test_and_clear_bit(NVME_TCP_Q_LIVE),
so for a queue that never went live - the one carrying a connect or an
in-band authentication command - nothing is shut down, restored or
cancelled. Retire the command id, drop the send references and the
receive context and complete the request under queue_lock, send_mutex and
the socket lock, the order nvme_tcp_try_send() uses, touching the socket,
the receive buffer and the send lists only while NVME_TCP_Q_ALLOCATED is
set, and requeue io_work afterwards as the send path expects of whoever
drops send_mutex. A retry is a new dispatch through nvme_setup_cmd(),
which recomputes the id.

nvme-rdma has the same shape - commit 0475a8dcbcee92 ("nvme-rdma: fix
timeout handler") is this handler's twin - and will be addressed in a
separate, rdma-tested patch.

Fixes: 236187c4ed19 ("nvme-tcp: fix timeout handler")
Fixes: e4fdb2b167ed ("nvme: increment request genctr on completion")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Yehyeong Lee <yhlee@xxxxxxxxxxxxxxxxxx>
---
Link: https://lore.kernel.org/linux-nvme/20261205114759.3239590-1-yhlee@xxxxxxxxxxxxxxxxxx/ [v6]

Changes since v6:

- The socket, the receive buffer and the send lists are now touched only
while NVME_TCP_Q_ALLOCATED is set, read under queue_lock, so the handler
cannot overlap nvme_tcp_free_queue() or nvme_tcp_alloc_queue() on the same
queue. nvme_tcp_alloc_queue() swaps all three without that lock and sets
the bit last; nvme_tcp_free_queue() clears it before taking the lock. A
non-NULL queue->sock is not enough: during a reallocation the new socket is
installed while the old receive buffer is still the freed one.
- Dropped the init_llist_node() the drop path had added. It is redundant:
nvme_tcp_fetch_request() reinitialises the node every time it picks the
request up, and nvme_tcp_init_request() does so once per tag.
- io_work is requeued when the queue still has work after the drop, which is
what the send path expects of whoever releases send_mutex.
- Corrected the stop-queue description in the commit message; the incorrect
nvme_tcp_poll() sentence is gone.

Earlier revisions:

v1: https://lore.kernel.org/linux-nvme/20261129031537.1993449-1-yhlee@xxxxxxxxxxxxxxxxxx/
v2: https://lore.kernel.org/linux-nvme/20261130104426.2140231-1-yhlee@xxxxxxxxxxxxxxxxxx/
v3: https://lore.kernel.org/linux-nvme/20261201113916.2322397-1-yhlee@xxxxxxxxxxxxxxxxxx/
v4: https://lore.kernel.org/linux-nvme/20261202131909.2556796-1-yhlee@xxxxxxxxxxxxxxxxxx/
v5: https://lore.kernel.org/linux-nvme/20261204135345.2994739-1-yhlee@xxxxxxxxxxxxxxxxxx/
v6: https://lore.kernel.org/linux-nvme/20261205114759.3239590-1-yhlee@xxxxxxxxxxxxxxxxxx/

Earlier completion-side proposals in this area:

[1] https://lore.kernel.org/linux-nvme/20261103-nvme-double-cmpl-uaf-blk-mq-v1-1-b10fea474423@xxxxxxxxxx/
[2] https://lore.kernel.org/linux-nvme/20260913124053.86565-1-mateusz.nowicki@xxxxxxxxxx/

drivers/nvme/host/tcp.c | 72 +++++++++++++++++++++++++++++++++++++++--
1 file changed, 70 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 62a594b8ab9bfa..293e63cd0a17be 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -3031,13 +3031,81 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
nvme_tcp_queue_request(&ctrl->async_req, true);
}

+static void nvme_tcp_drop_queue_ref(struct nvme_tcp_request *req)
+{
+ struct nvme_tcp_queue *queue = req->queue;
+
+ lockdep_assert_held(&queue->send_mutex);
+
+ if (queue->request == req)
+ queue->request = NULL;
+
+ /* a single entry cannot be unlinked from a lockless list */
+ nvme_tcp_process_req_list(queue);
+ if (!list_empty(&req->entry))
+ list_del_init(&req->entry);
+}
+
+static void nvme_tcp_drop_recv_ctx(struct nvme_tcp_request *req)
+{
+ struct nvme_tcp_queue *queue = req->queue;
+ struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
+
+ if (nvme_tcp_recv_state(queue) == NVME_TCP_RECV_PDU)
+ return;
+
+ if (nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id) !=
+ blk_mq_rq_from_pdu(req))
+ return;
+
+ nvme_tcp_init_recv_ctx(queue);
+}
+
+static void nvme_tcp_retire_command_id(struct request *rq)
+{
+ struct nvme_request *nr = nvme_req(rq);
+
+ if (!(nr->ctrl->quirks & NVME_QUIRK_SKIP_CID_GEN))
+ nr->genctr++;
+}
+
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;
+ struct nvme_tcp_queue *queue = req->queue;
+ struct nvme_ctrl *ctrl = &queue->ctrl->ctrl;
+ struct sock *sk = NULL;

- nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
+ nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(queue));
+
+ mutex_lock(&queue->queue_lock);
+ mutex_lock(&queue->send_mutex);
+ /*
+ * nvme_tcp_alloc_queue() installs the socket and the buffers without
+ * this lock and sets the bit last; nvme_tcp_free_queue() clears it first.
+ */
+ if (test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) {
+ sk = queue->sock->sk;
+ lock_sock(sk);
+ }
+
+ if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+ nvme_tcp_retire_command_id(rq);
+ if (sk) {
+ nvme_tcp_drop_queue_ref(req);
+ nvme_tcp_drop_recv_ctx(req);
+ }
nvmf_complete_timed_out_request(rq);
+
+ if (sk) {
+ release_sock(sk);
+ /* the send path leaves requeueing to whoever drops send_mutex */
+ if (nvme_tcp_queue_has_pending(queue))
+ queue_work_on(queue->io_cpu, nvme_tcp_wq,
+ &queue->io_work);
+ }
+ mutex_unlock(&queue->send_mutex);
+ mutex_unlock(&queue->queue_lock);
}

static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq)
--
2.43.0