Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [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

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