* [PATCH] uio_hv_generic: Close the channel on remove if it is still open
@ 2026-11-29 18:38 Yuho Choi
2026-11-29 18:59 ` Greg Kroah-Hartman
2026-11-29 19:05 ` sashiko-bot
0 siblings, 2 replies; 3+ messages in thread
From: Yuho Choi @ 2026-11-29 18:38 UTC (permalink / raw)
To: Haiyang Zhang, Wei Liu, Dexuan Cui, Greg Kroah-Hartman
Cc: Michael Kelley, Emerson Busson, linux-hyperv, linux-kernel,
Yuho Choi
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 <oss.patchbox@gmail.com>
---
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:
<IRQ>
vmbus_chan_sched+0x38e/0x4d0
__vmbus_isr+0x58/0x260
__sysvec_hyperv_callback+0x3e/0x70
sysvec_hyperv_callback+0x6b/0x80
</IRQ>
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
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] uio_hv_generic: Close the channel on remove if it is still open
2026-11-29 18:38 [PATCH] uio_hv_generic: Close the channel on remove if it is still open Yuho Choi
@ 2026-11-29 18:59 ` Greg Kroah-Hartman
2026-11-29 19:05 ` sashiko-bot
1 sibling, 0 replies; 3+ messages in thread
From: Greg Kroah-Hartman @ 2026-11-29 18:59 UTC (permalink / raw)
To: Yuho Choi
Cc: Haiyang Zhang, Wei Liu, Dexuan Cui, Michael Kelley,
Emerson Busson, linux-hyperv, linux-kernel
On Sun, Nov 29, 2026 at 06:38:53PM +0000, Yuho Choi wrote:
> 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 <oss.patchbox@gmail.com>
> ---
> 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:
> <IRQ>
> vmbus_chan_sched+0x38e/0x4d0
> __vmbus_isr+0x58/0x260
> __sysvec_hyperv_callback+0x3e/0x70
> sysvec_hyperv_callback+0x6b/0x80
> </IRQ>
>
> 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;
What's to keep the refcnt from changing right after you read it?
LLMs love to add boolean flags, are you sure you can't do it without it
and just properly use the reference count logic instead?
> + }
> + put_device(uio_dev);
This feels wrong, how is this code working at all today without this
reference count decrement?
And why does pdata have a reference count independent of uio_dev?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] uio_hv_generic: Close the channel on remove if it is still open
2026-11-29 18:38 [PATCH] uio_hv_generic: Close the channel on remove if it is still open Yuho Choi
2026-11-29 18:59 ` Greg Kroah-Hartman
@ 2026-11-29 19:05 ` sashiko-bot
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-29 19:05 UTC (permalink / raw)
To: Yuho Choi; +Cc: linux-hyperv
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] uio_hv_generic: hv_uio_remove() leaks ring vmaps and
pkt_buffer on keep_ring
- [Medium] uio_hv_generic: hv_uio_open() reuses ring buffer when
keep_ring is set
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] Drivers: hv: vmbus: memory and vmap leak in __vmbus_open() on
channel reopen [1]
--
commit 4463d397dd8d53f6ac54aff7e791ff2ec04b79d9
Author: Yuho Choi <oss.patchbox@gmail.com>
uio_hv_generic: Close the channel on remove if it is still open
This commit closes the VMBus channel in hv_uio_remove() if the device is
unbound while open, and keeps the ring buffer memory when the channel was
open at unbind or failed to close in hv_uio_release().
> diff --git a/drivers/uio/uio_hv_generic.c b/drivers/uio/uio_hv_generic.c
> index dff92dcbb10d9..bf6a78bcf369e 100644
> --- a/drivers/uio/uio_hv_generic.c
> +++ b/drivers/uio/uio_hv_generic.c
[ ... ]
> @@ -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;
> }
[Severity: Medium]
Can hv_uio_open() reuse the untorn-down ring buffer after
vmbus_disconnect_ring() fails here?
When vmbus_disconnect_ring() fails, pdata->refcnt is already 0 and
vmbus_close_internal() has already reset channel->state to
CHANNEL_OPEN_STATE before sending CHANNELMSG_CLOSECHANNEL or tearing down
the GPADL. Because hv_uio_open() does not check pdata->keep_ring, a
subsequent open of /dev/uioX before driver removal calls
vmbus_connect_ring() -> __vmbus_open() on the same
channel->ringbuffer_page, zeroing ringbuffer_gpadlhandle.gpadl_handle and
calling __vmbus_establish_gpadl() (or re-encrypting the pages on failure)
while the original GPADL is still active on the host.
[Severity: High]
This problem wasn't introduced by this patch, but does reopening the
channel after vmbus_disconnect_ring() leak the ring buffer vmap()
mappings and inbound packet buffer [1]?
vmbus_disconnect_ring() closes the channel via vmbus_close_internal()
without calling hv_ringbuffer_cleanup() on channel->outbound and
channel->inbound, leaving cleanup to vmbus_free_ring(). When
hv_uio_open() later calls vmbus_connect_ring() -> __vmbus_open() ->
hv_ringbuffer_init(), ring_info->ring_buffer and ring_info->pkt_buffer
are overwritten with new vmap() and kzalloc() allocations on every close
and reopen of /dev/uioX.
> @@ -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);
> }
[Severity: Medium]
Does skipping vmbus_free_ring() when pdata->keep_ring is set leak the
kernel ring vmap() mappings and inbound.pkt_buffer, and leave stale
pointers on dev->channel?
Userspace and the host GPADL only reference the physical pages in
channel->ringbuffer_page, whereas vmbus_free_ring() also cleans up the
kernel wraparound vmap() mappings and packet buffer and clears
channel->ringbuffer_page:
drivers/hv/channel.c:vmbus_free_ring() {
hv_ringbuffer_cleanup(&channel->outbound);
hv_ringbuffer_cleanup(&channel->inbound);
if (channel->ringbuffer_page) {
/* In a CoCo VM leak the memory if it didn't get re-encrypted */
if (!channel->ringbuffer_gpadlhandle.decrypted)
__free_pages(channel->ringbuffer_page,
get_order(channel->ringbuffer_pagecount
<< PAGE_SHIFT));
channel->ringbuffer_page = NULL;
}
}
Skipping vmbus_free_ring() altogether when unbinding while /dev/uioX is
open leaks outbound.ring_buffer, inbound.ring_buffer, and
inbound.pkt_buffer, and leaves ring_buffer non-NULL so VMBus sysfs ring
attributes continue reading through the old kernel mapping after unbind.
[1] https://sashiko.dev/#/bug/linux-9c8f4671-bdff-4843-98cd-1b0802f80100
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261129184024.1092604-2-oss.patchbox@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-29 19:05 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 18:38 [PATCH] uio_hv_generic: Close the channel on remove if it is still open Yuho Choi
2026-11-29 18:59 ` Greg Kroah-Hartman
2026-11-29 19:05 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox