From: sashiko-bot@kernel.org
To: "Yehyeong Lee" <yhlee@isslab.korea.ac.kr>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH] nvme-tcp: drop the queue's reference to a timed-out request
Date: Sun, 29 Nov 2026 03:46:13 +0000 [thread overview]
Message-ID: <sashiko-outbox-239302@kernel.org> (raw)
In-Reply-To: <20261129031537.1993449-1-yhlee@isslab.korea.ac.kr>
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] nvme-tcp: residual recv state and genctr in
nvme_tcp_complete_timed_out()
- [High] nvme-tcp: race in nvme_tcp_complete_timed_out() before dropping
queue ref
--
commit 3e4a03d71fdd623bf2fb0e79370c509e4168fd7a
Author: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
nvme-tcp: drop the queue's reference to a timed-out request
This commit drops the queue's send-path references (queue->request,
send_list, and req_list) 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..ca3a63d260ed8 100644
> --- a/drivers/nvme/host/tcp.c
> +++ b/drivers/nvme/host/tcp.c
> @@ -3031,12 +3031,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);
> +}
[Severity: High]
Can the receive path still reference the completed request when io_work
is re-armed on the stopped queue?
Neither __nvme_tcp_stop_queue() nor nvme_tcp_drop_queue_ref() clears
queue->rd_enabled or resets the receive context with
nvme_tcp_init_recv_ctx(), and kernel_sock_shutdown(queue->sock,
SHUT_RDWR) leaves already-queued packets in sk->sk_receive_queue. When a
new request is later dispatched to the stopped queue and re-arms io_work,
nvme_tcp_io_work() reaches nvme_tcp_try_recv():
- If the timed-out command was a read in NVME_TCP_RECV_DATA or
NVME_TCP_RECV_DDGST, nvme_tcp_recv_data() looks up rq directly via
nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id), copies
payload into req->iter / req->curr_bio (writing to the freed bio's
pages), and completes rq a second time on NVME_TCP_F_DATA_SUCCESS.
- If the queue is in NVME_TCP_RECV_PDU, nvmf_complete_timed_out_request()
does not increment nvme_req(rq)->genctr, so nvme_find_rq() still
matches a buffered nvme_tcp_rsp or nvme_tcp_r2t for rq's command_id.
nvme_tcp_process_nvme_cqe() then completes rq a second time, or
nvme_tcp_handle_r2t() sees the reinitialized req->lentry and
req->entry and queues the completed req back onto queue->req_list.
> +
> 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 requeue rq while nvme_tcp_stop_queue()
is shutting down the socket, allowing rq to be re-dispatched onto
queue->req_list before nvmf_complete_timed_out_request() completes it?
timeout work kworker
nvme_tcp_complete_timed_out(rq)
nvme_tcp_stop_queue()
__nvme_tcp_stop_queue()
kernel_sock_shutdown()
nvme_tcp_io_work()
nvme_tcp_try_send()
sock_sendmsg() -> -EPIPE
nvme_tcp_fail_request(req)
...
nvme_retry_req(rq)
blk_mq_requeue_request()
cancel_work_sync()
nvme_tcp_wait_queue()
nvme_tcp_drop_queue_ref(req)
blk_mq_requeue_work()
...
nvme_tcp_queue_rq(rq)
nvme_start_request(rq)
nvme_tcp_queue_request(req)
nvmf_complete_timed_out_request(rq)
blk_mq_complete_request(rq) <-- req still on queue->req_list
When sock_sendmsg() fails on the shut-down socket, nvme_tcp_fail_request()
completes rq with NVME_SC_HOST_PATH_ERROR. For retryable commands that
do not use multipath (such as fabrics auth commands during
NVME_CTRL_CONNECTING, or I/O when multipath is disabled),
__nvme_complete_rq() calls nvme_retry_req(), which resets rq->state to
MQ_RQ_IDLE and clears RQF_TIMED_OUT. Once nvme_tcp_queue_rq()
re-dispatches rq, rq->state is MQ_RQ_IN_FLIGHT again, so
nvmf_complete_timed_out_request() completes rq and frees its bio while
req is back on queue->req_list with io_work armed, causing the same
use-after-free in nvme_tcp_try_send_data() and double completion.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261129031537.1993449-1-yhlee@isslab.korea.ac.kr?part=1
prev parent reply other threads:[~2026-11-29 3:46 UTC|newest]
Thread overview: 2+ 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 [this message]
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-239302@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