All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Zhenwen Liang" <zhenwenliang@google.com>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH v2 2/2] nvme-tcp: set up R2T data transfer in the send path
Date: Tue, 24 Nov 2026 05:35:47 +0000	[thread overview]
Message-ID: <sashiko-outbox-230556@kernel.org> (raw)
In-Reply-To: <20261124051041.3099796-3-zhenwenliang@google.com>

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


  reply	other threads:[~2026-11-24  5:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]
2026-11-24 17:22     ` Zhenwen Liang
2026-11-24 17:31 ` (subset) [PATCH v2 0/2] nvme-tcp: fix the R2T send/receive race Keith Busch
     [not found]   ` <CAE3ZW7uKLR-MU7taHU+SCTHjWn_NDx22Xf_wRsrezAt3y8F3GQ@mail.gmail.com>
     [not found]     ` <CAE3ZW7tUK5CSccxMPv8kFc=m3gBBYcT_+hmkJq+OPZxEvth9kw@mail.gmail.com>
2026-11-30 15:45       ` 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-230556@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.