Skip to content

Commit 70693f4

Browse files
LuBaolujoergroedel
authored andcommitted
vfio: Set DMA ownership for VFIO devices
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 <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Acked-by: Alex Williamson <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent c7d4698 commit 70693f4

File tree

5 files changed

+13
-1
lines changed

5 files changed

+13
-1
lines changed

drivers/vfio/fsl-mc/vfio_fsl_mc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ static struct fsl_mc_driver vfio_fsl_mc_driver = {
588588
.name = "vfio-fsl-mc",
589589
.owner = THIS_MODULE,
590590
},
591+
.driver_managed_dma = true,
591592
};
592593

593594
static int __init vfio_fsl_mc_driver_init(void)

drivers/vfio/pci/vfio_pci.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ static struct pci_driver vfio_pci_driver = {
194194
.remove = vfio_pci_remove,
195195
.sriov_configure = vfio_pci_sriov_configure,
196196
.err_handler = &vfio_pci_core_err_handlers,
197+
.driver_managed_dma = true,
197198
};
198199

199200
static void __init vfio_pci_fill_ids(void)

drivers/vfio/platform/vfio_amba.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static struct amba_driver vfio_amba_driver = {
9595
.name = "vfio-amba",
9696
.owner = THIS_MODULE,
9797
},
98+
.driver_managed_dma = true,
9899
};
99100

100101
module_amba_driver(vfio_amba_driver);

drivers/vfio/platform/vfio_platform.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static struct platform_driver vfio_platform_driver = {
7676
.driver = {
7777
.name = "vfio-platform",
7878
},
79+
.driver_managed_dma = true,
7980
};
8081

8182
module_platform_driver(vfio_platform_driver);

drivers/vfio/vfio.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,8 @@ static void __vfio_group_unset_container(struct vfio_group *group)
11981198
driver->ops->detach_group(container->iommu_data,
11991199
group->iommu_group);
12001200

1201+
iommu_group_release_dma_owner(group->iommu_group);
1202+
12011203
group->container = NULL;
12021204
wake_up(&group->container_q);
12031205
list_del(&group->container_next);
@@ -1282,13 +1284,19 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd)
12821284
goto unlock_out;
12831285
}
12841286

1287+
ret = iommu_group_claim_dma_owner(group->iommu_group, f.file);
1288+
if (ret)
1289+
goto unlock_out;
1290+
12851291
driver = container->iommu_driver;
12861292
if (driver) {
12871293
ret = driver->ops->attach_group(container->iommu_data,
12881294
group->iommu_group,
12891295
group->type);
1290-
if (ret)
1296+
if (ret) {
1297+
iommu_group_release_dma_owner(group->iommu_group);
12911298
goto unlock_out;
1299+
}
12921300
}
12931301

12941302
group->container = container;

0 commit comments

Comments
 (0)