From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795979107; b=Yaq/tbmYdqHJZL2Gixue1rQZWf5WskdZXx07xomLg+WxIR2UN+m7fsFQdZjWhKG/nQ6R SJgbJ6dFWgWfZE+P9Fr+OZsFPUS73W9INWBEc2VT89yhsn2K+9+X7L+OuHdZtPHqHbeho B05Y/NBlryafdBeNQkfSpN34jKBSJaZFep6SOLRIK2S5KsIbZJGEsawE1H736UmJsaYSb dhvjk0LWcYV83sjgTmI6isSn1ND4Lp5sHwW5cn0F8r5CI5LPogcvz1PFkHZicvFWBA5qF rSoLE/6l0Tf79xuQEehNM+X0huSgVe/kIGCZ+8AHtbyNHcfJMzk21a0riIPYUaeAbVw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795979107; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=3nryWDiSH3yI8iUZiyfdKlwXB3kimw1jTmRXmxb3frA=; b=xv56HLB/P2cmUyOcAADFHZpXgy6tGhefJnf4+LGSxqKswlyoqlBUq9q75C3i91vtlC9K mGVpFedbtXk1JJKygV/JgUEEVLXMoBI/dMjQyGoDonBxsM1ZB7SKDkP4B9jBLhqOmx4am oa/wZlVuezl37/Dg1DUyOUCFNLdzL7qEPrEkk5NFGnksPpR34ZCnYTXxhNMa9/ZnfhFAd zlwW7eL52+XSKA6AL63f1LRWZV1upoORBjVBogBK51U4Uj+ZT4Y/BsPUdy0bYv64OGDXm IY0AypjpM3xkgh45kskOVu+n1MtzL69RGNODsAf/DhYQhjG6Junefu667NaeMdhvhpA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=k6pX8jLn; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8440E606A31C for ; Sun, 29 Nov 2026 19:05:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=k6pX8jLn Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0A0AB1F00896; Sun, 29 Nov 2026 19:05:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795979107; bh=3nryWDiSH3yI8iUZiyfdKlwXB3kimw1jTmRXmxb3frA=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=k6pX8jLn3EIsR6eX9/3oGc9LLB6P59ivAuHMeMYMYDx/IjpYHv5SPSOOHuD05SSyV gshsWxYWswNrAPO/FkRFMurctkkePFCQ13z8JCAUpmwXr23WKaAnUQn1a8P1mO6VqM 9CYiKzdb7eAlXfJkngVoENF5mr0GUTqYuOuysyLH+JNR4Xzg8NypNX5zWko3ivefiR AXo5LzgErTO31XV89QKhAoRX7BmZMmPje4k/gGxTb5pm8TLTEXr9Iqt4WycZq+9RJh JOGtGnN3kdtAF+FHr3WmmY1hSTQxM1DHaPakNWahJJPl1Ei2lrVYB2ht5zu1pxzmLn EYziN+ZsPEeiQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] uio_hv_generic: Close the channel on remove if it is still open Reply-To: sashiko-reviews@lists.linux.dev To: "Yuho Choi" Cc: linux-hyperv@vger.kernel.org In-Reply-To: <20261129184024.1092604-2-oss.patchbox@gmail.com> References: <20261129184024.1092604-2-oss.patchbox@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sun, 29 Nov 2026 19:05:06 +0000 X-Mailing-List: linux-hyperv@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 =3D pdata->device; > int ret =3D 0; > =20 > - if (atomic_dec_and_test(&pdata->refcnt)) > + if (atomic_dec_and_test(&pdata->refcnt)) { > ret =3D vmbus_disconnect_ring(dev->channel); > + if (ret) > + pdata->keep_ring =3D true; > + } > =20 > 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 =3D hv_get_drvdata(dev); > + struct device *uio_dev; > =20 > if (!pdata) > return; > =20 > hv_remove_ring_sysfs(dev->channel); > + > + /* hv_uio_channel_cb() may use it until the channel is closed */ > + uio_dev =3D 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 =3D true; > + } > + put_device(uio_dev); > + > hv_uio_cleanup(dev, pdata); > =20 > - 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 =3D 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 --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261129184024.1092= 604-2-oss.patchbox@gmail.com?part=3D1