vfio: Include No-IOMMU mode
Subsystems (2)
PATCH v4 1/1 vfio: Include No-IOMMU mode
There is really no way to safely give a user full access to a DMA capable device without an IOMMU to protect the host system. There is also no way to provide DMA translation, for use cases such as device assignment to virtual machines. However, there are still those users that want userspace drivers even under those conditions. The UIO driver exists for this use case, but does not provide the degree of device access and programming that VFIO has. In an effort to avoid code duplication, this introduces a No-IOMMU mode for VFIO.
This mode requires building VFIO with CONFIG_VFIO_NOIOMMU and enabling the "enable_unsafe_noiommu_mode" option on the vfio driver. This should make it very clear that this mode is not safe. Additionally, CAP_SYS_RAWIO privileges are necessary to work with groups and containers using this mode. Groups making use of this support are named /dev/vfio/noiommu-$GROUP and can only make use of the special VFIO_NOIOMMU_IOMMU for the container. Use of this mode, specifically binding a device without a native IOMMU group to a VFIO bus driver will taint the kernel and should therefore not be considered supported. This patch includes no-iommu support for the vfio-pci bus driver only.
---
v4: Fix build without CONFIG_VFIO_NOIOMMU (oops). Also avoid local
noiommu variable in vfio_create_group() to avoid scope confusion with global of the same name.
drivers/vfio/Kconfig | 15 ++++ drivers/vfio/pci/vfio_pci.c | 8 +- drivers/vfio/vfio.c | 184 ++++++++++++++++++++++++++++++++++++++++++- include/linux/vfio.h | 3 + include/uapi/linux/vfio.h | 7 ++ 5 files changed, 210 insertions(+), 7 deletions(-)
drivers/vfio/Kconfig +15 -0
@@ -31,6 +31,21 @@ menuconfig VFIO If you don't know what to do here, say N. +menuconfig VFIO_NOIOMMU + bool "VFIO No-IOMMU support" + depends on VFIO + help + VFIO is built on the ability to isolate devices using the IOMMU. + Only with an IOMMU can userspace access to DMA capable devices be + considered secure. VFIO No-IOMMU mode enables IOMMU groups for + devices without IOMMU backing for the purpose of re-using the VFIO + infrastructure in a non-secure mode. Use of this mode will result + in an unsupportable kernel and will therefore taint the kernel. + Device assignment to virtual machines is also not possible with + this mode since there is no IOMMU to provide DMA translation. + + If you don't know what to do here, say N. + source "drivers/vfio/pci/Kconfig" source "drivers/vfio/platform/Kconfig" source "virt/lib/Kconfig"
drivers/vfio/pci/vfio_pci.c +4 -4
@@ -940,13 +940,13 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (pdev->hdr_type !=PCI_HEADER_TYPE_NORMAL )return -EINVAL ; - group =iommu_group_get (&pdev->dev); + group =vfio_iommu_group_get (&pdev->dev);if (!group)return -EINVAL ; vdev =kzalloc (sizeof (*vdev),GFP_KERNEL );if (!vdev) { -iommu_group_put (group); +vfio_iommu_group_put (group, &pdev->dev);return -ENOMEM ; }
@@ -957,7 +957,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) ret =vfio_add_group_dev (&pdev->dev, &vfio_pci_ops, vdev);if (ret) { -iommu_group_put (group); +vfio_iommu_group_put (group, &pdev->dev);kfree (vdev);return ret; }
@@ -993,7 +993,7 @@ static void vfio_pci_remove(struct pci_dev *pdev)if (!vdev)return ; -iommu_group_put (pdev->dev.iommu_group); +vfio_iommu_group_put (pdev->dev.iommu_group, &pdev->dev);kfree (vdev);if (vfio_pci_is_vga (pdev)) {
drivers/vfio/vfio.c +181 -3
292 lines not on this page · raw message
include/linux/vfio.h +3 -0
@@ -44,6 +44,9 @@ struct vfio_device_ops {void (*request)(void *device_data,unsigned int count); }; +extern struct iommu_group *vfio_iommu_group_get (struct device *dev); +extern void vfio_iommu_group_put (struct iommu_group *group,struct device *dev); +extern int vfio_add_group_dev (struct device *dev,const struct vfio_device_ops *ops,void *device_data);
include/uapi/linux/vfio.h +7 -0
@@ -39,6 +39,13 @@#define VFIO_SPAPR_TCE_v2_IOMMU 7/* +* The No-IOMMU IOMMU offers no translation or isolation for devices and +* supports no ioctls outside of VFIO_CHECK_EXTENSION. Use of VFIO's No-IOMMU +* code will taint the host kernel and should be used with extreme caution. +*/ +#define VFIO_NOIOMMU_IOMMU 8 + +/* * The IOCTL interface is designed for extensibility by embedding the * structure length (argsz) and flags into structures passed between * kernel and userspace. We therefore use the _IO() macro for these