From: David Matlack <dmatlack@google.com>
To: Alex Williamson <alex.williamson@nvidia.com>
Cc: Alex Williamson <alex@shazbot.org>, kvm <kvm@vger.kernel.org>,
linux-kernel <linux-kernel@vger.kernel.org>,
Jason Gunthorpe <jgg@ziepe.ca>, Kevin Tian <kevin.tian@intel.com>,
Yi Liu <yi.l.liu@intel.com>
Subject: Re: [PATCH 1/4] vfio: Reject a second cdev open before mutating shared device state
Date: Wed, 9 Sep 2026 22:27:42 +0000 [thread overview]
Message-ID: <aqHdXvfcfCJbLajM@google.com> (raw)
In-Reply-To: <20260901215358.2421359-2-alex.williamson@nvidia.com>
On 2026-09-01 03:53 PM, Alex Williamson wrote:
> The cdev single-open check lives in vfio_df_open(), which runs at the
> end of the bind ioctl, after vfio_df_ioctl_bind_iommufd() has already
> updated state shared across all opens: vfio_df_check_token() can set
> the PF vf_token and vfio_df_get_kvm_safe() records the caller's KVM
> pointer in device->kvm and takes a reference.
>
> A second cdev bind of an already-open device runs both, only to be
> rejected in vfio_df_open(). The error path clears device->kvm and
> drops the reference, tearing down the current opener's KVM association
> and potentially resulting in an unbalanced reference on close or
> premature release, while the vf_token remains clobbered.
>
> Move the single-open check into vfio_df_ioctl_bind_iommufd() ahead of
> both mutations, so a bind that cannot complete leaves the current
> opener's state untouched. df->group is NULL on this path, so a
> non-zero open_count is exactly what vfio_df_open() rejected. The test
> in vfio_df_open() becomes redundant and is removed.
>
> Return -EBUSY rather than -EINVAL here. The arguments are not invalid,
> the device is in use, which could be a transient condition due to a
> delayed fput if the prior user is terminated. This provides
> compatibility with the group path, where a group open returns -EBUSY,
> and users may choose bounded polling to detect such a transient
> condition.
>
> Fixes: 839e692fa4eb ("vfio: Make vfio_df_open() single open for device cdev path")
> Fixes: 5fcc26969a16 ("vfio: Add VFIO_DEVICE_BIND_IOMMUFD")
> Fixes: 86624ba3b522 ("vfio/pci: Do vf_token checks for VFIO_DEVICE_BIND_IOMMUFD")
> Assisted-by: claude-opus-4-8
> Signed-off-by: Alex Williamson <alex.williamson@nvidia.com>
Can you add a regression test for this?
The VF token clobbering can be reproduced in vfio_pci_sriov_uapi_test:
diff --git a/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c b/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c
index 19d657d00b75..de8408b90a25 100644
--- a/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c
+++ b/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c
@@ -157,6 +157,42 @@ TEST_F(vfio_pci_sriov_uapi_test, override_token)
ASSERT_COND_VF_CREATION(ret);
}
+TEST(failed_second_open_does_not_clobber_token)
+{
+ struct vfio_pci_device *pf = NULL, *pf_second_fd = NULL, *vf = NULL;
+ struct iommu *iommu;
+ int ret;
+
+ iommu = iommu_init("iommufd");
+ if (!iommu)
+ SKIP(return, "iommufd mode not supported");
+
+ /* Create and bind PF using UUID_1 */
+ ret = device_init(pf_bdf, iommu, UUID_1, &pf);
+ ASSERT_EQ(ret, 0);
+
+ /*
+ * Attempt to open the same PF again and bind it with a *different*
+ * token (UUID_2). This must fail with EBUSY because it's a second open.
+ */
+ ret = device_init(pf_bdf, iommu, UUID_2, &pf_second_fd);
+ ASSERT_EQ(ret, -EBUSY);
+
+ /*
+ * Attempt to initialize a VF using the original PF token (UUID_1).
+ * If the failed open above clobbered the PF's token (i.e. updated it to
+ * UUID_2), this VF initialization will fail.
+ */
+ ret = device_init(vf_bdf, iommu, UUID_1, &vf);
+ ASSERT_EQ(ret, 0);
+
+ device_cleanup(vf);
+ if (pf_second_fd)
+ device_cleanup(pf_second_fd);
+ device_cleanup(pf);
+ iommu_cleanup(iommu);
+}
+
static void vf_teardown(void)
{
/*
next prev parent reply other threads:[~2026-09-09 22:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-01 21:53 [PATCH 0/4] vfio: Fix cdev second-open and harden selftests Alex Williamson
2026-09-01 21:53 ` [PATCH 1/4] vfio: Reject a second cdev open before mutating shared device state Alex Williamson
2026-09-09 22:27 ` David Matlack [this message]
2026-09-10 13:51 ` Jason Gunthorpe
2026-09-01 21:53 ` [PATCH 2/4] selftests/vfio: Wait out transient -EBUSY on open/bind Alex Williamson
2026-09-09 21:40 ` David Matlack
2026-09-10 22:50 ` Alex Williamson
2026-09-01 21:53 ` [PATCH 3/4] selftests/vfio: Extend mix_and_match timeout to 90s Alex Williamson
2026-09-09 21:42 ` David Matlack
2026-09-01 21:53 ` [PATCH 4/4] selftests/vfio: Extend timeout for runner executions Alex Williamson
2026-09-09 21:51 ` David Matlack
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=aqHdXvfcfCJbLajM@google.com \
--to=dmatlack@google.com \
--cc=alex.williamson@nvidia.com \
--cc=alex@shazbot.org \
--cc=jgg@ziepe.ca \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=yi.l.liu@intel.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox