Re: [PATCH v2 2/4] vfio: selftests: Verify a failed second open preserves the vf_token

From: Alex Williamson

Date: Thu Sep 24 2026 - 14:26:07 EST


Hey David,

Ping, you suggested[1] this selftest, I think this series is only
blocked by an ack here. Thanks,

Alex

[1]https://lore.kernel.org/all/aqHdXvfcfCJbLajM@xxxxxxxxxx/

On Fri, 11 Sep 2026 11:04:25 -0600
Alex Williamson <alex.williamson@xxxxxxxxxx> wrote:

> The cdev path enforces a single open per device and rejects a second
> bind of an already open device. That rejection must happen before the
> bind can mutate state shared across opens, notably the PF vf_token, so
> that a bind which cannot complete leaves the current opener's state
> untouched.
>
> Add a regression test that binds a PF with one token, attempts a
> second bind of the same PF with a different token, then initializes a
> VF with the original token. The VF init succeeds only if the second
> bind left the PF vf_token intact; a regression that clobbered it to
> the second token would make the VF init fail.
>
> Additionally add a second separate test that enforces the -EBUSY
> errno on second open so that the vf_token clobber and errno testing
> are independent.
>
> These hazards are specific to the cdev/iommufd single-open path, so
> the tests run only in iommufd mode.
>
> Suggested-by: David Matlack <dmatlack@xxxxxxxxxx>
> Assisted-by: LLM
> Signed-off-by: Alex Williamson <alex.williamson@xxxxxxxxxx>
> ---
> .../selftests/vfio/vfio_pci_sriov_uapi_test.c | 57 +++++++++++++++++++
> 1 file changed, 57 insertions(+)
>
> 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..b57e4498443f 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,63 @@ 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");
> +
> + /* 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). Return value intentionally unenforced.
> + */
> + device_init(pf_bdf, iommu, UUID_2, &pf_second_fd);
> +
> + /*
> + * 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);
> + device_cleanup(pf_second_fd);
> + device_cleanup(pf);
> + iommu_cleanup(iommu);
> +}
> +
> +TEST(failed_second_open_returns_ebusy)
> +{
> + struct vfio_pci_device *pf = NULL, *pf_second_fd = NULL;
> + struct iommu *iommu;
> + int ret;
> +
> + iommu = iommu_init("iommufd");
> +
> + /* 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.
> + * Previously failed with EINVAL.
> + */
> + ret = device_init(pf_bdf, iommu, UUID_2, &pf_second_fd);
> + ASSERT_EQ(ret, -EBUSY);
> +
> + device_cleanup(pf_second_fd);
> + device_cleanup(pf);
> + iommu_cleanup(iommu);
> +}
> +
> static void vf_teardown(void)
> {
> /*