[PATCH] nvme-tcp: set up R2T data transfer in the send path
sashiko-bot at kernel.org
sashiko-bot at kernel.org
Mon Nov 23 15:09:13 PST 2026
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 at 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
More information about the Linux-nvme
mailing list