From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795977630; b=f4nRW0lvUnby5NAmg65oOA3p0m96W9xIvwbZ44Iw8dkCwJee4FFu/t+Puvmp/JiNQ7Oy QMqnTyamis97vI007Z92UfIzUBPDC8YoxoDATVkF/jDZAd3fQSY+KpXMAQUgBPvXxndl7 pwHfBiD+wA8bHOaShYcaOfF9F+/ugqtCjv/3edrTFlo8skMxgcrUAVicTCosjYy19PO4i GNoeRhVF3uXG20KMbQvPQEfDetoWczT+2z9CU/hrl4osP+3VKJXJSofWAZO35/pvdsrRs ScgqYI9DWbWluOJR+lyS429OkcfAo1kxbQ2taaYw+Qv3L/zpUO/n4ls/5pJLNC4jwgQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795977630; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=wkuWXirO9MB+z1wt98gZA2b7Kaxnt/YYa+5i29Q1yGY=; b=MFmpKvuFWHqtzVMaagiuWkMELL+HaLXYgqDe8LFfwBCNI9bTm9gju0mN+x6C0+4b+F+a L9y6wFa0sDwtTe609Xc7pSGpMkAaHfQALw+sm9MBKTm6w2nNPK3zXv9tQOq5HIIuHuXnW dmgR8+DgPLSgHfJ5wSFUlTmJamN0G52pERUiFpaFc9GHoYx/K/UBxZhJHBMDevido2uIt BzSeV/bSPs1oQzg8ddvYkiQvXcokxOL/sAMxZyShOLzUfQkjWoIUmCRETPztrZKgIGZ56 eulSuXBApCqyxHNVNXAcGC4pq8lELbl6jFIElSNnJhGG0XuR2B0oAYSvcwH0d5AcLRQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=TAe3aIWI; arc=none smtp.remote-ip=209.85.161.50 Received: from mail-oo1-f50.google.com (mail-oo1-f50.google.com [209.85.161.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 781DD606A327 for ; Sun, 29 Nov 2026 18:40:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 781DD606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=TAe3aIWI Received: by mail-oo1-f50.google.com with SMTP id 006d021491bc7-71ae5389c00so6113488eaf.2 for ; Sun, 29 Nov 2026 10:40:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795977628; x=1796582428; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=wkuWXirO9MB+z1wt98gZA2b7Kaxnt/YYa+5i29Q1yGY=; b=TAe3aIWIuVlpKqlUU6v30cK2e4oNOCAcpGFbnsfaKiGJjL/+4mbt3SC3zs0/Ww+irh Y/y/m1A0wA0g9S9vcyVATeoDzuk+BeR8lYzrOwhO9sJpx/SQGty9FBPROI0YAW4fYaf+ Z5Tfd/2oV9xfI8WV6XTLDd3leb6agpXxf3/YdtSu6hw9kSHllnTozjj0oA9kpvrT+XZB dtMW0yv4gKvW72+mgK0SwF4Ht5g9rXrcvg9Y7nHVUA/ek+Yp1E2FPaa9Ab+lC0iSJDtY kHRzlvsdp9OWQxfQQ9qyCAwifzY+QOgC6YJ/VqHXxaDjGu449HRmgLsvjZsginSVv58c 8FkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795977628; x=1796582428; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=wkuWXirO9MB+z1wt98gZA2b7Kaxnt/YYa+5i29Q1yGY=; b=FBDTqCuNfGLl7l530p6+OGtvQjZKMbkMrD/coLnge+3KRWoa7UawfbV0lm26Q3R0tR wcIdtXxrlCC0i+xMDoW3BCSw7YnQrEIeyrTUsVwpWfJV0I0XReug1WKnj6n+GTx5go// Uq9IxVqjry24pwwpt1fUNEkRtukIgD+9VzCTsIYG2WEikghW1VUcpE1XxSYppfVt4Cdd 9apr/sbPFtkyuN50RaFD9DCGpUaSb2lrdZZRAlqbdOeNG3EWzXeqmUYYhOdB9xh7GcwO BNAnx+MAKeJ8NRETzrxfRcwuunLVQXtUA/LKLYM38VYeDrYWv3FCOvjPYQ7Fg9RilrBI bu8A== X-Forwarded-Encrypted: i=1; AL+hErFj3JDAXiXap2sO+zhh9Lazla7wMPTBN99c4GGjVJ7t85hYenRDnYXFCqzvxKO6Nz8LQEF91dQi177UmhE=@vger.kernel.org X-Gm-Message-State: APuU4Z/1b4IAQlNUCfcAIbA1ZrnYdUzVEeWSSTI+alS7jo3StQg1TZol VCJcAIN+HrXUjUsy00WKHXVzuCznLsBoQWqoLbNYt5kd1WyyJH+Yu9kW X-Gm-Gg: AZFdTZdjsT4u7XoPor3nCv6PS1YML/6I8wg36KPzn4vnv4peOxukdwyxUa0RSUUc31a 0kytJzFyuOceVl21fKpsm/mqUM4t6GhOzqhdId/vhbHxlnTB5OX6YB8b7VHSATxjOlKOuhgJPc3 vTYB9AlOW/Hd5cSy4sGinIN9BqeeAnmUvkr6IPwdXDQXTWFuVl6WjxBloT95KrefMVVBlM2+CVE Zp+ZwsizUBZFc7Tb99LmV+3dHvZwdsp8MMGlFwMcisx3yLhFjmIZLrL1AyunZqY+NJzEd9NpWHT KwfGlqy4Wzldh8wqBwOwFIwF2eQ5OvwbY1VBIMCoT0UbBg/cGIhYQy/ANukNMhmcoV6M9iE9QAj yp3lYTFRKhQ1Oxdr1AxyU9AGZCPltwEsv1Akmv+o2xvQ+iekV17SfYrFzsOARyDFll/oWQ8mYzb Z+tN8x1ZGcS0SyqFOq+kEBaqzKJ4At+a0DugEBwIHlx9XN2wPmgvDmdZjzpJ7XtASoFYnETf4uz +7s8mIbwHXbQRu3zJZL3aufLwKApLYXOxPyNxbBhk7mfqFFaJq2u2j/Th/ebJbNO+yPcX/ADO4G JMkhOLLG+TbGlatwknfQ3X6PvXaZ8q3QosLKq3MvLMu5pSC/6o+I7PtafH06m8KxipWJ1CHi6x2 w3y5srt0SDZ9RGUpqksA2jWlR1HlToEMLkJyLLriTC6o0cHD+Iqp0vxKDPzkiEv0F3RJP4ivQQ6 Z9uSv1/Y1kpRxXCQ== X-Received: by 2002:a05:6820:1342:b0:71b:3fec:3915 with SMTP id 006d021491bc7-71b3fec4aa6mr24409096eaf.64.1795977628264; Sun, 29 Nov 2026 10:40:28 -0800 (PST) Received: from tempest-testimage-cc-centos9-stream-server-1191361818 (chi-dyn-129-114-108-164.tacc.chameleoncloud.org. [129.114.108.164]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b37c52ae6sm20770887eaf.0.2026.11.29.10.40.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 10:40:26 -0800 (PST) From: Yuho Choi To: Haiyang Zhang , Wei Liu , Dexuan Cui , Greg Kroah-Hartman Cc: Michael Kelley , Emerson Busson , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, Yuho Choi Subject: [PATCH] uio_hv_generic: Close the channel on remove if it is still open Date: Sun, 29 Nov 2026 18:38:53 +0000 Message-ID: <20261129184024.1092604-2-oss.patchbox@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit hv_uio_release() closes the VMBus channel when the last file is closed, and hv_uio_remove() then frees the ring buffer. But once uio_unregister_device() has run, the uio core no longer calls ->release() for files that are still open. If the device is unbound while a process has /dev/uioX open, the channel is never closed: - the host keeps the channel open and the ring GPADL established; - hv_uio_channel_cb() stays installed, and dereferences a NULL drvdata the next time the host signals the channel; - after the driver is bound again, opening the device fails, since vmbus_connect_ring() finds the channel still open. hv_uio_remove() also frees the ring in that case. In most VMs the pages happen to be leaked instead, because ringbuffer_gpadlhandle.decrypted is still set. A channel with a confidential ring buffer never sets it, so there the pages go back to the allocator while the GPADL still covers them. The same happens when vmbus_disconnect_ring() fails in hv_uio_release(). Close the channel in hv_uio_remove() if it is still open, and hold a reference to the uio device until then, as hv_uio_channel_cb() uses it. A process may still have the ring mapped, and uio maps it with remap_pfn_range(), which takes no page references, so keep the ring memory in that case. Keep it too when closing the channel failed in hv_uio_release(), as vmbus_close() does, and warn when the ring is leaked. Fixes: cdfa835c6e5e ("uio_hv_generic: defer opening vmbus until first use") Signed-off-by: Yuho Choi --- Tested on 946d9ed1aa02 in a KVM guest with QEMU 11.2's VMBus emulation (-device vmbus-bridge -device hv-balloon, Hyper-V enlightenments), with KASAN. The hv-balloon device is bound to uio_hv_generic through new_id; a test program opens /dev/uio0, maps the ring and negotiates the protocol version and capabilities with the host. The device is then unbound while the file is still open. Without this patch, QEMU sees no CLOSECHANNEL and no GPADL teardown; binding the device again and opening it fails with -EINVAL; and when QEMU then sends a DM_BALLOON_REQUEST, the guest crashes: Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] SMP KASAN PTI KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:uio_event_notify+0x1a/0x80 Call Trace: vmbus_chan_sched+0x38e/0x4d0 __vmbus_isr+0x58/0x260 __sysvec_hyperv_callback+0x3e/0x70 sysvec_hyperv_callback+0x6b/0x80 With this patch, the channel is closed and its GPADL torn down at unbind, the new warning is printed, the device can be opened again after it is bound again, and there is no crash. Unbinding after the file is closed works as before. The confidential ring buffer case and a failing vmbus_disconnect_ring() were not tested. drivers/uio/uio_hv_generic.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/drivers/uio/uio_hv_generic.c b/drivers/uio/uio_hv_generic.c index dff92dcbb10d..bf6a78bcf369 100644 --- a/drivers/uio/uio_hv_generic.c +++ b/drivers/uio/uio_hv_generic.c @@ -55,6 +55,7 @@ struct hv_uio_private_data { struct uio_info info; struct hv_device *device; atomic_t refcnt; + bool keep_ring; /* the channel may not have been closed */ void *recv_buf; struct vmbus_gpadl recv_gpadl; @@ -240,8 +241,11 @@ hv_uio_release(struct uio_info *info, struct inode *inode) struct hv_device *dev = pdata->device; int ret = 0; - if (atomic_dec_and_test(&pdata->refcnt)) + if (atomic_dec_and_test(&pdata->refcnt)) { ret = vmbus_disconnect_ring(dev->channel); + if (ret) + pdata->keep_ring = true; + } return ret; } @@ -385,15 +389,35 @@ static void hv_uio_remove(struct hv_device *dev) { struct hv_uio_private_data *pdata = hv_get_drvdata(dev); + struct device *uio_dev; if (!pdata) return; hv_remove_ring_sysfs(dev->channel); + + /* hv_uio_channel_cb() may use it until the channel is closed */ + uio_dev = get_device(&pdata->info.uio_dev->dev); uio_unregister_device(&pdata->info); + + /* + * The uio core does not call hv_uio_release() for files that are + * still open after the device is unregistered, so close the channel + * here. A process may still have the ring mapped, so keep it. + */ + if (atomic_read(&pdata->refcnt)) { + vmbus_disconnect_ring(dev->channel); + pdata->keep_ring = true; + } + put_device(uio_dev); + hv_uio_cleanup(dev, pdata); - vmbus_free_ring(dev->channel); + if (pdata->keep_ring) + dev_warn(&dev->device, + "ring buffer may still be in use, leaking it\n"); + else + vmbus_free_ring(dev->channel); } static const struct hv_vmbus_device_id hv_uio_id_table[] = { base-commit: 946d9ed1aa021d572467a1cdcdc2991f89b788a1 -- 2.56.1