[PATCH 2/5] vfio: Require that devices support DMA cache coherence

Jason Gunthorpe
2022-04-05T23:26:10Z

dev_is_dma_coherent() is the control to determine if IOMMU_CACHE can be
supported.

IOMMU_CACHE means that normal DMAs do not require any additional coherency
mechanism and is the basic uAPI that VFIO exposes to userspace. For
instance VFIO applications like DPDK will not work if additional coherency
operations are required.

Therefore check dev_is_dma_coherent() before allowing a device to join a
domain. This will block device/platform/iommu combinations from using VFIO
that do not support cache coherent DMA.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/vfio/vfio.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index a4555014bd1e72..2a3aa3e742d943 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -32,6 +32,7 @@
#include <linux/vfio.h>
#include <linux/wait.h>
#include <linux/sched/signal.h>
+#include <linux/dma-map-ops.h>
#include "vfio.h"

#define DRIVER_VERSION "0.3"
@@ -1348,6 +1349,11 @@ static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
if (IS_ERR(device))
return PTR_ERR(device);

+ if (group->type == VFIO_IOMMU && !dev_is_dma_coherent(device->dev)) {
+ ret = -ENODEV;
+ goto err_device_put;
+ }
+
if (!try_module_get(device->dev->driver->owner)) {
ret = -ENODEV;
goto err_device_put;
--
2.35.1