Re: [RFC v1 5/5] iommu/hyperv: Add para-virtualized IOMMU support for Hyper-V guest

Yu Zhang
2025-12-11T08:41:25Z

On Wed, Dec 10, 2025 at 09:15:18AM -0800, Easwar Hariharan wrote:
> On 12/8/2025 9:11 PM, Yu Zhang wrote:
> > Add a para-virtualized IOMMU driver for Linux guests running on Hyper-V.
> > This driver implements stage-1 IO translation within the guest OS.
> > It integrates with the Linux IOMMU core, utilizing Hyper-V hypercalls
> > for:
> > - Capability discovery
> > - Domain allocation, configuration, and deallocation
> > - Device attachment and detachment
> > - IOTLB invalidation
> >
> > The driver constructs x86-compatible stage-1 IO page tables in the
> > guest memory using consolidated IO page table helpers. This allows
> > the guest to manage stage-1 translations independently of vendor-
> > specific drivers (like Intel VT-d or AMD IOMMU).
> >
> > Hyper-v consumes this stage-1 IO page table, when a device domain is
> > created and configured, and nests it with the host's stage-2 IO page
> > tables, therefore elemenating the VM exits for guest IOMMU mapping
> > operations.
> >
> > For guest IOMMU unmapping operations, VM exits to perform the IOTLB
> > flush(and possibly the device TLB flush) is still unavoidable. For
> > now, HVCALL_FLUSH_DEVICE_DOMAIN is used to implement a domain-selective
> > IOTLB flush. New hypercalls for finer-grained hypercall will be provided
> > in future patches.
> >
> > Co-developed-by: Wei Liu <wei.liu@kernel.org>
> > Signed-off-by: Wei Liu <wei.liu@kernel.org>
> > Co-developed-by: Jacob Pan <jacob.pan@linux.microsoft.com>
> > Signed-off-by: Jacob Pan <jacob.pan@linux.microsoft.com>
> > Co-developed-by: Easwar Hariharan <easwar.hariharan@linux.microsoft.com>
> > Signed-off-by: Easwar Hariharan <easwar.hariharan@linux.microsoft.com>
> > Signed-off-by: Yu Zhang <zhangyu1@linux.microsoft.com>
> > ---
> > drivers/iommu/hyperv/Kconfig | 14 +
> > drivers/iommu/hyperv/Makefile | 1 +
> > drivers/iommu/hyperv/iommu.c | 608 ++++++++++++++++++++++++++++++++++
> > drivers/iommu/hyperv/iommu.h | 53 +++
> > 4 files changed, 676 insertions(+)
> > create mode 100644 drivers/iommu/hyperv/iommu.c
> > create mode 100644 drivers/iommu/hyperv/iommu.h
> >
>
> <snip>
>
> > +
> > +static int __init hv_iommu_init(void)
> > +{
> > + int ret = 0;
> > + struct hv_iommu_dev *hv_iommu = NULL;
> > + struct hv_output_get_iommu_capabilities hv_iommu_cap = {0};
> > +
> > + if (no_iommu || iommu_detected)
> > + return -ENODEV;
> > +
> > + if (!hv_is_hyperv_initialized())
> > + return -ENODEV;
> > +
> > + if (hv_iommu_detect(&hv_iommu_cap) ||
> > + !hv_iommu_present(hv_iommu_cap.iommu_cap) ||
> > + !hv_iommu_s1_domain_supported(hv_iommu_cap.iommu_cap))
> > + return -ENODEV;
> > +
> > + iommu_detected = 1;
> > + pci_request_acs();
> > +
> > + hv_iommu = kzalloc(sizeof(*hv_iommu), GFP_KERNEL);
> > + if (!hv_iommu)
> > + return -ENOMEM;
> > +
> > + hv_init_iommu_device(hv_iommu, &hv_iommu_cap);
> > +
> > + ret = hv_initialize_static_domains();
> > + if (ret) {
> > + pr_err("hv_initialize_static_domains failed: %d\n", ret);
> > + goto err_sysfs_remove;
>
> This should be goto err_free since we haven't done the sysfs_add yet
>
> > + }
> > +
> > + ret = iommu_device_sysfs_add(&hv_iommu->iommu, NULL, NULL, "%s", "hv-iommu");
> > + if (ret) {
> > + pr_err("iommu_device_sysfs_add failed: %d\n", ret);
> > + goto err_free;
>
> And this should be probably a goto delete_static_domains that cleans up the allocated static
> domains...
>

Nice catch. And thanks! :)

Yu