Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yehyeong Lee" <yhlee@isslab.korea.ac.kr>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH v2] nvme-tcp: do not refer to a timed-out request after completing it
Date: Mon, 30 Nov 2026 11:17:42 +0000	[thread overview]
Message-ID: <sashiko-outbox-240937@kernel.org> (raw)
In-Reply-To: <20261130104426.2140231-1-yhlee@isslab.korea.ac.kr>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] nvme-tcp: missing queue stop in nvme_tcp_stop_queue_nowait()
  before LIVE
- [High] nvme-tcp: race in nvme_tcp_complete_timed_out() before dropping
  queue ref
--

commit 525e6d2baaccfcee477c6959e45a7456d4788b99
Author: Yehyeong Lee <yhlee@isslab.korea.ac.kr>

nvme-tcp: do not refer to a timed-out request after completing it

This commit clears queue->rd_enabled when stopping a queue and drops the
queue's send references to a timed-out request in
nvme_tcp_complete_timed_out() before completing it.

> diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> index 62a594b8ab9bf..e0a2fa1d8d355 100644
> --- a/drivers/nvme/host/tcp.c
> +++ b/drivers/nvme/host/tcp.c
> @@ -2303,6 +2303,21 @@ static void nvme_tcp_stop_queue_nowait(struct nvme_ctrl *nctrl, int qid)
>  	}
>  
>  	mutex_lock(&queue->queue_lock);
> +
> +	/*
> +	 * Stop receiving, whether or not this queue got as far as LIVE.
> +	 * SHUT_RD below does not discard what the socket has already
> +	 * received, and the receive path nvme-tcp uses - read_sock() -
> +	 * does not look at the shutdown state, so residual bytes stay
> +	 * readable; rd_enabled is what keeps nvme_tcp_recv_skb() from
> +	 * consuming them.  Clearing it here, before anything else, means
> +	 * neither an io_work that is running now nor one armed after the
> +	 * cancel_work_sync() below can resolve a command id on a queue
> +	 * that is being stopped.  nvme_tcp_start_queue() sets it again,
> +	 * next to the receive context, on the next connect.
> +	 */
> +	queue->rd_enabled = false;
> +
>  	if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
>  		__nvme_tcp_stop_queue(queue);
>  	/* Stopping the queue will disable TLS */

[Severity: High]
Does clearing queue->rd_enabled without calling __nvme_tcp_stop_queue()
leave io_work running and queue->sock open when a command times out before
NVME_TCP_Q_LIVE is set?

nvme_tcp_start_queue() enables rd_enabled, installs socket callbacks, and
runs nvmf_connect_admin_queue() or nvmf_connect_io_queue() (including
in-band authentication) before setting NVME_TCP_Q_LIVE.  If a Connect or
auth command times out there, test_and_clear_bit(NVME_TCP_Q_LIVE, ...) is
false, so __nvme_tcp_stop_queue() is skipped until connect returns.

Because nvme_tcp_recv_skb() checks queue->rd_enabled only once at entry
before its while (len) loop, an already running receive or zero-copy send
in io_work is not fenced by cancel_work_sync():

CPU0 (timeout)                    CPU1 (io_work)
nvme_tcp_Start_queue()
  nvmf_connect_admin_queue()
  ...
nvme_tcp_complete_timed_out(rq)
                                  nvme_tcp_recv_skb()
                                    if (!queue->rd_enabled)
                                    while (len) {
  nvme_tcp_stop_queue()
    queue->rd_enabled = false;
    /* NVME_TCP_Q_LIVE not set,
     * __nvme_tcp_stop_queue()
     * skipped
     */
  nvme_tcp_drop_queue_ref(req)
  nvmf_complete_timed_out_request(rq)
    /* rq completed, buffer freed */
                                      nvme_tcp_recv_pdu() /
                                      nvme_tcp_recv_data()
                                        <-- double completion or
                                            use-after-free

In addition, nvme_auth_submit() submits auth commands with
NVME_SUBMIT_RETRY, so completing a timed-out auth request with
NVME_SC_HOST_ABORTED_CMD retries it onto the still-open socket after
nvme_tcp_stop_queue_nowait() has cleared queue->rd_enabled and
queue->tls_enabled.  That bypasses the !nvme_tcp_queue_tls(queue) guard in
nvme_tcp_queue_request() (risking inline kTLS send under blk-mq set->srcu)
and drops the reply because rd_enabled is false, hanging each retry for
the full command timeout.

> @@ -3031,12 +3046,46 @@ static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
>  	nvme_tcp_queue_request(&ctrl->async_req, true);
>  }
>  
> +/*
> + * The timeout handler below completes the request itself, which ends it and
> + * frees its bio.  The queue the request was being sent on can still hold a
> + * reference to it: as queue->request if a send was in progress, or linked on
> + * the queue's send_list or req_list if it was waiting its turn.  Stopping the
> + * queue cancels io_work but drops none of those references, and nothing else
> + * drops them either - only the send path does, and that is what was just shut
> + * down.  If the queue is handed work again the send path picks the request up
> + * and walks the bio this completion is about to free.
> + *
> + * Drop the queue's references to the request before completing it.
> + */
> +static void nvme_tcp_drop_queue_ref(struct nvme_tcp_request *req)
> +{
> +	struct nvme_tcp_queue *queue = req->queue;
> +
> +	mutex_lock(&queue->send_mutex);
> +
> +	if (queue->request == req)
> +		queue->request = NULL;
> +
> +	/*
> +	 * req_list is a lockless list, so a single entry cannot be removed
> +	 * from it; splice it onto send_list first, as the send path does.
> +	 */
> +	nvme_tcp_process_req_list(queue);
> +	if (!list_empty(&req->entry))
> +		list_del_init(&req->entry);
> +	init_llist_node(&req->lentry);
> +
> +	mutex_unlock(&queue->send_mutex);
> +}
> +
>  static void nvme_tcp_complete_timed_out(struct request *rq)
>  {
>  	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
>  	struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
>  
>  	nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
> +	nvme_tcp_drop_queue_ref(req);
>  	nvmf_complete_timed_out_request(rq);
>  }

[Severity: High]
Can nvme_tcp_try_send() fail and complete req between
kernel_sock_shutdown() in nvme_tcp_stop_queue() and
nvme_tcp_drop_queue_ref(req)?

__nvme_tcp_stop_queue() calls kernel_sock_shutdown(queue->sock, SHUT_RDWR)
before cancel_work_sync(&queue->io_work) and without holding
queue->send_mutex, and nvme_tcp_wait_queue() then sleeps for up to 100ms
before nvme_tcp_drop_queue_ref() is reached.  If io_work or a direct send
from nvme_tcp_queue_request() runs during that window while req is still on
the queue, sock_sendmsg() returns -EPIPE and nvme_tcp_fail_request()
completes rq with NVME_SC_HOST_PATH_ERROR:

CPU0 (timeout)                    CPU1 (io_work / direct send)
nvme_tcp_complete_timed_out(rq)
  nvme_tcp_stop_queue()
    kernel_sock_shutdown()
                                  nvme_tcp_try_send()
                                    sock_sendmsg() -> -EPIPE
                                    nvme_tcp_fail_request(req)
                                      nvme_try_complete_req(rq)
    cancel_work_sync()
    nvme_tcp_wait_queue()
  nvme_tcp_drop_queue_ref(req)
                                  /* softirq or sync waiter frees
                                   * or requeues rq -> MQ_RQ_IDLE
                                   */
  nvmf_complete_timed_out_request(rq)
    blk_mq_request_started(rq)
      /* sees MQ_RQ_COMPLETE */
    !blk_mq_request_completed(rq)
      /* sees MQ_RQ_IDLE */
    blk_mq_complete_request(rq)   <-- double completion

If the request is retryable, nvme_complete_rq() can also requeue and
re-dispatch rq during nvme_tcp_wait_queue() (setting rq->state back to
MQ_RQ_IN_FLIGHT), causing nvmf_complete_timed_out_request(rq) to abort the
newly restarted request.  Dropping the queue reference under send_mutex
before shutting down the socket, or preventing nvme_tcp_try_send() from
failing requests once the queue is stopping, would close this window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130104426.2140231-1-yhlee@isslab.korea.ac.kr?part=1


  reply	other threads:[~2026-11-30 11:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  3:15 [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee
2026-11-29  3:46 ` sashiko-bot
2026-11-30 10:44 ` [PATCH v2] nvme-tcp: do not refer to a timed-out request after completing it Yehyeong Lee
2026-11-30 11:17   ` sashiko-bot [this message]
2026-11-30 10:49 ` [PATCH] nvme-tcp: drop the queue's reference to a timed-out request Yehyeong Lee

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-240937@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yhlee@isslab.korea.ac.kr \
    /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