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=1795978779; b=LHbHc0yo40FCzyH511LvwIDXF03NmbcQbGgxSLUmkk9hEgdB0OuCBtojMdSN8VDmXXGd B06ZuwQTN9tiEG7Klw0UoBo8P71tDf6jDlhs5k2Uu/UFthdti+f/I7XWIg+YnJdnXF+gF e4IqlN4IB/PcQzQV+rizYARLeHvbDuukEPDLwjOkcptBdTkR468G5xX7YxzeW7ToD3IVy ejVu8+aDVw4zKVTikBoMxHwtcysRKNjfJMGdSZ+7mqJZNH908WbelHsFR1gSNRGVRRUa5 qyEjgNcltjiNikWbV+BFAj04kO7KYpT4h1x/j5A+9eIp/oNwap6QlDl+XLK7ubLTFag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795978779; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=N/Uza2r6JetB70KFMP624n5XLxUe37ib287EJkfrB6E=; b=FzG2E/Hi0O0NztoRcYJVSTo0cV5CXwkzAMfSFTiPcknaKtqE/UwMR9F4EqM0L6FctEmi bAUT0kwtC4SYnqcJb4HplQZownwPRb4mSL0ym0YpRstY3mztwiX2les9FZ8cPkAI8dVVE V5FZn+qwIo56NmkR5HuGM5vcWn7+zEZws9BZP+vZwUUwl3ngH4hShu+b02SecDvSTVocY jyeqY3YuTvrlOXLE030NwzUBnL2HEQMtITm0vSSst4O7vlJNN/Y933dgD9HzZs/jNSehP L6QUI68mImRlZEpw1sIxmOwKGUBT/XsBWgNgYJEw+hySTvsvphFfDYLP/ikF72embDw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b="FAZhnCn/"; 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 68FE2606A31C; Sun, 29 Nov 2026 18:59:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=FAZhnCn/ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 088771F000C1; Sun, 29 Nov 2026 18:59:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795978779; bh=N/Uza2r6JetB70KFMP624n5XLxUe37ib287EJkfrB6E=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=FAZhnCn//wZ5khxLFyl5rZmAZy4QmGIanyI8AcU55mNoy0AqpVpbjQQb/wo7dwo28 AOi71FodeJ2BsiIcnwOwCH0PQlq2JInE23ttbnZcu197rGfyN9TXBsTsYA93FTlfXE f63RbKxCZKb3CzFW+YwKJE6B9DNOHfuguxP2DkEI= Date: Sun, 29 Nov 2026 19:59:27 +0100 From: Greg Kroah-Hartman To: Yuho Choi Cc: Haiyang Zhang , Wei Liu , Dexuan Cui , Michael Kelley , Emerson Busson , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] uio_hv_generic: Close the channel on remove if it is still open Message-ID: <2026112954-oops-pretext-8feb@gregkh> References: <20261129184024.1092604-2-oss.patchbox@gmail.com> X-Mailing-List: linux-hyperv@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261129184024.1092604-2-oss.patchbox@gmail.com> 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 > --- > 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; 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