[PATCH v2] nvme-tcp: do not refer to a timed-out request after completing it

sashiko-bot at kernel.org sashiko-bot at kernel.org
Mon Nov 30 03:17:42 PST 2026


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 at 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



More information about the Linux-nvme mailing list