* [PATCH v4 0/2] nvme-tcp: do not refer to a timed-out request after completing it
@ 2026-12-02 13:19 Yehyeong Lee
2026-12-02 13:19 ` [PATCH v4 1/2] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
2026-12-02 13:19 ` [PATCH v4 2/2] nvme-fabrics: do not leave a timed-out command's id resolvable Yehyeong Lee
0 siblings, 2 replies; 5+ messages in thread
From: Yehyeong Lee @ 2026-12-02 13:19 UTC (permalink / raw)
To: hch, kbusch, sagi; +Cc: linux-nvme, linux-kernel
When a command times out while the controller is not LIVE,
nvme_tcp_timeout() completes it in place: it stops the one queue the
command was on and ends the request, which frees its bio and hands its
command id back to the tagset.
Two references to that command outlive the completion, and they are
independent of each other:
- the queue's send references - queue->request, and the links on
send_list and req_list - which only the send path drops, and the
send path is what stopping the queue just shut down;
- the command id itself, because nvmf_complete_timed_out_request()
ends the request with blk_mq_complete_request() rather than through
nvme_try_complete_req(), which is the only place the host advances
the generation counter nvme_find_rq() checks against an incoming id.
One patch each. 1/2 is byte for byte the patch posted as v1 and has not
changed since. 2/2 is new in this version: it replaces the
queue->rd_enabled gate that v2 and v3 used with a generation advance,
for the reasons below. Either patch can be applied or reverted without
the other, but they close different halves of the same hole, so both are
marked for stable.
Our continued audit of this path is what moved the second half onto the
command id rather than onto the queue. Three receive paths resolve an
incoming id - nvme_tcp_process_nvme_cqe(), nvme_tcp_handle_r2t() and
nvme_tcp_handle_c2h_data() - and all three go through nvme_find_rq(), so
advancing the generation closes them together, while a per-queue receive
flag closed them only for a queue that had gone live.
Two notes on scope:
- nvme_tcp_stop_queue_nowait() already clears NVME_TCP_Q_IO_CPU_SET and
queue->tls_enabled outside the NVME_TCP_Q_LIVE test, so a queue that
never went live has those two reset while its socket stays up. That
is mainline's behaviour today, and neither patch changes it or relies
on it; it is not part of what either patch fixes.
- 2/2 is not the "nvme: reject completions for requests that are not in
flight" proposal [1], which added an MQ_RQ_IN_FLIGHT test inside
nvme_find_rq() and was not applied. That one rejects on the
completion side, from the request's current state; 2/2 advances the
generation on the timeout side, so the id the controller was told to
use simply stops matching. That also answers the objection raised
there - a reused tag turns in-flight again, and then a bogus
completion cannot be told from a real one - because a reused tag
carries a new generation. And R2T and C2HData are not completions,
so they were never in that proposal's reach. The same distinction
applies to "nvme: bump genctr when cancelling a request" [2], which
advanced the generation in nvme_cancel_request() for a PCIe
controller posting completions after the host stopped waiting, and
was not applied either.
2/2 touches a helper two transports use. Everything below was measured
on nvme-tcp; nvme-rdma was not tested. The generation advance 2/2 adds
sits in nvmf_complete_timed_out_request() itself, which nvme-rdma reaches
from nvme_rdma_complete_timed_out() in the same state - the queue
stopped, the request about to be ended without passing through
nvme_try_complete_req() - so it applies there identically; that part is
read from the source, not measured. nvme-fc does not use this helper.
2/2's Fixes: tag names the commit that factored the helper out, because
that is the point from which both fabrics transports complete a
timed-out command through one shared place that does not advance the
generation. The deeper origin is commit e7006de6c238 ("nvme: code
command_id with a genctr for use-after-free validation"), which
introduced the generation check this relies on; if you would rather the
tag named that commit, say so and I will respin.
[1] https://lore.kernel.org/linux-nvme/20261103-nvme-double-cmpl-uaf-blk-mq-v1-1-b10fea474423@igalia.com/
[2] https://lore.kernel.org/linux-nvme/20260913124053.86565-1-mateusz.nowicki@posteo.net/
v1: https://lore.kernel.org/linux-nvme/20261129031537.1993449-1-yhlee@isslab.korea.ac.kr/
v2: https://lore.kernel.org/linux-nvme/20261130104426.2140231-1-yhlee@isslab.korea.ac.kr/
v3: https://lore.kernel.org/linux-nvme/20261201113916.2322397-1-yhlee@isslab.korea.ac.kr/
Sent as its own thread this time.
Changes since v3:
- The receive half is now closed by advancing the command id generation
where the host gives up on a command, not by turning a queue's receive
off. v2 cleared queue->rd_enabled in nvme_tcp_stop_queue_nowait(),
outside the NVME_TCP_Q_LIVE test; v3 moved that clear inside it.
Neither placement can be right, because the flag cannot separate the
two things that have to be separated. Outside the test it also
silences a queue that never went live, and that is where in-band
authentication runs, so every auth retry lost its answer: with one
authentication response abandoned by the target, the unpatched kernel
and v1 take one timeout, 10.1 s, and authenticate, while v2 takes
three timeouts, 30.2 s, and fails. Inside the test authentication
recovers again, but the queue that never went live is then left with
no gate at all.
- Carrying the audit onto that residual is what changed the shape of the
fix. The v3 changelog said a second completion was the worst left
behind. That was measured against a target that never answers a
command it has already answered, so there was nothing there to see.
With the target made to send one response for the abandoned command
id, the residual is a fault on the first attempt. Four injections,
one stale PDU each, same build per arm, nvme_core.io_timeout=10:
stale capsule stale R2T stale R2T stale
response in capsule out of C2HData
capsule
unpatched GPF KASAN UAF KASAN UAF refused
v2 clean clean clean clean
v3 GPF KASAN UAF KASAN UAF refused
1/2 + 2/2 clean clean clean clean
With 2/2 all four are refused by name - "request 0x0 genctr
mismatch (got 0x0 expected 0x1)", then "got bad cqe.command_id" or
"got bad r2t.command_id" - and the stale C2HData does not even reach
the direction check that was refusing it before.
- The R2T column needs nothing unusual on the wire.
nvme_tcp_try_send_data() deliberately does not advance req->data_sent
on the last payload send of a request, because that would race the
receive path completing it. So a fabrics command whose payload goes
out in a single send - Connect, auth_send - still has data_sent == 0
with data_len == 1024 after it has been sent in full, and a stale R2T
at offset 0 for the full length passes both of
nvme_tcp_handle_r2t()'s length checks. Payload in the capsule,
nothing about the host changed.
- With the generation advance, in-band authentication recovers exactly
as on an unpatched kernel: one timeout, 10.05 s, authenticated,
connect returns 0, against v2's three timeouts, 30.18 s and failure.
The advance does run on that path - it fires on the abandoned auth
command, on a queue that never went live - and nothing is refused
afterwards, because a retry is a new dispatch through
nvme_setup_cmd(), which recomputes cmd->common.command_id from the
current generation.
- Three things the generation advance does not do:
* A controller with NVME_QUIRK_SKIP_CID_GEN gets no generation at all.
nvme_try_complete_req() skips those too, so this is the existing
policy and not a new hole; the quirk is set only for PCIe devices,
which do not reach this helper. 1/2 is unconditional either
way.
* The generation is four bits wide, so sixteen completions of the same
tag bring it back around. That bound is pre-existing and unchanged.
* A response for an abandoned command id now fails nvme_find_rq() and
goes to error recovery, where before it was resolved onto the
request. That is what already happens for every other generation
mismatch. nvme_cid_to_rq(), which nvme_tcp_recv_data() uses, still
checks nothing; it is only reachable once
nvme_tcp_handle_c2h_data() has accepted the PDU, which is the check
2/2 restores.
- On the original stimuli the receive-side shape the first Oops came
from is closed. With an already-live I/O queue held for as long as
the controller is CONNECTING, the command id the controller holds
still resolves onto the completed request 128 times out of 128 with
only the send-side half, and 0 times out of 128 with 2/2 as well
(nvme_cid_to_rq() keeps resolving, as above).
- Rebased on 7cd8495c0f16. checkpatch --strict clean. The six
ordinary-path checks pass 54 of 54 across mainline, v7.3.6 and
v6.12.118 on three arms - unpatched, v3, and both patches applied -
with 0 KASAN reports, 0 warnings and 0 oopses in all nine runs.
Yehyeong Lee (2):
nvme-tcp: drop the queue's reference to a timed-out request
nvme-fabrics: do not leave a timed-out command's id resolvable
drivers/nvme/host/fabrics.h | 11 ++++++++++-
drivers/nvme/host/tcp.c | 24 ++++++++++++++++++++++++
2 files changed, 34 insertions(+), 1 deletion(-)
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 1/2] nvme-tcp: drop the queue's reference to a timed-out request
2026-12-02 13:19 [PATCH v4 0/2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
@ 2026-12-02 13:19 ` Yehyeong Lee
2026-12-02 13:48 ` sashiko-bot
2026-12-02 13:19 ` [PATCH v4 2/2] nvme-fabrics: do not leave a timed-out command's id resolvable Yehyeong Lee
1 sibling, 1 reply; 5+ messages in thread
From: Yehyeong Lee @ 2026-12-02 13:19 UTC (permalink / raw)
To: hch, kbusch, sagi; +Cc: linux-nvme, linux-kernel
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@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v4 2/2] nvme-fabrics: do not leave a timed-out command's id resolvable
2026-12-02 13:19 [PATCH v4 0/2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
2026-12-02 13:19 ` [PATCH v4 1/2] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
@ 2026-12-02 13:19 ` Yehyeong Lee
2026-12-02 13:35 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: Yehyeong Lee @ 2026-12-02 13:19 UTC (permalink / raw)
To: hch, kbusch, sagi; +Cc: linux-nvme, linux-kernel
nvmf_complete_timed_out_request() ends the request with
blk_mq_complete_request() and not through nvme_try_complete_req(), which
is the only place the host advances the generation counter that
nvme_find_rq() checks against an incoming command id. The id goes back
to the tagset while the controller may still answer it, so the next PDU
the transport receives for that id resolves onto a request that is no
longer in flight.
Stopping the queue first is not a barrier. SHUT_RD does not discard what
the socket has already received, and read_sock() does not look at the
shutdown state. A queue that never went live is not stopped at all:
in-band authentication runs inside nvmf_connect_admin_queue() before
NVME_TCP_Q_LIVE is set, and nvme_tcp_stop_queue_nowait() reaches
__nvme_tcp_stop_queue() only if test_and_clear_bit(NVME_TCP_Q_LIVE)
succeeds, so such a queue keeps a healthy socket and a running io_work
until the next connect attempt.
One response for the abandoned id is enough. With the preceding patch
applied, a capsule response for it resolves onto the request and
completes it a second time:
[ 16.906252] Oops: general protection fault, probably for non-canonical address 0xdffffc0000000031: 0000 [#1] SMP KASAN NOPTI
[ 16.916384] KASAN: null-ptr-deref in range [0x0000000000000188-0x000000000000018f]
[ 16.917528] CPU: 1 UID: 0 PID: 26 Comm: kworker/1:0H Not tainted 7.4.0-rc4-N1SKR3-00743-g4236649415e0-dirty #37 PREEMPT(lazy)
[ 16.919364] 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
[ 16.921368] Workqueue: nvme_tcp_wq nvme_tcp_io_work
[ 16.922203] RIP: 0010:blk_mq_complete_request_remote+0xce/0x800
[ 16.923119] Code: c1 ea 03 80 3c 02 00 0f 85 05 06 00 00 48 b8 00 00 00 00 00 fc ff df 4c 8b 63 10 49 8d bc 24 8e 01 00 00 48 89 fa 48 c1 ea 03 <0f> b6 14 02 48 89 f8 83 e0 07 83 c0 01 38 d0 7c 08 84 d2 0f 85 61
[ 16.925806] RSP: 0018:ffffc900001bf7c8 EFLAGS: 00010203
[ 16.926596] RAX: dffffc0000000000 RBX: ffff88800b8cc000 RCX: 1ffff11001719821
[ 16.927735] RDX: 0000000000000031 RSI: ffff88800b8cc000 RDI: 000000000000018e
[ 16.928856] RBP: 1ffff92000037efd R08: 0000000000000001 R09: 0000000000000000
[ 16.929917] R10: ffffc900001bf65f R11: 0000000000008000 R12: 0000000000000000
[ 16.930980] R13: dffffc0000000000 R14: ffff88800bae7000 R15: 0000000000000018
[ 16.932048] FS: 0000000000000000(0000) GS:ffff88811d763000(0000) knlGS:0000000000000000
[ 16.933266] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 16.934127] CR2: 00000000004474b0 CR3: 000000000985c002 CR4: 0000000000770ef0
[ 16.935201] PKRU: 55555554
[ 16.935621] Call Trace:
[ 16.936009] <TASK>
[ 16.936352] ? __asan_memcpy+0x3c/0x60
[ 16.936953] ? __pfx_blk_mq_complete_request_remote+0x10/0x10
[ 16.937818] nvme_tcp_recv_skb+0x7f8/0x60b0
[ 16.938551] ? __pfx_nvme_tcp_recv_skb+0x10/0x10
[ 16.939200] __tcp_read_sock+0x1ab/0x810
[ 16.939727] ? __pfx_nvme_tcp_recv_skb+0x10/0x10
[ 16.940340] ? __pfx___tcp_read_sock+0x10/0x10
[ 16.940930] ? __local_bh_enable_ip+0xa7/0x120
[ 16.941523] ? lockdep_hardirqs_on_prepare+0xdc/0x190
[ 16.942182] nvme_tcp_try_recv+0x15f/0x520
[ 16.942723] ? __pfx_nvme_tcp_try_recv+0x10/0x10
[ 16.943374] ? reacquire_held_locks+0xdd/0x200
[ 16.944013] nvme_tcp_io_work+0x320/0x730
[ 16.944630] ? __pfx_nvme_tcp_io_work+0x10/0x10
[ 16.945281] ? lock_release+0xc6/0x280
[ 16.945808] process_one_work+0x8d0/0x1b50
[ 16.946390] ? __pfx_process_one_work+0x10/0x10
[ 16.947020] ? lock_acquire+0x18d/0x300
[ 16.947560] ? lock_is_held_type+0x8f/0x100
[ 16.948109] ? __pfx_nvme_tcp_io_work+0x10/0x10
[ 16.948711] worker_thread+0x528/0xeb0
[ 16.949217] ? __pfx_worker_thread+0x10/0x10
[ 16.949780] kthread+0x300/0x400
[ 16.950220] ? __pfx_kthread+0x10/0x10
[ 16.950720] ret_from_fork+0x52e/0x780
[ 16.951254] ? __pfx_ret_from_fork+0x10/0x10
[ 16.951859] ? __switch_to+0x572/0xdd0
[ 16.952399] ? __pfx_kthread+0x10/0x10
[ 16.952937] ret_from_fork_asm+0x1a/0x30
[ 16.953549] </TASK>
[ 16.953960] ---[ end trace 0000000000000000 ]---
With this patch the same response is refused instead:
nvme nvme0: request 0x0 genctr mismatch (got 0x0 expected 0x1)
nvme nvme0: got bad cqe.command_id 0x0 on queue 0
Three receive paths resolve a command id through nvme_find_rq(): the
capsule response above, nvme_tcp_handle_r2t() and
nvme_tcp_handle_c2h_data(). The R2T one needs nothing unusual on the
wire. nvme_tcp_try_send_data() deliberately does not advance
req->data_sent on a request's last payload send, because that would race
the receive path completing it, so a fabrics command whose payload goes
out in a single send still has data_sent == 0 with data_len == 1024 after
it has been sent in full, and a stale R2T at offset 0 for the full length
passes both of nvme_tcp_handle_r2t()'s length checks. The request goes
back on req_list and the send path walks the bio the completion freed.
No conforming target answers a command twice or sends an unsolicited R2T,
so each of these was reproduced by making the target send that one PDU.
The controller is a remote peer; nothing on the host has to cooperate.
Advance the generation where the host gives up, under the same quirk test
nvme_try_complete_req() uses. A retry is a new dispatch through
nvme_setup_cmd(), which recomputes cmd->common.command_id from the
current generation, so retries are unaffected - including the in-band
authentication retries on a queue that never went live, which still
recover in one command timeout.
What this does not cover: a controller with NVME_QUIRK_SKIP_CID_GEN gets
no generation at all, but nvme_try_complete_req() skips those too and the
quirk is set only for PCIe devices, which do not reach this helper. The
generation is four bits wide, so sixteen completions of the same tag
bring it around; that bound is pre-existing. And nvme_cid_to_rq(), used
by nvme_tcp_recv_data(), still checks nothing - it is only reachable once
nvme_tcp_handle_c2h_data() has accepted the PDU, which is the check this
restores.
nvme-rdma reaches the same helper from nvme_rdma_complete_timed_out() in
the same state, so the advance applies there too; that was read from the
source, while everything measured here was measured on nvme-tcp.
Fixes: 93ba75c90524 ("nvme-fabrics: add a request timeout helper")
Cc: stable@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---
drivers/nvme/host/fabrics.h | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
index caf5503d083329..2e3a4a5af2d44b 100644
--- a/drivers/nvme/host/fabrics.h
+++ b/drivers/nvme/host/fabrics.h
@@ -202,10 +202,19 @@ static inline char *nvmf_ctrl_subsysnqn(struct nvme_ctrl *ctrl)
return ctrl->subsys->subnqn;
}
+/*
+ * This does not go through nvme_try_complete_req(), so advance the generation
+ * here: otherwise nvme_find_rq() keeps resolving the abandoned command id onto
+ * a request that is no longer in flight.
+ */
static inline void nvmf_complete_timed_out_request(struct request *rq)
{
if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
- nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
+ struct nvme_request *nr = nvme_req(rq);
+
+ if (!(nr->ctrl->quirks & NVME_QUIRK_SKIP_CID_GEN))
+ nr->genctr++;
+ nr->status = NVME_SC_HOST_ABORTED_CMD;
blk_mq_complete_request(rq);
}
}
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v4 2/2] nvme-fabrics: do not leave a timed-out command's id resolvable
2026-12-02 13:19 ` [PATCH v4 2/2] nvme-fabrics: do not leave a timed-out command's id resolvable Yehyeong Lee
@ 2026-12-02 13:35 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-02 13:35 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: UAF and double completion via nvme_cid_to_rq() after
C2HData timeout
- [High] nvme-tcp: race with active io_work in
nvme_tcp_complete_timed_out()
--
commit 718cdfd33ac400884137a2b0ab0be73d96fee9e7
Author: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
nvme-fabrics: do not leave a timed-out command's id resolvable
This commit increments the request generation counter in
nvmf_complete_timed_out_request() so that late responses arriving for an
abandoned command id are rejected by nvme_find_rq().
> diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
> index caf5503d08332..2e3a4a5af2d44 100644
> --- a/drivers/nvme/host/fabrics.h
> +++ b/drivers/nvme/host/fabrics.h
> @@ -202,10 +202,19 @@ static inline char *nvmf_ctrl_subsysnqn(struct nvme_ctrl *ctrl)
> return ctrl->subsys->subnqn;
> }
>
> +/*
> + * This does not go through nvme_try_complete_req(), so advance the generation
> + * here: otherwise nvme_find_rq() keeps resolving the abandoned command id onto
> + * a request that is no longer in flight.
> + */
> static inline void nvmf_complete_timed_out_request(struct request *rq)
> {
> if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
> - nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
> + struct nvme_request *nr = nvme_req(rq);
> +
> + if (!(nr->ctrl->quirks & NVME_QUIRK_SKIP_CID_GEN))
> + nr->genctr++;
> + nr->status = NVME_SC_HOST_ABORTED_CMD;
> blk_mq_complete_request(rq);
> }
> }
[Severity: High]
Can an in-progress C2HData transfer bypass this generation increment via
nvme_cid_to_rq(), causing a use-after-free and double completion?
When a 24-byte C2HData PDU header arrives before the timeout (for example,
for an auth_receive command while NVME_TCP_Q_LIVE is not yet set),
nvme_tcp_handle_c2h_data() validates pdu->command_id with nvme_find_rq()
and sets queue->data_remaining, transitioning the queue to
NVME_TCP_RECV_DATA.
If the command times out before the payload or data digest arrives,
nvme_tcp_complete_timed_out() increments nr->genctr and completes rq,
freeing its bio, without resetting queue->data_remaining or
queue->ddgst_remaining (and without stopping queue->io_work when
NVME_TCP_Q_LIVE is clear). When the remaining bytes arrive,
nvme_tcp_recv_data() and nvme_tcp_recv_ddgst() resolve pdu->command_id
through nvme_cid_to_rq(), which strips the generation bits without
checking nr->genctr, writing into the freed bio and calling
nvme_tcp_end_request() a second time if NVME_TCP_F_DATA_SUCCESS is set.
[Severity: High]
Can nvmf_complete_timed_out_request() race with an active queue->io_work
when NVME_TCP_Q_LIVE is not set?
During in-band authentication, NVME_TCP_Q_LIVE is not yet set, so
nvme_tcp_stop_queue_nowait() skips __nvme_tcp_stop_queue() and leaves
queue->io_work running concurrently with nvme_tcp_complete_timed_out():
CPU0 (timeout) CPU1 (queue->io_work)
nvme_tcp_complete_timed_out()
nvme_tcp_stop_queue()
/* NVME_TCP_Q_LIVE is 0 */
nvme_tcp_drop_queue_ref()
nvme_tcp_try_recv()
nvme_tcp_handle_r2t()
nvme_find_rq() /* old genctr matches */
llist_add(&req->lentry,
&queue->req_list)
nvmf_complete_timed_out_request()
nr->genctr++
blk_mq_complete_request(rq)
nvme_tcp_try_send()
/* walks freed bio <-- use-after-free */
Similarly, if nvme_tcp_process_nvme_cqe() (or nvme_rdma_process_nvme_rsp()
before NVME_RDMA_Q_LIVE is set) passes nvme_find_rq() just before CPU0
increments nr->genctr, nvme_try_complete_req() and
nvmf_complete_timed_out_request() race without synchronization on
nr->genctr++ and rq->state and both complete the request.
In addition, nvme_tcp_stop_queue_nowait() clears queue->tls_enabled and
NVME_TCP_Q_IO_CPU_SET outside the test_and_clear_bit(NVME_TCP_Q_LIVE)
check, leaving queue->tls_enabled false if an authentication retry
recovers on the same TLS socket.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202131909.2556796-1-yhlee@isslab.korea.ac.kr?part=2
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v4 1/2] nvme-tcp: drop the queue's reference to a timed-out request
2026-12-02 13:19 ` [PATCH v4 1/2] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
@ 2026-12-02 13:48 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-02 13:48 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: UAF and double completion in nvme_tcp_recv_data()
after timeout
- [High] nvme-tcp: race in nvme_tcp_complete_timed_out() before dropping
queue ref
--
commit aeeaaf545efa2b40c5cf8bc8268997a438e0fbcb
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) under queue->send_mutex in
nvme_tcp_complete_timed_out() before completing a timed-out request.
> diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> index 62a594b8ab9bf..5320375d6f691 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);
> +}
[Severity: High]
Does nvme_tcp_drop_queue_ref() leave the receive context pointing at the
timed-out request when a read command times out after its C2H Data PDU
header has already been parsed?
When nvme_tcp_handle_c2h_data() validates the header before the timeout,
it stores the PDU in queue->pdu and sets queue->data_remaining, moving the
queue into NVME_TCP_RECV_DATA (or NVME_TCP_RECV_DDGST). Neither
__nvme_tcp_stop_queue() nor nvme_tcp_drop_queue_ref() clears
queue->rd_enabled or calls nvme_tcp_init_recv_ctx(), and
kernel_sock_shutdown(queue->sock, SHUT_RDWR) does not flush bytes already
queued in sk->sk_receive_queue.
Once the controller transitions to NVME_CTRL_LIVE and a new request is
dispatched to the stopped queue, nvme_tcp_io_work() fails
nvme_tcp_try_send() with -EPIPE, breaks out of the loop, and re-queues
io_work; on the next run nvme_tcp_try_send() returns 0 and
nvme_tcp_try_recv() executes:
drivers/nvme/host/tcp.c:nvme_tcp_recv_data() {
struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
struct request *rq =
nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
Because nvme_tcp_recv_data() and nvme_tcp_recv_ddgst() resolve
pdu->command_id directly with nvme_cid_to_rq() rather than nvme_find_rq(),
they bypass genctr validation, copy incoming data into req->iter and
req->curr_bio after the timed-out request's bio has been freed, and call
nvme_tcp_end_request() to complete rq a second time.
> +
> 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 complete req while nvme_tcp_stop_queue()
runs before nvme_tcp_drop_queue_ref()?
__nvme_tcp_stop_queue() shuts down the socket before cancelling io_work
and without holding queue->send_mutex, and nvme_tcp_stop_queue() then
sleeps in nvme_tcp_wait_queue() while req is still on queue->request,
send_list, or req_list:
CPU0 (timeout) CPU1 (io_work / .queue_rq)
nvme_tcp_complete_timed_out(rq)
nvme_tcp_stop_queue()
kernel_sock_shutdown(SHUT_RDWR)
nvme_tcp_try_send()
sock_sendmsg() -> -EPIPE
nvme_tcp_fail_request(queue->request)
nvme_tcp_end_request(rq, ...)
cancel_work_sync(&queue->io_work)
nvme_tcp_wait_queue()
msleep(2)
blk_mq_requeue_work()
nvme_tcp_queue_rq(rq)
nvme_tcp_drop_queue_ref(req)
nvmf_complete_timed_out_request(rq) <-- aborts or double-completes rq
If rq is retryable (such as an in-band authentication command submitted
with NVME_SUBMIT_RETRY), nvme_tcp_fail_request() requeues it with delay 0.
While nvme_tcp_wait_queue() sleeps, blk_mq_requeue_work() can re-dispatch
rq back to MQ_RQ_IN_FLIGHT on the queue, after which
nvme_tcp_drop_queue_ref() strips the re-dispatched request from the queue
and nvmf_complete_timed_out_request() aborts it. Even without a retry, if
the concurrent completion moves rq->state from MQ_RQ_COMPLETE to
MQ_RQ_IDLE between the two READ_ONCE(rq->state) checks in
blk_mq_request_started(rq) && !blk_mq_request_completed(rq),
nvmf_complete_timed_out_request() calls blk_mq_complete_request(rq) a
second time.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202131909.2556796-1-yhlee@isslab.korea.ac.kr?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-02 13:48 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 13:19 [PATCH v4 0/2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
2026-12-02 13:19 ` [PATCH v4 1/2] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
2026-12-02 13:48 ` sashiko-bot
2026-12-02 13:19 ` [PATCH v4 2/2] nvme-fabrics: do not leave a timed-out command's id resolvable Yehyeong Lee
2026-12-02 13:35 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox