[PATCH v2 5/8] pci/iommu: Check that IOMMU supports removing devices

Mykyta Poturai posted 8 patches 1 month ago
There is a newer version of this series
[PATCH v2 5/8] pci/iommu: Check that IOMMU supports removing devices
Posted by Mykyta Poturai 1 month ago
before trying to remove them.
Some IOMMU platforms, such as SMMU-V3 and IPMMU-VMSA don't support
removing devices. Trying to call remove_device will result in a crash.
So check that platform_ops have remove_device set before calling it.

Signed-off-by: Mykyta Poturai <mykyta_poturai@epam.com>
---
v1->v2:
* new patch
---
 xen/drivers/passthrough/pci.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c
index 464bb0fee4..704eb6e19f 100644
--- a/xen/drivers/passthrough/pci.c
+++ b/xen/drivers/passthrough/pci.c
@@ -1545,6 +1545,12 @@ static int iommu_remove_device(struct pci_dev *pdev)
     if ( !is_iommu_enabled(pdev->domain) )
         return 0;
 
+    if ( !hd->platform_ops->remove_device )
+    {
+        printk(XENLOG_ERR "IOMMU: remove_device not supported by platform\n");
+        return -EOPNOTSUPP;
+    }
+
     for ( devfn = pdev->devfn ; pdev->phantom_stride; )
     {
         int rc;
-- 
2.51.2
Re: [PATCH v2 5/8] pci/iommu: Check that IOMMU supports removing devices
Posted by Jan Beulich 1 week, 2 days ago
On 09.03.2026 12:08, Mykyta Poturai wrote:
> before trying to remove them.
> Some IOMMU platforms, such as SMMU-V3 and IPMMU-VMSA don't support
> removing devices. Trying to call remove_device will result in a crash.
> So check that platform_ops have remove_device set before calling it.

Hmm, but both have .add_device populated. They ought to support
.remove_device, especially if ...

> --- a/xen/drivers/passthrough/pci.c
> +++ b/xen/drivers/passthrough/pci.c
> @@ -1545,6 +1545,12 @@ static int iommu_remove_device(struct pci_dev *pdev)
>      if ( !is_iommu_enabled(pdev->domain) )
>          return 0;
>  
> +    if ( !hd->platform_ops->remove_device )
> +    {
> +        printk(XENLOG_ERR "IOMMU: remove_device not supported by platform\n");
> +        return -EOPNOTSUPP;
> +    }
> +
>      for ( devfn = pdev->devfn ; pdev->phantom_stride; )
>      {
>          int rc;

... this is for PCI. (I'm simply not qualified to discuss DT.) This
being in the SR-IOV series, I have to assume for the change to be about
a PF de-configuring its VFs. This imo shouldn't end in -EOPNOTSUPP. In
fact PHYSDEVOP_pci_device_remove serves largely as a notification -
please check the xen_remove_device() use in Linux. That is, the device
is going to be gone anyway, and hence we'd better take care of that fact
in Xen.

Jan