[PATCH v5 2/2] nvme-tcp: do not leave a timed-out request reachable from its queue
From: Yehyeong Lee
Date: Fri Dec 04 2026 - 08:54:17 EST
nvme_tcp_error_recovery() can only move the controller to RESETTING from
NEW or LIVE, so a command that times out while it is CONNECTING is
completed in place: nvme_tcp_complete_timed_out() stops the queue and ends
the request, which frees its bio. That does not take the request away
from the queue, and for a queue that never reached NVME_TCP_Q_LIVE - the
one carrying a connect or an in-band authentication command -
nvme_tcp_stop_queue_nowait() neither shuts the socket down nor cancels
io_work. The send references, queue->request and the send_list and
req_list links, are dropped only by the send path; io_work may already
have resolved the command id; and a C2HData header leaves the receive
context aimed at this request, where nvme_tcp_recv_data() resolves it with
nvme_cid_to_rq() and no generation check.
The send references are enough on their own. The completion releases the
reconnect's freeze wait and the controller goes LIVE while this queue is
still stopped, so it is dispatched to again 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
So retire the request first: the command id, while the socket is still up;
then cancel_work_sync(), which lets a resolve already in flight finish;
then the send references under send_mutex; then the receive context and
the completion under the socket lock, which the receive path holds across
read_sock(). Retiring the id last does not work - the lock serialises the
receive path but does not order it, so an io_work armed after the cancel
can take it first and resolve with the generation still unchanged.
Fixes: 236187c4ed19 ("nvme-tcp: fix timeout handler")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Yehyeong Lee <yhlee@xxxxxxxxxxxxxxxxxx>
---
drivers/nvme/host/tcp.c | 46 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 62a594b8ab9bfa..b89d763372b5c2 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -3031,13 +3031,59 @@ 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;
+
+ mutex_lock(&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);
+ init_llist_node(&req->lentry);
+
+ mutex_unlock(&queue->send_mutex);
+}
+
+/* the data and digest paths resolve this id without a generation check */
+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_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 sock *sk = req->queue->sock->sk;
+
+ if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
+ nvmf_retire_timed_out_command(rq);
nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
+
+ cancel_work_sync(&req->queue->io_work);
+ nvme_tcp_drop_queue_ref(req);
+
+ /* the receive path holds this across read_sock() */
+ lock_sock(sk);
+ nvme_tcp_drop_recv_ctx(req);
nvmf_complete_timed_out_request(rq);
+ release_sock(sk);
}
static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq)
--
2.43.0