mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Seiji Nishikawa <snishika@redhat.com>
To: Keith Busch <kbusch@kernel.org>, Jens Axboe <axboe@kernel.dk>,
	Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>
Cc: Hannes Reinecke <hare@suse.de>, Chris Leech <cleech@redhat.com>,
	Seiji Nishikawa <snishika@redhat.com>,
	linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH] nvme-tcp: fix use of a released socket in nvme_tcp_poll()
Date: Thu, 26 Nov 2026 17:56:53 +0900	[thread overview]
Message-ID: <20261126085653.3339227-1-snishika@redhat.com> (raw)

A controller reset while polled I/O is running crashes the kernel:

  BUG: kernel NULL pointer dereference, address: 0000000000000018
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page
  Oops: Oops: 0000 [#1] SMP PTI
  CPU: 2 UID: 0 PID: 2585 Comm: fio
  RIP: 0010:nvme_tcp_poll+0x14/0xd0 [nvme_tcp]
  Call Trace:
   <TASK>
   blk_mq_poll+0x6e/0xc0
   bio_poll+0x8e/0x140
   iocb_bio_iopoll+0x2e/0x50
   io_do_iopoll+0xda/0x470
   __se_sys_io_uring_enter+0x43f/0x7a0
   do_syscall_64+0xe9/0x340
   entry_SYSCALL_64_after_hwframe+0x76/0x7e

nvme_tcp_poll() reads queue->sock->sk before it checks NVME_TCP_Q_LIVE.
The reset stops the queue and then nvme_tcp_free_queue() releases the
socket and sets queue->sock to NULL. Nothing keeps nvme_tcp_poll() out
of the queue while this happens, so it reads a NULL queue->sock.
Checking NVME_TCP_Q_LIVE before the read of queue->sock does not suffice
because the socket can be released after the check.

In the vmcore of the crash above, the controller is in
NVME_CTRL_CONNECTING, and the queue that was polled has sock == NULL and
flags == 0. The reset work has freed all I/O queues and waits for the
admin queue connect command.

The crash is always reproducible by resetting the controller while
polled I/O is running on a poll queue. For example, connect with
--nr-poll-queues=4, run fio with --ioengine=io_uring --hipri on the
device, and run "nvme reset" every 10 seconds.

Take queue_lock in nvme_tcp_poll() and check NVME_TCP_Q_LIVE under it
before using the socket. nvme_tcp_stop_queue_nowait() clears
NVME_TCP_Q_LIVE under queue_lock, and nvme_tcp_free_queue() releases the
socket under queue_lock. So nvme_tcp_poll() sees either NVME_TCP_Q_LIVE
cleared or a valid socket, and the reset waits until nvme_tcp_poll()
drops the lock. nvme_tcp_get_address() already uses queue_lock in the
same way.

Use mutex_trylock() so that nvme_tcp_poll() does not sleep on the lock.
If the trylock fails, nvme_tcp_poll() returns 0 and the caller polls
again.

Fixes: 1a9460cef571 ("nvme-tcp: support simple polling")
Signed-off-by: Seiji Nishikawa <snishika@redhat.com>
---
 drivers/nvme/host/tcp.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 62a594b..dbd9bdc 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -3196,18 +3196,32 @@ static void nvme_tcp_map_queues(struct blk_mq_tag_set *set)
 static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
 {
 	struct nvme_tcp_queue *queue = hctx->driver_data;
-	struct sock *sk = queue->sock->sk;
-	int ret;
+	struct sock *sk;
+	int ret = 0;
 
-	if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
+	/*
+	 * A controller reset stops the queue and releases the socket under
+	 * queue_lock. Hold queue_lock so that the socket stays valid while it
+	 * is used here. Do not sleep on the lock. If the trylock fails, return
+	 * 0 and the caller polls again.
+	 */
+	if (!mutex_trylock(&queue->queue_lock))
 		return 0;
 
+	if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
+		goto out_unlock;
+
+	sk = queue->sock->sk;
 	set_bit(NVME_TCP_Q_POLLING, &queue->flags);
 	if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
 		sk_busy_loop(sk, true);
 	ret = nvme_tcp_try_recv(queue);
 	clear_bit(NVME_TCP_Q_POLLING, &queue->flags);
-	return ret < 0 ? ret : queue->nr_cqe;
+	if (ret >= 0)
+		ret = queue->nr_cqe;
+out_unlock:
+	mutex_unlock(&queue->queue_lock);
+	return ret;
 }
 
 static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
-- 
2.43.0


             reply	other threads:[~2026-11-26  8:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  8:56 Seiji Nishikawa [this message]
2026-11-26  9:13 ` Christoph Hellwig

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=20261126085653.3339227-1-snishika@redhat.com \
    --to=snishika@redhat.com \
    --cc=axboe@kernel.dk \
    --cc=cleech@redhat.com \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    /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

all inboxes | Powered by JetHome®