From: sashiko-bot@kernel.org
To: "Zhenwen Liang" <zhenwenliang@google.com>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH] nvme-tcp: set up R2T data transfer in the send path
Date: Mon, 23 Nov 2026 23:09:13 +0000 [thread overview]
Message-ID: <sashiko-outbox-230185@kernel.org> (raw)
In-Reply-To: <20261123225246.1653658-1-zhenwenliang@google.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] nvme-tcp: r2t_pending race in nvme_tcp_handle_r2t() and
nvme_tcp_setup_r2t()
- [Medium] nvme-tcp: 32-bit overflow in r2t_length check in
nvme_tcp_setup_r2t()
- [Medium] nvme-tcp: use-after-completion on send error after
nvme_tcp_handle_r2t()
--
commit 7be091feb52782cced17a5c0c1c30a0e41652084
Author: Zhenwen Liang <zhenwenliang@google.com>
nvme-tcp: set up R2T data transfer in the send path
This commit defers H2CData PDU setup and exact R2T validation from
nvme_tcp_handle_r2t() in the receive path to nvme_tcp_try_send() in the
send path so that R2T handling does not race with req->data_sent and
req->pdu_sent updates after sock_sendmsg().
> diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> index 62a594b8ab9bf..28a5d6da8f18c 100644
> --- a/drivers/nvme/host/tcp.c
> +++ b/drivers/nvme/host/tcp.c
[ ... ]
> @@ -847,17 +853,25 @@ static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
> return -EPROTO;
> }
>
> - if (unlikely(req->data_sent + r2t_length > req->data_len)) {
> + /*
> + * 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(data_sent + r2t_length > req->data_len)) {
> 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 +883,18 @@ 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;
> -
> - 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.
> + */
> + WRITE_ONCE(req->r2t_ttag, pdu->ttag);
> + WRITE_ONCE(req->r2t_offset, r2t_offset);
> + WRITE_ONCE(req->r2t_length, r2t_length);
> + req->r2t_pending = true;
>
> llist_add(&req->lentry, &queue->req_list);
> queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
[Severity: Medium]
Can an early R2T leave a completed request linked on queue->req_list when a
send fails?
While req is actively being sent as queue->request, both req->lentry and
req->entry are empty. Relaxing the offset check to r2t_offset < data_sent
allows an R2T with r2t_offset >= data_sent to pass while req is still
mid-send (for instance paused across -EAGAIN or sending its data digest) and
queue req->lentry onto queue->req_list. If the active send then fails with
an error other than -EAGAIN, nvme_tcp_try_send() completes the request via
nvme_tcp_fail_request(queue->request) while req->lentry is still on
queue->req_list:
CPU0 (send path) CPU1 (recv path)
nvme_tcp_try_send()
queue->request =
nvme_tcp_fetch_request(queue)
nvme_tcp_handle_r2t()
r2t_offset >= data_sent
llist_add(&req->lentry,
&queue->req_list)
nvme_tcp_try_send_data(req)
sock_sendmsg() -> -EPIPE
nvme_tcp_fail_request(req)
nvme_tcp_end_request(rq, ...)
nvme_tcp_process_req_list()
<-- processes completed req
When queue->req_list is next drained, the completed request is processed
again, or if its tag was reallocated and re-queued, queue->req_list forms a
cycle.
> @@ -1401,20 +1421,72 @@ 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);
> + /*
> + * Read the recorded R2T once: a controller that violates the protocol
> + * could send another R2T and rewrite these fields meanwhile.
> + */
> + u32 r2t_length = READ_ONCE(req->r2t_length);
> + u32 r2t_offset = READ_ONCE(req->r2t_offset);
> + u16 r2t_ttag = READ_ONCE(req->r2t_ttag);
> +
> + req->r2t_pending = false;
[Severity: Medium]
Can nvme_tcp_setup_r2t() race with a concurrent nvme_tcp_handle_r2t() and
clear req->r2t_pending after req has been re-added to queue->req_list?
nvme_tcp_fetch_request() clears req->entry and req->lentry before
nvme_tcp_setup_r2t() runs, and nvme_tcp_handle_r2t() only checks
llist_on_list(&req->lentry) || !list_empty(&req->entry) (not
req->r2t_pending or req->state == NVME_TCP_SEND_CMD_PDU). If a non-conforming
controller sends another R2T in that window, nvme_tcp_handle_r2t() can
overwrite the r2t_* fields and re-queue req on queue->req_list right before
nvme_tcp_setup_r2t() sets req->r2t_pending = false:
CPU0 (send path) CPU1 (recv path)
nvme_tcp_try_send()
nvme_tcp_fetch_request()
list_del_init(&req->entry)
init_llist_node(&req->lentry)
nvme_tcp_handle_r2t()
WRITE_ONCE(req->r2t_*, ...)
req->r2t_pending = true
llist_add(&req->lentry,
&queue->req_list)
nvme_tcp_setup_r2t()
req->r2t_pending = false <-- clobbers pending flag for queued req
Besides reading a torn combination of r2t_length, r2t_offset, and r2t_ttag
or clobbering NVME_TCP_SEND_CMD_PDU before the initial command PDU is sent,
the second fetch from queue->req_list sees req->r2t_pending == false, skips
nvme_tcp_setup_r2t(), and re-enters the send state machine in the stale
state of the finished transfer.
> +
> + if (unlikely(req->data_sent + r2t_length > req->data_len)) {
> + 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);
> + return -EPROTO;
> + }
[Severity: Medium]
Can req->data_sent + r2t_length wrap around on 32-bit architectures?
On 32-bit kernels size_t is 32 bits, so adding u32 r2t_length to
req->data_sent (and to data_sent in nvme_tcp_handle_r2t()) can wrap around
when req->data_sent > 0 and bypass the req->data_len check.
nvme_tcp_setup_h2c_data_pdu() then sets req->pdu_len larger than the
remaining payload bytes in the request; once nvme_tcp_try_send_data() sends
the last actual payload byte (req_data_sent + ret == req_data_len), it stops
calling nvme_tcp_advance_req(), so req->pdu_sent stops advancing and the
send loop spins indefinitely.
Checking r2t_length > req->data_len - req->data_sent (after verifying
req->data_sent <= req->data_len) avoids the overflow.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123225246.1653658-1-zhenwenliang@google.com?part=1
next prev parent reply other threads:[~2026-11-23 23:09 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-13 18:02 [PATCH v2 0/6] nvme: fix transport request and queue error handling shao.mingyin
2026-11-13 18:02 ` [PATCH v2 1/6] nvme-loop: release the target request when SG setup fails shao.mingyin
2026-11-13 18:02 ` [PATCH v2 2/6] nvmet-tcp: only wait for in-capsule data for inline SGLs shao.mingyin
2026-11-13 18:02 ` [PATCH v2 3/6] nvme-tcp: require the R2T offset to match the data sent shao.mingyin
2026-11-23 22:52 ` [PATCH] nvme-tcp: set up R2T data transfer in the send path Zhenwen Liang
2026-11-23 23:09 ` sashiko-bot [this message]
2026-11-13 18:02 ` [PATCH v2 4/6] nvmet-fcloop: free the LS request when delivery fails shao.mingyin
2026-11-13 18:02 ` [PATCH v2 5/6] nvmet-fc: reject duplicate queue IDs on Create Connection shao.mingyin
2026-11-13 18:02 ` [PATCH v2 6/6] nvmet: roll back queue bindings on connection failure shao.mingyin
2026-11-16 16:18 ` [PATCH v2 0/6] nvme: fix transport request and queue error handling Keith Busch
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-230185@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=zhenwenliang@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox