[PATCH v4 2/2] nvme-fabrics: do not leave a timed-out command's id resolvable

Yehyeong Lee yhlee at isslab.korea.ac.kr
Wed Dec 2 05:19:09 PST 2026


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 at vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee at 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




More information about the Linux-nvme mailing list