]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
vfio: Set DMA ownership for VFIO devices
authorLu Baolu <baolu.lu@linux.intel.com>
Mon, 18 Apr 2022 00:49:56 +0000 (08:49 +0800)
committerJoerg Roedel <jroedel@suse.de>
Thu, 28 Apr 2022 13:32:20 +0000 (15:32 +0200)
Claim group dma ownership when an IOMMU group is set to a container,
and release the dma ownership once the iommu group is unset from the
container.

This change disallows some unsafe bridge drivers to bind to non-ACS
bridges while devices under them are assigned to user space. This is an
intentional enhancement and possibly breaks some existing
configurations. The recommendation to such an affected user would be
that the previously allowed host bridge driver was unsafe for this use
case and to continue to enable assignment of devices within that group,
the driver should be unbound from the bridge device or replaced with the
pci-stub driver.

For any bridge driver, we consider it unsafe if it satisfies any of the
following conditions:

  1) The bridge driver uses DMA. Calling pci_set_master() or calling any
     kernel DMA API (dma_map_*() and etc.) is an indicate that the
     driver is doing DMA.

  2) If the bridge driver uses MMIO, it should be tolerant to hostile
     userspace also touching the same MMIO registers via P2P DMA
     attacks.

If the bridge driver turns out to be a safe one, it could be used as
before by setting the driver's .driver_managed_dma field, just like what
we have done in the pcieport driver.

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Acked-by: Alex Williamson <alex.williamson@redhat.com>
Link: https://lore.kernel.org/r/20220418005000.897664-8-baolu.lu@linux.intel.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/vfio/fsl-mc/vfio_fsl_mc.c
drivers/vfio/pci/vfio_pci.c
drivers/vfio/platform/vfio_amba.c
drivers/vfio/platform/vfio_platform.c
drivers/vfio/vfio.c

index 6e2e62c6f47aebff90a0e853a62d1d35ac6d2a6a..3feff729f3ce825cf759421241bdabe08e6d29a8 100644 (file)
@@ -588,6 +588,7 @@ static struct fsl_mc_driver vfio_fsl_mc_driver = {
                .name   = "vfio-fsl-mc",
                .owner  = THIS_MODULE,
        },
+       .driver_managed_dma = true,
 };
 
 static int __init vfio_fsl_mc_driver_init(void)
index 2b047469e02feea48b0bbea808fd119d1eeefcfe..58839206d1ca7f229c7d90f12a149e8f2c3a24eb 100644 (file)
@@ -194,6 +194,7 @@ static struct pci_driver vfio_pci_driver = {
        .remove                 = vfio_pci_remove,
        .sriov_configure        = vfio_pci_sriov_configure,
        .err_handler            = &vfio_pci_core_err_handlers,
+       .driver_managed_dma     = true,
 };
 
 static void __init vfio_pci_fill_ids(void)
index badfffea14fbdc2018dbb6c4490fcff387756f13..1aaa4f721bd2ce13f0332aa5c72934997182a4fe 100644 (file)
@@ -95,6 +95,7 @@ static struct amba_driver vfio_amba_driver = {
                .name = "vfio-amba",
                .owner = THIS_MODULE,
        },
+       .driver_managed_dma = true,
 };
 
 module_amba_driver(vfio_amba_driver);
index 68a1c87066d7604e5cbf8cce44dbde228ecb7b5d..04f40c5acfd673d8f087a0c10505ab837c9ffc0b 100644 (file)
@@ -76,6 +76,7 @@ static struct platform_driver vfio_platform_driver = {
        .driver = {
                .name   = "vfio-platform",
        },
+       .driver_managed_dma = true,
 };
 
 module_platform_driver(vfio_platform_driver);
index a4555014bd1e72b2ee5852cf4987901eda481fb6..56e741cbccce1297179f6c4f3aa59a05bf3f318a 100644 (file)
@@ -1198,6 +1198,8 @@ static void __vfio_group_unset_container(struct vfio_group *group)
                driver->ops->detach_group(container->iommu_data,
                                          group->iommu_group);
 
+       iommu_group_release_dma_owner(group->iommu_group);
+
        group->container = NULL;
        wake_up(&group->container_q);
        list_del(&group->container_next);
@@ -1282,13 +1284,19 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd)
                goto unlock_out;
        }
 
+       ret = iommu_group_claim_dma_owner(group->iommu_group, f.file);
+       if (ret)
+               goto unlock_out;
+
        driver = container->iommu_driver;
        if (driver) {
                ret = driver->ops->attach_group(container->iommu_data,
                                                group->iommu_group,
                                                group->type);
-               if (ret)
+               if (ret) {
+                       iommu_group_release_dma_owner(group->iommu_group);
                        goto unlock_out;
+               }
        }
 
        group->container = container;