From: David Matlack <dmatlack@google.com>
To: Samiullah Khawaja <skhawaja@google.com>
Cc: David Woodhouse <dwmw2@infradead.org>,
Lu Baolu <baolu.lu@linux.intel.com>,
Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
Pasha Tatashin <pasha.tatashin@soleen.com>,
Jason Gunthorpe <jgg@ziepe.ca>,
Robin Murphy <robin.murphy@arm.com>,
Pratyush Yadav <pratyush@kernel.org>,
Kevin Tian <kevin.tian@intel.com>,
Alex Williamson <alex@shazbot.org>, Shuah Khan <shuah@kernel.org>,
iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
kvm@vger.kernel.org, Saeed Mahameed <saeedm@nvidia.com>,
Adithya Jayachandran <ajayachandra@nvidia.com>,
Parav Pandit <parav@nvidia.com>,
Leon Romanovsky <leonro@nvidia.com>, William Tu <witu@nvidia.com>
Subject: Re: [PATCH 2/3] vfio: selftests: Add support of creating iommus from iommufd
Date: Thu, 8 Jan 2026 00:29:09 +0000 [thread overview]
Message-ID: <aV76VWKNxMw2t2kH@google.com> (raw)
In-Reply-To: <20260107201800.2486137-3-skhawaja@google.com>
On 2026-01-07 08:17 PM, Samiullah Khawaja wrote:
> Add API to init a struct iommu using an already opened iommufd instance
> and attach devices to it.
>
> Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
> ---
> .../vfio/lib/include/libvfio/iommu.h | 2 +
> .../lib/include/libvfio/vfio_pci_device.h | 2 +
> tools/testing/selftests/vfio/lib/iommu.c | 60 +++++++++++++++++--
> .../selftests/vfio/lib/vfio_pci_device.c | 16 ++++-
> 4 files changed, 74 insertions(+), 6 deletions(-)
>
> diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h b/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
> index 5c9b9dc6d993..9e96da1e6fd3 100644
> --- a/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
> +++ b/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
> @@ -29,10 +29,12 @@ struct iommu {
> int container_fd;
> int iommufd;
> u32 ioas_id;
> + u32 hwpt_id;
> struct list_head dma_regions;
> };
>
> struct iommu *iommu_init(const char *iommu_mode);
> +struct iommu *iommufd_iommu_init(int iommufd, u32 dev_id);
> void iommu_cleanup(struct iommu *iommu);
>
> int __iommu_map(struct iommu *iommu, struct dma_region *region);
> 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 2858885a89bb..1143ceb6a9b8 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
> @@ -19,6 +19,7 @@ struct vfio_pci_device {
> const char *bdf;
> int fd;
> int group_fd;
> + u32 dev_id;
>
> struct iommu *iommu;
>
> @@ -65,6 +66,7 @@ void vfio_pci_config_access(struct vfio_pci_device *device, bool write,
> #define vfio_pci_config_writew(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u16)
> #define vfio_pci_config_writel(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u32)
>
> +void vfio_pci_device_attach_iommu(struct vfio_pci_device *device, struct iommu *iommu);
> void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index,
> u32 vector, int count);
> void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index);
> diff --git a/tools/testing/selftests/vfio/lib/iommu.c b/tools/testing/selftests/vfio/lib/iommu.c
> index 58b7fb7430d4..2c67d7e24d0c 100644
> --- a/tools/testing/selftests/vfio/lib/iommu.c
> +++ b/tools/testing/selftests/vfio/lib/iommu.c
> @@ -408,6 +408,18 @@ struct iommu_iova_range *iommu_iova_ranges(struct iommu *iommu, u32 *nranges)
> return ranges;
> }
>
> +static u32 iommufd_hwpt_alloc(struct iommu *iommu, u32 dev_id)
> +{
> + struct iommu_hwpt_alloc args = {
> + .size = sizeof(args),
> + .pt_id = iommu->ioas_id,
> + .dev_id = dev_id,
> + };
> +
> + ioctl_assert(iommu->iommufd, IOMMU_HWPT_ALLOC, &args);
> + return args.out_hwpt_id;
> +}
> +
> static u32 iommufd_ioas_alloc(int iommufd)
> {
> struct iommu_ioas_alloc args = {
> @@ -418,11 +430,9 @@ static u32 iommufd_ioas_alloc(int iommufd)
> return args.out_ioas_id;
> }
>
> -struct iommu *iommu_init(const char *iommu_mode)
> +static struct iommu *iommu_alloc(const char *iommu_mode)
> {
> - const char *container_path;
> struct iommu *iommu;
> - int version;
>
> iommu = calloc(1, sizeof(*iommu));
> VFIO_ASSERT_NOT_NULL(iommu);
> @@ -430,6 +440,16 @@ struct iommu *iommu_init(const char *iommu_mode)
> INIT_LIST_HEAD(&iommu->dma_regions);
>
> iommu->mode = lookup_iommu_mode(iommu_mode);
> + return iommu;
> +}
> +
> +struct iommu *iommu_init(const char *iommu_mode)
> +{
> + const char *container_path;
> + struct iommu *iommu;
> + int version;
> +
> + iommu = iommu_alloc(iommu_mode);
>
> container_path = iommu->mode->container_path;
> if (container_path) {
> @@ -453,10 +473,42 @@ struct iommu *iommu_init(const char *iommu_mode)
> return iommu;
> }
>
> +struct iommu *iommufd_iommu_init(int iommufd, u32 dev_id)
I don't think the name really captures what this routine is doing. How
about iommufd_dup()?
> +{
> + struct iommu *iommu;
> +
> + iommu = iommu_alloc("iommufd");
> +
> + iommu->iommufd = dup(iommufd);
> + VFIO_ASSERT_GT(iommu->iommufd, 0);
> +
> + iommu->ioas_id = iommufd_ioas_alloc(iommu->iommufd);
> + iommu->hwpt_id = iommufd_hwpt_alloc(iommu, dev_id);
> +
> + return iommu;
> +}
> +
> +static void iommufd_iommu_cleanup(struct iommu *iommu)
nit: iommufd_cleanup()
> +{
> + struct iommu_destroy args = {
> + .size = sizeof(args),
> + };
> +
> + if (iommu->hwpt_id) {
> + args.id = iommu->hwpt_id;
> + ioctl_assert(iommu->iommufd, IOMMU_DESTROY, &args);
> + }
> +
> + args.id = iommu->ioas_id;
> + ioctl_assert(iommu->iommufd, IOMMU_DESTROY, &args);
> +
> + VFIO_ASSERT_EQ(close(iommu->iommufd), 0);
> +}
> +
> void iommu_cleanup(struct iommu *iommu)
> {
> if (iommu->iommufd)
> - VFIO_ASSERT_EQ(close(iommu->iommufd), 0);
> + iommufd_iommu_cleanup(iommu);
> else
> VFIO_ASSERT_EQ(close(iommu->container_fd), 0);
>
> diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> index fac4c0ecadef..9bc1f5ade5c4 100644
> --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
> @@ -298,7 +298,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
> return cdev_path;
> }
>
> -static void vfio_device_bind_iommufd(int device_fd, int iommufd)
> +static int vfio_device_bind_iommufd(int device_fd, int iommufd)
> {
> struct vfio_device_bind_iommufd args = {
> .argsz = sizeof(args),
> @@ -306,6 +306,7 @@ static void vfio_device_bind_iommufd(int device_fd, int iommufd)
> };
>
> ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args);
> + return args.out_devid;
> }
>
> static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id)
> @@ -326,10 +327,21 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b
> VFIO_ASSERT_GE(device->fd, 0);
> free((void *)cdev_path);
>
> - vfio_device_bind_iommufd(device->fd, device->iommu->iommufd);
> + device->dev_id = vfio_device_bind_iommufd(device->fd, device->iommu->iommufd);
> vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id);
> }
>
> +void vfio_pci_device_attach_iommu(struct vfio_pci_device *device, struct iommu *iommu)
> +{
> + u32 pt_id = iommu->ioas_id;
/* Only iommufd supports changing struct iommu attachments */
VFIO_ASSERT_TRUE(iommu->iommufd);
> +
> + if (iommu->hwpt_id)
> + pt_id = iommu->hwpt_id;
> +
> + VFIO_ASSERT_NE(pt_id, 0);
> + vfio_device_attach_iommufd_pt(device->fd, pt_id);
device->iommu = iommu;
> +}
> +
> struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iommu)
> {
> struct vfio_pci_device *device;
> --
> 2.52.0.351.gbe84eed79e-goog
>
next prev parent reply other threads:[~2026-01-08 0:29 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-07 20:17 [PATCH 0/3] iommu/vt-d: Add support to hitless replace IOMMU domain Samiullah Khawaja
2026-01-07 20:17 ` [PATCH 1/3] iommu/vt-d: Allow replacing no_pasid iommu_domain Samiullah Khawaja
2026-01-07 20:17 ` [PATCH 2/3] vfio: selftests: Add support of creating iommus from iommufd Samiullah Khawaja
2026-01-08 0:29 ` David Matlack [this message]
2026-01-09 21:39 ` Samiullah Khawaja
2026-01-07 20:18 ` [PATCH 3/3] vfio: selftests: Add iommufd hwpt replace test Samiullah Khawaja
2026-01-09 0:42 ` kernel test robot
2026-01-07 20:28 ` [PATCH 0/3] iommu/vt-d: Add support to hitless replace IOMMU domain Jason Gunthorpe
2026-01-07 20:46 ` Jason Gunthorpe
2026-01-09 20:06 ` Samiullah Khawaja
2026-01-11 22:14 ` Jason Gunthorpe
2026-01-12 6:57 ` Baolu Lu
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=aV76VWKNxMw2t2kH@google.com \
--to=dmatlack@google.com \
--cc=ajayachandra@nvidia.com \
--cc=alex@shazbot.org \
--cc=baolu.lu@linux.intel.com \
--cc=dwmw2@infradead.org \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=leonro@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=parav@nvidia.com \
--cc=pasha.tatashin@soleen.com \
--cc=pratyush@kernel.org \
--cc=robin.murphy@arm.com \
--cc=saeedm@nvidia.com \
--cc=shuah@kernel.org \
--cc=skhawaja@google.com \
--cc=will@kernel.org \
--cc=witu@nvidia.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