lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAJHc60zW+FzOfUQzZYCStmFJ_d8Gr2mi-nN297b=gU+26mt1BQ@mail.gmail.com>
Date: Mon, 24 Nov 2025 16:47:24 +0530
From: Raghavendra Rao Ananta <rananta@...gle.com>
To: David Matlack <dmatlack@...gle.com>
Cc: Alex Williamson <alex@...zbot.org>, Alex Mastro <amastro@...com>, Jason Gunthorpe <jgg@...dia.com>, 
	Josh Hilke <jrhilke@...gle.com>, kvm@...r.kernel.org, linux-kernel@...r.kernel.org, 
	linux-kselftest@...r.kernel.org, Vipin Sharma <vipinsh@...gle.com>
Subject: Re: [PATCH v3 06/18] vfio: selftests: Support multiple devices in the
 same container/iommufd

On Fri, Nov 21, 2025 at 11:44 PM David Matlack <dmatlack@...gle.com> wrote:
>
> Support tests that want to add multiple devices to the same
> container/iommufd by decoupling struct vfio_pci_device from
> struct iommu.
>
> Multi-devices tests can now put multiple devices in the same
> container/iommufd like so:
>
>   iommu = iommu_init(iommu_mode);
>
>   device1 = vfio_pci_device_init(bdf1, iommu);
>   device2 = vfio_pci_device_init(bdf2, iommu);
>   device3 = vfio_pci_device_init(bdf3, iommu);
>
>   ...
>
>   vfio_pci_device_cleanup(device3);
>   vfio_pci_device_cleanup(device2);
>   vfio_pci_device_cleanup(device1);
>
>   iommu_cleanup(iommu);
>

> +struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iommu)
>  {
>         struct vfio_pci_device *device;
>
>         device = calloc(1, sizeof(*device));
>         VFIO_ASSERT_NOT_NULL(device);
>
> -       device->iommu = calloc(1, sizeof(*device->iommu));
> -       VFIO_ASSERT_NOT_NULL(device->iommu);
> -
> -       INIT_LIST_HEAD(&device->iommu->dma_regions);
> -
> -       device->iommu->mode = lookup_iommu_mode(iommu_mode);
> +       device->iommu = iommu;
nit: Since we now depend on the caller to follow the right order,
should we have a VFIO_ASSERT_NOT_NULL(iommu), or something along the
lines of 'Is iommu initialized?" before this function starts using it,
and fail with an appropriate error message?

>
>         if (device->iommu->mode->container_path)
minor nit: if there's a v4, simply use iommu->mode->container_path.

Thank you.
Raghavendra


>                 vfio_pci_container_setup(device, bdf);
> @@ -853,17 +872,22 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *device)
>                 VFIO_ASSERT_EQ(close(device->msi_eventfds[i]), 0);
>         }
>
> -       if (device->iommu->iommufd) {
> -               VFIO_ASSERT_EQ(close(device->iommu->iommufd), 0);
> -       } else {
> +       if (device->group_fd)
>                 VFIO_ASSERT_EQ(close(device->group_fd), 0);
> -               VFIO_ASSERT_EQ(close(device->iommu->container_fd), 0);
> -       }
>
> -       free(device->iommu);
>         free(device);
>  }
>
> +void iommu_cleanup(struct iommu *iommu)
> +{
> +       if (iommu->iommufd)
> +               VFIO_ASSERT_EQ(close(iommu->iommufd), 0);
> +       else
> +               VFIO_ASSERT_EQ(close(iommu->container_fd), 0);
> +
> +       free(iommu);
> +}
> +
>  static bool is_bdf(const char *str)
>  {
>         unsigned int s, b, d, f;
> diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
> index 102603d4407d..4727feb214c8 100644
> --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
> +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
> @@ -94,6 +94,7 @@ static int iommu_mapping_get(const char *bdf, u64 iova,
>  }
>
>  FIXTURE(vfio_dma_mapping_test) {
> +       struct iommu *iommu;
>         struct vfio_pci_device *device;
>         struct iova_allocator *iova_allocator;
>  };
> @@ -119,7 +120,8 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |
>
>  FIXTURE_SETUP(vfio_dma_mapping_test)
>  {
> -       self->device = vfio_pci_device_init(device_bdf, variant->iommu_mode);
> +       self->iommu = iommu_init(variant->iommu_mode);
> +       self->device = vfio_pci_device_init(device_bdf, self->iommu);
>         self->iova_allocator = iova_allocator_init(self->device);
>  }
>
> @@ -127,6 +129,7 @@ FIXTURE_TEARDOWN(vfio_dma_mapping_test)
>  {
>         iova_allocator_cleanup(self->iova_allocator);
>         vfio_pci_device_cleanup(self->device);
> +       iommu_cleanup(self->iommu);
>  }
>
>  TEST_F(vfio_dma_mapping_test, dma_map_unmap)
> @@ -203,6 +206,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
>  }
>
>  FIXTURE(vfio_dma_map_limit_test) {
> +       struct iommu *iommu;
>         struct vfio_pci_device *device;
>         struct vfio_dma_region region;
>         size_t mmap_size;
> @@ -235,7 +239,8 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
>          */
>         self->mmap_size = 2 * region_size;
>
> -       self->device = vfio_pci_device_init(device_bdf, variant->iommu_mode);
> +       self->iommu = iommu_init(variant->iommu_mode);
> +       self->device = vfio_pci_device_init(device_bdf, self->iommu);
>         region->vaddr = mmap(NULL, self->mmap_size, PROT_READ | PROT_WRITE,
>                              MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
>         ASSERT_NE(region->vaddr, MAP_FAILED);
> @@ -253,6 +258,7 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
>  FIXTURE_TEARDOWN(vfio_dma_map_limit_test)
>  {
>         vfio_pci_device_cleanup(self->device);
> +       iommu_cleanup(self->iommu);
>         ASSERT_EQ(munmap(self->region.vaddr, self->mmap_size), 0);
>  }
>
> diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/testing/selftests/vfio/vfio_pci_device_test.c
> index 7a270698e4d2..e95217933c6b 100644
> --- a/tools/testing/selftests/vfio/vfio_pci_device_test.c
> +++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c
> @@ -23,17 +23,20 @@ static const char *device_bdf;
>  #define MAX_TEST_MSI 16U
>
>  FIXTURE(vfio_pci_device_test) {
> +       struct iommu *iommu;
>         struct vfio_pci_device *device;
>  };
>
>  FIXTURE_SETUP(vfio_pci_device_test)
>  {
> -       self->device = vfio_pci_device_init(device_bdf, default_iommu_mode);
> +       self->iommu = iommu_init(default_iommu_mode);
> +       self->device = vfio_pci_device_init(device_bdf, self->iommu);
>  }
>
>  FIXTURE_TEARDOWN(vfio_pci_device_test)
>  {
>         vfio_pci_device_cleanup(self->device);
> +       iommu_cleanup(self->iommu);
>  }
>
>  #define read_pci_id_from_sysfs(_file) ({                                                       \
> @@ -99,6 +102,7 @@ TEST_F(vfio_pci_device_test, validate_bars)
>  }
>
>  FIXTURE(vfio_pci_irq_test) {
> +       struct iommu *iommu;
>         struct vfio_pci_device *device;
>  };
>
> @@ -116,12 +120,14 @@ FIXTURE_VARIANT_ADD(vfio_pci_irq_test, msix) {
>
>  FIXTURE_SETUP(vfio_pci_irq_test)
>  {
> -       self->device = vfio_pci_device_init(device_bdf, default_iommu_mode);
> +       self->iommu = iommu_init(default_iommu_mode);
> +       self->device = vfio_pci_device_init(device_bdf, self->iommu);
>  }
>
>  FIXTURE_TEARDOWN(vfio_pci_irq_test)
>  {
>         vfio_pci_device_cleanup(self->device);
> +       iommu_cleanup(self->iommu);
>  }
>
>  TEST_F(vfio_pci_irq_test, enable_trigger_disable)
> diff --git a/tools/testing/selftests/vfio/vfio_pci_driver_test.c b/tools/testing/selftests/vfio/vfio_pci_driver_test.c
> index f69eec8b928d..b0c7d812de1f 100644
> --- a/tools/testing/selftests/vfio/vfio_pci_driver_test.c
> +++ b/tools/testing/selftests/vfio/vfio_pci_driver_test.c
> @@ -44,6 +44,7 @@ static void region_teardown(struct vfio_pci_device *device,
>  }
>
>  FIXTURE(vfio_pci_driver_test) {
> +       struct iommu *iommu;
>         struct vfio_pci_device *device;
>         struct iova_allocator *iova_allocator;
>         struct vfio_dma_region memcpy_region;
> @@ -73,7 +74,8 @@ FIXTURE_SETUP(vfio_pci_driver_test)
>  {
>         struct vfio_pci_driver *driver;
>
> -       self->device = vfio_pci_device_init(device_bdf, variant->iommu_mode);
> +       self->iommu = iommu_init(variant->iommu_mode);
> +       self->device = vfio_pci_device_init(device_bdf, self->iommu);
>         self->iova_allocator = iova_allocator_init(self->device);
>
>         driver = &self->device->driver;
> @@ -113,6 +115,7 @@ FIXTURE_TEARDOWN(vfio_pci_driver_test)
>
>         iova_allocator_cleanup(self->iova_allocator);
>         vfio_pci_device_cleanup(self->device);
> +       iommu_cleanup(self->iommu);
>  }
>
>  TEST_F(vfio_pci_driver_test, init_remove)
> @@ -231,18 +234,31 @@ TEST_F_TIMEOUT(vfio_pci_driver_test, memcpy_storm, 60)
>         ASSERT_NO_MSI(self->msi_fd);
>  }
>
> -int main(int argc, char *argv[])
> +static bool device_has_selftests_driver(const char *bdf)
>  {
>         struct vfio_pci_device *device;
> +       struct iommu *iommu;
> +       bool has_driver;
> +
> +       iommu = iommu_init(default_iommu_mode);
> +       device = vfio_pci_device_init(device_bdf, iommu);
> +
> +       has_driver = !!device->driver.ops;
> +
> +       vfio_pci_device_cleanup(device);
> +       iommu_cleanup(iommu);
>
> +       return has_driver;
> +}
> +
> +int main(int argc, char *argv[])
> +{
>         device_bdf = vfio_selftests_get_bdf(&argc, argv);
>
> -       device = vfio_pci_device_init(device_bdf, default_iommu_mode);
> -       if (!device->driver.ops) {
> +       if (!device_has_selftests_driver(device_bdf)) {
>                 fprintf(stderr, "No driver found for device %s\n", device_bdf);
>                 return KSFT_SKIP;
>         }
> -       vfio_pci_device_cleanup(device);
>
>         return test_harness_run(argc, argv);
>  }
> --
> 2.52.0.rc2.455.g230fcf2819-goog
>

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ