nvme-tcp: fix use of a released socket in nvme_tcp_poll()
PATCH 1/1 nvme-tcp: fix use of a released socket in nvme_tcp_poll()
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.
--- drivers/nvme/host/tcp.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
drivers/nvme/host/tcp.c +18 -4
@@ -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)
Signature · 1 line
2.43.0
Looks good: