mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] nvme-tcp: drop the queue's reference to a timed-out request
@ 2026-11-29  3:15 Yehyeong Lee
  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
  0 siblings, 2 replies; 6+ 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	[flat|nested] 6+ 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-30 10:44 ` Yehyeong Lee
  2026-12-01 11:39   ` [PATCH v3] " Yehyeong Lee
  2026-11-30 10:49 ` [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
  1 sibling, 1 reply; 6+ 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	[flat|nested] 6+ 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-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
  2026-12-01  8:03   ` Christoph Hellwig
  1 sibling, 1 reply; 6+ 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] 6+ messages in thread

* Re: [PATCH] nvme-tcp: drop the queue's reference to a timed-out request
  2026-11-30 10:49 ` [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
@ 2026-12-01  8:03   ` Christoph Hellwig
  2026-12-01 12:14     ` Yehyeong Lee
  0 siblings, 1 reply; 6+ messages in thread
From: Christoph Hellwig @ 2026-12-01  8:03 UTC (permalink / raw)
  To: Yehyeong Lee; +Cc: hch, kbusch, sagi, linux-nvme, linux-kernel

On Mon, Nov 30, 2026 at 07:49:01PM +0900, Yehyeong Lee wrote:
> Superseded by v2: https://lore.kernel.org/linux-nvme/20261130104426.2140231-1-yhlee@isslab.korea.ac.kr/

Please always send new versions in new threads.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3] 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-12-01 11:39   ` Yehyeong Lee
  0 siblings, 0 replies; 6+ messages in thread
From: Yehyeong Lee @ 2026-12-01 11:39 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() - the flag nvme_tcp_recv_skb()
already tests - next to the shutdown and the cancel_work_sync() it
belongs with, so that no command id is resolved against a request the
caller is about to end.  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.

Clearing it there rather than in nvme_tcp_stop_queue_nowait() leaves
one case open deliberately.  A queue that never reached
NVME_TCP_Q_LIVE is not stopped at all - __nvme_tcp_stop_queue() is not
called for it - so its receive stays on.  The only commands on such a
queue are the fabrics Connect and, where in-band authentication is
negotiated, the auth exchange, which runs inside
nvmf_connect_admin_queue() before the queue is made live.  The auth
commands carry NVME_SUBMIT_RETRY and nvme_check_ready() admits them on
a connecting controller, so they are re-dispatched to that same queue,
whose socket is still healthy precisely because nothing shut it down.
Turning the receive off there would drop the reply to every retry and
fail a connect that recovers today.  That is not a prediction.  An
earlier form of this change cleared the flag in
nvme_tcp_stop_queue_nowait(), outside the NVME_TCP_Q_LIVE test, and
carrying this audit further onto the queues that never go live showed
what that costs: with one authentication reply abandoned by the
target, the connect took three full command timeouts and then failed
outright, where the same test without the change recovers on the first
timeout and comes up authenticated.  Clearing the flag in the teardown
instead keeps that recovery and still covers every queue that was ever
live.

What the open case leaves behind is a state, not a demonstrated
use-after-free, and it is worth being plain about which.  Under the
stimulus that holds bytes unread past the stop, 27 of the 46 timeout
completions in a run still end on a queue that is allowed to read
them, and all 27 are the fabrics Connect of a queue that never went
live.  Across every run measured without this change - 710 such
completions - no receive ever resolved a command id onto a request
that was no longer in flight, and KASAN stayed quiet.  Connect is
answered by a capsule response that carries no data, so
nvme_tcp_recv_data(), the only place that writes through the bio the
completion freed, cannot be reached for it; a second completion is
the worst that is left.  When the connect does give up,
nvme_tcp_start_queue()'s own error path calls __nvme_tcp_stop_queue(),
measured 1.673 ms after the completion, and the clear above runs
there too.

Fixes: 236187c4ed19 ("nvme-tcp: fix timeout handler")
Cc: stable@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---
Changes since v2:
- v2 cleared queue->rd_enabled in nvme_tcp_stop_queue_nowait(), outside
  the NVME_TCP_Q_LIVE test, so it also applied to a queue that had
  never been live.  A queue in that state is where in-band
  authentication runs, and auth commands are retried onto it, so v2
  dropped the reply to every retry and turned a connect that recovers
  on one command timeout into three timeouts and a failure.  Measured
  on the same kernel with one authentication reply abandoned by the
  target: without the patch and with v1, one timeout, 10.1 s,
  authenticated, connect returns 0; with v2, three timeouts, 30.2 s,
  authentication failed, connect returns 1 and no device appears.
- v3 moves that clear into __nvme_tcp_stop_queue(), next to the socket
  shutdown and cancel_work_sync() it belongs with, which is inside the
  NVME_TCP_Q_LIVE test.  Authentication recovers again - one timeout,
  10.1 s, authenticated, connect returns 0 - and the receive side is
  still closed for every queue that was ever live: with the stimulus
  that holds an I/O queue's socket through a reconnect, the state v2
  removed goes from 128 of 128 timeout completions to 0, as it did
  under v2.
- The case now left open is described in the changelog above, with what
  was measured for it.
- nvme_tcp_drop_queue_ref() and the send-side half of the fix are
  unchanged from v1 and v2.
- Rebased on 7cd8495c0f16; checkpatch --strict clean; 54 of 54
  functional checks pass across mainline, 7.3.6 and 6.12.118 with and
  without the patch.

 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..db73adbb1a4700 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -2283,6 +2283,18 @@ static void nvme_tcp_restore_sock_ops(struct nvme_tcp_queue *queue)
 
 static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
 {
+	/*
+	 * Stop receiving before the socket goes down.  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 before the
+	 * cancel_work_sync() below means neither the io_work that is running
+	 * now nor one armed while this runs can resolve a command id against
+	 * a request the caller is about to end.  nvme_tcp_start_queue() sets
+	 * it again, next to the receive context, on the next connect.
+	 */
+	queue->rd_enabled = false;
 	kernel_sock_shutdown(queue->sock, SHUT_RDWR);
 	nvme_tcp_restore_sock_ops(queue);
 	cancel_work_sync(&queue->io_work);
@@ -3031,12 +3043,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	[flat|nested] 6+ messages in thread

* Re: [PATCH] nvme-tcp: drop the queue's reference to a timed-out request
  2026-12-01  8:03   ` Christoph Hellwig
@ 2026-12-01 12:14     ` Yehyeong Lee
  0 siblings, 0 replies; 6+ messages in thread
From: Yehyeong Lee @ 2026-12-01 12:14 UTC (permalink / raw)
  To: hch, kbusch, sagi; +Cc: linux-nvme, linux-kernel

On Tue, 1 Dec 2026 09:03:00 +0100, Christoph Hellwig wrote:
> Please always send new versions in new threads.

Understood, and sorry - I had already sent v3 into this thread before
your mail reached me.  Every later version will start its own thread.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-01 12:14 UTC | newest]

Thread overview: 6+ 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-30 10:44 ` [PATCH v2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
2026-12-01 11:39   ` [PATCH v3] " Yehyeong Lee
2026-11-30 10:49 ` [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
2026-12-01  8:03   ` Christoph Hellwig
2026-12-01 12:14     ` Yehyeong Lee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®