* [PATCH v2 0/2] nvme-tcp: fix the R2T send/receive race @ 2026-11-24 5:10 Zhenwen Liang 2026-11-24 5:10 ` [PATCH v2 1/2] nvme-tcp: avoid overflow in the R2T length check Zhenwen Liang 2026-11-24 5:10 ` [PATCH v2 2/2] nvme-tcp: set up R2T data transfer in the send path Zhenwen Liang 0 siblings, 2 replies; 4+ messages in thread From: Zhenwen Liang @ 2026-11-24 5:10 UTC (permalink / raw) To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg Cc: Shao Mingyin, linux-nvme nvme_tcp_handle_r2t() sets up the next H2CData transfer while the send path may still be finishing the previous R2T of the same request. On stable kernels (with the "<" offset check) this leaves pdu_sent wrong and the write stalls until the I/O timeout; since 347d50d92e25 on v7.4-rc4 the host rejects the valid R2T and reconnects. Patch 1/2 fixes a 32-bit overflow in the existing R2T length check. Patch 2/2 moves the R2T setup to the send path. Changes since v1, with the Sashiko review findings: - 32-bit overflow in the length checks -> patch 1/2 (and the new check in 2/2 uses the same form). - Use-after-completion on send error -> 2/2: the send error path no longer completes a request that the receive path has queued again; error recovery handles it. - Race with a second R2T / torn read of the recorded R2T -> 2/2: one R2T at a time (NVME_TCP_REQ_R2T_BUSY), published with NVME_TCP_REQ_R2T_READY. The send path never sets up an R2T before the command PDU has been sent completely; if it finds a recorded R2T for a request whose command PDU has not been sent (NVME_TCP_REQ_CMD_SENT), it treats it as a protocol error. Test results are in the notes of 2/2. I am open to a smaller approach if that is preferred. v1: https://lore.kernel.org/r/20261123225246.1653658-1-zhenwenliang@google.com The problem was found with a Lean-based formal model of the code and AI-assisted analysis. The patches and this text were written with AI assistance and reviewed by a human. Zhenwen Liang (2): nvme-tcp: avoid overflow in the R2T length check nvme-tcp: set up R2T data transfer in the send path drivers/nvme/host/tcp.c | 132 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 122 insertions(+), 10 deletions(-) ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/2] nvme-tcp: avoid overflow in the R2T length check 2026-11-24 5:10 [PATCH v2 0/2] nvme-tcp: fix the R2T send/receive race Zhenwen Liang @ 2026-11-24 5:10 ` Zhenwen Liang 2026-11-24 5:10 ` [PATCH v2 2/2] nvme-tcp: set up R2T data transfer in the send path Zhenwen Liang 1 sibling, 0 replies; 4+ messages in thread From: Zhenwen Liang @ 2026-11-24 5:10 UTC (permalink / raw) To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg Cc: Shao Mingyin, linux-nvme, stable nvme_tcp_handle_r2t() checks the R2T length with if (unlikely(req->data_sent + r2t_length > req->data_len)) data_sent is a size_t, which is 32 bits wide on 32-bit architectures, and r2t_length comes from the controller. A large r2t_length can make the sum wrap around and pass the check. Compare against the remaining length instead; data_sent never exceeds data_len, so the subtraction cannot underflow. The same check was already in the original driver (on pdu_len, then filled from the R2T); commit 1d3ef9c3a39e ("nvme-tcp: validate R2T PDU in nvme_tcp_handle_r2t()") moved it to r2t_length. Reported by the Sashiko review bot on v1 of "nvme-tcp: set up R2T data transfer in the send path". The patch and this description were written with AI assistance and reviewed by a human. Build-tested only (32-bit not tested). Fixes: 3f2304f8c6d6 ("nvme-tcp: add NVMe over TCP host driver") Cc: stable@vger.kernel.org Signed-off-by: Zhenwen Liang <zhenwenliang@google.com> --- drivers/nvme/host/tcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c index 62a594b8ab9b..4fa4b0199dee 100644 --- a/drivers/nvme/host/tcp.c +++ b/drivers/nvme/host/tcp.c @@ -847,7 +847,7 @@ static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue, return -EPROTO; } - if (unlikely(req->data_sent + r2t_length > req->data_len)) { + if (unlikely(r2t_length > req->data_len - req->data_sent)) { dev_err(queue->ctrl->ctrl.device, "req %d r2t len %u exceeded data len %u (%zu sent)\n", rq->tag, r2t_length, req->data_len, req->data_sent); -- 2.56.1.809.ga0b61035b5-goog ^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/2] nvme-tcp: set up R2T data transfer in the send path 2026-11-24 5:10 [PATCH v2 0/2] nvme-tcp: fix the R2T send/receive race Zhenwen Liang 2026-11-24 5:10 ` [PATCH v2 1/2] nvme-tcp: avoid overflow in the R2T length check Zhenwen Liang @ 2026-11-24 5:10 ` Zhenwen Liang 2026-11-24 5:35 ` sashiko-bot 1 sibling, 1 reply; 4+ messages in thread From: Zhenwen Liang @ 2026-11-24 5:10 UTC (permalink / raw) To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg Cc: Shao Mingyin, linux-nvme, stable nvme_tcp_handle_r2t() runs in the receive path and sets up the H2CData transfer directly in the request: it checks the R2T against req->data_sent and calls nvme_tcp_setup_h2c_data_pdu(), which resets req->pdu_sent and req->state. The send path updates req->data_sent and req->pdu_sent only after sock_sendmsg() has returned (nvme_tcp_advance_req(), for every send except the last one of the request). When a controller splits a write into several R2Ts, it can send the next R2T as soon as it has received the last bytes of the previous one. The receive path can then handle that R2T while the sender is still between sock_sendmsg() and nvme_tcp_advance_req() for those bytes. For example, on a poll queue nvme_tcp_poll() receives while io_work sends; the sender can also be the submitting task sending directly from queue_rq() (the tag set is BLK_MQ_F_BLOCKING) while io_work receives: send path receive path (nvme_tcp_poll) --------- ---------------------------- sock_sendmsg(last bytes of R2T) R2T, offset = end of previous R2T data_sent is still the old value setup_h2c_data_pdu(): pdu_sent = 0 nvme_tcp_advance_req() data_sent += ret; pdu_sent += ret nvme_tcp_done_send_req() Commit e371af033c56 ("nvme-tcp: fix incorrect h2cdata pdu offset accounting") already noted that an R2T can arrive before data_sent is updated, and took the H2CData offset from the R2T. But pdu_sent is still reset by the receive path and then advanced by the send path, so the next H2CData PDU carries fewer data bytes than its header says. The controller waits for the rest until the command times out, and the host starts error recovery and reconnects. Since commit 347d50d92e25 ("nvme-tcp: require the R2T offset to match the data sent") the receive path requires r2t_offset == data_sent, so with the old data_sent the host now rejects the valid R2T with "unexpected r2t offset" and starts error recovery instead. Let the receive path only record the R2T, and let the send path, which owns the send state under send_mutex, check it and set up the H2CData PDU when it next takes the request off the list. The R2T is recorded before llist_add(), and nvme_tcp_process_req_list() takes the list with llist_del_all(), so the sender sees it. The exact checks (offset equal to data_sent, length within the request) move to the send path, where data_sent is final. The receive path keeps the "<" offset check and the length check, which cannot reject a valid R2T because data_sent only grows. A bad R2T still starts error recovery, which then cancels the request. Three request flags keep this safe against a controller that does not follow the protocol (we advertise maxr2t = 0, so only one R2T may be outstanding). The receive path accepts an R2T only if it can set NVME_TCP_REQ_R2T_BUSY, so a second outstanding R2T is a protocol error; after it has written the R2T fields it sets NVME_TCP_REQ_R2T_READY, and the send path sets the transfer up only when it sees READY (with acquire ordering), then clears READY and BUSY. The send path never sets up an R2T before the command PDU has been sent completely; if it finds a recorded R2T for a request whose command PDU has not been sent (NVME_TCP_REQ_CMD_SENT), it treats it as a protocol error. If a send fails after the receive path has accepted an R2T and queued the request again, the request is left to the error recovery instead of being completed while it is still on req_list. Reproduced in QEMU with the nvmet-tcp target over loopback, a poll queue and RWF_HIPRI writes, using a debug-only patch, not part of this change, that makes nvmet send one R2T per 16 KiB (stock nvmet sends one R2T per command, so it does not hit this) and delays the host between sock_sendmsg() and nvme_tcp_advance_req() for the last bytes of an R2T. On v7.4-rc4, "unexpected r2t offset" and error recovery were seen in 6 of 15 runs; on v6.18.65, a 30 second I/O timeout and a reconnect were seen in 10 of 15 runs. With this patch (on v7.4-rc4, and backported to v6.18.65) there was no error in 15 of 15 runs on each, although a debug marker showed R2Ts handled during the delay, i.e. inside the window, in 6 and 3 of those runs. With the delay switched off (with the nvmet one-R2T-per-16KiB test helper still applied), the reject still happened in 1 of 10 runs on v7.4-rc4 (QEMU TCG, whose timing does not represent real hardware). The problem was found with a Lean-based formal model of the code and AI-assisted analysis. The patch and this description were written with AI assistance; a human checked them against the source. Fixes: e371af033c56 ("nvme-tcp: fix incorrect h2cdata pdu offset accounting") Fixes: 347d50d92e25 ("nvme-tcp: require the R2T offset to match the data sent") Cc: stable@vger.kernel.org Signed-off-by: Zhenwen Liang <zhenwenliang@google.com> --- Changes in v2 (after the Sashiko review of v1): - Split out the 32-bit overflow fix as patch 1/2 (the check predates this series). - Send error path: do not complete a request that the receive path has already queued again for an R2T; start error recovery instead (Sashiko: use-after-completion on send error). - Accept one R2T at a time per request (NVME_TCP_REQ_R2T_BUSY) and publish the recorded R2T with NVME_TCP_REQ_R2T_READY (set after the fields are written, tested with acquire ordering). The send path never sets up an R2T before the command PDU has been sent completely; if it finds a recorded R2T for a request whose command PDU has not been sent (NVME_TCP_REQ_CMD_SENT), it treats it as a protocol error. This replaces r2t_pending (Sashiko: race with a second R2T, torn read). - Tests in QEMU with KASAN and lockdep, using debug-only fault injection (not part of this patch): * early R2T accepted while the request is being sent, then an injected -EPIPE: with v1 the injection fired in 4 of 12 runs, and in all 4 the request was completed while still on req_list and queued again, followed by an I/O timeout and a hang. With v2 it fired in 20 of 120 runs; in all of them there was no such completion: the send path later picked up the requeued request, rejected the stale R2T with "unexpected r2t offset" and started error recovery; the host reconnected and the follow-up writes succeeded; * an R2T for a write whose command PDU has not been sent yet: with the first v2 draft the H2C transfer was set up before the command PDU in 10 of 10 runs; with this version it is treated as a protocol error and the host recovers in 10 of 10 runs. (This injection is sequential; it shows the ordering problem, not a torn read.) * the target sends an R2T twice: rejected as a protocol error and recovered in 5 of 5 runs (by the existing list or offset checks; the new R2T_BUSY check itself was not hit). - The original reproduction was re-run (numbers in the changelog). KASAN and lockdep reported nothing, apart from a known early-boot KASAN report of the v6.18.65 test image that also occurs without the patch. - Backports: older stable trees without test_bit_acquire() (5.10.y, 5.15.y; it exists since v6.0) need smp_load_acquire() or test_bit() + smp_rmb() in a backport. Trees without 347d50d92e25 keep their "<" offset check in the receive path. The v6.18.65 test backport used test_bit_acquire() as is. - If a smaller approach is preferred, I am happy to rework it. v1: https://lore.kernel.org/r/20261123225246.1653658-1-zhenwenliang@google.com drivers/nvme/host/tcp.c | 132 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 122 insertions(+), 10 deletions(-) diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c index 4fa4b0199dee..b7a44b56b2f5 100644 --- a/drivers/nvme/host/tcp.c +++ b/drivers/nvme/host/tcp.c @@ -93,6 +93,19 @@ enum nvme_tcp_send_state { NVME_TCP_SEND_DDGST, }; +enum nvme_tcp_req_flags { + /* + * Set by the receive path when it accepts an R2T and cleared by the + * send path once it has taken the R2T over; also set when the send + * path fails the request. No R2T is accepted while it is set. + */ + NVME_TCP_REQ_R2T_BUSY = 0, + /* The accepted R2T is recorded in r2t_* and can be set up. */ + NVME_TCP_REQ_R2T_READY = 1, + /* The command PDU has been sent completely. */ + NVME_TCP_REQ_CMD_SENT = 2, +}; + struct nvme_tcp_request { struct nvme_request req; void *pdu; @@ -103,6 +116,11 @@ struct nvme_tcp_request { u32 h2cdata_left; u32 h2cdata_offset; u16 ttag; + /* R2T accepted by the receive path, see NVME_TCP_REQ_R2T_BUSY */ + unsigned long flags; + u16 r2t_ttag; + u32 r2t_offset; + u32 r2t_length; __le16 status; struct list_head entry; struct llist_node lentry; @@ -823,6 +841,7 @@ static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue, struct request *rq; u32 r2t_length = le32_to_cpu(pdu->r2t_length); u32 r2t_offset = le32_to_cpu(pdu->r2t_offset); + size_t data_sent; rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id); if (!rq) { @@ -847,17 +866,25 @@ static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue, return -EPROTO; } - if (unlikely(r2t_length > req->data_len - req->data_sent)) { + /* + * The send path may not have accounted the last bytes of the + * previous R2T yet, so data_sent can still be too small here (it is + * written by the send path, hence READ_ONCE()). data_sent only + * grows, so these checks never reject a valid R2T; + * nvme_tcp_setup_r2t() checks the R2T exactly. + */ + data_sent = READ_ONCE(req->data_sent); + if (unlikely(r2t_length > req->data_len - data_sent)) { dev_err(queue->ctrl->ctrl.device, "req %d r2t len %u exceeded data len %u (%zu sent)\n", - rq->tag, r2t_length, req->data_len, req->data_sent); + rq->tag, r2t_length, req->data_len, data_sent); return -EPROTO; } - if (unlikely(r2t_offset != req->data_sent)) { + if (unlikely(r2t_offset < data_sent)) { dev_err(queue->ctrl->ctrl.device, "req %d unexpected r2t offset %u (expected %zu)\n", - rq->tag, r2t_offset, req->data_sent); + rq->tag, r2t_offset, data_sent); return -EPROTO; } @@ -869,12 +896,28 @@ static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue, return -EPROTO; } - req->pdu_len = 0; - req->h2cdata_left = r2t_length; - req->h2cdata_offset = r2t_offset; - req->ttag = pdu->ttag; + /* Only one R2T may be outstanding (we advertise maxr2t = 0). */ + if (test_and_set_bit(NVME_TCP_REQ_R2T_BUSY, &req->flags)) { + dev_err(queue->ctrl->ctrl.device, + "req %d unexpected r2t while another r2t is pending\n", + rq->tag); + return -EPROTO; + } - nvme_tcp_setup_h2c_data_pdu(req); + /* + * The controller can send this R2T as soon as it has the last bytes + * of the previous one, while nvme_tcp_try_send_data() has not yet + * updated data_sent and pdu_sent for them. Do not touch the send + * state here; record the R2T and let nvme_tcp_try_send() set it up + * when it takes the request off the list. llist_add() orders these + * stores before the request is visible on req_list. + */ + req->r2t_ttag = pdu->ttag; + req->r2t_offset = r2t_offset; + req->r2t_length = r2t_length; + /* Pairs with test_bit_acquire() in nvme_tcp_try_send() */ + smp_mb__before_atomic(); + set_bit(NVME_TCP_REQ_R2T_READY, &req->flags); llist_add(&req->lentry, &queue->req_list); queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); @@ -1320,6 +1363,7 @@ static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req) len -= ret; if (!len) { + set_bit(NVME_TCP_REQ_CMD_SENT, &req->flags); if (inline_data) { req->state = NVME_TCP_SEND_DATA; if (queue->data_digest) @@ -1401,20 +1445,77 @@ static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req) return -EAGAIN; } +static int nvme_tcp_setup_r2t(struct nvme_tcp_request *req) +{ + struct nvme_tcp_queue *queue = req->queue; + struct request *rq = blk_mq_rq_from_pdu(req); + + if (unlikely(!test_bit(NVME_TCP_REQ_CMD_SENT, &req->flags))) { + dev_err(queue->ctrl->ctrl.device, + "req %d unexpected r2t before the command was sent\n", + rq->tag); + return -EPROTO; + } + + if (unlikely(req->r2t_length > req->data_len - req->data_sent)) { + dev_err(queue->ctrl->ctrl.device, + "req %d r2t len %u exceeded data len %u (%zu sent)\n", + rq->tag, req->r2t_length, req->data_len, req->data_sent); + return -EPROTO; + } + + if (unlikely(req->r2t_offset != req->data_sent)) { + dev_err(queue->ctrl->ctrl.device, + "req %d unexpected r2t offset %u (expected %zu)\n", + rq->tag, req->r2t_offset, req->data_sent); + return -EPROTO; + } + + req->pdu_len = 0; + req->h2cdata_left = req->r2t_length; + req->h2cdata_offset = req->r2t_offset; + req->ttag = req->r2t_ttag; + /* + * The R2T is copied; accept the next one. Pairs with + * test_and_set_bit() in nvme_tcp_handle_r2t(). On error the bits + * stay set, so no further R2T is accepted for this request. + */ + clear_bit(NVME_TCP_REQ_R2T_READY, &req->flags); + clear_bit_unlock(NVME_TCP_REQ_R2T_BUSY, &req->flags); + + nvme_tcp_setup_h2c_data_pdu(req); + return 0; +} + static int nvme_tcp_try_send(struct nvme_tcp_queue *queue) { struct nvme_tcp_request *req; unsigned int noreclaim_flag; + bool fetched = false; int ret = 1; if (!queue->request) { queue->request = nvme_tcp_fetch_request(queue); if (!queue->request) return 0; + fetched = true; } req = queue->request; noreclaim_flag = memalloc_noreclaim_save(); + if (fetched && test_bit_acquire(NVME_TCP_REQ_R2T_READY, &req->flags)) { + ret = nvme_tcp_setup_r2t(req); + if (unlikely(ret)) { + /* + * Handle a bad R2T as the receive path does: leave + * the request to the error recovery. + */ + nvme_tcp_done_send_req(queue); + nvme_tcp_error_recovery(&queue->ctrl->ctrl); + goto out; + } + } + if (req->state == NVME_TCP_SEND_CMD_PDU) { ret = nvme_tcp_try_send_cmd_pdu(req); if (ret <= 0) @@ -1443,7 +1544,16 @@ static int nvme_tcp_try_send(struct nvme_tcp_queue *queue) } else if (ret < 0) { dev_err(queue->ctrl->ctrl.device, "failed to send request %d\n", ret); - nvme_tcp_fail_request(queue->request); + /* + * If the receive path has already accepted an R2T, the request + * is queued again and must not be completed here; leave it to + * the error recovery. Otherwise the bit now blocks later R2Ts. + */ + if (test_and_set_bit(NVME_TCP_REQ_R2T_BUSY, + &queue->request->flags)) + nvme_tcp_error_recovery(&queue->ctrl->ctrl); + else + nvme_tcp_fail_request(queue->request); nvme_tcp_done_send_req(queue); } out: @@ -3022,6 +3132,7 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg) nvme_tcp_set_sg_null(cmd); ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU; + ctrl->async_req.flags = 0; ctrl->async_req.offset = 0; ctrl->async_req.curr_bio = NULL; ctrl->async_req.data_len = 0; @@ -3120,6 +3231,7 @@ static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns, req->pdu_len = 0; req->pdu_sent = 0; req->h2cdata_left = 0; + req->flags = 0; req->data_len = blk_rq_nr_phys_segments(rq) ? blk_rq_payload_bytes(rq) : 0; req->curr_bio = rq->bio; -- 2.56.1.809.ga0b61035b5-goog ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2 2/2] nvme-tcp: set up R2T data transfer in the send path 2026-11-24 5:10 ` [PATCH v2 2/2] nvme-tcp: set up R2T data transfer in the send path Zhenwen Liang @ 2026-11-24 5:35 ` sashiko-bot 0 siblings, 0 replies; 4+ messages in thread From: sashiko-bot @ 2026-11-24 5:35 UTC (permalink / raw) To: Zhenwen Liang; +Cc: linux-nvme Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] nvme-tcp: use-after-completion in nvme_tcp_try_send_cmd_pdu() on req->flags - [High] nvme-tcp: early R2T in nvme_tcp_handle_r2t() corrupts req_list or bypasses CMD_SENT - [High] nvme-tcp: clearing R2T_BUSY on final R2T allows duplicate or inline-write R2Ts - [High] nvme-tcp: deferred R2T check lets nvme_tcp_process_nvme_cqe() complete queued req - [Medium] nvme-tcp: clearing R2T_BUSY in nvme_tcp_setup_r2t() allows concurrent R2Ts - [Medium] nvme-tcp: send failure in nvme_tcp_try_send() leaves R2T_READY on queued request -- commit 323f0b4025551d9c40345790574456e643351f07 Author: Zhenwen Liang <zhenwenliang@google.com> nvme-tcp: set up R2T data transfer in the send path This commit records incoming R2T PDUs in struct nvme_tcp_request on the receive path and defers exact offset validation and H2CData PDU setup to the send path under send_mutex. > diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c > index 4fa4b0199deef..b7a44b56b2f53 100644 > --- a/drivers/nvme/host/tcp.c > +++ b/drivers/nvme/host/tcp.c [ ... ] > @@ -847,17 +866,25 @@ static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue, > return -EPROTO; > } > > - if (unlikely(r2t_length > req->data_len - req->data_sent)) { > + /* > + * The send path may not have accounted the last bytes of the > + * previous R2T yet, so data_sent can still be too small here (it is > + * written by the send path, hence READ_ONCE()). data_sent only > + * grows, so these checks never reject a valid R2T; > + * nvme_tcp_setup_r2t() checks the R2T exactly. > + */ > + data_sent = READ_ONCE(req->data_sent); > + if (unlikely(r2t_length > req->data_len - data_sent)) { > dev_err(queue->ctrl->ctrl.device, > "req %d r2t len %u exceeded data len %u (%zu sent)\n", > - rq->tag, r2t_length, req->data_len, req->data_sent); > + rq->tag, r2t_length, req->data_len, data_sent); > return -EPROTO; > } > > - if (unlikely(r2t_offset != req->data_sent)) { > + if (unlikely(r2t_offset < data_sent)) { > dev_err(queue->ctrl->ctrl.device, > "req %d unexpected r2t offset %u (expected %zu)\n", > - rq->tag, r2t_offset, req->data_sent); > + rq->tag, r2t_offset, data_sent); > return -EPROTO; > } > > @@ -869,12 +896,28 @@ static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue, > return -EPROTO; > } > > - req->pdu_len = 0; > - req->h2cdata_left = r2t_length; > - req->h2cdata_offset = r2t_offset; > - req->ttag = pdu->ttag; > + /* Only one R2T may be outstanding (we advertise maxr2t = 0). */ > + if (test_and_set_bit(NVME_TCP_REQ_R2T_BUSY, &req->flags)) { > + dev_err(queue->ctrl->ctrl.device, > + "req %d unexpected r2t while another r2t is pending\n", > + rq->tag); > + return -EPROTO; > + } [Severity: High] Can an R2T arriving before the command PDU is sent corrupt queue->req_list or bypass the NVME_TCP_REQ_CMD_SENT check? nvme_tcp_setup_cmd_pdu() initializes req->flags = 0 (with NVME_TCP_REQ_R2T_BUSY clear), and nvme_tcp_handle_r2t() does not check NVME_TCP_REQ_CMD_SENT. In nvme_tcp_queue_rq(), nvme_start_request(rq) makes rq visible to nvme_find_rq() before nvme_tcp_queue_request() adds req->lentry to queue->req_list: CPU0 CPU1 nvme_tcp_queue_rq() nvme_start_request(rq) nvme_tcp_handle_r2t() nvme_find_rq() -> rq llist_on_list(&req->lentry) == false test_and_set_bit(R2T_BUSY) == false llist_add(&req->lentry, &queue->req_list) nvme_tcp_queue_request() llist_add(&req->lentry, &queue->req_list) <-- duplicate llist_add() cycles req_list A second window opens after nvme_tcp_fetch_request() pops req into queue->request and clears req->entry and req->lentry. If an R2T arrives while nvme_tcp_try_send_cmd_pdu() is sending the command PDU (or waiting to retry after -EAGAIN), nvme_tcp_handle_r2t() accepts it and queues req onto queue->req_list. When nvme_tcp_try_send_cmd_pdu() then finishes sending the command PDU, it sets NVME_TCP_REQ_CMD_SENT, so the later nvme_tcp_setup_r2t() sees NVME_TCP_REQ_CMD_SENT set (and for inline-data writes req remains active in queue->request in NVME_TCP_SEND_DATA while also queued on queue->req_list). > > - nvme_tcp_setup_h2c_data_pdu(req); > + /* > + * The controller can send this R2T as soon as it has the last bytes > + * of the previous one, while nvme_tcp_try_send_data() has not yet > + * updated data_sent and pdu_sent for them. Do not touch the send > + * state here; record the R2T and let nvme_tcp_try_send() set it up > + * when it takes the request off the list. llist_add() orders these > + * stores before the request is visible on req_list. > + */ > + req->r2t_ttag = pdu->ttag; > + req->r2t_offset = r2t_offset; > + req->r2t_length = r2t_length; > + /* Pairs with test_bit_acquire() in nvme_tcp_try_send() */ > + smp_mb__before_atomic(); > + set_bit(NVME_TCP_REQ_R2T_READY, &req->flags); > > llist_add(&req->lentry, &queue->req_list); > queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); [Severity: High] Because nvme_tcp_handle_r2t() defers the r2t_offset != req->data_sent check to the send path, it now queues req onto queue->req_list and returns 0 for an R2T with r2t_offset > data_sent. Can a subsequent CQE on the receive path complete rq while req->lentry is still on queue->req_list? When nvme_tcp_handle_r2t() returns 0, nvme_tcp_recv_skb() keeps queue->rd_enabled set and continues processing PDUs. If the controller follows the bad R2T with a response CQE before the send path runs nvme_tcp_setup_r2t(), nvme_tcp_process_nvme_cqe() completes rq while req is still linked on queue->req_list or queue->send_list. > @@ -1320,6 +1363,7 @@ static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req) > > len -= ret; > if (!len) { > + set_bit(NVME_TCP_REQ_CMD_SENT, &req->flags); > if (inline_data) { > req->state = NVME_TCP_SEND_DATA; > if (queue->data_digest) [Severity: High] Can set_bit(NVME_TCP_REQ_CMD_SENT, &req->flags) modify req after the request has already been completed and reallocated? For any command with !inline_data, the command PDU is the final bytes sent before the controller can reply with a CQE: CPU0 (send path) CPU1 (receive / submit) nvme_tcp_try_send_cmd_pdu() sock_sendmsg() nvme_tcp_process_nvme_cqe() nvme_complete_rq(rq) ... nvme_tcp_setup_cmd_pdu() req->flags = 0; set_bit(NVME_TCP_REQ_CMD_SENT, &req->flags) <-- overwrites new request's flags Setting NVME_TCP_REQ_CMD_SENT on the reallocated request before its own command PDU is sent defeats the NVME_TCP_REQ_CMD_SENT check for that new request. > @@ -1401,20 +1445,77 @@ static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req) > return -EAGAIN; > } > > +static int nvme_tcp_setup_r2t(struct nvme_tcp_request *req) > +{ > + struct nvme_tcp_queue *queue = req->queue; > + struct request *rq = blk_mq_rq_from_pdu(req); > + > + if (unlikely(!test_bit(NVME_TCP_REQ_CMD_SENT, &req->flags))) { > + dev_err(queue->ctrl->ctrl.device, > + "req %d unexpected r2t before the command was sent\n", > + rq->tag); > + return -EPROTO; > + } > + > + if (unlikely(req->r2t_length > req->data_len - req->data_sent)) { > + dev_err(queue->ctrl->ctrl.device, > + "req %d r2t len %u exceeded data len %u (%zu sent)\n", > + rq->tag, req->r2t_length, req->data_len, req->data_sent); > + return -EPROTO; > + } > + > + if (unlikely(req->r2t_offset != req->data_sent)) { > + dev_err(queue->ctrl->ctrl.device, > + "req %d unexpected r2t offset %u (expected %zu)\n", > + rq->tag, req->r2t_offset, req->data_sent); > + return -EPROTO; > + } > + > + req->pdu_len = 0; > + req->h2cdata_left = req->r2t_length; > + req->h2cdata_offset = req->r2t_offset; > + req->ttag = req->r2t_ttag; > + /* > + * The R2T is copied; accept the next one. Pairs with > + * test_and_set_bit() in nvme_tcp_handle_r2t(). On error the bits > + * stay set, so no further R2T is accepted for this request. > + */ > + clear_bit(NVME_TCP_REQ_R2T_READY, &req->flags); > + clear_bit_unlock(NVME_TCP_REQ_R2T_BUSY, &req->flags); [Severity: Medium] Does clearing NVME_TCP_REQ_R2T_BUSY here allow a second R2T to be accepted while the first R2T is still being sent? nvme_tcp_fetch_request() has already cleared req->entry and req->lentry, and nvme_tcp_setup_r2t() clears NVME_TCP_REQ_R2T_BUSY before any H2CData PDU bytes are transmitted. While req remains in queue->request sending the first R2T (for example, across multiple H2CData PDUs or -EAGAIN retries), both the list checks and test_and_set_bit(NVME_TCP_REQ_R2T_BUSY) in nvme_tcp_handle_r2t() will pass for a second R2T with r2t_offset at the end of the first R2T, queuing req onto queue->req_list while it is still active in queue->request. > + > + nvme_tcp_setup_h2c_data_pdu(req); > + return 0; > +} [Severity: High] What prevents a duplicate final R2T or an R2T on an inline-data write from being accepted and re-sending data after all payload bytes have already been transmitted? In nvme_tcp_try_send_data(), nvme_tcp_advance_req() is skipped on the final payload send of a request: drivers/nvme/host/tcp.c:nvme_tcp_try_send_data() { ... if (req_data_sent + ret < req_data_len) nvme_tcp_advance_req(req, ret); ... } Because req->data_sent is not updated on the last send, and nvme_tcp_setup_r2t() clears NVME_TCP_REQ_R2T_BUSY even when the R2T covers the remainder of the request (req->r2t_length == req->data_len - req->data_sent), a single-R2T write (or an inline write, which never sets NVME_TCP_REQ_R2T_BUSY) finishes sending with req->data_sent == 0, NVME_TCP_REQ_CMD_SENT == 1, and NVME_TCP_REQ_R2T_BUSY == 0. A duplicate R2T with r2t_offset == 0 then passes both nvme_tcp_handle_r2t() and nvme_tcp_setup_r2t(), re-queuing req onto queue->req_list while the request can complete concurrently on the receive path. [ ... ] > @@ -1443,7 +1544,16 @@ static int nvme_tcp_try_send(struct nvme_tcp_queue *queue) > } else if (ret < 0) { > dev_err(queue->ctrl->ctrl.device, > "failed to send request %d\n", ret); > - nvme_tcp_fail_request(queue->request); > + /* > + * If the receive path has already accepted an R2T, the request > + * is queued again and must not be completed here; leave it to > + * the error recovery. Otherwise the bit now blocks later R2Ts. > + */ > + if (test_and_set_bit(NVME_TCP_REQ_R2T_BUSY, > + &queue->request->flags)) > + nvme_tcp_error_recovery(&queue->ctrl->ctrl); > + else > + nvme_tcp_fail_request(queue->request); > nvme_tcp_done_send_req(queue); > } [Severity: Medium] When test_and_set_bit(NVME_TCP_REQ_R2T_BUSY, &queue->request->flags) sees that the receive path has already queued req onto queue->req_list, can a subsequent nvme_tcp_try_send() pick req back up before err_work stops the queue? nvme_tcp_done_send_req() clears queue->request while req remains on queue->req_list with NVME_TCP_REQ_R2T_READY set, and nvme_tcp_io_work() re-queues queue->io_work after breaking out of its send loop on result < 0. Before asynchronous error recovery stops the queue, the next nvme_tcp_try_send() fetches req and calls nvme_tcp_setup_r2t(req). If req->r2t_offset == req->data_sent (for example, when sending the data digest failed in nvme_tcp_try_send_ddgst() after nvme_tcp_try_send_data() already advanced req->data_sent for the payload), nvme_tcp_setup_r2t() succeeds, clears NVME_TCP_REQ_R2T_BUSY, and resumes sending the failed request; if that send also fails, test_and_set_bit(NVME_TCP_REQ_R2T_BUSY) is now false and nvme_tcp_fail_request() completes the request. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261124051041.3099796-1-zhenwenliang@google.com?part=2 ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-24 5:35 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-24 5:10 [PATCH v2 0/2] nvme-tcp: fix the R2T send/receive race Zhenwen Liang 2026-11-24 5:10 ` [PATCH v2 1/2] nvme-tcp: avoid overflow in the R2T length check Zhenwen Liang 2026-11-24 5:10 ` [PATCH v2 2/2] nvme-tcp: set up R2T data transfer in the send path Zhenwen Liang 2026-11-24 5: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