[PATCH 19/21] vfio: selftests: Expose low-level helper routines for setting up struct vfio_pci_device

David Matlack posted 21 patches 2 months, 1 week ago
[PATCH 19/21] vfio: selftests: Expose low-level helper routines for setting up struct vfio_pci_device
Posted by David Matlack 2 months, 1 week ago
Expose a few low-level helper routings for setting up vfio_pci_device
structs. These routines will be used in a subsequent commit to assert
that VFIO_GROUP_GET_DEVICE_FD fails under certain conditions.

Signed-off-by: David Matlack <dmatlack@google.com>
---
 .../lib/include/libvfio/vfio_pci_device.h     |  5 +++
 .../selftests/vfio/lib/vfio_pci_device.c      | 33 +++++++++++++------
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
index 896dfde88118..2389c7698335 100644
--- a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
+++ b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
@@ -125,4 +125,9 @@ static inline bool vfio_pci_device_match(struct vfio_pci_device *device,
 
 const char *vfio_pci_get_cdev_path(const char *bdf);
 
+/* Low-level routines for setting up a struct vfio_pci_device */
+struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iommu *iommu);
+void vfio_pci_group_setup(struct vfio_pci_device *device);
+void vfio_pci_iommu_setup(struct vfio_pci_device *device);
+
 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */
diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
index e9423dc3864a..c1a3886dee30 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
@@ -199,7 +199,7 @@ static unsigned int vfio_pci_get_group_from_dev(const char *bdf)
 	return group;
 }
 
-static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf)
+void vfio_pci_group_setup(struct vfio_pci_device *device)
 {
 	struct vfio_group_status group_status = {
 		.argsz = sizeof(group_status),
@@ -207,7 +207,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
 	char group_path[32];
 	int group;
 
-	group = vfio_pci_get_group_from_dev(bdf);
+	group = vfio_pci_get_group_from_dev(device->bdf);
 	snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group);
 
 	device->group_fd = open(group_path, O_RDWR);
@@ -219,14 +219,12 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
 	ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->container_fd);
 }
 
-static void vfio_pci_container_setup(struct vfio_pci_device *device, const char *bdf)
+void vfio_pci_iommu_setup(struct vfio_pci_device *device)
 {
 	struct iommu *iommu = device->iommu;
 	unsigned long iommu_type = iommu->mode->iommu_type;
 	int ret;
 
-	vfio_pci_group_setup(device, bdf);
-
 	ret = ioctl(iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type);
 	VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type);
 
@@ -236,8 +234,14 @@ static void vfio_pci_container_setup(struct vfio_pci_device *device, const char
 	 * because the IOMMU type is already set.
 	 */
 	(void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type);
+}
 
-	device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf);
+static void vfio_pci_container_setup(struct vfio_pci_device *device)
+{
+	vfio_pci_group_setup(device);
+	vfio_pci_iommu_setup(device);
+
+	device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, device->bdf);
 	VFIO_ASSERT_GE(device->fd, 0);
 }
 
@@ -337,9 +341,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device,
 	vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id);
 }
 
-struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
-					       struct iommu *iommu,
-					       int device_fd)
+struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iommu *iommu)
 {
 	struct vfio_pci_device *device;
 
@@ -349,9 +351,20 @@ struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
 	device->bdf = bdf;
 	device->iommu = iommu;
 
+	return device;
+}
+
+struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
+					       struct iommu *iommu,
+					       int device_fd)
+{
+	struct vfio_pci_device *device;
+
+	device = vfio_pci_device_alloc(bdf, iommu);
+
 	if (device->iommu->mode->container_path) {
 		VFIO_ASSERT_EQ(device_fd, -1);
-		vfio_pci_container_setup(device, bdf);
+		vfio_pci_container_setup(device);
 	} else {
 		vfio_pci_iommufd_setup(device, bdf, device_fd);
 	}
-- 
2.52.0.487.g5c8c507ade-goog
Re: [PATCH 19/21] vfio: selftests: Expose low-level helper routines for setting up struct vfio_pci_device
Posted by Zhu Yanjun 1 month, 1 week ago
在 2025/11/26 11:36, David Matlack 写道:
> Expose a few low-level helper routings for setting up vfio_pci_device
> structs. These routines will be used in a subsequent commit to assert
> that VFIO_GROUP_GET_DEVICE_FD fails under certain conditions.
>
> Signed-off-by: David Matlack <dmatlack@google.com>
> ---
>   .../lib/include/libvfio/vfio_pci_device.h     |  5 +++
>   .../selftests/vfio/lib/vfio_pci_device.c      | 33 +++++++++++++------
>   2 files changed, 28 insertions(+), 10 deletions(-)
>
> diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
> index 896dfde88118..2389c7698335 100644
> --- a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
> +++ b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
> @@ -125,4 +125,9 @@ static inline bool vfio_pci_device_match(struct vfio_pci_device *device,
>   
>   const char *vfio_pci_get_cdev_path(const char *bdf);
>   
> +/* Low-level routines for setting up a struct vfio_pci_device */
> +struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iommu *iommu);
> +void vfio_pci_group_setup(struct vfio_pci_device *device);
> +void vfio_pci_iommu_setup(struct vfio_pci_device *device);
> +
>   #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */
> diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> index e9423dc3864a..c1a3886dee30 100644
> --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> @@ -199,7 +199,7 @@ static unsigned int vfio_pci_get_group_from_dev(const char *bdf)
>   	return group;
>   }
>   
> -static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf)
> +void vfio_pci_group_setup(struct vfio_pci_device *device)
>   {
>   	struct vfio_group_status group_status = {
>   		.argsz = sizeof(group_status),
> @@ -207,7 +207,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
>   	char group_path[32];
>   	int group;
>   
> -	group = vfio_pci_get_group_from_dev(bdf);
> +	group = vfio_pci_get_group_from_dev(device->bdf);
>   	snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group);
>   
>   	device->group_fd = open(group_path, O_RDWR);
> @@ -219,14 +219,12 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
>   	ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->container_fd);
>   }
>   
> -static void vfio_pci_container_setup(struct vfio_pci_device *device, const char *bdf)
> +void vfio_pci_iommu_setup(struct vfio_pci_device *device)
>   {
>   	struct iommu *iommu = device->iommu;
>   	unsigned long iommu_type = iommu->mode->iommu_type;
>   	int ret;
>   
> -	vfio_pci_group_setup(device, bdf);
> -
>   	ret = ioctl(iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type);
>   	VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type);
>   
> @@ -236,8 +234,14 @@ static void vfio_pci_container_setup(struct vfio_pci_device *device, const char
>   	 * because the IOMMU type is already set.
>   	 */
>   	(void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type);
> +}
>   
> -	device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf);
> +static void vfio_pci_container_setup(struct vfio_pci_device *device)
> +{
> +	vfio_pci_group_setup(device);
> +	vfio_pci_iommu_setup(device);
> +
> +	device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, device->bdf);
>   	VFIO_ASSERT_GE(device->fd, 0);
>   }
>   
> @@ -337,9 +341,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device,
>   	vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id);
>   }
>   
> -struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
> -					       struct iommu *iommu,
> -					       int device_fd)
> +struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iommu *iommu)
>   {
>   	struct vfio_pci_device *device;
>   
> @@ -349,9 +351,20 @@ struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
>   	device->bdf = bdf;
>   	device->iommu = iommu;
>   
> +	return device;
> +}
> +

In the latest kernel, this part changes too much.

Yanjun.Zhu

> +struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
> +					       struct iommu *iommu,
> +					       int device_fd)
> +{
> +	struct vfio_pci_device *device;
> +
> +	device = vfio_pci_device_alloc(bdf, iommu);
> +
>   	if (device->iommu->mode->container_path) {
>   		VFIO_ASSERT_EQ(device_fd, -1);
> -		vfio_pci_container_setup(device, bdf);
> +		vfio_pci_container_setup(device);
>   	} else {
>   		vfio_pci_iommufd_setup(device, bdf, device_fd);
>   	}

-- 
Best Regards,
Yanjun.Zhu

Re: [PATCH 19/21] vfio: selftests: Expose low-level helper routines for setting up struct vfio_pci_device
Posted by David Matlack 1 month ago
On Sat, Dec 27, 2025 at 8:04 PM Zhu Yanjun <yanjun.zhu@linux.dev> wrote:
> 在 2025/11/26 11:36, David Matlack 写道:
> > @@ -349,9 +351,20 @@ struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
> >       device->bdf = bdf;
> >       device->iommu = iommu;
> >
> > +     return device;
> > +}
> > +
>
> In the latest kernel, this part changes too much.

Can you clarify what you mean by "changes too much"? What is the issue?
Re: [PATCH 19/21] vfio: selftests: Expose low-level helper routines for setting up struct vfio_pci_device
Posted by Yanjun.Zhu 1 month ago
On 1/5/26 9:54 AM, David Matlack wrote:
> On Sat, Dec 27, 2025 at 8:04 PM Zhu Yanjun <yanjun.zhu@linux.dev> wrote:
>> 在 2025/11/26 11:36, David Matlack 写道:
>>> @@ -349,9 +351,20 @@ struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
>>>        device->bdf = bdf;
>>>        device->iommu = iommu;
>>>
>>> +     return device;
>>> +}
>>> +
>> In the latest kernel, this part changes too much.
> Can you clarify what you mean by "changes too much"? What is the issue?

I tried to apply this commit to the linux and linux-next repositories 
and run tests.

However, I’m unable to apply [PATCH 19/21] vfio: selftests: Expose 
low-level helper routines for setting up struct vfio_pci_device, because 
the related source code has changed significantly in both linux and 
linux-next.

If you plan to resend this patch series based on the latest linux or 
linux-next, please feel free to ignore this comment.

I look forward to testing the updated patch series once it is available.

Best Regards,

Yanjun.Zhu

Re: [PATCH 19/21] vfio: selftests: Expose low-level helper routines for setting up struct vfio_pci_device
Posted by David Matlack 1 month ago
On Mon, Jan 5, 2026 at 4:08 PM Yanjun.Zhu <yanjun.zhu@linux.dev> wrote:
>
>
> On 1/5/26 9:54 AM, David Matlack wrote:
> > On Sat, Dec 27, 2025 at 8:04 PM Zhu Yanjun <yanjun.zhu@linux.dev> wrote:
> >> 在 2025/11/26 11:36, David Matlack 写道:
> >>> @@ -349,9 +351,20 @@ struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
> >>>        device->bdf = bdf;
> >>>        device->iommu = iommu;
> >>>
> >>> +     return device;
> >>> +}
> >>> +
> >> In the latest kernel, this part changes too much.
> > Can you clarify what you mean by "changes too much"? What is the issue?
>
> I tried to apply this commit to the linux and linux-next repositories
> and run tests.
>
> However, I’m unable to apply [PATCH 19/21] vfio: selftests: Expose
> low-level helper routines for setting up struct vfio_pci_device, because
> the related source code has changed significantly in both linux and
> linux-next.

Ahhh. This series depends on several in-flight series, so I'm not
surprised it doesn't apply cleanly. There is this blurb in the cover
letter:
---
This series was constructed on top of several in-flight series and on
top of mm-nonmm-unstable [2].

  +-- This series
  |
  +-- [PATCH v2 00/18] vfio: selftests: Support for multi-device tests
  |    https://lore.kernel.org/kvm/20251112192232.442761-1-dmatlack@google.com/
  |
  +-- [PATCH v3 0/4] vfio: selftests: update DMA mapping tests to use
queried IOVA ranges
  |   https://lore.kernel.org/kvm/20251111-iova-ranges-v3-0-7960244642c5@fb.com/
  |
  +-- [PATCH v8 0/2] Live Update: File-Lifecycle-Bound (FLB) State
  |   https://lore.kernel.org/linux-mm/20251125225006.3722394-1-pasha.tatashin@soleen.com/
  |
  +-- [PATCH v8 00/18] Live Update Orchestrator
  |   https://lore.kernel.org/linux-mm/20251125165850.3389713-1-pasha.tatashin@soleen.com/
  |

To simplify checking out the code, this series can be found on GitHub:

  https://github.com/dmatlack/linux/tree/liveupdate/vfio/cdev/v1
---

Cloning the GitHub repo is probably your simplest option if you want
to check out the code and run some tests.

>
> If you plan to resend this patch series based on the latest linux or
> linux-next, please feel free to ignore this comment.
>
> I look forward to testing the updated patch series once it is available.

I will send out an updated patch set hopefully within the next 2 weeks.

Thanks for taking a look!