All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yuho Choi" <oss.patchbox@gmail.com>
Cc: linux-hyperv@vger.kernel.org
Subject: Re: [PATCH] uio_hv_generic: Close the channel on remove if it is still open
Date: Sun, 29 Nov 2026 19:05:06 +0000	[thread overview]
Message-ID: <sashiko-outbox-239931@kernel.org> (raw)
In-Reply-To: <20261129184024.1092604-2-oss.patchbox@gmail.com>

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

  parent reply	other threads:[~2026-11-29 19:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 23:25   ` Yuho Choi
2026-11-30  6:36     ` Greg Kroah-Hartman
2026-11-29 19:05 ` sashiko-bot [this message]
2026-11-30  8:33 ` Naman Jain

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=sashiko-outbox-239931@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=oss.patchbox@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.