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

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