[PATCH v4 1/2] nvme-tcp: drop the queue's reference to a timed-out request

Yehyeong Lee yhlee at isslab.korea.ac.kr
Wed Dec 2 05:19:08 PST 2026


nvme_tcp_error_recovery() can only move the controller to RESETTING from
NEW or LIVE, so a command that times out while the controller is
CONNECTING is completed in place instead: 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 drops none of the queue's own
references to that request.  The queue refers to it as queue->request
while it is being sent, and through send_list or req_list while it waits
its turn; all three are dropped only by the send path, in
nvme_tcp_fetch_request() and nvme_tcp_done_send_req(), and stopping the
queue is what shuts that path down.

The same completion releases the reconnect's freeze wait, so the
controller goes 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() looks at the controller state and not at the queue, so
blk-mq dispatches to the stopped queue, nvme_tcp_queue_request() arms
io_work, and the send path walks the freed bio:

  [   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 reaches the state above - a LIVE controller dispatching to
a stopped queue - with no help from the host.  The queue is only left
referring to the completed request if the association also dies while
that request is being sent, so the window is narrow.

Drop the queue's references before completing the request.  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 in the send path is a direct send from .queue_rq; take send_mutex
against that.  send_mutex is valid for as long as the controller is.
This closes the second completion too, which can only reach the request
through the same references.

Fixes: 236187c4ed19 ("nvme-tcp: fix timeout handler")
Cc: stable at vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee at isslab.korea.ac.kr>
---
 drivers/nvme/host/tcp.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 62a594b8ab9bfa..5320375d6f691f 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -3031,12 +3031,36 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
 	nvme_tcp_queue_request(&ctrl->async_req, true);
 }
 
+/*
+ * Completing the request below frees its bio, but the queue can still refer to
+ * it - as queue->request, or linked on send_list or req_list - and only the
+ * send path drops those, which stopping the queue has just shut down.
+ */
+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);
+}
+
 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




More information about the Linux-nvme mailing list