* [PATCH v5 0/2] nvme-tcp: do not refer to a timed-out request after completing it
@ 2026-12-04 13:53 Yehyeong Lee
2026-12-04 13:53 ` [PATCH v5 1/2] nvme-fabrics: do not leave a timed-out command's id resolvable Yehyeong Lee
2026-12-04 13:53 ` [PATCH v5 2/2] nvme-tcp: do not leave a timed-out request reachable from its queue Yehyeong Lee
0 siblings, 2 replies; 5+ messages in thread
From: Yehyeong Lee @ 2026-12-04 13:53 UTC (permalink / raw)
To: Christoph Hellwig, Keith Busch, Sagi Grimberg; +Cc: linux-nvme, linux-kernel
When a command times out while the controller is not LIVE,
nvme_tcp_timeout() completes it in place: nvme_tcp_complete_timed_out()
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.
v4 closed two of the ways that command stays reachable. Carrying the
audit onto the queue that never reached NVME_TCP_Q_LIVE - the one an
in-band authentication or a connect runs on - found two more, and both are
open with v4 applied. nvme_tcp_stop_queue_nowait() only shuts the socket
down and cancel_work_sync()es io_work inside the NVME_TCP_Q_LIVE test, so
on such a queue the timeout handler runs alongside a live socket and a
running io_work:
- the receive context. A C2HData header that arrived before the timeout
leaves queue->data_remaining set and queue->pdu naming this command,
and nvme_tcp_recv_data() and nvme_tcp_recv_ddgst() resolve that id with
nvme_cid_to_rq(), which checks no generation. The remaining payload is
then copied into the request the host has given up on - a write through
a freed bio when that request was ended rather than retried.
- the command id itself, for as long as the handler takes to end the
request. v4 advances the generation where the request is completed,
which is the last thing the handler does; everything the handler does
before that runs with the id still resolving, and io_work is still
running. nvme_tcp_handle_r2t() can resolve it and put the request back
on req_list after the handler has already dropped it, so the send path
walks a bio the completion then frees.
So the order the handler does things in is the fix. 2/2 retires the
command id first, with the helper 1/2 splits out, while the queue is
still being used and before anything else is touched - under the same
started-and-not-completed test the completion uses, so a request another
path has already finished with keeps the generation it was given. Then
cancel_work_sync(), which finishes a resolve that was already in flight.
Then the send references, under send_mutex. Then, under the socket lock,
the unfinished receive transfer and the completion.
Retiring the id last does not work, and doing it last under the socket
lock does not work either. The socket lock serialises the receive path,
it does not order it: the socket callbacks can arm io_work again the
moment the cancel returns, and an io_work that takes the lock first
resolves the id while the generation is still the old one. Held open
deliberately, with the target sending an unsolicited R2T for the
abandoned id in that window, it does:
the send references are dropped, the generation is not yet advanced
io_work resolves the R2T and puts the request back on req_list
try_send picks that request up
the handler takes the socket lock, completes the request, frees its bio
try_send walks the freed bio, and KASAN reports it
With the id retired first, the same window ends in "genctr mismatch (got
0x0 expected 0x1)" and "got bad r2t.command_id 0x0" instead, in both
orders - whether io_work reaches the socket lock before the handler or
after it. The socket lock is still taken, for the receive context: the
data and digest paths resolve with nvme_cid_to_rq() and no generation
check, so that transfer has to be dropped, and the lock is what keeps the
drop from landing in the middle of a receive.
1/2 comes first because 2/2 calls nvmf_retire_timed_out_command(). 1/2 on
its own is v4 2/2 plus that split, and behaves exactly as v4 did.
The receive context is only dropped for the transfer that names the
abandoned command: the queue is left expecting a new PDU header rather
than silenced, so a later response for another command is still received.
That is what an authentication retry on the same queue needs, and it is
why this is not the queue->rd_enabled gate that v2 and v3 used. With one
authentication response abandoned by the target, this series recovers
exactly as an unpatched kernel does: one timeout, 10.11 s, authenticated,
connect returns 0, against v2's three timeouts, 30.18 s and failure.
Three 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.
- nvmf_complete_timed_out_request() tests blk_mq_request_started() and
!blk_mq_request_completed() and then completes the request, with
nothing serialising the two. If the controller answers in that window,
the receive path completes the request first and the handler completes
it a second time. Holding the window open on an unpatched kernel shows
it end to end: the handler enters with the request in flight, the
target's late answer completes it, blk_mq_free_request() clears
MQ_RQ_IN_FLIGHT from __nvme_submit_sync_cmd() while the handler is
still inside its window, and the handler then completes a tag that is
no longer in flight and faults.
On nvme-tcp this series closes that race, because the context that wins
it is the transport's own receive path and 2/2 holds the socket lock
across the test and the completion: the same forced window leaves the
request in flight and completes it once. The helper itself is still
unserialised, though. The state it tests is written by blk-mq with a
plain store from paths nvme holds no lock over, and blk-mq exposes no
way to claim a request atomically, so it cannot be made atomic inside
the helper; nvme-rdma, which shares it and takes no such lock, is
unchanged. That belongs in a patch of its own - either a claim
primitive in blk-mq or a completion claim in nvme core - rather than
folded in here.
One variant of that race is also still open and is not a helper
problem: nvme_tcp_stop_queue() runs before anything this series does,
and a send that fails inside it ends the request through
nvme_tcp_fail_request(); a retryable command is then requeued with no
delay and can be dispatched again while nvme_tcp_wait_queue() sleeps,
after which the handler strips and aborts the re-dispatched request.
Nothing here changes that path, and I have not been able to reproduce
it: 0 in 517,960 retry requeues.
- 2/2's early retire is nvme-tcp only. nvme_rdma_complete_timed_out()
keeps the old ordering and retires the id where it completes the
request, so an nvme-rdma queue that is still in use between the two
is unchanged by this series, and that path was not measured.
- 1/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; 1/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.
1/2 touches a helper two transports use. Everything below was measured on
nvme-tcp; nvme-rdma was not tested. The generation advance 1/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-rdma does not call the new helper, so nothing else about
it changes. nvme-fc does not use this helper.
1/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/
v4: https://lore.kernel.org/linux-nvme/20261202131909.2556796-1-yhlee@isslab.korea.ac.kr/
Changes since v4:
- The two patches swap places, because the nvme-tcp one now calls into the
nvme-fabrics one. 1/2 is v4 2/2 with the generation advance split out
as nvmf_retire_timed_out_command(); nothing else about it changed.
- 2/2's early retire is guarded by the same blk_mq_request_started() and
!blk_mq_request_completed() test the completion uses, so the handler
does not advance the generation of a request another path has already
finished with - and of the tag behind it, had it been reused. Measured
with the guard in place: the handler enters with the request in flight
in every cell that closes a route, and the forced window still refuses
the abandoned id.
- 2/2 retires the command id before it touches the queue, rather than when
it ends the request. Forced open between the drop of the send
references and the completion, with the target sending for the abandoned
id in that window:
io_work resolved request put reports
the id back on list
retire last yes yes 3 KASAN
retire last + sk lock yes yes 3 KASAN
retire first no, refused no 0
The middle row is why the socket lock is not enough on its own: it keeps
the two paths from running at the same time, it does not keep io_work
from getting there first.
- 2/2 waits for io_work before dropping the queue's references. With the
target made to send an unsolicited R2T for the abandoned command id and
the resolve held open, v4 faults:
nvme_find_rq accepts the id at 11.275, the handler completes and frees
the request at 12.812, the R2T lands on req_list at 15.307, and
nvme_tcp_try_send_data() reads the freed bio at 15.308.
With the wait, the same stimulus orders the two: cancel_work_sync() does
not return until that resolve has finished re-queueing the request, and
the drop then unlinks it. Measured: the handler enters at 13.017156 and
blocks in the cancel, the re-queue lands at 15.320605, and the handler
reaches the completion at 15.323819. 0 reports.
- 2/2 drops the queue's unfinished receive transfer when it names the
abandoned request. With the target made to send a C2HData header,
stop, and send the payload after the timeout:
payload copied into report
the abandoned request
unpatched yes KASAN use-after-free
v4 yes KASAN use-after-free
this series no clean
The use-after-free needs the abandoned request to be ended rather than
retried, which is what nvme_core.max_retries=0 does; at the default the
payload lands in the retry's buffer instead and there is no report to
see. Both are closed.
- The guard is the queue's receive state and the command id of the
transfer in progress, not a length. nvme_tcp_init_recv_ctx() leaves
data_remaining at SIZE_MAX, so an idle queue would pass a "still has
data to receive" test and have its receive context reset for nothing;
that is how authentication would break. Measured on an idle queue:
recv_state is RECV_PDU and the reset is declined. Measured on a queue
mid-transfer for a different command: the transfer is left alone.
- Payload bytes still on their way after the reset are read as a PDU
header and rejected - "unsupported pdu type (1)", then "receive failed:
-22" - which ends in error recovery. 0 reports, 0 lockdep complaints,
including with PROVE_LOCKING on across every cell: send_mutex is
released before the socket lock is taken, and io_work only trylocks
send_mutex, so the two orders cannot cross.
- 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, v4, and this series - with 0 KASAN reports, 0
warnings and 0 oopses.
Yehyeong Lee (2):
nvme-fabrics: do not leave a timed-out command's id resolvable
nvme-tcp: do not leave a timed-out request reachable from its queue
drivers/nvme/host/fabrics.h | 9 ++++++++
drivers/nvme/host/tcp.c | 46 +++++++++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+)
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v5 1/2] nvme-fabrics: do not leave a timed-out command's id resolvable
2026-12-04 13:53 [PATCH v5 0/2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
@ 2026-12-04 13:53 ` Yehyeong Lee
2026-12-04 14:28 ` sashiko-bot
2026-12-04 13:53 ` [PATCH v5 2/2] nvme-tcp: do not leave a timed-out request reachable from its queue Yehyeong Lee
1 sibling, 1 reply; 5+ messages in thread
From: Yehyeong Lee @ 2026-12-04 13:53 UTC (permalink / raw)
To: Christoph Hellwig, Keith Busch, Sagi Grimberg; +Cc: linux-nvme, linux-kernel
nvmf_complete_timed_out_request() ends the request with
blk_mq_complete_request() instead of nvme_try_complete_req(), which is the
only place the host advances the generation counter that nvme_find_rq()
checks a command id against. The id goes back to the tagset while the
controller may still answer it, so a late response, R2T or C2HData for it
resolves onto a request that is no longer in flight and completes it a
second time.
Stopping the queue first is no barrier: SHUT_RD does not discard what the
socket has already received, and a queue that never reached
NVME_TCP_Q_LIVE - the one carrying a connect or an in-band authentication
command - is not stopped at all, so it keeps a working socket and a
running io_work.
Advance the generation where the host gives up, under the same quirk test
nvme_try_complete_req() uses, and split it out as
nvmf_retire_timed_out_command() for a transport that keeps using the queue
after giving up and has to retire the id earlier than the completion.
Advancing a monotonic counter twice is harmless, and a retry is a new
dispatch through nvme_setup_cmd(), which recomputes the id from the
current generation.
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 | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
index caf5503d083329..07db19fc85a294 100644
--- a/drivers/nvme/host/fabrics.h
+++ b/drivers/nvme/host/fabrics.h
@@ -202,9 +202,18 @@ static inline char *nvmf_ctrl_subsysnqn(struct nvme_ctrl *ctrl)
return ctrl->subsys->subnqn;
}
+static inline void nvmf_retire_timed_out_command(struct request *rq)
+{
+ struct nvme_request *nr = nvme_req(rq);
+
+ if (!(nr->ctrl->quirks & NVME_QUIRK_SKIP_CID_GEN))
+ nr->genctr++;
+}
+
static inline void nvmf_complete_timed_out_request(struct request *rq)
{
if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
+ nvmf_retire_timed_out_command(rq);
nvme_req(rq)->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 v5 1/2] nvme-fabrics: do not leave a timed-out command's id resolvable
2026-12-04 13:53 ` [PATCH v5 1/2] nvme-fabrics: do not leave a timed-out command's id resolvable Yehyeong Lee
@ 2026-12-04 14:28 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-04 14:28 UTC (permalink / raw)
To: Yehyeong Lee; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] nvme-fabrics: unsynchronized nr->genctr++ in
nvmf_retire_timed_out_command()
- [Low] nvme-fabrics: Fixes tag cites refactor 93ba75c90524 instead of
e4fdb2b167ed
--
commit d42cf469d2abf13e2a74d2e7aa0bac5c918cf7ba
Author: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
> nvme-fabrics: do not leave a timed-out command's id resolvable
>
> nvmf_complete_timed_out_request() ends the request with
> blk_mq_complete_request() instead of nvme_try_complete_req(), which is the
> only place the host advances the generation counter that nvme_find_rq()
> checks a command id against. The id goes back to the tagset while the
> controller may still answer it, so a late response, R2T or C2HData for it
> resolves onto a request that is no longer in flight and completes it a
> second time.
>
> Stopping the queue first is no barrier: SHUT_RD does not discard what the
> socket has already received, and a queue that never reached
> NVME_TCP_Q_LIVE - the one carrying a connect or an in-band authentication
> command - is not stopped at all, so it keeps a working socket and a
> running io_work.
>
> Advance the generation where the host gives up, under the same quirk test
> nvme_try_complete_req() uses, and split it out as
> nvmf_retire_timed_out_command() for a transport that keeps using the queue
> after giving up and has to retire the id earlier than the completion.
> Advancing a monotonic counter twice is harmless, and a retry is a new
> dispatch through nvme_setup_cmd(), which recomputes the id from the
> current generation.
>
> Fixes: 93ba75c90524 ("nvme-fabrics: add a request timeout helper")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
[Severity: Low]
Should the Fixes tag cite commit e4fdb2b167ed ("nvme: increment request
genctr on completion") instead?
Commit 93ba75c90524 ("nvme-fabrics: add a request timeout helper") only
extracted the existing TCP and RDMA timeout completion code into
nvmf_complete_timed_out_request() without changing behavior. It was the
earlier commit that moved the genctr increment exclusively into
nvme_try_complete_req() while those timeout paths kept calling
blk_mq_complete_request() directly.
> diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
> index caf5503d08332..07db19fc85a29 100644
> --- a/drivers/nvme/host/fabrics.h
> +++ b/drivers/nvme/host/fabrics.h
> @@ -202,9 +202,18 @@ static inline char *nvmf_ctrl_subsysnqn(struct nvme_ctrl *ctrl)
> return ctrl->subsys->subnqn;
> }
>
> +static inline void nvmf_retire_timed_out_command(struct request *rq)
> +{
> + struct nvme_request *nr = nvme_req(rq);
> +
> + if (!(nr->ctrl->quirks & NVME_QUIRK_SKIP_CID_GEN))
> + nr->genctr++;
> +}
> +
> static inline void nvmf_complete_timed_out_request(struct request *rq)
> {
> if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
> + nvmf_retire_timed_out_command(rq);
> nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
> blk_mq_complete_request(rq);
> }
[Severity: Medium]
Can the unlocked nr->genctr++ in nvmf_retire_timed_out_command() race
with active RX, TX, or CQ paths on another CPU?
genctr is a plain u8 that was previously only updated on the serialized
completion path. When a connect or authentication command times out on
nvme-rdma before NVME_RDMA_Q_LIVE is set, nvme_rdma_stop_queue() is a
no-op and the CQ handler runs concurrently:
CPU0 (timeout work) CPU1 (RDMA CQ)
nvme_rdma_complete_timed_out()
nvme_rdma_stop_queue()
nvmf_complete_timed_out_request()
nvmf_retire_timed_out_command()
nr->genctr++ nvme_rdma_recv_done()
nvme_rdma_process_nvme_rsp()
nvme_find_rq()
reads nr->genctr <-- data race
...
nvme_try_complete_req()
rq->genctr++ <-- data race
This is a KCSAN data race against the plain reads in nvme_find_rq() and
the plain increment in nvme_try_complete_req(), and without atomic or
memory-ordered access a racing RX/CQ CPU on a weakly ordered architecture
is not guaranteed to observe the retired generation counter.
In commit "nvme-tcp: do not leave a timed-out request reachable from its
queue", nvme_tcp_complete_timed_out() also calls this helper before
stopping the queue or taking send_mutex/lock_sock(sk), which can race
with nvme_tcp_setup_h2c_data_pdu() and mismatch an H2CData command_id.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204135345.2994739-1-yhlee@isslab.korea.ac.kr?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v5 2/2] nvme-tcp: do not leave a timed-out request reachable from its queue
2026-12-04 13:53 [PATCH v5 0/2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
2026-12-04 13:53 ` [PATCH v5 1/2] nvme-fabrics: do not leave a timed-out command's id resolvable Yehyeong Lee
@ 2026-12-04 13:53 ` Yehyeong Lee
2026-12-04 15:06 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: Yehyeong Lee @ 2026-12-04 13:53 UTC (permalink / raw)
To: Christoph Hellwig, Keith Busch, Sagi Grimberg; +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 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@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v5 2/2] nvme-tcp: do not leave a timed-out request reachable from its queue
2026-12-04 13:53 ` [PATCH v5 2/2] nvme-tcp: do not leave a timed-out request reachable from its queue Yehyeong Lee
@ 2026-12-04 15:06 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-04 15:06 UTC (permalink / raw)
To: Yehyeong Lee; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] nvme-tcp: UAF and NULL deref of sock and pdu in
nvme_tcp_complete_timed_out()
- [High] nvme-tcp: TCP PDU framing corruption in
nvme_tcp_drop_recv_ctx()/queue_ref()
- [High] nvme-tcp: nvme_tcp_complete_timed_out() races with
nvme_tcp_poll() R2T
- [High] nvme-tcp: nvme_tcp_stop_queue() before drop_queue_ref() races
with send
- [Medium] nvme-tcp: unlocked data race on genctr in
nvme_tcp_complete_timed_out()
- [Medium] nvme-tcp: nvme_tcp_drop_queue_ref() can self-loop req_list on
auth retry
- [Medium] nvme-tcp: nvme_tcp_drop_queue_ref() can corrupt req_list into
a self-loop
- [Medium] nvme-tcp: nvme_tcp_drop_queue_ref() drains req_list without
kicking io_work
--
commit b2059b42e13e2c0cc53cb55c6db5edeebea6d283
Author: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
nvme-tcp: do not leave a timed-out request reachable from its queue
This commit updates nvme_tcp_complete_timed_out() to retire the command ID,
stop the queue, cancel io_work, unlink the request from the queue's send
state under send_mutex, and reset any matching receive context before
completing the request under lock_sock().
> diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> index 62a594b8ab9bf..b89d763372b5c 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);
[Severity: Medium]
Can init_llist_node(&req->lentry) here turn queue->req_list into a self-loop
if a retryable in-band authentication command is re-queued concurrently?
On a queue that has not yet set NVME_TCP_Q_LIVE, nvme_tcp_stop_queue() leaves
the socket open and its callbacks armed. If a re-armed io_work fails sending
an auth command (submitted with NVME_SUBMIT_RETRY in nvme_auth_submit()),
nvme_retry_req() re-dispatches the same request onto queue->req_list
locklessly without waiting for blk_mq_timeout_work():
CPU0 CPU1
nvme_tcp_drop_queue_ref()
mutex_lock(&queue->send_mutex)
nvme_tcp_process_req_list(queue)
blk_mq_requeue_work()
nvme_tcp_queue_rq()
nvme_tcp_queue_request()
llist_add(&req->lentry,
&queue->req_list)
init_llist_node(&req->lentry) <-- self-loop in queue->req_list
The next nvme_tcp_process_req_list() then spins forever holding
queue->send_mutex while repeatedly adding req->entry to queue->send_list. If
llist_add() instead runs right after nvme_tcp_drop_queue_ref() returns,
nvmf_complete_timed_out_request() can complete the retried request while it is
still on queue->req_list.
[Severity: Medium]
Can the same init_llist_node(&req->lentry) also race with
nvme_tcp_handle_r2t() and corrupt queue->req_list into a self-loop?
Because nvme_tcp_drop_queue_ref() runs without lock_sock(sk) and non-LIVE
sockets keep sk_data_ready armed, a concurrent nvme_tcp_io_work() that fails
mutex_trylock(&queue->send_mutex) proceeds directly into nvme_tcp_try_recv():
CPU0 CPU1
nvme_tcp_drop_queue_ref()
mutex_lock(&queue->send_mutex)
nvme_tcp_process_req_list(queue)
nvme_tcp_io_work()
mutex_trylock(&queue->send_mutex)
/* fails, continues to recv */
nvme_tcp_try_recv()
nvme_tcp_handle_r2t()
llist_add(&req->lentry,
&queue->req_list)
init_llist_node(&req->lentry) <-- self-loop in queue->req_list
If a target sends an R2T PDU matching the incremented 4-bit generation
counter for a Connect or auth_send request, queue->req_list becomes circular
and the next nvme_tcp_process_req_list() hangs holding send_mutex and
corrupts queue->send_list.
> +
> + mutex_unlock(&queue->send_mutex);
> +}
[Severity: Medium]
Can draining queue->req_list in nvme_tcp_drop_queue_ref() without re-queuing
io_work strand other requests on queue->send_list?
Previously only nvme_tcp_fetch_request() called nvme_tcp_process_req_list()
and immediately drove transmission. Here, nvme_tcp_complete_timed_out()
cancels io_work and then moves all pending requests from queue->req_list onto
queue->send_list while only unlinking req. Because nvme_tcp_commit_rqs()
only checks !llist_empty(&queue->req_list), and a concurrent
nvme_tcp_queue_request() can see both lists empty mid-drain, other requests
sharing the queue (such as commands submitted after a late Connect completion
sets NVME_TCP_Q_LIVE) can sit on queue->send_list until they time out.
Re-queuing io_work when nvme_tcp_queue_has_pending(queue) is true avoids
stranding them.
> +
> +/* 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);
> +}
[Severity: High]
Does resetting the receive state to NVME_TCP_RECV_PDU here desynchronize TCP
PDU framing?
When nvme_tcp_recv_state(queue) is NVME_TCP_RECV_DATA or NVME_TCP_RECV_DDGST,
the C2HData header has already been consumed and the next unread bytes on the
socket are payload or digest bytes. Because neither nvme_tcp_stop_queue()
nor nvme_tcp_drop_recv_ctx() clears queue->rd_enabled, subsequent reads on a
non-LIVE open socket (or leftover skbs in sk_receive_queue after
kernel_sock_shutdown()) feed those payload bytes into nvme_tcp_recv_pdu() as
PDU headers.
On the transmit side, clearing queue->request = NULL in
nvme_tcp_drop_queue_ref() on a non-LIVE queue whose socket remains open also
abandons a partially sent PDU, corrupting TX framing when a retryable in-band
auth command is retried on the same socket.
> +
> 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;
[Severity: High]
Can req->queue->sock->sk be NULL or freed before lock_sock(sk) and
nvme_tcp_drop_recv_ctx() run?
nvme_tcp_complete_timed_out() reads req->queue->sock->sk without
queue->queue_lock, without checking NVME_TCP_Q_ALLOCATED or queue->sock, and
without taking a reference on sk. It then sleeps in nvme_tcp_stop_queue(),
cancel_work_sync(), and mutex_lock(&queue->send_mutex) before calling
lock_sock(sk), reading queue->pdu in nvme_tcp_drop_recv_ctx(), and calling
release_sock(sk).
None of the teardown or error paths (such as nvme_tcp_configure_admin_queue()
on reconnect failure or nvme_tcp_setup_io_queues()) call blk_sync_queue() on
ctrl->fabrics_q or ctrl->connect_q before nvme_tcp_free_queue():
CPU0 CPU1
nvme_tcp_complete_timed_out()
sk = req->queue->sock->sk
nvme_tcp_stop_queue()
/* command completes concurrently */
nvme_tcp_configure_admin_queue()
nvme_tcp_free_admin_queue()
nvme_tcp_free_queue()
__fput_sync(queue->sock->file)
queue->sock = NULL
kfree(queue->pdu)
lock_sock(sk) <-- use-after-free
nvme_tcp_drop_recv_ctx() <-- reads freed queue->pdu
If nvme_tcp_free_queue() runs before the initial read, req->queue->sock->sk
is a NULL pointer dereference; if it runs afterward, lock_sock(sk),
release_sock(sk), and queue->pdu in nvme_tcp_drop_recv_ctx() hit
use-after-free.
> +
> + if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq))
> + nvmf_retire_timed_out_command(rq);
[Severity: Medium]
Does calling nvmf_retire_timed_out_command(rq) here introduce an unlocked
data race on nvme_req(rq)->genctr?
nvmf_retire_timed_out_command() performs a plain non-atomic nr->genctr++
while the socket is still live and neither io_work nor lock_sock(sk) has been
fenced. Meanwhile, nvme_tcp_io_work() or nvme_tcp_poll() (holding only
lock_sock(sk)) can concurrently read genctr in nvme_find_rq() and increment
it in nvme_try_complete_req(), risking a lost increment or spurious
generation mismatch errors.
>
> nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
[Severity: High]
Can calling nvme_tcp_stop_queue() before nvme_tcp_drop_queue_ref() race with
an active send and cause double completion or abort a retried request?
__nvme_tcp_stop_queue() calls kernel_sock_shutdown(queue->sock, SHUT_RDWR)
without send_mutex and nvme_tcp_wait_queue() can sleep up to 100 ms before
nvme_tcp_drop_queue_ref() unlinks req. Because nvme_tcp_try_send() does not
check genctr, an already-running io_work or inline send hits -EPIPE and
completes rq with NVME_SC_HOST_PATH_ERROR:
CPU0 CPU1
nvme_tcp_complete_timed_out()
nvme_tcp_stop_queue()
kernel_sock_shutdown(..., SHUT_RDWR)
nvme_tcp_try_send()
/* sock_sendmsg() returns -EPIPE */
nvme_tcp_fail_request(req)
blk_mq_end_request(rq)
/* rq->state = MQ_RQ_COMPLETE */
...
nvmf_complete_timed_out_request()
blk_mq_request_started(rq) /* sees MQ_RQ_COMPLETE */
blk_mq_free_request(rq)
/* rq->state = MQ_RQ_IDLE */
!blk_mq_request_completed(rq) /* sees MQ_RQ_IDLE */
blk_mq_complete_request(rq) <-- double completion
Because nvmf_complete_timed_out_request() reads rq->state twice, the
transition from MQ_RQ_COMPLETE to MQ_RQ_IDLE lets both checks pass and
triggers a second completion. For a retryable request (such as non-multipath
I/O or an auth command), blk_mq_requeue_work() can also re-dispatch rq back
to MQ_RQ_IN_FLIGHT during nvme_tcp_wait_queue(), causing
nvme_tcp_drop_queue_ref() and nvmf_complete_timed_out_request() to strip and
abort the newly retried request.
> +
> + 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);
> }
[Severity: High]
Can nvme_tcp_poll() re-add req to queue->req_list after
nvme_tcp_drop_queue_ref() returns and cause a use-after-free on its bio?
On a poll queue, nvme_tcp_poll() checks NVME_TCP_Q_LIVE locklessly and calls
nvme_tcp_try_recv() directly under lock_sock(sk) outside queue->io_work.
Because cancel_work_sync(&req->queue->io_work) does not wait for
nvme_tcp_poll() and nvme_tcp_drop_queue_ref() runs before lock_sock(sk), a
polling thread processing a buffered R2T PDU can re-queue req right after
send references were dropped:
CPU0 CPU1
nvme_tcp_poll()
/* passes NVME_TCP_Q_LIVE check */
nvme_tcp_try_recv()
lock_sock(sk)
nvme_tcp_handle_r2t()
nvme_find_rq() /* resolves rq */
nvme_tcp_complete_timed_out()
nvmf_retire_timed_out_command(rq)
nvme_tcp_stop_queue()
cancel_work_sync(&queue->io_work)
nvme_tcp_drop_queue_ref(req)
lock_sock(sk) /* blocks */
llist_add(&req->lentry,
&queue->req_list)
queue_work_on(..., &io_work)
release_sock(sk)
/* acquires lock_sock(sk) */
nvmf_complete_timed_out_request(rq) /* frees rq->bio */
release_sock(sk)
nvme_tcp_io_work()
nvme_tcp_try_send_data()
<-- use-after-free
Retiring the command ID and dropping the receive context under lock_sock(sk)
before calling nvme_tcp_drop_queue_ref() would fence nvme_tcp_poll().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204135345.2994739-1-yhlee@isslab.korea.ac.kr?part=2
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 15:08 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 13:53 [PATCH v5 0/2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
2026-12-04 13:53 ` [PATCH v5 1/2] nvme-fabrics: do not leave a timed-out command's id resolvable Yehyeong Lee
2026-12-04 14:28 ` sashiko-bot
2026-12-04 13:53 ` [PATCH v5 2/2] nvme-tcp: do not leave a timed-out request reachable from its queue Yehyeong Lee
2026-12-04 15:06 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox