The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] nvme-tcp: fix use of a released socket in nvme_tcp_poll()
@ 2026-11-26  8:56 Seiji Nishikawa
  2026-11-26  9:13 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ messages in thread
From: Seiji Nishikawa @ 2026-11-26  8:56 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg
  Cc: Hannes Reinecke, Chris Leech, Seiji Nishikawa, linux-nvme,
	linux-kernel

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-26  9:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  8:56 [PATCH] nvme-tcp: fix use of a released socket in nvme_tcp_poll() Seiji Nishikawa
2026-11-26  9:13 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox